-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathRoom.cs
More file actions
576 lines (523 loc) · 26 KB
/
Room.cs
File metadata and controls
576 lines (523 loc) · 26 KB
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
using System;
using System.Collections.Generic;
using LiveKit.Internal;
using LiveKit.Proto;
using System.Runtime.InteropServices;
using LiveKit.Internal.FFIClients.Requests;
using UnityEngine;
namespace LiveKit
{
public enum IceTransportType
{
TRANSPORT_RELAY = 0,
TRANSPORT_NOHOST = 1,
TRANSPORT_ALL = 2
}
public enum ContinualGatheringPolicy
{
GATHER_ONCE = 0,
GATHER_CONTINUALLY = 1
}
public class IceServer
{
public string[] Urls;
public string Username;
public string Password;
public Proto.IceServer ToProto()
{
var proto = new Proto.IceServer();
proto.Username = Username;
proto.Password = Password;
proto.Urls.AddRange(Urls);
return proto;
}
}
public class RTCConfiguration
{
IceTransportType IceTransportType = IceTransportType.TRANSPORT_ALL;
ContinualGatheringPolicy ContinualGatheringPolicy = ContinualGatheringPolicy.GATHER_ONCE;
IceServer[] IceServers;
public Proto.RtcConfig ToProto()
{
var proto = new Proto.RtcConfig();
switch (ContinualGatheringPolicy)
{
case ContinualGatheringPolicy.GATHER_ONCE:
proto.ContinualGatheringPolicy = Proto.ContinualGatheringPolicy.GatherOnce;
break;
case ContinualGatheringPolicy.GATHER_CONTINUALLY:
proto.ContinualGatheringPolicy = Proto.ContinualGatheringPolicy.GatherContinually;
break;
}
switch (IceTransportType)
{
case IceTransportType.TRANSPORT_ALL:
proto.IceTransportType = Proto.IceTransportType.TransportAll;
break;
case IceTransportType.TRANSPORT_RELAY:
proto.IceTransportType = Proto.IceTransportType.TransportRelay;
break;
case IceTransportType.TRANSPORT_NOHOST:
proto.IceTransportType = Proto.IceTransportType.TransportNohost;
break;
}
foreach (var item in IceServers)
{
proto.IceServers.Add(item.ToProto());
}
return proto;
}
}
public class RoomOptions
{
public bool AutoSubscribe = true;
public bool Dynacast = true;
public bool AdaptiveStream = true;
public uint JoinRetries = 3;
public RTCConfiguration RtcConfig = null;
public E2EEOptions E2EE = null;
public Proto.RoomOptions ToProto()
{
var proto = new Proto.RoomOptions();
proto.AutoSubscribe = AutoSubscribe;
proto.Dynacast = Dynacast;
proto.AdaptiveStream = AdaptiveStream;
proto.JoinRetries = JoinRetries;
proto.RtcConfig = RtcConfig?.ToProto();
proto.E2Ee = E2EE?.ToProto();
return proto;
}
}
public class Room
{
internal FfiHandle RoomHandle = null;
private readonly Dictionary<string, RemoteParticipant> _participants = new();
private StreamHandlerRegistry _streamHandlers = new();
public delegate void MetaDelegate(string metaData);
public delegate void ParticipantDelegate(Participant participant);
public delegate void RemoteParticipantDelegate(RemoteParticipant participant);
public delegate void LocalPublishDelegate(TrackPublication publication, LocalParticipant participant);
public delegate void PublishDelegate(RemoteTrackPublication publication, RemoteParticipant participant);
public delegate void SubscribeDelegate(IRemoteTrack track, RemoteTrackPublication publication, RemoteParticipant participant);
public delegate void MuteDelegate(TrackPublication publication, Participant participant);
public delegate void SpeakersChangeDelegate(List<Participant> speakers);
public delegate void ConnectionQualityChangeDelegate(ConnectionQuality quality, Participant participant);
public delegate void DataDelegate(byte[] data, Participant participant, DataPacketKind kind, string topic);
public delegate void SipDtmfDelegate(Participant participant, UInt32 code, string digit);
public delegate void ConnectionStateChangeDelegate(ConnectionState connectionState);
public delegate void ConnectionDelegate(Room room);
public delegate void DisconnectDelegate(DisconnectReason reason);
public delegate void E2EeStateChangedDelegate(Participant participant, EncryptionState state);
public string Sid { private set; get; }
public string Name { private set; get; }
public string Metadata { private set; get; }
public LocalParticipant LocalParticipant { private set; get; }
public ConnectionState ConnectionState { private set; get; }
public bool IsConnected => RoomHandle != null && ConnectionState != ConnectionState.ConnDisconnected;
public E2EEManager E2EEManager { internal set; get; }
public IReadOnlyDictionary<string, RemoteParticipant> RemoteParticipants => _participants;
public event ParticipantDelegate ParticipantConnected;
public event ParticipantDelegate ParticipantDisconnected;
public event LocalPublishDelegate LocalTrackPublished;
public event LocalPublishDelegate LocalTrackUnpublished;
public event PublishDelegate TrackPublished;
public event PublishDelegate TrackUnpublished;
public event SubscribeDelegate TrackSubscribed;
public event SubscribeDelegate TrackUnsubscribed;
public event MuteDelegate TrackMuted;
public event MuteDelegate TrackUnmuted;
public event SpeakersChangeDelegate ActiveSpeakersChanged;
public event ConnectionQualityChangeDelegate ConnectionQualityChanged;
public event DataDelegate DataReceived;
public event SipDtmfDelegate SipDtmfReceived;
public event ConnectionStateChangeDelegate ConnectionStateChanged;
public event ConnectionDelegate Connected;
public event DisconnectDelegate Disconnected;
public event ConnectionDelegate Reconnecting;
public event ConnectionDelegate Reconnected;
public event E2EeStateChangedDelegate E2EeStateChanged;
public event MetaDelegate RoomMetadataChanged;
public event ParticipantDelegate ParticipantMetadataChanged;
public event ParticipantDelegate ParticipantNameChanged;
public ConnectInstruction Connect(string url, string token, RoomOptions options)
{
using var response = FFIBridge.Instance.SendConnectRequest(url, token, options);
Utils.Debug("Connect....");
FfiResponse res = response;
Utils.Debug($"Connect response.... {response}");
return new ConnectInstruction(res.Connect.AsyncId, this, options);
}
public void Disconnect()
{
if (this.RoomHandle == null)
return;
using var response = FFIBridge.Instance.SendDisconnectRequest(this);
Utils.Debug($"Disconnect.... {RoomHandle}");
FfiResponse resp = response;
Utils.Debug($"Disconnect response.... {resp}");
}
/// <summary>
/// Registers a handler for incoming text streams matching the given topic.
/// </summary>
/// <param name="topic">Topic identifier that filters which streams will be handled.
/// Only streams with a matching topic will trigger the handler.</param>
/// <param name="handler">Handler that is invoked whenever a remote participant
/// opens a new stream with the matching topic. The handler receives a
/// <see cref="TextStreamReader" /> for consuming the stream data and the identity of
/// the remote participant who initiated the stream.</param>
/// <throws>Throws a <see cref="StreamError" /> if the topic is already registered.</throws>
public void RegisterTextStreamHandler(string topic, TextStreamHandler handler)
{
_streamHandlers.RegisterTextStreamHandler(topic, handler);
}
/// <summary>
/// Registers a handler for incoming byte streams matching the given topic.
/// </summary>
/// <param name="topic">Topic identifier that filters which streams will be handled.
/// Only streams with a matching topic will trigger the handler.</param>
/// <param name="handler">Handler that is invoked whenever a remote participant
/// opens a new stream with the matching topic. The handler receives a
/// <see cref="ByteStreamReader" /> for consuming the stream data and the identity of
/// the remote participant who initiated the stream.</param>
/// <throws>Throws a <see cref="StreamError" /> if the topic is already registered.</throws>
public void RegisterByteStreamHandler(string topic, ByteStreamHandler handler)
{
_streamHandlers.RegisterByteStreamHandler(topic, handler);
}
/// <summary>
/// Unregisters a handler for incoming text streams matching the given topic.
/// </summary>
/// <param name="topic">Topic identifier for which the handler should be unregistered.</param>
public void UnregisterTextStreamHandler(string topic)
{
_streamHandlers.UnregisterTextStreamHandler(topic);
}
/// <summary>
/// Unregisters a handler for incoming byte streams matching the given topic.
/// </summary>
/// <param name="topic">Topic identifier for which the handler should be unregistered.</param>
public void UnregisterByteStreamHandler(string topic)
{
_streamHandlers.UnregisterByteStreamHandler(topic);
}
internal void UpdateFromInfo(RoomInfo info)
{
Sid = info.Sid;
Name = info.Name;
Metadata = info.Metadata;
}
internal void OnRpcMethodInvocationReceived(RpcMethodInvocationEvent e)
{
if (e.LocalParticipantHandle == (ulong)LocalParticipant.Handle.DangerousGetHandle())
{
// Async but no need to await the response
LocalParticipant.HandleRpcMethodInvocation(
e.InvocationId,
e.Method,
e.RequestId,
e.CallerIdentity,
e.Payload,
e.ResponseTimeoutMs / 1000f);
}
}
internal void OnEventReceived(RoomEvent e)
{
if (e.RoomHandle != (ulong)RoomHandle.DangerousGetHandle())
return;
switch (e.MessageCase)
{
case RoomEvent.MessageOneofCase.RoomMetadataChanged:
{
Metadata = e.RoomMetadataChanged.Metadata;
RoomMetadataChanged?.Invoke(e.RoomMetadataChanged.Metadata);
}
break;
case RoomEvent.MessageOneofCase.ParticipantMetadataChanged:
{
var participant = GetParticipant(e.ParticipantMetadataChanged.ParticipantIdentity);
if (participant != null)
{
participant.SetMeta(e.ParticipantMetadataChanged.Metadata);
ParticipantMetadataChanged?.Invoke(participant);
}
else Utils.Debug("Unable to find participant: " + e.ParticipantMetadataChanged.ParticipantIdentity + " in Meta data Change Event");
}
break;
case RoomEvent.MessageOneofCase.ParticipantNameChanged:
{
var participant = GetParticipant(e.ParticipantNameChanged.ParticipantIdentity);
participant.SetName(e.ParticipantNameChanged.Name);
if (participant != null) ParticipantNameChanged?.Invoke(participant);
else Utils.Debug("Unable to find participant: " + e.ParticipantNameChanged.ParticipantIdentity + " in Meta data Change Event");
}
break;
case RoomEvent.MessageOneofCase.ParticipantConnected:
{
var participant = CreateRemoteParticipant(e.ParticipantConnected.Info);
ParticipantConnected?.Invoke(participant);
}
break;
case RoomEvent.MessageOneofCase.ParticipantDisconnected:
{
var sid = e.ParticipantDisconnected.ParticipantIdentity;
var participant = RemoteParticipants[sid];
_participants.Remove(sid);
ParticipantDisconnected?.Invoke(participant);
}
break;
case RoomEvent.MessageOneofCase.TrackPublished:
{
var participant = RemoteParticipants[e.TrackPublished.ParticipantIdentity];
var publication = new RemoteTrackPublication(e.TrackPublished.Publication.Info, FfiHandle.FromOwnedHandle(e.TrackPublished.Publication.Handle));
participant._tracks.Add(publication.Sid, publication);
participant.OnTrackPublished(publication);
TrackPublished?.Invoke(publication, participant);
}
break;
case RoomEvent.MessageOneofCase.TrackUnpublished:
{
var participant = RemoteParticipants[e.TrackUnpublished.ParticipantIdentity];
var publication = participant.Tracks[e.TrackUnpublished.PublicationSid];
participant._tracks.Remove(publication.Sid);
participant.OnTrackUnpublished(publication);
TrackUnpublished?.Invoke(publication, participant);
}
break;
case RoomEvent.MessageOneofCase.TrackSubscribed:
{
var track = e.TrackSubscribed.Track;
var info = track.Info;
var participant = RemoteParticipants[e.TrackSubscribed.ParticipantIdentity];
var publication = participant.Tracks[info.Sid];
if (publication == null)
{
participant._tracks.Add(publication.Sid, publication);
}
if (info.Kind == TrackKind.KindVideo)
{
var videoTrack = new RemoteVideoTrack(track, this, participant);
publication.UpdateTrack(videoTrack);
TrackSubscribed?.Invoke(videoTrack, publication, participant);
}
else if (info.Kind == TrackKind.KindAudio)
{
var audioTrack = new RemoteAudioTrack(track, this, participant);
publication.UpdateTrack(audioTrack);
TrackSubscribed?.Invoke(audioTrack, publication, participant);
}
}
break;
case RoomEvent.MessageOneofCase.TrackUnsubscribed:
{
var participant = RemoteParticipants[e.TrackUnsubscribed.ParticipantIdentity];
var publication = participant.Tracks[e.TrackUnsubscribed.TrackSid];
var track = publication.Track;
publication.UpdateTrack(null);
TrackUnsubscribed?.Invoke(track, publication, participant);
}
break;
case RoomEvent.MessageOneofCase.LocalTrackUnpublished:
{
if (LocalParticipant._tracks.ContainsKey(e.LocalTrackUnpublished.PublicationSid))
{
var publication = LocalParticipant._tracks[e.LocalTrackUnpublished.PublicationSid];
LocalTrackUnpublished?.Invoke(publication, LocalParticipant);
}
else
{
Utils.Debug("Unable to find local track after unpublish: " + e.LocalTrackPublished.TrackSid);
}
}
break;
case RoomEvent.MessageOneofCase.LocalTrackPublished:
{
if (LocalParticipant._tracks.ContainsKey(e.LocalTrackPublished.TrackSid))
{
var publication = LocalParticipant._tracks[e.LocalTrackPublished.TrackSid];
LocalTrackPublished?.Invoke(publication, LocalParticipant);
}
else
{
Utils.Debug("Unable to find local track after publish: " + e.LocalTrackPublished.TrackSid);
}
}
break;
case RoomEvent.MessageOneofCase.TrackMuted:
{
var participant = GetParticipant(e.TrackMuted.ParticipantIdentity);
var publication = participant.Tracks[e.TrackMuted.TrackSid];
publication.UpdateMuted(true);
TrackMuted?.Invoke(publication, participant);
}
break;
case RoomEvent.MessageOneofCase.TrackUnmuted:
{
var participant = GetParticipant(e.TrackUnmuted.ParticipantIdentity);
var publication = participant.Tracks[e.TrackUnmuted.TrackSid];
publication.UpdateMuted(false);
TrackUnmuted?.Invoke(publication, participant);
}
break;
case RoomEvent.MessageOneofCase.ActiveSpeakersChanged:
{
var identities = e.ActiveSpeakersChanged.ParticipantIdentities;
var speakers = new List<Participant>(identities.Count);
foreach (var id in identities)
speakers.Add(GetParticipant(id));
ActiveSpeakersChanged?.Invoke(speakers);
}
break;
case RoomEvent.MessageOneofCase.ConnectionQualityChanged:
{
var participant = GetParticipant(e.ConnectionQualityChanged.ParticipantIdentity);
var quality = e.ConnectionQualityChanged.Quality;
participant.ConnectionQuality = quality;
ConnectionQualityChanged?.Invoke(quality, participant);
}
break;
case RoomEvent.MessageOneofCase.DataPacketReceived:
{
var valueType = e.DataPacketReceived.ValueCase;
switch (valueType)
{
case DataPacketReceived.ValueOneofCase.None:
//do nothing.
break;
case DataPacketReceived.ValueOneofCase.User:
{
var dataInfo = e.DataPacketReceived.User;
var data = new byte[dataInfo.Data.Data.DataLen];
Marshal.Copy((IntPtr)dataInfo.Data.Data.DataPtr, data, 0, data.Length);
#pragma warning disable CS0612 // Type or member is obsolete
var participant = GetParticipant(e.DataPacketReceived.ParticipantIdentity);
#pragma warning restore CS0612 // Type or member is obsolete
DataReceived?.Invoke(data, participant, e.DataPacketReceived.Kind, dataInfo.Topic);
}
break;
case DataPacketReceived.ValueOneofCase.SipDtmf:
{
var dtmfInfo = e.DataPacketReceived.SipDtmf;
#pragma warning disable CS0612 // Type or member is obsolete
var participant = GetParticipant(e.DataPacketReceived.ParticipantIdentity);
#pragma warning restore CS0612 // Type or member is obsolete
SipDtmfReceived?.Invoke(participant, dtmfInfo.Code, dtmfInfo.Digit);
}
break;
}
}
break;
case RoomEvent.MessageOneofCase.ByteStreamOpened:
var byteReader = new ByteStreamReader(e.ByteStreamOpened.Reader);
_streamHandlers.Dispatch(byteReader, e.ByteStreamOpened.ParticipantIdentity);
// TODO: Immediately dispose unhandled stream reader
break;
case RoomEvent.MessageOneofCase.TextStreamOpened:
var textReader = new TextStreamReader(e.TextStreamOpened.Reader);
_streamHandlers.Dispatch(textReader, e.TextStreamOpened.ParticipantIdentity);
// TODO: Immediately dispose unhandled stream reader
break;
case RoomEvent.MessageOneofCase.ConnectionStateChanged:
ConnectionState = e.ConnectionStateChanged.State;
ConnectionStateChanged?.Invoke(e.ConnectionStateChanged.State);
break;
case RoomEvent.MessageOneofCase.Disconnected:
Disconnected?.Invoke(e.Disconnected.Reason);
OnDisconnect();
break;
case RoomEvent.MessageOneofCase.Reconnecting:
Reconnecting?.Invoke(this);
break;
case RoomEvent.MessageOneofCase.Reconnected:
Reconnected?.Invoke(this);
break;
case RoomEvent.MessageOneofCase.E2EeStateChanged:
{
var participant = GetParticipant(e.E2EeStateChanged.ParticipantIdentity);
E2EeStateChanged?.Invoke(participant, e.E2EeStateChanged.State);
}
break;
}
}
internal void OnConnect(ConnectCallback info)
{
RoomHandle = FfiHandle.FromOwnedHandle(info.Result.Room.Handle);
UpdateFromInfo(info.Result.Room.Info);
LocalParticipant = new LocalParticipant(info.Result.LocalParticipant, this);
// Add already connected participant
foreach (var p in info.Result.Participants)
CreateRemoteParticipantWithTracks(p);
FfiClient.Instance.RoomEventReceived += OnEventReceived;
FfiClient.Instance.DisconnectReceived += OnDisconnectReceived;
FfiClient.Instance.RpcMethodInvocationReceived += OnRpcMethodInvocationReceived;
Connected?.Invoke(this);
}
private void OnDisconnectReceived(DisconnectCallback e)
{
OnDisconnect();
Disconnected?.Invoke(DisconnectReason.ClientInitiated);
FfiClient.Instance.DisconnectReceived -= OnDisconnectReceived;
Utils.Debug($"OnDisconnect.... {e}");
}
private void OnDisconnect()
{
FfiClient.Instance.RoomEventReceived -= OnEventReceived;
}
internal RemoteParticipant CreateRemoteParticipantWithTracks(ConnectCallback.Types.ParticipantWithTracks item)
{
var participant = item.Participant;
var publications = item.Publications;
var newParticipant = new RemoteParticipant(participant, this);
_participants.Add(participant.Info.Identity, newParticipant);
foreach (var pub in publications)
{
var publication = new RemoteTrackPublication(pub.Info, FfiHandle.FromOwnedHandle(pub.Handle));
newParticipant._tracks.Add(publication.Sid, publication);
newParticipant.OnTrackPublished(publication);
}
return newParticipant;
}
internal RemoteParticipant CreateRemoteParticipant(OwnedParticipant participant)
{
var newParticipant = new RemoteParticipant(participant, this);
_participants.Add(participant.Info.Identity, newParticipant);
return newParticipant;
}
internal Participant GetParticipant(string identity)
{
if (identity == LocalParticipant.Identity)
return LocalParticipant;
RemoteParticipants.TryGetValue(identity, out var remoteParticipant);
return remoteParticipant;
}
}
public sealed class ConnectInstruction : YieldInstruction
{
private ulong _asyncId;
private Room _room;
private RoomOptions _roomOptions;
internal ConnectInstruction(ulong asyncId, Room room, RoomOptions options)
{
_asyncId = asyncId;
_room = room;
_roomOptions = options;
FfiClient.Instance.ConnectReceived += OnConnect;
}
void OnConnect(ConnectCallback e)
{
if (_asyncId != e.AsyncId)
return;
FfiClient.Instance.ConnectReceived -= OnConnect;
bool success = string.IsNullOrEmpty(e.Error);
if (success)
{
if (_roomOptions.E2EE != null)
{
_room.E2EEManager = new E2EEManager(_room.RoomHandle, _roomOptions.E2EE);
}
_room.OnConnect(e);
}
IsError = !success;
IsDone = true;
}
}
}