-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathclienttracksvc.go
357 lines (297 loc) · 7.86 KB
/
clienttracksvc.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package sfu
import (
"github.com/pion/rtp"
"github.com/pion/rtp/codecs"
)
type IQualityPreset interface {
GetSID() uint8
GetTID() uint8
}
type QualityPreset struct {
SID uint8 `json:"sid" example:"2"`
TID uint8 `json:"tid" example:"2"`
}
var DefaultQualityPresets = map[QualityLevel]QualityPreset{
QualityHigh: QualityPreset{
SID: 2,
TID: 2,
},
QualityHighMid: QualityPreset{
SID: 2,
TID: 1,
},
QualityHighLow: QualityPreset{
SID: 2,
TID: 0,
},
QualityMid: QualityPreset{
SID: 1,
TID: 2,
},
QualityMidMid: QualityPreset{
SID: 1,
TID: 1,
},
QualityMidLow: QualityPreset{
SID: 1,
TID: 0,
},
QualityLow: QualityPreset{
SID: 0,
TID: 2,
},
QualityLowMid: QualityPreset{
SID: 0,
TID: 1,
},
QualityLowLow: QualityPreset{
SID: 0,
TID: 0,
},
QualityNone: QualityPreset{
SID: 0,
TID: 0,
},
}
func (q QualityPreset) GetSID() uint8 {
return q.SID
}
func (q QualityPreset) GetTID() uint8 {
return q.TID
}
type QualityPresets struct {
High QualityPreset `json:"high"`
HighMid QualityPreset `json:"highmid"`
HighLow QualityPreset `json:"highlow"`
Mid QualityPreset `json:"mid"`
MidMid QualityPreset `json:"midmid"`
MidLow QualityPreset `json:"midlow"`
Low QualityPreset `json:"low"`
LowMid QualityPreset `json:"lowmid"`
LowLow QualityPreset `json:"lowlow"`
}
func DefaultQualityLevels() []QualityLevel {
return []QualityLevel{
QualityHigh,
QualityMid,
QualityLow,
QualityLowMid,
QualityLowLow,
}
}
type scaleableClientTrack struct {
*clientTrack
lastQuality QualityLevel
maxQuality QualityLevel
tid uint8
sid uint8
lastTimestamp uint32
lastSequence uint16
init bool
}
func newScaleableClientTrack(
c *Client,
t *Track,
) *scaleableClientTrack {
sct := &scaleableClientTrack{
clientTrack: newClientTrack(c, t, false, nil),
maxQuality: QualityHigh,
lastQuality: QualityHigh,
}
sct.SetMaxQuality(QualityHigh)
return sct
}
func (t *scaleableClientTrack) isKeyframe(vp9 *codecs.VP9Packet) bool {
if len(vp9.Payload) < 1 {
return false
}
// According to VP9 RFC, B bit set to 1 if packet is the beginning of a new VP9 frame
// For keyframes, P bit should be 0 (no inter-picture prediction)
if !vp9.B {
return false
}
// Check for keyframe by examining the VP9 payload
// This identifies the frame as an intra-frame (keyframe)
if (vp9.Payload[0] & 0xc0) != 0x80 {
return false
}
profile := (vp9.Payload[0] >> 4) & 0x3
if profile != 3 {
return (vp9.Payload[0]&0xC) == 0 && true
}
return (vp9.Payload[0]&0x6) == 0 && true
}
func (t *scaleableClientTrack) getQuality() QualityLevel {
claim := t.client.bitrateController.GetClaim(t.ID())
if claim == nil {
t.client.log.Warnf("scalabletrack: claim is nil")
return QualityNone
}
return min(t.MaxQuality(), claim.Quality(), Uint32ToQualityLevel(t.client.quality.Load()))
}
func (t *scaleableClientTrack) push(p *rtp.Packet, _ QualityLevel) {
vp9Packet := &codecs.VP9Packet{}
if _, err := vp9Packet.Unmarshal(p.Payload); err != nil {
_ = t.packetmap.Drop(p.SequenceNumber, vp9Packet.PictureID)
return
}
quality := t.getQuality()
qualityPreset := qualityLevelToPreset(quality)
targetSID := qualityPreset.GetSID()
targetTID := qualityPreset.GetTID()
if !t.init {
t.init = true
t.sid = targetSID
t.tid = targetTID
}
t.lastSequence = p.Header.SequenceNumber
currentTID := t.tid
currentSID := t.sid
// Temporal layer switching according to VP9 RFC
// U bit indicates a switching up point where we can safely switch to a higher frame rate
if t.tid < targetTID {
if vp9Packet.U && vp9Packet.B && currentTID < vp9Packet.TID && vp9Packet.TID <= targetTID {
// Scale temporal up - U bit confirms it's safe to switch up
t.tid = vp9Packet.TID
currentTID = t.tid
}
} else if t.tid > targetTID {
if vp9Packet.E {
// Scale temporal down - safe at the end of a frame
t.tid = vp9Packet.TID
}
}
// Spatial layer switching according to VP9 RFC
// D bit indicates inter-layer dependency
if currentSID < targetSID {
// Switching up to higher spatial layer
// For scaling up, we should ensure this is the start of a frame (B=1)
// and the layer doesn't depend on layers we might have skipped
if vp9Packet.B && currentSID < vp9Packet.SID && vp9Packet.SID <= targetSID {
// For non-base layers, check the D bit to understand dependencies
if vp9Packet.SID > 0 && vp9Packet.D {
// This layer depends on the previous spatial layer
// Only switch if we have the previous layer
if vp9Packet.SID == currentSID+1 {
t.sid = vp9Packet.SID
currentSID = t.sid
}
} else {
// This layer doesn't depend on previous spatial layer (D=0) or is base layer
// Safe to switch to this layer
t.sid = vp9Packet.SID
currentSID = t.sid
}
}
} else if currentSID > targetSID {
// Switching down to lower spatial layer
// Safe to switch down at the end of a frame
if vp9Packet.E {
t.sid = vp9Packet.SID
}
}
if vp9Packet.E && t.tid == targetTID && t.sid == targetSID {
t.setLastQuality(quality)
}
// Determine if packet should be dropped
shouldDrop := false
// Drop packets higher than our target temporal layer
if vp9Packet.TID > currentTID {
shouldDrop = true
}
// Drop packets higher than our target spatial layer
if vp9Packet.SID > currentSID {
shouldDrop = true
}
// Drop packets from non-reference frames for higher spatial layers that we're not using
// Similar to Z bit concept in the RFC
sidNonReference := (p.Payload[0] & 0x01) != 0
if vp9Packet.SID > currentSID && sidNonReference {
shouldDrop = true
}
if shouldDrop {
ok := t.packetmap.Drop(p.SequenceNumber, vp9Packet.PictureID)
if ok {
return
}
}
// Mark packet as a last spatial layer packet
// According to RFC: Marker bit (M) MUST be set to 1 for the final packet of the
// highest spatial layer frame (the final packet of the picture)
if vp9Packet.E && currentSID == vp9Packet.SID && targetSID <= currentSID {
p.Marker = true
}
ok, newseqno, piddelta := t.packetmap.Map(p.SequenceNumber, vp9Packet.PictureID)
if !ok {
return
}
marker := (p.Payload[1] & 0x80) != 0
if marker && newseqno == p.SequenceNumber && piddelta == 0 {
t.send(p)
return
}
if marker {
p.Payload[1] |= 0x80
}
p.SequenceNumber = newseqno
// If quality is none we need to send blank frame
// Make sure the player is paused when the quality is none.
// Quality none only possible when the video is not displayed
if quality == QualityNone {
if ok := t.packetmap.Drop(p.SequenceNumber, vp9Packet.PictureID); ok {
return
}
p.Payload = p.Payload[:0]
}
t.send(p)
}
func (t *scaleableClientTrack) send(p *rtp.Packet) {
t.mu.Lock()
t.lastTimestamp = p.Timestamp
t.mu.Unlock()
if err := t.localTrack.WriteRTP(p); err != nil {
t.client.log.Errorf("scaleabletrack: error on write rtp", err)
}
}
func (t *scaleableClientTrack) SetSourceType(sourceType TrackType) {
t.isScreen = (sourceType == TrackTypeScreen)
}
func (t *scaleableClientTrack) setLastQuality(quality QualityLevel) {
t.mu.Lock()
defer t.mu.Unlock()
t.lastQuality = quality
}
func (t *scaleableClientTrack) Quality() QualityLevel {
return t.LastQuality()
}
func (t *scaleableClientTrack) LastQuality() QualityLevel {
t.mu.RLock()
defer t.mu.RUnlock()
return QualityLevel(t.lastQuality)
}
func (t *scaleableClientTrack) SetMaxQuality(quality QualityLevel) {
t.mu.Lock()
t.maxQuality = quality
t.mu.Unlock()
claim := t.Client().bitrateController.GetClaim(t.ID())
if claim != nil {
if claim.Quality() > quality && quality != QualityNone {
claim.SetQuality(quality)
}
}
t.RequestPLI()
}
func (t *scaleableClientTrack) MaxQuality() QualityLevel {
t.mu.RLock()
defer t.mu.RUnlock()
return t.maxQuality
}
func (t *scaleableClientTrack) IsSimulcast() bool {
return false
}
func (t *scaleableClientTrack) IsScaleable() bool {
return true
}
func (t *scaleableClientTrack) RequestPLI() {
go t.remoteTrack.SendPLI()
}