Skip to main content
Version: 4.x.x

RTMP Livestream - iOS

RTMP is a protocol for live streaming video to platforms such as YouTube, Twitch and Facebook.

VideoSDK lets you livestream your room to any platform that supports RTMP ingestion. Provide the platform's stream URL and stream key.

VideoSDK also allows you to configure the livestream layouts by choosing a prebuilt layout in the configuration or by providing your own custom template for the livestream.

This guide will provide an overview of how to implement start and stop RTMP Livestreaming.

startLivestream()​

startLivestream() can be used to start an RTMP livestream of the room which can be accessed from the Room class. This method accepts two parameters:

  • outputs: This parameter accepts a list of LivestreamOutput objects which contains the RTMP url and streamKey of the platforms you want to start the livestream. Pass at least one output.
  • config (optional): A LivestreamConfig that sets the layout, theme and recording of the livestream. If you leave it out, the server defaults are used.

startLivestream() is async throws. Call it with try await inside a Task, after you join the room.

class RoomViewController: UIViewController {

var room: Room?
private let platformUrl = "<url-of-the-platform>"
private let privateKey = "<private-key>"

// button to start livestream
@IBAction func startLiveStreamButtonTapped(_ sender: Any) {
Task {
do {
try await self.room?.startLivestream(outputs: [LivestreamOutput(url: platformUrl, streamKey: privateKey)])
} catch {
print("Failed to start livestream: \(error)")
}
}
}
}
note
  • Before you join the room, or after you leave it, startLivestream() throws ERROR_ACTION_PERFORMED_BEFORE_ROOM_JOINED (3035). While the room is reconnecting, it throws ERROR_ROOM_RECONNECTING (3065). Call it again after the room reconnects.
  • If the server refuses the request, the call throws and onError(error:) also receives START_LIVESTREAM_FAILED (4013).
  • If the server accepts the request but the livestream has not started 20 seconds later, for example because the RTMP URL cannot be reached, onError(error:) receives START_LIVESTREAM_FAILED (4013).

stopLivestream()​

  • stopLivestream() is used to stop the room livestream which can be accessed from the Room class. It is also async throws, so call it with try await inside a Task.
  • If the server refuses the request, the call throws and onError(error:) also receives STOP_LIVESTREAM_FAILED (4014).

Example​

extension RoomViewController {
// button to stop livestream
@IBAction func stopLiveStreamButtonTapped(_ sender: Any) {
Task {
do {
try await self.room?.stopLivestream()
} catch {
print("Failed to stop livestream: \(error)")
}
}
}
}

Event associated with Livestream​

  • onLivestreamStateChanged - Whenever the room's livestream state changes, then onLivestreamStateChanged event will trigger.
extension RoomViewController: RoomEventListener {
// rtmp-event
func onLivestreamStateChanged(state: LiveStreamState) {
switch(state) {
case .LIVESTREAM_STARTING:
print("livestream starting")

case .LIVESTREAM_STARTED:
print("livestream started")

case .LIVESTREAM_STOPPING:
print("livestream stopping")

case .LIVESTREAM_STOPPED:
print("livestream stopped")

@unknown default:
break
}
}
}

API Reference​

The API references for all the methods utilised in this guide are provided below.

Got a Question? Ask us on discord