CloudHub Docs
Download Documents

RTC


CloudHub JSSDK

The description and interface of Stream

Base

init

What it does: Initialized audio video object. This method initializes the locally created audio and video stream object.

init(options?:object, onCallback?: function): void

// example:
stream.init({}, function(errinfos) {
  if(errinfos){
    console.error(stream init errinfos:, errinfos);
    // handle err
  }
  console.log("local stream initialized");
  // publish the stream
  //……
});
nametypedescription
onCallbackfunctionCallback function, which takes an errinfos parameter with a type of array | undefined,If errinfos is undefined, init has not failed. If errinfos is array, init has failed.

Note:

[
  {
    type: "video_device",
    errflag: "NOT_ALLOWED_ERROR",
    message: "Permission denied"
  }, {
    type: "audio_deivce",
    errflag: "NOT_ALLOWED_ERROR",
    message: "Permission denied"
  }
]

or

[
  {
    type: "screen",
    errflag: "NOT_SUPPORT",
    message: "not support not plugin screen shareing"
  }
]

errflag The following values may appear in the field

Note:

on

What it does: Event binding. Currently supported incidents and events for events, please see “Stream Event documentation”

on(eventType: string, listener: function): void

// example:
stream.on('camera-access-allowed', function (evt) {
  // This callback notifies the App that the permission to use the local camera has been obtained.
  console.log(''camera-access-allowed'', evt);
})
nametypedescription
eventTypestringEvent name to be bound
listenerfunctionEvent listener to be bound

off

What it does: Cancel the event binding. This method is used to remove events bound by Stream.on().

off(eventType: string, listener?: function): void
nametypedescription
eventTypestringEvent name to be removed
listenerfunctionEvent listener to be removed.

offAll

What it does: Cancel all event bindings under the specified type.

offAll(eventType: string): void

// example:
stream.offAll("videoplayer-status-change");

Audio

hasAudio

What it does: Get audio flag. The method obtains the video track in the audio and video stream and can be used with replaceTrack.

hasAudio(): boolean

Returns: boolean

true: The audio and video stream object contains audio resources. false: The audio resource is not included in the audio and video stream object.

getAudioTrack

What it does: Get the audio track. This method gets the audio tracks in the audio and video streams and can be used with replace Track.

getAudioTrack(): MediaStreamTrack | void

Returns: MediaStreamTrack | void

If the audio and video stream contains an audio track, it will be returned as a MediaStreamTrack object.

playAudio

What it does: Play the audio stream. This method plays audio stream

playAudio(options?: object, onCallback?: function): void

// example:
stream.playAudio({}, function(StreamPlayError){
  console.error(playAudio StreamPlayError, StreamPlayError);
  // Playing failed, typically blocked for browser strategies. Guide users to trigger recovery playback with gestures.
});
nametypedescription
optionsobjectThe options for streaming playback are configured as follows:
1. muted?: boolean: Set whether the playing stream is muted, the default is false. Muted is usually used to circumvent the browser’s autoplay strategy. In general, when playing audio / video with sound, the browser will ask the line to be triggered by a gesture. If you do not want to trigger with a gesture, you can also choose to set muted to true, so that you can bypass the autoplay policy. See Auto-play strategy for handling browsers
2. volume?: number: Set the volume size of the playback, the value range is [0,100], default is 100。
onCallbackfunctionWhether the playback is successful or not. The callback parameter carries StreamPlayError.If the playback is successful, the parameter is null/undefined. If the playback fails, you can use StreamPlayError to understand the possible causes. StreamPlayError: See CloudHubRTC Data object document Description of StreamPlayError.

Note: Affected by browser policy, on Chrome 70+ and Safari browsers, this method must be triggered by user gestures, see Auto-play strategy for handling browsers

unplayAudio

What it does: Stop the audio stream. Call this method to stop playing “Stream.playAudio”.

unplayAudio(): void

resumeAudio

What it does: Resume audio stream playback.

