Platform | Sample Code |
Windows (MFC) | |
Windows (Duilib) | |
Windows (C#) |
TRTCCloud and subscribe to the SDK’s event callbacks.ITRTCCloudCallback callback API class and rewrite the callback APIs for key events including room entry/exit by local user, room entry/exit by remote user, error event, and warning event.addCallback API to subscribe to the SDK’s events.addCallback is called N times, the SDK will trigger N callbacks for the same event. Therefore, you are advised to call addCallback only once.// TRTCMainViewController.h// Inherit the `ITRTCCloudCallback` callback API classclass TRTCMainViewController : public ITRTCCloudCallback{public:TRTCMainViewController();virtual ~TRTCMainViewController();virtual void onError(TXLiteAVError errCode, const char* errMsg, void* arg);virtual void onWarning(TXLiteAVWarning warningCode, const char* warningMsg, void* arg);virtual void onEnterRoom(uint64_t elapsed);virtual void onExitRoom(int reason);virtual void onRemoteUserEnterRoom(const char* userId);virtual void onRemoteUserLeaveRoom(const char* userId, int reason);virtual void onUserVideoAvailable(const char* userId, bool available);virtual void onUserAudioAvailable(const char* userId, bool available);...private:ITRTCCloud * m_pTRTCSDK = NULL;...}// TRTCMainViewController.cppTRTCMainViewController::TRTCMainViewController(){// Create a `TRTCCloud` instancem_pTRTCSDK = getTRTCShareInstance();// Subscribe to the SDK’s eventsm_pTRTCSDK->addCallback(this);}TRTCMainViewController::~TRTCMainViewController(){// Unsubscribe from the SDK’s eventsif(m_pTRTCSDK) {m_pTRTCSDK->removeCallback(this);}// Release the `TRTCCloud` instanceif(m_pTRTCSDK != NULL) {destroyTRTCShareInstance();m_pTRTCSDK = null;}}// Error notifications indicate that the SDK has stopped working and therefore must be listened for.virtual void TRTCMainViewController::onError(TXLiteAVError errCode, const char* errMsg, void* arg){if (errCode == ERR_ROOM_ENTER_FAIL) {LOGE(L"onError errorCode[%d], errorInfo[%s]", errCode, UTF82Wide(errMsg).c_str());exitRoom();}}
// TRTCMainForm.cs// Inherit the `ITRTCCloudCallback` callback API classpublic partial class TRTCMainForm : Form, ITRTCCloudCallback, ITRTCLogCallback{...private ITRTCCloud mTRTCCloud;...public TRTCMainForm(TRTCLoginForm loginForm){InitializeComponent();this.Disposed += new EventHandler(OnDisposed);// Create a `TRTCCloud` instancemTRTCCloud = ITRTCCloud.getTRTCShareInstance();// Subscribe to the SDK’s eventsmTRTCCloud.addCallback(this);...}private void OnDisposed(object sender, EventArgs e){if (mTRTCCloud != null){// Unsubscribe from the SDK’s eventsmTRTCCloud.removeCallback(this);// Release the `TRTCCloud` instanceITRTCCloud.destroyTRTCShareInstance();mTRTCCloud = null;}...}...// Error notifications indicate that the SDK has stopped working and therefore must be listened for.public void onError(TXLiteAVError errCode, string errMsg, IntPtr arg){if (errCode == TXLiteAVError.ERR_ROOM_ENTER_FAIL) {exitRoom();}...}...}
TRTCParamsTRTCParams is the most critical parameter in the SDK. It contains four required fields: sdkAppId, userId, userSig, and roomId.SDKAppID.userId in a room.SDKAppID and userID. For details, see UserSig.roomId to two rooms under the same application. For string-type room ID, use strRoomId in TRTCParams.startLocalPreview to turn the local camera on and enable preview, and stopLocalPreview to disable camera capturing and preview.setLocalViewFillMode to set the video display mode to Fill or Fit. Video may be resized proportionally in both modes, but they differ in that:Fill mode, the image fills the entire screen. If the dimensions of the image do not match those of the screen after scaling, the parts that do not fit are cropped.Fit mode, the image is displayed in whole. If the dimensions of the image do not match those of the screen after scaling, the unoccupied space is painted black.void TRTCMainViewController::onEnterRoom(uint64_t elapsed){// Get the handle of the rendering windowCWnd *pLocalVideoView = GetDlgItem(IDC_LOCAL_VIDEO_VIEW);HWND hwnd = pLocalVideoView->GetSafeHwnd();if(m_pTRTCSDK){// Call the APIs below to set the rendering mode and rendering windowm_pTRTCSDK->setLocalViewFillMode(TRTCVideoFillMode_Fit);m_pTRTCSDK->startLocalPreview(hwnd);}}
// TRTCMainForm.cspublic void onEnterRoom(int result){...// Get the handle of the rendering windowIntPtr ptr = GetHandle();if (mTRTCCloud != null){// Call the APIs below to set the rendering mode and rendering windowmTRTCCloud.setLocalViewFillMode(TRTCVideoFillMode_Fit);mTRTCCloud.startLocalPreview(ptr);}...}
startLocalAudio to enable local audio capturing and send the data captured, and stopLocalAudio to disable audio capturing. You can call startLocalAudio after startLocalPreview.startLocalAudio, the SDK will check mic access and will ask for mic permission from the user if it does not have access.enterRoom to create a room, setting role to TRTCRoleAnchor (anchor) and specifying roomId in the TRTCParams parameter.appScene, which indicates the application scenario. TRTCAppSceneLIVE (online live streaming) is used in the example of this document.onEnterRoom callback, in which the elapsed field represents the time (ms) room entry takes.onError callback, which contains errCode (error code, whose value is ERR_ROOM_ENTER_FAIL; for other error code values, please see TXLiteAVCode.h), errMsg (error message), and extraInfo (reserved parameter).// TRTCMainViewController.cppvoid TRTCMainViewController::startBroadCasting(){// For the definition of `TRTCParams`, please see the header file `TRTCCloudDef.h`.TRTCParams params;params.sdkAppId = sdkappid;params.userId = userid;params.userSig = usersig;params.roomId = 908; // Set it to the ID of the room you want to enterparams.role = TRTCRoleAnchor; //Anchorif(m_pTRTCSDK){m_pTRTCSDK->enterRoom(params, TRTCAppSceneLIVE);}}void TRTCMainViewController::onError(TXLiteAVError errCode, const char* errMsg, void* arg){if(errCode == ERR_ROOM_ENTER_FAIL){LOGE(L"onError errorCode[%d], errorInfo[%s]", errCode, UTF82Wide(errMsg).c_str());// Check whether `userSig` is valid, network is normal, etc.}}...void TRTCMainViewController::onEnterRoom(uint64_t elapsed){LOGI(L"onEnterRoom elapsed[%lld]", elapsed);// Enable local video preview. For details, please see the sections below about encoding settings and local video preview}
// TRTCMainForm.cspublic void createRoom(){// For the definition of `TRTCParams`, please see the `TRTCCloudDef.h` header fileTRTCParams @params = new TRTCParams();@params.sdkAppId = sdkappid;@params.userId = userid;@params.userSig = usersig;@params.roomId = 908; // Set it to the ID of the room you want to enter@params.role = TRTCRoleAnchor; // Anchorif(mTRTCCloud != null){mTRTCCloud.enterRoom(@params, TRTCAppSceneLIVE);}}...public void onError(TXLiteAVError errCode, string errMsg, IntPtr arg){if(errCode == TXLiteAVError.ERR_ROOM_ENTER_FAIL){Log.E(String.Format("errCode : {0}, errMsg : {1}, arg = {2}", errCode, errMsg, arg));// Check whether `userSig` is valid, network is normal, etc.}}...public void onEnterRoom(int result){// Enable local video preview. For details, please see the sections below about encoding settings and local video preview}
muteLocalVideo to stop publishing local video and muteLocalAudio to stop publishing local audio.enterRoom to enter the room, specifying the room number via the roomId field in TRTCParams.
Set appScene to TRTCAppSceneLIVE (online live streaming), and role to TRTCRoleAudience (audience).void TRTCMainViewController::startPlaying(){// For the definition of `TRTCParams`, please see the header file `TRTCCloudDef.h`.TRTCParams params;params.sdkAppId = sdkappid;params.userId = userid;params.userSig = usersig;params.roomId = 908; // Set it to the ID of the room you want to enterparams.role = TRTCRoleAudience; // Viewerif(m_pTRTCSDK){m_pTRTCSDK->enterRoom(params, TRTCAppSceneLIVE);}}
public void startPlaying(){// For the definition of `TRTCParams`, please see the `TRTCCloudDef.h` header fileTRTCParams @params = new TRTCParams();@params.sdkAppId = sdkappid;@params.userId = userid;@params.userSig = usersig;@params.roomId = 908; // Set it to the ID of the room you want to enter@params.role = TRTCRoleAudience; // Viewerif(mTRTCCloud != null){mTRTCCloud.enterRoom(@params, TRTCAppSceneLIVE);}}
userid in the onUserVideoAvailable callback in TRTCCloudDelegate, and then call startRemoteView to display the anchor’s video.setRemoteViewFillMode to set the video display mode to Fill or Fit. Video may be resized proportionally in both modes, but they differ in that:Fill mode, the image fills the entire screen. If the dimensions of the image do not match those of the screen after scaling, the excess parts are cropped.Fit mode, the image is displayed in whole. If the dimensions of the image do not match those of the screen after scaling, the blank area is filled with black bars.void TRTCMainViewController::onUserVideoAvailable(const char* userId, bool available){if (available) {// Get the handle of the rendering windowCWnd *pRemoteVideoView = GetDlgItem(IDC_REMOTE_VIDEO_VIEW);HWND hwnd = pRemoteVideoView->GetSafeHwnd();// Set the rendering mode of the remote videom_pTRTCSDK->setRemoteViewFillMode(TRTCVideoFillMode_Fill);// Call the API below to play the remote videom_pTRTCSDK->startRemoteView(userId, hwnd);} else {m_pTRTCSDK->stopRemoteView(userId);}}
public void onUserVideoAvailable(string userId, bool available){if (available){// Get the window handleIntPtr ptr = GetHandleAndSetUserId(pos, userId, false);SetVisableInfoView(pos, false);// Set the rendering mode of the remote videomTRTCCloud.setRemoteViewFillMode(userId, TRTCVideoFillMode.TRTCVideoFillMode_Fit);// Call the API below to play the remote videomTRTCCloud.startRemoteView(userId, ptr);}else{mTRTCCloud.stopRemoteView(userId);...}}
TRTCAppSceneLIVE mode, there is no limit on the number of users in the role of “audience” (TRTCRoleAudience) in a room.appScene must be the same on each client. Inconsistent appScene may cause unexpected problems.switchRole API of TRTCCloud to switch their roles. The most common application for the API is co-anchoring: audience call this API to switch their role to “anchor” so as to interact with the room owner.exitRoom to exit the room. Whether the live streaming has ended or not, the SDK will start a complex handshake process where it releases all resources used by the live streaming. The process finishes only after you receive the onExitRoom callback.// TRTCMainViewController.cppvoid TRTCMainViewController::exitRoom(){if(m_pTRTCSDK){m_pTRTCSDK->exitRoom();}}....void TRTCMainViewController::onExitRoom(int reason){// Exited room successfully. `reason` is a reserved parameter and is not used for the time being.}
// TRTCMainForm.cspublic void OnExit(){if(mTRTCCloud != null){mTRTCCloud.exitRoom();}}...public void onExitRoom(int reason){// Exited room successfully...}
Feedback