As of v3.0.0, the Agora RTC SDK enables users to join an unlimited number of channels and to receive the audio and video streams of all those channels.
Agora provides the following open-source sample projects on GitHub. You can download them and refer to the source code.
AgoraRtcChannel
channel:AgoraRtcEngineKit
channel: Breakout Class (iOS)The SDK uses the AgoraRtcChannel
and AgoraRtcChannelDelegate
classes to support the multi-channel function.
You can call createRtcChannel
multiple times to create multiple AgoraRtcChannel
objects with different channel names, and then call joinChannelByToken
in AgoraRtcChannel
to join the channel.
The following are the major steps of implementing the multi-channel function:
sharedEngineWithAppId
to create and initialize AgoraRtcEngineKit
.setChannelProfile
to set the channel profile as live streaming.createRtcChannel
to create an AgoraRtcChannel
object with channelId
.setRtcChannelDelegate
of the AgoraRtcChannel
class to receive the callbacks in this channel.joinChannelByToken
of the AgoraRtcChannel
class to join the channel.AgoraRtcEngineKit
channel: Call setClientRole
of the AgoraRtcEngineKit
class to set the user role as host, and then call joinChannelByToken
to join the channel. The SDK automatically publishes the local stream after joining the channel. This method applies to scenarios where the local stream is published in a fixed channel.AgoraRtcChannel
channel: Call setClientRole
of the AgoraRtcChannel
class to set the user role as host, and then call publish
to publish the local stream. This method applies to scenarios where the local stream needs to be published to different channels.publish
of the AgoraRtcChannel
class, ensure that the user role is set as host by calling setClientRole
.AgoraRtcChannel
channel, and you must call unpublish
before publishing the local stream to another channel.Refer to the following API sequence to join multiple channels and publish the local stream in an AgoraRtcEngineKit
channel:
Refer to the following API sequence to join multiple channels and publish the local stream in an AgoraRtcChannel
channel:
The following code demonstrates how to create two AgoraRtcChannel
objects to join two channels and publish the local stream in one channel.
Initialize AgoraRtcEngineKit
.
// Swift
let config = AgoraRtcEngineConfig()
config.appId = KeyCenter.AppId
agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
Set the channel profile as live streaming.
// Swift
agoraKit.setChannelProfile(.liveBroadcasting)
Create channel1
, and listen for events in this channel.
// Swift
channel1 = agoraKit.createRtcChannel(channelName1)
channel1?.setRtcChannelDelegate(self)
Join channel1
, and publish the local stream.
// Swift
// Set the usr role as host.
channel1?.setClientRole(.broadcaster)
// Join channel1.
var result = channel1?.join(byToken: token1, info: nil, uid: 0, options: mediaOptions) ?? -1
// Report errors if failed to join the channel.
if result != 0 {
self.showAlert(title: "Error", message: "joinChannel1 call failed: \(result), please check your params")
}
// Call publish for channel1. You can only publish in one channel.
channel1?.publish()
Create and join channel2
.
// Swift
// Create channel2.
channel2 = agoraKit.createRtcChannel(channelName2)
channel2?.setRtcChannelDelegate(self)
// Join channel2.
result = channel2?.join(byToken: token2, info: nil, uid: 0, options: mediaOptions) ?? -1
// Report errors if failed to join the channel.
if result != 0 {
self.showAlert(title: "Error", message: "joinChannel2 call failed: \(result), please check your params")
}
Leave and destroy channel1
and channel2
.
// Swift
channel1?.leave()
channel1?.destroy()
channel2?.leave()
channel2?.destroy()
If you set autoSubscribeAudio
or autoSubscribeVideo
as false, the method call of muteRemoteAudioStream
or muteRemoteVideoStream
does not take effect after joining an AgoraRtcChannel
channel.
joinChannelByToken
in the AgoraRtcChannel
class provides the media subscription options (autoSubscribeAudio
and autoSubscribeVideo
) to determine whether to subscribe to the remote streams after joining the channel. The default setting is to subscribe to all streams automatically.If you need to subscribe to the streams of specified users after joining an AgoraRtcChannel
channel, refer to the following steps:
setDefaultMuteAllRemoteAudioStreams
or setDefaultMuteAllRemoteVideoStreams
in the AgoraRtcChannel
class and set the mute
parameter as true to not receive any audio or video streams.joinChannelByToken
with default media subscription options to join the channel.muteRemoteAudioStream
or muteRemoteVideoStream
and set the mute
parameter as false to subscribe to the streams of the specified users.In video scenarios, if a remote user joins the channel using AgoraRtcChannel
, ensure that you specify the channel ID of the remote user in AgoraRtcVideoCanvas
when setting the remote video view.
// Swift
// Set the remote user's view in the didJoinedOfUid callback
func rtcChannel(_ rtcChannel: AgoraRtcChannel, didJoinedOfUid uid: UInt, elapsed: Int) {
LogUtils.log(message: "remote user join: \(uid) \(elapsed)ms", level: .info)
let videoCanvas = AgoraRtcVideoCanvas()
videoCanvas.uid = uid
// The view to bind to
videoCanvas.view = channel1 == rtcChannel ? channel1RemoteVideo.videoView : channel2RemoteVideo.videoView
videoCanvas.renderMode = .hidden
// Specify the channelId of the remote user
videoCanvas.channelId = rtcChannel.getId()
// Set the view for the remote user
agoraKit.setupRemoteVideo(videoCanvas)
}
The SDK supports publishing one stream to only one channel at a time, and the default behavior differs after the method call ofjoinChannelByToken
in the AgoraRtcEngineKit
and AgoraRtcChannel
classes:
AgoraRtcEngineKit
class, the SDK publishes the local stream by default.AgoraRtcChannel
class, the SDK does not publish the local stream.You must meet the following requirements to implement the multi-channel function. Otherwise, the SDK returns AgoraErrorCodeRefused(-5)
:
When joining multiple channels using the joinChannelByToken
method in the AgoraRtcChannel
class: After calling publish
for Channel 1, you need to call unpublish
for Channel 1 before calling publish
for Channel 2.
When joining multiple channels using both the AgoraRtcEngineKit
and AgoraRtcChannel
classes:
AgoraRtcEngineKit
class and Channel 2 using the AgoraRtcChannel
class, you cannot call publish
in the AgoraRtcChannel
class.AgoraRtcEgineKit
class and Channel 2 through the AgoraRtcChannel
class, you cannot call publish
in the AgoraRtcChannel
class.publish
method in the AgoraRtcChannel
class as an audience member.publish
in the AgoraRtcChannel
class, you need to call unpublish
in the AgoraRtcChannel
class; otherwise, you cannot call joinChannelByToken
in the AgoraRtcEngineKit
class.