-
Notifications
You must be signed in to change notification settings - Fork 8
/
send_subscription.go
174 lines (160 loc) · 4.33 KB
/
send_subscription.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
package moqtransport
import (
"context"
"errors"
"log/slog"
"sync"
"github.com/mengelbart/moqtransport/internal/wire"
"github.com/quic-go/quic-go"
)
var errUnsubscribed = errors.New("peer unsubscribed")
type sendSubscription struct {
logger *slog.Logger
cancelCtx context.CancelFunc
cancelWG sync.WaitGroup
ctx context.Context
subscriptionIDinTrack subscriberID
subscribeID, trackAlias uint64
namespace, trackname string
conn Connection
objectCh chan Object
trackHeaderStream *trackHeaderStream
groupHeaderStreams map[uint64]*groupHeaderStream
}
func newSendSubscription(conn Connection, subscribeID, trackAlias uint64, namespace, trackname string) *sendSubscription {
ctx, cancelCtx := context.WithCancel(context.Background())
sub := &sendSubscription{
logger: defaultLogger.WithGroup("MOQ_SEND_SUBSCRIPTION").With(
"namespace", namespace,
"trackname", trackname,
),
cancelCtx: cancelCtx,
cancelWG: sync.WaitGroup{},
ctx: ctx,
subscriptionIDinTrack: -1,
subscribeID: subscribeID,
trackAlias: trackAlias,
namespace: namespace,
trackname: trackname,
conn: conn,
objectCh: make(chan Object, 1024),
trackHeaderStream: nil,
groupHeaderStreams: map[uint64]*groupHeaderStream{},
}
sub.cancelWG.Add(1)
go sub.loop()
return sub
}
func (s *sendSubscription) loop() {
defer s.cancelWG.Done()
for {
select {
case o := <-s.objectCh:
s.sendObject(o)
case <-s.ctx.Done():
return
}
}
}
func (s *sendSubscription) sendObject(o Object) {
s.logger.Info("sending object", "group-id", o.GroupID, "object-id", o.ObjectID)
switch o.ForwardingPreference {
case ObjectForwardingPreferenceDatagram:
if err := s.sendDatagram(o); err != nil {
panic(err)
}
case ObjectForwardingPreferenceStream:
if err := s.sendObjectStream(o); err != nil {
panic(err)
}
case ObjectForwardingPreferenceStreamGroup:
if err := s.sendGroupHeaderStream(o); err != nil {
panic(err)
}
case ObjectForwardingPreferenceStreamTrack:
if err := s.sendTrackHeaderStream(o); err != nil {
panic(err)
}
}
}
func (s *sendSubscription) WriteObject(o Object) error {
select {
case s.objectCh <- o:
case <-s.ctx.Done():
return errUnsubscribed
default:
s.logger.Warn("send subscription dropped object", "group-id", o.GroupID, "object-id", o.ObjectID)
}
return nil
}
func (s *sendSubscription) sendDatagram(o Object) error {
om := &wire.ObjectMessage{
Type: wire.ObjectDatagramMessageType,
SubscribeID: s.subscribeID,
TrackAlias: s.trackAlias,
GroupID: o.GroupID,
ObjectID: o.ObjectID,
PublisherPriority: o.PublisherPriority,
ObjectStatus: 0,
ObjectPayload: o.Payload,
}
buf := make([]byte, 0, 48+len(o.Payload))
buf = om.Append(buf)
err := s.conn.SendDatagram(buf)
if !errors.Is(err, &quic.DatagramTooLargeError{}) {
return err
}
return nil
}
func (s *sendSubscription) sendObjectStream(o Object) error {
stream, err := s.conn.OpenUniStream()
if err != nil {
return err
}
os, err := newObjectStream(stream, s.subscribeID, s.trackAlias, o.GroupID, o.ObjectID, o.PublisherPriority)
if err != nil {
return err
}
if _, err := os.Write(o.Payload); err != nil {
return err
}
return os.Close()
}
func (s *sendSubscription) sendTrackHeaderStream(o Object) error {
if s.trackHeaderStream == nil {
stream, err := s.conn.OpenUniStream()
if err != nil {
return err
}
ts, err := newTrackHeaderStream(stream, s.subscribeID, s.trackAlias, o.PublisherPriority)
if err != nil {
return err
}
s.trackHeaderStream = ts
}
_, err := s.trackHeaderStream.writeObject(o.GroupID, o.ObjectID, o.Payload)
return err
}
func (s *sendSubscription) sendGroupHeaderStream(o Object) error {
gs, ok := s.groupHeaderStreams[o.GroupID]
if !ok {
var stream SendStream
var err error
stream, err = s.conn.OpenUniStream()
if err != nil {
return err
}
gs, err = newGroupHeaderStream(stream, s.subscribeID, s.trackAlias, o.GroupID, o.PublisherPriority)
if err != nil {
return err
}
s.groupHeaderStreams[o.GroupID] = gs
}
_, err := gs.writeObject(o.ObjectID, o.Payload)
return err
}
func (s *sendSubscription) Close() error {
s.cancelCtx()
s.cancelWG.Wait()
return nil
}