Skip to content

Commit

Permalink
Update publishOptions event handling to align with SFU
Browse files Browse the repository at this point in the history
  • Loading branch information
ipavlidakis committed Dec 20, 2024
1 parent 8a2a853 commit a5502a9
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import StreamWebRTC
/// updating of local audio tracks within a WebRTC session. It integrates
/// with WebRTC components and supports features like muting, quality updates,
/// and SFU communication.
final class LocalAudioMediaAdapter: LocalMediaAdapting {
final class LocalAudioMediaAdapter: LocalMediaAdapting, @unchecked Sendable {

/// The audio recorder for capturing audio during the call session.
@Injected(\.callAudioRecorder) private var audioRecorder
Expand Down Expand Up @@ -41,6 +41,8 @@ final class LocalAudioMediaAdapter: LocalMediaAdapting {
/// The last applied audio call settings.
private var lastUpdatedCallSettings: CallSettings.Audio?

private let processingQueue = SerialActorQueue()

/// The primary audio track for this adapter.
let primaryTrack: RTCAudioTrack

Expand Down Expand Up @@ -209,30 +211,34 @@ final class LocalAudioMediaAdapter: LocalMediaAdapting {
func didUpdatePublishOptions(
_ publishOptions: PublishOptions
) async throws {
guard primaryTrack.isEnabled else { return }
processingQueue.async { [weak self] in
guard let self else { return }

self.publishOptions = publishOptions.audio
self.publishOptions = publishOptions.audio

for option in self.publishOptions {
addTransceiverIfRequired(
for: option,
with: primaryTrack.clone(from: peerConnectionFactory)
)
}
guard primaryTrack.isEnabled else { return }

let activePublishOptions = Set(self.publishOptions)
for option in self.publishOptions {
addTransceiverIfRequired(
for: option,
with: primaryTrack.clone(from: peerConnectionFactory)
)
}

transceiverStorage
.forEach { $0.value.sender.track?.isEnabled = activePublishOptions.contains($0.key) }
let activePublishOptions = Set(self.publishOptions)

log.debug(
"""
Local audio tracks updated:
PublishOptions: \(self.publishOptions)
TransceiverStorage: \(transceiverStorage)
""",
subsystems: .webRTC
)
transceiverStorage
.forEach { $0.value.sender.track?.isEnabled = activePublishOptions.contains($0.key) }

log.debug(
"""
Local audio tracks updated:
PublishOptions: \(self.publishOptions)
TransceiverStorage: \(transceiverStorage)
""",
subsystems: .webRTC
)
}
}

/// Returns track information for the local audio tracks.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ final class LocalScreenShareMediaAdapter: LocalMediaAdapting, @unchecked Sendabl
/// Storage for managing transceivers associated with the screen sharing session.
private let transceiverStorage = MediaTransceiverStorage<PublishOptions.VideoPublishOptions>(for: .screenshare)

private let processingQueue = SerialActorQueue()

/// The primary video track used for screen sharing.
let primaryTrack: RTCVideoTrack

Expand Down Expand Up @@ -223,36 +225,40 @@ final class LocalScreenShareMediaAdapter: LocalMediaAdapting, @unchecked Sendabl
func didUpdatePublishOptions(
_ publishOptions: PublishOptions
) async throws {
guard
primaryTrack.isEnabled,
let activeSession = screenShareSessionProvider.activeSession
else { return }
processingQueue.async { [weak self] in
guard let self else { return }

self.publishOptions = publishOptions.screenShare
self.publishOptions = publishOptions.screenShare

for publishOption in self.publishOptions {
addTransceiverIfRequired(
for: publishOption,
with: primaryTrack.clone(from: peerConnectionFactory),
screenSharingType: activeSession.screenSharingType
)
}
guard
primaryTrack.isEnabled,
let activeSession = screenShareSessionProvider.activeSession
else { return }

let activePublishOptions = Set(self.publishOptions)
for publishOption in self.publishOptions {
addTransceiverIfRequired(
for: publishOption,
with: primaryTrack.clone(from: peerConnectionFactory),
screenSharingType: activeSession.screenSharingType
)
}

transceiverStorage
.forEach { $0.value.sender.track?.isEnabled = activePublishOptions.contains($0.key) }
let activePublishOptions = Set(self.publishOptions)

log.debug(
"""
Local screenShareTracks updated with:
PublishOptions:
\(self.publishOptions.map { "\($0)" }.joined(separator: "\n"))
TransceiverStorage:
\(transceiverStorage)
""",
subsystems: .webRTC
)
transceiverStorage
.forEach { $0.value.sender.track?.isEnabled = activePublishOptions.contains($0.key) }

log.debug(
"""
Local screenShareTracks updated with:
PublishOptions:
\(self.publishOptions.map { "\($0)" }.joined(separator: "\n"))
TransceiverStorage:
\(transceiverStorage)
""",
subsystems: .webRTC
)
}
}

/// Adjusts the publishing quality of the screen sharing track.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,16 @@ final class LocalVideoMediaAdapter: LocalMediaAdapting, @unchecked Sendable {
_ publishOptions: PublishOptions
) async throws {
processingQueue.async { [weak self] in
guard let self else { return }

self.publishOptions = publishOptions.video

guard
let self,
primaryTrack.isEnabled
else {
return
}

self.publishOptions = publishOptions.video

for publishOption in self.publishOptions {
addTransceiverIfRequired(
for: publishOption,
Expand Down

0 comments on commit a5502a9

Please sign in to comment.