Skip to content

Commit

Permalink
Add UnpublishTrack to local participant, fix LocalTrackPublished event (
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohelm97 authored Jul 22, 2024
1 parent da21284 commit 98eb558
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Runtime/Scripts/Internal/FFIClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions Runtime/Scripts/Internal/FFIClients/FFIEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);


Expand Down
48 changes: 45 additions & 3 deletions Runtime/Scripts/Participant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PublishTrackRequest>();
var publish = request.request;
Expand All @@ -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<UnpublishTrackRequest>();
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<string> destination_identities = null, bool reliable = true, string topic = null)
Expand Down Expand Up @@ -156,10 +172,14 @@ internal RemoteParticipant(OwnedParticipant participant, Room room) : base(parti
public sealed class PublishTrackInstruction : YieldInstruction
{
private ulong _asyncId;
private Dictionary<string, TrackPublication> _internalTracks;
private ILocalTrack _localTrack;

internal PublishTrackInstruction(ulong asyncId)
internal PublishTrackInstruction(ulong asyncId, ILocalTrack localTrack, Dictionary<string, TrackPublication> internalTracks)
{
_asyncId = asyncId;
_internalTracks = internalTracks;
_localTrack = localTrack;
FfiClient.Instance.PublishTrackReceived += OnPublish;
}

Expand All @@ -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;
}
}
}
6 changes: 6 additions & 0 deletions Runtime/Scripts/Room.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 6 additions & 2 deletions Runtime/Scripts/Track.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -19,7 +19,9 @@ public interface ITrack

public interface ILocalTrack : ITrack
{

public void UpdateSid (string sid) {
Sid = sid;
}
}

public interface IRemoteTrack : ITrack
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion Runtime/csc.rsp
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
-nullable:enable
-nullable:disable
-nowarn:8632

0 comments on commit 98eb558

Please sign in to comment.