File: ges-timeline.c

package info (click to toggle)
gstreamer-editing-services1.0 1.26.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 5,192 kB
  • sloc: ansic: 63,500; python: 4,167; lex: 57; makefile: 28; cpp: 20
file content (3481 lines) | stat: -rw-r--r-- 109,193 bytes parent folder | download | duplicates (2)
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
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
/* GStreamer Editing Services
 * Copyright (C) 2009 Edward Hervey <edward.hervey@collabora.co.uk>
 *               2009 Nokia Corporation
 *               2012 Thibault Saunier <tsaunier@gnome.org>
 *               2012 Collabora Ltd.
 *                 Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
 *               2019 Igalia S.L
 *                 Author: Thibault Saunier <tsaunier@igalia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

/**
 * SECTION:gestimeline
 * @title: GESTimeline
 * @short_description: Multimedia timeline
 *
 * #GESTimeline is the central object for any multimedia timeline.
 *
 * A timeline is composed of a set of #GESTrack-s and a set of
 * #GESLayer-s, which are added to the timeline using
 * ges_timeline_add_track() and ges_timeline_append_layer(), respectively.
 *
 * The contained tracks define the supported types of the timeline
 * and provide the media output. Essentially, each track provides an
 * additional source #GstPad.
 *
 * Most usage of a timeline will likely only need a single #GESAudioTrack
 * and/or a single #GESVideoTrack. You can create such a timeline with
 * ges_timeline_new_audio_video(). After this, you are unlikely to need to
 * work with the tracks directly.
 *
 * A timeline's layers contain #GESClip-s, which in turn control the
 * creation of #GESTrackElement-s, which are added to the timeline's
 * tracks. See #GESTimeline::select-tracks-for-object if you wish to have
 * more control over which track a clip's elements are added to.
 *
 * The layers are ordered, with higher priority layers having their
 * content prioritised in the tracks. This ordering can be changed using
 * ges_timeline_move_layer().
 *
 * ## Editing
 *
 * See #GESTimelineElement for the various ways the elements of a timeline
 * can be edited.
 *
 * If you change the timing or ordering of a timeline's
 * #GESTimelineElement-s, then these changes will not actually be taken
 * into account in the output of the timeline's tracks until the
 * ges_timeline_commit() method is called. This allows you to move its
 * elements around, say, in response to an end user's mouse dragging, with
 * little expense before finalising their effect on the produced data.
 *
 * ## Overlaps and Auto-Transitions
 *
 * There are certain restrictions placed on how #GESSource-s may overlap
 * in a #GESTrack that belongs to a timeline. These will be enforced by
 * GES, so the user will not need to keep track of them, but they should
 * be aware that certain edits will be refused as a result if the overlap
 * rules would be broken.
 *
 * Consider two #GESSource-s, `A` and `B`, with start times `startA` and
 * `startB`, and end times `endA` and `endB`, respectively. The start
 * time refers to their #GESTimelineElement:start, and the end time is
 * their #GESTimelineElement:start + #GESTimelineElement:duration. These
 * two sources *overlap* if:
 *
 * + they share the same #GESTrackElement:track (non %NULL), which belongs
 *   to the timeline;
 * + they share the same #GES_TIMELINE_ELEMENT_LAYER_PRIORITY; and
 * + `startA < endB` and `startB < endA `.
 *
 * Note that when `startA = endB` or `startB = endA` then the two sources
 * will *touch* at their edges, but are not considered overlapping.
 *
 * If, in addition, `startA < startB < endA`, then we can say that the
 * end of `A` overlaps the start of `B`.
 *
 * If, instead, `startA <= startB` and `endA >= endB`, then we can say
 * that `A` fully overlaps `B`.
 *
 * The overlap rules for a timeline are that:
 *
 * 1. One source cannot fully overlap another source.
 * 2. A source can only overlap the end of up to one other source at its
 *    start.
 * 3. A source can only overlap the start of up to one other source at its
 *    end.
 *
 * The last two rules combined essentially mean that at any given timeline
 * position, only up to two #GESSource-s may overlap at that position. So
 * triple or more overlaps are not allowed.
 *
 * If you switch on #GESTimeline:auto-transition, then at any moment when
 * the end of one source (the first source) overlaps the start of another
 * (the second source), a #GESTransitionClip will be automatically created
 * for the pair in the same layer and it will cover their overlap. If the
 * two elements are edited in a way such that the end of the first source
 * no longer overlaps the start of the second, the transition will be
 * automatically removed from the timeline. However, if the two sources
 * still overlap at the same edges after the edit, then the same
 * transition object will be kept, but with its timing and layer adjusted
 * accordingly.
 *
 * NOTE: if you know what you are doing and want to be in full control of the
 * timeline layout, you can disable the edit APIs with
 * #ges_timeline_disable_edit_apis.
 *
 * ## Saving
 *
 * To save/load a timeline, you can use the ges_timeline_load_from_uri()
 * and ges_timeline_save_to_uri() methods that use the default format.
 *
 * ## Playing
 *
 * A timeline is a #GstBin with a source #GstPad for each of its
 * tracks, which you can fetch with ges_timeline_get_pad_for_track(). You
 * will likely want to link these to some compatible sink #GstElement-s to
 * be able to play or capture the content of the timeline.
 *
 * You can use a #GESPipeline to easily preview/play the timeline's
 * content, or render it to a file.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "ges-internal.h"
#include "ges-project.h"
#include "ges-container.h"
#include "ges-timeline.h"
#include "ges-timeline-tree.h"
#include "ges-track.h"
#include "ges-layer.h"
#include "ges-auto-transition.h"
#include "ges.h"


static GPtrArray *select_tracks_for_object_default (GESTimeline * timeline,
    GESClip * clip, GESTrackElement * tr_obj, gpointer user_data);
static void ges_extractable_interface_init (GESExtractableInterface * iface);
static void ges_meta_container_interface_init
    (GESMetaContainerInterface * iface);

GST_DEBUG_CATEGORY_STATIC (ges_timeline_debug);
#undef GST_CAT_DEFAULT
#define GST_CAT_DEFAULT ges_timeline_debug

/* lock to protect dynamic callbacks, like pad-added */
#define DYN_LOCK(timeline) (&GES_TIMELINE (timeline)->priv->dyn_mutex)
#define LOCK_DYN(timeline) G_STMT_START {                       \
    GST_LOG_OBJECT (timeline, "Getting dynamic lock from %p", \
        g_thread_self());                                       \
    g_rec_mutex_lock (DYN_LOCK (timeline));                     \
    GST_LOG_OBJECT (timeline, "Got Dynamic lock from %p",     \
        g_thread_self());         \
  } G_STMT_END

#define UNLOCK_DYN(timeline) G_STMT_START {                         \
    GST_LOG_OBJECT (timeline, "Unlocking dynamic lock from %p", \
        g_thread_self());                                         \
    g_rec_mutex_unlock (DYN_LOCK (timeline));                     \
    GST_LOG_OBJECT (timeline, "Unlocked Dynamic lock from %p",  \
        g_thread_self());         \
  } G_STMT_END

#define CHECK_THREAD(timeline) g_assert(timeline->priv->valid_thread == g_thread_self())

struct _GESTimelinePrivate
{
  GNode *tree;

  /* The duration of the timeline */
  gint64 duration;

  /* The auto-transition of the timeline */
  gboolean auto_transition;

  /* Timeline edition modes and snapping management */
  guint64 snapping_distance;

  GRecMutex dyn_mutex;
  GList *priv_tracks;

  /* Avoid sorting layers when we are actually resyncing them ourself */
  gboolean resyncing_layers;
  GList *auto_transitions;

  /* Last snapping  properties */
  GstClockTime last_snap_ts;
  GESTrackElement *last_snaped1;
  GESTrackElement *last_snaped2;

  GESTrack *auto_transition_track;
  GESTrack *new_track;

  /* While we are creating and adding the TrackElements for a clip, we need to
   * ignore the child-added signal */
  gboolean track_elements_moving;
  /* whether any error occurred during track selection, including
   * programming or usage errors */
  gboolean has_any_track_selection_error;
  /* error set for non-programming/usage errors */
  GError *track_selection_error;
  GList *groups;

  guint stream_start_group_id;

  GHashTable *all_elements;

  /* With GST_OBJECT_LOCK */
  guint expected_async_done;
  /* With GST_OBJECT_LOCK */
  guint expected_commited;

  /* For ges_timeline_commit_sync */
  GMutex commited_lock;
  GCond commited_cond;
  gboolean commit_frozen;
  gboolean commit_delayed;

  GThread *valid_thread;
  gboolean disposed;

  GstStreamCollection *stream_collection;

  gboolean rendering_smartly;
  gboolean disable_edit_apis;
};

/* private structure to contain our track-related information */

typedef struct
{
  GESTimeline *timeline;
  GESTrack *track;
  GstPad *pad;                  /* Pad from the track */
  GstPad *ghostpad;
  gulong track_element_added_sigid;

  gulong probe_id;
  GstStream *stream;
} TrackPrivate;

enum
{
  PROP_0,
  PROP_DURATION,
  PROP_AUTO_TRANSITION,
  PROP_SNAPPING_DISTANCE,
  PROP_UPDATE,
  PROP_LAST
};

static GParamSpec *properties[PROP_LAST];

enum
{
  TRACK_ADDED,
  TRACK_REMOVED,
  LAYER_ADDED,
  LAYER_REMOVED,
  GROUP_ADDED,
  GROUP_REMOVED,
  SNAPING_STARTED,
  SNAPING_ENDED,
  SELECT_TRACKS_FOR_OBJECT,
  COMMITED,
  SELECT_ELEMENT_TRACK,
  LAST_SIGNAL
};

G_DEFINE_TYPE_WITH_CODE (GESTimeline, ges_timeline, GST_TYPE_BIN,
    G_ADD_PRIVATE (GESTimeline)
    G_IMPLEMENT_INTERFACE (GES_TYPE_EXTRACTABLE, ges_extractable_interface_init)
    G_IMPLEMENT_INTERFACE (GES_TYPE_META_CONTAINER,
        ges_meta_container_interface_init));

static GstBinClass *parent_class;

static guint ges_timeline_signals[LAST_SIGNAL] = { 0 };

static gint custom_find_track (TrackPrivate * tr_priv, GESTrack * track);

static guint nb_assets = 0;

/* GESExtractable implementation */
static gchar *
extractable_check_id (GType type, const gchar * id)
{
  gchar *res;

  if (id == NULL)
    res = g_strdup_printf ("%s-%i", "project", nb_assets);
  else
    res = g_strdup (id);

  nb_assets++;

  return res;
}

static gchar *
extractable_get_id (GESExtractable * self)
{
  GESAsset *asset;

  if (!(asset = ges_extractable_get_asset (self)))
    return NULL;

  return g_strdup (ges_asset_get_id (asset));
}

static void
ges_extractable_interface_init (GESExtractableInterface * iface)
{
  iface->asset_type = GES_TYPE_PROJECT;
  iface->check_id = (GESExtractableCheckId) extractable_check_id;
  iface->get_id = extractable_get_id;
  iface->can_update_asset = TRUE;
}

static void
ges_meta_container_interface_init (GESMetaContainerInterface * iface)
{
}

/* GObject Standard vmethods*/
static void
ges_timeline_get_property (GObject * object, guint property_id,
    GValue * value, GParamSpec * pspec)
{
  GESTimeline *timeline = GES_TIMELINE (object);

  switch (property_id) {
    case PROP_DURATION:
      g_value_set_uint64 (value, timeline->priv->duration);
      break;
    case PROP_AUTO_TRANSITION:
      g_value_set_boolean (value, timeline->priv->auto_transition);
      break;
    case PROP_SNAPPING_DISTANCE:
      g_value_set_uint64 (value, timeline->priv->snapping_distance);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  }
}

static void
ges_timeline_set_property (GObject * object, guint property_id,
    const GValue * value, GParamSpec * pspec)
{
  GESTimeline *timeline = GES_TIMELINE (object);

  switch (property_id) {
    case PROP_AUTO_TRANSITION:
      ges_timeline_set_auto_transition (timeline, g_value_get_boolean (value));
      break;
    case PROP_SNAPPING_DISTANCE:
      timeline->priv->snapping_distance = g_value_get_uint64 (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  }
}

gboolean
ges_timeline_is_disposed (GESTimeline * timeline)
{
  return timeline->priv->disposed;
}

static void
ges_timeline_dispose (GObject * object)
{
  GESTimeline *tl = GES_TIMELINE (object);
  GESTimelinePrivate *priv = tl->priv;
  GList *tmp, *groups;

  priv->disposed = TRUE;
  while (tl->layers) {
    GESLayer *layer = (GESLayer *) tl->layers->data;
    ges_timeline_remove_layer (GES_TIMELINE (object), layer);
  }

  /* FIXME: it should be possible to remove tracks before removing
   * layers, but at the moment this creates a problem because the track
   * objects aren't notified that their nleobjects have been destroyed.
   */

  LOCK_DYN (tl);
  while (tl->tracks)
    ges_timeline_remove_track (GES_TIMELINE (object), tl->tracks->data);
  UNLOCK_DYN (tl);

  /* NOTE: the timeline should not contain empty groups */
  groups = g_list_copy_deep (priv->groups, (GCopyFunc) gst_object_ref, NULL);
  for (tmp = groups; tmp; tmp = tmp->next) {
    GList *elems = ges_container_ungroup (tmp->data, FALSE);

    g_list_free_full (elems, gst_object_unref);
  }
  g_list_free_full (groups, gst_object_unref);
  g_list_free_full (priv->groups, gst_object_unref);

  g_list_free_full (priv->auto_transitions, gst_object_unref);

  g_hash_table_unref (priv->all_elements);
  gst_object_unref (priv->stream_collection);

  gst_clear_object (&priv->auto_transition_track);
  gst_clear_object (&priv->new_track);
  g_clear_error (&priv->track_selection_error);
  priv->track_selection_error = NULL;

  G_OBJECT_CLASS (ges_timeline_parent_class)->dispose (object);
}

static void
ges_timeline_finalize (GObject * object)
{
  GESTimeline *tl = GES_TIMELINE (object);

  g_rec_mutex_clear (&tl->priv->dyn_mutex);
  g_node_destroy (tl->priv->tree);

  G_OBJECT_CLASS (ges_timeline_parent_class)->finalize (object);
}

static void
ges_timeline_handle_message (GstBin * bin, GstMessage * message)
{
  GESTimeline *timeline = GES_TIMELINE (bin);

  if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ASYNC_START) {
    GST_INFO_OBJECT (timeline, "Dropping %" GST_PTR_FORMAT, message);
    gst_message_unref (message);
    return;
  }

  if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ASYNC_DONE) {
    GST_INFO_OBJECT (timeline, "Dropping %" GST_PTR_FORMAT, message);
    gst_message_unref (message);
    return;
  }

  if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ELEMENT) {
    GstMessage *amessage = NULL;
    const GstStructure *mstructure = gst_message_get_structure (message);

    if (gst_structure_has_name (mstructure, "NleCompositionStartUpdate")) {
      if (g_strcmp0 (gst_structure_get_string (mstructure, "reason"), "Seek")) {
        GST_INFO_OBJECT (timeline,
            "A composition is starting an update because of %s"
            " not considering async", gst_structure_get_string (mstructure,
                "reason"));

        goto forward;
      }

      GST_OBJECT_LOCK (timeline);
      if (timeline->priv->expected_async_done == 0) {
        amessage = gst_message_new_async_start (GST_OBJECT_CAST (bin));
        LOCK_DYN (timeline);
        timeline->priv->expected_async_done = g_list_length (timeline->tracks);
        UNLOCK_DYN (timeline);
        GST_INFO_OBJECT (timeline, "Posting ASYNC_START %s",
            gst_structure_get_string (mstructure, "reason"));
      }
      GST_OBJECT_UNLOCK (timeline);

    } else if (gst_structure_has_name (mstructure, "NleCompositionUpdateDone")) {
      if (g_strcmp0 (gst_structure_get_string (mstructure, "reason"), "Seek")) {
        GST_INFO_OBJECT (timeline,
            "A composition is done updating because of %s"
            " not considering async", gst_structure_get_string (mstructure,
                "reason"));

        goto forward;
      }

      GST_OBJECT_LOCK (timeline);
      timeline->priv->expected_async_done -= 1;
      if (timeline->priv->expected_async_done == 0) {
        amessage = gst_message_new_async_done (GST_OBJECT_CAST (bin),
            GST_CLOCK_TIME_NONE);
        GST_INFO_OBJECT (timeline, "Posting ASYNC_DONE %s",
            gst_structure_get_string (mstructure, "reason"));
      }
      GST_OBJECT_UNLOCK (timeline);
    } else {
      goto forward;
    }

    gst_message_unref (message);
    if (amessage) {
      gst_element_post_message (GST_ELEMENT_CAST (bin), amessage);
    }
    return;
  }

