vivid_protocol 1.5.0

Wire types, state models, and deterministic codecs for Vivid Protocol 1.5
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
//! Immutable track configuration, channel generations, and readiness state.

use crate::{
    HARD_MAX_RECORD_BODY,
    cbor::Value,
    media,
    messages::{
        LaneClass, MessageError, PayloadMap, StrictMap, TrackKind, invalid_value, require_nonzero,
        validate_header_object,
    },
    resource::{ChannelFlow, ResourceError},
    revision::{ChannelGeneration, TrackRevision},
};
use sha2::{Digest, Sha256};

pub const MILESTONE_CHANNEL_ACCEPTED: u64 = 1 << 0;
pub const MILESTONE_FIRST_MEDIA: u64 = 1 << 1;
pub const MILESTONE_DECODER_INITIALIZED: u64 = 1 << 2;
pub const MILESTONE_RANDOM_ACCESS: u64 = 1 << 3;
pub const MILESTONE_OUTPUT_READY: u64 = 1 << 4;
pub const MILESTONE_PRESENTED: u64 = 1 << 5;
pub const MILESTONE_CLOCK_STARTED: u64 = 1 << 6;
pub const MILESTONE_EOS_ACCEPTED: u64 = 1 << 7;
pub const MILESTONE_BUFFERED_ENDED: u64 = 1 << 8;
pub const MILESTONE_CHANNEL_DETACHED: u64 = 1 << 9;
pub const MILESTONE_TRACK_LOST: u64 = 1 << 10;
pub const MILESTONE_KNOWN_MASK: u64 = (1 << 11) - 1;

/// Unsigned Q32.32 linear amplitude used by `audio-gain-v1`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct AudioGain(u64);

impl AudioGain {
    pub const SILENT: Self = Self(0);
    pub const UNITY: Self = Self(1_u64 << 32);
    pub const MAX: Self = Self(2_u64 << 32);

    pub const fn new(raw: u64) -> Option<Self> {
        if raw <= Self::MAX.0 {
            Some(Self(raw))
        } else {
            None
        }
    }

    pub const fn raw(self) -> u64 {
        self.0
    }

    pub fn from_percent(percent: u32) -> Option<Self> {
        if percent > 200 {
            return None;
        }
        Some(Self((u64::from(percent) << 32) / 100))
    }

