RTMP Livestream - Android
RTMP is a popular protocol for live streaming video content from a VideoSDK to platforms such as YouTube, Twitch, Facebook, and others.
By providing the platform-specific stream key and stream URL, the VideoSDK can connect to the platform's RTMP server and transmit the live video stream.
VideoSDK allows you to livestream your meeting to platform which support RTMP ingestion just by providing the platform-specific stream key and stream URL, we can connect to the platform's RTMP server and transmit the live video stream.
VideoSDK also allows you to configure the livestream layouts in numerous ways like by simply setting different prebuilt layouts in the configuration or by providing your own custom template to do the livestream according to your layout choice.
This guide will provide an overview of how to implement start and stop RTMP Livestreaming.
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 ofLivestreamOutput
objects which contains the RTMPurl
andstreamKey
of the platforms you want to start the livestream. -
2. config
: This parameter will define how the livestream layout should look like. You can passnull
for default layout.
- Kotlin
- Java
val config = JSONObject()
// Layout Configuration
val layout = JSONObject()
JsonUtils.jsonPut(layout, "type", "GRID") // "SPOTLIGHT" | "SIDEBAR", Default : "GRID"
JsonUtils.jsonPut(layout, "priority", "SPEAKER") // "PIN", Default : "SPEAKER"
JsonUtils.jsonPut(layout, "gridSize", 4) // MAX : 4
JsonUtils.jsonPut(config, "layout", layout)
// Theme of livestream layout
JsonUtils.jsonPut(config, "theme", "DARK") // "LIGHT" | "DEFAULT"
val outputs: MutableList<LivestreamOutput> = ArrayList()
outputs.add(LivestreamOutput(RTMP_URL, RTMP_STREAM_KEY))
meeting!!.startLivestream(outputs,config)
JSONObject config = new JSONObject();
// Layout Configuration
JSONObject layout = new JSONObject();
JsonUtils.jsonPut(layout, "type", "GRID"); // "SPOTLIGHT" | "SIDEBAR", Default : "GRID"
JsonUtils.jsonPut(layout, "priority", "SPEAKER"); // "PIN", Default : "SPEAKER"
JsonUtils.jsonPut(layout, "gridSize", 4); // MAX : 4
JsonUtils.jsonPut(config, "layout", layout);
// Theme of livestream layout
JsonUtils.jsonPut(config, "theme", "DARK"); // "LIGHT" | "DEFAULT"
List<LivestreamOutput> outputs = new ArrayList<>();
outputs.add(new LivestreamOutput(RTMP_URL, RTMP_STREAM_KEY));
meeting.startLivestream(outputs,config);
stopLivestream()
stopLivestream()
is used to stop the meeting livestream which can be accessed from theMeeting
class.
Example
- Kotlin
- Java
// keep track of livestream status
val liveStream = false
findViewById<View>(R.id.btnLiveStream).setOnClickListener { view: View? ->
if (!liveStream) {
val config = JSONObject()
val layout = JSONObject()
JsonUtils.jsonPut(layout, "type", "GRID")
JsonUtils.jsonPut(layout, "priority", "SPEAKER")
JsonUtils.jsonPut(layout, "gridSize", 4)
JsonUtils.jsonPut(config, "layout", layout)
JsonUtils.jsonPut(config, "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()
}
}
// keep track of livestream status
boolean liveStream = false;
findViewById(R.id.btnLiveStream).setOnClickListener(view -> {
if (!liveStream) {
JSONObject config = new JSONObject();
JSONObject layout = new JSONObject();
JsonUtils.jsonPut(layout, "type", "GRID");
JsonUtils.jsonPut(layout, "priority", "SPEAKER");
JsonUtils.jsonPut(layout, "gridSize", 4);
JsonUtils.jsonPut(config, "layout", layout);
JsonUtils.jsonPut(config, "theme", "DARK");
List<LivestreamOutput> outputs = new ArrayList<>();
outputs.add(new LivestreamOutput(RTMP_URL, RTMP_STREAM_KEY));
// Start LiveStream
meeting.startLivestream(outputs,config);
} else {
// Stop LiveStream
meeting.stopLivestream();
}
});
Event associated with Livestream
- onLivestreamStateChanged - Whenever meeting livestream state changes, then
onLivestreamStateChanged
event will trigger.
- Kotlin
- Java
private val meetingEventListener: MeetingEventListener = object : MeetingEventListener() {
override fun onLivestreamStateChanged(livestreamState: String?) {
when (livestreamState) {
"LIVESTREAM_STARTING" -> Log.d( "LivestreamStateChanged",
"Meeting livestream is starting"
)
"LIVESTREAM_STARTED" -> Log.d( "LivestreamStateChanged",
"Meeting livestream is started"
)
"LIVESTREAM_STOPPING" -> Log.d("LivestreamStateChanged",
"Meeting livestream is stopping"
)
"LIVESTREAM_STOPPED" -> Log.d("LivestreamStateChanged",
"Meeting livestream is stopped"
)
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
//...
// add listener to meeting
meeting!!.addEventListener(meetingEventListener)
}
private final MeetingEventListener meetingEventListener = new MeetingEventListener() {
@Override
public void onLivestreamStateChanged(String livestreamState) {
switch (livestreamState) {
case "LIVESTREAM_STARTING":
Log.d("LivestreamStateChanged", "Meeting livestream is starting");
break;
case "LIVESTREAM_STARTED":
Log.d("LivestreamStateChanged", "Meeting livestream is started");
break;
case "LIVESTREAM_STOPPING":
Log.d("LivestreamStateChanged", "Meeting livestream is stopping");
break;
case "LIVESTREAM_STOPPED":
Log.d("LivestreamStateChanged", "Meeting livestream is stopped");
break;
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
//...
// add listener to meeting
meeting.addEventListener(meetingEventListener);
}
API Reference
The API references for all the methods utilised in this guide are provided below.
Got a Question? Ask us on discord