concept-collection / commoncall
commoncall / src / p2p / settings.ts
37 lines · 1.5 KBBlameHistoryRaw
1// Shared per-call settings. Both parties see and control ONE settings object
2// per call (reset to defaults each call); changes sync over the call's control
3// data channel with per-key last-writer-wins (see network.ts).
4//
5// To add a future setting: extend CallSettings, DEFAULT_SETTINGS, and
6// SETTING_VALIDATORS, then handle its side effect in Network.settingChanged.
8export const VIDEO_QUALITIES = ['auto', 'high', 'medium', 'low'] as const
9export type VideoQuality = (typeof VIDEO_QUALITIES)[number]
11export interface CallSettings {
12 videoQuality: VideoQuality
15export const DEFAULT_SETTINGS: CallSettings = {videoQuality: 'auto'}
17/** Encoder caps for each preset, applied by EACH side to its own outgoing
18 * video (the setting is symmetric). 'auto' clears all caps and leaves
19 * adaptation entirely to the browser's congestion control; the others are
20 * proactive ceilings for slow or metered links. */
21export const QUALITY_PARAMS: Record<
22 VideoQuality,
23 {maxBitrate?: number; scaleResolutionDownBy?: number; maxFramerate?: number}
24> = {
25 auto: {},
26 high: {maxBitrate: 2_500_000, maxFramerate: 30},
27 medium: {maxBitrate: 800_000, scaleResolutionDownBy: 2, maxFramerate: 24},
28 low: {maxBitrate: 200_000, scaleResolutionDownBy: 4, maxFramerate: 15}
31/** Settings arrive over the network, so every value is validated before use. */
32export const SETTING_VALIDATORS: {
33 [K in keyof CallSettings]: (v: unknown) => v is CallSettings[K]
34} = {
35 videoQuality: (v): v is VideoQuality =>
36 (VIDEO_QUALITIES as readonly unknown[]).includes(v)