forward:
  GST_BIN_CLASS (parent_class)->handle_message (bin, message);
}

static void
ges_timeline_post_stream_collection (GESTimeline * timeline)
{
  gst_element_post_message ((GstElement *) timeline,
      gst_message_new_element ((GstObject *) timeline,
          gst_structure_new ("ges-timeline-collection", "collection",
              GST_TYPE_STREAM_COLLECTION, timeline->priv->stream_collection,
              NULL)));
}

static GstStateChangeReturn
ges_timeline_change_state (GstElement * element, GstStateChange transition)
{
  GstStateChangeReturn res;
  GESTimeline *timeline = GES_TIMELINE (element);

  res = GST_ELEMENT_CLASS (ges_timeline_parent_class)->change_state (element,
      transition);

  if (transition == GST_STATE_CHANGE_READY_TO_PAUSED)
    ges_timeline_post_stream_collection (timeline);
  return res;
}

static gboolean
ges_timeline_send_event (GstElement * element, GstEvent * event)
{
  GESTimeline *timeline = GES_TIMELINE (element);

  if (GST_EVENT_TYPE (event) == GST_EVENT_SELECT_STREAMS) {
    GList *stream_ids = NULL, *tmp, *to_remove =
        ges_timeline_get_tracks (timeline);

    gst_event_parse_select_streams (event, &stream_ids);
    for (tmp = stream_ids; tmp; tmp = tmp->next) {
      GList *trackit;
      gchar *stream_id = tmp->data;

      LOCK_DYN (timeline);
      for (trackit = timeline->priv->priv_tracks; trackit;
          trackit = trackit->next) {
        TrackPrivate *tr_priv = trackit->data;

        if (!g_strcmp0 (gst_stream_get_stream_id (tr_priv->stream), stream_id)) {
          to_remove = g_list_remove (to_remove, tr_priv->track);
        }
      }
      UNLOCK_DYN (timeline);
    }
    for (tmp = to_remove; tmp; tmp = tmp->next) {
      GST_INFO_OBJECT (timeline, "Removed unselected track: %" GST_PTR_FORMAT,
          tmp->data);
      ges_timeline_remove_track (timeline, tmp->data);
    }

    g_list_free_full (stream_ids, g_free);
    g_list_free (to_remove);

    return TRUE;
  }

  return GST_ELEMENT_CLASS (ges_timeline_parent_class)->send_event (element,
      event);
}

/* we collect the first result */
static gboolean
_gst_array_accumulator (GSignalInvocationHint * ihint,
    GValue * return_accu, const GValue * handler_return, gpointer dummy)
{
  gpointer array;

  array = g_value_get_boxed (handler_return);
  if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
    g_value_set_boxed (return_accu, array);

  return FALSE;
}

static void
ges_timeline_class_init (GESTimelineClass * klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GstElementClass *element_class = (GstElementClass *) klass;
  GstBinClass *bin_class = GST_BIN_CLASS (klass);

  GST_DEBUG_CATEGORY_INIT (ges_timeline_debug, "gestimeline",
      GST_DEBUG_FG_YELLOW, "ges timeline");
  timeline_tree_init_debug ();

  parent_class = g_type_class_peek_parent (klass);

  object_class->get_property = ges_timeline_get_property;
  object_class->set_property = ges_timeline_set_property;
  object_class->dispose = ges_timeline_dispose;
  object_class->finalize = ges_timeline_finalize;

  element_class->change_state = GST_DEBUG_FUNCPTR (ges_timeline_change_state);
  element_class->send_event = GST_DEBUG_FUNCPTR (ges_timeline_send_event);

  bin_class->handle_message = GST_DEBUG_FUNCPTR (ges_timeline_handle_message);

  /**
   * GESTimeline:duration:
   *
   * The current duration (in nanoseconds) of the timeline. A timeline
   * 'starts' at time 0, so this is the maximum end time of all of its
   * #GESTimelineElement-s.
   */
  properties[PROP_DURATION] =
      g_param_spec_uint64 ("duration", "Duration",
      "The duration of the timeline", 0, G_MAXUINT64,
      GST_CLOCK_TIME_NONE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_DURATION,
      properties[PROP_DURATION]);

  /**
   * GESTimeline:auto-transition:
   *
   * Whether to automatically create a transition whenever two
   * #GESSource-s overlap in a track of the timeline. See
   * #GESLayer:auto-transition if you want this to only happen in some
   * layers.
   */
  g_object_class_install_property (object_class, PROP_AUTO_TRANSITION,
      g_param_spec_boolean ("auto-transition", "Auto-Transition",
          "whether the transitions are added", FALSE,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  /**
   * GESTimeline:snapping-distance:
   *
   * The distance (in nanoseconds) at which a #GESTimelineElement being
   * moved within the timeline should snap one of its #GESSource-s with
   * another #GESSource-s edge. See #GESEditMode for which edges can
   * snap during an edit. 0 means no snapping.
   */
  properties[PROP_SNAPPING_DISTANCE] =
      g_param_spec_uint64 ("snapping-distance", "Snapping distance",
      "Distance from which moving an object will snap with neighbours", 0,
      G_MAXUINT64, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_SNAPPING_DISTANCE,
      properties[PROP_SNAPPING_DISTANCE]);

  /**
   * GESTimeline::track-added:
   * @timeline: The #GESTimeline
   * @track: The track that was added to @timeline
   *
   * Will be emitted after the track is added to the timeline.
   *
   * Note that this should not be emitted whilst a timeline is being
   * loaded from its #GESProject asset. You should connect to the
   * project's #GESProject::loaded signal if you want to know which
   * tracks were created for the timeline.
   */
  ges_timeline_signals[TRACK_ADDED] =
      g_signal_new ("track-added", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, track_added), NULL,
      NULL, NULL, G_TYPE_NONE, 1, GES_TYPE_TRACK);

  /**
   * GESTimeline::track-removed:
   * @timeline: The #GESTimeline
   * @track: The track that was removed from @timeline
   *
   * Will be emitted after the track is removed from the timeline.
   */
  ges_timeline_signals[TRACK_REMOVED] =
      g_signal_new ("track-removed", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, track_removed),
      NULL, NULL, NULL, G_TYPE_NONE, 1, GES_TYPE_TRACK);

  /**
   * GESTimeline::layer-added:
   * @timeline: The #GESTimeline
   * @layer: The layer that was added to @timeline
   *
   * Will be emitted after the layer is added to the timeline.
   *
   * Note that this should not be emitted whilst a timeline is being
   * loaded from its #GESProject asset. You should connect to the
   * project's #GESProject::loaded signal if you want to know which
   * layers were created for the timeline.
   */
  ges_timeline_signals[LAYER_ADDED] =
      g_signal_new ("layer-added", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, layer_added), NULL,
      NULL, NULL, G_TYPE_NONE, 1, GES_TYPE_LAYER);

  /**
   * GESTimeline::layer-removed:
   * @timeline: The #GESTimeline
   * @layer: The layer that was removed from @timeline
   *
   * Will be emitted after the layer is removed from the timeline.
   */
  ges_timeline_signals[LAYER_REMOVED] =
      g_signal_new ("layer-removed", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, layer_removed),
      NULL, NULL, NULL, G_TYPE_NONE, 1, GES_TYPE_LAYER);

  /**
   * GESTimeline::group-added
   * @timeline: The #GESTimeline
   * @group: The group that was added to @timeline
   *
   * Will be emitted after the group is added to to the timeline. This can
   * happen when grouping with `ges_container_group`, or by adding
   * containers to a newly created group.
   *
   * Note that this should not be emitted whilst a timeline is being
   * loaded from its #GESProject asset. You should connect to the
   * project's #GESProject::loaded signal if you want to know which groups
   * were created for the timeline.
   */
  ges_timeline_signals[GROUP_ADDED] =
      g_signal_new ("group-added", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, group_added), NULL,
      NULL, NULL, G_TYPE_NONE, 1, GES_TYPE_GROUP);

  /**
   * GESTimeline::group-removed
   * @timeline: The #GESTimeline
   * @group: The group that was removed from @timeline
   * @children: (element-type GESContainer) (transfer none): A list
   * of #GESContainer-s that _were_ the children of the removed @group
   *
   * Will be emitted after the group is removed from the timeline through
   * `ges_container_ungroup`. Note that @group will no longer contain its
   * former children, these are held in @children.
   *
   * Note that if a group is emptied, then it will no longer belong to the
   * timeline, but this signal will **not** be emitted in such a case.
   */
  ges_timeline_signals[GROUP_REMOVED] =
      g_signal_new ("group-removed", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GESTimelineClass, group_removed),
      NULL, NULL, NULL, G_TYPE_NONE, 2, GES_TYPE_GROUP, G_TYPE_PTR_ARRAY);

  /**
   * GESTimeline::snapping-started:
   * @timeline: The #GESTimeline
   * @obj1: The first element that is snapping
   * @obj2: The second element that is snapping
   * @position: The position where the two objects will snap to
   *
   * Will be emitted whenever an element's movement invokes a snapping
   * event during an edit (usually of one of its ancestors) because its
   * start or end point lies within the #GESTimeline:snapping-distance of
   * another element's start or end point.
   *
   * See #GESEditMode to see what can snap during an edit.
   *
   * Note that only up to one snapping-started signal will be emitted per
   * element edit within a timeline.
   */
  ges_timeline_signals[SNAPING_STARTED] =
      g_signal_new ("snapping-started", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL,
      G_TYPE_NONE, 3, GES_TYPE_TRACK_ELEMENT, GES_TYPE_TRACK_ELEMENT,
      G_TYPE_UINT64);

  /**
   * GESTimeline::snapping-ended:
   * @timeline: The #GESTimeline
   * @obj1: The first element that was snapping
   * @obj2: The second element that was snapping
   * @position: The position where the two objects were to be snapped to
   *
   * Will be emitted whenever a snapping event ends. After a snap event
   * has started (see #GESTimeline::snapping-started), it can later end
   * because either another timeline edit has occurred (which may or may
   * not have created a new snapping event), or because the timeline has
   * been committed.
   */
  ges_timeline_signals[SNAPING_ENDED] =
      g_signal_new ("snapping-ended", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL,
      G_TYPE_NONE, 3, GES_TYPE_TRACK_ELEMENT, GES_TYPE_TRACK_ELEMENT,
      G_TYPE_UINT64);

  /**
   * GESTimeline::select-tracks-for-object:
   * @timeline: The #GESTimeline
   * @clip: The clip that @track_element is being added to
   * @track_element: The element being added
   *
   * This will be emitted whenever the timeline needs to determine which
   * tracks a clip's children should be added to. The track element will
   * be added to each of the tracks given in the return. If a track
   * element is selected to go into multiple tracks, it will be copied
   * into the additional tracks, under the same clip. Note that the copy
   * will *not* keep its properties or state in sync with the original.
   *
   * Connect to this signal once if you wish to control which element
   * should be added to which track. Doing so will overwrite the default
   * behaviour, which adds @track_element to all tracks whose
   * #GESTrack:track-type includes the @track_element's
   * #GESTrackElement:track-type.
   *
   * Note that under the default track selection, if a clip would produce
   * multiple core children of the same #GESTrackType, it will choose
   * one of the core children arbitrarily to place in the corresponding
   * tracks, with a warning for the other core children that are not
   * placed in the track. For example, this would happen for a #GESUriClip
   * that points to a file that contains multiple audio streams. If you
   * wish to choose the stream, you could connect to this signal, and use,
   * say, ges_uri_source_asset_get_stream_info() to choose which core
   * source to add.
   *
   * When a clip is first added to a timeline, its core elements will
   * be created for the current tracks in the timeline if they have not
   * already been created. Then this will be emitted for each of these
   * core children to select which tracks, if any, they should be added
   * to. It will then be called for any non-core children in the clip.
   *
   * In addition, if a new track element is ever added to a clip in a
   * timeline (and it is not already part of a track) this will be emitted
   * to select which tracks the element should be added to.
   *
   * Finally, as a special case, if a track is added to the timeline
   * *after* it already contains clips, then it will request the creation
   * of the clips' core elements of the corresponding type, if they have
   * not already been created, and this signal will be emitted for each of
   * these newly created elements. In addition, this will also be released
   * for all other track elements in the timeline's clips that have not
   * yet been assigned a track. However, in this final case, the timeline
   * will only check whether the newly added track appears in the track
   * list. If it does appear, the track element will be added to the newly
   * added track. All other tracks in the returned track list are ignored.
   *
   * In this latter case, track elements that are already part of a track
   * will not be asked if they want to be copied into the new track. If
   * you wish to do this, you can use ges_clip_add_child_to_track().
   *
   * Note that the returned #GPtrArray should own a new reference to each
   * of its contained #GESTrack. The timeline will set the #GDestroyNotify
   * free function on the #GPtrArray to dereference the elements.
   *
   * Returns: (transfer full) (element-type GESTrack): An array of
   * #GESTrack-s that @track_element should be added to, or %NULL to
   * not add the element to any track.
   */
  ges_timeline_signals[SELECT_TRACKS_FOR_OBJECT] =
      g_signal_new ("select-tracks-for-object", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, 0, _gst_array_accumulator, NULL, NULL,
      G_TYPE_PTR_ARRAY, 2, GES_TYPE_CLIP, GES_TYPE_TRACK_ELEMENT);

  /**
   * GESTimeline::select-element-track:
   * @timeline: The #GESTimeline
   * @clip: The clip that @track_element is being added to
   * @track_element: The element being added
   *
   * Simplified version of #GESTimeline::select-tracks-for-object which only
   * allows @track_element to be added to a single #GESTrack.
   *
   * Returns: (transfer full) (nullable): A track to put @track_element into, or %NULL if
   * it should be discarded.
   *
   * Since: 1.18
   */
  ges_timeline_signals[SELECT_ELEMENT_TRACK] =
      g_signal_new ("select-element-track", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL,
      GES_TYPE_TRACK, 2, GES_TYPE_CLIP, GES_TYPE_TRACK_ELEMENT);

  /**
   * GESTimeline::commited:
   * @timeline: The #GESTimeline
   *
   * This signal will be emitted once the changes initiated by
   * ges_timeline_commit() have been executed in the backend. Use
   * ges_timeline_commit_sync() if you do not want to have to connect
   * to this signal.
   */
  ges_timeline_signals[COMMITED] =
      g_signal_new ("commited", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
}

