O Web RTS SDK oferece APIs para recepção de stream em tempo real, ingestão de stream e tratamento de eventos.
Conteúdo
|
API |
Descrição |
|
Cria uma instância de cliente RTS. |
|
|
Verifica se o ambiente de recepção de stream é compatível. |
|
|
Verifica se o ambiente de ingestão de stream é compatível. |
|
|
Inicia a recepção de um stream RTS. |
|
|
Interrompe a reprodução RTS. |
|
|
Silencia o áudio. |
|
|
Obtém um stream de câmera local, um stream de compartilhamento de tela local ou um stream personalizado. |
|
|
Inicia a ingestão de stream. |
|
|
Interrompe a ingestão de stream. |
|
|
Registra um listener de evento. |
|
|
Remove um listener de evento. |
|
|
Registra um listener de evento único. |
Detalhes
-
createClient: Cria uma instância de cliente RTS.
var aliRts = AliRTS.createClient(); -
isSupport: Verifica se o ambiente de recepção de stream é compatível.
/** * isSupport checks whether the environment is available. * @param {Object} supportInfo Check information. * @param {boolean} supportInfo.isReceiveVideo Specifies whether to pull a video stream. * @return {Promise} */ aliRts.isSupport({isReceiveVideo: true}).then(re=> { // Available }).catch(err=> { // Unavailable console.log(`not support errorCode: ${err.errorCode}`); console.log(`not support message: ${err.message}`); }) -
checkPublishSupport: Verifica se o ambiente de ingestão de stream é compatível.
/** * checkPublishSupport checks whether the stream ingest environment is available. * @return {Promise} */ aliRts.checkPublishSupport().then(re => { console.log('support info',re); // re.isAudioMixSupported: boolean; Specifies whether local audio stream mixing is supported. // re.isH264EncodeSupported: boolean; Specifies whether H.264 encoding is supported. // re.isMediaDevicesSupported: boolean; Specifies whether access to cameras, microphones, and speakers is supported. // re.isScreenCaptureSupported: boolean; Specifies whether screen sharing is supported. // re.isWebRTCSupported: boolean; Specifies whether WebRTC is supported. // re.cameraList: MediaDeviceInfo[]; The list of video input devices. // re.micList: MediaDeviceInfo[]; The list of audio input devices. // re.speakerList: MediaDeviceInfo[]; The list of audio output devices. }).catch(err=> { console.log(err); }) -
subscribe: Inicia a recepção de um stream RTS.
/** * The API to start pulling an RTS stream. * @param {string} pullStreamUrl The stream pulling URL. You can add @subaudio=no or @subvideo=no to the end of the URL to unsubscribe from the audio or video stream. * @param {Object} [config] (Optional) Custom configurations. * @param {string} [config.signalUrl] (Optional) The signaling URL. * @param {number} [config.retryTimes] (Optional) The maximum number of reconnection attempts. Default value: 5. * @param {number} [config.retryInterval] (Optional) The reconnection interval. Unit: ms. Default value: 2000. * @return {Promise} */ aliRts.subscribe(pullStreamUrl).then((remoteStream) => { // mediaElement is an audio or video media tag. remoteStream.play(mediaElement); // Calling remoteStream.play attaches the media stream to the media tag and tries to enable autoplay. // If you do not want to enable autoplay, you can pass {autoplay:false} as the second parameter. This feature is supported from version 2.2.4. // remoteStream.play(mediaElement, {autoplay:false}); }).catch((err) => { // Subscription failed. })ImportanteAo decodificar áudio e vídeo para RTS (RTS), B-frames não são suportados para vídeo, o que pode causar travamentos na tela. A codificação AAC não é suportada para áudio, podendo resultar em ruído. Se necessário, realize a transcodificação RTS para remover essas limitações. Para mais informações, consulte Transcodificação RTS.
Se você importar o Web RTS SDK em um projeto uni-app, o método
remoteStream.play()exige um HTMLVideoElement real. Como o uni-app encapsula a tag <video>, consulte o método no demo para obter o HTMLVideoElement real. Por exemplo, em pages/index/index.vue, useremoteStream.play(this.$refs.myVideo.$refs.video).O remoteStream retornado pelo método subscribe contém dados brutos de áudio e vídeo, acessíveis por meio do WebRTC MediaStream.
-
unsubscribe: Interrompe a reprodução RTS.
aliRts.unsubscribe(); -
muted: Silencia o áudio.
remoteStream.muted = true; -
createStream
-
Obtém um stream de câmera local.
/** * Gets the local stream localStream. * @param {Object} config Configurations. * @param {boolean} config.audio Specifies whether to use an audio device. * @param {boolean} config.video Specifies whether to use a video device. * @param {boolean} config.skipProfile Specifies whether to skip the profile. We recommend that you set this parameter to true when the camera shows a black screen. * @returns {Promise} */ AliRTS.createStream({ audio: true, video: true, }).then((localStream) => { // Previews the ingested stream. mediaElement is an audio or video media tag. localStream.play(mediaElement); }).catch((err) => { // Failed to create the local stream. }) -
Obtém um stream de compartilhamento de tela local.
/** * Shares only the screen. */ AliRTS.createStream({ screen: true }); /** * Shares the screen and captures the screen audio. Chrome on macOS supports capturing tab audio. Chrome on Windows supports capturing tab and system audio. */ AliRTS.createStream({ screen: { audio: true } }); /** * Shares the screen, captures the screen audio, and captures the microphone audio. */ AliRTS.createStream({ screen: { audio: true }, audio: true }); /** * Custom capture parameters. * - Disables echo cancellation for audio. * - Chrome prioritizes the current tab. * The preceding code provides an example. You can pass any parameter that complies with getDisplayMedia. The actual effect depends on whether the browser supports the parameter. */ AliRTS.createStream({ screen: { audio: { echoCancellation: false }, preferCurrentTab: true } }); -
Obtém um stream personalizado.
/** * Gets the local stream localStream. * @param {Object} config Configurations. * @param {boolean} config.custom Specifies whether to pass a custom stream. * @param {boolean} config.mediaStream A valid custom stream. * @returns {Promise} */ AliRTS.createStream({ // Custom stream custom: true, mediaStream: myStream // Pass a valid MediaStream (https://developer.mozilla.org/en-US/docs/Web/API/MediaStream). }).then((localStream) => { // Previews the ingested stream. mediaElement is an audio or video media tag. localStream.play(mediaElement); }).catch((err) => { // Failed to create the local stream. })
-
-
publish: Inicia a ingestão de stream.
/** * Starts stream ingest. * @param {string} pushUrl The ingest URL. * @param {Object} localStream The local stream created by createStream. * @param {Object} [config] (Optional) Custom configurations. * @param {string} [config.signalUrl] (Optional) The signaling URL. * @param {number} [config.retryTimes] (Optional) The maximum number of reconnection attempts. Default value: 5. * @param {number} [config.retryInterval] (Optional) The reconnection interval. Unit: ms. Default value: 2000. * @return {Promise} */ aliRts.publish(pushUrl, localStream).then(() => { // Stream ingest successful. }).catch((err) => { // Stream ingest failed. }) -
unpublish: Interrompe a ingestão de stream.
aliRts.unpublish(); -
on: Registra um listener de evento.
/* * When error code 10201 is returned in onError, the audio on the web page is muted. * The user must manually trigger an event on the web page to unmute the audio. This requires user interaction and cannot be controlled by code. * Call remoteStream.muted = false to unmute. */ aliRts.on("onError", (err)=> { console.log(`errorCode: ${err.errorCode}`); console.log(`message: ${err.message}`); }) aliRts.on('reconnect', function(evt) { console.log('reconnect', evt); // Listens for the reconnection event. evt is the cause of the reconnection. }) const PLAY_EVENT = { CANPLAY: "canplay", // Playback is ready. WAITING: "waiting", // Stuttering occurs. PLAYING: "playing", // Playback resumes after stuttering. MEDIA: "media", // Reports real-time media status every second. } aliRts.on('onPlayEvent', function(evt) { /* evt data structure: { event: string, // PLAY_EVENT data: any, // Data } */ if (evt.event === PLAY_EVENT.CANPLAY) { console.log("Playback is ready"); } else if (evt.event === PLAY_EVENT.WAITING) { console.log("Stuttering occurs"); } else if (evt.event === PLAY_EVENT.PLAYING) { console.log("Playback resumes after stuttering"); } else if (evt.event === PLAY_EVENT.MEDIA) { console.log("Real-time media data per second: ", evt.data); /* evt.data data structure: { url: string, // The playback URL aMsid: stirng, // The audio ID. Default value: 'rts audio'. audio: { // (Not supported by some browsers) bytesReceivedPerSecond: number, // The audio bitrate lossRate: number, // The audio packet loss rate rtt: number, // The RTT for both audio and video }, vMsid: string, // The video ID. Default value: 'rts video'. video: { // (Not supported by some browsers) bytesReceivedPerSecond: number, // The video bitrate framesDecodedPerSecond: number, // The decoding frame rate fps: number, // The rendering frame rate height: number, // The resolution height width: number, // The resolution width lossRate: number, // The video packet loss rate rtt: number, // The RTT for both audio and video }, networkQuality: number, // The network quality score } // The network quality score (networkQuality) can be one of the following values: // 0: Unknown, 1: Excellent, 2: Good, 3: Fair, 4: Poor, 5: Very poor, 6: No network */ } }); -
off: Remove um listener de evento.
function handle() {}; aliRts.on('onPlayEvent', handle); // Cancel the listener. aliRts.off('onPlayEvent', handle); -
once: Registra um listener de evento único.
aliRts.once('onPlayEvent', handle);