From 60057c4c7b6c0270f6a9db68f1f53a7cc14dc6ef Mon Sep 17 00:00:00 2001 From: Hiroshi Horie <548776+hiroshihorie@users.noreply.github.com> Date: Tue, 3 Sep 2024 23:04:43 +0900 Subject: [PATCH] Local track subscribed event (#467) Fired when the first remote participant has subscribed to the localParticipant's track. --- .../Core/Room+SignalClientDelegate.swift | 18 ++++++++++++++++++ Sources/LiveKit/Core/SignalClient.swift | 4 ++-- .../Protocols/ParticipantDelegate.swift | 4 ++++ Sources/LiveKit/Protocols/RoomDelegate.swift | 4 ++++ .../Protocols/SignalClientDelegate.swift | 1 + 5 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Sources/LiveKit/Core/Room+SignalClientDelegate.swift b/Sources/LiveKit/Core/Room+SignalClientDelegate.swift index db503011c..9fc7bd3de 100644 --- a/Sources/LiveKit/Core/Room+SignalClientDelegate.swift +++ b/Sources/LiveKit/Core/Room+SignalClientDelegate.swift @@ -345,4 +345,22 @@ extension Room: SignalClientDelegate { // update token _state.mutate { $0.token = token } } + + func signalClient(_: SignalClient, didSubscribeTrack trackSid: Track.Sid) async { + // Find the local track publication. + guard let track = localParticipant.trackPublications[trackSid] as? LocalTrackPublication else { + log("Could not find local track publication for subscribed event") + return + } + + // Notify Room. + delegates.notify { + $0.room?(self, participant: self.localParticipant, remoteDidSubscribeTrack: track) + } + + // Notify LocalParticipant. + localParticipant.delegates.notify { + $0.participant?(self.localParticipant, remoteDidSubscribeTrack: track) + } + } } diff --git a/Sources/LiveKit/Core/SignalClient.swift b/Sources/LiveKit/Core/SignalClient.swift index 9492bc3b0..6b3324429 100644 --- a/Sources/LiveKit/Core/SignalClient.swift +++ b/Sources/LiveKit/Core/SignalClient.swift @@ -341,8 +341,8 @@ private extension SignalClient { case .requestResponse: log("Received requestResponse message") - case .trackSubscribed: - log("Received trackSubscribed message") + case let .trackSubscribed(trackSubscribed): + _delegate.notifyDetached { await $0.signalClient(self, didSubscribeTrack: Track.Sid(from: trackSubscribed.trackSid)) } } } } diff --git a/Sources/LiveKit/Protocols/ParticipantDelegate.swift b/Sources/LiveKit/Protocols/ParticipantDelegate.swift index 86a982560..68c6d428e 100644 --- a/Sources/LiveKit/Protocols/ParticipantDelegate.swift +++ b/Sources/LiveKit/Protocols/ParticipantDelegate.swift @@ -79,6 +79,10 @@ public protocol ParticipantDelegate: AnyObject { @objc(localParticipant:didUnpublishTrack:) optional func participant(_ participant: LocalParticipant, didUnpublishTrack publication: LocalTrackPublication) + /// Fired when the first remote participant has subscribed to the localParticipant's track. + @objc(localParticipant:remoteDidSubscribeTrack:) optional + func participant(_ participant: LocalParticipant, remoteDidSubscribeTrack publication: LocalTrackPublication) + // MARK: - RemoteTrackPublication /// When a new ``RemoteTrackPublication`` is published to ``Room`` after the ``LocalParticipant`` has joined. diff --git a/Sources/LiveKit/Protocols/RoomDelegate.swift b/Sources/LiveKit/Protocols/RoomDelegate.swift index 5e1b04183..9b17eb70c 100644 --- a/Sources/LiveKit/Protocols/RoomDelegate.swift +++ b/Sources/LiveKit/Protocols/RoomDelegate.swift @@ -120,6 +120,10 @@ public protocol RoomDelegate: AnyObject { @objc(room:localParticipant:didUnpublishTrack:) optional func room(_ room: Room, participant: LocalParticipant, didUnpublishTrack publication: LocalTrackPublication) + /// Fired when the first remote participant has subscribed to the localParticipant's track. + @objc(room:localParticipant:remoteDidSubscribeTrack:) optional + func room(_ room: Room, participant: LocalParticipant, remoteDidSubscribeTrack publication: LocalTrackPublication) + /// A ``RemoteParticipant`` has un-published a ``RemoteTrack``. @objc(room:remoteParticipant:didUnpublishTrack:) optional func room(_ room: Room, participant: RemoteParticipant, didUnpublishTrack publication: RemoteTrackPublication) diff --git a/Sources/LiveKit/Protocols/SignalClientDelegate.swift b/Sources/LiveKit/Protocols/SignalClientDelegate.swift index 35d9a62b5..bd97afcd1 100644 --- a/Sources/LiveKit/Protocols/SignalClientDelegate.swift +++ b/Sources/LiveKit/Protocols/SignalClientDelegate.swift @@ -39,4 +39,5 @@ protocol SignalClientDelegate: AnyObject { func signalClient(_ signalClient: SignalClient, didUpdateSubscriptionPermission permission: Livekit_SubscriptionPermissionUpdate) async func signalClient(_ signalClient: SignalClient, didUpdateToken token: String) async func signalClient(_ signalClient: SignalClient, didReceiveLeave canReconnect: Bool, reason: Livekit_DisconnectReason) async + func signalClient(_ signalClient: SignalClient, didSubscribeTrack trackSid: Track.Sid) async }