static void
ges_timeline_init (GESTimeline * self)
{
  GESTimelinePrivate *priv = self->priv;

  self->priv = ges_timeline_get_instance_private (self);
  self->priv->tree = g_node_new (self);

  priv = self->priv;
  self->layers = NULL;
  self->tracks = NULL;
  self->priv->duration = 0;
  self->priv->auto_transition = FALSE;
  priv->snapping_distance = 0;
  priv->expected_async_done = 0;
  priv->expected_commited = 0;

  self->priv->last_snap_ts = GST_CLOCK_TIME_NONE;

  priv->priv_tracks = NULL;

  priv->all_elements =
      g_hash_table_new_full (g_str_hash, g_str_equal, g_free, gst_object_unref);

  priv->stream_start_group_id = -1;
  priv->stream_collection = gst_stream_collection_new (NULL);

  g_signal_connect_after (self, "select-tracks-for-object",
      G_CALLBACK (select_tracks_for_object_default), NULL);

  g_rec_mutex_init (&priv->dyn_mutex);
  g_mutex_init (&priv->commited_lock);
  priv->valid_thread = g_thread_self ();
}

/* Private methods */

/* Sorting utils*/
static gint
sort_layers (gpointer a, gpointer b)
{
  GESLayer *layer_a, *layer_b;
  guint prio_a, prio_b;

  layer_a = GES_LAYER (a);
  layer_b = GES_LAYER (b);

  prio_a = ges_layer_get_priority (layer_a);
  prio_b = ges_layer_get_priority (layer_b);

  if (prio_a > prio_b)
    return 1;
  if (prio_a < prio_b)
    return -1;

  return 0;
}

static void
_resync_layers (GESTimeline * timeline)
{
  GList *tmp;
  gint i = 0;

  timeline->priv->resyncing_layers = TRUE;
  for (tmp = timeline->layers; tmp; tmp = tmp->next) {
    layer_set_priority (tmp->data, i, TRUE);
    i++;
  }
  timeline->priv->resyncing_layers = FALSE;
}

void
timeline_update_duration (GESTimeline * timeline)
{
  GstClockTime duration = timeline_tree_get_duration (timeline->priv->tree);

  if (timeline->priv->duration != duration) {
    GST_DEBUG ("track duration : %" GST_TIME_FORMAT " current : %"
        GST_TIME_FORMAT, GST_TIME_ARGS (duration),
        GST_TIME_ARGS (timeline->priv->duration));

    timeline->priv->duration = duration;

    g_object_notify_by_pspec (G_OBJECT (timeline), properties[PROP_DURATION]);
  }
}

static gint
custom_find_track (TrackPrivate * tr_priv, GESTrack * track)
{
  if (tr_priv->track == track)
    return 0;
  return -1;
}

static void
_destroy_auto_transition_cb (GESAutoTransition * auto_transition,
    GESTimeline * timeline)
{
  GESTimelinePrivate *priv = timeline->priv;
  GESClip *transition = auto_transition->transition_clip;
  GESLayer *layer = ges_clip_get_layer (transition);

  ges_layer_remove_clip (layer, transition);
  g_signal_handlers_disconnect_by_func (auto_transition,
      _destroy_auto_transition_cb, timeline);

  priv->auto_transitions =
      g_list_remove (priv->auto_transitions, auto_transition);
  gst_object_unref (auto_transition);
}

GESAutoTransition *
ges_timeline_create_transition (GESTimeline * timeline,
    GESTrackElement * previous, GESTrackElement * next, GESClip * transition,
    GESLayer * layer, guint64 start, guint64 duration)
{
  GESAutoTransition *auto_transition;
  GESTrackElement *child;
  /* track should not be NULL */
  GESTrack *track = ges_track_element_get_track (next);

  if (transition == NULL) {
    GESAsset *asset;

    LOCK_DYN (timeline);
    timeline->priv->auto_transition_track = gst_object_ref (track);
    UNLOCK_DYN (timeline);

    asset = ges_asset_request (GES_TYPE_TRANSITION_CLIP, "crossfade", NULL);
    transition = ges_layer_add_asset (layer, asset, start, 0, duration,
        ges_track_element_get_track_type (next));
    gst_object_unref (asset);

    LOCK_DYN (timeline);
    /* should have been set to NULL, but clear just in case */
    gst_clear_object (&timeline->priv->auto_transition_track);
    UNLOCK_DYN (timeline);
  } else {
    GST_DEBUG_OBJECT (timeline,
        "Reusing already existing transition: %" GST_PTR_FORMAT, transition);
  }

  g_return_val_if_fail (transition, NULL);
  g_return_val_if_fail (g_list_length (GES_CONTAINER_CHILDREN (transition)) ==
      1, NULL);
  child = GES_CONTAINER_CHILDREN (transition)->data;
  if (ges_track_element_get_track (child) != track) {
    GST_ERROR_OBJECT (timeline, "The auto transition element %"
        GES_FORMAT " for elements %" GES_FORMAT " and %" GES_FORMAT
        " is not in the same track %" GST_PTR_FORMAT,
        GES_ARGS (child), GES_ARGS (previous), GES_ARGS (next), track);
    return NULL;
  }

  /* We know there is only 1 TrackElement */
  auto_transition = ges_auto_transition_new (child, previous, next);

  g_signal_connect (auto_transition, "destroy-me",
      G_CALLBACK (_destroy_auto_transition_cb), timeline);

  timeline->priv->auto_transitions =
      g_list_prepend (timeline->priv->auto_transitions, auto_transition);

  return auto_transition;
}

GESAutoTransition *
ges_timeline_find_auto_transition (GESTimeline * timeline,
    GESTrackElement * prev, GESTrackElement * next,
    GstClockTime transition_duration)
{
  GList *tmp;

  for (tmp = timeline->priv->auto_transitions; tmp; tmp = tmp->next) {
    GESAutoTransition *auto_trans = (GESAutoTransition *) tmp->data;

    /* We already have a transition linked to one of the elements we want to
     * find a transition for */
    if (auto_trans->previous_source == prev || auto_trans->next_source == next) {
      if (auto_trans->previous_source != prev
          || auto_trans->next_source != next) {
        GST_ERROR_OBJECT (timeline, "Failed creating auto transition, "
            " trying to have 3 clips overlapping, rolling back");
      }

      return auto_trans;
    }
  }

  return NULL;
}

GESAutoTransition *
ges_timeline_get_auto_transition_at_edge (GESTimeline * timeline,
    GESTrackElement * source, GESEdge edge)
{
  GList *tmp, *auto_transitions;
  GESAutoTransition *ret = NULL;

  LOCK_DYN (timeline);
  auto_transitions = g_list_copy_deep (timeline->priv->auto_transitions,
      (GCopyFunc) gst_object_ref, NULL);
  UNLOCK_DYN (timeline);

  for (tmp = auto_transitions; tmp; tmp = tmp->next) {
    GESAutoTransition *auto_trans = (GESAutoTransition *) tmp->data;

    /* We already have a transition linked to one of the elements we want to
     * find a transition for */
    if (edge == GES_EDGE_END && auto_trans->previous_source == source) {
      ret = gst_object_ref (auto_trans);
      break;
    } else if (edge == GES_EDGE_START && auto_trans->next_source == source) {
      ret = gst_object_ref (auto_trans);
      break;
    }
  }

  g_list_free_full (auto_transitions, gst_object_unref);

  return ret;
}

static GESAutoTransition *
_create_auto_transition_from_transitions (GESTimeline * timeline,
    GESTrackElement * prev, GESTrackElement * next,
    GstClockTime transition_duration)
{
  GList *tmp, *elements;
  GESLayer *layer;
  guint32 layer_prio = GES_TIMELINE_ELEMENT_LAYER_PRIORITY (prev);
  GESTrack *track;
  GESAutoTransition *auto_transition =
      ges_timeline_find_auto_transition (timeline, prev, next,
      transition_duration);

  if (auto_transition)
    return auto_transition;

  layer = ges_timeline_get_layer (timeline, layer_prio);
  track = ges_track_element_get_track (prev);
  elements = ges_track_get_elements (track);
  for (tmp = elements; tmp; tmp = tmp->next) {
    GESTrackElement *maybe_transition = tmp->data;

    if (ges_timeline_element_get_layer_priority (tmp->data) != layer_prio)
      continue;

    if (_START (maybe_transition) > _START (next))
      break;
    else if (_START (maybe_transition) != _START (next) ||
        _DURATION (maybe_transition) != transition_duration)
      continue;
    else if (GES_IS_TRANSITION (maybe_transition)) {
      /* Use that transition */
      /* TODO We should make sure that the transition contains only
       * TrackElement-s in @track and if it is not the case properly unlink the
       * object to use it */
      auto_transition = ges_timeline_create_transition (timeline, prev, next,
          GES_CLIP (GES_TIMELINE_ELEMENT_PARENT (maybe_transition)), layer,
          _START (next), transition_duration);

      break;
    }
  }
  gst_object_unref (layer);
  g_list_free_full (elements, gst_object_unref);

  return auto_transition;
}

void
ges_timeline_emit_snapping (GESTimeline * timeline, GESTrackElement * elem1,
    GESTrackElement * elem2, GstClockTime snap_time)
{
  GESTimelinePrivate *priv = timeline->priv;
  GstClockTime last_snap_ts = timeline->priv->last_snap_ts;

  if (!GST_CLOCK_TIME_IS_VALID (snap_time)) {
    if (priv->last_snaped1 != NULL && priv->last_snaped2 != NULL) {
      g_signal_emit (timeline, ges_timeline_signals[SNAPING_ENDED], 0,
          priv->last_snaped1, priv->last_snaped2, last_snap_ts);
      priv->last_snaped1 = NULL;
      priv->last_snaped2 = NULL;
      priv->last_snap_ts = GST_CLOCK_TIME_NONE;
    }

    return;
  }

  g_assert (elem1 != elem2);

  if (GST_CLOCK_TIME_IS_VALID (last_snap_ts))
    g_signal_emit (timeline, ges_timeline_signals[SNAPING_ENDED], 0,
        priv->last_snaped1, priv->last_snaped2, (last_snap_ts));

  priv->last_snaped1 = elem1;
  priv->last_snaped2 = elem2;
  timeline->priv->last_snap_ts = snap_time;
  g_signal_emit (timeline, ges_timeline_signals[SNAPING_STARTED], 0,
      elem1, elem2, snap_time);
}

/* Accept @self == NULL, making it use default framerate */
void
timeline_get_framerate (GESTimeline * self, gint * fps_n, gint * fps_d)
{
  GList *tmp;

  *fps_n = *fps_d = -1;
  if (!self)
    goto done;

  LOCK_DYN (self);
  for (tmp = self->tracks; tmp; tmp = tmp->next) {
    if (GES_IS_VIDEO_TRACK (tmp->data)) {
      GstCaps *restriction = ges_track_get_restriction_caps (tmp->data);
      gint i;

      if (!restriction)
        continue;

      for (i = 0; i < gst_caps_get_size (restriction); i++) {
        gint n, d;

        if (!gst_structure_get_fraction (gst_caps_get_structure (restriction,
                    i), "framerate", &n, &d))
          continue;

        if (*fps_n != -1 && *fps_d != -1 && !(n == *fps_n && d == *fps_d)) {
          GST_WARNING_OBJECT (self,
              "Various framerates specified, this is not supported"
              " First one will be used.");
          continue;
        }

        *fps_n = n;
        *fps_d = d;
      }
      gst_caps_unref (restriction);
    }
  }
  UNLOCK_DYN (self);

done:
  if (*fps_n == -1 && *fps_d == -1) {
    GST_INFO_OBJECT (self,
        "No framerate found, using default " G_STRINGIFY (FRAMERATE_N) "/ "
        G_STRINGIFY (FRAMERATE_D));
    *fps_n = DEFAULT_FRAMERATE_N;
    *fps_d = DEFAULT_FRAMERATE_D;
  }
}

