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
|
/* GStreamer
* Copyright (C) 2008 Pioneers of the Inevitable <songbird@songbirdnest.com>
* 2010 FLUENDO S.A. <support@fluendo.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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "dshowvideosink.h"
#include "dshowvideofakesrc.h"
#include <gst/video/video.h>
#include <gst/video/videooverlay.h>
#include <gst/video/navigation.h>
#include "windows.h"
#define WM_GRAPH_NOTIFY WM_APP + 1 /* Private message */
GST_DEBUG_CATEGORY (dshowvideosink_debug);
#define GST_CAT_DEFAULT dshowvideosink_debug
static GstCaps * gst_directshow_media_type_to_caps (AM_MEDIA_TYPE *mediatype);
static gboolean gst_caps_to_directshow_media_type (GstDshowVideoSink * sink,
GstCaps *caps, AM_MEDIA_TYPE *mediatype);
/* TODO: Support RGB! */
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS (
"video/x-raw,"
"width = (int) [ 1, MAX ],"
"height = (int) [ 1, MAX ],"
"framerate = (fraction) [ 0, MAX ],"
"format = {(string)YUY2, (string)UYVY, (string)YV12 }")
);
static void gst_dshowvideosink_init_interfaces (GType type);
static void gst_dshowvideosink_videooverlay_init (GstVideoOverlayInterface *iface);
static void
gst_dshowvideosink_navigation_interface_init (GstNavigationInterface * iface);
#define gst_dshowvideosink_parent_class parent_class
G_DEFINE_TYPE_WITH_CODE (GstDshowVideoSink, gst_dshowvideosink,
GST_TYPE_VIDEO_SINK,
G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_OVERLAY, gst_dshowvideosink_videooverlay_init);
G_IMPLEMENT_INTERFACE (GST_TYPE_NAVIGATION, gst_dshowvideosink_navigation_interface_init))
enum
{
PROP_0,
PROP_KEEP_ASPECT_RATIO,
PROP_FULL_SCREEN,
PROP_RENDERER
};
/* GObject methods */
static void gst_dshowvideosink_finalize (GObject * gobject);
static void gst_dshowvideosink_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
static void gst_dshowvideosink_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
/* GstElement methods */
static GstStateChangeReturn gst_dshowvideosink_change_state (GstElement * element, GstStateChange transition);
/* GstBaseSink methods */
static gboolean gst_dshowvideosink_start (GstBaseSink * bsink);
static gboolean gst_dshowvideosink_stop (GstBaseSink * bsink);
static gboolean gst_dshowvideosink_unlock (GstBaseSink * bsink);
static gboolean gst_dshowvideosink_unlock_stop (GstBaseSink * bsink);
static gboolean gst_dshowvideosink_set_caps (GstBaseSink * bsink, GstCaps * caps);
static GstCaps *gst_dshowvideosink_get_caps (GstBaseSink * bsink, GstCaps * filter);
static GstFlowReturn gst_dshowvideosink_show_frame (GstVideoSink *sink, GstBuffer *buffer);
static void gst_dshowvideosink_set_window_for_renderer (GstDshowVideoSink *sink);
/* COM initialization/uninitialization thread */
static void gst_dshowvideosink_com_thread (GstDshowVideoSink * sink);
/* TODO: event, preroll, buffer_alloc?
* buffer_alloc won't generally be all that useful because the renderers require a
* different stride to GStreamer's implicit values.
*/
static void
gst_dshowvideosink_set_window_handle (GstVideoOverlay * overlay, guintptr window_id)
{
GstDshowVideoSink *sink = GST_DSHOWVIDEOSINK (overlay);
HWND previous_window = sink->window_id;
HWND videowindow = (HWND)window_id;
if (videowindow == sink->window_id) {
GST_DEBUG_OBJECT (sink, "Window already set");
return;
}
sink->window_id = videowindow;
/* Update window if we're already playing. */
if (sink->connected && sink->filter_media_event) {
HRESULT hres;
if (sink->is_new_window) {
/* If we created a new window */
SendMessage (previous_window, WM_CLOSE, NULL, NULL);
sink->is_new_window = FALSE;
sink->window_closed = FALSE;
} else {
/* Return control of application window */
SetWindowLongPtr (previous_window, GWLP_WNDPROC, (LONG_PTR)sink->prevWndProc);
SetWindowPos (previous_window, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
}
gst_dshowvideosink_set_window_for_renderer (sink);
hres = sink->filter_media_event->SetNotifyWindow ((OAHWND)sink->window_id, WM_GRAPH_NOTIFY, 0);
GST_DEBUG_OBJECT (sink, "SetNotifyWindow(%p) returned %x", sink->window_id, hres);
}
}
static void
gst_dshowvideosink_expose (GstVideoOverlay * overlay)
{
GstDshowVideoSink *sink = GST_DSHOWVIDEOSINK (overlay);
if (sink->renderersupport) {
sink->renderersupport->PaintWindow ();
}
}
static void
gst_dshowvideosink_videooverlay_init (GstVideoOverlayInterface * iface)
{
iface->set_window_handle = gst_dshowvideosink_set_window_handle;
iface->expose = gst_dshowvideosink_expose;
}
static void
gst_dshowvideosink_navigation_send_event (GstNavigation * navigation,
GstStructure * structure)
{
GstDshowVideoSink *sink = GST_DSHOWVIDEOSINK (navigation);
GstEvent *event = NULL;
GstPad *pad = NULL;
event = gst_event_new_navigation (structure);
/* FXIME: handle aspect ratio. */
pad = gst_pad_get_peer (GST_VIDEO_SINK_PAD (sink));
if (GST_IS_PAD (pad) && GST_IS_EVENT (event)) {
gst_pad_send_event (pad, event);
gst_object_unref (pad);
}
}
static void
gst_dshowvideosink_navigation_interface_init (GstNavigationInterface * iface)
{
/* FIXME: navigation interface partially implemented.
* Need to call gst_navigation_send_mouse_event and
* gst_navigation_send_key_event like in directdrawsink.
*/
iface->send_event = gst_dshowvideosink_navigation_send_event;
}
static void
gst_dshowvideosink_class_init (GstDshowVideoSinkClass * klass)
{
GObjectClass *o_class;
GstElementClass *e_class;
GstBaseSinkClass *bs_class;
GstVideoSinkClass *vs_class;
o_class = (GObjectClass *) klass;
e_class = (GstElementClass *) klass;
bs_class = (GstBaseSinkClass *) klass;
vs_class = (GstVideoSinkClass *) klass;
o_class->finalize = gst_dshowvideosink_finalize;
o_class->set_property = gst_dshowvideosink_set_property;
o_class->get_property = gst_dshowvideosink_get_property;
gst_element_class_set_static_metadata (e_class, "DirectShow video sink",
"Sink/Video", "Display data using a DirectShow video renderer",
"Pioneers of the Inevitable <songbird@songbirdnest.com>, " \
"FLUENDO S.A. <support@fluendo.com>");
gst_element_class_add_pad_template (e_class,
gst_static_pad_template_get (&sink_template));
e_class->change_state = GST_DEBUG_FUNCPTR (gst_dshowvideosink_change_state);
bs_class->get_caps = GST_DEBUG_FUNCPTR (gst_dshowvideosink_get_caps);
bs_class->set_caps = GST_DEBUG_FUNCPTR (gst_dshowvideosink_set_caps);
bs_class->start = GST_DEBUG_FUNCPTR (gst_dshowvideosink_start);
bs_class->stop = GST_DEBUG_FUNCPTR (gst_dshowvideosink_stop);
bs_class->unlock = GST_DEBUG_FUNCPTR (gst_dshowvideosink_unlock);
bs_class->unlock_stop =
GST_DEBUG_FUNCPTR (gst_dshowvideosink_unlock_stop);
vs_class->show_frame = GST_DEBUG_FUNCPTR (gst_dshowvideosink_show_frame);
/* Add properties */
g_object_class_install_property (G_OBJECT_CLASS (klass),
PROP_KEEP_ASPECT_RATIO, g_param_spec_boolean ("force-aspect-ratio",
"Force aspect ratio",
"When enabled, scaling will respect original aspect ratio", TRUE,
(GParamFlags)G_PARAM_READWRITE));
g_object_class_install_property (G_OBJECT_CLASS (klass),
PROP_FULL_SCREEN, g_param_spec_boolean ("fullscreen",
"Full screen mode",
"Use full-screen mode (not available when using XOverlay)", FALSE,
(GParamFlags)G_PARAM_READWRITE));
g_object_class_install_property (G_OBJECT_CLASS (klass),
PROP_RENDERER, g_param_spec_string ("renderer", "Renderer",
"Force usage of specific DirectShow renderer (EVR, VMR9 or VMR7)",
NULL, (GParamFlags)G_PARAM_READWRITE));
}
static void
gst_dshowvideosink_clear (GstDshowVideoSink *sink)
{
sink->renderersupport = NULL;
sink->fakesrc = NULL;
sink->filter_graph = NULL;
sink->filter_media_event = NULL;
sink->keep_aspect_ratio = FALSE;
sink->full_screen = FALSE;
sink->window_closed = FALSE;
sink->window_id = NULL;
sink->is_new_window = FALSE;
sink->connected = FALSE;
sink->graph_running = FALSE;
}
static void
gst_dshowvideosink_init (GstDshowVideoSink * sink)
{
gst_dshowvideosink_clear (sink);
g_mutex_init (&sink->graph_lock);
g_mutex_init (&sink->com_init_lock);
g_mutex_init (&sink->com_deinit_lock);
g_cond_init (&sink->com_initialized);
g_cond_init (&sink->com_uninitialize);
g_cond_init (&sink->com_uninitialized);
g_mutex_lock (&sink->com_init_lock);
/* create the COM initialization thread */
g_thread_new ("gstdshowvideosinkcomthread", (GThreadFunc)gst_dshowvideosink_com_thread, sink);
/* wait until the COM thread signals that COM has been initialized */
g_cond_wait (&sink->com_initialized, &sink->com_init_lock);
g_mutex_unlock (&sink->com_init_lock);
}
static void
gst_dshowvideosink_finalize (GObject * gobject)
{
GstDshowVideoSink *sink = GST_DSHOWVIDEOSINK (gobject);
if (sink->preferredrenderer)
g_free (sink->preferredrenderer);
/* signal the COM thread that it sould uninitialize COM */
if (sink->comInitialized) {
g_mutex_lock (&sink->com_deinit_lock);
g_cond_signal (&sink->com_uninitialize);
g_cond_wait (&sink->com_uninitialized, &sink->com_deinit_lock);
g_mutex_unlock (&sink->com_deinit_lock);
}
g_mutex_clear (&sink->com_init_lock);
g_mutex_clear (&sink->com_deinit_lock);
g_cond_clear (&sink->com_initialized);
g_cond_clear (&sink->com_uninitialize);
g_cond_clear (&sink->com_uninitialized);
g_mutex_clear (&sink->graph_lock);
G_OBJECT_CLASS (parent_class)->finalize (gobject);
}
static void
gst_dshowvideosink_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
GstDshowVideoSink *sink = GST_DSHOWVIDEOSINK (object);
switch (prop_id) {
case PROP_RENDERER:
if (sink->preferredrenderer)
g_free (sink->preferredrenderer);
sink->preferredrenderer = g_value_dup_string (value);
break;
case PROP_KEEP_ASPECT_RATIO:
sink->keep_aspect_ratio = g_value_get_boolean (value);
if (sink->renderersupport)
sink->renderersupport->SetAspectRatioMode();
break;
case PROP_FULL_SCREEN:
sink->full_screen = g_value_get_boolean (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_dshowvideosink_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
GstDshowVideoSink *sink = GST_DSHOWVIDEOSINK (object);
switch (prop_id) {
case PROP_RENDERER:
g_value_take_string (value, sink->preferredrenderer);
break;
case PROP_KEEP_ASPECT_RATIO:
g_value_set_boolean (value, sink->keep_aspect_ratio);
break;
case PROP_FULL_SCREEN:
g_value_set_boolean (value, sink->full_screen);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_dshowvideosink_com_thread (GstDshowVideoSink * sink)
{
HRESULT res;
g_mutex_lock (&sink->com_init_lock);
/* Initialize COM with a MTA for this process. This thread will
* be the first one to enter the apartement and the last one to leave
* it, unitializing COM properly */
res = CoInitializeEx (0, COINIT_MULTITHREADED);
if (res == S_FALSE)
GST_WARNING_OBJECT (sink, "COM has been already initialized in the same process");
else if (res == RPC_E_CHANGED_MODE)
GST_WARNING_OBJECT (sink, "The concurrency model of COM has changed.");
else
GST_INFO_OBJECT (sink, "COM intialized succesfully");
sink->comInitialized = TRUE;
/* Signal other threads waiting on this condition that COM was initialized */
g_cond_signal (&sink->com_initialized);
g_mutex_unlock (&sink->com_init_lock);
/* Wait until the unitialize condition is met to leave the COM apartement */
g_mutex_lock (&sink->com_deinit_lock);
g_cond_wait (&sink->com_uninitialize, &sink->com_deinit_lock);
CoUninitialize ();
GST_INFO_OBJECT (sink, "COM unintialized succesfully");
sink->comInitialized = FALSE;
g_cond_signal (&sink->com_uninitialized);
g_mutex_unlock (&sink->com_deinit_lock);
}
static GstCaps *
gst_dshowvideosink_get_caps (GstBaseSink * basesink, GstCaps * filter)
{
GstDshowVideoSink *sink = GST_DSHOWVIDEOSINK (basesink);
GstCaps *ret = NULL;
return ret;
}
static void dump_available_media_types (IPin *pin)
{
/* Enumerate all media types on this pin, output info about them */
IEnumMediaTypes *enumerator = NULL;
AM_MEDIA_TYPE *type;
GstCaps *caps;
int i = 0;
GST_INFO ("Enumerating media types on pin %p", pin);
pin->EnumMediaTypes (&enumerator);
while (enumerator->Next (1, &type, NULL) == S_OK) {
i++;
caps = gst_directshow_media_type_to_caps (type);
if (caps) {
gchar *str = gst_caps_to_string (caps);
GST_INFO ("Type %d: converted to caps \"%s\"", i, str);
g_free (str);
gst_caps_unref (caps);
}
else
GST_INFO ("Failed to convert type to GstCaps");
DeleteMediaType (type);
}
GST_INFO ("Enumeration complete");
enumerator->Release();
}
static void
dump_all_pin_media_types (IBaseFilter *filter)
{
IEnumPins *enumpins = NULL;
IPin *pin = NULL;
HRESULT hres;
hres = filter->EnumPins (&enumpins);
if (FAILED(hres)) {
GST_WARNING ("Cannot enumerate pins on filter");
return;
}
GST_INFO ("Enumerating pins on filter %p", filter);
while (enumpins->Next (1, &pin, NULL) == S_OK)
{
IMemInputPin *meminputpin;
PIN_DIRECTION pindir;
hres = pin->QueryDirection (&pindir);
GST_INFO ("Found a pin with direction: %s", (pindir == PINDIR_INPUT)? "input": "output");
dump_available_media_types (pin);
hres = pin->QueryInterface (
IID_IMemInputPin, (void **) &meminputpin);
if (hres == S_OK) {
GST_INFO ("Pin is a MemInputPin (push mode): %p", meminputpin);
meminputpin->Release();
}
else
GST_INFO ("Pin is not a MemInputPin (pull mode?): %p", pin);
pin->Release();
}
enumpins->Release();
}
gboolean
gst_dshow_get_pin_from_filter (IBaseFilter *filter, PIN_DIRECTION pindir, IPin **pin)
{
gboolean ret = FALSE;
IEnumPins *enumpins = NULL;
IPin *pintmp = NULL;
HRESULT hres;
*pin = NULL;
hres = filter->EnumPins (&enumpins);
if (FAILED(hres)) {
return ret;
}
while (enumpins->Next (1, &pintmp, NULL) == S_OK)
{
PIN_DIRECTION pindirtmp;
hres = pintmp->QueryDirection (&pindirtmp);
if (hres == S_OK && pindir == pindirtmp) {
*pin = pintmp;
ret = TRUE;
break;
}
pintmp->Release ();
}
enumpins->Release ();
return ret;
}
static void
gst_dshowvideosink_handle_event (GstDshowVideoSink *sink)
{
if (sink->filter_media_event) {
long evCode;
LONG_PTR param1, param2;
while (SUCCEEDED (sink->filter_media_event->GetEvent(&evCode, ¶m1, ¶m2, 0)))
{
GST_INFO_OBJECT (sink, "Received DirectShow graph event code 0x%x", evCode);
sink->filter_media_event->FreeEventParams(evCode, param1, param2);
}
}
}
/* WNDPROC for application-supplied windows */
LRESULT APIENTRY WndProcHook (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
/* Handle certain actions specially on the window passed to us.
* Then forward back to the original window.
*/
GstDshowVideoSink *sink = (GstDshowVideoSink *)GetProp (hWnd, (LPCSTR)"GstDShowVideoSink");
g_assert (sink != NULL);
switch (message) {
case WM_GRAPH_NOTIFY:
gst_dshowvideosink_handle_event (sink);
return 0;
case WM_PAINT:
sink->renderersupport->PaintWindow ();
break;
case WM_MOVE:
case WM_SIZE:
sink->renderersupport->MoveWindow ();
break;
case WM_DISPLAYCHANGE:
sink->renderersupport->DisplayModeChanged();
break;
case WM_ERASEBKGND:
/* DirectShow docs recommend ignoring this message to avoid flicker */
return TRUE;
case WM_CLOSE:
sink->window_closed = TRUE;
}
return CallWindowProc (sink->prevWndProc, hWnd, message, wParam, lParam);
}
/* WndProc for our default window, if the application didn't supply one */
LRESULT APIENTRY
WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
GstDshowVideoSink *sink = (GstDshowVideoSink *)GetWindowLongPtr (hWnd, GWLP_USERDATA);
if (!sink) {
/* I think these happen before we have a chance to set our userdata pointer */
GST_DEBUG ("No sink!");
return DefWindowProc (hWnd, message, wParam, lParam);
}
//GST_DEBUG_OBJECT (sink, "Got a window message for %x, %x", hWnd, message);
switch (message) {
case WM_GRAPH_NOTIFY:
GST_LOG_OBJECT (sink, "GRAPH_NOTIFY WINDOW MESSAGE");
gst_dshowvideosink_handle_event (sink);
return 0;
case WM_PAINT:
sink->renderersupport->PaintWindow ();
break;
case WM_MOVE:
case WM_SIZE:
sink->renderersupport->MoveWindow ();
break;
case WM_DISPLAYCHANGE:
sink->renderersupport->DisplayModeChanged();
break;
case WM_ERASEBKGND:
/* DirectShow docs recommend ignoring this message */
return TRUE;
case WM_CLOSE:
sink->renderersupport->DestroyWindow ();
sink->window_closed = TRUE;
PostQuitMessage (WM_QUIT);
return 0;
}
return DefWindowProc (hWnd, message, wParam, lParam);
}
static gpointer
gst_dshowvideosink_window_thread (GstDshowVideoSink * sink)
{
WNDCLASS WndClass;
int width, height;
int offx, offy;
DWORD exstyle, style;
memset (&WndClass, 0, sizeof (WNDCLASS));
WndClass.style = CS_HREDRAW | CS_VREDRAW;
WndClass.hInstance = GetModuleHandle (NULL);
WndClass.lpszClassName = (LPCSTR)"GST-DShowSink";
WndClass.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.lpfnWndProc = WndProc;
WndClass.hCursor = LoadCursor (NULL, IDC_ARROW);
RegisterClass (&WndClass);
if (sink->full_screen) {
/* This doesn't seem to work, it returns the wrong values! But when we
* later use ShowWindow to show it maximized, it goes to full-screen
* anyway. TODO: Figure out why. */
width = GetSystemMetrics (SM_CXFULLSCREEN);
height = GetSystemMetrics (SM_CYFULLSCREEN);
offx = 0;
offy = 0;
style = WS_POPUP; /* No window decorations */
exstyle = 0;
}
else {
/* By default, create a normal top-level window, the size
* of the video.
*/
RECT rect;
AM_MEDIA_TYPE pmt = (AM_MEDIA_TYPE)sink->mediatype;
VIDEOINFOHEADER *vi = (VIDEOINFOHEADER *)pmt.pbFormat;
if (vi == NULL)
{
GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND, ("Unknown media format"), (NULL));
return NULL;
}
/* rcTarget is the aspect-ratio-corrected size of the video. */
width = vi->rcTarget.right + GetSystemMetrics (SM_CXSIZEFRAME) * 2;
height = vi->rcTarget.bottom + GetSystemMetrics (SM_CYCAPTION) +
(GetSystemMetrics (SM_CYSIZEFRAME) * 2);
SystemParametersInfo (SPI_GETWORKAREA, NULL, &rect, 0);
int screenwidth = rect.right - rect.left;
int screenheight = rect.bottom - rect.top;
offx = rect.left;
offy = rect.top;
/* Make it fit into the screen without changing the
* aspect ratio. */
if (width > screenwidth) {
double ratio = (double)screenwidth/(double)width;
width = screenwidth;
height = (int)(height * ratio);
}
if (height > screenheight) {
double ratio = (double)screenheight/(double)height;
height = screenheight;
width = (int)(width * ratio);
}
style = WS_OVERLAPPEDWINDOW; /* Normal top-level window */
exstyle = 0;
}
HWND video_window = CreateWindowEx (exstyle, (LPCSTR)"GST-DShowSink",
(LPCSTR)"GStreamer DirectShow sink default window",
style, offx, offy, width, height, NULL, NULL,
WndClass.hInstance, NULL);
if (video_window == NULL) {
GST_ERROR_OBJECT (sink, "Failed to create window!");
return NULL;
}
sink->is_new_window = TRUE;
SetWindowLongPtr (video_window, GWLP_USERDATA, (LONG_PTR)sink);
sink->window_id = video_window;
/* signal application we created a window */
gst_video_overlay_got_window_handle (GST_VIDEO_OVERLAY (sink),
(gulong)video_window);
/* Set the renderer's clipping window */
if (!sink->renderersupport->SetRendererWindow (video_window)) {
GST_WARNING_OBJECT (sink, "Failed to set video clipping window on filter %p", sink->renderersupport);
}
/* Now show the window, as appropriate */
if (sink->full_screen) {
ShowWindow (video_window, SW_SHOWMAXIMIZED);
ShowCursor (FALSE);
}
else
ShowWindow (video_window, SW_SHOWNORMAL);
/* Trigger the initial paint of the window */
UpdateWindow (video_window);
ReleaseSemaphore (sink->window_created_signal, 1, NULL);
/* start message loop processing our default window messages */
while (1) {
MSG msg;
if (GetMessage (&msg, video_window, 0, 0) <= 0) {
GST_LOG_OBJECT (sink, "our window received WM_QUIT or error.");
break;
}
DispatchMessage (&msg);
}
return NULL;
}
static gboolean
gst_dshowvideosink_create_default_window (GstDshowVideoSink * sink)
{
sink->window_created_signal = CreateSemaphore (NULL, 0, 1, NULL);
if (sink->window_created_signal == NULL)
goto failed;
sink -> window_thread = g_thread_new ("windowthread",
(GThreadFunc) gst_dshowvideosink_window_thread,
sink);
/* wait maximum 10 seconds for window to be created */
if (WaitForSingleObject (sink->window_created_signal,
10000) != WAIT_OBJECT_0)
goto failed;
CloseHandle (sink->window_created_signal);
return TRUE;
failed:
CloseHandle (sink->window_created_signal);
GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
("Error creating our default window"), (NULL));
return FALSE;
}
static void gst_dshowvideosink_set_window_for_renderer (GstDshowVideoSink *sink)
{
WNDPROC prevWndProc = (WNDPROC)GetWindowLongPtr (sink->window_id, GWLP_WNDPROC);
if (prevWndProc == WndProcHook) {
/* The WndProc already points to our hook. Something has gone wrong
* somewhere else and this safety net prevents an infinite recursion */
return;
}
/* Application has requested a specific window ID */
sink->prevWndProc = (WNDPROC) SetWindowLongPtr (sink->window_id, GWLP_WNDPROC, (LONG_PTR)WndProcHook);
GST_DEBUG_OBJECT (sink, "Set wndproc to %p from %p", WndProcHook, sink->prevWndProc);
SetProp (sink->window_id, (LPCSTR)"GstDShowVideoSink", sink);
/* This causes the new WNDPROC to become active */
SetWindowPos (sink->window_id, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
if (!sink->renderersupport->SetRendererWindow (sink->window_id)) {
GST_WARNING_OBJECT (sink, "Failed to set HWND %x on renderer", sink->window_id);
return;
}
sink->is_new_window = FALSE;
/* This tells the renderer where the window is located, needed to
* start drawing in the right place. */
sink->renderersupport->MoveWindow();
GST_INFO_OBJECT (sink, "Set renderer window to %x", sink->window_id);
}
static void
gst_dshowvideosink_prepare_window (GstDshowVideoSink *sink)
{
HRESULT hres;
/* Give the app a last chance to supply a window id */
if (!sink->window_id) {
gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (sink));
}
/* If the app supplied one, use it. Otherwise, go ahead
* and create (and use) our own window */
if (sink->window_id) {
gst_dshowvideosink_set_window_for_renderer (sink);
}
else {
gst_dshowvideosink_create_default_window (sink);
}
if (sink->filter_media_event) {
sink->filter_media_event->Release();
sink->filter_media_event = NULL;
}
hres = sink->filter_graph->QueryInterface(
IID_IMediaEventEx, (void **) &sink->filter_media_event);
if (FAILED (hres)) {
GST_WARNING_OBJECT (sink, "Failed to get IMediaEventEx");
}
else {
hres = sink->filter_media_event->SetNotifyWindow ((OAHWND)sink->window_id,
WM_GRAPH_NOTIFY, 0);
GST_DEBUG_OBJECT (sink, "SetNotifyWindow(%p) returned %x", sink->window_id, hres);
}
}
static gboolean
gst_dshowvideosink_connect_graph (GstDshowVideoSink *sink)
{
HRESULT hres;
IPin *srcpin;
IPin *sinkpin;
GST_INFO_OBJECT (sink, "Connecting DirectShow pins");
srcpin = sink->fakesrc->GetOutputPin();
gst_dshow_get_pin_from_filter (sink->renderersupport->GetFilter(), PINDIR_INPUT,
&sinkpin);
if (!sinkpin) {
GST_WARNING_OBJECT (sink, "Cannot get input pin from Renderer");
return FALSE;
}
/* Be warned that this call WILL deadlock unless you call it from
* the main thread. Thus, we call this from the state change, not from
* setcaps (which happens in a streaming thread).
*/
hres = sink->filter_graph->ConnectDirect (
srcpin, sinkpin, NULL);
if (FAILED (hres)) {
GST_WARNING_OBJECT (sink, "Could not connect pins: %x", hres);
sinkpin->Release();
return FALSE;
}
sinkpin->Release();
return TRUE;
}
static GstStateChangeReturn
gst_dshowvideosink_start_graph (GstDshowVideoSink *sink)
{
IMediaControl *control = NULL;
HRESULT hres;
GstStateChangeReturn ret;
GST_DEBUG_OBJECT (sink, "Connecting and starting DirectShow graph");
hres = sink->filter_graph->QueryInterface(
IID_IMediaControl, (void **) &control);
if (FAILED (hres)) {
GST_WARNING_OBJECT (sink, "Failed to get IMediaControl interface");
ret = GST_STATE_CHANGE_FAILURE;
goto done;
}
GST_INFO_OBJECT (sink, "Running DirectShow graph");
hres = control->Run();
if (FAILED (hres)) {
GST_ERROR_OBJECT (sink,
"Failed to run the directshow graph (error=%x)", hres);
ret = GST_STATE_CHANGE_FAILURE;
goto done;
}
GST_DEBUG_OBJECT (sink, "DirectShow graph is now running");
ret = GST_STATE_CHANGE_SUCCESS;
done:
if (control)
control->Release();
return ret;
}
static GstStateChangeReturn
gst_dshowvideosink_pause_graph (GstDshowVideoSink *sink)
{
IMediaControl *control = NULL;
GstStateChangeReturn ret;
HRESULT hres;
hres = sink->filter_graph->QueryInterface(
IID_IMediaControl, (void **) &control);
if (FAILED (hres)) {
GST_WARNING_OBJECT (sink, "Failed to get IMediaControl interface");
ret = GST_STATE_CHANGE_FAILURE;
goto done;
}
GST_INFO_OBJECT (sink, "Pausing DirectShow graph");
hres = control->Pause();
if (FAILED (hres)) {
GST_WARNING_OBJECT (sink,
"Can't pause the directshow graph (error=%x)", hres);
ret = GST_STATE_CHANGE_FAILURE;
goto done;
}
ret = GST_STATE_CHANGE_SUCCESS;
done:
if (control)
control->Release();
return ret;
}
static GstStateChangeReturn
gst_dshowvideosink_stop_graph (GstDshowVideoSink *sink)
{
IMediaControl *control = NULL;
GstStateChangeReturn ret;
HRESULT hres;
IPin *sinkpin;
hres = sink->filter_graph->QueryInterface(
IID_IMediaControl, (void **) &control);
if (FAILED (hres)) {
GST_WARNING_OBJECT (sink, "Failed to get IMediaControl interface");
ret = GST_STATE_CHANGE_FAILURE;
goto done;
}
GST_INFO_OBJECT (sink, "Stopping DirectShow graph");
hres = control->Stop();
if (FAILED (hres)) {
GST_WARNING_OBJECT (sink,
"Can't stop the directshow graph (error=%x)", hres);
ret = GST_STATE_CHANGE_FAILURE;
goto done;
}
sink->filter_graph->Disconnect(sink->fakesrc->GetOutputPin());
gst_dshow_get_pin_from_filter (sink->renderersupport->GetFilter(), PINDIR_INPUT,
&sinkpin);
sink->filter_graph->Disconnect(sinkpin);
sinkpin->Release();
GST_DEBUG_OBJECT (sink, "DirectShow graph has stopped");
if (sink->window_id) {
/* Return control of application window */
SetWindowLongPtr (sink->window_id, GWLP_WNDPROC, (LONG_PTR)sink->prevWndProc);
RemoveProp (sink->window_id, (LPCSTR)"GstDShowVideoSink");
SetWindowPos (sink->window_id, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
sink->prevWndProc = NULL;
}
sink->connected = FALSE;
ret = GST_STATE_CHANGE_SUCCESS;
done:
if (control)
control->Release();
return ret;
}
static GstStateChangeReturn
gst_dshowvideosink_change_state (GstElement * element, GstStateChange transition)
{
GstDshowVideoSink *sink = GST_DSHOWVIDEOSINK (element);
GstStateChangeReturn ret, rettmp;
switch (transition) {
case GST_STATE_CHANGE_NULL_TO_READY:
break;
case GST_STATE_CHANGE_READY_TO_PAUSED:
break;
case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
ret = gst_dshowvideosink_start_graph (sink);
if (ret == GST_STATE_CHANGE_FAILURE)
return ret;
sink->graph_running = TRUE;
break;
}
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
switch (transition) {
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
GST_DSHOWVIDEOSINK_GRAPH_LOCK(sink);
rettmp = gst_dshowvideosink_pause_graph (sink);
if (rettmp == GST_STATE_CHANGE_FAILURE)
ret = rettmp;
sink->graph_running = FALSE;
GST_DSHOWVIDEOSINK_GRAPH_UNLOCK(sink);
break;
case GST_STATE_CHANGE_PAUSED_TO_READY:
GST_DSHOWVIDEOSINK_GRAPH_LOCK(sink);
rettmp = gst_dshowvideosink_stop_graph (sink);
if (rettmp == GST_STATE_CHANGE_FAILURE)
ret = rettmp;
sink->graph_running = FALSE;
GST_DSHOWVIDEOSINK_GRAPH_UNLOCK(sink);
break;
case GST_STATE_CHANGE_READY_TO_NULL:
gst_dshowvideosink_clear (sink);
break;
}
return ret;
}
class EVRSupport : public RendererSupport
{
private:
GstDshowVideoSink *sink;
IBaseFilter *filter;
IMFGetService *service;
IMFVideoDisplayControl *control;
HWND video_window;
public:
EVRSupport (GstDshowVideoSink *sink) :
sink(sink),
filter(NULL),
service(NULL),
control(NULL)
{
}
~EVRSupport() {
if (control)
control->Release();
if (service)
service->Release();
if (filter)
filter->Release();
}
const char *GetName() {
return "EnhancedVideoRenderer";
}
IBaseFilter *GetFilter() {
return filter;
}
gboolean CheckOS () {
OSVERSIONINFO info;
info.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
GetVersionEx (&info);
if (info.dwMajorVersion < 6) {
return false;
}
else {
return true;
}
}
gboolean Configure() {
HRESULT hres;
if (!this->CheckOS ()) {
GST_DEBUG_OBJECT (sink, "Windows Vista is required at least for EVR to work");
return FALSE;
}
hres = CoCreateInstance (CLSID_EnhancedVideoRenderer, NULL, CLSCTX_INPROC,
IID_IBaseFilter, (LPVOID *) &filter);
GST_DEBUG_OBJECT (sink, "cocreateinstance returned %d", hres);
if (FAILED (hres)) {
GST_ERROR_OBJECT (sink,
"Can't create an instance of renderer (error=%x)",
hres);
return FALSE;
}
hres = filter->QueryInterface (IID_IMFGetService,
(void **) &service);
if (FAILED (hres)) {
GST_WARNING_OBJECT (sink, "EVR service interface missing: %x", hres);
return FALSE;
}
hres = service->GetService (MR_VIDEO_RENDER_SERVICE,
IID_IMFVideoDisplayControl, (void **) &control);
if (FAILED (hres)) {
GST_WARNING_OBJECT (sink, "EVR control service missing: %x", hres);
return FALSE;
}
SetAspectRatioMode();
return TRUE;
}
void SetAspectRatioMode() {
if (sink->keep_aspect_ratio) {
control->SetAspectRatioMode(MFVideoARMode_PreservePicture);
}
else {
control->SetAspectRatioMode(MFVideoARMode_None);
}
}
gboolean SetRendererWindow(HWND window) {
video_window = window;
HRESULT hres = control->SetVideoWindow (video_window);
if (FAILED (hres)) {
GST_WARNING_OBJECT (sink, "Failed to set video clipping window on filter %p: %x", filter, hres);
return FALSE;
}
return TRUE;
}
void PaintWindow()
{
HRESULT hr;
PAINTSTRUCT ps;
HDC hdc;
RECT rcClient;
GetClientRect(video_window, &rcClient);
hdc = BeginPaint(video_window, &ps);
hr = control->RepaintVideo();
EndPaint(video_window, &ps);
}
void MoveWindow()
{
HRESULT hr;
RECT rect;
// Track the movement of the container window and resize as needed
GetClientRect(video_window, &rect);
hr = control->SetVideoPosition(NULL, &rect);
}
void DisplayModeChanged() {
}
void DestroyWindow() {
::DestroyWindow (video_window);
}
};
class VMR9Support : public RendererSupport
{
private:
GstDshowVideoSink *sink;
IBaseFilter *filter;
IVMRWindowlessControl9 *control;
IVMRFilterConfig9 *config;
HWND video_window;
public:
VMR9Support (GstDshowVideoSink *sink) :
sink(sink),
filter(NULL),
control(NULL),
config(NULL)
{
}
~VMR9Support() {
if (control)
control->Release();
if (config)
config->Release();
if (filter)
filter->Release();
}
const char *GetName() {
return "VideoMixingRenderer9";
}
IBaseFilter *GetFilter() {
return filter;
}
gboolean Configure() {
HRESULT hres;
hres = CoCreateInstance (CLSID_VideoMixingRenderer9, NULL, CLSCTX_INPROC,
IID_IBaseFilter, (LPVOID *) &filter);
if (FAILED (hres)) {
GST_ERROR_OBJECT (sink,
"Can't create an instance of renderer (error=%x)",
hres);
return FALSE;
}
hres = filter->QueryInterface (
IID_IVMRFilterConfig9, (void **) &config);
if (FAILED (hres)) {
GST_WARNING_OBJECT (sink, "VMR9 filter config interface missing: %x", hres);
return FALSE;
}
hres = config->SetRenderingMode (VMR9Mode_Windowless);
if (FAILED (hres)) {
GST_WARNING_OBJECT (sink, "VMR9 couldn't be set to windowless mode: %x", hres);
return FALSE;
}
else {
GST_DEBUG_OBJECT (sink, "Set VMR9 (%p) to windowless mode!", filter);
}
/* We can't QI to this until _after_ we've been set to windowless mode.
* Apparently this is against the rules in COM, but that's how it is... */
hres = filter->QueryInterface (
IID_IVMRWindowlessControl9, (void **) &control);
if (FAILED (hres)) {
GST_WARNING_OBJECT (sink, "VMR9 windowless control interface missing: %x", hres);
return FALSE;
}
SetAspectRatioMode();
return TRUE;
}
void SetAspectRatioMode() {
if (sink->keep_aspect_ratio) {
control->SetAspectRatioMode(VMR9ARMode_LetterBox);
}
else {
control->SetAspectRatioMode(VMR9ARMode_None);
}
}
gboolean SetRendererWindow(HWND window) {
video_window = window;
HRESULT hres = control->SetVideoClippingWindow (video_window);
if (FAILED (hres)) {
GST_WARNING_OBJECT (sink, "Failed to set video clipping window on filter %p: %x", filter, hres);
return FALSE;
}
return TRUE;
}
void PaintWindow()
{
HRESULT hr;
PAINTSTRUCT ps;
HDC hdc;
RECT rcClient;
GetClientRect(video_window, &rcClient);
hdc = BeginPaint(video_window, &ps);
hr = control->RepaintVideo(video_window, hdc);
EndPaint(video_window, &ps);
}
void MoveWindow()
{
HRESULT hr;
RECT rect;
// Track the movement of the container window and resize as needed
GetClientRect(video_window, &rect);
hr = control->SetVideoPosition(NULL, &rect);
}
void DisplayModeChanged() {
control->DisplayModeChanged();
}
void DestroyWindow() {
::DestroyWindow (video_window);
}
};
class VMR7Support : public RendererSupport
{
private:
GstDshowVideoSink *sink;
IBaseFilter *filter;
IVMRWindowlessControl *control;
IVMRFilterConfig *config;
HWND video_window;
public:
VMR7Support (GstDshowVideoSink *sink) :
sink(sink),
filter(NULL),
control(NULL),
config(NULL)
{
}
~VMR7Support() {
if (control)
control->Release();
if (config)
config->Release();
if (filter)
filter->Release();
}
const char *GetName() {
return "VideoMixingRenderer";
}
IBaseFilter *GetFilter() {
return filter;
}
gboolean Configure() {
HRESULT hres;
hres = CoCreateInstance (CLSID_VideoMixingRenderer, NULL, CLSCTX_INPROC,
IID_IBaseFilter, (LPVOID *) &filter);
if (FAILED (hres)) {
GST_ERROR_OBJECT (sink,
"Can't create an instance of renderer (error=%x)",
hres);
return FALSE;
}
hres = filter->QueryInterface (
IID_IVMRFilterConfig, (void **) &config);
if (FAILED (hres)) {
GST_WARNING_OBJECT (sink, "VMR filter config interface missing: %x", hres);
return FALSE;
}
hres = config->SetRenderingMode (VMRMode_Windowless);
if (FAILED (hres)) {
GST_WARNING_OBJECT (sink, "VMR couldn't be set to windowless mode: %x", hres);
return FALSE;
}
else {
GST_DEBUG_OBJECT (sink, "Set VMR (%p) to windowless mode!", filter);
}
hres = filter->QueryInterface (
IID_IVMRWindowlessControl, (void **) &control);
if (FAILED (hres)) {
GST_WARNING_OBJECT (sink, "VMR windowless control interface missing: %x", hres);
return FALSE;
}
SetAspectRatioMode();
return TRUE;
}
void SetAspectRatioMode() {
if (sink->keep_aspect_ratio) {
control->SetAspectRatioMode(VMR_ARMODE_LETTER_BOX);
}
else {
control->SetAspectRatioMode(VMR_ARMODE_NONE);
}
}
gboolean SetRendererWindow(HWND window) {
video_window = window;
HRESULT hres = control->SetVideoClippingWindow (video_window);
if (FAILED (hres)) {
GST_WARNING_OBJECT (sink, "Failed to set video clipping window on filter %p: %x", filter, hres);
return FALSE;
}
return TRUE;
}
void PaintWindow()
{
HRESULT hr;
PAINTSTRUCT ps;
HDC hdc;
RECT rcClient;
GetClientRect(video_window, &rcClient);
hdc = BeginPaint(video_window, &ps);
hr = control->RepaintVideo(video_window, hdc);
EndPaint(video_window, &ps);
}
void MoveWindow()
{
HRESULT hr;
RECT rect;
// Track the movement of the container window and resize as needed
GetClientRect(video_window, &rect);
hr = control->SetVideoPosition(NULL, &rect);
}
void DisplayModeChanged() {
control->DisplayModeChanged();
}
void DestroyWindow() {
::DestroyWindow (video_window);
}
};
static gboolean
gst_dshowvideosink_create_renderer (GstDshowVideoSink *sink)
{
GST_DEBUG_OBJECT (sink, "Trying to create renderer '%s'", "EVR");
RendererSupport *support = NULL;
if (sink->preferredrenderer) {
if (!strcmp (sink->preferredrenderer, "EVR")) {
GST_INFO_OBJECT (sink, "Forcing use of EVR");
support = new EVRSupport (sink);
}
else if (!strcmp (sink->preferredrenderer, "VMR9")) {
GST_INFO_OBJECT (sink, "Forcing use of VMR9");
support = new VMR9Support (sink);
}
else if (!strcmp (sink->preferredrenderer, "VMR")) {
GST_INFO_OBJECT (sink, "Forcing use of VMR");
support = new VMR7Support (sink);
}
else {
GST_ERROR_OBJECT (sink, "Unknown sink type '%s'", sink->preferredrenderer);
return FALSE;
}
if (!support->Configure()) {
GST_ERROR_OBJECT (sink, "Couldn't configure selected renderer");
delete support;
return FALSE;
}
goto done;
}
support = new EVRSupport (sink);
if (!support->Configure ()) {
GST_INFO_OBJECT (sink, "Failed to configure EVR, trying VMR9");
delete support;
support = new VMR9Support (sink);
if (!support->Configure()) {
GST_INFO_OBJECT (sink, "Failed to configure VMR9, trying VMR7");
delete support;
support = new VMR7Support (sink);
if (!support->Configure()) {
GST_ERROR_OBJECT (sink, "Failed to configure VMR9 or VMR7");
delete support;
return FALSE;
}
}
}
done:
sink->renderersupport = support;
return TRUE;
}
static gboolean
gst_dshowvideosink_build_filtergraph (GstDshowVideoSink *sink)
{
HRESULT hres;
/* Build our DirectShow FilterGraph, looking like:
*
* [ fakesrc ] -> [ sink filter ]
*
* so we can feed data in through the fakesrc.
*
* The sink filter can be one of our supported filters: VMR9 (VMR7?, EMR?)
*/
hres = CoCreateInstance (CLSID_FilterGraph, NULL, CLSCTX_INPROC,
IID_IFilterGraph, (LPVOID *) & sink->filter_graph);
if (FAILED (hres)) {
GST_ERROR_OBJECT (sink,
"Can't create an instance of the dshow graph manager (error=%x)", hres);
goto error;
}
sink->fakesrc = new VideoFakeSrc();
IBaseFilter *filter;
hres = sink->fakesrc->QueryInterface (
IID_IBaseFilter, (void **) &filter);
if (FAILED (hres)) {
GST_ERROR_OBJECT (sink, "Could not QI fakesrc to IBaseFilter");
goto error;
}
hres = sink->filter_graph->AddFilter (filter, L"fakesrc");
if (FAILED (hres)) {
GST_ERROR_OBJECT (sink,
"Can't add our fakesrc filter to the graph (error=%x)", hres);
goto error;
}
if (!gst_dshowvideosink_create_renderer (sink)) {
GST_ERROR_OBJECT (sink, "Could not create a video renderer");
goto error;
}
/* dump_all_pin_media_types (sink->renderer); */
hres =
sink->filter_graph->AddFilter (sink->renderersupport->GetFilter(),
L"renderer");
if (FAILED (hres)) {
GST_ERROR_OBJECT (sink,
"Can't add renderer to the graph (error=%x)", hres);
goto error;
}
return TRUE;
error:
if (sink->fakesrc) {
sink->fakesrc->Release();
sink->fakesrc = NULL;
}
if (sink->filter_graph) {
sink->filter_graph->Release();
sink->filter_graph = NULL;
}
if (sink->filter_media_event) {
sink->filter_media_event->Release();
sink->filter_media_event = NULL;
}
return FALSE;
}
static gboolean
gst_dshowvideosink_start (GstBaseSink * bsink)
{
HRESULT hres = S_FALSE;
GstDshowVideoSink *sink = GST_DSHOWVIDEOSINK (bsink);
/* Just build the filtergraph; we don't link or otherwise configure it yet */
return gst_dshowvideosink_build_filtergraph (sink);
}
static gboolean
gst_dshowvideosink_set_caps (GstBaseSink * bsink, GstCaps * caps)
{
GstDshowVideoSink *sink = GST_DSHOWVIDEOSINK (bsink);
if (sink->connected) {
IPin *sinkpin;
sink->filter_graph->Disconnect(sink->fakesrc->GetOutputPin());
gst_dshow_get_pin_from_filter (sink->renderersupport->GetFilter(), PINDIR_INPUT,
&sinkpin);
sink->filter_graph->Disconnect(sinkpin);
sinkpin->Release();
}
if (!gst_caps_to_directshow_media_type (sink, caps, &sink->mediatype)) {
GST_WARNING_OBJECT (sink, "Cannot convert caps to AM_MEDIA_TYPE, rejecting");
return FALSE;
}
GST_DEBUG_OBJECT (sink, "Configuring output pin media type");
/* Now we have an AM_MEDIA_TYPE describing what we're going to send.
* We set this on our DirectShow fakesrc's output pin.
*/
sink->fakesrc->GetOutputPin()->SetMediaType (&sink->mediatype);
GST_DEBUG_OBJECT (sink, "Configured output pin media type");
/* We have configured the ouput pin media type.
* So, create a window (or start using an application-supplied
* one, then connect the graph */
gst_dshowvideosink_prepare_window (sink);
if (!gst_dshowvideosink_connect_graph (sink)) {
GST_ELEMENT_ERROR (sink, CORE, NEGOTIATION,
("Failed to initialize DirectShow graph with the input caps"), (NULL));
return FALSE;
}
sink->connected = TRUE;
return TRUE;
}
static gboolean
gst_dshowvideosink_stop (GstBaseSink * bsink)
{
IPin *input_pin = NULL, *output_pin = NULL;
HRESULT hres = S_FALSE;
GstDshowVideoSink *sink = GST_DSHOWVIDEOSINK (bsink);
if (!sink->filter_graph) {
GST_WARNING_OBJECT (sink, "Cannot destroy filter graph; it doesn't exist");
return TRUE;
}
/* If we created a new window, send the close message and wait until
* it's closed in the window thread */
if (sink->is_new_window) {
SendMessage (sink->window_id, WM_CLOSE, NULL, NULL);
while (!sink->window_closed);
sink->is_new_window = FALSE;
}
/* Release the renderer */
if (sink->renderersupport) {
delete sink->renderersupport;
sink->renderersupport = NULL;
}
/* Release our dshow fakesrc */
if (sink->fakesrc) {
sink->fakesrc->Release();
sink->fakesrc = NULL;
}
/* Release the filter graph manager */
if (sink->filter_graph) {
sink->filter_graph->Release();
sink->filter_graph = NULL;
}
if (sink->filter_media_event) {
sink->filter_media_event->Release();
sink->filter_media_event = NULL;
}
return TRUE;
}
static GstFlowReturn
gst_dshowvideosink_show_frame (GstVideoSink *vsink, GstBuffer *buffer)
{
GstDshowVideoSink *sink = GST_DSHOWVIDEOSINK (vsink);
GstFlowReturn ret;
GstStateChangeReturn retst;
if (sink->window_closed) {
GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND, ("Output window was closed"), (NULL));
return GST_FLOW_ERROR;
}
GST_DEBUG_OBJECT (sink, "Pushing buffer through fakesrc->renderer");
GST_DSHOWVIDEOSINK_GRAPH_LOCK(sink);
if (!sink->graph_running){
retst = gst_dshowvideosink_start_graph(sink);
if (retst == GST_STATE_CHANGE_FAILURE)
return GST_FLOW_FLUSHING;
}
ret = sink->fakesrc->GetOutputPin()->PushBuffer (buffer);
if (!sink->graph_running){
retst = gst_dshowvideosink_pause_graph(sink);
if (retst == GST_STATE_CHANGE_FAILURE)
return GST_FLOW_FLUSHING;
}
GST_DSHOWVIDEOSINK_GRAPH_UNLOCK(sink);
GST_DEBUG_OBJECT (sink, "Done pushing buffer through fakesrc->renderer: %s", gst_flow_get_name(ret));
return ret;
}
/* TODO: How can we implement these? Figure that out... */
static gboolean
gst_dshowvideosink_unlock (GstBaseSink * bsink)
{
GstDshowVideoSink *sink = GST_DSHOWVIDEOSINK (bsink);
return TRUE;
}
static gboolean
gst_dshowvideosink_unlock_stop (GstBaseSink * bsink)
{
GstDshowVideoSink *sink = GST_DSHOWVIDEOSINK (bsink);
return TRUE;
}
/* TODO: Move all of this into generic code? */
/* Helpers to format GUIDs the same way we find them in the source */
#define GUID_FORMAT "{%.8x, %.4x, %.4x, { %.2x, %.2x, %.2x, %.2x, %.2x, %.2x, %.2x, %.2x }}"
#define GUID_ARGS(guid) \
guid.Data1, guid.Data2, guid.Data3, \
guid.Data4[0], guid.Data4[1], guid.Data4[3], guid.Data4[4], \
guid.Data4[5], guid.Data4[6], guid.Data4[7], guid.Data4[8]
static GstCaps *
audio_media_type_to_caps (AM_MEDIA_TYPE *mediatype)
{
return NULL;
}
static GstCaps *
video_media_type_to_caps (AM_MEDIA_TYPE *mediatype)
{
GstCaps *caps = NULL;
/* TODO: Add RGB types. */
if (IsEqualGUID (mediatype->subtype, MEDIASUBTYPE_YUY2))
caps = gst_caps_new_simple ("video/x-raw",
"format", GST_TYPE_VIDEO_FORMAT, GST_VIDEO_FORMAT_YUY2, NULL);
else if (IsEqualGUID (mediatype->subtype, MEDIASUBTYPE_UYVY))
caps = gst_caps_new_simple ("video/x-raw",
"format", GST_TYPE_VIDEO_FORMAT, GST_VIDEO_FORMAT_UYVY, NULL);
else if (IsEqualGUID (mediatype->subtype, MEDIASUBTYPE_YV12))
caps = gst_caps_new_simple ("video/x-raw",
"format", GST_TYPE_VIDEO_FORMAT, GST_VIDEO_FORMAT_YV12, NULL);
if (!caps) {
GST_DEBUG ("No subtype known; cannot continue");
return NULL;
}
if (IsEqualGUID (mediatype->formattype, FORMAT_VideoInfo) &&
mediatype->cbFormat >= sizeof(VIDEOINFOHEADER))
{
VIDEOINFOHEADER *vh = (VIDEOINFOHEADER *)mediatype->pbFormat;
/* TODO: Set PAR here. Based on difference between source and target RECTs?
* Do we want framerate? Based on AvgTimePerFrame? */
gst_caps_set_simple (caps,
"width", G_TYPE_INT, vh->bmiHeader.biWidth,
"height", G_TYPE_INT, vh->bmiHeader.biHeight,
NULL);
}
return caps;
}
/* Create a GstCaps object representing the same media type as
* this AM_MEDIA_TYPE.
*
* Returns NULL if no corresponding GStreamer type is known.
*
* May modify mediatype.
*/
static GstCaps *
gst_directshow_media_type_to_caps (AM_MEDIA_TYPE *mediatype)
{
GstCaps *caps = NULL;
if (IsEqualGUID (mediatype->majortype, MEDIATYPE_Video))
caps = video_media_type_to_caps (mediatype);
else if (IsEqualGUID (mediatype->majortype, MEDIATYPE_Audio))
caps = audio_media_type_to_caps (mediatype);
else {
GST_DEBUG ("Non audio/video media types not yet " \
"recognised, please add me: " GUID_FORMAT,
GUID_ARGS(mediatype->majortype));
}
if (caps) {
gchar *capsstring = gst_caps_to_string (caps);
GST_DEBUG ("Converted AM_MEDIA_TYPE to \"%s\"", capsstring);
g_free (capsstring);
}
else {
GST_WARNING ("Failed to convert AM_MEDIA_TYPE to caps");
}
return caps;
}
/* Fill in a DirectShow AM_MEDIA_TYPE structure representing the same media
* type as this GstCaps object.
*
* Returns FALSE if no corresponding type is known.
*
* Only operates on simple (single structure) caps.
*/
static gboolean
gst_caps_to_directshow_media_type (GstDshowVideoSink * sink, GstCaps *caps,
AM_MEDIA_TYPE *mediatype)
{
GstVideoInfo info;
int width, height;
int bpp;
gst_video_info_init (&info);
if (!gst_video_info_from_caps (&info, caps))
{
GST_WARNING_OBJECT (sink, "Couldn't parse caps");
return FALSE;
}
memset (mediatype, 0, sizeof (AM_MEDIA_TYPE));
if (GST_VIDEO_FORMAT_INFO_IS_YUV (info.finfo))
{
guint32 fourcc;
GST_VIDEO_SINK_WIDTH (sink) = info.width;
GST_VIDEO_SINK_HEIGHT (sink) = info.height;
width = info.width;
height = info.height;
mediatype->majortype = MEDIATYPE_Video;
switch (GST_VIDEO_INFO_FORMAT (&info))
{
case GST_VIDEO_FORMAT_YUY2:
mediatype->subtype = MEDIASUBTYPE_YUY2;
fourcc = GST_MAKE_FOURCC ('Y', 'U', 'Y', '2');
bpp = 16;
break;
case GST_VIDEO_FORMAT_UYVY:
mediatype->subtype = MEDIASUBTYPE_UYVY;
fourcc = GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y');
bpp = 16;
break;
case GST_VIDEO_FORMAT_YV12:
mediatype->subtype = MEDIASUBTYPE_YV12;
fourcc = GST_MAKE_FOURCC ('Y', 'V', '1', '2');
bpp = 12;
break;
default:
GST_WARNING_OBJECT (sink, "Couldn't parse caps");
return FALSE;
}
mediatype->bFixedSizeSamples = TRUE; /* Always true for raw video */
mediatype->bTemporalCompression = FALSE; /* Likewise, always false */
{
int par_n, par_d;
VIDEOINFOHEADER *vi = (VIDEOINFOHEADER *)CoTaskMemAlloc (sizeof (VIDEOINFOHEADER));
memset (vi, 0, sizeof (VIDEOINFOHEADER));
mediatype->formattype = FORMAT_VideoInfo;
mediatype->cbFormat = sizeof (VIDEOINFOHEADER);
mediatype->pbFormat = (BYTE *)vi;
mediatype->lSampleSize = width * height * bpp / 8;
GST_INFO_OBJECT (sink, "Set mediatype format: size %d, sample size %d",
mediatype->cbFormat, mediatype->lSampleSize);
vi->rcSource.top = 0;
vi->rcSource.left = 0;
vi->rcSource.bottom = height;
vi->rcSource.right = width;
vi->rcTarget.top = 0;
vi->rcTarget.left = 0;
if (sink->keep_aspect_ratio) {
par_n = GST_VIDEO_INFO_PAR_N (&info);
par_d = GST_VIDEO_INFO_PAR_D (&info);
/* To handle non-square pixels, we set the target rectangle to a
* different size than the source rectangle.
* There might be a better way, but this seems to work. */
vi->rcTarget.bottom = height;
vi->rcTarget.right = width * par_n / par_d;
GST_DEBUG_OBJECT (sink, "Got PAR: set target right to %d from width %d",
vi->rcTarget.right, width);
}
else {
vi->rcTarget.bottom = height;
vi->rcTarget.right = width;
}
vi->bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
vi->bmiHeader.biWidth = width;
vi->bmiHeader.biHeight = -height; /* Required to be negative. */
vi->bmiHeader.biPlanes = 1; /* Required to be 1 */
vi->bmiHeader.biBitCount = bpp;
vi->bmiHeader.biCompression = fourcc;
vi->bmiHeader.biSizeImage = width * height * bpp / 8;
/* We can safely zero these; they don't matter for our uses */
vi->bmiHeader.biXPelsPerMeter = 0;
vi->bmiHeader.biYPelsPerMeter = 0;
vi->bmiHeader.biClrUsed = 0;
vi->bmiHeader.biClrImportant = 0;
}
GST_DEBUG_OBJECT (sink, "Successfully built AM_MEDIA_TYPE from caps");
return TRUE;
}
GST_WARNING_OBJECT (sink, "Failed to convert caps, not a known caps type");
/* Only YUV supported so far */
return FALSE;
}
/* Plugin entry point */
extern "C" static gboolean
plugin_init (GstPlugin * plugin)
{
/* PRIMARY: this is the best videosink to use on windows */
if (!gst_element_register (plugin, "dshowvideosink",
GST_RANK_SECONDARY, GST_TYPE_DSHOWVIDEOSINK))
return FALSE;
return TRUE;
}
extern "C" GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
GST_VERSION_MINOR,
dshowsinkwrapper,
"DirectShow sink wrapper plugin",
plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
|