This topic describes the callback and listener interfaces of the Web SDK.

Notice As of February 2020, Alibaba Cloud RTC will gradually stop supporting and maintaining the previous SDK versions. Use SDK 1.9.1 or later. For more information, see SDK download.

Contents

API Description Supported earliest version
onPublisher Release stream callback. 1.2
onUnPublisher Cancel the release stream callback. 1.2
onMediaStream The callback for successful subscription to the stream. This callback is not required for 1.10 or later versions. 1.2
onJoin Add channel callback. 1.2
onLeave Leave the channel callback. 1.2
onError Error exception callback. 1.2
onBye Callback when the server is kicked out or the channel is closed. 1.11
onUpdateRole The callback for role switching. 1.12
onNotify Callback for user status change. 1.12.1
onAudioLevel Audio energy value callback. 1.13.0
onMedia The audio and video data callback for stream ingest and subscription. 1.14.0
onMediaStreamUpdate The callback is returned for the subscription stream. 1.15.4
onNetworkQuality The network quality callback. 1.16.0
onAudioMixingPlayed The accompaniment begins to play the callback. 1.16.0
onAudioMixingFinished Accompaniment Play End Callback. 1.16.0
onPublishChange Local publishing stream callback (automatic streaming). 1.17.0
onSubscribeChange Automatic subscription callback. 1.17.0
onShareSystemAudioStart Shared sound notification callback. 1.17.0

