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 AgoraRtcConnection
and AgoraRtcEngineKit(Ex)
classes to support the multi-channel functionality. AgoraRtcEngineKit(Ex)
provides methods that apply to a specific AgoraRtcConnection
object.
An AgoraRtcConnection
object contains the following information to identify a connection:
You can create multiple AgoraRtcConnection
objects, each with a different channel name and user ID.
To join multiple channels, call joinChannelExByToken
in the AgoraRtcEngineKit(Ex)
class multiple times with different AgoraRtcConnection
objects.
AgoraRtcConnection
object has a unique user ID that is not 0
.AgoraRtcConnection
object in joinChannelExByToken
.AgoraRtcConnection
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 the following quickstart guides:
To implement the multi-channel functionality, do the following for each channel:
Define an AgoraRtcConnection
object in the ViewController.swift
file of your project.
let connection1 = AgoraRtcConnection()
Join a channel with a random user ID.
var mediaOptions = AgoraRtcChannelMediaOptions()
mediaOptions.autoSubscribeVideo = .of(true)
mediaOptions.autoSubscribeAudio = .of(true)
connection1.channelId = channelName1
connection1.localUid = UInt.random(in: 1001...2000)
var result = agoraKit.joinChannelEx(byToken: "your token", connection: connection1, delegate: channel1, mediaOptions: mediaOptions, joinSuccess: nil)
channel1.channelId = channelName1
channel1.connecitonDelegate = self
if result != 0 {
self.showAlert(title: "Error", message: "joinChannel1 call failed: \(result), please check your params")
}
Set up the remote video in the didJoinedOfUid
callback.
func rtcEngine(_ engine: AgoraRtcEngineKit, channelId: String, didJoinedOfUid uid: UInt, elapsed: Int) {
LogUtils.log(message: "remote user join: \(uid) \(elapsed)ms", level: .info)
// Only one remote video view is available for this
// tutorial. Here we check if there exists a surface
// view tagged as this uid.
let videoCanvas = AgoraRtcVideoCanvas()
videoCanvas.uid = uid
// the view to be bound
videoCanvas.view = channelId == channelName1 ? channel1RemoteVideo.videoView : channel2RemoteVideo.videoView
videoCanvas.renderMode = .hidden
let connection = AgoraRtcConnection()
agoraKit.setupRemoteVideoEx(videoCanvas, connection: connection)
}
This section provides the reference information you might need when implementing the multi-channel functionality.
Agora provides the following open-sourced sample projects that implement the multi-channel functionality. You can download them and refer to the source code.