This method is usually called when playback fails after calling Stream.playAudio. Usually the playback failure is caused by browser policy blocking. This method needs to be triggered by gestures. See “Auto-play strategy for handling browsers”

resumeAudio(): Promise<any>

Returns Promise

muteAudio

What it does: Disable audio tracks. This method disables the audio track. For a local stream, calling this method stops sending audio, and the remote end triggers a Client.on(“mute-audio”) callback. For remote streams, calling this method does not receive audio and stops playing audio.

muteAudio(): boolean

Note: For local streams, this method can only be used if audio is set to true when createStream.

Returns: boolean

true: Disabled audio track success. false: Disabled audio track fail, there may be no audio, unproduction, and audio tracks have been disabled.

unmuteAudio

What it does: Enable the audio track. This method enables audio tracks. The remote end triggers a Client.on(“unmute-audio”) callback when the audio track is enabled for the local stream.

unmuteAudio(): boolean

Note:

Returns boolean

setAudioOutput

What it does: Set the audio output. The method can set the audio output device of the subscription stream and switch the loudspeaker during the call. This method can be called before or after the subscription stream is played.

setAudioOutput(deviceId: string, onSuccess?: function, onFailure?: function): void
nametypedescription
deviceIdstringDevice ID, Can be passed through getDevices, The kind attribute of the device should be “audiooutput”
onSuccessfunctionSet a successful callback
onFailurefunctionSet a failed callback

Note: Mobile browsers do not support it.

setAudioVolume

What it does: Adjust the volume size. This method can adjust the volume of the subscription stream (the volume attribute of the corresponding node element).

setAudioVolume(volume: number): void
nametypedescription
volumenumberVolume, ranging from 0 (mute) to 100 (loudest)

getAudioVolume

What it does: Get the volume size. This method can obtain the volume of the subscription stream (the volume attribute of the corresponding node element).

getAudioVolume(): number

Returns number

Range from 0 (mute) to 100 (loudest)

isAudioPlaying

What it does: Returns whether the audio stream is currently played in the play state.

isAudioPlaying(): boolean

Returns boolean

True: the audio stream is being rendered or played. False: the audio stream is not rendered.

isPubedAudio

What it does: Whether the publisher of the stream releases audio. That is, the publisher returns false, if muteAudio otherwise returns true.

isPubedAudio(): boolean

Returns boolean

true: Released audio false: Do not release audio

Video

hasVideo

What it does: Get the video flag. The method obtains the video track in the audio and video stream and can be used with replaceTrack.

hasVideo(): boolean

Returns: boolean

true: The audio and video stream object contains video resources. false: Video resources are not included in the audio and video stream object.

playVideo

What it does: Play the video stream. This method plays video streams

playVideo(HTMLElementID: string, options?: object, onCallback?: function): void

// example:
stream.playVideo("remote_stream_id", {fit: "contain"}, function(StreamPlayError){
    console.log("playVideo StreamPlayError", StreamPlayError);
});
nametypedescription
HTMLElementIDstring节点 ID
optionsobjectThe options for streaming playback are configured as follows:
1. fit?: “cover” | “contain”: Set the display mode for video playback. You can choose “cover” and “contain”. The default is “cover”. The difference between cover mode and contain mode is as follows:
* Cover mode: priority is given to ensuring that the window is filled. The video size is scaled proportionally until the entire window is filled with video. If the length and width of the video is different from that of the display window, the video stream will be clipped according to the proportion of the display window or the image will be stretched to fill the window. You can refer to the cover option of object-fit in the CSS attribute.
* Contain mode: priority is given to ensuring that all video content is displayed. The video size is scaled proportionally until one side of the video window is aligned with the window frame. If the video size is not consistent with the display window size, on the premise of maintaining the aspect ratio, the video is scaled and filled with the window. There will be a black edge around the scaled video. You can refer to the contain option of object-fit in the CSS attribute.
2. mirror?: boolean: Used to indicate whether the video is displayed in a mirror way. The default is false.
3. loader?: boolean: Used to indicate whether the ‘loading’ style is displayed when the video data is not loaded. Default is true。
onCallbackfunctionWhether the playback is successful or not. The callback parameter carries StreamPlayError.
This parameter is null/undefined if the playback is successful.
If the playback fails, you can find out the possible reasons through StreamPlayError.
StreamPlayError: See “CloudHubRTC Data object document” Description of Stream Play Error

