summaryrefslogtreecommitdiff
path: root/gst/timecode/gsttimecodestamper.c
blob: f115ed7d1bc753e631167ada0eecad182b8a9658 (plain)
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
/*
 * GStreamer
 * Copyright (C) 2016 Vivia Nikolaidou <vivia@toolsonair.com>
 * Copyright (C) 2019 Sebastian Dröge <sebastian@centricular.com>
 *
 * gsttimecodestamper.c
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

/**
 * SECTION:element-timecodestamper
 * @title: timecodestamper
 * @short_description: Attach a timecode into incoming video frames
 *
 * This element attaches a timecode into every incoming video frame. It starts
 * counting from the stream time of each segment start, which it converts into
 * a timecode.
 *
 * ## Example launch line
 * |[
 * gst-launch-1.0 videotestsrc ! timecodestamper ! autovideosink
 * ]|
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "gsttimecodestamper.h"

#include <gst/gst.h>
#include <gst/video/video.h>
#include <gst/audio/audio.h>
#include <stdlib.h>
#include <string.h>

#define ABSDIFF(a,b) (((a) > (b)) ? ((a) - (b)) : ((b) - (a)))

GST_DEBUG_CATEGORY_STATIC (timecodestamper_debug);
#define GST_CAT_DEFAULT timecodestamper_debug

/* GstTimeCodeStamper properties */
enum
{
  PROP_0,
  PROP_SOURCE,
  PROP_SET,
  PROP_AUTO_RESYNC,
  PROP_TIMEOUT,
  PROP_DROP_FRAME,
  PROP_POST_MESSAGES,
  PROP_SET_INTERNAL_TIMECODE,
  PROP_LTC_DAILY_JAM,
  PROP_LTC_AUTO_RESYNC,
  PROP_LTC_EXTRA_LATENCY,
  PROP_LTC_TIMEOUT,
  PROP_RTC_MAX_DRIFT,
  PROP_RTC_AUTO_RESYNC,
  PROP_TIMECODE_OFFSET
};

#define DEFAULT_SOURCE GST_TIME_CODE_STAMPER_SOURCE_INTERNAL
#define DEFAULT_SET GST_TIME_CODE_STAMPER_SET_KEEP
#define DEFAULT_AUTO_RESYNC TRUE
#define DEFAULT_TIMEOUT GST_CLOCK_TIME_NONE
#define DEFAULT_DROP_FRAME FALSE
#define DEFAULT_POST_MESSAGES FALSE
#define DEFAULT_SET_INTERNAL_TIMECODE NULL
#define DEFAULT_LTC_DAILY_JAM NULL
#define DEFAULT_LTC_AUTO_RESYNC TRUE
#define DEFAULT_LTC_TIMEOUT GST_CLOCK_TIME_NONE
#define DEFAULT_LTC_EXTRA_LATENCY (150 * GST_MSECOND)
#define DEFAULT_RTC_MAX_DRIFT 250000000
#define DEFAULT_RTC_AUTO_RESYNC TRUE
#define DEFAULT_TIMECODE_OFFSET 0

#define DEFAULT_LTC_QUEUE 100

static GstStaticPadTemplate gst_timecodestamper_src_template =
    GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/x-raw, framerate=[1/2147483647, 2147483647/1]; "
        "closedcaption/x-cea-608, framerate=[1/2147483647, 2147483647/1]; "
        "closedcaption/x-cea-708, framerate=[1/2147483647, 2147483647/1]; ")
    );

static GstStaticPadTemplate gst_timecodestamper_sink_template =
    GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/x-raw, framerate=[1/2147483647, 2147483647/1]; "
        "closedcaption/x-cea-608, framerate=[1/2147483647, 2147483647/1]; "
        "closedcaption/x-cea-708, framerate=[1/2147483647, 2147483647/1]; ")
    );

static GstStaticPadTemplate gst_timecodestamper_ltc_template =
GST_STATIC_PAD_TEMPLATE ("ltc_sink",
    GST_PAD_SINK,
    GST_PAD_REQUEST,
    GST_STATIC_CAPS ("audio/x-raw,format=U8,rate=[1,max],channels=1")
    );

static void gst_timecodestamper_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void gst_timecodestamper_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);
static void gst_timecodestamper_dispose (GObject * object);
static gboolean gst_timecodestamper_sink_event (GstBaseTransform * trans,
    GstEvent * event);
static gboolean gst_timecodestamper_src_event (GstBaseTransform * trans,
    GstEvent * event);
static GstFlowReturn gst_timecodestamper_transform_ip (GstBaseTransform *
    vfilter, GstBuffer * buffer);
static gboolean gst_timecodestamper_stop (GstBaseTransform * trans);
static gboolean gst_timecodestamper_start (GstBaseTransform * trans);
static GstPad *gst_timecodestamper_request_new_pad (GstElement * element,
    GstPadTemplate * temp, const gchar * unused, const GstCaps * caps);
static void gst_timecodestamper_release_pad (GstElement * element,
    GstPad * pad);

#if HAVE_LTC
typedef struct
{
  GstClockTime running_time;
  GstVideoTimeCode timecode;
} TimestampedTimecode;

static gboolean gst_timecodestamper_query (GstBaseTransform * trans,
    GstPadDirection direction, GstQuery * query);

static GstFlowReturn gst_timecodestamper_ltcpad_chain (GstPad * pad,
    GstObject * parent, GstBuffer * buffer);
static gboolean gst_timecodestamper_ltcpad_event (GstPad * pad,
    GstObject * parent, GstEvent * event);
static gboolean gst_timecodestamper_ltcpad_query (GstPad * pad,
    GstObject * parent, GstQuery * query);
static gboolean gst_timecodestamper_ltcpad_activatemode (GstPad * pad,
    GstObject * parent, GstPadMode mode, gboolean active);

static gboolean gst_timecodestamper_videopad_activatemode (GstPad * pad,
    GstObject * parent, GstPadMode mode, gboolean active);

static GstIterator *gst_timecodestamper_src_iterate_internal_link (GstPad * pad,
    GstObject * parent);
#endif

static void gst_timecodestamper_update_drop_frame (GstTimeCodeStamper *
    timecodestamper);

G_DEFINE_TYPE (GstTimeCodeStamper, gst_timecodestamper,
    GST_TYPE_BASE_TRANSFORM);
GST_ELEMENT_REGISTER_DEFINE (timecodestamper, "timecodestamper",
    GST_RANK_NONE, GST_TYPE_TIME_CODE_STAMPER);

GType
gst_timecodestamper_source_get_type (void)
{
  static GType type = 0;
  static const GEnumValue values[] = {
    {GST_TIME_CODE_STAMPER_SOURCE_INTERNAL,
          "Use internal timecode counter, starting at zero or value set by property",
        "internal"},
    {GST_TIME_CODE_STAMPER_SOURCE_ZERO,
        "Always use zero", "zero"},
    {GST_TIME_CODE_STAMPER_SOURCE_LAST_KNOWN,
          "Count up from the last known upstream timecode or internal if unknown",
        "last-known"},
    {GST_TIME_CODE_STAMPER_SOURCE_LAST_KNOWN_OR_ZERO,
          "Count up from the last known upstream timecode or zero if unknown",
        "last-known-or-zero"},
    {GST_TIME_CODE_STAMPER_SOURCE_LTC,
        "Linear timecode from an audio device", "ltc"},
    {GST_TIME_CODE_STAMPER_SOURCE_RTC,
        "Timecode from real time clock", "rtc"},
    {0, NULL, NULL},
  };

  if (!type) {
    type = g_enum_register_static ("GstTimeCodeStamperSource", values);
  }
  return type;
}

GType
gst_timecodestamper_set_get_type (void)
{
  static GType type = 0;
  static const GEnumValue values[] = {
    {GST_TIME_CODE_STAMPER_SET_NEVER,
        "Never set timecodes", "never"},
    {GST_TIME_CODE_STAMPER_SET_KEEP,
        "Keep upstream timecodes and only set if no upstream timecode", "keep"},
    {GST_TIME_CODE_STAMPER_SET_ALWAYS,
        "Always set timecode and remove upstream timecode", "always"},
    {0, NULL, NULL},
  };

  if (!type) {
    type = g_enum_register_static ("GstTimeCodeStamperSet", values);
  }
  return type;
}

