retina 0.4.19

high-level RTSP multimedia streaming library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
// Copyright (C) The Retina Authors
// SPDX-License-Identifier: MIT OR Apache-2.0

//! RTSP client: connect to a server via [`Session`].

use std::convert::TryFrom;
use std::io;
use std::mem::MaybeUninit;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::num::NonZeroU32;
use std::sync::{Arc, Mutex};
use std::task::Poll;
use std::time::Instant;
use std::{fmt::Debug, num::NonZeroU16, pin::Pin};

use self::channel_mapping::*;
pub use self::timeline::Timeline;
use crate::rtsp::msg::{self as msg, OwnedMessage, StatusCode};
use bytes::Bytes;
use futures::{Future, SinkExt, StreamExt, ready};
use log::{debug, trace, warn};
use pin_project::pin_project;
use tokio::net::UdpSocket;
use tokio::sync::Notify;
use url::Url;

use crate::client::parse::SessionHeader;
use crate::codec::CodecItem;
use crate::{
    Error, ErrorInt, RtspMessageContext, StreamContext, StreamContextInner, TcpStreamContext,
    UdpStreamContext,
};

mod channel_mapping;
mod parse;

/// Internal API, public for a benchmark only.
#[doc(hidden)]
pub mod rtp;

mod teardown;
mod timeline;

/// Assumed expiration time for stale live555 TCP sessions (case #2 of "Stale
/// sessions" in [`SessionGroup`]).
///
/// This constant is taken from
/// [here](https://github.com/rgaufman/live555/blob/41a5ec5f65bd626918a43951f743b4c9ffc52289/liveMedia/include/RTSPServer.hh#L35).
const LIVE555_EXPIRATION_SEC: u64 = 65;

/// A stale RTP session. See explanation at [`SessionGroup`].
struct StaleSession {
    seqnum: u64,

    /// If this stale session was created from a dropped [`Session`],
    /// a watch showing the results of the latest `TEARDOWN` attempt, or `None`
    /// if one hasn't yet concluded.
    teardown_rx: Option<tokio::sync::watch::Receiver<Option<Result<(), Error>>>>,

    maybe_playing: bool,
    has_tcp: bool,

    /// Upper bound of advertised expiration time.
    expires: tokio::time::Instant,
}

/// A group of sessions, currently used only to track stale sessions.
///
/// Sessions are associated with a group via [`SessionOptions::session_group`].
///
/// This is an experimental API which may change in an upcoming Retina version.
///
/// ## Stale sessions
///
/// Stale sessions are ones which are no longer active on the client side
/// (no [`Session`] struct exists) but may still be in state `Ready`, `Playing`,
/// or `Recording` on the server. The client has neither seen a `TEARDOWN`
/// response nor believes they have reached their expiration time. They are
/// tracked in two cases:
///
/// 1.  Dropped [`Session`]s if the [`TeardownPolicy`] says to do so
///     and a valid `SETUP` response has been received.
///
///     A tokio background task is responsible for attempting a `TEARDOWN` and
///     cleaning the session after success or expiration.
///     [`SessionGroup::await_teardown`] can be used to wait out this process.
///
///     In general, the tracked expiration time is worst-case. The exception is
///     if the sender hasn't responded to a keepalive request. In that case
///     there's theoretically no bound on when the server could see the request
///     and extend the session. Retina ignores this possibility.
///
/// 2.  TCP sessions discovered via unexpected RTSP interleaved data
///     packets. These are assumed to be due to a live555 bug in which
///     data continues to be sent on a stale file descriptor after a
///     connection is closed. The sessions' packets may be sent to unrelated
///     (even unauthenticated and/or non-RTSP!) connections after the file
///     descriptor is reused.  These sessions may have been started by a process
///     unknown to us and their session id is unknown, so in general it is not
///     possible to send a `TEARDOWN`.
///
///     These sessions are assumed to expire 65 seconds after discovery, a
///     constant taken from old live555 code.
///
/// ## Granularity
///
/// A `SessionGroup` can be of any granularity, but a typical use is to ensure
/// there are no stale sessions before starting a fresh session (see
/// [`SessionGroup::stale_sessions`] and [`SessionGroup::await_stale_sessions`]).
/// Groups should be sized to match that idea. If connecting to a live555 server
/// affected by the stale TCP session bug, it might be wise to have one group
/// per server, so that all such sessions can be drained before initiating new
/// connections. Otherwise it might be useful to have one group per describe
/// URL (potentially several per server) and have at most one active session per
/// URL.
#[derive(Default)]
pub struct SessionGroup {
    name: Option<String>,
    sessions: Mutex<SessionGroupInner>,
    notify: Notify,
}

#[derive(Default)]
struct SessionGroupInner {
    next_seqnum: u64,

    /// Stale sessions, unordered.
    sessions: Vec<StaleSession>,
}

/// The overall status of stale sessions that may be in state `Playing` and
/// belong to a particular group.
pub struct StaleSessionStatus {
    /// The maximum expire time of any such sessions.
    pub max_expires: Option<tokio::time::Instant>,

    /// The total number of sessions.
    pub num_sessions: usize,

    /// The `SessionGroupInner::next_seqnum` value as of when this was created.
    next_seqnum: u64,
}

impl SessionGroup {
    /// Returns this group with an assigned name.
    ///
    /// Typically called before placing into an `Arc`, e.g.
    /// `Arc::new(SessionGroup::default().named("foo"))`.
    pub fn named(self, name: String) -> Self {
        SessionGroup {
            name: Some(name),
            ..self
        }
    }

    /// Returns the name of this session group, if any.
    pub fn name(&self) -> Option<&str> {
        self.name.as_deref()
    }

    /// An identifier for this session group, for use in log messages.
    ///
    /// Currently uses the name if set, a pointer address otherwise.
    fn debug_id(&self) -> impl Debug + use<> {
        self.name.clone().unwrap_or_else(|| format!("{:p}", &self))
    }

    /// Returns the status of stale sessions in this group.
    ///
    /// Currently this only returns information about sessions which may be in
    /// state `Playing`. That is, ones for which Retina has either sent a
    /// `PLAY` request (regardless of whether it received a response) or
    /// discovered as described in [`SessionGroup`].
    ///
    /// The caller might use this in a loop with `await_stale_sessions` to sleep
    /// until there are no such sessions, logging updates
    pub fn stale_sessions(&self) -> StaleSessionStatus {
        let l = self.sessions.lock().unwrap();
        let playing = l.sessions.iter().filter(|s| s.maybe_playing);
        StaleSessionStatus {
            max_expires: playing.clone().map(|s| s.expires).max(),
            num_sessions: playing.count(),
            next_seqnum: l.next_seqnum,
        }
    }

    /// Waits for a reasonable attempt at `TEARDOWN` on all stale sessions that
    /// exist as of when this method is called, returning an error if any
    /// session's reasonable attempts fail.
    ///
    /// This has no timeout other than the sessions' expiration times. The
    /// caller can wrap the call in `tokio::time::timeout` for an earlier time.
    ///
    /// Currently on `Session::drop`, a `TEARDOWN` loop is started in the
    /// background. This method waits for an attempt on an existing connection
    /// (if any) and in some cases the first attempt on a fresh connection.
    /// Retina may continue sending more attempts even after this method
    /// returns.
    ///
    /// Ignores the discovered live555 bug sessions, as it's impossible to send
    /// a `TEARDOWN` without knowing the session id. If desired, the caller can
    /// learn of the existence of the sessions through
    /// [`SessionGroup::stale_sessions`] and sleep until they expire.
    ///
    /// ## Panics
    ///
    /// If the `TEARDOWN` was initiated from a tokio runtime which has since
    /// shut down.
    pub async fn await_teardown(&self) -> Result<(), Error> {
        let mut watches: Vec<_>;
        {
            let l = self.sessions.lock().unwrap();
            watches = l
                .sessions
                .iter()
                .filter_map(|s| s.teardown_rx.clone())
                .collect();
        }

        let mut overall_result = Ok(());
        for w in &mut watches {
            let mut r = (*w.borrow_and_update()).clone();

            if r.is_none() {
                // An attempt hasn't finished yet. Wait for it.
                w.changed().await.expect(
                    "teardown Sender shouldn't be dropped; \
                             ensure the Session's tokio runtime is still alive",
                );
                r.clone_from(&*w.borrow())
            }

            // Now an attempt has finished, success or failure.
            let r = r.expect("teardown result should be populated after change");
            overall_result = overall_result.and(r);
        }
        overall_result
    }

    /// Waits for all of the sessions described by `status` to expire or be torn down.
    pub async fn await_stale_sessions(&self, status: &StaleSessionStatus) {
        loop {
            let notified = self.notify.notified();
            {
                let l = self.sessions.lock().unwrap();
                let left = l
                    .sessions
                    .iter()
                    .filter(|s| s.maybe_playing && s.seqnum < status.next_seqnum)
                    .count();
                log::trace!(
                    "Session group {:?} has {} relevant sessions numbered < {}",
                    self.debug_id(),
                    left,
                    status.next_seqnum
                );
                if left == 0 {
                    return;
                }
            }
            notified.await;
        }
    }

    /// Removes the session with `seqnum` removing true iff it existed. Notifies waiters.
    fn try_remove_seqnum(&self, seqnum: u64) -> bool {
        let mut l = self.sessions.lock().unwrap();
        let i = l.sessions.iter().position(|s| s.seqnum == seqnum);
        match i {
            Some(i) => {
                l.sessions.swap_remove(i);
                drop(l);
                self.notify.notify_waiters();
                true
            }
            None => false,
        }
    }
}

/// Policy for when to send `TEARDOWN` requests.
///
/// Specify via [`SessionOptions::teardown`].
#[derive(Copy, Clone, Debug, Default, derive_more::Display)]
pub enum TeardownPolicy {
    /// Automatic.
    ///
    /// The current policy is as follows:
    ///
    /// *   Like `Always` if `Transport::Udp` is selected or if the server
    ///     appears to be using a using a [buggy live555
    ///     version](https://github.com/scottlamb/retina/issues/17) in which data
    ///     continues to be sent on a stale file descriptor after a connection is
    ///     closed.
    /// *   Otherwise (TCP, server not known to be buggy), tries a single `TEARDOWN`
    ///     on the existing connection. This is just in case; some servers appear
    ///     to be buggy but don't advertise buggy versions. After the single attempt,
    ///     closes the TCP connection and considers the session done.
    #[default]
    #[display("auto")]
    Auto,

    /// Always send `TEARDOWN` requests, regardless of transport.
    ///
    /// This tries repeatedly to tear down the session until success or expiration;
    /// [`SessionGroup`] will track it also.
    #[display("always")]
    Always,

    /// Never send `TEARDOWN` or track stale sessions.
    #[display("never")]
    Never,
}

impl std::str::FromStr for TeardownPolicy {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s {
            "auto" => TeardownPolicy::Auto,
            "never" => TeardownPolicy::Never,
            "always" => TeardownPolicy::Always,
            _ => bail!(ErrorInt::InvalidArgument(format!(
                "bad TeardownPolicy {s}; expected auto, never, or always"
            ))),
        })
    }
}

/// Policy for handling the `rtptime` parameter normally seen in the `RTP-Info` header.
/// This parameter is used to map each stream's RTP timestamp to NPT ("normal play time"),
/// allowing multiple streams to be played in sync.
#[derive(Copy, Clone, Debug, Default, derive_more::Display)]
pub enum InitialTimestampPolicy {
    /// Default policy: currently `Require` when playing multiple streams,
    /// `Ignore` otherwise.
    #[default]
    #[display("default")]
    Default,

    /// Require the `rtptime` parameter be present and use it to set NPT. Use
    /// when accurate multi-stream NPT is important.
    #[display("require")]
    Require,

    /// Ignore the `rtptime` parameter and assume the first received packet for
    /// each stream is at NPT 0. Use with cameras that are known to set
    /// `rtptime` incorrectly.
    #[display("ignore")]
    Ignore,

    /// Use the `rtptime` parameter when playing multiple streams if it's
    /// specified for all of them; otherwise assume the first received packet
    /// for each stream is at NPT 0.
    #[display("permissive")]
    Permissive,
}

impl std::str::FromStr for InitialTimestampPolicy {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s {
            "default" => InitialTimestampPolicy::Default,
            "require" => InitialTimestampPolicy::Require,
            "ignore" => InitialTimestampPolicy::Ignore,
            "permissive" => InitialTimestampPolicy::Permissive,
            _ => bail!(ErrorInt::InvalidArgument(format!(
                "bad InitialTimestampPolicy {s}; \
                 expected default, require, ignore or permissive"
            ))),
        })
    }
}

/// Policy for handling the `seq` parameter normally seen in the `RTP-Info` header.
#[derive(Copy, Clone, Debug, Default, derive_more::Display)]
#[non_exhaustive]
pub enum InitialSequenceNumberPolicy {
    /// Default policy: currently same as `IgnoreSuspiciousValues`.
    #[default]
    #[display("default")]
    Default,

    /// Always respect the value in the header if present.
    #[display("respect")]
    Respect,

    /// Ignore `0` and `1` values, which we consider "suspicious".
    ///
    /// Some cameras appear to send these fixed values then a completely
    /// different sequence number in the first RTP packet.
    ///
    /// *   The Hikvision DS-2CD2032-I appears to always send `seq=0` on its
    ///     metadata stream.
    /// *   The Tapo C320WS appears to always send `seq=1` in all streams.
    #[display("ignore-suspicious-values")]
    IgnoreSuspiciousValues,

    /// Always ignore, starting the sequence number from observed RTP packets.
    #[display("ignore")]
    Ignore,
}

