TRTCCalling API(Web)

Last updated: 2021-12-24 16:08:01

    TRTCCalling Overview

    The TRTCCalling component is based on Tencent Real-Time Communication (TRTC) and Instant Messaging (IM). It supports one-to-one and group audio/video calls. For detailed instructions on how to implement it, see Real-Time Video Call (Web).

    • TRTC SDK: The TRTC SDK is used as a low-latency audio/video call component.
    • IM SDK: The IM SDK is used to send and process signaling messages.

    TRTCCalling APIs

    Event subscribing/unsubscribing APIs

    This component bases its management on the dispatching of events. The application layer can change UI interactions according to dispatched events.

    API Description
    on(eventName, callback, context) Subscribes to an event.
    off(eventName, callback, context) Unsubscribes from an event.

    Basic SDK APIs

    API Description
    login({userID, userSig}) Logs in to IM. All IM features can be used only after login.
    logout() Logs out. No calls can be made after logout.

    Call operation APIs

    API Description
    call({userID, type, offlinePushInfo})) Invites a user to a one-to-one call.
    groupCall({userIDList, type, groupID, offlinePushInfo}) Invites users to a group call.
    accept() Accepts a call.
    reject() Rejects a call.
    hangup() Hangs up.

    Video APIs

    API Description
    startRemoteView({userID, videoViewDomID}) Starts rendering the video of a remote user.
    stopRemoteView({userID}) Stops rendering the video of a remote user.
    startLocalView({userID, videoViewDomID}) Starts rendering the video of the local user.
    stopLocalView({userID}) Stops rendering the video of the local user.
    openCamera() Turns the camera on.
    closeCamera() Turns the camera off.
    setMicMute(isMute) Mutes/Unmutes the mic.
    setVideoQuality(profile) Sets video quality.
    switchToAudioCall() Switches to audio call.
    switchToVideoCall() Switches to video call.
    getCameras() Gets the camera list.
    getMicrophones() Gets the mic list.
    switchDevice({deviceType, deviceID}) Switches to a different camera or mic.

    API Details

    Creating a TRTCCalling instance

    First, create an application in the TRTC console and get the SDKAppID.
    Then, obtain an instance of the TRTCCalling component using new TRTCCalling().

    let options = {
    SDKAppID: 0, // Replace 0 with the `SDKAppID` of your IM application when connecting.
    // The `tim` parameter was introduced in v0.10.2.
    // The parameter guarantees the uniqueness of an existing TIM instance.
    tim: tim
    };
    let trtcCalling = new TRTCCalling(options);

    Event subscribing/unsubscribing APIs

    on(eventName, callback, context)

    This API is used to subscribe to an event dispatched by the component. For details about the events, see the event list.

    let handleInvite = function ({inviteID, sponsor, inviteData}) {
    console.log(`inviteID: ${inviteID}, sponsor: ${sponsor}, inviteData: ${JSON.stringify(inviteData)}`);
    };
    trtcCalling.on('onInvited', handleInvite, this);

    off(eventName, callback, context)

    This API is used to unsubscribe from an event.

    let handleInvite = function ({inviteID, sponsor, inviteData}) {
    console.log(`inviteID: ${inviteID}, sponsor: ${sponsor}, inviteData: ${JSON.stringify(inviteData)}`);
    };
    trtcCalling.off('onInvited', handleInvite, this);

    Basic SDK APIs

    login({userID, userSig})

    This API is used to log in.

    trtcCalling.login({userID, userSig})

    The parameters are as detailed below:

    Parameter Type Description
    userID String ID of the current user, which is a string that can contain only letters (a-z and A-Z), digits (0-9), hyphens (-), and underscores (_)
    userSig String Tencent Cloud's proprietary security signature. For how to calculate it, see FAQs > UserSig.

    logout()

    This API is used to log out.

    trtcCalling.logout()

    Call operation APIs

    call({userID, type, offlinePushInfo})

    This API is used to make one-to-one calls. type indicates the type of a call. 1: audio call; 2: video call.

    Note:

    • The timeout parameter is deleted from v1.0.0 and later versions.
    • A new parameter, offlinePushInfo, is introduced for offline notifications, which are supported only on Android and iOS, not on web or WeChat Mini Program.
    // Versions earlier than v1.0.0
    trtcCalling.call({userID, type, timeout});

    // v1.0.0 and later versions
    const offlinePushInfo = {
    title: '',
    description: 'You are invited to a call.',
    }
    trtcCalling.call({userID, type, offlinePushInfo})

    The parameters are as detailed below:

    Parameter Type Description
    userID String userID of the invitee
    type Number Call type. 1: audio call; 2: video call
    timeout Number Timeout period (s). 0 means the call never times out. This parameter is available only in versions earlier than v1.0.0.
    offlinePushInfo Object Offline notifications (optional). This parameter is available only in v1.0.0 and later versions.

    offlinePushInfo (in v1.0.0 and later versions)

    Parameter Type Description
    title String Title of an offline notification (optional)
    description String Content of an offline notification (optional)
    androidOPPOChannelID String Channel ID for an offline notification on OPPO 8.0 and above (optional)

    groupCall({userIDList, type, groupID, offlinePushInfo})

    The groupID parameter is the group ID in the IM SDK. If this parameter is set, call invitations will be broadcast by the group messaging system, which is a simple and reliable way of sending call invitations. If this parameter is left empty, the TRTCCalling component will send an invitation to every invitee.

    Note:

    In v1.0.0 and later versions, a new parameter, offlinePushInfo, is introduced for offline notifications, which are supported only on Android and iOS, not on web or WeChat Mini Program.

    // Versions earlier than v1.0.0
    trtcCalling.groupCall({userIDList, type, groupID});

    // v1.0.0 and later versions
    const offlinePushInfo = {
    title: '',
    description: 'You are invited to a call.',
    }
    trtcCalling.groupCall({userIDList, type, groupID, offlinePushInfo})

    The parameters are as detailed below:

    Parameter Type Description
    userIDList Array List of the user IDs of invitees
    type Number Call type. 1: audio call; 2: video call
    groupID String IM group ID (optional)
    offlinePushInfo Object Offline notifications (optional). This parameter is available only in v1.0.0 and later versions.

    offlinePushInfo (in v1.0.0 and later versions)

    Parameter Type Description
    title String Title of an offline notification (optional)
    description String Content of an offline notification (optional)
    androidOPPOChannelID String Channel ID for an offline notification on OPPO 8.0 and above (optional)

    accept()

    This API is used to accept an invitation.

    Note:

    • If a prior invitation has not been processed, the component will return a message indicating that the line is busy.
    • params is deleted from v1.0.0 and later versions.
    import TRTCCalling from 'trtc-calling-js';
    trtcCalling.on(TRTCCalling.EVENT.INVITED, ({inviteID, sponsor, inviteData}) => {
    // ...
    // Versions earlier than v1.0.0
    trtcCalling.accept({inviteID, roomID, callType})
    // v1.0.0 and later versions
    trtcCalling.accept();
    })

    The parameters are as detailed below:

    Parameter Type Description
    inviteID String Invitation ID, which identifies an invitation. This parameter is available only in versions earlier than v1.0.0.
    roomID Number Room ID. This parameter is available only in versions earlier than v1.0.0.
    callType Number Call type. 1: audio call; 2: video call. This parameter is available only in versions earlier than v1.0.0.

    reject()

    This API is used to reject an invitation.

    Note:

    params is deleted from v1.0.0 and later versions.

    import TRTCCalling from 'trtc-calling-js';
    trtcCalling.on(TRTCCalling.EVENT.INVITED, ({inviteID, sponsor, inviteData}) => {
    // ...
    // Versions earlier than v1.0.0
    trtcCalling.reject({inviteID, isBusy, callType})
    // v1.0.0 and later versions
    trtcCalling.reject();
    })

    The parameters are as detailed below:

    Parameter Type Description
    inviteID String Invitation ID, which identifies an invitation. This parameter is available only in versions earlier than v1.0.0.
    isBusy Boolean Whether the line is busy. This parameter is available only in versions earlier than v1.0.0.
    callType Number Call type. 1: audio call; 2: video call. This parameter is available only in versions earlier than v1.0.0.

    hangup()

    1. If you are in a call, you can use this API to end the call.
    2. If your call is not answered yet, you can use this API to cancel the call.
    trtcCalling.hangup()

    Video APIs

    startRemoteView({userID, videoViewDomID})

    This API is used to render the camera data of a remote user in a specified DOM node.

    trtcCalling.startRemoteView({userID, videoViewDomID})

    The parameters are as detailed below:

    Parameter Type Description
    userID String User ID
    videoViewDomID String The DOM node in which the user’s data is to be rendered. The data will be played via the video tag of the node.

    stopRemoteView({userID})

    This API is used to delete the DOM node in which the camera data of a remote user is rendered.

    Note:

    videoViewDomID is deleted from v1.0.0 and later versions.

    // Versions earlier than v1.0.0
    trtcCalling.stopRemoteView({userID, videoViewDomID});
    // v1.0.0 and later versions
    trtcCalling.stopRemoteView({userID});

    The parameters are as detailed below:

    Parameter Type Description
    userID String User ID
    videoViewDomID String The DOM node whose video tag is to be deleted. The playback will stop. This parameter is available only in versions earlier than v1.0.0.

    startLocalView({userID, videoViewDomID})

    This API is used to render the camera data of the local user in a specified DOM node.

    trtcCalling.startLocalView({userID, videoViewDomID})

    The parameters are as detailed below:

    Parameter Type Description
    userID String User ID
    videoViewDomID String The DOM node in which the local user’s data is to be rendered. The data will be played via the video tag of the node.

    stopLocalView({userID})

    This API is used to delete the DOM node in which the camera data of the local user is rendered.

    Note:

    videoViewDomID is deleted from v1.0.0 and later versions.

    // Versions earlier than v1.0.0
    trtcCalling.stopLocalView({userID, videoViewDomID});
    // v1.0.0 and later versions
    trtcCalling.stopLocalView({userID});

    The parameters are as detailed below:

    Parameter Type Description
    userID String User ID
    videoViewDomID String The DOM node whose video tag is to be deleted. The playback will stop. This parameter is available only in versions earlier than v1.0.0.

    openCamera()

    This API is used to turn the local camera on.

    trtcCalling.openCamera()

    closeCamera()

    This API is used to turn the camera off.

    trtcCalling.closeCamera()

    setMicMute(isMute)

    This API is used to turn the mic on/off.

    trtcCalling.setMicMute(true) // Turn the mic off

    The parameters are as detailed below:

    Parameter Type Description
    isMute Boolean
  • true: turn the mic off;
  • false: turn the mic on
  • setVideoQuality(profile)

    This API is used to set video quality.

    Note:

    • This is a new API in v0.8.0 and later versions.
    • This API must be called before call, groupCall, or accept to take effect.
    trtcCalling.setVideoQuality('720p') // Set video quality to `720p`

    The parameters are as detailed below:

    Parameter Type Description
    profile String
  • 480p: 640 × 480
  • 720p: 1280 × 720
  • 1080p: 1920 × 1080
  • switchToAudioCall()

    This API is used to switch from video call to audio call.

    Note:

    • This is a new API in v0.10.0 and later versions.
    • It can be used only in one-to-one calls.
    • The error callback (code: 60001) indicates that the switching has failed.
    trtcCalling.switchToAudioCall() // Switch from video call to audio call

    switchToVideoCall()

    This API is used to switch from audio call to video call.

    Note:

    • This is a new API in v0.10.0 and later versions.
    • It can be used only in one-to-one calls.
    • The error callback (code: 60002) indicates that the switching has failed.
    trtcCalling.switchToVideoCall() // Switch from audio call to video call

    getCameras()

    This API is used to get the camera list.

    Note:

    This is a new API in v1.0.0 and later versions.

    trtcCalling.getCameras() // Get the camera list

    getMicrophones()

    This API is used to get the mic list.

    Note:

    This is a new API in v1.0.0 and later versions.

    trtcCalling.getMicrophones() // Get the mic list

    switchDevice({deviceType,deviceID})

    This API is used to switch to a different camera or mic.

    Note:

    This is a new API in v1.0.0 and later versions.

    trtcCalling.switchDevice(deviceType, deviceID) // Switch to a different device

    The parameters are as detailed below:

    Parameter Type Description
    deviceType String Device type. video: camera; audio: mic
    deviceID String Device ID.
  • You can use getCameras() to get the ID of a camera.
  • You can use getMicrophones() to get the ID of a mic.
  • TRTCCalling Events

    The code below demonstrates how to listen for TRTCCalling events.

    import TRTCCalling from 'trtc-calling-js';
    // etc
    function handleInviteeReject({userID}) {

    }
    trtcCalling.on(TRTCCalling.EVENT.REJECT, handleInviteeReject)

    Inviter events

    Code Event Recipient Description
    REJECT Inviter The invitee rejected the call.
    NO_RESP Inviter The invitation timed out without response from the invitee.
    LINE_BUSY Inviter The invitee is in a call, i.e., the line is busy.
    INVITED Invitee You received an invitation.
    CALLING_CANCEL Invitee The call is canceled.
    CALLING_TIMEOUT Invitee The invitation timed out.
    USER_ENTER Inviter and invitee A user entered the room.
    USER_LEAVE Inviter and invitee A user left the call.
    CALL_END Inviter and invitee The call ended.
    KICKED_OUT Inviter and invitee A user was kicked out due to repeated login.
    USER_VIDEO_AVAILABLE Inviter and invitee A remote user turned the camera on/off.
    USER_AUDIO_AVAILABLE Inviter and invitee A remote user turned the mic on/off.

    Common event callback APIs

    SDK_READY

    The SDK is ready.

    Note:

    This is a new callback in v1.0.0 and later versions.

    let onSDKReady = function(event) {
    console.log(event)
    };
    trtcCalling.on(TRTCCalling.EVENT.SDK_READY, onSDKReady);

    USER_ENTER

    A user entered the room.
    This callback is triggered when a user joins the call.

    let handleUserEnter = function({userID}) {
    console.log(userID)
    };
    trtcCalling.on(TRTCCalling.EVENT.USER_ENTER, handleUserEnter);

    The parameters are as detailed below:

    Parameter Type Description
    userID String User ID

    USER_LEAVE

    A user left the room.
    This callback is triggered when a user leaves the call.

    let handleUserLeave = function({userID}) {
    console.log(userID)
    };
    trtcCalling.on(TRTCCalling.EVENT.USER_LEAVE, handleUserLeave);

    The parameters are as detailed below:

    Parameter Type Description
    userID String User ID

    GROUP_CALL_INVITEE_LIST_UPDATE

    The invitee list for a group call was updated.

    Note:

    This is a new callback in v1.0.0 and later versions.

    let handleGroupInviteeListUpdate = function(event) {
    console.log(event)
    };
    trtcCalling.on(TRTCCalling.EVENT.GROUP_CALL_INVITEE_LIST_UPDATE, handleGroupInviteeListUpdate);

    CALL_END

    The call ended.
    This callback is triggered when a call ends.

    let handleCallingEnd = function(event) {
    console.log(event)
    };
    trtcCalling.on(TRTCCalling.EVENT.CALL_END, handleCallingEnd);

    KICKED_OUT

    A user was kicked out due to repeated login.
    This callback is triggered if a user logs in with the same account twice.

    let handleKickedOut = function(event) {
    console.log(event)
    };
    trtcCalling.on(TRTCCalling.EVENT.KICKED_OUT, handleKickedOut);

    USER_VIDEO_AVAILABLE

    A remote user turned the camera on/off.
    This callback is triggered when a remote user turns the camera on/off.

    let handleUserVideoChange = function({userID, isVideoAvailable}) {
    console.log(userID, isVideoAvailable)
    };
    trtcCalling.on(TRTCCalling.EVENT.USER_VIDEO_AVAILABLE, handleUserVideoChange);

    The parameters are as detailed below:

    Parameter Type Description
    userID String User ID
    isVideoAvailable Boolean
  • true: The remote user turned the camera on.
  • false: The remote user turned the camera off.
  • USER_AUDIO_AVAILABLE

    A remote user turned the mic on/off.
    This callback is triggered when a remote user turns the mic on/off.

    let handleUserAudioChange = function({userID, isAudioAvailable}) {
    console.log(userID, isAudioAvailable)
    };
    trtcCalling.on(TRTCCalling.EVENT.USER_AUDIO_AVAILABLE, handleUserAudioChange);

    The parameters are as detailed below:

    Parameter Type Description
    userID String User ID
    isAudioAvailable Boolean
  • true: The remote user turned the mic on.
  • false: The remote user turned the mic off.
  • Inviter event callback APIs

    REJECT

    The user rejected the call.
    The inviter of a call will receive this callback if an invitee rejects the call.

    let handleInviteeReject = function({userID}) {
    console.log(userID)
    };
    trtcCalling.on(TRTCCalling.EVENT.REJECT, handleInviteeReject);

    The parameters are as detailed below:

    Parameter Type Description
    userID String User ID

    NO_RESP

    The invitee did not answer.
    In cases where a timeout period is specified for a one-to-one or group call, the inviter will receive this callback if an invitee does not answer the call within the specified timeout period.

    let handleNoResponse = function({userID, userIDList}) {
    console.log(userID, userIDList)
    };
    trtcCalling.on(TRTCCalling.EVENT.NO_RESP, handleNoResponse);

    The parameters are as detailed below:

    Parameter Type Description
    userID String User ID
    userIDList Array List of timed out users

    LINE_BUSY

    The invitee is in a call, i.e., the line is busy.
    The inviter of a call will receive this callback if the invitee is already in a call.

    let handleLineBusy = function({userID}) {
    console.log(userID)
    };
    trtcCalling.on(TRTCCalling.EVENT.LINE_BUSY, handleLineBusy);

    The parameters are as detailed below:

    Parameter Type Description
    userID String User ID

    Invitee event callback APIs

    INVITED

    An invitation was received.
    A user will receive this callback if he or she is invited to a call.

    let handleNewInvitationReceived = function({
    sponsor, userIDList, isFromGroup, inviteData, inviteID
    }
    )
    {
    console.log(sponsor, userIDList, isFromGroup, inviteData, inviteID)
    };
    trtcCalling.on(TRTCCalling.EVENT.INVITED, handleNewInvitationReceived);

    The parameters are as detailed below:

    Parameter Type Description
    sponsor String Inviter
    userIDList Array Users invited to the same call
    isFromGroup Boolean Whether it is an IM group invitation
    inviteData Object
  • For a new user invitation: {version, callType, roomID}
  • For the last user hanging up: {version, callType, callEnd}
  • inviteID String Invitation ID, which identifies an invitation

    CALLING_CANCEL

    The call is canceled.
    An invitee of a call will receive this callback if the call is canceled.

    let handleCallingCancel = function(event) {
    console.log(event)
    };
    trtcCalling.on(TRTCCalling.EVENT.CALLING_CANCEL, handleCallingCancel);

    CALLING_TIMEOUT

    The call timed out.
    In cases where a timeout period is specified for a one-to-one or group call, an invitee will receive this callback if he or she does not answer the call within the specified timeout period.

    let handleCallingTimeout = function(event) {
    console.log(event)
    };
    trtcCalling.on(TRTCCalling.EVENT.CALLING_TIMEOUT, handleCallingTimeout);

    TRTCCalling Error Codes

    You can handle the errors thrown by the TRTCCalling component by listening for the “error” field in events. Below is an example.

    import TRTCCalling from 'trtc-calling-js';
    let onError = function(error) {
    console.log(error);
    };
    trtcCalling.on(TRTCCalling.EVENT.ERROR, onError);

    Error codes

    Code Type Description
    60001 API call failure Failed to call switchToAudioCall.
    60002 API call failure Failed to call switchToVideoCall.
    60003 Access failure No available mic.
    60004 Access failure No available camera.
    60005 Access failure The user denied access.

    FAQs

    Why can’t I get through to a user? Why am I kicked offline?

    The TRTCCalling component does not support login of multiple instances or offline signaling for the time being. Please make sure that your current login is unique.

    Note:

    • Multiple instances: a user ID logs in multiple times or on different devices, which disrupts signaling.
    • Offline signaling: only online instances can receive a message. Messages sent to offline instances will not be sent again when the instances go online.