moq-mux 0.10.13

Media muxers and demuxers for MoQ
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
use std::collections::BTreeSet;
use std::ops::{Deref, DerefMut};
use std::sync::{Arc, Mutex, MutexGuard};

use base64::Engine;

use super::hang::{Catalog, CatalogExt, Consumer, Extra};

/// Everything a catalog producer shares with its clones, under one lock.
///
/// One lock rather than one per concern. A name check only means anything if the catalog can't
/// change under it: split those and two reservations of the same name both pass, which is the state
/// that let one rendition's entry be written over and deleted by the other. Keeping the publish
/// gate here too means a [`Guard`] never has to take a second lock while holding this one.
struct State<E: CatalogExt> {
	catalog: Catalog<E>,

	/// The names live renditions hold, keyed by section (the [`RenditionConfig`](super::RenditionConfig)
	/// type) and name. The section is part of the key because each writes a disjoint map: `video["v"]`
	/// and `audio["v"]` are different renditions and neither says anything about the other.
	owned: BTreeSet<(std::any::TypeId, String)>,

	/// Gates the initial catalog publish until all reservations resolve (see
	/// [`reserve`](Producer::reserve)).
	reservations: Reservations,

	/// Why the catalog tracks are closed, so [`Producer::modify`] refuses further edits with the
	/// cause: [`Producer::finish`], or a drop-time publish that failed and aborted them.
	closed: Option<crate::Error>,
}

/// Take the shared state, ignoring a poisoned lock.
///
/// A panic under this lock comes from code we hand the catalog to: a [`RenditionConfig::insert`]
/// implementation, or serializing an extension during a publish. Neither leaves the state
/// inconsistent (at worst the catalog is missing an entry, while `owned` and `reservations` still
/// describe exactly what is live), so poisoning protects nothing here. It would only turn the next
/// [`Rendition`](super::Rendition) drop into a panic during unwinding, which aborts the process.
fn take<E: CatalogExt>(state: &Mutex<State<E>>) -> MutexGuard<'_, State<E>> {
	state.lock().unwrap_or_else(|poisoned| poisoned.into_inner())
}

/// The key a rendition name is held under: its section (the config type) plus the name.
fn owner_key<C: 'static>(name: &str) -> (std::any::TypeId, String) {
	(std::any::TypeId::of::<C>(), name.to_string())
}

/// The initial-publish gate, tracking the [`Reserved`](super::Reserved) handles still outstanding.
///
/// The initial catalog snapshot is withheld from the broadcast until `reservers == 0`. A live
/// `Reserved` counts as one reserver; a [`Rendition`](super::Rendition) holds its own `Reserved`
/// clone until it's fulfilled (or dropped), so an unresolved rendition keeps the gate shut too. See
/// [`Reserved`](super::Reserved).
#[derive(Default)]
struct Reservations {
	/// Live `Reserved` handles (including the one each unfulfilled `Rendition` holds).
	reservers: usize,
	/// A catalog change was made while buffering and is awaiting the initial publish. Without this
	/// we'd emit an empty snapshot when the gate opens on a catalog nobody ever touched.
	pending: bool,
	/// Whether the initial snapshot has been published (buffering is over).
	published: bool,
}

/// The built-in catalog's enrollment in the broadcast timeline.
struct CatalogTimeline {
	recorder: crate::timeline::Recorder,
	last_sequence: Option<u64>,
}

impl CatalogTimeline {
	/// Report a newly published plaintext catalog group once.
	fn record(&mut self, track: &moq_net::track::Producer) {
		let Some(sequence) = track.latest() else {
			return;
		};
		if self.last_sequence == Some(sequence) {
			return;
		}

		self.last_sequence = Some(sequence);
		self.recorder.record(sequence, moq_net::Timestamp::now(), true);
	}
}

/// The catalog tracks and the timeline enrollment updated with them.
struct Outputs<E: CatalogExt> {
	hang: moq_json::snapshot::Producer<Catalog<E>>,
	hang_track: moq_net::track::Producer,
	hangz: moq_json::snapshot::Producer<Catalog<E>>,
	msf_track: moq_net::track::Producer,
	catalog_timeline: Arc<Mutex<CatalogTimeline>>,
}

impl<E: CatalogExt> Clone for Outputs<E> {
	fn clone(&self) -> Self {
		Self {
			hang: self.hang.clone(),
			hang_track: self.hang_track.clone(),
			hangz: self.hangz.clone(),
			msf_track: self.msf_track.clone(),
			catalog_timeline: self.catalog_timeline.clone(),
		}
	}
}

impl<E: CatalogExt> Outputs<E> {
	/// Emit the catalog to hang, compressed hang, MSF, and the broadcast timeline.
	fn emit(&mut self, catalog: &Catalog<E>) -> crate::Result<()> {
		// One snapshot per group while deltas are disabled; the `.z` track carries the identical catalog.
		self.hang.update(catalog)?;
		self.catalog_timeline.lock().unwrap().record(&self.hang_track);
		self.hangz.update(catalog)?;

		// The MSF catalog is derived from our own types, so a serialize failure means an extension broke
		// the shape; report it like any other JSON failure rather than panicking. `abort` below is
		// what keeps the tracks consistent if this fails after the hang tracks were updated.
		let msf = to_msf(catalog).to_json().map_err(moq_json::Error::from)?;
		let mut group = self.msf_track.append_group()?;
		group.write_frame(moq_net::Timestamp::now(), msf)?;
		group.finish()?;

		Ok(())
	}

	/// Abort every catalog track after a publish nobody could return the error for.
	///
	/// The three tracks carry one catalog, so a failure part way through `emit` must not leave some
	/// of them serving a newer catalog than the others. Consumers see the error instead.
	fn abort(&mut self, err: &crate::Error) {
		// Only a transport error has a wire code; anything else (an extension that won't serialize)
		// reaches consumers as a generic failure while the cause stays on the producer.
		let reason = match err {
			crate::Error::Moq(err) => err.clone(),
			crate::Error::Json(moq_json::Error::Net(err)) => err.clone(),
			_ => moq_net::StreamError::Internal.into(),
		};
		// Idempotent: a track that is already closed keeps its first reason.
		let _ = self.hang.clone().abort(reason.clone());
		let _ = self.hangz.clone().abort(reason.clone());
		let _ = self.msf_track.clone().abort(reason);
	}
}

/// Produces both a hang and MSF catalog track for a broadcast.
///
/// Generic over the application extension `E` (defaulting to `()` for none). The catalog is a
/// [`Catalog<E>`](super::hang::Catalog): `video`/`audio` are direct fields (`catalog.video`) and the
/// extension is reachable through `catalog.ext` (for example, `catalog.ext.scte35`). Define an
/// extension with [`CatalogExt`](super::hang::CatalogExt). The MSF track carries the same catalog:
/// the media sections become MSF tracks and the extension's sections ride the MSF root.
///
/// The JSON catalog is updated when tracks are added/removed but is *not* automatically published.
/// You'll have to call [`modify`](Self::modify) to update and publish the catalog.
/// Both the hang (`catalog.json`) and MSF (`catalog`) tracks are published on drop of the guard.
///
/// The hang track is published through [`moq_json`], which currently emits one snapshot per
/// group (deltas disabled). This routes catalog publishing through the JSON merge-patch helper
/// so deltas can be enabled later without changing the wire format used today.
pub struct Producer<E: CatalogExt = ()> {
	outputs: Outputs<E>,

	current: Arc<Mutex<State<E>>>,

	/// Shared wall clock for the broadcast's tracks. Every importer on this catalog
	/// gets a copy (the same epoch), so timestamps they synthesize when
	/// a caller has none land on one timeline and audio/video stay in sync.
	///
	/// It also owns the broadcast's wall mapping, published at the catalog root as
	/// `clock: { wall, timescale }` independently of any archive timeline.
	clock: crate::Clock,

	/// The broadcast's timeline: the shared boundary list every enrolled track's groups map
	/// onto, and the track those segment records are published on. See
	/// [`timeline`](Self::timeline).
	timeline: crate::timeline::Producer,
	/// Retention override for the media tracks minted under this catalog, or `None` to keep
	/// hang's default. Fixed at construction, so every clone and every
	/// [`Reserved`](super::Reserved) mints tracks under one policy. See
	/// [`Config::with_max_age`].
	max_age: Option<std::time::Duration>,
	/// Connection allocator passthrough tracks claim their peak-hold bitrate on.
	/// See [`Config::with_bandwidth`].
	bandwidth: moq_net::bandwidth::Allocator,
	/// The minimum flush lateness across this catalog's renditions, which each rendition's
	/// advertised `delay` is measured against.
	baseline: super::estimate::Baseline,
}

