@voltkit-rtc/client 0.7.3 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/CHANGELOG.md +57 -0
  2. package/README.md +244 -2
  3. package/dist/cjs/adaptive.d.ts +104 -0
  4. package/dist/cjs/adaptive.d.ts.map +1 -0
  5. package/dist/cjs/adaptive.js +263 -0
  6. package/dist/cjs/adaptive.js.map +1 -0
  7. package/dist/cjs/devices.d.ts +42 -0
  8. package/dist/cjs/devices.d.ts.map +1 -0
  9. package/dist/cjs/devices.js +56 -0
  10. package/dist/cjs/devices.js.map +1 -0
  11. package/dist/cjs/diagnostics.d.ts +65 -0
  12. package/dist/cjs/diagnostics.d.ts.map +1 -0
  13. package/dist/cjs/diagnostics.js +69 -0
  14. package/dist/cjs/diagnostics.js.map +1 -0
  15. package/dist/cjs/fallback.d.ts +81 -0
  16. package/dist/cjs/fallback.d.ts.map +1 -0
  17. package/dist/cjs/fallback.js +148 -0
  18. package/dist/cjs/fallback.js.map +1 -0
  19. package/dist/cjs/health.d.ts +70 -0
  20. package/dist/cjs/health.d.ts.map +1 -0
  21. package/dist/cjs/health.js +116 -0
  22. package/dist/cjs/health.js.map +1 -0
  23. package/dist/cjs/index.d.ts +339 -0
  24. package/dist/cjs/index.d.ts.map +1 -1
  25. package/dist/cjs/index.js +1098 -26
  26. package/dist/cjs/index.js.map +1 -1
  27. package/dist/cjs/link.d.ts +99 -0
  28. package/dist/cjs/link.d.ts.map +1 -0
  29. package/dist/cjs/link.js +219 -0
  30. package/dist/cjs/link.js.map +1 -0
  31. package/dist/cjs/preflight.d.ts +79 -0
  32. package/dist/cjs/preflight.d.ts.map +1 -0
  33. package/dist/cjs/preflight.js +159 -0
  34. package/dist/cjs/preflight.js.map +1 -0
  35. package/dist/cjs/speakers.d.ts +34 -0
  36. package/dist/cjs/speakers.d.ts.map +1 -0
  37. package/dist/cjs/speakers.js +71 -0
  38. package/dist/cjs/speakers.js.map +1 -0
  39. package/dist/esm/adaptive.d.ts +104 -0
  40. package/dist/esm/adaptive.d.ts.map +1 -0
  41. package/dist/esm/adaptive.js +256 -0
  42. package/dist/esm/adaptive.js.map +1 -0
  43. package/dist/esm/devices.d.ts +42 -0
  44. package/dist/esm/devices.d.ts.map +1 -0
  45. package/dist/esm/devices.js +50 -0
  46. package/dist/esm/devices.js.map +1 -0
  47. package/dist/esm/diagnostics.d.ts +65 -0
  48. package/dist/esm/diagnostics.d.ts.map +1 -0
  49. package/dist/esm/diagnostics.js +65 -0
  50. package/dist/esm/diagnostics.js.map +1 -0
  51. package/dist/esm/fallback.d.ts +81 -0
  52. package/dist/esm/fallback.d.ts.map +1 -0
  53. package/dist/esm/fallback.js +142 -0
  54. package/dist/esm/fallback.js.map +1 -0
  55. package/dist/esm/health.d.ts +70 -0
  56. package/dist/esm/health.d.ts.map +1 -0
  57. package/dist/esm/health.js +111 -0
  58. package/dist/esm/health.js.map +1 -0
  59. package/dist/esm/index.d.ts +339 -0
  60. package/dist/esm/index.d.ts.map +1 -1
  61. package/dist/esm/index.js +1096 -26
  62. package/dist/esm/index.js.map +1 -1
  63. package/dist/esm/link.d.ts +99 -0
  64. package/dist/esm/link.d.ts.map +1 -0
  65. package/dist/esm/link.js +214 -0
  66. package/dist/esm/link.js.map +1 -0
  67. package/dist/esm/preflight.d.ts +79 -0
  68. package/dist/esm/preflight.d.ts.map +1 -0
  69. package/dist/esm/preflight.js +153 -0
  70. package/dist/esm/preflight.js.map +1 -0
  71. package/dist/esm/speakers.d.ts +34 -0
  72. package/dist/esm/speakers.d.ts.map +1 -0
  73. package/dist/esm/speakers.js +67 -0
  74. package/dist/esm/speakers.js.map +1 -0
  75. package/package.json +1 -1
  76. package/src/adaptive.ts +295 -0
  77. package/src/devices.ts +70 -0
  78. package/src/diagnostics.ts +94 -0
  79. package/src/fallback.ts +169 -0
  80. package/src/health.ts +127 -0
  81. package/src/index.ts +1097 -27
  82. package/src/link.ts +255 -0
  83. package/src/preflight.ts +191 -0
  84. package/src/speakers.ts +66 -0
