Skip to content

Commit

Permalink
Merge pull request #42 from DolbyIO/bugfix/rts-core-issues
Browse files Browse the repository at this point in the history
Bugfix/rts core issues
  • Loading branch information
aravind-raveendran authored Oct 19, 2023
2 parents a4b7879 + 71dddbf commit 28b7924
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 22 deletions.
6 changes: 5 additions & 1 deletion Sources/DolbyIORTSCore/Builder/StreamSourceBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ final class StreamSourceBuilder {
streamingStatistics = statistics
}

func build() throws -> StreamSource {
func build(isAudioEnabled: Bool) throws -> StreamSource {
if isAudioEnabled, hasMissingAudioTrack {
throw BuildError.missingAudioTrack
}

guard !hasMissingVideoTrack, let videoTrack = videoTrack else {
throw BuildError.missingVideoTrack
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/DolbyIORTSCore/Model/StreamSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public struct StreamSource: Equatable, Hashable, Identifiable {
case main
case other(sourceId: String)

init(id: String?) {
public init(id: String?) {
switch id {
case .none, .some(""):
self = .main
Expand Down
29 changes: 17 additions & 12 deletions Sources/DolbyIORTSCore/State/State.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,18 @@ struct SubscribedState {
private(set) var streamingStats: AllStreamStatistics?
private(set) var cachedSourceZeroVideoTrackAndMid: VideoTrackAndMid?
private(set) var cachedSourceZeroAudioTrackAndMid: AudioTrackAndMid?
private(set) var configuration: SubscriptionConfiguration

init(cachedVideoTrackDetail: VideoTrackAndMid?, cachedAudioTrackDetail: AudioTrackAndMid?) {
init(cachedVideoTrackDetail: VideoTrackAndMid?, cachedAudioTrackDetail: AudioTrackAndMid?, configuration: SubscriptionConfiguration) {
cachedSourceZeroVideoTrackAndMid = cachedVideoTrackDetail
cachedSourceZeroAudioTrackAndMid = cachedAudioTrackDetail
self.configuration = configuration
streamSourceBuilders = []
numberOfStreamViewers = 0
}

mutating func add(streamId: String, sourceId: String?, tracks: [String]) {
let streamSourceBuilder = StreamSourceBuilder.init(streamId: streamId, sourceId: sourceId, tracks: tracks)
let streamSourceBuilder = StreamSourceBuilder(streamId: streamId, sourceId: sourceId, tracks: tracks)
if let videoTrackAndMid = cachedSourceZeroVideoTrackAndMid {
streamSourceBuilder.addVideoTrack(videoTrackAndMid.videoTrack, mid: videoTrackAndMid.mid)
cachedSourceZeroVideoTrackAndMid = nil
Expand All @@ -73,10 +75,13 @@ struct SubscribedState {
}

mutating func remove(streamId: String, sourceId: String?) {
streamSourceBuilders.removeAll { $0.streamId == streamId && $0.sourceId.value == sourceId }
streamSourceBuilders.removeAll { $0.streamId == streamId && $0.sourceId == StreamSource.SourceId(id: sourceId) }
}

func addAudioTrack(_ track: MCAudioTrack, mid: String) {
guard !configuration.disableAudio else {
return
}
guard let builder = streamSourceBuilders.first(where: { $0.hasMissingAudioTrack}) else {
return
}
Expand All @@ -90,23 +95,23 @@ struct SubscribedState {
builder.addVideoTrack(track, mid: mid)
}

mutating func removeBuilder(with sourceId: String?) {
guard let indexToRemove = streamSourceBuilders.firstIndex(where: { $0.sourceId.value == sourceId }) else {
mutating func removeBuilder(with sourceId: StreamSource.SourceId) {
guard let indexToRemove = streamSourceBuilders.firstIndex(where: { $0.sourceId == sourceId }) else {
return
}

streamSourceBuilders.remove(at: indexToRemove)
}

func setPlayingAudio(_ enable: Bool, for sourceId: String?) {
guard let builder = streamSourceBuilders.first(where: { $0.sourceId.value == sourceId }) else {
func setPlayingAudio(_ enable: Bool, for sourceId: StreamSource.SourceId) {
guard let builder = streamSourceBuilders.first(where: { $0.sourceId == sourceId }) else {
return
}
builder.setPlayingAudio(enable)
}

func setPlayingVideo(_ enable: Bool, for sourceId: String?) {
guard let builder = streamSourceBuilders.first(where: { $0.sourceId.value == sourceId }) else {
func setPlayingVideo(_ enable: Bool, for sourceId: StreamSource.SourceId) {
guard let builder = streamSourceBuilders.first(where: { $0.sourceId == sourceId }) else {
return
}
builder.setPlayingVideo(enable)
Expand All @@ -120,8 +125,8 @@ struct SubscribedState {
builder.setAvailableVideoQualityList(list)
}

func setSelectedVideoQuality(_ videoQuality: VideoQuality, for sourceId: String?) {
guard let builder = streamSourceBuilders.first(where: { $0.sourceId.value == sourceId }) else {
func setSelectedVideoQuality(_ videoQuality: VideoQuality, for sourceId: StreamSource.SourceId) {
guard let builder = streamSourceBuilders.first(where: { $0.sourceId == sourceId }) else {
return
}
builder.setSelectedVideoQuality(videoQuality)
Expand Down Expand Up @@ -151,7 +156,7 @@ struct SubscribedState {
var sources: [StreamSource] {
streamSourceBuilders.compactMap {
do {
return try $0.build()
return try $0.build(isAudioEnabled: !configuration.disableAudio)
} catch {
return nil
}
Expand Down
14 changes: 9 additions & 5 deletions Sources/DolbyIORTSCore/State/StateMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ final class StateMachine {
lazy var statePublisher: AnyPublisher<State, Never> = stateSubject.eraseToAnyPublisher()
private(set) var cachedSourceZeroVideoTrackAndMid: VideoTrackAndMid?
private(set) var cachedSourceZeroAudioTrackAndMid: AudioTrackAndMid?
private(set) var configuration: SubscriptionConfiguration

init(initialState: State) {
currentState = initialState
configuration = .init()
}

func startConnection(streamName: String, accountID: String) {
func startConnection(streamName: String, accountID: String, configuration: SubscriptionConfiguration) {
self.configuration = configuration
currentState = .connecting
}

Expand All @@ -41,7 +44,7 @@ final class StateMachine {
func setPlayingAudio(_ enable: Bool, for source: StreamSource) {
switch currentState {
case let .subscribed(state):
state.setPlayingAudio(enable, for: source.sourceId.value)
state.setPlayingAudio(enable, for: source.sourceId)
currentState = .subscribed(state)
default:
Self.logger.error("🛑 Unexpected state on setPlayingAudio - \(self.currentState.description)")
Expand All @@ -51,7 +54,7 @@ final class StateMachine {
func setPlayingVideo(_ enable: Bool, for source: StreamSource) {
switch currentState {
case let .subscribed(state):
state.setPlayingVideo(enable, for: source.sourceId.value)
state.setPlayingVideo(enable, for: source.sourceId)
currentState = .subscribed(state)
default:
Self.logger.error("🛑 Unexpected state on setPlayingVideo - \(self.currentState.description)")
Expand All @@ -77,7 +80,8 @@ final class StateMachine {
currentState = .subscribed(
.init(
cachedVideoTrackDetail: cachedSourceZeroVideoTrackAndMid,
cachedAudioTrackDetail: cachedSourceZeroAudioTrackAndMid
cachedAudioTrackDetail: cachedSourceZeroAudioTrackAndMid,
configuration: configuration
)
)
cachedSourceZeroAudioTrackAndMid = nil
Expand Down Expand Up @@ -179,7 +183,7 @@ final class StateMachine {
func selectVideoQuality(_ quality: VideoQuality, for source: StreamSource) {
switch currentState {
case let .subscribed(state):
state.setSelectedVideoQuality(quality, for: source.sourceId.value)
state.setSelectedVideoQuality(quality, for: source.sourceId)
currentState = .subscribed(state)
default:
Self.logger.error("🛑 Unexpected state on selectVideoQuality - \(self.currentState.description)")
Expand Down
6 changes: 5 additions & 1 deletion Sources/DolbyIORTSCore/StreamOrchestrator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ public final actor StreamOrchestrator {
Self.logger.debug("👮‍♂️ Start subscribe")
logHandler.setLogFilePath(filePath: configuration.sdkLogPath)

async let startConnectionStateUpdate: Void = stateMachine.startConnection(streamName: streamName, accountID: accountID)
async let startConnectionStateUpdate: Void = stateMachine.startConnection(
streamName: streamName,
accountID: accountID,
configuration: configuration
)
async let startConnection = subscriptionManager.connect(streamName: streamName, accountID: accountID, configuration: configuration)

let (_, connectionResult) = await (startConnectionStateUpdate, startConnection)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ final class StreamViewModel: ObservableObject {
selectedVideoSource = sources.first { $0.id == currentlySelectedVideoSource.id } ?? sortedSources[0]
}

let selectedAudioSource = audioSelection(from: sortedSources, settings: settings, selectedVideoSource: selectedVideoSource)
let selectedAudioSource = audioSelection(from: sources, settings: settings, selectedVideoSource: selectedVideoSource)

let displayMode: DisplayMode
switch settings.multiviewLayout {
Expand Down Expand Up @@ -426,7 +426,7 @@ final class StreamViewModel: ObservableObject {
// Use audio from the video source, if no audio track uses the last one used or just the 1st one
selectedAudioSource = selectedVideoSource.audioTracksCount > 0 ? selectedVideoSource : internalState.selectedAudioSource
case let .source(sourceId: sourceId):
selectedAudioSource = sourcesWithAudio.first(where: { $0.sourceId.value == sourceId }) ?? sourcesWithAudio[0]
selectedAudioSource = sourcesWithAudio.first(where: { $0.sourceId == StreamSource.SourceId(id: sourceId) }) ?? sourcesWithAudio[0]
}
return selectedAudioSource
}
Expand Down

0 comments on commit 28b7924

Please sign in to comment.