// Manual Clone so a producer is cheaply clonable regardless of whether `E` is.
impl<E: CatalogExt> Clone for Producer<E> {
	fn clone(&self) -> Self {
		Self {
			outputs: self.outputs.clone(),
			current: self.current.clone(),
			clock: self.clock,
			timeline: self.timeline.clone(),
			max_age: self.max_age,
			bandwidth: self.bandwidth.clone(),
			baseline: self.baseline.clone(),
		}
	}
}

/// Everything a catalog [`Producer`] is built from: the initial catalog and any overrides.
///
/// Start from [`default`](Default::default), which is the media-only catalog with hang's own
/// defaults, and chain the setters.
///
/// These are constructor arguments rather than setters on a live producer, so a clone or a
/// [`Reserved`](super::Reserved) taken at any point mints tracks under the same policy.
#[derive(Clone)]
pub struct Config<E: CatalogExt = ()> {
	catalog: Catalog<E>,
	max_age: Option<std::time::Duration>,
	bandwidth: moq_net::bandwidth::Allocator,
	clock: crate::Clock,
	timeline: crate::timeline::Config,
}

impl Default for Config<()> {
	fn default() -> Self {
		Self {
			catalog: Catalog::default(),
			max_age: None,
			bandwidth: moq_net::bandwidth::Allocator::unlimited(),
			clock: crate::Clock::new(),
			timeline: crate::timeline::Config::default(),
		}
	}
}

impl<E: CatalogExt> Config<E> {
	/// Start from an extended catalog rather than the media-only one, e.g. the untyped
	/// [`Extra`] for the by-name / FFI path. Set application sections through
	/// [`Producer::modify`](Producer::modify) afterwards.
	pub fn with_catalog<F: CatalogExt>(self, catalog: Catalog<F>) -> Config<F> {
		Config {
			catalog,
			max_age: self.max_age,
			bandwidth: self.bandwidth,
			clock: self.clock,
			timeline: self.timeline,
		}
	}

	/// Publish with this broadcast clock instead of a fresh one.
	///
	/// The clock's wall mapping is advertised at the catalog root, so pass a clock whose PTS
	/// zero names the content's real start when importing a recording; live publishers use the
	/// default. The mapping is fixed for the broadcast and never overwritten.
	pub fn with_clock(mut self, clock: crate::Clock) -> Self {
		self.clock = clock;
		self
	}

	/// Override how long the media tracks minted under this catalog keep a non-latest group
	/// fetchable, replacing hang's default. `None` keeps it.
	///
	/// That default suits a segmented egress (HLS/DASH), which may only advertise segments a
	/// FETCH can still reach. Lower it when nothing reads history and the memory matters;
	/// raise it for a deeper seek window. It is a RETENTION budget, not a delivery one, so it
	/// never makes a subscriber play further behind live -- it caps what one may ask to wait
	/// for, and a subscriber's own budget still defaults to zero.
	///
	/// Applies to the media tracks the catalog mints. The catalog and timeline tracks keep
	/// moq-net's default: both are read at the live edge, which is retained unconditionally.
	pub fn with_max_age(mut self, max_age: impl Into<Option<std::time::Duration>>) -> Self {
		self.max_age = max_age.into();
		self
	}

	/// Claim each media track's peak-hold catalog bitrate on `bandwidth`.
	///
	/// A passthrough import has no configured ceiling, so it reserves the
	/// measured maximum instead: taken on the first 1 s window, raised when a
	/// later window exceeds it, never walked back. A co-resident encoder then
	/// targets what is left of the uplink. `unlimited` (the default) claims
	/// nothing a sender can follow.
	pub fn with_bandwidth(mut self, bandwidth: moq_net::bandwidth::Allocator) -> Self {
		self.bandwidth = bandwidth;
		self
	}

	/// Pace the broadcast's timeline with `timeline`.
	pub fn with_timeline(mut self, timeline: crate::timeline::Config) -> Self {
		self.timeline = timeline;
		self
	}
}

impl<E: CatalogExt> Producer<E> {
	/// Create a new catalog producer from a full [`Config`].
	pub fn new(broadcast: &mut moq_net::broadcast::Producer, config: Config<E>) -> Result<Self, moq_net::Error> {
		let hang_track = broadcast.create_track(hang::Catalog::DEFAULT_NAME, hang::Catalog::default_track_info())?;
		let hangz_track =
			broadcast.create_track(hang::Catalog::COMPRESSED_NAME, hang::Catalog::default_track_info())?;
		// The MSF track is the same catalog in another encoding, so it takes the same
		// priority. Leaving it at the default would rank a subscriber reading MSF
		// below every media track on a relay's upstream leg.
		let msf_info = moq_net::track::Info::default().with_priority(hang::catalog::PRIORITY.catalog);
		let msf_track = broadcast.create_track(moq_msf::DEFAULT_NAME, msf_info)?;

		// Disable deltas for now to stay byte-compatible with consumers that only read snapshots.
		let mut json_config = moq_json::snapshot::Config::default();
		json_config.delta_ratio = 0;
		let hang = moq_json::snapshot::Producer::new(hang_track.clone(), json_config.clone());

		// The `.z` track carries the same catalog, DEFLATE-compressed. Deltas stay off for parity
		// with the plaintext track; only the per-group compression differs.
		json_config.compression = moq_json::Compression::Deflate;
		let hangz = moq_json::snapshot::Producer::new(hangz_track, json_config);

		let timeline = crate::timeline::Producer::new(broadcast, config.timeline);
		#[allow(clippy::arc_with_non_send_sync)]
		let catalog_timeline = Arc::new(Mutex::new(CatalogTimeline {
			recorder: timeline.track(hang::Catalog::DEFAULT_NAME),
			last_sequence: None,
		}));

		// The broadcast clock is advertised at the catalog root from the first snapshot,
		// independently of any archive timeline: a live-only publisher exposes its mapping
		// without creating a segment index.
		let mut catalog = config.catalog;
		catalog.clock = Some(config.clock.wall());

		// The contents are `Send + Sync` natively; on wasm moq-net's handles are
		// `Rc`-backed, so clippy sees a pointlessly atomic `Arc`. Keeping one type for
		// both targets is worth the unused atomics on the single-threaded one.
		#[allow(clippy::arc_with_non_send_sync)]
		Ok(Self {
			outputs: Outputs {
				hang,
				hang_track,
				hangz,
				msf_track,
				catalog_timeline,
			},
			current: Arc::new(Mutex::new(State {
				catalog,
				owned: BTreeSet::new(),
				reservations: Reservations::default(),
				closed: None,
			})),
			clock: config.clock,
			timeline,
			max_age: config.max_age,
			bandwidth: config.bandwidth,
			baseline: Default::default(),
		})
	}

	/// Track properties for a media track under this catalog: hang's media defaults, plus any
	/// [`Config::with_max_age`] override.
	///
	/// `priority` should come from [`PRIORITY`](hang::catalog::PRIORITY) for the kind of media
	/// the track carries. Chain [`with_timescale`](moq_net::track::Info::with_timescale) for a
	/// container that carries the source's own scale (CMAF and Matroska both do).
	pub fn track_info(&self, priority: u8) -> moq_net::track::Info {
		let info = hang::container::track_info(priority);
		match self.max_age {
			Some(max_age) => info.with_max_age(max_age),
			None => info,
		}
	}

	/// Resolve a timestamp, synthesizing one from the broadcast's shared
	/// [`Clock`](crate::Clock) when the caller has none.
	///
	/// Sharing the clock across the catalog's tracks keeps concurrently-produced
	/// audio and video on a single timeline.
	pub fn timestamp(&self, hint: Option<moq_net::Timestamp>) -> crate::Result<moq_net::Timestamp> {
		match hint {
			Some(pts) => Ok(pts),
			None => Ok(self.clock.now()),
		}
	}

	/// Edit the catalog in place and publish the result.
	///
	/// The closure receives the current catalog, composed from every owner so far. Edit it in place;
	/// on return the result is published, a no-op if the closure changed nothing. Independent owners
	/// can each edit only their own sections, so they compose instead of clobbering one another.
	///
	/// This is [`modify`](Self::modify) opened and committed for you; take the guard to hold the lock
	/// across several edits, or to reach a [`Guard`] method like
	/// [`set_section`](Guard::set_section).
	pub fn mutate(&mut self, f: impl FnOnce(&mut Catalog<E>)) -> crate::Result<()> {
		let mut guard = self.modify()?;
		f(&mut guard);
		guard.commit()
	}

