Skip to content

Commit 8db02b4

Browse files
authored
[Fix]Revert reconnection ui-freeze (#548) (#554)
1 parent 1968d05 commit 8db02b4

File tree

17 files changed

+137
-162
lines changed

17 files changed

+137
-162
lines changed

Sources/StreamVideo/Controllers/CallController.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,10 @@ class CallController: @unchecked Sendable {
428428
private func handleParticipantsUpdated() {
429429
webRTCParticipantsObserver = debouncedParticipants?
430430
.$value
431-
.sinkTask { @MainActor [weak self] participants in
432-
self?.call?.state.participantsMap = participants
431+
.sink { [weak self] participants in
432+
Task { @MainActor [weak self] in
433+
self?.call?.state.participantsMap = participants
434+
}
433435
}
434436
}
435437

Sources/StreamVideo/Models/CallParticipant.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,12 @@ public struct CallParticipant: Identifiable, Sendable, Hashable {
116116
lhs.hasVideo == rhs.hasVideo &&
117117
lhs.hasAudio == rhs.hasAudio &&
118118
lhs.isScreensharing == rhs.isScreensharing &&
119+
lhs.track?.isEnabled == rhs.track?.isEnabled &&
120+
lhs.track?.trackId == rhs.track?.trackId &&
119121
lhs.trackSize == rhs.trackSize &&
122+
lhs.screenshareTrack === rhs.screenshareTrack &&
123+
lhs.screenshareTrack?.isEnabled == rhs.screenshareTrack?.isEnabled &&
124+
lhs.screenshareTrack?.trackId == rhs.screenshareTrack?.trackId &&
120125
lhs.showTrack == rhs.showTrack &&
121126
lhs.isSpeaking == rhs.isSpeaking &&
122127
lhs.isDominantSpeaker == rhs.isDominantSpeaker &&
@@ -125,9 +130,7 @@ public struct CallParticipant: Identifiable, Sendable, Hashable {
125130
lhs.joinedAt == rhs.joinedAt &&
126131
lhs.audioLevel == rhs.audioLevel &&
127132
lhs.audioLevels == rhs.audioLevels &&
128-
lhs.pin == rhs.pin &&
129-
lhs.track === rhs.track &&
130-
lhs.screenshareTrack === rhs.screenshareTrack
133+
lhs.pin == rhs.pin
131134
}
132135

133136
public var isPinned: Bool {
@@ -141,7 +144,7 @@ public struct CallParticipant: Identifiable, Sendable, Hashable {
141144

142145
/// Determines whether the track of the participant should be displayed.
143146
public var shouldDisplayTrack: Bool {
144-
hasVideo && showTrack
147+
hasVideo && showTrack && track?.isEnabled == true
145148
}
146149

147150
public func withUpdated(trackSize: CGSize) -> CallParticipant {

Sources/StreamVideo/WebRTC/v2/Extensions/Foundation/Publisher+TaskSink.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Combine
66
import Foundation
77

88
extension Publisher {
9-
public func sinkTask(
9+
func sinkTask(
1010
storeIn disposableBag: DisposableBag? = nil,
1111
identifier: String = UUID().uuidString,
1212
receiveCompletion: @escaping ((Subscribers.Completion<Failure>) -> Void) = { _ in },

Sources/StreamVideo/WebRTC/v2/PeerConnection/Adapters/ICEAdapter.swift

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,26 +78,20 @@ actor ICEAdapter: @unchecked Sendable {
7878
throw ClientError("PeerConnection type:\(peerType) was unable to trickle generated candidate\(candidate).")
7979
}
8080

81-
try Task.checkCancellation()
82-
8381
log.debug(
8482
"""
85-
PeerConnection type:\(peerType) generated candidate while remoteDescription.
83+
PeerConnection type:\(peerType) generated candidate while remoteDescription is \(peerConnection
84+
.remoteDescription == nil ? "nil" : "non-nil").
8685
Candidate: \(candidate)
8786
""",
8887
subsystems: .iceAdapter
8988
)
9089

91-
try Task.checkCancellation()
92-
9390
try await sfuAdapter.iCETrickle(
9491
candidate: jsonString,
9592
peerType: peerType == .publisher ? .publisherUnspecified : .subscriber,
9693
for: sessionID
9794
)
98-
99-
try Task.checkCancellation()
100-
10195
log.debug(
10296
"""
10397
PeerConnection type:\(peerType) will store trickled candidate for future use.
@@ -183,7 +177,7 @@ actor ICEAdapter: @unchecked Sendable {
183177
await withTaskGroup(of: Void.self) { [weak self] group in
184178
guard let self else { return }
185179
for candidate in await trickledCandidates {
186-
group.addTask { @MainActor [weak self] in
180+
group.addTask { [weak self] in
187181
guard let self else { return }
188182
do {
189183
try Task.checkCancellation()
@@ -213,7 +207,7 @@ actor ICEAdapter: @unchecked Sendable {
213207
private func task(
214208
for candidate: RTCIceCandidate
215209
) -> Task<Void, Never> {
216-
Task { @MainActor [weak peerConnection] in
210+
Task { [weak peerConnection] in
217211
guard let peerConnection else { return }
218212
do {
219213
try Task.checkCancellation()

Sources/StreamVideo/WebRTC/v2/PeerConnection/MediaAdapters/LocalMediaAdapters/LocalAudioMediaAdapter.swift

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,8 @@ final class LocalAudioMediaAdapter: LocalMediaAdapting {
7070

7171
/// Cleans up resources when the instance is deallocated.
7272
deinit {
73-
Task { @MainActor [sender, localTrack] in
74-
sender?.sender.track = nil
75-
localTrack?.isEnabled = false
76-
}
73+
sender?.sender.track = nil
74+
localTrack?.isEnabled = false
7775
if let localTrack {
7876
log.debug(
7977
"""
@@ -122,7 +120,7 @@ final class LocalAudioMediaAdapter: LocalMediaAdapting {
122120
)
123121
)
124122
}
125-
audioTrack.isEnabled = settings.audioOn
123+
audioTrack.isEnabled = false
126124

127125
log.debug(
128126
"""
@@ -149,38 +147,34 @@ final class LocalAudioMediaAdapter: LocalMediaAdapting {
149147

150148
/// Starts publishing the local audio track.
151149
func publish() {
152-
Task { @MainActor in
153-
guard
154-
let localTrack,
155-
localTrack.isEnabled == false || sender?.sender.track == nil
156-
else {
157-
return
158-
}
150+
guard
151+
let localTrack,
152+
localTrack.isEnabled == false || sender?.sender.track == nil
153+
else {
154+
return
155+
}
159156

160-
if sender == nil {
161-
sender = peerConnection.addTransceiver(
162-
with: localTrack,
163-
init: RTCRtpTransceiverInit(
164-
trackType: .audio,
165-
direction: .sendOnly,
166-
streamIds: streamIds
167-
)
157+
if sender == nil {
158+
sender = peerConnection.addTransceiver(
159+
with: localTrack,
160+
init: RTCRtpTransceiverInit(
161+
trackType: .audio,
162+
direction: .sendOnly,
163+
streamIds: streamIds
168164
)
169-
} else {
170-
sender?.sender.track = localTrack
171-
}
172-
localTrack.isEnabled = true
165+
)
166+
} else {
167+
sender?.sender.track = localTrack
173168
}
169+
localTrack.isEnabled = true
174170
}
175171

176172
/// Stops publishing the local audio track.
177173
func unpublish() {
178-
Task { @MainActor in
179-
guard let sender, let localTrack else { return }
180-
localTrack.isEnabled = false
181-
sender.sender.track = nil
182-
log.debug("Local audioTrack trackId:\(localTrack.trackId) is now unpublished.")
183-
}
174+
guard let sender, let localTrack else { return }
175+
localTrack.isEnabled = false
176+
sender.sender.track = nil
177+
log.debug("Local audioTrack trackId:\(localTrack.trackId) is now unpublished.")
184178
}
185179

186180
/// Updates the local audio media based on new call settings.

Sources/StreamVideo/WebRTC/v2/PeerConnection/MediaAdapters/LocalMediaAdapters/LocalVideoMediaAdapter.swift

Lines changed: 58 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,15 @@ final class LocalVideoMediaAdapter: LocalMediaAdapting, @unchecked Sendable {
8181

8282
/// Cleans up resources when the instance is deallocated.
8383
deinit {
84-
Task { @MainActor [sender, localTrack, capturer] in
85-
try? await capturer?.stopCapture()
86-
localTrack?.isEnabled = false
87-
sender?.sender.track = nil
88-
}
84+
Task { [capturer] in try? await capturer?.stopCapture() }
85+
localTrack?.isEnabled = false
86+
sender?.sender.track = nil
8987
if let localTrack {
9088
log.debug(
9189
"""
92-
Local videoTrack will be deallocated trackId:\(localTrack.trackId) isEnabled:\(localTrack.isEnabled).
90+
Local videoTrack will be deallocated
91+
trackId:\(localTrack.trackId)
92+
isEnabled:\(localTrack.isEnabled)
9393
"""
9494
)
9595
}
@@ -124,6 +124,7 @@ final class LocalVideoMediaAdapter: LocalMediaAdapting, @unchecked Sendable {
124124
)
125125
}
126126
} else if !hasVideo {
127+
localTrack?.isEnabled = false
127128
Task { [weak self] in
128129
do {
129130
try await self?.capturer?.stopCapture()
@@ -132,51 +133,40 @@ final class LocalVideoMediaAdapter: LocalMediaAdapting, @unchecked Sendable {
132133
}
133134
}
134135
}
135-
136-
localTrack?.isEnabled = settings.videoOn
137136
}
138137

139138
/// Starts publishing the local video track.
140139
func publish() {
141-
Task { @MainActor [weak self] in
142-
guard
143-
let self,
144-
let localTrack,
145-
localTrack.isEnabled == false || sender == nil
146-
else {
147-
return
148-
}
140+
guard
141+
let localTrack,
142+
localTrack.isEnabled == false || sender == nil
143+
else {
144+
return
145+
}
149146

150-
if sender == nil {
151-
sender = peerConnection.addTransceiver(
152-
with: localTrack,
153-
init: RTCRtpTransceiverInit(
154-
trackType: .video,
155-
direction: .sendOnly,
156-
streamIds: streamIds,
157-
codecs: videoOptions.supportedCodecs
158-
)
147+
if sender == nil {
148+
sender = peerConnection.addTransceiver(
149+
with: localTrack,
150+
init: RTCRtpTransceiverInit(
151+
trackType: .video,
152+
direction: .sendOnly,
153+
streamIds: streamIds,
154+
codecs: videoOptions.supportedCodecs
159155
)
160-
} else {
161-
sender?.sender.track = localTrack
162-
}
163-
localTrack.isEnabled = true
164-
log.debug("Local videoTrack trackId:\(localTrack.trackId) is now published.")
156+
)
157+
} else {
158+
sender?.sender.track = localTrack
165159
}
160+
localTrack.isEnabled = true
161+
log.debug("Local videoTrack trackId:\(localTrack.trackId) is now published.")
166162
}
167163

168164
/// Stops publishing the local video track.
169165
func unpublish() {
170-
Task { @MainActor [weak self] in
171-
guard
172-
let self,
173-
let sender,
174-
let localTrack
175-
else { return }
176-
sender.sender.track = nil
177-
localTrack.isEnabled = false
178-
log.debug("Local videoTrack trackId:\(localTrack.trackId) is now unpublished.")
179-
}
166+
guard let sender, let localTrack else { return }
167+
sender.sender.track = nil
168+
localTrack.isEnabled = false
169+
log.debug("Local videoTrack trackId:\(localTrack.trackId) is now unpublished.")
180170
}
181171

182172
/// Updates the local video media based on new call settings.
@@ -281,45 +271,39 @@ final class LocalVideoMediaAdapter: LocalMediaAdapting, @unchecked Sendable {
281271
func changePublishQuality(
282272
with activeEncodings: Set<String>
283273
) {
284-
Task { @MainActor [weak self] in
285-
guard
286-
let self,
287-
let sender,
288-
!activeEncodings.isEmpty
289-
else {
290-
return
291-
}
274+
guard let sender, !activeEncodings.isEmpty else {
275+
return
276+
}
292277

293-
var hasChanges = false
294-
let params = sender
295-
.sender
296-
.parameters
297-
var updatedEncodings = [RTCRtpEncodingParameters]()
278+
var hasChanges = false
279+
let params = sender
280+
.sender
281+
.parameters
282+
var updatedEncodings = [RTCRtpEncodingParameters]()
298283

299-
for encoding in params.encodings {
300-
guard let rid = encoding.rid else {
301-
continue
302-
}
303-
let shouldEnable = activeEncodings.contains(rid)
304-
305-
switch (shouldEnable, encoding.isActive) {
306-
case (true, true):
307-
break
308-
case (false, false):
309-
break
310-
default:
311-
hasChanges = true
312-
encoding.isActive = shouldEnable
313-
}
314-
updatedEncodings.append(encoding)
284+
for encoding in params.encodings {
285+
guard let rid = encoding.rid else {
286+
continue
315287
}
316-
317-
guard hasChanges else {
318-
return
288+
let shouldEnable = activeEncodings.contains(rid)
289+
290+
switch (shouldEnable, encoding.isActive) {
291+
case (true, true):
292+
break
293+
case (false, false):
294+
break
295+
default:
296+
hasChanges = true
297+
encoding.isActive = shouldEnable
319298
}
320-
params.encodings = updatedEncodings
321-
sender.sender.parameters = params
299+
updatedEncodings.append(encoding)
300+
}
301+
302+
guard hasChanges else {
303+
return
322304
}
305+
params.encodings = updatedEncodings
306+
sender.sender.parameters = params
323307
}
324308

325309
// MARK: - Private helpers

Sources/StreamVideo/WebRTC/v2/PeerConnection/Protocols/StreamRTCPeerConnectionProtocol.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,5 @@ protocol StreamRTCPeerConnectionProtocol: AnyObject {
8080
func restartIce()
8181

8282
/// Closes the peer connection.
83-
func close() async
83+
func close()
8484
}

Sources/StreamVideo/WebRTC/v2/PeerConnection/RTCPeerConnectionCoordinator.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ class RTCPeerConnectionCoordinator: @unchecked Sendable {
207207
subsystems: subsystem
208208
)
209209
disposableBag.removeAll()
210-
Task { [peerConnection] in await peerConnection.close() }
210+
peerConnection.close()
211211
}
212212

213213
func prepareForClosing() async {
@@ -366,7 +366,7 @@ class RTCPeerConnectionCoordinator: @unchecked Sendable {
366366
}
367367

368368
/// Closes the peer connection.
369-
func close() async {
369+
func close() {
370370
log.debug(
371371
"""
372372
Closing PeerConnection
@@ -378,7 +378,7 @@ class RTCPeerConnectionCoordinator: @unchecked Sendable {
378378
subsystems: subsystem
379379
)
380380
disposableBag.removeAll()
381-
await peerConnection.close()
381+
peerConnection.close()
382382
}
383383

384384
/// Restarts ICE for the peer connection.

0 commit comments

Comments
 (0)