impl std::str::FromStr for InitialSequenceNumberPolicy {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s {
            "default" => InitialSequenceNumberPolicy::Default,
            "respect" => InitialSequenceNumberPolicy::Respect,
            "ignore-suspicious-values" => InitialSequenceNumberPolicy::IgnoreSuspiciousValues,
            "ignore" => InitialSequenceNumberPolicy::Ignore,
            _ => bail!(ErrorInt::InvalidArgument(format!(
                "bad InitialSequenceNumberPolicy {s}; \
                 expected default, respect, ignore-suspicious-values, or ignore"
            ))),
        })
    }
}

/// Policy for handling unknown `ssrc` value in RTCP messages.
#[derive(Copy, Clone, Debug, Default, derive_more::Display)]
#[non_exhaustive]
pub enum UnknownRtcpSsrcPolicy {
    /// Default policy: currently same as `DropPackets`.
    #[default]
    #[display("default")]
    Default,

    /// Abort the session on encountering an unknown `ssrc`.
    #[display("abort-session")]
    AbortSession,

    /// Drop RTCP packets with an unknown `ssrc`.
    #[display("drop-packets")]
    DropPackets,

    /// Process the packets as if they had the expected `ssrc`.
    #[display("process-packets")]
    ProcessPackets,
}

impl std::str::FromStr for UnknownRtcpSsrcPolicy {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s {
            "default" => UnknownRtcpSsrcPolicy::Default,
            "abort-session" => UnknownRtcpSsrcPolicy::AbortSession,
            "drop-packets" => UnknownRtcpSsrcPolicy::DropPackets,
            "process-packets" => UnknownRtcpSsrcPolicy::ProcessPackets,
            _ => bail!(ErrorInt::InvalidArgument(format!(
                "bad UnknownRtcpSsrcPolicy {s}; \
                 expected default, abort-session, drop-packets, or process-packets"
            ))),
        })
    }
}

/// Returns an appropriate keepalive interval for `session`.
///
/// This generally uses half the session timeout. However, it's capped in case
/// the server offers a generous timeout, messages are rare (e.g. ONVIF metadata
/// streams), and there's a NAT box between with a shorter timeout.
fn keepalive_interval(session: &SessionHeader) -> std::time::Duration {
    std::time::Duration::from_secs(std::cmp::min(u64::from(session.timeout_sec), 60)) / 2
}

/// Options which must be known right as a session is created.
///
/// Decisions which can be deferred are in [`SetupOptions`] or [`PlayOptions`] instead.
#[derive(Default)]
pub struct SessionOptions {
    creds: Option<Credentials>,
    user_agent: Option<Box<str>>,
    session_group: Option<Arc<SessionGroup>>,
    teardown: TeardownPolicy,
    unassigned_channel_data: UnassignedChannelDataPolicy,
    session_id: SessionIdPolicy,
}

/// Policy for handling data received on unassigned RTSP interleaved channels.
#[derive(Copy, Clone, Default, derive_more::Display)]
pub enum UnassignedChannelDataPolicy {
    /// Automatic (default).
    ///
    /// The current policy (which may change) is as follows:
    ///
    /// *   if the server has sent a SDP `tool` attribute for which
    ///     [`Tool::has_live555_tcp_bug`] is true, use `AssumeStaleSession`.
    /// *   otherwise (prior to receiving the `DESCRIBE` response, if there was
    ///     no tool attribute, or if it does not match the known pattern),
    ///     use `Ignore`.
    #[default]
    #[display("auto")]
    Auto,

    /// Assume the data is due to the live555 stale TCP session bug described
    /// in "Stale sessions" under [`SessionGroup`].
    ///
    /// This session will return error, and the `SessionGroup` will track the
    /// expiration of a stale session.
    #[display("assume-stale-session")]
    AssumeStaleSession,

    /// Returns an error.
    #[display("error")]
    Error,

    /// Ignores the data.
    ///
    /// Some broken IP cameras appear to have some default assignment of streams
    /// to interleaved channels. If there is no `SETUP` for that stream before
    /// `PLAY`, they will send data anyway, on this channel. In this mode, such
    /// data messages are ignored.
    #[display("ignore")]
    Ignore,
}

impl std::str::FromStr for UnassignedChannelDataPolicy {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s {
            "auto" => UnassignedChannelDataPolicy::Auto,
            "assume-stale-session" => UnassignedChannelDataPolicy::AssumeStaleSession,
            "error" => UnassignedChannelDataPolicy::Error,
            "ignore" => UnassignedChannelDataPolicy::Ignore,
            _ => bail!(ErrorInt::InvalidArgument(format!(
                "bad UnassignedChannelDataPolicy {s}; expected auto, assume-stale-session, error, \
                 or ignore"
            ))),
        })
    }
}

/// Policy for handling the session ID returned by the server in response to
/// `SETUP` requests.
#[derive(Copy, Clone, Debug, Default, derive_more::Display)]
pub enum SessionIdPolicy {
    /// Default policy: currently `RequireMatch`.
    #[default]
    #[display("default")]
    Default,

    /// Requires the server to return the same session ID for all `SETUP`
    /// requests in the session.
    #[display("require-match")]
    RequireMatch,

    /// Uses the session ID returned from the first `SETUP` request and ignores
    /// any subsequent changes. Required for some broken cameras.
    #[display("use-first")]
    UseFirst,
}

impl std::str::FromStr for SessionIdPolicy {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s {
            "default" => SessionIdPolicy::Default,
            "require-match" => SessionIdPolicy::RequireMatch,
            "use-first" => SessionIdPolicy::UseFirst,
            _ => bail!(ErrorInt::InvalidArgument(format!(
                "bad SessionIdPolicy {s}; \
                 expected default, require-match or use-first"
            ))),
        })
    }
}

/// The RTP packet transport to request.
///
/// Defaults to `Transport::Tcp`.
#[derive(Clone, Debug, derive_more::Display)]
#[non_exhaustive]
pub enum Transport {
    /// Sends RTP packets over the RTSP TCP connection via interleaved data.
    #[display("tcp")]
    Tcp(TcpTransportOptions),

    /// Sends RTP packets over UDP (experimental).
    ///
    /// This support is currently only suitable for a LAN for a couple reasons:
    /// *   There's no reorder buffer, so out-of-order packets are all dropped.
    /// *   There's no support for sending RTCP RRs (receiver reports), so
    ///     servers won't have the correct information to measure packet loss
    ///     and pace packets appropriately.
    #[display("udp")]
    Udp(UdpTransportOptions),
}

impl Default for Transport {
    fn default() -> Self {
        Transport::Tcp(TcpTransportOptions::default())
    }
}

impl std::str::FromStr for Transport {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s {
            "tcp" => Transport::Tcp(TcpTransportOptions::default()),
            "udp" => Transport::Udp(UdpTransportOptions::default()),
            _ => bail!(ErrorInt::InvalidArgument(format!(
                "bad Transport {s}; \
                 expected tcp or udp"
            ))),
        })
    }
}

/// Per-stream TCP transport options (placeholder for future expansion).
#[derive(Clone, Default, Debug)]
#[non_exhaustive]
pub struct TcpTransportOptions;

/// Per-stream UDP transport options (placeholder for future expansion).
#[derive(Clone, Default, Debug)]
#[non_exhaustive]
pub struct UdpTransportOptions;

impl SessionOptions {
    /// Uses the given credentials when/if the server requests digest authentication.
    #[inline]
    pub fn creds(mut self, creds: Option<Credentials>) -> Self {
        self.creds = creds;
        self
    }

    /// Sends the given user agent string with each request.
    pub fn user_agent(mut self, user_agent: String) -> Self {
        self.user_agent = if user_agent.is_empty() {
            None
        } else {
            Some(user_agent.into_boxed_str())
        };
        self
    }

    pub fn session_group(mut self, session_group: Arc<SessionGroup>) -> Self {
        self.session_group = Some(session_group);
        self
    }

    pub fn teardown(mut self, teardown: TeardownPolicy) -> Self {
        self.teardown = teardown;
        self
    }

    pub fn unassigned_channel_data(mut self, policy: UnassignedChannelDataPolicy) -> Self {
        self.unassigned_channel_data = policy;
        self
    }

    pub fn session_id(mut self, policy: SessionIdPolicy) -> Self {
        self.session_id = policy;
        self
    }
}

/// Per-stream options decided at `SETUP` time.
#[derive(Default)]
pub struct SetupOptions {
    transport: Transport,
    frame_format: Option<crate::codec::FrameFormat>,
}

impl SetupOptions {
    /// Sets the underlying transport to use.
    #[inline]
    pub fn transport(mut self, transport: Transport) -> Self {
        self.transport = transport;
        self
    }

    /// Sets the frame format for output assembly.
    ///
    /// This controls H.26x NAL framing (length-prefixed vs Annex B) and
    /// parameter set insertion. See [`crate::codec::FrameFormat`] for details
    /// and preset constants like [`FrameFormat::MP4`](crate::codec::FrameFormat::MP4).
    #[inline]
    pub fn frame_format(mut self, format: crate::codec::FrameFormat) -> Self {
        self.frame_format = Some(format);
        self
    }
}

/// Options which must be decided at `PLAY` time.
///
/// These are mostly adjustments for non-compliant server implementations.
/// See also [`SessionOptions`] for options which must be decided earlier.
#[derive(Default)]
pub struct PlayOptions {
    initial_timestamp: InitialTimestampPolicy,
    initial_seq: InitialSequenceNumberPolicy,
    enforce_timestamps_with_max_jump_secs: Option<NonZeroU32>,
    unknown_rtcp_ssrc: UnknownRtcpSsrcPolicy,
}

impl PlayOptions {
    /// Sets the policy for handling the `rtptime` parameter normally seem in the `RTP-Info` header.
    pub fn initial_timestamp(self, initial_timestamp: InitialTimestampPolicy) -> Self {
        Self {
            initial_timestamp,
            ..self
        }
    }

    pub fn initial_seq(self, initial_seq: InitialSequenceNumberPolicy) -> Self {
        Self {
            initial_seq,
            ..self
        }
    }

    pub fn unknown_rtcp_ssrc(self, unknown_rtcp_ssrc: UnknownRtcpSsrcPolicy) -> Self {
        Self {
            unknown_rtcp_ssrc,
            ..self
        }
    }

    /// If the `RTP-Time` specifies `seq=0` or `seq=1`, ignore it.
    ///
    /// `ignore_zero_seq(true)` is an outdated spelling of
    /// `initial_seq(InitialSequenceNumberPolicy::IgnoreSuspiciousValues)`,
    /// which is currently the default anyway.
    #[deprecated]
    pub fn ignore_zero_seq(self, ignore_zero_seq: bool) -> Self {
        Self {
            initial_seq: match ignore_zero_seq {
                true => InitialSequenceNumberPolicy::IgnoreSuspiciousValues,
                false => InitialSequenceNumberPolicy::Respect,
            },
            ..self
        }
    }

    /// Enforces that timestamps are non-decreasing and jump forward by no more
    /// than the given number of seconds.
    ///
    /// By default, no enforcement is done, and computed [crate::Timestamp]
    /// values will go backward if subsequent 32-bit RTP timestamps differ by
    /// more than `i32::MAX`.
    pub fn enforce_timestamps_with_max_jump_secs(self, secs: NonZeroU32) -> Self {
        Self {
            enforce_timestamps_with_max_jump_secs: Some(secs),
            ..self
        }
    }
}

#[derive(Debug)]
pub(crate) struct Presentation {
    pub streams: Box<[Stream]>,
    base_url: Url,
    pub control: Url,
    tool: Option<Tool>,
}

/// The server's version as declared in the `DESCRIBE` response's `a:tool` SDP attribute.
#[derive(Eq, PartialEq)]
pub struct Tool(Box<str>);

impl Tool {
    pub fn new(raw: &str) -> Self {
        Self(raw.into())
    }

    /// Returns if the given tool is known to be a live555 version that causes
    /// the stale TCP sessions described at [`SessionGroup`].
    pub fn has_live555_tcp_bug(&self) -> bool {
        if let Some(version) = self.0.strip_prefix("LIVE555 Streaming Media v") {
            version > "0000.00.00" && version < "2017.06.04"
        } else {
            false
        }
    }
}

impl std::fmt::Debug for Tool {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Debug::fmt(&*self.0, f)
    }
}

impl std::ops::Deref for Tool {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

/// Information about a stream offered within a presentation.
///
/// Currently if multiple formats are offered, this only describes the first.
pub struct Stream {
    depacketizer: Result<crate::codec::Depacketizer, String>,
    state: StreamState,

    // See the matching accessors for descriptions of these fields.
    media: Box<str>,
    encoding_name: Box<str>,
    rtp_payload_type: u8,
    clock_rate_hz: u32,
    channels: Option<NonZeroU16>,
    framerate: Option<f32>,
    control: Option<Url>,
}

impl std::fmt::Debug for Stream {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
        f.debug_struct("Stream")
            .field("media", &self.media)
            .field("control", &self.control.as_ref().map(Url::as_str))
            .field("encoding_name", &self.encoding_name)
            .field("rtp_payload_type", &self.rtp_payload_type)
            .field("clock_rate", &self.clock_rate_hz)
            .field("channels", &self.channels)
            .field("framerate", &self.framerate)
            .field("depacketizer", &self.depacketizer)
            .field("state", &self.state)
            .finish()
    }
}

impl Stream {
    /// Returns the media type, as specified in the [IANA SDP parameters media
    /// registry](https://www.iana.org/assignments/sdp-parameters/sdp-parameters.xhtml#sdp-parameters-1).
    #[inline]
    pub fn media(&self) -> &str {
        &self.media
    }

