const call = useCall();
const camera = call.camera;
Camera & Microphone
Handling audio and video devices in your application means working with MediaStream
, MediaDeviceInfo
and other WebRTC API objects.
To simplify this, we hide all the complexity inside the SDK and export utility functions and states.
In this guide, we shall go over their usage.
Camera management
The SDK does its best to make working with the camera easy. We expose the following camera object on the call:
Call settings
The default state of the camera is determined by the settings in the call object.
import { useCallStateHooks } from "@stream-io/video-react-native-sdk";
const { useCallSettings } = useCallStateHooks();
const settings = useCallSettings();
console.log(settings?.video.camera_default_on);
Make sure, call.get()
is called at least once in the application, after the call is created.
Start-Stop Camera
We can use the functions camera.enable()
and camera.disable()
to control the publishing and unpublishing our video stream.
Alternatively, you can use camera.toggle()
.
import { useCall, useCallStateHooks } from "@stream-io/video-react-native-sdk";
const call = useCall();
const { useCameraState } = useCallStateHooks();
const { camera, isMute } = useCameraState();
console.log(`Camera is ${isMute ? "off" : "on"}`);
await camera.toggle();
// or, alternatively
await camera.enable();
await camera.disable();
It’s always best to await calls to enable()
, disable()
, and toggle()
, however the SDK does its best to resolve potential race conditions: the last call always wins, so it’s safe to make these calls in an event handler.
The status
is updated once the camera is actually enabled or disabled.
Use optimisticIsMute
for the “optimistic” status that is updated immediately after toggling the camera.
Manage Camera Facing Mode
We can toggle the camera face from front to back and vice versa using camera.flip()
.
import { useCallStateHooks } from "@stream-io/video-react-native-sdk";
const { useCameraState } = useCallStateHooks();
const { camera } = useCameraState();
console.log(direction); // direction returns 'front' or 'back'.
camera.flip();
We can get the facing mode state of the camera by:
import { useCallStateHooks } from "@stream-io/video-react-native-sdk";
const { useCameraState } = useCallStateHooks();
const { direction } = useCameraState(); // direction returns 'front' or 'back'.
Video mute status
We can get the mute state of our video stream by checking the status
value returned from the useCameraState
hook:
import { useCallStateHooks } from "@stream-io/video-react-native-sdk";
const { useCameraState } = useCallStateHooks();
const { status } = useCameraState(); // status returns enabled, disabled or undefined
Show Video Preview
We can get the video stream from the camera using the media stream from the call.camera
object and show it using the RTCView
component from @stream-io/react-native-webrtc
library:
import { useCallStateHooks } from "@stream-io/video-react-native-sdk";
import { RTCView } from "@stream-io/react-native-webrtc";
const { useCameraState } = useCallStateHooks();
const { camera } = useCameraState();
const localVideoStream = camera.state.mediaStream;
return <RTCView streamURL={localVideoStream?.toURL()} />;
Access to the Camera’s MediaStream
Our SDK exposes the current mediaStream
instance that you can use for your needs (for example, local recording, etc…):
import { useCallStateHooks } from "@stream-io/video-react-native-sdk";
const { useCameraState } = useCallStateHooks();
const { mediaStream } = useCameraState();
const [videoTrack] = mediaStream.getVideoTracks();
console.log("Video track", videoTrack);
Microphone management
The SDK does its best to make working with the microphone easy. We expose the following microphone object on the call:
const call = useCall();
const microphone = call.microphone;
Call settings
The default state of the microphone is determined by the settings in the call object.
import { useCallStateHooks } from "@stream-io/video-react-native-sdk";
const { useCallSettings } = useCallStateHooks();
const settings = useCallSettings();
console.log(settings?.audio.mic_default_on);
Make sure, call.get()
is called at least once in the application, after the call is created.
Start-Stop Microphone
We can use the functions microphone.enable()
and microphone.disable()
to control the publishing and unpublishing our audio stream:
Alternatively, you can use microphone.toggle()
.
import { useCallStateHooks } from "@stream-io/video-react-native-sdk";
const { useMicrophoneState } = useCallStateHooks();
const { microphone, isMute } = useMicrophoneState();
console.log(`Microphone is ${isMute ? "off" : "on"}`);
await microphone.toggle();
// or, alternatively
await microphone.enable();
await microphone.disable();
It’s always best to await calls to enable()
, disable()
, and toggle()
, however the SDK does its best to resolve potential race conditions: the last call always wins, so it’s safe to make these calls in an event handler.
The status
is updated once the microphone is actually enabled or disabled. Use optimisticIsMute
for the “optimistic” status that is updated immediately after toggling the microphone.
Audio mute status
We can get the mute state of our audio stream by checking the status
value returned from the useMicrophoneState
hook:
import { useCallStateHooks } from "@stream-io/video-react-native-sdk";
const { useMicrophoneState } = useCallStateHooks();
const { status } = useMicrophoneState(); // status returns enabled, disabled or undefined
Speaking while muted detection
Our SDK provides a mechanism that can detect whether the user started to speak while being muted. Through this mechanism, you can display a notification to the user, or apply any custom logic.
This feature is enabled by default unless the user doesn’t have the permission to send audio or explicitly disabled.
import { useCallStateHooks } from "@stream-io/video-react-native-sdk";
const { useMicrophoneState } = useCallStateHooks();
const { isSpeakingWhileMuted, microphone } = useMicrophoneState();
if (isSpeakingWhileMuted) {
// your custom logic comes here
console.log("You are speaking while muted!");
}
// to disable this feature completely:
await microphone.disableSpeakingWhileMutedNotification();
// to enable it back:
await microphone.enableSpeakingWhileMutedNotification();
Access to the Microphone’s MediaStream
Our SDK exposes the current mediaStream
instance that you can use for your needs (for example, local recording, etc…):
import { useCallStateHooks } from "@stream-io/video-react-native-sdk";
const { useMicrophoneState } = useCallStateHooks();
const { mediaStream } = useMicrophoneState();
const [audioTrack] = mediaStream.getAudioTracks();
console.log("Audio track", audioTrack);
Speaker management
We can use the callManager
module from the SDK for managing speaker audio output in our SDK.
The start()
and stop()
methods are to be used for managing audio. Before joining a call or as soon as you join a call, call the start()
method. Once you leave the call
, use the stop()
method.
import { callManager } from "@stream-io/video-react-native-sdk";
// To be called before joining a call or as soon as joining a call
callManager.start({
audioRole: "communicator", // or "listener"
deviceEndpointType: "speaker", // or "earpiece"
});
// To be called when a call is left
callManager.stop();
audioRole:
communicator
(default) orlistener
. Uselistener
for users that won’t publish audio but only listen (typical for livestream audience). Usecommunicator
otherwise.deviceEndpointType:
speaker
orearpiece
. Available only whenaudioRole
is set tocommunicator
.speaker
enables the loudspeaker when no bluetooth device or other wired headset is connected.earpiece
routes the audio through the ear-speaker unless another external device is connected.earpiece
should be passed only on a scenario of an audio call similar to a mobile cellular phone call.
The following device priority is used:
- Bluetooth Headset or Wired Headset
- Speakerphone or Earpiece.
As platform-specific methods are necessary to handle audio output, we do not support the useSpeakerState()
hook.
Switching audio output device
The API for switching the audio output device is vastly different on iOS and Android.
In iOS, use the following method to open the popover from AVRoutePickerView
that is presented by the system.
The popover will show the audio devices that are available, and the user can tap on the list to choose the desired audio device.
import { callManager } from "@stream-io/video-react-native-sdk";
callManager.ios.showDeviceSelector();
Once this method is called, a popover similar to the on below will appear.
In Android, there is no system-wide audio picker view that we can use. Instead, Android exposes the device names and provides an API to switch to a specific audio device. The SDK internally manages the state in the native to provide a seamless way of switching audio devices on Android.
- getAudioDeviceStatus(): Returns the current audio status.
- selectAudioDevice(endpointName: string): Method to switch to the specific audio device.
- addAudioDeviceChangeListener(): Adds a listener for listening to changes in the audio statuses.
Here is a minimal snippet showing how these APIs can be used in a component:
import { useEffect, useState } from "react";
import {
AudioDeviceStatus,
callManager,
} from "@stream-io/video-react-native-sdk";
const [audioDeviceStatus, setAudioDeviceStatus] = useState<AudioDeviceStatus>();
useEffect(() => {
// set the initial value and listen for changes
callManager.android.getAudioDeviceStatus().then(setAudioDeviceStatus);
return callManager.android.addAudioDeviceChangeListener(setAudioDeviceStatus);
}, []);
const {
devices, // ["Pixel Buds Pro 2", "Wired Headset", "Speaker", "Earpiece"]
selectedDevice, // "Wired Headset"
currentEndpointType, // "speaker" or "earpiece"
} = audioDeviceStatus ?? {};
// switch to a specific audio device
callManager.android.selectAudioDevice(devices[0]);
When callManager.android.selectAudioDevice()
is used. The SDK will persist the selection even if a new external device is connected.
The selection is dropped automatically only when the selected device is disconnected.
Here is a preview of a custom modal based audio device-picker component built using the above APIs.
Force audio through the loudspeaker
A common use case is to allow switching the audio through the load speaker and ear speaker, e.g.: through a button toggle. To support this behavior, you can use the following method on both iOS and Android:
import { callManager } from "@stream-io/video-react-native-sdk";
// route audio through loud speaker immediately (audio outputs here until a new external device is connected)
callManager.speaker.setForceSpeakerphoneOn(true);
// revert back to default behaviour
callManager.speaker.setForceSpeakerphoneOn(false);
Audio volume control
The SDK supports both system-wide audio volume control and individual track level audio volume control.
System wide mute and unmute of audio volume
import { callManager } from "@stream-io/video-react-native-sdk";
// to mute audio
callManager.setMute(true);
// to unmute audio
callManager.setMute(false);
Participant volume control
We also support setting a participant audio volume.
Here is a small snippet of how to set a participant volume to 50%
:
import { type StreamVideoParticipant } from "@stream-io/video-react-native-sdk";
let participant: StreamVideoParticipant; // the intended participant
call.speaker.setParticipantVolume(participant.sessionId, 0.5);