Skip to main content

Working with Devices

This guide explores how to manage audio devices using the ODIN SDK. You'll learn how to list available audio devices, allowing users or your application to select a specific one. We'll cover using a chosen

Device

to create an

AudioInput

—the primary component for configuring audio capture—and how to adjust its Audio Processing Module (APM) settings. Additionally, this guide will demonstrate managing

AudioInput

from

RemotePeer

instances, including tasks like volume control and activity detection.

Listing and selecting Devices

The ODIN SDK utilizes the

DeviceManager

class for listing audio

Device

s and creating

AudioInput

instances. As its methods are static, they can be called directly on the class without needing to create an instance.

warning

Important Note: Firefox browsers provide an empty device list, even when the permission for Audio Devices is granted until the first MediaStream was requested. A workaround is, to fetch a MediaStream and stopping it directly. More info on MDN getUserMedia()

Firefox list devices workaround
  async function requestUserMediaTest() {
const ms = await navigator.mediaDevices.getUserMedia({
audio: true,
});
ms?.getTracks().forEach((track) => {
track.stop();
ms.removeTrack(track);
});
return true;
}
Example working with input Devices
const inputDevices = DeviceManager.listAudioInputs();
// Let the user or app select an input device, in this exampe we just take the first.
const selectedInputDevice = inputDevices[0];

// Both params are optional. If empty will take the system's default device and our recommended default settings.
// It makes sense to store the Input in a scope where it can be reused, for example if the user wan'ts to change to volume later on.
const audioInput = DeviceManager.createAudioInput(
{
device: inputDevice,
onStatusChanged: status => console.log(status)
}, {
// Default values
volume: 1,
echoCanceller: true,
noiseSuppression: true,
gainController: true,
voiceActivity: {
attackThreshold: 0.9,
releaseThreshold: 0.8,
},
volumeGate: {
attackThreshold: -30,
releaseThreshold: -40,
},
});

Starting AudioInputs

Once an audio device has been selected and an

AudioInput

created using the

DeviceManager

(as demonstrated in the previous example), this

DeviceManager

must then be added to your

Room

instance. This step is crucial for transmitting audio to other participants, referred to as

RemotePeer

, within the room.

Starting AudioInputs
const inputDevices = DeviceManager.listAudioInputs();
const selectedInputDevice = inputDevices[0];
const audioInput = DeviceManager.createAudioInput();

// Adding the AudioInput to the room
room.addAudioInput(audioInput);

Stopping AudioInputs

To stop an

AudioInput

, just remove it from the

Room

instance. The

AudioInput

is reusable. It can be added to the same room again or to a different one.

Stopping AudioInputs
room.removeAudioInput(audioInput);

Updating the Device and Settings on an AudioInput

The audio

Device

and also the audio

InputSettings

on an

AudioInput

can get changed at any time.

Updating AudioInput
const inputDevices = DeviceManager.listAudioInputs();
const selectedInputDevice = inputDevices[1];
// Setting a new device
audioInput.setDevice(device);
// Updating the volume
audioInput.setVolume(0.5);
// Fully muting the input and also stopping the MediaStream (Which also stops system indactors i.E. of the browser).
audioInput.setVolume('muted');
// Updating APM settings
audioInput.setInputSettings({
// Default values
volume: 1,
echoCanceller: true,
noiseSuppression: true,
gainController: true,
voiceActivity: {
attackThreshold: 0.9,
releaseThreshold: 0.8,
},
volumeGate: {
attackThreshold: -30,
releaseThreshold: -40,
},
})