Skip to content

Commit

Permalink
refactor internal engine delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroshihorie committed Dec 17, 2023
1 parent 66bd743 commit 3eb2bf4
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Sources/LiveKit/Core/Engine+TransportDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ extension Engine: TransportDelegate {

func transport(_ transport: Transport, didRemove track: LKRTCMediaStreamTrack) {
if transport.target == .subscriber {
notify { $0.engine(self, didRemove: track) }
notify { $0.engine(self, didRemoveTrack: track) }
}
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/LiveKit/Core/Engine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ class Engine: MulticastDelegate<EngineDelegate> {
lazy var subscriberDataChannel: DataChannelPairActor = .init(onDataPacket: { [weak self] dataPacket in
guard let self else { return }
switch dataPacket.value {
case let .speaker(update): self.notify { $0.engine(self, didUpdate: update.speakers) }
case let .user(userPacket): self.notify { $0.engine(self, didReceive: userPacket) }
case let .speaker(update): self.notify { $0.engine(self, didUpdateSpeakers: update.speakers) }
case let .user(userPacket): self.notify { $0.engine(self, didReceiveUserPacket: userPacket) }
default: return
}
})
Expand Down Expand Up @@ -102,7 +102,7 @@ class Engine: MulticastDelegate<EngineDelegate> {
self.log("connectionState: \(oldState.connectionState) -> \(newState.connectionState), reconnectMode: \(String(describing: newState.reconnectMode))")
}

self.notify { $0.engine(self, didMutate: newState, oldState: oldState) }
self.notify { $0.engine(self, didMutateState: newState, oldState: oldState) }

// execution control
self._blockProcessQueue.async { [weak self] in
Expand Down
18 changes: 9 additions & 9 deletions Sources/LiveKit/Core/Room+EngineDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Foundation
@_implementationOnly import WebRTC

extension Room: EngineDelegate {
func engine(_: Engine, didMutate state: Engine.State, oldState: Engine.State) {
func engine(_: Engine, didMutateState state: Engine.State, oldState: Engine.State) {
if state.connectionState != oldState.connectionState {
// connectionState did update

Expand Down Expand Up @@ -69,7 +69,7 @@ extension Room: EngineDelegate {
}
}

func engine(_ engine: Engine, didUpdate speakers: [Livekit_SpeakerInfo]) {
func engine(_ engine: Engine, didUpdateSpeakers speakers: [Livekit_SpeakerInfo]) {
let activeSpeakers = _state.mutate { state -> [Participant] in

var activeSpeakers: [Participant] = []
Expand Down Expand Up @@ -153,28 +153,28 @@ extension Room: EngineDelegate {
}
}

func engine(_: Engine, didRemove track: LKRTCMediaStreamTrack) {
func engine(_: Engine, didRemoveTrack track: LKRTCMediaStreamTrack) {
// find the publication
guard let publication = _state.remoteParticipants.values.map(\._state.trackPublications.values).joined()
.first(where: { $0.sid == track.trackId }) else { return }
publication.set(track: nil)
}

func engine(_ engine: Engine, didReceive userPacket: Livekit_UserPacket) {
func engine(_ engine: Engine, didReceiveUserPacket packet: Livekit_UserPacket) {
// participant could be null if data broadcasted from server
let participant = _state.remoteParticipants[userPacket.participantIdentity]
let participant = _state.remoteParticipants[packet.participantIdentity]

engine.executeIfConnected { [weak self] in
guard let self else { return }

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

if let participant {
participant.delegates.notify(label: { "participant.didReceive data: \(userPacket.payload)" }) { [weak participant] delegate in
participant.delegates.notify(label: { "participant.didReceive data: \(packet.payload)" }) { [weak participant] delegate in
guard let participant else { return }
delegate.participant?(participant, didReceiveData: userPacket.payload, topic: userPacket.topic)
delegate.participant?(participant, didReceiveData: packet.payload, topic: packet.topic)
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/LiveKit/Protocols/EngineDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import Foundation
@_implementationOnly import WebRTC

protocol EngineDelegate: AnyObject {
func engine(_ engine: Engine, didMutate state: Engine.State, oldState: Engine.State)
func engine(_ engine: Engine, didUpdate speakers: [Livekit_SpeakerInfo])
func engine(_ engine: Engine, didMutateState state: Engine.State, oldState: Engine.State)
func engine(_ engine: Engine, didUpdateSpeakers speakers: [Livekit_SpeakerInfo])
func engine(_ engine: Engine, didAddTrack track: LKRTCMediaStreamTrack, rtpReceiver: LKRTCRtpReceiver, streams: [LKRTCMediaStream])
func engine(_ engine: Engine, didRemove track: LKRTCMediaStreamTrack)
func engine(_ engine: Engine, didReceive userPacket: Livekit_UserPacket)
func engine(_ engine: Engine, didRemoveTrack track: LKRTCMediaStreamTrack)
func engine(_ engine: Engine, didReceiveUserPacket packet: Livekit_UserPacket)
}

0 comments on commit 3eb2bf4

Please sign in to comment.