Join Multiple Channels
Introduction
As of v3.0.0, the Agora Video SDK enables users to join an unlimited number of channels and to receive the audio and video streams of all those channels.
Sample project
Agora provides the following open-source sample projects on GitHub. You can download them and refer to the source code.
- Publish the local stream in an
AgoraRtcChannel
channel:- iOS: JoinMultiChannel
- macOS: JoinMultiChannel
- Publish the local stream in an
AgoraRtcEngineKit
channel: Breakout Class (iOS)
Implementation
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 channelId
, and then call joinChannelByToken
in AgoraRtcChannel
to join the channel.
The following are the major steps of implementing the multi-channel function:
- Call
sharedEngineWithAppId
to create and initializeAgoraRtcEngineKit
. - Call
setChannelProfile
to set the channel profile as live streaming. - Call
createRtcChannel
to create anAgoraRtcChannel
object with achannelId
. - Call
setRtcChannelDelegate
of theAgoraRtcChannel
class to receive callbacks in this channel. - Call
setClientRole
of theAgoraRtcChannel
class to set the user role. - Call
joinChannelByToken
of theAgoraRtcChannel
class to join the channel. After joining a channel, the user publishes the local streams and automatically subscribes to the streams of all the other users in the channel by default. You can set the publishing and subscribing states inChannelMediaOptions
when joining a channel, and you can change the publishing and subscribing states in the methods with prefixesmuteLocal
andmuteRemote
of theAgoraRtcChannel
class after joining a channel. - Repeat steps 3, 4, 5, and 6 to join more channels.
API call sequence
Refer to the following API sequence to join multiple channels:
Sample code
The following code demonstrates how to join an AgoraRtcChannel
channel, and then publish the local streams in the first channel.
- Create and initialize
AgoraRtcEngineKit
.
- Enable the video module.
- Set the channel profile as interactive live streaming.
- Create an
AgoraRtcChannel1
object, and listen for events in this channel.
- Set the user role as a host.
- Pass in
token
andchannelId
to join theAgoraRtcChannel1
channel. The SDK publishes the local streams and automatically subscribes to the streams of all the other users in the channel by default.
- Create an
AgoraRtcChannel2
object, and listen for events in this channel.
- Set the user role as an audience member. Note that audience members cannot publish local streams.
- Pass in
token
andchannelId
to join theAgoraRtcChannel2
channel. You need to setpublishLocalAudio
andpublishLocalVideo
toNO
; otherwise, the user fails to join the channel.
- Leave and destroy the
AgoraRtcChannel2
channel.
- Leave and destroy the
AgoraRtcChannel1
channel.
- Destroy the
AgoraRtcEngineKit
object.
API reference
Considerations
Subscribing limits
joinChannelByToken
in the AgoraRtcChannel
class provides the media-subscription options (autoSubscribeAudio
and autoSubscribeVideo
) that determine whether to automatically subscribe to the remote streams after joining the channel. The default setting is to subscribe to all the streams automatically. After a user joins a channel, you can change the subscribing state by calling muteRemoteAudioStream
or muteRemoteVideoStream
.
If you need to subscribe to the streams of specified users after joining an AgoraRtcChannel
channel, refer to the following steps:
- Call
joinChannelByToken
and setautoSubscribeAudio = NO
orautoSubscribeVideo = NO
ofChannelMediaOptions
to unsubscribe from all remote users. - In the channel, call
muteRemoteAudioStream (uid,NO)
ormuteRemoteVideoStream(uid,NO)
to subscribe to specified remote users.
Set the remote video view
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.
Publishing limits
The SDK supports publishing local streams to only one channel at a time. Agora recommends setting the user role as an audience when joining a channel where the user do not need to publish streams, and setting publishLocalAudio
and publishLocalVideo
in ChannelMediaOptions
to false
when joining the channel.
In the interactive streaming profile, if a user joins the first channel as a host, the SDK publishes local streams in the first channel by default. If the user needs to join a second channel, you need to change the publishing state according to your actual scenario:
- If the user does not need to publish local streams in the second channel, Agora recommends joining the second channel as an audience member (
setClientRole(Audience)
). - If the user needs to publish local streams in the second channel, the user needs to stop publishing in the current channel before joining the second channel. Agora recommends setting the user role as an audience member (
setClientRole(Audience)
) in the first channel and joining the second channel as a host (setClientRole(Broadcaster)
).
If a user publishes local streams in a channel and you call the following methods in a second channel, the method call fails, and the SDK returns -5(AgoraErrorCodeRefused)
:
- Set
publishLocalAudio = YES
orpublishLocalVideo = YES
when joining the second channel. - Call
muteLocalAudioStream(NO)
ormuteLocalVideoStream(NO)
after joining the second channel. - Call
setClientRole(Broadcaster)
.
API changes
As of v3.4.5, the AgoraRtcChannel
class changes as follows:
- Deprecates
publish
andunpublish
, and addsmuteLocalAudioStream
andmuteLocalVideoStream
instead. After joining a channel, you can set the publishing state of the audio stream and video stream separately.muteLocalAudioStream
andmuteLocalVideoStream
in theAgoraRtcEngineKit
andAgoraRtcChannel
classes control the publishing state of each channel in their respective classes only. - Adds the
publishLocalAudio
andpublishLocalVideo
members inAgoraRtcChannelMediaOptions
. The default value isYES
. You can calljoinChannelByToken
to join a channel and set the publishing state. If a user publishes streams in a channel, regardless of whether the user is a host or an audience member, they need to setpublishLocalAudio
andpublishLocalVideo
toNO
when joining other channels; otherwise, they fail to join the channel. - After calling
setClientRole(Broadcaster)
, the local user publishes audio and video streams by default. You no longer need to callpublish
.
Earlier than v3.4.5:
- Calling
muteLocalAudioStream(true)
ormuteLocalVideoStream(true)
inAgoraRtcEngineKit
takes effect in channels created by both theAgoraRtcEngineKit
andAgoraRtcChannel
classes.muteLocalAudioStream
andmuteLocalVideoStream
take effect when they are called before or after a user joins a channel. joinChannelByToken
cannot set the publishing state of local streams.- Calling
setClientRole(Broadcaster)
of theAgoraRtcChannel
class does not publish local streams. You also need to callpublish
.