Skip to content

Fix 288ms UI hang in video publishing flow #838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ final class LocalAudioMediaAdapter: LocalMediaAdapting, @unchecked Sendable {

/// Cleans up resources when the instance is deallocated.
deinit {
Task { @MainActor [transceiverStorage] in
Task { [transceiverStorage] in
transceiverStorage.removeAll()
}
log.debug(
Expand Down Expand Up @@ -126,7 +126,7 @@ final class LocalAudioMediaAdapter: LocalMediaAdapting, @unchecked Sendable {
/// This enables the primary track and creates additional transceivers based
/// on the current publish options. It also starts the audio recorder.
func publish() {
processingQueue.async { @MainActor [weak self] in
processingQueue.async { [weak self] in
guard
let self,
!primaryTrack.isEnabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ final class LocalVideoMediaAdapter: LocalMediaAdapting, @unchecked Sendable {
/// Removes all transceivers from storage and logs details about the
/// deallocation process.
deinit {
Task { @MainActor [transceiverStorage] in
Task { [transceiverStorage] in
transceiverStorage.removeAll()
}

Expand Down Expand Up @@ -207,35 +207,52 @@ final class LocalVideoMediaAdapter: LocalMediaAdapting, @unchecked Sendable {

/// Starts publishing the local video track.
func publish() {
processingQueue.async { @MainActor [weak self] in
processingQueue.async { [weak self] in
guard
let self,
!primaryTrack.isEnabled
else {
return
}
primaryTrack.isEnabled = true

do {
try await startVideoCapturingSession()
} catch {
log.error(error)

// Don't wait for camera to start - do it in parallel
Task { [weak self] in
do {
try await self?.startVideoCapturingSession()
} catch {
log.error("Failed to start video capturing session: \(error)")
}
}

publishOptions
.forEach {
self.addTransceiverIfRequired(
for: $0,
with: self
.primaryTrack
.clone(from: self.peerConnectionFactory)
)

// Clone tracks and setup transceivers (don't block UI thread)
Task { [weak self] in
guard let self else { return }

// Clone tracks in parallel
await withTaskGroup(of: (PublishOptions.VideoPublishOptions, RTCVideoTrack)?.self) { group in
for option in self.publishOptions {
group.addTask { [weak self] in
guard let self else { return nil }

let clonedTrack = await Task.detached {
self.primaryTrack.clone(from: self.peerConnectionFactory)
}.value

return (option, clonedTrack)
}
}

// Add transceivers as clones complete (off main thread - thread safe!)
for await result in group {
guard let (option, track) = result else { continue }
self.addTransceiverIfRequired(for: option, with: track)
}
}

let activePublishOptions = Set(self.publishOptions)

transceiverStorage
.forEach {

// Update transceiver states (off main thread - thread safe!)
let activePublishOptions = Set(self.publishOptions)
self.transceiverStorage.forEach {
if activePublishOptions.contains($0.key) {
$0.value.track.isEnabled = true
$0.value.transceiver.sender.track = $0.value.track
Expand All @@ -244,15 +261,16 @@ final class LocalVideoMediaAdapter: LocalMediaAdapting, @unchecked Sendable {
$0.value.transceiver.sender.track = nil
}
}

log.debug(
"""
Local videoTracks are now published
primary: \(primaryTrack.trackId) isEnabled:\(primaryTrack.isEnabled)
clones: \(transceiverStorage.map(\.value.track.trackId).joined(separator: ","))
""",
subsystems: .webRTC
)

log.debug(
"""
Local videoTracks are now published
primary: \(self.primaryTrack.trackId) isEnabled:\(self.primaryTrack.isEnabled)
clones: \(self.transceiverStorage.map(\.value.track.trackId).joined(separator: ","))
""",
subsystems: .webRTC
)
}
}
}

Expand All @@ -271,7 +289,7 @@ final class LocalVideoMediaAdapter: LocalMediaAdapting, @unchecked Sendable {
transceiverStorage
.forEach { $0.value.track.isEnabled = false }

Task { @MainActor [weak self] in
Task { [weak self] in
do {
try await self?.stopVideoCapturingSession()
} catch {
Expand Down