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:

  • RtcConnection

    The RtcConnection object identifies a connection. It contains the following information:

    • Channel name
    • User ID of the local user

    You create multiple RtcConnection objects, each with a different channel name and user ID. Each RtcConnection instance can independently publish multiple audio streams and a single video stream.

  • IRtcEngineEx

    The class contains methods tailored for interacting with a designated RtcConnection object.

    To join multiple channels, you call joinChannelEx method in the IRtcEngineEx class multiple times, using a different RtcConnection instance each time.

When joining multiple channels:

  • Ensure that the user ID for each RtcConnection object is unique and nonzero.

  • Configure publishing and subscribing options for the RtcConnection object in joinChannelEx.

  • Pass the IRtcEngineEventHandler object to the eventHandler parameter when calling the joinChannelEx method 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.

  1. 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);
  2. 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);
    }
  3. 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.

API reference