void
ges_timeline_freeze_auto_transitions (GESTimeline * timeline, gboolean freeze)
{
  GList *tmp, *trans = g_list_copy (timeline->priv->auto_transitions);
  for (tmp = trans; tmp; tmp = tmp->next) {
    GESAutoTransition *auto_transition = tmp->data;
    auto_transition->frozen = freeze;
    if (freeze == FALSE) {
      GST_LOG_OBJECT (timeline, "Un-Freezing %" GES_FORMAT,
          GES_ARGS (auto_transition->transition_clip));
      ges_auto_transition_update (auto_transition);
    } else {
      GST_LOG_OBJECT (timeline, "Freezing %" GES_FORMAT,
          GES_ARGS (auto_transition->transition_clip));
    }
  }
  g_list_free (trans);
}

static gint
_edit_auto_transition (GESTimeline * timeline, GESTimelineElement * element,
    gint64 new_layer_priority, GESEditMode mode, GESEdge edge,
    GstClockTime position, GError ** error)
{
  GList *tmp;
  guint32 layer_prio = ges_timeline_element_get_layer_priority (element);
  GESLayer *layer = ges_timeline_get_layer (timeline, layer_prio);

  if (!ges_layer_get_auto_transition (layer)) {
    gst_object_unref (layer);
    return -1;
  }

  gst_object_unref (layer);
  for (tmp = timeline->priv->auto_transitions; tmp; tmp = tmp->next) {
    GESTimelineElement *replace;
    GESAutoTransition *auto_transition = tmp->data;

    if (GES_TIMELINE_ELEMENT (auto_transition->transition) == element ||
        GES_TIMELINE_ELEMENT (auto_transition->transition_clip) == element) {
      if (auto_transition->positioning) {
        GST_ERROR_OBJECT (element, "Trying to edit an auto-transition "
            "whilst it is being positioned");
        return FALSE;
      }
      if (new_layer_priority != layer_prio) {
        GST_WARNING_OBJECT (element, "Cannot edit an auto-transition to a "
            "new layer");
        return FALSE;
      }
      if (mode != GES_EDIT_MODE_TRIM) {
        GST_WARNING_OBJECT (element, "Cannot edit an auto-transition "
            "under the edit mode %i", mode);
        return FALSE;
      }

      if (edge == GES_EDGE_END)
        replace = GES_TIMELINE_ELEMENT (auto_transition->previous_source);
      else
        replace = GES_TIMELINE_ELEMENT (auto_transition->next_source);

      GST_INFO_OBJECT (element, "Trimming %" GES_FORMAT " in place  of "
          "trimming the corresponding auto-transition", GES_ARGS (replace));
      return ges_timeline_element_edit_full (replace, -1, mode, edge,
          position, error);
    }
  }

  return -1;
}

gboolean
ges_timeline_edit (GESTimeline * timeline, GESTimelineElement * element,
    gint64 new_layer_priority, GESEditMode mode, GESEdge edge,
    guint64 position, GError ** error)
{
  GstClockTimeDiff edge_diff = (edge == GES_EDGE_END ?
      GST_CLOCK_DIFF (position, element->start + element->duration) :
      GST_CLOCK_DIFF (position, element->start));
  gint64 prio_diff = (gint64) ges_timeline_element_get_layer_priority (element)
      - new_layer_priority;
  gint res = -1;

  if ((GES_IS_TRANSITION (element) || GES_IS_TRANSITION_CLIP (element)))
    res = _edit_auto_transition (timeline, element, new_layer_priority, mode,
        edge, position, error);

  if (res != -1)
    return res;

  switch (mode) {
    case GES_EDIT_MODE_RIPPLE:
      return timeline_tree_ripple (timeline->priv->tree, element, prio_diff,
          edge_diff, edge, timeline->priv->snapping_distance, error);
    case GES_EDIT_MODE_TRIM:
      return timeline_tree_trim (timeline->priv->tree, element, prio_diff,
          edge_diff, edge, timeline->priv->snapping_distance, error);
    case GES_EDIT_MODE_NORMAL:
      return timeline_tree_move (timeline->priv->tree, element, prio_diff,
          edge_diff, edge, timeline->priv->snapping_distance, error);
    case GES_EDIT_MODE_ROLL:
      if (prio_diff != 0) {
        GST_WARNING_OBJECT (element, "Cannot roll an element to a new layer");
        return FALSE;
      }
      return timeline_tree_roll (timeline->priv->tree, element,
          edge_diff, edge, timeline->priv->snapping_distance, error);
    case GES_EDIT_MODE_SLIDE:
      GST_ERROR_OBJECT (element, "Sliding not implemented.");
      return FALSE;
  }
  return FALSE;
}

void
timeline_add_group (GESTimeline * timeline, GESGroup * group)
{
  GST_DEBUG_OBJECT (timeline, "Adding group %" GST_PTR_FORMAT, group);

  timeline->priv->groups = g_list_prepend (timeline->priv->groups,
      gst_object_ref_sink (group));

  ges_timeline_element_set_timeline (GES_TIMELINE_ELEMENT (group), timeline);
}

/**
 * timeline_emit_group_added:
 * @timeline: The #GESTimeline
 * @group: group that was added
 *
 * Emit group-added signal.
 */
void
timeline_emit_group_added (GESTimeline * timeline, GESGroup * group)
{
  g_signal_emit (timeline, ges_timeline_signals[GROUP_ADDED], 0, group);
}

/**
 * timeline_emit_group_removed:
 * @timeline: The #GESTimeline
 * @group: group that was removed
 *
 * Emit group-removed signal.
 */
void
timeline_emit_group_removed (GESTimeline * timeline, GESGroup * group,
    GPtrArray * array)
{
  g_signal_emit (timeline, ges_timeline_signals[GROUP_REMOVED], 0, group,
      array);
}

void
timeline_remove_group (GESTimeline * timeline, GESGroup * group)
{
  GST_DEBUG_OBJECT (timeline, "Removing group %" GST_PTR_FORMAT, group);

  timeline->priv->groups = g_list_remove (timeline->priv->groups, group);

  ges_timeline_element_set_timeline (GES_TIMELINE_ELEMENT (group), NULL);
  gst_object_unref (group);
}

static GESTrackElement *
_core_in_track (GESTrack * track, GESClip * clip)
{
  GList *tmp;
  for (tmp = GES_CONTAINER_CHILDREN (clip); tmp; tmp = tmp->next) {
    GESTrackElement *el = tmp->data;
    if (ges_track_element_is_core (el)
        && ges_track_element_get_track (el) == track) {
      return tmp->data;
    }
  }
  return NULL;
}

static GPtrArray *
select_tracks_for_object_default (GESTimeline * timeline,
    GESClip * clip, GESTrackElement * tr_object, gpointer user_data)
{
  GPtrArray *result;
  GList *tmp;
  GESTrackElement *core;

  result = g_ptr_array_new ();

  LOCK_DYN (timeline);
  for (tmp = timeline->tracks; tmp; tmp = tmp->next) {
    GESTrack *track = GES_TRACK (tmp->data);

    if ((track->type & ges_track_element_get_track_type (tr_object))) {
      if (ges_track_element_is_core (tr_object)) {
        core = _core_in_track (track, clip);
        if (core) {
          GST_WARNING_OBJECT (timeline, "The clip '%s' contains multiple "
              "core elements of the same %s track type. The core child "
              "'%s' has already been chosen arbitrarily for the track %"
              GST_PTR_FORMAT ", which means that the other core child "
              "'%s' of the same type can not be added to the track. "
              "Consider connecting to "
              "GESTimeline::select-tracks-for-objects to be able to "
              "specify which core element should land in the track",
              GES_TIMELINE_ELEMENT_NAME (clip),
              ges_track_type_name (track->type),
              GES_TIMELINE_ELEMENT_NAME (core), track,
              GES_TIMELINE_ELEMENT_NAME (tr_object));
          continue;
        }
      }
      gst_object_ref (track);
      g_ptr_array_add (result, track);
    }
  }
  UNLOCK_DYN (timeline);

  return result;
}

static GPtrArray *
_get_selected_tracks (GESTimeline * timeline, GESClip * clip,
    GESTrackElement * track_element)
{
  guint i, j;
  GPtrArray *tracks = NULL;
  GESTrack *track = NULL;

  g_signal_emit (G_OBJECT (timeline),
      ges_timeline_signals[SELECT_ELEMENT_TRACK], 0, clip, track_element,
      &track);

  if (track) {
    tracks = g_ptr_array_new ();

    g_ptr_array_add (tracks, track);
  } else {
    g_signal_emit (G_OBJECT (timeline),
        ges_timeline_signals[SELECT_TRACKS_FOR_OBJECT], 0, clip, track_element,
        &tracks);
  }

  if (tracks == NULL)
    tracks = g_ptr_array_new ();

  g_ptr_array_set_free_func (tracks, gst_object_unref);

  /* make sure unique */
  for (i = 0; i < tracks->len;) {
    GESTrack *track = GES_TRACK (g_ptr_array_index (tracks, i));

    for (j = i + 1; j < tracks->len;) {
      if (track == g_ptr_array_index (tracks, j)) {
        GST_WARNING_OBJECT (timeline, "Found the track %" GST_PTR_FORMAT
            " more than once in the return for select-tracks-for-object "
            "signal for track element %" GES_FORMAT " in clip %"
            GES_FORMAT ". Ignoring the extra track", track,
            GES_ARGS (track_element), GES_ARGS (clip));
        g_ptr_array_remove_index (tracks, j);
        /* don't increase index since the next track is in its place */
        continue;
      }
      j++;
    }

    if (ges_track_get_timeline (track) != timeline) {
      GST_WARNING_OBJECT (timeline, "The track %" GST_PTR_FORMAT
          " found in the return for select-tracks-for-object belongs "
          "to a different timeline %" GST_PTR_FORMAT ". Ignoring this "
          "track", track, ges_track_get_timeline (track));
      g_ptr_array_remove_index (tracks, i);
      /* don't increase index since the next track is in its place */
      continue;
    }
    i++;
  }

  return tracks;
}

/* returns TRUE if track element was successfully added to all the
 * selected tracks */
static gboolean
_add_track_element_to_tracks (GESTimeline * timeline, GESClip * clip,
    GESTrackElement * track_element, GError ** error)
{
  guint i;
  gboolean ret = TRUE;
  GPtrArray *tracks = _get_selected_tracks (timeline, clip, track_element);

  for (i = 0; i < tracks->len; i++) {
    GESTrack *track = GES_TRACK (g_ptr_array_index (tracks, i));
    if (!ges_clip_add_child_to_track (clip, track_element, track, error)) {
      ret = FALSE;
      if (error)
        break;
    }
  }

  g_ptr_array_unref (tracks);

  return ret;
}

static gboolean
_try_add_track_element_to_track (GESTimeline * timeline, GESClip * clip,
    GESTrackElement * track_element, GESTrack * track, GError ** error)
{
  gboolean no_error = TRUE;
  GPtrArray *tracks = _get_selected_tracks (timeline, clip, track_element);

  /* if we are trying to add the element to a newly added track, then
   * we only check whether the track list contains the newly added track,
   * if it does we add the track element to the track, or add a copy if
   * the track element is already in a track */
  if (g_ptr_array_find (tracks, track, NULL)) {
    if (!ges_clip_add_child_to_track (clip, track_element, track, error))
      no_error = FALSE;
  }

  g_ptr_array_unref (tracks);
  return no_error;
}

/* accepts NULL */
void
ges_timeline_set_moving_track_elements (GESTimeline * timeline, gboolean moving)
{
  if (timeline) {
    LOCK_DYN (timeline);
    timeline->priv->track_elements_moving = moving;
    UNLOCK_DYN (timeline);
  }
}

void
ges_timeline_set_track_selection_error (GESTimeline * timeline,
    gboolean was_error, GError * error)
{
  GESTimelinePrivate *priv;

  LOCK_DYN (timeline);

  priv = timeline->priv;
  g_clear_error (&priv->track_selection_error);
  priv->track_selection_error = error;
  priv->has_any_track_selection_error = was_error;

  UNLOCK_DYN (timeline);
}

gboolean
ges_timeline_take_track_selection_error (GESTimeline * timeline,
    GError ** error)
{
  gboolean ret;
  GESTimelinePrivate *priv;

  LOCK_DYN (timeline);

  priv = timeline->priv;
  if (error) {
    if (*error) {
      GST_ERROR_OBJECT (timeline, "Error not handled %s", (*error)->message);
      g_error_free (*error);
    }
    *error = priv->track_selection_error;
  } else if (priv->track_selection_error) {
    GST_WARNING_OBJECT (timeline, "Got track selection error: %s",
        priv->track_selection_error->message);
    g_error_free (priv->track_selection_error);
  }
  priv->track_selection_error = NULL;
  ret = priv->has_any_track_selection_error;
  priv->has_any_track_selection_error = FALSE;

  UNLOCK_DYN (timeline);

  return ret;
}

static void
clip_track_element_added_cb (GESClip * clip,
    GESTrackElement * track_element, GESTimeline * timeline)
{
  GESTrack *auto_trans_track, *new_track;
  GError *error = NULL;
  gboolean success = FALSE;

  if (timeline->priv->track_elements_moving) {
    GST_DEBUG_OBJECT (timeline, "Ignoring element added: %" GES_FORMAT
        " in %" GES_FORMAT, GES_ARGS (track_element), GES_ARGS (clip));
    return;
  }

  if (ges_track_element_get_track (track_element) != NULL) {
    GST_DEBUG_OBJECT (timeline, "Not selecting tracks for %" GES_FORMAT
        " in %" GES_FORMAT " because it already part of the track %"
        GST_PTR_FORMAT, GES_ARGS (track_element), GES_ARGS (clip),
        ges_track_element_get_track (track_element));
    return;
  }

  LOCK_DYN (timeline);
  /* take ownership of auto_transition_track. For auto-transitions, this
   * should be used exactly once! */
  auto_trans_track = timeline->priv->auto_transition_track;
  timeline->priv->auto_transition_track = NULL;
  /* don't take ownership of new_track */
  new_track = timeline->priv->new_track;
  UNLOCK_DYN (timeline);

  if (auto_trans_track) {
    /* don't use track-selection */
    success = !!ges_clip_add_child_to_track (clip, track_element,
        auto_trans_track, &error);
    gst_object_unref (auto_trans_track);
  } else {
    if (new_track)
      success = _try_add_track_element_to_track (timeline, clip, track_element,
          new_track, &error);
    else
      success = _add_track_element_to_tracks (timeline, clip, track_element,
          &error);
  }

  if (error || !success) {
    if (!error)
      GST_WARNING_OBJECT (timeline, "Track selection failed for %" GES_FORMAT,
          GES_ARGS (track_element));
    ges_timeline_set_track_selection_error (timeline, TRUE, error);
  }
}

