@sdeck/plugin-audio 0.0.0-stage → 0.1.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.
- package/bin/sdeck-audio.exe +0 -0
- package/dist/helper.d.ts +95 -0
- package/dist/helper.d.ts.map +1 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1466 -0
- package/dist/index.js.map +22 -0
- package/native/Cargo.lock +230 -0
- package/native/Cargo.toml +29 -0
- package/native/src/main.rs +28 -0
- package/native/src/policy.rs +41 -0
- package/native/src/protocol.rs +132 -0
- package/native/src/windows_audio.rs +609 -0
- package/package.json +65 -4
- package/src/helper.ts +270 -0
- package/src/index.tsx +541 -0
- package/README.md +0 -3
package/src/helper.ts
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Talks to `sdeck-audio`, the helper process that reaches the system's audio devices. It runs
|
|
3
|
+
* only while something needs it, and is restarted if it stops on its own.
|
|
4
|
+
*/
|
|
5
|
+
import { type ChildProcess, spawn } from "node:child_process";
|
|
6
|
+
import { existsSync } from "node:fs";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
|
|
9
|
+
export interface Level {
|
|
10
|
+
/** 0–100. */
|
|
11
|
+
volume: number;
|
|
12
|
+
muted: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface AudioDevice extends Level {
|
|
16
|
+
id: string;
|
|
17
|
+
name: string;
|
|
18
|
+
default: boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface AudioApp extends Level {
|
|
22
|
+
/** Executable name, lowercased (`system` for system sounds). */
|
|
23
|
+
name: string;
|
|
24
|
+
active: boolean;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface Snapshot {
|
|
28
|
+
outputs: AudioDevice[];
|
|
29
|
+
inputs: AudioDevice[];
|
|
30
|
+
apps: AudioApp[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface Change {
|
|
34
|
+
before: Level;
|
|
35
|
+
after: Level;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export type Target =
|
|
39
|
+
| { kind: "output"; device?: string }
|
|
40
|
+
| { kind: "input"; device?: string }
|
|
41
|
+
| { kind: "app"; app: string };
|
|
42
|
+
|
|
43
|
+
export interface Requests {
|
|
44
|
+
snapshot: { params: undefined; result: Snapshot };
|
|
45
|
+
setVolume: { params: { target: Target; volume?: number; delta?: number }; result: Change };
|
|
46
|
+
setMute: { params: { target: Target; muted?: boolean }; result: Change };
|
|
47
|
+
setDefault: { params: { device: string }; result: { previous: string | null } };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Why there is no audio: shown to the user. */
|
|
51
|
+
export class AudioUnavailable extends Error {}
|
|
52
|
+
|
|
53
|
+
export const BUILD_HINT = "Build the audio helper with `bun run build:audio`";
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Where the helper is: `SDECK_AUDIO_BIN`, the cargo build next to this package, or `bin/` next to
|
|
57
|
+
* a bundled app.
|
|
58
|
+
*/
|
|
59
|
+
export function findHelper(
|
|
60
|
+
env: Record<string, string | undefined> = process.env,
|
|
61
|
+
base: string = import.meta.dir,
|
|
62
|
+
exists: (path: string) => boolean = existsSync,
|
|
63
|
+
): string | undefined {
|
|
64
|
+
const name = process.platform === "win32" ? "sdeck-audio.exe" : "sdeck-audio";
|
|
65
|
+
return [
|
|
66
|
+
env.SDECK_AUDIO_BIN,
|
|
67
|
+
join(base, "..", "native", "target", "release", name),
|
|
68
|
+
join(base, "..", "bin", name),
|
|
69
|
+
join(base, "..", "native", "target", "debug", name),
|
|
70
|
+
].find((path): path is string => Boolean(path) && exists(path as string));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
interface Pending {
|
|
74
|
+
resolve: (value: unknown) => void;
|
|
75
|
+
reject: (error: Error) => void;
|
|
76
|
+
timer: ReturnType<typeof setTimeout>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface AudioHelperOptions {
|
|
80
|
+
/** The command line to run; throw {@link AudioUnavailable} when there is none. */
|
|
81
|
+
command: () => string[];
|
|
82
|
+
/** The helper could not run, or stopped. `undefined`: it works again. */
|
|
83
|
+
onProblem?: (problem: Error | undefined) => void;
|
|
84
|
+
/** How long it keeps running after the last user leaves. */
|
|
85
|
+
lingerMs?: number;
|
|
86
|
+
timeoutMs?: number;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export class AudioHelper {
|
|
90
|
+
readonly #options: AudioHelperOptions;
|
|
91
|
+
readonly #listeners = new Set<(snapshot: Snapshot | undefined) => void>();
|
|
92
|
+
readonly #pending = new Map<number, Pending>();
|
|
93
|
+
#child: ChildProcess | undefined;
|
|
94
|
+
#snapshot: Snapshot | undefined;
|
|
95
|
+
#users = 0;
|
|
96
|
+
#next = 1;
|
|
97
|
+
#linger: ReturnType<typeof setTimeout> | undefined;
|
|
98
|
+
#restart: ReturnType<typeof setTimeout> | undefined;
|
|
99
|
+
#delay = 1000;
|
|
100
|
+
|
|
101
|
+
constructor(options: AudioHelperOptions) {
|
|
102
|
+
this.#options = options;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** The devices as last reported, while the helper runs. */
|
|
106
|
+
get snapshot(): Snapshot | undefined {
|
|
107
|
+
return this.#snapshot;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Whether the helper process is running. */
|
|
111
|
+
get running(): boolean {
|
|
112
|
+
return this.#child !== undefined;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Hears every snapshot while subscribed (which keeps the helper running). */
|
|
116
|
+
subscribe(listener: (snapshot: Snapshot | undefined) => void): () => void {
|
|
117
|
+
this.#listeners.add(listener);
|
|
118
|
+
const release = this.#acquire();
|
|
119
|
+
if (this.#snapshot) listener(this.#snapshot);
|
|
120
|
+
return () => {
|
|
121
|
+
this.#listeners.delete(listener);
|
|
122
|
+
release();
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async request<M extends keyof Requests>(
|
|
127
|
+
method: M,
|
|
128
|
+
params?: Requests[M]["params"],
|
|
129
|
+
): Promise<Requests[M]["result"]> {
|
|
130
|
+
const release = this.#acquire();
|
|
131
|
+
try {
|
|
132
|
+
const child = this.#child ?? this.#start();
|
|
133
|
+
const id = this.#next++;
|
|
134
|
+
return (await new Promise<unknown>((resolve, reject) => {
|
|
135
|
+
const timer = setTimeout(() => {
|
|
136
|
+
this.#pending.delete(id);
|
|
137
|
+
reject(new Error("the audio helper did not answer"));
|
|
138
|
+
}, this.#options.timeoutMs ?? 5000);
|
|
139
|
+
this.#pending.set(id, { resolve, reject, timer });
|
|
140
|
+
child.stdin?.write(`${JSON.stringify({ id, method, params })}\n`);
|
|
141
|
+
})) as Requests[M]["result"];
|
|
142
|
+
} finally {
|
|
143
|
+
release();
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Stops the helper now, whoever uses it. */
|
|
148
|
+
close(): void {
|
|
149
|
+
this.#users = 0;
|
|
150
|
+
this.#listeners.clear();
|
|
151
|
+
clearTimeout(this.#linger);
|
|
152
|
+
this.#stop();
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
#acquire(): () => void {
|
|
156
|
+
this.#users++;
|
|
157
|
+
clearTimeout(this.#linger);
|
|
158
|
+
if (!this.#child && !this.#restart) {
|
|
159
|
+
try {
|
|
160
|
+
this.#start();
|
|
161
|
+
} catch (error) {
|
|
162
|
+
this.#options.onProblem?.(error as Error);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
let released = false;
|
|
166
|
+
return () => {
|
|
167
|
+
if (released) return;
|
|
168
|
+
released = true;
|
|
169
|
+
this.#users--;
|
|
170
|
+
if (this.#users > 0) return;
|
|
171
|
+
this.#linger = setTimeout(() => this.#stop(), this.#options.lingerMs ?? 3000);
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
#start(): ChildProcess {
|
|
176
|
+
clearTimeout(this.#restart);
|
|
177
|
+
this.#restart = undefined;
|
|
178
|
+
const [file, ...args] = this.#options.command();
|
|
179
|
+
if (!file) throw new AudioUnavailable(BUILD_HINT);
|
|
180
|
+
const child = spawn(file, args, { stdio: ["pipe", "pipe", "pipe"], windowsHide: true });
|
|
181
|
+
this.#child = child;
|
|
182
|
+
let buffer = "";
|
|
183
|
+
let errors = "";
|
|
184
|
+
child.stdout?.setEncoding("utf8");
|
|
185
|
+
child.stdout?.on("data", (chunk: string) => {
|
|
186
|
+
buffer += chunk;
|
|
187
|
+
const lines = buffer.split("\n");
|
|
188
|
+
buffer = lines.pop() ?? "";
|
|
189
|
+
for (const line of lines) this.#receive(line);
|
|
190
|
+
});
|
|
191
|
+
child.stderr?.setEncoding("utf8");
|
|
192
|
+
child.stderr?.on("data", (chunk: string) => {
|
|
193
|
+
errors = (errors + chunk).slice(-2000);
|
|
194
|
+
});
|
|
195
|
+
// Writing to a helper that just died must not crash the host.
|
|
196
|
+
child.stdin?.on("error", () => {});
|
|
197
|
+
const ended = (reason: string) => {
|
|
198
|
+
if (this.#child !== child) return;
|
|
199
|
+
this.#child = undefined;
|
|
200
|
+
this.#fail(new Error(reason));
|
|
201
|
+
this.#publish(undefined);
|
|
202
|
+
this.#options.onProblem?.(new AudioUnavailable(reason));
|
|
203
|
+
if (this.#users <= 0) return;
|
|
204
|
+
this.#restart = setTimeout(() => {
|
|
205
|
+
this.#restart = undefined;
|
|
206
|
+
if (this.#users <= 0 || this.#child) return;
|
|
207
|
+
try {
|
|
208
|
+
this.#start();
|
|
209
|
+
} catch (error) {
|
|
210
|
+
this.#options.onProblem?.(error as Error);
|
|
211
|
+
}
|
|
212
|
+
}, this.#delay);
|
|
213
|
+
this.#delay = Math.min(this.#delay * 2, 30_000);
|
|
214
|
+
};
|
|
215
|
+
child.once("error", (error) => ended(`the audio helper cannot run: ${error.message}`));
|
|
216
|
+
child.once("close", (code) =>
|
|
217
|
+
ended(errors.trim() || `the audio helper stopped (exit ${code})`),
|
|
218
|
+
);
|
|
219
|
+
return child;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
#stop(): void {
|
|
223
|
+
clearTimeout(this.#restart);
|
|
224
|
+
this.#restart = undefined;
|
|
225
|
+
const child = this.#child;
|
|
226
|
+
this.#child = undefined;
|
|
227
|
+
this.#fail(new Error("the audio helper was stopped"));
|
|
228
|
+
this.#publish(undefined);
|
|
229
|
+
if (!child) return;
|
|
230
|
+
// Closing its input asks it to exit; insist if it does not.
|
|
231
|
+
child.stdin?.end();
|
|
232
|
+
const kill = setTimeout(() => child.kill(), 2000);
|
|
233
|
+
child.once("close", () => clearTimeout(kill));
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
#receive(line: string): void {
|
|
237
|
+
let message: { id?: number; result?: unknown; error?: string; event?: string; data?: unknown };
|
|
238
|
+
try {
|
|
239
|
+
message = JSON.parse(line);
|
|
240
|
+
} catch {
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
if (message.event === "snapshot") {
|
|
244
|
+
this.#delay = 1000;
|
|
245
|
+
this.#options.onProblem?.(undefined);
|
|
246
|
+
this.#publish(message.data as Snapshot);
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
const pending = this.#pending.get(message.id ?? -1);
|
|
250
|
+
if (!pending) return;
|
|
251
|
+
this.#pending.delete(message.id as number);
|
|
252
|
+
clearTimeout(pending.timer);
|
|
253
|
+
if (message.error !== undefined) pending.reject(new Error(message.error));
|
|
254
|
+
else pending.resolve(message.result);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
#publish(snapshot: Snapshot | undefined): void {
|
|
258
|
+
if (this.#snapshot === undefined && snapshot === undefined) return;
|
|
259
|
+
this.#snapshot = snapshot;
|
|
260
|
+
for (const listener of [...this.#listeners]) listener(snapshot);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
#fail(error: Error): void {
|
|
264
|
+
for (const pending of this.#pending.values()) {
|
|
265
|
+
clearTimeout(pending.timer);
|
|
266
|
+
pending.reject(error);
|
|
267
|
+
}
|
|
268
|
+
this.#pending.clear();
|
|
269
|
+
}
|
|
270
|
+
}
|