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 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 of LivestreamOutput objects which contains the RTMP url and streamKey of 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 pass null for 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 meeting 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.
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 the Meeting class.

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 onLivestreamStateChanged event 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