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 ofLivestreamOutputobjects, one per platform, each with the RTMPurlandstreamKey. Pass at least one output. The SDK doesn't check the URL or key; a wrong one fails on the server and is reported ononErroras4013(START_LIVESTREAM_FAILED). Treat that4013as the end of the attempt: aLIVESTREAM_STARTINGthat arrives after it doesn't mean a start is still in progress, so callstartLivestream()again to retry. -
config: aLivestreamConfigfor the layout. It takes alayout(ConfigLayout), atheme(ConfigTheme) and arecording(LivestreamRecordingConfig), all from thelive.videosdk.rtc.android.modelpackage. It defaults tonull, which uses the default layout.
startLivestream()andstopLivestream()aresuspendfunctions. Call them from a coroutine, such aslifecycleScope.launch(fromandroidx.lifecycle:lifecycle-runtime-ktx).- They return once the request is sent, not when the livestream has started or stopped. Wait for
onLivestreamStateChangedto reportLIVESTREAM_STARTEDorLIVESTREAM_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) or3065(ERROR_ROOM_RECONNECTING) ononErrorinstead 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