	/// Get mutable access to the catalog, publishing it after any changes.
	///
	/// The publish happens when the returned [`Guard`] drops. Use [`mutate`](Self::mutate) for a
	/// single edit, which is this guard opened and committed for you. Fails once the catalog tracks
	/// are closed, the one publication failure that happens in normal operation, so nothing is left
	/// to check after the guard drops. Anything else that stops the drop from publishing (an extension
	/// that won't serialize, a catalog too large for a frame) aborts the catalog tracks with that
	/// error: consumers see it instead of a stale catalog, and the next `modify` returns it here.
	/// Call [`Guard::commit`] to get the error back immediately instead.
	///
	/// The in-memory catalog is already mutated, so a panic while the guard is held still
	/// publishes: skipping would leave `snapshot` ahead of the wire with no dirty flag to catch up.
	pub fn modify(&mut self) -> crate::Result<Guard<'_, E>> {
		let state = take(&self.current);
		if let Some(err) = &state.closed {
			return Err(err.clone());
		}

		Ok(Guard {
			state,
			outputs: &mut self.outputs,
			updated: false,
		})
	}

	/// Get a snapshot of the current catalog.
	pub fn snapshot(&self) -> Catalog<E> {
		take(&self.current).catalog.clone()
	}

	/// The broadcast's shared clock: the epoch importers stamp against and the wall mapping
	/// advertised at the catalog root.
	///
	/// Copies share the epoch, so handing them to concurrent producers keeps every track on
	/// one timeline. Translate a source with its own zero through
	/// [`Clock::source`](crate::Clock::source).
	pub fn clock(&self) -> crate::Clock {
		self.clock
	}

	/// Begin reserving the initial track set, returning a clonable [`Reserved`](super::Reserved).
	///
	/// Hand it (or clones) to importers, or call its role-specific track constructors. The catalog is
	/// withheld until every reservation is dropped and every created producer either publishes its
	/// config or drops. A one-shot muxer therefore publishes the complete track list in its first
	/// snapshot instead of a half-converged one. Producers created directly on this catalog publish
	/// incrementally.
	pub fn reserve(&self) -> super::Reserved<E> {
		super::Reserved::new(self.clone())
	}

	/// Take a rendition name without withholding the catalog's initial snapshot.
	///
	/// Use this for a long-lived incremental producer whose config may arrive
	/// later or never arrive. The returned handle owns the name immediately,
	/// publishes the config on [`set`](super::Rendition::set), and retires it on
	/// drop. Use [`reserve`](Self::reserve) when the initial catalog must wait for
	/// the complete track set instead.
	pub(super) fn rendition<C: super::RenditionConfig<E>>(
		&self,
		name: impl Into<String>,
	) -> crate::Result<super::Rendition<E, C>> {
		super::Rendition::live(self.clone(), name.into())
	}

	/// Publish a video track and own its catalog rendition.
	pub fn video<C: crate::container::Container>(
		&self,
		track: moq_net::track::Producer,
		container: C,
		config: impl Into<Option<hang::catalog::VideoConfig>>,
	) -> crate::Result<crate::container::Producer<C, hang::catalog::VideoConfig>>
	where
		crate::Error: From<C::Error>,
	{
		self.track(track, container, config)
	}

	/// Publish an audio track and own its catalog rendition.
	pub fn audio<C: crate::container::Container>(
		&self,
		track: moq_net::track::Producer,
		container: C,
		config: impl Into<Option<hang::catalog::AudioConfig>>,
	) -> crate::Result<crate::container::Producer<C, hang::catalog::AudioConfig>>
	where
		crate::Error: From<C::Error>,
	{
		self.track(track, container, config)
	}

	/// Publish a text track and own its catalog rendition.
	pub fn text<C: crate::container::Container>(
		&self,
		track: moq_net::track::Producer,
		container: C,
		config: impl Into<Option<hang::catalog::TextConfig>>,
	) -> crate::Result<crate::container::Producer<C, hang::catalog::TextConfig>>
	where
		crate::Error: From<C::Error>,
	{
		self.track(track, container, config)
	}

	/// Publish a track using a custom catalog config.
	pub fn track<C, R>(
		&self,
		track: moq_net::track::Producer,
		container: C,
		config: impl Into<Option<R>>,
	) -> crate::Result<crate::container::Producer<C, R>>
	where
		C: crate::container::Container,
		R: super::RenditionConfig<E>,
		crate::Error: From<C::Error>,
	{
		let rendition = self.rendition(track.name())?;
		self.media(track, container, rendition, config.into())
	}

	/// Whether a live producer or published config already claims `name` in `R`'s section.
	pub fn is_claimed<R: super::RenditionConfig<E>>(&self, name: &str) -> bool {
		let mut state = take(&self.current);
		R::get_mut(&mut state.catalog, name).is_some() || state.owned.contains(&owner_key::<R>(name))
	}

	/// Take `name` in `C`'s section for a new [`Rendition`](super::Rendition), which owns it until
	/// the handle drops.
	///
	/// Errors if a live rendition already holds the name, or if the catalog already carries an entry
	/// under it. That's what makes the rendition the only thing that writes or removes its own slot:
	/// a lazily-configured importer (H.264 before its first SPS) has no catalog entry yet, and
	/// without the name being held an outside write would land in the gap, be overwritten by
	/// [`set`](super::Rendition::set), and then be deleted by the rendition's `Drop`.
	pub(super) fn acquire<C: super::RenditionConfig<E>>(&self, name: &str) -> crate::Result<()> {
		let mut state = take(&self.current);
		if C::get_mut(&mut state.catalog, name).is_some() || !state.owned.insert(owner_key::<C>(name)) {
			return Err(hang::Error::Duplicate(name.to_string()).into());
		}
		Ok(())
	}

	/// Register a live [`Reserved`](super::Reserved) handle.
	pub(super) fn add_reserver(&self) {
		take(&self.current).reservations.reservers += 1;
	}

	/// Drop a [`Reserved`](super::Reserved) handle, flushing the initial snapshot if that was the
	/// last thing gating it.
	pub(super) fn release_reserver(&mut self) {
		{
			let mut state = take(&self.current);
			let r = &mut state.reservations;
			r.reservers = r.reservers.saturating_sub(1);
		}
		self.flush_if_ready();
	}

	/// Publish the buffered snapshot once, if buffering has just finished with a change staged.
	pub(super) fn flush_if_ready(&mut self) {
		// Snapshot under the lock and emit outside it, since emitting writes to the catalog tracks.
		let catalog = {
			let mut state = take(&self.current);
			let r = &mut state.reservations;
			if r.published || r.reservers != 0 {
				return;
			}
			// Nothing was staged (e.g. every stream was ignored): stay unpublished so a later change
			// still triggers the first emit, rather than publishing an empty catalog now.
			if !r.pending {
				return;
			}
			r.pending = false;
			r.published = true;
			state.catalog.clone()
		};
		if let Err(err) = self.outputs.emit(&catalog) {
			tracing::error!(%err, "failed to publish the catalog, aborting its tracks");
			self.outputs.abort(&err);
			take(&self.current).closed = Some(err);
		}
	}

	/// Build the media [`container::Producer`](crate::container::Producer) for `track`,
	/// enrolling it in the broadcast's timeline so its groups are indexed into the aligned
	/// segments.
	///
	/// The broadcast's one timeline track is created (and advertised in the catalog's root
	/// `archive` entry) on first use; see [`timeline`](crate::timeline) for the whole model.
	pub(super) fn media<C, R>(
		&self,
		track: moq_net::track::Producer,
		container: C,
		rendition: super::Rendition<E, R>,
		config: Option<R>,
	) -> crate::Result<crate::container::Producer<C, R>>
	where
		C: crate::container::Container,
		R: super::RenditionConfig<E>,
		crate::Error: From<C::Error>,
	{
		let mut catalog = self.clone();
		let recorder = catalog.enroll(track.name())?;
		let mut producer = crate::container::Producer::with_rendition(track, container, rendition)
			.with_recorder(recorder)
			.with_bandwidth(self.bandwidth.clone());
		if let Some(config) = config {
			producer.set(config)?;
		}
		Ok(producer)
	}

	/// Build an internal container producer for a non-rendition catalog entry.
	pub(crate) fn media_raw<C>(
		&self,
		track: moq_net::track::Producer,
		container: C,
	) -> crate::Result<crate::container::Producer<C>>
	where
		C: crate::container::Container,
		crate::Error: From<C::Error>,
	{
		let mut catalog = self.clone();
		let recorder = catalog.enroll(track.name())?;
		Ok(crate::container::Producer::new(track, container)
			.with_recorder(recorder)
			.with_bandwidth(self.bandwidth.clone()))
	}

	/// A fresh estimator whose `delay` is measured against this catalog's other renditions.
	pub(crate) fn estimator(&self) -> super::Estimator {
		super::Estimator::with_broadcast(self.baseline.clone())
	}

	/// The allocator passthrough tracks claim on. fMP4 writes groups by hand, so it reads this itself.
	pub(crate) fn bandwidth(&self) -> moq_net::bandwidth::Allocator {
		self.bandwidth.clone()
	}

	/// Enroll `track` in the broadcast's timeline, advertising the timeline in the catalog's
	/// root section the first time.
	///
	/// The role-specific track constructors call this for you. fMP4 passthrough calls it directly
	/// because it writes groups by hand instead of using a [`container::Producer`](crate::container::Producer).
	pub(crate) fn enroll(&mut self, track: &str) -> crate::Result<crate::timeline::Recorder> {
		let recorder = self.timeline.pacing_track(track)?;

		let section = self.timeline.section();
		let mut catalog = self.modify()?;
		if catalog.archive.is_none() {
			catalog.archive = Some(section);
		}
		catalog.commit()?;

		Ok(recorder)
	}

	/// The broadcast's [`timeline::Producer`](crate::timeline::Producer): its segment index.
	///
	/// The MoQ track behind it is created (and advertised in the catalog's root `archive`
	/// entry) when the first media track enrolls, so reading this costs nothing on a
	/// broadcast that never segments. Use it to declare boundaries
	/// ([`cut`](crate::timeline::Producer::cut)) or to hold publishing back while tracks
	/// enroll ([`reserve`](crate::timeline::Producer::reserve), the timeline's counterpart to
	/// this producer's own [`reserve`](Self::reserve)).
	pub fn timeline(&self) -> crate::timeline::Producer {
		self.timeline.clone()
	}

	/// Publish `track` as a latest-value JSON track, advertising it in the catalog.
	///
	/// The caller creates the track on the broadcast, as it does for a media track; this writes its
	/// catalog entry and removes the entry when the returned handle drops. The catalog key is
	/// [`track.name()`](moq_net::track::Producer::name) verbatim, with no `.z` suffix even when
	/// compressed, since the entry's compression flag is what a consumer reads.
	///
	/// `config` is a [`json::Config`](crate::json::Config) for the `json` section, or an
	/// application's own entry embedding a [`JsonConfig`](hang::catalog::JsonConfig) (see
	/// [`IntoRendition`](super::IntoRendition)). The producer sets its `mode`, encodes the track
	/// with its `compression`, and fills an absent `bitrate` from what it writes. A
	/// [`delta_ratio`](crate::json::Config::delta_ratio) on `json::Config` selects the snapshot
	/// encoder; it is not written into the catalog entry. The config is `'static` so that ratio
	/// can be read off the builder.
	///
	/// Errors if the entry's section already carries that name, for example an entry seeded
	/// through [`Config::with_catalog`] or one pointing at a sibling broadcast, or if the entry
	/// declares a compression this build can't write or references another broadcast.
	pub fn json_snapshot<T: serde::Serialize>(
		&self,
		track: moq_net::track::Producer,
		config: impl super::IntoRendition<E, hang::catalog::JsonConfig> + 'static,
	) -> crate::Result<crate::json::Snapshot<T, E>> {
		let rendition = self.data_entry(track.name())?;
		crate::json::Snapshot::new(track, rendition, config)
	}

	/// Publish `track` as an append-log JSON track, advertising it in the catalog.
	///
	/// See [`json_snapshot`](Self::json_snapshot) for the lifecycle; this differs only in that every
	/// record is preserved rather than superseded.
	pub fn json_stream<T: serde::Serialize>(
		&self,
		track: moq_net::track::Producer,
		config: impl super::IntoRendition<E, hang::catalog::JsonConfig>,
	) -> crate::Result<crate::json::Stream<T, E>> {
		let rendition = self.data_entry(track.name())?;
		crate::json::Stream::new(track, rendition, config.into_rendition())
	}

	/// Publish `track` as a latest-value binary track, advertising it in the catalog.
	///
	/// See [`json_snapshot`](Self::json_snapshot) for the lifecycle; this differs only in that the
	/// payloads are opaque bytes, and `config` is a [`binary::Config`](crate::binary::Config) or an
	/// entry embedding a [`BinaryConfig`](hang::catalog::BinaryConfig).
	pub fn binary_snapshot(
		&self,
		track: moq_net::track::Producer,
		config: impl super::IntoRendition<E, hang::catalog::BinaryConfig>,
	) -> crate::Result<crate::binary::Snapshot<E>> {
		let rendition = self.data_entry(track.name())?;
		crate::binary::Snapshot::new(track, rendition, config.into_rendition())
	}

	/// Publish `track` as an append-log binary track, advertising it in the catalog.
	///
	/// See [`binary_snapshot`](Self::binary_snapshot); this differs only in that every payload is
	/// preserved rather than superseded.
	pub fn binary_stream(
		&self,
		track: moq_net::track::Producer,
		config: impl super::IntoRendition<E, hang::catalog::BinaryConfig>,
	) -> crate::Result<crate::binary::Stream<E>> {
		let rendition = self.data_entry(track.name())?;
		crate::binary::Stream::new(track, rendition, config.into_rendition())
	}

	/// Reserve the catalog entry a data producer owns, keyed by its track name.
	///
	/// The rendition is reserved but not yet set, so the caller fills it in with the config for the
	/// mode it is about to publish. Reserving is what rejects a name the catalog already carries,
	/// including an entry with no local track behind it (seeded through
	/// [`Config::with_catalog`], or pointing at a sibling broadcast), which publishing over would
	/// silently replace and then retire when this handle drops.
	fn data_entry<C: super::RenditionConfig<E>>(&self, name: &str) -> crate::Result<super::Rendition<E, C>> {
		self.reserve().init(name)
	}

	/// Create a consumer for this catalog, receiving updates as they're published.
	pub fn consume(&self) -> Result<Consumer<E>, moq_net::Error> {
		Ok(Consumer::new(self.outputs.hang.consume()))
	}

	/// Finish publishing to this catalog.
	pub fn finish(&mut self) -> crate::Result<()> {
		take(&self.current).closed = Some(moq_net::Error::Closed.into());
		self.outputs.hang.finish()?;
		self.outputs.hangz.finish()?;
		self.outputs.msf_track.finish()?;
		self.timeline.finish()?;
		Ok(())
	}
}

