Skip to main content
Version: 4.x.x

RTMP Livestream - iOS

RTMP is a widely used protocol for live streaming video content from VideoSDK to platforms like YouTube, Twitch, Facebook, and others.

To initiate live streaming from VideoSDK to platforms supporting RTMP ingestion, you simply need to provide the platform-specific stream key and stream URL. This enables VideoSDK to connect to the platform's RTMP server and transmit the live video stream.

You can set the livestream layout by choosing a prebuilt layout in the configuration or by providing your own custom template.

This guide will provide an overview of how to implement starting and stopping RTMP livestreaming with VideoSDK.

startLivestream()​

startLivestream() of the Room class starts an RTMP livestream of the room. 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 livestream to.
  • 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, so call it with try await inside a Task after you join the room. If the request fails, for example because you have not joined yet, it throws a VideoSDKError.

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 livestream of the room, which can be accessed from the Room class. It is also async throws.

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 livestream state of the room 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
}
}
}

Custom Template​

With VideoSDK, you have the option to employ your own custom-designed layout template for RTMP livestreaming. To use a custom template, follow this guide to create and set up the template. Once the template is configured, you can start the livestream using the REST API, specifying 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