unplayVideo

What it does: Stop the video stream. Call this method to stop playing the video stream “Stream.playVideo”.

unplayVideo(): void

resumeVideo

What it does: Resume audio stream playback.

This method is generally called when the playback fails after calling Stream.playVideo.

resumeVideo(): Promise<any>

Returns Promise

muteVideo

What it does: Disable video tracks.

For a local stream, calling this method stops sending video, and the remote end triggers a Client.on(“mute-video”) callback.

For a remote stream, calling this method will not receive the video and will stop playing the video.

muteVideo(): boolean

Note: For locally created streams, set video to true when createStream to use this method.

Returns: boolean

true: Video track disabled successfully.

false: Failed to disable video track, possible reasons include no video, stream not initialized, video track has been disabled, and so on.

unmuteVideo

What it does: Enable video tracks.

This method enables video tracks. Client.on(“unmute-video”) callback will be triggered by the remote end when the video track is enabled for the local stream.

unmuteVideo(): boolean

Note:

Returns boolean

true: Video track enabled successfully.

false: Failed to enable video track. Possible reasons include no video, uninitialized stream, enabled video track, and so on.

setVideoProfile

What it does: Set video properties.

This method sets the video properties of the local stream.

Valid only for stream type video (that is, stream.getType() = “video”).

setVideoProfile(profile: object): object

// example:
stream.setVideoProfile({width: 320, height:240, fps:15});
nametypedescription
profileobjectVideo attributes, with the following attributes:
* width?:number Video width, default is the default video width.
* height?:number Video height, the default is the default video height.
* fps?:number The video frame rate, the default is the default video frame rate.

Note:

Returns object

Returns the actual set video properties, such as: {width: 320, height:240, fps:15}。

Video resolution reference form

Resolution (width × height)Frame rate (FPS)ChromeFirefoxSafariNotes
80 × 4510
80 × 4810
80 × 6010
176 × 14410
200 × 15010
120 × 12015
160 × 12015Mainstream
180 × 18015
240 × 18015
320 × 18015Mainstream
240 × 24015
320 × 24010
320 × 24015Mainstream
424 × 24015
360 × 36015
360 × 36030
480 × 36015
480 × 36030
640 × 36015Mainstream
640 × 36024
640 × 36030
480 × 48015
480 × 48030
640 × 48010
640 × 48015Mainstream
640 × 48030
848 × 48015
848 × 48030
960 × 72015
960 × 72030
1280 × 72015Mainstream
1280 × 72030
1920 × 108015
1920 × 108030Mainstream

For more information on the form, please see Video resolution form

Note:

getAttributes

What it does: Get a custom attribute

/**Get a custom attribute
  * @returns {
    mute_audio: string;
    sourceid: string;
    type: string
    videofps: number
    videoheight: number
    videowidth: number
  }
*/
stream.getAttributes();

getVideoProfile

What it does: Gets the video properties of the local stream.

Only valid for stream type video (ie: stream.getType() === “video”).

getVideoProfile(): object

// example:
stream.getVideoProfile();

Returns object

Set video attributes, and return the video attributes that have been successfully set, otherwise return the default video attributes, such as: {width: 320, height:240, fps:15}

getType

What it does: Get the type of the stream.

This method returns the type of the current stream.

getType(): string

Note: The current types are “video” 、“media” 、“screen”、“file”

Returns string

getSourceID

What it does: Gets the sourceID of the stream.

This method returns the sourceID of the current stream.

getSourceID(): string

Returns string

getAudioLevel

What it does: Gets the current volume.

