A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
// SPDX-License-Identifier: AGPL-3.0-or-later
import {VoiceTrackSource} from '@app/features/voice/engine/VoiceTrackSource';
import type {TrackPublishOptions} from 'livekit-client';
const OPUS_MAX_AUDIO_BITRATE_BPS = 510000;
const VOICE_CHANNEL_MIN_AUDIO_BITRATE_BPS = 8000;
const STEREO_VOICE_MIN_AUDIO_BITRATE_BPS = 64000;
const SCREEN_SHARE_AUDIO_BITRATE_BPS = 128000;
function normaliseAudioBitrateBps(value: number | null | undefined): number | undefined {
if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0) return undefined;
const rounded = Math.round(value);
const bitsPerSecond = rounded >= 8 && rounded <= 512 ? rounded * 1000 : rounded;
return Math.min(Math.max(bitsPerSecond, VOICE_CHANNEL_MIN_AUDIO_BITRATE_BPS), OPUS_MAX_AUDIO_BITRATE_BPS);
}
export function sendsStereoMicrophone(channelBitrate: number | null | undefined, stereoCapture: boolean): boolean {
const maxBitrate = normaliseAudioBitrateBps(channelBitrate);
return stereoCapture && maxBitrate !== undefined && maxBitrate >= STEREO_VOICE_MIN_AUDIO_BITRATE_BPS;
}
export function buildMicrophonePublishOptions(
channelBitrate: number | null | undefined,
stereoCapture = false,
): TrackPublishOptions | undefined {
const maxBitrate = normaliseAudioBitrateBps(channelBitrate);
if (!maxBitrate) return undefined;
return {
audioPreset: {
maxBitrate,
priority: 'high',
},
dtx: false,
red: true,
forceStereo: sendsStereoMicrophone(maxBitrate, stereoCapture),
};
}
export const SCREEN_SHARE_AUDIO_PUBLISH_OPTIONS: TrackPublishOptions = {
audioPreset: {
maxBitrate: SCREEN_SHARE_AUDIO_BITRATE_BPS,
priority: 'high',
},
dtx: false,
forceStereo: true,
red: true,
source: VoiceTrackSource.ScreenShareAudio as TrackPublishOptions['source'],
stream: 'screen_share',
};
export function prepareHighFidelityScreenShareAudioTrack(track: MediaStreamTrack | undefined): void {
if (!track) return;
try {
(
track as MediaStreamTrack & {
contentHint?: string;
}
).contentHint = 'music';
} catch {}
if (typeof track.applyConstraints === 'function') {
void track
.applyConstraints({
channelCount: 2,
sampleRate: 48000,
echoCancellation: false,
noiseSuppression: false,
autoGainControl: false,
})
.catch(() => undefined);
}
}