This topic describes the methods provided by the co-streaming SDK for Web.
Basic classes
Class | Description | ||||||
AlivcLivePusher | The class for stream ingest. Note You can access the class by using window.AlivcLivePush.AlivcLivePusher. | ||||||
AlivcLivePlayer | The class for stream pulling. Note You can access the class by using window.AlivcLivePush.AlivcLivePlayer. |
AlivcLivePusher - provides methods related to stream ingest
List of methods
Method | Description | ||||||
Queries cameras. This is a static method. | |||||||
Queries microphones. This is a static method. | |||||||
Queries playback devices. This is a static method. | |||||||
Checks whether the requirements for WebRTC are met. This is a static method. | |||||||
Checks whether screen sharing is supported. This is a static method. | |||||||
Initializes stream ingest parameters. | |||||||
Releases the stream ingest instance. | |||||||
Starts preview. | |||||||
Stops preview. | |||||||
Starts stream ingest. | |||||||
Restarts stream ingest. | |||||||
Reconnects and continues stream ingest. | |||||||
Stops stream ingest. | |||||||
Turns on the microphone or switches to another microphone. | |||||||
Turns off the microphone. | |||||||
Queries the device ID of the microphone. | |||||||
Turns on the camera or switches to another camera. | |||||||
Turns off the camera. | |||||||
Queries the device ID of the camera. | |||||||
Starts screen sharing. | |||||||
Stops screen sharing. | |||||||
Starts ingest of the custom secondary stream. | |||||||
Stops ingest of the custom secondary stream. | |||||||
Mutes or unmutes the stream. (Stream ingest is not stopped.) | |||||||
Displays or stops displaying the stream. (Stream ingest is not stopped.) | |||||||
Queries the stream ingest statistics that was last reported. | |||||||
Queries the ingest URL. | |||||||
Queries the channel ID. | |||||||
Queries the user ID. | |||||||
Updates the layout for stream relay and mixing. | |||||||
Queries the resolution. | |||||||
Updates the resolution. | |||||||
Queries the frame rate. | |||||||
Updates the frame rate. | |||||||
Queries the MediaStream information of the current stream. | |||||||
Queries the trace ID. |
Details
getCameras: queries cameras. This is a static method.
/** * Query cameras. * @return {Promise<MediaDeviceInfo[]>} */ const cameras = await AlivcLivePusher.getCameras();
getMicrophones: queries microphones. This is a static method.
/** * Query microphones. * @return {Promise<MediaDeviceInfo[]>} */ const microphones = await AlivcLivePusher.getMicrophones();
getPlayoutDevices: queries playback devices. This is a static method.
/** * Query playback devices. * @return {Promise<MediaDeviceInfo[]>} */ const playoutDevices = await AlivcLivePusher.getPlayoutDevices();
checkSystemRequirements: checks whether the requirements for WebRTC are met. This is a static method.
/** * Check whether the requirements for WebRTC are met. * @param {('sendonly' | 'recvonly' | 'sendrecv')} [direction] * @return {Promise<CheckResult>} */ const checkResult = await AlivcLivePusher.checkSystemRequirements(); // checkResult.support: boolean; Indicates whether the requirements are met. // checkResult.detail.isBrowserSupported: boolean; Indicates whether the browser is supported. // checkResult.detail.isH264DecodeSupported: boolean; Indicates whether H.264 decoding is supported. // checkResult.detail.isH264EncodeSupported: boolean; Indicates whether H.264 encoding is supported. // checkResult.detail.isWebRTCSupported: boolean; Indicates whether WebRTC is supported.
checkScreenShareSupported: checks whether screen sharing is supported. This is a static method.
/** * Check whether screen sharing is supported. * @returns {boolean} */ const isScreenShareSupported = AlivcLivePusher.checkScreenShareSupported();
init: initializes stream ingest parameters.
/** * Initialize the RTC engine. * @param config Configure parameters. All the parameters are optional. * config.resolution: AlivcResolutionEnum; The resolution. * config.fps: AlivcFpsEnum; The frame rate. Unit: frames per second (FPS). * config.logLevel: LogLevel; The log level. Default value: ERROR. * config.connectRetryCount: number; The maximum number of reconnection attempts. * config.audio: boolean; Specifies whether to enable audio. * config.audioId: string; The ID of the default audio device. * config.video: boolean; Specifies whether to turn on the camera. * config.cameraId: string; The device ID of the default camera. * config.screen: boolean; Specifies whether to enable screen sharing. */ const pusher = new AlivcLivePush.AlivcLivePusher(); pusher.init({ resolution: AlivcLivePush.AlivcResolutionEnum.RESOLUTION_720P });
destroy: releases the stream ingest instance.
// Release the stream ingest instance. You cannot use the instance after you release it. pusher.destroy()
startPreview: starts preview.
/** * Start video preview. * @param {string | HTMLVideoElement} elementOrId The node where the video resides or the ID of the video. * @param {boolean} secondary Specifies whether to preview the secondary stream. This parameter is optional. By default, only the primary stream is previewed. * @return {Promise<MediaStream>} */ const stream = pusher.startPreview(elementOrId);
stopPreview: stops preview.
/** * Stop video preview. * @param {string | HTMLVideoElement} elementOrId The node where the video resides or the ID of the video. This parameter is optional. If you do not specify this parameter, previewing for all videos is stopped. */ pusher.stopPreview(elementOrId);
startPush: starts stream ingest.
/** * Start stream ingest. * @param {string} url The ingest URL. Example: artc://live.aliyun.com/push/.... * @return {Promise} */ await pusher.startPush(url);
ImportantYou must specify an ingest URL that is used for co-streaming. For information about how to generate URLs for co-streaming, see Co-streaming URL generator.
restartPush: uses the current ingest URL to restart stream ingest.
/** * Use the current ingest URL to restart stream ingest. * @return {Promise} */ await pusher.restartPush();
reconnectPush: reconnects and continues stream ingest.
/** * Reconnect and continue stream ingest. * @param {string} url The new ingest URL. Example: artc://live.aliyun.com/push/.... * @return {Promise} */ await pusher.reconnectPush(url);
stopPush: stops stream ingest.
/** * Stop stream ingest. * @return {Promise} */ await pusher.stopPush(url);
startMicrophone: turns on the microphone or switches to another microphone.
/** * Turn on the microphone or switch to another microphone. * @param {string} deviceId The device ID of the microphone. This parameter is optional. * @return {Promise} */ await pusher.startMicrophone(deviceId);
stopMicrophone: turns off the microphone.
/** * Turn off the microphone. * @return {Promise} */ await pusher.stopMicrophone();
getCurrentMicDeviceId: queries the device ID of the microphone.
/** * Query the device ID of the microphone. * @return {string | undefined} The device ID of the microphone. */ const micId = pusher.getCurrentMicDeviceId();
startCamera: turns on the camera or switches to another camera.
/** * Turn on the camera or switch to another camera. * @param {string} deviceId The device ID of the camera. This parameter is optional. * @return {Promise} */ await pusher.startCamera(deviceId);
stopCamera: turns off the camera.
/** * Turn off the camera. * @return {Promise} */ await pusher.stopCamera();
getCurrentCameraDeviceId: queries the device ID of the camera.
/** * Query the device ID of the camera. * @return {string | undefined} The device ID of the camera. */ const cameraId = pusher.getCurrentCameraDeviceId();
startScreenShare: starts screen sharing.
/** * Start screen sharing. * @return {Promise} */ await pusher.startScreenShare();
stopScreenShare: stops screen sharing.
/** * Stop screen sharing. * @return {Promise} */ await pusher.stopScreenShare();
startCustomStream: starts ingest of the custom secondary stream.
/** * Start ingest of the custom secondary stream. * @param mediaStream The custom secondary stream. * @return {Promise<MediaStream>} */ await pusher.startCustomStream(mediaStream);
stopCustomStream: stops ingest of the custom secondary stream.
/** * Stop ingest of the custom secondary stream. * @return {Promise<void>} */ await pusher.stopCustomStream();
mute: mutes or unmutes the stream. (Stream ingest is not stopped.)
/** * Mute or unmute the stream. * @param {boolean} Specifies whether to mute the stream. * @returns {boolean} The mute or unmute result. */ pusher.mute(true);
muteVideo: displays or stops displaying the stream. (Stream ingest is not stopped.)
/** * Display or stop displaying the stream. * @param mute Specifies whether to stop sending video data. Valid values: true and false. * @returns Indicates whether the operation is successful. */ pusher.muteVideo(true);
getLivePushStatsInfo: queries the stream ingest statistics that was last reported.
/** * Query the stream ingest statistics that was last reported. * @return {StatsInfo} The stream ingest statistics. */ const statsInfo = pusher.getLivePushStatsInfo();
getPushUrl: queries the ingest URL.
/** * Query the ingest URL. * @return {string | undefined} The current ingest URL. */ const url = pusher.getPushUrl();
getChannelId: queries the channel ID.
/** * Query the channel ID. * @return {string | undefined} The current channel ID. */ const channelId = pusher.getChannelId();
getUserId: queries the user ID.
/** * Query the user ID. * @return {string | undefined} The current user ID. */ const userId = pusher.getUserId();
setLiveMixTranscodingConfig: updates the layout for stream relay and mixing.
/** * Update the layout for stream relay and mixing. * @param {AlivcLiveTranscodingConfig} config This parameter is optional. If you do not specify this parameter, stream relay and mixing are stopped. For more information, check the details about the AlivcLiveTranscodingConfig class. * @return {Promise} The result of the operation. */ const response = await pusher.setLiveMixTranscodingConfig(config);
getResolution: queries the resolution.
/** * Query the resolution. * @return {AlivcResolutionEnum | undefined} The resolution. */ const resolution = pusher.getResolution();
changeResolution: updates the resolution.
/** * Update the resolution. * @param resolutionEnum The resolution that you want to use. If you set this parameter to Custom, specify a custom resolution. * @param width The width, which is required when you specify a custom resolution. * @param height The height, which is required when you specify a custom resolution. * @param bitrate The maximum bitrate. * @return {Promise} */ await pusher.changeResolution(AlivcResolutionEnum.RESOLUTION_720P);
getFps: queries the frame rate.
/** * Query the frame rate. * @return {AlivcFpsEnum | undefined} The current frame rate. */ const fps = pusher.getFps();
changeFps: updates the frame rate.
/** * Update the frame rate. * @param {AlivcFpsEnum} fps The frame rate that you want to use. * @return {Promise} */ await pusher.changeFps(AlivcFpsEnum.FPS_30);
getPublishMediaStream: queries the MediaStream information of the current stream.
/** * Query the MediaStream information of the current stream. * @return {MediaStream | undefined} The MediaStream information of the stream. */ const mediaStream = pusher.getPublishMediaStream();
getLiveTraceId: queries the trace ID.
/** * Query the trace ID. * @return {string} TraceId */ pusher.getLiveTraceId();
AlivcLiveTranscodingConfig
List of parameters
Parameter | Description | ||||||
width | A numeric value that indicates the width of the relayed stream. | ||||||
height | A numeric value that indicates the height of the relayed stream. | ||||||
backgroundColor | A hexadecimal value that indicates the background color. Example: 0x000000. | ||||||
cropMode | AlivcLiveTranscodingCropModeEnum
| ||||||
mixStreams | AlivcLiveMixStream[]: The details about the layout of each stream that is included in the mixed stream. |
AlivcLiveMixStream
List of parameters
Parameter | Description | ||||||
userId | A string that indicates the ID of the user who performs stream mixing. | ||||||
x | A numeric value that indicates the number of pixels along the x-axis from where stream mixing starts. | ||||||
y | A numeric value that indicates the number of pixels along the y-axis from where stream mixing starts. | ||||||
width | A numeric value that indicates the width of the mixed stream. | ||||||
height | A numeric value that indicates the height of the mixed stream. | ||||||
zOrder | A numeric value that indicates the hierarchical level. The greater the value, the higher the level. |
AlivcLivePusher.info
Event | Description | ||||||
bye | Indicates that a viewer leaves the room. The viewer may have been displaced by other viewers or kicked out by the streamer. | ||||||
pushstatistics | The callback for data statistics about ingested streams. This callback is returned every 2 seconds. |
Sample code:
pusher.info.on('bye', (_code, reason) => {
// console.log ('You have left the room. Reason: ${reason}');
});
pusher.info.on('pushstatistics', _stat => {
// console.log(_stat);
});
AlivcLivePusher.error
Event | Description | ||||||
system | Indicates a system error. | ||||||
sdk | Indicates an SDK internal error. |
Sample code:
pusher.error.on('system', error => {
// console.log(error);
});
pusher.error.on('sdk', error => {
// console.log(error);
});
AlivcLivePusher.network
Event | Description | ||||||
connectionlost | Indicates disconnection. | ||||||
networkrecovery | Indicates network recovery. | ||||||
reconnectstart | Indicates that the reconnection starts. | ||||||
reconnectfail | Indicates that the reconnection fails. | ||||||
reconnectsucceed | Indicates that the reconnection is successful. |
Sample code:
pusher.network.on('connectionlost', () => {
// console.log ('A network exception occurs, which results in a disconnection.');
});
AlivcLivePlayer - provides methods related to stream pulling
List of methods
Method | Description | ||||||
Starts the playback of the audio and video streams. | |||||||
Plays the audio and video streams on another node. | |||||||
Stops the playback of the audio and video streams. | |||||||
Pauses the playback of the audio stream. | |||||||
Pauses the playback of the video stream. | |||||||
Resumes the playback of the audio stream. | |||||||
Resumes the playback of the video stream. | |||||||
Releases the stream pulling instance. |
Details
startPlay: starts the playback of the audio and video streams.
/** * Start the playback of the audio and video streams. * @param url The streaming URL. Example: artc://live.aliyun.com/play/... * @param elementOrId The label or ID of the media resource. * @param secondaryElementOrId The label or ID of the media resource of the secondary stream. * @return {Promise<AlivcLivePlayInfo>} You can use AlivcLivePlayInfo to listen to related events. */ const playInfo = await player.startPlay(url, elementOrId, secondaryElementOrId);
ImportantYou must specify a streaming URL that is used for co-streaming. For information about how to generate URLs for co-streaming, see Co-streaming URL generator.
Ordinary viewers can use ApsaraVideo Player SDK for Web to play the stream. For more information, see Quick access to a web player.
playAnotherElement: plays the audio and video streams on another node.
/** * Play the audio and video streams on another node. * @param elementOrId The label or ID of the media resource. * @param secondary Specifies whether to play the secondary stream. This parameter is optional. By default, only the primary stream is played. */ player.playAnotherElement(elementOrId);
stopPlay: stops the playback of the audio and video streams.
/** * Stop the playback of the audio and video streams. * @param elementOrId The label or ID of the media resource. This parameter is optional. If you do not specify this parameter, playback of all media resources is stopped. * @return {Promise} */ await player.stopPlay(elementOrId);
pauseAudioPlaying: pauses the playback of the audio stream.
/** * Pause the playback of the audio stream. */ player.pauseAudioPlaying();
pauseVideoPlaying: pauses the playback of the video stream.
/** * Pause the playback of the video stream. */ player.pauseVideoPlaying();
resumeAudioPlaying: resumes the playback of the audio stream.
/** * Resume the playback of the audio stream. */ player.resumeAudioPlaying();
resumeVideoPlaying: resumes the playback of the video stream.
/** * Resume the playback of the video stream. */ player.resumeVideoPlaying();
destroy: releases the stream pulling instance.
/** * Release the stream pulling instance. You cannot use the instance after you release it. */ player.destroy();
AlivcLivePlayInfo
Event | Description | ||||||
canplay | Indicates the canplay event. | ||||||
userleft | Indicates that the remote user leaves the room. | ||||||
statistics | Indicates the playback statistics. | ||||||
update | Updates the remote stream. |
Sample code:
playInfo.on('statistics', _stat => {
// console.log(_stat);
});
playInfo.on('userleft', () => {
// console.log ('The remote user leaves the room.');
});
playInfo.on('canplay', function () {
// console.log ('The remote stream can be played.');
});
playInfo.on('update', function (previousStatus) {
// console.log(previousStatus.mediaStream);
// console.log(previousStatus.secondaryMediaStream);
});