Skip to main content

Camera & Microphone

This page details how to interact with Microphone and Camera devices to transmit audio and video streams to other call participants. Handling camera and microphone inputs in Unity can be challenging; however, Stream's Video SDK for Unity simplifies the process by managing the complexities internally, facilitating easy interaction with these devices.

Interacting with Microphone

All interactions with microphone devices are handled by the AudioDeviceManager that can be accessed via _client.AudioDeviceManager. The _client field is an instance of IStreamVideoClient; if you haven't setup the video chat client yet, you can follow the Client & Auth guide to learn how to do it.

List available microphone devices

All available microphone devices can be accessed with AudioDeviceManager.EnumerateDevices(). This method returns the IEnumerable<MicrophoneDeviceInfo>. The MicrophoneDeviceInfo is a struct representing a single device. You can access the name of the device by the Name property.

var microphones = _client.AudioDeviceManager.EnumerateDevices();

foreach (var mic in microphones)
{
Debug.Log(mic.Name); // Get microphone name
}

Select microphone

Declaration

public void SelectDevice(MicrophoneDeviceInfo device, bool enable)

ArgumentDescription
deviceMicrophone to select
enableEnabled device is capturing audio input
_client.AudioDeviceManager.SelectDevice(microphone, enable: true);

Get selected microphone

You get the currently selected microphone device via AudioDeviceManager.SelectedDevice. Please note, that the returned MicrophoneDeviceInfo is a struct meaning

var selectedMicrophone = _client.AudioDeviceManager.SelectedDevice;

Start/Stop audio capturing

Once a device is selected, you can start/stop the audio capturing with the Enable(), Disable(), or SetEnabled(bool isEnabled) methods.

// Enable device to start capturing microphone input
_client.AudioDeviceManager.Enable();

// Disable device to stop capturing microphone input
_client.AudioDeviceManager.Disable();

// Set the enabled state by passing a boolean argument
_client.AudioDeviceManager.SetEnabled(true);

Check if microphone is enabled

Enabled device is actively capturing audio input from a selected microphone.

var isDeviceEnabled = _client.AudioDeviceManager.IsEnabled;

Events

The SelectedDeviceChanged and IsEnabledChanged events are triggered when a new device is selected or a device enabled state changes respectively.

public void AudioDeviceManagerEvents()
{
// Triggered when the selected devices changes
_client.AudioDeviceManager.SelectedDeviceChanged += OnSelectedDeviceChanged;

// Triggered when the IsEnabled property changes
_client.AudioDeviceManager.IsEnabledChanged += OnIsEnabledChanged;
}

private void OnIsEnabledChanged(bool isEnabled) { }

private void OnSelectedDeviceChanged(MicrophoneDeviceInfo previousDevice, MicrophoneDeviceInfo currentDevice) { }

Android & iOS

Users must grant permission to use the Microphone device for platforms like Android and IOS. Otherwise, capturing audio will not work. Typical patterns are requesting permissions when the application starts or when a user attempts to enable audio capturing.

iOS and WebGL

You can request permission to use a microphone device on iOS and WebGL platforms by using Unity's RequestUserAuthorization:

// Request permission to use the Microphone
Application.RequestUserAuthorization(UserAuthorization.Microphone);

// Check if user granted microphone permission
if (!Application.HasUserAuthorization(UserAuthorization.Microphone))
{
// Notify user that microphone permission was not granted and the microphone capturing will not work.
}

Android

For the Android platform, Unity recommends using the Permission.RequestUserPermission:

// Request microphone permissions
Permission.RequestUserPermission(Permission.Microphone);

// Check if user granted microphone permission
if (!Permission.HasUserAuthorizedPermission(Permission.Microphone))
{
// Notify user that microphone permission was not granted and the microphone capturing will not work.
}

Interacting with Web Camera

All interactions with camera devices are handled by the VideoDeviceManager that can be accessed via _client.VideoDeviceManager. The _client field is an instance of IStreamVideoClient; if you haven't setup the video chat client yet, you can follow the Client & Auth guide to learn how to do it.

List available camera devices

All available microphone devices can be accessed with AudioDeviceManager.EnumerateDevices(). This method returns the IEnumerable<MicrophoneDeviceInfo>. The MicrophoneDeviceInfo is a struct representing a single device. You can access the name of the device by the Name property.

var cameras = _client.VideoDeviceManager.EnumerateDevices();

foreach (var camera in cameras)
{
Debug.Log(camera.Name); // Get camera name
}

Select camera

Declaration

public void SelectDevice(CameraDeviceInfo device, bool enable, int fps = 30)

ArgumentDescription
deviceCamera to select
enableEnabled device is capturing video input
fps(OPTIONAL) How many frames per second should the video be captured. The default value is 30
_client.VideoDeviceManager.SelectDevice(camera, enable: true);
_client.VideoDeviceManager.SelectDevice(camera, enable: true, requestedFPS: 24);

Declaration

public void SelectDevice(CameraDeviceInfo device, VideoResolution requestedResolution, bool enable, int requestedFPS = 30)

ArgumentDescription
deviceCamera to select
enableEnabled device is capturing video input
requestedResolutionAt what resolution should the video be captured
fps(OPTIONAL) How many frames per second should the video be captured. The default value is 30
_client.VideoDeviceManager.SelectDevice(camera, VideoResolution.Res_720p, enable: true);
_client.VideoDeviceManager.SelectDevice(camera, VideoResolution.Res_720p, enable: true, requestedFPS: 30);

