From 98eb5583bdafdb6130458d82ee8730536feb1a34 Mon Sep 17 00:00:00 2001 From: Mohelm97 Date: Mon, 22 Jul 2024 13:21:52 +0300 Subject: [PATCH] Add UnpublishTrack to local participant, fix LocalTrackPublished event (#44) --- Runtime/Scripts/Internal/FFIClient.cs | 4 ++ .../Scripts/Internal/FFIClients/FFIEvents.cs | 3 ++ Runtime/Scripts/Participant.cs | 48 +++++++++++++++++-- Runtime/Scripts/Room.cs | 6 +++ Runtime/Scripts/Track.cs | 8 +++- Runtime/csc.rsp | 3 +- 6 files changed, 66 insertions(+), 6 deletions(-) diff --git a/Runtime/Scripts/Internal/FFIClient.cs b/Runtime/Scripts/Internal/FFIClient.cs index 5ce2666..5017f1f 100644 --- a/Runtime/Scripts/Internal/FFIClient.cs +++ b/Runtime/Scripts/Internal/FFIClient.cs @@ -32,6 +32,7 @@ internal sealed class FfiClient : IFFIClient private readonly IMemoryPool memoryPool; public event PublishTrackDelegate? PublishTrackReceived; + public event UnpublishTrackDelegate? UnpublishTrackReceived; public event ConnectReceivedDelegate? ConnectReceived; public event DisconnectReceivedDelegate? DisconnectReceived; public event RoomEventReceivedDelegate? RoomEventReceived; @@ -228,6 +229,9 @@ static unsafe void FFICallback(UIntPtr data, UIntPtr size) case FfiEvent.MessageOneofCase.PublishTrack: Instance.PublishTrackReceived?.Invoke(r.PublishTrack!); break; + case FfiEvent.MessageOneofCase.UnpublishTrack: + Instance.UnpublishTrackReceived?.Invoke(r.UnpublishTrack!); + break; case FfiEvent.MessageOneofCase.RoomEvent: Instance.RoomEventReceived?.Invoke(r.RoomEvent); break; diff --git a/Runtime/Scripts/Internal/FFIClients/FFIEvents.cs b/Runtime/Scripts/Internal/FFIClients/FFIEvents.cs index de4ca1c..3f67840 100644 --- a/Runtime/Scripts/Internal/FFIClients/FFIEvents.cs +++ b/Runtime/Scripts/Internal/FFIClients/FFIEvents.cs @@ -12,6 +12,9 @@ namespace LiveKit.Internal internal delegate void PublishTrackDelegate(PublishTrackCallback e); + internal delegate void UnpublishTrackDelegate(UnpublishTrackCallback e); + + internal delegate void ConnectReceivedDelegate(ConnectCallback e); diff --git a/Runtime/Scripts/Participant.cs b/Runtime/Scripts/Participant.cs index 2bf44e8..4357ce4 100644 --- a/Runtime/Scripts/Participant.cs +++ b/Runtime/Scripts/Participant.cs @@ -72,7 +72,7 @@ public PublishTrackInstruction PublishTrack(ILocalTrack localTrack, TrackPublish if (!Room.TryGetTarget(out var room)) throw new Exception("room is invalid"); - var track = (Track)localTrack; + var track = (Track)localTrack; using var request = FFIBridge.Instance.NewRequest(); var publish = request.request; @@ -81,7 +81,23 @@ public PublishTrackInstruction PublishTrack(ILocalTrack localTrack, TrackPublish publish.Options = options; using var response = request.Send(); FfiResponse res = response; - return new PublishTrackInstruction(res.PublishTrack.AsyncId); + return new PublishTrackInstruction(res.PublishTrack.AsyncId, localTrack, _tracks); + } + + public UnpublishTrackInstruction UnpublishTrack(ILocalTrack localTrack, bool stopOnUnpublish) + { + if (!Room.TryGetTarget(out var room)) + throw new Exception("room is invalid"); + + using var request = FFIBridge.Instance.NewRequest(); + var unpublish = request.request; + unpublish.LocalParticipantHandle = (ulong) Handle.DangerousGetHandle(); + unpublish.StopOnUnpublish = false; + unpublish.TrackSid = localTrack.Sid; + using var response = request.Send(); + FfiResponse res = response; + _tracks.Remove (localTrack.Sid); + return new UnpublishTrackInstruction(res.UnpublishTrack.AsyncId); } public void PublishData(byte[] data, IReadOnlyCollection destination_identities = null, bool reliable = true, string topic = null) @@ -156,10 +172,14 @@ internal RemoteParticipant(OwnedParticipant participant, Room room) : base(parti public sealed class PublishTrackInstruction : YieldInstruction { private ulong _asyncId; + private Dictionary _internalTracks; + private ILocalTrack _localTrack; - internal PublishTrackInstruction(ulong asyncId) + internal PublishTrackInstruction(ulong asyncId, ILocalTrack localTrack, Dictionary internalTracks) { _asyncId = asyncId; + _internalTracks = internalTracks; + _localTrack = localTrack; FfiClient.Instance.PublishTrackReceived += OnPublish; } @@ -170,7 +190,29 @@ internal void OnPublish(PublishTrackCallback e) IsError = !string.IsNullOrEmpty(e.Error); IsDone = true; + var publication = new LocalTrackPublication (e.Publication.Info); + publication.UpdateTrack (_localTrack as Track); + _localTrack.UpdateSid(publication.Sid); + _internalTracks.Add (e.Publication.Info.Sid, publication); FfiClient.Instance.PublishTrackReceived -= OnPublish; } } + public sealed class UnpublishTrackInstruction : YieldInstruction + { + private ulong _asyncId; + + internal UnpublishTrackInstruction(ulong asyncId) { + _asyncId = asyncId; + FfiClient.Instance.UnpublishTrackReceived += OnUnpublish; + } + + internal void OnUnpublish(UnpublishTrackCallback e) { + if (e.AsyncId != _asyncId) + return; + + IsError = !string.IsNullOrEmpty(e.Error); + IsDone = true; + FfiClient.Instance.UnpublishTrackReceived -= OnUnpublish; + } + } } diff --git a/Runtime/Scripts/Room.cs b/Runtime/Scripts/Room.cs index 2dcc768..73cbf17 100644 --- a/Runtime/Scripts/Room.cs +++ b/Runtime/Scripts/Room.cs @@ -169,6 +169,8 @@ public ConnectInstruction Connect(string url, string token, RoomOptions options) public void Disconnect() { + if (this.RoomHandle == null) + return; using var response = FFIBridge.Instance.SendDisconnectRequest(this); Utils.Debug($"Disconnect.... {RoomHandle}"); FfiResponse resp = response; @@ -356,14 +358,18 @@ internal void OnEventReceived(RoomEvent e) 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.ParticipantSid); +#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.ParticipantSid); +#pragma warning restore CS0612 // Type or member is obsolete SipDtmfReceived?.Invoke(participant, dtmfInfo.Code, dtmfInfo.Digit); } break; diff --git a/Runtime/Scripts/Track.cs b/Runtime/Scripts/Track.cs index 57a3af3..34e4f0b 100644 --- a/Runtime/Scripts/Track.cs +++ b/Runtime/Scripts/Track.cs @@ -7,7 +7,7 @@ namespace LiveKit { public interface ITrack { - string Sid { get; } + string Sid { get; protected set; } string Name { get; } TrackKind Kind { get; } StreamState StreamState { get; } @@ -19,7 +19,9 @@ public interface ITrack public interface ILocalTrack : ITrack { - + public void UpdateSid (string sid) { + Sid = sid; + } } public interface IRemoteTrack : ITrack @@ -55,6 +57,8 @@ public class Track : ITrack FfiHandle ITrack.TrackHandle => Handle; + string ITrack.Sid { get => _info.Sid; set => _info.Sid = value; } + internal Track(OwnedTrack track, Room room, Participant participant) { Handle = FfiHandle.FromOwnedHandle(track.Handle); diff --git a/Runtime/csc.rsp b/Runtime/csc.rsp index a0c7129..134a86e 100644 --- a/Runtime/csc.rsp +++ b/Runtime/csc.rsp @@ -1 +1,2 @@ --nullable:enable \ No newline at end of file +-nullable:disable +-nowarn:8632 \ No newline at end of file