-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.go
More file actions
188 lines (173 loc) · 5.24 KB
/
Copy pathvalidate.go
File metadata and controls
188 lines (173 loc) · 5.24 KB
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
package msf
import (
"errors"
"fmt"
"reflect"
)
// Validate enforces the rules from §5.1 and §5.2 that can be checked
// from a Catalog value alone. It does NOT validate cross-document
// state (e.g. whether a delta's parentName exists in some prior
// catalog) — that is [Apply]'s responsibility.
//
// Validate returns nil for valid catalogs and a descriptive error for
// the first violation it encounters.
func (c *Catalog) Validate() error {
if c.IsDelta() {
return c.validateDelta()
}
return c.validateIndependent()
}
func (c *Catalog) validateIndependent() error {
if c.Version != Version {
// Per §5.1.1: subscriber MUST NOT parse unknown versions. We
// still emit an error so producers learn about the mismatch.
if c.Version == "" {
return errors.New("moqt/msf: version is required (§5.1.1)")
}
return fmt.Errorf("moqt/msf: unsupported version %q (expected %q)", c.Version, Version)
}
// §5.1.2 — generatedAt SHOULD NOT be included when isLive is false
// for every track. We treat the SHOULD as advisory and skip it.
// Per-track cross-field constraints.
for i, tr := range c.Tracks {
if err := validateTrack(tr); err != nil {
return fmt.Errorf("moqt/msf: tracks[%d]: %w", i, err)
}
}
if err := validateTargetLatencyGroups(
c.Tracks,
"renderGroup",
func(t Track) *int { return t.RenderGroup },
); err != nil {
return err
}
if err := validateTargetLatencyGroups(c.Tracks, "altGroup", func(t Track) *int { return t.AltGroup }); err != nil {
return err
}
return nil
}
func (c *Catalog) validateDelta() error {
if c.Version != "" {
return errors.New("moqt/msf: delta update must not contain version (§5.3)")
}
if c.Tracks != nil {
return errors.New("moqt/msf: delta update must not contain tracks (§5.3)")
}
if len(c.DeltaUpdate) == 0 {
return errors.New("moqt/msf: delta update must contain at least one operation (§5.3)")
}
for i, op := range c.DeltaUpdate {
if err := validateDeltaOp(op); err != nil {
return fmt.Errorf("moqt/msf: deltaUpdate[%d]: %w", i, err)
}
}
return nil
}
func validateDeltaOp(op DeltaOp) error {
switch op.Op {
case DeltaOpAdd:
for i, tr := range op.Tracks {
if err := validateTrack(tr); err != nil {
return fmt.Errorf("tracks[%d]: %w", i, err)
}
}
case DeltaOpRemove:
for i, tr := range op.Tracks {
if tr.Name == "" {
return fmt.Errorf("tracks[%d]: name required (§5.1.6)", i)
}
if !isOnlyNameAndNamespace(tr) {
return fmt.Errorf("tracks[%d]: only name and namespace allowed (§5.1.6)", i)
}
}
case DeltaOpClone:
for i, tr := range op.Tracks {
if tr.ParentName == "" {
return fmt.Errorf("tracks[%d]: parentName required (§5.1.6)", i)
}
if tr.Name == "" {
return fmt.Errorf("tracks[%d]: name required (§5.3)", i)
}
}
default:
return fmt.Errorf("unknown op %q (§5.1.6)", op.Op)
}
return nil
}
func validateTrack(t Track) error {
if t.Name == "" {
return errors.New("name is required (§5.2.3)")
}
if t.Packaging == "" {
return errors.New("packaging is required (§5.2.4)")
}
switch t.Packaging {
case PackagingLOC, PackagingMediaTimeline, PackagingEventTimeline,
PackagingMoQLog, PackagingMoQMetrics:
default:
return fmt.Errorf("unknown packaging %q (§5.2.4)", t.Packaging)
}
if t.IsLive == nil {
return errors.New("isLive is required (§5.2.7)")
}
live := *t.IsLive
if t.Packaging == PackagingEventTimeline {
if t.EventType == "" {
return errors.New("eventType is required when packaging=eventtimeline (§5.2.5)")
}
} else if t.EventType != "" {
return errors.New("eventType MUST NOT be used unless packaging=eventtimeline (§5.2.5)")
}
// §5.2.8 / §5.2.9 — targetLatency and buffers are mutually
// exclusive within a single track.
if t.TargetLatency != nil && t.Buffers != nil {
return errors.New("targetLatency and buffers are mutually exclusive (§5.2.8, §5.2.9)")
}
if live && t.TrackDuration != 0 {
return errors.New("trackDuration MUST NOT be present when isLive is true (§5.2.35)")
}
return nil
}
// validateTargetLatencyGroups enforces §5.2.8's requirement that all
// tracks belonging to the same render/altGroup share the same
// targetLatency (treating nil as a distinct "absent" value).
func validateTargetLatencyGroups(tracks []Track, groupName string, accessor func(Track) *int) error {
groups := map[int]*uint32{}
groupsSeen := map[int]bool{}
for i, tr := range tracks {
gp := accessor(tr)
if gp == nil {
continue
}
g := *gp
if !groupsSeen[g] {
groups[g] = tr.TargetLatency
groupsSeen[g] = true
continue
}
if !targetLatencyEqual(groups[g], tr.TargetLatency) {
return fmt.Errorf(
"moqt/msf: tracks[%d] %s=%d targetLatency mismatch within group (§5.2.8)",
i, groupName, g)
}
}
return nil
}
func targetLatencyEqual(a, b *uint32) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
return *a == *b
}
// isOnlyNameAndNamespace reports whether tr has no fields set beyond
// Name and Namespace. Used by §5.1.6: remove-operation entries MUST
// hold only Name and may hold Namespace. Clearing those two fields and
// comparing against the zero Track keeps this robust as Track grows.
func isOnlyNameAndNamespace(tr Track) bool {
tr.Name = ""
tr.Namespace = ""
return reflect.DeepEqual(tr, Track{})
}