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 meeting 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()
startLivestream() can be used to start a RTMP livestream of the meeting which can be accessed from the Meeting class. This method accepts two parameters:
-
1. outputs: This parameter accepts a list ofLivestreamOutputobjects which contains the RTMPurlandstreamKeyof the platforms you want to start the livestream. An empty list does nothing. -
2. config: This parameter will define how the livestream layout should look like. You can passnullfor 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 meeting 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.
val config = JSONObject()
// Layout Configuration
val layout = JSONObject()
layout.put("type", "GRID") // "SPOTLIGHT" | "SIDEBAR", Default : "GRID"
layout.put("priority", "SPEAKER") // "PIN", Default : "SPEAKER"
layout.put("gridSize", 4) // MAX : 4
config.put("layout", layout)
// Theme of livestream layout
config.put("theme", "DARK") // "LIGHT" | "DEFAULT"
val outputs: MutableList<LivestreamOutput> = ArrayList()
outputs.add(LivestreamOutput(RTMP_URL, RTMP_STREAM_KEY))
lifecycleScope.launch {
try {
meeting!!.startLivestream(outputs, config)
} catch (e: VideoSDKException) {
Log.e("VideoSDK", "Start livestream failed: ${e.code} ${e.name}")
}
}
stopLivestream()
stopLivestream()is used to stop the meeting livestream which can be accessed from theMeetingclass.
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 = JSONObject()
val layout = JSONObject()
layout.put("type", "GRID")
layout.put("priority", "SPEAKER")
layout.put("gridSize", 4)
config.put("layout", layout)
config.put("theme", "DARK")
val outputs: MutableList<LivestreamOutput> = ArrayList()
outputs.add(LivestreamOutput(RTMP_URL, RTMP_STREAM_KEY))
// Start LiveStream
meeting!!.startLivestream(outputs, config)
} else {
// Stop LiveStream
meeting!!.stopLivestream()
}
} catch (e: VideoSDKException) {
Log.e("VideoSDK", "Livestream request failed: ${e.code} ${e.name}")
}
}
}
Event associated with Livestream
- onLivestreamStateChanged - Whenever meeting livestream state changes, then
onLivestreamStateChangedevent will trigger.
private val meetingEventListener: MeetingEventListener = object : MeetingEventListener() {
override fun onLivestreamStateChanged(livestreamState: String) {
when (livestreamState) {
"LIVESTREAM_STARTING" -> Log.d( "LivestreamStateChanged",
"Meeting livestream is starting"
)
"LIVESTREAM_STARTED" -> {
isLivestreaming = true
Log.d( "LivestreamStateChanged", "Meeting livestream is started")
}
"LIVESTREAM_STOPPING" -> Log.d("LivestreamStateChanged",
"Meeting livestream is stopping"
)
"LIVESTREAM_STOPPED" -> {
isLivestreaming = false
Log.d("LivestreamStateChanged", "Meeting livestream is stopped")
}
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
//...
// add listener to meeting
meeting!!.addEventListener(meetingEventListener)
}
Custom Template
With VideoSDK, you can also use your own custom designed layout template to livestream the meetings. In order to use the custom template, you need to create a template for which you can follow this guide. Once you have setup 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

