forked from livekit/server-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalparticipant.go
313 lines (266 loc) · 7.97 KB
/
localparticipant.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
package lksdk
import (
"sort"
"time"
"github.com/pion/webrtc/v3"
"google.golang.org/protobuf/proto"
"github.com/livekit/protocol/livekit"
)
const (
trackPublishTimeout = 10 * time.Second
)
type LocalParticipant struct {
baseParticipant
engine *RTCEngine
}
func newLocalParticipant(engine *RTCEngine, roomcallback *RoomCallback) *LocalParticipant {
return &LocalParticipant{
baseParticipant: *newBaseParticipant(roomcallback),
engine: engine,
}
}
func (p *LocalParticipant) PublishTrack(track webrtc.TrackLocal, opts *TrackPublicationOptions) (*LocalTrackPublication, error) {
if opts == nil {
opts = &TrackPublicationOptions{}
}
kind := KindFromRTPType(track.Kind())
// default sources, since clients generally look for camera/mic
if opts.Source == livekit.TrackSource_UNKNOWN {
if kind == TrackKindVideo {
opts.Source = livekit.TrackSource_CAMERA
} else if kind == TrackKindAudio {
opts.Source = livekit.TrackSource_MICROPHONE
}
}
pub := NewLocalTrackPublication(kind, track, opts.Name, p.engine.client)
pub.OnRttUpdate(func(rtt uint32) {
p.engine.setRTT(rtt)
})
req := &livekit.AddTrackRequest{
Cid: track.ID(),
Name: opts.Name,
Source: opts.Source,
Type: kind.ProtoType(),
Width: uint32(opts.VideoWidth),
Height: uint32(opts.VideoHeight),
DisableDtx: opts.DisableDTX,
Stereo: opts.Stereo,
}
if kind == TrackKindVideo {
// single layer
req.Layers = []*livekit.VideoLayer{
{
Quality: livekit.VideoQuality_HIGH,
Width: uint32(opts.VideoWidth),
Height: uint32(opts.VideoHeight),
},
}
}
err := p.engine.client.SendRequest(&livekit.SignalRequest{
Message: &livekit.SignalRequest_AddTrack{
AddTrack: req,
},
})
if err != nil {
return nil, err
}
pubChan := p.engine.TrackPublishedChan()
var pubRes *livekit.TrackPublishedResponse
select {
case pubRes = <-pubChan:
break
case <-time.After(trackPublishTimeout):
return nil, ErrTrackPublishTimeout
}
// add transceivers
transceiver, err := p.engine.publisher.PeerConnection().AddTransceiverFromTrack(track, webrtc.RTPTransceiverInit{
Direction: webrtc.RTPTransceiverDirectionSendonly,
})
if err != nil {
return nil, err
}
pub.setSender(transceiver.Sender())
pub.updateInfo(pubRes.Track)
p.addPublication(pub)
p.engine.publisher.Negotiate()
logger.Infow("published track", "name", opts.Name, "source", opts.Source.String())
return pub, nil
}
// PublishSimulcastTrack publishes up to three layers to the server
func (p *LocalParticipant) PublishSimulcastTrack(tracks []*LocalSampleTrack, opts *TrackPublicationOptions) (*LocalTrackPublication, error) {
if len(tracks) == 0 {
return nil, nil
}
for _, track := range tracks {
if track.Kind() != webrtc.RTPCodecTypeVideo {
return nil, ErrUnsupportedSimulcastKind
}
if track.videoLayer == nil || track.RID() == "" {
return nil, ErrInvalidSimulcastTrack
}
}
// tracks should be low to high
sort.Slice(tracks, func(i, j int) bool {
return tracks[i].videoLayer.Width < tracks[j].videoLayer.Width
})
if opts == nil {
opts = &TrackPublicationOptions{}
}
// default sources, since clients generally look for camera/mic
if opts.Source == livekit.TrackSource_UNKNOWN {
opts.Source = livekit.TrackSource_CAMERA
}
mainTrack := tracks[len(tracks)-1]
pub := NewLocalTrackPublication(KindFromRTPType(mainTrack.Kind()), nil, opts.Name, p.engine.client)
var layers []*livekit.VideoLayer
for _, st := range tracks {
layers = append(layers, st.videoLayer)
}
err := p.engine.client.SendRequest(&livekit.SignalRequest{
Message: &livekit.SignalRequest_AddTrack{
AddTrack: &livekit.AddTrackRequest{
Cid: mainTrack.ID(),
Name: opts.Name,
Source: opts.Source,
Type: pub.Kind().ProtoType(),
Width: mainTrack.videoLayer.Width,
Height: mainTrack.videoLayer.Height,
Layers: layers,
},
},
})
if err != nil {
return nil, err
}
pubChan := p.engine.TrackPublishedChan()
var pubRes *livekit.TrackPublishedResponse
select {
case pubRes = <-pubChan:
break
case <-time.After(trackPublishTimeout):
return nil, ErrTrackPublishTimeout
}
// add transceivers
publishPC := p.engine.publisher.PeerConnection()
var transceiver *webrtc.RTPTransceiver
var sender *webrtc.RTPSender
for idx, st := range tracks {
if idx == 0 {
transceiver, err = publishPC.AddTransceiverFromTrack(st, webrtc.RTPTransceiverInit{
Direction: webrtc.RTPTransceiverDirectionSendonly,
})
if err != nil {
return nil, err
}
sender = transceiver.Sender()
pub.setSender(sender)
} else {
if err = sender.AddEncoding(st); err != nil {
return nil, err
}
}
pub.addSimulcastTrack(st)
st.SetTransceiver(transceiver)
}
pub.updateInfo(pubRes.Track)
p.addPublication(pub)
p.engine.publisher.Negotiate()
logger.Infow("published simulcast track", "name", opts.Name, "source", opts.Source.String())
return pub, nil
}
func (p *LocalParticipant) PublishData(data []byte, kind livekit.DataPacket_Kind, destinationSids []string) error {
packet := &livekit.DataPacket{
Kind: kind,
Value: &livekit.DataPacket_User{
User: &livekit.UserPacket{
// this is enforced on the server side, setting for completeness
ParticipantSid: p.sid,
Payload: data,
DestinationSids: destinationSids,
},
},
}
if err := p.engine.ensurePublisherConnected(true); err != nil {
return err
}
// encode packet
encoded, err := proto.Marshal(packet)
if err != nil {
return err
}
return p.engine.GetDataChannel(kind).Send(encoded)
}
func (p *LocalParticipant) UnpublishTrack(sid string) error {
obj, loaded := p.tracks.LoadAndDelete(sid)
if !loaded {
return ErrCannotFindTrack
}
p.audioTracks.Delete(sid)
p.videoTracks.Delete(sid)
pub, ok := obj.(*LocalTrackPublication)
if !ok {
return nil
}
var err error
if localTrack, ok := pub.track.(webrtc.TrackLocal); ok {
for _, sender := range p.engine.publisher.pc.GetSenders() {
if sender.Track() == localTrack {
err = p.engine.publisher.pc.RemoveTrack(sender)
break
}
}
p.engine.publisher.Negotiate()
}
return err
}
// GetSubscriberPeerConnection is a power-user API that gives access to the underlying subscriber peer connection
// subscribed tracks are received using this PeerConnection
func (p *LocalParticipant) GetSubscriberPeerConnection() *webrtc.PeerConnection {
return p.engine.subscriber.PeerConnection()
}
// GetPublisherPeerConnection is a power-user API that gives access to the underlying publisher peer connection
// local tracks are published to server via this PeerConnection
func (p *LocalParticipant) GetPublisherPeerConnection() *webrtc.PeerConnection {
return p.engine.publisher.PeerConnection()
}
// SetName sets the name of the current participant.
// updates will be performed only if the participant has canUpdateOwnMetadata grant
func (p *LocalParticipant) SetName(name string) {
_ = p.engine.client.SendUpdateParticipantMetadata(&livekit.UpdateParticipantMetadata{
Name: name,
})
}
// SetMetadata sets the metadata of the current participant.
// updates will be performed only if the participant has canUpdateOwnMetadata grant
func (p *LocalParticipant) SetMetadata(metadata string) {
_ = p.engine.client.SendUpdateParticipantMetadata(&livekit.UpdateParticipantMetadata{
Metadata: metadata,
})
}
func (p *LocalParticipant) updateInfo(info *livekit.ParticipantInfo) {
p.baseParticipant.updateInfo(info, p)
// detect tracks that have been muted remotely, and apply changes
for _, ti := range info.Tracks {
pub := p.getLocalPublication(ti.Sid)
if pub == nil {
continue
}
if pub.IsMuted() != ti.Muted {
pub.SetMuted(ti.Muted)
// trigger callback
if ti.Muted {
p.Callback.OnTrackMuted(pub, p)
p.roomCallback.OnTrackMuted(pub, p)
} else if !ti.Muted {
p.Callback.OnTrackUnmuted(pub, p)
p.roomCallback.OnTrackUnmuted(pub, p)
}
}
}
}
func (p *LocalParticipant) getLocalPublication(sid string) *LocalTrackPublication {
if pub, ok := p.getPublication(sid).(*LocalTrackPublication); ok {
return pub
}
return nil
}