package/src/health.ts ADDED
@@ -0,0 +1,127 @@
1
+ /**
2
+ * Media health warnings (docs/implementation-progress/RTC_PERF_PROGRESS.md K1).
3
+ *
4
+ * - Agora's `exception` event pairs each problem with its recovery: FRAMERATE_INPUT_TOO_LOW / _RECOVER (the camera
5
+ * delivers too few frames), AUDIO_INPUT_LEVEL_TOO_LOW / _RECOVER, ...
6
+ * - LiveKit's `AudioSilenceDetected`: a microphone that delivers nothing but silence (a hardware mute switch, a
7
+ * broken device) right after it starts.
8
+ * - LiveKit watches the track's `mute` event (the OS took the device: a phone call, another app) and reacts after 5 s.
9
+ *
10
+ * Each warning is reported once when it starts (`active: true`) and once when it clears (`active: false`).
11
+ * Pure: the decisions take samples.
12
+ */
13
+
14
+ export type MediaWarningCode =
15
+ /** The microphone is on but delivers exact digital silence: a mute switch, a broken or wrongly chosen device. */
16
+ | 'mic-silent'
17
+ /** The camera delivers far fewer frames than asked: usually a dark room (the camera exposes longer), or a busy device. */
18
+ | 'camera-fps-low'
19
+ /** The system took the camera from this page for now (a phone call, another app, a locked screen). */
20
+ | 'camera-interrupted'
21
+ /** The system took the microphone (a phone call, another app). */
22
+ | 'mic-interrupted';
23
+
24
+ export interface MediaWarning {
25
+ code: MediaWarningCode;
26
+ /** true when it starts, false when it clears. */
27
+ active: boolean;
28
+ }
29
+
30
+ /** How often the published camera and microphone are sampled while someone listens. */
31
+ export const HEALTH_SAMPLE_MS = 2000;
32
+ /** Samples of exact silence before mic-silent (6 s). */
33
+ export const SILENT_SAMPLES = 3;
34
+ /** Below this the level counts as exact digital silence (a real room's noise floor is well above it). */
35
+ export const SILENCE_LEVEL = 0.0001;
36
+ /** Fewer frames than this per second, when 24 or more were asked, is camera-fps-low. */
37
+ export const LOW_FPS = 15;
38
+ /** Samples below LOW_FPS before warning (6 s); samples at RECOVERED_FPS or more before clearing. */
39
+ export const LOW_FPS_SAMPLES = 3;
40
+ export const RECOVERED_FPS = 20;
41
+ export const RECOVER_SAMPLES = 2;
42
+ /** A system mute shorter than this is a glitch, not an interruption. */
43
+ export const INTERRUPTED_AFTER_MS = 2000;
44
+
45
+ /** One reading of the published devices. null: not published (or nothing measured). */
46
+ export interface HealthSample {
47
+ /** The microphone's level, while it is on. */
48
+ micLevel: number | null;
49
+ /** The frames per second the camera delivers, while it is on, and what was asked. */
50
+ cameraFps: number | null;
51
+ askedFps: number;
52
+ }
53
+
54
+ /** Decides mic-silent and camera-fps-low from samples. */
55
+ export class HealthDecisions {
56
+ private silent = 0;
57
+ private lowFps = 0;
58
+ private goodFps = 0;
59
+ private readonly activeNow = new Set<MediaWarningCode>();
60
+
61
+ constructor(private readonly report: (warning: MediaWarning) => void) {}
62
+
63
+ isActive(code: MediaWarningCode): boolean {
64
+ return this.activeNow.has(code);
65
+ }
66
+
67
+ feed(sample: HealthSample): void {
68
+ // mic-silent: exact silence held; any sound clears it. Off or unpublished: not a warning.
69
+ if (sample.micLevel === null) {
70
+ this.silent = 0;
71
+ this.set('mic-silent', false);
72
+ } else if (sample.micLevel <= SILENCE_LEVEL) {
73
+ this.silent += 1;
74
+ if (this.silent >= SILENT_SAMPLES) this.set('mic-silent', true);
75
+ } else {
76
+ this.silent = 0;
77
+ this.set('mic-silent', false);
78
+ }
79
+
80
+ // camera-fps-low: only when a smooth rate was asked; cleared once it holds again.
81
+ if (sample.cameraFps === null || sample.askedFps < 24) {
82
+ this.lowFps = 0;
83
+ this.goodFps = 0;
84
+ this.set('camera-fps-low', false);
85
+ } else if (sample.cameraFps < LOW_FPS) {
86
+ this.goodFps = 0;
87
+ this.lowFps += 1;
88
+ if (this.lowFps >= LOW_FPS_SAMPLES) this.set('camera-fps-low', true);
89
+ } else {
90
+ this.lowFps = 0;
91
+ if (sample.cameraFps >= RECOVERED_FPS) this.goodFps += 1;
92
+ if (this.goodFps >= RECOVER_SAMPLES) this.set('camera-fps-low', false);
93
+ }
94
+ }
95
+
96
+ /** A system interruption started or ended (decided by the caller's timer). */
97
+ interrupted(code: 'camera-interrupted' | 'mic-interrupted', active: boolean): void {
98
+ this.set(code, active);
99
+ }
100
+
101
+ /** Everything clears (unpublished, closed): each active warning is reported cleared. */
102
+ clearAll(): void {
103
+ for (const code of [...this.activeNow]) this.set(code, false);
104
+ this.silent = 0;
105
+ this.lowFps = 0;
106
+ this.goodFps = 0;
107
+ }
108
+
109
+ private set(code: MediaWarningCode, active: boolean): void {
110
+ if (this.activeNow.has(code) === active) return;
111
+ if (active) this.activeNow.add(code);
112
+ else this.activeNow.delete(code);
113
+ this.report({ code, active });
114
+ }
115
+ }
116
+
117
+ /** The microphone level and camera frame rate from a producer's statistics (media-source). */
118
+ export function readMediaSource(report: Iterable<Record<string, unknown>>): { level: number | null; fps: number | null } {
119
+ let level: number | null = null;
120
+ let fps: number | null = null;
121
+ for (const s of report) {
122
+ if (s.type !== 'media-source') continue;
123
+ if (typeof s.audioLevel === 'number') level = s.audioLevel;
124
+ if (typeof s.framesPerSecond === 'number') fps = s.framesPerSecond;
125
+ }
126
+ return { level, fps };
127
+ }