Details

  • onPublisher: the callback for publishing streams. This callback is triggered when someone else in the channel publishes a local stream.
    aliWebrtc.on('onPublisher',(publisher) =>{
        // The user ID of the remote publisher.
        console.log(publisher.userId);
        // The name of the remote release.
        console.log(publisher.displayName);
        // The content of the remote stream. streamConfigs is an array.
        console.log(publisher.streamConfigs);
      });
  • onUnPublisher: the callback for canceling the stream release. This callback is triggered when someone else in the channel unpublishes the local stream.
    aliWebrtc.on('onUnPublisher',(publisher) =>{
        // The user ID of the remote publisher.
        console.log(publisher.userId);
        // The name of the remote release.
        console.log(publisher.displayName);
    });
  • onMediaStream: the callback for the success of the subscription stream. Triggered when the subscription to the remote stream is successful. A Stream object of the remote stream is returned and played through H5 Video or Audio.
    Note 1.10 and later versions do not support this callback.
    aliWebrtc.on('onMediaStream',(subscriber, stream) =>{
         var video = document.getElementByTag('video');
         aliWebrtc.setDisplayRemoteVideo(
            subscribe, // Parameters returned in onMediaStream
            video, // The video element used to display the stream object in html
            stream // Parameters returned in onMediaStream
         )
     });
  • onJoin: Add a channel callback. If another user joins the channel, the onJoin callback is triggered.
    aliWebrtc.on('onJoin',(data) =>{
       console.log(data.userId);
       console.log(data.displayName + "Add channel");
     });
  • onLeave: leave the channel callback. When other users leave the channel, the onLeave callback is triggered.
    aliWebrtc.on('onLeave',(data) =>{
       console.log(data.userId);
       console.log(data.displayName + "Leave the channel");
     });
  • onError: the callback for errors and exceptions. If an error occurs, the onError callback is fired.
    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: callback when the server is kicked out or the channel is closed.
    aliWebrtc.on('onBye',(message) =>{ 
    // 1: kicked out by the server.
    // 2: The channel is closed.
    // 3: The same ID is logged on to another end and kicked out by the server.
    console.log(message.code); 
    });
  • onUpdateRole: the callback for role switching.
    aliWebrtc.on("onUpdateRole",(data)=>{
        console.log(data);
    })
    Note In interactive mode, the switchover is successful only when the setClientRole operation is called to return true and the onUpdateRole callback is received to obtain the correct new value.
  • onNotify: the callback for user status changes.
    aliWebrtc.on("onNotify",(data)=>{
        console.log(data);
    })
    Note
    • The onNotify callback is received when the ingest status of a local or remote user changes (call muteLocalMic and muteLocalCamera).
    • The data returned by this callback is the same as the result returned by calling the getUserList operation.
  • onAudioLevel: the audio energy value callback.
    Note You need to set the callback after enableAudioVolumeIndicator, which is returned once per second (returns the maximum audio energy per second).
    aliWebrtc.on("onAudioLevel", (data) => {
        console.log(data)
    })
    Response:
    • If you push the audio stream, the value of userId in the array is the string 0, which is your own audio information.
    • If you subscribe to another user's audio stream, this array contains audio information about the subscribed user.
    • The information of the specific array items is as follows:
      Response parameters Type Description
      userId String The userId of the subscriber. The userId of the subscriber is 0.
      displayName String The username that is used to log on to the SAP HANA database.
      level Number The audio energy value. Valid values: 0 to 100.
      buffer Array The data of the audio PCM per second.
  • onMedia: the callback of audio and video data for stream ingest and subscription.
    aliWebrtc.on("onMedia", (data) => {
        console.log(data)
    })
    Note
    • This callback returns information once per second.
    • Ingest data has only bytesSentPerSecond and subscribed data has only bytesReceivedPerSecond.
    Response parameters Type Description
    userId String The userId of the subscriber. The userId of the subscriber is 0.
    rtt Number The network latency. Unit: milliseconds. Indicates the network round trip time between the SDK and the Alibaba Cloud server. The smaller the value, the better. Generally, a delay of less than 50 ms is relatively ideal, while a delay of more than 100 ms will cause a large call delay.
    audio Object Audio stream
    level Number

    The sound energy value. Valid values: 0 to 1.

    camera Object Camera Stream
    width Number The width of the remote videos.
    height Number The height of the remote videos.
    fps Number The frame rate of the transcoded stream.
    screen Object Screen Flow
    width Number The width of the remote videos.
    height Number The height of the remote videos.
    fps Number The frame rate of the transcoded stream.
    bytesSentPerSecond Number The number of bytes sent per second (used only by streaming).
    bytesReceivedPerSecond Number The number of bytes received per second (only for subscription use).
  • onMediaStreamUpdate: A callback is returned for the subscription stream.
    aliWebrtc.on("onMediaStreamUpdate", (data) => {
      console.log(data)
    })
    Response parameters Type Description
    userId String The ID of the subscriber.
    mediaStream MediaStream The remote flow.
    Note Use the getTrack() method of mediaStream to obtain the track for custom playback. track.id is used to distinguish the track type, as shown in the following table.
    track.id Description
    sophon_video_camera_large Camera stream.
    sophon_video_camera_small Camera small stream.
    sophon_video_screen_share Screen sharing stream.
    sophon_audio The audio stream.
  • onNetworkQuality: network quality callback.
    aliWebrtc.on("onNetworkQuality", (data) => {
        console.log(data)
    })
    Response parameters Type Description
    uplinkNetworkQuality Number Upstream network quality. The following table describes the network quality details.
    downlinkNetworkQuality Number The downstream network quality. The following table describes the network quality details.
    Network quality Value Description
    NETWORK_QUALITY_UNKNOWN 0 The network quality is unknown, that is, there is no ingestion or no subscription.
    NETWORK_QUALITY_EXCELLENT 1 The network is excellent.
    NETWORK_QUALITY_GOOD 2 The network is good, the user's subjective feeling is similar to excellent, but the code rate may be slightly lower than excellent.
    NETWORK_QUALITY_POOR 3 The user's subjective feelings are flawed but do not affect communication.
    NETWORK_QUALITY_BAD 4 Can barely communicate but not smoothly.
    NETWORK_QUALITY_VERYBAD 5 The quality of the network is very poor and basically unable to communicate.
    NETWORK_QUALITY_DISCONNECT 6 The network is disconnected.
  • onAudioMixingPlayed: The accompaniment starts to play the callback.
    aliWebrtc.on("onAudioMixingPlayed", () => {
        console.log("The accompaniment starts or resumes playback")
    })
  • onAudioMixingFinished: Callback for the end of accompaniment playback.
    aliWebrtc.on("onAudioMixingFinished", () => {
        console.log("End of accompaniment playback")
    })
  • onPublishChange: local publishing stream callback (automatic streaming).
    aliWebrtc.on('onPublishChange', (streamList) => {
      // The specific contents of streamlist are automatically pushed as follows:
      console.log(streamList);
    });
    Note After automatic streaming is enabled, it will be triggered every time the published stream content changes.
  • onSubscribeChange: automatic subscription callback.
    aliWebrtc.on('onPublishChange', (publisher) => {
        // User ID of automatic subscription
        console.log(publisher.userId);
        // User name for automatic subscription
        console.log(publisher.displayName);
        // Automatically subscribed remote stream content. Streamconfigures is an array
        console.log(publisher.streamConfigs);
    });
  • onShareSystemAudioStart: shared sound notification callback.
    aliWebrtc.on('onShareSystemAudioStart', () => {
      console.log('You have shared audio from the system');
    });
    Note Only the local will receive this notification, and the remote will not receive this notification.