tencent cloud

Feedback

Live Streaming and Watching

Last updated: 2025-01-08 17:56:20
    This document mainly introduces how to use the RTC Room Engine SDK to implement the video broadcasting feature.

    Prerequisites

    Before using the RTC RoomEngine SDK, you need to call the SDK login to ensure the subsequent features work properly.

    Starting Live Streaming

    Using the RTC Room Engine to implement the core steps of starting a voice chat requires 4 steps: create and join a live room, go live on stage, enable media devices, and set up local preview. The following will provide a detailed introduction.

    Step 1: Creating and Joining a Live Room

    iOS
    Android
    Before broadcasting, you need to fill in the key parameters TUIRoomInfo. The following sections will provide a detailed introduction:

    Parameters: TUIRoomInfo

    TUIRoomInfo consists of many fields, but usually, you only need to focus on filling in the following fields:
    Parameter Name
    Field Description
    Additional Notes
    Data Type
    Example
    roomId
    Room ID
    Only letters (a-z, A-Z), digits (0-9), underscores, and hyphens are allowed.
    This field is a required parameter when creating a room and can contain up to 48 bytes.
    New Character String
    "live_100001"
    name
    Room Name
    Room name of string type. (If not provided, it will be the same as roomId)
    New Character String
    "denny`s room"
    seatMode
    Microphone Mode
    This field is effective only when microphone control is enabled.
    It is divided into "freeToTake" and "applyToTake" modes. In freeToTake mode, audience members can take the microphone freely without applying. In applyToTake mode, audience members need the room owner's approval to take the microphone.
    Enumeration Value
    TUISeatMode.applyToTake
    maxSeatCount
    Maximum Number of Microphones
    The maximum number of mic positions of numeric type, referring to the maximum number of people on mic simultaneously, maxSeatCount.
    The maximum number of seats is consistent with the maximum number of participants in the package you purchased.
    Digits
    10
    isSeatEnabled
    Whether to enable microphone position control
    Boolean type mic control enable flag.
    Boolean value
    true
    roomType
    Room type
    Divided into two room types: "conference" and "live". Voice chat belongs to live, so use live here.
    Enumeration Value
    TUIRoomType.live
    After preparing the parameters TUIRoomInfo, you can call the createRoom and enterRoom API functions to create and join a live room.
    Before broadcasting, you need to fill in the key parameters RoomInfo. The following sections will provide a detailed introduction:

    Parameters: RoomInfo

    RoomInfo consists of many fields, but usually, you only need to focus on filling in the following fields:
    Parameter Name
    Field Description
    Additional Notes
    Data Type
    Example
    roomId
    Room ID
    Only letters (a-z, A-Z), digits (0-9), underscores, and hyphens are allowed.
    This field is a required parameter when creating a room and can contain up to 48 bytes.
    New Character String
    "live_100001"
    name
    Room Name
    Room name of string type. (If not provided, it will be the same as roomId)
    New Character String
    "denny`s room"
    seatMode
    Microphone Mode
    This field is effective only when microphone control is enabled.
    It is divided into "FREE_TO_TAKE" and "APPLY_TO_TAKE" modes. In FREE_TO_TAKE mode, audience members can take the microphone freely without applying. In APPLY_TO_TAKE mode, audience members need the room owner's approval to take the microphone.
    Enumeration Value
    SeatMode.APPLY_TO_TAKE
    maxSeatCount
    Maximum Number of Microphones
    The maximum number of mic positions of numeric type, referring to the maximum number of people on mic simultaneously, maxSeatCount.
    The maximum number of seats is consistent with the maximum number of participants in the package you purchased.
    Digits
    10
    isSeatEnabled
    Whether to enable microphone position control
    Boolean type mic control enable flag.
    Boolean value
    true
    roomType
    Room type
    Divided into two room types: "CONFERENCE" and "LIVE". Voice chat belongs to live, so use live here.
    Enumeration Value
    RoomType.LIVE
    After preparing the parameters TUIRoomInfo, you can call the createRoom and enterRoom API functions to create and join a live room.
    iOS
    Android
    import RTCRoomEngine
    
    let roomInfo = TUIRoomInfo()
    roomInfo.roomId = "video_100001" // Please replace it with your own room ID
    roomInfo.name = "denny`s room" // Please replace it with your own room name.
    roomInfo.seatMode = .applyToTake // In typical video live streaming scenarios, the apply-to-take mode is used for joining the mic. If in your case, the audience doesn't need to apply to join the mic, you can change this to freeToTake.
    roomInfo.maxSeatCount = 10 // Please replace it with the maximum number of seats in the Live package you purchased.
    roomInfo.isSeatEnabled = true // If you do not need seat control, you can set isSeatEnabled to false
    roomInfo.roomType = .live // Ensure the room type is live.
    
    TUIRoomEngine.sharedInstance().createRoom(roomInfo) { [weak self] in
    guard let self = self else { return }
    TUIRoomEngine.sharedInstance.enterRoom(self.roomId, roomType: .live) { [weak self] roomInfo in
    guard let self = self else { return }
    // Successfully joined the live streaming room
    } onError: { code, message in
    // Failed to join the live streaming room
    }
    } onError: { code, message in
    // Failed to create the live streaming room
    }
    TUIRoomDefine.RoomInfo roomInfo = new TUIRoomDefine.RoomInfo();
    roomInfo.roomId = "video_100001"; // Please replace it with your own room ID
    roomInfo.name = "denny`s room"; // Please replace it with your own room name.
    roomInfo.seatMode = TUIRoomDefine.SeatMode.APPLY_TO_TAKE; // In typical video live streaming scenarios, the apply-to-take mode is used for joining the mic. If in your case, the audience doesn't need to apply to join the mic, you can change this to freeToTake.
    roomInfo.maxSeatCount = 10; // Please replace it with the maximum number of seats in the Live package you purchased.
    roomInfo.isSeatEnabled = true; // If you do not need seat control, you can set isSeatEnabled to false
    roomInfo.roomType = TUIRoomDefine.RoomType.LIVE; // Ensure the room type is live.
    
    TUIRoomEngine.sharedInstance().createRoom(roomInfo, new TUIRoomDefine.ActionCallback() {
    @Override
    public void onSuccess() {
    TUIRoomEngine.sharedInstance().enterRoom(roomInfo.roomId, TUIRoomDefine.RoomType.LIVE, new TUIRoomDefine.GetRoomInfoCallback() {
    @Override
    public void onSuccess(TUIRoomDefine.RoomInfo roomInfo) {
    // Successfully joined the live streaming room
    }
    @Override
    public void onError(TUICommonDefine.Error error, String message) {
    // Failed to join the live streaming room
    }
    });
    }
    @Override
    public void onError(TUICommonDefine.Error error, String message) {
    // Failed to create the live streaming room
    }
    });

    Step 2: Pushing Stream to the Stage

    You can start streaming only after taking the mic. Therefore, after successfully creating the room, you need to call the takeSeat API to take the mic and start streaming.
    iOS
    Android
    import RTCRoomEngine
    
    let index = -1; // Please replace this with the seat number you want to apply for. Setting it to -1 means no need to pay attention to the seat index.
    let timeout = 30 // Please replace this with the timeout duration for your speaking request, in seconds. If set to 0, the SDK will not perform timeout detection or trigger timeout callbacks.
    
    TUIRoomEngine.sharedInstance().takeSeat(index, timeout: TimeInterval(timeout)) { requestId, userId in
    // Took the mic successfully
    } onRejected: { requestId, userId, messagae in
    // Seat request rejected
    } onCancelled: { requestId, userId in
    // Seat request 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. Setting it to -1 means no need to pay attention to the seat index.
    int timeout = 30; // Replace this with the timeout duration for requesting to speak, in seconds. If set to 0, the SDK will not perform timeout detection or trigger timeout callbacks
    
    TUIRoomEngine.sharedInstance().takeSeat(index, timeout, new TUIRoomDefine.RequestCallback() {
    @Override
    public void onAccepted(String requestId, String userId) {
    // Took the mic successfully
    }
    @Override
    public void onRejected(String requestId, String userId, String message) {
    // Seat request rejected
    }
    
    @Override
    public void onCancelled(String requestId, String userId) {
    // Seat request canceled
    }
    @Override
    public void onTimeout(String requestId, String userId) {
    // Seat request timed out
    }
    @Override
    public void onError(String requestId, String userId, TUICommonDefine.Error error, String message) {
    // Failed to join the microphone
    }
    });

    Step 3: Enabling Media Devices

    iOS
    Android
    After starting the broadcast, you also need to call the openLocalMicrophone and openLocalCamera APIs to enable the microphone and camera, ensuring that the audience can see your video and hear your voice.
    The openLocalMicrophone API requires an audio quality parameter quality.
    quality is an enumeration of type TUIAudioQuality.
    Enumeration value types
    Meaning
    speech
    Voice mode. Mono; audio bitrate: 18kbps; suitable for voice call scenarios.
    default
    Default mode. Mono; audio bitrate: 50kbps; the default audio quality of the SDK, recommended if there are no special requirements.
    music
    Music mode. Stereo + full band; audio bitrate: 128kbps; suitable for scenarios requiring high-fidelity music transmission, such as online karaoke and music live streaming.
    The openLocalCamera API requires two parameters: isFront to select the front or rear camera, and quality for video quality.
    isFront is a boolean value, true for the front camera and false for the rear camera.
    quality is an enumeration of type TUIVideoQuality.
    Enumeration value types
    Meaning
    quality360P
    360p: SD
    quality540P
    540p: SD
    quality720P
    720p: HD
    quality1080P
    1080p: FHD
    Below is an example of opening the local microphone and camera with audio quality set to default, front camera enabled, and video quality set to quality1080P.
    After starting the broadcast, you also need to call the openLocalMicrophone and openLocalCamera APIs to enable the microphone and camera, ensuring that the audience can see your video and hear your voice.
    The openLocalMicrophone API requires an audio quality parameter quality.
    quality is an enumeration of type AudioQuality.
    Enumeration value types
    Meaning
    SPEECH
    Voice mode. Mono; audio bitrate: 18kbps; suitable for voice call scenarios.
    DEFAULT
    Default mode. Mono; audio bitrate: 50kbps; the default audio quality of the SDK, recommended if there are no special requirements.
    MUSIC
    Music mode. Stereo + full band; audio bitrate: 128kbps; suitable for scenarios requiring high-fidelity music transmission, such as online karaoke and music live streaming.
    The openLocalCamera API requires two parameters: isFront to select the front or rear camera, and quality for video quality.
    isFront is a boolean value, true for the front camera and false for the rear camera.
    quality is an enumeration of type VideoQuality.
    Enumeration value types
    Meaning
    Q_360P
    360p: SD
    Q_540P
    540p: SD
    Q_720P
    720p: HD
    Q_1080P
    1080p: FHD
    Below is an example of opening the local microphone and camera with audio quality set to DEFAULT, front camera enabled, and video quality set to Q_1080P.
    iOS
    Android
    import RTCRoomEngine
    
    // Turn on the local mic
    let audioQuality: TUIAudioQuality = .default
    TUIRoomEngine.sharedInstance().openLocalMicrophone(audioQuality) {
    // Successfully turned on the mic
    } onError: { code, message in
    // Failed to turn on the mic
    }
    
    // Turn on the front camera
    let isFrontCamera = true
    let videoQuality: TUIVideoQuality = .quality1080P
    TUIRoomEngine.sharedInstance().openLocalCamera(isFront: isFrontCamera, quality: videoQuality) {
    // Successfully turned on the front camera
    } onError: { code, message in
    // Failed to open the front camera
    }
    // Turn on the local mic
    TUIRoomDefine.AudioQuality audioQuality = TUIRoomDefine.AudioQuality.MUSIC;
    TUIRoomEngine.sharedInstance().openLocalMicrophone(audioQuality, new TUIRoomDefine.ActionCallback() {
    @Override
    public void onSuccess() {
    // Successfully turned on the mic
    }
    @Override
    public void onError(TUICommonDefine.Error error, String message) {
    // Failed to turn on the mic
    }
    });
    
    
    // Turn on the front camera
    boolean isFrontCamera = true;
    TUIRoomDefine.VideoQuality videoQuality = TUIRoomDefine.VideoQuality.Q_1080P;
    TUIRoomEngine.sharedInstance().openLocalCamera(isFrontCamera, videoQuality, new TUIRoomDefine.ActionCallback() {
    @Override
    public void onSuccess() {
    // Successfully opened the front camera
    }
    @Override
    public void onError(TUICommonDefine.Error error, String message) {
    // Failed to open the front camera
    }
    });

    Step 4: Setting the Local Preview Image

    If you need to view the local preview image, you can call the setLocalVideo API.
    iOS
    Android
    New Option
    import RTCRoomEngine
    
    let videoView = UIView()
    // ...add viewoView to your view and layout it
    TUIRoomEngine.sharedInstance().setLocalVideoView(view: videoView)
    TUIVideoView videoView = new TUIVideoView(context);
    // ...add viewoView to your view and layout it
    TUIRoomEngine.sharedInstance().setLocalVideoView(videoView);
    

    Video Watching

    Step 1: Joining a Live Room

    You only need to call the enterRoom API to successfully enter the room to hear the video host's voice. If you also want to watch the host's video, please ensure you have completed Step 2 after successfully entering the room.
    For enterRoom, you need to pass in two parameters: the room ID of the host you want to listen to and the room type.
    Note:
    There are two room types: conference and live. Voice chat rooms belong to live rooms, so when you call enterRoom to enter a voice chat room, the room type should be set to live.
    iOS
    Android
    New Option
    import RTCRoomEngine
    
    let roomId = "video_100001"
    let roomType = .live
    
    TUIRoomEngine.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() {
    @Override
    public void onSuccess(TUIRoomDefine.RoomInfo roomInfo) {
    // Entered room successfully
    }
    @Override
    public void onError(TUICommonDefine.Error error, String message) {
    // Failed to enter the room
    }
    });
    

    Step 2: Setting the Viewing View

    iOS
    Android
    You can call setRemoteVideoView to set the remote video viewing view, passing in three parameters: the userId of the user to be viewed, the type of video stream to be played, and the view object used to play the video.
    If you want to view the user's host, the user Id is the roomInfo.ownerId after room entry successful in Step 1.
    The video stream type is an enumeration value of type TUIVideoStreamType.
    Enumeration value types
    Meaning
    cameraStream
    HD Camera Video Stream
    screenStream
    Screen Share Video Stream
    cameraStreamLow
    Low-definition Camera Video Stream
    You can call setRemoteVideoView to set the remote video viewing view, passing in three parameters: the userId of the user to be viewed, the type of video stream to be played, and the view object used to play the video.
    If you expect to watch the host of the user, the user ID is the roomInfo.ownerId after Step 1 room entry successful.
    The video stream type is an enumeration value of type VideoStreamType.
    Enumeration value types
    Meaning
    CAMERA_STREAM
    HD Camera Video Stream
    SCREEN_STREAM
    Screen Share Video Stream
    CAMERA_STREAM_LOW
    Low-definition Camera Video Stream
    For example, to watch the high-definition camera video stream of the host:
    iOS
    Android
    import RTCRoomEngine
    
    let videoView = UIView()
    // ...add viewoView to your view and layout it
    
    let ownerId = "" // Please replace it with the host user ID of the live room you joined.
    let streamType = TUIVideoStreamType.cameraStream
    TUIRoomEngine.sharedInstance().setRemoteVideoView(userId: ownerId,
    streamType: streamType,
    view: videoView)
    
    TUIVideoView videoView = new TUIVideoView(context);
    // ...add viewoView to your view and layout it
    
    String ownerId = ""; // Please replace it with the host user ID of the live room you joined.
    TUIRoomDefine.VideoStreamType streamType = TUIRoomDefine.VideoStreamType.CAMERA_STREAM;
    TUIRoomEngine.sharedInstance().setRemoteVideoView(ownerId, streamType, videoView);

    Step 3: Watching the Host Video Screen

    You can call the startPlayRemoteVideo API to watch the remote user's video stream. The playing screen will be displayed on the view object set in Step 2.
    startPlayRemoteVideo requires two video parameters: the user ID of the user to watch and the type of video stream to play.
    For example, to play the high-definition video stream of the host:
    iOS
    Android
    import RTCRoomEngine
    let ownerId = "" // Please replace it with the host user ID of the live room you joined.
    let streamType = TUIVideoStreamType.cameraStream
    TUIRoomEngine.sharedInstance().startPlayRemoteVideo(userId: ownerId,
    streamType: streamType) { userId in
    // Playing video screen
    } onLoading: { userId in
    // Video screen loading
    } onError: { userId, code, message in
    // Video screen playback failed
    }
    String ownerId = ""; // Please replace it with the host user ID of the live room you joined.
    TUIRoomDefine.VideoStreamType streamType = TUIRoomDefine.VideoStreamType.CAMERA_STREAM;
    
    TUIRoomEngine.sharedInstance().startPlayRemoteVideo(ownerId, streamType, new TUIRoomDefine.PlayCallback() {
    @Override
    public void onPlaying(String userId) {
    // Playing video screen
    }
    @Override
    public void onLoading(String userId) {
    // Video screen loading
    }
    @Override
    public void onPlayError(String userId, TUICommonDefine.Error error, String message) {
    // Video screen playback failed
    }
    });
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support