Video Live Broadcast and Viewing
this document primarily introduces how to use
RTC Room Engine
SDK to implement video live broadcast and viewing functionality.Prerequisites
Before using the
RTC RoomEngine
SDK, you need to call the Log In SDK first so that subsequent features can be used normally.Video Live Broadcast
Using
RTC Room Engine
to implement voice chat live streaming requires 4 core steps: create and join a live room, become a speaker and push the stream, enable media devices, set the local preview screen. The following provides a detailed introduction.Step 1: Create and Join a Live Room
Before going live, you need to fill in the key parameters
TUIRoomInfo
. Next, we will provide a detailed introduction:Parameter: TUIRoomInfo
TUIRoomInfo
is composed of many fields, but you only need to care about filling in the following fields:Parameter Name | Field Description | Additional Notes | Data Type | Sample for Completion |
roomId | room ID | Only allow combination of uppercase and lowercase letters (a-z, A-Z), digits (0-9), underline and hyphen. This field is a required parameter when creating a room. It supports up to 48 bytes. | New Character String | "live_100001" |
name | room name | String type room name. (If left empty, it will be consistent with the roomId.) | New Character String | "denny`s room" |
seatMode | Mic on mode | This field takes effect only after microphone position control is enabled. Divide into "Free to Take the Podium mode (freeToTake)" and "Apply to Take the Podium mode (applyToTake)". In Free to Take the Podium mode, audiences can freely become speakers without application. In Mic on mode, audiences need the room owner's approval after consent to become speakers. | Enumeration Value | TUISeatMode.applyToTake |
maxSeatCount | maximum number of microphone slots | The maximum number of microphone slots of digit type means that a maximum of maxSeatCount people can be online simultaneously here. The maximum number of microphone slots matches the upper limit of the maximum number of mic-on users of the purchased package. | Digits | 10 |
isSeatEnabled | Whether microphone position control is enabled? | Boolean type microphone position control enable flag. | Boolean value | true |
roomType | Room type | Divide into two room types: "conference" and "live". Voice chat belongs to live streaming, so use live here. | Enumeration Value | TUIRoomType.live |
After preparing the parameter
TUIRoomInfo
, you can call the API functions createRoom
and enterRoom
to create and join a live room.Before going live, you need to fill in the key parameters
RoomInfo
. Next, we will provide a detailed introduction:Parameter: RoomInfo
RoomInfo
is composed of many fields, but you only need to care about filling in the following fields:Parameter Name | Field Description | Additional Notes | Data Type | Sample for Completion |
roomId | room ID | Only allow combination of uppercase and lowercase letters (a-z, A-Z), digits (0-9), underline and hyphen. This field is a required parameter when creating a room. It supports up to 48 bytes. | New Character String | "live_100001" |
name | room name | String type room name. (If left empty, it will be consistent with the roomId.) | New Character String | "denny`s room" |
seatMode | Mic on mode | This field takes effect only after microphone position control is enabled. Divided into "Free to Take the Podium mode (FREE_TO_TAKE)" and "Apply to Take the Podium mode (APPLY_TO_TAKE)". In Free to Take the Podium mode, audiences can freely become speakers without application. In Microphone mode, audiences need the room owner's approval after consent to become speakers. | Enumeration Value | SeatMode.APPLY_TO_TAKE |
maxSeatCount | maximum number of microphone slots | The maximum number of microphone slots of digit type means that a maximum of maxSeatCount people can be online simultaneously here. The maximum number of microphone slots matches the upper limit of the maximum number of mic-on users of the purchased package. | Digits | 10 |
isSeatEnabled | Whether microphone position control is enabled? | Boolean type microphone position control enable flag. | Boolean value | true |
roomType | Room type | Divide into two room types: "CONFERENCE" and "LIVE". Voice chat belongs to live streaming, so use live here. | Enumeration Value | RoomType.LIVE |
After preparing the parameter
TUIRoomInfo
, you can call the API functions createRoom
and enterRoom
to create and join a live room.import RTCRoomEnginelet roomInfo = TUIRoomInfo()roomInfo.roomId = "video_100001" // Please replace with your own room IDroomInfo.name = "denny's room" // Please replace with your own room nameroomInfo.seatMode =.applyToTake // Normally, in live video broadcasting scenarios, applying for microphone mode is adopted. If in your business, the audience can take the microphone without applying, this can be rewritten as freeToTakeroomInfo.maxSeatCount = 10 // Please replace with the maximum number of microphone slots of the Live package you have purchasedroomInfo.isSeatEnabled = true // If you don't need microphone position control, you can set isSeatEnabled to falseroomInfo.roomType = .live // Please ensure the room type is the live typeTUIRoomEngine.sharedInstance().createRoom(roomInfo) { [weak self] inguard let self = self else { return }TUIRoomEngine.sharedInstance.enterRoom(self.roomId, roomType: .live) { [weak self] roomInfo inguard let self = self else { return }// Successfully joined the live room} onError: { code, message in// Failed to join the live room}} onError: { code, message in// Failed to create live streaming room}
TUIRoomDefine.RoomInfo roomInfo = new TUIRoomDefine.RoomInfo();roomInfo.roomId = "video_100001"; // Please replace with your own room IDroomInfo.name = "denny's room"; // Please replace with your own room nameroomInfo.seatMode = TUIRoomDefine.SeatMode.APPLY_TO_TAKE; // Normally, in live video broadcasting scenarios, applying for microphone mode is adopted. If in your business, the audience can take the microphone without applying, this can be rewritten as freeToTakeroomInfo.maxSeatCount = 10; // Please replace with the maximum number of microphone slots of the Live package you have purchasedroomInfo.isSeatEnabled = true; // If you don't need microphone position control, you can set isSeatEnabled to falseroomInfo.roomType = TUIRoomDefine.RoomType.LIVE; // Please ensure the room type is the live typeTUIRoomEngine.sharedInstance().createRoom(roomInfo, new TUIRoomDefine.ActionCallback() {@Overridepublic void onSuccess() {TUIRoomEngine.sharedInstance().enterRoom(roomInfo.roomId, TUIRoomDefine.RoomType.LIVE, new TUIRoomDefine.GetRoomInfoCallback() {@Overridepublic void onSuccess(TUIRoomDefine.RoomInfo roomInfo) {// Successfully joined the live room}@Overridepublic void onError(TUICommonDefine.Error error, String message) {// Failed to join the live room}});}@Overridepublic void onError(TUICommonDefine.Error error, String message) {// Failed to create live streaming room}});
Mixed-Stream Live Streaming
If you want to start a mixed-stream live streaming room, you need to call an experimental API to enable the mixed-stream settings before creating a room.
private func enableUnlimitedRoom() {var jsonObject = [String: Any]()jsonObject["api"] = "enableUnlimitedRoom"var params = [String: Any]()params["enable"] = truejsonObject["params"] = paramsif let jsonData = try? JSONSerialization.data(withJSONObject: jsonObject, options: []),let jsonString = String(data: jsonData, encoding: .utf8) {TUIRoomEngine.callExperimentalAPI(jsonStr: jsonString)}}
Step 2: Become a Speaker and Push the Stream
You can only push the stream after becoming a speaker. Therefore, after you successfully create a room, you need to call the
takeSeat
API to become a speaker and start streaming.import RTCRoomEnginelet index = -1 // Please replace this with the seat number you want to apply for. Enter -1 if you do not need to pay attention to the seat index.let timeout = 30 // Please replace this with the timeout period for your request to speak, in seconds. If set to 0, the SDK will not perform timeout detection or trigger a timeout callback.TUIRoomEngine.sharedInstance().takeSeat(index, timeout: TimeInterval(timeout)) { requestId, userId in// Become a speaker} onRejected: { requestId, userId, messagae in// Seat request rejected} onCancelled: { requestId, userId in// Request to speak canceled} onTimeout: { requestId, userId in// Seat request timed out} onError: { requestId, userId, code, message in// Failed to join the microphone}
int index = -1; // Please replace this with the seat number you want to apply for. Enter -1 if you do not need to focus on the seat index.int timeout = 30; // Please replace this with the timeout period for your request to speak, in seconds. If set to 0, the SDK will not perform timeout detection or trigger a timeout callback.TUIRoomEngine.sharedInstance().takeSeat(index, timeout, new TUIRoomDefine.RequestCallback() {@Overridepublic void onAccepted(String requestId, String userId) {// Become a speaker}@Overridepublic void onRejected(String requestId, String userId, String message) {// Seat request rejected}@Overridepublic void onCancelled(String requestId, String userId) {// Request to speak canceled}@Overridepublic void onTimeout(String requestId, String userId) {// Seat request timed out}@Overridepublic void onError(String requestId, String userId, TUICommonDefine.Error error, String message) {// Failed to join the microphone}});
Step 3: Turn on the Media Device
After going live, you also need to call the
openLocalMicrophone
and openLocalCamera
APIs to turn on the microphone and camera, ensuring that the audience can see your visuals and hear your sound.The
openLocalMicrophone
API requires the input of audio quality parameters quality
.quality
is an enumeration of type TUIAudioQuality
.Enumeration Value Type | Meaning |
speech | Voice mode. Mono; Raw Bit Rate of audio: 18 kbps; suitable for scenarios mainly for voice calls. |
default | Default mode. Mono; Raw Bit Rate of audio: 50 kbps; the recommended choice of audio quality by default in SDK, unless otherwise needed. |
music | Music mode. Stereo + Full-band; Raw Bit Rate of audio: 128 kbps; suitable for scenarios requiring high-quality music transmission, such as online karaoke, music live streaming. |
The
openLocalCamera
API requires the input of two parameters: select front or rear camera isFront
and video quality quality
.isFront
is a boolean value. true means opening the front camera, and false means opening the rear camera.quality
is an enumeration of type TUIVideoQuality
.Enumeration Value Type | Meaning |
quality360P | Low-definition 360P |
quality540P | Standard-definition 540P |
quality720P | HD 720P |
quality1080P | Ultra HD 1080P |
Open the local microphone and camera, with the audio quality set to
default
, the front camera turned on, and the video quality set to quality1080P
as an example.After going live, you also need to call the
openLocalMicrophone
and openLocalCamera
APIs to turn on the microphone and camera, ensuring that the audience can see your visuals and hear your sound.The
openLocalMicrophone
API requires the input of audio quality parameters quality
.quality
is an enumeration of type AudioQuality
.Enumeration Value Type | Meaning |
SPEECH | Voice mode. Mono; Raw Bit Rate of audio: 18 kbps; suitable for scenarios mainly for voice calls. |
DEFAULT | Default mode. Mono; Raw Bit Rate of audio: 50 kbps; the recommended choice of audio quality by default in SDK, unless otherwise needed. |
MUSIC | Music mode. Stereo + Full-band; Raw Bit Rate of audio: 128 kbps; suitable for scenarios requiring high-quality music transmission, such as online karaoke, music live streaming. |
The
openLocalCamera
API requires the input of two parameters: select front or rear camera isFront
and video quality quality
.isFront
is a boolean value. true means opening the front camera, and false means opening the rear camera.quality
is an enumeration of type VideoQuality
.Enumeration Value Type | Meaning |
Q_360P | Low-definition 360P |
Q_540P | Standard-definition 540P |
Q_720P | HD 720P |
Q_1080P | Ultra HD 1080P |
Open the local microphone and camera, with the audio quality set to
DEFAULT
, the front camera turned on, and the video quality set to Q_1080P
.import RTCRoomEngine// Open local microphonelet audioQuality: TUIAudioQuality = .defaultTUIRoomEngine.sharedInstance().openLocalMicrophone(audioQuality) {// Successfully turn on the microphone} onError: { code, message in// Failed to open the microphone}// Open front cameralet isFrontCamera = truelet videoQuality: TUIVideoQuality = .quality1080PTUIRoomEngine.sharedInstance().openLocalCamera(isFront: isFrontCamera, quality: videoQuality) {// Successfully open front camera} onError: { code, message in// Fail to open front camera}
// Open local microphoneTUIRoomDefine.AudioQuality audioQuality = TUIRoomDefine.AudioQuality.MUSIC;TUIRoomEngine.sharedInstance().openLocalMicrophone(audioQuality, new TUIRoomDefine.ActionCallback() {@Overridepublic void onSuccess() {// Successfully turn on the microphone}@Overridepublic void onError(TUICommonDefine.Error error, String message) {// Failed to open the mic}});// Open front cameraboolean isFrontCamera = true;TUIRoomDefine.VideoQuality videoQuality = TUIRoomDefine.VideoQuality.Q_1080P;TUIRoomEngine.sharedInstance().openLocalCamera(isFrontCamera, videoQuality, new TUIRoomDefine.ActionCallback() {@Overridepublic void onSuccess() {// Successfully open front camera}@Overridepublic void onError(TUICommonDefine.Error error, String message) {// Fail to open front camera}});
Step 4: Set Local Preview Screen
If you need to view the local preview screen, you can achieve this by calling the
setLocalVideo
API.import RTCRoomEnginelet videoView = UIView()//...Add the viewoView to your view and perform layout on itTUIRoomEngine.sharedInstance().setLocalVideoView(view: videoView)
TUIVideoView videoView = new TUIVideoView(context);//...Add the viewoView to your view and perform layout on itTUIRoomEngine.sharedInstance().setLocalVideoView(videoView);
Video Viewing
Step 1: Join a Live Room
You only need to successfully enter the room by calling the
enterRoom
API to listen to the video host's sound. If you also want to view the host's video footage, please ensure that you have executed Step 2 after successfully entering the room.enterRoom requires the input of two parameters: the room Id of the room where the host you want to listen to is located and the room type.
Note:
There are two room types: conference and live. Voice chat rooms belong to live streaming rooms. Therefore, when you are calling
enterRoom
to enter a voice chat room, you need to input live
as the room type.import RTCRoomEnginelet roomId = "video_100001"let roomType = .liveTUIRoomEngine.sharedInstance().enterRoom(roomId, roomType: roomType) { roomInfo in// Entered room successfully} onError: { code, message in// Failed to enter the room}
String roomId = "video_100001";TUIRoomDefine.RoomType roomType = TUIRoomDefine.RoomType.LIVE;TUIRoomEngine.sharedInstance().enterRoom(roomId, roomType, new TUIRoomDefine.GetRoomInfoCallback() {@Overridepublic void onSuccess(TUIRoomDefine.RoomInfo roomInfo) {// Entered room successfully}@Overridepublic void onError(TUICommonDefine.Error error, String message) {// Failed to enter the room}});
Step 2: Set Viewing View
You can call
setRemoteVideoView
to set the remote video viewing view, importing three parameters: the userId of the viewing user, the video stream type being played, and the view object used to play the video.If you expect to view the room owner of the user, the user ID is roomInfo.ownerId after successfully entering the room in Step 1.
The video stream type is an enumeration value of type
TUIVideoStreamType
.Enumeration Value Type | Meaning |
cameraStream | High-definition camera video stream |
screenStream | Screen sharing video stream |
cameraStreamLow | Low-definition camera video stream |
You can call
setRemoteVideoView
to set the remote video viewing view, importing three parameters: the userId of the viewing user, the video stream type being played, and the view object used to play the video.If you expect to view the room owner of the user, the user ID is roomInfo.ownerId after successfully entering the room in Step 1.
The video stream type is an enumeration value of type
VideoStreamType
.Enumeration Value Type | Meaning |
CAMERA_STREAM | High-definition camera video stream |
SCREEN_STREAM | Screen sharing video stream |
CAMERA_STREAM_LOW | Low-definition camera video stream |
Take viewing the high-definition camera video stream of the room owner as an example:
import RTCRoomEnginelet videoView = UIView()//...Add the viewoView to your view and perform layout on itlet ownerId = "" // Replace it with the room owner user ID you joinedlet streamType = TUIVideoStreamType.cameraStreamTUIRoomEngine.sharedInstance().setRemoteVideoView(userId: ownerId,streamType: streamType,view: videoView)
TUIVideoView videoView = new TUIVideoView(context);//...Add the viewoView to your view and perform layout on itString ownerId = ""; // Replace it with the room owner user ID you joinedTUIRoomDefine.VideoStreamType streamType = TUIRoomDefine.VideoStreamType.CAMERA_STREAM;TUIRoomEngine.sharedInstance().setRemoteVideoView(ownerId, streamType, videoView);
Step 3: View the Anchor'S Video Footage
You can call the
startPlayRemoteVideo
API to view the remote user's video stream. The playback will be displayed on the view object set in Step 2.startPlayRemoteVideo
requires the input of two video parameters: the user Id of the viewing user and the video stream type for playback.Take playing the high-definition video stream of the room owner as an example:
import RTCRoomEnginelet ownerId = "" // Replace it with the room owner user ID you joinedlet streamType = TUIVideoStreamType.cameraStreamTUIRoomEngine.sharedInstance().startPlayRemoteVideo(userId: ownerId,streamType: streamType) { userId in// Playing video images} onLoading: { userId in// Video screen loading} onError: { userId, code, message in// Video screen playback failed}
String ownerId = ""; // Replace it with the room owner user ID you joinedTUIRoomDefine.VideoStreamType streamType = TUIRoomDefine.VideoStreamType.CAMERA_STREAM;TUIRoomEngine.sharedInstance().startPlayRemoteVideo(ownerId, streamType, new TUIRoomDefine.PlayCallback() {@Overridepublic void onPlaying(String userId) {// Playing video images}@Overridepublic void onLoading(String userId) {// Video screen loading}@Overridepublic void onPlayError(String userId, TUICommonDefine.Error error, String message) {