From 243b1fad3b798657e19c817f012ff7325e353b64 Mon Sep 17 00:00:00 2001 From: Martin Mitrevski Date: Tue, 28 Jan 2025 10:36:10 +0100 Subject: [PATCH] Exposed methods for RTMP broadcasting (#642) --- Sources/StreamVideo/Call.swift | 27 +++++++++++++++++ Sources/StreamVideo/CallState.swift | 6 ++-- StreamVideoTests/Call/Call_Tests.swift | 40 ++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 3 deletions(-) diff --git a/Sources/StreamVideo/Call.swift b/Sources/StreamVideo/Call.swift index a674acf32..6e2033b3a 100644 --- a/Sources/StreamVideo/Call.swift +++ b/Sources/StreamVideo/Call.swift @@ -715,17 +715,20 @@ public class Call: @unchecked Sendable, WSEventsSubscriber { /// - Parameters: /// - startsHls: whether hls streaming should be started. /// - startRecording: whether recording should be started. + /// - startRtmpBroadcasts: whether RTMP broadcasting should be started. /// - startTranscription: whether transcription should be started. /// - Returns: `GoLiveResponse`. @discardableResult public func goLive( startHls: Bool? = nil, startRecording: Bool? = nil, + startRtmpBroadcasts: Bool? = nil, startTranscription: Bool? = nil ) async throws -> GoLiveResponse { let goLiveRequest = GoLiveRequest( startHls: startHls, startRecording: startRecording, + startRtmpBroadcasts: startRtmpBroadcasts, startTranscription: startTranscription ) return try await coordinatorClient.goLive( @@ -791,6 +794,30 @@ public class Call: @unchecked Sendable, WSEventsSubscriber { public func stopHLS() async throws -> StopHLSBroadcastingResponse { try await coordinatorClient.stopHLSBroadcasting(type: callType, id: callId) } + + /// Starts RTMP broadcasting of the call. + /// - Parameter request: The request to start RTMP broadcasting. + /// - Returns: `StartRTMPBroadcastsResponse`. + /// - Throws: An error if starting RTMP broadcasting. + @discardableResult + public func startRTMPBroadcast( + request: StartRTMPBroadcastsRequest + ) async throws -> StartRTMPBroadcastsResponse { + try await coordinatorClient.startRTMPBroadcasts( + type: callType, + id: callId, + startRTMPBroadcastsRequest: request + ) + } + + /// Stops RTMP broadcasting of the call. + /// - Parameter name: The name of the RTMP broadcast. + /// - Returns: `StopRTMPBroadcastsResponse`. + /// - Throws: An error if stopping RTMP broadcasting. + @discardableResult + public func stopRTMPBroadcasts(name: String) async throws -> StopRTMPBroadcastsResponse { + try await coordinatorClient.stopRTMPBroadcast(type: callType, id: callId, name: name) + } // MARK: - Events diff --git a/Sources/StreamVideo/CallState.swift b/Sources/StreamVideo/CallState.swift index 73661a1e5..3fad113aa 100644 --- a/Sources/StreamVideo/CallState.swift +++ b/Sources/StreamVideo/CallState.swift @@ -253,11 +253,11 @@ public class CallState: ObservableObject { case .typeCallMissedEvent: break case .typeCallRtmpBroadcastStartedEvent: - break + broadcasting = true case .typeCallRtmpBroadcastStoppedEvent: - break + broadcasting = false case .typeCallRtmpBroadcastFailedEvent: - break + broadcasting = false case .typeCallSessionParticipantCountsUpdatedEvent: break case .typeUserUpdatedEvent: diff --git a/StreamVideoTests/Call/Call_Tests.swift b/StreamVideoTests/Call/Call_Tests.swift index 699d0b819..5514636a8 100644 --- a/StreamVideoTests/Call/Call_Tests.swift +++ b/StreamVideoTests/Call/Call_Tests.swift @@ -418,6 +418,46 @@ final class Call_Tests: StreamVideoTestCase { XCTAssertEqual(participants?[0].name, "Alexey") XCTAssertEqual(participants?[1].name, "Ilias") } + + // MARK: - RTMP Broadcasting + + func test_updateState_fromBroadcastStartedEvent() async throws { + // Given + let call = streamVideo?.call(callType: callType, callId: callId) + let event = CallRtmpBroadcastStartedEvent(callCid: callCid, createdAt: Date(), name: "test") + + // When + call?.state.updateState(from: .typeCallRtmpBroadcastStartedEvent(event)) + + // Then + XCTAssert(call?.state.broadcasting == true) + } + + func test_updateState_fromBroadcastStoppedEvent() async throws { + // Given + let call = streamVideo?.call(callType: callType, callId: callId) + let event = CallRtmpBroadcastStoppedEvent(callCid: callCid, createdAt: Date(), name: "test") + call?.state.broadcasting = true + + // When + call?.state.updateState(from: .typeCallRtmpBroadcastStoppedEvent(event)) + + // Then + XCTAssert(call?.state.broadcasting == false) + } + + func test_updateState_fromBroadcastFailedEvent() async throws { + // Given + let call = streamVideo?.call(callType: callType, callId: callId) + let event = CallRtmpBroadcastFailedEvent(callCid: callCid, createdAt: Date(), name: "test") + call?.state.broadcasting = true + + // When + call?.state.updateState(from: .typeCallRtmpBroadcastFailedEvent(event)) + + // Then + XCTAssert(call?.state.broadcasting == false) + } // MARK: - Private helpers