Skip to content

Commit

Permalink
Reset projection timestamps when track goes inactive
Browse files Browse the repository at this point in the history
  • Loading branch information
aravind-raveendran committed Oct 17, 2024
1 parent de71760 commit 7e382b9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ final actor VideoTracksManager {

private let subscriptionManager: SubscriptionManager
private var layerEventsObservationDictionary: [SourceID: Task<Void, Never>] = [:]
private var activeEventObservationDictionary: [SourceID: Task<Void, Never>] = [:]

private let videoQualitySubject: CurrentValueSubject<[SourceID: VideoQuality], Never> = CurrentValueSubject([:])
private let layersSubject: CurrentValueSubject<[SourceID: [MCRTSRemoteTrackLayer]], Never> = CurrentValueSubject([:])

Expand Down Expand Up @@ -82,7 +84,7 @@ final actor VideoTracksManager {
}
}

func observeLayerUpdates(for source: StreamSource) {
func observeEvents(for source: StreamSource) {
Task { [weak self] in
guard
let self,
Expand All @@ -101,11 +103,38 @@ final actor VideoTracksManager {
await self.addLayerEventsObservationTask(layerEventsObservationTask, for: source)
await layerEventsObservationTask.value
}

Task { [weak self] in
guard
let self,
await self.activeEventObservationDictionary[source.sourceId] == nil
else {
return
}
let task = Task {
for await activity in source.videoTrack.activity() {
switch activity {
case .active:
// No-op
break
case .inactive:
if let mid = source.videoTrack.currentMID {
await self.remove(mid: mid)
}
}
}
}

Self.logger.debug("♼ Registering activity events of \(source.sourceId)")
await self.addActivityEventsObservationTask(task, for: source)
await task.value
}
}

func reset() {
sourceToTasks.removeAll()
layerEventsObservationDictionary.removeAll()
activeEventObservationDictionary.removeAll()
sourceToActiveViewsMapping.removeAll()
viewToRequestedVideoQualityMapping.removeAll()
sourceToSimulcastLayersMapping.removeAll()
Expand Down Expand Up @@ -366,6 +395,10 @@ private extension VideoTracksManager {
func addLayerEventsObservationTask(_ task: Task<Void, Never>, for source: StreamSource) {
layerEventsObservationDictionary[source.sourceId] = task
}

func addActivityEventsObservationTask(_ task: Task<Void, Never>, for source: StreamSource) {
activeEventObservationDictionary[source.sourceId] = task
}
}

private extension Array where Self.Element == VideoQuality {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ private extension StreamViewModel {
await withTaskGroup(of: Void.self) { group in
for source in activeSources {
group.addTask {
await self.videoTracksManager.observeLayerUpdates(for: source)
await self.videoTracksManager.observeEvents(for: source)
}
}
}
Expand Down

0 comments on commit 7e382b9

Please sign in to comment.