Welcome to mirror list, hosted at ThFree Co, Russian Federation.

fallbacksrc.rs « src « fallbackswitch « utils - gitlab.freedesktop.org/gstreamer/gst-plugins-rs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5ae3c297952831df6363d96ff6d05f896b52daa6 (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
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
// Copyright (C) 2020 Sebastian Dröge <sebastian@centricular.com>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the
// Free Software Foundation, Inc., 51 Franklin Street, Suite 500,
// Boston, MA 02110-1335, USA.

use glib::prelude::*;
use glib::subclass;
use glib::subclass::prelude::*;
use gst::prelude::*;
use gst::subclass::prelude::*;

use std::mem;
use std::sync::Mutex;
use std::time::{Duration, Instant};

use once_cell::sync::Lazy;

static CAT: Lazy<gst::DebugCategory> = Lazy::new(|| {
    gst::DebugCategory::new(
        "fallbacksrc",
        gst::DebugColorFlags::empty(),
        Some("Fallback Source Bin"),
    )
});

#[derive(Debug, Clone)]
struct Settings {
    enable_audio: bool,
    enable_video: bool,
    uri: Option<String>,
    source: Option<gst::Element>,
    fallback_uri: Option<String>,
    timeout: u64,
    retry_timeout: u64,
}

impl Default for Settings {
    fn default() -> Self {
        Settings {
            enable_audio: true,
            enable_video: true,
            uri: None,
            source: None,
            fallback_uri: None,
            timeout: 5 * gst::SECOND_VAL,
            retry_timeout: 60 * gst::SECOND_VAL,
        }
    }
}

#[derive(Debug)]
enum Source {
    Uri(String),
    Element(gst::Element),
}

// Blocking buffer pad probe on the source pads. Once blocked we have a running time for the
// current buffer that can later be used for offsetting
//
// This is used for the initial offsetting after starting of the stream and for "pausing" when
// buffering.
struct Block {
    pad: gst::Pad,
    probe_id: gst::PadProbeId,
    running_time: gst::ClockTime,
}

// Connects one source pad with fallbackswitch and the corresponding fallback input
struct Stream {
    // Fallback input stream
    //   for video: filesrc, decoder, converters, imagefreeze
    //   for audio: live audiotestsrc, converters
    fallback_input: gst::Element,

    // source pad from source
    source_srcpad: Option<gst::Pad>,
    source_srcpad_block: Option<Block>,

    // clocksync for source source pad
    clocksync: gst::Element,

    // fallbackswitch
    switch: gst::Element,

    // output source pad, connected to switch
    srcpad: gst::Pad,
}

struct State {
    // uridecodebin3 or custom source element
    source: gst::Element,
    source_is_live: bool,
    source_pending_restart: bool,

    // For timing out the source if we have to wait some additional time
    // after fallbackswitch switched due to recent buffering activity
    source_pending_timeout: Option<gst::ClockId>,
    // For restarting the source after shutting it down
    source_pending_restart_timeout: Option<gst::ClockId>,
    // For failing completely if we didn't recover after the retry timeout
    source_retry_timeout: Option<gst::ClockId>,

    // All our output streams, selected by properties
    video_stream: Option<Stream>,
    audio_stream: Option<Stream>,
    flow_combiner: gst_base::UniqueFlowCombiner,

    buffering_percent: u8,
    last_buffering_update: Option<Instant>,

    // Stream collection posted by source
    streams: Option<gst::StreamCollection>,

    // Configure settings
    settings: Settings,
    configured_source: Source,
}

struct FallbackSrc {
    settings: Mutex<Settings>,
    state: Mutex<Option<State>>,
}

#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy, GEnum)]
#[repr(u32)]
#[genum(type_name = "GstFallbackSourceStatus")]
enum Status {
    Stopped,
    Buffering,
    Retrying,
    Running,
}

static PROPERTIES: [subclass::Property; 8] = [
    subclass::Property("enable-audio", |name| {
        glib::ParamSpec::boolean(
            name,
            "Enable Audio",
            "Enable the audio stream, this will output silence if there's no audio in the configured URI",
            true,
            glib::ParamFlags::READWRITE,
        )
    }),
    subclass::Property("enable-video", |name| {
        glib::ParamSpec::boolean(
            name,
            "Enable Video",
            "Enable the video stream, this will output black or the fallback video if there's no video in the configured URI",
            true,
            glib::ParamFlags::READWRITE,
        )
    }),
    subclass::Property("uri", |name| {
        glib::ParamSpec::string(name, "URI", "URI to use", None, glib::ParamFlags::READWRITE)
    }),
    subclass::Property("source", |name| {
        glib::ParamSpec::object(
            name,
            "Source",
            "Source to use instead of the URI",
            gst::Element::static_type(),
            glib::ParamFlags::READWRITE,
        )
    }),
    subclass::Property("fallback-uri", |name| {
        glib::ParamSpec::string(
            name,
            "Fallback URI",
            "Fallback URI to use for video in case the main stream doesn't work",
            None,
            glib::ParamFlags::READWRITE,
        )
    }),
    subclass::Property("timeout", |name| {
        glib::ParamSpec::uint64(
            name,
            "Timeout",
            "Timeout for switching to the fallback URI",
            0,
            std::u64::MAX,
            5 * gst::SECOND_VAL,
            glib::ParamFlags::READWRITE,
        )
    }),
    subclass::Property("retry-timeout", |name| {
        glib::ParamSpec::uint64(
            name,
            "Retry Timeout",
            "Timeout for stopping after repeated failure",
            0,
            std::u64::MAX,
            60 * gst::SECOND_VAL,
            glib::ParamFlags::READWRITE,
        )
    }),
    subclass::Property("status", |name| {
        glib::ParamSpec::enum_(
            name,
            "Status",
            "Current source status",
            Status::static_type(),
            Status::Stopped as i32,
            glib::ParamFlags::READABLE,
        )
    }),
];

impl ObjectSubclass for FallbackSrc {
    const NAME: &'static str = "FallbackSrc";
    type ParentType = gst::Bin;
    type Instance = gst::subclass::ElementInstanceStruct<Self>;
    type Class = subclass::simple::ClassStruct<Self>;

    glib_object_subclass!();

    fn new() -> Self {
        Self {
            settings: Mutex::new(Settings::default()),
            state: Mutex::new(None),
        }
    }

    fn class_init(klass: &mut subclass::simple::ClassStruct<Self>) {
        klass.set_metadata(
            "Fallback Source",
            "Generic/Source",
            "Live source with uridecodebin3 or custom source, and fallback image stream",
            "Sebastian Dröge <sebastian@centricular.com>",
        );

        let src_pad_template = gst::PadTemplate::new(
            "audio",
            gst::PadDirection::Src,
            gst::PadPresence::Sometimes,
            &gst::Caps::new_any(),
        )
        .unwrap();
        klass.add_pad_template(src_pad_template);

        let src_pad_template = gst::PadTemplate::new(
            "video",
            gst::PadDirection::Src,
            gst::PadPresence::Sometimes,
            &gst::Caps::new_any(),
        )
        .unwrap();
        klass.add_pad_template(src_pad_template);

        klass.install_properties(&PROPERTIES);
    }
}

impl ObjectImpl for FallbackSrc {
    glib_object_impl!();

    fn set_property(&self, obj: &glib::Object, id: usize, value: &glib::Value) {
        let prop = &PROPERTIES[id];
        let element = obj.downcast_ref::<gst::Bin>().unwrap();

        match *prop {
            subclass::Property("enable-audio", ..) => {
                let mut settings = self.settings.lock().unwrap();
                let new_value = value.get_some().expect("type checked upstream");
                gst_info!(
                    CAT,
                    obj: element,
                    "Changing enable-audio from {:?} to {:?}",
                    settings.enable_audio,
                    new_value,
                );
                settings.enable_audio = new_value;
            }
            subclass::Property("enable-video", ..) => {
                let mut settings = self.settings.lock().unwrap();
                let new_value = value.get_some().expect("type checked upstream");
                gst_info!(
                    CAT,
                    obj: element,
                    "Changing enable-video from {:?} to {:?}",
                    settings.enable_video,
                    new_value,
                );
                settings.enable_video = new_value;
            }
            subclass::Property("uri", ..) => {
                let mut settings = self.settings.lock().unwrap();
                let new_value = value.get().expect("type checked upstream");
                gst_info!(
                    CAT,
                    obj: element,
                    "Changing URI from {:?} to {:?}",
                    settings.uri,
                    new_value,
                );
                settings.uri = new_value;
            }
            subclass::Property("source", ..) => {
                let mut settings = self.settings.lock().unwrap();
                let new_value = value.get().expect("type checked upstream");
                gst_info!(
                    CAT,
                    obj: element,
                    "Changing source from {:?} to {:?}",
                    settings.source,
                    new_value,
                );
                settings.source = new_value;
            }
            subclass::Property("fallback-uri", ..) => {
                let mut settings = self.settings.lock().unwrap();
                let new_value = value.get().expect("type checked upstream");
                gst_info!(
                    CAT,
                    obj: element,
                    "Changing Fallback URI from {:?} to {:?}",
                    settings.fallback_uri,
                    new_value,
                );
                settings.fallback_uri = new_value;
            }
            subclass::Property("timeout", ..) => {
                let mut settings = self.settings.lock().unwrap();
                let new_value = value.get_some().expect("type checked upstream");
                gst_info!(
                    CAT,
                    obj: element,
                    "Changing timeout from {:?} to {:?}",
                    settings.timeout,
                    new_value,
                );
                settings.timeout = new_value;
            }
            subclass::Property("retry-timeout", ..) => {
                let mut settings = self.settings.lock().unwrap();
                let new_value = value.get_some().expect("type checked upstream");
                gst_info!(
                    CAT,
                    obj: element,
                    "Changing Retry Timeout from {:?} to {:?}",
                    settings.retry_timeout,
                    new_value,
                );
                settings.retry_timeout = new_value;
            }
            _ => unimplemented!(),
        }
    }

    // Called whenever a value of a property is read. It can be called
    // at any time from any thread.
    #[allow(clippy::block_in_if_condition_stmt)]
    fn get_property(&self, _obj: &glib::Object, id: usize) -> Result<glib::Value, ()> {
        let prop = &PROPERTIES[id];

        match *prop {
            subclass::Property("enable-audio", ..) => {
                let settings = self.settings.lock().unwrap();
                Ok(settings.enable_audio.to_value())
            }
            subclass::Property("enable-video", ..) => {
                let settings = self.settings.lock().unwrap();
                Ok(settings.enable_video.to_value())
            }
            subclass::Property("uri", ..) => {
                let settings = self.settings.lock().unwrap();
                Ok(settings.uri.to_value())
            }
            subclass::Property("source", ..) => {
                let settings = self.settings.lock().unwrap();
                Ok(settings.source.to_value())
            }
            subclass::Property("fallback-uri", ..) => {
                let settings = self.settings.lock().unwrap();
                Ok(settings.fallback_uri.to_value())
            }
            subclass::Property("timeout", ..) => {
                let settings = self.settings.lock().unwrap();
                Ok(settings.timeout.to_value())
            }
            subclass::Property("retry-timeout", ..) => {
                let settings = self.settings.lock().unwrap();
                Ok(settings.retry_timeout.to_value())
            }
            subclass::Property("status", ..) => {
                let state_guard = self.state.lock().unwrap();

                // If we have no state then we'r stopped
                let state = match &*state_guard {
                    None => return Ok(Status::Stopped.to_value()),
                    Some(ref state) => state,
                };

                // If any restarts/retries are pending, we're retrying
                if state.source_pending_restart
                    || state.source_pending_restart_timeout.is_some()
                    || state.source_retry_timeout.is_some()
                {
                    return Ok(Status::Retrying.to_value());
                }

                // Otherwise if buffering < 100, we have no streams yet or of the expected
                // streams there is no source pad yet, we're buffering
                let mut have_audio = false;
                let mut have_video = false;
                if let Some(ref streams) = state.streams {
                    for stream in streams.iter() {
                        have_audio =
                            have_audio || stream.get_stream_type().contains(gst::StreamType::AUDIO);
                        have_video =
                            have_video || stream.get_stream_type().contains(gst::StreamType::VIDEO);
                    }
                }

                if state.buffering_percent < 100
                    || state.source_pending_timeout.is_some()
                    || state.streams.is_none()
                    || (have_audio
                        && state
                            .audio_stream
                            .as_ref()
                            .map(|s| s.source_srcpad.is_none() || s.source_srcpad_block.is_some())
                            .unwrap_or(false))
                    || (have_video
                        && state
                            .video_stream
                            .as_ref()
                            .map(|s| s.source_srcpad.is_none() || s.source_srcpad_block.is_some())
                            .unwrap_or(false))
                {
                    return Ok(Status::Buffering.to_value());
                }

                // Otherwise we're running now
                Ok(Status::Running.to_value())
            }
            _ => unimplemented!(),
        }
    }

    fn constructed(&self, obj: &glib::Object) {
        self.parent_constructed(obj);

        let bin = obj.downcast_ref::<gst::Bin>().unwrap();
        bin.set_suppressed_flags(gst::ElementFlags::SOURCE | gst::ElementFlags::SINK);
        bin.set_element_flags(gst::ElementFlags::SOURCE);
        bin.set_bin_flags(gst::BinFlags::STREAMS_AWARE);
    }
}

impl ElementImpl for FallbackSrc {
    #[allow(clippy::single_match)]
    fn change_state(
        &self,
        element: &gst::Element,
        transition: gst::StateChange,
    ) -> Result<gst::StateChangeSuccess, gst::StateChangeError> {
        match transition {
            gst::StateChange::NullToReady => {
                self.start(element)?;
            }
            _ => (),
        }

        self.parent_change_state(element, transition)?;

        // Change the source state manually here to be able to catch errors. State changes always
        // happen from sink to source, so we do this after chaining up.
        self.change_source_state(element, transition)?;

        // Ignore parent state change return to prevent spurious async/no-preroll return values
        // due to core state change bugs
        match transition {
            gst::StateChange::ReadyToPaused | gst::StateChange::PlayingToPaused => {
                Ok(gst::StateChangeSuccess::NoPreroll)
            }
            gst::StateChange::ReadyToNull => {
                self.stop(element)?;
                Ok(gst::StateChangeSuccess::Success)
            }
            _ => Ok(gst::StateChangeSuccess::Success),
        }
    }
}

impl BinImpl for FallbackSrc {
    fn handle_message(&self, bin: &gst::Bin, msg: gst::Message) {
        use gst::MessageView;

        match msg.view() {
            MessageView::Buffering(ref m) => {
                // Don't forward upwards, we handle this internally
                self.handle_buffering(bin, m);
            }
            MessageView::StreamsSelected(ref m) => {
                // Don't forward upwards, we are exposing streams based on properties
                // TODO: Do stream configuration via our own stream collection and handling
                // of stream select events
                // TODO: Also needs updating of StreamCollection handling in CustomSource
                self.handle_streams_selected(bin, m);
            }
            MessageView::Error(ref m) => {
                if !self.handle_error(bin, m) {
                    self.parent_handle_message(bin, msg);
                }
            }
            _ => self.parent_handle_message(bin, msg),
        }
    }
}

impl FallbackSrc {
    fn create_main_input(
        &self,
        element: &gst::Bin,
        source: &Source,
    ) -> Result<gst::Element, gst::StateChangeError> {
        let source = match source {
            Source::Uri(ref uri) => {
                let source = gst::ElementFactory::make("uridecodebin3", Some("uridecodebin"))
                    .expect("No uridecodebin3 found");

                source.set_property("uri", &uri).unwrap();
                source.set_property("use-buffering", &true).unwrap();

                source
            }
            Source::Element(ref source) => custom_source::CustomSource::new(source),
        };

        // Handle any async state changes internally, they don't affect the pipeline because we
        // convert everything to a live stream
        source.set_property("async-handling", &true).unwrap();
        // Don't let the bin handle state changes of the source. We want to do it manually to catch
        // possible errors and retry, without causing the whole bin state change to fail
        source.set_locked_state(true);

        let element_weak = element.downgrade();
        source.connect_pad_added(move |_, pad| {
            let element = match element_weak.upgrade() {
                None => return,
                Some(element) => element,
            };
            let src = FallbackSrc::from_instance(&element);

            if let Err(msg) = src.handle_source_pad_added(&element, pad) {
                element.post_error_message(&msg);
            }
        });
        let element_weak = element.downgrade();
        source.connect_pad_removed(move |_, pad| {
            let element = match element_weak.upgrade() {
                None => return,
                Some(element) => element,
            };
            let src = FallbackSrc::from_instance(&element);

            if let Err(msg) = src.handle_source_pad_removed(&element, pad) {
                element.post_error_message(&msg);
            }
        });

        element.add_many(&[&source]).unwrap();

        Ok(source)
    }

    fn create_fallback_video_input(
        &self,
        element: &gst::Bin,
        fallback_uri: Option<&str>,
    ) -> Result<gst::Element, gst::StateChangeError> {
        let input = gst::Bin::new(Some("fallback_video"));

        let srcpad = match fallback_uri {
            Some(fallback_uri) => {
                let filesrc = gst::ElementFactory::make("filesrc", Some("fallback_filesrc"))
                    .expect("No filesrc found");
                let typefind = gst::ElementFactory::make("typefind", Some("fallback_typefind"))
                    .expect("No typefind found");
                let videoconvert =
                    gst::ElementFactory::make("videoconvert", Some("fallback_videoconvert"))
                        .expect("No videoconvert found");
                let videoscale =
                    gst::ElementFactory::make("videoscale", Some("fallback_videoscale"))
                        .expect("No videoscale found");
                let imagefreeze =
                    gst::ElementFactory::make("imagefreeze", Some("fallback_imagefreeze"))
                        .expect("No imagefreeze found");
                let clocksync = gst::ElementFactory::make("clocksync", Some("fallback_clocksync"))
                    .or_else(|_| -> Result<_, glib::BoolError> {
                        let identity =
                            gst::ElementFactory::make("identity", Some("fallback_clocksync"))?;
                        identity.set_property("sync", &true).unwrap();
                        Ok(identity)
                    })
                    .expect("No clocksync or identity found");

                input
                    .add_many(&[
                        &filesrc,
                        &typefind,
                        &videoconvert,
                        &videoscale,
                        &imagefreeze,
                        &clocksync,
                    ])
                    .unwrap();
                gst::Element::link_many(&[&filesrc, &typefind]).unwrap();
                gst::Element::link_many(&[&videoconvert, &videoscale, &imagefreeze, &clocksync])
                    .unwrap();

                filesrc
                    .dynamic_cast_ref::<gst::URIHandler>()
                    .unwrap()
                    .set_uri(fallback_uri)
                    .map_err(|err| {
                        gst_error!(CAT, obj: element, "Failed to set fallback URI: {}", err);
                        gst_element_error!(
                            element,
                            gst::LibraryError::Settings,
                            ["Failed to set fallback URI: {}", err]
                        );
                        gst::StateChangeError
                    })?;

                let element_weak = element.downgrade();
                let input_weak = input.downgrade();
                let videoconvert_weak = videoconvert.downgrade();
                typefind
                    .connect("have-type", false, move |args| {
                        let typefind = args[0].get::<gst::Element>().unwrap().unwrap();
                        let _probability = args[1].get_some::<u32>().unwrap();
                        let caps = args[2].get::<gst::Caps>().unwrap().unwrap();

                        let element = match element_weak.upgrade() {
                            Some(element) => element,
                            None => return None,
                        };

                        let input = match input_weak.upgrade() {
                            Some(element) => element,
                            None => return None,
                        };

                        let videoconvert = match videoconvert_weak.upgrade() {
                            Some(element) => element,
                            None => return None,
                        };

                        let s = caps.get_structure(0).unwrap();
                        let decoder;
                        if s.get_name() == "image/jpeg" {
                            decoder = gst::ElementFactory::make("jpegdec", Some("decoder"))
                                .expect("jpegdec not found");
                        } else if s.get_name() == "image/png" {
                            decoder = gst::ElementFactory::make("pngdec", Some("decoder"))
                                .expect("pngdec not found");
                        } else {
                            gst_error!(CAT, obj: &element, "Unsupported caps {}", caps);
                            gst_element_error!(
                                element,
                                gst::StreamError::Format,
                                ["Unsupported caps {}", caps]
                            );
                            return None;
                        }

                        input.add(&decoder).unwrap();
                        decoder.sync_state_with_parent().unwrap();
                        if let Err(_err) =
                            gst::Element::link_many(&[&typefind, &decoder, &videoconvert])
                        {
                            gst_error!(CAT, obj: &element, "Can't link fallback image decoder");
                            gst_element_error!(
                                element,
                                gst::StreamError::Format,
                                ["Can't link fallback image decoder"]
                            );
                            return None;
                        }

                        None
                    })
                    .unwrap();

                clocksync.get_static_pad("src").unwrap()
            }
            None => {
                let videotestsrc =
                    gst::ElementFactory::make("videotestsrc", Some("fallback_videosrc"))
                        .expect("No videotestsrc found");
                input.add_many(&[&videotestsrc]).unwrap();

                videotestsrc.set_property_from_str("pattern", "black");
                videotestsrc.set_property("is-live", &true).unwrap();

                videotestsrc.get_static_pad("src").unwrap()
            }
        };

        input
            .add_pad(
                &gst::GhostPad::builder(Some("src"), gst::PadDirection::Src)
                    .build_with_target(&srcpad)
                    .unwrap(),
            )
            .unwrap();

        Ok(input.upcast())
    }

    fn create_fallback_audio_input(
        &self,
        _element: &gst::Bin,
    ) -> Result<gst::Element, gst::StateChangeError> {
        let input = gst::Bin::new(Some("fallback_audio"));
        let audiotestsrc = gst::ElementFactory::make("audiotestsrc", Some("fallback_audiosrc"))
            .expect("No audiotestsrc found");
        input.add_many(&[&audiotestsrc]).unwrap();

        audiotestsrc.set_property_from_str("wave", "silence");
        audiotestsrc.set_property("is-live", &true).unwrap();

        let srcpad = audiotestsrc.get_static_pad("src").unwrap();
        input
            .add_pad(
                &gst::GhostPad::builder(Some("src"), gst::PadDirection::Src)
                    .build_with_target(&srcpad)
                    .unwrap(),
            )
            .unwrap();

        Ok(input.upcast())
    }

    fn create_stream(
        &self,
        element: &gst::Bin,
        timeout: u64,
        is_audio: bool,
        fallback_uri: Option<&str>,
    ) -> Result<Stream, gst::StateChangeError> {
        let fallback_input = if is_audio {
            self.create_fallback_audio_input(element)?
        } else {
            self.create_fallback_video_input(element, fallback_uri)?
        };

        let switch =
            gst::ElementFactory::make("fallbackswitch", None).expect("No fallbackswitch found");
        let clocksync = gst::ElementFactory::make("clocksync", None)
            .or_else(|_| -> Result<_, glib::BoolError> {
                let identity = gst::ElementFactory::make("identity", None)?;
                identity.set_property("sync", &true).unwrap();
                Ok(identity)
            })
            .expect("No clocksync or identity found");

        element
            .add_many(&[&fallback_input, &switch, &clocksync])
            .unwrap();

        let element_weak = element.downgrade();
        switch.connect_notify(Some("active-pad"), move |_switch, _pspec| {
            let element = match element_weak.upgrade() {
                None => return,
                Some(element) => element,
            };

            let src = FallbackSrc::from_instance(&element);
            src.handle_switch_active_pad_change(&element);
        });
        switch.set_property("timeout", &timeout).unwrap();

        gst::Element::link_pads(&fallback_input, Some("src"), &switch, Some("fallback_sink"))
            .unwrap();
        gst::Element::link_pads(&clocksync, Some("src"), &switch, Some("sink")).unwrap();
        // clocksync sink pad is not connected to anything yet at this point!

        let srcpad = switch.get_static_pad("src").unwrap();
        let templ = element
            .get_pad_template(if is_audio { "audio" } else { "video" })
            .unwrap();
        let ghostpad = gst::GhostPad::builder_with_template(&templ, Some(&templ.get_name()))
            .proxy_pad_chain_function({
                let element_weak = element.downgrade();
                move |pad, _parent, buffer| {
                    let element = match element_weak.upgrade() {
                        None => return Err(gst::FlowError::Flushing),
                        Some(element) => element,
                    };

                    let src = FallbackSrc::from_instance(&element);
                    src.proxy_pad_chain(&element, pad, buffer)
                }
            })
            .build_with_target(&srcpad)
            .unwrap();

        element.add_pad(&ghostpad).unwrap();

        Ok(Stream {
            fallback_input,
            source_srcpad: None,
            source_srcpad_block: None,
            clocksync,
            switch,
            srcpad: ghostpad.upcast(),
        })
    }

    fn start(&self, element: &gst::Element) -> Result<(), gst::StateChangeError> {
        let element = element.downcast_ref::<gst::Bin>().unwrap();

        gst_debug!(CAT, obj: element, "Starting");
        let mut state_guard = self.state.lock().unwrap();
        if state_guard.is_some() {
            return Err(gst::StateChangeError);
        }

        let settings = self.settings.lock().unwrap().clone();
        let configured_source = match settings
            .uri
            .as_ref()
            .cloned()
            .map(Source::Uri)
            .or_else(|| settings.source.as_ref().cloned().map(Source::Element))
        {
            Some(source) => source,
            None => {
                gst_error!(CAT, obj: element, "No URI or source element configured");
                gst_element_error!(
                    element,
                    gst::LibraryError::Settings,
                    ["No URI or source element configured"]
                );
                return Err(gst::StateChangeError);
            }
        };

        let fallback_uri = &settings.fallback_uri;

        // Create main input
        let source = self.create_main_input(element, &configured_source)?;

        let mut flow_combiner = gst_base::UniqueFlowCombiner::new();

        // Create video stream
        let video_stream = if settings.enable_video {
            let stream =
                self.create_stream(element, settings.timeout, false, fallback_uri.as_deref())?;
            flow_combiner.add_pad(&stream.srcpad);
            Some(stream)
        } else {
            None
        };

        // Create audio stream
        let audio_stream = if settings.enable_audio {
            let stream = self.create_stream(element, settings.timeout, true, None)?;
            flow_combiner.add_pad(&stream.srcpad);
            Some(stream)
        } else {
            None
        };

        *state_guard = Some(State {
            source,
            source_is_live: false,
            source_pending_restart: false,
            source_pending_timeout: None,
            source_pending_restart_timeout: None,
            source_retry_timeout: None,
            video_stream,
            audio_stream,
            flow_combiner,
            buffering_percent: 100,
            last_buffering_update: None,
            streams: None,
            settings,
            configured_source,
        });

        drop(state_guard);

        element.no_more_pads();

        element.notify("status");

        gst_debug!(CAT, obj: element, "Started");
        Ok(())
    }

    fn stop(&self, element: &gst::Element) -> Result<(), gst::StateChangeError> {
        let element = element.downcast_ref::<gst::Bin>().unwrap();

        gst_debug!(CAT, obj: element, "Stopping");
        let mut state_guard = self.state.lock().unwrap();
        let mut state = match state_guard.take() {
            Some(state) => state,
            None => return Ok(()),
        };
        drop(state_guard);

        element.notify("status");

        // In theory all streams should've been removed from the source's pad-removed signal
        // handler when going from Paused to Ready but better safe than sorry here
        for stream in [&state.video_stream, &state.audio_stream]
            .iter()
            .filter_map(|v| v.as_ref())
        {
            element.remove(&stream.switch).unwrap();
            element.remove(&stream.clocksync).unwrap();
            element.remove(&stream.fallback_input).unwrap();
            element.remove_pad(&stream.srcpad).unwrap();
        }
        state.video_stream = None;
        state.audio_stream = None;

        element.remove(&state.source).unwrap();

        if let Some(timeout) = state.source_pending_restart_timeout.take() {
            timeout.unschedule();
        }

        if let Some(timeout) = state.source_retry_timeout.take() {
            timeout.unschedule();
        }

        if let Some(timeout) = state.source_pending_timeout.take() {
            timeout.unschedule();
        }

        gst_debug!(CAT, obj: element, "Stopped");
        Ok(())
    }

    fn change_source_state(
        &self,
        element: &gst::Element,
        transition: gst::StateChange,
    ) -> Result<(), gst::StateChangeError> {
        let element = element.downcast_ref::<gst::Bin>().unwrap();

        gst_debug!(CAT, obj: element, "Changing source state: {:?}", transition);
        let mut state_guard = self.state.lock().unwrap();
        let state = match &mut *state_guard {
            Some(state) => state,
            None => return Ok(()),
        };

        if transition.current() <= transition.next() && state.source_pending_restart {
            gst_debug!(
                CAT,
                obj: element,
                "Not starting source because pending restart"
            );
            return Ok(());
        } else if transition.next() <= gst::State::Ready && state.source_pending_restart {
            gst_debug!(
                CAT,
                obj: element,
                "Unsetting pending restart because shutting down"
            );
            state.source_pending_restart = false;
            if let Some(timeout) = state.source_pending_restart_timeout.take() {
                timeout.unschedule();
            }
        }
        let source = state.source.clone();
        drop(state_guard);

        element.notify("status");

        let res = source.set_state(transition.next());
        match res {
            Err(_) => {
                gst_error!(CAT, obj: element, "Source failed to change state");
                // Try again later if we're not shutting down
                if transition != gst::StateChange::ReadyToNull {
                    let _ = source.set_state(gst::State::Null);
                    let mut state_guard = self.state.lock().unwrap();
                    let state = state_guard.as_mut().expect("no state");
                    self.handle_source_error(element, state);
                }
            }
            Ok(res) => {
                gst_debug!(
                    CAT,
                    obj: element,
                    "Source changed state successfully: {:?}",
                    res
                );
                // Remember if the source is live
                if transition == gst::StateChange::ReadyToPaused {
                    let mut state_guard = self.state.lock().unwrap();
                    let state = state_guard.as_mut().expect("no state");
                    state.source_is_live = res == gst::StateChangeSuccess::NoPreroll;
                }
            }
        }

        Ok(())
    }

    fn proxy_pad_chain(
        &self,
        element: &gst::Bin,
        pad: &gst::ProxyPad,
        buffer: gst::Buffer,
    ) -> Result<gst::FlowSuccess, gst::FlowError> {
        let res = gst::ProxyPad::chain_default(pad, Some(element), buffer);

        let mut state_guard = self.state.lock().unwrap();
        let state = match &mut *state_guard {
            None => return res,
            Some(state) => state,
        };

        state.flow_combiner.update_pad_flow(pad, res)
    }

    fn handle_source_pad_added(
        &self,
        element: &gst::Bin,
        pad: &gst::Pad,
    ) -> Result<(), gst::ErrorMessage> {
        gst_debug!(CAT, obj: element, "Pad {} added to source", pad.get_name(),);

        let mut state_guard = self.state.lock().unwrap();
        let state = match &mut *state_guard {
            None => {
                return Ok(());
            }
            Some(state) => state,
        };

        let (type_, stream) = match pad.get_name() {
            x if x.starts_with("audio_") => ("audio", &mut state.audio_stream),
            x if x.starts_with("video_") => ("video", &mut state.video_stream),
            _ => {
                let caps = match pad.get_current_caps().or_else(|| pad.query_caps(None)) {
                    Some(caps) if !caps.is_any() && !caps.is_empty() => caps,
                    _ => return Ok(()),
                };

                let s = caps.get_structure(0).unwrap();

                if s.get_name().starts_with("audio/") {
                    ("audio", &mut state.audio_stream)
                } else if s.get_name().starts_with("video/") {
                    ("video", &mut state.video_stream)
                } else {
                    // TODO: handle subtitles etc
                    return Ok(());
                }
            }
        };

        let stream = match stream {
            None => {
                gst_debug!(CAT, obj: element, "No {} stream enabled", type_);
                return Ok(());
            }
            Some(Stream {
                source_srcpad: Some(_),
                ..
            }) => {
                gst_debug!(CAT, obj: element, "Already configured a {} stream", type_);
                return Ok(());
            }
            Some(ref mut stream) => stream,
        };

        let sinkpad = stream.clocksync.get_static_pad("sink").unwrap();
        pad.link(&sinkpad).map_err(|err| {
            gst_error!(
                CAT,
                obj: element,
                "Failed to link source pad to clocksync: {}",
                err
            );
            gst_error_msg!(
                gst::CoreError::Negotiation,
                ["Failed to link source pad to clocksync: {}", err]
            )
        })?;

        stream.source_srcpad = Some(pad.clone());
        stream.source_srcpad_block = Some(self.add_pad_probe(element, pad));

        drop(state_guard);
        element.notify("status");

        Ok(())
    }

    fn add_pad_probe(&self, element: &gst::Bin, pad: &gst::Pad) -> Block {
        gst_debug!(CAT, obj: element, "Adding probe to pad {}", pad.get_name());

        let element_weak = element.downgrade();
        let probe_id = pad
            .add_probe(
                gst::PadProbeType::BLOCK
                    | gst::PadProbeType::BUFFER
                    | gst::PadProbeType::EVENT_DOWNSTREAM,
                move |pad, info| {
                    let element = match element_weak.upgrade() {
                        None => return gst::PadProbeReturn::Pass,
                        Some(element) => element,
                    };
                    let pts = match info.data {
                        Some(gst::PadProbeData::Buffer(ref buffer)) => buffer.get_pts(),
                        Some(gst::PadProbeData::Event(ref ev)) => match ev.view() {
                            gst::EventView::Gap(ref ev) => ev.get().0,
                            _ => return gst::PadProbeReturn::Pass,
                        },
                        _ => unreachable!(),
                    };

                    let src = FallbackSrc::from_instance(&element);

                    if let Err(msg) = src.handle_pad_blocked(&element, pad, pts) {
                        element.post_error_message(&msg);
                    }

                    gst::PadProbeReturn::Ok
                },
            )
            .unwrap();

        Block {
            pad: pad.clone(),
            probe_id,
            running_time: gst::CLOCK_TIME_NONE,
        }
    }

    fn handle_pad_blocked(
        &self,
        element: &gst::Bin,
        pad: &gst::Pad,
        pts: gst::ClockTime,
    ) -> Result<(), gst::ErrorMessage> {
        gst_debug!(CAT, obj: element, "Called probe on pad {}", pad.get_name());

        let mut state_guard = self.state.lock().unwrap();
        let state = match &mut *state_guard {
            None => {
                return Ok(());
            }
            Some(state) => state,
        };

        // Directly unblock for live streams
        if state.source_is_live {
            for block in [state.video_stream.as_mut(), state.audio_stream.as_mut()]
                .iter_mut()
                .filter_map(|s| s.as_mut())
                .filter_map(|s| s.source_srcpad_block.take())
            {
                block.pad.remove_probe(block.probe_id);
            }

            gst_debug!(CAT, obj: element, "Live source, unblocking directly");

            drop(state_guard);
            element.notify("status");

            return Ok(());
        }

        // Update running time for this block
        let stream = if let Some(stream) = state
            .audio_stream
            .as_mut()
            .filter(|s| s.source_srcpad.as_ref() == Some(pad))
        {
            stream
        } else if let Some(stream) = state
            .video_stream
            .as_mut()
            .filter(|s| s.source_srcpad.as_ref() == Some(pad))
        {
            stream
        } else {
            unreachable!();
        };

        let block = match stream.source_srcpad_block {
            Some(ref mut block) => block,
            None => return Ok(()),
        };

        let ev = pad
            .get_sticky_event(gst::EventType::Segment, 0)
            .ok_or_else(|| {
                gst_error!(CAT, obj: element, "Have no segment event");
                gst_error_msg!(gst::CoreError::Clock, ["Have no segment event"])
            })?;
        let segment = match ev.view() {
            gst::EventView::Segment(s) => s.get_segment(),
            _ => unreachable!(),
        };
        let segment = segment.downcast_ref::<gst::ClockTime>().ok_or_else(|| {
            gst_error!(CAT, obj: element, "Have no time segment");
            gst_error_msg!(gst::CoreError::Clock, ["Have no time segment"])
        })?;

        let running_time = if pts < segment.get_start() {
            segment.get_start()
        } else if segment.get_stop().is_some() && pts >= segment.get_stop() {
            segment.get_stop()
        } else {
            segment.to_running_time(pts)
        };

        assert!(running_time.is_some());

        gst_debug!(
            CAT,
            obj: element,
            "Have block running time {} for pad {}",
            running_time,
            pad.get_name()
        );

        block.running_time = running_time;

        self.unblock_pads(element, state);

        drop(state_guard);
        element.notify("status");

        Ok(())
    }

    fn unblock_pads(&self, element: &gst::Bin, state: &mut State) {
        // Check if all streams are blocked and have a running time and we have
        // 100% buffering
        if state.buffering_percent < 100 {
            gst_debug!(
                CAT,
                obj: element,
                "Not unblocking yet: buffering {}%",
                state.buffering_percent
            );
            return;
        }

        let streams = match state.streams {
            None => {
                gst_debug!(CAT, obj: element, "Have no stream collection yet");
                return;
            }
            Some(ref streams) => streams,
        };
        let mut have_audio = false;
        let mut have_video = false;
        for stream in streams.iter() {
            have_audio = have_audio || stream.get_stream_type().contains(gst::StreamType::AUDIO);
            have_video = have_video || stream.get_stream_type().contains(gst::StreamType::VIDEO);
        }

        let want_audio = state.settings.enable_audio;
        let want_video = state.settings.enable_video;

        let audio_running_time = state
            .audio_stream
            .as_ref()
            .and_then(|s| s.source_srcpad_block.as_ref().map(|b| b.running_time))
            .unwrap_or(gst::CLOCK_TIME_NONE);
        let video_running_time = state
            .video_stream
            .as_ref()
            .and_then(|s| s.source_srcpad_block.as_ref().map(|b| b.running_time))
            .unwrap_or(gst::CLOCK_TIME_NONE);

        let audio_srcpad = state
            .audio_stream
            .as_ref()
            .and_then(|s| s.source_srcpad.as_ref().cloned());
        let video_srcpad = state
            .video_stream
            .as_ref()
            .and_then(|s| s.source_srcpad.as_ref().cloned());

        let audio_is_eos = audio_srcpad
            .as_ref()
            .map(|p| p.get_pad_flags().contains(gst::PadFlags::EOS))
            .unwrap_or(false);
        let video_is_eos = video_srcpad
            .as_ref()
            .map(|p| p.get_pad_flags().contains(gst::PadFlags::EOS))
            .unwrap_or(false);

        // If we need both, wait for both and take the minimum, otherwise take the one we need.
        // Also consider EOS, we'd never get a new running time after EOS so don't need to wait.
        // FIXME: All this surely can be simplified somehow

        let current_running_time = element.get_current_running_time();

        if have_audio && want_audio && have_video && want_video {
            if audio_running_time.is_none() && !audio_is_eos {
                gst_debug!(CAT, obj: element, "Waiting for audio pad to block");
                return;
            }
            if video_running_time.is_none() && !video_is_eos {
                gst_debug!(CAT, obj: element, "Waiting for video pad to block");
                return;
            }

            let min_running_time = if audio_is_eos {
                video_running_time
            } else if video_is_eos {
                audio_running_time
            } else {
                std::cmp::min(audio_running_time, video_running_time)
            };
            let offset = if current_running_time > min_running_time {
                (current_running_time - min_running_time).unwrap() as i64
            } else {
                -((min_running_time - current_running_time).unwrap() as i64)
            };

            gst_debug!(
                CAT,
                obj: element,
                "Unblocking at {} with pad offset {} (audio: {} eos {}, video {} eos {})",
                current_running_time,
                offset,
                audio_running_time,
                audio_is_eos,
                video_running_time,
                video_is_eos,
            );

            if let Some(block) = state
                .audio_stream
                .as_mut()
                .and_then(|s| s.source_srcpad_block.take())
            {
                if !audio_is_eos {
                    block.pad.set_offset(offset);
                }
                block.pad.remove_probe(block.probe_id);
            }

            if let Some(block) = state
                .video_stream
                .as_mut()
                .and_then(|s| s.source_srcpad_block.take())
            {
                if !video_is_eos {
                    block.pad.set_offset(offset);
                }
                block.pad.remove_probe(block.probe_id);
            }
        } else if have_audio && want_audio {
            if audio_running_time.is_none() {
                gst_debug!(CAT, obj: element, "Waiting for audio pad to block");
                return;
            }

            let offset = if current_running_time > audio_running_time {
                (current_running_time - audio_running_time).unwrap() as i64
            } else {
                -((audio_running_time - current_running_time).unwrap() as i64)
            };

            gst_debug!(
                CAT,
                obj: element,
                "Unblocking at {} with pad offset {} (audio: {} eos {})",
                current_running_time,
                offset,
                audio_running_time,
                audio_is_eos
            );

            if let Some(block) = state
                .audio_stream
                .as_mut()
                .and_then(|s| s.source_srcpad_block.take())
            {
                if !audio_is_eos {
                    block.pad.set_offset(offset);
                }
                block.pad.remove_probe(block.probe_id);
            }
        } else if have_video && want_video {
            if video_running_time.is_none() {
                gst_debug!(CAT, obj: element, "Waiting for video pad to block");
                return;
            }

            let offset = if current_running_time > video_running_time {
                (current_running_time - video_running_time).unwrap() as i64
            } else {
                -((video_running_time - current_running_time).unwrap() as i64)
            };

            gst_debug!(
                CAT,
                obj: element,
                "Unblocking at {} with pad offset {} (video: {} eos {})",
                current_running_time,
                offset,
                video_running_time,
                video_is_eos
            );

            if let Some(block) = state
                .video_stream
                .as_mut()
                .and_then(|s| s.source_srcpad_block.take())
            {
                if !video_is_eos {
                    block.pad.set_offset(offset);
                }
                block.pad.remove_probe(block.probe_id);
            }
        }
    }

    fn handle_source_pad_removed(
        &self,
        element: &gst::Bin,
        pad: &gst::Pad,
    ) -> Result<(), gst::ErrorMessage> {
        gst_debug!(
            CAT,
            obj: element,
            "Pad {} removed from source",
            pad.get_name()
        );

        let mut state_guard = self.state.lock().unwrap();
        let state = match &mut *state_guard {
            None => {
                return Ok(());
            }
            Some(state) => state,
        };

        // Don't have to do anything here other than forgetting about the pad. Unlinking will
        // automatically happen while the pad is being removed from source and thus leaves the
        // bin hierarchy
        let stream = if let Some(stream) = state
            .audio_stream
            .as_mut()
            .filter(|s| s.source_srcpad.as_ref() == Some(pad))
        {
            stream
        } else if let Some(stream) = state
            .video_stream
            .as_mut()
            .filter(|s| s.source_srcpad.as_ref() == Some(pad))
        {
            stream
        } else {
            return Ok(());
        };

        stream.source_srcpad = None;

        self.unblock_pads(element, state);

        drop(state_guard);
        element.notify("status");

        Ok(())
    }

    fn handle_buffering(&self, element: &gst::Bin, m: &gst::message::Buffering) {
        let mut state_guard = self.state.lock().unwrap();
        let state = match &mut *state_guard {
            None => {
                return;
            }
            Some(state) => state,
        };

        gst_debug!(CAT, obj: element, "Got buffering {}%", m.get_percent());

        state.buffering_percent = m.get_percent() as u8;
        if state.buffering_percent < 100 {
            state.last_buffering_update = Some(Instant::now());
            // Block source pads if needed to pause
            if let Some(ref mut stream) = state.audio_stream {
                if stream.source_srcpad_block.is_none() {
                    if let Some(ref pad) = stream.source_srcpad {
                        stream.source_srcpad_block = Some(self.add_pad_probe(element, pad));
                    }
                }
            }
            if let Some(ref mut stream) = state.video_stream {
                if stream.source_srcpad_block.is_none() {
                    if let Some(ref pad) = stream.source_srcpad {
                        stream.source_srcpad_block = Some(self.add_pad_probe(element, pad));
                    }
                }
            }

            drop(state_guard);
            element.notify("status");
        } else {
            // Check if we can unblock now
            self.unblock_pads(element, state);

            drop(state_guard);
            element.notify("status");
        }
    }

    fn handle_streams_selected(&self, element: &gst::Bin, m: &gst::message::StreamsSelected) {
        let mut state_guard = self.state.lock().unwrap();
        let state = match &mut *state_guard {
            None => {
                return;
            }
            Some(state) => state,
        };

        let streams = m.get_stream_collection();

        gst_debug!(
            CAT,
            obj: element,
            "Got stream collection {:?}",
            streams.debug()
        );

        let mut have_audio = false;
        let mut have_video = false;
        for stream in streams.iter() {
            have_audio = have_audio || stream.get_stream_type().contains(gst::StreamType::AUDIO);
            have_video = have_video || stream.get_stream_type().contains(gst::StreamType::VIDEO);
        }

        if !have_audio && state.settings.enable_audio {
            gst_warning!(
                CAT,
                obj: element,
                "Have no audio streams but audio is enabled"
            );
        }

        if !have_video && state.settings.enable_video {
            gst_warning!(
                CAT,
                obj: element,
                "Have no video streams but video is enabled"
            );
        }

        state.streams = Some(streams);

        // This might not be the first stream collection and we might have some unblocked pads from
        // before already, which would need to be blocked again now for keeping things in sync
        for stream in [&mut state.video_stream, &mut state.audio_stream]
            .iter_mut()
            .filter_map(|v| v.as_mut())
        {
            if let Some(ref srcpad) = stream.source_srcpad {
                if stream.source_srcpad_block.is_none() {
                    stream.source_srcpad_block = Some(self.add_pad_probe(element, srcpad));
                }
            }
        }

        self.unblock_pads(element, state);

        drop(state_guard);
        element.notify("status");
    }

    fn handle_error(&self, element: &gst::Bin, m: &gst::message::Error) -> bool {
        let mut state_guard = self.state.lock().unwrap();
        let state = match &mut *state_guard {
            None => {
                return false;
            }
            Some(state) => state,
        };

        let src = match m.get_src().and_then(|s| s.downcast::<gst::Element>().ok()) {
            None => return false,
            Some(src) => src,
        };

        gst_debug!(
            CAT,
            obj: element,
            "Got error message from {}",
            src.get_path_string()
        );

        if src == state.source || src.has_as_ancestor(&state.source) {
            self.handle_source_error(element, state);
            drop(state_guard);
            element.notify("status");
            return true;
        }

        false
    }

    fn handle_source_error(&self, element: &gst::Bin, state: &mut State) {
        gst_debug!(CAT, obj: element, "Handling source error");
        if state.source_pending_restart {
            gst_debug!(CAT, obj: element, "Source is already pending restart");
            return;
        }

        // Unschedule pending timeout, we're restarting now
        if let Some(timeout) = state.source_pending_timeout.take() {
            timeout.unschedule();
        }

        // Prevent state changes from changing the state in an uncoordinated way
        state.source_pending_restart = true;

        // Drop any EOS events from any source pads of the source that might happen because of the
        // error. We don't need to remove these pad probes because restarting the source will also
        // remove/add the pads again.
        for pad in state.source.get_src_pads() {
            pad.add_probe(
                gst::PadProbeType::EVENT_DOWNSTREAM,
                |_pad, info| match info.data {
                    Some(gst::PadProbeData::Event(ref event)) => {
                        if event.get_type() == gst::EventType::Eos {
                            gst::PadProbeReturn::Drop
                        } else {
                            gst::PadProbeReturn::Ok
                        }
                    }
                    _ => unreachable!(),
                },
            )
            .unwrap();
        }

        let source_weak = state.source.downgrade();
        element.call_async(move |element| {
            let source = match source_weak.upgrade() {
                None => return,
                Some(source) => source,
            };

            gst_debug!(CAT, obj: element, "Shutting down source");
            let _ = source.set_state(gst::State::Null);

            // Sleep for 1s before retrying

            let src = FallbackSrc::from_instance(element);

            let mut state_guard = src.state.lock().unwrap();
            let state = match &mut *state_guard {
                None
                | Some(State {
                    source_pending_restart: false,
                    ..
                }) => {
                    gst_debug!(CAT, obj: element, "Restarting source not needed anymore");
                    return;
                }
                Some(state) => state,
            };
            gst_debug!(CAT, obj: element, "Waiting for 1s before retrying");
            let clock = gst::SystemClock::obtain();
            let wait_time = clock.get_time() + gst::SECOND;
            assert!(wait_time.is_some());
            assert!(state.source_pending_restart_timeout.is_none());

            let timeout = clock
                .new_single_shot_id(wait_time)
                .expect("can't create clock id");
            let element_weak = element.downgrade();
            timeout
                .wait_async(move |_clock, _time, _id| {
                    let element = match element_weak.upgrade() {
                        None => return,
                        Some(element) => element,
                    };

                    gst_debug!(CAT, obj: &element, "Woke up, retrying");
                    element.call_async(|element| {
                        let src = FallbackSrc::from_instance(element);

                        let mut state_guard = src.state.lock().unwrap();
                        let state = match &mut *state_guard {
                            None
                            | Some(State {
                                source_pending_restart: false,
                                ..
                            }) => {
                                gst_debug!(
                                    CAT,
                                    obj: element,
                                    "Restarting source not needed anymore"
                                );
                                return;
                            }
                            Some(state) => state,
                        };

                        let (source, old_source) = if let Source::Uri(..) = state.configured_source
                        {
                            // FIXME: Create a new uridecodebin3 because it currently is not reusable
                            // See https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/-/issues/746
                            element.remove(&state.source).unwrap();

                            let source = src
                                .create_main_input(element, &state.configured_source)
                                .expect("failed to create new source");

                            (
                                source.clone(),
                                Some(mem::replace(&mut state.source, source)),
                            )
                        } else {
                            (state.source.clone(), None)
                        };

                        state.source_pending_restart = false;
                        state.source_pending_restart_timeout = None;
                        state.buffering_percent = 100;
                        state.last_buffering_update = None;
                        drop(state_guard);

                        if let Some(old_source) = old_source {
                            // Drop old source after releasing the lock, it might call the pad-removed callback
                            // still
                            drop(old_source);
                        }

                        if source.sync_state_with_parent().is_err() {
                            gst_error!(CAT, obj: element, "Source failed to change state");
                            let _ = source.set_state(gst::State::Null);
                            let mut state_guard = src.state.lock().unwrap();
                            let state = state_guard.as_mut().expect("no state");
                            src.handle_source_error(element, state);
                        }
                    });
                })
                .expect("Failed to wait async");
            state.source_pending_restart_timeout = Some(timeout);
        });
    }

    #[allow(clippy::block_in_if_condition_stmt)]
    fn handle_switch_active_pad_change(&self, element: &gst::Bin) {
        let mut state_guard = self.state.lock().unwrap();
        let state = match &mut *state_guard {
            None => {
                return;
            }
            Some(state) => state,
        };

        let mut have_audio = false;
        let mut have_video = false;
        if let Some(ref streams) = state.streams {
            for stream in streams.iter() {
                have_audio =
                    have_audio || stream.get_stream_type().contains(gst::StreamType::AUDIO);
                have_video =
                    have_video || stream.get_stream_type().contains(gst::StreamType::VIDEO);
            }
        }

        // We will schedule a new one if needed below
        if let Some(timeout) = state.source_pending_timeout.take() {
            gst_debug!(CAT, obj: element, "Unscheduling pending timeout");
            timeout.unschedule();
        }

        // If we have neither audio nor video (no streams yet), or active pad for the ones we have
        // is the fallback pad then start the retry timeout unless it was started already.
        // Otherwise cancel the retry timeout.
        if (!have_audio && !have_video)
            || (have_audio
                && state
                    .audio_stream
                    .as_ref()
                    .and_then(|s| {
                        s.switch
                            .get_property("active-pad")
                            .unwrap()
                            .get::<gst::Pad>()
                            .unwrap()
                    })
                    .map(|p| p.get_name() == "fallback_sink")
                    .unwrap_or(true))
            || (have_video
                && state
                    .video_stream
                    .as_ref()
                    .and_then(|s| {
                        s.switch
                            .get_property("active-pad")
                            .unwrap()
                            .get::<gst::Pad>()
                            .unwrap()
                    })
                    .map(|p| p.get_name() == "fallback_sink")
                    .unwrap_or(true))
        {
            gst_warning!(CAT, obj: element, "Switched to fallback stream");

            // If we're not actively buffering right now let's restart the source
            if state
                .last_buffering_update
                .map(|i| i.elapsed() >= Duration::from_nanos(state.settings.timeout))
                .unwrap_or(state.buffering_percent == 100)
            {
                gst_debug!(CAT, obj: element, "Not buffering, restarting source");
                self.handle_source_error(element, state);
            } else {
                // Schedule another timeout after the last buffering activity
                let clock = gst::SystemClock::obtain();
                let diff = gst::ClockTime::from(
                    state.settings.timeout.saturating_sub(
                        state
                            .last_buffering_update
                            .map(|i| i.elapsed().as_nanos() as u64)
                            .unwrap_or(state.settings.timeout),
                    ),
                );
                let wait_time = clock.get_time() + diff;
                assert!(wait_time.is_some());

                gst_debug!(CAT, obj: element, "Starting pending timeout for {}", diff);
                let timeout = clock
                    .new_single_shot_id(wait_time)
                    .expect("can't create clock id");

                let element_weak = element.downgrade();
                timeout
                    .wait_async(move |_clock, _time, _id| {
                        let element = match element_weak.upgrade() {
                            None => return,
                            Some(element) => element,
                        };

                        element.call_async(|element| {
                            let src = FallbackSrc::from_instance(element);
                            src.handle_switch_active_pad_change(element);
                        });
                    })
                    .expect("failed to wait async");
            }

            if state.source_retry_timeout.is_none() {
                let clock = gst::SystemClock::obtain();
                let wait_time =
                    clock.get_time() + gst::ClockTime::from(state.settings.retry_timeout);
                assert!(wait_time.is_some());

                gst_debug!(CAT, obj: element, "Starting retry timeout");
                let timeout = clock
                    .new_single_shot_id(wait_time)
                    .expect("can't create clock id");

                let element_weak = element.downgrade();
                timeout
                    .wait_async(move |_clock, _time, _id| {
                        let element = match element_weak.upgrade() {
                            None => return,
                            Some(element) => element,
                        };

                        element.call_async(|element| {
                            let src = FallbackSrc::from_instance(element);
                            let mut state_guard = src.state.lock().unwrap();
                            let state = match &mut *state_guard {
                                None => return,
                                Some(ref mut state) => state,
                            };
                            if state.source_retry_timeout.take().is_none() {
                                return;
                            }
                            if let Some(timeout) = state.source_pending_restart_timeout.take() {
                                timeout.unschedule();
                            }
                            if let Some(timeout) = state.source_pending_timeout.take() {
                                timeout.unschedule();
                            }
                            state.source_pending_restart = false;
                            drop(state_guard);

                            gst_element_warning!(
                                element,
                                gst::ResourceError::OpenRead,
                                ["Failed to start playback"]
                            );
                            gst_warning!(CAT, obj: element, "Retry timeout, finishing");

                            for pad in element.get_src_pads() {
                                element.call_async(move |_element| {
                                    pad.push_event(gst::event::Eos::new());
                                });
                            }
                        });
                    })
                    .expect("failed to wait async");

                state.source_retry_timeout = Some(timeout);

                drop(state_guard);
                element.notify("status");
            }
        } else if let Some(timeout) = state.source_retry_timeout.take() {
            gst_debug!(CAT, obj: element, "Unscheduling retry timeout");
            timeout.unschedule();

            drop(state_guard);
            element.notify("status");
        }
    }
}

pub fn register(plugin: &gst::Plugin) -> Result<(), glib::BoolError> {
    gst::Element::register(
        Some(plugin),
        "fallbacksrc",
        gst::Rank::None,
        FallbackSrc::get_type(),
    )
}

mod custom_source {
    use super::CAT;
    use glib::prelude::*;
    use glib::subclass;
    use glib::subclass::prelude::*;
    use gst::prelude::*;
    use gst::subclass::prelude::*;

    use std::{mem, sync::Mutex};

    use once_cell::sync::OnceCell;

    static PROPERTIES: [subclass::Property; 1] = [subclass::Property("source", |name| {
        glib::ParamSpec::object(
            name,
            "Source",
            "Source",
            gst::Element::static_type(),
            glib::ParamFlags::WRITABLE | glib::ParamFlags::CONSTRUCT_ONLY,
        )
    })];

    struct Stream {
        source_pad: gst::Pad,
        ghost_pad: gst::Pad,
        // Dummy stream we created
        stream: gst::Stream,
    }

    struct State {
        pads: Vec<Stream>,
        num_audio: usize,
        num_video: usize,
    }

    pub struct CustomSource {
        source: OnceCell<gst::Element>,
        state: Mutex<State>,
    }

    impl ObjectSubclass for CustomSource {
        const NAME: &'static str = "FallbackSrcCustomSource";
        type ParentType = gst::Bin;
        type Instance = gst::subclass::ElementInstanceStruct<Self>;
        type Class = subclass::simple::ClassStruct<Self>;

        glib_object_subclass!();

        fn new() -> Self {
            Self {
                source: OnceCell::default(),
                state: Mutex::new(State {
                    pads: vec![],
                    num_audio: 0,
                    num_video: 0,
                }),
            }
        }

        fn class_init(klass: &mut subclass::simple::ClassStruct<Self>) {
            let src_pad_template = gst::PadTemplate::new(
                "audio_%u",
                gst::PadDirection::Src,
                gst::PadPresence::Sometimes,
                &gst::Caps::new_any(),
            )
            .unwrap();
            klass.add_pad_template(src_pad_template);

            let src_pad_template = gst::PadTemplate::new(
                "video_%u",
                gst::PadDirection::Src,
                gst::PadPresence::Sometimes,
                &gst::Caps::new_any(),
            )
            .unwrap();
            klass.add_pad_template(src_pad_template);
            klass.install_properties(&PROPERTIES);
        }
    }

    impl ObjectImpl for CustomSource {
        glib_object_impl!();

        fn set_property(&self, obj: &glib::Object, id: usize, value: &glib::Value) {
            let prop = &PROPERTIES[id];
            let element = obj.downcast_ref::<gst::Bin>().unwrap();

            match *prop {
                subclass::Property("source", ..) => {
                    let source = value.get::<gst::Element>().unwrap().unwrap();
                    self.source.set(source.clone()).unwrap();
                    element.add(&source).unwrap();
                }
                _ => unreachable!(),
            }
        }

        fn constructed(&self, obj: &glib::Object) {
            self.parent_constructed(obj);

            let bin = obj.downcast_ref::<gst::Bin>().unwrap();
            bin.set_suppressed_flags(gst::ElementFlags::SOURCE | gst::ElementFlags::SINK);
            bin.set_element_flags(gst::ElementFlags::SOURCE);
            bin.set_bin_flags(gst::BinFlags::STREAMS_AWARE);
        }
    }

    impl ElementImpl for CustomSource {
        #[allow(clippy::single_match)]
        fn change_state(
            &self,
            element: &gst::Element,
            transition: gst::StateChange,
        ) -> Result<gst::StateChangeSuccess, gst::StateChangeError> {
            let element = element.downcast_ref::<gst::Bin>().unwrap();

            match transition {
                gst::StateChange::NullToReady => {
                    self.start(element)?;
                }
                _ => (),
            }

            self.parent_change_state(element.upcast_ref(), transition)?;

            match transition {
                gst::StateChange::ReadyToNull => {
                    self.stop(element)?;
                    Ok(gst::StateChangeSuccess::Success)
                }
                _ => Ok(gst::StateChangeSuccess::Success),
            }
        }
    }

    impl BinImpl for CustomSource {
        #[allow(clippy::single_match)]
        fn handle_message(&self, bin: &gst::Bin, msg: gst::Message) {
            use gst::MessageView;

            match msg.view() {
                MessageView::StreamCollection(_) => {
                    // TODO: Drop stream collection message for now, we only create a simple custom
                    // one here so that fallbacksrc can know about our streams. It is never
                    // forwarded.
                    if let Err(msg) = self.handle_source_no_more_pads(&bin) {
                        bin.post_error_message(&msg);
                    }
                }
                _ => self.parent_handle_message(bin, msg),
            }
        }
    }

    impl CustomSource {
        fn start(
            &self,
            element: &gst::Bin,
        ) -> Result<gst::StateChangeSuccess, gst::StateChangeError> {
            gst_debug!(CAT, obj: element, "Starting");
            let source = self.source.get().unwrap();

            let templates = source.get_pad_template_list();

            if templates
                .iter()
                .any(|templ| templ.get_property_presence() == gst::PadPresence::Request)
            {
                gst_error!(CAT, obj: element, "Request pads not supported");
                gst_element_error!(
                    element,
                    gst::LibraryError::Settings,
                    ["Request pads not supported"]
                );
                return Err(gst::StateChangeError);
            }

            let has_sometimes_pads = templates
                .iter()
                .any(|templ| templ.get_property_presence() == gst::PadPresence::Sometimes);

            // Handle all source pads that already exist
            for pad in source.get_src_pads() {
                if let Err(msg) = self.handle_source_pad_added(&element, &pad) {
                    element.post_error_message(&msg);
                    return Err(gst::StateChangeError);
                }
            }

            if !has_sometimes_pads {
                if let Err(msg) = self.handle_source_no_more_pads(&element) {
                    element.post_error_message(&msg);
                    return Err(gst::StateChangeError);
                }
            } else {
                gst_debug!(CAT, obj: element, "Found sometimes pads");

                let element_weak = element.downgrade();
                source.connect_pad_added(move |_, pad| {
                    let element = match element_weak.upgrade() {
                        None => return,
                        Some(element) => element,
                    };
                    let src = CustomSource::from_instance(&element);

                    if let Err(msg) = src.handle_source_pad_added(&element, pad) {
                        element.post_error_message(&msg);
                    }
                });
                let element_weak = element.downgrade();
                source.connect_pad_removed(move |_, pad| {
                    let element = match element_weak.upgrade() {
                        None => return,
                        Some(element) => element,
                    };
                    let src = CustomSource::from_instance(&element);

                    if let Err(msg) = src.handle_source_pad_removed(&element, pad) {
                        element.post_error_message(&msg);
                    }
                });

                let element_weak = element.downgrade();
                source.connect_no_more_pads(move |_| {
                    let element = match element_weak.upgrade() {
                        None => return,
                        Some(element) => element,
                    };
                    let src = CustomSource::from_instance(&element);

                    if let Err(msg) = src.handle_source_no_more_pads(&element) {
                        element.post_error_message(&msg);
                    }
                });
            }

            Ok(gst::StateChangeSuccess::Success)
        }

        fn handle_source_pad_added(
            &self,
            element: &gst::Bin,
            pad: &gst::Pad,
        ) -> Result<(), gst::ErrorMessage> {
            gst_debug!(CAT, obj: element, "Source added pad {}", pad.get_name());

            let mut state = self.state.lock().unwrap();

            let mut stream_type = None;

            // Take stream type from stream-start event if we can
            if let Some(event) = pad.get_sticky_event(gst::EventType::StreamStart, 0) {
                if let gst::EventView::StreamStart(ev) = event.view() {
                    stream_type = ev.get_stream().map(|s| s.get_stream_type());
                }
            }

            // Otherwise from the caps
            if stream_type.is_none() {
                let caps = match pad.get_current_caps().or_else(|| pad.query_caps(None)) {
                    Some(caps) if !caps.is_any() && !caps.is_empty() => caps,
                    _ => {
                        gst_error!(CAT, obj: element, "Pad {} had no caps", pad.get_name());
                        return Err(gst_error_msg!(
                            gst::CoreError::Negotiation,
                            ["Pad had no caps"]
                        ));
                    }
                };

                let s = caps.get_structure(0).unwrap();

                if s.get_name().starts_with("audio/") {
                    stream_type = Some(gst::StreamType::AUDIO);
                } else if s.get_name().starts_with("video/") {
                    stream_type = Some(gst::StreamType::VIDEO);
                } else {
                    return Ok(());
                }
            }

            let stream_type = stream_type.unwrap();

            let (templ, name) = if stream_type.contains(gst::StreamType::AUDIO) {
                let name = format!("audio_{}", state.num_audio);
                state.num_audio += 1;
                (element.get_pad_template("audio_%u").unwrap(), name)
            } else {
                let name = format!("video_{}", state.num_video);
                state.num_video += 1;
                (element.get_pad_template("video_%u").unwrap(), name)
            };

            let ghost_pad = gst::GhostPad::builder_with_template(&templ, Some(&name))
                .build_with_target(pad)
                .unwrap();

            let stream = Stream {
                source_pad: pad.clone(),
                ghost_pad: ghost_pad.clone().upcast(),
                // TODO: We only add the stream type right now
                stream: gst::Stream::new(None, None, stream_type, gst::StreamFlags::NONE),
            };
            state.pads.push(stream);
            drop(state);

            ghost_pad.set_active(true).unwrap();
            element.add_pad(&ghost_pad).unwrap();

            Ok(())
        }

        fn handle_source_pad_removed(
            &self,
            element: &gst::Bin,
            pad: &gst::Pad,
        ) -> Result<(), gst::ErrorMessage> {
            gst_debug!(CAT, obj: element, "Source removed pad {}", pad.get_name());

            let mut state = self.state.lock().unwrap();
            let (i, stream) = match state
                .pads
                .iter()
                .enumerate()
                .find(|(_i, p)| &p.source_pad == pad)
            {
                None => return Ok(()),
                Some(v) => v,
            };

            let ghost_pad = stream.ghost_pad.clone();
            state.pads.remove(i);
            drop(state);

            ghost_pad.set_active(false).unwrap();
            let _ = element.remove_pad(&ghost_pad);

            Ok(())
        }

        fn handle_source_no_more_pads(&self, element: &gst::Bin) -> Result<(), gst::ErrorMessage> {
            gst_debug!(CAT, obj: element, "Source signalled no-more-pads");

            let state = self.state.lock().unwrap();
            let streams = state
                .pads
                .iter()
                .map(|p| p.stream.clone())
                .collect::<Vec<_>>();
            let collection = gst::StreamCollection::builder(None)
                .streams(&streams)
                .build();
            drop(state);

            element.no_more_pads();

            let _ = element.post_message(
                &gst::message::StreamsSelected::builder(&collection)
                    .src(element)
                    .build(),
            );

            Ok(())
        }

        fn stop(
            &self,
            element: &gst::Bin,
        ) -> Result<gst::StateChangeSuccess, gst::StateChangeError> {
            gst_debug!(CAT, obj: element, "Stopping");

            let mut state = self.state.lock().unwrap();
            let pads = mem::replace(&mut state.pads, vec![]);
            state.num_audio = 0;
            state.num_video = 0;
            drop(state);

            for pad in pads {
                let _ = element.remove_pad(&pad.ghost_pad);
            }

            Ok(gst::StateChangeSuccess::Success)
        }

        #[allow(clippy::new_ret_no_self)]
        pub fn new(source: &gst::Element) -> gst::Element {
            glib::Object::new(CustomSource::get_type(), &[("source", source)])
                .unwrap()
                .downcast::<gst::Element>()
                .unwrap()
        }
    }
}