summaryrefslogtreecommitdiffstats
path: root/dom/media/test/manifest.js
blob: be52d566ce082fd67d8efbc5499e7db536818567 (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
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
const { AppConstants } = SpecialPowers.ChromeUtils.importESModule(
  "resource://gre/modules/AppConstants.sys.mjs"
);

// In each list of tests below, test file types that are not supported should
// be ignored. To make sure tests respect that, we include a file of type
// "bogus/duh" in each list.

// Make sure to not touch navigator in here, since we want to push prefs that
// will affect the APIs it exposes, but the set of exposed APIs is determined
// when Navigator.prototype is created.  So if we touch navigator before pushing
// the prefs, the APIs it exposes will not take those prefs into account.  We
// work around this by using a navigator object from a different global for our
// UA string testing.
var gManifestNavigatorSource = document.documentElement.appendChild(
  document.createElement("iframe")
);
gManifestNavigatorSource.style.display = "none";
function manifestNavigator() {
  return gManifestNavigatorSource.contentWindow.navigator;
}

// Similarly, use a <video> element from a different global for canPlayType or
// other feature testing.  If we used one from our global and did so before our
// prefs are pushed, then we'd instantiate HTMLMediaElement.prototype before the
// prefs are pushed and APIs we expect to be on that object would not be there.
function manifestVideo() {
  return gManifestNavigatorSource.contentDocument.createElement("video");
}

// Need to get the server url composed with ip:port instead of mochi.test.
// Since we will provide the url to Exoplayer which cannot recognize the domain
// name "mochi.test".
let serverUrl = SpecialPowers.Services.prefs.getCharPref(
  "media.hls.server.url"
);
var gHLSTests = [
  {
    name: serverUrl + "/bipbop_4x3_variant.m3u8",
    type: "audio/x-mpegurl",
    duration: 20.0,
  },
];

// These are small test files, good for just seeing if something loads. We
// really only need one test file per backend here.
var gSmallTests = [
  { name: "small-shot.ogg", type: "audio/ogg", duration: 0.276 },
  { name: "small-shot.m4a", type: "audio/mp4", duration: 0.29 },
  { name: "small-shot.mp3", type: "audio/mpeg", duration: 0.27 },
  { name: "small-shot-mp3.mp4", type: "audio/mp4; codecs=mp3", duration: 0.34 },
  { name: "small-shot.flac", type: "audio/flac", duration: 0.197 },
  { name: "r11025_s16_c1-short.wav", type: "audio/x-wav", duration: 0.37 },
  {
    name: "320x240.ogv",
    type: "video/ogg",
    width: 320,
    height: 240,
    duration: 0.266,
    contentDuration: 0.133,
  },
  {
    name: "seek-short.webm",
    type: "video/webm",
    width: 320,
    height: 240,
    duration: 0.23,
  },
  {
    name: "vp9-short.webm",
    type: "video/webm",
    width: 320,
    height: 240,
    duration: 0.2,
  },
  {
    name: "detodos-short.opus",
    type: "audio/ogg; codecs=opus",
    duration: 0.22,
  },
  {
    name: "gizmo-short.mp4",
    type: "video/mp4",
    width: 560,
    height: 320,
    duration: 0.27,
  },
  { name: "flac-s24.flac", type: "audio/flac", duration: 4.04 },
  { name: "bogus.duh", type: "bogus/duh" },
];

var gFrameCountTests = [
  { name: "bipbop.mp4", type: "video/mp4", totalFrameCount: 297 },
  { name: "gizmo.mp4", type: "video/mp4", totalFrameCount: 166 },
  { name: "seek-short.webm", type: "video/webm", totalFrameCount: 8 },
  { name: "seek.webm", type: "video/webm", totalFrameCount: 120 },
  { name: "320x240.ogv", type: "video/ogg", totalFrameCount: 8 },
  { name: "av1.mp4", type: "video/mp4", totalFrameCount: 24 },
];

gSmallTests = gSmallTests.concat([
  { name: "sample.3gp", type: "video/3gpp", duration: 4.933 },
  { name: "sample.3g2", type: "video/3gpp2", duration: 4.933 },
]);

// Used by test_bug654550.html, for videoStats preference
var gVideoTests = [
  {
    name: "320x240.ogv",
    type: "video/ogg",
    width: 320,
    height: 240,
    duration: 0.266,
  },
  {
    name: "seek-short.webm",
    type: "video/webm",
    width: 320,
    height: 240,
    duration: 0.23,
  },
  { name: "bogus.duh", type: "bogus/duh" },
];

// Temp hack for trackIDs and captureStream() -- bug 1215769
var gLongerTests = [
  {
    name: "seek.webm",
    type: "video/webm",
    width: 320,
    height: 240,
    duration: 3.966,
  },
  {
    name: "gizmo.mp4",
    type: "video/mp4",
    width: 560,
    height: 320,
    duration: 5.56,
  },
];

// Used by test_progress to ensure we get the correct progress information
// during resource download.
var gProgressTests = [
  { name: "r11025_u8_c1.wav", type: "audio/x-wav", duration: 1.0, size: 11069 },
  { name: "big-short.wav", type: "audio/x-wav", duration: 1.11, size: 12366 },
  { name: "seek-short.ogv", type: "video/ogg", duration: 1.03, size: 79921 },
  {
    name: "320x240.ogv",
    type: "video/ogg",
    width: 320,
    height: 240,
    duration: 0.266,
    size: 28942,
  },
  { name: "seek-short.webm", type: "video/webm", duration: 0.23, size: 19267 },
  { name: "gizmo-short.mp4", type: "video/mp4", duration: 0.27, size: 29905 },
  { name: "bogus.duh", type: "bogus/duh" },
];

// Used by test_played.html
var gPlayedTests = [
  { name: "big-short.wav", type: "audio/x-wav", duration: 1.11 },
  { name: "seek-short.ogv", type: "video/ogg", duration: 1.03 },
  { name: "seek-short.webm", type: "video/webm", duration: 0.23 },
  { name: "gizmo-short.mp4", type: "video/mp4", duration: 0.27 },
  { name: "owl-short.mp3", type: "audio/mpeg", duration: 0.52 },
  { name: "very-short.mp3", type: "audio/mpeg", duration: 0.07 },
  // Disable vbr.mp3 to see if it reduces the error of AUDCLNT_E_CPUUSAGE_EXCEEDED.
  // See bug 1110922 comment 26.
  //{ name:"vbr.mp3", type:"audio/mpeg", duration:10.0 },
  { name: "bug495794.ogg", type: "audio/ogg", duration: 0.3 },
];

if (
  manifestNavigator().userAgent.includes("Windows") &&
  manifestVideo().canPlayType('video/mp4; codecs="avc1.64000c"')
) {
  gPlayedTests = gPlayedTests.concat(
    { name: "red-46x48.mp4", type: "video/mp4", duration: 1.0 },
    { name: "red-48x46.mp4", type: "video/mp4", duration: 1.0 }
  );
}

// Used by test_mozLoadFrom.  Need one test file per decoder backend, plus
// anything for testing clone-specific bugs.
var cloneKey = Math.floor(Math.random() * 100000000);
var gCloneTests = [
  // short-video is more like 1s, so if you load this twice you'll get an unexpected duration
  {
    name:
      "dynamic_resource.sjs?key=" +
      cloneKey +
      "&res1=320x240.ogv&res2=short-video.ogv",
    type: "video/ogg",
    duration: 0.266,
  },
];

// Used by test_play_twice.  Need one test file per decoder backend, plus
// anything for testing bugs that occur when replying a played file.
var gReplayTests = gSmallTests.concat([
  { name: "bug533822.ogg", type: "audio/ogg" },
]);

// Used by test_paused_after_ended. Need one test file per decoder backend, plus
// anything for testing bugs that occur when replying a played file.
var gPausedAfterEndedTests = gSmallTests.concat([
  { name: "r11025_u8_c1.wav", type: "audio/x-wav", duration: 1.0 },
  { name: "small-shot.ogg", type: "video/ogg", duration: 0.276 },
]);

// Test the mozHasAudio property, and APIs that detect different kinds of
// tracks
var gTrackTests = [
  {
    name: "big-short.wav",
    type: "audio/x-wav",
    duration: 1.11,
    size: 12366,
    hasAudio: true,
    hasVideo: false,
  },
  {
    name: "320x240.ogv",
    type: "video/ogg",
    width: 320,
    height: 240,
    duration: 0.266,
    size: 28942,
    hasAudio: false,
    hasVideo: true,
  },
  {
    name: "short-video.ogv",
    type: "video/ogg",
    duration: 1.081,
    hasAudio: true,
    hasVideo: true,
  },
  {
    name: "seek-short.webm",
    type: "video/webm",
    duration: 0.23,
    size: 19267,
    hasAudio: false,
    hasVideo: true,
  },
  {
    name: "flac-s24.flac",
    type: "audio/flac",
    duration: 4.04,
    hasAudio: true,
    hasVideo: false,
  },
  { name: "bogus.duh", type: "bogus/duh" },
];

var gClosingConnectionsTest = [
  { name: "seek-short.ogv", type: "video/ogg", duration: 1.03 },
];

// Used by any media recorder test. Need one test file per decoder backend
// currently supported by the media encoder.
var gMediaRecorderTests = [
  // Duration should be greater than 500ms because we will record 2
  // time slices (250ms per slice)
  {
    name: "detodos-recorder-test.opus",
    type: "audio/ogg; codecs=opus",
    duration: 0.62,
  },
];

// Used by video media recorder tests
var gMediaRecorderVideoTests = [
  {
    name: "seek-short.webm",
    type: "video/webm",
    width: 320,
    height: 240,
    duration: 0.23,
  },
];

// These are files that we want to make sure we can play through.  We can
// also check metadata.  Put files of the same type together in this list so if
// something crashes we have some idea of which backend is responsible.
// Used by test_playback, which expects no error event and one ended event.
var gPlayTests = [
  // Test playback of a WebM file with vp9 video
  { name: "vp9cake-short.webm", type: "video/webm", duration: 1.0 },
  // 8-bit samples
  { name: "r11025_u8_c1.wav", type: "audio/x-wav", duration: 1.0 },
  // 8-bit samples, file is truncated
  { name: "r11025_u8_c1_trunc.wav", type: "audio/x-wav", duration: 1.8 },
  // file has trailing non-PCM data
  { name: "r11025_s16_c1_trailing.wav", type: "audio/x-wav", duration: 1.0 },
  // file with list chunk
  { name: "r16000_u8_c1_list.wav", type: "audio/x-wav", duration: 4.2 },
  // file with 2 extra bytes of metadata
  {
    name: "16bit_wave_extrametadata.wav",
    type: "audio/x-wav",
    duration: 1.108,
  },
  // IEEE float wave file
  { name: "wavedata_float.wav", type: "audio/x-wav", duration: 1.0 },
  // 24-bit samples
  { name: "wavedata_s24.wav", type: "audio/x-wav", duration: 1.0 },
  // aLaw compressed wave file
  { name: "wavedata_alaw.wav", type: "audio/x-wav", duration: 1.0 },
  // uLaw compressed wave file
  { name: "wavedata_ulaw.wav", type: "audio/x-wav", duration: 1.0 },
  // Data length 0xFFFFFFFF
  { name: "bug1301226.wav", type: "audio/x-wav", duration: 0.003673 },
  // Data length 0xFFFFFFFF and odd chunk lengths.
  { name: "bug1301226-odd.wav", type: "audio/x-wav", duration: 0.003673 },

  // Ogg stream without eof marker
  { name: "bug461281.ogg", type: "application/ogg", duration: 2.208 },

  // oggz-chop stream
  { name: "bug482461.ogv", type: "video/ogg", duration: 4.34 },
  // Theora only oggz-chop stream
  { name: "bug482461-theora.ogv", type: "video/ogg", duration: 4.138 },
  // With first frame a "duplicate" (empty) frame.
  {
    name: "bug500311.ogv",
    type: "video/ogg",
    duration: 1.96,
    contentDuration: 1.958,
  },
  // Small audio file
  { name: "small-shot.ogg", type: "audio/ogg", duration: 0.276 },
  // More audio in file than video.
  { name: "short-video.ogv", type: "video/ogg", duration: 1.081 },
  // First Theora data packet is zero bytes.
  { name: "bug504613.ogv", type: "video/ogg", duration: Number.NaN },
  // Multiple audio streams.
  { name: "bug516323.ogv", type: "video/ogg", duration: 4.208 },
  // oggz-chop with non-keyframe as first frame
  {
    name: "bug556821.ogv",
    type: "video/ogg",
    duration: 2.936,
    contentDuration: 2.903,
  },

  // Encoded with vorbis beta1, includes unusually sized codebooks
  { name: "beta-phrasebook.ogg", type: "audio/ogg", duration: 4.01 },
  // Small file, only 1 frame with audio only.
  { name: "bug520493.ogg", type: "audio/ogg", duration: 0.458 },
  // Small file with vorbis comments with 0 length values and names.
  { name: "bug520500.ogg", type: "audio/ogg", duration: 0.123 },

  // Various weirdly formed Ogg files
  {
    name: "bug499519.ogv",
    type: "video/ogg",
    duration: 0.24,
    contentDuration: 0.22,
  },
  { name: "bug506094.ogv", type: "video/ogg", duration: 0 },
  { name: "bug498855-1.ogv", type: "video/ogg", duration: 0.24 },
  { name: "bug498855-2.ogv", type: "video/ogg", duration: 0.24 },
  { name: "bug498855-3.ogv", type: "video/ogg", duration: 0.24 },
  {
    name: "bug504644.ogv",
    type: "video/ogg",
    duration: 1.6,
    contentDuration: 1.52,
  },
  {
    name: "chain.ogv",
    type: "video/ogg",
    duration: Number.NaN,
    contentDuration: 0.266,
  },
  {
    name: "bug523816.ogv",
    type: "video/ogg",
    duration: 0.766,
    contentDuration: 0,
  },
  { name: "bug495129.ogv", type: "video/ogg", duration: 2.41 },
  {
    name: "bug498380.ogv",
    type: "video/ogg",
    duration: 0.7663,
    contentDuration: 0,
  },
  { name: "bug495794.ogg", type: "audio/ogg", duration: 0.3 },
  { name: "bug557094.ogv", type: "video/ogg", duration: 0.24 },
  { name: "multiple-bos.ogg", type: "video/ogg", duration: 0.431 },
  { name: "audio-overhang.ogg", type: "video/ogg", duration: 2.3 },
  { name: "video-overhang.ogg", type: "video/ogg", duration: 3.966 },

  // bug461281.ogg with the middle second chopped out.
  { name: "audio-gaps.ogg", type: "audio/ogg", duration: 2.208 },

  // Test playback/metadata work after a redirect
  {
    name: "redirect.sjs?domain=mochi.test:8888&file=320x240.ogv",
    type: "video/ogg",
    duration: 0.266,
  },

  // Test playback of a webm file
  { name: "seek-short.webm", type: "video/webm", duration: 0.23 },

  // Test playback of a webm file with 'matroska' doctype
  { name: "bug1377278.webm", type: "video/webm", duration: 4.0 },

  // Test playback of a WebM file with non-zero start time.
  { name: "split.webm", type: "video/webm", duration: 1.967 },

  // Test playback of a WebM file with resolution changes.
  { name: "resolution-change.webm", type: "video/webm", duration: 6.533 },

  // The following webm files test cases where the webm metadata dimensions do
  // not match those in the stream. See bug 1695033 for more info.

  // Reference file with correct dimensions (webm metadata matches stream
  // resolution).
  { name: "bipbop_short_vp8.webm", type: "video/webm", duration: 1.011 },

  // The webm resolution is greater in both dimensions than the in stream
  // resolution.
  {
    name: "bipbop_short_pixel_metadata_bigger_than_in_stream_vp8.webm",
    type: "video/webm",
    duration: 1.011,
  },

  // The webm resolution is correct for height, but is narrower than the stream
  // resolution.
  {
    name: "bipbop_short_pixel_metadata_narrower_than_in_stream_vp8.webm",
    type: "video/webm",
    duration: 1.011,
  },

  // The webm resolution is smaller in both dimensions than the in stream
  // resolution.
  {
    name: "bipbop_short_pixel_metadata_smaller_than_in_stream_vp8.webm",
    type: "video/webm",
    duration: 1.011,
  },

  // End of webm dimension clashing files.

  // A really short, low sample rate, single channel file. This tests whether
  // we can handle playing files when only push very little audio data to the
  // hardware.
  { name: "spacestorm-1000Hz-100ms.ogg", type: "audio/ogg", duration: 0.099 },

  // Opus data in an ogg container
  {
    name: "detodos-short.opus",
    type: "audio/ogg; codecs=opus",
    duration: 0.22,
    contentDuration: 0.2135,
  },
  // Opus data in a webm container
  {
    name: "detodos-short.webm",
    type: "audio/webm; codecs=opus",
    duration: 0.26,
    contentDuration: 0.2535,
  },
  // Opus in webm channel mapping=2 sample file
  {
    name: "opus-mapping2.webm",
    type: "audio/webm; codecs=opus",
    duration: 10.01,
    contentDuration: 9.99,
  },
  { name: "bug1066943.webm", type: "audio/webm; codecs=opus", duration: 1.383 },

  // Multichannel Opus in an ogg container
  { name: "test-1-mono.opus", type: "audio/ogg; codecs=opus", duration: 1.044 },
  {
    name: "test-2-stereo.opus",
    type: "audio/ogg; codecs=opus",
    duration: 2.925,
  },
  { name: "test-3-LCR.opus", type: "audio/ogg; codecs=opus", duration: 4.214 },
  { name: "test-4-quad.opus", type: "audio/ogg; codecs=opus", duration: 6.234 },
  { name: "test-5-5.0.opus", type: "audio/ogg; codecs=opus", duration: 7.558 },
  { name: "test-6-5.1.opus", type: "audio/ogg; codecs=opus", duration: 10.333 },
  { name: "test-7-6.1.opus", type: "audio/ogg; codecs=opus", duration: 11.69 },
  { name: "test-8-7.1.opus", type: "audio/ogg; codecs=opus", duration: 13.478 },

  {
    name: "gizmo-short.mp4",
    type: "video/mp4",
    duration: 0.27,
    contentDuration: 0.267,
  },
  // Test playback of a MP4 file with a non-zero start time (and audio starting
  // a second later).
  { name: "bipbop-lateaudio.mp4", type: "video/mp4" },
  // Opus in MP4 channel mapping=0 sample file (content shorter due to preskip)
  {
    name: "opus-sample.mp4",
    type: "audio/mp4; codecs=opus",
    duration: 10.92,
    contentDuration: 10.09,
  },
  // Opus in MP4 channel mapping=2 sample file
  { name: "opus-mapping2.mp4", type: "audio/mp4; codecs=opus", duration: 10.0 },

  { name: "small-shot.m4a", type: "audio/mp4", duration: 0.29 },
  { name: "small-shot.mp3", type: "audio/mpeg", duration: 0.27 },
  { name: "owl.mp3", type: "audio/mpeg", duration: 3.343 },
  // owl.mp3 as above, but with something funny going on in the ID3v2 tag
  // that caused DirectShow to fail.
  { name: "owl-funny-id3.mp3", type: "audio/mpeg", duration: 3.343 },
  // owl.mp3 as above, but with something even funnier going on in the ID3v2 tag
  // that caused DirectShow to fail.
  { name: "owl-funnier-id3.mp3", type: "audio/mpeg", duration: 3.343 },
  // One second of silence with ~140KB of ID3 tags. Usually when the first MP3
  // frame is at such a high offset into the file, MP3FrameParser will give up
  // and report that the stream is not MP3. However, it does not count ID3 tags
  // in that offset. This test case makes sure that ID3 exclusion holds.
  { name: "huge-id3.mp3", type: "audio/mpeg", duration: 1.0 },
  // Half a second file of a sine with a large ID3v2 tag, followed by an ID3v1
  // tag. The ID3v1 tags should be at the end of the file, but software usually
  // play it anyway.
  { name: "id3v1afterlongid3v2.mp3", type: "audio/mpeg", duration: 0.5 },
  // An VBR file with a padding value that is greater than an mp3 packet, and
  // also subsequent packets after the theoretical EOF computed from metadata,
  // to test padding trimming edge cases.
  {
    name: "padding-spanning-multiple-packets.mp3",
    type: "audio/mpeg",
  },
  // A truncated VBR MP3 with just enough frames to keep most decoders happy.
  // The Xing header reports the length of the file to be around 10 seconds, but
  // there is really only one second worth of data. We want MP3FrameParser to
  // trust the header, so this should be reported as 10 seconds.
  {
    name: "vbr-head.mp3",
    type: "audio/mpeg",
    duration: 10.0,
    contentDuration: 1.019,
  },

  // A flac file where the STREAMINFO block was removed.
  // It is necessary to parse the file to find an audio frame instead.
  { name: "flac-noheader-s16.flac", type: "audio/flac", duration: 4.0 },
  { name: "flac-s24.flac", type: "audio/flac", duration: 4.04 },
  {
    name: "flac-sample.mp4",
    type: "audio/mp4; codecs=flac",
    duration: 4.95,
    contentDuration: 5.03,
  },
  // Ogg with theora video and flac audio.
  {
    name: "A4.ogv",
    type: "video/ogg",
    width: 320,
    height: 240,
    duration: 3.13,
  },
  // A file that has no codec delay at the container level, but has a delay at
  // the codec level.
  {
    name: "no-container-codec-delay.webm",
    type: "video/webm",
  },
  // A file that has a codec delay at a container level of 0, but as a delay at
  // the codec level that is non-zero.
  { name: "invalid-preskip.webm", type: "audio/webm; codecs=opus" },

  // Invalid file
  { name: "bogus.duh", type: "bogus/duh", duration: Number.NaN },
];

const win32 =
  SpecialPowers.Services.appinfo.OS == "WINNT" &&
  !SpecialPowers.Services.appinfo.is64Bit;
if (!win32) {
  gPlayTests.push({ name: "av1.mp4", type: "video/mp4", duration: 1.0 });
}

// AAC files with different sample rates. We add these here as some are added
// conditionally.
gPlayTests.push(
  {
    name: "bipbop_audio_aac_8k.mp4",
    type: "audio/mp4",
    duration: 1.06,
  },
  {
    name: "bipbop_audio_aac_22.05k.mp4",
    type: "audio/mp4",
    duration: 1.06,
  },
  {
    name: "bipbop_audio_aac_44.1k.mp4",
    type: "audio/mp4",
    duration: 1.06,
  },
  {
    name: "bipbop_audio_aac_48k.mp4",
    type: "audio/mp4",
    duration: 1.06,
  }
);
if (AppConstants.platform != "win") {
  // Windows WMF decoder doesn't do >48K everywhere. See bug 1698639.
  gPlayTests.push(
    {
      name: "bipbop_audio_aac_88.2k.mp4",
      type: "audio/mp4",
      duration: 1.06,
    },
    {
      name: "bipbop_audio_aac_96k.mp4",
      type: "audio/mp4",
      duration: 1.06,
    }
  );
}

// ambisonics.mp4 causes intermittents, so we conditionally add it until we fix
// the root cause.
const skipAmbisonics =
  // Bug 1484451 - skip on mac debug
  (AppConstants.platform == "macosx" && AppConstants.DEBUG) ||
  // Bug 1483259 - skip on linux64 opt
  (AppConstants.platform == "linux" &&
    !AppConstants.DEBUG &&
    SpecialPowers.Services.appinfo.is64Bit);
if (!skipAmbisonics) {
  // Ambisonics AAC, requires AAC extradata to be set when creating decoder (see bug 1431169)
  // Also test 4.0 decoding.
  gPlayTests.push({
    name: "ambisonics.mp4",
    type: "audio/mp4",
    duration: 16.48,
  });
}

var gSeekToNextFrameTests = [
  // Test playback of a WebM file with vp9 video
  { name: "vp9-short.webm", type: "video/webm", duration: 0.2 },
  { name: "vp9cake-short.webm", type: "video/webm", duration: 1.0 },
  // oggz-chop stream
  { name: "bug482461.ogv", type: "video/ogg", duration: 4.34 },
  // Theora only oggz-chop stream
  { name: "bug482461-theora.ogv", type: "video/ogg", duration: 4.138 },
  // With first frame a "duplicate" (empty) frame.
  { name: "bug500311.ogv", type: "video/ogg", duration: 1.96 },

  // More audio in file than video.
  { name: "short-video.ogv", type: "video/ogg", duration: 1.081 },
  // First Theora data packet is zero bytes.
  { name: "bug504613.ogv", type: "video/ogg", duration: Number.NaN },
  // Multiple audio streams.
  { name: "bug516323.ogv", type: "video/ogg", duration: 4.208 },
  // oggz-chop with non-keyframe as first frame
  { name: "bug556821.ogv", type: "video/ogg", duration: 2.936 },
  // Various weirdly formed Ogg files
  { name: "bug498855-1.ogv", type: "video/ogg", duration: 0.24 },
  { name: "bug498855-2.ogv", type: "video/ogg", duration: 0.24 },
  { name: "bug498855-3.ogv", type: "video/ogg", duration: 0.24 },
  { name: "bug504644.ogv", type: "video/ogg", duration: 1.6 },

  { name: "bug523816.ogv", type: "video/ogg", duration: 0.766 },

  { name: "bug498380.ogv", type: "video/ogg", duration: 0.2 },
  { name: "bug557094.ogv", type: "video/ogg", duration: 0.24 },
  { name: "multiple-bos.ogg", type: "video/ogg", duration: 0.431 },
  // Test playback/metadata work after a redirect
  {
    name: "redirect.sjs?domain=mochi.test:8888&file=320x240.ogv",
    type: "video/ogg",
    duration: 0.266,
  },
  // Test playback of a webm file
  { name: "seek-short.webm", type: "video/webm", duration: 0.23 },
  // Test playback of a WebM file with non-zero start time.
  { name: "split.webm", type: "video/webm", duration: 1.967 },

  { name: "gizmo-short.mp4", type: "video/mp4", duration: 0.27 },

  // Test playback of a MP4 file with a non-zero start time (and audio starting
  // a second later).
  { name: "bipbop-lateaudio.mp4", type: "video/mp4" },
];

// A file for each type we can support.
var gSnifferTests = [
  { name: "big.wav", type: "audio/x-wav", duration: 9.278982, size: 102444 },
  {
    name: "320x240.ogv",
    type: "video/ogg",
    width: 320,
    height: 240,
    duration: 0.233,
    size: 28942,
  },
  { name: "seek.webm", type: "video/webm", duration: 3.966, size: 215529 },
  { name: "gizmo.mp4", type: "video/mp4", duration: 5.56, size: 383631 },
  // A mp3 file with id3 tags.
  { name: "id3tags.mp3", type: "audio/mpeg", duration: 0.28, size: 3530 },
  { name: "bogus.duh", type: "bogus/duh" },
];

// Files that contain resolution changes
var gResolutionChangeTests = [
  { name: "resolution-change.webm", type: "video/webm", duration: 6.533 },
];

// Files we must reject as invalid.
var gInvalidTests = [
  { name: "invalid-m0c0.opus", type: "audio/ogg; codecs=opus" },
  { name: "invalid-m0c3.opus", type: "audio/ogg; codecs=opus" },
  { name: "invalid-m1c0.opus", type: "audio/ogg; codecs=opus" },
  { name: "invalid-m1c9.opus", type: "audio/ogg; codecs=opus" },
  { name: "invalid-m2c0.opus", type: "audio/ogg; codecs=opus" },
  { name: "invalid-m2c1.opus", type: "audio/ogg; codecs=opus" },
  { name: "invalid-cmap-short.opus", type: "audio/ogg; codecs=opus" },
  { name: "invalid-cmap-s0c0.opus", type: "audio/ogg; codecs=opus" },
  { name: "invalid-cmap-s0c2.opus", type: "audio/ogg; codecs=opus" },
  { name: "invalid-cmap-s1c2.opus", type: "audio/ogg; codecs=opus" },
];

var gInvalidPlayTests = [
  { name: "invalid-excess_discard.webm", type: "audio/webm; codecs=opus" },
  { name: "invalid-excess_neg_discard.webm", type: "audio/webm; codecs=opus" },
  { name: "invalid-neg_discard.webm", type: "audio/webm; codecs=opus" },
  {
    name: "invalid-discard_on_multi_blocks.webm",
    type: "audio/webm; codecs=opus",
  },
];

// Files to check different cases of ogg skeleton information.
// sample-fisbone-skeleton4.ogv
// - Skeleton v4, w/ Content-Type,Role,Name,Language,Title for both theora/vorbis
// sample-fisbone-wrong-header.ogv
// - Skeleton v4, wrong message field sequence for vorbis
// multiple-bos-more-header-fields.ogg
// - Skeleton v3, w/ Content-Type,Role,Name,Language,Title for both theora/vorbis
// seek-short.ogv
// - No skeleton, but theora
// audio-gaps-short.ogg
// - No skeleton, but vorbis
var gMultitrackInfoOggPlayList = [
  { name: "sample-fisbone-skeleton4.ogv", type: "video/ogg", duration: 1.0 },
  { name: "sample-fisbone-wrong-header.ogv", type: "video/ogg", duration: 1.0 },
  {
    name: "multiple-bos-more-header-fileds.ogg",
    type: "video/ogg",
    duration: 0.431,
  },
  { name: "seek-short.ogv", type: "video/ogg", duration: 1.03 },
  { name: "audio-gaps-short.ogg", type: "audio/ogg", duration: 0.5 },
];
// Pre-parsed results of gMultitrackInfoOggPlayList.
var gOggTrackInfoResults = {
  "sample-fisbone-skeleton4.ogv": {
    audio_id: " audio_1",
    audio_kind: "main",
    audio_language: " en-US",
    audio_label: " Audio track for test",
    video_id: " video_1",
    video_kind: "main",
    video_language: " fr",
    video_label: " Video track for test",
  },
  "sample-fisbone-wrong-header.ogv": {
    audio_id: "1",
    audio_kind: "main",
    audio_language: "",
    audio_label: "",
    video_id: " video_1",
    video_kind: "main",
    video_language: " fr",
    video_label: " Video track for test",
  },
  "multiple-bos-more-header-fileds.ogg": {
    audio_id: "1",
    audio_kind: "main",
    audio_language: "",
    audio_label: "",
    video_id: "2",
    video_kind: "main",
    video_language: "",
    video_label: "",
  },
  "seek-short.ogv": {
    video_id: "2",
    video_kind: "main",
    video_language: "",
    video_label: "",
  },
  "audio-gaps-short.ogg": {
    audio_id: "1",
    audio_kind: "main",
    audio_language: "",
    audio_label: "",
  },
};

// Returns a promise that resolves to a function that converts
// relative paths to absolute, to test loading files from file: URIs.
// Optionally checks whether the file actually exists on disk at the location
// we've specified.
function makeAbsolutePathConverter() {
  const url = SimpleTest.getTestFileURL("chromeHelper.js");
  const script = SpecialPowers.loadChromeScript(url);
  return new Promise((resolve, reject) => {
    script.addMessageListener("media-test:cwd", cwd => {
      if (!cwd) {
        ok(false, "Failed to find path to test files");
      }

      resolve((path, mustExist) => {
        // android mochitest doesn't support file://
        if (manifestNavigator().appVersion.includes("Android")) {
          return path;
        }

        const { Ci, Cc } = SpecialPowers;
        var f = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
        f.initWithPath(cwd);
        var split = path.split("/");
        for (var i = 0; i < split.length; ++i) {
          f.append(split[i]);
        }
        if (mustExist && !f.exists()) {
          ok(false, "We expected '" + path + "' to exist, but it doesn't!");
        }
        return f.path;
      });
    });
    script.sendAsyncMessage("media-test:getcwd");
  });
}

// Returns true if two TimeRanges are equal, false otherwise
function range_equals(r1, r2) {
  if (r1.length != r2.length) {
    return false;
  }
  for (var i = 0; i < r1.length; i++) {
    if (r1.start(i) != r2.start(i) || r1.end(i) != r2.end(i)) {
      return false;
    }
  }
  return true;
}

// These are URIs to files that we use to check that we don't leak any state
// or other information such that script can determine stuff about a user's
// environment. Used by test_info_leak.
function makeInfoLeakTests() {
  return makeAbsolutePathConverter().then(fileUriToSrc => [
    {
      type: "video/ogg",
      src: fileUriToSrc("tests/dom/media/test/320x240.ogv", true),
    },
    {
      type: "video/ogg",
      src: fileUriToSrc("tests/dom/media/test/404.ogv", false),
    },
    {
      type: "audio/x-wav",
      src: fileUriToSrc("tests/dom/media/test/r11025_s16_c1.wav", true),
    },
    {
      type: "audio/x-wav",
      src: fileUriToSrc("tests/dom/media/test/404.wav", false),
    },
    {
      type: "audio/ogg",
      src: fileUriToSrc("tests/dom/media/test/bug461281.ogg", true),
    },
    {
      type: "audio/ogg",
      src: fileUriToSrc("tests/dom/media/test/404.ogg", false),
    },
    {
      type: "video/webm",
      src: fileUriToSrc("tests/dom/media/test/seek.webm", true),
    },
    {
      type: "video/webm",
      src: fileUriToSrc("tests/dom/media/test/404.webm", false),
    },
    {
      type: "video/ogg",
      src: "http://localhost/404.ogv",
    },
    {
      type: "audio/x-wav",
      src: "http://localhost/404.wav",
    },
    {
      type: "video/webm",
      src: "http://localhost/404.webm",
    },
    {
      type: "video/ogg",
      src: "http://example.com/tests/dom/media/test/test_info_leak.html",
    },
    {
      type: "audio/ogg",
      src: "http://example.com/tests/dom/media/test/test_info_leak.html",
    },
  ]);
}

// These are files that must fire an error during load or playback, and do not
// cause a crash. Used by test_playback_errors, which expects one error event
// and no ended event. Put files of the same type together in this list so if
// something crashes we have some idea of which backend is responsible.
var gErrorTests = [
  { name: "bogus.wav", type: "audio/x-wav" },
  { name: "bogus.ogv", type: "video/ogg" },
  { name: "448636.ogv", type: "video/ogg" },
  { name: "bug504843.ogv", type: "video/ogg" },
  { name: "bug501279.ogg", type: "audio/ogg" },
  { name: "bug603918.webm", type: "video/webm" },
  { name: "bug604067.webm", type: "video/webm" },
  { name: "bug1535980.webm", type: "video/webm" },
  { name: "bug1799787.webm", type: "video/webm" },
  { name: "bogus.duh", type: "bogus/duh" },
];

// These files would get error after receiving "loadedmetadata", we would like
// to check duration in "onerror" and make sure the duration is still available.
var gDurationTests = [
  { name: "bug603918.webm", duration: 6.076 },
  { name: "bug604067.webm", duration: 6.076 },
];

// These are files that have nontrivial duration and are useful for seeking within.
var gSeekTests = [
  { name: "r11025_s16_c1.wav", type: "audio/x-wav", duration: 1.0 },
  { name: "audio.wav", type: "audio/x-wav", duration: 0.031247 },
  { name: "seek.ogv", type: "video/ogg", duration: 3.966 },
  { name: "320x240.ogv", type: "video/ogg", duration: 0.266 },
  { name: "seek.webm", type: "video/webm", duration: 3.966 },
  { name: "sine.webm", type: "audio/webm", duration: 4.001 },
  { name: "bug516323.indexed.ogv", type: "video/ogg", duration: 4.208333 },
  { name: "split.webm", type: "video/webm", duration: 1.967 },
  { name: "detodos.opus", type: "audio/ogg; codecs=opus", duration: 2.9135 },
  { name: "gizmo.mp4", type: "video/mp4", duration: 5.56 },
  { name: "owl.mp3", type: "audio/mpeg", duration: 3.343 },
  { name: "bogus.duh", type: "bogus/duh", duration: 123 },

  // Bug 1242338: hit a numerical problem while seeking to the duration.
  { name: "bug482461-theora.ogv", type: "video/ogg", duration: 4.138 },
];

var gFastSeekTests = [
  {
    name: "gizmo.mp4",
    type: "video/mp4",
    keyframes: [0, 1.0, 2.0, 3.0, 4.0, 5.0],
  },
  // Note: Not all keyframes in the file are actually referenced in the Cues in this file.
  { name: "seek.webm", type: "video/webm", keyframes: [0, 0.8, 1.6, 2.4, 3.2] },
  // Note: the sync points are the points on both the audio and video streams
  // before the keyframes. You can't just assume that the keyframes are the sync
  // points, as the audio required for that sync point may be before the keyframe.
  {
    name: "bug516323.indexed.ogv",
    type: "video/ogg",
    keyframes: [0, 0.46, 3.06],
  },
];

// These files are WebMs without cues. They're seekable within their buffered
// ranges. If work renders WebMs fully seekable these files should be moved
// into gSeekTests
var gCuelessWebMTests = [
  { name: "no-cues.webm", type: "video/webm", duration: 3.967 },
];

// These are files that are non seekable, due to problems with the media,
// for example broken or missing indexes.
var gUnseekableTests = [{ name: "bogus.duh", type: "bogus/duh" }];

var androidVersion = -1; // non-Android platforms
if (
  manifestNavigator().userAgent.includes("Mobile") ||
  manifestNavigator().userAgent.includes("Tablet")
) {
  androidVersion = SpecialPowers.Services.sysinfo.getProperty("version");
}

function getAndroidVersion() {
  return androidVersion;
}

// These are files suitable for using with a "new Audio" constructor.
var gAudioTests = [
  { name: "adts.aac", type: "audio/mp4", duration: 1.37 },
  { name: "r11025_s16_c1.wav", type: "audio/x-wav", duration: 1.0 },
  { name: "sound.ogg", type: "audio/ogg" },
  { name: "owl.mp3", type: "audio/mpeg", duration: 3.343 },
  { name: "small-shot.m4a", type: "audio/mp4", duration: 0.29 },
  { name: "bogus.duh", type: "bogus/duh", duration: 123 },
  { name: "empty_size.mp3", type: "audio/mpeg", duration: 2.235 },
];

// These files ensure our handling of 404 errors is consistent across the
// various backends.
var g404Tests = [
  { name: "404.wav", type: "audio/x-wav" },
  { name: "404.ogv", type: "video/ogg" },
  { name: "404.oga", type: "audio/ogg" },
  { name: "404.webm", type: "video/webm" },
  { name: "bogus.duh", type: "bogus/duh" },
];

// These are files suitable for testing various decoder failures that are
// expected to fire MEDIA_ERR_DECODE.  Used by test_decode_error, which expects
// an error and emptied event, and no loadedmetadata or ended event.
var gDecodeErrorTests = [
  // Valid files with unsupported codecs
  { name: "r11025_msadpcm_c1.wav", type: "audio/x-wav" },
  { name: "dirac.ogg", type: "video/ogg" },
  // Invalid files
  { name: "bogus.wav", type: "audio/x-wav" },
  { name: "bogus.ogv", type: "video/ogg" },

  { name: "bogus.duh", type: "bogus/duh" },
];

// These are files that are used for media fragments tests
var gFragmentTests = [
  { name: "big.wav", type: "audio/x-wav", duration: 9.278982, size: 102444 },
];

// Used by test_chaining.html. The |links| attributes is the number of links in
// this file that we should be able to play.
var gChainingTests = [
  // Vorbis and Opus chained file. They have user comments |index=n| where `n`
  // is the index of this segment in the file, 0 indexed.
  { name: "chain.ogg", type: "audio/ogg", links: 4 },
  { name: "chain.opus", type: "audio/ogg; codec=opus", links: 4 },
  // Those files are chained files with a different number of channels in each
  // part. This is not supported and should stop playing after the first part.
  { name: "variable-channel.ogg", type: "audio/ogg", links: 1 },
  { name: "variable-channel.opus", type: "audio/ogg; codec=opus", links: 1 },
  // Those files are chained files with a different sample rate in each
  // part. This is not supported and should stop playing after the first part.
  { name: "variable-samplerate.ogg", type: "audio/ogg", links: 1 },
  // Opus decoding in Firefox outputs 48 kHz PCM despite having a different
  // original sample rate, so we can safely play Opus chained media that have
  // different samplerate accross links.
  { name: "variable-samplerate.opus", type: "audio/ogg; codec=opus", links: 2 },
  // A chained video file. We don't support those, so only one link should be
  // reported.
  { name: "chained-video.ogv", type: "video/ogg", links: 1 },
  // A file that consist in 4 links of audio, then another link that has video.
  // We should stop right after the 4 audio links.
  { name: "chained-audio-video.ogg", type: "video/ogg", links: 4 },
  // An opus file that has two links, with a different preskip value for each
  // link. We should be able to play both links.
  { name: "variable-preskip.opus", type: "audio/ogg; codec=opus", links: 2 },
  { name: "bogus.duh", type: "bogus/duh" },
];

// Videos with an aspect ratio. Used for testing that displaying frames
// on a canvas works correctly in the case of non-standard aspect ratios.
// See bug 874897 for an example.
var gAspectRatioTests = [
  { name: "VID_0001.ogg", type: "video/ogg", duration: 19.966 },
];

// These are files with non-trivial tag sets.
// Used by test_metadata.html.
var gMetadataTests = [
  // Ogg Vorbis files
  {
    name: "short-video.ogv",
    tags: {
      TITLE: "Lepidoptera",
      ARTIST: "Epoq",
      ALBUM: "Kahvi Collective",
      DATE: "2002",
      COMMENT: "http://www.kahvi.org",
    },
  },
  {
    name: "bug516323.ogv",
    tags: {
      GENRE: "Open Movie",
      ENCODER: "Audacity",
      TITLE: "Elephants Dream",
      ARTIST: "Silvia Pfeiffer",
      COMMENTS: "Audio Description",
    },
  },
  {
    name: "bug516323.indexed.ogv",
    tags: {
      GENRE: "Open Movie",
      ENCODER: "Audacity",
      TITLE: "Elephants Dream",
      ARTIST: "Silvia Pfeiffer",
      COMMENTS: "Audio Description",
    },
  },
  {
    name: "detodos.opus",
    tags: {
      title: "De todos. Para todos.",
      artist: "Mozilla.org",
    },
  },
  { name: "sound.ogg", tags: {} },
  {
    name: "small-shot.ogg",
    tags: {
      title: "Pew SFX",
    },
  },
  {
    name: "badtags.ogg",
    tags: {
      // We list only the valid tags here, and verify
      // the invalid ones are filtered out.
      title: "Invalid comments test file",
      empty: "",
      "": "empty",
      "{- [(`!@\"#$%^&')] -}": "valid tag name, surprisingly",
      // The file also includes the following invalid tags.
      // "A description with no separator is a common problem.",
      // "雨":"Likely, but an invalid key (non-ascii).",
      // "not\nval\x1fid":"invalid tag name",
      // "not~valid":"this isn't a valid name either",
      // "not-utf-8":"invalid sequences: \xff\xfe\xfa\xfb\0eol"
    },
  },
  {
    name: "wave_metadata.wav",
    tags: {
      name: "Track Title",
      artist: "Artist Name",
      comments: "Comments",
    },
  },
  {
    name: "wave_metadata_utf8.wav",
    tags: {
      name: "歌曲名稱",
      artist: "作曲者",
      comments: "註解",
    },
  },
  {
    name: "wave_metadata_unknown_tag.wav",
    tags: {
      name: "Track Title",
      comments: "Comments",
    },
  },
  {
    name: "wave_metadata_bad_len.wav",
    tags: {
      name: "Track Title",
      artist: "Artist Name",
      comments: "Comments",
    },
  },
  {
    name: "wave_metadata_bad_no_null.wav",
    tags: {
      name: "Track Title",
      artist: "Artist Name",
      comments: "Comments!!",
    },
  },
  {
    name: "wave_metadata_bad_utf8.wav",
    tags: {
      name: "歌曲名稱",
      comments: "註解",
    },
  },
  { name: "wavedata_u8.wav", tags: {} },
];

// Now Fennec doesn't support flac, so only test it on non-android platforms.
if (getAndroidVersion() < 0) {
  gMetadataTests = gMetadataTests.concat([
    {
      name: "flac-s24.flac",
      tags: {
        ALBUM: "Seascapes",
        TITLE: "(La Mer) - II. Jeux de vagues. Allegro",
        COMPOSER: "Debussy, Claude",
        TRACKNUMBER: "2/9",
        DISCNUMBER: "1/1",
        encoder: "Lavf57.41.100",
      },
    },
  ]);
}

// Test files for Encrypted Media Extensions
var gEMETests = [
  {
    name: "vp9 in mp4",
    tracks: [
      {
        name: "video",
        type: 'video/mp4; codecs="vp9.0"',
        fragments: ["short-vp9-encrypted-video.mp4"],
      },
      {
        name: "audio",
        type: 'audio/mp4; codecs="mp4a.40.2"',
        fragments: ["short-aac-encrypted-audio.mp4"],
      },
    ],
    keys: {
      "2cdb0ed6119853e7850671c3e9906c3c": "808B9ADAC384DE1E4F56140F4AD76194",
    },
    sessionType: "temporary",
    sessionCount: 2,
    duration: 0.47,
  },
  {
    name: "video-only with 2 keys",
    tracks: [
      {
        name: "video",
        type: 'video/mp4; codecs="avc1.4d4015"',
        fragments: [
          "bipbop-cenc-videoinit.mp4",
          "bipbop-cenc-video1.m4s",
          "bipbop-cenc-video2.m4s",
        ],
      },
    ],
    keys: {
      // "keyid" : "key"
      "7e571d037e571d037e571d037e571d03": "7e5733337e5733337e5733337e573333",
      "7e571d047e571d047e571d047e571d04": "7e5744447e5744447e5744447e574444",
    },
    sessionType: "temporary",
    sessionCount: 1,
    duration: 1.6,
  },
  {
    name: "video-only with 2 keys, CORS",
    tracks: [
      {
        name: "video",
        type: 'video/mp4; codecs="avc1.4d4015"',
        fragments: [
          "bipbop-cenc-videoinit.mp4",
          "bipbop-cenc-video1.m4s",
          "bipbop-cenc-video2.m4s",
        ],
      },
    ],
    keys: {
      // "keyid" : "key"
      "7e571d037e571d037e571d037e571d03": "7e5733337e5733337e5733337e573333",
      "7e571d047e571d047e571d047e571d04": "7e5744447e5744447e5744447e574444",
    },
    sessionType: "temporary",
    sessionCount: 1,
    crossOrigin: true,
    duration: 1.6,
  },
  {
    name: "audio&video tracks, both with all keys",
    tracks: [
      {
        name: "audio",
        type: 'audio/mp4; codecs="mp4a.40.2"',
        fragments: [
          "bipbop-cenc-audioinit.mp4",
          "bipbop-cenc-audio1.m4s",
          "bipbop-cenc-audio2.m4s",
          "bipbop-cenc-audio3.m4s",
        ],
      },
      {
        name: "video",
        type: 'video/mp4; codecs="avc1.4d4015"',
        fragments: [
          "bipbop-cenc-videoinit.mp4",
          "bipbop-cenc-video1.m4s",
          "bipbop-cenc-video2.m4s",
        ],
      },
    ],
    keys: {
      // "keyid" : "key"
      "7e571d037e571d037e571d037e571d03": "7e5733337e5733337e5733337e573333",
      "7e571d047e571d047e571d047e571d04": "7e5744447e5744447e5744447e574444",
    },
    sessionType: "temporary",
    sessionCount: 2,
    duration: 1.6,
  },
  {
    name: "audio&video tracks, both with all keys, CORS",
    tracks: [
      {
        name: "audio",
        type: 'audio/mp4; codecs="mp4a.40.2"',
        fragments: [
          "bipbop-cenc-audioinit.mp4",
          "bipbop-cenc-audio1.m4s",
          "bipbop-cenc-audio2.m4s",
          "bipbop-cenc-audio3.m4s",
        ],
      },
      {
        name: "video",
        type: 'video/mp4; codecs="avc1.4d4015"',
        fragments: [
          "bipbop-cenc-videoinit.mp4",
          "bipbop-cenc-video1.m4s",
          "bipbop-cenc-video2.m4s",
        ],
      },
    ],
    keys: {
      // "keyid" : "key"
      "7e571d037e571d037e571d037e571d03": "7e5733337e5733337e5733337e573333",
      "7e571d047e571d047e571d047e571d04": "7e5744447e5744447e5744447e574444",
    },
    sessionType: "temporary",
    sessionCount: 2,
    crossOrigin: true,
    duration: 1.6,
  },
  {
    name: "400x300 audio&video tracks, each with its key",
    tracks: [
      {
        name: "audio",
        type: 'audio/mp4; codecs="mp4a.40.2"',
        fragments: [
          "bipbop_300_215kbps-cenc-audio-key1-init.mp4",
          "bipbop_300_215kbps-cenc-audio-key1-1.m4s",
          "bipbop_300_215kbps-cenc-audio-key1-2.m4s",
          "bipbop_300_215kbps-cenc-audio-key1-3.m4s",
          "bipbop_300_215kbps-cenc-audio-key1-4.m4s",
        ],
      },
      {
        name: "video",
        type: 'video/mp4; codecs="avc1.4d4015"',
        fragments: [
          "bipbop_300_215kbps-cenc-video-key1-init.mp4",
          "bipbop_300_215kbps-cenc-video-key1-1.m4s",
          "bipbop_300_215kbps-cenc-video-key1-2.m4s",
        ],
      },
    ],
    keys: {
      // "keyid" : "key"
      "7e571d037e571d037e571d037e571d11": "7e5733337e5733337e5733337e573311",
      "7e571d047e571d047e571d047e571d21": "7e5744447e5744447e5744447e574421",
    },
    sessionType: "temporary",
    sessionCount: 2,
    duration: 1.6,
  },
  {
    name: "640x480@624kbps audio&video tracks, each with its key",
    tracks: [
      {
        name: "audio",
        type: 'audio/mp4; codecs="mp4a.40.2"',
        fragments: [
          "bipbop_480_624kbps-cenc-audio-key1-init.mp4",
          "bipbop_480_624kbps-cenc-audio-key1-1.m4s",
          "bipbop_480_624kbps-cenc-audio-key1-2.m4s",
          "bipbop_480_624kbps-cenc-audio-key1-3.m4s",
          "bipbop_480_624kbps-cenc-audio-key1-4.m4s",
        ],
      },
      {
        name: "video",
        type: 'video/mp4; codecs="avc1.4d401e"',
        fragments: [
          "bipbop_480_624kbps-cenc-video-key1-init.mp4",
          "bipbop_480_624kbps-cenc-video-key1-1.m4s",
          "bipbop_480_624kbps-cenc-video-key1-2.m4s",
        ],
      },
    ],
    keys: {
      // "keyid" : "key"
      "7e571d037e571d037e571d037e571d11": "7e5733337e5733337e5733337e573311",
      "7e571d047e571d047e571d047e571d21": "7e5744447e5744447e5744447e574421",
    },
    sessionType: "temporary",
    sessionCount: 2,
    duration: 1.6,
  },
  {
    name: "640x480@959kbps audio&video tracks, each with its key",
    tracks: [
      {
        name: "audio",
        type: 'audio/mp4; codecs="mp4a.40.2"',
        fragments: [
          "bipbop_480_959kbps-cenc-audio-key1-init.mp4",
          "bipbop_480_959kbps-cenc-audio-key1-1.m4s",
          "bipbop_480_959kbps-cenc-audio-key1-2.m4s",
          "bipbop_480_959kbps-cenc-audio-key1-3.m4s",
          "bipbop_480_959kbps-cenc-audio-key1-4.m4s",
        ],
      },
      {
        name: "video",
        type: 'video/mp4; codecs="avc1.4d401e"',
        fragments: [
          "bipbop_480_959kbps-cenc-video-key1-init.mp4",
          "bipbop_480_959kbps-cenc-video-key1-1.m4s",
          "bipbop_480_959kbps-cenc-video-key1-2.m4s",
        ],
      },
    ],
    keys: {
      // "keyid" : "key"
      "7e571d037e571d037e571d037e571d11": "7e5733337e5733337e5733337e573311",
      "7e571d047e571d047e571d047e571d21": "7e5744447e5744447e5744447e574421",
    },
    sessionType: "temporary",
    sessionCount: 2,
    duration: 1.6,
  },
  {
    name: "640x480 then 400x300, same key (1st) per track",
    tracks: [
      {
        name: "audio",
        type: 'audio/mp4; codecs="mp4a.40.2"',
        fragments: [
          "bipbop_480_624kbps-cenc-audio-key1-init.mp4",
          "bipbop_480_624kbps-cenc-audio-key1-1.m4s",
          "bipbop_480_624kbps-cenc-audio-key1-2.m4s",
          "bipbop_480_624kbps-cenc-audio-key1-3.m4s",
          "bipbop_480_624kbps-cenc-audio-key1-4.m4s",
        ],
      },
      {
        name: "video",
        type: 'video/mp4; codecs="avc1.4d401e,avc1.4d4015"',
        fragments: [
          "bipbop_480_624kbps-cenc-video-key1-init.mp4",
          "bipbop_480_624kbps-cenc-video-key1-1.m4s",
          "bipbop_300_215kbps-cenc-video-key1-init.mp4",
          "bipbop_300_215kbps-cenc-video-key1-2.m4s",
        ],
      },
    ],
    keys: {
      // "keyid" : "key"
      "7e571d037e571d037e571d037e571d11": "7e5733337e5733337e5733337e573311",
      "7e571d047e571d047e571d047e571d21": "7e5744447e5744447e5744447e574421",
    },
    sessionType: "temporary",
    sessionCount: 3,
    duration: 1.6,
  },
  {
    name: "640x480 then 400x300, same key (2nd) per track",
    tracks: [
      {
        name: "audio",
        type: 'audio/mp4; codecs="mp4a.40.2"',
        fragments: [
          "bipbop_480_624kbps-cenc-audio-key2-init.mp4",
          "bipbop_480_624kbps-cenc-audio-key2-1.m4s",
          "bipbop_480_624kbps-cenc-audio-key2-2.m4s",
          "bipbop_480_624kbps-cenc-audio-key2-3.m4s",
          "bipbop_480_624kbps-cenc-audio-key2-4.m4s",
        ],
      },
      {
        name: "video",
        type: 'video/mp4; codecs="avc1.4d401e,avc1.4d4015"',
        fragments: [
          "bipbop_480_624kbps-cenc-video-key2-init.mp4",
          "bipbop_480_624kbps-cenc-video-key2-1.m4s",
          "bipbop_300_215kbps-cenc-video-key2-init.mp4",
          "bipbop_300_215kbps-cenc-video-key2-2.m4s",
        ],
      },
    ],
    keys: {
      // "keyid" : "key"
      "7e571d037e571d037e571d037e571d12": "7e5733337e5733337e5733337e573312",
      "7e571d047e571d047e571d047e571d22": "7e5744447e5744447e5744447e574422",
    },
    sessionType: "temporary",
    sessionCount: 3,
    duration: 1.6,
  },
  {
    name: "640x480 with 1st keys then 400x300 with 2nd keys",
    tracks: [
      {
        name: "audio",
        type: 'audio/mp4; codecs="mp4a.40.2"',
        fragments: [
          "bipbop_480_624kbps-cenc-audio-key1-init.mp4",
          "bipbop_480_624kbps-cenc-audio-key1-1.m4s",
          "bipbop_480_624kbps-cenc-audio-key1-2.m4s",
          "bipbop_480_624kbps-cenc-audio-key1-3.m4s",
          "bipbop_480_624kbps-cenc-audio-key1-4.m4s",
        ],
      },
      {
        name: "video",
        type: 'video/mp4; codecs="avc1.4d401e,avc1.4d4015"',
        fragments: [
          "bipbop_480_624kbps-cenc-video-key1-init.mp4",
          "bipbop_480_624kbps-cenc-video-key1-1.m4s",
          "bipbop_300_215kbps-cenc-video-key2-init.mp4",
          "bipbop_300_215kbps-cenc-video-key2-2.m4s",
        ],
      },
    ],
    keys: {
      // "keyid" : "key"
      "7e571d037e571d037e571d037e571d11": "7e5733337e5733337e5733337e573311",
      "7e571d037e571d037e571d037e571d12": "7e5733337e5733337e5733337e573312",
      "7e571d047e571d047e571d047e571d21": "7e5744447e5744447e5744447e574421",
    },
    sessionType: "temporary",
    sessionCount: 3,
    duration: 1.6,
  },
  {
    name: "400x300 with 1st keys then 640x480 with 2nd keys",
    tracks: [
      {
        name: "audio",
        type: 'audio/mp4; codecs="mp4a.40.2"',
        fragments: [
          "bipbop_300_215kbps-cenc-audio-key1-init.mp4",
          "bipbop_300_215kbps-cenc-audio-key1-1.m4s",
          "bipbop_300_215kbps-cenc-audio-key1-2.m4s",
          "bipbop_300_215kbps-cenc-audio-key1-3.m4s",
          "bipbop_300_215kbps-cenc-audio-key1-4.m4s",
        ],
      },
      {
        name: "video",
        type: 'video/mp4; codecs="avc1.4d4015,avc1.4d401e"',
        fragments: [
          "bipbop_300_215kbps-cenc-video-key1-init.mp4",
          "bipbop_300_215kbps-cenc-video-key1-1.m4s",
          "bipbop_480_624kbps-cenc-video-key2-init.mp4",
          "bipbop_480_624kbps-cenc-video-key2-2.m4s",
        ],
      },
    ],
    keys: {
      // "keyid" : "key"
      "7e571d037e571d037e571d037e571d11": "7e5733337e5733337e5733337e573311",
      "7e571d037e571d037e571d037e571d12": "7e5733337e5733337e5733337e573312",
      "7e571d047e571d047e571d047e571d21": "7e5744447e5744447e5744447e574421",
    },
    sessionType: "temporary",
    sessionCount: 3,
    duration: 1.6,
  },
  {
    name: "640x480@959kbps with 1st keys then 640x480@624kbps with 2nd keys",
    tracks: [
      {
        name: "audio",
        type: 'audio/mp4; codecs="mp4a.40.2"',
        fragments: [
          "bipbop_480_959kbps-cenc-audio-key1-init.mp4",
          "bipbop_480_959kbps-cenc-audio-key1-1.m4s",
          "bipbop_480_959kbps-cenc-audio-key1-2.m4s",
          "bipbop_480_959kbps-cenc-audio-key1-3.m4s",
          "bipbop_480_959kbps-cenc-audio-key1-4.m4s",
        ],
      },
      {
        name: "video",
        type: 'video/mp4; codecs="avc1.4d401e"',
        fragments: [
          "bipbop_480_959kbps-cenc-video-key1-init.mp4",
          "bipbop_480_959kbps-cenc-video-key1-1.m4s",
          "bipbop_480_624kbps-cenc-video-key2-init.mp4",
          "bipbop_480_624kbps-cenc-video-key2-2.m4s",
        ],
      },
    ],
    keys: {
      // "keyid" : "key"
      "7e571d037e571d037e571d037e571d11": "7e5733337e5733337e5733337e573311",
      "7e571d037e571d037e571d037e571d12": "7e5733337e5733337e5733337e573312",
      "7e571d047e571d047e571d047e571d21": "7e5744447e5744447e5744447e574421",
    },
    sessionType: "temporary",
    sessionCount: 3,
    duration: 1.6,
  },
  {
    name: "640x480@624kbps with 1st keys then 640x480@959kbps with 2nd keys",
    tracks: [
      {
        name: "audio",
        type: 'audio/mp4; codecs="mp4a.40.2"',
        fragments: [
          "bipbop_480_624kbps-cenc-audio-key1-init.mp4",
          "bipbop_480_624kbps-cenc-audio-key1-1.m4s",
          "bipbop_480_624kbps-cenc-audio-key1-2.m4s",
          "bipbop_480_624kbps-cenc-audio-key1-3.m4s",
          "bipbop_480_624kbps-cenc-audio-key1-4.m4s",
        ],
      },
      {
        name: "video",
        type: 'video/mp4; codecs="avc1.4d401e"',
        fragments: [
          "bipbop_480_624kbps-cenc-video-key1-init.mp4",
          "bipbop_480_624kbps-cenc-video-key1-1.m4s",
          "bipbop_480_959kbps-cenc-video-key2-init.mp4",
          "bipbop_480_959kbps-cenc-video-key2-2.m4s",
        ],
      },
    ],
    keys: {
      // "keyid" : "key"
      "7e571d037e571d037e571d037e571d11": "7e5733337e5733337e5733337e573311",
      "7e571d037e571d037e571d037e571d12": "7e5733337e5733337e5733337e573312",
      "7e571d047e571d047e571d047e571d21": "7e5744447e5744447e5744447e574421",
    },
    sessionType: "temporary",
    sessionCount: 3,
    duration: 1.6,
  },
  {
    name: "400x300 with presentation size 533x300",
    tracks: [
      {
        name: "audio",
        type: 'audio/mp4; codecs="mp4a.40.2"',
        fragments: [
          "bipbop_300wp_227kbps-cenc-audio-key1-init.mp4",
          "bipbop_300wp_227kbps-cenc-audio-key1-1.m4s",
          "bipbop_300wp_227kbps-cenc-audio-key1-2.m4s",
          "bipbop_300wp_227kbps-cenc-audio-key1-3.m4s",
          "bipbop_300wp_227kbps-cenc-audio-key1-4.m4s",
        ],
      },
      {
        name: "video",
        type: 'video/mp4; codecs="avc1.4d4015"',
        fragments: [
          "bipbop_300wp_227kbps-cenc-video-key1-init.mp4",
          "bipbop_300wp_227kbps-cenc-video-key1-1.m4s",
          "bipbop_300wp_227kbps-cenc-video-key1-2.m4s",
        ],
      },
    ],
    keys: {
      // "keyid" : "key"
      "7e571d037e571d037e571d037e571d11": "7e5733337e5733337e5733337e573311",
      "7e571d047e571d047e571d047e571d21": "7e5744447e5744447e5744447e574421",
    },
    sessionType: "temporary",
    sessionCount: 2,
    duration: 1.6,
  },
  {
    name: "400x300 as-is then 400x300 presented as 533x300",
    tracks: [
      {
        name: "audio",
        type: 'audio/mp4; codecs="mp4a.40.2"',
        fragments: [
          "bipbop_300_215kbps-cenc-audio-key1-init.mp4",
          "bipbop_300_215kbps-cenc-audio-key1-1.m4s",
          "bipbop_300_215kbps-cenc-audio-key1-2.m4s",
          "bipbop_300_215kbps-cenc-audio-key1-3.m4s",
          "bipbop_300_215kbps-cenc-audio-key1-4.m4s",
        ],
      },
      {
        name: "video",
        type: 'video/mp4; codecs="avc1.4d4015"',
        fragments: [
          "bipbop_300_215kbps-cenc-video-key1-init.mp4",
          "bipbop_300_215kbps-cenc-video-key1-1.m4s",
          "bipbop_300wp_227kbps-cenc-video-key1-init.mp4",
          "bipbop_300wp_227kbps-cenc-video-key1-2.m4s",
        ],
      },
    ],
    keys: {
      // "keyid" : "key"
      "7e571d037e571d037e571d037e571d11": "7e5733337e5733337e5733337e573311",
      "7e571d047e571d047e571d047e571d21": "7e5744447e5744447e5744447e574421",
    },
    sessionType: "temporary",
    sessionCount: 3,
    duration: 1.6,
  },
  {
    name: "400x225",
    tracks: [
      {
        name: "audio",
        type: 'audio/mp4; codecs="mp4a.40.2"',
        fragments: [
          "bipbop_225w_175kbps-cenc-audio-key1-init.mp4",
          "bipbop_225w_175kbps-cenc-audio-key1-1.m4s",
          "bipbop_225w_175kbps-cenc-audio-key1-2.m4s",
          "bipbop_225w_175kbps-cenc-audio-key1-3.m4s",
          "bipbop_225w_175kbps-cenc-audio-key1-4.m4s",
        ],
      },
      {
        name: "video",
        type: 'video/mp4; codecs="avc1.64000d"',
        fragments: [
          "bipbop_225w_175kbps-cenc-video-key1-init.mp4",
          "bipbop_225w_175kbps-cenc-video-key1-1.m4s",
        ],
      },
    ],
    keys: {
      // "keyid" : "key"
      "7e571d037e571d037e571d037e571d11": "7e5733337e5733337e5733337e573311",
      "7e571d047e571d047e571d047e571d21": "7e5744447e5744447e5744447e574421",
    },
    sessionType: "temporary",
    sessionCount: 2,
    duration: 1.6,
  },
  {
    name: "640x360",
    tracks: [
      {
        name: "audio",
        type: 'audio/mp4; codecs="mp4a.40.2"',
        fragments: [
          "bipbop_360w_253kbps-cenc-audio-key1-init.mp4",
          "bipbop_360w_253kbps-cenc-audio-key1-1.m4s",
          "bipbop_360w_253kbps-cenc-audio-key1-2.m4s",
          "bipbop_360w_253kbps-cenc-audio-key1-3.m4s",
          "bipbop_360w_253kbps-cenc-audio-key1-4.m4s",
        ],
      },
      {
        name: "video",
        type: 'video/mp4; codecs="avc1.64001e"',
        fragments: [
          "bipbop_360w_253kbps-cenc-video-key1-init.mp4",
          "bipbop_360w_253kbps-cenc-video-key1-1.m4s",
        ],
      },
    ],
    keys: {
      // "keyid" : "key"
      "7e571d037e571d037e571d037e571d11": "7e5733337e5733337e5733337e573311",
      "7e571d047e571d047e571d047e571d21": "7e5744447e5744447e5744447e574421",
    },
    sessionType: "temporary",
    sessionCount: 2,
    duration: 1.6,
  },
  {
    name: "400x225 then 640x360",
    tracks: [
      {
        name: "audio",
        type: 'audio/mp4; codecs="mp4a.40.2"',
        fragments: [
          "bipbop_225w_175kbps-cenc-audio-key1-init.mp4",
          "bipbop_225w_175kbps-cenc-audio-key1-1.m4s",
          "bipbop_225w_175kbps-cenc-audio-key1-2.m4s",
          "bipbop_225w_175kbps-cenc-audio-key1-3.m4s",
          "bipbop_225w_175kbps-cenc-audio-key1-4.m4s",
        ],
      },
      {
        name: "video",
        type: 'video/mp4; codecs="avc1.64000d,avc1.64001e"',
        fragments: [
          "bipbop_225w_175kbps-cenc-video-key1-init.mp4",
          "bipbop_225w_175kbps-cenc-video-key1-1.m4s",
          "bipbop_360w_253kbps-cenc-video-key2-init.mp4",
          "bipbop_360w_253kbps-cenc-video-key2-1.m4s",
        ],
      },
    ],
    keys: {
      // "keyid" : "key"
      "7e571d037e571d037e571d037e571d11": "7e5733337e5733337e5733337e573311",
      "7e571d047e571d047e571d047e571d21": "7e5744447e5744447e5744447e574421",
      "7e571d037e571d037e571d037e571d12": "7e5733337e5733337e5733337e573312",
    },
    sessionType: "temporary",
    sessionCount: 3,
    duration: 1.6,
  },
  {
    name: "640x360 then 640x480",
    tracks: [
      {
        name: "audio",
        type: 'audio/mp4; codecs="mp4a.40.2"',
        fragments: [
          "bipbop_360w_253kbps-cenc-audio-key1-init.mp4",
          "bipbop_360w_253kbps-cenc-audio-key1-1.m4s",
          "bipbop_360w_253kbps-cenc-audio-key1-2.m4s",
          "bipbop_360w_253kbps-cenc-audio-key1-3.m4s",
          "bipbop_360w_253kbps-cenc-audio-key1-4.m4s",
        ],
      },
      {
        name: "video",
        type: 'video/mp4; codecs="avc1.64001e,avc1.4d401e"',
        fragments: [
          "bipbop_360w_253kbps-cenc-video-key1-init.mp4",
          "bipbop_360w_253kbps-cenc-video-key1-1.m4s",
          "bipbop_480_624kbps-cenc-video-key2-init.mp4",
          "bipbop_480_624kbps-cenc-video-key2-2.m4s",
        ],
      },
    ],
    keys: {
      // "keyid" : "key"
      "7e571d037e571d037e571d037e571d11": "7e5733337e5733337e5733337e573311",
      "7e571d047e571d047e571d047e571d21": "7e5744447e5744447e5744447e574421",
      "7e571d037e571d037e571d037e571d12": "7e5733337e5733337e5733337e573312",
    },
    sessionType: "temporary",
    sessionCount: 3,
    duration: 1.6,
  },
  {
    // File generated with shaka packager:
    // packager-osx --enable_raw_key_encryption --keys label=:key_id=7e571d047e571d047e571d047e571d21:key=7e5744447e5744447e5744447e574421 --segment_duration 1 --clear_lead 0 in=test-flac.mp4,stream=audio,output=flac-sample-cenc.mp4
    name: "flac in mp4 clearkey",
    tracks: [
      {
        name: "audio",
        type: 'audio/mp4; codecs="flac"',
        fragments: ["flac-sample-cenc.mp4"],
      },
    ],
    keys: {
      // "keyid" : "key"
      "7e571d047e571d047e571d047e571d21": "7e5744447e5744447e5744447e574421",
    },
    sessionType: "temporary",
    sessionCount: 1,
    duration: 2.05,
  },
  {
    // File generated with shaka packager:
    // packager-osx --enable_raw_key_encryption --keys label=:key_id=7e571d047e571d047e571d047e571d21:key=7e5744447e5744447e5744447e574421 --segment_duration 1 --clear_lead 0 in=test-opus.mp4,stream=audio,output=opus-sample-cenc.mp4
    name: "opus in mp4 clearkey",
    tracks: [
      {
        name: "audio",
        type: 'audio/mp4; codecs="opus"',
        fragments: ["opus-sample-cenc.mp4"],
      },
    ],
    keys: {
      // "keyid" : "key"
      "7e571d047e571d047e571d047e571d21": "7e5744447e5744447e5744447e574421",
    },
    sessionType: "temporary",
    sessionCount: 1,
    duration: 1.98,
  },
  {
    name: "WebM vorbis audio & vp8 video clearkey",
    tracks: [
      {
        name: "audio",
        type: 'audio/webm; codecs="vorbis"',
        fragments: ["bipbop_360w_253kbps-clearkey-audio.webm"],
      },
      {
        name: "video",
        type: 'video/webm; codecs="vp8"',
        fragments: ["bipbop_360w_253kbps-clearkey-video-vp8.webm"],
      },
    ],
    keys: {
      // "keyid" : "key"
      f1f3ee1790527e9de47217d43835f76a: "97b9ddc459c8d5ff23c1f2754c95abe8",
      "8b5df745ad84145b5617c33116e35a67": "bddfd35dd9be033ee73bc18bc1885056",
    },
    sessionType: "temporary",
    sessionCount: 2,
    duration: 1.6,
  },
  {
    name: "WebM vorbis audio & vp9 video clearkey",
    tracks: [
      {
        name: "audio",
        type: 'audio/webm; codecs="vorbis"',
        fragments: ["bipbop_360w_253kbps-clearkey-audio.webm"],
      },
      {
        name: "video",
        type: 'video/webm; codecs="vp9"',
        fragments: ["bipbop_360w_253kbps-clearkey-video-vp9.webm"],
      },
    ],
    keys: {
      // "keyid" : "key"
      f1f3ee1790527e9de47217d43835f76a: "97b9ddc459c8d5ff23c1f2754c95abe8",
      eedf63a94fa7c398ee094f123a4ee709: "973b679a746c82f3acdb856b30e9378e",
    },
    sessionType: "temporary",
    sessionCount: 2,
    duration: 1.6,
  },
  {
    name: "WebM vorbis audio & vp9 video clearkey with subsample encryption",
    tracks: [
      {
        name: "audio",
        type: 'audio/webm; codecs="vorbis"',
        fragments: ["sintel-short-clearkey-subsample-encrypted-audio.webm"],
      },
      {
        name: "video",
        type: 'video/webm; codecs="vp9"',
        fragments: ["sintel-short-clearkey-subsample-encrypted-video.webm"],
      },
    ],
    keys: {
      // "keyid" : "key"
      "2cdb0ed6119853e7850671c3e9906c3c": "808B9ADAC384DE1E4F56140F4AD76194",
    },
    sessionType: "temporary",
    sessionCount: 2,
    duration: 2.0,
  },
  {
    // Files adapted from testcase for bug 1560092. See bug 1630381 for a
    // detailed explanation on how they were adapted.
    name: "avc3 h264 video in mp4 using clearkey cenc encryption",
    tracks: [
      {
        name: "video",
        type: 'video/mp4; codecs="avc3.640015"',
        fragments: [
          "big-buck-bunny-cenc-avc3-init.mp4",
          "big-buck-bunny-cenc-avc3-1.m4s",
        ],
      },
    ],
    keys: {
      // "keyid" : "key"
      "10000000100010001000100000000001": "3A2A1B68DD2BD9B2EEB25E84C4776668",
    },
    sessionType: "temporary",
    sessionCount: 1,
    duration: 2.08,
  },
  // The following cbcs files are created using shaka-packager using commands like
  // ./packager-win.exe 'in=bipbop_2s.mp4,stream=audio,init_segment=bipbop_cbcs_1_9_audio_init.mp4,segment_template=bipbop_cbcs_1_9_audio_$Number$.m4s' \
  // 'in=bipbop_2s.mp4,stream=video,init_segment=bipbop_cbcs_1_9_video_init.mp4,segment_template=bipbop_cbcs_1_9_video_$Number$.m4s' --protection_scheme cbcs \
  // --enable_raw_key_encryption --keys label=:key_id=7e571d047e571d047e571d047e571d21:key=7e5744447e5744447e5744447e574421 --iv 11223344556677889900112233445566 \
  // --clear_lead 0 --crypt_byte_block 1 --skip_byte_block 9
  // See bug 1726202 for more details on their creation.
  {
    name: "mp4 h264 + aac clearkey cbcs 1:9 pattern",
    tracks: [
      {
        name: "video",
        type: 'video/mp4; codecs="avc1.4d4015"',
        fragments: [
          "bipbop_cbcs_1_9_video_init.mp4",
          "bipbop_cbcs_1_9_video_1.m4s",
        ],
      },
      {
        name: "audio",
        type: 'audio/mp4; codecs="mp4a.40.2"',
        fragments: [
          "bipbop_cbcs_1_9_audio_init.mp4",
          "bipbop_cbcs_1_9_audio_1.m4s",
        ],
      },
    ],
    keys: {
      // "keyid" : "key"
      "7e571d047e571d047e571d047e571d21": "7e5744447e5744447e5744447e574421",
    },
    sessionType: "temporary",
    sessionCount: 2,
    duration: 2.04,
  },
  {
    name: "mp4 h264 + aac clearkey cbcs 5:5 pattern",
    tracks: [
      {
        name: "video",
        type: 'video/mp4; codecs="avc1.4d4015"',
        fragments: [
          "bipbop_cbcs_5_5_video_init.mp4",
          "bipbop_cbcs_5_5_video_1.m4s",
        ],
      },
      {
        name: "audio",
        type: 'audio/mp4; codecs="mp4a.40.2"',
        fragments: [
          "bipbop_cbcs_5_5_audio_init.mp4",
          "bipbop_cbcs_5_5_audio_1.m4s",
        ],
      },
    ],
    keys: {
      // "keyid" : "key"
      "7e571d047e571d047e571d047e571d21": "7e5744447e5744447e5744447e574421",
    },
    sessionType: "temporary",
    sessionCount: 2,
    duration: 2.04,
  },
  {
    name: "mp4 h264 + aac clearkey cbcs 10:0 pattern",
    tracks: [
      {
        name: "video",
        type: 'video/mp4; codecs="avc1.4d4015"',
        fragments: [
          "bipbop_cbcs_10_0_video_init.mp4",
          "bipbop_cbcs_10_0_video_1.m4s",
        ],
      },
      {
        name: "audio",
        type: 'audio/mp4; codecs="mp4a.40.2"',
        fragments: [
          "bipbop_cbcs_10_0_audio_init.mp4",
          "bipbop_cbcs_10_0_audio_1.m4s",
        ],
      },
    ],
    keys: {
      // "keyid" : "key"
      "7e571d047e571d047e571d047e571d21": "7e5744447e5744447e5744447e574421",
    },
    sessionType: "temporary",
    sessionCount: 2,
    duration: 2.04,
  },
  {
    name: "mp4 h264 + aac clearkey cbcs 7:7 pattern",
    tracks: [
      {
        name: "video",
        type: 'video/mp4; codecs="avc1.4d4015"',
        fragments: [
          "bipbop_cbcs_7_7_video_init.mp4",
          "bipbop_cbcs_7_7_video_1.m4s",
        ],
      },
      {
        name: "audio",
        type: 'audio/mp4; codecs="mp4a.40.2"',
        fragments: [
          "bipbop_cbcs_7_7_audio_init.mp4",
          "bipbop_cbcs_7_7_audio_1.m4s",
        ],
      },
    ],
    keys: {
      // "keyid" : "key"
      "7e571d047e571d047e571d047e571d21": "7e5744447e5744447e5744447e574421",
    },
    sessionType: "temporary",
    sessionCount: 2,
    duration: 2.04,
  },
  {
    name: "mp4 h264 + aac clearkey cbcs 9:8 pattern",
    tracks: [
      {
        name: "video",
        type: 'video/mp4; codecs="avc1.4d4015"',
        fragments: [
          "bipbop_cbcs_9_8_video_init.mp4",
          "bipbop_cbcs_9_8_video_1.m4s",
        ],
      },
      {
        name: "audio",
        type: 'audio/mp4; codecs="mp4a.40.2"',
        fragments: [
          "bipbop_cbcs_9_8_audio_init.mp4",
          "bipbop_cbcs_9_8_audio_1.m4s",
        ],
      },
    ],
    keys: {
      // "keyid" : "key"
      "7e571d047e571d047e571d047e571d21": "7e5744447e5744447e5744447e574421",
    },
    sessionType: "temporary",
    sessionCount: 2,
    duration: 2.04,
  },
];

var gEMENonMSEFailTests = [
  {
    name: "short-cenc.mp4",
    audioType: 'audio/mp4; codecs="mp4a.40.2"',
    videoType: 'video/mp4; codecs="avc1.64000d"',
    duration: 0.47,
  },
];

// Test files that are supposed to loop seamlessly when played back.
var gSeamlessLoopingTests = [
  // MP4 files dont't loop seamlessly yet, the seeking logic seeks to 0, not the
  // actual first packet, resulting in incorrect decoding.
  // See bug 1817989
  // { name: "sin-441-1s-44100-fdk_aac.mp4", type: "audio/mp4" },
  // { name: "sin-441-1s-44100-afconvert.mp4", type: "audio/mp4" },
  // { name: "sin-441-1s-44100.ogg", type: "audio/vorbis" },
  // { name: "sin-441-1s-44100.opus", type: "audio/opus" },
  { name: "sin-441-1s-44100-lame.mp3", type: "audio/mpeg" },
  { name: "sin-441-1s-44100.flac", type: "audio/flac" },
];

// These are files that are used for video decode suspend in
// background tabs tests.
var gDecodeSuspendTests = [
  { name: "gizmo.mp4", type: "video/mp4", duration: 5.56 },
  { name: "gizmo-noaudio.mp4", type: "video/mp4", duration: 5.56 },
  { name: "gizmo.webm", type: 'video/webm; codecs="vp9,opus"', duration: 5.56 },
  {
    name: "gizmo-noaudio.webm",
    type: 'video/webm; codecs="vp9"',
    duration: 5.56,
  },
];

// These are video files with hardware-decodable formats and longer
// durations that are looped while we check telemetry for macOS video
// low power mode.
var gVideoLowPowerTests = [
  { name: "seek.ogv", type: "video/ogg", duration: 3.966 },
  { name: "gizmo.mp4", type: "video/mp4", duration: 5.56 },
];

function checkMetadata(msg, e, test) {
  if (test.width) {
    is(e.videoWidth, test.width, msg + " video width");
  }
  if (test.height) {
    is(e.videoHeight, test.height, msg + " video height");
  }
  if (test.duration) {
    ok(
      Math.abs(e.duration - test.duration) < 0.1,
      msg + " duration (" + e.duration + ") should be around " + test.duration
    );
  }
  is(
    !!test.keys,
    SpecialPowers.do_lookupGetter(e, "isEncrypted").apply(e),
    msg + " isEncrypted should be true if we have decryption keys"
  );
}

// Returns the first test from candidates array which we can play with the
// installed video backends.
function getPlayableVideo(candidates) {
  var resources = getPlayableVideos(candidates);
  if (resources.length) {
    return resources[0];
  }
  return null;
}

function getPlayableVideos(candidates) {
  var v = manifestVideo();
  return candidates.filter(function (x) {
    return /^video/.test(x.type) && v.canPlayType(x.type);
  });
}

function getPlayableAudio(candidates) {
  var v = manifestVideo();
  var resources = candidates.filter(function (x) {
    return /^audio/.test(x.type) && v.canPlayType(x.type);
  });
  if (resources.length) {
    return resources[0];
  }
  return null;
}

// Returns the type of element that should be created for the given mimetype.
function getMajorMimeType(mimetype) {
  if (/^video/.test(mimetype)) {
    return "video";
  }
  return "audio";
}

// Force releasing decoder to avoid timeout in waiting for decoding resource.
function removeNodeAndSource(n) {
  n.remove();
  // reset |srcObject| first since it takes precedence over |src|.
  n.srcObject = null;
  n.removeAttribute("src");
  n.load();
  while (n.firstChild) {
    n.firstChild.remove();
  }
}

function once(target, name, cb) {
  var p = new Promise(function (resolve, reject) {
    target.addEventListener(
      name,
      function () {
        resolve();
      },
      { once: true }
    );
  });
  if (cb) {
    p.then(cb);
  }
  return p;
}

/**
 * @param {HTMLMediaElement} video target of interest.
 * @param {string} eventName the event to wait on.
 * @returns {Promise} A promise that is resolved when event happens.
 */
function nextEvent(video, eventName) {
  return new Promise(function (resolve, reject) {
    let f = function (event) {
      video.removeEventListener(eventName, f);
      resolve(event);
    };
    video.addEventListener(eventName, f);
  });
}

function TimeStamp(token) {
  function pad(x) {
    return x < 10 ? "0" + x : x;
  }
  var now = new Date();
  var ms = now.getMilliseconds();
  var time =
    "[" +
    pad(now.getHours()) +
    ":" +
    pad(now.getMinutes()) +
    ":" +
    pad(now.getSeconds()) +
    "." +
    ms +
    "]" +
    // eslint-disable-next-line no-nested-ternary
    (ms < 10 ? "  " : ms < 100 ? " " : "");
  return token ? time + " " + token : time;
}

function Log(token, msg) {
  info(TimeStamp(token) + " " + msg);
}

// Number of tests to run in parallel.
var PARALLEL_TESTS = 2;

// Prefs to set before running tests.  Use this to improve coverage of
// conditions that might not otherwise be encountered on the test data.
var gTestPrefs = [
  ["media.recorder.max_memory", 1024],
  ["media.audio-max-decode-error", 0],
  ["media.video-max-decode-error", 0],
];

// When true, we'll loop forever on whatever test we run. Use this to debug
// intermittent test failures.
const DEBUG_TEST_LOOP_FOREVER = false;

// Manages a run of media tests. Runs them in chunks in order to limit
// the number of media elements/threads running in parallel. This limits peak
// memory use, particularly on Linux x86 where thread stacks use 10MB of
// virtual address space.
// Usage:
//   1. Create a new MediaTestManager object.
//   2. Create a test startTest function. This takes a test object and a token,
//      and performs anything necessary to start the test. The test object is an
//      element in one of the g*Tests above. Your startTest function must call
//      MediaTestManager.start(token) if it starts a test. The test object is
//      guaranteed to be playable by our supported decoders; you don't need to
//      check canPlayType.
//   3. When your tests finishes, call MediaTestManager.finished(), passing
//      the token back to the manager. The manager may either start the next run
//      or end the mochitest if all the tests are done.
function MediaTestManager() {
  // Set a very large timeout to prevent Mochitest timeout.
  // Instead MediaTestManager will manage timeout of each test.
  SimpleTest.requestLongerTimeout(1000);

  // Return how many seconds elapsed since |begin|.
  function elapsedTime(begin) {
    var end = new Date();
    return (end.getTime() - begin.getTime()) / 1000;
  }
  // Sets up a MediaTestManager to runs through the 'tests' array, which needs
  // to be one of, or have the same fields as, the g*Test arrays of tests. Uses
  // the user supplied 'startTest' function to initialize the test. This
  // function must accept two arguments, the test entry from the 'tests' array,
  // and a token. Call MediaTestManager.started(token) if you start the test,
  // and MediaTestManager.finished(token) when the test finishes. You don't have
  // to start every test, but if you call started() you *must* call finish()
  // else you'll timeout.
  this.runTests = function (tests, startTest) {
    this.startTime = new Date();
    SimpleTest.info(
      "Started " +
        this.startTime +
        " (" +
        this.startTime.getTime() / 1000 +
        "s)"
    );
    this.testNum = 0;
    this.tests = tests;
    this.startTest = startTest;
    this.tokens = [];
    this.isShutdown = false;
    this.numTestsRunning = 0;
    this.handlers = {};
    this.timers = {};

    // Always wait for explicit finish.
    SimpleTest.waitForExplicitFinish();
    SpecialPowers.pushPrefEnv({ set: gTestPrefs }, () => {
      this.nextTest();
    });

    SimpleTest.registerCleanupFunction(() => {
      if (this.tokens.length) {
        info("Test timed out. Remaining tests=" + this.tokens);
      }
      for (var token of this.tokens) {
        var handler = this.handlers[token];
        if (handler && handler.ontimeout) {
          handler.ontimeout();
        }
      }
    });
  };

  // Registers that the test corresponding to 'token' has been started.
  // Don't call more than once per token.
  this.started = function (token, handler) {
    this.tokens.push(token);
    this.numTestsRunning++;
    this.handlers[token] = handler;

    var onTimeout = async () => {
      ok(false, "Test timed out!");
      info(`${token} timed out!`);
      await dumpDebugInfoForToken(token);
      this.finished(token);
    };
    // Default timeout to 180s for each test.
    // Call SimpleTest._originalSetTimeout() to bypass the flaky timeout checker.
    this.timers[token] = SimpleTest._originalSetTimeout.call(
      window,
      onTimeout,
      180000
    );

    is(
      this.numTestsRunning,
      this.tokens.length,
      "[started " +
        token +
        " t=" +
        elapsedTime(this.startTime) +
        "] Length of array should match number of running tests"
    );
  };

  // Registers that the test corresponding to 'token' has finished. Call when
  // you've finished your test. If all tests are complete this will finish the
  // run, otherwise it may start up the next run. It's ok to call multiple times
  // per token.
  this.finished = function (token) {
    var i = this.tokens.indexOf(token);
    if (i != -1) {
      // Remove the element from the list of running tests.
      this.tokens.splice(i, 1);
    }

    if (this.timers[token]) {
      // Cancel the timer when the test finishes.
      clearTimeout(this.timers[token]);
      this.timers[token] = null;
    }

    info("[finished " + token + "] remaining= " + this.tokens);
    this.numTestsRunning--;
    is(
      this.numTestsRunning,
      this.tokens.length,
      "[finished " +
        token +
        " t=" +
        elapsedTime(this.startTime) +
        "] Length of array should match number of running tests"
    );
    if (this.tokens.length < PARALLEL_TESTS) {
      this.nextTest();
    }
  };

  // Starts the next batch of tests, or finishes if they're all done.
  // Don't call this directly, call finished(token) when you're done.
  this.nextTest = function () {
    while (
      this.testNum < this.tests.length &&
      this.tokens.length < PARALLEL_TESTS
    ) {
      var test = this.tests[this.testNum];
      var token = (test.name ? test.name + "-" : "") + this.testNum;
      this.testNum++;

      if (DEBUG_TEST_LOOP_FOREVER && this.testNum == this.tests.length) {
        this.testNum = 0;
      }

      // Ensure we can play the resource type.
      if (
        test.type &&
        !document.createElement("video").canPlayType(test.type)
      ) {
        continue;
      }

      // Do the init. This should start the test.
      this.startTest(test, token);
    }

    if (
      this.testNum == this.tests.length &&
      !DEBUG_TEST_LOOP_FOREVER &&
      !this.tokens.length &&
      !this.isShutdown
    ) {
      this.isShutdown = true;
      if (this.onFinished) {
        this.onFinished();
      }
      var onCleanup = () => {
        var end = new Date();
        SimpleTest.info(
          "Finished at " + end + " (" + end.getTime() / 1000 + "s)"
        );
        SimpleTest.info("Running time: " + elapsedTime(this.startTime) + "s");
        SimpleTest.finish();
      };
      mediaTestCleanup(onCleanup);
    }
  };
}

// Ensures we've got no active video or audio elements in the document, and
// forces a GC to release the address space reserved by the decoders' threads'
// stacks.
function mediaTestCleanup(callback) {
  var V = document.getElementsByTagName("video");
  for (let i = 0; i < V.length; i++) {
    removeNodeAndSource(V[i]);
    V[i] = null;
  }
  var A = document.getElementsByTagName("audio");
  for (let i = 0; i < A.length; i++) {
    removeNodeAndSource(A[i]);
    A[i] = null;
  }
  SpecialPowers.exactGC(callback);
}

async function dumpDebugInfoForToken(token) {
  for (let v of document.getElementsByTagName("video")) {
    if (token === v.token) {
      info(JSON.stringify(await SpecialPowers.wrap(v).mozRequestDebugInfo()));
      return;
    }
  }
  for (let a of document.getElementsByTagName("audio")) {
    if (token === a.token) {
      info(JSON.stringify(await SpecialPowers.wrap(a).mozRequestDebugInfo()));
      return;
    }
  }
}

// Could be undefined in a page opened by the parent test page
// like file_access_controls.html.
if ("SimpleTest" in window) {
  SimpleTest.requestFlakyTimeout("untriaged");

  // Register timeout function to dump debugging logs.
  SimpleTest.registerTimeoutFunction(async function () {
    for (const v of document.getElementsByTagName("video")) {
      SimpleTest.info(
        JSON.stringify(await SpecialPowers.wrap(v).mozRequestDebugInfo())
      );
    }
    for (const a of document.getElementsByTagName("audio")) {
      SimpleTest.info(
        JSON.stringify(await SpecialPowers.wrap(a).mozRequestDebugInfo())
      );
    }
  });
}