feat: Add microphone audio processing constraints (#2708)
## Summary
- add optional microphone constraints for echo cancellation, noise
suppression, automatic gain control, and channel count
- request configured values as non-mandatory `ideal` constraints
- expose privacy-safe microphone capabilities, requested constraints,
and applied settings in card diagnostics
- document the new configuration and add schema, microphone manager, and
diagnostics tests
## Motivation
The card currently calls `getUserMedia()` with `audio: true`. This
leaves echo cancellation, noise suppression, automatic gain control, and
channel count implicit.
Browser and device behavior differs. Explicit processing defaults can
regress microphone gain or amplify noise on some devices. This change
therefore keeps all processing constraints optional and configurable.
## Configuration
```yaml
live:
microphone:
constraints:
echo_cancellation: true
noise_suppression: true
auto_gain_control: false
channel_count: 1
```
Configured values use `ideal` constraints. A browser can ignore
unsupported values. Card diagnostics show the browser capabilities, the
requested constraints, and the reported applied settings.
## Backward compatibility
- existing configurations still use `audio: true`
- no audio-processing defaults are added
- explicit `false` values are preserved
- diagnostic output excludes device and group identifiers
## Validation
- focused microphone, schema, and diagnostics tests: 46 passed
- full test suite: 7,177 passed
- lint passed
- format check passed
- typecheck passed
- unused-code check passed
- production build passed
The optional constraints were also tested successfully with an iOS Home
Assistant Companion client and a go2rtc-based full-duplex intercom. This
is a client microphone-processing change only. It does not add backend
audio denoise.
---------
Co-authored-by: dermotduffy <dermot.duffy@gmail.com>
This commit is contained in:
co-authored by
dermotduffy
parent
543e5d0fcf
commit
3ee6b6059e
@@ -1,7 +1,13 @@
|
||||
import { omit } from 'lodash-es';
|
||||
|
||||
import { localize } from '../localize/localize';
|
||||
import { AdvancedCameraCardError } from '../types';
|
||||
import { Generation } from '../utils/concurrency/generation';
|
||||
import type { CardMicrophoneAPI, MicrophoneState } from './types';
|
||||
import type { CardMicrophoneAPI, MicrophoneDiagnostics, MicrophoneState } from './types';
|
||||
|
||||
const MICROPHONE_DEVICE_IDENTIFIERS = ['deviceId', 'groupId'] as const;
|
||||
|
||||
export type MicrophoneDeviceIdentifier = (typeof MICROPHONE_DEVICE_IDENTIFIERS)[number];
|
||||
|
||||
export class MicrophoneNotSupportedError extends AdvancedCameraCardError {
|
||||
constructor() {
|
||||
@@ -13,6 +19,9 @@ export class MicrophoneManager {
|
||||
private _api: CardMicrophoneAPI;
|
||||
private _stream: MediaStream | null = null;
|
||||
|
||||
// The most recent microphone connection's diagnostics.
|
||||
private _diagnostics: MicrophoneDiagnostics | null = null;
|
||||
|
||||
// Whether the browser denied the most recent microphone request. Cleared by
|
||||
// a later successful connect.
|
||||
private _forbidden = false;
|
||||
@@ -45,6 +54,10 @@ export class MicrophoneManager {
|
||||
return this._state;
|
||||
}
|
||||
|
||||
public getDiagnostics(): MicrophoneDiagnostics | null {
|
||||
return this._diagnostics;
|
||||
}
|
||||
|
||||
public initialize(): void {
|
||||
this._setState();
|
||||
}
|
||||
@@ -79,7 +92,7 @@ export class MicrophoneManager {
|
||||
let stream: MediaStream;
|
||||
try {
|
||||
stream = await navigator.mediaDevices.getUserMedia({
|
||||
audio: true,
|
||||
audio: this._getAudioProcessingConstraints(),
|
||||
video: false,
|
||||
});
|
||||
} catch (e: unknown) {
|
||||
@@ -104,6 +117,7 @@ export class MicrophoneManager {
|
||||
this._removeEndedListeners(this._stream);
|
||||
this._stopTracks(this._stream);
|
||||
this._stream = stream;
|
||||
this._diagnostics = this._getTrackDiagnostics(stream.getAudioTracks()[0]);
|
||||
this._addEndedListeners(stream);
|
||||
this._forbidden = false;
|
||||
this._reconcile();
|
||||
@@ -169,6 +183,55 @@ export class MicrophoneManager {
|
||||
return !this._stream || this._stream.getTracks().every((track) => !track.enabled);
|
||||
}
|
||||
|
||||
private _getAudioProcessingConstraints(): true | MediaTrackConstraints {
|
||||
const audioProcessing = this._api.getConfigManager().getConfig()?.live
|
||||
.microphone?.audio_processing;
|
||||
|
||||
const constraints: MediaTrackConstraints = {};
|
||||
if (typeof audioProcessing?.auto_gain_control === 'boolean') {
|
||||
constraints.autoGainControl = { ideal: audioProcessing.auto_gain_control };
|
||||
}
|
||||
if (audioProcessing?.channel_count !== undefined) {
|
||||
constraints.channelCount = { ideal: audioProcessing.channel_count };
|
||||
}
|
||||
if (typeof audioProcessing?.echo_cancellation === 'boolean') {
|
||||
constraints.echoCancellation = { ideal: audioProcessing.echo_cancellation };
|
||||
}
|
||||
if (typeof audioProcessing?.noise_suppression === 'boolean') {
|
||||
constraints.noiseSuppression = { ideal: audioProcessing.noise_suppression };
|
||||
}
|
||||
|
||||
return Object.keys(constraints).length ? constraints : true;
|
||||
}
|
||||
|
||||
private _getTrackDiagnostics(track?: MediaStreamTrack): MicrophoneDiagnostics | null {
|
||||
if (!track) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Remove values not suitable for sharing.
|
||||
const getReportableValues = <
|
||||
T extends Partial<Record<MicrophoneDeviceIdentifier, unknown>>,
|
||||
>(
|
||||
values?: T,
|
||||
): Omit<T, MicrophoneDeviceIdentifier> | null => {
|
||||
if (!values) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const reportable = omit(values, MICROPHONE_DEVICE_IDENTIFIERS);
|
||||
return Object.keys(reportable).length ? reportable : null;
|
||||
};
|
||||
|
||||
const capabilities = getReportableValues(track.getCapabilities?.());
|
||||
const settings = getReportableValues(track.getSettings());
|
||||
const diagnostics = {
|
||||
...(capabilities && { capabilities }),
|
||||
...(settings && { settings }),
|
||||
};
|
||||
return Object.keys(diagnostics).length ? diagnostics : null;
|
||||
}
|
||||
|
||||
private _stopTracks(stream: MediaStream | null): void {
|
||||
stream?.getTracks().forEach((track) => track.stop());
|
||||
}
|
||||
|
||||
@@ -24,7 +24,10 @@ import type { KeyboardStateManager } from './keyboard-state-manager';
|
||||
import type { LockManager } from './lock/manager';
|
||||
import type { MediaLoadedInfoManager } from './media-info-manager';
|
||||
import type { MediaPlayerManager } from './media-player-manager';
|
||||
import type { MicrophoneManager } from './microphone-manager';
|
||||
import type {
|
||||
MicrophoneDeviceIdentifier,
|
||||
MicrophoneManager,
|
||||
} from './microphone-manager';
|
||||
import type { NotificationManager } from './notification-manager';
|
||||
import type { PIPManager } from './pip-manager';
|
||||
import type { QueryStringManager } from './query-string-manager';
|
||||
@@ -377,6 +380,14 @@ export interface MicrophoneState {
|
||||
forbidden: boolean;
|
||||
}
|
||||
|
||||
export interface MicrophoneDiagnostics {
|
||||
// What the microphone is able to do.
|
||||
capabilities?: Omit<MediaTrackCapabilities, MicrophoneDeviceIdentifier>;
|
||||
|
||||
// What the browser actually applied.
|
||||
settings?: Omit<MediaTrackSettings, MicrophoneDeviceIdentifier>;
|
||||
}
|
||||
|
||||
export interface TaggedAutomation extends Automation {
|
||||
tag?: unknown;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user