Skip to content

Commit

Permalink
Organize delegates (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroshihorie authored Jan 8, 2024
1 parent 98cb93d commit 55085d4
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 139 deletions.
6 changes: 3 additions & 3 deletions Sources/LiveKit/Core/Room+EngineDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ extension Room: EngineDelegate {
}

delegates.notify(label: { "room.didUpdate connectionState: \(state.connectionState) oldValue: \(oldState.connectionState)" }) {
$0.room?(self, didUpdateConnectionState: state.connectionState, oldConnectionState: oldState.connectionState)
$0.room?(self, didUpdateConnectionState: state.connectionState, from: oldState.connectionState)
}

// Individual connectionState delegates
Expand Down Expand Up @@ -166,13 +166,13 @@ extension Room: EngineDelegate {
guard let self else { return }

self.delegates.notify(label: { "room.didReceive data: \(packet.payload)" }) {
$0.room?(self, participant: participant, didReceiveData: packet.payload, topic: packet.topic)
$0.room?(self, participant: participant, didReceiveData: packet.payload, forTopic: packet.topic)
}

if let participant {
participant.delegates.notify(label: { "participant.didReceive data: \(packet.payload)" }) { [weak participant] delegate in
guard let participant else { return }
delegate.participant?(participant, didReceiveData: packet.payload, topic: packet.topic)
delegate.participant?(participant, didReceiveData: packet.payload, forTopic: packet.topic)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/LiveKit/Core/Room+SignalClientDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ extension Room: SignalClientDelegate {
engine.executeIfConnected { [weak self] in
guard let self else { return }

self.delegates.notify(label: { "room.participantDidJoin participant: \(participant)" }) {
$0.room?(self, participantDidJoin: participant)
self.delegates.notify(label: { "room.remoteParticipantDidConnect: \(participant)" }) {
$0.room?(self, participantDidConnect: participant)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/LiveKit/E2EE/E2EEManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ extension E2EEManager {
log("frameCryptor didStateChangeWithParticipantId \(participantId) with state \(state.rawValue)")

room.delegates.notify { delegate in
delegate.room?(room, publication: publication, didUpdateE2EEState: state.toLKType())
delegate.room?(room, track: publication, didUpdateE2EEState: state.toLKType())
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/LiveKit/Participant/LocalParticipant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,10 @@ public class LocalParticipant: Participant {

// Notify didPublish
delegates.notify(label: { "localParticipant.didPublish \(publication)" }) {
$0.localParticipant?(self, didPublishPublication: publication)
$0.participant?(self, didPublishTrack: publication)
}
room.delegates.notify(label: { "localParticipant.didPublish \(publication)" }) {
$0.room?(self.room, localParticipant: self, didPublishPublication: publication)
$0.room?(self.room, participant: self, didPublishTrack: publication)
}

log("[publish] success \(publication)", .info)
Expand Down Expand Up @@ -266,10 +266,10 @@ public class LocalParticipant: Participant {
func _notifyDidUnpublish() async {
guard _notify else { return }
delegates.notify(label: { "localParticipant.didUnpublish \(publication)" }) {
$0.localParticipant?(self, didUnpublishPublication: publication)
$0.participant?(self, didUnpublishTrack: publication)
}
room.delegates.notify(label: { "room.didUnpublish \(publication)" }) {
$0.room?(self.room, localParticipant: self, didUnpublishPublication: publication)
$0.room?(self.room, participant: self, didUnpublishTrack: publication)
}
}

Expand Down
28 changes: 14 additions & 14 deletions Sources/LiveKit/Participant/RemoteParticipant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ public class RemoteParticipant: Participant {

for publication in newTrackPublications.values {
self.delegates.notify(label: { "participant.didPublish \(publication)" }) {
$0.participant?(self, didPublishPublication: publication)
$0.participant?(self, didPublishTrack: publication)
}
self.room.delegates.notify(label: { "room.didPublish \(publication)" }) {
$0.room?(self.room, participant: self, didPublishPublication: publication)
$0.room?(self.room, participant: self, didPublishTrack: publication)
}
}
}
Expand All @@ -89,10 +89,10 @@ public class RemoteParticipant: Participant {
log("Could not subscribe to mediaTrack \(sid), unable to locate track publication. existing sids: (\(_state.trackPublications.keys.joined(separator: ", ")))", .error)
let error = LiveKitError(.invalidState, message: "Could not find published track with sid: \(sid)")
delegates.notify(label: { "participant.didFailToSubscribe trackSid: \(sid)" }) {
$0.participant?(self, didFailToSubscribe: sid, error: error)
$0.participant?(self, didFailToSubscribeTrack: sid, withError: error)
}
room.delegates.notify(label: { "room.didFailToSubscribe trackSid: \(sid)" }) {
$0.room?(self.room, participant: self, didFailToSubscribe: sid, error: error)
$0.room?(self.room, participant: self, didFailToSubscribeTrack: sid, withError: error)
}
throw error
}
Expand All @@ -111,10 +111,10 @@ public class RemoteParticipant: Participant {
default:
let error = LiveKitError(.invalidState, message: "Unsupported type: \(rtcTrack.kind.description)")
delegates.notify(label: { "participant.didFailToSubscribe trackSid: \(sid)" }) {
$0.participant?(self, didFailToSubscribe: sid, error: error)
$0.participant?(self, didFailToSubscribeTrack: sid, withError: error)
}
room.delegates.notify(label: { "room.didFailToSubscribe trackSid: \(sid)" }) {
$0.room?(self.room, participant: self, didFailToSubscribe: sid, error: error)
$0.room?(self.room, participant: self, didFailToSubscribeTrack: sid, withError: error)
}
throw error
}
Expand All @@ -132,18 +132,18 @@ public class RemoteParticipant: Participant {
try await track.start()

delegates.notify(label: { "participant.didSubscribe \(publication)" }) {
$0.participant?(self, didSubscribePublication: publication)
$0.participant?(self, didSubscribeTrack: publication)
}
room.delegates.notify(label: { "room.didSubscribe \(publication)" }) {
$0.room?(self.room, participant: self, didSubscribePublication: publication)
$0.room?(self.room, participant: self, didSubscribeTrack: publication)
}
}

override func cleanUp(notify _notify: Bool = true) async {
await super.cleanUp(notify: _notify)

room.delegates.notify(label: { "room.participantDidLeave" }) {
$0.room?(self.room, participantDidLeave: self)
room.delegates.notify(label: { "room.remoteParticipantDidDisconnect:" }) {
$0.room?(self.room, participantDidDisconnect: self)
}
}

Expand All @@ -163,10 +163,10 @@ public class RemoteParticipant: Participant {
func _notifyUnpublish() async {
guard _notify else { return }
delegates.notify(label: { "participant.didUnpublish \(publication)" }) {
$0.participant?(self, didUnpublishPublication: publication)
$0.participant?(self, didUnpublishTrack: publication)
}
room.delegates.notify(label: { "room.didUnpublish \(publication)" }) {
$0.room?(self.room, participant: self, didUnpublishPublication: publication)
$0.room?(self.room, participant: self, didUnpublishTrack: publication)
}
}

Expand All @@ -183,10 +183,10 @@ public class RemoteParticipant: Participant {
if _notify {
// Notify unsubscribe
delegates.notify(label: { "participant.didUnsubscribe \(publication)" }) {
$0.participant?(self, didUnsubscribePublication: publication)
$0.participant?(self, didUnsubscribeTrack: publication)
}
room.delegates.notify(label: { "room.didUnsubscribe \(publication)" }) {
$0.room?(self.room, participant: self, didUnsubscribePublication: publication)
$0.room?(self.room, participant: self, didUnsubscribeTrack: publication)
}
}

Expand Down
54 changes: 27 additions & 27 deletions Sources/LiveKit/Protocols/ParticipantDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,25 @@ public protocol ParticipantDelegate: AnyObject {

/// A ``Participant``'s metadata has updated.
/// `participant` Can be a ``LocalParticipant`` or a ``RemoteParticipant``.
@objc(participant:didUpdateMetadata:) optional
@objc optional
func participant(_ participant: Participant, didUpdateMetadata metadata: String?)

/// A ``Participant``'s name has updated.
/// `participant` Can be a ``LocalParticipant`` or a ``RemoteParticipant``.
@objc(participant:didUpdateName:) optional
@objc optional
func participant(_ participant: Participant, didUpdateName name: String?)

/// The isSpeaking status of a ``Participant`` has changed.
/// `participant` Can be a ``LocalParticipant`` or a ``RemoteParticipant``.
@objc(participant:didUpdateSpeaking:) optional
@objc optional
func participant(_ participant: Participant, didUpdateIsSpeaking isSpeaking: Bool)

/// The connection quality of a ``Participant`` has updated.
/// `participant` Can be a ``LocalParticipant`` or a ``RemoteParticipant``.
@objc(participant:didUpdateConnectionQuality:) optional
@objc optional
func participant(_ participant: Participant, didUpdateConnectionQuality connectionQuality: ConnectionQuality)

@objc(participant:didUpdatePermissions:) optional
@objc optional
func participant(_ participant: Participant, didUpdatePermissions permissions: ParticipantPermissions)

// MARK: - TrackPublication
Expand All @@ -59,60 +59,60 @@ public protocol ParticipantDelegate: AnyObject {
/// or if the server has requested the participant to be muted.
///
/// `participant` Can be a ``LocalParticipant`` or a ``RemoteParticipant``.
@objc(participant:publication:didUpdateMuted:) optional
func participant(_ participant: Participant, didUpdatePublication publication: TrackPublication, isMuted: Bool)
@objc optional
func participant(_ participant: Participant, track: TrackPublication, didUpdateIsMuted isMuted: Bool)

// MARK: - LocalTrackPublication

/// The ``LocalParticipant`` has published a ``LocalTrackPublication``.
@objc(localParticipant:didPublish:) optional
func localParticipant(_ participant: LocalParticipant, didPublishPublication publication: LocalTrackPublication)
@objc(localParticipant:didPublishTrack:) optional
func participant(_ participant: LocalParticipant, didPublishTrack publication: LocalTrackPublication)

/// The ``LocalParticipant`` has unpublished a ``LocalTrackPublication``.
@objc(localParticipant:didUnpublish:) optional
func localParticipant(_ participant: LocalParticipant, didUnpublishPublication publication: LocalTrackPublication)
@objc(localParticipant:didUnpublishTrack:) optional
func participant(_ participant: LocalParticipant, didUnpublishTrack publication: LocalTrackPublication)

// MARK: - RemoteTrackPublication

/// When a new ``RemoteTrackPublication`` is published to ``Room`` after the ``LocalParticipant`` has joined.
///
/// This delegate method will not be called for tracks that are already published.
@objc(remoteParticipant:didPublish:) optional
func participant(_ participant: RemoteParticipant, didPublishPublication publication: RemoteTrackPublication)
@objc(remoteParticipant:didPublishTrack:) optional
func participant(_ participant: RemoteParticipant, didPublishTrack publication: RemoteTrackPublication)

/// The ``RemoteParticipant`` has unpublished a ``RemoteTrackPublication``.
@objc(remoteParticipant:didUnpublish:) optional
func participant(_ participant: RemoteParticipant, didUnpublishPublication publication: RemoteTrackPublication)
@objc(remoteParticipant:didUnpublishTrack:) optional
func participant(_ participant: RemoteParticipant, didUnpublishTrack publication: RemoteTrackPublication)

/// The ``LocalParticipant`` has subscribed to a new ``RemoteTrackPublication``.
///
/// This event will always fire as long as new tracks are ready for use.
@objc(participant:didSubscribe:) optional
func participant(_ participant: RemoteParticipant, didSubscribePublication publication: RemoteTrackPublication)
@objc optional
func participant(_ participant: RemoteParticipant, didSubscribeTrack publication: RemoteTrackPublication)

/// Unsubscribed from a ``RemoteTrackPublication`` and is no longer available.
///
/// Clients should listen to this event and handle cleanup.
@objc(participant:didUnsubscribePublication:) optional
func participant(_ participant: RemoteParticipant, didUnsubscribePublication publication: RemoteTrackPublication)
@objc optional
func participant(_ participant: RemoteParticipant, didUnsubscribeTrack publication: RemoteTrackPublication)

/// Could not subscribe to a track.
///
/// This is an error state, the subscription can be retried.
@objc(participant:didFailToSubscribeTrackWithSid:error:) optional
func participant(_ participant: RemoteParticipant, didFailToSubscribe trackSid: String, error: LiveKitError)
@objc optional
func participant(_ participant: RemoteParticipant, didFailToSubscribeTrack trackSid: String, withError error: LiveKitError)

/// ``TrackPublication/streamState`` has updated for the ``RemoteTrackPublication``.
@objc(participant:publication:didUpdateStreamState:) optional
func participant(_ participant: RemoteParticipant, didUpdatePublication publication: RemoteTrackPublication, streamState: StreamState)
@objc optional
func participant(_ participant: RemoteParticipant, track: RemoteTrackPublication, didUpdateStreamState streamState: StreamState)

/// ``RemoteTrackPublication/isSubscriptionAllowed`` has updated for the ``RemoteTrackPublication``.
@objc(participant:publication:didUpdateCanSubscribe:) optional
func participant(_ participant: RemoteParticipant, didUpdatePublication publication: RemoteTrackPublication, isSubscriptionAllowed: Bool)
@objc optional
func participant(_ participant: RemoteParticipant, track: RemoteTrackPublication, didUpdateIsSubscriptionAllowed isSubscriptionAllowed: Bool)

// MARK: - Data

/// Data was received from a ``RemoteParticipant``.
@objc(participant:didReceiveData:topic:) optional
func participant(_ participant: RemoteParticipant, didReceiveData data: Data, topic: String)
@objc optional
func participant(_ participant: RemoteParticipant, didReceiveData data: Data, forTopic topic: String)
}
Loading

0 comments on commit 55085d4

Please sign in to comment.