You may need to switch to an alternate Camera, Microphone or Speaker being in a session without any break-in communication. APIs helps you achieve this seamlessly.

Table of Contents

The following APIs allow you to switch to alternate Media Devices after having published Stream, thus facilitating a switch on the fly.

The API allows you to switch the camera and microphone separately or simultaneously using the Device ID of the new devices. To fetch the list of available devices and their IDs, use EnxRtc.getDevices() method.

Simultaneous Switching Camera & Microphone Source of Published Stream

The EnxRtc.switchMediaDevice() method allows you to switch both Camera and Microphone, thus transferring the Audio and Video input to alternate Camera and Microphone.

Method: EnxRtc.switchMediaDevice(LocalStream, micDeviceId, camDeviceId, Callback)

Parameters:

  • LocalStream – Published Stream.
  • micDeviceId – DeviceId of New Microphone.
  • camDeviceId – DeviceId of New Camera. On Mobile Browsers, video input is generally received from the Rear Camera. To use Front Camera instead of Rear Camera use {facingMode: 'user'}. For Rear Camera, use { facingMode: 'environment'}.
  • Callback – Returns the Updated Stream with new Devices. In case Audio/Video is muted, then it would return the following JSON:

{ result: 0, msg: 'set but not active' }

EnxRtc.switchMediaDevice(localStream, micDeviceId, camDeviceId, function(Stream) {
         if(stream && stream.getID) {
             localStream = stream; // LocalStream updated   
         }
         else {
             // Failed to switch
         }
 });

For switching from Rear Camera to Front Camera on Mobile Browsers:

EnxRtc.switchMediaDevice(localStream, micDeviceId, { facingMode: 'user'}, function(Stream) {
         if(stream && stream.getID) {
             localStream = stream; // LocalStream updated   
         }
         else {
             // Failed to switch
         }
 });

Error Codes / Exceptions

CodeDescription
1140Repeated switchMediaDevice() call made while a previous mute request is in process.
1155Invalid parameter passed.
1159Audio / Video Stream not available.
1189Media device switching not allowed in mute state.

Switching Video Source of Published Stream

The EnxStream.switchCamera() method allows you to switch the Camera used to create published Stream to an alternate Camera.

Method: EnxStream.switchCamera(localStream, camDeviceId, Callback)

Parameters:

  • LocalStream – Published Stream.
  • camDeviceId – DeviceId of New Camera. On Mobile Browsers, video input is generally received from the Rear Camera. To use Front Camera instead of Rear Camera use {facingMode: 'user'}. For Rear Camera, use { facingMode: 'environment'}.
  • Callback – Returns the Updated Stream with new Camera In case Video is muted, then it would return the following JSON:

{ result: 0, msg: 'set but not active' }

 localStream.switchCamera(localStream, camDeviceId, function(Stream) {
         if(stream && stream.getID) {
             localStream = stream; // LocalStream updated   
         }
         else {
             // Failed to switch
         }
 });

For switching from Rear Camera to Front Camera on Mobile Browsers:

 localStream.switchCamera(localStream, { facingMode: 'user'}, function(Stream) {
         if(stream && stream.getID) {
             localStream = stream; // LocalStream updated   
         }
         else {
             // Failed to switch
         }
 });

Switching Audio Source of Published Stream

The EnxStream.switchMicrophone() method allows you to switch Micrcophone used to create published Stream to an alternate Microphone.

Method: EnxStream.switchMicrophone(localStream, micDeviceId, Callback)

Parameters:

  • localStream – Published Stream.
  • micDeviceId – DeviceId of New Microphone.
  • Callback – Returns the updated Stream with new Microphone. In case Audio is muted, then it would return the following JSON:

{ result: 0, msg: 'set but not active' }

localStream.switchMicrophone(localStream, micDeviceId, function(Stream) {
         if(stream && stream.getID) {
             localStream = stream; // LocalStream updated   
         }
         else {
             // Failed to switch
         }
 });

Switching Speaker

The EnxRoom.switchSpeaker() method is used to switch to a different Speaker to play Audio.

Method: EnxRoom.switchSpeaker(speakerId, Callback)

Parameters:

  • speakerId – DeviceId of the new Speaker.
  • Callback – Callback to handle status of the switching process.
room.switchSpeaker(newSpeakerId, function(res) {
         if(res.result == 0) {
             // Switching successful   
         }
         else {
             // Failed to switch
         }
 });