    /// An encoding name, as specified in the [IANA media type
    /// registry](https://www.iana.org/assignments/media-types/media-types.xhtml), with
    /// ASCII characters in lowercase.
    ///
    /// Commonly used but not specified in that registry: the ONVIF types
    /// claimed in the
    /// [ONVIF Streaming Spec](https://www.onvif.org/specs/stream/ONVIF-Streaming-Spec.pdf):
    /// *   `vnd.onvif.metadata`
    /// *   `vnd.onvif.metadata.gzip`,
    /// *   `vnd.onvif.metadata.exi.onvif`
    /// *   `vnd.onvif.metadata.exi.ext`
    #[inline]
    pub fn encoding_name(&self) -> &str {
        &self.encoding_name
    }

    /// Returns the RTP payload type.
    ///
    /// See the [registry](https://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml#rtp-parameters-1).
    /// It's common to use one of the dynamically assigned values, 96–127.
    #[inline]
    pub fn rtp_payload_type(&self) -> u8 {
        self.rtp_payload_type
    }

    /// Returns the RTP clock rate, in Hz.
    #[inline]
    pub fn clock_rate_hz(&self) -> u32 {
        self.clock_rate_hz
    }

    /// Returns the number of audio channels, if applicable (`media` is `audio`) and known.
    #[inline]
    pub fn channels(&self) -> Option<NonZeroU16> {
        self.channels
    }

    /// Returns the video framerate if present in SDP attributes.
    #[inline]
    pub fn framerate(&self) -> Option<f32> {
        self.framerate
    }

    /// Returns the specified control URL.
    ///
    /// This is needed with multiple streams to send `SETUP` requests and
    /// interpret the `PLAY` response's `RTP-Info` header.
    /// [RFC 2326 section C.3](https://datatracker.ietf.org/doc/html/rfc2326#appendix-C.3)
    /// says the server is allowed to omit it when there is only a single stream.
    #[inline]
    pub fn control(&self) -> Option<&Url> {
        self.control.as_ref()
    }
}

struct UdpSockets {
    rtp: UdpSocket,
    rtcp: UdpSocket,
}

/// Placeholder `Debug` impl to allow `UdpSockets` to be a field within a `#[derive(Debug)]` struct.
impl std::fmt::Debug for UdpSockets {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("UdpSockets").finish()
    }
}

impl Stream {
    /// Returns codec-specified parameters for this stream, if available.
    ///
    /// Returns `None` on unknown codecs, bad parameters, or if parameters aren't (yet) known.
    ///
    /// This is initially populated from the `DESCRIBE` response's SDP. Not all codecs guarantee
    /// parameters are provided in the SDP. Notably, H.264 allows parameters to be specified
    /// "in-band" (with the data packets) instead of or in addition to "out-of-band" (via SDP).
    /// Thus, it's unspecified whether a `parameters` call immediately after `Session::describe`
    /// will return `Some` or `None`.
    ///
    /// Additionally, parameters obtained before [`Session::setup`](crate::client::Session::setup)
    /// always use the default framing. After `setup`, parameters reflect the supplied
    /// [`SetupOptions::frame_format`](crate::client::SetupOptions::frame_format).
    ///
    /// # With [`Demuxed`]
    ///
    /// When using [`Demuxed`]'s frame-by-frame `futures::Stream` impl, `parameters` reflects
    /// all parameters as of returned frames that have been returned from from `poll_next` via
    /// `Poll::Ready`.
    ///
    /// It's guaranteed to *not* reflect any parameter changes in the upcoming frame, even after
    /// a `Poll::Pending` return.
    ///
    /// If there is no packet loss, parameters are generally available after the first frame is
    /// returned. In the case of H.264, [RFC 6184 section
    /// 8.4](https://datatracker.ietf.org/doc/html/rfc6184#section-8.4) says "when parameter sets
    /// are added or updated, care SHOULD be taken to ensure that any parameter set is delivered
    /// prior to its usage."
    ///
    /// # Without [`Demuxed`]
    ///
    /// When directly using [`Session`]'s packet-by-packet `futures::Stream` impl, codec
    /// depacketization logic is bypassed. The parameters returned by this function may be out of
    /// date.
    pub fn parameters(&self) -> Option<crate::codec::ParametersRef<'_>> {
        self.depacketizer.as_ref().ok().and_then(|d| d.parameters())
    }

    /// Returns a context for this stream, if it has been set up.
    pub fn ctx(&self) -> Option<&StreamContext> {
        match &self.state {
            StreamState::Uninit => None,
            StreamState::Init(init) => Some(&init.ctx),
            StreamState::Playing { ctx, .. } => Some(ctx),
        }
    }
}

#[derive(Debug)]
enum StreamState {
    /// Uninitialized; no `SETUP` has yet been sent.
    Uninit,

    /// `SETUP` response has been received.
    Init(StreamStateInit),

    /// `PLAY` response has been received.
    Playing {
        timeline: Timeline,
        rtp_handler: rtp::InorderParser,
        ctx: StreamContext,
        udp_sockets: Option<UdpSockets>,
    },
}

#[derive(Debug)]
struct StreamStateInit {
    /// The RTP synchronization source (SSRC), as defined in
    /// [RFC 3550](https://tools.ietf.org/html/rfc3550). This is normally
    /// supplied in the `SETUP` response's `Transport` header. Reolink cameras
    /// instead supply it in the `PLAY` response's `RTP-Info` header.
    ssrc: Option<u32>,

    /// The initial RTP sequence number, as specified in the `PLAY` response's
    /// `RTP-Info` header. This field is only used during the `play()` call
    /// itself; by the time it returns, the stream will be in state `Playing`.
    initial_seq: Option<u16>,

    /// The initial RTP timestamp, as specified in the `PLAY` response's
    /// `RTP-Info` header. This field is only used during the `play()` call
    /// itself; by the time it returns, the stream will be in state `Playing`.
    initial_rtptime: Option<u32>,

    ctx: StreamContext,
    udp_sockets: Option<UdpSockets>,
}

/// Username and password authentication credentials.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Credentials {
    pub username: String,
    pub password: String,
}

/// Marker trait for the state of a [Session].
/// This doesn't closely match [RFC 2326
/// A.1](https://tools.ietf.org/html/rfc2326#appendix-A.1). In practice, we've
/// found that cheap IP cameras are more restrictive than RTSP suggests. Eg, a
/// `DESCRIBE` changes the connection's state such that another one will fail,
/// before assigning a session id. Thus [Session] represents something more like
/// an RTSP connection than an RTSP session.
#[doc(hidden)]
pub trait State {}

/// Initial state after a `DESCRIBE`; use via `Session<Described>`.
#[doc(hidden)]
pub struct Described {
    sdp: Bytes,
}
impl State for Described {}

enum KeepaliveState {
    Idle,
    Flushing { cseq: u32, method: KeepaliveMethod },
    Waiting { cseq: u32, method: KeepaliveMethod },
}

#[repr(u8)]
#[derive(Copy, Clone, Debug)]
enum KeepaliveMethod {
    Options,
    SetParameter,
    GetParameter,
}

impl From<KeepaliveMethod> for msg::Method {
    fn from(method: KeepaliveMethod) -> Self {
        match method {
            KeepaliveMethod::Options => msg::Method::OPTIONS,
            KeepaliveMethod::SetParameter => msg::Method::SET_PARAMETER,
            KeepaliveMethod::GetParameter => msg::Method::GET_PARAMETER,
        }
    }
}

/// State after a `PLAY`; use via `Session<Playing>`.
#[doc(hidden)]
pub struct Playing(());
impl State for Playing {}

/// The raw connection, without tracking session state.
struct RtspConnection {
    inner: crate::tokio::Connection,
    channels: ChannelMappings,

    /// The next `CSeq` header value to use when sending an RTSP request.
    next_cseq: u32,

    /// If Retina has received data on an unassigned RTSP interleaved data channel.
    seen_unassigned: bool,
}

/// Mode to use in `RtspConnection::send` when looking for a response.
enum ResponseMode {
    /// Anything but the response to this request is an error.
    Normal,

    /// Silently discard data messages on assigned channels.
    ///
    /// This is a workaround for recent Reolink cameras which appear to send
    /// RTCP sender reports immediately *before* the `PLAY` response when
    /// using interleaved data. It's simplest to discard them rather than
    /// attempt to interpret them before having `RTP-Info`.
    Play,

    /// Discard data messages and unrelated responses while awaiting the
    /// response to this request.
    Teardown,
}

/// An RTSP session.
///
/// The expected lifecycle is as follows:
///
/// 1. Create a session via [`Session::describe`].
/// 2. Examine the session's available streams via [`Session::streams`] and set
///    up one or more via [`Session::setup`].
/// 3. Start playing via [`Session::play`].
/// 4. Get packets via the [`futures::stream::Stream`] impl on `Session<Playing>`,
///    or frames via the [`futures::stream::Stream`] impl returned by [`Session<Playing>::demuxed`].
/// 5. Drop the session. Retina may issue a `TEARDOWN` in the background, depending on the
///    [`SessionOptions::teardown`] parameter.
/// 6. Possibly wait for `TEARDOWN` to complete; see
///    [`SessionOptions::session_group`] and [`SessionGroup::await_teardown`].
///
/// ## tokio runtime
///
/// All `Session` operations are currently expected to be performed from
/// "within" a tokio runtime with both time and I/O resource drivers enabled.
/// Operations may panic or fail otherwise.
pub struct Session<S: State>(Pin<Box<SessionInner>>, S);

#[pin_project(PinnedDrop)]
struct SessionInner {
    /// The connection. Currently there's expected to always be a RTSP
    /// connection, even while playing a RTP/AVP/UDP session. The only
    /// exception is during drop.
    conn: Option<RtspConnection>,

    options: SessionOptions,
    requested_auth: Option<http_auth::PasswordClient>,
    presentation: Presentation,

    /// This will be set iff one or more `SETUP` calls have been issued.
    /// This is sometimes true in state `Described` and always true in state
    /// `Playing`.
    session: Option<parse::SessionHeader>,

    // Keep some information about the DESCRIBE response. If a depacketizer
    // couldn't be constructed correctly for one or more streams, this will be
    // used to create a RtspResponseError on `State<Playing>::demuxed()`.
    // We defer such errors from DESCRIBE time until then because they only
    // matter if the stream is setup and the caller wants depacketization.
    describe_ctx: RtspMessageContext,
    describe_cseq: u32,
    describe_status: StatusCode,

    /// The state of the keepalive request; only used in state `Playing`.
    keepalive_state: KeepaliveState,

    keepalive_timer: Option<Pin<Box<tokio::time::Sleep>>>,

    /// Bitmask of [`SessionFlag`]s.
    flags: u8,

    /// The index within `presentation.streams` to start the next poll at.
    /// Round-robining between them rather than always starting at 0 should
    /// prevent one stream from starving the others.
    udp_next_poll_i: usize,
}

#[derive(Copy, Clone)]
#[repr(u8)]
enum SessionFlag {
    /// Set if the server may be in state Playing: we have sent a `PLAY`
    /// request, regardless of if the response has been received.
    MaybePlaying = 0x1,

    /// Set if one or more streams are configured to use TCP.
    TcpStreams = 0x2,

    /// Set if one or more streams are configured to use UDP.
    UdpStreams = 0x4,

    /// Set if an `OPTIONS` request has completed and advertised supported for
    /// `SET_PARAMETER`.
    SetParameterSupported = 0x8,

    /// Set if an `OPTIONS` request has completed and advertised supported for
    /// `GET_PARAMETER`.
    GetParameterSupported = 0x10,
}

impl RtspConnection {
    async fn connect(url: &Url) -> Result<Self, Error> {
        let host =
            RtspConnection::validate_url(url).map_err(|e| wrap!(ErrorInt::InvalidArgument(e)))?;
        let port = url.port().unwrap_or(554);
        let inner = crate::tokio::Connection::connect(host, port)
            .await
            .map_err(|e| wrap!(ErrorInt::ConnectError(e)))?;
        Ok(Self {
            inner,
            channels: ChannelMappings::default(),
            next_cseq: 1,
            seen_unassigned: false,
        })
    }

    fn validate_url(url: &Url) -> Result<url::Host<&str>, String> {
        if url.scheme() != "rtsp" {
            return Err(format!(
                "Bad URL {}; only scheme rtsp supported",
                url.as_str()
            ));
        }
        if url.username() != "" || url.password().is_some() {
            // Url apparently doesn't even have a way to clear the credentials,
            // so this has to be an error.
            // TODO: that's not true; revisit this.
            return Err("URL must not contain credentials".to_owned());
        }
        url.host()
            .ok_or_else(|| format!("Must specify host in rtsp url {}", &url))
    }