static void
clip_track_element_removed_cb (GESClip * clip,
    GESTrackElement * track_element, GESTimeline * timeline)
{
  GESTrack *track = ges_track_element_get_track (track_element);

  if (timeline->priv->track_elements_moving) {
    GST_DEBUG_OBJECT (timeline, "Ignoring element removed (%" GST_PTR_FORMAT
        " in %" GST_PTR_FORMAT, track_element, clip);

    return;
  }

  if (track) {
    /* if we have non-core elements in the same track, they should be
     * removed from them to preserve the rule that a non-core can only be
     * in the same track as a core element from the same clip */
    if (ges_track_element_is_core (track_element))
      ges_clip_empty_from_track (clip, track);
    ges_track_remove_element (track, track_element);
  }
}

static void
track_element_added_cb (GESTrack * track, GESTrackElement * element,
    GESTimeline * timeline)
{
  if (GES_IS_SOURCE (element))
    timeline_tree_create_transitions_for_track_element (timeline->priv->tree,
        element, ges_timeline_find_auto_transition);
}

/* returns TRUE if no errors in adding to tracks */
static gboolean
_add_clip_children_to_tracks (GESTimeline * timeline, GESClip * clip,
    gboolean add_core, GESTrack * new_track, GList * blacklist, GError ** error)
{
  GList *tmp, *children;
  gboolean no_errors = TRUE;

  /* list of children may change if some are copied into tracks */
  children = ges_container_get_children (GES_CONTAINER (clip), FALSE);
  for (tmp = children; tmp; tmp = tmp->next) {
    GESTrackElement *el = tmp->data;
    if (ges_track_element_is_core (el) != add_core)
      continue;
    if (g_list_find (blacklist, el))
      continue;
    if (ges_track_element_get_track (el) == NULL) {
      gboolean res;
      if (new_track)
        res = _try_add_track_element_to_track (timeline, clip, el, new_track,
            error);
      else
        res = _add_track_element_to_tracks (timeline, clip, el, error);
      if (!res) {
        no_errors = FALSE;
        if (error)
          goto done;
      }
    }
  }

done:
  g_list_free_full (children, gst_object_unref);

  return no_errors;
}

/* returns TRUE if no errors in adding to tracks */
static gboolean
add_object_to_tracks (GESTimeline * timeline, GESClip * clip,
    GESTrack * new_track, GError ** error)
{
  GList *tracks, *tmp, *list, *created, *just_added = NULL;
  gboolean no_errors = TRUE;

  GST_DEBUG_OBJECT (timeline, "Creating %" GST_PTR_FORMAT
      " trackelements and adding them to our tracks", clip);

  LOCK_DYN (timeline);
  tracks =
      g_list_copy_deep (timeline->tracks, (GCopyFunc) gst_object_ref, NULL);
  timeline->priv->new_track = new_track ? gst_object_ref (new_track) : NULL;
  UNLOCK_DYN (timeline);

  /* create core elements */
  for (tmp = tracks; tmp; tmp = tmp->next) {
    GESTrack *track = GES_TRACK (tmp->data);
    if (new_track && track != new_track)
      continue;

    list = ges_clip_create_track_elements (clip, track->type);
    /* just_added only used for pointer comparison, so safe to include
     * elements that may be destroyed because they fail to be added to
     * the clip */
    just_added = g_list_concat (just_added, list);

    for (created = list; created; created = created->next) {
      GESTimelineElement *el = created->data;

      gst_object_ref (el);

      /* make track selection be handled by clip_track_element_added_cb
       * This is needed for backward-compatibility: when adding a clip to
       * a layer, the track is set for the core elements of the clip
       * during the child-added signal emission, just before the user's
       * own connection.
       * NOTE: for the children that have not just been created, they
       * are already part of the clip and so child-added will not be
       * released. And when a child is selected for multiple tracks, their
       * copy will be added to the clip before the track is selected, so
       * the track will not be set in the child-added signal */
      ges_timeline_set_track_selection_error (timeline, FALSE, NULL);
      ges_clip_set_add_error (clip, NULL);
      if (!ges_container_add (GES_CONTAINER (clip), el)) {
        no_errors = FALSE;
        if (!error)
          GST_ERROR_OBJECT (clip, "Could not add the core element %s "
              "to the clip", el->name);
      }
      gst_object_unref (el);
      ges_clip_take_add_error (clip, error);

      if (error && !no_errors)
        goto done;

      if (ges_timeline_take_track_selection_error (timeline, error)) {
        no_errors = FALSE;
        if (error)
          goto done;
        /* else, carry on as much as we can */
      }
    }
  }

  /* set the tracks for the other children, with core elements first to
   * make sure the non-core can be placed above them in the track (a
   * non-core can not be in a track by itself) */
  /* include just_added as a blacklist to ensure we do not try the track
   * selection a second time when track selection returns no tracks */
  if (!_add_clip_children_to_tracks (timeline, clip, TRUE, new_track,
          just_added, error)) {
    no_errors = FALSE;
    if (error)
      goto done;
  }

  if (!_add_clip_children_to_tracks (timeline, clip, FALSE, new_track,
          just_added, error)) {
    no_errors = FALSE;
    if (error)
      goto done;
  }

done:
  g_list_free_full (tracks, gst_object_unref);

  LOCK_DYN (timeline);
  gst_clear_object (&timeline->priv->new_track);
  UNLOCK_DYN (timeline);

  g_list_free (just_added);

  return no_errors;
}

static void
layer_active_changed_cb (GESLayer * layer, gboolean active G_GNUC_UNUSED,
    GPtrArray * tracks G_GNUC_UNUSED, GESTimeline * timeline)
{
  timeline_tree_reset_layer_active (timeline->priv->tree, layer);
}

static void
layer_auto_transition_changed_cb (GESLayer * layer,
    GParamSpec * arg G_GNUC_UNUSED, GESTimeline * timeline)
{
  GList *tmp, *clips;

  timeline_tree_create_transitions (timeline->priv->tree,
      _create_auto_transition_from_transitions);
  clips = ges_layer_get_clips (layer);
  for (tmp = clips; tmp; tmp = tmp->next) {
    if (GES_IS_TRANSITION_CLIP (tmp->data)) {
      GList *tmpautotrans;
      gboolean found = FALSE;

      for (tmpautotrans = timeline->priv->auto_transitions; tmpautotrans;
          tmpautotrans = tmpautotrans->next) {
        if (GES_AUTO_TRANSITION (tmpautotrans->data)->transition_clip ==
            tmp->data) {
          found = TRUE;
          break;
        }
      }

      if (!found) {
        GST_ERROR_OBJECT (timeline,
            "Transition %s could not be wrapped into an auto transition"
            " REMOVING it", GES_TIMELINE_ELEMENT_NAME (tmp->data));

        ges_layer_remove_clip (layer, tmp->data);
      }
    }
  }
  g_list_free_full (clips, gst_object_unref);
}

/* returns TRUE if selecting of tracks did not error */
gboolean
ges_timeline_add_clip (GESTimeline * timeline, GESClip * clip, GError ** error)
{
  GESProject *project;
  gboolean ret;

  ges_timeline_element_set_timeline (GES_TIMELINE_ELEMENT (clip), timeline);

  /* We make sure not to be connected twice */
  g_signal_handlers_disconnect_by_func (clip, clip_track_element_added_cb,
      timeline);
  g_signal_handlers_disconnect_by_func (clip, clip_track_element_removed_cb,
      timeline);

  /* And we connect to the object */
  g_signal_connect (clip, "child-added",
      G_CALLBACK (clip_track_element_added_cb), timeline);
  g_signal_connect (clip, "child-removed",
      G_CALLBACK (clip_track_element_removed_cb), timeline);

  GST_DEBUG ("Making sure that the asset is in our project");
  project =
      GES_PROJECT (ges_extractable_get_asset (GES_EXTRACTABLE (timeline)));
  ges_project_add_asset (project,
      ges_extractable_get_asset (GES_EXTRACTABLE (clip)));

  if (ges_clip_is_moving_from_layer (clip)) {
    GST_DEBUG ("Clip %p moving from one layer to another, not creating "
        "TrackElement", clip);
    /* timeline-tree handles creation of auto-transitions */
    ret = TRUE;
  } else {
    ret = add_object_to_tracks (timeline, clip, NULL, error);
  }

  GST_DEBUG ("Done");

  return ret;
}

static void
layer_priority_changed_cb (GESLayer * layer,
    GParamSpec * arg G_GNUC_UNUSED, GESTimeline * timeline)
{
  if (timeline->priv->resyncing_layers)
    return;

  timeline->layers = g_list_sort (timeline->layers, (GCompareFunc)
      sort_layers);
}

void
ges_timeline_remove_clip (GESTimeline * timeline, GESClip * clip)
{
  GList *tmp;

  if (ges_clip_is_moving_from_layer (clip)) {
    GST_DEBUG ("Clip %p is moving from a layer to another, not doing"
        " anything on it", clip);
    return;
  }

  GST_DEBUG_OBJECT (timeline, "Clip %" GES_FORMAT " removed from layer",
      GES_ARGS (clip));

  LOCK_DYN (timeline);
  for (tmp = timeline->tracks; tmp; tmp = tmp->next)
    ges_clip_empty_from_track (clip, tmp->data);
  UNLOCK_DYN (timeline);

  g_signal_handlers_disconnect_by_func (clip, clip_track_element_added_cb,
      timeline);
  g_signal_handlers_disconnect_by_func (clip, clip_track_element_removed_cb,
      timeline);

  ges_timeline_element_set_timeline (GES_TIMELINE_ELEMENT (clip), NULL);

  GST_DEBUG ("Done");
}

static gboolean
update_stream_object (TrackPrivate * tr_priv)
{
  gboolean res = FALSE;
  GstStreamType type = GST_STREAM_TYPE_UNKNOWN;
  gchar *stream_id;

  g_object_get (tr_priv->track, "id", &stream_id, NULL);
  if (tr_priv->track->type == GES_TRACK_TYPE_VIDEO)
    type = GST_STREAM_TYPE_VIDEO;
  if (tr_priv->track->type == GES_TRACK_TYPE_AUDIO)
    type = GST_STREAM_TYPE_AUDIO;

  if (!tr_priv->stream ||
      g_strcmp0 (stream_id, gst_stream_get_stream_id (tr_priv->stream))) {
    res = TRUE;
    gst_object_replace ((GstObject **) & tr_priv->stream,
        (GstObject *) gst_stream_new (stream_id,
            (GstCaps *) ges_track_get_caps (tr_priv->track),
            type, GST_STREAM_FLAG_NONE)
        );
  }

  g_free (stream_id);

  return res;
}

static GstPadProbeReturn
_pad_probe_cb (GstPad * mixer_pad, GstPadProbeInfo * info,
    TrackPrivate * tr_priv)
{
  GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
  GESTimeline *timeline = tr_priv->timeline;

  if (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_START) {
    LOCK_DYN (timeline);
    if (timeline->priv->stream_start_group_id == -1) {
      if (!gst_event_parse_group_id (event,
              &timeline->priv->stream_start_group_id))
        timeline->priv->stream_start_group_id = gst_util_group_id_next ();
    }

    gst_event_unref (event);
    event = info->data =
        gst_event_new_stream_start (gst_stream_get_stream_id (tr_priv->stream));
    gst_event_set_stream (event, tr_priv->stream);
    gst_event_set_group_id (event, timeline->priv->stream_start_group_id);
    UNLOCK_DYN (timeline);

    return GST_PAD_PROBE_REMOVE;
  }

  return GST_PAD_PROBE_OK;
}

static void
_ghost_track_srcpad (TrackPrivate * tr_priv)
{
  GstPad *pad;
  gchar *padname;
  gboolean no_more;
  GList *tmp;
  GESTrack *track = tr_priv->track;

  pad = gst_element_get_static_pad (GST_ELEMENT (track), "src");

  GST_DEBUG ("track:%p, pad:%s:%s", track, GST_DEBUG_PAD_NAME (pad));

  /* Remember the pad */
  LOCK_DYN (tr_priv->timeline);
  GST_OBJECT_LOCK (track);
  tr_priv->pad = pad;

  no_more = TRUE;
  for (tmp = tr_priv->timeline->priv->priv_tracks; tmp; tmp = g_list_next (tmp)) {
    TrackPrivate *tr_priv = (TrackPrivate *) tmp->data;

    if (!tr_priv->pad) {
      GST_LOG ("Found track without pad %p", tr_priv->track);
      no_more = FALSE;
    }
  }
  GST_OBJECT_UNLOCK (track);

  /* ghost it ! */
  GST_DEBUG ("Ghosting pad and adding it to ourself");
  padname = g_strdup_printf ("track_%p_src", track);
  tr_priv->ghostpad = gst_ghost_pad_new (padname, pad);
  g_free (padname);
  gst_pad_set_active (tr_priv->ghostpad, TRUE);
  gst_element_add_pad (GST_ELEMENT (tr_priv->timeline), tr_priv->ghostpad);

  if (no_more) {
    GST_DEBUG ("Signaling no-more-pads");
    gst_element_no_more_pads (GST_ELEMENT (tr_priv->timeline));
  }

  tr_priv->probe_id = gst_pad_add_probe (pad,
      GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
      (GstPadProbeCallback) _pad_probe_cb, tr_priv, NULL);

  UNLOCK_DYN (tr_priv->timeline);
}

gboolean
timeline_add_element (GESTimeline * timeline, GESTimelineElement * element)
{
  /* FIXME: handle NULL element->name */
  GESTimelineElement *same_name =
      g_hash_table_lookup (timeline->priv->all_elements,
      element->name);

  GST_DEBUG_OBJECT (timeline, "Adding element: %s", element->name);
  if (same_name) {
    GST_ERROR_OBJECT (timeline, "%s Already in the timeline %" GST_PTR_FORMAT,
        element->name, same_name);
    return FALSE;
  }

  /* FIXME: why is the hash table using the name of the element, rather than
   * the pointer to the element itself as the key? This makes it awkward
   * to change the name of an element after it has been added. See
   * ges_timeline_element_set_name. It means we have to remove and then
   * re-add the element. */
  g_hash_table_insert (timeline->priv->all_elements,
      ges_timeline_element_get_name (element), gst_object_ref (element));

  timeline_tree_track_element (timeline->priv->tree, element);
  if (GES_IS_SOURCE (element)) {
    ges_source_set_rendering_smartly (GES_SOURCE (element),
        timeline->priv->rendering_smartly);
  }

  return TRUE;
}

