Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

fix(meeting): stop meeting detection firing on our own dictation (#1570)

Gabriel Steinf879ebcb72fd
diff --git a/src/helpers/audioActivityDetector.js b/src/helpers/audioActivityDetector.js
index 3fd7189ff..7f63f9862 100644
--- a/src/helpers/audioActivityDetector.js
+++ b/src/helpers/audioActivityDetector.js
@@ -3,6 +3,7 @@ const { promisify } = require("util");
const EventEmitter = require("events");
const debugLogger = require("./debugLogger");
const { resolveBundledBinary } = require("./binaryResolver");
+const { getOwnProcessPids } = require("./ownProcessPids");
const execAsync = promisify(exec);
@@ -325,6 +326,10 @@ class AudioActivityDetector extends EventEmitter {
const startMatch = line.match(/^MIC_START\s+(\d+)$/);
if (startMatch) {
const pid = parseInt(startMatch[1], 10);
+ // --exclude-pid only covers the main process, but dictation captures from
+ // Chromium's audio service, so our own mic reads arrive here as if they
+ // were another app's (#1392).
+ if (getOwnProcessPids().has(pid)) return;
this._activeMicPids.add(pid);
this._onMicStateChanged(true);
return;
diff --git a/src/helpers/ownProcessPids.js b/src/helpers/ownProcessPids.js
new file mode 100644
index 000000000..6f925a80f
--- /dev/null
+++ b/src/helpers/ownProcessPids.js
@@ -0,0 +1,20 @@
+// Every PID OpenWhispr is currently running under, main process included.
+//
+// The Windows mic listener reports the PID that opened a capture session, and
+// dictation opens the mic from Chromium's audio service — a child process — so
+// the single --exclude-pid the helper is given can never match it. Without this
+// the app detects its own dictation as third-party mic activity (#1392).
+function getOwnProcessPids() {
+ const pids = new Set([process.pid]);
+ try {
+ const { app } = require("electron");
+ for (const metric of app.getAppMetrics()) {
+ if (typeof metric?.pid === "number") pids.add(metric.pid);
+ }
+ } catch {
+ // Outside Electron, or before the app is ready: the main PID is all we have.
+ }
+ return pids;
+}
+
+module.exports = { getOwnProcessPids };
diff --git a/test/helpers/audioActivityDetector.test.js b/test/helpers/audioActivityDetector.test.js
index 87a91bef7..a5746130a 100644
--- a/test/helpers/audioActivityDetector.test.js
+++ b/test/helpers/audioActivityDetector.test.js
@@ -17,7 +17,7 @@ function setPlatform(platform) {
afterEach(() => setPlatform(originalPlatform));
-function loadDetector(platform, spawn) {
+function loadDetector(platform, spawn, ownPids) {
delete require.cache[detectorModulePath];
setPlatform(platform);
@@ -34,6 +34,11 @@ function loadDetector(platform, spawn) {
if (request === "./binaryResolver") {
return { resolveBundledBinary: (name) => `/fake/bin/${name}` };
}
+ // Outside Electron the real module can only see the main pid, so the
+ // child-process PIDs that matter for #1392 have to be injected.
+ if (request === "./ownProcessPids" && ownPids) {
+ return { getOwnProcessPids: () => new Set(ownPids) };
+ }
return originalLoad.call(this, request, parent, isMain);
};
@@ -63,15 +68,19 @@ function createFakeChild(spawnError) {
return child;
}
-function createDetector(platform, { spawnError } = {}) {
+function createDetector(platform, { spawnError, ownPids } = {}) {
const children = [];
const calls = [];
- const AudioActivityDetector = loadDetector(platform, (command, args, options) => {
- calls.push({ command, args, options });
- const child = createFakeChild(spawnError);
- children.push(child);
- return child;
- });
+ const AudioActivityDetector = loadDetector(
+ platform,
+ (command, args, options) => {
+ calls.push({ command, args, options });
+ const child = createFakeChild(spawnError);
+ children.push(child);
+ return child;
+ },
+ ownPids
+ );
const detector = new AudioActivityDetector();
detector._isMicActive = async () => false;
@@ -370,3 +379,51 @@ test("win32: an unrelated app's mic session ending does not hide an ongoing dism
assert.equal(emitted.length, 2, "the still-running call must re-prompt after the cooldown");
detector.stop();
});
+
+// #1392: the helper is given a single --exclude-pid for the main process, but
+// dictation opens the mic from Chromium's audio service, so OpenWhispr's own
+// capture is reported back to us under a child PID and read as a meeting.
+test("win32: a mic session from one of our own child processes is ignored", async () => {
+ const AUDIO_SERVICE_PID = 4242;
+ const { detector, children } = createDetector("win32", {
+ ownPids: [process.pid, AUDIO_SERVICE_PID],
+ });
+
+ await detector.start();
+ children[0].stdout.emit("data", `MIC_START ${AUDIO_SERVICE_PID}\n`);
+
+ assert.equal(detector._activeMicPids.size, 0);
+ assert.equal(detector._sustainedTimer, null, "our own dictation must not arm detection");
+ assert.equal(detector._lastKnownMicState, false, "and must not leave stale state behind");
+ detector.stop();
+});
+
+test("win32: a mic session from another application is still detected", async () => {
+ const { detector, children } = createDetector("win32", {
+ ownPids: [process.pid, 4242],
+ });
+
+ await detector.start();
+ children[0].stdout.emit("data", "MIC_START 9001\n");
+
+ assert.deepEqual([...detector._activeMicPids], [9001]);
+ assert.notEqual(detector._sustainedTimer, null);
+ detector.stop();
+});
+
+test("win32: our own capture cannot cancel a real meeting already in progress", async () => {
+ const AUDIO_SERVICE_PID = 4242;
+ const { detector, children } = createDetector("win32", {
+ ownPids: [process.pid, AUDIO_SERVICE_PID],
+ });
+
+ await detector.start();
+ children[0].stdout.emit("data", "MIC_START 9001\n");
+ children[0].stdout.emit("data", `MIC_START ${AUDIO_SERVICE_PID}\n`);
+ children[0].stdout.emit("data", `MIC_STOP ${AUDIO_SERVICE_PID}\n`);
+
+ // Dropping our own stop must not empty the set while the other app holds it.
+ assert.deepEqual([...detector._activeMicPids], [9001]);
+ assert.notEqual(detector._sustainedTimer, null);
+ detector.stop();
+});