    pub fn as_f32(self) -> f32 {
        self.0 as f32 / (1_u64 << 32) as f32
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct AcceptedOpen {
    generation: ChannelGeneration,
    nonce: [u8; 16],
    open_hash: [u8; 32],
    acceptance: PayloadMap,
    transport_live: bool,
    media_admitted: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ChannelOpenDecision {
    Fresh(PayloadMap),
    ExactReplay(PayloadMap),
    Busy,
    DifferentBytes,
    StaleGeneration,
}

#[derive(Debug, Default)]
pub struct ChannelOpenState {
    accepted: Option<AcceptedOpen>,
}

impl ChannelOpenState {
    pub fn open(
        &mut self,
        current_generation: ChannelGeneration,
        generation: ChannelGeneration,
        nonce: [u8; 16],
        complete_open_bytes: &[u8],
        acceptance: PayloadMap,
    ) -> ChannelOpenDecision {
        if generation != current_generation {
            return ChannelOpenDecision::StaleGeneration;
        }
        let open_hash: [u8; 32] = Sha256::digest(complete_open_bytes).into();
        if let Some(accepted) = &mut self.accepted {
            if accepted.generation != generation {
                self.accepted = None;
            } else if accepted.nonce != nonce || accepted.open_hash != open_hash {
                return ChannelOpenDecision::DifferentBytes;
            } else if accepted.transport_live {
                return ChannelOpenDecision::Busy;
            } else if accepted.media_admitted {
                return ChannelOpenDecision::StaleGeneration;
            } else {
                accepted.transport_live = true;
                return ChannelOpenDecision::ExactReplay(accepted.acceptance.clone());
            }
        }
        self.accepted = Some(AcceptedOpen {
            generation,
            nonce,
            open_hash,
            acceptance: acceptance.clone(),
            transport_live: true,
            media_admitted: false,
        });
        ChannelOpenDecision::Fresh(acceptance)
    }

    pub fn admit_media(&mut self, generation: ChannelGeneration) -> Result<(), MessageError> {
        let accepted = self
            .accepted
            .as_mut()
            .ok_or_else(|| invalid_value("media record", 0, "has no accepted channel"))?;
        if accepted.generation != generation || !accepted.transport_live {
            return Err(invalid_value(
                "media record",
                0,
                "does not use the accepted live channel",
            ));
        }
        accepted.media_admitted = true;
        Ok(())
    }

    pub fn transport_lost(&mut self, generation: ChannelGeneration) {
        if let Some(accepted) = &mut self.accepted {
            if accepted.generation == generation {
                accepted.transport_live = false;
            }
        }
    }

    pub fn advance(&mut self) {
        self.accepted = None;
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u64)]
pub enum TrackMode {
    Live = 1,
    Timed = 2,
}

impl TryFrom<u64> for TrackMode {
    type Error = MessageError;

    fn try_from(value: u64) -> Result<Self, Self::Error> {
        match value {
            1 => Ok(Self::Live),
            2 => Ok(Self::Timed),
            _ => Err(invalid_value(
                "track configuration",
                5,
                "has an unknown mode",
            )),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VideoConfiguration {
    pub codec: String,
    pub packetization: String,
    pub extradata: Vec<u8>,
    pub coded_width: u32,
    pub coded_height: u32,
    pub profile: i32,
    pub level: i32,
    pub maximum_reorder_depth: u8,
    pub color_primaries: u64,
    pub transfer: u64,
    pub matrix: u64,
    pub signal_range: u64,
    pub aspect_numerator: u64,
    pub aspect_denominator: u64,
    pub maximum_access_unit_bytes: u32,
    pub codec_string: Option<String>,
    pub decoder_configuration: Option<Vec<u8>>,
}

impl VideoConfiguration {
    fn validate(&self) -> Result<(), MessageError> {
        if !media::is_portable_packetization(&self.codec, &self.packetization) {
            return Err(invalid_value("video configuration", 1, "is not canonical"));
        }
        if self.extradata.len() > 65_536 {
            return Err(invalid_value(
                "video configuration",
                2,
                "extradata exceeds 65536 bytes",
            ));
        }
        dimension("video configuration", 3, self.coded_width)?;
        dimension("video configuration", 4, self.coded_height)?;
        if self.maximum_reorder_depth > 64 {
            return Err(invalid_value(
                "video configuration",
                8,
                "reorder depth exceeds 64",
            ));
        }
        if !(1..=4).contains(&self.color_primaries)
            || !(1..=2).contains(&self.transfer)
            || self.matrix > 3
            || !(1..=2).contains(&self.signal_range)
        {
            return Err(invalid_value(
                "video configuration",
                10,
                "has unsupported colorimetry",
            ));
        }
        require_nonzero("video configuration", 14, self.aspect_numerator)?;
        require_nonzero("video configuration", 15, self.aspect_denominator)?;
        media::video_body_len(self.maximum_access_unit_bytes)
            .map_err(|_| invalid_value("video configuration", 16, "exceeds the body ceiling"))?;
        if self.codec_string.as_ref().is_some_and(|value| {
            value.len() > 64 || !value.bytes().all(|byte| (0x20..=0x7e).contains(&byte))
        }) {
            return Err(invalid_value(
                "video configuration",
                17,
                "is not at most 64 printable ASCII bytes",
            ));
        }
        if self
            .decoder_configuration
            .as_ref()
            .is_some_and(|value| value.len() > 4096)
        {
            return Err(invalid_value(
                "video configuration",
                18,
                "exceeds 4096 bytes",
            ));
        }
        Ok(())
    }

    fn to_value(&self) -> Result<Value, MessageError> {
        self.validate()?;
        let mut fields = vec![
            (0, Value::Text(self.codec.clone())),
            (1, Value::Text(self.packetization.clone())),
            (2, Value::Bytes(self.extradata.clone())),
            (3, Value::Unsigned(u64::from(self.coded_width))),
            (4, Value::Unsigned(u64::from(self.coded_height))),
            (5, Value::from_i64(i64::from(self.profile))),
            (6, Value::from_i64(i64::from(self.level))),
            (7, Value::Unsigned(0)),
            (8, Value::Unsigned(u64::from(self.maximum_reorder_depth))),
            (9, Value::Text("source-timebase-us".into())),
            (10, Value::Unsigned(self.color_primaries)),
            (11, Value::Unsigned(self.transfer)),
            (12, Value::Unsigned(self.matrix)),
            (13, Value::Unsigned(self.signal_range)),
            (14, Value::Unsigned(self.aspect_numerator)),
            (15, Value::Unsigned(self.aspect_denominator)),
            (
                16,
                Value::Unsigned(u64::from(self.maximum_access_unit_bytes)),
            ),
        ];
        if let Some(codec_string) = &self.codec_string {
            fields.push((17, Value::Text(codec_string.clone())));
        }
        if let Some(configuration) = &self.decoder_configuration {
            fields.push((18, Value::Bytes(configuration.clone())));
        }
        Ok(Value::Map(fields))
    }

    fn from_value(value: &Value) -> Result<Self, MessageError> {
        let map = StrictMap::new(
            "video configuration",
            value,
            &[
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
            ],
        )?;
        if map.required_u64(7)? != 0 || map.required_text(9)? != "source-timebase-us" {
            return Err(invalid_value(
                "video configuration",
                7,
                "uses an unsupported alpha mode or timeline",
            ));
        }
        let configuration = Self {
            codec: map.required_text(0)?.to_owned(),
            packetization: map.required_text(1)?.to_owned(),
            extradata: map.required_bytes(2)?.to_vec(),
            coded_width: map.required_u32(3)?,
            coded_height: map.required_u32(4)?,
            profile: required_i32(&map, 5)?,
            level: required_i32(&map, 6)?,
            maximum_reorder_depth: u8::try_from(map.required_u64(8)?)
                .map_err(|_| invalid_value("video configuration", 8, "does not fit in u8"))?,
            color_primaries: map.required_u64(10)?,
            transfer: map.required_u64(11)?,
            matrix: map.required_u64(12)?,
            signal_range: map.required_u64(13)?,
            aspect_numerator: map.required_u64(14)?,
            aspect_denominator: map.required_u64(15)?,
            maximum_access_unit_bytes: map.required_u32(16)?,
            codec_string: map
                .optional(17)
                .map(|value| {
                    value
                        .as_text()
                        .map(ToOwned::to_owned)
                        .ok_or_else(|| invalid_value("video configuration", 17, "is not text"))
                })
                .transpose()?,
            decoder_configuration: map.optional_bytes(18)?.map(ToOwned::to_owned),
        };
        configuration.validate()?;
        Ok(configuration)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AudioConfiguration {
    pub codec: String,
    pub packetization: String,
    pub extradata: Vec<u8>,
    pub sample_rate: u32,
    pub channels: u8,
    pub channel_mask: u64,
    pub maximum_access_unit_bytes: u32,
    pub codec_string: Option<String>,
}

impl AudioConfiguration {
    fn validate(&self) -> Result<(), MessageError> {
        media::validate_audio_initialization(
            &self.codec,
            &self.packetization,
            &self.extradata,
            self.sample_rate,
            u16::from(self.channels),
        )
        .map_err(|_| invalid_value("audio configuration", 1, "is not canonical"))?;
        if self.extradata.len() > 65_536
            || !(8_000..=192_000).contains(&self.sample_rate)
            || !(1..=8).contains(&self.channels)
            || self.maximum_access_unit_bytes == 0
            || self.maximum_access_unit_bytes > 1_048_576
        {
            return Err(invalid_value(
                "audio configuration",
                2,
                "has an out-of-range field",
            ));
        }
        if self.channel_mask != 0 && self.channel_mask.count_ones() != u32::from(self.channels) {
            return Err(invalid_value(
                "audio configuration",
                5,
                "does not contain the declared channel count",
            ));
        }
        if self
            .codec_string
            .as_ref()
            .is_some_and(|value| value.len() > 64)
        {
            return Err(invalid_value(
                "audio configuration",
                8,
                "codec string exceeds 64 bytes",
            ));
        }
        Ok(())
    }

    fn to_value(&self) -> Result<Value, MessageError> {
        self.validate()?;
        let mut fields = vec![
            (0, Value::Text(self.codec.clone())),
            (1, Value::Text(self.packetization.clone())),
            (2, Value::Bytes(self.extradata.clone())),
            (3, Value::Unsigned(u64::from(self.sample_rate))),
            (4, Value::Unsigned(u64::from(self.channels))),
            (5, Value::Unsigned(self.channel_mask)),
            (
                6,
                Value::Unsigned(u64::from(self.maximum_access_unit_bytes)),
            ),
            (7, Value::Text("source-timebase-us".into())),
        ];
        if let Some(codec_string) = &self.codec_string {
            fields.push((8, Value::Text(codec_string.clone())));
        }
        Ok(Value::Map(fields))
    }

    fn from_value(value: &Value) -> Result<Self, MessageError> {
        let map = StrictMap::new("audio configuration", value, &[0, 1, 2, 3, 4, 5, 6, 7, 8])?;
        if map.required_text(7)? != "source-timebase-us" {
            return Err(invalid_value(
                "audio configuration",
                7,
                "has an unsupported timeline",
            ));
        }
        let configuration = Self {
            codec: map.required_text(0)?.to_owned(),
            packetization: map.required_text(1)?.to_owned(),
            extradata: map.required_bytes(2)?.to_vec(),
            sample_rate: map.required_u32(3)?,
            channels: u8::try_from(map.required_u64(4)?)
                .map_err(|_| invalid_value("audio configuration", 4, "does not fit in u8"))?,
            channel_mask: map.required_u64(5)?,
            maximum_access_unit_bytes: map.required_u32(6)?,
            codec_string: map
                .optional(8)
                .map(|value| {
                    value
                        .as_text()
                        .map(ToOwned::to_owned)
                        .ok_or_else(|| invalid_value("audio configuration", 8, "is not text"))
                })
                .transpose()?,
        };
        configuration.validate()?;
        Ok(configuration)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RasterConfiguration {
    pub width: u32,
    pub height: u32,
    pub alpha_mode: u64,
    pub delta_enabled: bool,
    pub maximum_delta_operations: u8,
    pub zstd_enabled: bool,
}

impl RasterConfiguration {
    fn validate(&self) -> Result<(), MessageError> {
        dimension("raster configuration", 0, self.width)?;
        dimension("raster configuration", 1, self.height)?;
        if !(1..=2).contains(&self.alpha_mode)
            || !(1..=16).contains(&self.maximum_delta_operations)
            || (!self.delta_enabled && self.maximum_delta_operations != 1)
        {
            return Err(invalid_value(
                "raster configuration",
                3,
                "has invalid alpha or delta settings",
            ));
        }
        media::rgba8_raw_frame_body_len(self.width, self.height).map_err(|_| {
            invalid_value(
                "raster configuration",
                0,
                "raw full frame exceeds the body ceiling",
            )
        })?;
        Ok(())
    }

    fn to_value(&self) -> Result<Value, MessageError> {
        self.validate()?;
        Ok(Value::Map(vec![
            (0, Value::Unsigned(u64::from(self.width))),
            (1, Value::Unsigned(u64::from(self.height))),
            (2, Value::Unsigned(1)),
            (3, Value::Unsigned(self.alpha_mode)),
            (4, Value::Unsigned(u64::from(self.delta_enabled))),
            (5, Value::Unsigned(u64::from(self.maximum_delta_operations))),
            (6, Value::Unsigned(u64::from(self.zstd_enabled))),
            (7, Value::Unsigned(1)),
        ]))
    }

    fn from_value(value: &Value) -> Result<Self, MessageError> {
        let map = StrictMap::new("raster configuration", value, &[0, 1, 2, 3, 4, 5, 6, 7])?;
        if map.required_u64(2)? != 1 || map.required_u64(7)? != 1 {
            return Err(invalid_value(
                "raster configuration",
                2,
                "requires RGBA8 and sRGB",
            ));
        }
        let update_mode = map.required_u64(4)?;
        let compression = map.required_u64(6)?;
        if update_mode > 1 || compression > 1 {
            return Err(invalid_value(
                "raster configuration",
                4,
                "has an unknown update or compression mode",
            ));
        }
        let configuration = Self {
            width: map.required_u32(0)?,
            height: map.required_u32(1)?,
            alpha_mode: map.required_u64(3)?,
            delta_enabled: update_mode == 1,
            maximum_delta_operations: u8::try_from(map.required_u64(5)?)
                .map_err(|_| invalid_value("raster configuration", 5, "does not fit in u8"))?,
            zstd_enabled: compression == 1,
        };
        configuration.validate()?;
        Ok(configuration)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ImageConfiguration {
    pub encoding: u64,
    pub width: u32,
    pub height: u32,
    pub encoded_length: u32,
    pub sha256: Option<[u8; 32]>,
    pub cache_lookup: bool,
}

impl ImageConfiguration {
    fn validate(&self) -> Result<(), MessageError> {
        if !(1..=2).contains(&self.encoding) {
            return Err(invalid_value(
                "image configuration",
                0,
                "has an unknown encoding",
            ));
        }
        dimension("image configuration", 1, self.width)?;
        dimension("image configuration", 2, self.height)?;
        if self.encoded_length == 0 || self.encoded_length > HARD_MAX_RECORD_BODY {
            return Err(invalid_value(
                "image configuration",
                3,
                "has an invalid encoded length",
            ));
        }
        if self.cache_lookup && self.sha256.is_none() {
            return Err(invalid_value(
                "image configuration",
                6,
                "requires a SHA-256 value",
            ));
        }
        Ok(())
    }

    fn to_value(&self) -> Result<Value, MessageError> {
        self.validate()?;
        let mut fields = vec![
            (0, Value::Unsigned(self.encoding)),
            (1, Value::Unsigned(u64::from(self.width))),
            (2, Value::Unsigned(u64::from(self.height))),
            (3, Value::Unsigned(u64::from(self.encoded_length))),
        ];
        if let Some(hash) = self.sha256 {
            fields.push((4, Value::Bytes(hash.to_vec())));
        }
        fields.push((5, Value::Unsigned(1)));
        fields.push((6, Value::Bool(self.cache_lookup)));
        Ok(Value::Map(fields))
    }

    fn from_value(value: &Value) -> Result<Self, MessageError> {
        let map = StrictMap::new("image configuration", value, &[0, 1, 2, 3, 4, 5, 6])?;
        if map.required_u64(5)? != 1 {
            return Err(invalid_value(
                "image configuration",
                5,
                "requires sRGB output",
            ));
        }
        let configuration = Self {
            encoding: map.required_u64(0)?,
            width: map.required_u32(1)?,
            height: map.required_u32(2)?,
            encoded_length: map.required_u32(3)?,
            sha256: map.optional_fixed_bytes(4)?,
            cache_lookup: map.required_bool(6)?,
        };
        configuration.validate()?;
        Ok(configuration)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum KindConfiguration {
    Video(VideoConfiguration),
    Audio(AudioConfiguration),
    Raster(RasterConfiguration),
    EncodedImage(ImageConfiguration),
}

impl KindConfiguration {
    pub const fn kind(&self) -> TrackKind {
        match self {
            Self::Video(_) => TrackKind::Video,
            Self::Audio(_) => TrackKind::Audio,
            Self::Raster(_) => TrackKind::Raster,
            Self::EncodedImage(_) => TrackKind::EncodedImage,
        }
    }

    fn to_value(&self) -> Result<Value, MessageError> {
        match self {
            Self::Video(value) => value.to_value(),
            Self::Audio(value) => value.to_value(),
            Self::Raster(value) => value.to_value(),
            Self::EncodedImage(value) => value.to_value(),
        }
    }

    fn from_value(kind: TrackKind, value: &Value) -> Result<Self, MessageError> {
        match kind {
            TrackKind::Video => Ok(Self::Video(VideoConfiguration::from_value(value)?)),
            TrackKind::Audio => Ok(Self::Audio(AudioConfiguration::from_value(value)?)),
            TrackKind::Raster => Ok(Self::Raster(RasterConfiguration::from_value(value)?)),
            TrackKind::EncodedImage => {
                Ok(Self::EncodedImage(ImageConfiguration::from_value(value)?))
            }
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TrackConfiguration {
    pub context_id: u64,
    pub surface_id: u64,
    pub track_id: u64,
    pub slot: u64,
    pub mode: TrackMode,
    pub lane: LaneClass,
    pub maximum_record_body: u32,
    pub maximum_rate_millihertz: u64,
    pub maximum_encoded_bits_per_second: u64,
    pub maximum_records_per_second: u64,
    pub maximum_inflight_body_bytes: u64,
    pub kind: KindConfiguration,
    pub target_latency_us: u64,
    pub maximum_latency_us: u64,
    pub retained_pixel_charge: u64,
}

impl TrackConfiguration {
    pub fn validate(&self, probe: bool) -> Result<(), MessageError> {
        require_nonzero("track configuration", 0, self.context_id)?;
        require_nonzero("track configuration", 1, self.surface_id)?;
        if probe {
            if self.track_id != 0 {
                return Err(invalid_value(
                    "track configuration",
                    2,
                    "must be zero for a probe",
                ));
            }
        } else {
            require_nonzero("track configuration", 2, self.track_id)?;
            for (key, value) in [
                (7, u64::from(self.maximum_record_body)),
                (8, self.maximum_rate_millihertz),
                (9, self.maximum_encoded_bits_per_second),
                (10, self.maximum_records_per_second),
                (11, self.maximum_inflight_body_bytes),
            ] {
                require_nonzero("track configuration", key, value)?;
            }
        }
        if self.maximum_record_body > HARD_MAX_RECORD_BODY {
            return Err(invalid_value(
                "track configuration",
                7,
                "exceeds the hard body limit",
            ));
        }
        if !matches!(self.lane, LaneClass::Realtime | LaneClass::Bulk) {
            return Err(invalid_value(
                "track configuration",
                6,
                "must use realtime or bulk",
            ));
        }
        if self.target_latency_us > self.maximum_latency_us {
            return Err(invalid_value(
                "track configuration",
                13,
                "exceeds maximum latency",
            ));
        }
        self.kind.to_value()?;
        let required_body = match &self.kind {
            KindConfiguration::Video(configuration) => {
                media::video_body_len(configuration.maximum_access_unit_bytes)
            }
            KindConfiguration::Audio(configuration) => {
                media::audio_body_len(configuration.maximum_access_unit_bytes)
            }
            KindConfiguration::Raster(configuration) => {
                media::rgba8_raw_frame_body_len(configuration.width, configuration.height)
            }
            KindConfiguration::EncodedImage(configuration) => Ok(configuration.encoded_length),
        }
        .map_err(|_| {
            invalid_value(
                "track configuration",
                7,
                "cannot carry its maximum legal media body",
            )
        })?;
        if self.maximum_record_body < required_body
            || self.maximum_inflight_body_bytes < u64::from(self.maximum_record_body)
        {
            return Err(invalid_value(
                "track configuration",
                11,
                "cannot admit one maximum legal media record",
            ));
        }
        Ok(())
    }

    pub fn payload(&self, probe: bool) -> Result<PayloadMap, MessageError> {
        self.validate(probe)?;
        Ok(vec![
            (0, Value::Unsigned(self.context_id)),
            (1, Value::Unsigned(self.surface_id)),
            (2, Value::Unsigned(self.track_id)),
            (3, Value::Unsigned(self.kind.kind() as u64)),
            (4, Value::Unsigned(self.slot)),
            (5, Value::Unsigned(self.mode as u64)),
            (6, Value::Unsigned(self.lane as u64)),
            (7, Value::Unsigned(u64::from(self.maximum_record_body))),
            (8, Value::Unsigned(self.maximum_rate_millihertz)),
            (9, Value::Unsigned(self.maximum_encoded_bits_per_second)),
            (10, Value::Unsigned(self.maximum_records_per_second)),
            (11, Value::Unsigned(self.maximum_inflight_body_bytes)),
            (12, self.kind.to_value()?),
            (13, Value::Unsigned(self.target_latency_us)),
            (14, Value::Unsigned(self.maximum_latency_us)),
            (15, Value::Unsigned(self.retained_pixel_charge)),
        ])
    }

    pub fn decode(
        header_object_id: u64,
        payload: &Value,
        probe: bool,
    ) -> Result<Self, MessageError> {
        let map = StrictMap::new(
            "track configuration",
            payload,
            &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
        )?;
        let track_id = map.required_u64(2)?;
        validate_header_object(header_object_id, track_id)?;
        let kind = TrackKind::try_from(map.required_u64(3)?)?;
        let configuration = Self {
            context_id: map.required_u64(0)?,
            surface_id: map.required_u64(1)?,
            track_id,
            slot: map.required_u64(4)?,
            mode: TrackMode::try_from(map.required_u64(5)?)?,
            lane: LaneClass::try_from(map.required_u64(6)?)?,
            maximum_record_body: map.required_u32(7)?,
            maximum_rate_millihertz: map.required_u64(8)?,
            maximum_encoded_bits_per_second: map.required_u64(9)?,
            maximum_records_per_second: map.required_u64(10)?,
            maximum_inflight_body_bytes: map.required_u64(11)?,
            kind: KindConfiguration::from_value(kind, map.required(12)?)?,
            target_latency_us: map.required_u64(13)?,
            maximum_latency_us: map.required_u64(14)?,
            retained_pixel_charge: map.required_u64(15)?,
        };
        configuration.validate(probe)?;
        Ok(configuration)
    }
}

#[derive(Debug, Clone)]
pub struct TrackState {
    pub revision: TrackRevision,
    pub channel_generation: ChannelGeneration,
    pub milestones: u64,
    pub media_epoch: u32,
    pub last_media_id: u64,
    pub flow: ChannelFlow,
    pub lost: bool,
}

impl TrackState {
    pub fn new() -> Self {
        Self {
            revision: TrackRevision::ONE,
            channel_generation: ChannelGeneration::ONE,
            milestones: 0,
            media_epoch: 0,
            last_media_id: 0,
            flow: ChannelFlow::default(),
            lost: false,
        }
    }

    pub fn accept_channel(
        &mut self,
        generation: ChannelGeneration,
        maximum_body_bytes: u64,
        maximum_records: u64,
        maximum_record_body: u32,
    ) -> Result<(), MessageError> {
        if self.lost || generation != self.channel_generation {
            return Err(invalid_value(
                "CHANNEL_ACCEPTED",
                3,
                "does not match the live channel generation",
            ));
        }
        if maximum_body_bytes < u64::from(maximum_record_body) || maximum_records == 0 {
            return Err(invalid_value(
                "CHANNEL_ACCEPTED",
                4,
                "does not admit one maximum legal record",
            ));
        }
        self.flow = ChannelFlow::new(maximum_body_bytes, maximum_records);
        self.milestones = MILESTONE_CHANNEL_ACCEPTED;
        self.advance_revision("CHANNEL_ACCEPTED")?;
        Ok(())
    }

    pub fn admit_media(
        &mut self,
        generation: ChannelGeneration,
        body_length: u32,
        epoch: u32,
        media_id: u64,
        random_access: bool,
    ) -> Result<(), MessageError> {
        if self.lost || generation != self.channel_generation {
            return Err(invalid_value(
                "media record",
                0,
                "uses a stale channel generation",
            ));
        }
        self.flow.admit(body_length).map_err(|error| match error {
            ResourceError::FlowControl => {
                invalid_value("media record", 0, "exceeds absolute flow allowance")
            }
            _ => invalid_value("media record", 0, "overflows accounting"),
        })?;
        if media_id == 0 || media_id <= self.last_media_id || epoch < self.media_epoch {
            return Err(invalid_value(
                "media record",
                0,
                "has a stale epoch or non-increasing media ID",
            ));
        }
        if (self.last_media_id == 0 || epoch > self.media_epoch) && !random_access {
            return Err(invalid_value(
                "media record",
                0,
                "must begin the epoch with a recovery unit",
            ));
        }
        self.media_epoch = epoch;
        self.last_media_id = media_id;
        self.milestones |= MILESTONE_FIRST_MEDIA;
        if random_access {
            self.milestones |= MILESTONE_RANDOM_ACCESS;
        }
        Ok(())
    }

    pub fn advance_channel(
        &mut self,
        expected: ChannelGeneration,
        next: ChannelGeneration,
    ) -> Result<(), MessageError> {
        if expected != self.channel_generation
            || next.get() != expected.get().checked_add(1).unwrap_or(0)
        {
            return Err(invalid_value(
                "ADVANCE_CHANNEL",
                4,
                "is not exactly the current generation plus one",
            ));
        }
        self.channel_generation = next;
        self.milestones = 0;
        self.flow = ChannelFlow::default();
        self.advance_revision("ADVANCE_CHANNEL")
    }

    pub fn detach(&mut self) -> Result<(), MessageError> {
        self.milestones |= MILESTONE_CHANNEL_DETACHED;
        self.advance_revision("channel detach")
    }

    pub fn lose(&mut self) -> Result<(), MessageError> {
        self.lost = true;
        self.milestones |= MILESTONE_TRACK_LOST;
        self.advance_revision("TRACK_LOST")
    }

    fn advance_revision(&mut self, schema: &'static str) -> Result<(), MessageError> {
        self.revision = self
            .revision
            .advance()
            .map_err(|_| invalid_value(schema, 0, "exhausted the track revision"))?;
        Ok(())
    }
}

impl Default for TrackState {
    fn default() -> Self {
        Self::new()
    }
}

fn dimension(schema: &'static str, key: u64, value: u32) -> Result<(), MessageError> {
    if !(1..=8192).contains(&value) {
        Err(invalid_value(schema, key, "is outside 1..=8192"))
    } else {
        Ok(())
    }
}

fn required_i32(map: &StrictMap<'_>, key: u64) -> Result<i32, MessageError> {
    let value = map
        .required(key)?
        .as_i64()
        .ok_or_else(|| invalid_value("track configuration", key, "is not an integer"))?;
    i32::try_from(value)
        .map_err(|_| invalid_value("track configuration", key, "does not fit in i32"))
}

trait SignedValue {
    fn from_i64(value: i64) -> Self;
}

impl SignedValue for Value {
    fn from_i64(value: i64) -> Self {
        if value >= 0 {
            Self::Unsigned(value as u64)
        } else {
            Self::Negative(value)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn audio_gain_covers_the_kitim_volume_range() {
        assert_eq!(AudioGain::from_percent(0), Some(AudioGain::SILENT));
        assert_eq!(AudioGain::from_percent(100), Some(AudioGain::UNITY));
        assert_eq!(AudioGain::from_percent(200), Some(AudioGain::MAX));
        assert_eq!(AudioGain::from_percent(201), None);
        assert_eq!(AudioGain::new(AudioGain::MAX.raw() + 1), None);
    }

    #[test]
    fn channel_generation_resets_generation_local_state() {
        let mut state = TrackState::new();
        state
            .accept_channel(ChannelGeneration::ONE, 100, 4, 100)
            .unwrap();
        state
            .admit_media(ChannelGeneration::ONE, 20, 1, 1, true)
            .unwrap();
        state
            .advance_channel(ChannelGeneration::ONE, ChannelGeneration::new(2))
            .unwrap();
        assert_eq!(state.milestones, 0);
        assert_eq!(state.flow.sent_body_bytes, 0);
        assert_eq!(state.last_media_id, 1);
    }

    #[test]
    fn lower_flow_update_is_harmless() {
        let mut state = TrackState::new();
        state
            .accept_channel(ChannelGeneration::ONE, 100, 4, 100)
            .unwrap();
        state.flow.raise_maxima(50, 2);
        assert_eq!(state.flow.maximum_body_bytes, 100);
        assert_eq!(state.flow.maximum_media_records, 4);
    }

    #[test]
    fn uncertain_channel_open_replays_only_before_media() {
        let mut open = ChannelOpenState::default();
        let acceptance = vec![(0, Value::Unsigned(1))];
        assert!(matches!(
            open.open(
                ChannelGeneration::ONE,
                ChannelGeneration::ONE,
                [1; 16],
                b"open",
                acceptance.clone()
            ),
            ChannelOpenDecision::Fresh(_)
        ));
        assert_eq!(
            open.open(
                ChannelGeneration::ONE,
                ChannelGeneration::ONE,
                [1; 16],
                b"open",
                acceptance.clone()
            ),
            ChannelOpenDecision::Busy
        );
        open.transport_lost(ChannelGeneration::ONE);
        assert_eq!(
            open.open(
                ChannelGeneration::ONE,
                ChannelGeneration::ONE,
                [1; 16],
                b"open",
                acceptance
            ),
            ChannelOpenDecision::ExactReplay(vec![(0, Value::Unsigned(1))])
        );
        open.admit_media(ChannelGeneration::ONE).unwrap();
        open.transport_lost(ChannelGeneration::ONE);
        assert_eq!(
            open.open(
                ChannelGeneration::ONE,
                ChannelGeneration::ONE,
                [1; 16],
                b"open",
                vec![]
            ),
            ChannelOpenDecision::StaleGeneration
        );
    }

    #[test]
    fn strict_raster_track_configuration_round_trips() {
        let configuration = TrackConfiguration {
            context_id: 1,
            surface_id: 2,
            track_id: 3,
            slot: 0,
            mode: TrackMode::Live,
            lane: LaneClass::Bulk,
            maximum_record_body: 72 + 64 * 64 * 4,
            maximum_rate_millihertz: 60_000,
            maximum_encoded_bits_per_second: 100_000_000,
            maximum_records_per_second: 60,
            maximum_inflight_body_bytes: 1_000_000,
            kind: KindConfiguration::Raster(RasterConfiguration {
                width: 64,
                height: 64,
                alpha_mode: 1,
                delta_enabled: true,
                maximum_delta_operations: 16,
                zstd_enabled: true,
            }),
            target_latency_us: 50_000,
            maximum_latency_us: 250_000,
            retained_pixel_charge: 64 * 64,
        };
        let payload = Value::Map(configuration.payload(false).unwrap());
        assert_eq!(
            TrackConfiguration::decode(3, &payload, false).unwrap(),
            configuration
        );
    }

    #[test]
    fn canonical_audio_track_configuration_round_trips() {
        let mut opus_head = b"OpusHead".to_vec();
        opus_head.extend_from_slice(&[1, 2, 0, 0, 0x80, 0xbb, 0, 0, 0, 0, 0]);
        let maximum_record_body = media::audio_body_len(4_096).unwrap();
        let configuration = TrackConfiguration {
            context_id: 1,
            surface_id: 2,
            track_id: 3,
            slot: 2,
            mode: TrackMode::Timed,
            lane: LaneClass::Realtime,
            maximum_record_body,
            maximum_rate_millihertz: 50_000,
            maximum_encoded_bits_per_second: 512_000,
            maximum_records_per_second: 50,
            maximum_inflight_body_bytes: u64::from(maximum_record_body) * 4,
            kind: KindConfiguration::Audio(AudioConfiguration {
                codec: "opus".into(),
                packetization: media::AUDIO_PACKETIZATION_OPUS.into(),
                extradata: opus_head,
                sample_rate: 48_000,
                channels: 2,
                channel_mask: 3,
                maximum_access_unit_bytes: 4_096,
                codec_string: Some("opus".into()),
            }),
            target_latency_us: 0,
            maximum_latency_us: 2_000_000,
            retained_pixel_charge: 0,
        };
        let payload = Value::Map(configuration.payload(false).unwrap());
        assert_eq!(
            TrackConfiguration::decode(3, &payload, false).unwrap(),
            configuration
        );
    }
}