Skip to content

Commit

Permalink
fix start/stop track flow
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroshihorie committed Nov 14, 2023
1 parent 2d44fcd commit 6b1aff5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 38 deletions.
24 changes: 10 additions & 14 deletions Sources/LiveKit/Track/Local/LocalVideoTrack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,23 @@ public class LocalVideoTrack: Track, LocalTrack, VideoTrack {
track: rtcTrack)
}

@discardableResult
override public func start() async throws -> Bool {
let didStart = try await super.start()
if didStart { try await capturer.startCapture() }
return didStart
}

@discardableResult
override public func stop() async throws -> Bool {
let didStop = try await super.stop()
if didStop { try await capturer.stopCapture() }
return didStop
}

public func mute() async throws {
try await super._mute()
}

public func unmute() async throws {
try await super._unmute()
}

// MARK: - Internal

override func startCapture() async throws {
try await capturer.startCapture()
}

override func stopCapture() async throws {
try await capturer.stopCapture()
}
}

public extension LocalVideoTrack {
Expand Down
22 changes: 10 additions & 12 deletions Sources/LiveKit/Track/Remote/RemoteAudioTrack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,6 @@ public class RemoteAudioTrack: Track, RemoteTrack, AudioTrack {
track: track)
}

override public func start() async throws -> Bool {
let didStart = try await super.start()
if didStart { AudioManager.shared.trackDidStart(.remote) }
return didStart
}

override public func stop() async throws -> Bool {
let didStop = try await super.stop()
if didStop { AudioManager.shared.trackDidStop(.remote) }
return didStop
}

public func add(audioRenderer: AudioRenderer) {
guard let audioTrack = mediaTrack as? LKRTCAudioTrack else { return }
audioTrack.add(AudioRendererAdapter(target: audioRenderer))
Expand All @@ -63,4 +51,14 @@ public class RemoteAudioTrack: Track, RemoteTrack, AudioTrack {
guard let audioTrack = mediaTrack as? LKRTCAudioTrack else { return }
audioTrack.remove(AudioRendererAdapter(target: audioRenderer))
}

// MARK: - Internal

override func startCapture() async throws {
AudioManager.shared.trackDidStart(.remote)
}

override func stopCapture() async throws {
AudioManager.shared.trackDidStop(.remote)
}
}
36 changes: 24 additions & 12 deletions Sources/LiveKit/Track/Track.swift
Original file line number Diff line number Diff line change
Expand Up @@ -208,24 +208,36 @@ public class Track: NSObject, Loggable {
resumeOrSuspendStatisticsTimer()
}

// Returns true if didStart
func set(trackState: TrackState) {
_state.mutate { $0.trackState = trackState }
}

// Intended for child class to override
func startCapture() async throws {}

// Intended for child class to override
func stopCapture() async throws {}

@objc
@discardableResult
public func start() async throws -> Bool {
guard trackState != .started else { return false }
_state.mutate { $0.trackState = .started }
public final func start() async throws {
guard _state.trackState != .started else {
log("Already started", .warning)
return
}
try await startCapture()
if self is RemoteTrack { try await enable() }
return true
_state.mutate { $0.trackState = .started }
}

// Returns true if didStop
@objc
@discardableResult
public func stop() async throws -> Bool {
guard trackState != .stopped else { return false }
_state.mutate { $0.trackState = .stopped }
public final func stop() async throws {
guard _state.trackState != .stopped else {
log("Already stopped", .warning)
return
}
try await stopCapture()
if self is RemoteTrack { try await disable() }
return true
_state.mutate { $0.trackState = .stopped }
}

// Returns true if didEnable
Expand Down

0 comments on commit 6b1aff5

Please sign in to comment.