-
Notifications
You must be signed in to change notification settings - Fork 8
/
local_track.go
180 lines (157 loc) · 4.27 KB
/
local_track.go
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
package moqtransport
import (
"context"
"errors"
"io"
"log/slog"
"sync"
"github.com/mengelbart/moqtransport/internal/wire"
)
type subscriberID int
func (id *subscriberID) next() subscriberID {
*id += 1
return *id
}
type addSubscriberOp struct {
subscriber ObjectWriter
resultCh chan subscriberID
}
type removeSubscriberOp struct {
subscriberID subscriberID
}
type ObjectForwardingPreference int
const (
ObjectForwardingPreferenceDatagram ObjectForwardingPreference = iota
ObjectForwardingPreferenceStream
ObjectForwardingPreferenceStreamGroup
ObjectForwardingPreferenceStreamTrack
)
func objectForwardingPreferenceFromMessageType(t wire.ObjectMessageType) ObjectForwardingPreference {
switch t {
case wire.ObjectDatagramMessageType:
return ObjectForwardingPreferenceDatagram
case wire.ObjectStreamMessageType:
return ObjectForwardingPreferenceStream
case wire.StreamHeaderTrackMessageType:
return ObjectForwardingPreferenceStreamTrack
case wire.StreamHeaderGroupMessageType:
return ObjectForwardingPreferenceStreamGroup
}
panic("invalid object message type")
}
type Object struct {
GroupID uint64
ObjectID uint64
PublisherPriority uint8
ForwardingPreference ObjectForwardingPreference
Payload []byte
}
// An ObjectWriter allows sending objects using.
type ObjectWriter interface {
WriteObject(Object) error
io.Closer
}
// A LocalTrack is a local media source. Writing objects to the track will relay
// the objects to all subscribers.
// All methods are safe for concurrent use. Ordering of objects is only
// guaranteed within MultiObjectStreams. LocalTracks must be created with
// NewLocalTrack to ensure proper initialization.
type LocalTrack struct {
logger *slog.Logger
Namespace string
Name string
cancelCtx context.CancelFunc
cancelWG sync.WaitGroup
ctx context.Context
addSubscriberCh chan addSubscriberOp
removeSubscriberCh chan removeSubscriberOp
subscribers map[subscriberID]ObjectWriter
objectCh chan Object
subscriberCountCh chan int
nextID subscriberID
}
// NewLocalTrack creates a new LocalTrack
func NewLocalTrack(namespace, trackname string) *LocalTrack {
ctx, cancelCtx := context.WithCancel(context.Background())
lt := &LocalTrack{
logger: defaultLogger.WithGroup("MOQ_LOCAL_TRACK").With("namespace", namespace, "trackname", trackname),
Namespace: namespace,
Name: trackname,
cancelCtx: cancelCtx,
cancelWG: sync.WaitGroup{},
ctx: ctx,
addSubscriberCh: make(chan addSubscriberOp),
removeSubscriberCh: make(chan removeSubscriberOp),
subscribers: map[subscriberID]ObjectWriter{},
objectCh: make(chan Object),
subscriberCountCh: make(chan int),
nextID: 0,
}
lt.cancelWG.Add(1)
go lt.loop()
return lt
}
func (t *LocalTrack) loop() {
defer t.cancelWG.Done()
for {
select {
case <-t.ctx.Done():
for _, v := range t.subscribers {
v.Close()
}
return
case op := <-t.addSubscriberCh:
id := t.nextID.next()
t.subscribers[id] = op.subscriber
op.resultCh <- id
case rem := <-t.removeSubscriberCh:
delete(t.subscribers, rem.subscriberID)
case object := <-t.objectCh:
for _, v := range t.subscribers {
if err := v.WriteObject(object); err != nil {
// TODO: Notify / remove subscriber?
panic(err)
}
}
case t.subscriberCountCh <- len(t.subscribers):
}
}
}
func (t *LocalTrack) subscribe(
subscriber ObjectWriter,
) (subscriberID, error) {
if subscriber == nil {
return 0, errors.New("nil subscriber")
}
addOp := addSubscriberOp{
subscriber: subscriber,
resultCh: make(chan subscriberID),
}
// TODO: Should this have a timeout or similar?
t.addSubscriberCh <- addOp
res := <-addOp.resultCh
return res, nil
}
func (t *LocalTrack) unsubscribe(id subscriberID) {
removeOp := removeSubscriberOp{
subscriberID: id,
}
t.removeSubscriberCh <- removeOp
}
// WriteObject adds an object to the track
func (t *LocalTrack) WriteObject(ctx context.Context, o Object) error {
select {
case <-ctx.Done():
return ctx.Err()
case t.objectCh <- o:
}
return nil
}
func (t *LocalTrack) Close() error {
t.cancelCtx()
t.cancelWG.Wait()
return nil
}
func (t *LocalTrack) SubscriberCount() int {
return <-t.subscriberCountCh
}