    /// Sends a request and expects an upcoming message from the peer to be its response.
    /// Takes care of authorization and `CSeq`. Returns `Error` if not successful.
    async fn send(
        &mut self,
        mode: ResponseMode,
        options: &SessionOptions,
        tool: Option<&Tool>,
        requested_auth: &mut Option<http_auth::PasswordClient>,
        req: &mut OwnedMessage,
    ) -> Result<(RtspMessageContext, u32, msg::Response, Bytes), Error> {
        loop {
            let cseq = self.fill_req(options, requested_auth, req)?;
            self.inner.send(req.clone()).await.map_err(|e| wrap!(e))?;
            let method: &str = req.method_str();
            let (resp, resp_body, msg_ctx) = loop {
                let msg = self.inner.next().await.unwrap_or_else(|| {
                    bail!(ErrorInt::RtspReadError {
                        conn_ctx: *self.inner.ctx(),
                        msg_ctx: self.inner.eof_ctx(),
                        source: std::io::Error::new(
                            std::io::ErrorKind::UnexpectedEof,
                            format!("EOF while expecting response to {method} CSeq {cseq}"),
                        ),
                    })
                })?;
                let msg_ctx = msg.ctx;
                let description = match msg.msg {
                    msg::Message::Response(r) => {
                        if let Some(response_cseq) = parse::get_cseq(&r) {
                            if response_cseq == cseq {
                                break (r, msg.body, msg_ctx);
                            }
                            if matches!(mode, ResponseMode::Teardown) {
                                debug!("ignoring unrelated response during TEARDOWN");
                                continue;
                            }
                            format!("{} response with CSeq {}", r.reason_phrase, response_cseq)
                        } else {
                            format!("{} response with no/unparseable cseq", r.reason_phrase)
                        }
                    }
                    msg::Message::Data(d) => {
                        if matches!(mode, ResponseMode::Teardown) {
                            debug!("ignoring RTSP interleaved data during TEARDOWN");
                            continue;
                        } else if let (ResponseMode::Play, Some(m)) =
                            (&mode, self.channels.lookup(d.channel_id))
                        {
                            debug!(
                                "ignoring interleaved data message on {:?} channel {} while \
                                     waiting for response to {} CSeq {}",
                                m.channel_type, d.channel_id, method, cseq
                            );
                            continue;
                        }
                        self.handle_unassigned_data(
                            msg_ctx,
                            options,
                            tool,
                            d.channel_id,
                            msg.body,
                        )?;
                        continue;
                    }
                    msg::Message::Request(r) => format!("{:?} request", r.method),
                };
                bail!(ErrorInt::RtspFramingError {
                    conn_ctx: *self.inner.ctx(),
                    msg_ctx,
                    description: format!(
                        "Expected response to {method} CSeq {cseq}, got {description}",
                    ),
                });
            };
            if resp.status_code == StatusCode::UNAUTHORIZED {
                if requested_auth.is_some() {
                    // TODO: the WWW-Authenticate might indicate a new domain or nonce.
                    // In that case, we should retry rather than returning error.
                    bail!(ErrorInt::RtspResponseError {
                        conn_ctx: *self.inner.ctx(),
                        msg_ctx,
                        method: req.method().clone(),
                        cseq,
                        status: resp.status_code,
                        description: "Received Unauthorized after trying digest auth".into(),
                    })
                }
                let www_authenticate = match resp.headers.get("WWW-Authenticate") {
                    None => bail!(ErrorInt::RtspResponseError {
                        conn_ctx: *self.inner.ctx(),
                        msg_ctx,
                        method: req.method().clone(),
                        cseq,
                        status: resp.status_code,
                        description: "Unauthorized without WWW-Authenticate header".into(),
                    }),
                    Some(h) => h,
                };
                if options.creds.is_none() {
                    bail!(ErrorInt::RtspResponseError {
                        conn_ctx: *self.inner.ctx(),
                        msg_ctx,
                        method: req.method().clone(),
                        cseq,
                        status: resp.status_code,
                        description: "Authentication requested and no credentials supplied"
                            .to_owned(),
                    })
                }
                let www_authenticate: &str = www_authenticate;
                *requested_auth = match http_auth::PasswordClient::try_from(www_authenticate) {
                    Ok(c) => Some(c),
                    Err(e) => bail!(ErrorInt::RtspResponseError {
                        conn_ctx: *self.inner.ctx(),
                        msg_ctx,
                        method: req.method().clone(),
                        cseq,
                        status: resp.status_code,
                        description: format!("Can't understand WWW-Authenticate header: {e}"),
                    }),
                };
                continue;
            } else if !resp.status_code.is_success() {
                bail!(ErrorInt::RtspResponseError {
                    conn_ctx: *self.inner.ctx(),
                    msg_ctx,
                    method: req.method().clone(),
                    cseq,
                    status: resp.status_code,
                    description: "Unexpected RTSP response status".into(),
                });
            }
            return Ok((msg_ctx, cseq, resp, resp_body));
        }
    }

    /// Handles data on an unassigned RTSP channel.
    fn handle_unassigned_data(
        &mut self,
        msg_ctx: RtspMessageContext,
        options: &SessionOptions,
        tool: Option<&Tool>,
        channel_id: u8,
        data: Bytes,
    ) -> Result<(), Error> {
        let live555 = match options.unassigned_channel_data {
            UnassignedChannelDataPolicy::Auto
                if tool.map(Tool::has_live555_tcp_bug).unwrap_or(false) =>
            {
                true
            }
            UnassignedChannelDataPolicy::AssumeStaleSession => true,
            UnassignedChannelDataPolicy::Error => false,
            UnassignedChannelDataPolicy::Ignore | UnassignedChannelDataPolicy::Auto => {
                if !self.seen_unassigned {
                    log::warn!(
                        "Ignoring data on unassigned RTSP interleaved data channel {}. \
                         This is the first such message. Following messages will be logged \
                         at trace priority only.\n\n\
                         conn: {}\nmsg: {}\ndata: {:#?}",
                        channel_id,
                        self.inner.ctx(),
                        &msg_ctx,
                        crate::hex::LimitedHex::new(&data, 128),
                    );
                    self.seen_unassigned = true;
                } else {
                    log::trace!(
                        "Ignoring data on unassigned RTSP interleaved data channel {}.\n\n\
                         conn: {}\nmsg: {}\ndata: {:#?}",
                        channel_id,
                        self.inner.ctx(),
                        &msg_ctx,
                        crate::hex::LimitedHex::new(&data, 128),
                    );
                }
                return Ok(());
            }
        };

        if live555 {
            note_stale_live555_data(tool, options, self.inner.ctx(), channel_id, &msg_ctx);
        }

        bail!(ErrorInt::RtspUnassignedChannelError {
            conn_ctx: *self.inner.ctx(),
            msg_ctx,
            channel_id,
            data,
        });
    }

    /// Fills out `req` with authorization and `CSeq` headers.
    fn fill_req(
        &mut self,
        options: &SessionOptions,
        requested_auth: &mut Option<http_auth::PasswordClient>,
        req: &mut OwnedMessage,
    ) -> Result<u32, Error> {
        let cseq = self.next_cseq;
        self.next_cseq += 1;
        if let Some(auth) = requested_auth {
            let creds = options
                .creds
                .as_ref()
                .expect("creds were checked when filling request_auth");
            let authorization = auth
                .respond(&http_auth::PasswordParams {
                    username: &creds.username,
                    password: &creds.password,
                    uri: req.request_uri_str(),
                    method: req.method_str(),
                    body: Some(&[]),
                })
                .map_err(|e| wrap!(ErrorInt::Internal(e.into())))?;
            let headers = req.headers_mut();
            headers.insert(
                msg::HeaderName::AUTHORIZATION,
                msg::HeaderValue::try_from(authorization).unwrap(),
            );
        }
        let headers = req.headers_mut();
        headers.insert(
            msg::HeaderName::CSEQ,
            msg::HeaderValue::try_from(cseq.to_string()).unwrap(),
        );

        let user_agent = if let Some(ref u) = options.user_agent {
            u
        } else {
            DEFAULT_USER_AGENT
        };
        headers.insert(
            msg::HeaderName::USER_AGENT,
            msg::HeaderValue::try_from(user_agent.to_string()).unwrap(),
        );

        Ok(cseq)
    }
}

const DEFAULT_USER_AGENT: &str = concat!("retina_", env!("CARGO_PKG_VERSION"));

impl<S: State> Session<S> {
    /// Returns the available streams as described by the server.
    pub fn streams(&self) -> &[Stream] {
        &self.0.presentation.streams
    }

    /// Returns the server's version as declared in the `DESCRIBE` response's `a:tool` SDP
    /// attribute.
    pub fn tool(&self) -> Option<&Tool> {
        self.0.presentation.tool.as_ref()
    }
}

impl Session<Described> {
    /// Creates a new session from a `DESCRIBE` request on the given URL.
    ///
    /// This method is permissive; it will return success even if there are
    /// errors in the SDP that would prevent one or more streams from being
    /// depacketized correctly. If those streams are setup via
    /// `Session<Described>::setup`, the erorrs in question will be ultimately
    /// returned from `Stream<Playing>::demuxed`.
    ///
    /// Expects to be called from a tokio runtime.
    pub async fn describe(url: Url, options: SessionOptions) -> Result<Self, Error> {
        let conn = RtspConnection::connect(&url).await?;
        Self::describe_with_conn(conn, options, url).await
    }

    async fn describe_with_conn(
        mut conn: RtspConnection,
        options: SessionOptions,
        url: Url,
    ) -> Result<Self, Error> {
        let mut req = OwnedMessage::Request {
            head: msg::Request {
                method: msg::Method::DESCRIBE,
                request_uri: Some(url.clone()),
                headers: [(
                    msg::HeaderName::ACCEPT,
                    msg::HeaderValue::try_from("application/sdp").unwrap(),
                )]
                .into(),
            },
            body: Bytes::new(),
        };
        let mut requested_auth = None;
        let (msg_ctx, cseq, response, resp_body) = conn
            .send(
                ResponseMode::Normal,
                &options,
                None, // tool isn't known until after the DESCRIBE response is parsed below.
                &mut requested_auth,
                &mut req,
            )
            .await?;
        let presentation =
            parse::parse_describe(url, &response, &resp_body).map_err(|description| {
                wrap!(ErrorInt::RtspResponseError {
                    conn_ctx: *conn.inner.ctx(),
                    msg_ctx,
                    method: msg::Method::DESCRIBE,
                    cseq,
                    status: response.status_code,
                    description,
                })
            })?;
        let describe_status = response.status_code;
        let sdp = resp_body;
        Ok(Session(
            Box::pin(SessionInner {
                conn: Some(conn),
                options,
                requested_auth,
                presentation,
                session: None,
                describe_ctx: msg_ctx,
                describe_cseq: cseq,
                describe_status,
                keepalive_state: KeepaliveState::Idle,
                keepalive_timer: None,
                flags: 0,
                udp_next_poll_i: 0,
            }),
            Described { sdp },
        ))
    }

    /// Returns the raw SDP (Session Description Protocol) session description of this URL.
    ///
    /// Retina interprets the SDP automatically, but the raw bytes may be useful for debugging.
    /// They're accessibled in the `Session<Described>` state. Currently, they're discarded on
    /// `play` to reduce memory usage.
    pub fn sdp(&self) -> &[u8] {
        &self.1.sdp
    }

