Join multiple channels
Updated
Broadcast or subscribe to multiple channels.
Agora Video SDK enables you to simultaneously join multiple channels. This capability allows you to receive and publish audio and video streams across multiple channels concurrently.
Understand the tech
Video SDK's multi-channel functionality is based on two key components:
-
RtcConnectionThe
RtcConnectionobject identifies a connection. It contains the following information:- Channel name
- User ID of the local user
You create multiple
RtcConnectionobjects, each with a different channel name and user ID. EachRtcConnectioninstance can independently publish multiple audio streams and a single video stream. -
IRtcEngineExThe class contains methods tailored for interacting with a designated
RtcConnectionobject.To join multiple channels, you call
joinChannelExmethod in theIRtcEngineExclass multiple times, using a differentRtcConnectioninstance each time.
When joining multiple channels:
-
Ensure that the user ID for each
RtcConnectionobject is unique and nonzero. -
Configure publishing and subscribing options for the
RtcConnectionobject injoinChannelEx. -
Pass the
IRtcEngineEventHandlerobject to theeventHandlerparameter when calling thejoinChannelExmethod to receive multiple channel-related event notifications.
Prerequisites
Ensure that you have implemented the SDK quickstart in your project.
Implementation
This section explains how to join a second channel as a host after you have already joined the first channel.
-
Define a connection and join the channel using a random user ID.
agora::rtc::ChannelMediaOptions options2; options2.autoSubscribeAudio = false; options2.autoSubscribeVideo = false; options2.publishAudioTrack = false; options2.publishCameraTrack = false; options2.publishSecondaryCameraTrack = true; options2.clientRoleType = CLIENT_ROLE_BROADCASTER; // Define the connection connection.localUid = generateUid(); connection.channelId = szChannelId.data(); // Join the channel int ret = m_rtcEngine->joinChannelEx(APP_TOKEN, connection, options2, &m_camera2EventHandler); -
Listen for the
onUserJoinedcallback and set up the remote video.// Listen for the onUserJoined callback void CAGEngineEventHandler::onUserJoined(uid_t uid, int elapsed) { LPAGE_USER_JOINED lpData = new AGE_USER_JOINED; lpData->uid = uid; lpData->elapsed = elapsed; if (m_hMainWnd != NULL) ::PostMessage(m_hMainWnd, WM_MSGID(EID_USER_JOINED), (WPARAM)lpData, 0); } -
Set up the remote video
VideoCanvas canvas; canvas.renderMode = RENDER_MODE_FIT; canvas.uid = 123; canvas.view = videoWnd.GetSafeHwnd(); connection.localUid = 123; connection.channelId = "channel123"; // Set up the remote video rtcEngine->setupRemoteVideoEx(canvas, connection);
Reference
This section contains content that completes the information on this page, or points you to documentation that explains other aspects to this product.
Sample project
Agora provides the MultiChannels open-source sample project for your reference. Download the project or view the source code for a more detailed example.
