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 ofLivestreamOutputobjects which contains the RTMPurlandstreamKeyof the platforms you want to start the livestream. Pass at least one output.config(optional): ALivestreamConfigthat sets thelayout,themeandrecordingof 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.
- Swift
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)")
}
}
}
}
- Before you join the room, or after you leave it,
startLivestream()throwsERROR_ACTION_PERFORMED_BEFORE_ROOM_JOINED(3035). While the room is reconnecting, it throwsERROR_ROOM_RECONNECTING(3065). Call it again after the room reconnects. - If the server refuses the request, the call throws and
onError(error:)also receivesSTART_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:)receivesSTART_LIVESTREAM_FAILED(4013).
stopLivestream()
stopLivestream()is used to stop the room livestream which can be accessed from theRoomclass. It is alsoasync throws, so call it withtry awaitinside aTask.- If the server refuses the request, the call throws and
onError(error:)also receivesSTOP_LIVESTREAM_FAILED(4014).
Example
- Swift
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
onLivestreamStateChangedevent will trigger.
- Swift
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