    /// Sends a `SETUP` request for a stream.
    ///
    /// Note these can't reasonably be pipelined because subsequent requests
    /// are expected to adopt the previous response's `Session`. Likewise,
    /// the server may override the preferred interleaved channel id and it
    /// seems like a bad idea to try to assign more interleaved channels without
    /// inspect that first.
    ///
    /// Panics if `stream_i >= self.streams().len()`.
    pub async fn setup(&mut self, stream_i: usize, options: SetupOptions) -> Result<(), Error> {
        let inner = &mut self.0.as_mut().project();
        let conn = inner
            .conn
            .as_mut()
            .ok_or_else(|| wrap!(ErrorInt::FailedPrecondition("no connection".into())))?;
        let stream = &mut inner.presentation.streams[stream_i];
        if !matches!(stream.state, StreamState::Uninit) {
            bail!(ErrorInt::FailedPrecondition("stream already set up".into()));
        }
        let url = stream
            .control
            .as_ref()
            .unwrap_or(&inner.presentation.control)
            .clone();
        let mut headers = msg::Headers::default();
        let udp = match options.transport {
            Transport::Tcp(_) => {
                let proposed_channel_id = conn.channels.next_unassigned().ok_or_else(|| {
                    wrap!(ErrorInt::FailedPrecondition(
                        "no unassigned channels".into()
                    ))
                })?;
                headers.insert(
                    msg::HeaderName::TRANSPORT,
                    msg::HeaderValue::try_from(format!(
                        "RTP/AVP/TCP;unicast;interleaved={}-{}",
                        proposed_channel_id,
                        proposed_channel_id + 1
                    ))
                    .unwrap(),
                );
                *inner.flags |= SessionFlag::TcpStreams as u8;
                None
            }
            Transport::Udp(_) => {
                // Bind an ephemeral UDP port on the same local address used to connect
                // to the RTSP server.
                let local_ip = conn.inner.ctx().local_addr.ip();
                let pair = crate::tokio::UdpPair::for_ip(local_ip)
                    .map_err(|e| wrap!(ErrorInt::Internal(e.into())))?;
                headers.insert(
                    msg::HeaderName::TRANSPORT,
                    msg::HeaderValue::try_from(format!(
                        "RTP/AVP/UDP;unicast;client_port={}-{}",
                        pair.rtp_port,
                        pair.rtp_port + 1,
                    ))
                    .unwrap(),
                );
                *inner.flags |= SessionFlag::UdpStreams as u8;
                Some((
                    UdpStreamContext {
                        local_ip,
                        peer_ip: IpAddr::V4(Ipv4Addr::UNSPECIFIED),
                        local_rtp_port: pair.rtp_port,
                        peer_rtp_port: 0,
                    },
                    UdpSockets {
                        rtp: pair.rtp_socket,
                        rtcp: pair.rtcp_socket,
                    },
                ))
            }
        };
        if let &mut Some(ref s) = inner.session {
            headers.insert(
                msg::HeaderName::SESSION,
                msg::HeaderValue::try_from(s.id.to_string()).unwrap(),
            );
        }
        let mut req = OwnedMessage::Request {
            head: msg::Request {
                method: msg::Method::SETUP,
                request_uri: Some(url),
                headers,
            },
            body: Bytes::new(),
        };
        let (msg_ctx, cseq, response, _resp_body) = conn
            .send(
                ResponseMode::Normal,
                inner.options,
                inner.presentation.tool.as_ref(),
                inner.requested_auth,
                &mut req,
            )
            .await?;
        debug!("SETUP response: {:#?}", &response);
        let conn_ctx = conn.inner.ctx();
        let status = response.status_code;
        let response = parse::parse_setup(&response).map_err(|description| {
            wrap!(ErrorInt::RtspResponseError {
                conn_ctx: *conn_ctx,
                msg_ctx,
                method: msg::Method::SETUP,
                cseq,
                status,
                description,
            })
        })?;
        match inner.session.as_ref() {
            Some(SessionHeader { id, .. }) if id.as_ref() != &*response.session.id => {
                match inner.options.session_id {
                    SessionIdPolicy::UseFirst => (),
                    _ => {
                        bail!(ErrorInt::RtspResponseError {
                            conn_ctx: *conn.inner.ctx(),
                            msg_ctx,
                            method: msg::Method::SETUP,
                            cseq,
                            status,
                            description: format!(
                                "session id changed from {:?} to {:?}",
                                id, response.session.id,
                            ),
                        });
                    }
                }
            }
            Some(_) => {}
            None => {
                debug!(
                    "established session {:?}, timeout={}s",
                    response.session.id, response.session.timeout_sec
                );
                *inner.session = Some(response.session)
            }
        };
        let conn_ctx = conn.inner.ctx();
        let (stream_ctx, udp_sockets);
        match udp {
            None => {
                let channel_id = match response.channel_id {
                    Some(id) => id,
                    None => bail!(ErrorInt::RtspResponseError {
                        conn_ctx: *conn.inner.ctx(),
                        msg_ctx,
                        method: msg::Method::SETUP,
                        cseq,
                        status,
                        description: "Transport header is missing interleaved parameter".to_owned(),
                    }),
                };
                conn.channels
                    .assign(channel_id, stream_i)
                    .map_err(|description| {
                        wrap!(ErrorInt::RtspResponseError {
                            conn_ctx: *conn_ctx,
                            msg_ctx,
                            method: msg::Method::SETUP,
                            cseq,
                            status,
                            description,
                        })
                    })?;
                stream_ctx = StreamContext(StreamContextInner::Tcp(TcpStreamContext {
                    rtp_channel_id: channel_id,
                }));
                udp_sockets = None;
            }
            Some((mut ctx, sockets)) => {
                // TODO: RFC 2326 section 12.39 says "If the source address for
                // the stream is different than can be derived from the RTSP
                // endpoint address (the server in playback or the client in
                // recording), the source MAY be specified." Not MUST,
                // unfortunately. But let's see if we can get away with this
                // for now.
                let source = match response.source {
                    Some(s) => s,
                    None => conn.inner.ctx().peer_addr.ip(),
                };
                let server_port = response.server_port.ok_or_else(|| {
                    wrap!(ErrorInt::RtspResponseError {
                        conn_ctx: *conn_ctx,
                        msg_ctx,
                        method: msg::Method::SETUP,
                        cseq,
                        status,
                        description: "Transport header is missing server_port parameter".to_owned(),
                    })
                })?;
                ctx.peer_ip = source;
                ctx.peer_rtp_port = server_port;
                sockets
                    .rtp
                    .connect(SocketAddr::new(source, server_port))
                    .await
                    .map_err(|e| wrap!(ErrorInt::ConnectError(e)))?;
                sockets
                    .rtcp
                    .connect(SocketAddr::new(source, server_port + 1))
                    .await
                    .map_err(|e| wrap!(ErrorInt::ConnectError(e)))?;
                punch_firewall_hole(&sockets)
                    .await
                    .map_err(|e| wrap!(ErrorInt::ConnectError(e)))?;
                stream_ctx = StreamContext(StreamContextInner::Udp(ctx));
                udp_sockets = Some(sockets);
            }
        };
        if let Some(format) = options.frame_format
            && let Ok(d) = &mut stream.depacketizer
        {
            d.set_frame_format(format);
        }
        stream.state = StreamState::Init(StreamStateInit {
            ssrc: response.ssrc,
            initial_seq: None,
            initial_rtptime: None,
            ctx: stream_ctx,
            udp_sockets,
        });
        Ok(())
    }

    /// Sends a `PLAY` request for the entire presentation.
    ///
    /// The presentation must support aggregate control, as defined in [RFC 2326
    /// section 1.3](https://tools.ietf.org/html/rfc2326#section-1.3).
    pub async fn play(mut self, policy: PlayOptions) -> Result<Session<Playing>, Error> {
        let inner = self.0.as_mut().project();
        let conn = inner
            .conn
            .as_mut()
            .ok_or_else(|| wrap!(ErrorInt::FailedPrecondition("no connection".into())))?;
        let session = inner.session.as_ref().ok_or_else(|| {
            wrap!(ErrorInt::FailedPrecondition(
                "must SETUP before PLAY".into()
            ))
        })?;
        if let Some(ref t) = inner.presentation.tool
            && (*inner.flags & (SessionFlag::TcpStreams as u8)) != 0
            && t.has_live555_tcp_bug()
        {
            warn!(
                "Connecting via TCP to known-broken RTSP server {:?}. \
                        See <https://github.com/scottlamb/retina/issues/17>. \
                        Consider using UDP instead!",
                t
            );
        }

        trace!("PLAY with channel mappings: {:#?}", &conn.channels);
        *inner.flags |= SessionFlag::MaybePlaying as u8;
        let (msg_ctx, cseq, response, _resp_body) = conn
            .send(
                ResponseMode::Play,
                inner.options,
                inner.presentation.tool.as_ref(),
                inner.requested_auth,
                &mut OwnedMessage::Request {
                    head: msg::Request {
                        method: msg::Method::PLAY,
                        request_uri: Some(inner.presentation.control.clone()),
                        headers: [
                            (
                                msg::HeaderName::SESSION,
                                msg::HeaderValue::try_from(&*session.id).unwrap(),
                            ),
                            (
                                msg::HeaderName::RANGE,
                                msg::HeaderValue::try_from("npt=0.000-").unwrap(),
                            ),
                        ]
                        .into(),
                    },
                    body: Bytes::new(),
                },
            )
            .await?;
        parse::parse_play(&response, inner.presentation).map_err(|description| {
            wrap!(ErrorInt::RtspResponseError {
                conn_ctx: *conn.inner.ctx(),
                msg_ctx,
                method: msg::Method::PLAY,
                cseq,
                status: response.status_code,
                description,
            })
        })?;

        // Count how many streams have been setup (not how many are in the presentation).
        let setup_streams = inner
            .presentation
            .streams
            .iter()
            .filter(|s| matches!(s.state, StreamState::Init(_)))
            .count();

        let all_have_time = inner.presentation.streams.iter().all(|s| match s.state {
            StreamState::Init(StreamStateInit {
                initial_rtptime, ..
            }) => initial_rtptime.is_some(),
            _ => true,
        });

        // Move all streams that have been set up from Init to Playing state. Check that required
        // parameters are present while doing so.
        for (i, s) in inner.presentation.streams.iter_mut().enumerate() {
            match std::mem::replace(&mut s.state, StreamState::Uninit) {
                StreamState::Init(StreamStateInit {
                    initial_rtptime,
                    initial_seq,
                    ssrc,
                    ctx,
                    udp_sockets,
                }) => {
                    let initial_rtptime = match policy.initial_timestamp {
                        InitialTimestampPolicy::Require | InitialTimestampPolicy::Default
                            if setup_streams > 1 =>
                        {
                            if initial_rtptime.is_none() {
                                bail!(ErrorInt::RtspResponseError {
                                    conn_ctx: *conn.inner.ctx(),
                                    msg_ctx,
                                    method: msg::Method::PLAY,
                                    cseq,
                                    status: response.status_code,
                                    description: format!(
                                        "Expected rtptime on PLAY with mode {:?}, missing on \
                                             stream {} ({:?}). Consider setting initial timestamp \
                                             mode permissive.",
                                        policy.initial_timestamp, i, &s.control
                                    ),
                                });
                            }
                            initial_rtptime
                        }
                        InitialTimestampPolicy::Permissive
                            if setup_streams > 1 && all_have_time =>
                        {
                            initial_rtptime
                        }
                        _ => None,
                    };
                    let initial_seq = match (initial_seq, policy.initial_seq) {
                        (Some(seq), InitialSequenceNumberPolicy::Ignore)
                        | (
                            Some(seq @ 0 | seq @ 1),
                            InitialSequenceNumberPolicy::Default
                            | InitialSequenceNumberPolicy::IgnoreSuspiciousValues,
                        ) => {
                            log::info!(
                                "ignoring PLAY seq={} on stream {} due to policy {:?}",
                                seq,
                                i,
                                policy.initial_seq
                            );
                            None
                        }
                        (Some(seq), _) => {
                            log::debug!("setting PLAY seq={} on stream {}", seq, i);
                            Some(seq)
                        }
                        (None, _) => {
                            log::debug!("no PLAY seq on stream {}", i);
                            None
                        }
                    };
                    let conn_ctx = conn.inner.ctx();
                    s.state = StreamState::Playing {
                        timeline: Timeline::new(
                            initial_rtptime,
                            s.clock_rate_hz,
                            policy.enforce_timestamps_with_max_jump_secs,
                        )
                        .map_err(|description| {
                            wrap!(ErrorInt::RtspResponseError {
                                conn_ctx: *conn_ctx,
                                msg_ctx,
                                method: msg::Method::PLAY,
                                cseq,
                                status: response.status_code,
                                description,
                            })
                        })?,
                        rtp_handler: rtp::InorderParser::new(
                            ssrc,
                            initial_seq,
                            policy.unknown_rtcp_ssrc,
                        ),
                        ctx,
                        udp_sockets,
                    };
                }
                StreamState::Uninit => {}
                StreamState::Playing { .. } => unreachable!(),
            };
        }
        *inner.keepalive_timer = Some(Box::pin(tokio::time::sleep(keepalive_interval(session))));
        Ok(Session(self.0, Playing(())))
    }
}

/// Notes an unexpected RTSP interleaved data message.
///
/// This is assumed to be due to a live555 RTP/AVP/TCP session that belonged
/// to a since-closed RTSP connection, as described in case 2 of "Stale sessions"
/// at [`SessionGroup`]. If there's no known session which explains this,
/// adds an unknown session with live555's default timeout.
fn note_stale_live555_data(
    tool: Option<&Tool>,
    options: &SessionOptions,
    conn_ctx: &crate::ConnectionContext,
    channel_id: u8,
    msg_ctx: &RtspMessageContext,
) {
    let known_to_have_live555_tcp_bug = tool.map(Tool::has_live555_tcp_bug).unwrap_or(false);
    if !known_to_have_live555_tcp_bug {
        log::warn!(
            "saw unexpected RTSP packet. This is presumed to be due to a bug in old
             live555 servers' TCP handling, though tool attribute {tool:?} does not refer to a \
             known-buggy version. Consider switching to UDP.\n\n\
             conn: {conn_ctx:?}\n\
             channel: {channel_id}\n\
             msg: {msg_ctx:?}"
        );
    }

    let group = match options.session_group.as_ref() {
        Some(g) => g,
        None => {
            log::debug!("Not tracking stale session because there's no session group.");
            return;
        }
    };

    // The caller *might* have a better guess than LIVE555_EXPIRATION_SEC via a SETUP response,
    // but it's also possible for note_stale_live555_data to be called prior to SETUP.
    let expires =
        tokio::time::Instant::now() + std::time::Duration::from_secs(LIVE555_EXPIRATION_SEC);
    let seqnum;
    {
        let mut lock = group.sessions.lock().unwrap();
        for s in &lock.sessions {
            if s.has_tcp {
                // This session plausibly explains the packet.
                // (We could go so far as to examine the data packet's SSRC to
                // see if it matches one associated with this session. But
                // retrying once per expiration is probably good enough.)
                log::debug!(
                    "Unexpected RTSP interleaved packet (live555 stale file \
                     descriptor bug) plausibly explained by known stale \
                     session {:?}/{}. Not adding a session entry.",
                    group.debug_id(),
                    s.seqnum,
                );
                return;
            }
        }
        seqnum = lock.next_seqnum;
        lock.next_seqnum += 1;
        log::debug!(
            "Unexpected RTSP interleaved packet, presumed due to live555 stale \
             file descriptor bug; adding stale session {:?}/{} that will \
             expire in {} seconds.",
            group.debug_id(),
            seqnum,
            LIVE555_EXPIRATION_SEC,
        );
        lock.sessions.push(StaleSession {
            seqnum,
            expires,
            teardown_rx: None,
            has_tcp: true,
            maybe_playing: true,
        });
    }

    // Spawn a task which removes the stale session at its expiration.
    // We could instead prune expired entries on stale_session() calls and
    // set a deadline within await_stale_sessions() calls, which might be
    // a bit more efficient. But this is simpler given that we already are
    // spawning tasks for stale sessions created from Session's Drop impl.
    let group = group.clone();
    tokio::spawn(async move {
        tokio::time::sleep_until(expires).await;
        if !group.try_remove_seqnum(seqnum) {
            log::warn!(
                "Unable to find stale live555 file descriptor session {:?}/{} at expiration",
                group.debug_id(),
                seqnum
            );
        } else {
            log::debug!(
                "Stale live555 file descriptor bug session {:?}/{} presumed expired.",
                group.debug_id(),
                seqnum
            );
        }
    });
}