static void
gst_timecodestamper_class_init (GstTimeCodeStamperClass * klass)
{
  GObjectClass *gobject_class = (GObjectClass *) klass;
  GstElementClass *element_class = (GstElementClass *) klass;
  GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;

  GST_DEBUG_CATEGORY_INIT (timecodestamper_debug, "timecodestamper", 0,
      "timecodestamper");
  gst_element_class_set_static_metadata (element_class, "Timecode stamper",
      "Filter/Video", "Attaches a timecode meta into each video frame",
      "Vivia Nikolaidou <vivia@toolsonair.com>");

  gobject_class->set_property = gst_timecodestamper_set_property;
  gobject_class->get_property = gst_timecodestamper_get_property;
  gobject_class->dispose = gst_timecodestamper_dispose;

  g_object_class_install_property (gobject_class, PROP_SOURCE,
      g_param_spec_enum ("source", "Timecode Source",
          "Choose from what source the timecode should be taken",
          GST_TYPE_TIME_CODE_STAMPER_SOURCE,
          DEFAULT_SOURCE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (gobject_class, PROP_SET,
      g_param_spec_enum ("set", "Timecode Set",
          "Choose whether timecodes should be overridden or not",
          GST_TYPE_TIME_CODE_STAMPER_SET,
          DEFAULT_SET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (gobject_class, PROP_AUTO_RESYNC,
      g_param_spec_boolean ("auto-resync",
          "Auto Resync",
          "If true resync last known timecode from upstream, otherwise only "
          "count up from the last known one",
          DEFAULT_AUTO_RESYNC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (gobject_class, PROP_TIMEOUT,
      g_param_spec_uint64 ("timeout",
          "Timeout",
          "Time out upstream timecode if no new timecode was detected after this time",
          0, G_MAXUINT64, DEFAULT_TIMEOUT,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (gobject_class, PROP_DROP_FRAME,
      g_param_spec_boolean ("drop-frame", "Drop Frame",
          "Use drop-frame timecodes for 29.97 and 59.94 FPS",
          DEFAULT_DROP_FRAME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (gobject_class, PROP_POST_MESSAGES,
      g_param_spec_boolean ("post-messages", "Post element message",
          "Post element message containing the current timecode",
          DEFAULT_POST_MESSAGES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (gobject_class, PROP_SET_INTERNAL_TIMECODE,
      g_param_spec_boxed ("set-internal-timecode",
          "Set Internal Timecode",
          "If set, take this timecode as the internal timecode for the first "
          "frame and increment from it. Only the values itself and daily jam are taken, "
          "flags and frame rate are always determined by timecodestamper "
          "itself. If unset, the internal timecode will start at 0 with the daily jam "
          "being the current real-time clock time",
          GST_TYPE_VIDEO_TIME_CODE,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (gobject_class, PROP_LTC_DAILY_JAM,
      g_param_spec_boxed ("ltc-daily-jam",
          "LTC Daily jam",
          "The daily jam of the LTC timecode",
          G_TYPE_DATE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (gobject_class, PROP_LTC_AUTO_RESYNC,
      g_param_spec_boolean ("ltc-auto-resync",
          "LTC Auto Resync",
          "If true the LTC timecode will be automatically resynced if it drifts, "
          "otherwise it will only be counted up from the last known one",
          DEFAULT_LTC_AUTO_RESYNC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (gobject_class, PROP_LTC_EXTRA_LATENCY,
      g_param_spec_uint64 ("ltc-extra-latency", "LTC Extra Latency",
          "Extra latency to introduce for waiting for LTC timecodes",
          0, G_MAXUINT64, DEFAULT_LTC_EXTRA_LATENCY,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (gobject_class, PROP_LTC_TIMEOUT,
      g_param_spec_uint64 ("ltc-timeout", "LTC Timeout",
          "Time out LTC timecode if no new timecode was detected after this time",
          0, G_MAXUINT64, DEFAULT_LTC_TIMEOUT,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (gobject_class, PROP_RTC_MAX_DRIFT,
      g_param_spec_uint64 ("rtc-max-drift",
          "RTC Maximum Offset",
          "Maximum number of nanoseconds the RTC clock is allowed to drift from "
          "the video before it is resynced",
          0, G_MAXUINT64, DEFAULT_RTC_MAX_DRIFT,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (gobject_class, PROP_RTC_AUTO_RESYNC,
      g_param_spec_boolean ("rtc-auto-resync",
          "RTC Auto Resync",
          "If true the RTC timecode will be automatically resynced if it drifts, "
          "otherwise it will only be counted up from the last known one",
          DEFAULT_RTC_AUTO_RESYNC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (gobject_class, PROP_TIMECODE_OFFSET,
      g_param_spec_int ("timecode-offset",
          "Timecode Offset",
          "Add this offset in frames to internal, LTC or RTC timecode, "
          "useful if there is an offset between the timecode source and video",
          G_MININT, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&gst_timecodestamper_sink_template));
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&gst_timecodestamper_src_template));
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&gst_timecodestamper_ltc_template));

  element_class->request_new_pad =
      GST_DEBUG_FUNCPTR (gst_timecodestamper_request_new_pad);
  element_class->release_pad =
      GST_DEBUG_FUNCPTR (gst_timecodestamper_release_pad);

  trans_class->sink_event = GST_DEBUG_FUNCPTR (gst_timecodestamper_sink_event);
  trans_class->src_event = GST_DEBUG_FUNCPTR (gst_timecodestamper_src_event);
#if HAVE_LTC
  trans_class->query = GST_DEBUG_FUNCPTR (gst_timecodestamper_query);
#endif
  trans_class->stop = GST_DEBUG_FUNCPTR (gst_timecodestamper_stop);
  trans_class->start = GST_DEBUG_FUNCPTR (gst_timecodestamper_start);

  trans_class->transform_ip =
      GST_DEBUG_FUNCPTR (gst_timecodestamper_transform_ip);

  gst_type_mark_as_plugin_api (GST_TYPE_TIME_CODE_STAMPER_SOURCE, 0);
  gst_type_mark_as_plugin_api (GST_TYPE_TIME_CODE_STAMPER_SET, 0);
}

static void
gst_timecodestamper_init (GstTimeCodeStamper * timecodestamper)
{
  timecodestamper->ltcpad = NULL;

  timecodestamper->tc_source = GST_TIME_CODE_STAMPER_SOURCE_INTERNAL;
  timecodestamper->tc_set = GST_TIME_CODE_STAMPER_SET_KEEP;
  timecodestamper->tc_auto_resync = DEFAULT_AUTO_RESYNC;
  timecodestamper->tc_timeout = DEFAULT_TIMEOUT;
  timecodestamper->drop_frame = DEFAULT_DROP_FRAME;
  timecodestamper->post_messages = DEFAULT_POST_MESSAGES;
  timecodestamper->set_internal_tc = NULL;
  timecodestamper->ltc_daily_jam = DEFAULT_LTC_DAILY_JAM;
  timecodestamper->ltc_auto_resync = DEFAULT_LTC_AUTO_RESYNC;
  timecodestamper->ltc_extra_latency = DEFAULT_LTC_EXTRA_LATENCY;
  timecodestamper->ltc_timeout = DEFAULT_LTC_TIMEOUT;
  timecodestamper->rtc_max_drift = DEFAULT_RTC_MAX_DRIFT;
  timecodestamper->rtc_auto_resync = DEFAULT_RTC_AUTO_RESYNC;
  timecodestamper->timecode_offset = 0;

  timecodestamper->internal_tc = NULL;
  timecodestamper->last_tc = NULL;
  timecodestamper->last_tc_running_time = GST_CLOCK_TIME_NONE;
  timecodestamper->rtc_tc = NULL;

  timecodestamper->seeked_frames = -1;

#if HAVE_LTC
  g_mutex_init (&timecodestamper->mutex);
  g_cond_init (&timecodestamper->ltc_cond_video);
  g_cond_init (&timecodestamper->ltc_cond_audio);

  gst_segment_init (&timecodestamper->ltc_segment, GST_FORMAT_UNDEFINED);
  timecodestamper->ltc_first_running_time = GST_CLOCK_TIME_NONE;
  timecodestamper->ltc_current_running_time = GST_CLOCK_TIME_NONE;

  g_queue_init (&timecodestamper->ltc_current_tcs);
  timecodestamper->ltc_internal_tc = NULL;
  timecodestamper->ltc_internal_running_time = GST_CLOCK_TIME_NONE;
  timecodestamper->ltc_dec = NULL;
  timecodestamper->ltc_total = 0;

  timecodestamper->ltc_eos = TRUE;
  timecodestamper->ltc_flushing = TRUE;

  timecodestamper->audio_live = FALSE;
  timecodestamper->audio_latency = GST_CLOCK_TIME_NONE;
  timecodestamper->video_live = FALSE;
  timecodestamper->video_latency = GST_CLOCK_TIME_NONE;
  timecodestamper->latency = GST_CLOCK_TIME_NONE;

  timecodestamper->video_activatemode_default =
      GST_PAD_ACTIVATEMODEFUNC (GST_BASE_TRANSFORM_SINK_PAD (timecodestamper));
  GST_PAD_ACTIVATEMODEFUNC (GST_BASE_TRANSFORM_SINK_PAD (timecodestamper)) =
      gst_timecodestamper_videopad_activatemode;
  gst_pad_set_iterate_internal_links_function (GST_BASE_TRANSFORM_SRC_PAD
      (timecodestamper), gst_timecodestamper_src_iterate_internal_link);
#endif
}

static void
gst_timecodestamper_dispose (GObject * object)
{
  GstTimeCodeStamper *timecodestamper = GST_TIME_CODE_STAMPER (object);

  if (timecodestamper->ltc_daily_jam) {
    g_date_time_unref (timecodestamper->ltc_daily_jam);
    timecodestamper->ltc_daily_jam = NULL;
  }

  if (timecodestamper->internal_tc != NULL) {
    gst_video_time_code_free (timecodestamper->internal_tc);
    timecodestamper->internal_tc = NULL;
  }

  if (timecodestamper->set_internal_tc != NULL) {
    gst_video_time_code_free (timecodestamper->set_internal_tc);
    timecodestamper->set_internal_tc = NULL;
  }

  if (timecodestamper->last_tc != NULL) {
    gst_video_time_code_free (timecodestamper->last_tc);
    timecodestamper->last_tc = NULL;
  }
  timecodestamper->last_tc_running_time = GST_CLOCK_TIME_NONE;

  if (timecodestamper->rtc_tc != NULL) {
    gst_video_time_code_free (timecodestamper->rtc_tc);
    timecodestamper->rtc_tc = NULL;
  }
#if HAVE_LTC
  g_cond_clear (&timecodestamper->ltc_cond_video);
  g_cond_clear (&timecodestamper->ltc_cond_audio);
  g_mutex_clear (&timecodestamper->mutex);
  {
    TimestampedTimecode *tc;
    while ((tc = g_queue_pop_tail (&timecodestamper->ltc_current_tcs))) {
      gst_video_time_code_clear (&tc->timecode);
      g_free (tc);
    }
  }
  if (timecodestamper->ltc_internal_tc != NULL) {
    gst_video_time_code_free (timecodestamper->ltc_internal_tc);
    timecodestamper->ltc_internal_tc = NULL;
  }
  timecodestamper->ltc_internal_running_time = GST_CLOCK_TIME_NONE;

  if (timecodestamper->ltc_dec) {
    ltc_decoder_free (timecodestamper->ltc_dec);
    timecodestamper->ltc_dec = NULL;
  }

  if (timecodestamper->stream_align) {
    gst_audio_stream_align_free (timecodestamper->stream_align);
    timecodestamper->stream_align = NULL;
  }
#endif

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

static void
gst_timecodestamper_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstTimeCodeStamper *timecodestamper = GST_TIME_CODE_STAMPER (object);

  GST_OBJECT_LOCK (timecodestamper);
  switch (prop_id) {
    case PROP_SOURCE:
      timecodestamper->tc_source = (GstTimeCodeStamperSource)
          g_value_get_enum (value);
      break;
    case PROP_SET:
      timecodestamper->tc_set = (GstTimeCodeStamperSet)
          g_value_get_enum (value);
      break;
    case PROP_AUTO_RESYNC:
      timecodestamper->tc_auto_resync = g_value_get_boolean (value);
      break;
    case PROP_TIMEOUT:
      timecodestamper->tc_timeout = g_value_get_uint64 (value);
      break;
    case PROP_DROP_FRAME:
      timecodestamper->drop_frame = g_value_get_boolean (value);
      gst_timecodestamper_update_drop_frame (timecodestamper);
      break;
    case PROP_LTC_DAILY_JAM:
      if (timecodestamper->ltc_daily_jam)
        g_date_time_unref (timecodestamper->ltc_daily_jam);
      timecodestamper->ltc_daily_jam = g_value_dup_boxed (value);

#if HAVE_LTC
      {
        GList *l;

        for (l = timecodestamper->ltc_current_tcs.head; l; l = l->next) {
          TimestampedTimecode *tc = l->data;

          if (tc->timecode.config.latest_daily_jam) {
            g_date_time_unref (tc->timecode.config.latest_daily_jam);
          }
          tc->timecode.config.latest_daily_jam =
              g_date_time_ref (timecodestamper->ltc_daily_jam);
        }
      }

      if (timecodestamper->ltc_internal_tc) {
        if (timecodestamper->ltc_internal_tc->config.latest_daily_jam) {
          g_date_time_unref (timecodestamper->ltc_internal_tc->
              config.latest_daily_jam);
        }
        timecodestamper->ltc_internal_tc->config.latest_daily_jam =
            g_date_time_ref (timecodestamper->ltc_daily_jam);
      }
#endif
      break;
    case PROP_POST_MESSAGES:
      timecodestamper->post_messages = g_value_get_boolean (value);
      break;
    case PROP_SET_INTERNAL_TIMECODE:{
      if (timecodestamper->set_internal_tc)
        gst_video_time_code_free (timecodestamper->set_internal_tc);
      timecodestamper->set_internal_tc = g_value_dup_boxed (value);

      /* Reset the internal timecode on the next opportunity if a new
       * timecode was set here. If none was set we just continue counting
       * from the previous one */
      if (timecodestamper->set_internal_tc && timecodestamper->internal_tc) {
        gst_video_time_code_free (timecodestamper->internal_tc);
        timecodestamper->internal_tc = NULL;
      }
      break;
    }
    case PROP_LTC_AUTO_RESYNC:
      timecodestamper->ltc_auto_resync = g_value_get_boolean (value);
      break;
    case PROP_LTC_TIMEOUT:
      timecodestamper->ltc_timeout = g_value_get_uint64 (value);
      break;
    case PROP_LTC_EXTRA_LATENCY:
      timecodestamper->ltc_extra_latency = g_value_get_uint64 (value);
      break;
    case PROP_RTC_MAX_DRIFT:
      timecodestamper->rtc_max_drift = g_value_get_uint64 (value);
      break;
    case PROP_RTC_AUTO_RESYNC:
      timecodestamper->rtc_auto_resync = g_value_get_boolean (value);
      break;
    case PROP_TIMECODE_OFFSET:
      timecodestamper->timecode_offset = g_value_get_int (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }

  GST_OBJECT_UNLOCK (timecodestamper);
}

static void
gst_timecodestamper_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstTimeCodeStamper *timecodestamper = GST_TIME_CODE_STAMPER (object);

  GST_OBJECT_LOCK (timecodestamper);
  switch (prop_id) {
    case PROP_SOURCE:
      g_value_set_enum (value, timecodestamper->tc_source);
      break;
    case PROP_SET:
      g_value_set_enum (value, timecodestamper->tc_set);
      break;
    case PROP_AUTO_RESYNC:
      g_value_set_boolean (value, timecodestamper->tc_auto_resync);
      break;
    case PROP_TIMEOUT:
      g_value_set_uint64 (value, timecodestamper->tc_timeout);
      break;
    case PROP_DROP_FRAME:
      g_value_set_boolean (value, timecodestamper->drop_frame);
      break;
    case PROP_LTC_DAILY_JAM:
      g_value_set_boxed (value, timecodestamper->ltc_daily_jam);
      break;
    case PROP_POST_MESSAGES:
      g_value_set_boolean (value, timecodestamper->post_messages);
      break;
    case PROP_SET_INTERNAL_TIMECODE:
      g_value_set_boxed (value, timecodestamper->set_internal_tc);
      break;
    case PROP_LTC_AUTO_RESYNC:
      g_value_set_boolean (value, timecodestamper->ltc_auto_resync);
      break;
    case PROP_LTC_TIMEOUT:
      g_value_set_uint64 (value, timecodestamper->ltc_timeout);
      break;
    case PROP_LTC_EXTRA_LATENCY:
      g_value_set_uint64 (value, timecodestamper->ltc_extra_latency);
      break;
    case PROP_RTC_MAX_DRIFT:
      g_value_set_uint64 (value, timecodestamper->rtc_max_drift);
      break;
    case PROP_RTC_AUTO_RESYNC:
      g_value_set_boolean (value, timecodestamper->rtc_auto_resync);
      break;
    case PROP_TIMECODE_OFFSET:
      g_value_set_int (value, timecodestamper->timecode_offset);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
  GST_OBJECT_UNLOCK (timecodestamper);
}

static gboolean
gst_timecodestamper_stop (GstBaseTransform * trans)
{
  GstTimeCodeStamper *timecodestamper = GST_TIME_CODE_STAMPER (trans);

#if HAVE_LTC
  g_mutex_lock (&timecodestamper->mutex);
  timecodestamper->video_flushing = TRUE;
  timecodestamper->video_current_running_time = GST_CLOCK_TIME_NONE;
  if (timecodestamper->video_clock_id)
    gst_clock_id_unschedule (timecodestamper->video_clock_id);
  timecodestamper->ltc_flushing = TRUE;
  g_cond_signal (&timecodestamper->ltc_cond_video);
  g_cond_signal (&timecodestamper->ltc_cond_audio);
  g_mutex_unlock (&timecodestamper->mutex);
#endif

  timecodestamper->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
  timecodestamper->fps_n = 0;
  timecodestamper->fps_d = 1;

  if (timecodestamper->internal_tc != NULL) {
    gst_video_time_code_free (timecodestamper->internal_tc);
    timecodestamper->internal_tc = NULL;
  }

  if (timecodestamper->rtc_tc != NULL) {
    gst_video_time_code_free (timecodestamper->rtc_tc);
    timecodestamper->rtc_tc = NULL;
  }

  if (timecodestamper->last_tc != NULL) {
    gst_video_time_code_free (timecodestamper->last_tc);
    timecodestamper->last_tc = NULL;
  }
  timecodestamper->last_tc_running_time = GST_CLOCK_TIME_NONE;
#if HAVE_LTC
  g_mutex_lock (&timecodestamper->mutex);
  gst_audio_info_init (&timecodestamper->ainfo);
  gst_segment_init (&timecodestamper->ltc_segment, GST_FORMAT_UNDEFINED);

  timecodestamper->ltc_first_running_time = GST_CLOCK_TIME_NONE;
  timecodestamper->ltc_current_running_time = GST_CLOCK_TIME_NONE;

  if (timecodestamper->ltc_internal_tc != NULL) {
    gst_video_time_code_free (timecodestamper->ltc_internal_tc);
    timecodestamper->ltc_internal_tc = NULL;
  }
  timecodestamper->ltc_internal_running_time = GST_CLOCK_TIME_NONE;

  {
    TimestampedTimecode *tc;
    while ((tc = g_queue_pop_tail (&timecodestamper->ltc_current_tcs))) {
      gst_video_time_code_clear (&tc->timecode);
      g_free (tc);
    }
  }

  if (timecodestamper->ltc_dec) {
    ltc_decoder_free (timecodestamper->ltc_dec);
    timecodestamper->ltc_dec = NULL;
  }

  if (timecodestamper->stream_align) {
    gst_audio_stream_align_free (timecodestamper->stream_align);
    timecodestamper->stream_align = NULL;
  }

  timecodestamper->ltc_total = 0;
  g_mutex_unlock (&timecodestamper->mutex);
#endif

  return TRUE;
}

static gboolean
gst_timecodestamper_start (GstBaseTransform * trans)
{
  GstTimeCodeStamper *timecodestamper = GST_TIME_CODE_STAMPER (trans);

#if HAVE_LTC
  g_mutex_lock (&timecodestamper->mutex);
  timecodestamper->video_flushing = FALSE;
  timecodestamper->video_eos = FALSE;
  g_mutex_unlock (&timecodestamper->mutex);
#endif

  timecodestamper->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
  timecodestamper->fps_n = 0;
  timecodestamper->fps_d = 1;

  return TRUE;
}

/* Must be called with object lock */
static void
gst_timecodestamper_update_drop_frame (GstTimeCodeStamper * timecodestamper)
{
  if (timecodestamper->drop_frame && timecodestamper->fps_d == 1001 &&
      (timecodestamper->fps_n == 30000 || timecodestamper->fps_n == 60000)) {
    if (timecodestamper->internal_tc)
      timecodestamper->internal_tc->config.flags |=
          GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME;
    if (timecodestamper->rtc_tc)
      timecodestamper->rtc_tc->config.flags |=
          GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME;
#if HAVE_LTC
    {
      GList *l;

      for (l = timecodestamper->ltc_current_tcs.head; l; l = l->next) {
        TimestampedTimecode *tc = l->data;

        tc->timecode.config.flags |= GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME;
      }
    }
    if (timecodestamper->ltc_internal_tc)
      timecodestamper->ltc_internal_tc->config.flags |=
          GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME;
#endif
  } else {
    if (timecodestamper->internal_tc)
      timecodestamper->internal_tc->config.flags &=
          ~GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME;
    if (timecodestamper->rtc_tc)
      timecodestamper->rtc_tc->config.flags &=
          ~GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME;
#if HAVE_LTC
    {
      GList *l;

      for (l = timecodestamper->ltc_current_tcs.head; l; l = l->next) {
        TimestampedTimecode *tc = l->data;

        tc->timecode.config.flags &= ~GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME;
      }
    }
    if (timecodestamper->ltc_internal_tc)
      timecodestamper->ltc_internal_tc->config.flags &=
          ~GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME;
#endif
  }
}

static void
gst_timecodestamper_update_timecode_framerate (GstTimeCodeStamper *
    timecodestamper, gint fps_n, gint fps_d, GstVideoTimeCode * timecode,
    gboolean is_ltc)
{
  guint64 nframes;
  GstClockTime time;
  GDateTime *jam = NULL;
  GstVideoTimeCodeFlags tc_flags = 0;

  if (!timecode)
    return;

  if (timecodestamper->interlace_mode != GST_VIDEO_INTERLACE_MODE_PROGRESSIVE)
    tc_flags |= GST_VIDEO_TIME_CODE_FLAGS_INTERLACED;

  if (timecodestamper->drop_frame && timecodestamper->fps_d == 1001 &&
      (timecodestamper->fps_n == 30000 || timecodestamper->fps_n == 60000))
    tc_flags |= GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME;

  /* If this is an LTC timecode and we have no framerate yet in there then
   * just do nothing. We're going to set the framerate at a later time */
  if (timecode->config.fps_d != 0 || !is_ltc) {
    nframes = gst_video_time_code_frames_since_daily_jam (timecode);
    time =
        gst_util_uint64_scale (nframes,
        GST_SECOND * timecodestamper->fps_d, timecodestamper->fps_n);
    jam =
        timecode->config.latest_daily_jam ? g_date_time_ref (timecode->config.
        latest_daily_jam) : NULL;
    gst_video_time_code_clear (timecode);
    gst_video_time_code_init (timecode, timecodestamper->fps_n,
        timecodestamper->fps_d, jam, tc_flags, 0, 0, 0, 0, 0);
    if (jam)
      g_date_time_unref (jam);

    nframes = gst_util_uint64_scale (time, fps_n, GST_SECOND * fps_d);
    gst_video_time_code_add_frames (timecode, nframes);
  }
}

/* Must be called with object lock */
static gboolean
gst_timecodestamper_update_framerate (GstTimeCodeStamper * timecodestamper,
    gint fps_n, gint fps_d)
{
  /* Nothing changed */
  if (fps_n == timecodestamper->fps_n && fps_d == timecodestamper->fps_d)
    return FALSE;

  gst_timecodestamper_update_timecode_framerate (timecodestamper, fps_n, fps_d,
      timecodestamper->internal_tc, FALSE);
  gst_timecodestamper_update_timecode_framerate (timecodestamper, fps_n, fps_d,
      timecodestamper->last_tc, FALSE);
  gst_timecodestamper_update_timecode_framerate (timecodestamper, fps_n, fps_d,
      timecodestamper->rtc_tc, FALSE);

#if HAVE_LTC
  {
    GList *l;

    for (l = timecodestamper->ltc_current_tcs.head; l; l = l->next) {
      TimestampedTimecode *tc = l->data;

      gst_timecodestamper_update_timecode_framerate (timecodestamper, fps_n,
          fps_d, &tc->timecode, TRUE);
    }
  }
  gst_timecodestamper_update_timecode_framerate (timecodestamper, fps_n, fps_d,
      timecodestamper->ltc_internal_tc, FALSE);
#endif

  return TRUE;
}

static gboolean
gst_timecodestamper_sink_event (GstBaseTransform * trans, GstEvent * event)
{
  GstTimeCodeStamper *timecodestamper = GST_TIME_CODE_STAMPER (trans);
  gboolean ret = FALSE;

  GST_DEBUG_OBJECT (trans, "received event %" GST_PTR_FORMAT, event);
  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_SEGMENT:
    {
      GstSegment segment;

      gst_event_copy_segment (event, &segment);
      if (segment.format != GST_FORMAT_TIME) {
        GST_ERROR_OBJECT (timecodestamper, "Invalid segment format");
        gst_event_unref (event);
        return FALSE;
      }

      GST_OBJECT_LOCK (timecodestamper);
      if (timecodestamper->tc_source == GST_TIME_CODE_STAMPER_SOURCE_INTERNAL
          && GST_EVENT_SEQNUM (event) == timecodestamper->prev_seek_seqnum) {
        timecodestamper->reset_internal_tc_from_seek = TRUE;
        timecodestamper->prev_seek_seqnum = GST_SEQNUM_INVALID;
      }
      GST_OBJECT_UNLOCK (timecodestamper);

      break;
    }
    case GST_EVENT_CAPS:
    {
      GstCaps *caps;
      gboolean latency_changed;
      const gchar *interlace_mode;
      GstStructure *s;
      gint fps_n, fps_d;

      GST_OBJECT_LOCK (timecodestamper);
      gst_event_parse_caps (event, &caps);

      s = gst_caps_get_structure (caps, 0);

      if (!gst_structure_get_fraction (s, "framerate", &fps_n, &fps_d)) {
        GST_ERROR_OBJECT (timecodestamper, "Expected framerate in caps");
        GST_OBJECT_UNLOCK (timecodestamper);
        gst_event_unref (event);
        return FALSE;
      }

      if (fps_n == 0) {
        GST_ERROR_OBJECT (timecodestamper,
            "Non-constant frame rate found. Refusing to create a timecode");
        GST_OBJECT_UNLOCK (timecodestamper);
        gst_event_unref (event);
        return FALSE;
      }

      if ((interlace_mode = gst_structure_get_string (s, "interlace-mode"))) {
        timecodestamper->interlace_mode =
            gst_video_interlace_mode_from_string (interlace_mode);
      }

      latency_changed =
          gst_timecodestamper_update_framerate (timecodestamper, fps_n, fps_d);

      timecodestamper->fps_n = fps_n;
      timecodestamper->fps_d = fps_d;

      GST_OBJECT_UNLOCK (timecodestamper);

      if (latency_changed)
        gst_element_post_message (GST_ELEMENT_CAST (timecodestamper),
            gst_message_new_latency (GST_OBJECT_CAST (timecodestamper)));
      break;
    }
#if HAVE_LTC
    case GST_EVENT_FLUSH_START:
      g_mutex_lock (&timecodestamper->mutex);
      timecodestamper->video_flushing = TRUE;
      timecodestamper->video_current_running_time = GST_CLOCK_TIME_NONE;
      if (timecodestamper->video_clock_id)
        gst_clock_id_unschedule (timecodestamper->video_clock_id);
      g_cond_signal (&timecodestamper->ltc_cond_video);
      g_mutex_unlock (&timecodestamper->mutex);
      break;
    case GST_EVENT_FLUSH_STOP:
      g_mutex_lock (&timecodestamper->mutex);
      timecodestamper->video_flushing = FALSE;
      timecodestamper->video_eos = FALSE;
      g_mutex_unlock (&timecodestamper->mutex);
      break;
    case GST_EVENT_EOS:
      g_mutex_lock (&timecodestamper->mutex);
      timecodestamper->video_eos = TRUE;
      g_cond_signal (&timecodestamper->ltc_cond_audio);
      g_mutex_unlock (&timecodestamper->mutex);
      break;
#endif
    default:
      break;
  }
  ret =
      GST_BASE_TRANSFORM_CLASS (gst_timecodestamper_parent_class)->sink_event
      (trans, event);
  return ret;
}

static gboolean
gst_timecodestamper_src_event (GstBaseTransform * trans, GstEvent * event)
{
  GstTimeCodeStamper *timecodestamper = GST_TIME_CODE_STAMPER (trans);

  GST_DEBUG_OBJECT (trans, "received event %" GST_PTR_FORMAT, event);
  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_SEEK:
    {
      gdouble rate;
      GstSeekType start_type;
      gint64 start;
      GstFormat format;

      gst_event_parse_seek (event, &rate, &format, NULL, &start_type, &start,
          NULL, NULL);

      if (rate < 0) {
        GST_ERROR_OBJECT (timecodestamper, "Reverse playback is not supported");
        return FALSE;
      }

      if (format != GST_FORMAT_TIME) {
        GST_ERROR_OBJECT (timecodestamper,
            "Seeking is only supported in TIME format");
        return FALSE;
      }

      GST_OBJECT_LOCK (timecodestamper);
      if (timecodestamper->fps_d && timecodestamper->fps_n) {
        timecodestamper->prev_seek_seqnum = GST_EVENT_SEQNUM (event);
        timecodestamper->seeked_frames = gst_util_uint64_scale (start,
            timecodestamper->fps_n, timecodestamper->fps_d * GST_SECOND);
      }
      GST_OBJECT_UNLOCK (timecodestamper);
      break;
    }
    default:
      break;
  }

  return
      GST_BASE_TRANSFORM_CLASS (gst_timecodestamper_parent_class)->src_event
      (trans, event);
}

#if HAVE_LTC
static gboolean
gst_timecodestamper_query (GstBaseTransform * trans,
    GstPadDirection direction, GstQuery * query)
{
  GstTimeCodeStamper *timecodestamper = GST_TIME_CODE_STAMPER (trans);

  if (direction == GST_PAD_SINK)
    return
        GST_BASE_TRANSFORM_CLASS (gst_timecodestamper_parent_class)->query
        (trans, direction, query);

  switch (GST_QUERY_TYPE (query)) {
    case GST_QUERY_LATENCY:{
      gboolean res;
      gboolean live;
      GstClockTime min_latency, max_latency;
      GstClockTime latency;

      res =
          gst_pad_query_default (GST_BASE_TRANSFORM_SRC_PAD (trans),
          GST_OBJECT_CAST (trans), query);
      g_mutex_lock (&timecodestamper->mutex);
      if (res && timecodestamper->fps_n && timecodestamper->fps_d) {
        gst_query_parse_latency (query, &live, &min_latency, &max_latency);
        if (live && timecodestamper->ltcpad) {
          /* Introduce additional LTC for waiting for LTC timecodes. The
           * LTC library introduces some as well as the encoding of the LTC
           * signal. */
          latency = timecodestamper->ltc_extra_latency;
          min_latency += latency;
          if (max_latency != GST_CLOCK_TIME_NONE)
            max_latency += latency;
          timecodestamper->latency = min_latency;
          GST_DEBUG_OBJECT (timecodestamper,
              "Reporting latency min %" GST_TIME_FORMAT " max %" GST_TIME_FORMAT
              " ours %" GST_TIME_FORMAT, GST_TIME_ARGS (min_latency),
              GST_TIME_ARGS (max_latency), GST_TIME_ARGS (latency));
          gst_query_set_latency (query, live, min_latency, max_latency);
        } else {
          timecodestamper->latency = 0;
        }
      } else if (res) {
        GST_ERROR_OBJECT (timecodestamper,
            "Need a known, non-variable framerate to answer LATENCY query");
        res = FALSE;
        timecodestamper->latency = GST_CLOCK_TIME_NONE;
      }
      g_mutex_unlock (&timecodestamper->mutex);

      return res;
    }
    default:
      return
          GST_BASE_TRANSFORM_CLASS (gst_timecodestamper_parent_class)->query
          (trans, direction, query);
  }
}
#endif

static gboolean
remove_timecode_meta (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
{
  if (meta && *meta && (*meta)->info->api == GST_VIDEO_TIME_CODE_META_API_TYPE) {
    *meta = NULL;
  }

  return TRUE;
}

#if HAVE_LTC
static void
gst_timecodestamper_update_latency (GstTimeCodeStamper * timecodestamper,
    GstPad * pad, gboolean * live, GstClockTime * latency)
{
  GstQuery *query;

  query = gst_query_new_latency ();
  if (!gst_pad_peer_query (pad, query)) {
    GST_WARNING_OBJECT (pad, "Failed to query latency");
    gst_pad_mark_reconfigure (pad);
    gst_query_unref (query);
    return;
  }

  g_mutex_lock (&timecodestamper->mutex);
  gst_query_parse_latency (query, live, latency, NULL);
  /* If we're not live, consider a latency of 0 */
  if (!*live)
    *latency = 0;
  GST_DEBUG_OBJECT (pad,
      "Queried latency: live %d, min latency %" GST_TIME_FORMAT, *live,
      GST_TIME_ARGS (*latency));
  g_mutex_unlock (&timecodestamper->mutex);
  gst_query_unref (query);
}
#endif

static GstFlowReturn
gst_timecodestamper_transform_ip (GstBaseTransform * vfilter,
    GstBuffer * buffer)
{
  GstTimeCodeStamper *timecodestamper = GST_TIME_CODE_STAMPER (vfilter);
  GstClockTime running_time, base_time, clock_time;
  GstClock *clock;
  GstClockTime clock_time_now;
  GDateTime *dt_now, *dt_frame;
  GstVideoTimeCode *tc = NULL;
  gboolean free_tc = FALSE;
  GstVideoTimeCodeMeta *tc_meta;
  GstFlowReturn flow_ret = GST_FLOW_OK;
  GstVideoTimeCodeFlags tc_flags = 0;

  if (timecodestamper->fps_n == 0 || timecodestamper->fps_d == 0
      || !GST_BUFFER_PTS_IS_VALID (buffer)) {
    gst_buffer_unref (buffer);
    return GST_FLOW_NOT_NEGOTIATED;
  }
#if HAVE_LTC
  if (timecodestamper->video_latency == -1
      || gst_pad_check_reconfigure (GST_BASE_TRANSFORM_SINK_PAD (vfilter))) {
    gst_timecodestamper_update_latency (timecodestamper,
        GST_BASE_TRANSFORM_SINK_PAD (vfilter), &timecodestamper->video_live,
        &timecodestamper->video_latency);
  }
#endif

  /* Collect all the current times */
  base_time = gst_element_get_base_time (GST_ELEMENT (timecodestamper));
  clock = gst_element_get_clock (GST_ELEMENT (timecodestamper));
  if (clock) {
    clock_time_now = gst_clock_get_time (clock);
    gst_object_unref (clock);
  } else {
    clock_time_now = GST_CLOCK_TIME_NONE;
  }

  dt_now = g_date_time_new_now_local ();

  running_time =
      gst_segment_to_running_time (&vfilter->segment, GST_FORMAT_TIME,
      GST_BUFFER_PTS (buffer));

  if (clock_time_now != GST_CLOCK_TIME_NONE) {
    gdouble seconds_diff;

    clock_time = running_time + base_time;
    if (clock_time_now > clock_time) {
      seconds_diff = (clock_time_now - clock_time) / -1000000000.0;
    } else {
      seconds_diff = (clock_time - clock_time_now) / 1000000000.0;
    }
    dt_frame = g_date_time_add_seconds (dt_now, seconds_diff);
  } else {
    /* If we have no clock we can't really know the time of the frame */
    dt_frame = g_date_time_ref (dt_now);
  }

  GST_DEBUG_OBJECT (timecodestamper,
      "Handling video frame with running time %" GST_TIME_FORMAT,
      GST_TIME_ARGS (running_time));

  tc_meta = gst_buffer_get_video_time_code_meta (buffer);

  /* Update all our internal timecodes as needed */
  GST_OBJECT_LOCK (timecodestamper);

  if (timecodestamper->interlace_mode != GST_VIDEO_INTERLACE_MODE_PROGRESSIVE)
    tc_flags |= GST_VIDEO_TIME_CODE_FLAGS_INTERLACED;

  if (timecodestamper->drop_frame && timecodestamper->fps_d == 1001 &&
      (timecodestamper->fps_n == 30000 || timecodestamper->fps_n == 60000))
    tc_flags |= GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME;

  /* If we don't have an internal timecode yet then either a new one was just
   * set via the property or we just started. Initialize it here, otherwise
   * increment it by one */
  if (!timecodestamper->internal_tc
      || timecodestamper->reset_internal_tc_from_seek) {
    gchar *tc_str;

    if (timecodestamper->internal_tc)
      gst_video_time_code_free (timecodestamper->internal_tc);

    timecodestamper->reset_internal_tc_from_seek = FALSE;
    if (timecodestamper->set_internal_tc) {
      timecodestamper->internal_tc =
          gst_video_time_code_new (timecodestamper->fps_n,
          timecodestamper->fps_d,
          timecodestamper->set_internal_tc->config.latest_daily_jam, tc_flags,
          timecodestamper->set_internal_tc->hours,
          timecodestamper->set_internal_tc->minutes,
          timecodestamper->set_internal_tc->seconds,
          timecodestamper->set_internal_tc->frames,
          timecodestamper->set_internal_tc->field_count);
    } else {
      timecodestamper->internal_tc =
          gst_video_time_code_new (timecodestamper->fps_n,
          timecodestamper->fps_d, dt_frame, tc_flags, 0, 0, 0, 0, 0);
      if (timecodestamper->seeked_frames > 0) {
        GST_DEBUG_OBJECT (timecodestamper,
            "Adding %" G_GINT64_FORMAT " frames that were seeked",
            timecodestamper->seeked_frames);
        gst_video_time_code_add_frames (timecodestamper->internal_tc,
            timecodestamper->seeked_frames);
        timecodestamper->seeked_frames = -1;
      }
    }

    tc_str = gst_video_time_code_to_string (timecodestamper->internal_tc);
    GST_DEBUG_OBJECT (timecodestamper, "Initialized internal timecode to %s",
        tc_str);
    g_free (tc_str);
  } else {
    gchar *tc_str;

    gst_video_time_code_increment_frame (timecodestamper->internal_tc);
    tc_str = gst_video_time_code_to_string (timecodestamper->internal_tc);
    GST_DEBUG_OBJECT (timecodestamper, "Incremented internal timecode to %s",
        tc_str);
    g_free (tc_str);
  }

  /* If we have a new timecode on the incoming frame, update our last known
   * timecode or otherwise increment it by one */
  if (tc_meta && (!timecodestamper->last_tc || timecodestamper->tc_auto_resync)) {
    gchar *tc_str;

    if (timecodestamper->last_tc)
      gst_video_time_code_free (timecodestamper->last_tc);
    timecodestamper->last_tc = gst_video_time_code_copy (&tc_meta->tc);
    timecodestamper->last_tc_running_time = running_time;

    tc_str = gst_video_time_code_to_string (timecodestamper->last_tc);
    GST_DEBUG_OBJECT (timecodestamper, "Updated upstream timecode to %s",
        tc_str);
    g_free (tc_str);
  } else {
    if (timecodestamper->last_tc) {
      if (timecodestamper->tc_auto_resync
          && timecodestamper->tc_timeout != GST_CLOCK_TIME_NONE
          && (running_time + timecodestamper->tc_timeout <
              timecodestamper->last_tc_running_time
              || running_time >=
              timecodestamper->last_tc_running_time +
              timecodestamper->tc_timeout)) {
        if (timecodestamper->last_tc)
          gst_video_time_code_free (timecodestamper->last_tc);
        timecodestamper->last_tc = NULL;
        timecodestamper->last_tc_running_time = GST_CLOCK_TIME_NONE;
        GST_DEBUG_OBJECT (timecodestamper, "Upstream timecode timed out");
      } else {
        gchar *tc_str;

        gst_video_time_code_increment_frame (timecodestamper->last_tc);

        tc_str = gst_video_time_code_to_string (timecodestamper->last_tc);
        GST_DEBUG_OBJECT (timecodestamper,
            "Incremented upstream timecode to %s", tc_str);
        g_free (tc_str);
      }
    } else {
      GST_DEBUG_OBJECT (timecodestamper, "Never saw an upstream timecode");
    }
  }

  /* Update RTC-based timecode */
  {
    GstVideoTimeCode rtc_timecode_now;
    gchar *tc_str, *dt_str;

    /* Create timecode for the current frame time */
    memset (&rtc_timecode_now, 0, sizeof (rtc_timecode_now));
    gst_video_time_code_init_from_date_time_full (&rtc_timecode_now,
        timecodestamper->fps_n, timecodestamper->fps_d, dt_frame, tc_flags, 0);

    tc_str = gst_video_time_code_to_string (&rtc_timecode_now);
    dt_str = g_date_time_format (dt_frame, "%F %R %z");
    GST_DEBUG_OBJECT (timecodestamper,
        "Created RTC timecode %s for %s (%06u us)", tc_str, dt_str,
        g_date_time_get_microsecond (dt_frame));
    g_free (dt_str);
    g_free (tc_str);

    /* If we don't have an RTC timecode yet, directly initialize with this one */
    if (!timecodestamper->rtc_tc) {
      timecodestamper->rtc_tc = gst_video_time_code_copy (&rtc_timecode_now);
      tc_str = gst_video_time_code_to_string (timecodestamper->rtc_tc);
      GST_DEBUG_OBJECT (timecodestamper, "Initialized RTC timecode to %s",
          tc_str);
      g_free (tc_str);
    } else {
      GstClockTime rtc_now_time, rtc_tc_time;
      GstClockTime rtc_diff;

      /* Increment the old RTC timecode to this frame */
      gst_video_time_code_increment_frame (timecodestamper->rtc_tc);

      /* Otherwise check if we drifted too much and need to resync */
      rtc_tc_time =
          gst_video_time_code_nsec_since_daily_jam (timecodestamper->rtc_tc);
      rtc_now_time =
          gst_video_time_code_nsec_since_daily_jam (&rtc_timecode_now);
      if (rtc_tc_time > rtc_now_time)
        rtc_diff = rtc_tc_time - rtc_now_time;
      else
        rtc_diff = rtc_now_time - rtc_tc_time;

      if (timecodestamper->rtc_auto_resync
          && timecodestamper->rtc_max_drift != GST_CLOCK_TIME_NONE
          && rtc_diff > timecodestamper->rtc_max_drift) {
        gst_video_time_code_free (timecodestamper->rtc_tc);
        timecodestamper->rtc_tc = gst_video_time_code_copy (&rtc_timecode_now);
        tc_str = gst_video_time_code_to_string (timecodestamper->rtc_tc);
        GST_DEBUG_OBJECT (timecodestamper,
            "Updated RTC timecode to %s (%s%" GST_TIME_FORMAT " drift)", tc_str,
            (rtc_tc_time > rtc_now_time ? "-" : "+"), GST_TIME_ARGS (rtc_diff));
        g_free (tc_str);
      } else {
        /* Else nothing to do here, we use the current one */
        tc_str = gst_video_time_code_to_string (timecodestamper->rtc_tc);
        GST_DEBUG_OBJECT (timecodestamper,
            "Incremented RTC timecode to %s (%s%" GST_TIME_FORMAT " drift)",
            tc_str, (rtc_tc_time > rtc_now_time ? "-" : "+"),
            GST_TIME_ARGS (rtc_diff));
        g_free (tc_str);
      }
    }

    gst_video_time_code_clear (&rtc_timecode_now);
  }
  GST_OBJECT_UNLOCK (timecodestamper);

  /* Update LTC-based timecode as needed */
#if HAVE_LTC
  if (timecodestamper->ltcpad) {
    GstClockTime frame_duration;
    gchar *tc_str;
    TimestampedTimecode *ltc_tc;
    gboolean updated_internal = FALSE;

    frame_duration = gst_util_uint64_scale_int_ceil (GST_SECOND,
        timecodestamper->fps_d, timecodestamper->fps_n);

    g_mutex_lock (&timecodestamper->mutex);

    timecodestamper->video_current_running_time = running_time;

    /* Wait to compensate for the latency we introduce and to allow the LTC
     * audio to provide enough audio to extract timecodes, or until the video
     * pad is flushing or the LTC pad is EOS.
     * In non-live mode we introduce 4 frames of latency compared to the LTC
     * audio, see LATENCY query handling for details. */
    if (timecodestamper->video_live) {
      GstClock *clock =
          gst_element_get_clock (GST_ELEMENT_CAST (timecodestamper));

      if (clock) {
        GstClockID clock_id;
        GstClockTime base_time =
            gst_element_get_base_time (GST_ELEMENT_CAST (timecodestamper));
        GstClockTime wait_time;

        /* If we have no latency yet then wait at least for the LTC extra
         * latency. See LATENCY query handling for details. */
        if (timecodestamper->latency == GST_CLOCK_TIME_NONE) {
          wait_time =
              base_time + running_time + timecodestamper->ltc_extra_latency;
        } else {
          wait_time = base_time + running_time + timecodestamper->latency;
        }

        GST_TRACE_OBJECT (timecodestamper,
            "Waiting for clock to reach %" GST_TIME_FORMAT
            " (base time %" GST_TIME_FORMAT
            " + running time %" GST_TIME_FORMAT
            " + latency %" GST_TIME_FORMAT
            "), now %" GST_TIME_FORMAT,
            GST_TIME_ARGS (wait_time),
            GST_TIME_ARGS (base_time),
            GST_TIME_ARGS (running_time),
            GST_TIME_ARGS (timecodestamper->latency ==
                GST_CLOCK_TIME_NONE ? timecodestamper->ltc_extra_latency :
                timecodestamper->latency),
            GST_TIME_ARGS (gst_clock_get_time (clock))
            );
        clock_id = gst_clock_new_single_shot_id (clock, wait_time);

        timecodestamper->video_clock_id = clock_id;
        g_mutex_unlock (&timecodestamper->mutex);
        gst_clock_id_wait (clock_id, NULL);
        g_mutex_lock (&timecodestamper->mutex);
        timecodestamper->video_clock_id = NULL;
        gst_clock_id_unref (clock_id);
        gst_object_unref (clock);
      } else {
        GST_WARNING_OBJECT (timecodestamper,
            "No clock in live mode, not waiting");
      }
    } else {
      while ((timecodestamper->ltc_current_running_time == GST_CLOCK_TIME_NONE
              || timecodestamper->ltc_current_running_time <
              running_time + 8 * frame_duration)
          && !timecodestamper->video_flushing && !timecodestamper->ltc_eos) {
        GST_TRACE_OBJECT (timecodestamper,
            "Waiting for LTC audio to advance, EOS or flushing");
        g_cond_wait (&timecodestamper->ltc_cond_video, &timecodestamper->mutex);
      }
    }

    if (timecodestamper->video_flushing) {
      g_mutex_unlock (&timecodestamper->mutex);
      flow_ret = GST_FLOW_FLUSHING;
      goto out;
    }

    GST_OBJECT_LOCK (timecodestamper);
    /* Take timecodes out of the queue until we're at the current video
     * position. */
    while ((ltc_tc = g_queue_pop_head (&timecodestamper->ltc_current_tcs))) {
      /* First update framerate and flags according to the video stream if not
       * done yet */
      if (ltc_tc->timecode.config.fps_d == 0) {
        gint fps_n_div =
            ((gdouble) timecodestamper->fps_n) /
            timecodestamper->fps_d > 30 ? 2 : 1;

        ltc_tc->timecode.config.flags = tc_flags;
        ltc_tc->timecode.config.fps_n = timecodestamper->fps_n / fps_n_div;
        ltc_tc->timecode.config.fps_d = timecodestamper->fps_d;
      }

      tc_str = gst_video_time_code_to_string (&ltc_tc->timecode);
      GST_INFO_OBJECT (timecodestamper,
          "Retrieved LTC timecode %s at %" GST_TIME_FORMAT
          " (%u timecodes queued)", tc_str,
          GST_TIME_ARGS (ltc_tc->running_time),
          g_queue_get_length (&timecodestamper->ltc_current_tcs));
      g_free (tc_str);

      if (!gst_video_time_code_is_valid (&ltc_tc->timecode)) {
        tc_str = gst_video_time_code_to_string (&ltc_tc->timecode);
        GST_INFO_OBJECT (timecodestamper, "Invalid LTC timecode %s", tc_str);
        g_free (tc_str);
        gst_video_time_code_clear (&ltc_tc->timecode);
        g_free (ltc_tc);
        ltc_tc = NULL;
        continue;
      }

      /* A timecode frame that starts +/- half a frame to the
       * video frame is considered belonging to that video frame.
       *
       * If it's further ahead than half a frame duration, break out of
       * the loop here and reconsider on the next frame. */
      if (ABSDIFF (running_time, ltc_tc->running_time) <= frame_duration / 2) {
        /* If we're resyncing LTC in general, directly replace the current
         * LTC timecode with the new one we read. Otherwise we'll continue
         * counting based on the previous timecode we had
         */
        if (timecodestamper->ltc_auto_resync) {
          if (timecodestamper->ltc_internal_tc)
            gst_video_time_code_free (timecodestamper->ltc_internal_tc);
          timecodestamper->ltc_internal_tc =
              gst_video_time_code_copy (&ltc_tc->timecode);
          timecodestamper->ltc_internal_running_time = ltc_tc->running_time;
          updated_internal = TRUE;
          GST_INFO_OBJECT (timecodestamper, "Resynced internal LTC counter");
        }

        /* And store it back for the next frame in case it has more or less
         * the same running time */
        g_queue_push_head (&timecodestamper->ltc_current_tcs,
            g_steal_pointer (&ltc_tc));
        break;
      } else if (ltc_tc->running_time > running_time
          && ltc_tc->running_time - running_time > frame_duration / 2) {
        /* Store it back for the next frame */
        g_queue_push_head (&timecodestamper->ltc_current_tcs,
            g_steal_pointer (&ltc_tc));
        ltc_tc = NULL;
        break;
      }

      /* otherwise it's in the past and we need to consider the next
       * timecode. Read a new one */
      gst_video_time_code_clear (&ltc_tc->timecode);
      g_free (ltc_tc);
      ltc_tc = NULL;
    }

    /* If we didn't update from LTC above, increment our internal timecode
     * for this frame */
    if (!updated_internal && timecodestamper->ltc_internal_tc) {
      gst_video_time_code_increment_frame (timecodestamper->ltc_internal_tc);
    }

    if (timecodestamper->ltc_internal_tc) {
      if (timecodestamper->ltc_auto_resync
          && timecodestamper->ltc_timeout != GST_CLOCK_TIME_NONE
          && (running_time + timecodestamper->ltc_timeout <
              timecodestamper->ltc_internal_running_time
              || running_time >=
              timecodestamper->ltc_internal_running_time +
              timecodestamper->ltc_timeout)) {
        if (timecodestamper->ltc_internal_tc)
          gst_video_time_code_free (timecodestamper->ltc_internal_tc);
        timecodestamper->ltc_internal_tc = NULL;
        GST_DEBUG_OBJECT (timecodestamper, "LTC timecode timed out");
        timecodestamper->ltc_internal_running_time = GST_CLOCK_TIME_NONE;
      } else {
        tc_str =
            gst_video_time_code_to_string (timecodestamper->ltc_internal_tc);
        GST_DEBUG_OBJECT (timecodestamper, "Updated LTC timecode to %s",
            tc_str);
        g_free (tc_str);
      }
    } else {
      GST_DEBUG_OBJECT (timecodestamper, "Have no LTC timecode yet");
    }

    GST_OBJECT_UNLOCK (timecodestamper);

    g_cond_signal (&timecodestamper->ltc_cond_audio);

    g_mutex_unlock (&timecodestamper->mutex);
  }
#endif

  GST_OBJECT_LOCK (timecodestamper);
  switch (timecodestamper->tc_source) {
    case GST_TIME_CODE_STAMPER_SOURCE_INTERNAL:
      tc = timecodestamper->internal_tc;
      break;
    case GST_TIME_CODE_STAMPER_SOURCE_ZERO:
      tc = gst_video_time_code_new (timecodestamper->fps_n,
          timecodestamper->fps_d, NULL, tc_flags, 0, 0, 0, 0, 0);
      free_tc = TRUE;
      break;
    case GST_TIME_CODE_STAMPER_SOURCE_LAST_KNOWN:
      tc = timecodestamper->last_tc;
      if (!tc)
        tc = timecodestamper->internal_tc;
      break;
    case GST_TIME_CODE_STAMPER_SOURCE_LAST_KNOWN_OR_ZERO:
      tc = timecodestamper->last_tc;
      if (!tc) {
        tc = gst_video_time_code_new (timecodestamper->fps_n,
            timecodestamper->fps_d, NULL, tc_flags, 0, 0, 0, 0, 0);
        free_tc = TRUE;
      }
      break;
    case GST_TIME_CODE_STAMPER_SOURCE_LTC:
#if HAVE_LTC
      if (timecodestamper->ltc_internal_tc)
        tc = timecodestamper->ltc_internal_tc;
#endif
      if (!tc) {
        tc = gst_video_time_code_new (timecodestamper->fps_n,
            timecodestamper->fps_d, NULL, tc_flags, 0, 0, 0, 0, 0);
        free_tc = TRUE;
      }
      break;
    case GST_TIME_CODE_STAMPER_SOURCE_RTC:
      tc = timecodestamper->rtc_tc;
      break;
  }

  switch (timecodestamper->tc_set) {
    case GST_TIME_CODE_STAMPER_SET_NEVER:
      break;
    case GST_TIME_CODE_STAMPER_SET_KEEP:
      if (!tc_meta && tc) {
        gchar *tc_str;

        if (timecodestamper->timecode_offset) {
          if (!free_tc) {
            tc = gst_video_time_code_copy (tc);
            free_tc = TRUE;
          }
          gst_video_time_code_add_frames (tc, timecodestamper->timecode_offset);
        }

        tc_str = gst_video_time_code_to_string (tc);
        GST_DEBUG_OBJECT (timecodestamper, "Storing timecode %s", tc_str);
        g_free (tc_str);

        gst_buffer_add_video_time_code_meta (buffer, tc);
      }
      break;
    case GST_TIME_CODE_STAMPER_SET_ALWAYS:
      gst_buffer_foreach_meta (buffer, remove_timecode_meta, NULL);
      if (tc) {
        gchar *tc_str;

        if (timecodestamper->timecode_offset) {
          if (!free_tc) {
            tc = gst_video_time_code_copy (tc);
            free_tc = TRUE;
          }
          gst_video_time_code_add_frames (tc, timecodestamper->timecode_offset);
        }

        tc_str = gst_video_time_code_to_string (tc);
        GST_DEBUG_OBJECT (timecodestamper, "Storing timecode %s", tc_str);
        g_free (tc_str);

        gst_buffer_add_video_time_code_meta (buffer, tc);
      }
      break;
  }

  GST_OBJECT_UNLOCK (timecodestamper);

  if (timecodestamper->post_messages && tc) {
    GstClockTime stream_time, running_time, duration;
    GstStructure *s;
    GstMessage *msg;

    running_time =
        gst_segment_to_running_time (&vfilter->segment, GST_FORMAT_TIME,
        GST_BUFFER_PTS (buffer));
    stream_time =
        gst_segment_to_stream_time (&vfilter->segment, GST_FORMAT_TIME,
        GST_BUFFER_PTS (buffer));
    duration =
        gst_util_uint64_scale_int (GST_SECOND, timecodestamper->fps_d,
        timecodestamper->fps_n);
    s = gst_structure_new ("timecodestamper", "timestamp", G_TYPE_UINT64,
        GST_BUFFER_PTS (buffer), "stream-time", G_TYPE_UINT64, stream_time,
        "running-time", G_TYPE_UINT64, running_time, "duration",
        G_TYPE_UINT64, duration, "timecode", GST_TYPE_VIDEO_TIME_CODE, tc,
        NULL);
    msg = gst_message_new_element (GST_OBJECT (timecodestamper), s);
    gst_element_post_message (GST_ELEMENT (timecodestamper), msg);
  }
#if HAVE_LTC
out:
#endif

  if (dt_now)
    g_date_time_unref (dt_now);
  if (dt_frame)
    g_date_time_unref (dt_frame);
  if (free_tc && tc)
    gst_video_time_code_free (tc);

  return flow_ret;
}

static GstPad *
gst_timecodestamper_request_new_pad (GstElement * element,
    GstPadTemplate * templ, const gchar * name_templ, const GstCaps * caps)
{
#if HAVE_LTC
  GstTimeCodeStamper *timecodestamper = GST_TIME_CODE_STAMPER (element);

  GST_OBJECT_LOCK (timecodestamper);
  if (timecodestamper->ltcpad) {
    GST_OBJECT_UNLOCK (timecodestamper);
    return NULL;
  }

  if (GST_STATE (timecodestamper) > GST_STATE_READY ||
      GST_STATE_TARGET (timecodestamper) > GST_STATE_READY) {
    GST_ERROR_OBJECT (timecodestamper,
        "LTC audio pad can only be requested in NULL or READY state");
    GST_OBJECT_UNLOCK (timecodestamper);
    return NULL;
  }

  timecodestamper->ltcpad = gst_pad_new_from_static_template
      (&gst_timecodestamper_ltc_template, "ltc_sink");

  gst_pad_set_chain_function (timecodestamper->ltcpad,
      GST_DEBUG_FUNCPTR (gst_timecodestamper_ltcpad_chain));
  gst_pad_set_event_function (timecodestamper->ltcpad,
      GST_DEBUG_FUNCPTR (gst_timecodestamper_ltcpad_event));
  gst_pad_set_query_function (timecodestamper->ltcpad,
      GST_DEBUG_FUNCPTR (gst_timecodestamper_ltcpad_query));
  gst_pad_set_activatemode_function (timecodestamper->ltcpad,
      GST_DEBUG_FUNCPTR (gst_timecodestamper_ltcpad_activatemode));

  GST_OBJECT_UNLOCK (timecodestamper);

  g_mutex_lock (&timecodestamper->mutex);
  timecodestamper->audio_live = FALSE;
  timecodestamper->audio_latency = GST_CLOCK_TIME_NONE;
  g_mutex_unlock (&timecodestamper->mutex);

  gst_element_add_pad (element, timecodestamper->ltcpad);

  gst_element_post_message (GST_ELEMENT_CAST (timecodestamper),
      gst_message_new_latency (GST_OBJECT_CAST (timecodestamper)));

  return timecodestamper->ltcpad;
#else
  return NULL;
#endif
}

static void
gst_timecodestamper_release_pad (GstElement * element, GstPad * pad)
{
#if HAVE_LTC
  GstTimeCodeStamper *timecodestamper = GST_TIME_CODE_STAMPER (element);

  GST_OBJECT_LOCK (timecodestamper);
  if (timecodestamper->ltcpad != pad) {
    GST_OBJECT_UNLOCK (timecodestamper);
    return;
  }

  timecodestamper->ltcpad = NULL;

  if (timecodestamper->ltc_internal_tc != NULL) {
    gst_video_time_code_free (timecodestamper->ltc_internal_tc);
    timecodestamper->ltc_internal_tc = NULL;
  }
  timecodestamper->ltc_internal_running_time = GST_CLOCK_TIME_NONE;

  {
    TimestampedTimecode *tc;
    while ((tc = g_queue_pop_tail (&timecodestamper->ltc_current_tcs))) {
      gst_video_time_code_clear (&tc->timecode);
      g_free (tc);
    }
  }
  GST_OBJECT_UNLOCK (timecodestamper);

  gst_pad_set_active (pad, FALSE);

  g_mutex_lock (&timecodestamper->mutex);
  timecodestamper->ltc_flushing = TRUE;
  timecodestamper->ltc_eos = TRUE;
  g_cond_signal (&timecodestamper->ltc_cond_video);
  g_cond_signal (&timecodestamper->ltc_cond_audio);

  gst_audio_info_init (&timecodestamper->ainfo);
  gst_segment_init (&timecodestamper->ltc_segment, GST_FORMAT_UNDEFINED);

  timecodestamper->ltc_first_running_time = GST_CLOCK_TIME_NONE;
  timecodestamper->ltc_current_running_time = GST_CLOCK_TIME_NONE;

  if (timecodestamper->ltc_dec) {
    ltc_decoder_free (timecodestamper->ltc_dec);
    timecodestamper->ltc_dec = NULL;
  }

  if (timecodestamper->stream_align) {
    gst_audio_stream_align_free (timecodestamper->stream_align);
    timecodestamper->stream_align = NULL;
  }

  timecodestamper->ltc_total = 0;

  timecodestamper->audio_live = FALSE;
  timecodestamper->audio_latency = GST_CLOCK_TIME_NONE;
  g_mutex_unlock (&timecodestamper->mutex);

  gst_element_post_message (GST_ELEMENT_CAST (timecodestamper),
      gst_message_new_latency (GST_OBJECT_CAST (timecodestamper)));

  gst_element_remove_pad (element, pad);
#endif
}

#if HAVE_LTC
static GstFlowReturn
gst_timecodestamper_ltcpad_chain (GstPad * pad,
    GstObject * parent, GstBuffer * buffer)
{
  GstFlowReturn fr = GST_FLOW_OK;
  GstTimeCodeStamper *timecodestamper = GST_TIME_CODE_STAMPER (parent);
  GstMapInfo map;
  GstClockTime timestamp, running_time, duration;
  guint nsamples;
  gboolean discont;

  if (timecodestamper->audio_latency == -1 || gst_pad_check_reconfigure (pad)) {
    gst_timecodestamper_update_latency (timecodestamper, pad,
        &timecodestamper->audio_live, &timecodestamper->audio_latency);
  }

  g_mutex_lock (&timecodestamper->mutex);
  if (timecodestamper->ltc_flushing) {
    g_mutex_unlock (&timecodestamper->mutex);
    gst_buffer_unref (buffer);
    return GST_FLOW_FLUSHING;
  }

  nsamples = gst_buffer_get_size (buffer) /
      GST_AUDIO_INFO_BPF (&timecodestamper->ainfo);

  if (!timecodestamper->stream_align) {
    timecodestamper->stream_align =
        gst_audio_stream_align_new (timecodestamper->ainfo.rate,
        500 * GST_MSECOND, 20 * GST_MSECOND);
  }

  discont =
      gst_audio_stream_align_process (timecodestamper->stream_align,
      GST_BUFFER_IS_DISCONT (buffer), GST_BUFFER_PTS (buffer), nsamples,
      &timestamp, &duration, NULL);

  if (discont) {
    if (timecodestamper->ltc_dec) {
      GST_WARNING_OBJECT (timecodestamper, "Got discont at %" GST_TIME_FORMAT,
          GST_TIME_ARGS (timestamp));
      ltc_decoder_queue_flush (timecodestamper->ltc_dec);
    }
    timecodestamper->ltc_total = 0;
  }

  if (!timecodestamper->ltc_dec) {
    gint samples_per_frame = 1920;

    GST_OBJECT_LOCK (timecodestamper);
    /* This is only for initialization and needs to be somewhat close to the
     * real value. It will be tracked automatically afterwards */
    if (timecodestamper->fps_n) {
      samples_per_frame = timecodestamper->ainfo.rate *
          timecodestamper->fps_d / timecodestamper->fps_n;
    }
    GST_OBJECT_UNLOCK (timecodestamper);

    timecodestamper->ltc_dec =
        ltc_decoder_create (samples_per_frame, DEFAULT_LTC_QUEUE);
    timecodestamper->ltc_total = 0;
  }

  running_time = gst_segment_to_running_time (&timecodestamper->ltc_segment,
      GST_FORMAT_TIME, timestamp);

  GST_DEBUG_OBJECT (timecodestamper,
      "Handling LTC audio buffer at %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT
      " (offset %" G_GUINT64_FORMAT ")",
      GST_TIME_ARGS (running_time),
      GST_TIME_ARGS (running_time + duration),
      (guint64) timecodestamper->ltc_total);

  if (timecodestamper->ltc_total == 0) {
    timecodestamper->ltc_first_running_time = running_time;
  }

  gst_buffer_map (buffer, &map, GST_MAP_READ);
  ltc_decoder_write (timecodestamper->ltc_dec, map.data, map.size,
      timecodestamper->ltc_total);
  timecodestamper->ltc_total += map.size;
  gst_buffer_unmap (buffer, &map);

  /* Now read all the timecodes from the decoder that are currently available
   * and store them in our own queue, which gives us more control over how
   * things are working. */
  {
    LTCFrameExt ltc_frame;

    while (ltc_decoder_read (timecodestamper->ltc_dec, &ltc_frame) == 1) {
      SMPTETimecode stc;
      TimestampedTimecode *ltc_tc;
      GstClockTime ltc_running_time;

      if (ltc_frame.off_start < 0) {
        GstClockTime offset =
            gst_util_uint64_scale (GST_SECOND, -ltc_frame.off_start,
            timecodestamper->ainfo.rate);

        if (offset > timecodestamper->ltc_first_running_time)
          ltc_running_time = 0;
        else
          ltc_running_time = timecodestamper->ltc_first_running_time - offset;
      } else {
        ltc_running_time = timecodestamper->ltc_first_running_time +
            gst_util_uint64_scale (GST_SECOND, ltc_frame.off_start,
            timecodestamper->ainfo.rate);
      }

      ltc_frame_to_time (&stc, &ltc_frame.ltc, 0);
      GST_INFO_OBJECT (timecodestamper,
          "Got LTC timecode %02d:%02d:%02d:%02d at %" GST_TIME_FORMAT,
          stc.hours, stc.mins, stc.secs, stc.frame,
          GST_TIME_ARGS (ltc_running_time));

      ltc_tc = g_new0 (TimestampedTimecode, 1);
      ltc_tc->running_time = ltc_running_time;
      /* We fill in the framerate and other metadata later */
      gst_video_time_code_init (&ltc_tc->timecode,
          0, 0, timecodestamper->ltc_daily_jam, 0,
          stc.hours, stc.mins, stc.secs, stc.frame, 0);

      /* If we have a discontinuity it might happen that we're getting
       * timecodes that are in the past relative to timecodes we already have
       * in our queue. We have to get rid of all the timecodes that are in the
       * future now. */
      if (discont) {
        TimestampedTimecode *tmp;

        while ((tmp = g_queue_peek_tail (&timecodestamper->ltc_current_tcs)) &&
            tmp->running_time >= ltc_running_time) {
          gst_video_time_code_clear (&tmp->timecode);
          g_free (tmp);
          g_queue_pop_tail (&timecodestamper->ltc_current_tcs);
        }

        g_queue_push_tail (&timecodestamper->ltc_current_tcs,
            g_steal_pointer (&ltc_tc));
      } else {
        g_queue_push_tail (&timecodestamper->ltc_current_tcs,
            g_steal_pointer (&ltc_tc));
      }
    }
  }

  /* Notify the video streaming thread that new data is available */
  g_cond_signal (&timecodestamper->ltc_cond_video);

  /* Wait until video has caught up, if needed */
  if (timecodestamper->audio_live) {
    /* In live-mode, do no waiting as we're guaranteed to be more or less in
     * sync (~latency) with the video */
  } else {
    /* If we're ahead of the video, wait until the video has caught up.
     * Otherwise don't wait and drop any too old items from the ringbuffer */
    while ((timecodestamper->video_current_running_time == GST_CLOCK_TIME_NONE
            || running_time + duration >=
            timecodestamper->video_current_running_time)
        && timecodestamper->ltc_dec
        && g_queue_get_length (&timecodestamper->ltc_current_tcs) >
        DEFAULT_LTC_QUEUE / 2 && !timecodestamper->video_eos
        && !timecodestamper->ltc_flushing) {
      GST_TRACE_OBJECT (timecodestamper,
          "Waiting for video to advance, EOS or flushing");
      g_cond_wait (&timecodestamper->ltc_cond_audio, &timecodestamper->mutex);
    }
  }

  if (timecodestamper->ltc_flushing)
    fr = GST_FLOW_FLUSHING;
  else
    fr = GST_FLOW_OK;

  g_mutex_unlock (&timecodestamper->mutex);

  gst_buffer_unref (buffer);
  return fr;
}

static gboolean
gst_timecodestamper_ltcpad_event (GstPad * pad,
    GstObject * parent, GstEvent * event)
{
  GstTimeCodeStamper *timecodestamper = GST_TIME_CODE_STAMPER (parent);

  GstCaps *caps;
  gboolean ret = TRUE;

  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_CAPS:
      gst_event_parse_caps (event, &caps);

      if (!gst_audio_info_from_caps (&timecodestamper->ainfo, caps)) {
        gst_event_unref (event);
        return FALSE;
      }

      if (timecodestamper->stream_align) {
        gst_audio_stream_align_set_rate (timecodestamper->stream_align,
            timecodestamper->ainfo.rate);
      }

      break;
    case GST_EVENT_SEGMENT:
      gst_event_copy_segment (event, &timecodestamper->ltc_segment);
      break;

    case GST_EVENT_FLUSH_START:
      g_mutex_lock (&timecodestamper->mutex);
      timecodestamper->ltc_flushing = TRUE;
      g_cond_signal (&timecodestamper->ltc_cond_audio);
      g_mutex_unlock (&timecodestamper->mutex);
      break;
    case GST_EVENT_FLUSH_STOP:
      g_mutex_lock (&timecodestamper->mutex);
      timecodestamper->ltc_flushing = FALSE;
      timecodestamper->ltc_eos = FALSE;
      gst_segment_init (&timecodestamper->ltc_segment, GST_FORMAT_UNDEFINED);
      g_mutex_unlock (&timecodestamper->mutex);
      break;
    case GST_EVENT_EOS:
      g_mutex_lock (&timecodestamper->mutex);
      timecodestamper->ltc_eos = TRUE;
      g_cond_signal (&timecodestamper->ltc_cond_video);
      g_mutex_unlock (&timecodestamper->mutex);
      break;

    default:
      break;
  }

  gst_event_unref (event);
  return ret;
}

static gboolean
gst_timecodestamper_ltcpad_query (GstPad * pad,
    GstObject * parent, GstQuery * query)
{
  GstCaps *caps, *filter, *tcaps;

  switch (GST_QUERY_TYPE (query)) {
    case GST_QUERY_CAPS:
      gst_query_parse_caps (query, &filter);
      tcaps = gst_pad_get_pad_template_caps (pad);
      if (filter)
        caps = gst_caps_intersect_full (tcaps, filter,
            GST_CAPS_INTERSECT_FIRST);
      else
        caps = gst_caps_ref (tcaps);
      gst_query_set_caps_result (query, caps);
      gst_caps_unref (tcaps);
      gst_caps_unref (caps);
      return TRUE;
    default:
      return gst_pad_query_default (pad, parent, query);
  }
}

static gboolean
gst_timecodestamper_ltcpad_activatemode (GstPad * pad,
    GstObject * parent, GstPadMode mode, gboolean active)
{
  GstTimeCodeStamper *timecodestamper = GST_TIME_CODE_STAMPER (parent);

  if (active) {
    g_mutex_lock (&timecodestamper->mutex);
    timecodestamper->ltc_flushing = FALSE;
    timecodestamper->ltc_eos = FALSE;
    timecodestamper->audio_live = FALSE;
    timecodestamper->audio_latency = GST_CLOCK_TIME_NONE;
    g_mutex_unlock (&timecodestamper->mutex);
  } else {
    g_mutex_lock (&timecodestamper->mutex);
    timecodestamper->ltc_flushing = TRUE;
    timecodestamper->ltc_eos = TRUE;
    g_cond_signal (&timecodestamper->ltc_cond_audio);
    g_mutex_unlock (&timecodestamper->mutex);
  }

  return TRUE;
}

static gboolean
gst_timecodestamper_videopad_activatemode (GstPad * pad,
    GstObject * parent, GstPadMode mode, gboolean active)
{
  GstTimeCodeStamper *timecodestamper = GST_TIME_CODE_STAMPER (parent);

  if (active) {
    g_mutex_lock (&timecodestamper->mutex);
    timecodestamper->video_flushing = FALSE;
    timecodestamper->video_eos = FALSE;
    timecodestamper->video_live = FALSE;
    timecodestamper->video_latency = GST_CLOCK_TIME_NONE;
    timecodestamper->video_current_running_time = GST_CLOCK_TIME_NONE;
    g_mutex_unlock (&timecodestamper->mutex);
  } else {
    g_mutex_lock (&timecodestamper->mutex);
    timecodestamper->video_flushing = TRUE;
    timecodestamper->video_current_running_time = GST_CLOCK_TIME_NONE;
    if (timecodestamper->video_clock_id)
      gst_clock_id_unschedule (timecodestamper->video_clock_id);
    g_cond_signal (&timecodestamper->ltc_cond_video);
    g_mutex_unlock (&timecodestamper->mutex);
  }

  return timecodestamper->video_activatemode_default (pad, parent, mode,
      active);
}

static GstIterator *
gst_timecodestamper_src_iterate_internal_link (GstPad * pad, GstObject * parent)
{
  GstTimeCodeStamper *timecodestamper = GST_TIME_CODE_STAMPER (parent);
  GValue value = G_VALUE_INIT;
  GstIterator *it;

  g_value_init (&value, GST_TYPE_PAD);
  g_value_set_object (&value, GST_BASE_TRANSFORM_SINK_PAD (timecodestamper));
  it = gst_iterator_new_single (GST_TYPE_PAD, &value);
  g_value_unset (&value);

  return it;
}
#endif