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. -
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. If the call fails, it throws a VideoSDKError.
- 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)")
}
}
}
}
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 theRoomclass. It is alsoasync throws, so call it withtry awaitinside aTask.
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
}
}
}
Custom Template
With VideoSDK, you can also use your own custom designed layout template to livestream the rooms. In order to use the custom template, you need to create a template for which you can follow this guide. Once you have set up the template, you can use the REST API to start the livestream with the templateUrl parameter.
API Reference
The API references for all the methods utilised in this guide are provided below.
Got a Question? Ask us on discord