/// RAII guard for modifying a catalog with automatic publishing on drop.
///
/// Obtained via [`Producer::modify`]. Derefs to the [`Catalog<E>`](super::hang::Catalog), so base
/// sections are editable directly and application sections are editable through `ext`.
///
/// On drop, the hang, compressed-hang, and MSF catalog tracks are updated if the catalog was
/// mutated. That publish cannot return an error, so a failure (an extension that won't serialize,
/// a catalog too large for a frame) aborts the catalog tracks instead: consumers see the error and
/// the next [`Producer::modify`] returns it. Call [`commit`](Self::commit) when the caller can act
/// on the failure itself.
pub struct Guard<'a, E: CatalogExt = ()> {
	state: MutexGuard<'a, State<E>>,
	outputs: &'a mut Outputs<E>,
	updated: bool,
}

impl<E: CatalogExt> Guard<'_, E> {
	/// Publish the edited catalog to every catalog track, returning any error.
	///
	/// Consumes the guard, so the subsequent drop publishes nothing. A no-op if the catalog was never
	/// mutated, and still withheld while a [`Reserved`](super::Reserved) gates the initial snapshot.
	/// Unlike a drop, a failure here leaves the tracks open: the caller has the error and decides
	/// what to do with it.
	pub fn commit(mut self) -> crate::Result<()> {
		self.publish()
	}

	/// Publish a mutated catalog once, clearing the flag so it isn't published again.
	fn publish(&mut self) -> crate::Result<()> {
		if !self.updated {
			return Ok(());
		}
		self.updated = false;

		{
			let r = &mut self.state.reservations;
			// Withhold every emit while still buffering the initial reserved set; the mutation stays
			// in `current` and `pending` marks it for the flush once the gate opens.
			if !r.published && r.reservers != 0 {
				r.pending = true;
				return Ok(());
			}
			r.pending = false;
			r.published = true;
		}

		self.outputs.emit(&self.state.catalog)
	}

	/// Release a name taken by [`Producer::acquire`], along with the entry it owns.
	///
	/// Both under one lock, so an [`acquire`](Producer::acquire) never sees the rendition half-gone:
	/// name freed with its entry still there (a spurious duplicate), or entry gone with the name
	/// still held (a spurious refusal).
	///
	/// The name goes first, before any caller code. [`RenditionConfig::remove`](super::RenditionConfig::remove)
	/// is a third-party hook that can panic, and a stranded entry is recoverable while a stranded
	/// name is not: nothing would ever reserve it again.
	pub(super) fn release<C: super::RenditionConfig<E>>(&mut self, name: &str, present: bool) {
		self.state.owned.remove(&owner_key::<C>(name));

		if present {
			C::remove(&mut self.state.catalog, name);
			self.updated = true;
		}
	}
}

impl<E: CatalogExt> Deref for Guard<'_, E> {
	type Target = Catalog<E>;

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

impl<E: CatalogExt> DerefMut for Guard<'_, E> {
	fn deref_mut(&mut self) -> &mut Self::Target {
		self.updated = true;
		&mut self.state.catalog
	}
}

