Handle a Local Stream
The Web SDK provides the following methods to handle a local stream:
- publish(): To publish a local stream in the connected room.
- unpublish(): To disconnect a local stream from the room.
- close(): To close the stream.
- switchMediaDevice(): To switch both camera and microphone.
- switchCamera(): To switch the camera.
- switchMicrophone(): To switch the microphone.
- switchSpeaker(): To switch to a different speaker to play audio.
- muteAudio(): To mute the audio track of a local stream.
- unmuteAudio(): To unmute the audio track of a local stream.
- muteVideo(): To mute the video track of a local stream.
- unmuteVideo(): To unmute the video tarack of a local stream.
Publish a Local Stream
It is important to publish your media stream into the room for other connected users to see and communicate with you. You can continue publishing your stream until you exit, or unpublish and republish your media stream multiple times within the same session if required.
After initializing the stream, you need to publish it in the room so that other users in the room can see and hear the user on this stream.
The EnxRoom.publish()
method publishes a local stream in the connected room.
- Method:
EnxRoom.publish(LocalStream, PublishOpt, Callback)
- Parameters:
LocalStream
: String. The initiated stream object.PublishOpt
: String. Configurable options for publishing.
- Event Listeners:
stream-added
: Notification to everyone in a room when a new stream is published in the room.stream-failed
: Notification to the stream publisher when the stream publishing fails.
Sample Code
var PublishOpt = {"minVideoBW":"Number","maxVideoBW": "Number"};room.publish(localStream, PublishOpt, function(StreamId) {});// Event Listener: For all. New Stream availableroom.addEventListener("stream-added", function(event) {if (localStream.getID() === event.stream.getID()) {// Your stream published} else {// Someone else has published}});// Event Listener: For self. Publishing failedroom.addEventListener("stream-failed", function(event) {// Failed to publish});
Error Codes
Error Code | Description |
---|---|
1170 | The feature is not enabled because of room configuration or due to lack of the required license. |
Unpublish a Local Stream
The EnxRoom.unpublish()
method disconnects the local stream from the room. Unpublishing a stream does not release the device access, so subsequent republishing does not require device access permission.
- Method:
EnxRoom.unpublish(localStream, Callback)
- Parameters:
localStream
: String. Local stream object to unpublish.
- Event Listeners:
stream-removed
: Notification to everyone in a room when a stream is removed from the room.
Sample Code
room.unpublish(localStream, function(result, error) {if (result === undefined) {// Failed} else {// Unpublished}});// Event Listener: For all. Stream removedroom.addEventListener("stream-removed", function(event) {if (localStream.getID() === event.stream.getID()) {// Your stream removed} else {// Someone's stream is removed}});
Close a Local Stream
The EnxStream.close()
method allows you to disconnect the local stream from the room, similar to the unpublishing process. Closing a stream leads to the following:
- If the stream includes audio and video tracks, it stops capturing media streams from the camera and the microphone.
- If the stream is published into the room, the stream is unpublished.
- If the stream is being played in a player, it stops playing.
- Method:
EnxStream.close()
Sample Code
localStream.close();
Switch Camera, Microphone, and Speaker
You may need to switch to an alternate camera, microphone, or speaker while being in a session without any break in the communication. APIs helps you achieve this seamlessly.
The following APIs allow you to switch to alternate media devices after publishing a stream, thus facilitating a switch on the fly.
The method allows you to switch the camera and the microphone separately or simultaneously using the Device ID of the new devices.
To fetch the list of available devices and their IDs, use the EnxRtc.getDevices()
method.
Simultaneous Switching of Camera and Microphone Source of Published Streams
The EnxRtc.switchMediaDevice()
method allows you to switch both the camera and microphone, thus transferring the audio and video input to an alternate camera and microphone.
- Method:
EnxRtc.switchMediaDevice(LocalStream, micDeviceId, camDeviceId, Callback)
- Parameters:
LocalStream
: EnxStream. Published stream.micDeviceId
: String. New Microphone's Device ID.camDeviceId
: As String (New Camera's Device ID). As Object ({ 'facingMode': 'user/environment'
for Front/Rear Camera of Mobile Device)Callback
: Returns the updated stream with new devices. If the audio or video is muted, then it returns the following JSON:{ result: 0, msg: 'set but not active' }
.
Sample Code: (With Device ID)
EnxRtc.switchMediaDevice(localStream, micDeviceId, camDeviceId, function(Stream) {if(stream && stream.getID) {localStream = stream; // LocalStream updated}else {// Failed to switch}});
Sample Code: (With Front Camera of Mobile Device)
EnxRtc.switchMediaDevice(localStream, micDeviceId, { 'facingMode': 'user' }, function(Stream) {if(stream && stream.getID) {localStream = stream; // LocalStream updated}else {// Failed to switch}});
Error Codes
Code | Description |
---|---|
1140 | Repeated switchMediaDevice() calls are issued when a previous mute request is still in process. |
1155 | An invalid parameter is passed. |
1159 | The audio or video stream is not available. |
1189 | The media device switching is not allowed in the mute state. |
Switch the Video Source of Published Streams
The EnxStream.switchCamera()
method allows you to switch the camera used to create a published stream to an alternate camera.
- Method:
EnxStream.switchCamera(localStream, camDeviceId, Callback)
- Parameters:
LocalStream
: EnxStream. Published stream.camDeviceId
: As String (New Camera's Device ID). As Object ({ 'facingMode': 'user/environment'
for Front/Rear Camera of Mobile Device)Callback
: Returns the updated stream with new devices. If the video is muted, then it returns the following JSON:{ result: 0, msg: 'set but not active' }
.
Sample Code: (With Device ID)
EnxRtc.switchCamera(localStream, camDeviceId, function(Stream) {if(stream && stream.getID) {localStream = stream; // LocalStream updated}else {// Failed to switch}});
Sample Code: (With Front Camera of Mobile Device)
EnxRtc.switchCamera(localStream, { 'facingMode': 'user' }, function(Stream) {if(stream && stream.getID) {localStream = stream; // LocalStream updated}else {// Failed to switch}});
Switch the Audio Source of Published Streams
The EnxStream.switchMicrophone()
method allows you to switch the microphone used to create a published stream to an alternate microphone.
- Method:
EnxStream.switchMicrophone(localStream, micDeviceId, Callback)
- Parameters:
LocalStream
: EnxStream. Published stream.micDeviceId
: String. New Microphone's Device ID.Callback
: Returns the updated stream with new devices. If the audio is muted, then it returns the following JSON:{ result: 0, msg: 'set but not active' }
.
Sample Code
localStream.switchMicrophone(localStream, micDeviceId, function(Stream) {if(stream && stream.getID) {localStream = stream; // LocalStream updated}else {// Failed to switch}});
Switch the Speaker
The EnxRoom.switchSpeaker()
method is used to switch to a different speaker to play audio.
- Method:
EnxRoom.switchSpeaker(speakerId, Callback)
- Parameters:
speakerId
: String. New Speaker's Device ID.Callback
: Callback to handle the status of the switching process.
Sample Code
room.switchSpeaker(newSpeakerId, function(res) {if(res.result == 0) {// Switching successful}else {// Failed to switch}});
Mute or Unmute Audio/Video in a Stream
You can mute audio or turn off video of your own published media stream. Similarly, you can unmute audio or turn on a video on a media stream again simply using Method Call.
Mute Audio in a Stream
The EnxStream.muteAudio()
method allow you to mute the audio track of a local stream.
- Method:
muteAudio(Callback)
- Event Notifications:
user-audio-muted
: Notification to everyone in a room when a user mutes self-audio.
Sample Code
localStream.muteAudio( function(res) {// Audio muted. Handle UI here});// Notification to others when a user muted audioroom.addEventListener("user-audio-muted", function (event) {// Handle UI here});
Error Codes
ErrorCode | Description |
---|---|
1140 | Repeated API calls are issued when a previous mute request is still in process. |
1176 | The moderator has the right to control the media devices. |
1191 | The stream track has ended. Users must rejoin to restore the audio. |
Unmute Audio in a Stream
The EnxStream.unmuteAudio()
method allow you to unmute the audio track of a local stream.
- Method:
unmuteAudio(Callback)
- Event Notifications:
user-audio-unmuted
: Notification to everyone in a room when a user unmutes self-audio.
Sample Code
localStream.unmuteAudio(function(res) {// Audio unmuted. Handle UI here});// Notification to others when a user unmuted audioroom.addEventListener("user-audio-unmuted", function (event) {// Handle UI here});
Error Codes
Error Code | Description |
---|---|
1140 | Notification to everyone in a room when a user unmutes self-audio. |
1176 | The moderator has the right to control the media devices. |
1191 | The stream track has ended. Users must rejoin to restore the audio. |
Mute Video in a Stream
The EnxStream.muteVideo()
method allow you to mute the video track of a local stream.
- Method:
muteVideo(Callback)
- Event Notifications:
user-video-muted
: Notification to everyone in a room when a user unmutes self-video.
Sample Code
localStream.muteVideo( function(res) {// Video muted. Handle UI here});// Notification to others when a user muted videoroom.addEventListener("user-video-muted", function (event) {// Handle UI here});
Error Codes
Error Code | Description |
---|---|
1140 | Repeated API calls are issued when a previous mute request is still in process. |
1176 | The moderator has the right to control the media devices. |
Unmute Video in a Stream
The EnxStream.unmuteVideo()
methods allow you to unmute the video track of a local stream.
- Method:
unmuteVideo(Callback)
- Event Notification:
user-video-unmuted
: Notification to everyone in a room when a user unmutes self-video.
Sample Code
localStream.unmuteVideo( function(res) {// Video unmuted. Handle UI here});// Notification to others when a user muted videoroom.addEventListener("user-video-unmuted", function (event) {// Handle UI here});
Error Codes
Error Code | Description |
---|---|
1140 | Repeated API calls are issued when a previous mute request is in process. |
1176 | The moderator has the right to control the media devices. |