Multi-streaming
Some special scenarios require live streaming over two or more separate channels. For example, consider the case of a real-time monitoring system, where camera feed is shared over two channels for two types of users. There are also certain applications where you want to send multiple streams over the same channel. Consider the case of a monitoring system where feeds from multiple cameras are shared on the same channel.
Agora IoT SDK multi-streaming allows you to join multiple channels at the same time or send multiple streams over a single channel. This page shows you how to implement two different multi-streaming methods into your app using IoT SDK. Choose the method that best fits your particular scenario.
Understand the tech
Agora IoT SDK provides the following approaches to implementing multi-streaming:
-
Push multiple streams to a single channel
To push multiple streams to a single channel, you create multiple connections using Agora engine. You call the join channel method multiple times with the same channel name but different connection Ids and distinct user Ids to set up multiple streams. To send audio or video data, you use the connection Id of the intended stream.
-
Stream to multiple channels
To stream over multiple channels, you create multiple connections using Agora engine. To join each channel, you use a distinct channel name and a dedicated connection Id. You use the connection Id to specify the intended channel when sending audio or video data, when leaving a particular channel, or when closing a connection.
The following figure shows the workflow you need to implement to add multi-streaming to your app:
Prerequisites
To follow this procedure you must have implemented the SDK quickstart for IoT SDK.
Project setup
In order to create the environment necessary to implement Agora multi-streaming feature into your app, open the IoT SDK SDK quickstart project you created previously.
Implement multi-streaming
This section shows you how to implement the following multi-streaming methods:
To implement either of the two multi-streaming methods, update the UI and then follow the step-by-step procedure in the relevant section.
Implement the user interface
You add two buttons to the UI to enable users to start and stop sending two streams.
-
In
/app/res/layout/activity_main.xml
, add the following code before</RelativeLayout>
:You see errors in your IDE. This is because this layout refers to methods that you create later.
-
To access and modify the buttons from your code, add the following to the list of
import
statements inMainActivity.java
:
Push multiple streams to a single channel
To send multiple audio and video streams to a single channel, take the following steps:
-
Declare the variables you need
To manage a second connection and a second stream, in
/app/java/com.example.<projectname>/MainActivity
, add the following variable declarations to theMainActivity
class: -
Create multiple connections
When the app starts, you create multiple connections. You can use each connection to send an audio and a video stream. To create two connections, in
setupAgoraRtcService
replace the code after// Create a connection
with the following: -
Join the same channel multiple times
When a user presses the Join button, you join the same channel twice with different user Ids and different connection Ids. To join the channel a second time, add the following code at the end of
joinChannel(View view)
: -
Set joined status
When your call to
agoraEngine.joinChannel
succeeds, you receive a notification through theonJoinChannelSuccess
callback. You use connection Id to identify the connection and set the correspondingisJoined
variable to true. To do this, replace theonJoinChannelSuccess
method underagoraRtcEvents
with the following: -
Stream video over the first connection
In this example, you start a video stream over the first connection and a second video stream over the second connection to the same channel. To start or stop the first stream when a user taps the Start stream 1 button, add the following method to the
MainActivity
class: -
Stream video over the second connection
To push another video stream to the same channel when a user taps the Start stream 2 button, add the following method to the
MainActivity
class: -
Leave all connections to a channel
To leave the channel on the second connection when a user taps Leave, add the following lines to
leaveChannel(View view)
afterint ret = agoraEngine.leaveChannel(connectionId);
: -
Destroy all connections
To close all connections when a user exits the app, add the following lines to
onDestroy
afteragoraEngine.destroyConnection(connectionId);
Stream to multiple channels
To send audio and video streams to multiple channels, take the following steps:
-
Declare the variables you need
To manage a second connection and join an additional channel, in
/app/java/com.example.<projectname>/MainActivity
, add the following variable declarations to theMainActivity
class: -
Create multiple connections
When the app starts, you create multiple connections. You can use each connection to send an audio and a video stream. To create two connections, in
setupAgoraEngine
replace the code after// Create a connection
with the following: -
Join multiple channels
When a user presses the Join button, you join two channels. To join a second channel, add the following code at the end of
joinChannel(View view)
: -
Set joined status
When your call to
agoraEngine.joinChannel
succeeds, you receive a notification through theonJoinChannelSuccess
callback. You use connection Id to identify the channel and set the correspondingisJoined
variable to true. To do this, replace theonJoinChannelSuccess
method underagoraRtcEvents
with the following: -
Stream audio to the first channel
In this example, you stream audio to the first channel and video to the second channel. To start or stop the audio stream when a user taps the Start stream 1 button, add the following method to the
MainActivity
class: -
Stream video to the second channel
To stream video to the second channel when a user taps the Start stream 2 button, add the following method to the
MainActivity
class: -
Leave all channels
To leave both channels when a user taps Leave, add the following lines to
leaveChannel(View view)
afterint ret = agoraEngine.leaveChannel(connectionId);
: -
Destroy all connections
To close all connections when a user exits the app, add the following lines to
onDestroy
afteragoraEngine.destroyConnection(connectionId);
Test your implementation
To ensure that you have implemented multi-streaming into your app, follow the relevant testing procedure:
Test pushing multiple streams to a single channel
-
Generate a temporary token in Agora Console.
-
In your browser, navigate to the Agora web demo and update App ID, Channel, and Token with the values for your temporary token, then click Join.
-
In Android Studio, in
app/java/com.example.<projectname>/MainActivity
, updateappId
,channelName
andtoken
with the values for your temporary token. -
Update
uid
anduid2
with distinct positive-integer values. -
Connect a physical Android device to your development device.
-
In Android Studio, click Run app. A moment later you see the project installed on your device. If this is the first time you run the project, you need to grant microphone and camera access to your app.
You see notifications confirming creation of two connections.
-
Click Join to join a channel over each connection.
You see notifications confirming joining success over each connection.
-
Click Start steam 1.
You see a video stream playing in the browser.
-
Click Start steam 2.
You see a second video stream playing in the same channel.
-
Try starting and stopping the two streams.
You see that the streams are independent of each other.
-
Click Leave to stop the two streams and exit the channel.
Test streaming to multiple channels
-
Generate two temporary tokens in Agora Console.
-
Generate
token
using theappId
andchannelName
. -
Generate
token2
using theappId
andchannelName2
.
-
-
In your browser, navigate to the Agora web demo and join a channel using
appId
,channelName
andtoken
. -
In another browser tab, join the Agora web demo using
appId
,channelName2
andtoken2
. -
In Android Studio, open
app/java/com.example.<projectname>/MainActivity
, and updateappId
,channelName
,channelName2
,token
andtoken2
with the values for your temporary tokens. -
Connect a physical Android device to your development device.
-
In Android Studio, click Run app. A moment later you see the project installed on your device. If this is the first time you run the project, you need to grant microphone and camera access to your app.
You see notifications confirming creation of two connections.
-
Click Join to join two channels.
You see notifications confirming success in joining each channel.
-
Click Start steam 1.
You hear an audio stream playing in the web demo connected to
channelName
. -
Click Start steam 2.
You see a video stream playing in the web demo connected to
channelName2
. -
Click the buttons to stop stream 1 and stream 2.
-
Click Leave to stop the two streams and exit the channel.
Reference
This section contains content that completes the information on this page, or points you to documentation that explains other aspects to this product.