tencent cloud

Feedback

Last updated: 2024-07-18 14:26:15

    Business Process

    This document summarizes some common business processes in the e-commerce live streaming scenario, helping you better understand the implementation process of the entire scenario.
    Anchor starts and ends live broadcast
    Anchor initiates the cross-room mic-connection PK
    The RTC audience enters the room for mic-connection
    The CDN audience enters the room for mic-connection
    Product Management for Merchandising
    The following diagram shows the process of an anchor (room owner) local preview, creating a room, entering a room to start live streaming, and leaving the room to end the live streaming.
    
    
    
    The following diagram shows the process of Anchor A inviting Anchor B for a cross-room PK. During the cross-room PK, the audiences in both rooms can see the PK mic-connection live streaming of the two room owners.
    
    
    
    The following diagram shows the process for RTC live interactive streaming audience to enter the room, apply for the mic-connection, end the mic-connection, and exit the room.
    
    
    
    The following diagram shows the process for RTC CDN live streaming audience to enter the room, apply for the mic-connection, end the mic-connection, and exit the room.
    
    
    
    The diagram below shows the process in live streaming merchandising scenarios, where the anchor edits and lists products, while audience browses and purchases products.
    
    
    

    Integration Preparations

    Step 1: activate the service

    E-commerce live streaming scenarios usually rely on paid PaaS services such as Real-Time Communication (TRTC), Beauty Special Effect, Player SDK. Among them, TRTC provides real-time audio and video interactive capabilities, Special Effect provides beauty special effects, and the player is responsible for live and on-demand playback. You can freely choose to activate the above services according to your actual business needs.
    Activate TRTC Service
    Activate Special Effect Service
    Activate Player Service
    1. First, you need to log in to the TRTC Console to create an application. You can choose to upgrade the TRTC application version according to your needs. For example, the professional edition unlocks more value-added feature services.
    
    
    
    Note:
    It is recommended to create two separate applications for testing and production environments. Each account (UIN) is provided with 10,000 minutes of free usage per month within one year.
    The TRTC monthly package is divided into Trial Version (by default), Basic Version, and Professional Version, which can unlock different value-added features and services. For details, see Version Features and Monthly Package Description.
    2. Once the application is created, you can find basic information about it under the Application Management - Application Overview section. It is important to store the SDKAppID and SDKSecretKey for later use and to avoid key leakage to prevent unauthorized traffic usage.
    
    
    
    1. Log in to Cloud Special Effect Console > Mobile License. Click Create Trial License (the free trial validity period for Trial Version License is 14 days. It is extendable once for a total of 28 days). Fill in the actual requirements for App Name, Package Name and Bundle ID. Select Special Effect, and choose the capabilities to be tested: Advanced Package S1-07, Atomic Capability X1-01, Atomic Capability X1-02, and Atomic Capability X1-03. After you check it, accurately fill in the company name, and industry type. Upload Company Service License, click OK to submit the review application, and wait for the manual review process.
    
    
    
    2. After the Trial License is successfully created, the page will display the generated License information. At this time, the License URL and License Key parameters are not yet effective and will only become active after the submission is approved. When configuring SDK initialization, you need to input both the License URL and License Key parameters. Keep the following information secure.
    
    
    
    1. Log in to VOD Console or CSS Console > License Management > Mobile License, and click Create Trial License.
    
    
    
    2. Enter the App Name,Package Name, and Bundle ID according to your actual needs, select Player Premium, and click OK.
    
    
    
    3. After the Trial License is successfully created, the page will display the generated License information. When initializing the SDK configuration, you need to enter two parameters: License Key and License URL, so carefully save the following information.
    
    
    
    Note:
    The License URL and Key for the same application are unique; after the Trial License is upgraded to the official version, the License URL and Key remain unchanged.

    Step 2: import SDK

    TRTC SDK and Special Effect SDK have been released to the CocoaPods repository, and you can integrate them via CocoaPods.
    1. Install CocoaPods
    Enter the following command in a terminal window (you need to install Ruby on your Mac first):
    sudo gem install cocoapods
    2. Create Podfile File
    Go to the project directory, and enter the following command. A Podfile file will then be created in the project directory.
    pod init
    3. Edit Podfile File
    Choose an appropriate version for your project and edit the Podfile file:
    platform :ios, '8.0'
    target 'App' do
    # The full feature version of SDK
    # Includes a wide range of features such as Real-Time Communication (TRTC), TXLivePlayer for live streaming playback, TXLivePusher for RTMP push streams, TXVodPlayer for on-demand playback, and UGSV for short video recording and editing.
    pod 'TXLiteAVSDK_Professional', :podspec => 'https://liteav.sdk.qcloud.com/pod/liteavsdkspec/TXLiteAVSDK_Professional.podspec'
    # Special Effect SDK example of S1-07 package is as follows
    pod 'TencentEffect_S1-07'
    
    end
    Note:
    The implementation of e-commerce live streaming scenarios usually depends on the combination of several capabilities such as TRTC and players. To avoid the symbol conflict issue that arises from single integration, it is recommended to integrate the full feature version of the SDK.
    4. Update and install the SDK
    Enter the following command in a terminal window to update the local repository files and install the SDK:
    pod install
    Or run this command to update the local repository:
    pod update
    Upon the completion of pod command execution, a project file suffixed with .xcworkspace and integrated with the SDK will be generated. Double-click to open it.
    Note:
    If the pod search fails, it is recommended to try updating the local repo cache of pod. The update command is as follows:
    pod setup
    pod repo update
    rm ~/Library/Caches/CocoaPods/search_index.json
    Besides the recommended automatic loading method, you can also choose to download the SDK and manually import it. For details, see Manually Integrate the TRTC SDK and Manually Integrate Special Effect SDK.
    5. Add beauty resources to the actual project
    Download and unzip the corresponding package of SDK and Beauty Resources. Add the bundle resources under the resources/motionRes folder to the actual project.
    Add -ObjC in Other Linker Flags of Build Settings.
    6. Modify the Bundle Identifier to match the applied trial authorization.

    Step 3: project configuration

    1. Configure permissions
    For e-commerce live streaming scenarios, LiteAVSDK and Special Effect SDK require the following permissions. Add the following two items to the App's Info.plist, corresponding to the microphone and camera prompts in the system pop-up authorization dialog box.
    Privacy - Microphone Usage Description. Enter a prompt specifying the purpose of microphone use.
    Privacy - Camera Usage Description. Enter a prompt specifying the purpose of camera use.
    
    
    
    2. If you need your App to continue running certain features in the background, go to XCode, select your current project, and under Capabilities, set the setting for Background Modes to ON, and check Audio, AirPlay, and Picture in Picture, as shown below:
    
    
    

    Step 4: authentication and authorization

    TRTC Authentication Credential
    Special Effect Authentication License
    Player Authentication License
    UserSig is a security protection signature designed by the cloud platform to prevent malicious attackers from misappropriating your cloud service usage rights. TRTC validates this authentication credential when entering a room.
    Debugging and testing stage: UserSig can be generated through Client Sample Code and Console Access, which are only used for debugging and testing.
    Production stage: It is recommended to use the server computing UserSig solution, which has a higher security level and helps prevent the client from being decompiled and reversed, to avoid the risk of key leakage.
    The specific implementation process is as follows:
    1. Before calling the initialization API of the SDK, your app must first request UserSig from your server.
    2. Your server generates the UserSig based on the SDKAppID and UserID.
    3. The server returns the generated UserSig to your app.
    4. Your app sends the obtained UserSig to the SDK through a specific API.
    5. The SDK submits the SDKAppID + UserID + UserSig to the cloud server for verification.
    6. The cloud platform verifies the validity of the UserSig.
    7. After the verification is passed, real-time audio and video services will be provided to the TRTC SDK.
    
    
    
    Note:
    The method of generating UserSig locally during the debugging and testing stage is not recommended for the online environment because it may be easily decompiled and reversed, causing key leakage.
    We provide server computation source code for UserSig in multiple programming languages (Java/GO/PHP/Nodejs/Python/C#/C++). For details, see Server Computation of UserSig.
    Before using Beauty Special Effect, you need to verify the license credential with the cloud platform. Configuring the License requires License Key and License Url. Sample code is as follows.
    [TELicenseCheck setTELicense:LicenseURL key:LicenseKey completion:^(NSInteger authresult, NSString * _Nonnull errorMsg) {
    if (authresult == TELicenseCheckOk) {
    NSLog(@"Authentication successful");
    } else {
    NSLog(@"Authentication failed");
    }
    }];
    Note:
    It is recommended to trigger the authentication permission in the initialization code of related business modules, to avoid having to download the License temporarily before use. Additionally, during authentication, network permissions must be ensured.
    The actual application's Bundle ID must match exactly with the Bundle ID associated with the License creation. Otherwise, it will lead to License verification failure. For details, see Authentication Error Code.
    The live streaming and on-demand playback features require setting the License before success in playback. Otherwise, playback will fail (black screen). It needs to be set globally only once. If you have not obtained the License, you can freely apply for a Trial Version License for normal playback. The Official Version License requires purchase. After successfully applying for License, you will receive two strings: License URL and License Key.
    Before your App calls the SDK-related features (recommended in - [AppDelegate application:didFinishLaunchingWithOptions:]), set the following settings:
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSString * const licenceURL = @"<the obtained licenseUrl>";
    NSString * const licenceKey = @"<the obtained key>";
    
    // TXLiveBase is located in the "TXLiveBase.h" header file
    [TXLiveBase setLicence:licenceURL key:licenceKey];
    [TXLiveBase setObserver:self];
    NSLog(@"SDK Version = %@", [TXLiveBase getSDKVersionStr]);
    return YES;
    }
    
    #pragma mark - TXLiveBaseDelegate
    - (void)onLicenceLoaded:(int)result Reason:(NSString *)reason {
    NSLog(@"onLicenceLoaded: result:%d reason:%@", result, reason);
    // If the result is not 0, it means the setting has failed, and you need to retry
    if (result != 0) {
    [TXLiveBase setLicence:licenceURL key:licenceKey];
    }
    }
    @end
    After the License is successfully set (you need to wait for a while, the specific time depends on the network conditions), you can use the following method to view the License information:
    NSLog(@"%@", [TXLiveBase getLicenceInfo]);
    Note:
    The actual application's Bundle ID must match exactly with the Bundle ID associated with the License creation. Otherwise, it will lead to License verification failure.
    The License is a strong online verification logic. When the TXLiveBase#setLicence is called after the application is started for the first time, the network must be available. At the first launch of the App, if the network permission is not yet authorized, you need to wait until the permission is granted before calling TXLiveBase#setLicence again.
    Listen to the loading result of TXLiveBase#setLicence: For onLicenceLoaded API, if it fails, you should retry and guide according to the actual situation. If it fails multiple times, you can limit the frequency and supplement with product pop-ups and other guides to allow users to check the network conditions.
    TXLiveBase#setLicence can be called multiple times. It is recommended to call TXLiveBase#setLicence when entering the main interface of the App to ensure successful loading.
    For multi-process Apps, ensure that every process using the player calls TXLiveBase#setLicence when it starts. For example, for Apps on the Android side that use a separate process for video playback, when the process is killed and restarted by the system during background playback, TXLiveBase#setLicence should also be called.

    Step 5: initialize the SDK

    Initialize the TRTC SDK
    Initialize the Special Effect SDK
    Initialize Player SDK
    // Create TRTC SDK instance (single instance pattern)
    self.trtcCloud = [TRTCCloud sharedInstance];
    // Set event listeners
    self.trtcCloud.delegate = self;
    
    // Notifications from various SDK events (e.g., error codes, warning codes, audio and video status parameters, etc.)
    - (void)onError:(TXLiteAVError)errCode errMsg:(nullable NSString *)errMsg extInfo:(nullable NSDictionary *)extInfo {
    NSLog(@"%d: %@", errCode, errMsg);
    }
    
    - (void)onWarning:(TXLiteAVWarning)warningCode warningMsg:(nullable NSString *)warningMsg extInfo:(nullable NSDictionary *)extInfo {
    NSLog(@"%d: %@", warningCode, warningMsg);
    }
    
    // Remove event listener
    self.trtcCloud.delegate = nil;
    // Destroy TRTC SDK instance (single instance pattern)
    [TRTCCloud destroySharedIntance];
    Note:
    It is recommended to listen to SDK event notifications. Perform log printing and handling for some common errors. For details, see Error Code Table.
    // Load beauty-related resources
    NSDictionary *assetsDict = @{@"core_name":@"LightCore.bundle",
    @"root_path":[[NSBundle mainBundle] bundlePath]
    };
    
    // Initialize the Special Effect SDK
    self.beautyKit = [[XMagic alloc] initWithRenderSize:previewSize assetsDict:assetsDict];
    
    // Release the Special Effect SDK
    [self.beautyKit deinit];
    Note:
    Before initializing the Special Effect SDK, resource copying and other preparatory work are needed. For detailed steps, see Special Effect SDK Integration Steps.
    On-demand Playback Scenario SDK Initialization.
    // 1. Set the SDK Connect Environment
    // If you serve global users, configure the SDK connect environment for global connect
    [TXLiveBase setGlobalEnv:"GDPR"];
    
    // 2. Create Player
    TXVodPlayer *_txVodPlayer = [[TXVodPlayer alloc] init];
    
    // 3. Associate Rendering View
    [_txVodPlayer setupVideoWidget:_myView insertIndex:0];
    
    // 4. Player Parameter Configuration
    TXVodPlayConfig *_config = [[TXVodPlayConfig alloc]init];
    [_config setEnableAccurateSeek:true];// Set whether to seek accurately. The default value is true
    [_config setMaxCacheItems:5]; // Set the number of cache files to 5
    [_config setProgressInterval:200]; // Set the interval for progress callbacks, in milliseconds
    [_config setMaxBufferSize:50]; // The maximum pre-load size, in MB
    [_txVodPlayer setConfig:_config]; // Pass config to _txVodPlayer
    
    // 5. Player Event Listener
    - (void)onPlayEvent:(TXVodPlayer *)player event:(int)EvtID withParam:(NSDictionary*)param { if (EvtID == PLAY_EVT_VOD_PLAY_PREPARED) { // Received event that the player is ready, now you can call pause, resume, getWidth, getSupportedBitrates, etc. } else if (EvtID == PLAY_EVT_PLAY_BEGIN) { // Received the start playback event } else if (EvtID == PLAY_EVT_PLAY_END) { // Received the playback end event } }
    Live Streaming Scenarios SDK initialization.
    // 1. Create Player
    V2TXLivePlayer *_txLivePlayer = [[V2TXLivePlayer alloc] init];
    
    // 2. Associate Rendering View
    [_txLivePlayer setRenderView:_myView];
    
    // 3. Player Event Listener
    [_txLivePlayer setObserver:self];
    
    - (void)onVideoLoading:(id<V2TXLivePlayer>)player extraInfo:(NSDictionary *)extraInfo {
    // Video loading event
    }
    
    - (void)onVideoPlaying:(id<V2TXLivePlayer>)player firstPlay:(BOOL)firstPlay extraInfo:(NSDictionary *)extraInfo {
    // Video playback event
    }

    Integration Process

    API Sequence Diagram

    
    
    

    Step 1: The anchor enters the room to push streams

    1. The anchor activates local video preview and audio capture before entering the room.
    // Obtain the video rendering control for displaying the anchor's local video preview
    @property (nonatomic, strong) UIView *anchorPreviewView;
    @property (nonatomic, strong) TRTCCloud *trtcCloud;
    
    - (void)setupTRTC {
    self.trtcCloud = [TRTCCloud sharedInstance];
    self.trtcCloud.delegate = self;
    // Set video encoding parameters to determine the picture quality seen by remote users
    TRTCVideoEncParam *encParam = [[TRTCVideoEncParam alloc] init];
    encParam.videoResolution = TRTCVideoResolution_960_540;
    encParam.videoFps = 15;
    encParam.videoBitrate = 1300;
    encParam.resMode = TRTCVideoResolutionModePortrait;
    [self.trtcCloud setVideoEncoderParam:encParam];
    // isFrontCamera can specify the use of front/rear camera for video capture
    [self.trtcCloud startLocalPreview:self.isFrontCamera view:self.anchorPreviewView];
    
    // Here you can specify the audio quality, from low to high as SPEECH/DEFAULT/MUSIC
    [self.trtcCloud startLocalAudio:TRTCAudioQualityDefault];
    }
    Note:
    You can set the video encoding parameters TRTCVideoEncParam according to business needs. For the best combinations of resolutions and bitrates for each tier, see Resolution and Bitrate Reference Table.
    Call the above API before enterRoom. The SDK will only start the camera preview and audio capture, and wait until you call enterRoom to start streaming.
    Call the above API after enterRoom. The SDK will start the camera preview and audio capture and automatically start streaming.
    2. The anchor sets rendering parameters for the local video, and the encoder output video mode (optional).
    - (void)setupRenderParams {
    TRTCRenderParams *params = [[TRTCRenderParams alloc] init];
    // Video mirror mode
    params.mirrorType = TRTCVideoMirrorTypeAuto;
    // Video fill mode
    params.fillMode = TRTCVideoFillMode_Fill;
    // Video rotation angle
    params.rotation = TRTCVideoRotation_0;
    // Set the rendering parameters for the local video
    [self.trtcCloud setLocalRenderParams:params];
    
    // Set the video mirror mode for the encoder output
    [self.trtcCloud setVideoEncoderMirror:YES];
    // Set the rotation of the video encoder output
    [self.trtcCloud setVideoEncoderRotation:TRTCVideoRotation_0];
    }
    Note:
    Setting local screen rendering parameters only affects the rendering effect of the local screen.
    Setting encoder output pattern affects the viewing effect for other users in the room (as well as the cloud recording files).
    3. The anchor starts the live streaming, entering the room and start streaming.
    - (void)enterRoomByAnchorWithUserId:(NSString *)userId roomId:(NSString *)roomId {
    TRTCParams *params = [[TRTCParams alloc] init];
    // Take the room ID string as an example
    params.strRoomId = roomId;
    params.userId = userId;
    // UserSig obtained from the business backend
    params.userSig = @"userSig";
    // Replace with your SDKAppID
    params.sdkAppId = 0;
    // Specify the anchor role
    params.role = TRTCRoleAnchor;
    // Enter the room in an interactive live streaming scenario
    [self.trtcCloud enterRoom:params appScene:TRTCAppSceneLIVE];
    }
    
    // Event callback for the result of entering the room
    - (void)onEnterRoom:(NSInteger)result {
    if (result > 0) {
    // The result represents the time taken to join the room (in milliseconds)
    NSLog(@"Enter room succeed!");
    } else {
    // The result represents the error code fwhen you fail to enter the room
    NSLog(@"Enter room failed!");
    }
    }
    Note:
    TRTC room IDs are divided into integer type roomId and string type strRoomId. The rooms of these two types are not interconnected. It is recommended to unify the room ID type.
    TRTC user roles are divided into anchors and audiences. Only anchors have streaming permissions. It is necessary to specify the user role when entering the room. If not specified, the default will be the anchor role.
    In the e-commerce live streaming scenario, it is recommended to choose TRTCAppSceneLIVE as the room entry mode.

    Step 2: The audience enters the room to pull streams

    1. Audience enters the TRTC room.
    - (void)enterRoomByAudienceWithUserId:(NSString *)userId roomId:(NSString *)roomId {
    TRTCParams *params = [[TRTCParams alloc] init];
    // Take the room ID string as an example
    params.strRoomId = roomId;
    params.userId = userId;
    // UserSig obtained from the business backend
    params.userSig = @"userSig";
    // Replace with your SDKAppID
    params.sdkAppId = 0;
    // Specify the audience role
    params.role = TRTCRoleAudience;
    // Enter the room in an interactive live streaming scenario
    [self.trtcCloud enterRoom:params appScene:TRTCAppSceneLIVE];
    }
    
    // Event callback for the result of entering the room
    - (void)onEnterRoom:(NSInteger)result {
    if (result > 0) {
    // The result represents the time taken to join the room (in milliseconds)
    NSLog(@"Enter room succeed!");
    } else {
    // The result represents the error code fwhen you fail to enter the room
    NSLog(@"Enter room failed!");
    }
    }
    2. Audience subscribes to the anchor's audio and video streams.
    - (void)onUserAudioAvailable:(NSString *)userId available:(BOOL)available {
    // The remote user publishes/unpublishes their audio
    // Under the automatic subscription mode, you do not need to do anything. The SDK will automatically play the remote user's audio
    }
    
    - (void)onUserVideoAvailable:(NSString *)userId available:(BOOL)available {
    // The remote user publishes/unpublishes the primary video
    if (available) {
    // Subscribe to the remote user's video stream and bind the video rendering control
    [self.trtcCloud startRemoteView:userId streamType:TRTCVideoStreamTypeBig view:self.remoteView];
    } else {
    // Unsubscribe to the remote user's video stream and release the rendering control
    [self.trtcCloud stopRemoteView:userId streamType:TRTCVideoStreamTypeBig];
    }
    }
    3. Audience sets the rendering mode for the remote video (optional).
    - (void)setupRemoteRenderParams {
    TRTCRenderParams *params = [[TRTCRenderParams alloc] init];
    // Video mirror mode
    params.mirrorType = TRTCVideoMirrorTypeAuto;
    // Video fill mode
    params.fillMode = TRTCVideoFillMode_Fill;
    // Video rotation angle
    params.rotation = TRTCVideoRotation_0;
    // Set the rendering mode for the remote video
    [self.trtcCloud setRemoteRenderParams:@"userId" streamType:TRTCVideoStreamTypeBig params:params];
    }

    Step 3: The audience interacts via mic-connection

    1. The audience is switched to the anchor role.
    - (void)switchToAnchor {
    // Switch to the anchor role
    [self.trtcCloud switchRole:TRTCRoleAnchor];
    }
    
    // Event callback for switching role
    - (void)onSwitchRole:(TXLiteAVError)errCode errMsg:(NSString *)errMsg {
    if (errCode == ERR_NULL) {
    // Role switched successfully
    }
    }
    2. The audience starts local audio and video capture and streaming.
    - (void)setupTRTC {
    // Set video encoding parameters to determine the picture quality seen by remote users
    TRTCVideoEncParam *encParam = [[TRTCVideoEncParam alloc] init];
    encParam.videoResolution = TRTCVideoResolution_480_270;
    encParam.videoFps = 15;
    encParam.videoBitrate = 550;
    encParam.resMode = TRTCVideoResolutionModePortrait;
    [self.trtcCloud setVideoEncoderParam:encParam];
    // isFrontCamera can specify the use of front/rear camera for video capture
    [self.trtcCloud startLocalPreview:self.isFrontCamera view:self.audiencePreviewView];
    // Here you can specify the audio quality, from low to high as SPEECH/DEFAULT/MUSIC
    [self.trtcCloud startLocalAudio:TRTCAudioQualityDefault];
    }
    Note:
    You can set the video encoding parameters TRTCVideoEncParam according to business needs. For the best combinations of resolutions and bitrates for each tier, see Resolution and Bitrate Reference Table.
    3. The audience drops the mic and stops streaming.
    - (void)switchToAudience {
    // Switch to the audience role
    [self.trtcCloud switchRole:TRTCRoleAudience];
    }
    
    // Event callback for switching role
    - (void)onSwitchRole:(TXLiteAVError)errCode errMsg:(NSString *)errMsg {
    if (errCode == ERR_NULL) {
    // Stop camera capture and streaming
    [self.trtcCloud stopLocalPreview];
    // Stop microphone capture and streaming
    [self.trtcCloud stopLocalAudio];
    }
    }

    Step 4: Exit and dissolve the room

    1. Exit Room
    - (void)exitRoom {
    [self.trtcCloud stopLocalAudio];
    [self.trtcCloud stopLocalPreview];
    [self.trtcCloud exitRoom];
    }
    
    // Event callback for exiting the room
    - (void)onExitRoom:(NSInteger)reason {
    if (reason == 0) {
    NSLog(@"Proactively call exitRoom to exit the room");
    } else if (reason == 1) {
    NSLog(@"Removed from the current room by the server");
    } else if (reason == 2) {
    NSLog(@"The current room is dissolved");
    }
    }
    Note:
    After all resources occupied by the SDK are released, the SDK will throw the onExitRoom callback notification to inform you.
    If you wish to call enterRoom again or switch to another audio and video SDK, wait for the onExitRoom callback before proceeding. Otherwise, you may encounter various exceptional issues such as the camera, microphone device being forcibly occupied.
    2. Dissolve Room
    Server dissolves the room
    TRTC provides the server dissolves digit type room API DismissRoom, as well as server dissolves string type room API DismissRoomByStrRoomId. You can call the server dissolves the room API to remove all users from the room and dissolve the room.
    Client dissolves the room
    The client does not have a API to directly dissolve the room. Each client needs to call exitRoom to exit the room. Once all anchors and audience have exited, the room will automatically be dissolved according to TRTC's room lifecycle rules. For more details, see TRTC Exits Room.
    Warning:
    It is recommended that after the end of live streaming, you call the room dissolvement API on the server to ensure the room is dissolved. This will prevent audiences from accidentally entering the room and incurring unexpected charges.

    Alternative Solutions

    API Sequence Diagram

    
    
    

    Step 1: The anchor relays stream pushing

    1. Related configurations for relaying to live streaming CDN.
    Global Automatic Relayed Push
    If you need to automatically relay all anchors' audio and video streams in the room to live streaming CDN, you need to enable Relay to CDN in the TRTC Console Advanced Features page.
    
    
    
    Relayed Push of the Specified Streams
    If you need to manually specify the audio and video streams to be published to live streaming CDN, or publish the mixed audio and video streams to live streaming CDN, you can do so by calling the startPublishMediaStream API. In this case, you do not need to activate global automatically relaying to CDN in the console. For a detailed introduction, see Publish Audio and Video Streams to Live Streaming CDN.
    2. The anchor activates local video preview and audio capture before entering the room.
    // Obtain the video rendering control for displaying the anchor's local video preview
    @property (nonatomic, strong) UIView *anchorPreviewView;
    
    
    - (void)setupTRTC {
    self.trtcCloud = [TRTCCloud sharedInstance];
    self.trtcCloud.delegate = self;
    // Set video encoding parameters to determine the picture quality seen by remote users
    TRTCVideoEncParam *encParam = [[TRTCVideoEncParam alloc] init];
    encParam.videoResolution = TRTCVideoResolution_960_540;
    encParam.videoFps = 15;
    encParam.videoBitrate = 1300;
    encParam.resMode = TRTCVideoResolutionModePortrait;
    [self.trtcCloud setVideoEncoderParam:encParam];
    
    // isFrontCamera can specify the use of front/rear camera for video capture
    [self.trtcCloud startLocalPreview:self.isFrontCamera view:self.anchorPreviewView];
    // Here you can specify the audio quality, from low to high as SPEECH/DEFAULT/MUSIC
    [self.trtcCloud startLocalAudio:TRTCAudioQualityDefault];
    }
    Note:
    You can set the video encoding parameters TRTCVideoEncParam according to business needs. For the best combinations of resolutions and bitrates for each tier, see Resolution and Bitrate Reference Table.
    Call the above API before enterRoom. The SDK will only start the camera preview and audio capture, and wait until you call enterRoom to start streaming.
    Call the above API after enterRoom. The SDK will start the camera preview and audio capture and automatically start streaming.
    3. The anchor sets rendering parameters for the local screen, and the encoder output video mode.
    - (void)setupRenderParams {
    TRTCRenderParams *params = [[TRTCRenderParams alloc] init];
    // Video mirror mode
    params.mirrorType = TRTCVideoMirrorTypeAuto;
    // Video fill mode
    params.fillMode = TRTCVideoFillMode_Fill;
    // Video rotation angle
    params.rotation = TRTCVideoRotation_0;
    // Set the rendering parameters for the local video
    [self.trtcCloud setLocalRenderParams:params];
    // Set the video mirror mode for the encoder output
    [self.trtcCloud setVideoEncoderMirror:YES];
    // Set the rotation of the video encoder output
    [self.trtcCloud setVideoEncoderRotation:TRTCVideoRotation_0];
    }
    Note:
    Setting local screen rendering parameters only affects the rendering effect of the local screen.
    Setting encoder output pattern affects the viewing effect for other users in the room (as well as the cloud recording files).
    4. The anchor starts the live streaming, entering the room and start streaming.
    - (void)enterRoomByAnchorWithUserId:(NSString *)userId roomId:(NSString *)roomId {
    TRTCParams *params = [[TRTCParams alloc] init];
    // Take the room ID string as an example
    params.strRoomId = roomId;
    params.userId = userId;
    // UserSig obtained from the business backend
    params.userSig = @"userSig";
    // Replace with your SDKAppID
    params.sdkAppId = 0;
    // Specify the anchor role
    params.role = TRTCRoleAnchor;
    // Enter the room in an interactive live streaming scenario
    [self.trtcCloud enterRoom:params appScene:TRTCAppSceneLIVE];
    }
    
    // Event callback for the result of entering the room
    - (void)onEnterRoom:(NSInteger)result {
    if (result > 0) {
    // The result represents the time taken to join the room (in milliseconds)
    NSLog(@"Enter room succeed!");
    } else {
    // The result represents the error code fwhen you fail to enter the room
    NSLog(@"Enter room failed!");
    }
    }
    Note:
    TRTC room IDs are divided into integer type roomId and string type strRoomId. The rooms of these two types are not interconnected. It is recommended to unify the room ID type.
    TRTC user roles are divided into anchors and audiences. Only anchors have streaming permissions. It is necessary to specify the user role when entering the room. If not specified, the default will be the anchor role.
    In the e-commerce live streaming scenario, it is recommended to choose TRTCAppSceneLIVE as the room entry mode.
    5. The anchor relays the audio and video streams to the live streaming CDN.
    - (void)startPublishMediaToCDN:(NSString *)streamName {
    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0];
    // Set the expiration time for the push URLs
    NSTimeInterval time = [date timeIntervalSince1970] + (24 * 60 * 60);
    // Generate authentication information. The getSafeUrl method can be obtained in the CSS console - Domain Name Management - Push Configuration - Sample Code for Push URLs
    NSString *secretParam = [self getSafeUrl:LIVE_URL_KEY streamName:streamName time:time];
    // The target URLs for media stream publication
    TRTCPublishTarget* target = [[TRTCPublishTarget alloc] init];
    // The target URLs are set for relaying to CDN
    target.mode = TRTCPublishBigStreamToCdn;
    TRTCPublishCdnUrl* cdnUrl = [[TRTCPublishCdnUrl alloc] init];
    // Construct push URLs (in RTMP format) to the live streaming service provider
    cdnUrl.rtmpUrl = [NSString stringWithFormat:@"rtmp://%@/live/%@?%@", PUSH_DOMAIN, streamName, secretParam];
    // True means CSS push URLs, and false means third-party services
    cdnUrl.isInternalLine = YES;
    NSMutableArray* cdnUrlList = [NSMutableArray array];
    // Multiple CDN push URLs can be added
    [cdnUrlList addObject:cdnUrl];
    target.cdnUrlList = cdnUrlList;
    // Set media stream encoding output parameters (can be defined according to business needs)
    TRTCStreamEncoderParam* encoderParam = [[TRTCStreamEncoderParam alloc] init];
    encoderParam.audioEncodedSampleRate = 48000;
    encoderParam.audioEncodedChannelNum = 1;
    encoderParam.audioEncodedKbps = 50;
    encoderParam.audioEncodedCodecType = 0;
    encoderParam.videoEncodedWidth = 540;
    encoderParam.videoEncodedHeight = 960;
    encoderParam.videoEncodedFPS = 15;
    encoderParam.videoEncodedGOP = 2;
    encoderParam.videoEncodedKbps = 1300;
    
    // Start publishing media stream
    [self.trtcCloud startPublishMediaStream:target encoderParam:encoderParam mixingConfig:nil];
    }
    Note:
    During single-anchor live streaming, only initiate the relayed push task. When there is an audience mic-connection or anchor PK, update this task to a mixed-stream transcoding task.
    Information of push authentication KEY LIVE_URL_KEY and push domain name PUSH_DOMAIN are required to obtain on Domain Management page in the CSS Console.
    After the media stream is published, SDK will provide the backend-initiated task identifier (taskId) through the callback onStartPublishMediaStream.
    - (void)onStartPublishMediaStream:(NSString *)taskId code:(int)code message:(NSString *)message extraInfo:(NSDictionary *)extraInfo {
    // taskId: When the request is successful, TRTC backend will provide the taskId of this task in the callback. You can later use this taskId with updatePublishMediaStream and stopPublishMediaStream to update and stop
    // code: Callback result. 0 means success and other values mean failure
    }

    Step 2: The audience pulls streams for playback

    CDN audience do not need to enter the TRTC room; they can directly pull the anchor's CDN stream for playback.
    // Initialize the player
    self.livePlayer = [[V2TXLivePlayer alloc] init];
    // Set the player callback listener
    [self.livePlayer setObserver:self];
    // Set the video rendering control for the player
    [self.livePlayer setRenderView:self.remoteView];
    // Set delay management mode (optional)
    [self.livePlayer setCacheParams:1.f maxTime:5.f]; // Auto mode
    [self.livePlayer setCacheParams:1.f maxTime:1.f]; // Speed mode
    [self.livePlayer setCacheParams:5.f maxTime:5.f]; // Smooth mode
    
    // Concatenate the pull URLs for playback
    NSString *flvUrl = [NSString stringWithFormat:@"http://%@/live/%@.flv", PLAY_DOMAIN, streamName];
    NSString *hlsUrl = [NSString stringWithFormat:@"http://%@/live/%@.m3u8", PLAY_DOMAIN, streamName];
    NSString *rtmpUrl = [NSString stringWithFormat:@"rtmp://%@/live/%@", PLAY_DOMAIN, streamName];
    NSString *webrtcUrl = [NSString stringWithFormat:@"webrtc://%@/live/%@", PLAY_DOMAIN, streamName];
    
    // Start playing
    [self.livePlayer startLivePlay:flvUrl];
    
    // Custom set fill mode (optional)
    [self.livePlayer setRenderFillMode:V2TXLiveFillModeFit];
    // Customize video rendering direction (optional)
    [self.livePlayer setRenderRotation:V2TXLiveRotation0];
    Note:
    The playback domain name PLAY_DOMAIN requires you to Add Your Own Domain in the CSS console for live streaming playback. You also should configure domain CNAME.
    To use the live streaming, you need to configure the player's Licence authorization in advance, or the playback will fail (black screen). For details, see Authentication and Authorization.

    Step 3: The audience interacts via mic-connection

    1. The mic-connection audiences need to enter the TRTC room for real-time interaction with the anchor.
    // Enter the TRTC room and start streaming
    - (void)enterRoomWithUserId:(NSString *)userId roomId:(NSString *)roomId {
    TRTCParams *params = [[TRTCParams alloc] init];
    // Take the room ID string as an example
    params.strRoomId = roomId;
    params.userId = userId;
    // UserSig obtained from the business backend
    params.userSig = @"userSig";
    // Replace with your SDKAppID
    params.sdkAppId = 0;
    // Specify the anchor role
    params.role = TRTCRoleAnchor;
    // Enable local audio and video capture
    [self startLocalMedia];
    // Enter the room in an interactive live streaming scenario
    [self.trtcCloud enterRoom:params appScene:TRTCAppSceneLIVE];
    }
    
    // Enable local video preview and audio capture
    - (void)startLocalMedia {
    // Set video encoding parameters to determine the picture quality seen by remote users
    TRTCVideoEncParam *encParam = [[TRTCVideoEncParam alloc] init];
    encParam.videoResolution = TRTCVideoResolution_480_270;
    encParam.videoFps = 15;
    encParam.videoBitrate = 550;
    encParam.resMode = TRTCVideoResolutionModePortrait;
    [self.trtcCloud setVideoEncoderParam:encParam];
    // isFrontCamera can specify the use of front/rear camera for video capture
    [self.trtcCloud startLocalPreview:self.isFrontCamera view:self.audiencePreviewView];
    // Here you can specify the audio quality, from low to high as SPEECH/DEFAULT/MUSIC
    [self.trtcCloud startLocalAudio:TRTCAudioQualityDefault];
    }
    
    // Event callback for the result of entering the room
    - (void)onEnterRoom:(NSInteger)result {
    if (result > 0) {
    // The result represents the time taken to join the room (in milliseconds)
    NSLog(@"Enter room succeed!");
    } else {
    // The result represents the error code fwhen you fail to enter the room
    NSLog(@"Enter room failed!");
    }
    }
    Note:
    You can set the video encoding parameters TRTCVideoEncParam according to business needs. For the best combinations of resolutions and bitrates for each tier, see Resolution and Bitrate Reference Table.
    2. The mic-connection audience start subscribing to the anchor's audio and video streams after they successfully enter the room.
    - (void)onUserAudioAvailable:(NSString *)userId available:(BOOL)available {
    // The remote user publishes/unpublishes their audio
    // Under the automatic subscription mode, you do not need to do anything. The SDK will automatically play the remote user's audio
    }
    
    - (void)onUserVideoAvailable:(NSString *)userId available:(BOOL)available {
    // The remote user publishes/unpublishes the primary video
    if (available) {
    // Subscribe to the remote user's video stream and bind the video rendering control
    [self.trtcCloud startRemoteView:userId streamType:TRTCVideoStreamTypeBig view:self.remoteView];
    } else {
    // Unsubscribe to the remote user's video stream and release the rendering control
    [self.trtcCloud stopRemoteView:userId streamType:TRTCVideoStreamTypeBig];
    }
    }
    
    - (void)onFirstVideoFrame:(NSString *)userId streamType:(TRTCVideoStreamType)streamType width:(int)width height:(int)height {
    // The SDK starts rendering the first frame of the local or remote user's video
    if (![userId isEqualToString:@""]) {
    // Stop playing the CDN stream upon receiving the first frame of the anchor's video
    [self.livePlayer stopPlay];
    }
    }
    Note:
    TRTC stream pulling startRemoteView can directly reuse the video rendering control previously used by the CDN stream pullingsetRenderView.
    To avoid video interruptions when switching between stream pullers, it is recommended to wait until the TRTC first frame callback onFirstVideoFrame is received before stopping the CDN stream pulling.
    3. The anchor updates the publication of mixed media streams.
    // Event callback for the mic-connection audience's room entry
    - (void)onRemoteUserEnterRoom:(NSString *)userId {
    if (![self.mixUserList containsObject:userId]) {
    [self.mixUserList addObject:userId];
    }
    [self updatePublishMediaToCDN];
    }
    
    // Update the publication of mixed media streams to the live streaming CDN
    - (void)updatePublishMediaToCDN {
    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0];
    // Set the expiration time for the push URLs
    NSTimeInterval time = [date timeIntervalSince1970] + (24 * 60 * 60);
    // Generate authentication information. The getSafeUrl method can be obtained in the CSS console - Domain Name Management - Push Configuration - Sample Code for Push URLs
    NSString *secretParam = [self getSafeUrl:LIVE_URL_KEY streamName:self.streamName time:time];
    // The target URLs for media stream publication
    TRTCPublishTarget* target = [[TRTCPublishTarget alloc] init];
    // The target URLs are set for relaying the mixed streams to CDN
    target.mode = TRTCPublishMixStreamToCdn;
    TRTCPublishCdnUrl* cdnUrl = [[TRTCPublishCdnUrl alloc] init];
    // Construct push URLs (in RTMP format) to the live streaming service provider
    cdnUrl.rtmpUrl = [NSString stringWithFormat:@"rtmp://%@/live/%@?%@", PUSH_DOMAIN, self.streamName, secretParam];
    // True means CSS push URLs, and false means third-party services
    cdnUrl.isInternalLine = YES;
    NSMutableArray* cdnUrlList = [NSMutableArray array];
    // Multiple CDN push URLs can be added
    [cdnUrlList addObject:cdnUrl];
    target.cdnUrlList = cdnUrlList;
    // Set media stream encoding output parameters
    TRTCStreamEncoderParam* encoderParam = [[TRTCStreamEncoderParam alloc] init];
    encoderParam.audioEncodedSampleRate = 48000;
    encoderParam.audioEncodedChannelNum = 1;
    encoderParam.audioEncodedKbps = 50;
    encoderParam.audioEncodedCodecType = 0;
    encoderParam.videoEncodedWidth = 540;
    encoderParam.videoEncodedHeight = 960;
    encoderParam.videoEncodedFPS = 15;
    encoderParam.videoEncodedGOP = 2;
    encoderParam.videoEncodedKbps = 1300;
    TRTCStreamMixingConfig *config = [[TRTCStreamMixingConfig alloc] init];
    if (self.mixUserList.count) {
    NSMutableArray<TRTCUser *> *userList = [NSMutableArray array];
    NSMutableArray<TRTCVideoLayout *> *layoutList = [NSMutableArray array];
    for (int i = 1; i < MIN(self.mixUserList.count, 16); i++) {
    TRTCUser *user = [[TRTCUser alloc] init];
    // The integer room number is intRoomId
    user.strRoomId = self.roomId;
    user.userId = self.mixUserList[i];
    [userList addObject:user];
    TRTCVideoLayout *layout = [[TRTCVideoLayout alloc] init];
    if ([self.mixUserList[i] isEqualToString:self.userId]) {
    // The layout for the anchor's video
    layout.rect = CGRectMake(0, 0, 540, 960);
    layout.zOrder = 0;
    } else {
    // The layout for the mic-connection audience's video
    layout.rect = CGRectMake(400, 5 + i * 245, 135, 240);
    layout.zOrder = 1;
    }
    layout.fixedVideoUser = user;
    layout.fixedVideoStreamType = TRTCVideoStreamTypeBig;
    [layoutList addObject:layout];
    }
    // Specify the information for each input audio stream in the transcoding stream
    config.audioMixUserList = [userList copy];
    // Specify the information of position, size, layer, and stream type for each video screen in the mixed display
    config.videoLayoutList = [layoutList copy];
    }
    // Update the published media stream
    [self.trtcCloud updatePublishMediaStream:self.taskId publishTarget:target encoderParam:encoderParam mixingConfig:config];
    }
    
    // Event callback for updating the media stream
    - (void)onUpdatePublishMediaStream:(NSString *)taskId code:(int)code message:(NSString *)message extraInfo:(NSDictionary *)extraInfo {
    // When you call the publish media stream API (updatePublishMediaStream), the taskId you provide will be returned to you through this callback. It is used to identify which update request the callback belongs to
    // code: Callback result. 0 means success and other values mean failure
    }
    Note:
    To ensure continuous CDN playback without stream disconnection, you need to keep the media stream encoding output parameter encoderParam and the stream name streamName unchanged.
    Media stream encoding output parameters and mixed display layout parameters can be customized according to business needs. Currently, up to 16 channels of audio and video input are supported. If a user only provides audio, it will still be counted as one channel.
    Switching between audio only, audio and video, and video only is not supported within the same task.
    4. The off-streaming audience exit the room, and the anchor updates the mixed stream task.
    
    // Set the player callback listener
    [self.livePlayer setObserver:self];
    // The reusable TRTC video rendering control
    [self.livePlayer setRenderView:self.remoteView];
    // Restart playing CDN media stream
    [self.livePlayer startLivePlay:flvUrl];
    
    
    - (void)onVideoLoading:(id<V2TXLivePlayer>)player extraInfo:(NSDictionary *)extraInfo {
    // Video loading event
    }
    
    // Video playback event
    - (void)onVideoPlaying:(id<V2TXLivePlayer>)player firstPlay:(BOOL)firstPlay extraInfo:(NSDictionary *)extraInfo {
    if (firstPlay) {
    [self.trtcCloud stopAllRemoteView];
    [self.trtcCloud stopLocalAudio];
    [self.trtcCloud stopLocalPreview];
    [self.trtcCloud exitRoom];
    }
    }
    Note:
    To avoid video interruptions when switching the stream puller, it is recommended to wait for the player's video playback event onVideoPlaying before exiting the TRTC room.
    // Event callback for the mic-connection audience's room exit
    - (void)onRemoteUserLeaveRoom:(NSString *)userId reason:(NSInteger)reason {
    if ([self.mixUserList containsObject:userId]) {
    [self.mixUserList removeObject:userId];
    }
    // The anchor updates the mixed stream task
    [self updatePublishMediaToCDN];
    }
    
    // Event callback for updating the media stream
    - (void)onUpdatePublishMediaStream:(NSString *)taskId code:(int)code message:(NSString *)message extraInfo:(NSDictionary *)extraInfo {
    // When you call the publish media stream API (updatePublishMediaStream), the taskId you provide will be returned to you through this callback. It is used to identify which update request the callback belongs to
    // code: Callback result. 0 means success and other values mean failure
    }

    Step 4: The anchor stops the live streaming and exits the room

    - (void)exitRoom {
    // Stop all published media streams
    [self.trtcCloud stopPublishMediaStream:@""];
    [self.trtcCloud stopLocalAudio];
    [self.trtcCloud stopLocalPreview];
    [self.trtcCloud exitRoom];
    }
    
    // Event callback for stopping media streams
    - (void)onStopPublishMediaStream:(NSString *)taskId code:(int)code message:(NSString *)message extraInfo:(NSDictionary *)extraInfo {
    // When you call stopPublishMediaStream, the taskId you provide will be returned to you through this callback. It is used to identify which stop request the callback belongs to
    // code: Callback result. 0 means success and other values mean failure
    }
    
    // Event callback for exiting the room
    - (void)onExitRoom:(NSInteger)reason {
    if (reason == 0) {
    NSLog(@"Proactively call exitRoom to exit the room");
    } else if (reason == 1) {
    NSLog(@"Removed from the current room by the server");
    } else if (reason == 2) {
    NSLog(@"The current room is dissolved");
    }
    }
    Note:
    To stop publishing media streams, enter an empty string for taskId. This will stop all the media streams you have published.
    After all resources occupied by the SDK are released, the SDK will throw the onExitRoom callback notification to inform you.

    Advanced Features

    Product Information Pop-up

    The Product Information Pop-up feature can be implemented through IM Custom Message or SEI Information. Below are the specific information of the two implementation methods.

    Custom Message

    Custom messages depend on Instant Messaging (IM). You need to activate the service and import the IM SDK in advance. For detailed guidelines, see Voice Chat Room Connection Guide - Connection Preparation.
    1. Send Custom Messages
    Method 1: The anchor sends product pop-up related custom group messages on the client.
    // Construct product pop-up message body
    NSDictionary *msgDict = @{
    @"itemNumber": @1, // Item number
    @"itemPrice": @199.0,// Item price
    @"itemTitle": @"xxx",// Item title
    @"itemUrl": @"xxx" // Item URL
    };
    NSDictionary *dataDict = @{
    @"cmd": @"item_popup_msg",
    @"msg": msgDict
    };
    NSError *error;
    NSData *data = [NSJSONSerialization dataWithJSONObject:dataDict options:0 error:&error];
    
    // Send custom group messages (it is recommended that product pop-up messages should be set to high priority)
    [[V2TIMManager sharedInstance] sendGroupCustomMessage:data to:groupID priority:V2TIM_PRIORITY_HIGH succ:^{
    // Successfully sent product pop-up message
    // Locally rendering of product pop-up effect
    } fail:^(int code, NSString *desc) {
    // Failed to send product pop-up message
    }];
    Method 2: The backend operators sends product pop-up related custom group messages on the server.
    Request URL sample:
    https://xxxxxx/v4/group_open_http_svc/send_group_msg?sdkappid=88888888&identifier=admin&usersig=xxx&random=99999999&contenttype=json
    Request packet body sample:
    {
    "GroupId": "@TGS#12DEVUDHQ",
    "Random": 2784275388,
    "MsgPriority": "High", // The priority of the message. It is recommended to set product pop-up messages to high priority
    "MsgBody": [
    {
    "MsgType": "TIMCustomElem",
    "MsgContent": {
    // itemNumber: item number; itemPrice: item price; itemTitle: item title; itemUrl: item URL
    "Data": "{\\"cmd\\": \\"item_popup_msg\\", \\"msg\\": {\\"itemNumber\\": 1, \\"itemPrice\\": 199.0, \\"itemTitle\\": \\"xxx\\", \\"itemUrl\\": \\"xxx\\"}}"
    }
    }
    ]
    }
    2. Receive Custom Messages
    Other users in the room receive callback for custom group messages, then proceed with message parsing and product pop-up effect rendering.
    // Custom group messages received
    [[V2TIMManager sharedInstance] addSimpleMsgListener:self];
    - (void)onRecvGroupCustomMessage:(NSString *)msgID groupID:(NSString *)groupID sender:(V2TIMGroupMemberInfo *)info customData:(NSData *)data {
    if (data.length > 0) {
    NSError *error;
    NSDictionary *dataDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    if (!error) {
    NSString *command = dataDict[@"cmd"];
    NSDictionary *msgDict = dataDict[@"msg"];
    if ([command isEqualToString:@"item_popup_msg"]) {
    NSNumber *itemNumber = msgDict[@"itemNumber"];// Item number
    NSNumber *itemPrice = msgDict[@"itemPrice"]; // Item price
    NSString *itemTitle = msgDict[@"itemTitle"]; // Item title
    NSString *itemUrl = msgDict[@"itemUrl"]; // Item URL
    // Render product pop-up effect based on item number, item price, item title, and item URL
    }
    } else {
    NSLog(@"Parsing error: %@", error.localizedDescription);
    }
    }
    }

    SEI Information

    SEI information will be inserted into the anchor's video stream for transmission, achieving precise sync between the product information pop-up and the anchor's live streaming.
    1. Send SEI Information
    The anchor sends SEI messages related to product pop-up on the TRTC client.
    // Construct product pop-up message body
    NSDictionary *msgDict = @{
    @"itemNumber": @1, // Item number
    @"itemPrice": @199.0,// Item price
    @"itemTitle": @"xxx",// Item title
    @"itemUrl": @"xxx" // Item URL
    };
    NSDictionary *dataDict = @{
    @"cmd": @"item_popup_msg",
    @"msg": msgDict
    };
    NSError *error;
    NSData *data = [NSJSONSerialization dataWithJSONObject:dataDict options:0 error:&error];
    
    // Send SEI information
    [self.trtcCloud sendSEIMsg:data repeatCount:1];
    2. Receive SEI Information
    Method 1: The audience receives SEI messages on the TRTC client, then proceeds with message parsing and product pop-up effect rendering.
    // Set TRTC event listener
    self.trtcCloud.delegate = self;
    
    // Receive SEI messages
    - (void)onRecvSEIMsg:(NSString *)userId message:(NSData *)message {
    if (message.length > 0) {
    NSError *error;
    NSDictionary *dataDict = [NSJSONSerialization JSONObjectWithData:message options:0 error:&error];
    if (!error) {
    NSString *command = dataDict[@"cmd"];
    NSDictionary *msgDict = dataDict[@"msg"];
    if ([command isEqualToString:@"item_popup_msg"]) {
    NSNumber *itemNumber = msgDict[@"itemNumber"];// Item number
    NSNumber *itemPrice = msgDict[@"itemPrice"]; // Item price
    NSString *itemTitle = msgDict[@"itemTitle"]; // Item title
    NSString *itemUrl = msgDict[@"itemUrl"]; // Item URL
    // Render product pop-up effect based on item number, item price, item title, and item URL
    }
    } else {
    NSLog(@"Parsing error: %@", error.localizedDescription);
    }
    }
    }
    Method 2: The audience receives SEI messages on the CDN stream player, then proceeds with message parsing and product pop-up effect rendering.
    // Set the PayloadType for sending SEI messages in TRTC (must be set before sending)
    [self.trtcCloud callExperimentalAPI:@"{\\"api\\":\\"setSEIPayloadType\\",\\"params\\":{\\"payloadType\\":5}}"];
    
    // Enable receiving SEI messages on the player and set the PayloadType
    [self.livePlayer enableReceiveSeiMessage:YES payloadType:5];
    
    // SEI message callback and parsing
    - (void)onReceiveSeiMessage:(id<V2TXLivePlayer>)player payloadType:(int)payloadType data:(NSData *)data {
    if (data.length > 0) {
    NSError *error;
    NSDictionary *dataDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    if (!error) {
    NSString *command = dataDict[@"cmd"];
    NSDictionary *msgDict = dataDict[@"msg"];
    if ([command isEqualToString:@"item_popup_msg"]) {
    NSNumber *itemNumber = msgDict[@"itemNumber"];// Item number
    NSNumber *itemPrice = msgDict[@"itemPrice"]; // Item price
    NSString *itemTitle = msgDict[@"itemTitle"]; // Item title
    NSString *itemUrl = msgDict[@"itemUrl"]; // Item URL
    // Render product pop-up effect based on item number, item price, item title, and item URL
    }
    } else {
    NSLog(@"Parsing error: %@", error.localizedDescription);
    }
    }
    }
    Note:
    It is necessary to ensure that the SEI PayloadType of the TRTC sender and the player receiver are consistent, so that the audience can successfully receive the SEI messages relayed via TRTC.

    Product Explanation Replay

    By playing pre-recorded product explanation videos, the product explanation replay feature is implemented.
    First, it is necessary to initialize the player, then start playing the recorded video. TXVodPlayer supports two playback modes, which you can choose according to your needs:
    Using the URL method
    Using the FileId method
    // Play URL video resource
    NSString* url = @"http://1252463788.vod2.myqcloud.com/xxxxx/v.f20.mp4";
    [_txVodPlayer startVodPlay:url];
    
    // Play sandbox local video resources
    // Obtain the Documents path
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    // Obtain the local video path
    NSString *videoPath = [NSString stringWithFormat:@"%@/video1.m3u8",documentPath];
    [_txVodPlayer startVodPlay:videoPath];
    TXPlayerAuthParams *p = [TXPlayerAuthParams new];
    p.appId = 1252463788;
    p.fileId = @"4564972819220421305";
    // The psign means player signature. For more information about the signature and how to generate it, see: https://www.tencentcloud.com/document/product/266/42436?from_cn_redirect=1
    p.sign = @"psignxxxx"; // Player signature
    [_txVodPlayer startVodPlayWithParams:p];
    Playback control: adjust the progress, pause playback, resume playback, and end playback.
    // Adjust the progress (seconds)
    [_txVodPlayer seek:time];
    
    // Pause playback
    [_txVodPlayer pause];
    
    // Resume playback
    [_txVodPlayer resume];
    
    // End playback
    [_txVodPlayer stopPlay];
    Note:
    When stopping playback, remember to use removeVideoWidget to Destroy the view control before exiting the current UI interface. Otherwise, it may cause a memory leak or screen flash.
    // Destroy the view control
    [_txVodPlayer removeVideoWidget];

    Cross-room Mic-connection PK

    1. Either party initiates the cross-room mic-connection PK.
    - (void)connectOtherRoom:(NSString *)roomId {
    NSMutableDictionary *jsonDict = [[NSMutableDictionary alloc] init];
    // The digit room number is roomId
    [jsonDict setObject:roomId forKey:@"strRoomId"];
    [jsonDict setObject:self.userId forKey:@"userId"];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    [self.trtcCloud connectOtherRoom:jsonString];
    }
    
    // Result callback for requesting cross-room mic-connection
    - (void)onConnectOtherRoom:(NSString *)userId errCode:(TXLiteAVError)errCode errMsg:(NSString *)errMsg {
    // The user ID of the anchor in the other room you want to initiate the cross-room link-up
    // Error code. ERR_NULL indicates the request is successful
    // Error message
    }
    Note:
    Both local and remote users participating in the cross-room mic-connection must be in the anchor role and must have audio/video uplink capabilities.
    Cross-room mic-connection PK with multiple room anchors can be achieved by calling ConnectOtherRoom() multiple times. Currently, a room can connect with up to three other room anchors at most, and up to 10 anchors in a room can conduct cross-room mic-connection competition with anchors in other rooms.
    2. All users in both rooms will receive a callback indicating that the audio and video streams from the PK anchor in the other room are available.
    - (void)onUserAudioAvailable:(NSString *)userId available:(BOOL)available {
    // The remote user publishes/unpublishes their audio
    // Under the automatic subscription mode, you do not need to do anything. The SDK will automatically play the remote user's audio
    }
    
    - (void)onUserVideoAvailable:(NSString *)userId available:(BOOL)available {
    // The remote user publishes/unpublishes the primary video
    if (available) {
    // Subscribe to the remote user's video stream and bind the video rendering control
    [self.trtcCloud startRemoteView:userId streamType:TRTCVideoStreamTypeBig view:self.remoteView];
    } else {
    // Unsubscribe to the remote user's video stream and release the rendering control
    [self.trtcCloud stopRemoteView:userId streamType:TRTCVideoStreamTypeBig];
    }
    }
    3. Either party exits the cross-room mic-connection PK.
    // Exiting cross-room mic-connection
    [self.trtcCloud disconnectOtherRoom];
    
    // Result callback for exiting cross-room mic-connection
    - (void)onDisconnectOtherRoom:(TXLiteAVError)errCode errMsg:(NSString *)errMsg {
    }
    Note:
    After calling DisconnectOtherRoom(), you may exit the cross-room mic-connection PK with all other room anchors.
    Either the initiator or the receiver can call DisconnectOtherRoom() to exit the cross-room mic-connection PK.

    Third-Party Beauty Feature Integration

    TRTC supports integrating third-party beauty effect products. Use the example of Special Effect to demonstrate the process of integrating the third-party beauty features.
    1. Integrate the Special Effect SDK, and apply for an authorization license. For details, see Integration Preparation for steps.
    2. Set the SDK material resource path (if any).
    NSString *beautyConfigPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    beautyConfigPath = [beautyConfigPath stringByAppendingPathComponent:@"beauty_config.json"];
    NSFileManager *localFileManager=[[NSFileManager alloc] init];
    BOOL isDir = YES;
    NSDictionary * beautyConfigJson = @{};
    if ([localFileManager fileExistsAtPath:beautyConfigPath isDirectory:&isDir] && !isDir) {
    NSString *beautyConfigJsonStr = [NSString stringWithContentsOfFile:beautyConfigPath encoding:NSUTF8StringEncoding error:nil];
    NSError *jsonError;
    NSData *objectData = [beautyConfigJsonStr dataUsingEncoding:NSUTF8StringEncoding];
    beautyConfigJson = [NSJSONSerialization JSONObjectWithData:objectData
    options:NSJSONReadingMutableContainers
    error:&jsonError];
    }
    NSDictionary *assetsDict = @{@"core_name":@"LightCore.bundle",
    @"root_path":[[NSBundle mainBundle] bundlePath],
    @"tnn_"
    @"beauty_config":beautyConfigJson
    };
    // Initialize SDK: Width and height are the width and height of the texture respectively
    self.xMagicKit = [[XMagic alloc] initWithRenderSize:CGSizeMake(width,height) assetsDict:assetsDict];
    3. Set the video data callback for third-party beauty features. Pass the results of the beauty SDK processing each frame of data into the TRTC SDK for rendering processing.
    // Set the video data callback for third-party beauty features in the TRTC SDK
    [self.trtcCloud setLocalVideoProcessDelegete:self pixelFormat:TRTCVideoPixelFormat_Texture_2D bufferType:TRTCVideoBufferType_Texture];
    
    #pragma mark - TRTCVideoFrameDelegate
    
    // Construct the YTProcessInput and pass it into the SDK for rendering processing
    - (uint32_t)onProcessVideoFrame:(TRTCVideoFrame *_Nonnull)srcFrame dstFrame:(TRTCVideoFrame *_Nonnull)dstFrame {
    if (!self.xMagicKit) {
    [self buildBeautySDK:srcFrame.width and:srcFrame.height texture:srcFrame.textureId];// Initialize the XMagic SDK.
    self.heightF = srcFrame.height;
    self.widthF = srcFrame.width;
    }
    if(self.xMagicKit!=nil && (self.heightF!=srcFrame.height || self.widthF!=srcFrame.width)){
    self.heightF = srcFrame.height;
    self.widthF = srcFrame.width;
    [self.xMagicKit setRenderSize:CGSizeMake(srcFrame.width, srcFrame.height)];
    }
    YTProcessInput *input = [[YTProcessInput alloc] init];
    input.textureData = [[YTTextureData alloc] init];
    input.textureData.texture = srcFrame.textureId;
    input.textureData.textureWidth = srcFrame.width;
    input.textureData.textureHeight = srcFrame.height;
    input.dataType = kYTTextureData;
    YTProcessOutput *output = [self.xMagicKit process:input withOrigin:YtLightImageOriginTopLeft withOrientation:YtLightCameraRotation0];
    dstFrame.textureId = output.textureData.texture;
    return 0;
    }
    Note:
    Steps 1 and 2 vary depending on the different third-party beauty products, while Step 3 is a general and important step for integrating third-party beauty features into TRTC.
    For scenario-specific integration guidelines of beauty effects, see Integrating Special Effect into TRTC SDK. For guidelines on integrating beauty effects independently, see Integrating Special Effect SDK.

    Dual-Stream Encoding Mode

    When the dual-stream encoding mode is enabled, the current user's encoder outputs two video streams, a high-definition large screen and a low-definition small screen, at the same time (but only one audio stream). In this way, other users in the room can choose to subscribe to the high-definition large screen or low-definition small screen based on their network conditions or screen sizes.
    1. Enable large-and-small-screen dual-stream encoding mode.
    - (void)enableDualStreamMode:(BOOL)enable {
    // Video encoding parameters for the small stream (customizable).
    TRTCVideoEncParam *smallVideoEncParam = [[TRTCVideoEncParam alloc] init];
    smallVideoEncParam.videoResolution = TRTCVideoResolution_480_270;
    smallVideoEncParam.videoFps = 15;
    smallVideoEncParam.videoBitrate = 550;
    smallVideoEncParam.resMode = TRTCVideoResolutionModePortrait;
    [self.trtcCloud enableEncSmallVideoStream:enable withQuality:smallVideoEncParam];
    }
    Note:
    When the dual-stream encoding mode is enabled, it consumes more CPU and network bandwidth. Therefore, it may be considered for use on Mac, Windows, or high-performance Pads. It is not recommended for mobile devices.
    2. Select the type of remote user's video stream to pull.
    // Optional video stream types when you subscribe to a remote user's video stream
    [self.trtcCloud startRemoteView:userId streamType:TRTCVideoStreamTypeBig view:view];
    
    // You can switch the size of the specified remote user's screen at any time
    [self.trtcCloud setRemoteVideoStreamType:userId type:TRTCVideoStreamTypeSmall];
    Note:
    When the dual-stream encoding mode is enabled, you can specify the video stream type as TRTCVideoStreamTypeSmall with streamType to pull a low-quality small video for viewing.

    View rendering control

    If your business involves scenarios of switching display zones, you can use the TRTC SDK to update the local preview screen and update the remote user's video rendering control feature.
    // Update local preview screen rendering control
    [self.trtcCloud updateLocalView:view];
    
    // Update the remote user's video rendering control
    [self.trtcCloud updateRemoteView:view streamType:TRTCVideoStreamTypeBig forUser:userId];
    Note:
    The pass-through parameter view refers to the target video rendering control. And streamType only supports TRTCVideoStreamTypeBig and TRTCVideoStreamTypeSub.

    Exception Handling

    Exception error handling

    When the TRTC SDK encounters an unrecoverable error, the error is thrown in the onError callback. For details, see Error Code Table.
    1. UserSig related
    UserSig verification failure leads to room-entering failure. You can use the UserSig tool for verification.
    Enumeration
    Value
    Description
    ERR_TRTC_INVALID_USER_SIG
    -3320
    Room entry parameter userSig is incorrect. Check if TRTCParams.userSig is empty.
    ERR_TRTC_USER_SIG_CHECK_FAILED
    -100018
    UserSig verification failed. Check if the parameter TRTCParams.userSig is filled in correctly or has expired.
    2. Room entry and exit related
    If room entry is failed, you should first verify the correctness of the room entry parameters. It is essential that the room entry and exit APIs are called in a paired manner. This means that, even in the event of a failed room entry, the room exit API must still be called.
    Enumeration
    Value
    Description
    ERR_TRTC_CONNECT_SERVER_TIMEOUT
    -3308
    Room entry request timed out. Check if your internet connection is lost or if a VPN is enabled. You may also attempt to switch to 4G for testing.
    ERR_TRTC_INVALID_SDK_APPID
    -3317
    Room entry parameter sdkAppId is incorrect. Check if TRTCParams.sdkAppId is empty
    ERR_TRTC_INVALID_ROOM_ID
    -3318
    Room entry parameter roomId is incorrect.Check if TRTCParams.roomId or TRTCParams.strRoomId is empty. Nnote that roomId and strRoomId cannot be used interchangeably.
    ERR_TRTC_INVALID_USER_ID
    -3319
    Room entry parameter userId is incorrect. Check if TRTCParams.userId is empty.
    ERR_TRTC_ENTER_ROOM_REFUSED
    -3340
    Room entry request was denied. Check if enterRoom is called consecutively to enter rooms with the same ID.
    3. Device related
    Errors for related monitoring devices. Prompt the user via UI in case of relevant errors.
    Enumeration
    Value
    Description
    ERR_CAMERA_START_FAIL
    -1301
    Failed to enable the camera. For example, if there is an exception for the camera's configuration program (driver) on a Windows or Mac device, you should try disabling then re-enabling the device, restarting the machine, or updating the configuration program.
    ERR_MIC_START_FAIL
    -1302
    Failed to open the mic. For example, if there is an exception for the camera's configuration program (driver) on a Windows or Mac device, you should try disabling then re-enabling the device, restarting the machine, or updating the configuration program.
    ERR_CAMERA_NOT_AUTHORIZED
    -1314
    The device of camera is unauthorized. This typically occurs on mobile devices and may be due to the user having denied the permission.
    ERR_MIC_NOT_AUTHORIZED
    -1317
    The device of mic is unauthorized. This typically occurs on mobile devices and may be due to the user having denied the permission.
    ERR_CAMERA_OCCUPY
    -1316
    The camera is occupied. Try a different camera.
    ERR_MIC_OCCUPY
    -1319
    The mic is occupied. This occurs when, for example, the user is currently having a call on the mobile device.

    Issues with the remote mirror mode not functioning properly

    In TRTC, video mirror settings are divided into local preview mirror setLocalRenderParams and video encoding mirror setVideoEncoderMirror. These settings separately affect the mirror effect of the local preview and the video encoding output (the mirror mode for remote viewers and cloud recordings). If you expect the mirror effect seen in the local preview to also take effect on the remote viewer's end, follow these encoding procedures.
    // Set the rendering parameters for the local video
    TRTCRenderParams *params = [[TRTCRenderParams alloc] init];
    params.mirrorType = TRTCVideoMirrorTypeEnable; // Video mirror mode
    params.fillMode = TRTCVideoFillMode_Fill; // Video fill mode
    params.rotation = TRTCVideoRotation_0; // Video rotation angle
    [self.trtcCloud setLocalRenderParams:params];
    // Set the video mirror mode for the encoder output
    [self.trtcCloud setVideoEncoderMirror:YES];

    Issues with camera scale, focus, and switch

    In e-commerce live streaming scenarios, the anchor may need to custom adjust the camera settings. The TRTC SDK's device management class provides APIs for these needs.
    1. Query and set the zoom factor for the camera.
    // Get the maximum zoom factor for the camera (only for mobile devices)
    CGFloat zoomRatio = [[self.trtcCloud getDeviceManager] getCameraZoomMaxRatio];
    // Set the zoom factor for the camera (only for mobile devices)
    // Value range is 1-5. 1 means the furthest field of view (normal lens), and 5 means the closest field of view (zoom-in lens). The maximum recommended value is 5. Exceeding this may result in blurry video.
    [[self.trtcCloud getDeviceManager] setCameraZoomRatio:zoomRatio];
    2. Set the focus feature and position of the camera.
    // Enable or disable the camera's autofocus feature (only for mobile devices)
    [[self.trtcCloud getDeviceManager] enableCameraAutoFocus:NO];
    // Set the focus position of the camera (only for mobile devices)
    // The precondition for using this API is to first disable the autofocus feature using enableCameraAutoFocus
    [[self.trtcCloud getDeviceManager] setCameraFocusPosition:CGPointMake(x, y)];
    3. Determine and switch to front or rear cameras.
    // Determine if the current camera is the front camera (only for mobile devices)
    BOOL isFrontCamera = [[self.trtcCloud getDeviceManager] isFrontCamera];
    // Switch to front or rear cameras (only for mobile devices)
    // Passing true means switching to front, and passing false means switching to rear
    [[self.trtcCloud getDeviceManager] switchCamera:!isFrontCamera];
    
    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