Co-hosting across channels refers to the process where the Agora SDK relays the media stream of a host from an interactive live streaming channel (source channel) to a maximum of four interactive live streaming channels (destination channels). It has the following benefits:
Co-hosting across channels applies to scenarios such as an online singing contest, where hosts of different channels interact with each other.
We provide an open-source channel media relay demo project that implements channel media relay on GitHub. You can try the demo and view the source code.
Before relaying media streams across channels, ensure that you have implemented the basic real-time communication functions in your project. For details, see the following documents:
As of v2.9.0, the Agora Native SDK supports co-hosting across channels with the following methods:
startChannelMediaRelay
updateChannelMediaRelay
stopChannelMediaRelay
Once the channel media relay starts, the SDK relays the media streams of a host from the source channel to at most four destination channels.
During the relay, the SDK reports the states and events of the channel media relay with the channelMediaRelayStateDidChange
and didReceiveChannelMediaRelayEvent
callbacks. Refer to the following codes and their corresponding states to implement you code logic:
State codes | Event codes | The media stream relay state |
---|---|---|
AgoraChannelMediaRelayStateRunning(2) and AgoraChannelMediaRelayErrorNone(0) | AgoraChannelMediaRelayEventSent-ToDestinationChannel(4) | The channel media relay starts. |
AgoraChannelMediaRelayStateFailure(3) | / | Exceptions occur for the media stream relay. Refer to the error parameter for troubleshooting. |
AgoraChannelMediaRelayStateIdle(0) and AgoraChannelMediaRelayErrorNone(0) | / | The channel media relay stops. |
Note:
startChannelMediaRelay
method to enable channel media stream relay. The SDK relays the media streams of the host who calls the method.didOfflineOfUid
callback.Follow the API call sequence to implement your code logic:
// Configures the information of the source channel. Set channelName as the default value, meaning the current channel name. Set uid as the default value 0.
let config = AgoraChannelMediaRelayConfiguration()
// Note that sourceChannelToken is different from the token used for joining the source channel. You need to generate sourceChannelToken with the source channel name and a uid of 0.
config.sourceInfo = AgoraChannelMediaRelayInfo(token: sourceChannelToken)
// Configures the information of the destination channel.
let destinationInfo = AgoraChannelMediaRelayInfo(token: destinationChannelToken)
config.setDestinationInfo(destinationInfo, forChannelName: destinationChannelName)
// Starts the channel media relay.
agoraKit.startChannelMediaRelay(config)
// Stops the channel media relay.
agoraKit.stopChannelMediaRelay()
// Uses the channelMediaRelayStateDidChange callback to monitor the state of the channel media relay.
func rtcEngine)(_ engine: AgoraRtcEngineKit, channelMediaRelayStateDidChange state: AgoraChannelMediaRelayState, error: AgoraChannelMediaRelayError) {
LogUtils.log(message: "channelMediaRelayStateDidChange: \(state.rawVlue) error \(error.rawValue)", level: .info)
switch(state){
case .running:
isRelaying = true
break
case .failure:
showAlert(message: "Media Relay Failed: \(error.rawValue)")
isRelaying = false
break
case .idle:
isRelaying = false
break
default:break
}
}
startChannelMediaRelay
method, you can call the updateChannelMediaRelay
method to add or delete the relay channels.startChannelMediaRelay
updateChannelMediaRelay
stopChannelMediaRelay
channelMediaRelayStateDidChange
didReceiveChannelMediaRelayEvent
updateChannelMediaRelay
.When setting the source channel information, ensure that you set uid
as 0, and the uid
that you use to generate the token should also be set as 0.
To call startChannelMediaRelay
again after it succeeds, you must call stopChannelMediaRelay
to quit the current relay.