Skip to main content

Camera & Microphone

This page shortly describes how to send Audio and Video data to other participants.

Before you can set audio and video input sources you need to set up an instance of StreamVideoClient. Follow the Client & Auth guide to learn how to do it.

Setup sending Audio

In order to send audio data you need to set an instance of AudioSource as an input source by calling the SetAudioInputSource method on the instance of StreamVideoClient.

_client.SetAudioInputSource(audioSource); // audioSource is of type AudioSource

Handle microphone input in Unity

The way you start streaming audio from a microphone device is by calling Unity's Microphone.Start method and providing the microphone device name. You obtain the microphone device name from Unity's Microphone.devices array.

// Obtain reference to an AudioSource that will be used a source of audio
var inputAudioSource = GetComponent<AudioSource>();

// Get a valid microphone device name.
// You usually want to populate a dropdown list with Microphone.devices so that the user can pick which device should be used
_activeMicrophoneDeviceName = Microphone.devices.First();

inputAudioSource.clip
= Microphone.Start(_activeMicrophoneDeviceName, true, 3, AudioSettings.outputSampleRate);
inputAudioSource.loop = true;
inputAudioSource.Play();

_client.SetAudioInputSource(inputAudioSource);

Change microphone device during the call

Here's an example of how to change the active microphone device:

// Stop previously active microphone
Microphone.End(_activeMicrophoneDeviceName);

// Obtain reference to an AudioSource that was setup as an input source
var inputAudioSource = GetComponent<AudioSource>();

inputAudioSource.clip = Microphone.Start(newMicrophoneDeviceName, true, 3, AudioSettings.outputSampleRate);

In case you need to use a different reference to AudioSource, you can simply call the _client.SetAudioInputSource again, and the audio track will be updated with the new audio input:

_client.SetAudioInputSource(inputAudioSource);

Additional Notes

  • For standalone platforms like Windows or macOS you'd usually implement a dropdown menu populated with Microphone.devices so that the user can pick which microphone device should be used. The reason for this is that on standalone platforms there can be multiple microphone devices plugged in.
  • For mobile platforms like Android or iOS microphone devices are handled by the OS, so you usually just pick the first device.
  • You should handle the case where user does not have a microphone device at all and the Microphone.devices array is empty.
  • For mobile platforms like Android or iOS it's best to request a permission to access the microphone and handle the case where user did not grant the permission to use it. Read more in Unity's docs

Please refer to Unity's documentation for more information on how to use Microphone devices:

Setup sending Video

In order to send video data you need to set an instance of WebCamTexture as a video source by calling the SetCameraInputSource method on the instance of StreamVideoClient.

_client.SetCameraInputSource(activeCamera); // activeCamera is of type WebCamTexture

Handle camera device input in Unity

They way you start streaming video from a camera device is by creating a WebCamTexture instance using the camera device name (obtained from WebCamTexture.devices) and calling Play() on the WebCamTexture instance.

// Obtain a camera device
var cameraDevice = WebCamTexture.devices.First();

// Use device name to create a new WebCamTexture instance
var activeCamera = new WebCamTexture(cameraDevice.name, 1920, 1080, 24);

// Call Play() in order to start capturing the video
activeCamera.Play();

// Set WebCamTexture in Stream's Client - this WebCamTexture will be the video source in video calls
_client.SetCameraInputSource(activeCamera);

The video resolution and FPS parameters you set in your WebCamTexture instance will be used for the video publishing settings. In the above example, the video will aim to be streamed at 1080p (1920x1080) resolution and 24 frames per second.

note

Stream service will dynamically adjust the video resolution and FPS parameters based on the network traffic. The ultimate goal is to ensure a smooth video experience without video stuttering. The settings you provide are the maximum aimed for if the network conditions allow it.

Change camera device during the call

The most efficient way to change the camera device is to update the deviceName property on the instance of WebCamTexture that was previously set as an input source via _client.SetCameraInputSource:

_activeCamera.Stop();
_activeCamera.deviceName = newDeviceName;
_activeCamera.Play();

In case you need to use a different reference to WebCamTexture, you can simply call the _client.SetCameraInputSource again, and the video track will be updated with the new camera input:

_client.SetCameraInputSource(activeCamera);

Additional Notes

  • For standalone platforms like Windows or macOS you'd usually implement a dropdown menu populated with WebCamTexture.devices so that the user can pick which camera device should be used.
  • For mobile platforms like Android or iOS there are usually two cameras available: Front and Back cameras. Depending on your use case you may either want to automatically select the Front camera or allow user to toggle between the Front and the Back cameras.
  • You should handle the case where user does not have a camera device at all and the WebCamTexture.devices array is empty.
  • For mobile platforms like Android or iOS it's best to request a permission to access the camera and handle the case where user did not grant the permission to use it. Read more in Unity's docs
note

For Android, if you're setting the WebCamTexture resolution, you need to set the resolution as a multiple of 16x16 as required by webRTC

Please refer to Unity's documentation for more information on how to use Camera devices:

Did you find this page helpful?