The Agora RTC SDK enables users to join an unlimited number of channels at the same time and receive the audio and video streams of all the channels.
The SDK uses the RtcConnection
and IRtcEngineEx
classes to support the multi-channel functionality. IRtcEngineEx
provides methods that apply to a specific RtcConnection
object.
An RtcConnection
object contains the following information to identify a connection:
You can create multiple RtcConnection
objects, each with a different channel name and user ID.
To join multiple channels, call joinChannelEx
in the IRtcEngineEx
class multiple times with different RtcConnection
objects.
RtcConnection
object has a unique user ID that is not 0
.RtcConnection
object in joinChannelEx
.RtcConnection
can publish multiple audio streams and a unique video stream independently.Before proceeding, ensure that you have implemented the basic real-time communication functions in your project. For details, see Start a Video Call or Start Interactive Live Streaming.
To implement the multi-channel functionality, do the following for each channel:
Define a connection, and join a channel with 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 a connection
connection.localUid = generateUid();
connection.channelId = szChannelId.data();
// Join a channel
int ret = m_rtcEngine->joinChannelEx(APP_TOKEN, connection, options2, &m_camera2EventHandler);
Listen for the onUserJoined
callback, 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);
}
VideoCanvas canvas;
canvas.renderMode = RENDER_MODE_FIT;
canvas.uid = 123;
canvas.view = videoWnd.GetSafeHwnd();
connection.localUid =123;
connection.channelId = "channel123";
// Set the remote video view
rtcEngine->setupRemoteVideoEx(canvas, connection);
This section provides the reference information you might need when implementing the multi-channel functionality.
Agora provides an open-source sample project MultiCamera on GitHub. You can download it and refer to the source code.