Live streaming over multiple channels
Some special scenarios require streaming over two or more separate channels. Depending on the scenario, the game joins both channels as host, both channels as audience, or one channel as host and the other as audience. For example, consider the case of an online singing contest, where hosts of different channels interact with each other. The audience receives streams from two channels while the contestants broadcast to both channels.
Agora Video SDK multi-channel streaming allows you to join multiple channels at the same time and broadcast or receive audio and video over them. This page shows you how to implement two different multi-channel methods into your game using Video SDK. Choose the method that best fits your particular scenario.
Understand the tech
Agora Video SDK provides the following approaches to implementing multi-channel streaming:
-
Channel media relay
In channel media relay, one channel is used as the source channel from which audio and video is relayed to a second channel called the destination channel. The audience on the destination channel subscribes to the relayed media stream. A host can relay media stream to a maximum of four destination channels. Relaying media streams provides the following benefits:
- All hosts in the channels can see and hear each other.
- The audience in the channels can see and hear all hosts.
-
Join multiple channels
To join multiple channels, a host first sets up a primary channel and starts an event. The host then joins a second channel and publishes to the new channel. The audience joins either channel and subscribes to the host channel. The two channels are independent and users on one channels don't see users on the other channel. You can extend this functionality to join as many channels as required.
The following figure shows the workflow you need to implement to add multi-channel streaming to your game:
Prerequisites
To follow this procedure you must have implemented the SDK quickstart for Interactive Live Streaming project.
Project setup
In order to create the environment necessary to implement Agora multi-channel streaming into your game, do the following:
- Open the SDK quickstart Interactive Live Streaming project you created previously.
- Contact support@agora.io to enable channel media relay.
Implement multi-channel streaming
This section shows you how to implement the following methods of multi-channel streaming:
Choose the method that best suits your scenario and follow the step by step procedure.
Channel media relay
In this example, you use a single Button
to start and stop channel media relay.
Implement the user interface
To enable your users to start and stop relaying to another channel, add a Button
to the user interface by taking the following steps:
-
Right-click Sample Scene, then click GameObject > UI > Button - TextMeshPro. A button appears in the Scene Canvas.
-
In Inspector, rename Button to StartChannelMediaRelay, then change the following coordinates:
- Pos X - 350
- Pos Y - 172
Handle the system logic
In your project, import the relevant libraries and declare the required variables.
-
Declare the variables you need
To store source and destination channel settings and manage channel relay, in your script file, add the following declarations to
NewBehaviourScript
: -
Add the required UI namespace
To import the required Unity UI namespace, in your script file, add the following to the list of namespace declarations:
-
Access the channel relay button
To programmatically access the channel media relay button, add the following at the end of
SetupUI
:
Implement channel media relay
To enable game users to relay channel media to a destination channel, take the following steps:
- Start or stop channel media relay
When a user presses the button, the game starts relaying media from the source channel to the destination channel. If channel media relay is already running, the game stops it. To integrate this workflow, add the following method to NewBehaviourScript
.
- Monitor the channel media relay state
To receive the state change notifications sent during media relay, you add a method to UserEventHandler
. Your game responds to connection and failure events in the onChannelMediaRelayStateChanged
event handler. In your script file, add the following method after the OnUserOffline
:
- Monitor channel media relay events
To receive notifications of important channel relay events such as network disconnection, reconnection, and users joining channels, you add the onChannelMediaRelayEvent
method to UserEventHandler
. To implement this callback in your game, add the following method after OnChannelMediaRelayStateChanged
:
Join multiple channels
The alternate approach to multi-channel live streaming is joining multiple channels. In this section, you learn how to implement joining a second channel in your game.
Implement the user interface
In this example, you use a single Button
to join and leave a second channel. Add a Button
to the user interface by taking the following steps:
-
Right-click Sample Scene, then click UI > Button - TextMeshPro. A button appears in the Scene Canvas.
-
In Inspector, rename Button to multiChannel, then change the following coordinates:
- Pos X - -350
- Pos Y - 172
Handle the system logic
In your project, import the relevant libraries, declare the required variables, and acces the UI elements.
-
Declare the variables you need
To join and manage a second channel, in your script file, add the following declarations to
NewBehaviourScript
: -
Add the required UI namespace
To import the required Uinty UI namespace, in your script file, add the following to the list of namespace declarations:
-
Access the multiple channel button in your code
To programmatically access the button, add the following at the end of
SetupUI
:
Implement joining multiple channels
To add multi-channel functionality to your game, take the following steps:
-
Use the multi-channel enabled
RtcEngineEx
interfaceIn the SDK quickstart code, the Agora Engine instance was declared as
IRtcEngine
. To implement multi-channel functionality, you use the multi-channel interface of the Agora EngineIRtcEngineEx
. In order to switch to the multi-user interface, do the following:- In
NewBehaviourScript.cs
, replace the declarationinternal IRtcEngine RtcEngine;
with:
- In your script file, locate
SetupVideoSDKEngine
, replace the lineRtcEngine = Agora.Rtc.RtcEngine.CreateAgoraRtcEngine();
with the following:
- In
-
Join a second channel
When a user presses the button, the game joins a second channel. If the game is already connected to a second channel, it leaves the channel. To implement this workflow, add the following method to
NewBehaviourScript
: -
Update
onUserJoined
callback to render the second channel remote videoWhen the user presses Join Second Channel, you join the channel and wait for the second channel remote user to join the channel. When a remote user joins the channel second, your game stops rendering the first channel remote video and starts rendering the second channel remote video. To implement this workflow, in your script file, replace
onUserJoined
with the following:
Test your implementation
To ensure that you have implemented multi-channel streaming into your game, follow the relevant testing procedure:
Test channel media Relay
-
In Unity Editor, in your script file, update
_appID
,_channelName
anddestChannelName
. -
Generate a temporary token in using
_appID
and_channelName
. Use it to update_token
inNewBehaviourScript
. Use the same values to generate another token and updatesourceChannelToken
. -
Generate a third token in using
_appID
anddestChannelName
. Use it to updatedestChannelToken
inNewBehaviourScript
. -
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.
-
Repeat the previous step on a second device, but this time use
_appID
,destChannelName
, anddestChannelToken
to Join the channel. -
In Unity Editor, click Play. A moment later you see the game running on your development device.
-
Select Host and press Join. You see the video from the web browser demo app connected to
_channelName
in the local view of your device. -
Press Start Channel Media Relay. You see the video from the web browser demo app connected to
_channelName
relayed to the web browser demo app connected todestChannelName
. -
Press Stop Channel Media Relay. The media relaying is stopped.
Test joining multiple channels
-
In Unity Editor, in your script file, update
_appID
,_channelName
anddestChannelName
. -
Generate a temporary token in using
_appID
and_channelName
. Use it to update_token
inNewBehaviourScript
. -
Generate a second token in using
_appID
andsecondChannelName
. Use it to updatesecondChannelToken
inNewBehaviourScript
. -
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.
-
Repeat the previous step on a second device, but this time use
_appID
,secondChannelName
, andsecondChannelToken
to Join the channel. -
In Unity Editor, click Play. A moment later you see the game running on your development device.
-
Select Audience
- Press Join. You see the video from the web browser demo app connected to
_channelName
in the remote view of your development device. - Press Join Second Channel. Now, you see the video from the web browser demo app connected to
secondChannelName
in the remote view of your development device. - Press Leave Second Channel and then Leave to exit both channels.
- Press Join. You see the video from the web browser demo app connected to
-
Select Host
-
Press Join. You see the local video in the local view of your development device. The web browser demo app connected to
channelName
shows the video from your development device. -
Press Join Second Channel. The web browser demo app connected to
secondChannelName
plays only the audio stream from your development device. This is because you can publish video stream only in one channel. -
Press Leave Second Channel and then Leave to exit both channels.
-
Publish video stream in the second channel:
- Press Join Second Channel. The web browser demo app connected to
secondChannelName
shows the video from your development device. - Press Join. You see, the web browser demo app connected to
channelName
plays only the audio stream from your development device.
- Press Join Second Channel. The web browser demo app connected to
-
Reference
This section contains information that completes the information in this page, or points you to documentation that explains other aspects to this product.
- Ensure that each RtcConnection object has a unique user ID that is not 0.
- You can configure the publishing and subscribing options for the RtcConnection object in joinChannelEx.
- Each RtcConnection can publish multiple audio streams and a video stream simultaneously.
- When calls to
startOrUpdateChannelMediaRelay
are too frequent, they are rejected andRELAY_EVENT_PACKET_UPDATE_DEST_CHANNEL_REFUSED(8)
is returned.