#[rustfmt::skip]
const HOLE_PUNCH_RTP: [u8; 12] = [
    2 << 6,     // version=2 + p=0 + x=0 + cc=0
    0,          // m=0 + pt=0
    0, 0,       // sequence number=0
    0, 0, 0, 0, // timestamp=0 0,
    0, 0, 0, 0, // ssrc=0
];
#[rustfmt::skip]
const HOLE_PUNCH_RTCP: [u8; 8] = [
    2 << 6,     // version=2 + p=0 + rc=0
    201,        // pt=200 (reception report)
    0, 1,       // length=1 (in 4-byte words minus 1)
    0, 0, 0, 0, // ssrc=0 (bogus but we don't know the ssrc reliably yet)
];

/// Sends dummy RTP and RTCP packets to punch a hole in connection-tracking
/// firewalls.
///
/// This is useful when the client is on the protected side of the firewall and
/// the server isn't. The server should discard these dummy packets, but they
/// prompt the firewall to add the appropriate connection tracking state for
/// server->client packets to make it through.
///
/// Note this is insufficient for NAT traversal; the NAT firewall must be
/// RTSP-aware to rewrite the Transport header's client_ports.
async fn punch_firewall_hole(sockets: &UdpSockets) -> Result<(), std::io::Error> {
    sockets.rtp.send(&HOLE_PUNCH_RTP[..]).await?;
    sockets.rtcp.send(&HOLE_PUNCH_RTCP[..]).await?;
    Ok(())
}

/// An item yielded by [`Session<Playing>`]'s [`futures::stream::Stream`] impl.
#[derive(Debug)]
#[non_exhaustive]
pub enum PacketItem {
    Rtp(crate::rtp::ReceivedPacket),
    Rtcp(crate::rtcp::ReceivedCompoundPacket),
}

impl Session<Playing> {
    /// Returns a wrapper which demuxes/depacketizes into frames.
    ///
    /// Fails if a stream that has been setup can't be depacketized.
    pub fn demuxed(mut self) -> Result<Demuxed, Error> {
        let inner = self.0.as_mut().project();
        let conn = inner
            .conn
            .as_ref()
            .ok_or_else(|| wrap!(ErrorInt::FailedPrecondition("no connection".into())))?;
        for s in &mut *inner.presentation.streams {
            if matches!(s.state, StreamState::Playing { .. })
                && let Err(ref description) = s.depacketizer
            {
                bail!(ErrorInt::RtspResponseError {
                    conn_ctx: *conn.inner.ctx(),
                    msg_ctx: *inner.describe_ctx,
                    method: msg::Method::DESCRIBE,
                    cseq: *inner.describe_cseq,
                    status: *inner.describe_status,
                    description: description.clone(),
                });
            }
        }
        Ok(Demuxed {
            state: DemuxedState::Waiting,
            session: self,
        })
    }

    fn handle_keepalive_timer(
        mut self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> Result<(), Error> {
        let inner = self.0.as_mut().project();
        let session = inner
            .session
            .as_ref()
            .expect("keepalive_timer can't fire without a session");
        let keepalive_interval = keepalive_interval(session);
        let conn = inner
            .conn
            .as_mut()
            .ok_or_else(|| wrap!(ErrorInt::FailedPrecondition("no connection".into())))?;
        // Expect the previous keepalive request to have finished.
        match inner.keepalive_state {
            KeepaliveState::Flushing { cseq, .. } => bail!(ErrorInt::WriteError {
                conn_ctx: *conn.inner.ctx(),
                source: std::io::Error::new(
                    std::io::ErrorKind::TimedOut,
                    format!("Unable to write keepalive {cseq} within {keepalive_interval:?}",),
                ),
            }),
            KeepaliveState::Waiting { cseq, .. } => bail!(ErrorInt::RtspReadError {
                conn_ctx: *conn.inner.ctx(),
                msg_ctx: conn.inner.eof_ctx(),
                source: std::io::Error::new(
                    std::io::ErrorKind::TimedOut,
                    format!(
                        "Server failed to respond to keepalive {cseq} within {keepalive_interval:?}",
                    ),
                ),
            }),
            KeepaliveState::Idle => {}
        }

        // Currently the only outbound data should be keepalives, and the previous one
        // has already been flushed, so there's no reason the Sink shouldn't be ready.
        if matches!(conn.inner.poll_ready_unpin(cx), Poll::Pending) {
            bail!(ErrorInt::Internal(
                "Unexpectedly not ready to send keepalive".into()
            ));
        }

        // Send a new keepalive and reset the timer.
        //
        // RTSP/1.0 (the version Retina implements) doesn't describe how to send
        // a keepalive. The ONVIF Streaming Specification (in version 21.06 section
        // 5.2.2.2
        // <https://www.onvif.org/specs/stream/ONVIF-Streaming-Spec.pdf>) and
        // RTSP/2.0 recommend using `SET_PARAMETER`. However, this method is optional,
        // and some servers (e.g. rtsp-simple-server as of 2021-08-07) behave badly
        // on receiving unsupported methods. See discussion at
        // <https://github.com/aler9/rtsp-simple-server/issues/1066>. Initially
        // send `OPTIONS`, then follow recommendations to use (bodyless)
        // `SET_PARAMETER` or `GET_PARAMETER` if available.
        let method = if *inner.flags & (SessionFlag::SetParameterSupported as u8) != 0 {
            KeepaliveMethod::SetParameter
        } else if *inner.flags & (SessionFlag::GetParameterSupported as u8) != 0 {
            KeepaliveMethod::GetParameter
        } else {
            KeepaliveMethod::Options
        };
        let mut req = OwnedMessage::Request {
            head: msg::Request {
                method: method.into(),
                request_uri: Some(inner.presentation.base_url.clone()),
                headers: [(
                    msg::HeaderName::SESSION,
                    msg::HeaderValue::try_from(session.id.to_string()).unwrap(),
                )]
                .into(),
            },
            body: Bytes::new(),
        };
        let cseq = conn.fill_req(inner.options, inner.requested_auth, &mut req)?;
        trace!("sending {:?} keepalive", method);
        conn.inner
            .start_send_unpin(req)
            .expect("encoding is infallible");
        *inner.keepalive_state = match conn.inner.poll_flush_unpin(cx) {
            Poll::Ready(Ok(())) => KeepaliveState::Waiting { cseq, method },
            Poll::Ready(Err(e)) => bail!(e),
            Poll::Pending => KeepaliveState::Flushing { cseq, method },
        };

        inner
            .keepalive_timer
            .as_mut()
            .expect("keepalive timer set in state Playing")
            .as_mut()
            .reset(tokio::time::Instant::now() + keepalive_interval);
        Ok(())
    }

    fn handle_response(
        mut self: Pin<&mut Self>,
        msg_ctx: &crate::RtspMessageContext,
        response: msg::Response,
    ) -> Result<(), Error> {
        let inner = self.0.as_mut().project();
        match inner.keepalive_state {
            KeepaliveState::Waiting { cseq, method }
                if parse::get_cseq(&response) == Some(*cseq) =>
            {
                // We don't care if the keepalive response succeeds or fails, but we should
                // log it, to help debugging if on failure the server doesn't extend the
                // timeout or gets angry and closes the connection. (rtsp-simple-server
                // does the latter as of 2022-08-07, though I'm told this will be fixed.)
                if !response.status_code.is_success() {
                    warn!("keepalive failed with {:?}", response.status_code);
                } else {
                    trace!("keepalive succeeded with {:?}", response.status_code);
                    if matches!(method, KeepaliveMethod::Options) {
                        match parse::parse_options(&response) {
                            Ok(r) => {
                                if r.set_parameter_supported {
                                    *inner.flags |= SessionFlag::SetParameterSupported as u8;
                                }
                                if r.get_parameter_supported {
                                    *inner.flags |= SessionFlag::GetParameterSupported as u8;
                                }
                            }
                            Err(e) => {
                                warn!("Unable to parse OPTIONS response: {}", e);
                            }
                        }
                    }
                }
                *inner.keepalive_state = KeepaliveState::Idle;
                return Ok(());
            }
            _ => {}
        }

        // The only response we expect in this state is to our keepalive request.
        bail!(ErrorInt::RtspFramingError {
            conn_ctx: *inner
                .conn
                .as_ref()
                .expect("have conn when handling response")
                .inner
                .ctx(),
            msg_ctx: *msg_ctx,
            description: format!("Unexpected RTSP response {response:#?}"),
        })
    }

    fn handle_data(
        mut self: Pin<&mut Self>,
        msg_ctx: &RtspMessageContext,
        channel_id: u8,
        body: Bytes,
    ) -> Result<Option<PacketItem>, Error> {
        let inner = self.0.as_mut().project();
        let conn = inner
            .conn
            .as_mut()
            .ok_or_else(|| wrap!(ErrorInt::FailedPrecondition("no connection".into())))?;
        let pkt_ctx = crate::PacketContext(crate::PacketContextInner::Tcp { msg_ctx: *msg_ctx });
        let m = match conn.channels.lookup(channel_id) {
            Some(m) => m,
            None => {
                conn.handle_unassigned_data(
                    *msg_ctx,
                    inner.options,
                    inner.presentation.tool.as_ref(),
                    channel_id,
                    body,
                )?;
                return Ok(None);
            }
        };
        let stream = &mut inner.presentation.streams[m.stream_i];
        let (timeline, rtp_handler, stream_ctx) = match &mut stream.state {
            StreamState::Playing {
                timeline,
                rtp_handler,
                ctx,
                ..
            } => (timeline, rtp_handler, ctx),
            _ => unreachable!(
                "Session<Playing>'s {}->{:?} not in Playing state",
                channel_id, m
            ),
        };
        match m.channel_type {
            ChannelType::Rtp => Ok(rtp_handler.rtp(
                inner.options,
                stream_ctx,
                inner.presentation.tool.as_ref(),
                conn.inner.ctx(),
                &pkt_ctx,
                timeline,
                m.stream_i,
                body,
            )?),
            ChannelType::Rtcp => {
                match rtp_handler.rtcp(
                    inner.options,
                    stream_ctx,
                    inner.presentation.tool.as_ref(),
                    conn.inner.ctx(),
                    &pkt_ctx,
                    timeline,
                    m.stream_i,
                    body,
                ) {
                    Ok(p) => Ok(p),
                    Err(description) => Err(wrap!(ErrorInt::PacketError {
                        conn_ctx: *conn.inner.ctx(),
                        stream_ctx: stream.ctx().unwrap().to_owned(),
                        pkt_ctx,
                        stream_id: m.stream_i,
                        description,
                    })),
                }
            }
        }
    }

    /// Polls a single UDP stream, `inner.presentation.streams[i]`.
    ///
    /// Assumes `buf` is cleared and large enough for any UDP packet.
    /// Only returns `Poll::Pending` after both RTCP and RTP sockets have
    /// returned `Poll::Pending`.
    fn poll_udp_stream(
        &mut self,
        cx: &mut std::task::Context,
        buf: &mut tokio::io::ReadBuf,
        i: usize,
    ) -> Poll<Option<Result<PacketItem, Error>>> {
        debug_assert!(buf.filled().is_empty());
        let inner = self.0.as_mut().project();
        let s = &mut inner.presentation.streams[i];
        let (timeline, rtp_handler, stream_ctx, udp_sockets) = match &mut s.state {
            StreamState::Playing {
                timeline,
                rtp_handler,
                ctx,
                udp_sockets: Some(udp_sockets),
                ..
            } => (timeline, rtp_handler, ctx, udp_sockets),
            _ => return Poll::Pending,
        };
        let conn_ctx = inner
            .conn
            .as_ref()
            .ok_or_else(|| wrap!(ErrorInt::FailedPrecondition("no connection".into())))?
            .inner
            .ctx();

        // Prioritize RTCP over RTP within a stream.
        while let Poll::Ready(r) = udp_sockets.rtcp.poll_recv(cx, buf) {
            let when = Instant::now();
            let when_wall = crate::WallTime::now();
            match r {
                Ok(()) => {
                    let pkt_ctx = crate::PacketContext(crate::PacketContextInner::Udp {
                        received: when,
                        received_wall: when_wall,
                    });
                    let msg = Bytes::copy_from_slice(buf.filled());
                    match rtp_handler.rtcp(
                        inner.options,
                        stream_ctx,
                        inner.presentation.tool.as_ref(),
                        conn_ctx,
                        &pkt_ctx,
                        timeline,
                        i,
                        msg,
                    ) {
                        Ok(Some(p)) => return Poll::Ready(Some(Ok(p))),
                        Ok(None) => buf.clear(),
                        Err(description) => {
                            return Poll::Ready(Some(Err(wrap!(ErrorInt::PacketError {
                                conn_ctx: *conn_ctx,
                                stream_ctx: stream_ctx.to_owned(),
                                pkt_ctx,
                                stream_id: i,
                                description,
                            }))));
                        }
                    }
                }
                Err(source) if source.kind() == io::ErrorKind::ConnectionRefused => {
                    // The packets sent by `punch_firewall_hole` can elicit a
                    // 'destination unreachable' ICMP response from the server,
                    // which gets turned into a 'connection refused' error. This
                    // is not actually a problem so just ignore it.
                    debug!("Ignoring UDP connection refused error");
                }
                Err(source) => {
                    return Poll::Ready(Some(Err(wrap!(ErrorInt::UdpRecvError {
                        conn_ctx: *conn_ctx,
                        stream_ctx: stream_ctx.to_owned(),
                        when: when_wall,
                        source,
                    }))));
                }
            }
        }
        while let Poll::Ready(r) = udp_sockets.rtp.poll_recv(cx, buf) {
            let when = Instant::now();
            let when_wall = crate::WallTime::now();
            match r {
                Ok(()) => {
                    let msg = Bytes::copy_from_slice(buf.filled());
                    let pkt_ctx = crate::PacketContext(crate::PacketContextInner::Udp {
                        received: when,
                        received_wall: when_wall,
                    });
                    match rtp_handler.rtp(
                        inner.options,
                        stream_ctx,
                        inner.presentation.tool.as_ref(),
                        conn_ctx,
                        &pkt_ctx,
                        timeline,
                        i,
                        msg,
                    ) {
                        Ok(Some(p)) => return Poll::Ready(Some(Ok(p))),
                        Ok(None) => buf.clear(),
                        Err(e) => return Poll::Ready(Some(Err(e))),
                    }
                }
                Err(source) if source.kind() == io::ErrorKind::ConnectionRefused => {
                    // See comment above
                    debug!("Ignoring UDP connection refused error");
                }
                Err(source) => {
                    return Poll::Ready(Some(Err(wrap!(ErrorInt::UdpRecvError {
                        conn_ctx: *conn_ctx,
                        stream_ctx: stream_ctx.to_owned(),
                        when: when_wall,
                        source,
                    }))));
                }
            }
        }
        Poll::Pending
    }

    /// Polls all UDP streams, round-robining between them to avoid starvation.
    fn poll_udp(&mut self, cx: &mut std::task::Context) -> Poll<Option<Result<PacketItem, Error>>> {
        // For now, create a buffer on the stack large enough for any UDP packet, then
        // copy into a fresh allocation if it's actually used.
        // TODO: a ring buffer would be better: see
        // <https://github.com/scottlamb/retina/issues/6>.

        // SAFETY: this exactly matches an example in the documentation:
        // <https://doc.rust-lang.org/nightly/core/mem/union.MaybeUninit.html#initializing-an-array-element-by-element>.
        let mut buf: [MaybeUninit<u8>; 65_536] = unsafe { MaybeUninit::uninit().assume_init() };
        let mut buf = tokio::io::ReadBuf::uninit(&mut buf);

        // Assume 0 <= inner.udp_next_poll_i < inner.presentation.streams.len().
        // play() would have failed if there were no (setup) streams.
        let starting_i = *self.0.as_mut().project().udp_next_poll_i;
        loop {
            let inner = self.0.as_mut().project();
            let i = *inner.udp_next_poll_i;
            *inner.udp_next_poll_i += 1;
            if *inner.udp_next_poll_i == inner.presentation.streams.len() {
                *inner.udp_next_poll_i = 0;
            }

            if let Poll::Ready(r) = self.poll_udp_stream(cx, &mut buf, i) {
                return Poll::Ready(r);
            }

            if *self.0.as_mut().project().udp_next_poll_i == starting_i {
                break;
            }
        }
        Poll::Pending
    }
}

#[pin_project::pinned_drop]
impl PinnedDrop for SessionInner {
    fn drop(self: Pin<&mut Self>) {
        let this = self.project();

        let has_tcp = (*this.flags & (SessionFlag::TcpStreams as u8)) != 0;
        let just_try_once = match this.options.teardown {
            TeardownPolicy::Auto if has_tcp => {
                // If the server is known to have the live555 bug, try really hard to send a
                // TEARDOWN before considering the session cleaned up. Otherwise, try once on
                // the existing connection, primarily in case the server has
                // this bug but doesn't advertise a buggy version.
                !this
                    .presentation
                    .tool
                    .as_ref()
                    .map(Tool::has_live555_tcp_bug)
                    .unwrap_or(false)
            }
            TeardownPolicy::Auto | TeardownPolicy::Always => false,
            TeardownPolicy::Never => return,
        };

        let session = match this.session.take() {
            Some(s) => s,
            None => return,
        };

        // For now, assume the whole timeout is left.
        let expires = tokio::time::Instant::now()
            + std::time::Duration::from_secs(session.timeout_sec.into());

        // Track the session, if there is a group.
        let (teardown_tx, teardown_rx) = tokio::sync::watch::channel(None);
        let seqnum = if let Some(session_group) = this.options.session_group.as_ref() {
            let mut lock = session_group.sessions.lock().unwrap();
            let seqnum = lock.next_seqnum;
            lock.next_seqnum += 1;
            lock.sessions.push(StaleSession {
                seqnum,
                expires,
                teardown_rx: Some(teardown_rx),
                has_tcp,
                maybe_playing: *this.flags & (SessionFlag::MaybePlaying as u8) != 0,
            });
            log::debug!(
                "{:?}/{} tracking TEARDOWN of session id {}",
                session_group.debug_id(),
                seqnum,
                &session.id
            );
            Some(seqnum)
        } else {
            None
        };

        tokio::spawn(teardown::background_teardown(
            seqnum,
            this.presentation.base_url.clone(),
            this.presentation.tool.take(),
            session.id,
            just_try_once,
            std::mem::take(this.options),
            this.requested_auth.take(),
            this.conn.take(),
            teardown_tx,
            expires,
        ));
    }
}

impl futures::Stream for Session<Playing> {
    type Item = Result<PacketItem, Error>;

