Callbacks

更新时间:
复制 MD 格式

This topic describes the callbacks provided by Real-Time Communication (RTC) SDK for web.

Important From February 2020, Alibaba Cloud starts to phase out RTC SDKs whose versions are earlier than V1.9.1. Install and use RTC SDKs of V1.9.1 and later versions. For more information, see SDK download.

List of methods

MethodDescriptionSupported earliest version
onPublisherThe callback that is invoked when a remote stream is ingested. V1.2
onUnPublisherThe callback that is invoked when remote stream ingest is canceled. V1.2
onMediaStreamThe callback that is invoked when a remote stream is subscribed. This method is valid only for RTC SDK whose version is earlier than V1.10. V1.2
onJoinThe callback that is invoked when a user joins a channel. V1.2
onLeaveThe callback that is invoked when a user leaves a channel. V1.2
onErrorThe callback that is invoked when an error occurs. V1.2
onByeThe callback that is invoked when a user is forced to leave a channel. V1.11
onUpdateRoleThe callback that is invoked when the role of a user is changed. V1.12
onNotifyThe callback that is invoked when the stream ingest status of a user changes. V1.12.1
onAudioLevelThe callback that is invoked to return the maximum volume of the audio track. V1.13.0
onMediaThe callback that is invoked to return the information about ingested and subscribed streams. V1.14.0
onMediaStreamUpdateThe callback that is invoked to return subscribed streams. V1.15.4
onNetworkQualityThe callback that is invoked to return the network quality. V1.16.0
onAudioMixingPlayedThe callback that is invoked when the playback of the background music starts. V1.16.0
onAudioMixingFinishedThe callback that is invoked when the playback of the background music stops. V1.16.0
onPublishChangeThe callback that is invoked when local streams are automatically ingested. V1.17.0
onSubscribeChangeThe callback that is invoked when a remote stream is automatically subscribed. V1.17.0
onShareSystemAudioStart The callback that is invoked when the system audio is shared. V1.17.0

