forked from inlivedev/sfu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclienttrack.go
247 lines (198 loc) · 5.3 KB
/
clienttrack.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
package sfu
import (
"context"
"sync"
"github.com/inlivedev/sfu/pkg/packetmap"
"github.com/pion/rtp"
"github.com/pion/webrtc/v4"
)
type iClientTrack interface {
push(rtp *rtp.Packet, quality QualityLevel)
ID() string
StreamID() string
Context() context.Context
Kind() webrtc.RTPCodecType
MimeType() string
LocalTrack() *webrtc.TrackLocalStaticRTP
IsScreen() bool
IsSimulcast() bool
IsScaleable() bool
SetSourceType(TrackType)
Client() *Client
RequestPLI()
SetMaxQuality(quality QualityLevel)
MaxQuality() QualityLevel
ReceiveBitrate() uint32
SendBitrate() uint32
Quality() QualityLevel
OnEnded(func())
}
type clientTrack struct {
id string
streamid string
context context.Context
mu sync.RWMutex
client *Client
kind webrtc.RTPCodecType
mimeType string
localTrack *webrtc.TrackLocalStaticRTP
remoteTrack *remoteTrack
baseTrack *baseTrack
packetmap *packetmap.Map
isScreen bool
ssrc webrtc.SSRC
onTrackEndedCallbacks []func()
}
func newClientTrack(c *Client, t ITrack, isScreen bool, localTrack *webrtc.TrackLocalStaticRTP) *clientTrack {
ctx, cancel := context.WithCancel(t.Context())
track := t.(*Track)
if localTrack == nil {
localTrack = track.createLocalTrack()
}
ct := &clientTrack{
id: localTrack.ID(),
streamid: localTrack.StreamID(),
context: ctx,
mu: sync.RWMutex{},
client: c,
kind: localTrack.Kind(),
mimeType: localTrack.Codec().MimeType,
localTrack: localTrack,
remoteTrack: track.remoteTrack,
baseTrack: track.base,
isScreen: isScreen,
ssrc: track.remoteTrack.track.SSRC(),
onTrackEndedCallbacks: make([]func(), 0),
packetmap: &packetmap.Map{},
}
t.OnEnded(func() {
ct.onEnded()
cancel()
})
return ct
}
func (t *clientTrack) ID() string {
return t.id
}
func (t *clientTrack) StreamID() string {
return t.streamid
}
func (t *clientTrack) ReceiveBitrate() uint32 {
if t.remoteTrack == nil || t.client == nil {
return 0
}
bitrate, err := t.baseTrack.client.stats.GetReceiverBitrate(t.remoteTrack.track.ID(), t.remoteTrack.track.RID())
if err != nil {
return 0
}
return bitrate
}
func (t *clientTrack) SendBitrate() uint32 {
bitrate, err := t.client.stats.GetSenderBitrate(t.ID())
if err != nil {
t.client.log.Errorf("clienttrack: error on get sender bitrate %s", err.Error())
return 0
}
return bitrate
}
func (t *clientTrack) Context() context.Context {
return t.context
}
func (t *clientTrack) Client() *Client {
return t.client
}
func (t *clientTrack) Kind() webrtc.RTPCodecType {
return t.remoteTrack.track.Kind()
}
func (t *clientTrack) MimeType() string {
return t.mimeType
}
func (t *clientTrack) push(p *rtp.Packet, _ QualityLevel) {
if t.client.peerConnection.PC().ConnectionState() != webrtc.PeerConnectionStateConnected {
return
}
ok, newseqno, _ := t.packetmap.Map(p.SequenceNumber, 0)
if !ok {
return
}
p.SequenceNumber = newseqno
if t.Kind() == webrtc.RTPCodecTypeAudio {
// do something here with audio level
}
// if video 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 t.Kind() == webrtc.RTPCodecTypeVideo {
quality := t.getQuality()
if quality == QualityNone {
if ok := t.packetmap.Drop(p.SequenceNumber, 0); ok {
return
}
p.Payload = p.Payload[:0]
}
}
if err := t.localTrack.WriteRTP(p); err != nil {
t.client.log.Errorf("clienttrack: error on write rtp", err)
}
}
func (t *clientTrack) LocalTrack() *webrtc.TrackLocalStaticRTP {
return t.localTrack
}
func (t *clientTrack) IsScreen() bool {
return t.isScreen
}
func (t *clientTrack) SetSourceType(sourceType TrackType) {
t.mu.Lock()
defer t.mu.Unlock()
t.isScreen = sourceType == TrackTypeScreen
}
func (t *clientTrack) IsSimulcast() bool {
return false
}
func (t *clientTrack) IsScaleable() bool {
return false
}
func (t *clientTrack) RequestPLI() {
t.remoteTrack.sendPLI()
}
func (t *clientTrack) SetMaxQuality(_ QualityLevel) {
// do nothing
}
func (t *clientTrack) MaxQuality() QualityLevel {
if t.Kind() == webrtc.RTPCodecTypeVideo {
return QualityHigh
}
return QualityAudio
}
func (t *clientTrack) SSRC() webrtc.SSRC {
return t.ssrc
}
func (t *clientTrack) Quality() QualityLevel {
if t.Kind() == webrtc.RTPCodecTypeVideo {
return QualityHigh
}
return QualityAudio
}
func (t *clientTrack) OnEnded(f func()) {
t.mu.Lock()
defer t.mu.Unlock()
t.onTrackEndedCallbacks = append(t.onTrackEndedCallbacks, f)
}
func (t *clientTrack) onEnded() {
t.mu.RLock()
defer t.mu.RUnlock()
for _, f := range t.onTrackEndedCallbacks {
f()
}
}
func (t *clientTrack) 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 qualityLevelToPreset(lvl QualityLevel) (qualityPreset QualityPreset) {
return DefaultQualityPresets[lvl]
}