-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
78 lines (69 loc) · 2.04 KB
/
Copy pathexample_test.go
File metadata and controls
78 lines (69 loc) · 2.04 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
package msf_test
import (
"encoding/json"
"fmt"
"time"
"github.com/floatdrop/moq-go/pkg/moqt/msf"
)
// A publisher builds the initial catalog with BeginBroadcast, validates it,
// JSON-marshals it, and publishes it as the first object on the well-known
// "catalog" track (msf.CatalogTrackName).
func ExampleBeginBroadcast() {
live := true
cat := msf.BeginBroadcast([]msf.Track{{
Name: "video",
Namespace: "moq-example/msf",
Packaging: msf.PackagingLOC,
Role: msf.RoleVideo,
IsLive: &live,
Codec: "av01.0.08M.10.0.110.09",
Width: 1920,
Height: 1080,
Framerate: 30,
Bitrate: 1_500_000,
}}, time.Time{})
if err := cat.Validate(); err != nil { // §5.1 / §5.2 invariants
panic(err)
}
catalogJSON, _ := json.Marshal(cat)
_ = catalogJSON // publish as the first object on the "catalog" track
fmt.Printf("tracks=%d track0=%s\n", len(cat.Tracks), cat.Tracks[0].Name)
// Output: tracks=1 track0=video
}
// A subscriber unmarshals the catalog object payload back into an msf.Catalog
// and picks a track to subscribe to.
func Example_subscribeCatalog() {
var objectPayload []byte // the first object received on the "catalog" track
var cat msf.Catalog
if err := json.Unmarshal(objectPayload, &cat); err != nil {
return
}
if err := cat.Validate(); err != nil {
return
}
for _, tr := range cat.Tracks {
if tr.Packaging == msf.PackagingLOC && tr.Role == msf.RoleVideo {
// session.Subscribe to (tr.Namespace, tr.Name), then loc.Decode
// each object.
fmt.Println(tr.Name)
}
}
}
// MSF also covers delta catalogs: Apply replays a delta's deltaUpdate
// operations (add / remove / clone) in document order per §5.3.
func ExampleApply() {
live := true
base := msf.BeginBroadcast([]msf.Track{{
Name: "video",
Namespace: "moq-example/msf",
Packaging: msf.PackagingLOC,
Role: msf.RoleVideo,
IsLive: &live,
}}, time.Time{})
var delta msf.Catalog // a parsed delta catalog (sequenceNumber = base+1)
updated, err := msf.Apply(base, delta)
if err != nil {
return
}
_ = updated
}