Details of methods

  • onPublisher: the callback that is invoked when a remote stream is ingested. This callback is invoked when another user in the channel ingests a stream.
    aliWebrtc.on('onPublisher',(publisher) =>{
        // The ID of the user who ingests the remote stream.
        console.log(publisher.userId);
        // The display name of the user who ingests the remote stream.
        console.log(publisher.displayName);
        // The content of the remote stream. The value of the streamConfigs parameter is an array.
        console.log(publisher.streamConfigs);
      });
  • onUnPublisher: the callback that is invoked when remote stream ingest is canceled. This callback is invoked when another user in the channel cancels the ingest of a stream.
    aliWebrtc.on('onUnPublisher',(publisher) =>{
        // The ID of the user who cancels the ingest of the remote stream.
        console.log(publisher.userId);
        // The display name of the user who cancels the ingest of the remote stream.
        console.log(publisher.displayName);
    });
  • onMediaStream: the callback that is invoked when a remote stream is subscribed. This callback is invoked when the request to subscribe to a remote stream is successful. The callback returns the stream object that can be played by using an HTML5 player.
    Note This callback is not supported in RTC SDK of V1.10 or later.
    aliWebrtc.on('onMediaStream',(subscriber, stream) =>{
         var video = document.getElementByTag('video');
         aliWebrtc.setDisplayRemoteVideo(
            subscriber,        // The parameter returned in the callback.
            video,             // The HTML video element that is used to play the stream object.
            stream             // The parameter returned in the callback.
         )
     });
  • onJoin: the callback that is invoked when a user joins a channel.
    aliWebrtc.on('onJoin',(data) =>{
       console.log(data.userId);
       console.log(data.displayName + "joins the channel");
     });
  • onLeave: the callback that is invoked when a user leaves a channel.
    aliWebrtc.on('onLeave',(data) =>{
       console.log(data.userId);
       console.log(data.displayName  + "leaves the channel");
     });
  • onError: the callback that is invoked when an error occurs.
    aliWebrtc.on('onError',(error) =>{
       var msg = error && error.message ? error.message : error;
       if (msg && msg.indexOf('no session') > 0) {
            error = "Please log on again:" + msg;
       }
       console.log ('Error:' + msg);
     });
  • onBye: the callback that is invoked when a user is forced to leave a channel.
    aliWebrtc.on('onBye',(message) =>{
    // 1: The user is removed from a channel by the server.
    // 2: The user is removed from a channel because the channel is closed.
    // 3: A user joins the channel by using the same user ID on another device.
    console.log(message.code);
    });
  • onUpdateRole: the callback that is invoked when the role of a user is changed.
    aliWebrtc.on("onUpdateRole",(data)=>{
        console.log(data);
    })
    Note For a channel in the interaction mode, the request to change the role of a user is successful only if the setClientRole method returns a value of true and the onUpdateRole callback returns the value of the new role.
  • onNotify: the callback that is invoked when the stream ingest status of a user changes.
    aliWebrtc.on("onNotify",(data)=>{
        console.log(data);
    })
    Note
    • If the muteLocalMic or muteLocalCamera method is called to change the stream ingest status of a local or remote user, this callback is invoked.
    • The content of the data parameter returned by this callback is the same as that returned by the getUserList method.
  • onAudioLevel: the callback that is invoked to return the maximum volume of the audio track.
    Note This callback can be invoked only after you call the enableAudioVolumeIndicator method to enable audio callbacks. After that, this callback returns the maximum volume of the audio track every second.
    aliWebrtc.on("onAudioLevel", (data) => {
      console.log(data)
    })
    Return value description:
    • After you ingest an audio stream, you can obtain the information about the audio stream in the returned array. The user ID for your audio stream is 0.
    • If you subscribe to remote audio streams, the returned array contains information about the remote audio streams.
    • The following table describes the parameters in the returned array.
      ParameterTypeDescription
      userIdStringThe ID of the user who ingests the stream for an audio track. The user ID for your audio track is 0.
      displayNameStringThe display name of the user.
      levelNumberThe volume of the audio track. Valid values: 0 to 100.
      bufferArrayThe pulse-code modulation (PCM) audio data in each second.
  • onMedia: the callback that is invoked to return the information about ingested and subscribed streams.
    aliWebrtc.on("onMedia", (data) => {
      console.log(data)
    })
    Note
    • This callback is invoked to return information about ingested and subscribed streams every second.
    • The bytesSentPerSecond parameter is returned only for ingested streams, and the bytesReceivedPerSecond parameter is returned only for subscribed streams.
    ParameterTypeDescription
    userIdStringThe ID of the user who ingests the stream. The user ID for your stream is 0.
    rttNumberThe network latency. Unit: milliseconds. The value indicates the round trip time of the network link between the SDK and the Alibaba Cloud server. A smaller value indicates better network quality. In most cases, a value less than 50 ms is preferred. A value greater than 100 ms causes delays in communication.
    audioObjectThe audio track.
    levelNumber

    The audio energy level. Valid values: 0 to 1.

    cameraObjectThe camera track.
    widthNumberThe width of the video.
    heightNumberThe height of the video.
    fpsNumberThe frame rate of the video.
    screenObjectThe screen track.
    widthNumberThe width of the video.
    heightNumberThe height of the video.
    fpsNumberThe frame rate of the video.
    bytesSentPerSecondNumberThe number of bytes sent per second. This parameter is valid only for ingested streams.
    bytesReceivedPerSecondNumberThe number of bytes received per second. This parameter is valid only for subscribed streams.
  • onMediaStreamUpdate: the callback that is invoked to return subscribed streams.
    aliWebrtc.on("onMediaStreamUpdate", (data) => {
      console.log(data)
    })
    ParameterTypeDescription
    userIdStringThe ID of the user who ingest the stream.
    mediaStreamMediaStreamThe remote stream that is returned.
    Note You can call the getTrack() method of the mediaStream object to obtain the remote stream. Then, you can play the stream based on custom playback configurations. The type of the stream is identified by the track.id parameter. The following table describes the values of the track.id parameter.
    track.idDescription
    sophon_video_camera_largeThe major stream from the camera.
    sophon_video_camera_smallThe minor stream from the camera.
    sophon_video_screen_shareThe screen track.
    sophon_audioThe audio track.
  • onNetworkQuality: the callback that is invoked to return the network quality.
    aliWebrtc.on("onNetworkQuality", (data) => {
        console.log(data)
    })
    ParameterTypeDescription
    uplinkNetworkQualityNumberThe quality of the upstream network. The following table describes the details of the network quality.
    downlinkNetworkQualityNumberThe quality of the downstream network. The following table describes the details of the network quality.
    Network qualityValueDescription
    NETWORK_QUALITY_UNKNOWN0The network quality is unknown. This value is returned if no streams are ingested or subscribed.
    NETWORK_QUALITY_EXCELLENT1The network quality is excellent.
    NETWORK_QUALITY_GOOD2The network quality is good. The video is as smooth and clear as that transmitted in the excellent network quality. However, the bitrate of the video is lower.
    NETWORK_QUALITY_POOR3The network quality is merely acceptable. The video is not smooth and clear enough but the communication is not affected.
    NETWORK_QUALITY_BAD4The network quality is poor. Stuttering occurs but the communication is not interrupted.
    NETWORK_QUALITY_VERYBAD5The network quality is worse, and the communication is severely affected.
    NETWORK_QUALITY_DISCONNECT6The network is disconnected.
  • onAudioMixingPlayed: the callback that is invoked when the playback of the background music starts.
    aliWebrtc.on("onAudioMixingPlayed", () => {
        console.log("The playback of the background music starts or is resumed.")
    })
  • onAudioMixingFinished: the callback that is invoked when the playback of the background music stops.
    aliWebrtc.on("onAudioMixingFinished", () => {
        console.log("The playback of the background music stops.")
    })
  • onPublishChange: the callback that is invoked when local streams are automatically ingested.
    aliWebrtc.on('onPublishChange', (streamList) => {
      // The streams that are automatically ingested. The value of the streamList parameter is an array.
      console.log(streamList);
    });
    Note After you enable automatic stream ingest, this callback is invoked every time the ingested streams change.
  • onSubscribeChange: the callback that is invoked when a remote stream is automatically subscribed.
    aliWebrtc.on('onPublishChange', (publisher) => {
        // The ID of the user whose stream is automatically subscribed.
        console.log(publisher.userId);
        // The display name of the user.
        console.log(publisher.displayName);
        // The content of the remote stream that is automatically subscribed. The value of the streamConfigs parameter is an array.
        console.log(publisher.streamConfigs);
    });
  • onShareSystemAudioStart: the callback that is invoked when the system audio is shared.
    aliWebrtc.on('onShareSystemAudioStart', () => {
      console.log ('The system audio is shared.');
    });
    Note Only the local user receives this notification. Remote users do not receive this notification.