impl Guard<'_, Extra> {
	/// Set (or replace) a top-level application catalog section, republished on drop.
	///
	/// Errors if `name` is a HANG root (`video`, `audio`, `text`, `archive`, `clock`, `json`,
	/// `binary`, or retired `timeline`) or an MSF root (`version`, `generatedAt`, `isComplete`,
	/// `tracks`, or `initDataList`).
	pub fn set_section(&mut self, name: impl Into<String>, value: serde_json::Value) -> crate::Result<()> {
		self.state.catalog.ext.set(name, value)?;
		self.updated = true;
		Ok(())
	}

	/// Remove a top-level application catalog section, republished on drop if it existed.
	///
	/// Returns the section's previous value, or `None` if it was absent.
	pub fn remove_section(&mut self, name: &str) -> Option<serde_json::Value> {
		let removed = self.state.catalog.ext.remove(name);
		if removed.is_some() {
			self.updated = true;
		}
		removed
	}
}

impl<E: CatalogExt> Drop for Guard<'_, E> {
	fn drop(&mut self) {
		if let Err(err) = self.publish() {
			if !std::thread::panicking() {
				tracing::error!(%err, "failed to publish the catalog on guard drop, aborting its tracks");
			}
			self.outputs.abort(&err);
			self.state.closed = Some(err);
		}
	}
}

/// Determine the SAP starting type for a given video codec.
///
/// SAP type 1: closed GOP with no leading pictures (IDR at every SAP).
/// Used for VP8, which has no B-frames.
///
/// SAP type 2: closed GOP with possible leading pictures. Used for codecs
/// that can carry B-frames (H.264, AV1, VP9) since the encoder may emit
/// leading B-frames after an IDR.
///
/// Returns None for unknown codecs (and H.265, which we don't yet validate
/// the SAP behavior of) so the field is omitted from the catalog.
fn video_sap_type(codec: &hang::catalog::VideoCodec) -> Option<u8> {
	use hang::catalog::VideoCodec;
	match codec {
		VideoCodec::VP8 => Some(1),
		VideoCodec::H264(_) | VideoCodec::AV1(_) | VideoCodec::VP9(_) => Some(2),
		_ => None,
	}
}

/// The MSF packaging that announces `container`, or `None` to leave the rendition out of the catalog.
///
/// Exhaustive on purpose: a catch-all is what announced LOC as legacy, and it would do the same to the
/// next container added. hang says an unrecognized one must be ignored, so it keeps its wire kind (no
/// MSF consumer claims one it does not know) and is dropped when it does not even name one.
fn packaging_of(container: &hang::catalog::Container) -> Option<moq_msf::Packaging> {
	match container {
		hang::catalog::Container::Cmaf { .. } => Some(moq_msf::Packaging::Cmaf),
		hang::catalog::Container::Loc => Some(moq_msf::Packaging::Loc),
		hang::catalog::Container::Legacy => Some(moq_msf::Packaging::Legacy),
		hang::catalog::Container::Unknown(unknown) => {
			unknown.kind().map(|kind| moq_msf::Packaging::Unknown(kind.to_string()))
		}
	}
}

/// Convert a catalog to its MSF equivalent: the media sections become MSF tracks and the
/// extension rides the MSF root under the same member names it uses in the hang catalog.
fn to_msf<E: CatalogExt>(catalog: &Catalog<E>) -> moq_msf::Catalog<E> {
	let mut msf = to_msf_media(&catalog.media());
	msf.ext = catalog.ext.clone();
	msf
}

/// Convert the base media sections of a hang catalog into MSF tracks.
fn to_msf_media<E: CatalogExt>(catalog: &hang::Catalog) -> moq_msf::Catalog<E> {
	let mut tracks = Vec::new();

	let has_multiple_video = catalog.video.renditions.len() > 1;
	for (name, config) in &catalog.video.renditions {
		let Some(packaging) = packaging_of(&config.container) else {
			continue;
		};

		let init_data = match &config.container {
			hang::catalog::Container::Cmaf { init, .. } => Some(base64::engine::general_purpose::STANDARD.encode(init)),
			_ => config
				.description
				.as_ref()
				.map(|d| base64::engine::general_purpose::STANDARD.encode(d.as_ref())),
		};

		let sap_type = video_sap_type(&config.codec);
		let mut track = moq_msf::Track::new(name.clone(), packaging);
		track.is_live = true;
		track.role = Some(moq_msf::Role::Video);
		track.codec = Some(config.codec.to_string());
		track.width = config.coded_width;
		track.height = config.coded_height;
		track.framerate = config.framerate;
		track.bitrate = config.bitrate;
		track.stalled = config.stalled;
		track.init_data = init_data;
		track.render_group = Some(1);
		track.alt_group = if has_multiple_video { Some(1) } else { None };
		track.max_grp_sap_starting_type = sap_type;
		track.max_obj_sap_starting_type = sap_type;
		track.jitter = config.jitter;
		track.delay = config.delay;
		tracks.push(track);
	}

	let has_multiple_audio = catalog.audio.renditions.len() > 1;
	for (name, config) in &catalog.audio.renditions {
		let Some(packaging) = packaging_of(&config.container) else {
			continue;
		};

		let init_data = match &config.container {
			hang::catalog::Container::Cmaf { init, .. } => Some(base64::engine::general_purpose::STANDARD.encode(init)),
			_ => config
				.description
				.as_ref()
				.map(|d| base64::engine::general_purpose::STANDARD.encode(d.as_ref())),
		};

		let mut track = moq_msf::Track::new(name.clone(), packaging);
		track.is_live = true;
		track.role = Some(moq_msf::Role::Audio);
		track.codec = Some(config.codec.to_string());
		track.samplerate = Some(config.sample_rate);
		track.channel_config = Some(config.channel_count.to_string());
		track.bitrate = config.bitrate;
		track.init_data = init_data;
		track.render_group = Some(1);
		track.alt_group = if has_multiple_audio { Some(1) } else { None };
		track.max_grp_sap_starting_type = Some(1);
		track.max_obj_sap_starting_type = Some(1);
		track.jitter = config.jitter;
		track.delay = config.delay;
		tracks.push(track);
	}

	moq_msf::Catalog::with_ext(tracks, E::default())
}

#[cfg(test)]
mod test {
	use std::collections::BTreeMap;

	use std::task::Poll;

	use bytes::Bytes;
	use hang::catalog::{AudioCodec, AudioConfig, Container, H264, VideoConfig};

	use super::*;

	/// The catalog, its MSF twin, and the timeline all rank above media. They're the
	/// index a player reads before any media is useful, and they're small enough that
	/// sitting above media can't starve it. Regression: the MSF and timeline tracks
	/// were minted with no `Info` at all, so one broadcast advertised its hang catalog
	/// at 100 and the same catalog in MSF at 0.
	#[tokio::test]
	async fn non_media_tracks_rank_above_media() {
		use hang::catalog::PRIORITY;

		let mut broadcast = moq_net::broadcast::Info::new().produce();
		let catalog = Producer::new(&mut broadcast, Config::default()).unwrap();
		// Pacing enrollment is what mints the timeline track; a passive one publishes none.
		catalog.timeline().pacing_track("video").unwrap();

		let consumer = broadcast.consume();
		for name in [
			hang::Catalog::DEFAULT_NAME,
			hang::Catalog::COMPRESSED_NAME,
			moq_msf::DEFAULT_NAME,
			hang::timeline::DEFAULT_NAME,
		] {
			let track = consumer.track(name).expect("track");
			let info = track.query().await.expect("info");
			assert_eq!(info.priority, PRIORITY.catalog, "{name} should rank with the catalog");
		}
	}

	#[test]
	fn media_tracks_inherit_the_catalogs_declared_retention() {
		let mut broadcast = moq_net::broadcast::Info::new().produce();

		// Unset, a catalog mints hang's media defaults, sized so a segmented egress can serve a
		// full playlist window rather than moq-net's live-edge default.
		let catalog = Producer::new(&mut broadcast, Config::default()).unwrap();
		assert_eq!(
			catalog.track_info(hang::catalog::PRIORITY.video).max_age,
			hang::container::track_info(hang::catalog::PRIORITY.video).max_age
		);
		assert!(catalog.track_info(hang::catalog::PRIORITY.video).max_age > moq_net::track::DEFAULT_MAX_AGE);

		// An override reaches every media track this catalog mints, and does NOT disturb the
		// timescale hang pins (or survive a retimescale for a source-scale container).
		let mut broadcast = moq_net::broadcast::Info::new().produce();
		let config = Config::default().with_max_age(std::time::Duration::from_secs(3));
		let catalog = Producer::new(&mut broadcast, config).unwrap();

		let info = catalog.track_info(hang::catalog::PRIORITY.video);
		assert_eq!(info.max_age, std::time::Duration::from_secs(3));
		assert_eq!(info.timescale, hang::container::TIMESCALE);

		let at = info.with_timescale(moq_net::Timescale::MILLI);
		assert_eq!(at.max_age, std::time::Duration::from_secs(3));
		assert_eq!(at.timescale, moq_net::Timescale::MILLI);

		// Every handle mints under the same policy, whatever order it was taken in: the codec
		// paths hold a reservation and the container paths hold a clone.
		assert_eq!(
			catalog.reserve().track_info(hang::catalog::PRIORITY.video).max_age,
			std::time::Duration::from_secs(3)
		);
		assert_eq!(
			catalog.clone().track_info(hang::catalog::PRIORITY.video).max_age,
			std::time::Duration::from_secs(3)
		);
	}

