Platform docs
Auth, users, webhooks & more

Broadcasting

The StreamVideo SDK has support for broadcasting calls. You can find a tutorial on how to implement broadcasting calls in our livestreaming tutorial.

Creating a call

First, you need to create a call with a user that has the capability to start broadcasting calls (start-broadcast-call).

let call = streamVideo.call(callType: .default, callId: callId)
Task {
    try await call.join()
}

The code above will create and join the call. If you just want to create it (and not join it), you can use the method create:

let call = try await call.create(members: members, custom: [:], startsAt: Date(), ring: false)

Start HLS

You can start HLS broadcasting a call, by calling the method startHLS:

try await call.startHLS()

After few seconds of setup, the call would be broadcasted, and you will receive an event called BroadcastingStartedEvent. Also, the state of the call would be updated - the broadcasting value would become true.

You can listen to the broadcasting events on the call by subscribing to its AsyncStream:

for await event in call.subscribe() {
    switch event {
        case .typeCallHLSBroadcastingStartedEvent(let broadcastingStartedEvent):
            log.debug("received an event \(broadcastingStartedEvent)")
            /* handle recording event */
        case .typeCallHLSBroadcastingStoppedEvent(let broadcastingStoppedEvent):
            log.debug("received an event \(broadcastingStoppedEvent)")
            /* handle recording event */
        default:
            break
    }
}

In the BroadcastingStartedEvent, you will receive the hlsPlaylistUrl, a URL that can be used by other participants to watch the broadcasting.

Stop HLS

When you are done with broadcasting, you should call the method stopHLS.

try await call.stopHLS()

This action will also send BroadcastingStoppedEvent to all participants who are watching the call.

RTMP-out broadcasting

Apart from HLS, you can also forward a call to any external service that accepts RTMP(s) as input, such as YouTube, Twitch or Facebook. Multiple broadcasts can run at the same time for the same call, so you can stream to several platforms at once. More details about the feature are available in the RTMP broadcast API docs.

Start an RTMP broadcast

Each broadcast needs a name, which you use later to refer to it, and the stream URL and stream key provided by the target platform:

try await call.startRTMPBroadcast(
    request: StartRTMPBroadcastsRequest(
        broadcasts: [
            RTMPBroadcastRequest(
                name: "youtube_channel",
                quality: ._1080p,
                streamKey: "your_stream_key",
                streamUrl: "rtmps://x.rtmps.youtube.com/live2"
            )
        ]
    )
)

The quality and the layout used to compose the call into a single stream can also be set on the call type, in which case you don't need to pass them per broadcast.

Stop an RTMP broadcast

To stop a single broadcast, pass the name you used when starting it:

try await call.stopRTMPBroadcasts(name: "youtube_channel")

If you have several broadcasts running and want to stop all of them at once, you don't need to keep track of their names, use stopAllRTMPBroadcasts instead:

try await call.stopAllRTMPBroadcasts()

Note that ending the call also stops its RTMP broadcasts.

RTMP broadcast events

RTMP broadcasts report their progress through the following events, which you can observe like any other call event:

for await event in call.subscribe() {
    switch event {
        case .typeCallRtmpBroadcastStartedEvent(let startedEvent):
            log.debug("received an event \(startedEvent)")
            /* handle broadcast event */
        case .typeCallRtmpBroadcastStoppedEvent(let stoppedEvent):
            log.debug("received an event \(stoppedEvent)")
            /* handle broadcast event */
        case .typeCallRtmpBroadcastFailedEvent(let failedEvent):
            log.debug("received an event \(failedEvent)")
            /* handle broadcast event */
        default:
            break
    }
}

The call state's broadcasting property is true while either HLS or any RTMP broadcast is active. The active RTMP broadcasts themselves are listed in call.state.egress?.rtmps.