The VideoResolution argument type contains multiple predefined resolutions:

ValueResolution
VideoResolution.Res_144p256x144
VideoResolution.Res_240p320x240
VideoResolution.Res_360p480x360
VideoResolution.Res_480p640x480
VideoResolution.Res_720p1280x720
VideoResolution.Res_1080p1920x1080
new VideoResolution(int width, int heightcustom resolution
_client.VideoDeviceManager.SelectDevice(camera, VideoResolution.Res_144p, enable: true);
_client.VideoDeviceManager.SelectDevice(camera, VideoResolution.Res_240p, enable: true);
_client.VideoDeviceManager.SelectDevice(camera, VideoResolution.Res_360p, enable: true);
_client.VideoDeviceManager.SelectDevice(camera, VideoResolution.Res_480p, enable: true);
_client.VideoDeviceManager.SelectDevice(camera, VideoResolution.Res_720p, enable: true);
_client.VideoDeviceManager.SelectDevice(camera, VideoResolution.Res_1080p, enable: true);
_client.VideoDeviceManager.SelectDevice(camera, new VideoResolution(500, 500), enable: true);

Requested resolution and FPS

Please note that the video resolution and the FPS are the requested values passed to the camera device. Each device has it's own limitations and in case the passed values are not supported by the device the closest possible value will be selected.

Get Selected Camera

You can retrieve the currently selected camera device using VideoDeviceManager.SelectedDevice:

public void GetSelectedCamera()
{
var selectedCamera = _client.VideoDeviceManager.SelectedDevice;
}

Start/Stop Camera Capturing

Once a camera device is selected, you can start or stop the video capturing using the Enable(), Disable(), or SetEnabled(bool isEnabled) methods.

public void StartStopCamera()
{
// Enable device to start capturing camera input
_client.VideoDeviceManager.Enable();

// Disable device to stop capturing camera input
_client.VideoDeviceManager.Disable();

// Set the enabled state by passing a boolean argument
_client.VideoDeviceManager.SetEnabled(true);
}

Check if Camera is Enabled

Check if the camera is enabled and actively capturing video input:

public void CheckCameraStatus()
{
// Check if currently selected device is enabled
var isDeviceEnabled = _client.VideoDeviceManager.IsEnabled;
}

Get Local Participant Camera Preview

For the local participant there will be no video and audio track defined in the IStreamVideoCallParticipant object because those streams are not being received from the remote servers like for all remote participants. If you'd wish to present the local participant video stream you get a reference to the instance of WebCamTexture associated with the selected device via _client.VideoDeviceManager.GetSelectedDeviceWebCamTexture().

var webCamTexture = _client.VideoDeviceManager.GetSelectedDeviceWebCamTexture();

// You can attach this texture to RawImage UI Component
GetComponent<RawImage>().texture = webCamTexture;

Please note that the instance of WebCamTexture may change every time a new device is selected therefore you should subscribe to the SelectedDeviceChanged event:

public void GetLocalParticipantVideoPreviewFull()
{
// Triggered when the selected devices changes
_client.VideoDeviceManager.SelectedDeviceChanged += UpdateLocalParticipantPreview;
}

private void UpdateLocalParticipantPreview(CameraDeviceInfo previousDevice, CameraDeviceInfo currentDevice)
{
var webCamTexture = _client.VideoDeviceManager.GetSelectedDeviceWebCamTexture();

// You can attach this texture to RawImage UI Component
GetComponent<RawImage>().texture = webCamTexture;
}

Check if camera is enabled

public void CheckCameraStatus()
{
// Check if currently selected device is enabled
var isDeviceEnabled = _client.VideoDeviceManager.IsEnabled;
}

Events

SelectedDeviceChanged and IsEnabledChanged events occur when a new device is selected or the device's enabled state changes.

public void VideoDeviceManagerEvents()
{
// Triggered when the selected devices changes
_client.VideoDeviceManager.SelectedDeviceChanged += OnSelectedDeviceChanged;

// Triggered when the IsEnabled property changes
_client.VideoDeviceManager.IsEnabledChanged += OnIsEnabledChanged;
}

private void OnIsEnabledChanged(bool isEnabled) { }

private void OnSelectedDeviceChanged(CameraDeviceInfo previousDevice, CameraDeviceInfo currentDevice) { }

Android & iOS

Users must grant permission to use the Camera device on Android and iOS platforms. Permissions are typically requested when the application starts or when a user attempts to enable video capturing.

iOS and WebGL

For iOS and WebGL platforms, you can request camera permission using Unity's RequestUserAuthorization:

public void CameraIOSPermissions()
{
// Request permission to use the Camera
Application.RequestUserAuthorization(UserAuthorization.WebCam);

// Check if user granted camera permission
if (!Application.HasUserAuthorization(UserAuthorization.WebCam))
{
// Notify user that camera permission was not granted and the camera capturing will not work.
}
}

Android

On Android, request camera permissions using Permission.RequestUserPermission:

public void CameraAndroidPermissions()
{
// Request camera permissions
Permission.RequestUserPermission(Permission.Camera);

// Check if user granted camera permission
if (!Permission.HasUserAuthorizedPermission(Permission.Camera))
{
// Notify user that camera permission was not granted and the camera capturing will not work.
}
}

Did you find this page helpful?