Cross-channel media stream relay
Updated
Forward the media stream from a source channel to multiple target channels at the same time
Some special use-cases require cross-channel media stream forwarding functionality. RTC SDK enables you to relay the media stream of a host, from a source channel, to multiple target channels simultaneously. This functionality allows you to realize the following interactions:
-
The hosts publish and receive each other's audio and video streams while engaging in cross-channel real-time interaction.
-
The audience receive all audio and video streams from hosts and watch multiple hosts interact at the same time.
Due to its real-time and interactive nature, this feature enriches live broadcasts and game-play, It is especially suitable for live scenes such as co-hosting PK and online choir. It provides the audience with a better viewing experience, while bringing more traffic and revenue to the hosts.
Cross-channel media stream relay is included in Agora's policy of 10,000 free minutes every month. For usage beyond the free quota, please refer to Pricing.
Prerequisites
- Ensure that you have implemented the SDK quickstart in your project.
- Contact support@agora.io to activate the cross-channel media stream relay feature.
Implement cross-channel media stream relay
To implement cross-channel media stream relay in your app, take the following steps:
-
Configure cross-channel media stream relay
To create a cross-channel media stream relay configuration object, and set the source and target channel information, call
createChannelMediaRelayConfiguration.const channelMediaConfig = AgoraRTC.createChannelMediaRelayConfiguration(); // Set source channel information channelMediaConfig.setSrcChannelInfo({ channelName: "srcChannel", uid: <USER_UID>, token: "yourSrcToken", }); // Set target channel information. // It can be called multiple times, for up to 4 target channels channelMediaConfig.addDestChannelInfo({ channelName: "destChannel1", uid: 123, token: "yourDestToken", });- Set the source channel
uidto a value different from the current host'suid. Best practice is to set it to0so the server assigns a randomuid. - Cross-channel media stream relay doesn't support String usernames.
- Set the source channel
-
Start cross-channel media stream relay
To start cross-channel media stream relay, call
startChannelMediaRelayafter callingAgoraRTCClient.publish. The SDK forwards the stream of the host who calls this method, and multiple hosts in a channel can forward media streams. To callstartChannelMediaRelayagain after a successful call, first callstopChannelMediaRelay.client.startChannelMediaRelay(channelMediaConfig).then(() => { console.log(`startChannelMediaRelay success`); }).catch(e => { console.log(`startChannelMediaRelay failed`, e); });clientrefers to the local client object you create usingAgoraRTC.createClient. -
Listen for cross-channel media stream status changes
During cross-channel media stream relay, the SDK reports the status of media stream relay through
AgoraRTCClient.on("channel-media-relay-state")callback with state codes and error codes. Listen to theAgoraRTCClient.on("channel-media-relay-event")callback for relay events. After you callstartChannelMediaRelayorupdateChannelMediaRelaysuccessfully, users in the target channels receive theAgoraRTCClient.on("user-published")callback. If a host in a target channel goes offline or leaves the channel during relay, the host in the source channel receives theAgoraRTCClient.on("user-left")callback. -
Update media stream relay channels
To add or remove target channels after successfully calling
startChannelMediaRelay, callupdateChannelMediaRelay. You can forward a media stream to up to four target channels.// Remove a target channel channelMediaConfig.removeDestChannelInfo("destChannel1"); // Update cross-channel media stream relay settings client.updateChannelMediaRelay(channelMediaConfig).then(() => { console.log("updateChannelMediaRelay success"); }).catch(e => { console.log("updateChannelMediaRelay failed", e); }); -
Stop cross-channel media stream relay
To stop cross-channel media stream relay, call
stopChannelMediaRelay.client.stopChannelMediaRelay().then(() => { console.log("stop media relay success"); }).catch(e => { console.log("stop media relay failed", e); });
Reference
This section contains content that completes the information on this page, or points you to documentation that explains other aspects to this product.