This method gets the volume of the current moment. If you want to indicate a change in local or remote volume, we recommend that you use the setTimeout or setInterval method to obtain it in real time.

getAudioLevel(): number | void

// example:
setInterval(function() {
  var audioLevel = stream.getAudioLevel();
  // Use audioLevel to render the UI
}, 100)

Note: Safari 11/12.0 does not support.

Returns: number | void Get the volume value, range of value [0,100]。

getId

What it does: Get the audio and video stream ID.

The audio and video stream ID can be obtained by this method.

getId(): string

// example:
stream.getId();

Note: Stream ID is based on uid、type、sourceID Generate, stream ID format is: uid:type:sourceID

Returns: string

getStats

What it does: Get connecting data.

The method acquires the connection data of the audio and video stream.

getStats(onCallback: function): void

// example:
localStream.getStats((errinfo, stats) => {
  console.log("Local Stream stats:", stats, errinfo);
});
remoteStream.getStats((errinfo, stats) => {
  console.log("Remote Stream stats:", stats, errinfo);
});
nametypedescription
onCallbackfunctionThe callback contains the connection statistics of the audio / video stream. The callback parameters contain errinfo and stats. If errinfo is not undefined, it means that the data acquisition failed. Otherwise, errinfo is undefined, stats is LocalStreamStats/RemoteStreamStats。 If it is a published stream, the statistics are LocalStreamStats.If it is a subscription stream, the statistic is RemoteStreamStats. LocalStreamStats: See “CloudHubRTC Data object document” Description of Local Stream Stats. RemoteStreamStats: See “CloudHubRTC Data object document” Description of Remote Stream Stats

Note:

getVideoTrack

What it does: Get the video track.

The method obtains the video track in the audio and video stream and can be used with replaceTrack.

getVideoTrack(): MediaStreamTrack | void

Returns: MediaStreamTrack | void

If the audio and video stream contains a video track, it will be returned as a MediaStreamTrack object.

replaceTrack

What it does: Replace audio and video tracks.

This method can replace the audio and video track in the local audio and video stream.

After the local stream is released, you can use this method to switch cameras or microphones.

The new audio and video tracks can be obtained through DeviceManager’s getVideoTrack/getAudioTrack and other methods.

The replaced audio and video track will be forced to stop.

replaceTrack(mediaStreamTrack: MediaStreamTrack, onSuccess?: function, onFailure?: function): void

// example:
stream.replaceTrack(newVideoTrack);
nametypedescription
mediaStreamTrackMediaStreamTrackNew audio and video tracks. MediaStreamTrack: See “CloudHubRTC Data object document” Description of MediaStreamTrack
onSuccessfunctionMethod call successful callback
onFailurefunctionA callback that fails to call a method, and the failed parameters carry a format such as: {errflag: “NOT_ALLOWED_ERROR”, message: “Permission denied”}.
errflag The following values may appear in the field:
* “INVALID_TRACK”: The new incoming MediaStreamTrack object is not recognized. Please check the incoming object.
* “MEDIASTREAM_TRACK_NOT_FOUND”: No replacement was found, such as trying to replace the Track of a video on a pure audio stream.
* “NO_STREAM_FOUND”: Can’t find the local stream, you can’t replace it.
* “NOT_SUPPORT”: The browser does not support replacement.
* “UNDEFINED_ERROR”: Undefined error.

Note:

isVideoPlaying

What it does: Returns whether the video stream is currently playing in the play state.

Call this method to stop playing “video stream played by Stream.playVideo”.

isVideoPlaying(): boolean

Returns boolean

true: The video stream is being rendered or played.

false: The video stream is not rendered.

true: Audio track enabled successfully.

false: Failed to enable audio track, possible reasons include no audio, stream not initialized, audio track has been enabled, and so on.

isRemote

What it does: Whether it is a remote stream.

isRemote(): boolean

Returns boolean

true: Remote stream.

false: Local stream.

isPubedVideo

