forked from inlivedev/sfu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remotetrack.go
236 lines (191 loc) · 5.24 KB
/
remotetrack.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package sfu
import (
"context"
"errors"
"io"
"sync"
"time"
"sync/atomic"
"github.com/inlivedev/sfu/pkg/networkmonitor"
"github.com/inlivedev/sfu/pkg/rtppool"
"github.com/pion/interceptor"
"github.com/pion/interceptor/pkg/stats"
"github.com/pion/logging"
"github.com/pion/rtp"
)
type remoteTrack struct {
context context.Context
cancel context.CancelFunc
mu sync.RWMutex
track IRemoteTrack
onRead func(interceptor.Attributes, *rtp.Packet)
onPLI func()
bitrate *atomic.Uint32
previousBytesReceived *atomic.Uint64
currentBytesReceived *atomic.Uint64
latestUpdatedTS *atomic.Uint64
lastPLIRequestTime time.Time
onEndedCallbacks []func()
statsGetter stats.Getter
onStatsUpdated func(*stats.Stats)
log logging.LeveledLogger
rtppool *rtppool.RTPPool
}
func newRemoteTrack(ctx context.Context, log logging.LeveledLogger, useBuffer bool, track IRemoteTrack, minWait, maxWait, pliInterval time.Duration, onPLI func(), statsGetter stats.Getter, onStatsUpdated func(*stats.Stats), onRead func(interceptor.Attributes, *rtp.Packet), pool *rtppool.RTPPool, onNetworkConditionChanged func(networkmonitor.NetworkConditionType)) *remoteTrack {
localctx, cancel := context.WithCancel(ctx)
rt := &remoteTrack{
context: localctx,
cancel: cancel,
mu: sync.RWMutex{},
track: track,
bitrate: &atomic.Uint32{},
previousBytesReceived: &atomic.Uint64{},
currentBytesReceived: &atomic.Uint64{},
latestUpdatedTS: &atomic.Uint64{},
onEndedCallbacks: make([]func(), 0),
statsGetter: statsGetter,
onStatsUpdated: onStatsUpdated,
onPLI: onPLI,
onRead: onRead,
log: log,
rtppool: pool,
}
if pliInterval > 0 {
rt.enableIntervalPLI(pliInterval)
}
go rt.readRTP()
return rt
}
func (t *remoteTrack) Context() context.Context {
return t.context
}
func (t *remoteTrack) readRTP() {
readCtx, cancel := context.WithCancel(t.context)
defer cancel()
defer t.cancel()
defer t.onEnded()
for {
select {
case <-readCtx.Done():
return
default:
if err := t.track.SetReadDeadline(time.Now().Add(1 * time.Second)); err != nil {
t.log.Errorf("remotetrack: set read deadline error - %s", err.Error())
return
}
buffer := t.rtppool.GetPayload()
n, attrs, readErr := t.track.Read(*buffer)
if readErr != nil {
if readErr == io.EOF {
t.log.Infof("remotetrack: track ended %s ", t.track.ID())
t.rtppool.PutPayload(buffer)
return
}
t.log.Tracef("remotetrack: read error: %s", readErr.Error())
t.rtppool.PutPayload(buffer)
continue
}
// could be read deadline reached
if n == 0 {
t.rtppool.PutPayload(buffer)
continue
}
p := t.rtppool.GetPacket()
if err := t.unmarshal((*buffer)[:n], p); err != nil {
t.log.Errorf("remotetrack: unmarshal error: %s", err.Error())
t.rtppool.PutPayload(buffer)
t.rtppool.PutPacket(p)
continue
}
if !t.IsRelay() {
go t.updateStats()
}
t.onRead(attrs, p)
t.rtppool.PutPayload(buffer)
t.rtppool.PutPacket(p)
}
}
}
func (t *remoteTrack) unmarshal(buf []byte, p *rtp.Packet) error {
n, err := p.Header.Unmarshal(buf)
if err != nil {
return err
}
end := len(buf)
if p.Header.Padding {
p.PaddingSize = buf[end-1]
end -= int(p.PaddingSize)
}
if end < n {
return errors.New("remote track buffer too short")
}
p.Payload = buf[n:end]
return nil
}
func (t *remoteTrack) updateStats() {
s := t.statsGetter.Get(uint32(t.track.SSRC()))
if s == nil {
t.log.Warnf("remotetrack: stats not found for track: ", t.track.SSRC())
return
}
// update the stats if the last update equal or more than 1 second
latestUpdated := t.latestUpdatedTS.Load()
if time.Since(time.Unix(0, int64(latestUpdated))).Seconds() <= 1 {
return
}
if latestUpdated == 0 {
t.latestUpdatedTS.Store(uint64(s.LastPacketReceivedTimestamp.UnixNano()))
return
}
t.latestUpdatedTS.Store(uint64(s.LastPacketReceivedTimestamp.UnixNano()))
if t.onStatsUpdated != nil {
t.onStatsUpdated(s)
}
}
func (t *remoteTrack) Track() IRemoteTrack {
return t.track
}
func (t *remoteTrack) sendPLI() {
t.mu.Lock()
defer t.mu.Unlock()
// return if there is a pending PLI request
maxGapSeconds := 250 * time.Millisecond
requestGap := time.Since(t.lastPLIRequestTime)
if requestGap < maxGapSeconds {
return // ignore PLI request
}
t.lastPLIRequestTime = time.Now()
go t.onPLI()
}
func (t *remoteTrack) enableIntervalPLI(interval time.Duration) {
go func() {
ctx, cancel := context.WithCancel(t.context)
defer cancel()
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
t.sendPLI()
}
}
}()
}
func (t *remoteTrack) IsRelay() bool {
_, ok := t.track.(*RelayTrack)
return ok
}
func (t *remoteTrack) OnEnded(f func()) {
t.mu.Lock()
defer t.mu.Unlock()
t.onEndedCallbacks = append(t.onEndedCallbacks, f)
}
func (t *remoteTrack) onEnded() {
t.mu.RLock()
defer t.mu.RUnlock()
for _, f := range t.onEndedCallbacks {
f()
}
}