	#[test]
	fn publishes_plain_and_compressed_tracks() {
		let mut broadcast = moq_net::broadcast::Info::new().produce();
		let mut catalog = Producer::new(&mut broadcast, Config::default()).unwrap();

		let mut plain = Consumer::new(catalog.outputs.hang.consume());
		let mut compressed = Consumer::compressed(catalog.outputs.hangz.consume());

		{
			let mut guard = catalog.modify().unwrap();
			guard
				.audio
				.renditions
				.insert("audio0".to_string(), AudioConfig::new(AudioCodec::Opus, 48_000, 2));
		}
		let expected = catalog.snapshot();

		let waiter = kio::Waiter::noop();
		let got_plain = match plain.poll_next(&waiter) {
			Poll::Ready(Ok(Some(c))) => c,
			other => panic!("expected plain catalog, got {other:?}"),
		};
		let got_compressed = match compressed.poll_next(&waiter) {
			Poll::Ready(Ok(Some(c))) => c,
			other => panic!("expected compressed catalog, got {other:?}"),
		};

		assert_eq!(got_plain, expected);
		assert_eq!(got_compressed, expected);
	}

	#[test]
	fn mutate_composes_independent_owners() {
		let mut broadcast = moq_net::broadcast::Info::new().produce();
		let mut catalog = Producer::new(&mut broadcast, Config::default()).unwrap();
		let mut plain = Consumer::new(catalog.outputs.hang.consume());

		catalog
			.mutate(|c| {
				c.audio
					.renditions
					.insert("audio0".to_string(), AudioConfig::new(AudioCodec::Opus, 48_000, 2));
			})
			.unwrap();

		// The second owner starts from the published catalog and adds its own section.
		catalog
			.mutate(|c| {
				c.video.renditions.insert("video0".to_string(), h264_config());
			})
			.unwrap();

		// A closure that changes nothing publishes nothing: the catalog track runs one snapshot per
		// group, so a third publish would open a third group.
		catalog.mutate(|_| {}).unwrap();
		assert_eq!(catalog.outputs.hang_track.latest(), Some(1));

		let expected = catalog.snapshot();
		let waiter = kio::Waiter::noop();
		let mut last = None;
		while let Poll::Ready(Ok(Some(c))) = plain.poll_next(&waiter) {
			last = Some(c);
		}
		assert_eq!(last.unwrap(), expected);
		assert!(expected.audio.renditions.contains_key("audio0"));
		assert!(expected.video.renditions.contains_key("video0"));
	}

	#[test]
	fn modify_refuses_a_finished_catalog() {
		let mut broadcast = moq_net::broadcast::Info::new().produce();
		let mut catalog = Producer::new(&mut broadcast, Config::default()).unwrap();

		catalog
			.modify()
			.unwrap()
			.audio
			.renditions
			.insert("audio0".to_string(), AudioConfig::new(AudioCodec::Opus, 48_000, 2));
		catalog.finish().unwrap();

		assert!(matches!(
			catalog.modify(),
			Err(crate::Error::Moq(moq_net::Error::Closed))
		));
		assert!(matches!(
			catalog.enroll("audio0"),
			Err(crate::Error::Moq(moq_net::Error::Closed))
		));
	}

	#[test]
	fn a_dropped_failed_edit_aborts_every_catalog_track() {
		let mut broadcast = moq_net::broadcast::Info::new().produce();
		let config = Config::default().with_catalog(Catalog::<Extra>::default());
		let mut catalog = Producer::<Extra>::new(&mut broadcast, config).unwrap();
		let mut hang = catalog.outputs.hang.consume().ordered();
		let mut msf = catalog.outputs.msf_track.subscribe(None).ordered();

		// Something the hang track rejects, so the publish fails before the MSF track is reached.
		catalog
			.modify()
			.unwrap()
			.set_section("big", "x".repeat(moq_net::group::MAX_CACHE_BYTES as usize + 1).into())
			.unwrap();

		assert!(matches!(
			catalog.modify(),
			Err(crate::Error::Json(moq_json::Error::Net(moq_net::Error::FrameTooLarge)))
		));
		let waiter = kio::Waiter::noop();
		assert!(matches!(
			hang.poll_next_group(&waiter),
			Poll::Ready(Err(moq_net::Error::FrameTooLarge))
		));
		assert!(
			matches!(
				msf.poll_next_group(&waiter),
				Poll::Ready(Err(moq_net::Error::FrameTooLarge))
			),
			"a track the publish never reached must not outlive the others"
		);
	}

	#[test]
	fn a_failed_commit_leaves_the_catalog_open() {
		let mut broadcast = moq_net::broadcast::Info::new().produce();
		let config = Config::default().with_catalog(Catalog::<Extra>::default());
		let mut catalog = Producer::<Extra>::new(&mut broadcast, config).unwrap();
		let track = catalog.outputs.hang.consume();

		let mut guard = catalog.modify().unwrap();
		guard
			.set_section("big", "x".repeat(moq_net::group::MAX_CACHE_BYTES as usize + 1).into())
			.unwrap();
		assert!(matches!(
			guard.commit(),
			Err(crate::Error::Json(moq_json::Error::Net(moq_net::Error::FrameTooLarge)))
		));

		// The caller got the error, so the tracks stay usable once the catalog fits again.
		catalog.modify().unwrap().remove_section("big").unwrap();
		catalog.finish().unwrap();
		assert_eq!(track.latest(), Some(0));
	}

	#[test]
	fn commit_publishes_once() {
		let mut broadcast = moq_net::broadcast::Info::new().produce();
		let mut catalog = Producer::new(&mut broadcast, Config::default()).unwrap();
		let track = catalog.outputs.hang.consume();

		let mut guard = catalog.modify().unwrap();
		guard
			.audio
			.renditions
			.insert("audio0".to_string(), AudioConfig::new(AudioCodec::Opus, 48_000, 2));
		guard.commit().unwrap();

		// The drop that follows `commit` must not publish a second snapshot.
		catalog.finish().unwrap();
		assert_eq!(track.latest(), Some(0));
	}

	#[test]
	fn timeline_reports_a_track_collision() {
		let mut broadcast = moq_net::broadcast::Info::new().produce();
		let mut catalog = Producer::new(&mut broadcast, Config::default()).unwrap();

		// Something else already took the name the timeline track wants.
		let _taken = broadcast.create_track(hang::timeline::DEFAULT_NAME, None).unwrap();
		assert!(catalog.enroll("video0").is_err());
	}

	#[test]
	fn enrolling_advertises_the_catalog_section() {
		let mut broadcast = moq_net::broadcast::Info::new().produce();
		let mut catalog = Producer::new(&mut broadcast, Config::default()).unwrap();

		// A broadcast that never segments never advertises an archive.
		assert_eq!(catalog.snapshot().archive, None);
		let _recorder = catalog.enroll("video0").unwrap();
		assert_eq!(
			catalog.snapshot().archive,
			Some(catalog.timeline().section()),
			"the root archive should advertise the timeline track"
		);
	}

	#[test]
	fn clock_is_advertised_without_an_archive() {
		let mut broadcast = moq_net::broadcast::Info::new().produce();
		let catalog = Producer::new(&mut broadcast, Config::default()).unwrap();

		// A live-only broadcast exposes its clock without creating a segment index.
		let snapshot = catalog.snapshot();
		assert_eq!(snapshot.archive, None);
		assert_eq!(snapshot.clock, Some(catalog.clock().wall()));
	}

	#[test]
	fn with_clock_names_the_contents_real_start() {
		use std::time::{Duration, SystemTime};

		let mut broadcast = moq_net::broadcast::Info::new().produce();
		let wall = SystemTime::UNIX_EPOCH + Duration::from_millis(hang::catalog::MOQ_EPOCH_UNIX_MILLIS + 60_000);
		let config = Config::default()
			.with_clock(crate::Clock::at(std::time::Instant::now(), wall).expect("a representable wall"));
		let catalog = Producer::new(&mut broadcast, config).unwrap();

		// A recording import advertises the content's start, not the construction instant.
		assert_eq!(
			catalog.snapshot().clock.map(|clock| clock.wall),
			Some(moq_net::Timestamp::from_micros(60_000_000).unwrap())
		);
	}