What it does: Whether the publisher of the stream publishes the video.

That is, the publisher returns false, if muteVideo otherwise returns true.

isPubedVideo(): boolean

Returns boolean

true: Released the video.

false: Do not release video

setMirror

setMirror(isMirror:boolean): void

What it does: Set whether or not used image.

nametypedescription
isMirrorbooleanWhether to use mirror images. True means to use images, and false means not to use them.

close

What it does: Close the audio and video stream.

This method closes the video stream or audio stream. Calling this method will cancel the access rights of the camera and microphone.

close(): void

User

getUserId

What it does: Gets the user id of the stream.

This method returns the user id of the current stream.

getUserId(): string

Returns string

设备

switchDevice

What it does: Switch input devices.

This method can switch the media input device of the local stream:

An audio input device, such as a microphone

A video input device, such as a camera

The flow has been released, and there is no need to re-discharge after switching.

The following cases cannot be used:

switchDevice(type: "audio" | "video", deviceId: string, onSuccess?: function, onFailure?: function): void
nametypedescription
typestringThe type of device, such as the following values:
* “audio”: Audio input device < br/>* “video”: Video input device
deviceIdstringThe ID of the device, Can be passed through getDevices
onSuccessfunctionCallback for successful switching.
onFailurefunctionSwitch the callback that failed, and the failed parameters are in a format such as: {errflag: "NOT_ALLOWED_ERROR", message: "Permission denied"}。 errflag The following values may occur:
* “NOT_SUPPORT”: The browser is not supported.
* “INVALID_ARG”: Invalid parameter.
* “NOT_ALLOWED_ERROR”: The user refuses to grant permission to the corresponding camera or microphone.
* “NOT_READABL_EERROR”: The camera or microphone is occupied.
* “ABORT_ERROR”: Device abort error.
* “NOT_FOUND_ERROR”: Did not find the device.
* “OVER_CONSTRAINED_ERROR”: The configured constraint cannot meet the requirements.
* “SECURITY_ERROR”: Security error, webpage use of device media is prohibited. Whether this mechanism is turned on or off depends on the preferences of individual users.
* “TYPE_ERROR”: Device constraints are set to false
* “REQ_TIMEOUT”: Request timeout.
* “UNDEFINED_ERROR”: Undefined error.
1. See Browser getUserMedia Interface error type
2. See “Constant declaration and supplementary notes” The error type of the browser’s getUserMedia interface failure.

Note: Mobile browsers do not support it.

switchMobileCamera

What it does: Switch the front and rear cameras on the mobile.

For streams that have been released, there is no need to resend the streams after switching.

This method cannot be used in the following situations:

switchMobileCamera(onSuccess?: function, onFailure?: function): void
nametypedescription
onSuccessfunctionCallback for successful switching.
onFailurefunctionSwitch the callback that failed, and the failed parameters are in a format such as: {errflag: "NOT_ALLOWED_ERROR", message: "Permission denied"}。 errflag The following values may occur:
* “NOT_SUPPORT”: The browser is not supported.
* “INVALID_ARG”: Invalid parameter.
* “NOT_ALLOWED_ERROR”: The user refuses to grant permission to the corresponding camera or microphone.
* “NOT_READABL_EERROR”: The camera or microphone is occupied.
* “ABORT_ERROR”: Device abort error.
* “NOT_FOUND_ERROR”: Did not find the device.
* “OVER_CONSTRAINED_ERROR”: The configured constraint cannot meet the requirements.
* “SECURITY_ERROR”: Security error, webpage use of device media is prohibited. Whether this mechanism is turned on or off depends on the preferences of individual users.
* “TYPE_ERROR”: Device constraints are set to false
* “REQ_TIMEOUT”: Request timeout.
* “UNDEFINED_ERROR”: Undefined error.
1. See Browser getUserMedia Interface error type
2. See “Constant declaration and supplementary notes” The error type of the browser’s getUserMedia interface failure.

Note: Non-mobile browsers do not support it.