gboolean
timeline_remove_element (GESTimeline * timeline, GESTimelineElement * element)
{
  if (g_hash_table_remove (timeline->priv->all_elements, element->name)) {
    timeline_tree_stop_tracking_element (timeline->priv->tree, element);

    return TRUE;
  }

  return FALSE;
}

void
timeline_fill_gaps (GESTimeline * timeline)
{
  GList *tmp;

  LOCK_DYN (timeline);
  for (tmp = timeline->tracks; tmp; tmp = tmp->next) {
    track_resort_and_fill_gaps (tmp->data);
  }
  UNLOCK_DYN (timeline);
}

GNode *
timeline_get_tree (GESTimeline * timeline)
{
  return timeline->priv->tree;
}

void
ges_timeline_set_smart_rendering (GESTimeline * timeline,
    gboolean rendering_smartly)
{
  if (rendering_smartly) {
    GList *tmp;

    for (tmp = timeline->tracks; tmp; tmp = tmp->next) {
      if (ges_track_get_mixing (tmp->data)) {
        GST_INFO_OBJECT (timeline, "Smart rendering will not"
            " work as track %" GST_PTR_FORMAT " is doing mixing", tmp->data);
      } else {
        ges_track_set_smart_rendering (tmp->data, rendering_smartly);
      }
    }
  }
  timeline_tree_set_smart_rendering (timeline->priv->tree, rendering_smartly);
  timeline->priv->rendering_smartly = rendering_smartly;
}

gboolean
ges_timeline_get_smart_rendering (GESTimeline * timeline)
{
  return timeline->priv->rendering_smartly;
}

GstStreamCollection *
ges_timeline_get_stream_collection (GESTimeline * timeline)
{
  return gst_object_ref (timeline->priv->stream_collection);
}

gboolean
ges_timeline_in_current_thread (GESTimeline * timeline)
{
  return timeline->priv->valid_thread == g_thread_self ();
}

/**** API *****/
/**
 * ges_timeline_new:
 *
 * Creates a new empty timeline.
 *
 * Returns: (transfer floating): The new timeline.
 */

GESTimeline *
ges_timeline_new (void)
{
  GESProject *project = ges_project_new (NULL);
  GESExtractable *timeline = g_object_new (GES_TYPE_TIMELINE, NULL);

  ges_extractable_set_asset (timeline, GES_ASSET (project));
  gst_object_unref (project);

  return GES_TIMELINE (timeline);
}

/**
 * ges_timeline_new_from_uri:
 * @uri: The URI to load from
 * @error: An error to be set if loading fails, or
 * %NULL to ignore
 *
 * Creates a timeline from the given URI.
 *
 * Returns: (transfer floating): A new timeline if the uri was loaded
 * successfully, or %NULL if the uri could not be loaded.
 */
GESTimeline *
ges_timeline_new_from_uri (const gchar * uri, GError ** error)
{
  GESTimeline *ret;
  GESProject *project = ges_project_new (uri);

  ret = GES_TIMELINE (ges_asset_extract (GES_ASSET (project), error));
  gst_object_unref (project);

  return ret;
}

/**
 * ges_timeline_load_from_uri:
 * @timeline: An empty #GESTimeline into which to load the formatter
 * @uri: The URI to load from
 * @error: An error to be set if loading fails, or
 * %NULL to ignore
 *
 * Loads the contents of URI into the timeline.
 *
 * Returns: %TRUE if the timeline was loaded successfully from @uri.
 */
gboolean
ges_timeline_load_from_uri (GESTimeline * timeline, const gchar * uri,
    GError ** error)
{
  GESProject *project;
  gboolean ret = FALSE;

  g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);
  g_return_val_if_fail ((ges_extractable_get_asset (GES_EXTRACTABLE
              (timeline)) == NULL), FALSE);

  project = ges_project_new (uri);
  ret = ges_project_load (project, timeline, error);
  gst_object_unref (project);

  return ret;
}

/**
 * ges_timeline_save_to_uri:
 * @timeline: The #GESTimeline
 * @uri: The location to save to
 * @formatter_asset: (allow-none): The formatter asset to use, or %NULL
 * @overwrite: %TRUE to overwrite file if it exists
 * @error: An error to be set if saving fails, or
 * %NULL to ignore
 *
 * Saves the timeline to the given location. If @formatter_asset is %NULL,
 * the method will attempt to save in the same format the timeline was
 * loaded from, before defaulting to the formatter with highest rank.
 *
 * Returns: %TRUE if @timeline was successfully saved to @uri.
 */
gboolean
ges_timeline_save_to_uri (GESTimeline * timeline, const gchar * uri,
    GESAsset * formatter_asset, gboolean overwrite, GError ** error)
{
  GESProject *project;

  gboolean ret, created_proj = FALSE;

  g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);
  project =
      GES_PROJECT (ges_extractable_get_asset (GES_EXTRACTABLE (timeline)));

  if (project == NULL) {
    project = ges_project_new (NULL);
    created_proj = TRUE;
  }

  ret = ges_project_save (project, timeline, uri, formatter_asset, overwrite,
      error);

  if (created_proj)
    gst_object_unref (project);

  return ret;
}

/**
 * ges_timeline_get_groups:
 * @timeline: The #GESTimeline
 *
 * Get the list of #GESGroup-s present in the timeline.
 *
 * Returns: (transfer none) (element-type GESGroup): The list of
 * groups that contain clips present in @timeline's layers.
 * Must not be changed.
 */
GList *
ges_timeline_get_groups (GESTimeline * timeline)
{
  g_return_val_if_fail (GES_IS_TIMELINE (timeline), NULL);
  CHECK_THREAD (timeline);

  return timeline->priv->groups;
}

/**
 * ges_timeline_append_layer:
 * @timeline: The #GESTimeline
 *
 * Append a newly created layer to the timeline. The layer will
 * be added at the lowest #GESLayer:priority (numerically, the highest).
 *
 * Returns: (transfer none): The newly created layer.
 */
GESLayer *
ges_timeline_append_layer (GESTimeline * timeline)
{
  GList *tmp;
  guint32 priority;
  GESLayer *layer;

  g_return_val_if_fail (GES_IS_TIMELINE (timeline), NULL);
  CHECK_THREAD (timeline);

  layer = ges_layer_new ();

  priority = 0;
  for (tmp = timeline->layers; tmp; tmp = tmp->next)
    priority = MAX (priority, ges_layer_get_priority (tmp->data) + 1);

  ges_layer_set_priority (layer, priority);

  ges_timeline_add_layer (timeline, layer);

  return layer;
}

/**
 * ges_timeline_add_layer:
 * @timeline: The #GESTimeline
 * @layer: (transfer floating): The layer to add
 *
 * Add a layer to the timeline.
 *
 * If the layer contains #GESClip-s, then this may trigger the creation of
 * their core track element children for the timeline's tracks, and the
 * placement of the clip's children in the tracks of the timeline using
 * #GESTimeline::select-tracks-for-object. Some errors may occur if this
 * would break one of the configuration rules of the timeline in one of
 * its tracks. In such cases, some track elements would fail to be added
 * to their tracks, but this method would still return %TRUE. As such, it
 * is advised that you only add clips to layers that already part of a
 * timeline. In such situations, ges_layer_add_clip() is able to fail if
 * adding the clip would cause such an error.
 *
 * Deprecated: 1.18: This method requires you to ensure the layer's
 * #GESLayer:priority will be unique to the timeline. Use
 * ges_timeline_append_layer() and ges_timeline_move_layer() instead.
 *
 * Returns: %TRUE if @layer was properly added.
 */
gboolean
ges_timeline_add_layer (GESTimeline * timeline, GESLayer * layer)
{
  gboolean auto_transition;
  GList *objects, *tmp;

  g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);
  g_return_val_if_fail (GES_IS_LAYER (layer), FALSE);
  CHECK_THREAD (timeline);

  GST_DEBUG ("timeline:%p, layer:%p", timeline, layer);

  /* We can only add a layer that doesn't already belong to another timeline */
  if (G_UNLIKELY (layer->timeline)) {
    GST_WARNING ("Layer belongs to another timeline, can't add it");
    gst_object_ref_sink (layer);
    gst_object_unref (layer);
    return FALSE;
  }

  /* Add to the list of layers, make sure we don't already control it */
  if (G_UNLIKELY (g_list_find (timeline->layers, (gconstpointer) layer))) {
    GST_WARNING ("Layer is already controlled by this timeline");
    gst_object_ref_sink (layer);
    gst_object_unref (layer);
    return FALSE;
  }

  /* FIXME: ensure the layer->priority does not conflict with an existing
   * layer in the timeline. Currently can add several layers with equal
   * layer priorities */

  auto_transition = ges_layer_get_auto_transition (layer);

  /* If the user doesn't explicitely set layer auto_transition, then set our */
  if (!auto_transition) {
    auto_transition = ges_timeline_get_auto_transition (timeline);
    ges_layer_set_auto_transition (layer, auto_transition);
  }

  gst_object_ref_sink (layer);
  timeline->layers = g_list_insert_sorted (timeline->layers, layer,
      (GCompareFunc) sort_layers);

  /* Inform the layer that it belongs to a new timeline */
  ges_layer_set_timeline (layer, timeline);

  /* Connect to 'clip-added'/'clip-removed' signal from the new layer */
  g_signal_connect (layer, "notify::priority",
      G_CALLBACK (layer_priority_changed_cb), timeline);
  g_signal_connect (layer, "notify::auto-transition",
      G_CALLBACK (layer_auto_transition_changed_cb), timeline);
  g_signal_connect_after (layer, "active-changed",
      G_CALLBACK (layer_active_changed_cb), timeline);

  GST_DEBUG ("Done adding layer, emitting 'layer-added' signal");
  g_signal_emit (timeline, ges_timeline_signals[LAYER_ADDED], 0, layer);

  /* add any existing clips to the timeline */
  objects = ges_layer_get_clips (layer);
  for (tmp = objects; tmp; tmp = tmp->next)
    ges_timeline_add_clip (timeline, tmp->data, NULL);
  g_list_free_full (objects, gst_object_unref);

  return TRUE;
}

/**
 * ges_timeline_remove_layer:
 * @timeline: The #GESTimeline
 * @layer: The layer to remove
 *
 * Removes a layer from the timeline.
 *
 * Returns: %TRUE if @layer was properly removed.
 */

gboolean
ges_timeline_remove_layer (GESTimeline * timeline, GESLayer * layer)
{
  GList *layer_objects, *tmp;

  g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);
  g_return_val_if_fail (GES_IS_LAYER (layer), FALSE);

  if (!timeline->priv->disposed)
    CHECK_THREAD (timeline);

  GST_DEBUG ("timeline:%p, layer:%p", timeline, layer);

  if (G_UNLIKELY (!g_list_find (timeline->layers, layer))) {
    GST_WARNING ("Layer doesn't belong to this timeline");
    return FALSE;
  }

  /* remove objects from any private data structures */

  layer_objects = ges_layer_get_clips (layer);
  for (tmp = layer_objects; tmp; tmp = tmp->next)
    ges_timeline_remove_clip (timeline, tmp->data);
  g_list_free_full (layer_objects, gst_object_unref);

  /* Disconnect signals */
  GST_DEBUG ("Disconnecting signal callbacks");
  g_signal_handlers_disconnect_by_func (layer, layer_priority_changed_cb,
      timeline);
  g_signal_handlers_disconnect_by_func (layer,
      layer_auto_transition_changed_cb, timeline);
  g_signal_handlers_disconnect_by_func (layer, layer_active_changed_cb,
      timeline);

  timeline->layers = g_list_remove (timeline->layers, layer);
  ges_layer_set_timeline (layer, NULL);
  /* FIXME: we should resync the layer priorities */

  g_signal_emit (timeline, ges_timeline_signals[LAYER_REMOVED], 0, layer);

  gst_object_unref (layer);

  return TRUE;
}

/**
 * ges_timeline_add_track:
 * @timeline: The #GESTimeline
 * @track: (transfer floating): The track to add
 *
 * Add a track to the timeline.
 *
 * If the timeline already contains clips, then this may trigger the
 * creation of their core track element children for the track, and the
 * placement of the clip's children in the track of the timeline using
 * #GESTimeline::select-tracks-for-object. Some errors may occur if this
 * would break one of the configuration rules for the timeline in the
 * track. In such cases, some track elements would fail to be added to the
 * track, but this method would still return %TRUE. As such, it is advised
 * that you avoid adding tracks to timelines that already contain clips.
 *
 * Returns: %TRUE if @track was properly added.
 */

/* FIXME: create track elements for clips which have already been
 * added to existing layers.
 */

gboolean
ges_timeline_add_track (GESTimeline * timeline, GESTrack * track)
{
  TrackPrivate *tr_priv;
  GList *tmp;

  g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);
  g_return_val_if_fail (GES_IS_TRACK (track), FALSE);
  CHECK_THREAD (timeline);

  GST_DEBUG_OBJECT (timeline, "Adding %" GST_PTR_FORMAT, track);

  /* make sure we don't already control it */
  LOCK_DYN (timeline);
  if (G_UNLIKELY (g_list_find (timeline->tracks, (gconstpointer) track))) {
    UNLOCK_DYN (timeline);
    GST_WARNING ("Track is already controlled by this timeline");
    gst_object_ref_sink (track);
    gst_object_unref (track);
    return FALSE;
  }

  /* Add the track to ourself (as a GstBin)
   * Reference is stolen ! */
  if (G_UNLIKELY (!gst_bin_add (GST_BIN (timeline), GST_ELEMENT (track)))) {
    UNLOCK_DYN (timeline);
    GST_WARNING ("Couldn't add track to ourself (GST)");
    return FALSE;
  }

  tr_priv = g_new0 (TrackPrivate, 1);
  tr_priv->timeline = timeline;
  tr_priv->track = track;
  tr_priv->track_element_added_sigid = g_signal_connect (track,
      "track-element-added", G_CALLBACK (track_element_added_cb), timeline);

  update_stream_object (tr_priv);
  gst_stream_collection_add_stream (timeline->priv->stream_collection,
      gst_object_ref (tr_priv->stream));

  /* Add the track to the list of tracks we track */
  timeline->priv->priv_tracks = g_list_append (timeline->priv->priv_tracks,
      tr_priv);
  timeline->tracks = g_list_append (timeline->tracks, track);

  /* Inform the track that it's currently being used by ourself */
  ges_track_set_timeline (track, timeline);

  GST_DEBUG ("Done adding track, emitting 'track-added' signal");

  _ghost_track_srcpad (tr_priv);
  UNLOCK_DYN (timeline);

  /* emit 'track-added' */
  g_signal_emit (timeline, ges_timeline_signals[TRACK_ADDED], 0, track);

  /* ensure that each existing clip has the opportunity to create a
   * track element for this track*/

  for (tmp = timeline->layers; tmp; tmp = tmp->next) {
    GList *objects, *obj;
    objects = ges_layer_get_clips (tmp->data);

    for (obj = objects; obj; obj = obj->next)
      add_object_to_tracks (timeline, obj->data, track, NULL);

    g_list_free_full (objects, gst_object_unref);
  }

  /* FIXME Check if we should rollback if we can't sync state */
  gst_element_sync_state_with_parent (GST_ELEMENT (track));
  g_object_set (track, "message-forward", TRUE, NULL);

  return TRUE;
}

