diff --git a/Sources/StreamVideo/WebRTC/v2/PeerConnection/RTCPeerConnectionCoordinator.swift b/Sources/StreamVideo/WebRTC/v2/PeerConnection/RTCPeerConnectionCoordinator.swift index cde0ef124..149328f1f 100644 --- a/Sources/StreamVideo/WebRTC/v2/PeerConnection/RTCPeerConnectionCoordinator.swift +++ b/Sources/StreamVideo/WebRTC/v2/PeerConnection/RTCPeerConnectionCoordinator.swift @@ -36,6 +36,7 @@ class RTCPeerConnectionCoordinator: @unchecked Sendable { private let subsystem: LogSubsystem private let disposableBag: DisposableBag = .init() private let dispatchQueue = DispatchQueue(label: "io.getstream.peerconnection.serial.offer.queue") + private let debouncedIntervalInMilliseconds = 250 // The interval used to debounce the SetPublisher requests. /// `SetPublisher` and `HandleSubscriberOffer` are expected from the SFU to be sent/handled /// in a serial manner. The processing queues below ensure that the respective tasks are being executed @@ -206,6 +207,7 @@ class RTCPeerConnectionCoordinator: @unchecked Sendable { peerConnection .publisher(eventType: StreamRTCPeerConnection.ShouldNegotiateEvent.self) .log(.debug) { _ in "Publisher will negotiate" } + .debounce(for: .milliseconds(250), scheduler: RunLoop.main) .receive(on: dispatchQueue) .map { _ in () } .sinkTask(queue: setPublisherProcessingQueue) { [weak self] in await self?.negotiate() } diff --git a/StreamVideoTests/WebRTC/v2/PeerConnection/RTCPeerConnectionCoordinator_Tests.swift b/StreamVideoTests/WebRTC/v2/PeerConnection/RTCPeerConnectionCoordinator_Tests.swift index 6371812ce..258c4864b 100644 --- a/StreamVideoTests/WebRTC/v2/PeerConnection/RTCPeerConnectionCoordinator_Tests.swift +++ b/StreamVideoTests/WebRTC/v2/PeerConnection/RTCPeerConnectionCoordinator_Tests.swift @@ -307,7 +307,7 @@ final class RTCPeerConnectionCoordinator_Tests: XCTestCase, @unchecked Sendable XCTAssertEqual(mockPeerConnection?.timesCalled(.setRemoteDescription), 0) } - func test_negotiate_subjectIsPublisher_multipleRequestsExecuteSerially_callSetPublisherOnSFUWithCorrectOfferEveryTime( + func test_negotiate_subjectIsPublisher_multipleRequestsExecuteSerially_callSetPublisherOnSFUWithCorrectOfferOnlyOnce( ) async throws { _ = subject let offerA = RTCSessionDescription( @@ -341,7 +341,7 @@ final class RTCPeerConnectionCoordinator_Tests: XCTestCase, @unchecked Sendable } await fulfillment { [mockPeerConnection] in - mockPeerConnection?.timesCalled(.setLocalDescription) == 2 + mockPeerConnection?.timesCalled(.setLocalDescription) == 1 } XCTAssertEqual(