	fn h264_config() -> VideoConfig {
		let mut config = VideoConfig::new(H264 {
			profile: 0x64,
			constraints: 0x00,
			level: 0x1f,
			inline: true,
		});
		config.container = Container::Legacy;
		config
	}

	// A reserved audio rendition that resolves before a reserved video rendition must not publish an
	// audio-only catalog; the first snapshot a consumer sees carries the complete track set. This is
	// the moq-dev/moq#1979 convergence race.
	#[test]
	fn reservation_gates_until_all_renditions_resolve() {
		let mut broadcast = moq_net::broadcast::Info::new().produce();
		let catalog = Producer::new(&mut broadcast, Config::default()).unwrap();
		let mut consumer: Consumer = Consumer::new(catalog.outputs.hang.consume());
		let waiter = kio::Waiter::noop();

		let reserved = catalog.reserve();
		let mut audio = reserved.init::<AudioConfig>("audio0").unwrap();
		let mut video = reserved.init::<VideoConfig>("video0").unwrap();
		drop(reserved); // done reserving; both renditions still outstanding

		// Audio resolves first: withheld, because video is still outstanding.
		audio.set(AudioConfig::new(AudioCodec::Opus, 48_000, 2)).unwrap();
		assert!(
			matches!(consumer.poll_next(&waiter), Poll::Pending),
			"an audio-only catalog must not publish while video is unresolved"
		);

		// Video resolves: the complete catalog publishes now, in one snapshot.
		video.set(h264_config()).unwrap();
		let snapshot = match consumer.poll_next(&waiter) {
			Poll::Ready(Ok(Some(c))) => c,
			other => panic!("expected the complete catalog, got {other:?}"),
		};
		assert!(snapshot.audio.renditions.contains_key("audio0"));
		assert!(snapshot.video.renditions.contains_key("video0"));
		assert!(
			matches!(consumer.poll_next(&waiter), Poll::Pending),
			"only the one complete snapshot should be published"
		);
	}

	#[test]
	fn live_rendition_owns_its_name_without_gating_the_catalog() {
		let mut broadcast = moq_net::broadcast::Info::new().produce();
		let catalog = Producer::new(&mut broadcast, Config::default()).unwrap();
		let mut consumer: Consumer = Consumer::new(catalog.outputs.hang.consume());
		let waiter = kio::Waiter::noop();

		let _unresolved = catalog.rendition::<VideoConfig>("video0").unwrap();
		assert!(
			catalog.rendition::<VideoConfig>("video0").is_err(),
			"the unresolved live rendition owns its name"
		);
		let mut audio = catalog.rendition::<AudioConfig>("audio0").unwrap();
		audio.set(AudioConfig::new(AudioCodec::Opus, 48_000, 2)).unwrap();

		let snapshot = match consumer.poll_next(&waiter) {
			Poll::Ready(Ok(Some(c))) => c,
			other => panic!("expected the incremental audio catalog, got {other:?}"),
		};
		assert!(snapshot.audio.renditions.contains_key("audio0"));
		assert!(!snapshot.video.renditions.contains_key("video0"));
	}

	// Dropping a reservation without fulfilling it (a stream that never produced a config) still opens
	// the gate, publishing whatever did resolve.
	#[test]
	fn reservation_gate_opens_when_unresolved_reservation_is_dropped() {
		let mut broadcast = moq_net::broadcast::Info::new().produce();
		let catalog = Producer::new(&mut broadcast, Config::default()).unwrap();
		let mut consumer: Consumer = Consumer::new(catalog.outputs.hang.consume());
		let waiter = kio::Waiter::noop();

		let reserved = catalog.reserve();
		let mut audio = reserved.init::<AudioConfig>("audio0").unwrap();
		let video = reserved.init::<VideoConfig>("video0").unwrap();
		drop(reserved);

		audio.set(AudioConfig::new(AudioCodec::Opus, 48_000, 2)).unwrap();
		assert!(matches!(consumer.poll_next(&waiter), Poll::Pending));

		// The video stream never resolves and is cancelled; the audio-only catalog publishes.
		drop(video);
		let snapshot = match consumer.poll_next(&waiter) {
			Poll::Ready(Ok(Some(c))) => c,
			other => panic!("expected the audio catalog, got {other:?}"),
		};
		assert!(snapshot.audio.renditions.contains_key("audio0"));
		assert!(!snapshot.video.renditions.contains_key("video0"));
	}

	// A change staged while a deferred importer still holds a reservation (mimicking a TS stream whose
	// config resolves late, e.g. AAC) must not publish until that rendition resolves, so no incomplete
	// intermediate snapshot leaks out.
	#[test]
	fn staged_change_waits_for_a_held_reservation() {
		let mut broadcast = moq_net::broadcast::Info::new().produce();
		let catalog = Producer::new(&mut broadcast, Config::default()).unwrap();
		let mut consumer: Consumer = Consumer::new(catalog.outputs.hang.consume());
		let waiter = kio::Waiter::noop();

		// A deferred importer grabs a reservation up front and holds it until it can resolve.
		let deferred = catalog.reserve();

		// Meanwhile an eager rendition resolves and stages a catalog change. The importer keeps its
		// rendition alive (dropping it would retire the track), so bind it.
		let early = catalog.reserve();
		let mut a0 = early.init::<AudioConfig>("audio0").unwrap();
		a0.set(AudioConfig::new(AudioCodec::Opus, 48_000, 2)).unwrap();
		drop(early);
		assert!(
			matches!(consumer.poll_next(&waiter), Poll::Pending),
			"the staged rendition must wait for the deferred importer's held reservation"
		);

		// The deferred importer finally builds its rendition and resolves it.
		let mut late = deferred.init::<AudioConfig>("audio1").unwrap();
		drop(deferred); // the importer releases its own hold; only the rendition's remains
		assert!(matches!(consumer.poll_next(&waiter), Poll::Pending));
		late.set(AudioConfig::new(AudioCodec::Opus, 48_000, 1)).unwrap();

		let snapshot = match consumer.poll_next(&waiter) {
			Poll::Ready(Ok(Some(c))) => c,
			other => panic!("expected one complete catalog, got {other:?}"),
		};
		assert!(snapshot.audio.renditions.contains_key("audio0"));
		assert!(snapshot.audio.renditions.contains_key("audio1"));
		assert!(
			matches!(consumer.poll_next(&waiter), Poll::Pending),
			"only the one complete snapshot should be published"
		);
	}

	#[test]
	fn convert_simple() {
		let mut video_config = VideoConfig::new(H264 {
			profile: 0x64,
			constraints: 0x00,
			level: 0x1f,
			inline: true,
		});
		video_config.coded_width = Some(1280);
		video_config.coded_height = Some(720);
		video_config.bitrate = Some(6_000_000);
		video_config.stalled = Some(true);
		video_config.framerate = Some(30.0);
		video_config.container = Container::Legacy;

		let mut video_renditions = BTreeMap::new();
		video_renditions.insert("video0.avc3".to_string(), video_config);

		let mut audio_config = AudioConfig::new(AudioCodec::Opus, 48_000, 2);
		audio_config.bitrate = Some(128_000);
		audio_config.container = Container::Legacy;

		let mut audio_renditions = BTreeMap::new();
		audio_renditions.insert("audio0".to_string(), audio_config);

		let mut catalog = hang::Catalog::default();
		catalog.video.renditions = video_renditions;
		catalog.audio.renditions = audio_renditions;

		let msf = to_msf_media::<()>(&catalog);

		assert_eq!(msf.tracks.len(), 2);

		let video = &msf.tracks[0];
		assert_eq!(video.name, "video0.avc3");
		assert_eq!(video.role, Some(moq_msf::Role::Video));
		assert_eq!(video.packaging, moq_msf::Packaging::Legacy);
		assert_eq!(video.codec, Some("avc3.64001f".to_string()));
		assert_eq!(video.width, Some(1280));
		assert_eq!(video.height, Some(720));
		assert_eq!(video.framerate, Some(30.0));
		assert_eq!(video.bitrate, Some(6_000_000));
		assert_eq!(video.stalled, Some(true));
		assert!(video.init_data.is_none());
		// H.264 may carry B-frames, so SAP starting type is 2 (leading pictures allowed).
		assert_eq!(video.max_grp_sap_starting_type, Some(2));
		assert_eq!(video.max_obj_sap_starting_type, Some(2));
		assert_eq!(video.jitter, None);

		let audio = &msf.tracks[1];
		assert_eq!(audio.name, "audio0");
		assert_eq!(audio.role, Some(moq_msf::Role::Audio));
		assert_eq!(audio.packaging, moq_msf::Packaging::Legacy);
		assert_eq!(audio.codec, Some("opus".to_string()));
		assert_eq!(audio.samplerate, Some(48_000));
		assert_eq!(audio.channel_config, Some("2".to_string()));
		assert_eq!(audio.bitrate, Some(128_000));
		assert_eq!(audio.max_grp_sap_starting_type, Some(1));
		assert_eq!(audio.max_obj_sap_starting_type, Some(1));
		assert_eq!(audio.jitter, None);
	}