/**
 * ges_timeline_remove_track:
 * @timeline: The #GESTimeline
 * @track: The track to remove
 *
 * Remove a track from the timeline.
 *
 * Returns: %TRUE if @track was properly removed.
 */

/* FIXME: release any track elements associated with this layer. currenly this
 * will not happen if you remove the track before removing *all*
 * clips which have a track element in this track.
 */

gboolean
ges_timeline_remove_track (GESTimeline * timeline, GESTrack * track)
{
  GList *tmp;
  TrackPrivate *tr_priv;
  GESTimelinePrivate *priv;

  g_return_val_if_fail (GES_IS_TRACK (track), FALSE);
  g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);

  GST_DEBUG ("timeline:%p, track:%p", timeline, track);

  priv = timeline->priv;
  LOCK_DYN (timeline);
  if (G_UNLIKELY (!(tmp = g_list_find_custom (priv->priv_tracks,
                  track, (GCompareFunc) custom_find_track)))) {
    GST_WARNING ("Track doesn't belong to this timeline");
    UNLOCK_DYN (timeline);
    return FALSE;
  }

  tr_priv = tmp->data;
  gst_object_unref (tr_priv->pad);
  priv->priv_tracks = g_list_remove (priv->priv_tracks, tr_priv);
  UNLOCK_DYN (timeline);

  /* empty track of all elements that belong to the timeline's clips */
  /* elements with no parent can stay in the track, but their timeline
   * will be set to NULL when the track's timeline is set to NULL */

  for (tmp = timeline->layers; tmp; tmp = tmp->next) {
    GList *clips, *clip;
    clips = ges_layer_get_clips (tmp->data);

    for (clip = clips; clip; clip = clip->next)
      ges_clip_empty_from_track (clip->data, track);

    g_list_free_full (clips, gst_object_unref);
  }

  timeline->tracks = g_list_remove (timeline->tracks, track);
  ges_track_set_timeline (track, NULL);

  /* Remove ghost pad */
  if (tr_priv->ghostpad) {
    GST_DEBUG ("Removing ghostpad");
    gst_pad_set_active (tr_priv->ghostpad, FALSE);
    gst_ghost_pad_set_target ((GstGhostPad *) tr_priv->ghostpad, NULL);
    gst_element_remove_pad (GST_ELEMENT (timeline), tr_priv->ghostpad);
  }

  /* Signal track removal to all layers/objects */
  g_signal_emit (timeline, ges_timeline_signals[TRACK_REMOVED], 0, track);

  /* remove track from our bin */
  gst_object_ref (track);
  if (G_UNLIKELY (!gst_bin_remove (GST_BIN (timeline), GST_ELEMENT (track)))) {
    GST_WARNING ("Couldn't remove track to ourself (GST)");
    gst_object_unref (track);
    return FALSE;
  }

  g_signal_handler_disconnect (track, tr_priv->track_element_added_sigid);

  /* set track state to NULL */
  gst_element_set_state (GST_ELEMENT (track), GST_STATE_NULL);

  gst_object_unref (track);

  g_free (tr_priv);

  return TRUE;
}

/**
 * ges_timeline_get_track_for_pad:
 * @timeline: The #GESTimeline
 * @pad: A pad
 *
 * Search for the #GESTrack corresponding to the given timeline's pad.
 *
 * Returns: (transfer none) (nullable): The track corresponding to @pad,
 * or %NULL if there is an error.
 */

GESTrack *
ges_timeline_get_track_for_pad (GESTimeline * timeline, GstPad * pad)
{
  GList *tmp;

  g_return_val_if_fail (GES_IS_TIMELINE (timeline), NULL);

  LOCK_DYN (timeline);
  for (tmp = timeline->priv->priv_tracks; tmp; tmp = g_list_next (tmp)) {
    TrackPrivate *tr_priv = (TrackPrivate *) tmp->data;
    if (pad == tr_priv->ghostpad) {
      UNLOCK_DYN (timeline);
      return tr_priv->track;
    }
  }
  UNLOCK_DYN (timeline);

  return NULL;
}

/**
 * ges_timeline_get_pad_for_track:
 * @timeline: The #GESTimeline
 * @track: A track
 *
 * Search for the #GstPad corresponding to the given timeline's track.
 * You can link to this pad to receive the output data of the given track.
 *
 * Returns: (transfer none) (nullable): The pad corresponding to @track,
 * or %NULL if there is an error.
 */

GstPad *
ges_timeline_get_pad_for_track (GESTimeline * timeline, GESTrack * track)
{
  GList *tmp;

  LOCK_DYN (timeline);
  for (tmp = timeline->priv->priv_tracks; tmp; tmp = g_list_next (tmp)) {
    TrackPrivate *tr_priv = (TrackPrivate *) tmp->data;

    if (track == tr_priv->track) {
      if (tr_priv->ghostpad)
        gst_object_ref (tr_priv->ghostpad);

      UNLOCK_DYN (timeline);
      return tr_priv->ghostpad;
    }
  }
  UNLOCK_DYN (timeline);

  return NULL;
}

/**
 * ges_timeline_get_tracks:
 * @timeline: The #GESTimeline
 *
 * Get the list of #GESTrack-s used by the timeline.
 *
 * Returns: (transfer full) (element-type GESTrack): The list of tracks
 * used by @timeline.
 */
GList *
ges_timeline_get_tracks (GESTimeline * timeline)
{
  GList *res = NULL;
  g_return_val_if_fail (GES_IS_TIMELINE (timeline), NULL);

  LOCK_DYN (timeline);
  res = g_list_copy_deep (timeline->tracks, (GCopyFunc) gst_object_ref, NULL);
  UNLOCK_DYN (timeline);

  return res;
}

/**
 * ges_timeline_get_layers:
 * @timeline: The #GESTimeline
 *
 * Get the list of #GESLayer-s present in the timeline.
 *
 * Returns: (transfer full) (element-type GESLayer): The list of
 * layers present in @timeline sorted by priority.
 */
GList *
ges_timeline_get_layers (GESTimeline * timeline)
{
  GList *tmp, *res = NULL;

  g_return_val_if_fail (GES_IS_TIMELINE (timeline), NULL);
  CHECK_THREAD (timeline);

  for (tmp = timeline->layers; tmp; tmp = g_list_next (tmp)) {
    res = g_list_insert_sorted (res, gst_object_ref (tmp->data),
        (GCompareFunc) sort_layers);
  }

  return res;
}

static void
track_commited_cb (GESTrack * track, GESTimeline * timeline)
{
  gboolean emit_commited = FALSE;
  GST_OBJECT_LOCK (timeline);
  timeline->priv->expected_commited -= 1;
  if (timeline->priv->expected_commited == 0)
    emit_commited = TRUE;
  g_signal_handlers_disconnect_by_func (track, track_commited_cb, timeline);
  GST_OBJECT_UNLOCK (timeline);

  if (emit_commited) {
    g_signal_emit (timeline, ges_timeline_signals[COMMITED], 0);
  }
}

/* Must be called with the timeline's DYN_LOCK */
static gboolean
ges_timeline_commit_unlocked (GESTimeline * timeline)
{
  GList *tmp;
  gboolean res = TRUE;

  if (timeline->priv->commit_frozen) {
    GST_DEBUG_OBJECT (timeline, "commit locked");
    timeline->priv->commit_delayed = TRUE;
    return res;
  }

  GST_DEBUG_OBJECT (timeline, "commiting changes");

  timeline_tree_create_transitions (timeline->priv->tree,
      ges_timeline_find_auto_transition);
  for (tmp = timeline->layers; tmp; tmp = tmp->next) {
    GESLayer *layer = tmp->data;

    /* Ensure clip priorities are correct after an edit */
    ges_layer_resync_priorities (layer);
  }

  timeline->priv->expected_commited =
      g_list_length (timeline->priv->priv_tracks);

  if (timeline->priv->expected_commited == 0) {
    g_signal_emit (timeline, ges_timeline_signals[COMMITED], 0);
  } else {
    GstStreamCollection *collection = gst_stream_collection_new (NULL);

    LOCK_DYN (timeline);
    for (tmp = timeline->tracks; tmp; tmp = tmp->next) {
      TrackPrivate *tr_priv =
          g_list_find_custom (timeline->priv->priv_tracks, tmp->data,
          (GCompareFunc) custom_find_track)->data;

      update_stream_object (tr_priv);
      gst_stream_collection_add_stream (collection,
          gst_object_ref (tr_priv->stream));
      g_signal_connect (tmp->data, "commited", G_CALLBACK (track_commited_cb),
          timeline);
      if (!ges_track_commit (GES_TRACK (tmp->data)))
        res = FALSE;
    }

    gst_object_unref (timeline->priv->stream_collection);
    timeline->priv->stream_collection = collection;
    UNLOCK_DYN (timeline);
  }

  return res;
}

/**
 * ges_timeline_commit:
 * @timeline: A #GESTimeline
 *
 * Commit all the pending changes of the clips contained in the
 * timeline.
 *
 * When changes happen in a timeline, they are not immediately executed
 * internally, in a way that effects the output data of the timeline. You
 * should call this method when you are done with a set of changes and you
 * want them to be executed.
 *
 * Any pending changes will be executed in the backend. The
 * #GESTimeline::commited signal will be emitted once this has completed.
 * You should not try to change the state of the timeline, seek it or add
 * tracks to it before receiving this signal. You can use
 * ges_timeline_commit_sync() if you do not want to perform other tasks in
 * the mean time.
 *
 * Note that all the pending changes will automatically be executed when
 * the timeline goes from #GST_STATE_READY to #GST_STATE_PAUSED, which is
 * usually triggered by a corresponding state changes in a containing
 * #GESPipeline.
 *
 * Returns: %TRUE if pending changes were committed, or %FALSE if nothing
 * needed to be committed.
 */
gboolean
ges_timeline_commit (GESTimeline * timeline)
{
  gboolean ret;
  GstStreamCollection *pcollection = timeline->priv->stream_collection;

  g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);

  LOCK_DYN (timeline);
  ret = ges_timeline_commit_unlocked (timeline);
  UNLOCK_DYN (timeline);

  if (pcollection != timeline->priv->stream_collection) {
    ges_timeline_post_stream_collection (timeline);
  }

  ges_timeline_emit_snapping (timeline, NULL, NULL, GST_CLOCK_TIME_NONE);
  return ret;
}

static void
commited_cb (GESTimeline * timeline)
{
  g_mutex_lock (&timeline->priv->commited_lock);
  g_cond_signal (&timeline->priv->commited_cond);
  g_mutex_unlock (&timeline->priv->commited_lock);
}

/**
 * ges_timeline_commit_sync:
 * @timeline: A #GESTimeline
 *
 * Commit all the pending changes of the clips contained in the
 * timeline and wait for the changes to complete.
 *
 * See ges_timeline_commit().
 *
 * Returns: %TRUE if pending changes were committed, or %FALSE if nothing
 * needed to be committed.
 */
gboolean
ges_timeline_commit_sync (GESTimeline * timeline)
{
  gboolean ret;
  gboolean wait_for_signal;

  g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);

  /* Let's make sure our state is stable */
  gst_element_get_state (GST_ELEMENT (timeline), NULL, NULL,
      GST_CLOCK_TIME_NONE);

  /* Let's make sure no track gets added between now and the actual commiting */
  LOCK_DYN (timeline);
  wait_for_signal = g_list_length (timeline->priv->priv_tracks) > 0
      && GST_STATE (timeline) >= GST_STATE_PAUSED;

  if (!wait_for_signal) {
    ret = ges_timeline_commit_unlocked (timeline);
  } else {
    gulong handler_id =
        g_signal_connect (timeline, "commited", (GCallback) commited_cb, NULL);

    g_mutex_lock (&timeline->priv->commited_lock);

    ret = ges_timeline_commit_unlocked (timeline);
    g_cond_wait (&timeline->priv->commited_cond,
        &timeline->priv->commited_lock);
    g_mutex_unlock (&timeline->priv->commited_lock);
    g_signal_handler_disconnect (timeline, handler_id);
  }

  UNLOCK_DYN (timeline);

  return ret;
}

/**
 * ges_timeline_freeze_commit:
 * @timeline: The #GESTimeline
 *
 * Freezes the timeline from being committed. This is usually needed while the
 * timeline is being rendered to ensure that not change to the timeline are
 * taken into account during that moment. Once the rendering is done, you
 * should call #ges_timeline_thaw_commit so that committing becomes possible
 * again and any call to `commit()` that happened during the rendering is
 * actually taken into account.
 *
 * Since: 1.20
 *
 */
void
ges_timeline_freeze_commit (GESTimeline * timeline)
{
  LOCK_DYN (timeline);
  timeline->priv->commit_frozen = TRUE;
  UNLOCK_DYN (timeline);
}

/**
 * ges_timeline_thaw_commit:
 * @timeline: The #GESTimeline
 *
 * Thaw the timeline so that comiting becomes possible
 * again and any call to `commit()` that happened during the rendering is
 * actually taken into account.
 *
 * Since: 1.20
 *
 */
void
ges_timeline_thaw_commit (GESTimeline * timeline)
{
  LOCK_DYN (timeline);
  timeline->priv->commit_frozen = FALSE;
  UNLOCK_DYN (timeline);
  if (timeline->priv->commit_delayed) {
    ges_timeline_commit (timeline);
    timeline->priv->commit_delayed = FALSE;
  }
}

/**
 * ges_timeline_get_duration:
 * @timeline: The #GESTimeline
 *
 * Get the current #GESTimeline:duration of the timeline
 *
 * Returns: The current duration of @timeline.
 */
