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
| Method | Description | Supported earliest version |
| onPublisher | The callback that is invoked when a remote stream is ingested. | V1.2 |
| onUnPublisher | The callback that is invoked when remote stream ingest is canceled. | V1.2 |
| onMediaStream | The 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 |
| onJoin | The callback that is invoked when a user joins a channel. | V1.2 |
| onLeave | The callback that is invoked when a user leaves a channel. | V1.2 |
| onError | The callback that is invoked when an error occurs. | V1.2 |
| onBye | The callback that is invoked when a user is forced to leave a channel. | V1.11 |
| onUpdateRole | The callback that is invoked when the role of a user is changed. | V1.12 |
| onNotify | The callback that is invoked when the stream ingest status of a user changes. | V1.12.1 |
| onAudioLevel | The callback that is invoked to return the maximum volume of the audio track. | V1.13.0 |
| onMedia | The callback that is invoked to return the information about ingested and subscribed streams. | V1.14.0 |
| onMediaStreamUpdate | The callback that is invoked to return subscribed streams. | V1.15.4 |
| onNetworkQuality | The callback that is invoked to return the network quality. | V1.16.0 |
| onAudioMixingPlayed | The callback that is invoked when the playback of the background music starts. | V1.16.0 |
| onAudioMixingFinished | The callback that is invoked when the playback of the background music stops. | V1.16.0 |
| onPublishChange | The callback that is invoked when local streams are automatically ingested. | V1.17.0 |
| onSubscribeChange | The 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.
Parameter Type Description userId String The ID of the user who ingests the stream for an audio track. The user ID for your audio track is 0. displayName String The display name of the user. level Number The volume of the audio track. Valid values: 0 to 100. buffer Array The 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.
Parameter Type Description userId String The ID of the user who ingests the stream. The user ID for your stream is 0. rtt Number The 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. audio Object The audio track. level Number The audio energy level. Valid values: 0 to 1.
camera Object The camera track. width Number The width of the video. height Number The height of the video. fps Number The frame rate of the video. screen Object The screen track. width Number The width of the video. height Number The height of the video. fps Number The frame rate of the video. bytesSentPerSecond Number The number of bytes sent per second. This parameter is valid only for ingested streams. bytesReceivedPerSecond Number The 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) })Parameter Type Description userId String The ID of the user who ingest the stream. mediaStream MediaStream The 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.id Description sophon_video_camera_large The major stream from the camera. sophon_video_camera_small The minor stream from the camera. sophon_video_screen_share The screen track. sophon_audio The audio track. - onNetworkQuality: the callback that is invoked to return the network quality.
aliWebrtc.on("onNetworkQuality", (data) => { console.log(data) })Parameter Type Description uplinkNetworkQuality Number The quality of the upstream network. The following table describes the details of the network quality. downlinkNetworkQuality Number The quality of the downstream network. The following table describes the details of the network quality. Network quality Value Description NETWORK_QUALITY_UNKNOWN 0 The network quality is unknown. This value is returned if no streams are ingested or subscribed. NETWORK_QUALITY_EXCELLENT 1 The network quality is excellent. NETWORK_QUALITY_GOOD 2 The 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_POOR 3 The network quality is merely acceptable. The video is not smooth and clear enough but the communication is not affected. NETWORK_QUALITY_BAD 4 The network quality is poor. Stuttering occurs but the communication is not interrupted. NETWORK_QUALITY_VERYBAD 5 The network quality is worse, and the communication is severely affected. NETWORK_QUALITY_DISCONNECT 6 The 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.
该文章对您有帮助吗?