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
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
|
/* GStreamer
* Copyright (C) 2001 Wim Taymans <wim.taymans@gmail.com>
* 2004-2008 Edward Hervey <bilboed@bilboed.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., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gnl.h"
/**
* SECTION:element-gnlcomposition
* @short_description: Combines and controls GNonLin elements
*
* A GnlComposition contains GnlObjects such as GnlSources and GnlOperations,
* and connects them dynamically to create a composition timeline.
*/
GST_BOILERPLATE (GnlComposition, gnl_composition, GnlObject, GNL_TYPE_OBJECT);
static GstElementDetails gnl_composition_details =
GST_ELEMENT_DETAILS ("GNonLin Composition",
"Filter/Editor",
"Combines GNL objects",
"Wim Taymans <wim.taymans@gmail.com>, Edward Hervey <bilboed@bilboed.com>");
static GstStaticPadTemplate gnl_composition_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_SOMETIMES,
GST_STATIC_CAPS_ANY);
GST_DEBUG_CATEGORY_STATIC (gnlcomposition);
#define GST_CAT_DEFAULT gnlcomposition
struct _GnlCompositionPrivate
{
gboolean dispose_has_run;
/*
Sorted List of GnlObjects , ThreadSafe
objects_start : sorted by start-time then priority
objects_stop : sorted by stop-time then priority
objects_hash : contains signal handlers id for controlled objects
objects_lock : mutex to acces/modify any of those lists/hashtable
*/
GList *objects_start;
GList *objects_stop;
GHashTable *objects_hash;
GMutex *objects_lock;
/*
thread-safe Seek handling.
flushing_lock : mutex to access flushing and pending_idle
flushing :
pending_idle :
*/
GMutex *flushing_lock;
gboolean flushing;
guint pending_idle;
/* source top-level ghostpad */
GstPad *ghostpad;
/* current stack, list of GnlObject* */
GNode *current;
GnlObject *defaultobject;
/*
current segment seek start/stop time.
Reconstruct pipeline ONLY if seeking outside of those values
FIXME : segment_start isn't always the earliest time before which the
timeline doesn't need to be modified
*/
GstClockTime segment_start;
GstClockTime segment_stop;
/* pending child seek */
GstEvent *childseek;
/* Seek segment handler */
GstSegment *segment;
/* number of pads we are waiting to appear so be can do proper linking */
guint waitingpads;
/*
OUR sync_handler on the child_bus
We are called before gnl_object_sync_handler
*/
GstPadEventFunction gnl_event_pad_func;
/* List of SEGMENT_START/SEGMENT_DONE, protected by objects lock */
GList *segmessages;
GMutex *messages_lock;
};
#define OBJECT_IN_ACTIVE_SEGMENT(comp,element) \
(((((GnlObject*)element)->start >= comp->private->segment_start) && \
(((GnlObject*)element)->start < comp->private->segment_stop)) || \
((((GnlObject*)element)->stop > comp->private->segment_start) && \
(((GnlObject*)element)->stop <= comp->private->segment_stop))) \
static void gnl_composition_dispose (GObject * object);
static void gnl_composition_finalize (GObject * object);
static void gnl_composition_reset (GnlComposition * comp);
static gboolean gnl_composition_add_object (GstBin * bin, GstElement * element);
static void gnl_composition_handle_message (GstBin * bin, GstMessage * message);
static gboolean
gnl_composition_remove_object (GstBin * bin, GstElement * element);
static GstStateChangeReturn
gnl_composition_change_state (GstElement * element, GstStateChange transition);
static GstPad *get_src_pad (GstElement * element);
static void pad_blocked (GstPad * pad, gboolean blocked, GnlComposition * comp);
static gboolean
seek_handling (GnlComposition * comp, gboolean initial, gboolean update);
static gboolean
update_pipeline (GnlComposition * comp, GstClockTime currenttime,
gboolean initial, gboolean change_state, gboolean modify);
static void flush_messages (GnlComposition * comp);
#define COMP_REAL_START(comp) \
(MAX (comp->private->segment->start, ((GnlObject*)comp)->start))
#define COMP_REAL_STOP(comp) \
((GST_CLOCK_TIME_IS_VALID (comp->private->segment->stop) \
? (MIN (comp->private->segment->stop, ((GnlObject*)comp)->stop))) \
: (((GnlObject*)comp)->stop))
#define COMP_ENTRY(comp, object) \
(g_hash_table_lookup (comp->private->objects_hash, (gconstpointer) object))
#define COMP_OBJECTS_LOCK(comp) G_STMT_START { \
GST_LOG_OBJECT (comp, "locking objects_lock from thread %p", \
g_thread_self()); \
g_mutex_lock (comp->private->objects_lock); \
GST_LOG_OBJECT (comp, "locked objects_lock from thread %p", \
g_thread_self()); \
} G_STMT_END
#define COMP_OBJECTS_UNLOCK(comp) G_STMT_START { \
GST_LOG_OBJECT (comp, "unlocking objects_lock from thread %p", \
g_thread_self()); \
g_mutex_unlock (comp->private->objects_lock); \
} G_STMT_END
#define COMP_MESSAGES_LOCK(comp) G_STMT_START { \
GST_LOG_OBJECT (comp, "locking messages_lock from thread %p", \
g_thread_self()); \
g_mutex_lock (comp->private->messages_lock); \
GST_LOG_OBJECT (comp, "locked messages_lock from thread %p", \
g_thread_self()); \
} G_STMT_END
#define COMP_MESSAGES_UNLOCK(comp) G_STMT_START { \
GST_LOG_OBJECT (comp, "unlocking messages_lock from thread %p", \
g_thread_self()); \
g_mutex_unlock (comp->private->messages_lock); \
} G_STMT_END
#define COMP_FLUSHING_LOCK(comp) G_STMT_START { \
GST_LOG_OBJECT (comp, "locking flushing_lock from thread %p", \
g_thread_self()); \
g_mutex_lock (comp->private->flushing_lock); \
GST_LOG_OBJECT (comp, "locked flushing_lock from thread %p", \
g_thread_self()); \
} G_STMT_END
#define COMP_FLUSHING_UNLOCK(comp) G_STMT_START { \
GST_LOG_OBJECT (comp, "unlocking flushing_lock from thread %p", \
g_thread_self()); \
g_mutex_unlock (comp->private->flushing_lock); \
} G_STMT_END
static gboolean gnl_composition_prepare (GnlObject * object);
typedef struct _GnlCompositionEntry GnlCompositionEntry;
struct _GnlCompositionEntry
{
GnlObject *object;
/* handler ids for property notifications */
gulong starthandler;
gulong stophandler;
gulong priorityhandler;
gulong activehandler;
/* handler id for 'no-more-pads' signal */
gulong nomorepadshandler;
gulong padaddedhandler;
gulong padremovedhandler;
};
static void
gnl_composition_base_init (gpointer g_class)
{
GstElementClass *gstclass = GST_ELEMENT_CLASS (g_class);
gst_element_class_set_details (gstclass, &gnl_composition_details);
}
static void
gnl_composition_class_init (GnlCompositionClass * klass)
{
GObjectClass *gobject_class;
GstElementClass *gstelement_class;
GstBinClass *gstbin_class;
GnlObjectClass *gnlobject_class;
gobject_class = (GObjectClass *) klass;
gstelement_class = (GstElementClass *) klass;
gstbin_class = (GstBinClass *) klass;
gnlobject_class = (GnlObjectClass *) klass;
GST_DEBUG_CATEGORY_INIT (gnlcomposition, "gnlcomposition",
GST_DEBUG_FG_BLUE | GST_DEBUG_BOLD, "GNonLin Composition");
gobject_class->dispose = GST_DEBUG_FUNCPTR (gnl_composition_dispose);
gobject_class->finalize = GST_DEBUG_FUNCPTR (gnl_composition_finalize);
gstelement_class->change_state = gnl_composition_change_state;
/* gstelement_class->query = gnl_composition_query; */
gstbin_class->add_element = GST_DEBUG_FUNCPTR (gnl_composition_add_object);
gstbin_class->remove_element =
GST_DEBUG_FUNCPTR (gnl_composition_remove_object);
gstbin_class->handle_message =
GST_DEBUG_FUNCPTR (gnl_composition_handle_message);
gnlobject_class->prepare = GST_DEBUG_FUNCPTR (gnl_composition_prepare);
gst_element_class_add_pad_template (gstelement_class,
gst_static_pad_template_get (&gnl_composition_src_template));
}
static void
hash_value_destroy (GnlCompositionEntry * entry)
{
if (entry->starthandler)
g_signal_handler_disconnect (entry->object, entry->starthandler);
if (entry->stophandler)
g_signal_handler_disconnect (entry->object, entry->stophandler);
if (entry->priorityhandler)
g_signal_handler_disconnect (entry->object, entry->priorityhandler);
g_signal_handler_disconnect (entry->object, entry->activehandler);
g_signal_handler_disconnect (entry->object, entry->padremovedhandler);
g_signal_handler_disconnect (entry->object, entry->padaddedhandler);
if (entry->nomorepadshandler)
g_signal_handler_disconnect (entry->object, entry->nomorepadshandler);
g_free (entry);
}
static void
gnl_composition_init (GnlComposition * comp, GnlCompositionClass * klass)
{
GST_OBJECT_FLAG_SET (comp, GNL_OBJECT_SOURCE);
comp->private = g_new0 (GnlCompositionPrivate, 1);
comp->private->objects_lock = g_mutex_new ();
comp->private->objects_start = NULL;
comp->private->objects_stop = NULL;
comp->private->flushing_lock = g_mutex_new ();
comp->private->flushing = FALSE;
comp->private->pending_idle = 0;
comp->private->segment = gst_segment_new ();
comp->private->waitingpads = 0;
comp->private->defaultobject = NULL;
comp->private->objects_hash = g_hash_table_new_full
(g_direct_hash,
g_direct_equal, NULL, (GDestroyNotify) hash_value_destroy);
comp->private->messages_lock = g_mutex_new ();
gnl_composition_reset (comp);
}
static void
gnl_composition_dispose (GObject * object)
{
GnlComposition *comp = GNL_COMPOSITION (object);
if (comp->private->dispose_has_run)
return;
comp->private->dispose_has_run = TRUE;
if (comp->private->ghostpad) {
gnl_object_remove_ghost_pad ((GnlObject *) object, comp->private->ghostpad);
comp->private->ghostpad = NULL;
}
if (comp->private->childseek) {
gst_event_unref (comp->private->childseek);
comp->private->childseek = NULL;
}
if (comp->private->current) {
g_node_destroy (comp->private->current);
comp->private->current = NULL;
}
G_OBJECT_CLASS (parent_class)->dispose (object);
}
static void
gnl_composition_finalize (GObject * object)
{
GnlComposition *comp = GNL_COMPOSITION (object);
GST_INFO ("finalize");
COMP_OBJECTS_LOCK (comp);
g_list_free (comp->private->objects_start);
g_list_free (comp->private->objects_stop);
if (comp->private->current)
g_node_destroy (comp->private->current);
g_hash_table_destroy (comp->private->objects_hash);
COMP_OBJECTS_UNLOCK (comp);
g_mutex_free (comp->private->objects_lock);
gst_segment_free (comp->private->segment);
g_mutex_free (comp->private->flushing_lock);
g_mutex_free (comp->private->messages_lock);
flush_messages (comp);
g_free (comp->private);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static gboolean
unblock_child_pads (GstElement * child, GValue * ret, GnlComposition * comp)
{
GstPad *pad;
GST_DEBUG_OBJECT (child, "unblocking pads");
pad = get_src_pad (child);
if (pad) {
gst_pad_set_blocked_async (pad, FALSE, (GstPadBlockCallback) pad_blocked,
comp);
gst_object_unref (pad);
}
gst_object_unref (child);
return TRUE;
}
static void
unblock_childs (GnlComposition * comp)
{
GstIterator *childs;
GstIteratorResult res;
GValue val = { 0 };
g_value_init (&val, G_TYPE_BOOLEAN);
g_value_set_boolean (&val, FALSE);
childs = gst_bin_iterate_elements (GST_BIN (comp));
res =
gst_iterator_fold (childs, (GstIteratorFoldFunction) unblock_child_pads,
&val, comp);
gst_iterator_free (childs);
}
static gboolean
unlock_child_state (GstElement * child, GValue * ret, gpointer udata)
{
GST_DEBUG_OBJECT (child, "unlocking state");
gst_element_set_locked_state (child, FALSE);
gst_object_unref (child);
return TRUE;
}
static gboolean
lock_child_state (GstElement * child, GValue * ret, gpointer udata)
{
GST_DEBUG_OBJECT (child, "locking state");
gst_element_set_locked_state (child, TRUE);
gst_object_unref (child);
return TRUE;
}
static void
unlock_childs (GnlComposition * comp)
{
GstIterator *childs;
GstIteratorResult res;
GValue val = { 0 };
g_value_init (&val, G_TYPE_BOOLEAN);
g_value_set_boolean (&val, FALSE);
childs = gst_bin_iterate_elements (GST_BIN (comp));
res =
gst_iterator_fold (childs, (GstIteratorFoldFunction) unlock_child_state,
&val, NULL);
gst_iterator_free (childs);
}
static void
gnl_composition_reset (GnlComposition * comp)
{
GST_DEBUG_OBJECT (comp, "resetting");
comp->private->segment_start = GST_CLOCK_TIME_NONE;
comp->private->segment_stop = GST_CLOCK_TIME_NONE;
gst_segment_init (comp->private->segment, GST_FORMAT_TIME);
if (comp->private->current)
g_node_destroy (comp->private->current);
comp->private->current = NULL;
if (comp->private->ghostpad) {
gnl_object_remove_ghost_pad ((GnlObject *) comp, comp->private->ghostpad);
comp->private->ghostpad = NULL;
}
if (comp->private->childseek) {
gst_event_unref (comp->private->childseek);
comp->private->childseek = NULL;
}
comp->private->waitingpads = 0;
unlock_childs (comp);
COMP_FLUSHING_LOCK (comp);
if (comp->private->pending_idle)
g_source_remove (comp->private->pending_idle);
comp->private->pending_idle = 0;
comp->private->flushing = FALSE;
COMP_FLUSHING_UNLOCK (comp);
GST_DEBUG_OBJECT (comp, "Composition now resetted");
}
static gboolean
segment_done_main_thread (GnlComposition * comp)
{
/* Set up a non-initial seek on segment_stop */
GST_DEBUG_OBJECT (comp,
"Setting segment->start to segment_stop:%" GST_TIME_FORMAT,
GST_TIME_ARGS (comp->private->segment_stop));
comp->private->segment->start = comp->private->segment_stop;
seek_handling (comp, TRUE, TRUE);
if (!comp->private->current) {
/* If we're at the end, post SEGMENT_DONE, or push EOS */
GST_DEBUG_OBJECT (comp, "Nothing else to play");
if (!(comp->private->segment->flags & GST_SEEK_FLAG_SEGMENT)
&& comp->private->ghostpad) {
GST_LOG_OBJECT (comp, "Pushing out EOS");
gst_pad_push_event (comp->private->ghostpad, gst_event_new_eos ());
} else if (comp->private->segment->flags & GST_SEEK_FLAG_SEGMENT) {
gint64 epos;
if (GST_CLOCK_TIME_IS_VALID (comp->private->segment->stop))
epos = (MIN (comp->private->segment->stop, ((GnlObject *) comp)->stop));
else
epos = (((GnlObject *) comp)->stop);
GST_LOG_OBJECT (comp, "Emitting segment done pos %" GST_TIME_FORMAT,
GST_TIME_ARGS (epos));
GST_BIN_CLASS (parent_class)->handle_message
(GST_BIN (comp),
gst_message_new_segment_done (GST_OBJECT (comp),
comp->private->segment->format, epos));
}
}
return FALSE;
}
/* add_message
* flush_messages
* replace_message
* has_message
*
* Must be called with the MESSAGES_LOCK taken
*/
static void
add_message (GnlComposition * comp, GstMessage * msg)
{
GList *tmp;
/* make sure we don't already have a msg with the
* same src/type */
for (tmp = comp->private->segmessages; tmp; tmp = tmp->next) {
GstMessage *tmpmsg = (GstMessage *) tmp->data;
if ((GST_MESSAGE_SRC (msg) == GST_MESSAGE_SRC (tmpmsg)) &&
(GST_MESSAGE_TYPE (msg) == GST_MESSAGE_TYPE (tmpmsg)))
return;
}
comp->private->segmessages = g_list_append (comp->private->segmessages,
gst_message_ref (msg));
}
static void
flush_messages (GnlComposition * comp)
{
GList *tmp;
for (tmp = comp->private->segmessages; tmp; tmp = tmp->next)
gst_message_unref ((GstMessage *) tmp->data);
g_list_free (comp->private->segmessages);
comp->private->segmessages = NULL;
}
static void
replace_message (GnlComposition * comp, GstMessage * msg, GstMessageType type)
{
GList *tmp = NULL;
/* Find msg of type 'type' and source 'msg'->src */
for (tmp = comp->private->segmessages; tmp; tmp = tmp->next) {
GstMessage *tmpmsg = (GstMessage *) tmp->data;
if ((GST_MESSAGE_TYPE (tmpmsg) == type) &&
(GST_MESSAGE_SRC (tmpmsg) == GST_MESSAGE_SRC (msg)))
break;
}
if (tmp != NULL) {
gst_message_unref ((GstMessage *) tmp->data);
comp->private->segmessages =
g_list_delete_link (comp->private->segmessages, tmp);
}
add_message (comp, msg);
}
static gboolean
has_message (GnlComposition * comp, GstMessageType type)
{
GList *tmp;
gboolean res = FALSE;
for (tmp = comp->private->segmessages; tmp; tmp = tmp->next)
if (GST_MESSAGE_TYPE ((GstMessage *) tmp->data) == type) {
res = TRUE;
break;
}
return res;
}
static void
dump_messages (GnlComposition * comp)
{
GList *tmp;
for (tmp = comp->private->segmessages; tmp; tmp = tmp->next) {
GstMessage *msg = (GstMessage *) tmp->data;
GST_LOG ("type:%s , src:%s",
GST_MESSAGE_TYPE_NAME (msg), GST_ELEMENT_NAME (GST_MESSAGE_SRC (msg)));
}
}
/* Warning : Don't take the objects lock in this method */
static void
gnl_composition_handle_message (GstBin * bin, GstMessage * message)
{
GnlComposition *comp = (GnlComposition *) bin;
gboolean dropit = FALSE;
GST_DEBUG_OBJECT (comp, "message:%s from %s",
gst_message_type_get_name (GST_MESSAGE_TYPE (message)),
GST_MESSAGE_SRC (message) ? GST_ELEMENT_NAME (GST_MESSAGE_SRC (message)) :
"UNKNOWN");
switch (GST_MESSAGE_TYPE (message)) {
case GST_MESSAGE_SEGMENT_START:{
COMP_FLUSHING_LOCK (comp);
if (comp->private->pending_idle) {
GST_DEBUG_OBJECT (comp, "removing pending seek for main thread");
g_source_remove (comp->private->pending_idle);
}
comp->private->pending_idle = 0;
comp->private->flushing = FALSE;
COMP_FLUSHING_UNLOCK (comp);
COMP_MESSAGES_LOCK (comp);
add_message (comp, message);
dump_messages (comp);
COMP_MESSAGES_UNLOCK (comp);
dropit = TRUE;
break;
}
case GST_MESSAGE_SEGMENT_DONE:{
gboolean has_start = FALSE;
COMP_FLUSHING_LOCK (comp);
if (comp->private->flushing) {
GST_DEBUG_OBJECT (comp, "flushing, bailing out");
COMP_FLUSHING_UNLOCK (comp);
dropit = TRUE;
break;
}
COMP_FLUSHING_UNLOCK (comp);
COMP_MESSAGES_LOCK (comp);
replace_message (comp, message, GST_MESSAGE_SEGMENT_START);
has_start = has_message (comp, GST_MESSAGE_SEGMENT_START);
dump_messages (comp);
COMP_MESSAGES_UNLOCK (comp);
if (has_start == TRUE) {
GST_DEBUG_OBJECT (comp, "Still waiting for more SEGMENT_DONE");
dropit = TRUE;
break;
}
GST_DEBUG_OBJECT (comp, "Adding segment_done handling to main thread");
if (comp->private->pending_idle) {
GST_WARNING_OBJECT (comp,
"There was already a pending segment_done in main thread !");
g_source_remove (comp->private->pending_idle);
}
/* FIXME : This should be switched to using a g_thread_create() instead
* of a g_idle_add(). EXTENSIVE TESTING AND ANALYSIS REQUIRED BEFORE
* DOING THE SWITCH !!! */
comp->private->pending_idle =
g_idle_add ((GSourceFunc) segment_done_main_thread, (gpointer) comp);
dropit = TRUE;
break;
}
case GST_MESSAGE_ERROR:
case GST_MESSAGE_WARNING:{
/* FIXME / HACK
* There is a massive issue with reverse negotiation and dynamic pipelines.
*
* Since we're not waiting for the pads of the previous stack to block before
* re-switching, we might end up switching sources in the middle of a downstrea
* negotiation which will do reverse negotiation... with the new source (which
* is no longer the one that issues the request). That negotiation will fail
* and the original source will emit an ERROR message.
*
* In order to avoid those issues, we just ignore error messages from elements
* which aren't in the currently configured stack
*/
if (GST_MESSAGE_SRC (message)
&& !OBJECT_IN_ACTIVE_SEGMENT (comp, GST_MESSAGE_SRC (message))) {
GST_DEBUG_OBJECT (comp,
"HACK Dropping error message from object not in currently configured stack !");
dropit = TRUE;
}
}
default:
break;
}
if (dropit)
gst_message_unref (message);
else
GST_BIN_CLASS (parent_class)->handle_message (bin, message);
}
static gint
priority_comp (GnlObject * a, GnlObject * b)
{
if (a->priority < b->priority)
return -1;
if (a->priority > b->priority)
return 1;
return 0;
}
static gboolean
have_to_update_pipeline (GnlComposition * comp)
{
GST_DEBUG_OBJECT (comp,
"segment[%" GST_TIME_FORMAT "--%" GST_TIME_FORMAT "] current[%"
GST_TIME_FORMAT "--%" GST_TIME_FORMAT "]",
GST_TIME_ARGS (comp->private->segment->start),
GST_TIME_ARGS (comp->private->segment->stop),
GST_TIME_ARGS (comp->private->segment_start),
GST_TIME_ARGS (comp->private->segment_stop));
if (comp->private->segment->start < comp->private->segment_start)
return TRUE;
if (comp->private->segment->start >= comp->private->segment_stop)
return TRUE;
return FALSE;
}
/*
* get_new_seek_event:
*
* Returns a seek event for the currently configured segment
* and start/stop values
*
* The GstSegment and segment_start|stop must have been configured
* before calling this function.
*/
static GstEvent *
get_new_seek_event (GnlComposition * comp, gboolean initial,
gboolean updatestoponly)
{
GstSeekFlags flags;
gint64 start, stop;
GstSeekType starttype = GST_SEEK_TYPE_SET;
GST_DEBUG_OBJECT (comp, "initial:%d", initial);
/* remove the seek flag */
if (!(initial))
flags = comp->private->segment->flags;
else
flags =
GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_FLUSH;
GST_DEBUG_OBJECT (comp,
"private->segment->start:%" GST_TIME_FORMAT " segment_start%"
GST_TIME_FORMAT, GST_TIME_ARGS (comp->private->segment->start),
GST_TIME_ARGS (comp->private->segment_start));
GST_DEBUG_OBJECT (comp,
"private->segment->stop:%" GST_TIME_FORMAT " segment_stop%"
GST_TIME_FORMAT, GST_TIME_ARGS (comp->private->segment->stop),
GST_TIME_ARGS (comp->private->segment_stop));
start = MAX (comp->private->segment->start, comp->private->segment_start);
stop = GST_CLOCK_TIME_IS_VALID (comp->private->segment->stop)
? MIN (comp->private->segment->stop, comp->private->segment_stop)
: comp->private->segment_stop;
if (updatestoponly) {
starttype = GST_SEEK_TYPE_NONE;
start = GST_CLOCK_TIME_NONE;
}
GST_DEBUG_OBJECT (comp,
"Created new seek event. Flags:%d, start:%" GST_TIME_FORMAT ", stop:%"
GST_TIME_FORMAT, flags, GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
return gst_event_new_seek (comp->private->segment->rate,
comp->private->segment->format, flags, starttype, start,
GST_SEEK_TYPE_SET, stop);
}
/* OBJECTS LOCK must be taken when calling this ! */
static GstClockTime
get_current_position (GnlComposition * comp)
{
GstFormat format = GST_FORMAT_TIME;
gint64 value = GST_CLOCK_TIME_NONE;
GstPad *pad;
GnlObject *obj;
gboolean res;
/* 1. Try querying position downstream */
if (comp->private->ghostpad) {
GstPad *peer = gst_pad_get_peer (comp->private->ghostpad);
if (peer) {
res = gst_pad_query_position (peer, &format, &value);
gst_object_unref (peer);
if (res && (format == GST_FORMAT_TIME)) {
GST_LOG_OBJECT (comp,
"Successfully got downstream position %" GST_TIME_FORMAT,
GST_TIME_ARGS ((guint64) value));
goto beach;
}
}
GST_DEBUG_OBJECT (comp, "Downstream position query failed");
/* resetting format/value */
format = GST_FORMAT_TIME;
value = GST_CLOCK_TIME_NONE;
}
/* 2. If downstream fails , try within the current stack */
if (!comp->private->current) {
GST_DEBUG_OBJECT (comp, "No current stack, can't send query");
goto beach;
}
obj = (GnlObject *) comp->private->current->data;
if (!(pad = get_src_pad ((GstElement *) obj)))
goto beach;
res = gst_pad_query_position (pad, &format, &value);
if (format != GST_FORMAT_TIME) {
GST_WARNING_OBJECT (comp, "query returned a format different from TIME");
value = GST_CLOCK_TIME_NONE;
} else {
GST_LOG_OBJECT (comp, "Query returned %" GST_TIME_FORMAT,
GST_TIME_ARGS ((guint64) value));
}
beach:
return (guint64) value;
}
/*
Figures out if pipeline needs updating.
Updates it and sends the seek event.
Sends flush events downstream if needed.
can be called by user_seek or segment_done
*/
static gboolean
seek_handling (GnlComposition * comp, gboolean initial, gboolean update)
{
GST_DEBUG_OBJECT (comp, "initial:%d, update:%d", initial, update);
COMP_FLUSHING_LOCK (comp);
GST_DEBUG_OBJECT (comp, "Setting flushing to TRUE");
comp->private->flushing = TRUE;
/* Send downstream flush start/stop if needed */
/* FIXME : The problem is that we might have a streaming thread which will
* be able to push AFTER FLUSH_STOP.
* We can't really block... since maybe we're not pushing anything currently !!! */
if (comp->private->ghostpad
&& (comp->private->segment->flags & GST_SEEK_FLAG_FLUSH)
&& (!update)) {
GST_LOG_OBJECT (comp, "Sending downstream flush start/stop");
gst_pad_push_event (comp->private->ghostpad, gst_event_new_flush_start ());
gst_pad_push_event (comp->private->ghostpad, gst_event_new_flush_stop ());
}
COMP_FLUSHING_UNLOCK (comp);
if (update || have_to_update_pipeline (comp)) {
update_pipeline (comp, comp->private->segment->start, initial, TRUE, FALSE);
}
return TRUE;
}
static void
handle_seek_event (GnlComposition * comp, GstEvent * event)
{
gboolean update;
gdouble rate;
GstFormat format;
GstSeekFlags flags;
GstSeekType cur_type, stop_type;
gint64 cur, stop;
gst_event_parse_seek (event, &rate, &format, &flags,
&cur_type, &cur, &stop_type, &stop);
GST_DEBUG_OBJECT (comp,
"start:%" GST_TIME_FORMAT " -- stop:%" GST_TIME_FORMAT " flags:%d",
GST_TIME_ARGS (cur), GST_TIME_ARGS (stop), flags);
gst_segment_set_seek (comp->private->segment,
rate, format, flags, cur_type, cur, stop_type, stop, &update);
GST_DEBUG_OBJECT (comp, "Segment now has flags:%d",
comp->private->segment->flags);
/* crop the segment start/stop values */
/* Only crop segment start value if we don't have a default object */
if (comp->private->defaultobject == NULL)
comp->private->segment->start = MAX (comp->private->segment->start,
((GnlObject *) comp)->start);
comp->private->segment->stop = MIN (comp->private->segment->stop,
((GnlObject *) comp)->stop);
seek_handling (comp, TRUE, FALSE);
}
static gboolean
gnl_composition_event_handler (GstPad * ghostpad, GstEvent * event)
{
GnlComposition *comp = (GnlComposition *) gst_pad_get_parent (ghostpad);
gboolean res = TRUE;
GST_DEBUG_OBJECT (comp, "event type:%s", GST_EVENT_TYPE_NAME (event));
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_SEEK:{
GstEvent *nevent;
handle_seek_event (comp, event);
/* the incoming event might not be quite correct, we get a new proper
* event to pass on to the childs. */
COMP_OBJECTS_LOCK (comp);
nevent = get_new_seek_event (comp, FALSE, FALSE);
COMP_OBJECTS_UNLOCK (comp);
gst_event_unref (event);
event = nevent;
break;
}
default:
break;
}
/* FIXME : What should we do here if waitingpads != 0 ?? */
/* Delay ? Ignore ? Refuse ? */
if (res) {
GST_DEBUG_OBJECT (comp, "About to call gnl_event_pad_func()");
COMP_OBJECTS_LOCK (comp);
res = comp->private->gnl_event_pad_func (ghostpad, event);
COMP_OBJECTS_UNLOCK (comp);
GST_DEBUG_OBJECT (comp, "Done calling gnl_event_pad_func() %d", res);
}
gst_object_unref (comp);
return res;
}
static void
pad_blocked (GstPad * pad, gboolean blocked, GnlComposition * comp)
{
GST_DEBUG_OBJECT (comp, "Pad : %s:%s , blocked:%d",
GST_DEBUG_PAD_NAME (pad), blocked);
}
/* gnl_composition_ghost_pad_set_target:
* target: The target #GstPad. The refcount will be decremented (given to the ghostpad).
*/
static void
gnl_composition_ghost_pad_set_target (GnlComposition * comp, GstPad * target)
{
gboolean hadghost = (comp->private->ghostpad) ? TRUE : FALSE;
if (target)
GST_DEBUG_OBJECT (comp, "%s:%s , hadghost:%d",
GST_DEBUG_PAD_NAME (target), hadghost);
else
GST_DEBUG_OBJECT (comp, "Removing target, hadghost:%d", hadghost);
if (!(hadghost)) {
comp->private->ghostpad =
gnl_object_ghost_pad_no_target ((GnlObject *) comp, "src", GST_PAD_SRC);
GST_DEBUG_OBJECT (comp->private->ghostpad,
"About to replace event_pad_func");
comp->private->gnl_event_pad_func =
GST_PAD_EVENTFUNC (comp->private->ghostpad);
gst_pad_set_event_function (comp->private->ghostpad,
GST_DEBUG_FUNCPTR (gnl_composition_event_handler));
GST_DEBUG_OBJECT (comp->private->ghostpad, "eventfunc is now %s",
GST_DEBUG_FUNCPTR_NAME (GST_PAD_EVENTFUNC (comp->private->ghostpad)));
} else {
GstPad *ptarget =
gst_ghost_pad_get_target (GST_GHOST_PAD (comp->private->ghostpad));
if (ptarget && ptarget == target) {
GST_DEBUG_OBJECT (comp,
"Target of ghostpad is the same as existing one, not changing");
gst_object_unref (ptarget);
return;
}
if (ptarget) {
GST_DEBUG_OBJECT (comp, "Previous target was %s:%s, blocking that pad",
GST_DEBUG_PAD_NAME (ptarget));
gst_pad_set_blocked_async (ptarget, TRUE,
(GstPadBlockCallback) pad_blocked, comp);
gst_object_unref (ptarget);
}
}
gnl_object_ghost_pad_set_target ((GnlObject *) comp,
comp->private->ghostpad, target);
if (!(hadghost)) {
gst_pad_set_active (comp->private->ghostpad, TRUE);
if (!(gst_element_add_pad (GST_ELEMENT (comp), comp->private->ghostpad)))
GST_WARNING ("Couldn't add the ghostpad");
else {
COMP_OBJECTS_UNLOCK (comp);
gst_element_no_more_pads (GST_ELEMENT (comp));
COMP_OBJECTS_LOCK (comp);
}
}
GST_DEBUG_OBJECT (comp, "END");
}
static void
refine_start_stop_in_region_above_priority (GnlComposition * composition,
GstClockTime timestamp, GstClockTime start,
GstClockTime stop,
GstClockTime * rstart, GstClockTime * rstop, guint32 priority)
{
GList *tmp = composition->private->objects_start;
GnlObject *object;
GstClockTime nstart = start, nstop = stop;
GST_DEBUG_OBJECT (composition,
"timestamp:%" GST_TIME_FORMAT " start: %" GST_TIME_FORMAT " stop: %"
GST_TIME_FORMAT " priority:%u", GST_TIME_ARGS (timestamp),
GST_TIME_ARGS (start), GST_TIME_ARGS (stop), priority);
for (tmp = composition->private->objects_start; tmp; tmp = g_list_next (tmp)) {
object = (GnlObject *) tmp->data;
GST_LOG_OBJECT (object, "START %" GST_TIME_FORMAT "--%" GST_TIME_FORMAT,
GST_TIME_ARGS (object->start), GST_TIME_ARGS (object->stop));
if ((object->priority >= priority) || (!object->active))
continue;
if (object->start <= timestamp)
continue;
if (object->start >= nstop)
continue;
nstop = object->start;
GST_DEBUG_OBJECT (composition,
"START Found %s [prio:%u] at %" GST_TIME_FORMAT,
GST_OBJECT_NAME (object), object->priority,
GST_TIME_ARGS (object->start));
break;
}
for (tmp = composition->private->objects_stop; tmp; tmp = g_list_next (tmp)) {
object = (GnlObject *) tmp->data;
GST_LOG_OBJECT (object, "STOP %" GST_TIME_FORMAT "--%" GST_TIME_FORMAT,
GST_TIME_ARGS (object->start), GST_TIME_ARGS (object->stop));
if ((object->priority >= priority) || (!object->active))
continue;
if (object->stop >= timestamp)
continue;
if (object->stop <= nstart)
continue;
nstart = object->stop;
GST_DEBUG_OBJECT (composition,
"STOP Found %s [prio:%u] at %" GST_TIME_FORMAT,
GST_OBJECT_NAME (object), object->priority,
GST_TIME_ARGS (object->start));
break;
}
if (*rstart)
*rstart = nstart;
if (*rstop)
*rstop = nstop;
}
/*
* Converts a sorted list to a tree
* Recursive
*
* stack will be set to the next item to use in the parent.
* If operations number of sinks is limited, it will only use that number.
*/
static GNode *
convert_list_to_tree (GList ** stack, GstClockTime * start,
GstClockTime * stop, guint32 * highprio)
{
GNode *ret;
guint nbsinks;
gboolean limit;
GList *tmp;
GnlObject *object;
if (!stack || !*stack)
return NULL;
object = (GnlObject *) (*stack)->data;
GST_DEBUG ("object:%s , *start:%" GST_TIME_FORMAT ", *stop:%"
GST_TIME_FORMAT " highprio:%d",
GST_ELEMENT_NAME (object), GST_TIME_ARGS (*start),
GST_TIME_ARGS (*stop), *highprio);
/* update earliest stop */
if (GST_CLOCK_TIME_IS_VALID (*stop)) {
if (GST_CLOCK_TIME_IS_VALID (object->stop) && (*stop > object->stop))
*stop = object->stop;
} else {
*stop = object->stop;
}
if (GST_CLOCK_TIME_IS_VALID (*start)) {
if (GST_CLOCK_TIME_IS_VALID (object->start) && (*start < object->start))
*start = object->start;
} else {
*start = object->start;
}
if (GNL_IS_SOURCE (object)) {
*stack = g_list_next (*stack);
/* update highest priority.
* We do this here, since it's only used with sources (leafs of the tree) */
if (object->priority > *highprio)
*highprio = object->priority;
ret = g_node_new (object);
goto beach;
} else { /* GnlOperation */
GnlOperation *oper = (GnlOperation *) object;
GST_LOG_OBJECT (oper, "operation, num_sinks:%d", oper->num_sinks);
ret = g_node_new (object);
limit = (oper->dynamicsinks == FALSE);
nbsinks = oper->num_sinks;
/* FIXME : if num_sinks == -1 : request the proper number of pads */
for (tmp = g_list_next (*stack); tmp && (!limit || nbsinks);) {
g_node_append (ret, convert_list_to_tree (&tmp, start, stop, highprio));
if (limit)
nbsinks--;
}
}
beach:
GST_DEBUG_OBJECT (object,
"*start:%" GST_TIME_FORMAT " *stop:%" GST_TIME_FORMAT " priority:%u",
GST_TIME_ARGS (*start), GST_TIME_ARGS (*stop), *highprio);
return ret;
}
/*
* get_stack_list:
* @comp: The #GnlComposition
* @timestamp: The #GstClockTime to look at
* @priority: The priority level to start looking from
* @activeonly: Only look for active elements if TRUE
* @start: The biggest start time of the objects in the stack
* @stop: The smallest stop time of the objects in the stack
* @highprio: The highest priority in the stack
*
* Not MT-safe, you should take the objects lock before calling it.
* Returns: A tree of #GNode sorted in priority order, corresponding
* to the given search arguments. The returned value can be #NULL.
*/
static GNode *
get_stack_list (GnlComposition * comp, GstClockTime timestamp,
guint32 priority, gboolean activeonly, GstClockTime * start,
GstClockTime * stop, guint * highprio)
{
GList *tmp = comp->private->objects_start;
GList *stack = NULL;
GNode *ret = NULL;
GstClockTime nstart = GST_CLOCK_TIME_NONE;
GstClockTime nstop = GST_CLOCK_TIME_NONE;
guint32 highest = 0;
GST_DEBUG_OBJECT (comp,
"timestamp:%" GST_TIME_FORMAT ", priority:%u, activeonly:%d",
GST_TIME_ARGS (timestamp), priority, activeonly);
GST_LOG ("objects_start:%p", comp->private->objects_start);
for (; tmp; tmp = g_list_next (tmp)) {
GnlObject *object = (GnlObject *) tmp->data;
GST_LOG_OBJECT (object,
"start: %" GST_TIME_FORMAT " , stop:%" GST_TIME_FORMAT " , duration:%"
GST_TIME_FORMAT ", priority:%u", GST_TIME_ARGS (object->start),
GST_TIME_ARGS (object->stop), GST_TIME_ARGS (object->duration),
object->priority);
if (object->start <= timestamp) {
if ((object->stop > timestamp) &&
(object->priority >= priority) &&
((!activeonly) || (object->active))) {
GST_LOG_OBJECT (comp, "adding %s: sorted to the stack",
GST_OBJECT_NAME (object));
stack = g_list_insert_sorted (stack, object,
(GCompareFunc) priority_comp);
}
} else {
GST_LOG_OBJECT (comp, "too far, stopping iteration");
break;
}
}
/* append the default source if we have one */
if ((timestamp < ((GnlObject *) comp)->stop) && comp->private->defaultobject)
stack = g_list_append (stack, comp->private->defaultobject);
/* convert that list to a stack */
tmp = stack;
ret = convert_list_to_tree (&tmp, &nstart, &nstop, &highest);
GST_DEBUG ("nstart:%" GST_TIME_FORMAT ", nstop:%" GST_TIME_FORMAT,
GST_TIME_ARGS (nstart), GST_TIME_ARGS (nstop));
if (*stop)
*stop = nstop;
if (*start)
*start = nstart;
if (highprio)
*highprio = highest;
g_list_free (stack);
return ret;
}
/*
* get_clean_toplevel_stack:
* @comp: The #GnlComposition
* @timestamp: The #GstClockTime to look at
* @stop_time: Pointer to a #GstClockTime for min stop time of returned stack
* @start_time: Pointer to a #GstClockTime for greatest start time of returned stack
*
* Returns: The new current stack for the given #GnlComposition and @timestamp.
*/
static GNode *
get_clean_toplevel_stack (GnlComposition * comp, GstClockTime * timestamp,
GstClockTime * start_time, GstClockTime * stop_time)
{
GNode *stack = NULL;
GList *tmp;
GstClockTime start = G_MAXUINT64;
GstClockTime stop = G_MAXUINT64;
guint highprio;
GST_DEBUG_OBJECT (comp, "timestamp:%" GST_TIME_FORMAT,
GST_TIME_ARGS (*timestamp));
stack = get_stack_list (comp, *timestamp, 0, TRUE, &start, &stop, &highprio);
GST_DEBUG ("start:%" GST_TIME_FORMAT ", stop:%" GST_TIME_FORMAT,
GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
if (!stack) {
GnlObject *object = NULL;
/* Case for gaps, therefore no objects at specified *timestamp */
GST_DEBUG_OBJECT (comp,
"Got empty stack, checking if it really was after the last object");
/* Find the first active object just after *timestamp */
for (tmp = comp->private->objects_start; tmp; tmp = g_list_next (tmp)) {
object = (GnlObject *) tmp->data;
if ((object->start > *timestamp) && object->active)
break;
}
if (tmp) {
GST_DEBUG_OBJECT (comp,
"Found a valid object after %" GST_TIME_FORMAT " : %s [%"
GST_TIME_FORMAT "]", GST_TIME_ARGS (*timestamp),
GST_ELEMENT_NAME (object), GST_TIME_ARGS (object->start));
*timestamp = object->start;
stack =
get_stack_list (comp, *timestamp, 0, TRUE, &start, &stop, &highprio);
}
}
GST_DEBUG ("start:%" GST_TIME_FORMAT ", stop:%" GST_TIME_FORMAT,
GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
if (stack) {
guint32 top_priority;
top_priority = ((GnlObject *) stack->data)->priority;
/* Figure out if there's anything blocking us with smaller priority */
refine_start_stop_in_region_above_priority (comp, *timestamp, start, stop,
&start, &stop, (highprio == 0) ? top_priority : highprio);
}
if (*stop_time) {
if (stack)
*stop_time = stop;
else
*stop_time = 0;
}
if (*start_time) {
if (stack)
*start_time = start;
else
*start_time = 0;
}
GST_DEBUG_OBJECT (comp,
"Returning timestamp:%" GST_TIME_FORMAT " , start_time:%" GST_TIME_FORMAT
" , stop_time:%" GST_TIME_FORMAT, GST_TIME_ARGS (*timestamp),
GST_TIME_ARGS (*start_time), GST_TIME_ARGS (*stop_time));
return stack;
}
/*
*
* UTILITY FUNCTIONS
*
*/
/*
* get_src_pad:
* element: a #GstElement
*
* Returns: The src pad for the given element. A reference was added to the
* returned pad, remove it when you don't need that pad anymore.
* Returns NULL if there's no source pad.
*/
static GstPad *
get_src_pad (GstElement * element)
{
GstIterator *it;
GstIteratorResult itres;
GstPad *srcpad;
it = gst_element_iterate_src_pads (element);
itres = gst_iterator_next (it, (gpointer) & srcpad);
if (itres != GST_ITERATOR_OK) {
GST_DEBUG ("%s doesn't have a src pad !", GST_ELEMENT_NAME (element));
srcpad = NULL;
}
gst_iterator_free (it);
return srcpad;
}
/*
*
* END OF UTILITY FUNCTIONS
*
*/
static gboolean
gnl_composition_prepare (GnlObject * object)
{
gboolean ret = TRUE;
return ret;
}
static GstStateChangeReturn
gnl_composition_change_state (GstElement * element, GstStateChange transition)
{
GnlComposition *comp = (GnlComposition *) element;
GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
switch (transition) {
case GST_STATE_CHANGE_READY_TO_PAUSED:{
GstIterator *childs;
GstIteratorResult res;
GValue val = { 0 };
/* state-lock all elements */
GST_DEBUG_OBJECT (comp,
"Setting all childs to READY and locking their state");
g_value_init (&val, G_TYPE_BOOLEAN);
g_value_set_boolean (&val, FALSE);
childs = gst_bin_iterate_elements (GST_BIN (comp));
res = gst_iterator_fold (childs,
(GstIteratorFoldFunction) lock_child_state, &val, NULL);
gst_iterator_free (childs);
}
/* set ghostpad target */
if (!(update_pipeline (comp, COMP_REAL_START (comp), TRUE, FALSE, TRUE))) {
ret = GST_STATE_CHANGE_FAILURE;
goto beach;
}
break;
case GST_STATE_CHANGE_PAUSED_TO_READY:
case GST_STATE_CHANGE_READY_TO_NULL:
gnl_composition_reset (comp);
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_PAUSED_TO_READY:
case GST_STATE_CHANGE_READY_TO_NULL:
unblock_childs (comp);
break;
default:
break;
}
beach:
return ret;
}
static gint
objects_start_compare (GnlObject * a, GnlObject * b)
{
if (a->start == b->start) {
if (a->priority < b->priority)
return -1;
if (a->priority > b->priority)
return 1;
return 0;
}
if (a->start < b->start)
return -1;
if (a->start > b->start)
return 1;
return 0;
}
static gint
objects_stop_compare (GnlObject * a, GnlObject * b)
{
if (a->stop == b->stop) {
if (a->priority < b->priority)
return -1;
if (a->priority > b->priority)
return 1;
return 0;
}
if (b->stop < a->stop)
return -1;
if (b->stop > a->stop)
return 1;
return 0;
}
static void
update_start_stop_duration (GnlComposition * comp)
{
GnlObject *obj;
GnlObject *cobj = (GnlObject *) comp;
if (!(comp->private->objects_start)) {
GST_LOG ("no objects, resetting everything to 0");
if (cobj->start) {
cobj->start = 0;
g_object_notify (G_OBJECT (cobj), "start");
}
if (cobj->duration) {
cobj->duration = 0;
g_object_notify (G_OBJECT (cobj), "duration");
}
if (cobj->stop) {
cobj->stop = 0;
g_object_notify (G_OBJECT (cobj), "stop");
}
return;
}
/* If we have a default object, the start position is 0 */
if (comp->private->defaultobject) {
if (cobj->start != 0) {
cobj->start = 0;
g_object_notify (G_OBJECT (cobj), "start");
}
} else {
/* Else it's the first object's start value */
obj = (GnlObject *) comp->private->objects_start->data;
if (obj->start != cobj->start) {
GST_LOG_OBJECT (obj, "setting start from %s to %" GST_TIME_FORMAT,
GST_OBJECT_NAME (obj), GST_TIME_ARGS (obj->start));
cobj->start = obj->start;
g_object_notify (G_OBJECT (cobj), "start");
}
}
obj = (GnlObject *) comp->private->objects_stop->data;
if (obj->stop != cobj->stop) {
GST_LOG_OBJECT (obj, "setting stop from %s to %" GST_TIME_FORMAT,
GST_OBJECT_NAME (obj), GST_TIME_ARGS (obj->stop));
if (comp->private->defaultobject) {
g_object_set (comp->private->defaultobject, "duration", obj->stop, NULL);
g_object_set (comp->private->defaultobject, "media-duration", obj->stop,
NULL);
}
comp->private->segment->stop = obj->stop;
cobj->stop = obj->stop;
g_object_notify (G_OBJECT (cobj), "stop");
}
if ((cobj->stop - cobj->start) != cobj->duration) {
cobj->duration = cobj->stop - cobj->start;
g_object_notify (G_OBJECT (cobj), "duration");
}
GST_LOG_OBJECT (comp,
"start:%" GST_TIME_FORMAT
" stop:%" GST_TIME_FORMAT
" duration:%" GST_TIME_FORMAT,
GST_TIME_ARGS (cobj->start),
GST_TIME_ARGS (cobj->stop), GST_TIME_ARGS (cobj->duration));
}
static void
no_more_pads_object_cb (GstElement * element, GnlComposition * comp)
{
GnlObject *object = (GnlObject *) element;
GNode *tmp;
GstPad *pad = NULL;
GstPad *tpad = NULL;
GST_LOG_OBJECT (element, "no more pads");
if (!(pad = get_src_pad (element)))
goto no_source;
COMP_OBJECTS_LOCK (comp);
if (comp->private->current == NULL) {
GST_DEBUG_OBJECT (comp, "current stack is empty !");
goto done;
}
tmp =
g_node_find (comp->private->current, G_IN_ORDER, G_TRAVERSE_ALL, object);
if (tmp) {
GnlCompositionEntry *entry = COMP_ENTRY (comp, object);
comp->private->waitingpads--;
GST_LOG_OBJECT (comp, "Number of waiting pads is now %d",
comp->private->waitingpads);
if (tmp->parent) {
/* child, link to parent */
/* FIXME, shouldn't we check the order in which we link to the parent ? */
if (!(gst_element_link (element, GST_ELEMENT (tmp->parent->data)))) {
GST_WARNING_OBJECT (comp, "Couldn't link %s to %s",
GST_ELEMENT_NAME (element),
GST_ELEMENT_NAME (GST_ELEMENT (tmp->parent->data)));
goto done;
}
gst_pad_set_blocked_async (pad, FALSE, (GstPadBlockCallback) pad_blocked,
comp);
}
if (comp->private->current && comp->private->waitingpads == 0) {
tpad = get_src_pad (GST_ELEMENT (comp->private->current->data));
/* There are no more waiting pads for the currently configured timeline */
/* stack. */
/* 1. set target of ghostpad to toplevel element src pad */
gnl_composition_ghost_pad_set_target (comp, tpad);
/* 2. send pending seek */
if (comp->private->childseek) {
GstEvent *childseek = comp->private->childseek;
comp->private->childseek = NULL;
GST_INFO_OBJECT (comp, "Sending pending seek on %s:%s",
GST_DEBUG_PAD_NAME (tpad));
COMP_OBJECTS_UNLOCK (comp);
if (!(gst_pad_send_event (tpad, childseek)))
GST_ERROR_OBJECT (comp, "Sending seek event failed!");
COMP_OBJECTS_LOCK (comp);
}
comp->private->childseek = NULL;
/* Check again if this element is still in the stack */
if (comp->private->current && (tmp =
g_node_find (comp->private->current, G_IN_ORDER, G_TRAVERSE_ALL,
object))) {
/* 3. unblock ghostpad */
GST_LOG_OBJECT (comp, "About to unblock top-level pad : %s:%s",
GST_DEBUG_PAD_NAME (tpad));
gst_pad_set_blocked_async (tpad, FALSE,
(GstPadBlockCallback) pad_blocked, comp);
GST_LOG_OBJECT (comp, "Unblocked top-level pad");
} else {
GST_DEBUG ("Element went away from currently configured stack");
}
}
/* deactivate nomorepads handler */
g_signal_handler_disconnect (object, entry->nomorepadshandler);
entry->nomorepadshandler = 0;
} else {
GST_LOG_OBJECT (comp,
"The following object is not in currently configured stack : %s",
GST_ELEMENT_NAME (object));
}
done:
COMP_OBJECTS_UNLOCK (comp);
if (pad)
gst_object_unref (pad);
if (tpad)
gst_object_unref (tpad);
GST_DEBUG_OBJECT (comp, "end");
return;
no_source:
{
GST_LOG_OBJECT (comp, "no source pad");
return;
}
}
/*
* recursive depth-first relink stack function on new stack
*
* _ relink nodes with changed parent/order
* _ links new nodes with parents
* _ unblocks available source pads (except for toplevel)
*
* WITH OBJECTS LOCK TAKEN
*/
static void
compare_relink_single_node (GnlComposition * comp, GNode * node,
GNode * oldstack)
{
GNode *child;
GNode *oldnode = NULL;
GnlObject *newobj;
GnlObject *newparent;
GnlObject *oldparent = NULL;
GstPad *srcpad = NULL;
if (!node)
return;
newparent = G_NODE_IS_ROOT (node) ? NULL : (GnlObject *) node->parent->data;
newobj = (GnlObject *) node->data;
if (oldstack) {
oldnode = g_node_find (oldstack, G_IN_ORDER, G_TRAVERSE_ALL, newobj);
if (oldnode)
oldparent =
G_NODE_IS_ROOT (oldnode) ? NULL : (GnlObject *) oldnode->parent->data;
}
GST_DEBUG_OBJECT (comp, "newobj:%s",
GST_ELEMENT_NAME ((GstElement *) newobj));
srcpad = get_src_pad ((GstElement *) newobj);
if (srcpad) {
gst_pad_set_blocked_async (srcpad, TRUE, (GstPadBlockCallback) pad_blocked,
comp);
}
if (GNL_IS_OPERATION (newobj)) {
guint nbchilds = g_node_n_children (node);
GnlOperation *oper = (GnlOperation *) newobj;
GST_LOG_OBJECT (newobj, "is operation, analyzing the %d childs", nbchilds);
if (oper->dynamicsinks)
g_object_set (G_OBJECT (newobj), "sinks", nbchilds, NULL);
for (child = node->children; child; child = child->next)
compare_relink_single_node (comp, child, oldstack);
if (nbchilds < oper->num_sinks)
GST_ERROR ("ACHTUNG ! NOT ENOUGH SINKPADS ! %d / %d",
oper->num_sinks, nbchilds);
if (nbchilds == 0)
GST_ERROR ("ACHTUNG ! OPERATION HAS NO CHILDS !!!");
/* Make sure we have enough sinkpads */
} else {
/* FIXME : do we need to do something specific for sources ? */
}
if (srcpad) {
GST_LOG_OBJECT (newobj, "has a valid source pad");
/* POST PROCESSING */
if ((oldparent != newparent) ||
(oldparent && newparent &&
(g_node_child_index (node, newobj) != g_node_child_index (oldnode,
newobj)))) {
GST_LOG_OBJECT (newobj,
"not same parent, or same parent but in different order");
/* relink to new parent in required order */
if (newparent) {
/* FIXME : do it in required order */
if (!(gst_element_link ((GstElement *) newobj,
(GstElement *) newparent)))
GST_ERROR_OBJECT (comp, "Couldn't link %s to %s",
GST_ELEMENT_NAME (newobj), GST_ELEMENT_NAME (newparent));
}
} else
GST_LOG_OBJECT (newobj, "Same parent and same position in the new stack");
/* the new root handling is taken care of in the global compare_relink_stack() */
if (!G_NODE_IS_ROOT (node))
gst_pad_set_blocked_async (srcpad, FALSE,
(GstPadBlockCallback) pad_blocked, comp);
} else {
GnlCompositionEntry *entry = COMP_ENTRY (comp, newobj);
GST_LOG_OBJECT (newobj, "no existing pad, connecting to 'no-more-pads'");
comp->private->waitingpads++;
if (!(entry->nomorepadshandler))
entry->nomorepadshandler = g_signal_connect
(G_OBJECT (newobj), "no-more-pads",
G_CALLBACK (no_more_pads_object_cb), comp);
}
if (srcpad)
gst_object_unref (srcpad);
GST_LOG_OBJECT (newobj, "DONE");
}
/*
* recursive depth-first compare stack function on old stack
*
* _ Add no-longer used objects to the deactivate list
* _ unlink child-parent relations that have changed (not same parent, or not same order)
* _ blocks available source pads
*
* WITH OBJECTS LOCK TAKEN
*/
static GList *
compare_deactivate_single_node (GnlComposition * comp, GNode * node,
GNode * newstack, gboolean modify)
{
GNode *child;
GNode *newnode = NULL; /* Same node in newstack */
GnlObject *oldparent;
GList *deactivate = NULL;
GnlObject *oldobj = NULL;
GstPad *srcpad = NULL;
if (!node)
return NULL;
oldparent = G_NODE_IS_ROOT (node) ? NULL : (GnlObject *) node->parent->data;
oldobj = (GnlObject *) node->data;
if (newstack)
newnode = g_node_find (newstack, G_IN_ORDER, G_TRAVERSE_ALL, oldobj);
GST_DEBUG_OBJECT (comp, "oldobj:%s",
GST_ELEMENT_NAME ((GstElement *) oldobj));
srcpad = get_src_pad ((GstElement *) oldobj);
/* PRE PROCESSING */
if (srcpad) {
gst_pad_set_blocked_async (srcpad, TRUE, (GstPadBlockCallback) pad_blocked,
comp);
if (!oldparent) {
/* previous root of the tree */
/* if modify, send flush events downstream */
if (modify) {
GstPad *peerpad = NULL;
if ((peerpad = gst_pad_get_peer (srcpad))) {
GST_DEBUG_OBJECT (comp, "Sending flush events downstream");
gst_pad_send_event (peerpad, gst_event_new_flush_start ());
gst_pad_send_event (peerpad, gst_event_new_flush_stop ());
gst_object_unref (peerpad);
GST_DEBUG_OBJECT (comp, "DONE Sending flush events downstream");
} else
GST_WARNING_OBJECT (comp,
"Top level object wasn't connected to a peerpad. Can't send downstream flushes");
}
}
}
/* remove the target of the ghostpad */
if (!oldparent && comp->private->ghostpad) {
GST_DEBUG_OBJECT (comp,
"Setting ghostpad target to NULL so oldobj srcpad is no longer linked");
gnl_composition_ghost_pad_set_target (comp, NULL);
}
/* Optionnal OPERATION PROCESSING */
if (GNL_IS_OPERATION (oldobj)) {
for (child = node->children; child; child = child->next) {
GList *newdeac =
compare_deactivate_single_node (comp, child, newstack, modify);
if (newdeac)
deactivate = g_list_concat (deactivate, newdeac);
}
} else {
/* FIXME : do we need to do something specific for sources ? */
}
/* POST PROCESSING */
if (newnode) {
GnlObject *newparent =
G_NODE_IS_ROOT (newnode) ? NULL : (GnlObject *) newnode->parent->data;
GST_LOG_OBJECT (oldobj, "exists in new stack");
if ((oldparent != newparent) ||
(oldparent && newparent &&
(g_node_child_index (node, oldobj) != g_node_child_index (newnode,
oldobj)))) {
GST_LOG_OBJECT (comp,
"not same parent, or same parent but in different order");
/* unlink */
if (oldparent) {
GstPad *peerpad = NULL;
if (srcpad)
peerpad = gst_pad_get_peer (srcpad);
gst_element_unlink ((GstElement *) oldobj, (GstElement *) oldparent);
/* send flush start / flush stop */
if (peerpad) {
GST_LOG_OBJECT (peerpad, "Sending flush start/stop");
gst_pad_send_event (peerpad, gst_event_new_flush_start ());
gst_pad_send_event (peerpad, gst_event_new_flush_stop ());
gst_object_unref (peerpad);
}
}
} else {
GST_LOG_OBJECT (comp, "same parent, same order");
}
} else {
/* no longer used in new stack */
GST_LOG_OBJECT (comp, "%s not used anymore", GST_ELEMENT_NAME (oldobj));
if (oldparent) {
GstPad *peerpad = NULL;
/* unlink from oldparent */
GST_LOG_OBJECT (comp, "unlinking from previous parent");
gst_element_unlink ((GstElement *) oldobj, (GstElement *) oldparent);
if (srcpad && (peerpad = gst_pad_get_peer (srcpad))) {
GST_LOG_OBJECT (peerpad, "Sending flush start/stop");
gst_pad_send_event (peerpad, gst_event_new_flush_start ());
gst_pad_send_event (peerpad, gst_event_new_flush_stop ());
gst_object_unref (peerpad);
}
}
GST_LOG_OBJECT (comp, "adding %s to deactivate list",
GST_ELEMENT_NAME (oldobj));
deactivate = g_list_append (deactivate, oldobj);
}
/* only unblock if it's not the ROOT */
if (srcpad)
gst_object_unref (srcpad);
return deactivate;
}
/*
* compare_relink_stack:
* @comp: The #GnlComposition
* @stack: The new stack
* @modify: TRUE if the timeline has changed and needs downstream flushes.
*
* Compares the given stack to the current one and relinks it if needed.
*
* WITH OBJECTS LOCK TAKEN
*
* Returns: The #GList of #GnlObject no longer used
*/
static GList *
compare_relink_stack (GnlComposition * comp, GNode * stack, gboolean modify)
{
GList *deactivate = NULL;
/* 1. reset waiting pads for new stack */
comp->private->waitingpads = 0;
/* 2. Traverse old stack to deactivate no longer used objects */
deactivate =
compare_deactivate_single_node (comp, comp->private->current, stack,
modify);
/* 3. Traverse new stack to do needed (re)links */
compare_relink_single_node (comp, stack, comp->private->current);
return deactivate;
}
static void
unlock_activate_stack (GnlComposition * comp, GNode * node,
gboolean change_state, GstState state)
{
GNode *child;
GST_LOG_OBJECT (comp, "object:%s",
GST_ELEMENT_NAME ((GstElement *) (node->data)));
gst_element_set_locked_state ((GstElement *) (node->data), FALSE);
if (change_state)
gst_element_set_state (GST_ELEMENT (node->data), state);
for (child = node->children; child; child = child->next)
unlock_activate_stack (comp, child, change_state, state);
}
static gboolean
are_same_stacks (GNode * stack1, GNode * stack2)
{
gboolean res = FALSE;
/* TODO : FIXME : we should also compare start/media-start */
/* stacks are not equal if one of them is NULL but not the other */
if ((!stack1 && stack2) || (stack1 && !stack2))
goto beach;
if (stack1 && stack2) {
GNode *child1, *child2;
/* if they don't contain the same source, not equal */
if (!(stack1->data == stack2->data))
goto beach;
/* if they don't have the same number of childs, not equal */
if (!(g_node_n_children (stack1) == g_node_n_children (stack2)))
goto beach;
child1 = stack1->children;
child2 = stack2->children;
while (child1 && child2) {
if (!(are_same_stacks (child1, child2)))
goto beach;
child1 = g_node_next_sibling (child1);
child2 = g_node_next_sibling (child2);
}
/* if there's a difference in child number, stacks are not equal */
if (child1 || child2)
goto beach;
}
/* if stack1 AND stack2 are NULL, then they're equal (both empty) */
res = TRUE;
beach:
GST_LOG ("Stacks are equal : %d", res);
return res;
}
/*
* update_pipeline:
* @comp: The #GnlComposition
* @currenttime: The #GstClockTime to update at, can be GST_CLOCK_TIME_NONE.
* @initial: TRUE if this is the first setup
* @change_state: Change the state of the (de)activated objects if TRUE.
* @modify: Flush downstream if TRUE. Needed for modified timelines.
*
* Updates the internal pipeline and properties. If @currenttime is
* GST_CLOCK_TIME_NONE, it will not modify the current pipeline
*
* Returns: FALSE if there was an error updating the pipeline.
*/
static gboolean
update_pipeline (GnlComposition * comp, GstClockTime currenttime,
gboolean initial, gboolean change_state, gboolean modify)
{
gboolean ret = TRUE;
GST_DEBUG_OBJECT (comp,
"currenttime:%" GST_TIME_FORMAT
" initial:%d , change_state:%d , modify:%d", GST_TIME_ARGS (currenttime),
initial, change_state, modify);
COMP_OBJECTS_LOCK (comp);
update_start_stop_duration (comp);
if ((GST_CLOCK_TIME_IS_VALID (currenttime))) {
GstState state = GST_STATE (comp);
GstState nextstate =
(GST_STATE_NEXT (comp) ==
GST_STATE_VOID_PENDING) ? GST_STATE (comp) : GST_STATE_NEXT (comp);
GNode *stack = NULL;
GList *deactivate = NULL;
GstClockTime new_start = GST_CLOCK_TIME_NONE;
GstClockTime new_stop = GST_CLOCK_TIME_NONE;
gboolean samestack = FALSE;
gboolean startchanged, stopchanged;
GST_DEBUG_OBJECT (comp,
"now really updating the pipeline, current-state:%s",
gst_element_state_get_name (state));
/* Flush pending segment messages */
COMP_MESSAGES_LOCK (comp);
flush_messages (comp);
COMP_MESSAGES_UNLOCK (comp);
/* (re)build the stack and relink new elements */
stack =
get_clean_toplevel_stack (comp, ¤ttime, &new_start, &new_stop);
samestack = are_same_stacks (comp->private->current, stack);
if (!samestack)
deactivate = compare_relink_stack (comp, stack, modify);
startchanged = comp->private->segment_start != currenttime;
stopchanged = comp->private->segment_stop != new_stop;
/* set new segment_start/stop */
comp->private->segment_start = currenttime;
comp->private->segment_stop = new_stop;
/* Clear pending child seek */
if (comp->private->childseek) {
GST_DEBUG ("unreffing event %p", comp->private->childseek);
gst_event_unref (comp->private->childseek);
comp->private->childseek = NULL;
}
/* activate new stack */
if (comp->private->current)
g_node_destroy (comp->private->current);
comp->private->current = NULL;
COMP_OBJECTS_UNLOCK (comp);
if (deactivate) {
GList *tmp;
GST_DEBUG_OBJECT (comp, "De-activating objects no longer used");
/* state-lock elements no more used */
for (tmp = deactivate; tmp; tmp = g_list_next (tmp)) {
GST_LOG ("%p", tmp->data);
if (change_state)
gst_element_set_state (GST_ELEMENT (tmp->data), state);
gst_element_set_locked_state (GST_ELEMENT (tmp->data), TRUE);
}
g_list_free (deactivate);
GST_DEBUG_OBJECT (comp, "Finished de-activating objects no longer used");
}
comp->private->current = stack;
GST_DEBUG_OBJECT (comp, "activating objects in new stack to %s",
gst_element_state_get_name (nextstate));
if (!samestack && stack)
unlock_activate_stack (comp, stack, change_state, nextstate);
GST_DEBUG_OBJECT (comp, "Finished activating objects in new stack");
if (comp->private->current) {
GstEvent *event;
/* There is a valid timeline stack */
COMP_OBJECTS_LOCK (comp);
/* 1. Create new seek event for newly configured timeline stack */
if (samestack && stopchanged)
event =
get_new_seek_event (comp,
(state == GST_STATE_PLAYING) ? FALSE : TRUE, TRUE);
else
event = get_new_seek_event (comp, initial, FALSE);
/* 2. Is the stack entirely ready ? */
if (comp->private->waitingpads == 0) {
GstPad *pad = NULL;
/* 2.a. Stack is entirely ready */
/* 3. Get toplevel object source pad */
if ((pad = get_src_pad (GST_ELEMENT (comp->private->current->data)))) {
GST_DEBUG_OBJECT (comp, "We have a valid toplevel element pad %s:%s",
GST_DEBUG_PAD_NAME (pad));
/* 4. Unconditionnaly set the ghostpad target to pad */
GST_LOG_OBJECT (comp,
"Setting the composition's ghostpad target to %s:%s",
GST_DEBUG_PAD_NAME (pad));
gnl_composition_ghost_pad_set_target (comp, pad);
/* 5. send seek event */
GST_LOG_OBJECT (comp, "sending seek event");
if (!(gst_pad_send_event (pad, event))) {
ret = FALSE;
} else {
/* 6. unblock top-level pad */
GST_LOG_OBJECT (comp, "About to unblock top-level srcpad");
gst_pad_set_blocked_async (pad, FALSE,
(GstPadBlockCallback) pad_blocked, comp);
}
gst_object_unref (pad);
} else {
GST_WARNING_OBJECT (comp,
"Timeline is entirely linked, but couldn't get top-level element's source pad");
ret = FALSE;
}
} else {
/* 2.b. Stack isn't entirely ready, save seek event for later on */
GST_LOG_OBJECT (comp,
"The timeline stack isn't entirely linked, delaying sending seek event (waitingpads:%d)",
comp->private->waitingpads);
comp->private->childseek = event;
ret = TRUE;
}
COMP_OBJECTS_UNLOCK (comp);
} else {
if ((!comp->private->objects_start) && comp->private->ghostpad) {
GST_DEBUG_OBJECT (comp, "composition is now empty, removing ghostpad");
gnl_object_remove_ghost_pad ((GnlObject *) comp,
comp->private->ghostpad);
comp->private->ghostpad = NULL;
}
}
} else {
COMP_OBJECTS_UNLOCK (comp);
}
GST_DEBUG_OBJECT (comp, "Returning %d", ret);
return ret;
}
/*
* Child modification updates
*/
static void
object_start_changed (GnlObject * object, GParamSpec * arg,
GnlComposition * comp)
{
GST_DEBUG_OBJECT (object, "start position changed (%" GST_TIME_FORMAT
"), evaluating pipeline update", GST_TIME_ARGS (object->start));
comp->private->objects_start = g_list_sort
(comp->private->objects_start, (GCompareFunc) objects_start_compare);
comp->private->objects_stop = g_list_sort
(comp->private->objects_stop, (GCompareFunc) objects_stop_compare);
if (comp->private->current && OBJECT_IN_ACTIVE_SEGMENT (comp, object)) {
GstClockTime curpos = get_current_position (comp);
if (curpos == GST_CLOCK_TIME_NONE)
curpos = comp->private->segment->start = comp->private->segment_start;
update_pipeline (comp, curpos, TRUE, TRUE, TRUE);
} else
update_start_stop_duration (comp);
}
static void
object_stop_changed (GnlObject * object, GParamSpec * arg,
GnlComposition * comp)
{
GST_DEBUG_OBJECT (object, "stop position changed (%" GST_TIME_FORMAT
"), evaluating pipeline update", GST_TIME_ARGS (object->stop));
comp->private->objects_stop = g_list_sort
(comp->private->objects_stop, (GCompareFunc) objects_stop_compare);
comp->private->objects_start = g_list_sort
(comp->private->objects_start, (GCompareFunc) objects_start_compare);
if (comp->private->current && OBJECT_IN_ACTIVE_SEGMENT (comp, object)) {
GstClockTime curpos = get_current_position (comp);
if (curpos == GST_CLOCK_TIME_NONE)
curpos = comp->private->segment->start = comp->private->segment_start;
update_pipeline (comp, curpos, TRUE, TRUE, TRUE);
} else
update_start_stop_duration (comp);
}
static void
object_priority_changed (GnlObject * object, GParamSpec * arg,
GnlComposition * comp)
{
GST_DEBUG_OBJECT (object, "priority changed (%u), evaluating pipeline update",
object->priority);
comp->private->objects_start = g_list_sort
(comp->private->objects_start, (GCompareFunc) objects_start_compare);
comp->private->objects_stop = g_list_sort
(comp->private->objects_stop, (GCompareFunc) objects_stop_compare);
if (comp->private->current && OBJECT_IN_ACTIVE_SEGMENT (comp, object)) {
GstClockTime curpos = get_current_position (comp);
if (curpos == GST_CLOCK_TIME_NONE)
curpos = comp->private->segment->start = comp->private->segment_start;
update_pipeline (comp, curpos, TRUE, TRUE, TRUE);
} else
update_start_stop_duration (comp);
}
static void
object_active_changed (GnlObject * object, GParamSpec * arg,
GnlComposition * comp)
{
GST_DEBUG_OBJECT (object,
"active flag changed (%d), evaluating pipeline update", object->active);
if (comp->private->current && OBJECT_IN_ACTIVE_SEGMENT (comp, object)) {
GstClockTime curpos = get_current_position (comp);
if (curpos == GST_CLOCK_TIME_NONE)
curpos = comp->private->segment->start = comp->private->segment_start;
update_pipeline (comp, curpos, TRUE, TRUE, TRUE);
} else
update_start_stop_duration (comp);
}
static void
object_pad_removed (GnlObject * object, GstPad * pad, GnlComposition * comp)
{
GST_DEBUG_OBJECT (comp, "pad %s:%s was removed", GST_DEBUG_PAD_NAME (pad));
/* remove ghostpad if it's the current top stack object */
if (comp->private->current
&& ((GnlObject *) comp->private->current->data == object)
&& comp->private->ghostpad) {
GST_DEBUG_OBJECT (comp, "Removing ghostpad");
gnl_object_remove_ghost_pad ((GnlObject *) comp, comp->private->ghostpad);
comp->private->ghostpad = NULL;
} else {
/* unblock it ! */
gst_pad_set_blocked_async (pad, FALSE, (GstPadBlockCallback) pad_blocked,
comp);
}
}
static void
object_pad_added (GnlObject * object, GstPad * pad, GnlComposition * comp)
{
if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK)
return;
GST_DEBUG_OBJECT (comp, "pad %s:%s was added, blocking it",
GST_DEBUG_PAD_NAME (pad));
gst_pad_set_blocked_async (pad, TRUE, (GstPadBlockCallback) pad_blocked,
comp);
}
static gboolean
gnl_composition_add_object (GstBin * bin, GstElement * element)
{
gboolean ret;
GnlCompositionEntry *entry;
GnlComposition *comp = (GnlComposition *) bin;
GstClockTime curpos;
GST_DEBUG_OBJECT (bin, "element %s", GST_OBJECT_NAME (element));
/* we only accept GnlObject */
g_return_val_if_fail (GNL_IS_OBJECT (element), FALSE);
gst_object_ref (element);
GST_DEBUG_OBJECT (element, "%" GST_TIME_FORMAT "--%" GST_TIME_FORMAT,
GST_TIME_ARGS (((GnlObject *) element)->start),
GST_TIME_ARGS (((GnlObject *) element)->stop));
COMP_OBJECTS_LOCK (comp);
if ((((GnlObject *) element)->priority == G_MAXUINT32)
&& comp->private->defaultobject) {
GST_WARNING_OBJECT (comp,
"We already have a default source, remove it before adding new one");
ret = FALSE;
goto chiringuito;
}
ret = GST_BIN_CLASS (parent_class)->add_element (bin, element);
if (!ret) {
GST_WARNING_OBJECT (bin, "couldn't add element");
goto chiringuito;
}
/* lock state of child ! */
GST_LOG_OBJECT (bin, "Locking state of %s", GST_ELEMENT_NAME (element));
gst_element_set_locked_state (element, TRUE);
/* wrap new element in a GnlCompositionEntry ... */
entry = g_new0 (GnlCompositionEntry, 1);
entry->object = (GnlObject *) element;
if ((((GnlObject *) element)->priority != G_MAXUINT32)) {
/* Only react on non-default objects properties */
entry->starthandler = g_signal_connect (G_OBJECT (element),
"notify::start", G_CALLBACK (object_start_changed), comp);
entry->stophandler = g_signal_connect (G_OBJECT (element),
"notify::stop", G_CALLBACK (object_stop_changed), comp);
entry->priorityhandler = g_signal_connect (G_OBJECT (element),
"notify::priority", G_CALLBACK (object_priority_changed), comp);
} else {
/* We set the default source start/stop values to 0 and composition-stop */
g_object_set (element,
"start", (GstClockTime) 0,
"media-start", (GstClockTime) 0,
"duration", (GstClockTimeDiff) ((GnlObject *) comp)->stop,
"media-duration", (GstClockTimeDiff) ((GnlObject *) comp)->stop, NULL);
}
entry->activehandler = g_signal_connect (G_OBJECT (element),
"notify::active", G_CALLBACK (object_active_changed), comp);
entry->padremovedhandler = g_signal_connect (G_OBJECT (element),
"pad-removed", G_CALLBACK (object_pad_removed), comp);
entry->padaddedhandler = g_signal_connect (G_OBJECT (element),
"pad-added", G_CALLBACK (object_pad_added), comp);
/* ...and add it to the hash table */
g_hash_table_insert (comp->private->objects_hash, element, entry);
/* Special case for default source. */
if (((GnlObject *) element)->priority == G_MAXUINT32) {
/* It doesn't get added to objects_start and objects_stop. */
comp->private->defaultobject = ((GnlObject *) element);
goto chiringuito;
}
/* add it sorted to the objects list */
comp->private->objects_start = g_list_append
(comp->private->objects_start, element);
comp->private->objects_start = g_list_sort
(comp->private->objects_start, (GCompareFunc) objects_start_compare);
if (comp->private->objects_start)
GST_LOG_OBJECT (comp,
"Head of objects_start is now %s [%" GST_TIME_FORMAT "--%"
GST_TIME_FORMAT "]",
GST_OBJECT_NAME (comp->private->objects_start->data),
GST_TIME_ARGS (((GnlObject *) comp->private->objects_start->data)->
start),
GST_TIME_ARGS (((GnlObject *) comp->private->objects_start->data)->
stop));
comp->private->objects_stop = g_list_append
(comp->private->objects_stop, element);
comp->private->objects_stop = g_list_sort
(comp->private->objects_stop, (GCompareFunc) objects_stop_compare);
if (comp->private->objects_stop)
GST_LOG_OBJECT (comp,
"Head of objects_stop is now %s [%" GST_TIME_FORMAT "--%"
GST_TIME_FORMAT "]",
GST_OBJECT_NAME (comp->private->objects_stop->data),
GST_TIME_ARGS (((GnlObject *) comp->private->objects_stop->data)->
start),
GST_TIME_ARGS (((GnlObject *) comp->private->objects_stop->data)->
stop));
GST_DEBUG_OBJECT (comp,
"segment_start:%" GST_TIME_FORMAT " segment_stop:%" GST_TIME_FORMAT,
GST_TIME_ARGS (comp->private->segment_start),
GST_TIME_ARGS (comp->private->segment_stop));
if ((curpos = get_current_position (comp)) == GST_CLOCK_TIME_NONE)
curpos = comp->private->segment_start;
COMP_OBJECTS_UNLOCK (comp);
/* If we added within currently configured segment OR the pipeline was *
* previously empty, THEN update pipeline */
if (OBJECT_IN_ACTIVE_SEGMENT (comp, element) || (!comp->private->current))
update_pipeline (comp, curpos, TRUE, TRUE, TRUE);
else
update_start_stop_duration (comp);
beach:
gst_object_unref (element);
return ret;
chiringuito:
COMP_OBJECTS_UNLOCK (comp);
update_start_stop_duration (comp);
goto beach;
}
static gboolean
gnl_composition_remove_object (GstBin * bin, GstElement * element)
{
gboolean ret = GST_STATE_CHANGE_FAILURE;
GnlComposition *comp = (GnlComposition *) bin;
GstClockTime curpos;
GST_DEBUG_OBJECT (bin, "element %s", GST_OBJECT_NAME (element));
/* we only accept GnlObject */
g_return_val_if_fail (GNL_IS_OBJECT (element), FALSE);
COMP_OBJECTS_LOCK (comp);
gst_object_ref (element);
gst_element_set_locked_state (element, FALSE);
/* handle default source */
if (((GnlObject *) element)->priority == G_MAXUINT32) {
comp->private->defaultobject = NULL;
} else {
/* remove it from the objects list and resort the lists */
comp->private->objects_start = g_list_remove
(comp->private->objects_start, element);
comp->private->objects_start = g_list_sort
(comp->private->objects_start, (GCompareFunc) objects_start_compare);
comp->private->objects_stop = g_list_remove
(comp->private->objects_stop, element);
comp->private->objects_stop = g_list_sort
(comp->private->objects_stop, (GCompareFunc) objects_stop_compare);
GST_LOG_OBJECT (element, "Removed from the objects start/stop list");
}
if (!(g_hash_table_remove (comp->private->objects_hash, element)))
goto chiringuito;
if ((curpos = get_current_position (comp)) == GST_CLOCK_TIME_NONE)
curpos = comp->private->segment_start;
COMP_OBJECTS_UNLOCK (comp);
/* If we removed within currently configured segment, or it was the default source, *
* update pipeline */
if (OBJECT_IN_ACTIVE_SEGMENT (comp, element)
|| (((GnlObject *) element)->priority == G_MAXUINT32))
update_pipeline (comp, curpos, TRUE, TRUE, TRUE);
else
update_start_stop_duration (comp);
ret = GST_BIN_CLASS (parent_class)->remove_element (bin, element);
GST_LOG_OBJECT (element, "Done removing from the composition");
beach:
/* unblock source pad */
if (1) {
GstPad *pad = get_src_pad (element);
if (pad) {
gst_pad_set_blocked_async (pad, FALSE, (GstPadBlockCallback) pad_blocked,
comp);
gst_object_unref (pad);
}
}
gst_object_unref (element);
return ret;
chiringuito:
COMP_OBJECTS_UNLOCK (comp);
goto beach;
}
|