GstClockTime
ges_timeline_get_duration (GESTimeline * timeline)
{
  g_return_val_if_fail (GES_IS_TIMELINE (timeline), GST_CLOCK_TIME_NONE);
  CHECK_THREAD (timeline);

  return timeline->priv->duration;
}

/**
 * ges_timeline_get_auto_transition:
 * @timeline: The #GESTimeline
 *
 * Gets #GESTimeline:auto-transition for the timeline.
 *
 * Returns: The auto-transition of @self.
 */
gboolean
ges_timeline_get_auto_transition (GESTimeline * timeline)
{
  g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);
  CHECK_THREAD (timeline);

  return timeline->priv->auto_transition;
}

/**
 * ges_timeline_set_auto_transition:
 * @timeline: The #GESTimeline
 * @auto_transition: Whether transitions should be automatically added
 * to @timeline's layers
 *
 * Sets #GESTimeline:auto-transition for the timeline. This will also set
 * the corresponding #GESLayer:auto-transition for all of the timeline's
 * layers to the same value. See ges_layer_set_auto_transition() if you
 * wish to set the layer's #GESLayer:auto-transition individually.
 */
void
ges_timeline_set_auto_transition (GESTimeline * timeline,
    gboolean auto_transition)
{
  GList *layers;
  GESLayer *layer;

  g_return_if_fail (GES_IS_TIMELINE (timeline));
  g_return_if_fail (!timeline->priv->disable_edit_apis);
  CHECK_THREAD (timeline);

  timeline->priv->auto_transition = auto_transition;
  g_object_notify (G_OBJECT (timeline), "auto-transition");

  layers = timeline->layers;
  for (; layers; layers = layers->next) {
    layer = layers->data;
    ges_layer_set_auto_transition (layer, auto_transition);
  }
}

/**
 * ges_timeline_get_snapping_distance:
 * @timeline: The #GESTimeline
 *
 * Gets the #GESTimeline:snapping-distance for the timeline.
 *
 * Returns: The snapping distance (in nanoseconds) of @timeline.
 */
GstClockTime
ges_timeline_get_snapping_distance (GESTimeline * timeline)
{
  g_return_val_if_fail (GES_IS_TIMELINE (timeline), GST_CLOCK_TIME_NONE);
  CHECK_THREAD (timeline);

  return timeline->priv->snapping_distance;

}

/**
 * ges_timeline_set_snapping_distance:
 * @timeline: The #GESTimeline
 * @snapping_distance: The snapping distance to use (in nanoseconds)
 *
 * Sets #GESTimeline:snapping-distance for the timeline. This new value
 * will only effect future snappings and will not be used to snap the
 * current element positions within the timeline.
 */
void
ges_timeline_set_snapping_distance (GESTimeline * timeline,
    GstClockTime snapping_distance)
{
  g_return_if_fail (GES_IS_TIMELINE (timeline));
  g_return_if_fail (GST_CLOCK_TIME_IS_VALID (snapping_distance));
  CHECK_THREAD (timeline);

  timeline->priv->snapping_distance = snapping_distance;
}

/**
 * ges_timeline_get_element:
 * @timeline: The #GESTimeline
 * @name: The name of the element to find
 *
 * Gets the element contained in the timeline with the given name.
 *
 * Returns: (transfer full) (nullable): The timeline element in @timeline
 * with the given @name, or %NULL if it was not found.
 */
GESTimelineElement *
ges_timeline_get_element (GESTimeline * timeline, const gchar * name)
{
  GESTimelineElement *ret;

  g_return_val_if_fail (GES_IS_TIMELINE (timeline), NULL);
  CHECK_THREAD (timeline);

  /* FIXME: handle NULL name */
  ret = g_hash_table_lookup (timeline->priv->all_elements, name);

  if (ret)
    return gst_object_ref (ret);

#ifndef GST_DISABLE_GST_DEBUG
  {
    GList *element_names, *tmp;
    element_names = g_hash_table_get_keys (timeline->priv->all_elements);

    GST_INFO_OBJECT (timeline, "Does not contain element %s", name);

    for (tmp = element_names; tmp; tmp = tmp->next) {
      GST_DEBUG_OBJECT (timeline, "Containes: %s", (gchar *) tmp->data);
    }
    g_list_free (element_names);
  }
#endif

  return NULL;
}

/**
 * ges_timeline_is_empty:
 * @timeline: The #GESTimeline
 *
 * Check whether the timeline is empty or not.
 *
 * Returns: %TRUE if @timeline is empty.
 */
gboolean
ges_timeline_is_empty (GESTimeline * timeline)
{
  GHashTableIter iter;
  gpointer key, value;

  g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);
  CHECK_THREAD (timeline);

  if (g_hash_table_size (timeline->priv->all_elements) == 0)
    return TRUE;

  g_hash_table_iter_init (&iter, timeline->priv->all_elements);
  while (g_hash_table_iter_next (&iter, &key, &value)) {
    if (GES_IS_SOURCE (value) &&
        ges_track_element_is_active (GES_TRACK_ELEMENT (value)))
      return FALSE;
  }

  return TRUE;
}

/**
 * ges_timeline_get_layer:
 * @timeline: The #GESTimeline to retrieve a layer from
 * @priority: The priority/index of the layer to find
 *
 * Retrieve the layer whose index in the timeline matches the given
 * priority.
 *
 * Returns: (transfer full) (nullable): The layer with the given
 * @priority, or %NULL if none was found.
 *
 * Since 1.6
 */
GESLayer *
ges_timeline_get_layer (GESTimeline * timeline, guint priority)
{
  GList *tmp;
  GESLayer *layer = NULL;

  g_return_val_if_fail (GES_IS_TIMELINE (timeline), NULL);
  CHECK_THREAD (timeline);

  for (tmp = timeline->layers; tmp; tmp = tmp->next) {
    GESLayer *tmp_layer = GES_LAYER (tmp->data);
    guint tmp_priority;

    g_object_get (tmp_layer, "priority", &tmp_priority, NULL);
    if (tmp_priority == priority) {
      layer = gst_object_ref (tmp_layer);
      break;
    }
  }

  return layer;
}

gboolean
ges_timeline_layer_priority_in_gap (GESTimeline * timeline, guint priority)
{
  GList *tmp;

  CHECK_THREAD (timeline);

  for (tmp = timeline->layers; tmp; tmp = tmp->next) {
    GESLayer *layer = GES_LAYER (tmp->data);
    guint tmp_priority = ges_layer_get_priority (layer);

    if (tmp_priority == priority)
      return FALSE;
    else if (tmp_priority > priority)
      return TRUE;
  }

  return FALSE;
}

/**
 * ges_timeline_paste_element:
 * @timeline: The #GESTimeline onto which @element should be pasted
 * @element: The element to paste
 * @position: The position in the timeline @element should be pasted to,
 * i.e. the #GESTimelineElement:start value for the pasted element.
 * @layer_priority: The layer into which the element should be pasted.
 * -1 means paste to the same layer from which @element has been copied from
 *
 * Paste an element inside the timeline. @element **must** be the return of
 * ges_timeline_element_copy() with `deep=TRUE`,
 * and it should not be changed before pasting. @element itself is not
 * placed in the timeline, instead a new element is created, alike to the
 * originally copied element. Note that the originally copied element must
 * also lie within @timeline, at both the point of copying and pasting.
 *
 * Pasting may fail if it would place the timeline in an unsupported
 * configuration.
 *
 * After calling this function @element should not be used. In particular,
 * @element can **not** be pasted again. Instead, you can copy the
 * returned element and paste that copy (although, this is only possible
 * if the paste was successful).
 *
 * See also ges_timeline_element_paste().
 *
 * Returns: (transfer full) (nullable): The newly created element, or
 * %NULL if pasting fails.
 */
GESTimelineElement *
ges_timeline_paste_element (GESTimeline * timeline,
    GESTimelineElement * element, GstClockTime position, gint layer_priority)
{
  GESTimelineElement *res, *copied_from;
  GESTimelineElementClass *element_class;

  g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);
  g_return_val_if_fail (GES_IS_TIMELINE_ELEMENT (element), FALSE);
  g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (position), FALSE);
  CHECK_THREAD (timeline);

  element_class = GES_TIMELINE_ELEMENT_GET_CLASS (element);
  /* steal ownership of the copied element */
  copied_from = ges_timeline_element_get_copied_from (element);

  if (!copied_from) {
    GST_ERROR_OBJECT (element, "Is not being 'deeply' copied!");

    return NULL;
  }

  if (!element_class->paste) {
    GST_ERROR_OBJECT (element, "No paste vmethod implemented");
    gst_object_unref (copied_from);
    return NULL;
  }

  /*
   * Currently the API only supports pasting onto the same layer from which
   * the @element has been copied from, i.e., @layer_priority needs to be -1.
   */
  if (layer_priority != -1) {
    GST_WARNING_OBJECT (timeline,
        "Only -1 value for layer priority is supported");
    gst_object_unref (copied_from);
    return NULL;
  }

  res = element_class->paste (element, copied_from, position);

  gst_object_unref (copied_from);

  return res ? g_object_ref_sink (res) : res;
}

/**
 * ges_timeline_move_layer:
 * @timeline: A #GESTimeline
 * @layer: A layer within @timeline, whose priority should be changed
 * @new_layer_priority: The new index for @layer
 *
 * Moves a layer within the timeline to the index given by
 * @new_layer_priority.
 * An index of 0 corresponds to the layer with the highest priority in a
 * timeline. If @new_layer_priority is greater than the number of layers
 * present in the timeline, it will become the lowest priority layer.
 *
 * Since: 1.16
 */
gboolean
ges_timeline_move_layer (GESTimeline * timeline, GESLayer * layer,
    guint new_layer_priority)
{
  gint current_priority;

  g_return_val_if_fail (GES_IS_TIMELINE (timeline), FALSE);
  g_return_val_if_fail (GES_IS_LAYER (layer), FALSE);
  g_return_val_if_fail (ges_layer_get_timeline (layer) == timeline, FALSE);
  CHECK_THREAD (timeline);

  current_priority = ges_layer_get_priority (layer);

  if (new_layer_priority == current_priority) {
    GST_DEBUG_OBJECT (timeline,
        "Nothing to do for %" GST_PTR_FORMAT ", same priorities", layer);

    return TRUE;
  }

  timeline->layers = g_list_remove (timeline->layers, layer);
  timeline->layers = g_list_insert (timeline->layers, layer,
      (gint) new_layer_priority);

  _resync_layers (timeline);

  return TRUE;
}

/**
 * ges_timeline_get_frame_time:
 * @self: The self on which to retrieve the timestamp for @frame_number
 * @frame_number: The frame number to get the corresponding timestamp of in the
 *                timeline coordinates
 *
 * This method allows you to convert a timeline output frame number into a
 * timeline #GstClockTime. For example, this time could be used to seek to a
 * particular frame in the timeline's output, or as the edit position for
 * an element within the timeline.
 *
 * Returns: The timestamp corresponding to @frame_number in the output of @self.
 *
 * Since: 1.18
 */
GstClockTime
ges_timeline_get_frame_time (GESTimeline * self, GESFrameNumber frame_number)
{
  gint fps_n, fps_d;

  g_return_val_if_fail (GES_IS_TIMELINE (self), GST_CLOCK_TIME_NONE);
  g_return_val_if_fail (GES_FRAME_NUMBER_IS_VALID (frame_number),
      GST_CLOCK_TIME_NONE);

  timeline_get_framerate (self, &fps_n, &fps_d);

  return gst_util_uint64_scale_ceil (frame_number, fps_d * GST_SECOND, fps_n);
}

/**
 * ges_timeline_get_frame_at:
 * @self: A #GESTimeline
 * @timestamp: The timestamp to get the corresponding frame number of
 *
 * This method allows you to convert a timeline #GstClockTime into its
 * corresponding #GESFrameNumber in the timeline's output.
 *
 * Returns: The frame number @timestamp corresponds to.
 *
 * Since: 1.18
 */
GESFrameNumber
ges_timeline_get_frame_at (GESTimeline * self, GstClockTime timestamp)
{
  gint fps_n, fps_d;

  g_return_val_if_fail (GES_IS_TIMELINE (self), GES_FRAME_NUMBER_NONE);
  g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp),
      GES_FRAME_NUMBER_NONE);

  timeline_get_framerate (self, &fps_n, &fps_d);

  return gst_util_uint64_scale (timestamp, fps_n, fps_d * GST_SECOND);
}

/**
 * ges_timeline_disable_edit_apis:
 * @self: A #GESTimeline
 * @disable_edit_apis: %TRUE to disable all the edit APIs so the user is in full
 * control of ensuring timeline state validity %FALSE otherwise.
 *
 * WARNING: When using that mode, GES won't guarantee the coherence of the
 * timeline. You need to ensure that the rules described in the [Overlaps and
 * auto transitions](#overlaps-and-autotransitions) section are respected any time
 * the timeline is [commited](ges_timeline_commit) (otherwise playback will most
 * probably fail in different ways).
 *
 * When disabling editing APIs, GES won't be able to enforce the rules that
 * makes the timeline overall state to be valid but some feature won't be
 * usable:
 *   * #GESTimeline:snapping-distance
 *   * #GESTimeline:auto-transition
 *
 * Since: 1.22
 */
void
ges_timeline_disable_edit_apis (GESTimeline * self, gboolean disable_edit_apis)
{
  CHECK_THREAD (self);
  g_return_if_fail (GES_IS_TIMELINE (self));

  if (disable_edit_apis) {
    if (self->priv->snapping_distance > 0) {
      GST_INFO_OBJECT (self,
          "Disabling snapping as we are disabling edit APIs");

      ges_timeline_set_snapping_distance (self, 0);
    }

    if (self->priv->auto_transition || self->priv->auto_transitions) {
      GST_INFO_OBJECT (self,
          "Disabling auto transitions as we are disabling auto edit APIs");
      ges_timeline_set_auto_transition (self, FALSE);
    }
  }

  self->priv->disable_edit_apis = disable_edit_apis;
}

/**
 * ges_timeline_get_edit_apis_disabled:
 * @self: A #GESTimeline
 *
 * Returns: %TRUE if edit APIs are disabled, %FALSE otherwise.
 *
 * Since: 1.22
 */
gboolean
ges_timeline_get_edit_apis_disabled (GESTimeline * self)
{
  CHECK_THREAD (self);
  g_return_val_if_fail (GES_IS_TIMELINE (self), FALSE);

  return self->priv->disable_edit_apis;
}