Skip to main content
Version: 3.x.x

RTMP Livestream - Android

RTMP is a popular protocol for live streaming video content from VideoSDK to platforms such as YouTube, Twitch, Facebook, and others.

VideoSDK can livestream your room to any platform that supports RTMP ingestion. Provide the platform's stream key and stream URL, and VideoSDK connects to the platform's RTMP server and sends the live video stream.

You can set the livestream layout with a prebuilt layout in the configuration, or with your own custom template.

This guide shows how to start and stop an RTMP livestream.

startLivestream()​

room.startLivestream() starts an RTMP livestream of the room. It takes two parameters:

  • outputs: a list of LivestreamOutput objects, one per platform, each with the RTMP url and streamKey. Pass at least one output. The SDK doesn't check the URL or key; a wrong one fails on the server and is reported on onError as 4013 (START_LIVESTREAM_FAILED). Treat that 4013 as the end of the attempt: a LIVESTREAM_STARTING that arrives after it doesn't mean a start is still in progress, so call startLivestream() again to retry.

  • config: a LivestreamConfig for the layout. It takes a layout (ConfigLayout), a theme (ConfigTheme) and a recording (LivestreamRecordingConfig), all from the live.videosdk.rtc.android.model package. It defaults to null, which uses the default layout.

note
  • startLivestream() and stopLivestream() are suspend functions. Call them from a coroutine, such as lifecycleScope.launch (from androidx.lifecycle:lifecycle-runtime-ktx).
  • They return once the request is sent, not when the livestream has started or stopped. Wait for onLivestreamStateChanged to report LIVESTREAM_STARTED or LIVESTREAM_STOPPED.
  • If the request fails, they throw VideoSDKException.
  • Called before the room is joined, or while it is reconnecting, they do nothing and report 3035 (ERROR_ACTION_PERFORMED_BEFORE_ROOM_JOINED) or 3065 (ERROR_ROOM_RECONNECTING) on onError instead of throwing.
// Layout Configuration
val layout = ConfigLayout(
type = ConfigLayoutGrid.GRID, // SPOTLIGHT | SIDEBAR, Default : GRID
priority = ConfigLayoutPriority.SPEAKER, // PIN, Default : SPEAKER
gridSize = 4, // MAX : 4
)

val config = LivestreamConfig(
layout = layout,
// Theme of livestream layout
theme = ConfigTheme.DARK, // LIGHT | DEFAULT
)

val outputs = listOf(LivestreamOutput(RTMP_URL, RTMP_STREAM_KEY))

lifecycleScope.launch {
try {
room!!.startLivestream(outputs, config)
} catch (e: VideoSDKException) {
Log.e("VideoSDK", "Start livestream failed: ${e.code} ${e.name}")
}
}

stopLivestream()​

  • room.stopLivestream() stops the livestream.

Example​

// keep track of livestream status, updated in onLivestreamStateChanged below
var isLivestreaming = false

findViewById<View>(R.id.btnLiveStream).setOnClickListener { view: View? ->
lifecycleScope.launch {
try {
if (!isLivestreaming) {
val config = LivestreamConfig(
layout = ConfigLayout(ConfigLayoutGrid.GRID, ConfigLayoutPriority.SPEAKER, 4),
theme = ConfigTheme.DARK,
)

val outputs = listOf(LivestreamOutput(RTMP_URL, RTMP_STREAM_KEY))

// Start LiveStream
room!!.startLivestream(outputs, config)
} else {
// Stop LiveStream
room!!.stopLivestream()
}
} catch (e: VideoSDKException) {
Log.e("VideoSDK", "Livestream request failed: ${e.code} ${e.name}")
}
}
}

Event associated with Livestream​

  • onLivestreamStateChanged - Triggered whenever the room's livestream state changes.
private val roomEventListener: RoomEventListener = object : RoomEventListener() {
override fun onLivestreamStateChanged(state: LiveStreamState) {
when (state) {
LiveStreamState.LIVESTREAM_STARTING -> Log.d( "LivestreamStateChanged",
"Livestream is starting"
)
LiveStreamState.LIVESTREAM_STARTED -> {
isLivestreaming = true
Log.d( "LivestreamStateChanged", "Livestream is started")
}
LiveStreamState.LIVESTREAM_STOPPING -> Log.d("LivestreamStateChanged",
"Livestream is stopping"
)
LiveStreamState.LIVESTREAM_STOPPED -> {
isLivestreaming = false
Log.d("LivestreamStateChanged", "Livestream is stopped")
}
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
//...

// add listener to room
room!!.addEventListener(roomEventListener)
}

API Reference​

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

Got a Question? Ask us on discord