	// A LOC rendition must advertise `packaging: loc`. MSF-01 has no legacy packaging, so falling into
	// the legacy arm published a catalog that named a container the receiver would not find on the wire.
	#[test]
	fn convert_video_loc_container() {
		let mut video_config = VideoConfig::new(H264 {
			profile: 0x64,
			constraints: 0x00,
			level: 0x1f,
			inline: true,
		});
		video_config.container = Container::Loc;

		let mut video_renditions = BTreeMap::new();
		video_renditions.insert("video0.avc3".to_string(), video_config);

		let mut catalog = hang::Catalog::default();
		catalog.video.renditions = video_renditions;

		let msf = to_msf_media::<()>(&catalog);
		assert_eq!(msf.tracks.len(), 1);
		assert_eq!(msf.tracks[0].packaging, moq_msf::Packaging::Loc);
	}

	#[test]
	fn convert_audio_loc_container() {
		let mut audio_config = AudioConfig::new(AudioCodec::Opus, 48_000, 2);
		audio_config.container = Container::Loc;

		let mut audio_renditions = BTreeMap::new();
		audio_renditions.insert("audio0".to_string(), audio_config);

		let mut catalog = hang::Catalog::default();
		catalog.audio.renditions = audio_renditions;

		let msf = to_msf_media::<()>(&catalog);
		assert_eq!(msf.tracks.len(), 1);
		assert_eq!(msf.tracks[0].packaging, moq_msf::Packaging::Loc);
	}

	// An unrecognized container keeps its wire kind instead of posing as legacy: hang says such a
	// rendition must be ignored, and no MSF consumer claims an unknown packaging.
	#[test]
	fn convert_unknown_container_keeps_its_kind() {
		let container: Container = serde_json::from_value(serde_json::json!({ "kind": "future" })).unwrap();
		assert!(matches!(container, Container::Unknown(_)));

		let mut audio_config = AudioConfig::new(AudioCodec::Opus, 48_000, 2);
		audio_config.container = container;

		let mut audio_renditions = BTreeMap::new();
		audio_renditions.insert("audio0".to_string(), audio_config);

		let mut catalog = hang::Catalog::default();
		catalog.audio.renditions = audio_renditions;

		let msf = to_msf_media::<()>(&catalog);
		assert_eq!(msf.tracks.len(), 1);
		assert_eq!(
			msf.tracks[0].packaging,
			moq_msf::Packaging::Unknown("future".to_string())
		);
	}

	#[test]
	fn convert_with_description() {
		let mut video_config = VideoConfig::new(H264 {
			profile: 0x64,
			constraints: 0x00,
			level: 0x1f,
			inline: false,
		});
		video_config.description = Some(Bytes::from_static(&[0x01, 0x02, 0x03]));
		video_config.coded_width = Some(1920);
		video_config.coded_height = Some(1080);
		video_config.container = Container::Legacy;

		let mut video_renditions = BTreeMap::new();
		video_renditions.insert("video0.m4s".to_string(), video_config);

		let mut catalog = hang::Catalog::default();
		catalog.video.renditions = video_renditions;

		let msf = to_msf_media::<()>(&catalog);
		let video = &msf.tracks[0];
		assert_eq!(video.init_data, Some("AQID".to_string()));
	}

	#[test]
	fn convert_empty() {
		let catalog = hang::Catalog::default();
		let msf = to_msf_media::<()>(&catalog);
		assert!(msf.tracks.is_empty());
	}

	#[test]
	fn convert_cmaf_packaging() {
		let mut video_config = VideoConfig::new(H264 {
			profile: 0x64,
			constraints: 0x00,
			level: 0x28,
			inline: false,
		});
		video_config.coded_width = Some(1920);
		video_config.coded_height = Some(1080);
		video_config.container = Container::Cmaf {
			init: base64::engine::general_purpose::STANDARD
				.decode("AAAYZ2Z0eXA=")
				.unwrap()
				.into(),
		};

		let mut video_renditions = BTreeMap::new();
		video_renditions.insert("video0.m4s".to_string(), video_config);

		let mut catalog = hang::Catalog::default();
		catalog.video.renditions = video_renditions;

		let msf = to_msf_media::<()>(&catalog);
		let video = &msf.tracks[0];
		assert_eq!(video.packaging, moq_msf::Packaging::Cmaf);
		assert_eq!(video.init_data, Some("AAAYZ2Z0eXA=".to_string()));
	}

	#[test]
	fn convert_sap_h264_with_jitter() {
		let mut video_config = VideoConfig::new(H264 {
			profile: 0x64,
			constraints: 0x00,
			level: 0x1f,
			inline: true,
		});
		video_config.coded_width = Some(1280);
		video_config.coded_height = Some(720);
		video_config.framerate = Some(30.0);
		video_config.container = Container::Legacy;
		video_config.jitter = Some(std::time::Duration::from_millis(100));
		video_config.delay = Some(std::time::Duration::from_millis(200));

		let mut video_renditions = BTreeMap::new();
		video_renditions.insert("video0".to_string(), video_config);

		let mut audio_config = AudioConfig::new(AudioCodec::Opus, 48_000, 2);
		audio_config.container = Container::Legacy;
		audio_config.jitter = Some(std::time::Duration::from_millis(40));
		audio_config.delay = Some(std::time::Duration::from_millis(80));

		let mut audio_renditions = BTreeMap::new();
		audio_renditions.insert("audio0".to_string(), audio_config);

		let mut catalog = hang::Catalog::default();
		catalog.video.renditions = video_renditions;
		catalog.audio.renditions = audio_renditions;

		let msf = to_msf_media::<()>(&catalog);

		let video = &msf.tracks[0];
		assert_eq!(video.role, Some(moq_msf::Role::Video));
		// H.264 may carry B-frames, so SAP starting type is 2.
		assert_eq!(video.max_grp_sap_starting_type, Some(2));
		assert_eq!(video.max_obj_sap_starting_type, Some(2));
		assert_eq!(video.jitter, Some(std::time::Duration::from_millis(100)));
		assert_eq!(video.delay, Some(std::time::Duration::from_millis(200)));

		let audio = &msf.tracks[1];
		assert_eq!(audio.role, Some(moq_msf::Role::Audio));
		assert_eq!(audio.max_grp_sap_starting_type, Some(1));
		assert_eq!(audio.max_obj_sap_starting_type, Some(1));
		assert_eq!(audio.jitter, Some(std::time::Duration::from_millis(40)));
		assert_eq!(audio.delay, Some(std::time::Duration::from_millis(80)));
	}

	#[test]
	fn convert_sap_h265() {
		use hang::catalog::H265;

		let mut video_config = VideoConfig::new(H265 {
			in_band: false,
			profile_space: 0,
			profile_idc: 1,
			profile_compatibility_flags: [0, 0, 0, 0],
			tier_flag: false,
			level_idc: 93,
			constraint_flags: [0, 0, 0, 0, 0, 0],
		});
		video_config.coded_width = Some(1920);
		video_config.coded_height = Some(1080);
		video_config.framerate = Some(60.0);
		video_config.container = Container::Legacy;

		let mut video_renditions = BTreeMap::new();
		video_renditions.insert("video0".to_string(), video_config);

		let mut catalog = hang::Catalog::default();
		catalog.video.renditions = video_renditions;

		let msf = to_msf_media::<()>(&catalog);
		let video = &msf.tracks[0];
		// H.265 SAP behavior isn't validated end-to-end yet, so we omit the
		// SAP fields rather than advertise something we haven't verified.
		assert_eq!(video.max_grp_sap_starting_type, None);
		assert_eq!(video.max_obj_sap_starting_type, None);
		assert_eq!(video.jitter, None);
	}

	#[test]
	fn convert_sap_unknown_codec() {
		use hang::catalog::VideoCodec;

		let mut video_config = VideoConfig::new(VideoCodec::Unknown("future-codec.01".to_string()));
		video_config.container = Container::Legacy;

		let mut video_renditions = BTreeMap::new();
		video_renditions.insert("video0".to_string(), video_config);

		let mut catalog = hang::Catalog::default();
		catalog.video.renditions = video_renditions;

		let msf = to_msf_media::<()>(&catalog);
		let video = &msf.tracks[0];
		assert_eq!(video.max_grp_sap_starting_type, None);
		assert_eq!(video.max_obj_sap_starting_type, None);
	}
}