    fn poll_next(
        mut self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Option<Self::Item>> {
        loop {
            // First try receiving data on the RTSP connection. Let this starve
            // sending keepalives; if we can't keep up, the server should
            // probably drop us.
            match Pin::new(&mut self.0.conn.as_mut().unwrap().inner).poll_next(cx) {
                Poll::Ready(Some(Ok(msg))) => match msg.msg {
                    msg::Message::Data(d) => {
                        match self.as_mut().handle_data(&msg.ctx, d.channel_id, msg.body) {
                            Err(e) => return Poll::Ready(Some(Err(e))),
                            Ok(Some(pkt)) => return Poll::Ready(Some(Ok(pkt))),
                            Ok(None) => continue,
                        };
                    }
                    msg::Message::Response(response) => {
                        if let Err(e) = self.as_mut().handle_response(&msg.ctx, response) {
                            return Poll::Ready(Some(Err(e)));
                        }
                        continue;
                    }
                    msg::Message::Request(request) => {
                        warn!(
                            "Received RTSP request in Playing state. Responding unimplemented.\n{:#?}",
                            request
                        );
                    }
                },
                Poll::Ready(Some(Err(e))) => {
                    debug!("RTSP connection error: {:?}", e);
                    return Poll::Ready(Some(Err(e)));
                }
                Poll::Ready(None) => {
                    debug!("Server closed RTSP connection");
                    return Poll::Ready(None);
                }
                std::task::Poll::Pending => {}
            }

            // Next try receiving data on the UDP sockets, if any.
            if self.0.flags & (SessionFlag::UdpStreams as u8) != 0
                && let Poll::Ready(result) = self.as_mut().poll_udp(cx)
            {
                return Poll::Ready(result);
            }

            // Then check if it's time for a new keepalive.
            // Note: in production keepalive_timer is always Some. Tests may disable it.
            if let Some(t) = self.0.keepalive_timer.as_mut()
                && matches!(t.as_mut().poll(cx), Poll::Ready(()))
            {
                self.as_mut().handle_keepalive_timer(cx)?;
            }

            // Then finish flushing the current keepalive if necessary.
            if let KeepaliveState::Flushing { cseq, method } = self.0.keepalive_state {
                match self.0.conn.as_mut().unwrap().inner.poll_flush_unpin(cx) {
                    Poll::Ready(Ok(())) => {
                        self.0.keepalive_state = KeepaliveState::Waiting { cseq, method }
                    }
                    Poll::Ready(Err(e)) => return Poll::Ready(Some(Err(Error(Arc::new(e))))),
                    Poll::Pending => {}
                }
            }

            // Nothing to do. The poll calls above have already registered cx as necessary.
            return Poll::Pending;
        }
    }
}

enum DemuxedState {
    Waiting,
    Pulling(usize),
    Fused,
}

/// Wrapper returned by [`Session<Playing>::demuxed`] which demuxes/depacketizes into frames.
pub struct Demuxed {
    state: DemuxedState,
    session: Session<Playing>,
}

impl Demuxed {
    /// Returns the server's version as declared in the `DESCRIBE` response's `a:tool` SDP
    /// attribute.
    pub fn tool(&self) -> Option<&Tool> {
        self.session.tool()
    }

    /// Returns the available streams as described by the server.
    pub fn streams(&self) -> &[Stream] {
        self.session.streams()
    }
}

impl futures::Stream for Demuxed {
    type Item = Result<CodecItem, Error>;

