Skip to content

Commit

Permalink
update TrackDelegate
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroshihorie committed Dec 18, 2023
1 parent 30cf935 commit a0b27d0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 33 deletions.
17 changes: 4 additions & 13 deletions Sources/LiveKit/Protocols/TrackDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,17 @@ import Foundation
public protocol TrackDelegate: AnyObject {
/// Dimensions of the video track has updated
@objc(track:didUpdateDimensions:) optional
func track(_ track: VideoTrack, didUpdate dimensions: Dimensions?)

/// A ``VideoView`` was attached to the ``VideoTrack``
@objc optional
func track(_ track: VideoTrack, didAttach videoView: VideoView)

/// A ``VideoView`` was detached from the ``VideoTrack``
@objc optional
func track(_ track: VideoTrack, didDetach videoView: VideoView)

/// ``Track/isMuted`` has updated.
@objc(track:didUpdateMuted:shouldSendSignal:) optional
func track(_ track: Track, didUpdate muted: Bool, shouldSendSignal: Bool)
func track(_ track: VideoTrack, didUpdateDimensions dimensions: Dimensions?)

/// Statistics for the track has been generated (v2).
@objc(track:didUpdateStatistics:simulcastStatistics:) optional
func track(_ track: Track, didUpdateStatistics: TrackStatistics, simulcastStatistics: [VideoCodec: TrackStatistics])
}

protocol TrackDelegateInternal: TrackDelegate {
/// Notify RemoteTrackPublication to send isMuted state to server.
func track(_ track: Track, didUpdateIsMuted isMuted: Bool, shouldSendSignal: Bool)

/// Used to report track state mutation to TrackPublication if attached.
func track(_ track: Track, didMutateState newState: Track.State, oldState: Track.State)
}
10 changes: 6 additions & 4 deletions Sources/LiveKit/Track/Track.swift
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,10 @@ public class Track: NSObject, Loggable {
}

if _notify {
delegates.notify(label: { "track.didUpdate muted: \(newValue)" }) {
$0.track?(self, didUpdate: newValue, shouldSendSignal: shouldSendSignal)
delegates.notify(label: { "track.didUpdateIsMuted: \(newValue)" }) { delegate in
if let delegate = delegate as? TrackDelegateInternal {
delegate.track(self, didUpdateIsMuted: newValue, shouldSendSignal: shouldSendSignal)
}
}
}
}
Expand Down Expand Up @@ -320,8 +322,8 @@ extension Track {
_state.mutate { $0.dimensions = newValue }

guard let videoTrack = self as? VideoTrack else { return true }
delegates.notify(label: { "track.didUpdate dimensions: \(newValue == nil ? "nil" : String(describing: newValue))" }) {
$0.track?(videoTrack, didUpdate: newValue)
delegates.notify(label: { "track.didUpdateDimensions: \(newValue == nil ? "nil" : String(describing: newValue))" }) {
$0.track?(videoTrack, didUpdateDimensions: newValue)
}

return true
Expand Down
8 changes: 4 additions & 4 deletions Sources/LiveKit/TrackPublications/TrackPublication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,18 +205,18 @@ extension TrackPublication: TrackDelegateInternal {
}
}

public func track(_: Track, didUpdate muted: Bool, shouldSendSignal: Bool) {
log("isMuted: \(muted) shouldSendSignal: \(shouldSendSignal)")
public func track(_: Track, didUpdateIsMuted isMuted: Bool, shouldSendSignal: Bool) {
log("isMuted: \(isMuted) shouldSendSignal: \(shouldSendSignal)")

Task {
let participant = try await requireParticipant()

if shouldSendSignal {
try await participant.room.engine.signalClient.sendMuteTrack(trackSid: sid, muted: muted)
try await participant.room.engine.signalClient.sendMuteTrack(trackSid: sid, muted: isMuted)
}

participant.delegates.notify {
$0.participant?(participant, didUpdatePublication: self, isMuted: muted)
$0.participant?(participant, didUpdatePublication: self, isMuted: isMuted)
}
participant.room.delegates.notify {
$0.room?(participant.room, participant: participant, didUpdatePublication: self, isMuted: self.isMuted)
Expand Down
12 changes: 0 additions & 12 deletions Sources/LiveKit/Views/VideoView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,6 @@ public class VideoView: NativeView, Loggable {
if let localTrack = track as? LocalVideoTrack {
localTrack.capturer.remove(delegate: self)
}

// notify detach
track.delegates.notify(label: { "track.didDetach videoView: \(self)" }) { [weak self, weak track] delegate in
guard let self, let track else { return }
delegate.track?(track, didDetach: self)
}
}

// set new track
Expand All @@ -265,12 +259,6 @@ public class VideoView: NativeView, Loggable {
if let localTrack = track as? LocalVideoTrack {
localTrack.capturer.add(delegate: self)
}

// notify attach
track.delegates.notify(label: { "track.didAttach videoView: \(self)" }) { [weak self, weak track] delegate in
guard let self, let track else { return }
delegate.track?(track, didAttach: self)
}
}
}

Expand Down

0 comments on commit a0b27d0

Please sign in to comment.