-
Notifications
You must be signed in to change notification settings - Fork 8
/
remote_track.go
83 lines (73 loc) · 1.74 KB
/
remote_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
package moqtransport
import (
"context"
"errors"
"io"
"log/slog"
"github.com/mengelbart/moqtransport/internal/wire"
)
type RemoteTrack struct {
logger *slog.Logger
responseCh chan subscribeIDer
session *Session
subscribeID uint64
buffer chan Object
closeCh chan struct{}
}
func newRemoteTrack(id uint64, s *Session) *RemoteTrack {
t := &RemoteTrack{
logger: defaultLogger.WithGroup("MOQ_REMOTE_TRACK"),
responseCh: make(chan subscribeIDer),
session: s,
subscribeID: id,
buffer: make(chan Object),
closeCh: make(chan struct{}),
}
return t
}
func (t *RemoteTrack) ReadObject(ctx context.Context) (Object, error) {
select {
case <-ctx.Done():
return Object{}, ctx.Err()
case obj, ok := <-t.buffer:
if !ok {
return Object{}, errors.New("track closed")
}
return obj, nil
}
}
func (t *RemoteTrack) Unsubscribe() {
t.session.unsubscribe(t.subscribeID)
}
func (t *RemoteTrack) close() {
close(t.closeCh)
}
func (t *RemoteTrack) push(o Object) {
t.logger.Info("push object", "object", o)
select {
case t.buffer <- o:
case <-t.closeCh:
}
}
func (t *RemoteTrack) readObjectStream(p *wire.ObjectStreamParser) {
t.logger.Info("reading object stream")
defer t.logger.Info("finished reading object stream")
for {
msg, err := p.Parse()
if err != nil {
if err == io.EOF {
return
}
t.logger.Info("stream canceled by peer", "error", err)
return
}
t.logger.Info("object stream got object", "msg", msg)
t.push(Object{
GroupID: msg.GroupID,
ObjectID: msg.ObjectID,
PublisherPriority: msg.PublisherPriority,
ForwardingPreference: objectForwardingPreferenceFromMessageType(msg.Type),
Payload: msg.ObjectPayload,
})
}
}