    fn poll_next(
        mut self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> Poll<Option<Self::Item>> {
        loop {
            let (stream_id, pkt) = match self.state {
                DemuxedState::Waiting => match ready!(Pin::new(&mut self.session).poll_next(cx)) {
                    Some(Ok(PacketItem::Rtp(p))) => (p.stream_id(), Some(p)),
                    Some(Ok(PacketItem::Rtcp(p))) => {
                        return Poll::Ready(Some(Ok(CodecItem::Rtcp(p))));
                    }
                    Some(Err(e)) => return Poll::Ready(Some(Err(e))),
                    None => return Poll::Ready(None),
                },
                DemuxedState::Pulling(stream_id) => (stream_id, None),
                DemuxedState::Fused => return Poll::Ready(None),
            };
            let inner = self.session.0.as_mut().project();
            let stream = &mut inner.presentation.streams[stream_id];
            let stream_ctx = match stream.state {
                StreamState::Playing { ref ctx, .. } => ctx,
                _ => unreachable!(),
            };
            let depacketizer = match &mut stream.depacketizer {
                Ok(d) => d,
                Err(_) => unreachable!("depacketizer was Ok"),
            };
            let conn_ctx = inner
                .conn
                .as_ref()
                .ok_or_else(|| wrap!(ErrorInt::FailedPrecondition("no connection".into())))?
                .inner
                .ctx();
            if let Some(p) = pkt {
                let pkt_ctx = *p.ctx();
                let stream_id = p.stream_id();
                let ssrc = p.ssrc();
                let sequence_number = p.sequence_number();
                depacketizer.push(p).map_err(|description| {
                    wrap!(ErrorInt::RtpPacketError {
                        conn_ctx: *conn_ctx,
                        stream_ctx: stream_ctx.to_owned(),
                        pkt_ctx,
                        stream_id,
                        ssrc,
                        sequence_number,
                        description,
                    })
                })?;

                // Note we're committed now to calling `pull` and returning
                // `Ready` if it has a frame. This is because the
                // `Stream::parameters` contract guarantees that changes in
                // upcoming frames are *not* reflected. It's implemented by a
                // call to `Depacketizer::pull`, which doesn't make a like
                // guarantee about the state between `push` and `pull`. So we
                // can't let our callers observe that state.
            }

            match depacketizer.pull() {
                Some(Ok(item)) => {
                    self.state = DemuxedState::Pulling(stream_id);
                    return Poll::Ready(Some(Ok(item)));
                }
                None => {
                    self.state = DemuxedState::Waiting;
                    continue;
                }
                Some(Err(e)) => {
                    let conn_ctx = *conn_ctx;
                    let stream_ctx = *stream_ctx;
                    self.state = DemuxedState::Fused;
                    return Poll::Ready(Some(Err(Error(Arc::new(ErrorInt::RtpPacketError {
                        conn_ctx,
                        stream_ctx,
                        pkt_ctx: e.pkt_ctx,
                        stream_id,
                        ssrc: e.ssrc,
                        sequence_number: e.sequence_number,
                        description: e.description,
                    })))));
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::testutil::{init_logging, response};

    /// Cross-platform, tokio equivalent of `socketpair(2)`.
    async fn socketpair() -> (tokio::net::TcpStream, tokio::net::TcpStream) {
        // Another process on the machine could connect to the server and mess
        // this up, but that's unlikely enough to ignore in test code.
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        let client = tokio::net::TcpStream::connect(addr);
        let server = listener.accept();
        (client.await.unwrap(), server.await.unwrap().0)
    }

    async fn connect_to_mock() -> (RtspConnection, crate::tokio::Connection) {
        let (client, server) = socketpair().await;
        let client = crate::tokio::Connection::from_stream(client).unwrap();
        let server = crate::tokio::Connection::from_stream(server).unwrap();
        let client = RtspConnection {
            inner: client,
            channels: ChannelMappings::default(),
            next_cseq: 1,
            seen_unassigned: false,
        };
        (client, server)
    }

    /// Receives a request and sends a response, filling in the matching `CSeq`.
    async fn req_response(
        server: &mut crate::tokio::Connection,
        expected_method: msg::Method,
        (mut response, resp_body): (msg::Response, Bytes),
    ) {
        let msg = server.next().await.unwrap().unwrap();
        let cseq = match msg.msg {
            msg::Message::Request(ref r) => {
                assert_eq!(r.method, expected_method);
                r.headers.get("CSeq").unwrap().to_string()
            }
            _ => panic!(),
        };
        response.headers.insert(
            msg::HeaderName::CSEQ,
            msg::HeaderValue::try_from(cseq).unwrap(),
        );
        server
            .send(OwnedMessage::Response {
                head: response,
                body: resp_body,
            })
            .await
            .unwrap();
    }

    /// Tests the happy path from initialization to teardown (first attempt succeeds).
    #[tokio::test(start_paused = true)]
    async fn simple() {
        init_logging();
        let (conn, mut server) = connect_to_mock().await;
        let url = Url::parse("rtsp://192.168.5.206:554/h264Preview_01_main").unwrap();
        let group = Arc::new(SessionGroup::default());

        // DESCRIBE.
        let (session, _) = tokio::join!(
            Session::describe_with_conn(
                conn,
                SessionOptions::default()
                    .session_group(group.clone())
                    .unassigned_channel_data(UnassignedChannelDataPolicy::Ignore),
                url
            ),
            req_response(
                &mut server,
                msg::Method::DESCRIBE,
                response(include_bytes!("testdata/reolink_describe.txt"))
            ),
        );
        let mut session = session.unwrap();
        assert_eq!(session.streams().len(), 2);

        // SETUP.
        tokio::join!(
            async {
                session.setup(0, SetupOptions::default()).await.unwrap();
            },
            req_response(
                &mut server,
                msg::Method::SETUP,
                response(include_bytes!("testdata/reolink_setup.txt"))
            ),
        );

        // PLAY.
        let (session, _) = tokio::join!(
            session.play(PlayOptions::default()),
            req_response(
                &mut server,
                msg::Method::PLAY,
                response(include_bytes!("testdata/reolink_play.txt"))
            ),
        );
        {
            let session = session.unwrap();
            tokio::pin!(session);

            // XXX: tokio will "auto-advance" paused time when timers are polled.
            // This is not great for this test. Disable keepalives to prevent it.
            // <https://github.com/tokio-rs/tokio/issues/4522>
            session.0.keepalive_timer = None;

            // Packets: first ignored one (unassigned channel), then one passed through.
            tokio::join!(
                async {
                    match session.next().await {
                        Some(Ok(PacketItem::Rtp(p))) => {
                            assert_eq!(p.ssrc(), 0xdcc4a0d8);
                            assert_eq!(p.sequence_number(), 0x41d4);
                            assert_eq!(p.payload(), b"hello world");
                        }
                        o => panic!("unexpected item: {o:#?}"),
                    }
                },
                async {
                    let bad_pkt = b"data on unassigned channel";
                    server
                        .send(OwnedMessage::Data {
                            channel_id: 2,
                            body: Bytes::from_static(bad_pkt),
                        })
                        .await
                        .unwrap();
                    let good_pkt = b"\x80\x60\x41\xd4\x00\x00\x00\x00\xdc\xc4\xa0\xd8hello world";
                    server
                        .send(OwnedMessage::Data {
                            channel_id: 0,
                            body: Bytes::from_static(good_pkt),
                        })
                        .await
                        .unwrap();
                },
            );
        };

        // Drop (initiated by exiting the scope above).
        // This server advertises an ancient version of live555, so Retina
        // sends a TEARDOWN even with TCP.

        let stale_sessions = group.stale_sessions();
        assert_eq!(stale_sessions.num_sessions, 1);

        // Resume real time for the TEARDOWN exchange. With paused time,
        // the runtime may auto-advance through the teardown's I/O timeout
        // before the I/O exchange with the mock server completes, because
        // the auto-advance's non-blocking I/O poll can race with kernel
        // TCP event delivery on localhost.
        tokio::time::resume();
        tokio::join!(
            group.await_stale_sessions(&stale_sessions),
            req_response(
                &mut server,
                msg::Method::TEARDOWN,
                response(include_bytes!("testdata/reolink_teardown.txt"))
            ),
        );
        tokio::time::pause();
        assert_eq!(group.stale_sessions().num_sessions, 0);
    }

    /// As above, but TEARDOWN fails until session expiration.
    #[tokio::test(start_paused = true)]
    async fn session_expiration() {
        init_logging();
        let (conn, mut server) = connect_to_mock().await;
        let url = Url::parse("rtsp://192.168.5.206:554/h264Preview_01_main").unwrap();
        let group = Arc::new(SessionGroup::default());

        // DESCRIBE.
        let (session, _) = tokio::join!(
            Session::describe_with_conn(
                conn,
                SessionOptions::default().session_group(group.clone()),
                url
            ),
            req_response(
                &mut server,
                msg::Method::DESCRIBE,
                response(include_bytes!("testdata/reolink_describe.txt"))
            ),
        );
        let mut session = session.unwrap();
        assert_eq!(session.streams().len(), 2);

        // SETUP.
        tokio::join!(
            async {
                session.setup(0, SetupOptions::default()).await.unwrap();
            },
            req_response(
                &mut server,
                msg::Method::SETUP,
                response(include_bytes!("testdata/reolink_setup.txt"))
            ),
        );

        // PLAY.
        let (session, _) = tokio::join!(
            session.play(PlayOptions::default()),
            req_response(
                &mut server,
                msg::Method::PLAY,
                response(include_bytes!("testdata/reolink_play.txt"))
            ),
        );
        let drop_time;
        {
            let session = session.unwrap();
            tokio::pin!(session);

            // Packet.
            tokio::join!(
                async {
                    match session.next().await {
                        Some(Ok(PacketItem::Rtp(p))) => {
                            assert_eq!(p.ssrc(), 0xdcc4a0d8);
                            assert_eq!(p.sequence_number(), 0x41d4);
                            assert_eq!(p.payload(), b"hello world");
                        }
                        o => panic!("unexpected item: {o:#?}"),
                    }
                },
                async {
                    let pkt = b"\x80\x60\x41\xd4\x00\x00\x00\x00\xdc\xc4\xa0\xd8hello world";
                    server
                        .send(OwnedMessage::Data {
                            channel_id: 0,
                            body: Bytes::from_static(pkt),
                        })
                        .await
                        .unwrap();
                },
            );
            drop_time = tokio::time::Instant::now();
        }

        // Drop (initiated by exiting the scope above).
        // This server advertises an ancient version of live555, so Retina
        // sends a TEARDOWN even with TCP.
        server.close().await.unwrap();
        let stale_sessions = group.stale_sessions();
        assert_eq!(stale_sessions.num_sessions, 1);

        // Even if repeated attempts fail, the stale session will go away on timeout.
        // The "60" in the assert below is the RFC-defined default timeout when
        // none is specified in the SETUP response.
        group.await_stale_sessions(&stale_sessions).await;
        assert_eq!(group.stale_sessions().num_sessions, 0);

        // elapsed is not zero because tokio advances the time unnecessarily, grr.
        // https://github.com/tokio-rs/tokio/issues/3108
        let elapsed = tokio::time::Instant::now() - drop_time;
        assert!(
            elapsed >= std::time::Duration::from_secs(60),
            "elapsed={elapsed:?}"
        );
    }

    /// Stale sessions detected via unexpected RTSP interleaved packets should be tracked
    /// until expiration.
    #[tokio::test(start_paused = true)]
    async fn stale_file_descriptor_session() {
        init_logging();
        let (conn, mut server) = connect_to_mock().await;
        let url = Url::parse("rtsp://192.168.5.206:554/h264Preview_01_main").unwrap();
        let group = Arc::new(SessionGroup::default());
        let bogus_rtp = OwnedMessage::Data {
            channel_id: 0,
            body: Bytes::from_static(b"bogus pkt"),
        };

        let start = tokio::time::Instant::now();

        // DESCRIBE.
        tokio::join!(
            async {
                let e = Session::describe_with_conn(
                    conn,
                    SessionOptions::default()
                        .session_group(group.clone())
                        .unassigned_channel_data(UnassignedChannelDataPolicy::AssumeStaleSession),
                    url,
                )
                .await
                .map(|_s| ())
                .unwrap_err();
                assert!(matches!(*e.0, ErrorInt::RtspUnassignedChannelError { .. }));
            },
            async { server.send(bogus_rtp).await.unwrap() },
        );

        let stale_sessions = group.stale_sessions();
        assert_eq!(stale_sessions.num_sessions, 1);

        group.await_stale_sessions(&stale_sessions).await;
        let elapsed = tokio::time::Instant::now() - start;
        assert_eq!(group.stale_sessions().num_sessions, 0);

        assert!(
            elapsed >= std::time::Duration::from_secs(LIVE555_EXPIRATION_SEC),
            "elapsed={elapsed:?}"
        );
    }

    /// Tests ignoring bogus RTP and RTCP messages while waiting for PLAY response.
    #[tokio::test]
    async fn ignore_early_rtp_rtcp() {
        init_logging();
        let (conn, mut server) = connect_to_mock().await;
        let url = Url::parse("rtsp://192.168.5.206:554/h264Preview_01_main").unwrap();
        let bogus_rtp = OwnedMessage::Data {
            channel_id: 0,
            body: Bytes::from_static(b"bogus pkt"),
        };
        let bogus_rtcp = OwnedMessage::Data {
            channel_id: 1,
            body: Bytes::from_static(b"bogus pkt"),
        };

        // DESCRIBE.
        let (session, _) = tokio::join!(
            Session::describe_with_conn(conn, SessionOptions::default(), url),
            async {
                req_response(
                    &mut server,
                    msg::Method::DESCRIBE,
                    response(include_bytes!("testdata/reolink_describe.txt")),
                )
                .await;
            },
        );
        let mut session = session.unwrap();
        assert_eq!(session.streams().len(), 2);

        // SETUP.
        tokio::join!(
            async {
                session.setup(0, SetupOptions::default()).await.unwrap();
            },
            req_response(
                &mut server,
                msg::Method::SETUP,
                response(include_bytes!("testdata/reolink_setup.txt"))
            ),
        );

        // PLAY.
        let (session, _) = tokio::join!(session.play(PlayOptions::default()), async move {
            server.send(bogus_rtp).await.unwrap();
            server.send(bogus_rtcp).await.unwrap();
            req_response(
                &mut server,
                msg::Method::PLAY,
                response(include_bytes!("testdata/reolink_play.txt")),
            )
            .await
        },);
        let _session = session.unwrap();
    }

    #[tokio::test]
    async fn reject_session_id_change() {
        session_id_change(SessionIdPolicy::RequireMatch, true).await
    }

    #[tokio::test]
    async fn ignore_session_id_change() {
        session_id_change(SessionIdPolicy::UseFirst, false).await
    }

    async fn session_id_change(policy: SessionIdPolicy, expect_error: bool) {
        init_logging();
        let (conn, mut server) = connect_to_mock().await;
        let url = Url::parse("rtsp://127.0.0.1:554/camera").unwrap();
        let group = Arc::new(SessionGroup::default());

        // DESCRIBE.
        let (session, _) = tokio::join!(
            Session::describe_with_conn(
                conn,
                SessionOptions::default()
                    .session_group(group.clone())
                    .session_id(policy),
                url
            ),
            req_response(
                &mut server,
                msg::Method::DESCRIBE,
                response(include_bytes!("testdata/h264dvr_describe.txt"))
            ),
        );
        let mut session = session.unwrap();
        assert_eq!(session.streams().len(), 2);

        // SETUP.
        tokio::join!(
            async {
                session
                    .setup(
                        0,
                        SetupOptions::default()
                            .transport(Transport::Udp(UdpTransportOptions::default())),
                    )
                    .await
                    .unwrap();
            },
            req_response(
                &mut server,
                msg::Method::SETUP,
                response(include_bytes!("testdata/h264dvr_setup_video.txt"))
            ),
        );

        tokio::join!(
            async {
                let r = session
                    .setup(
                        1,
                        SetupOptions::default()
                            .transport(Transport::Udp(UdpTransportOptions::default())),
                    )
                    .await;
                if expect_error {
                    let e = r.unwrap_err();
                    assert!(matches!(*e.0, ErrorInt::RtspResponseError { .. }));
                } else {
                    r.unwrap();
                }
            },
            req_response(
                &mut server,
                msg::Method::SETUP,
                response(include_bytes!("testdata/h264dvr_setup_audio.txt"))
            ),
        );
    }

    // See with: cargo test -- --nocapture client::tests::print_sizes
    #[test]
    fn print_sizes() {
        init_logging();
        for (name, size) in &[
            ("PacketItem", std::mem::size_of::<PacketItem>()),
            ("Presentation", std::mem::size_of::<Presentation>()),
            ("RtspConnection", std::mem::size_of::<RtspConnection>()),
            (
                "Session",
                std::mem::size_of::<Session<Described>>(), // <Playing> is the same size.
            ),
            ("SessionInner", std::mem::size_of::<SessionInner>()),
            ("SessionOptions", std::mem::size_of::<SessionOptions>()),
            ("Demuxed", std::mem::size_of::<Demuxed>()),
            ("Stream", std::mem::size_of::<Stream>()),
        ] {
            log::info!("{name:-40} {size:4}");
        }
    }

    #[test]
    fn check_live555_tcp_bug() {
        init_logging();
        assert!(!Tool::new("not live555").has_live555_tcp_bug());
        assert!(!Tool::new("LIVE555 Streaming Media v").has_live555_tcp_bug());
        assert!(Tool::new("LIVE555 Streaming Media v2013.04.08").has_live555_tcp_bug());
        assert!(!Tool::new("LIVE555 Streaming Media v2017.06.04").has_live555_tcp_bug());
        assert!(!Tool::new("LIVE555 Streaming Media v2020.01.01").has_live555_tcp_bug());
    }

    #[test]
    fn await_stale_sessions_is_send() {
        // There's probably a more elegant way to test this, but here goes.
        fn assert_send<T: Send>(_: T) {}
        let group = SessionGroup::default();
        let stale_sessions = group.stale_sessions();
        assert_send(group.await_stale_sessions(&stale_sessions));
    }

    #[test]
    fn validate_hole_punch_rtp() {
        let (pkt_ref, _) = crate::rtp::RawPacket::new(Bytes::from_static(&HOLE_PUNCH_RTP)).unwrap();
        assert_eq!(pkt_ref.payload_type(), 0);
    }

    #[test]
    fn validate_hole_punch_rtcp() {
        let (pkt_ref, _) = crate::rtcp::PacketRef::parse(&HOLE_PUNCH_RTCP).unwrap();
        assert!(matches!(
            pkt_ref.as_typed().unwrap(),
            Some(crate::rtcp::TypedPacketRef::ReceiverReport(_))
        ));
    }
}