1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
|
/*
* GStreamer
* Copyright (C) 2003 Julien Moutte <julien@moutte.net>
* Copyright (C) 2005,2006,2007 David A. Schleef <ds@schleef.org>
* Copyright (C) 2008 Julien Isorce <julien.isorce@gmail.com>
* Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/**
* SECTION:element-glimagesink
*
* glimagesink renders video frames to a drawable on a local or remote
* display using OpenGL. This element can receive a Window ID from the
* application through the VideoOverlay interface and will then render video
* frames in this drawable.
* If no Window ID was provided by the application, the element will
* create its own internal window and render into it.
*
* See the #GstGLDisplay documentation for a list of environment variables that
* can override window/platform detection.
*
* <refsect2>
* <title>Scaling</title>
* <para>
* Depends on the driver, OpenGL handles hardware accelerated
* scaling of video frames. This means that the element will just accept
* incoming video frames no matter their geometry and will then put them to the
* drawable scaling them on the fly. Using the #GstGLImageSink:force-aspect-ratio
* property it is possible to enforce scaling with a constant aspect ratio,
* which means drawing black borders around the video frame.
* </para>
* </refsect2>
* <refsect2>
* <title>Events</title>
* <para>
* Through the gl thread, glimagesink handle some events coming from the drawable
* to manage its appearance even when the data is not flowing (GST_STATE_PAUSED).
* That means that even when the element is paused, it will receive expose events
* from the drawable and draw the latest frame with correct borders/aspect-ratio.
* </para>
* </refsect2>
* <refsect2>
* <title>Examples</title>
* |[
* gst-launch-1.0 -v videotestsrc ! video/x-raw ! glimagesink
* ]| A pipeline to test hardware scaling.
* No special opengl extension is used in this pipeline, that's why it should work
* with OpenGL >= 1.1. That's the case if you are using the MESA3D driver v1.3.
* |[
* gst-launch-1.0 -v videotestsrc ! video/x-raw,format=I420 ! glimagesink
* ]| A pipeline to test hardware scaling and hardware colorspace conversion.
* When your driver supports GLSL (OpenGL Shading Language needs OpenGL >= 2.1),
* the 4 following format YUY2, UYVY, I420, YV12 and AYUV are converted to RGB32
* through some fragment shaders and using one framebuffer (FBO extension OpenGL >= 1.4).
* If your driver does not support GLSL but supports MESA_YCbCr extension then
* the you can use YUY2 and UYVY. In this case the colorspace conversion is automatically
* made when loading the texture and therefore no framebuffer is used.
* |[
* gst-launch-1.0 -v gltestsrc ! glimagesink
* ]| A pipeline 100% OpenGL.
* No special opengl extension is used in this pipeline, that's why it should work
* with OpenGL >= 1.1. That's the case if you are using the MESA3D driver v1.3.
* |[
* gst-plugins-bas/tests/examples/gl/generic/cube
* ]| The graphic FPS scene can be greater than the input video FPS.
* The graphic scene can be written from a client code through the
* two glfilterapp properties.
* </refsect2>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gst/video/videooverlay.h>
#include <gst/video/navigation.h>
#include "gstglimagesink.h"
#include "gstglsinkbin.h"
#if GST_GL_HAVE_PLATFORM_EGL
#include <gst/gl/egl/gsteglimagememory.h>
#endif
#include <gst/gl/gstglviewconvert.h>
GST_DEBUG_CATEGORY (gst_debug_glimage_sink);
#define GST_CAT_DEFAULT gst_debug_glimage_sink
#define DEFAULT_SHOW_PREROLL_FRAME TRUE
#define DEFAULT_HANDLE_EVENTS TRUE
#define DEFAULT_FORCE_ASPECT_RATIO TRUE
#define DEFAULT_IGNORE_ALPHA TRUE
#define DEFAULT_MULTIVIEW_MODE GST_VIDEO_MULTIVIEW_MODE_MONO
#define DEFAULT_MULTIVIEW_FLAGS GST_VIDEO_MULTIVIEW_FLAGS_NONE
#define DEFAULT_MULTIVIEW_DOWNMIX GST_GL_STEREO_DOWNMIX_ANAGLYPH_GREEN_MAGENTA_DUBOIS
typedef GstGLSinkBin GstGLImageSinkBin;
typedef GstGLSinkBinClass GstGLImageSinkBinClass;
G_DEFINE_TYPE (GstGLImageSinkBin, gst_gl_image_sink_bin, GST_TYPE_GL_SINK_BIN);
enum
{
PROP_BIN_0,
PROP_BIN_FORCE_ASPECT_RATIO,
PROP_BIN_PIXEL_ASPECT_RATIO,
PROP_BIN_HANDLE_EVENTS,
PROP_BIN_CONTEXT,
PROP_BIN_IGNORE_ALPHA,
PROP_BIN_SHOW_PREROLL_FRAME,
PROP_BIN_OUTPUT_MULTIVIEW_LAYOUT,
PROP_BIN_OUTPUT_MULTIVIEW_FLAGS,
PROP_BIN_OUTPUT_MULTIVIEW_DOWNMIX_MODE
};
enum
{
SIGNAL_BIN_0,
SIGNAL_BIN_CLIENT_DRAW,
SIGNAL_BIN_CLIENT_RESHAPE,
SIGNAL_BIN_LAST,
};
static guint gst_gl_image_sink_bin_signals[SIGNAL_BIN_LAST] = { 0 };
static void
gst_gl_image_sink_bin_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * param_spec)
{
g_object_set_property (G_OBJECT (GST_GL_SINK_BIN (object)->sink),
param_spec->name, value);
}
static void
gst_gl_image_sink_bin_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * param_spec)
{
g_object_get_property (G_OBJECT (GST_GL_SINK_BIN (object)->sink),
param_spec->name, value);
}
static gboolean
_on_client_reshape (GstGLImageSink * sink, GstGLContext * context,
guint width, guint height, gpointer data)
{
gboolean ret;
g_signal_emit (data, gst_gl_image_sink_bin_signals[SIGNAL_BIN_CLIENT_RESHAPE],
0, context, width, height, &ret);
return ret;
}
static gboolean
_on_client_draw (GstGLImageSink * sink, GstGLContext * context,
GstSample * sample, gpointer data)
{
gboolean ret;
g_signal_emit (data, gst_gl_image_sink_bin_signals[SIGNAL_BIN_CLIENT_DRAW], 0,
context, sample, &ret);
return ret;
}
static void
gst_gl_image_sink_bin_init (GstGLImageSinkBin * self)
{
GstGLImageSink *sink = g_object_new (GST_TYPE_GLIMAGE_SINK, NULL);
g_signal_connect (sink, "client-reshape", (GCallback) _on_client_reshape,
self);
g_signal_connect (sink, "client-draw", (GCallback) _on_client_draw, self);
gst_gl_sink_bin_finish_init_with_element (GST_GL_SINK_BIN (self),
GST_ELEMENT (sink));
}
static void
gst_gl_image_sink_bin_class_init (GstGLImageSinkBinClass * klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->get_property = gst_gl_image_sink_bin_get_property;
gobject_class->set_property = gst_gl_image_sink_bin_set_property;
/* gl sink */
g_object_class_install_property (gobject_class, PROP_BIN_FORCE_ASPECT_RATIO,
g_param_spec_boolean ("force-aspect-ratio",
"Force aspect ratio",
"When enabled, scaling will respect original aspect ratio",
DEFAULT_FORCE_ASPECT_RATIO,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_BIN_HANDLE_EVENTS,
g_param_spec_boolean ("handle-events", "Handle XEvents",
"When enabled, XEvents will be selected and handled",
DEFAULT_HANDLE_EVENTS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_BIN_IGNORE_ALPHA,
g_param_spec_boolean ("ignore-alpha", "Ignore Alpha",
"When enabled, alpha will be ignored and converted to black",
DEFAULT_IGNORE_ALPHA, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_BIN_CONTEXT,
g_param_spec_object ("context", "OpenGL context", "Get OpenGL context",
GST_GL_TYPE_CONTEXT, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_BIN_PIXEL_ASPECT_RATIO,
gst_param_spec_fraction ("pixel-aspect-ratio", "Pixel Aspect Ratio",
"The pixel aspect ratio of the device", 0, 1, G_MAXINT, 1, 1, 1,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
/* video sink */
g_object_class_install_property (gobject_class, PROP_BIN_SHOW_PREROLL_FRAME,
g_param_spec_boolean ("show-preroll-frame", "Show preroll frame",
"Whether to render video frames during preroll",
DEFAULT_SHOW_PREROLL_FRAME,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_BIN_OUTPUT_MULTIVIEW_LAYOUT,
g_param_spec_enum ("output-multiview-mode", "Output Multiview Mode",
"Choose output mode for multiview/3D video",
GST_TYPE_VIDEO_MULTIVIEW_MODE, DEFAULT_MULTIVIEW_MODE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_BIN_OUTPUT_MULTIVIEW_FLAGS,
g_param_spec_flags ("output-multiview-flags", "Output Multiview Flags",
"Output multiview layout modifier flags",
GST_TYPE_VIDEO_MULTIVIEW_FLAGS, DEFAULT_MULTIVIEW_FLAGS,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_BIN_OUTPUT_MULTIVIEW_DOWNMIX_MODE,
g_param_spec_enum ("output-multiview-downmix-mode",
"Mode for mono downmixed output",
"Output anaglyph type to generate when downmixing to mono",
GST_TYPE_GL_STEREO_DOWNMIX_MODE_TYPE, DEFAULT_MULTIVIEW_DOWNMIX,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
gst_gl_image_sink_bin_signals[SIGNAL_BIN_CLIENT_DRAW] =
g_signal_new ("client-draw", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2,
GST_GL_TYPE_CONTEXT, GST_TYPE_SAMPLE);
gst_gl_image_sink_bin_signals[SIGNAL_BIN_CLIENT_RESHAPE] =
g_signal_new ("client-reshape", G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
G_TYPE_BOOLEAN, 3, GST_GL_TYPE_CONTEXT, G_TYPE_UINT, G_TYPE_UINT);
}
#define GST_GLIMAGE_SINK_GET_LOCK(glsink) \
(GST_GLIMAGE_SINK(glsink)->drawing_lock)
#define GST_GLIMAGE_SINK_LOCK(glsink) \
(g_mutex_lock(&GST_GLIMAGE_SINK_GET_LOCK (glsink)))
#define GST_GLIMAGE_SINK_UNLOCK(glsink) \
(g_mutex_unlock(&GST_GLIMAGE_SINK_GET_LOCK (glsink)))
#define USING_OPENGL(context) (gst_gl_context_check_gl_version (context, GST_GL_API_OPENGL, 1, 0))
#define USING_OPENGL3(context) (gst_gl_context_check_gl_version (context, GST_GL_API_OPENGL3, 3, 1))
#define USING_GLES(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES, 1, 0))
#define USING_GLES2(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES2, 2, 0))
#define USING_GLES3(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES2, 3, 0))
#define SUPPORTED_GL_APIS GST_GL_API_OPENGL | GST_GL_API_GLES2 | GST_GL_API_OPENGL3
static void gst_glimage_sink_thread_init_redisplay (GstGLImageSink * gl_sink);
static void gst_glimage_sink_cleanup_glthread (GstGLImageSink * gl_sink);
static void gst_glimage_sink_on_close (GstGLImageSink * gl_sink);
static void gst_glimage_sink_on_resize (GstGLImageSink * gl_sink,
gint width, gint height);
static void gst_glimage_sink_do_resize (GstGLImageSink * gl_sink,
gint width, gint height);
static void gst_glimage_sink_on_draw (GstGLImageSink * gl_sink);
static gboolean gst_glimage_sink_redisplay (GstGLImageSink * gl_sink);
static void gst_glimage_sink_finalize (GObject * object);
static void gst_glimage_sink_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * param_spec);
static void gst_glimage_sink_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * param_spec);
static gboolean gst_glimage_sink_query (GstBaseSink * bsink, GstQuery * query);
static void gst_glimage_sink_set_context (GstElement * element,
GstContext * context);
static GstStateChangeReturn
gst_glimage_sink_change_state (GstElement * element, GstStateChange transition);
static void gst_glimage_sink_get_times (GstBaseSink * bsink, GstBuffer * buf,
GstClockTime * start, GstClockTime * end);
static gboolean gst_glimage_sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
static GstCaps *gst_glimage_sink_get_caps (GstBaseSink * bsink,
GstCaps * filter);
static GstFlowReturn gst_glimage_sink_prepare (GstBaseSink * bsink,
GstBuffer * buf);
static GstFlowReturn gst_glimage_sink_show_frame (GstVideoSink * bsink,
GstBuffer * buf);
static gboolean gst_glimage_sink_propose_allocation (GstBaseSink * bsink,
GstQuery * query);
static void gst_glimage_sink_video_overlay_init (GstVideoOverlayInterface *
iface);
static void gst_glimage_sink_set_window_handle (GstVideoOverlay * overlay,
guintptr id);
static void gst_glimage_sink_expose (GstVideoOverlay * overlay);
static void gst_glimage_sink_set_render_rectangle (GstVideoOverlay * overlay,
gint x, gint y, gint width, gint height);
static void gst_glimage_sink_handle_events (GstVideoOverlay * overlay,
gboolean handle_events);
static gboolean update_output_format (GstGLImageSink * glimage_sink);
#define GST_GL_SINK_CAPS \
GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_MEMORY_GL_MEMORY, "RGBA")
#define GST_GL_SINK_OVERLAY_CAPS \
GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_MEMORY_GL_MEMORY "," \
GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION, "RGBA")
static GstStaticPadTemplate gst_glimage_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS (GST_GL_SINK_CAPS ";" GST_GL_SINK_OVERLAY_CAPS));
enum
{
ARG_0,
ARG_DISPLAY,
PROP_FORCE_ASPECT_RATIO,
PROP_PIXEL_ASPECT_RATIO,
PROP_CONTEXT,
PROP_HANDLE_EVENTS,
PROP_IGNORE_ALPHA,
PROP_OUTPUT_MULTIVIEW_LAYOUT,
PROP_OUTPUT_MULTIVIEW_FLAGS,
PROP_OUTPUT_MULTIVIEW_DOWNMIX_MODE
};
enum
{
SIGNAL_0,
CLIENT_DRAW_SIGNAL,
CLIENT_RESHAPE_SIGNAL,
LAST_SIGNAL
};
static guint gst_glimage_sink_signals[LAST_SIGNAL] = { 0 };
static void
gst_glimage_sink_navigation_send_event (GstNavigation * navigation, GstStructure
* structure)
{
GstGLImageSink *sink = GST_GLIMAGE_SINK (navigation);
GstEvent *event = NULL;
GstPad *pad = NULL;
GstGLWindow *window;
guint width, height;
gdouble x, y, xscale, yscale;
if (!sink->context)
return;
window = gst_gl_context_get_window (sink->context);
g_return_if_fail (GST_GL_IS_WINDOW (window));
width = GST_VIDEO_SINK_WIDTH (sink);
height = GST_VIDEO_SINK_HEIGHT (sink);
gst_gl_window_get_surface_dimensions (window, &width, &height);
event = gst_event_new_navigation (structure);
pad = gst_pad_get_peer (GST_VIDEO_SINK_PAD (sink));
/* Converting pointer coordinates to the non scaled geometry */
if (width != GST_VIDEO_SINK_WIDTH (sink) &&
width != 0 && gst_structure_get_double (structure, "pointer_x", &x)) {
xscale = (gdouble) GST_VIDEO_SINK_WIDTH (sink) / width;
gst_structure_set (structure, "pointer_x", G_TYPE_DOUBLE,
(gdouble) x * xscale, NULL);
}
if (height != GST_VIDEO_SINK_HEIGHT (sink) &&
height != 0 && gst_structure_get_double (structure, "pointer_y", &y)) {
yscale = (gdouble) GST_VIDEO_SINK_HEIGHT (sink) / height;
gst_structure_set (structure, "pointer_y", G_TYPE_DOUBLE,
(gdouble) y * yscale, NULL);
}
if (GST_IS_PAD (pad) && GST_IS_EVENT (event))
gst_pad_send_event (pad, event);
gst_object_unref (pad);
gst_object_unref (window);
}
static void
gst_glimage_sink_navigation_interface_init (GstNavigationInterface * iface)
{
iface->send_event = gst_glimage_sink_navigation_send_event;
}
#define gst_glimage_sink_parent_class parent_class
G_DEFINE_TYPE_WITH_CODE (GstGLImageSink, gst_glimage_sink,
GST_TYPE_VIDEO_SINK, G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_OVERLAY,
gst_glimage_sink_video_overlay_init);
G_IMPLEMENT_INTERFACE (GST_TYPE_NAVIGATION,
gst_glimage_sink_navigation_interface_init);
GST_DEBUG_CATEGORY_INIT (gst_debug_glimage_sink, "glimagesink", 0,
"OpenGL Video Sink"));
static void
gst_glimage_sink_class_init (GstGLImageSinkClass * klass)
{
GObjectClass *gobject_class;
GstElementClass *gstelement_class;
GstBaseSinkClass *gstbasesink_class;
GstVideoSinkClass *gstvideosink_class;
GstElementClass *element_class;
gobject_class = (GObjectClass *) klass;
gstelement_class = (GstElementClass *) klass;
gstbasesink_class = (GstBaseSinkClass *) klass;
gstvideosink_class = (GstVideoSinkClass *) klass;
element_class = GST_ELEMENT_CLASS (klass);
gobject_class->set_property = gst_glimage_sink_set_property;
gobject_class->get_property = gst_glimage_sink_get_property;
g_object_class_install_property (gobject_class, PROP_FORCE_ASPECT_RATIO,
g_param_spec_boolean ("force-aspect-ratio", "Force aspect ratio",
"When enabled, scaling will respect original aspect ratio",
DEFAULT_FORCE_ASPECT_RATIO,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_PIXEL_ASPECT_RATIO,
gst_param_spec_fraction ("pixel-aspect-ratio", "Pixel Aspect Ratio",
"The pixel aspect ratio of the device", 0, 1, G_MAXINT, 1, 1, 1,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_CONTEXT,
g_param_spec_object ("context", "OpenGL context", "Get OpenGL context",
GST_GL_TYPE_CONTEXT, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_HANDLE_EVENTS,
g_param_spec_boolean ("handle-events", "Handle XEvents",
"When enabled, XEvents will be selected and handled",
DEFAULT_HANDLE_EVENTS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_IGNORE_ALPHA,
g_param_spec_boolean ("ignore-alpha", "Ignore Alpha",
"When enabled, alpha will be ignored and converted to black",
DEFAULT_IGNORE_ALPHA, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_OUTPUT_MULTIVIEW_LAYOUT,
g_param_spec_enum ("output-multiview-mode",
"Output Multiview Mode",
"Choose output mode for multiview/3D video",
GST_TYPE_VIDEO_MULTIVIEW_MODE, DEFAULT_MULTIVIEW_MODE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_OUTPUT_MULTIVIEW_FLAGS,
g_param_spec_flags ("output-multiview-flags",
"Output Multiview Flags",
"Output multiview layout modifier flags",
GST_TYPE_VIDEO_MULTIVIEW_FLAGS, DEFAULT_MULTIVIEW_FLAGS,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_OUTPUT_MULTIVIEW_DOWNMIX_MODE,
g_param_spec_enum ("output-multiview-downmix-mode",
"Mode for mono downmixed output",
"Output anaglyph type to generate when downmixing to mono",
GST_TYPE_GL_STEREO_DOWNMIX_MODE_TYPE, DEFAULT_MULTIVIEW_DOWNMIX,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
gst_element_class_set_metadata (element_class, "OpenGL video sink",
"Sink/Video", "A videosink based on OpenGL",
"Julien Isorce <julien.isorce@gmail.com>");
/**
* GstGLImageSink::client-draw:
* @object: the #GstGLImageSink
* @texture: the #guint id of the texture.
* @width: the #guint width of the texture.
* @height: the #guint height of the texture.
*
* Will be emitted before actually drawing the texture. The client should
* redraw the surface/contents with the @texture, @width and @height and
* and return %TRUE.
*
* Returns: whether the texture was redrawn by the signal. If not, a
* default redraw will occur.
*/
gst_glimage_sink_signals[CLIENT_DRAW_SIGNAL] =
g_signal_new ("client-draw", G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
G_TYPE_BOOLEAN, 2, GST_GL_TYPE_CONTEXT, GST_TYPE_SAMPLE);
/**
* GstGLImageSink::client-reshape:
* @object: the #GstGLImageSink
* @width: the #guint width of the texture.
* @height: the #guint height of the texture.
*
* The client should resize the surface/window/viewport with the @width and
* @height and return %TRUE.
*
* Returns: whether the content area was resized by the signal. If not, a
* default viewport resize will occur.
*/
gst_glimage_sink_signals[CLIENT_RESHAPE_SIGNAL] =
g_signal_new ("client-reshape", G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
G_TYPE_BOOLEAN, 3, GST_GL_TYPE_CONTEXT, G_TYPE_UINT, G_TYPE_UINT);
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&gst_glimage_sink_template));
gobject_class->finalize = gst_glimage_sink_finalize;
gstelement_class->change_state = gst_glimage_sink_change_state;
gstelement_class->set_context = gst_glimage_sink_set_context;
gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_glimage_sink_query);
gstbasesink_class->set_caps = gst_glimage_sink_set_caps;
gstbasesink_class->get_caps = gst_glimage_sink_get_caps;
gstbasesink_class->get_times = gst_glimage_sink_get_times;
gstbasesink_class->prepare = gst_glimage_sink_prepare;
gstbasesink_class->propose_allocation = gst_glimage_sink_propose_allocation;
gstvideosink_class->show_frame =
GST_DEBUG_FUNCPTR (gst_glimage_sink_show_frame);
}
static void
gst_glimage_sink_init (GstGLImageSink * glimage_sink)
{
glimage_sink->window_id = 0;
glimage_sink->new_window_id = 0;
glimage_sink->display = NULL;
glimage_sink->keep_aspect_ratio = TRUE;
glimage_sink->par_n = 0;
glimage_sink->par_d = 1;
glimage_sink->redisplay_texture = 0;
glimage_sink->handle_events = TRUE;
glimage_sink->ignore_alpha = TRUE;
glimage_sink->overlay_compositor = NULL;
glimage_sink->mview_output_mode = DEFAULT_MULTIVIEW_MODE;
glimage_sink->mview_output_flags = DEFAULT_MULTIVIEW_FLAGS;
glimage_sink->mview_downmix_mode = DEFAULT_MULTIVIEW_DOWNMIX;
g_mutex_init (&glimage_sink->drawing_lock);
}
static void
gst_glimage_sink_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
GstGLImageSink *glimage_sink;
g_return_if_fail (GST_IS_GLIMAGE_SINK (object));
glimage_sink = GST_GLIMAGE_SINK (object);
switch (prop_id) {
case PROP_FORCE_ASPECT_RATIO:
{
glimage_sink->keep_aspect_ratio = g_value_get_boolean (value);
break;
}
case PROP_PIXEL_ASPECT_RATIO:
{
glimage_sink->par_n = gst_value_get_fraction_numerator (value);
glimage_sink->par_d = gst_value_get_fraction_denominator (value);
break;
}
case PROP_HANDLE_EVENTS:
gst_glimage_sink_handle_events (GST_VIDEO_OVERLAY (glimage_sink),
g_value_get_boolean (value));
break;
case PROP_IGNORE_ALPHA:
glimage_sink->ignore_alpha = g_value_get_boolean (value);
break;
case PROP_OUTPUT_MULTIVIEW_LAYOUT:
GST_GLIMAGE_SINK_LOCK (glimage_sink);
glimage_sink->mview_output_mode = g_value_get_enum (value);
glimage_sink->output_mode_changed = TRUE;
GST_GLIMAGE_SINK_UNLOCK (glimage_sink);
break;
case PROP_OUTPUT_MULTIVIEW_FLAGS:
GST_GLIMAGE_SINK_LOCK (glimage_sink);
glimage_sink->mview_output_flags = g_value_get_flags (value);
glimage_sink->output_mode_changed = TRUE;
GST_GLIMAGE_SINK_UNLOCK (glimage_sink);
break;
case PROP_OUTPUT_MULTIVIEW_DOWNMIX_MODE:
GST_GLIMAGE_SINK_LOCK (glimage_sink);
glimage_sink->mview_downmix_mode = g_value_get_enum (value);
glimage_sink->output_mode_changed = TRUE;
GST_GLIMAGE_SINK_UNLOCK (glimage_sink);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_glimage_sink_finalize (GObject * object)
{
GstGLImageSink *glimage_sink;
g_return_if_fail (GST_IS_GLIMAGE_SINK (object));
glimage_sink = GST_GLIMAGE_SINK (object);
g_mutex_clear (&glimage_sink->drawing_lock);
GST_DEBUG ("finalized");
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gst_glimage_sink_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
GstGLImageSink *glimage_sink;
g_return_if_fail (GST_IS_GLIMAGE_SINK (object));
glimage_sink = GST_GLIMAGE_SINK (object);
switch (prop_id) {
case PROP_FORCE_ASPECT_RATIO:
g_value_set_boolean (value, glimage_sink->keep_aspect_ratio);
break;
case PROP_PIXEL_ASPECT_RATIO:
gst_value_set_fraction (value, glimage_sink->par_n, glimage_sink->par_d);
break;
case PROP_CONTEXT:
g_value_set_object (value, glimage_sink->context);
break;
case PROP_HANDLE_EVENTS:
g_value_set_boolean (value, glimage_sink->handle_events);
break;
case PROP_IGNORE_ALPHA:
g_value_set_boolean (value, glimage_sink->ignore_alpha);
break;
case PROP_OUTPUT_MULTIVIEW_LAYOUT:
g_value_set_enum (value, glimage_sink->mview_output_mode);
break;
case PROP_OUTPUT_MULTIVIEW_FLAGS:
g_value_set_flags (value, glimage_sink->mview_output_flags);
break;
case PROP_OUTPUT_MULTIVIEW_DOWNMIX_MODE:
g_value_set_enum (value, glimage_sink->mview_downmix_mode);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_glimage_sink_key_event_cb (GstGLWindow * window, char *event_name, char
*key_string, GstGLImageSink * gl_sink)
{
GST_DEBUG_OBJECT (gl_sink, "glimagesink event %s key %s pressed", event_name,
key_string);
gst_navigation_send_key_event (GST_NAVIGATION (gl_sink),
event_name, key_string);
}
static void
gst_glimage_sink_mouse_event_cb (GstGLWindow * window, char *event_name,
int button, double posx, double posy, GstGLImageSink * gl_sink)
{
GST_DEBUG_OBJECT (gl_sink, "glimagesink event %s at %g, %g", event_name, posx,
posy);
gst_navigation_send_mouse_event (GST_NAVIGATION (gl_sink),
event_name, button, posx, posy);
}
static gboolean
_ensure_gl_setup (GstGLImageSink * gl_sink)
{
GError *error = NULL;
GST_TRACE_OBJECT (gl_sink, "Ensuring setup");
if (!gl_sink->context) {
GST_OBJECT_LOCK (gl_sink->display);
do {
GstGLContext *other_context = NULL;
GstGLWindow *window = NULL;
if (gl_sink->context) {
gst_object_unref (gl_sink->context);
gl_sink->context = NULL;
}
GST_DEBUG_OBJECT (gl_sink,
"No current context, creating one for %" GST_PTR_FORMAT,
gl_sink->display);
if (gl_sink->other_context) {
other_context = gst_object_ref (gl_sink->other_context);
} else {
other_context =
gst_gl_display_get_gl_context_for_thread (gl_sink->display, NULL);
}
if (!gst_gl_display_create_context (gl_sink->display,
other_context, &gl_sink->context, &error)) {
if (other_context)
gst_object_unref (other_context);
GST_OBJECT_UNLOCK (gl_sink->display);
goto context_error;
}
GST_DEBUG_OBJECT (gl_sink,
"created context %" GST_PTR_FORMAT " from other context %"
GST_PTR_FORMAT, gl_sink->context, gl_sink->other_context);
window = gst_gl_context_get_window (gl_sink->context);
GST_DEBUG_OBJECT (gl_sink, "got window %" GST_PTR_FORMAT, window);
if (!gl_sink->window_id && !gl_sink->new_window_id)
gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (gl_sink));
GST_DEBUG_OBJECT (gl_sink,
"window_id : %" G_GUINTPTR_FORMAT " , new_window_id : %"
G_GUINTPTR_FORMAT, gl_sink->window_id, gl_sink->new_window_id);
if (gl_sink->window_id != gl_sink->new_window_id) {
gl_sink->window_id = gl_sink->new_window_id;
GST_DEBUG_OBJECT (gl_sink, "Setting window handle on gl window");
gst_gl_window_set_window_handle (window, gl_sink->window_id);
}
gst_gl_window_handle_events (window, gl_sink->handle_events);
/* setup callbacks */
gst_gl_window_set_resize_callback (window,
GST_GL_WINDOW_RESIZE_CB (gst_glimage_sink_on_resize),
gst_object_ref (gl_sink), (GDestroyNotify) gst_object_unref);
gst_gl_window_set_draw_callback (window,
GST_GL_WINDOW_CB (gst_glimage_sink_on_draw),
gst_object_ref (gl_sink), (GDestroyNotify) gst_object_unref);
gst_gl_window_set_close_callback (window,
GST_GL_WINDOW_CB (gst_glimage_sink_on_close),
gst_object_ref (gl_sink), (GDestroyNotify) gst_object_unref);
gl_sink->key_sig_id = g_signal_connect (window, "key-event", G_CALLBACK
(gst_glimage_sink_key_event_cb), gl_sink);
gl_sink->mouse_sig_id =
g_signal_connect (window, "mouse-event",
G_CALLBACK (gst_glimage_sink_mouse_event_cb), gl_sink);
if (gl_sink->x >= 0 && gl_sink->y >= 0 && gl_sink->width > 0 &&
gl_sink->height > 0) {
gst_gl_window_set_render_rectangle (window, gl_sink->x, gl_sink->y,
gl_sink->width, gl_sink->height);
}
if (other_context)
gst_object_unref (other_context);
gst_object_unref (window);
} while (!gst_gl_display_add_context (gl_sink->display, gl_sink->context));
GST_OBJECT_UNLOCK (gl_sink->display);
} else
GST_TRACE_OBJECT (gl_sink, "Already have a context");
return TRUE;
context_error:
{
GST_ELEMENT_ERROR (gl_sink, RESOURCE, NOT_FOUND, ("%s", error->message),
(NULL));
if (gl_sink->context) {
gst_object_unref (gl_sink->context);
gl_sink->context = NULL;
}
g_clear_error (&error);
return FALSE;
}
}
static gboolean
gst_glimage_sink_query (GstBaseSink * bsink, GstQuery * query)
{
GstGLImageSink *glimage_sink = GST_GLIMAGE_SINK (bsink);
gboolean res = FALSE;
switch (GST_QUERY_TYPE (query)) {
case GST_QUERY_CONTEXT:
{
const gchar *context_type;
GstContext *context, *old_context;
res = gst_gl_handle_context_query ((GstElement *) glimage_sink, query,
&glimage_sink->display, &glimage_sink->other_context);
if (glimage_sink->display)
gst_gl_display_filter_gl_api (glimage_sink->display, SUPPORTED_GL_APIS);
gst_query_parse_context_type (query, &context_type);
if (g_strcmp0 (context_type, "gst.gl.local_context") == 0) {
GstStructure *s;
gst_query_parse_context (query, &old_context);
if (old_context)
context = gst_context_copy (old_context);
else
context = gst_context_new ("gst.gl.local_context", FALSE);
s = gst_context_writable_structure (context);
gst_structure_set (s, "context", GST_GL_TYPE_CONTEXT,
glimage_sink->context, NULL);
gst_query_set_context (query, context);
gst_context_unref (context);
res = glimage_sink->context != NULL;
}
GST_LOG_OBJECT (glimage_sink, "context query of type %s %i", context_type,
res);
if (res)
return res;
break;
}
case GST_QUERY_DRAIN:
{
GstBuffer *buf[2];
GST_GLIMAGE_SINK_LOCK (glimage_sink);
glimage_sink->redisplay_texture = 0;
buf[0] = glimage_sink->stored_buffer[0];
buf[1] = glimage_sink->stored_buffer[1];
glimage_sink->stored_buffer[0] = glimage_sink->stored_buffer[1] = NULL;
GST_GLIMAGE_SINK_UNLOCK (glimage_sink);
gst_buffer_replace (buf, NULL);
gst_buffer_replace (buf + 1, NULL);
gst_buffer_replace (&glimage_sink->input_buffer, NULL);
gst_buffer_replace (&glimage_sink->input_buffer2, NULL);
gst_buffer_replace (&glimage_sink->next_buffer, NULL);
gst_buffer_replace (&glimage_sink->next_buffer2, NULL);
gst_buffer_replace (&glimage_sink->next_sync, NULL);
res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
break;
}
default:
res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
break;
}
return res;
}
static void
gst_glimage_sink_set_context (GstElement * element, GstContext * context)
{
GstGLImageSink *gl_sink = GST_GLIMAGE_SINK (element);
gst_gl_handle_set_context (element, context, &gl_sink->display,
&gl_sink->other_context);
if (gl_sink->display)
gst_gl_display_filter_gl_api (gl_sink->display, SUPPORTED_GL_APIS);
}
static GstStateChangeReturn
gst_glimage_sink_change_state (GstElement * element, GstStateChange transition)
{
GstGLImageSink *glimage_sink;
GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
GST_DEBUG ("changing state: %s => %s",
gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
glimage_sink = GST_GLIMAGE_SINK (element);
switch (transition) {
case GST_STATE_CHANGE_NULL_TO_READY:
if (!gst_gl_ensure_element_data (glimage_sink, &glimage_sink->display,
&glimage_sink->other_context))
return GST_STATE_CHANGE_FAILURE;
gst_gl_display_filter_gl_api (glimage_sink->display, SUPPORTED_GL_APIS);
break;
case GST_STATE_CHANGE_READY_TO_PAUSED:
if (!_ensure_gl_setup (glimage_sink))
return GST_STATE_CHANGE_FAILURE;
glimage_sink->overlay_compositor =
gst_gl_overlay_compositor_new (glimage_sink->context);
g_atomic_int_set (&glimage_sink->to_quit, 0);
break;
case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
break;
default:
break;
}
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
if (ret == GST_STATE_CHANGE_FAILURE)
return ret;
switch (transition) {
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
break;
case GST_STATE_CHANGE_PAUSED_TO_READY:
{
GstBuffer *buf[2];
GST_GLIMAGE_SINK_LOCK (glimage_sink);
/* mark the redisplay_texture as unavailable (=0)
* to avoid drawing
*/
glimage_sink->redisplay_texture = 0;
buf[0] = glimage_sink->stored_buffer[0];
buf[1] = glimage_sink->stored_buffer[1];
glimage_sink->stored_buffer[0] = glimage_sink->stored_buffer[1] = NULL;
if (glimage_sink->stored_sync)
gst_buffer_unref (glimage_sink->stored_sync);
glimage_sink->stored_sync = NULL;
GST_GLIMAGE_SINK_UNLOCK (glimage_sink);
gst_buffer_replace (buf, NULL);
gst_buffer_replace (buf + 1, NULL);
gst_object_replace ((GstObject **) & glimage_sink->convert_views, NULL);
gst_buffer_replace (&glimage_sink->input_buffer, NULL);
gst_buffer_replace (&glimage_sink->input_buffer2, NULL);
gst_buffer_replace (&glimage_sink->next_buffer, NULL);
gst_buffer_replace (&glimage_sink->next_buffer2, NULL);
gst_buffer_replace (&glimage_sink->next_sync, NULL);
glimage_sink->window_id = 0;
/* but do not reset glimage_sink->new_window_id */
GST_VIDEO_SINK_WIDTH (glimage_sink) = 1;
GST_VIDEO_SINK_HEIGHT (glimage_sink) = 1;
/* Clear cached caps */
if (glimage_sink->out_caps) {
gst_caps_unref (glimage_sink->out_caps);
glimage_sink->out_caps = NULL;
}
if (glimage_sink->context) {
GstGLWindow *window = gst_gl_context_get_window (glimage_sink->context);
gst_gl_window_send_message (window,
GST_GL_WINDOW_CB (gst_glimage_sink_cleanup_glthread), glimage_sink);
gst_gl_window_set_resize_callback (window, NULL, NULL, NULL);
gst_gl_window_set_draw_callback (window, NULL, NULL, NULL);
gst_gl_window_set_close_callback (window, NULL, NULL, NULL);
if (glimage_sink->key_sig_id)
g_signal_handler_disconnect (window, glimage_sink->key_sig_id);
glimage_sink->key_sig_id = 0;
if (glimage_sink->mouse_sig_id)
g_signal_handler_disconnect (window, glimage_sink->mouse_sig_id);
glimage_sink->mouse_sig_id = 0;
gst_object_unref (glimage_sink->overlay_compositor);
glimage_sink->overlay_compositor = NULL;
gst_object_unref (window);
gst_object_unref (glimage_sink->context);
glimage_sink->context = NULL;
}
break;
}
case GST_STATE_CHANGE_READY_TO_NULL:
if (glimage_sink->other_context) {
gst_object_unref (glimage_sink->other_context);
glimage_sink->other_context = NULL;
}
if (glimage_sink->display) {
gst_object_unref (glimage_sink->display);
glimage_sink->display = NULL;
}
break;
default:
break;
}
return ret;
}
static void
gst_glimage_sink_get_times (GstBaseSink * bsink, GstBuffer * buf,
GstClockTime * start, GstClockTime * end)
{
GstGLImageSink *glimagesink;
glimagesink = GST_GLIMAGE_SINK (bsink);
if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
*start = GST_BUFFER_TIMESTAMP (buf);
if (GST_BUFFER_DURATION_IS_VALID (buf))
*end = *start + GST_BUFFER_DURATION (buf);
else {
if (GST_VIDEO_INFO_FPS_N (&glimagesink->out_info) > 0) {
*end = *start +
gst_util_uint64_scale_int (GST_SECOND,
GST_VIDEO_INFO_FPS_D (&glimagesink->out_info),
GST_VIDEO_INFO_FPS_N (&glimagesink->out_info));
}
}
}
}
static GstCaps *
gst_glimage_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
{
GstCaps *tmp = NULL;
GstCaps *result = NULL;
tmp = gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD (bsink));
if (filter) {
GST_DEBUG_OBJECT (bsink, "intersecting with filter caps %" GST_PTR_FORMAT,
filter);
result = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
gst_caps_unref (tmp);
} else {
result = tmp;
}
result = gst_gl_overlay_compositor_add_caps (result);
GST_DEBUG_OBJECT (bsink, "returning caps: %" GST_PTR_FORMAT, result);
return result;
}
static gboolean
configure_display_from_info (GstGLImageSink * glimage_sink,
GstVideoInfo * vinfo)
{
gint width;
gint height;
gboolean ok;
gint par_n, par_d;
gint display_par_n, display_par_d;
guint display_ratio_num, display_ratio_den;
width = GST_VIDEO_INFO_WIDTH (vinfo);
height = GST_VIDEO_INFO_HEIGHT (vinfo);
par_n = GST_VIDEO_INFO_PAR_N (vinfo);
par_d = GST_VIDEO_INFO_PAR_D (vinfo);
if (!par_n)
par_n = 1;
/* get display's PAR */
if (glimage_sink->par_n != 0 && glimage_sink->par_d != 0) {
display_par_n = glimage_sink->par_n;
display_par_d = glimage_sink->par_d;
} else {
display_par_n = 1;
display_par_d = 1;
}
ok = gst_video_calculate_display_ratio (&display_ratio_num,
&display_ratio_den, width, height, par_n, par_d, display_par_n,
display_par_d);
if (!ok)
return FALSE;
GST_TRACE ("PAR: %u/%u DAR:%u/%u", par_n, par_d, display_par_n,
display_par_d);
if (height % display_ratio_den == 0) {
GST_DEBUG ("keeping video height");
GST_VIDEO_SINK_WIDTH (glimage_sink) = (guint)
gst_util_uint64_scale_int (height, display_ratio_num,
display_ratio_den);
GST_VIDEO_SINK_HEIGHT (glimage_sink) = height;
} else if (width % display_ratio_num == 0) {
GST_DEBUG ("keeping video width");
GST_VIDEO_SINK_WIDTH (glimage_sink) = width;
GST_VIDEO_SINK_HEIGHT (glimage_sink) = (guint)
gst_util_uint64_scale_int (width, display_ratio_den, display_ratio_num);
} else {
GST_DEBUG ("approximating while keeping video height");
GST_VIDEO_SINK_WIDTH (glimage_sink) = (guint)
gst_util_uint64_scale_int (height, display_ratio_num,
display_ratio_den);
GST_VIDEO_SINK_HEIGHT (glimage_sink) = height;
}
GST_DEBUG ("scaling to %dx%d", GST_VIDEO_SINK_WIDTH (glimage_sink),
GST_VIDEO_SINK_HEIGHT (glimage_sink));
return TRUE;
}
/* Called with GST_GLIMAGE_SINK lock held, to
* copy in_info to out_info and update out_caps */
static gboolean
update_output_format (GstGLImageSink * glimage_sink)
{
GstVideoInfo *out_info = &glimage_sink->out_info;
gboolean input_is_mono = FALSE;
GstVideoMultiviewMode mv_mode;
gboolean ret;
*out_info = glimage_sink->in_info;
mv_mode = GST_VIDEO_INFO_MULTIVIEW_MODE (&glimage_sink->in_info);
if (mv_mode == GST_VIDEO_MULTIVIEW_MODE_NONE ||
mv_mode == GST_VIDEO_MULTIVIEW_MODE_MONO ||
mv_mode == GST_VIDEO_MULTIVIEW_MODE_LEFT ||
mv_mode == GST_VIDEO_MULTIVIEW_MODE_RIGHT)
input_is_mono = TRUE;
if (input_is_mono == FALSE &&
glimage_sink->mview_output_mode != GST_VIDEO_MULTIVIEW_MODE_NONE) {
/* Input is multiview, and output wants a conversion - configure 3d converter now,
* otherwise defer it until either the caps or the 3D output mode changes */
gst_video_multiview_video_info_change_mode (out_info,
glimage_sink->mview_output_mode, glimage_sink->mview_output_flags);
if (glimage_sink->convert_views == NULL) {
glimage_sink->convert_views = gst_gl_view_convert_new ();
gst_gl_view_convert_set_context (glimage_sink->convert_views,
glimage_sink->context);
}
} else {
if (glimage_sink->convert_views) {
gst_object_unref (glimage_sink->convert_views);
glimage_sink->convert_views = NULL;
}
}
ret = configure_display_from_info (glimage_sink, out_info);
if (glimage_sink->convert_views) {
/* Match actual output window size for pixel-aligned output,
* even though we can't necessarily match the starting left/right
* view parity properly */
glimage_sink->out_info.width = MAX (1, glimage_sink->display_rect.w);
glimage_sink->out_info.height = MAX (1, glimage_sink->display_rect.h);
GST_LOG_OBJECT (glimage_sink, "Set 3D output scale to %d,%d",
glimage_sink->display_rect.w, glimage_sink->display_rect.h);
GST_GLIMAGE_SINK_UNLOCK (glimage_sink);
gst_gl_view_convert_set_format (glimage_sink->convert_views,
&glimage_sink->in_info, &glimage_sink->out_info);
g_object_set (glimage_sink->convert_views, "downmix-mode",
glimage_sink->mview_downmix_mode, NULL);
GST_GLIMAGE_SINK_LOCK (glimage_sink);
}
glimage_sink->output_mode_changed = FALSE;
glimage_sink->caps_change = TRUE;
if (glimage_sink->out_caps)
gst_caps_unref (glimage_sink->out_caps);
glimage_sink->out_caps = gst_video_info_to_caps (out_info);
return ret;
}
static gboolean
gst_glimage_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
{
GstGLImageSink *glimage_sink;
gboolean ok;
GstVideoInfo vinfo;
GST_DEBUG_OBJECT (bsink, "set caps with %" GST_PTR_FORMAT, caps);
glimage_sink = GST_GLIMAGE_SINK (bsink);
ok = gst_video_info_from_caps (&vinfo, caps);
if (!ok)
return FALSE;
if (!_ensure_gl_setup (glimage_sink))
return FALSE;
GST_GLIMAGE_SINK_LOCK (glimage_sink);
glimage_sink->in_info = vinfo;
ok = update_output_format (glimage_sink);
GST_GLIMAGE_SINK_UNLOCK (glimage_sink);
return ok;
}
/* Take the input_buffer and run it through 3D conversion if needed.
* Called with glimagesink lock, but might drop it temporarily */
static gboolean
prepare_next_buffer (GstGLImageSink * glimage_sink)
{
GstBuffer *in_buffer, *next_buffer, *old_buffer;
GstBuffer *in_buffer2 = NULL, *next_buffer2 = NULL, *old_buffer2;
GstBuffer *next_sync, *old_sync;
GstGLSyncMeta *sync_meta;
GstVideoFrame gl_frame;
GstGLViewConvert *convert_views = NULL;
GstVideoInfo *info;
if (glimage_sink->input_buffer == NULL)
return TRUE; /* No input buffer to process */
if (GST_VIDEO_INFO_MULTIVIEW_MODE (&glimage_sink->in_info) ==
GST_VIDEO_MULTIVIEW_MODE_FRAME_BY_FRAME) {
if (glimage_sink->input_buffer2 == NULL)
return TRUE; /* Need 2nd input buffer to process */
in_buffer2 = gst_buffer_ref (glimage_sink->input_buffer2);
}
in_buffer = gst_buffer_ref (glimage_sink->input_buffer);
if (glimage_sink->convert_views &&
(GST_VIDEO_INFO_MULTIVIEW_MODE (&glimage_sink->in_info) !=
GST_VIDEO_INFO_MULTIVIEW_MODE (&glimage_sink->out_info) ||
GST_VIDEO_INFO_MULTIVIEW_FLAGS (&glimage_sink->in_info) !=
GST_VIDEO_INFO_MULTIVIEW_FLAGS (&glimage_sink->out_info)))
convert_views = gst_object_ref (glimage_sink->convert_views);
GST_GLIMAGE_SINK_UNLOCK (glimage_sink);
if (convert_views) {
info = &glimage_sink->out_info;
if (gst_gl_view_convert_submit_input_buffer (glimage_sink->convert_views,
GST_BUFFER_IS_DISCONT (in_buffer), in_buffer) != GST_FLOW_OK) {
gst_buffer_replace (&in_buffer2, NULL);
goto fail;
}
if (in_buffer2) {
if (gst_gl_view_convert_submit_input_buffer (glimage_sink->convert_views,
GST_BUFFER_IS_DISCONT (in_buffer2), in_buffer2) != GST_FLOW_OK) {
goto fail;
}
}
if (gst_gl_view_convert_get_output (glimage_sink->convert_views,
&next_buffer) != GST_FLOW_OK)
goto fail;
if (GST_VIDEO_INFO_MULTIVIEW_MODE (info) ==
GST_VIDEO_MULTIVIEW_MODE_FRAME_BY_FRAME) {
if (gst_gl_view_convert_get_output (glimage_sink->convert_views,
&next_buffer2) != GST_FLOW_OK)
goto fail;
}
gst_object_unref (convert_views);
if (next_buffer == NULL) {
/* Not ready to paint a buffer yet */
GST_GLIMAGE_SINK_LOCK (glimage_sink);
return TRUE;
}
} else {
next_buffer = in_buffer;
info = &glimage_sink->in_info;
}
gst_gl_overlay_compositor_upload_overlays (glimage_sink->overlay_compositor,
next_buffer);
/* in_buffer invalid now */
if (!gst_video_frame_map (&gl_frame, info, next_buffer,
GST_MAP_READ | GST_MAP_GL)) {
gst_buffer_unref (next_buffer);
goto fail;
}
next_sync = gst_buffer_new ();
sync_meta = gst_buffer_add_gl_sync_meta (glimage_sink->context, next_sync);
gst_gl_sync_meta_set_sync_point (sync_meta, glimage_sink->context);
GST_GLIMAGE_SINK_LOCK (glimage_sink);
glimage_sink->next_tex = *(guint *) gl_frame.data[0];
old_buffer = glimage_sink->next_buffer;
glimage_sink->next_buffer = next_buffer;
old_buffer2 = glimage_sink->next_buffer2;
glimage_sink->next_buffer2 = next_buffer2;
old_sync = glimage_sink->next_sync;
glimage_sink->next_sync = next_sync;
/* Need to drop the lock again, to avoid a deadlock if we're
* dropping the last ref on this buffer and it goes back to our
* allocator */
GST_GLIMAGE_SINK_UNLOCK (glimage_sink);
if (old_buffer)
gst_buffer_unref (old_buffer);
if (old_buffer2)
gst_buffer_unref (old_buffer2);
if (old_sync)
gst_buffer_unref (old_sync);
gst_video_frame_unmap (&gl_frame);
GST_GLIMAGE_SINK_LOCK (glimage_sink);
return TRUE;
fail:
GST_GLIMAGE_SINK_LOCK (glimage_sink);
return FALSE;
}
static GstFlowReturn
gst_glimage_sink_prepare (GstBaseSink * bsink, GstBuffer * buf)
{
GstGLImageSink *glimage_sink;
GstBuffer **target;
GstBuffer *old_input;
glimage_sink = GST_GLIMAGE_SINK (bsink);
GST_TRACE ("preparing buffer:%p", buf);
if (GST_VIDEO_SINK_WIDTH (glimage_sink) < 1 ||
GST_VIDEO_SINK_HEIGHT (glimage_sink) < 1) {
return GST_FLOW_NOT_NEGOTIATED;
}
if (!_ensure_gl_setup (glimage_sink))
return GST_FLOW_NOT_NEGOTIATED;
GST_GLIMAGE_SINK_LOCK (glimage_sink);
target = &glimage_sink->input_buffer;
if (GST_VIDEO_INFO_MULTIVIEW_MODE (&glimage_sink->in_info) ==
GST_VIDEO_MULTIVIEW_MODE_FRAME_BY_FRAME &&
!GST_BUFFER_FLAG_IS_SET (buf, GST_VIDEO_BUFFER_FLAG_FIRST_IN_BUNDLE)) {
target = &glimage_sink->input_buffer2;
}
old_input = *target;
*target = gst_buffer_ref (buf);
if (glimage_sink->output_mode_changed)
update_output_format (glimage_sink);
if (!prepare_next_buffer (glimage_sink)) {
GST_GLIMAGE_SINK_UNLOCK (glimage_sink);
if (old_input)
gst_buffer_unref (old_input);
goto convert_views_failed;
}
GST_GLIMAGE_SINK_UNLOCK (glimage_sink);
if (old_input)
gst_buffer_unref (old_input);
if (glimage_sink->window_id != glimage_sink->new_window_id) {
GstGLWindow *window = gst_gl_context_get_window (glimage_sink->context);
glimage_sink->window_id = glimage_sink->new_window_id;
gst_gl_window_set_window_handle (window, glimage_sink->window_id);
gst_object_unref (window);
}
return GST_FLOW_OK;
convert_views_failed:
{
GST_ELEMENT_ERROR (glimage_sink, RESOURCE, NOT_FOUND,
("%s", "Failed to convert multiview video buffer"), (NULL));
return GST_FLOW_ERROR;
}
}
static GstFlowReturn
gst_glimage_sink_show_frame (GstVideoSink * vsink, GstBuffer * buf)
{
GstGLImageSink *glimage_sink;
GST_TRACE ("rendering buffer:%p", buf);
glimage_sink = GST_GLIMAGE_SINK (vsink);
GST_TRACE ("redisplay texture:%u of size:%ux%u, window size:%ux%u",
glimage_sink->next_tex, GST_VIDEO_INFO_WIDTH (&glimage_sink->out_info),
GST_VIDEO_INFO_HEIGHT (&glimage_sink->out_info),
GST_VIDEO_SINK_WIDTH (glimage_sink),
GST_VIDEO_SINK_HEIGHT (glimage_sink));
/* Ask the underlying window to redraw its content */
if (!gst_glimage_sink_redisplay (glimage_sink))
goto redisplay_failed;
GST_TRACE ("post redisplay");
if (g_atomic_int_get (&glimage_sink->to_quit) != 0) {
GST_ELEMENT_ERROR (glimage_sink, RESOURCE, NOT_FOUND,
("%s", gst_gl_context_get_error ()), (NULL));
return GST_FLOW_ERROR;
}
return GST_FLOW_OK;
/* ERRORS */
redisplay_failed:
{
GST_ELEMENT_ERROR (glimage_sink, RESOURCE, NOT_FOUND,
("%s", gst_gl_context_get_error ()), (NULL));
return GST_FLOW_ERROR;
}
}
static void
gst_glimage_sink_video_overlay_init (GstVideoOverlayInterface * iface)
{
iface->set_window_handle = gst_glimage_sink_set_window_handle;
iface->set_render_rectangle = gst_glimage_sink_set_render_rectangle;
iface->handle_events = gst_glimage_sink_handle_events;
iface->expose = gst_glimage_sink_expose;
}
static void
gst_glimage_sink_set_window_handle (GstVideoOverlay * overlay, guintptr id)
{
GstGLImageSink *glimage_sink = GST_GLIMAGE_SINK (overlay);
guintptr window_id = (guintptr) id;
g_return_if_fail (GST_IS_GLIMAGE_SINK (overlay));
GST_DEBUG ("set_xwindow_id %" G_GUINT64_FORMAT, (guint64) window_id);
glimage_sink->new_window_id = window_id;
}
static void
gst_glimage_sink_expose (GstVideoOverlay * overlay)
{
GstGLImageSink *glimage_sink = GST_GLIMAGE_SINK (overlay);
/* redisplay opengl scene */
if (glimage_sink->display && glimage_sink->window_id) {
if (glimage_sink->window_id != glimage_sink->new_window_id) {
GstGLWindow *window = gst_gl_context_get_window (glimage_sink->context);
glimage_sink->window_id = glimage_sink->new_window_id;
gst_gl_window_set_window_handle (window, glimage_sink->window_id);
gst_object_unref (window);
}
gst_glimage_sink_redisplay (glimage_sink);
}
}
static void
gst_glimage_sink_handle_events (GstVideoOverlay * overlay,
gboolean handle_events)
{
GstGLImageSink *glimage_sink = GST_GLIMAGE_SINK (overlay);
glimage_sink->handle_events = handle_events;
if (G_LIKELY (glimage_sink->context)) {
GstGLWindow *window;
window = gst_gl_context_get_window (glimage_sink->context);
gst_gl_window_handle_events (window, handle_events);
gst_object_unref (window);
}
}
static void
gst_glimage_sink_set_render_rectangle (GstVideoOverlay * overlay,
gint x, gint y, gint width, gint height)
{
GstGLImageSink *glimage_sink = GST_GLIMAGE_SINK (overlay);
if (G_LIKELY (glimage_sink->context)) {
GstGLWindow *window;
window = gst_gl_context_get_window (glimage_sink->context);
gst_gl_window_set_render_rectangle (window, x, y, width, height);
gst_object_unref (window);
}
glimage_sink->x = x;
glimage_sink->y = y;
glimage_sink->width = width;
glimage_sink->height = height;
}
static gboolean
gst_glimage_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
{
GstGLImageSink *glimage_sink = GST_GLIMAGE_SINK (bsink);
GstStructure *config;
GstCaps *caps;
guint size;
gboolean need_pool;
GstStructure *allocation_meta = NULL;
if (!_ensure_gl_setup (glimage_sink))
return FALSE;
gst_query_parse_allocation (query, &caps, &need_pool);
if (caps == NULL)
goto no_caps;
if (need_pool) {
GstBufferPool *pool;
GstVideoInfo info;
if (!gst_video_info_from_caps (&info, caps))
goto invalid_caps;
/* the normal size of a frame */
size = info.size;
GST_DEBUG_OBJECT (glimage_sink, "create new pool");
pool = gst_gl_buffer_pool_new (glimage_sink->context);
config = gst_buffer_pool_get_config (pool);
gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
if (!gst_buffer_pool_set_config (pool, config)) {
g_object_unref (pool);
goto config_failed;
}
/* we need at least 2 buffer because we hold on to the last one */
gst_query_add_allocation_pool (query, pool, size, 2, 0);
g_object_unref (pool);
}
if (glimage_sink->context->gl_vtable->FenceSync)
gst_query_add_allocation_meta (query, GST_GL_SYNC_META_API_TYPE, 0);
if (glimage_sink->window_width != 0 && glimage_sink->window_height != 0) {
allocation_meta =
gst_structure_new ("GstVideoOverlayCompositionMeta",
"width", G_TYPE_UINT, glimage_sink->window_width,
"height", G_TYPE_UINT, glimage_sink->window_height, NULL);
GST_DEBUG ("sending alloc query with size %dx%d",
glimage_sink->window_width, glimage_sink->window_height);
}
gst_query_add_allocation_meta (query,
GST_VIDEO_OVERLAY_COMPOSITION_META_API_TYPE, allocation_meta);
if (allocation_meta)
gst_structure_free (allocation_meta);
return TRUE;
/* ERRORS */
no_caps:
{
GST_WARNING_OBJECT (bsink, "no caps specified");
return FALSE;
}
invalid_caps:
{
GST_WARNING_OBJECT (bsink, "invalid caps specified");
return FALSE;
}
config_failed:
{
GST_WARNING_OBJECT (bsink, "failed setting config");
return FALSE;
}
}
/* *INDENT-OFF* */
static const GLfloat vertices[] = {
1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f, 0.0f, 0.0f,
-1.0f, -1.0f, 0.0f, 0.0f, 1.0f,
1.0f, -1.0f, 0.0f, 1.0f, 1.0f
};
static const GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
/* *INDENT-ON* */
static void
_bind_buffer (GstGLImageSink * gl_sink)
{
const GstGLFuncs *gl = gl_sink->context->gl_vtable;
gl->BindBuffer (GL_ELEMENT_ARRAY_BUFFER, gl_sink->vbo_indices);
gl->BindBuffer (GL_ARRAY_BUFFER, gl_sink->vertex_buffer);
/* Load the vertex position */
gl->VertexAttribPointer (gl_sink->attr_position, 3, GL_FLOAT, GL_FALSE,
5 * sizeof (GLfloat), (void *) 0);
/* Load the texture coordinate */
gl->VertexAttribPointer (gl_sink->attr_texture, 2, GL_FLOAT, GL_FALSE,
5 * sizeof (GLfloat), (void *) (3 * sizeof (GLfloat)));
gl->EnableVertexAttribArray (gl_sink->attr_position);
gl->EnableVertexAttribArray (gl_sink->attr_texture);
}
static void
_unbind_buffer (GstGLImageSink * gl_sink)
{
const GstGLFuncs *gl = gl_sink->context->gl_vtable;
gl->BindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0);
gl->BindBuffer (GL_ARRAY_BUFFER, 0);
gl->DisableVertexAttribArray (gl_sink->attr_position);
gl->DisableVertexAttribArray (gl_sink->attr_texture);
}
/* Called in the gl thread */
static void
gst_glimage_sink_thread_init_redisplay (GstGLImageSink * gl_sink)
{
const GstGLFuncs *gl = gl_sink->context->gl_vtable;
gl_sink->redisplay_shader = gst_gl_shader_new (gl_sink->context);
if (!gst_gl_shader_compile_with_default_vf_and_check
(gl_sink->redisplay_shader, &gl_sink->attr_position,
&gl_sink->attr_texture))
gst_glimage_sink_cleanup_glthread (gl_sink);
if (gl->GenVertexArrays) {
gl->GenVertexArrays (1, &gl_sink->vao);
gl->BindVertexArray (gl_sink->vao);
}
if (!gl_sink->vertex_buffer) {
gl->GenBuffers (1, &gl_sink->vertex_buffer);
gl->BindBuffer (GL_ARRAY_BUFFER, gl_sink->vertex_buffer);
gl->BufferData (GL_ARRAY_BUFFER, 4 * 5 * sizeof (GLfloat), vertices,
GL_STATIC_DRAW);
}
if (!gl_sink->vbo_indices) {
gl->GenBuffers (1, &gl_sink->vbo_indices);
gl->BindBuffer (GL_ELEMENT_ARRAY_BUFFER, gl_sink->vbo_indices);
gl->BufferData (GL_ELEMENT_ARRAY_BUFFER, sizeof (indices), indices,
GL_STATIC_DRAW);
}
if (gl->GenVertexArrays) {
_bind_buffer (gl_sink);
gl->BindVertexArray (0);
}
gl->BindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0);
gl->BindBuffer (GL_ARRAY_BUFFER, 0);
}
static void
gst_glimage_sink_cleanup_glthread (GstGLImageSink * gl_sink)
{
const GstGLFuncs *gl = gl_sink->context->gl_vtable;
if (gl_sink->redisplay_shader) {
gst_object_unref (gl_sink->redisplay_shader);
gl_sink->redisplay_shader = NULL;
}
if (gl_sink->vao) {
gl->DeleteVertexArrays (1, &gl_sink->vao);
gl_sink->vao = 0;
}
if (gl_sink->vertex_buffer) {
gl->DeleteBuffers (1, &gl_sink->vertex_buffer);
gl_sink->vertex_buffer = 0;
}
if (gl_sink->vbo_indices) {
gl->DeleteBuffers (1, &gl_sink->vbo_indices);
gl_sink->vbo_indices = 0;
}
gst_gl_overlay_compositor_free_overlays (gl_sink->overlay_compositor);
}
static void
gst_glimage_sink_on_resize (GstGLImageSink * gl_sink, gint width, gint height)
{
GST_DEBUG_OBJECT (gl_sink, "GL Window resized to %ux%u", width, height);
GST_GLIMAGE_SINK_LOCK (gl_sink);
gl_sink->output_mode_changed = TRUE;
gst_glimage_sink_do_resize (gl_sink, width, height);
GST_GLIMAGE_SINK_UNLOCK (gl_sink);
}
/* Called with object lock held */
static void
gst_glimage_sink_do_resize (GstGLImageSink * gl_sink, gint width, gint height)
{
/* Here gl_sink members (ex:gl_sink->out_info) have a life time of set_caps.
* It means that they cannot change between two set_caps
*/
gboolean do_reshape;
gboolean reconfigure;
GST_GLIMAGE_SINK_UNLOCK (gl_sink);
/* check if a client reshape callback is registered */
g_signal_emit (gl_sink, gst_glimage_sink_signals[CLIENT_RESHAPE_SIGNAL], 0,
gl_sink->context, width, height, &do_reshape);
GST_GLIMAGE_SINK_LOCK (gl_sink);
width = MAX (1, width);
height = MAX (1, height);
/* Check if we would suggest a different width/height now */
reconfigure = ((gl_sink->window_width != width)
|| (gl_sink->window_height != height))
&& (gl_sink->window_width != 0)
&& (gl_sink->window_height != 0);
if (reconfigure) {
GST_DEBUG ("Sending reconfigure event on sinkpad.");
gst_pad_push_event (GST_BASE_SINK (gl_sink)->sinkpad,
gst_event_new_reconfigure ());
}
gl_sink->window_width = width;
gl_sink->window_height = height;
/* default reshape */
if (!do_reshape) {
if (gl_sink->keep_aspect_ratio) {
GstVideoRectangle src, dst, result;
src.x = 0;
src.y = 0;
src.w = GST_VIDEO_SINK_WIDTH (gl_sink);
src.h = GST_VIDEO_SINK_HEIGHT (gl_sink);
dst.x = 0;
dst.y = 0;
dst.w = width;
dst.h = height;
gst_video_sink_center_rect (src, dst, &result, TRUE);
gl_sink->output_mode_changed |= (result.w != gl_sink->display_rect.w);
gl_sink->output_mode_changed |= (result.h != gl_sink->display_rect.h);
gl_sink->display_rect = result;
} else {
gl_sink->output_mode_changed |= (width != gl_sink->display_rect.w);
gl_sink->output_mode_changed |= (height != gl_sink->display_rect.h);
gl_sink->display_rect.x = 0;
gl_sink->display_rect.y = 0;
gl_sink->display_rect.w = width;
gl_sink->display_rect.h = height;
}
gl_sink->update_viewport = TRUE;
}
}
static void
gst_glimage_sink_on_draw (GstGLImageSink * gl_sink)
{
/* Here gl_sink members (ex:gl_sink->out_info) have a life time of set_caps.
* It means that they cannot not change between two set_caps as well as
* for the redisplay_texture size.
* Whereas redisplay_texture id changes every sink_render
*/
const GstGLFuncs *gl = NULL;
GstGLWindow *window = NULL;
gboolean do_redisplay = FALSE;
GstGLSyncMeta *sync_meta = NULL;
GstSample *sample = NULL;
g_return_if_fail (GST_IS_GLIMAGE_SINK (gl_sink));
gl = gl_sink->context->gl_vtable;
GST_GLIMAGE_SINK_LOCK (gl_sink);
/* check if texture is ready for being drawn */
if (!gl_sink->redisplay_texture) {
GST_GLIMAGE_SINK_UNLOCK (gl_sink);
return;
}
window = gst_gl_context_get_window (gl_sink->context);
window->is_drawing = TRUE;
/* opengl scene */
GST_TRACE ("redrawing texture:%u", gl_sink->redisplay_texture);
if (gl_sink->caps_change && gl_sink->window_width > 0
&& gl_sink->window_height > 0) {
gst_glimage_sink_do_resize (gl_sink, gl_sink->window_width,
gl_sink->window_height);
gl_sink->caps_change = FALSE;
}
if (gl_sink->update_viewport == TRUE) {
gl->Viewport (gl_sink->display_rect.x, gl_sink->display_rect.y,
gl_sink->display_rect.w, gl_sink->display_rect.h);
GST_DEBUG_OBJECT (gl_sink, "GL output area now %u,%u %ux%u",
gl_sink->display_rect.x, gl_sink->display_rect.y,
gl_sink->display_rect.w, gl_sink->display_rect.h);
gl_sink->update_viewport = FALSE;
}
sync_meta = gst_buffer_get_gl_sync_meta (gl_sink->stored_sync);
if (sync_meta)
gst_gl_sync_meta_wait (sync_meta, gst_gl_context_get_current ());
/* make sure that the environnement is clean */
gst_gl_context_clear_shader (gl_sink->context);
gl->BindTexture (GL_TEXTURE_2D, 0);
#if GST_GL_HAVE_OPENGL
if (USING_OPENGL (gl_sink->context))
gl->Disable (GL_TEXTURE_2D);
#endif
sample = gst_sample_new (gl_sink->stored_buffer[0],
gl_sink->out_caps, &GST_BASE_SINK (gl_sink)->segment, NULL);
g_signal_emit (gl_sink, gst_glimage_sink_signals[CLIENT_DRAW_SIGNAL], 0,
gl_sink->context, sample, &do_redisplay);
gst_sample_unref (sample);
if (gl_sink->stored_buffer[1]) {
sample = gst_sample_new (gl_sink->stored_buffer[1],
gl_sink->out_caps, &GST_BASE_SINK (gl_sink)->segment, NULL);
g_signal_emit (gl_sink, gst_glimage_sink_signals[CLIENT_DRAW_SIGNAL], 0,
gl_sink->context, sample, &do_redisplay);
gst_sample_unref (sample);
}
if (!do_redisplay) {
gfloat alpha = gl_sink->ignore_alpha ? 1.0f : 0.0f;
gl->ClearColor (0.0, 0.0, 0.0, alpha);
gl->Clear (GL_COLOR_BUFFER_BIT);
if (gl_sink->ignore_alpha) {
gl->BlendColor (0.0, 0.0, 0.0, alpha);
gl->BlendFunc (GL_SRC_ALPHA, GL_CONSTANT_COLOR);
gl->BlendEquation (GL_FUNC_ADD);
gl->Enable (GL_BLEND);
}
gst_gl_shader_use (gl_sink->redisplay_shader);
if (gl->GenVertexArrays)
gl->BindVertexArray (gl_sink->vao);
else
_bind_buffer (gl_sink);
gl->ActiveTexture (GL_TEXTURE0);
gl->BindTexture (GL_TEXTURE_2D, gl_sink->redisplay_texture);
gst_gl_shader_set_uniform_1i (gl_sink->redisplay_shader, "tex", 0);
gl->DrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
gst_gl_overlay_compositor_draw_overlays (gl_sink->overlay_compositor);
gst_gl_context_clear_shader (gl_sink->context);
if (gl->GenVertexArrays)
gl->BindVertexArray (0);
else
_unbind_buffer (gl_sink);
if (gl_sink->ignore_alpha)
gl->Disable (GL_BLEND);
}
/* end default opengl scene */
window->is_drawing = FALSE;
gst_object_unref (window);
GST_GLIMAGE_SINK_UNLOCK (gl_sink);
}
static void
gst_glimage_sink_on_close (GstGLImageSink * gl_sink)
{
GstGLWindow *window;
gst_gl_context_set_error (gl_sink->context, "Output window was closed");
window = gst_gl_context_get_window (gl_sink->context);
if (gl_sink->key_sig_id)
g_signal_handler_disconnect (window, gl_sink->key_sig_id);
gl_sink->key_sig_id = 0;
if (gl_sink->mouse_sig_id)
g_signal_handler_disconnect (window, gl_sink->mouse_sig_id);
gl_sink->mouse_sig_id = 0;
g_atomic_int_set (&gl_sink->to_quit, 1);
gst_object_unref (window);
}
static gboolean
gst_glimage_sink_redisplay (GstGLImageSink * gl_sink)
{
GstGLWindow *window;
gboolean alive;
GstBuffer *old_stored_buffer[2], *old_sync;
window = gst_gl_context_get_window (gl_sink->context);
if (!window)
return FALSE;
if (gst_gl_window_is_running (window)) {
gulong handler_id =
g_signal_handler_find (GST_ELEMENT_PARENT (gl_sink), G_SIGNAL_MATCH_ID,
gst_gl_image_sink_bin_signals[SIGNAL_BIN_CLIENT_DRAW], 0,
NULL, NULL, NULL);
if (G_UNLIKELY (!gl_sink->redisplay_shader) && (!handler_id
|| !gl_sink->other_context)) {
gst_gl_window_send_message (window,
GST_GL_WINDOW_CB (gst_glimage_sink_thread_init_redisplay), gl_sink);
/* if the shader is still null it means it failed to be useable */
if (G_UNLIKELY (!gl_sink->redisplay_shader)) {
gst_object_unref (window);
return FALSE;
}
gst_gl_window_set_preferred_size (window, GST_VIDEO_SINK_WIDTH (gl_sink),
GST_VIDEO_SINK_HEIGHT (gl_sink));
gst_gl_window_show (window);
}
/* Recreate the output texture if needed */
GST_GLIMAGE_SINK_LOCK (gl_sink);
if (gl_sink->output_mode_changed && gl_sink->input_buffer != NULL) {
GST_DEBUG ("Recreating output after mode/size change");
update_output_format (gl_sink);
prepare_next_buffer (gl_sink);
}
if (gl_sink->next_buffer == NULL) {
/* Nothing to display yet */
GST_GLIMAGE_SINK_UNLOCK (gl_sink);
return TRUE;
}
/* Avoid to release the texture while drawing */
gl_sink->redisplay_texture = gl_sink->next_tex;
old_stored_buffer[0] = gl_sink->stored_buffer[0];
old_stored_buffer[1] = gl_sink->stored_buffer[1];
gl_sink->stored_buffer[0] = gst_buffer_ref (gl_sink->next_buffer);
if (gl_sink->next_buffer2)
gl_sink->stored_buffer[1] = gst_buffer_ref (gl_sink->next_buffer2);
else
gl_sink->stored_buffer[1] = NULL;
old_sync = gl_sink->stored_sync;
gl_sink->stored_sync = gst_buffer_ref (gl_sink->next_sync);
GST_GLIMAGE_SINK_UNLOCK (gl_sink);
gst_buffer_replace (old_stored_buffer, NULL);
gst_buffer_replace (old_stored_buffer + 1, NULL);
if (old_sync)
gst_buffer_unref (old_sync);
/* Drawing is asynchronous: gst_gl_window_draw is not blocking
* It means that it does not wait for stuff to be executed in other threads
*/
gst_gl_window_draw (window);
}
alive = gst_gl_window_is_running (window);
gst_object_unref (window);
return alive;
}
|