From 240c49648f48102c115042ab17bf1c11d7fc92d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sierpi=C5=84ski?= <33436839+sierpinskid@users.noreply.github.com> Date: Mon, 4 Dec 2023 18:48:20 +0100 Subject: [PATCH] [Docs] Add info on how to update audio & video input sources during the call --- .../03-guides/CameraAndMicrophone.cs | 83 +++++++++++++++++++ .../03-guides/CameraAndMicrophone.cs.meta | 3 + .../03-guides/04-camera-and-microphone.mdx | 70 ++++++++++++---- 3 files changed, 139 insertions(+), 17 deletions(-) create mode 100644 Packages/StreamVideo/DocsCodeSamples/03-guides/CameraAndMicrophone.cs create mode 100644 Packages/StreamVideo/DocsCodeSamples/03-guides/CameraAndMicrophone.cs.meta diff --git a/Packages/StreamVideo/DocsCodeSamples/03-guides/CameraAndMicrophone.cs b/Packages/StreamVideo/DocsCodeSamples/03-guides/CameraAndMicrophone.cs new file mode 100644 index 00000000..a58a97e8 --- /dev/null +++ b/Packages/StreamVideo/DocsCodeSamples/03-guides/CameraAndMicrophone.cs @@ -0,0 +1,83 @@ +using System.Linq; +using StreamVideo.Core; +using UnityEngine; + +namespace DocsCodeSamples._03_guides +{ + internal class CameraAndMicrophone : MonoBehaviour + { + public void SetupMicrophoneInput() + { + // Obtain reference to an AudioSource that will be used a source of audio + var inputAudioSource = GetComponent(); + +// 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); + } + + public void ChangeMicrophoneDevice() + { + var newMicrophoneDeviceName = "test"; + + // Stop previously active microphone + Microphone.End(_activeMicrophoneDeviceName); + + // Obtain reference to an AudioSource that was setup as an input source + var inputAudioSource = GetComponent(); + + inputAudioSource.clip = Microphone.Start(newMicrophoneDeviceName, true, 3, AudioSettings.outputSampleRate); + } + + public void SetupCameraInput() + { + // Obtain a camera device + var cameraDevice = WebCamTexture.devices.First(); + + // Use device name to create a new WebCamTexture instance + var activeCamera = new WebCamTexture(cameraDevice.name); + + // 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); + } + + public void ChangeVideoDevice() + { + // Item from WebCamTexture.devices + var newDeviceName = "deviceName"; + + _activeCamera.Stop(); + _activeCamera.deviceName = newDeviceName; + _activeCamera.Play(); + } + + public void UpdateCameraInputSource() + { + // Obtain a camera device + var cameraDevice = WebCamTexture.devices.First(); + + // Use device name to create a new WebCamTexture instance + var activeCamera = new WebCamTexture(cameraDevice.name); + + // Call Play() in order to start capturing the video + activeCamera.Play(); + + _client.SetCameraInputSource(activeCamera); + } + + private IStreamVideoClient _client; + private string _activeMicrophoneDeviceName; + + private WebCamTexture _activeCamera; + } +} \ No newline at end of file diff --git a/Packages/StreamVideo/DocsCodeSamples/03-guides/CameraAndMicrophone.cs.meta b/Packages/StreamVideo/DocsCodeSamples/03-guides/CameraAndMicrophone.cs.meta new file mode 100644 index 00000000..a2004283 --- /dev/null +++ b/Packages/StreamVideo/DocsCodeSamples/03-guides/CameraAndMicrophone.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: b9f82543f3124c3a92f47b665daed7d1 +timeCreated: 1701704500 \ No newline at end of file diff --git a/docusaurus/docs/Unity/03-guides/04-camera-and-microphone.mdx b/docusaurus/docs/Unity/03-guides/04-camera-and-microphone.mdx index 36e14ebe..0de3f780 100644 --- a/docusaurus/docs/Unity/03-guides/04-camera-and-microphone.mdx +++ b/docusaurus/docs/Unity/03-guides/04-camera-and-microphone.mdx @@ -7,7 +7,7 @@ This page shortly describes how to send **Audio** and **Video** data to other pa Before you can set audio and video input sources you need to set up an instance of `StreamVideoClient`. Follow the [Client & Auth](../03-guides/01-client-auth.mdx) guide to learn how to do it. -### Send Audio +## Setup sending Audio In order to send audio data you need to set an instance of [AudioSource](https://docs.unity3d.com/ScriptReference/AudioSource.html) as an **input source** by calling the `SetAudioInputSource` method on the instance of `StreamVideoClient`. @@ -15,9 +15,9 @@ In order to send audio data you need to set an instance of [AudioSource](https:/ _client.SetAudioInputSource(audioSource); // audioSource is of type AudioSource ``` -### Handling microphone input in Unity +### Handle microphone input in Unity -They way you start streaming audio from a microphone device is by calling Unity's `Microphone.Start` method and providing the microphone device name. +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. ```csharp @@ -32,12 +32,28 @@ inputAudioSource.clip = Microphone.Start(_activeMicrophoneDeviceName, true, 3, AudioSettings.outputSampleRate); inputAudioSource.loop = true; inputAudioSource.Play(); + +_client.SetAudioInputSource(inputAudioSource); ``` -Please refer to Unity's documentation for more information on how to use **Microphone** devices: -* [Microphone.devices](https://docs.unity3d.com/ScriptReference/Microphone-devices.html) -* [Microphone.Start](https://docs.unity3d.com/ScriptReference/Microphone.Start.html) -* [Microphone.End](https://docs.unity3d.com/ScriptReference/Microphone.End.html) +#### Change microphone device during the call + +Here's an example of how to change the active microphone device: + +```csharp +// Stop previously active microphone +Microphone.End(_activeMicrophoneDeviceName); + +// Obtain reference to an AudioSource that was setup as an input source +var inputAudioSource = GetComponent(); + +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: +```csharp +_client.SetAudioInputSource(inputAudioSource); +``` #### Additional Notes @@ -46,7 +62,12 @@ Please refer to Unity's documentation for more information on how to use **Micro - 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](https://docs.unity3d.com/ScriptReference/Application.RequestUserAuthorization.html) -### Send Video +Please refer to Unity's documentation for more information on how to use **Microphone** devices: +* [Microphone.devices](https://docs.unity3d.com/ScriptReference/Microphone-devices.html) +* [Microphone.Start](https://docs.unity3d.com/ScriptReference/Microphone.Start.html) +* [Microphone.End](https://docs.unity3d.com/ScriptReference/Microphone.End.html) + +## Setup sending Video In order to send video data you need to set an instance of [WebCamTexture](https://docs.unity3d.com/ScriptReference/WebCamTexture.html) as a **video source** by calling the `SetCameraInputSource` method on the instance of `StreamVideoClient`. @@ -54,7 +75,7 @@ In order to send video data you need to set an instance of [WebCamTexture](https _client.SetCameraInputSource(activeCamera); // activeCamera is of type WebCamTexture ``` -### Handling camera input in Unity +### 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. @@ -72,6 +93,28 @@ activeCamera.Play(); _client.SetCameraInputSource(activeCamera); ``` +#### 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`: + +```csharp +_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: +```csharp +_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](https://docs.unity3d.com/ScriptReference/Application.RequestUserAuthorization.html) + :::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 @@ -82,11 +125,4 @@ Please refer to Unity's documentation for more information on how to use **Camer * [WebCamTexture](https://docs.unity3d.com/ScriptReference/WebCamTexture.html) * [WebCamTexture.devices](https://docs.unity3d.com/ScriptReference/WebCamTexture-devices.html) * [WebCamTexture.Play](https://docs.unity3d.com/ScriptReference/WebCamTexture.Play.html) -* [WebCamTexture.Stop](https://docs.unity3d.com/ScriptReference/WebCamTexture.Stop.html) - -#### 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](https://docs.unity3d.com/ScriptReference/Application.RequestUserAuthorization.html) \ No newline at end of file +* [WebCamTexture.Stop](https://docs.unity3d.com/ScriptReference/WebCamTexture.Stop.html) \ No newline at end of file