From e36739b66dce75ffbe25711a05c5a4e620a60868 Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Thu, 17 Aug 2023 12:13:14 +0200 Subject: [PATCH] voice - some cleanup and :lipstick: --- src/vs/base/common/marshallingIds.ts | 3 +-- src/vs/base/parts/ipc/node/ipc.mp.ts | 4 ++-- .../sharedProcess/contrib/voiceTranscriber.ts | 1 + .../code/node/sharedProcess/sharedProcessMain.ts | 6 +++--- .../sharedProcess/common/sharedProcess.ts | 12 ++++++------ .../sharedProcess/electron-main/sharedProcess.ts | 6 +++--- .../electron-sandbox/sharedProcessService.ts | 8 ++++---- .../electron-sandbox/bufferedVoiceTranscriber.js | 15 +++++++-------- .../workbenchVoiceRecognitionService.ts | 6 +++--- 9 files changed, 30 insertions(+), 31 deletions(-) diff --git a/src/vs/base/common/marshallingIds.ts b/src/vs/base/common/marshallingIds.ts index 7cb27d18f36..abd7698ed92 100644 --- a/src/vs/base/common/marshallingIds.ts +++ b/src/vs/base/common/marshallingIds.ts @@ -20,6 +20,5 @@ export const enum MarshalledId { NotebookCellActionContext, NotebookActionContext, TestItemContext, - Date, - Float32Array + Date } diff --git a/src/vs/base/parts/ipc/node/ipc.mp.ts b/src/vs/base/parts/ipc/node/ipc.mp.ts index 1a8b107dc99..b1648d260d6 100644 --- a/src/vs/base/parts/ipc/node/ipc.mp.ts +++ b/src/vs/base/parts/ipc/node/ipc.mp.ts @@ -43,7 +43,7 @@ export interface IClientConnectionFilter { * @returns `true` if the event was handled * and should not be processed by the server. */ - handled(e: MessageEvent): boolean; + handledClientConnection(e: MessageEvent): boolean; } /** @@ -58,7 +58,7 @@ export class Server extends IPCServer { const onCreateMessageChannel = new Emitter(); process.parentPort.on('message', (e: MessageEvent) => { - if (filter?.handled(e)) { + if (filter?.handledClientConnection(e)) { return; } diff --git a/src/vs/code/node/sharedProcess/contrib/voiceTranscriber.ts b/src/vs/code/node/sharedProcess/contrib/voiceTranscriber.ts index cbd23de96c4..e195f2b8b7a 100644 --- a/src/vs/code/node/sharedProcess/contrib/voiceTranscriber.ts +++ b/src/vs/code/node/sharedProcess/contrib/voiceTranscriber.ts @@ -35,6 +35,7 @@ export class VoiceTranscriber extends Disposable { this._register(toDisposable(() => port.off('message', portHandler))); port.start(); + this._register(toDisposable(() => port.close())); })); } } diff --git a/src/vs/code/node/sharedProcess/sharedProcessMain.ts b/src/vs/code/node/sharedProcess/sharedProcessMain.ts index 91e93fccc59..63b36ab8b0f 100644 --- a/src/vs/code/node/sharedProcess/sharedProcessMain.ts +++ b/src/vs/code/node/sharedProcess/sharedProcessMain.ts @@ -117,7 +117,7 @@ import { NativeEnvironmentService } from 'vs/platform/environment/node/environme import { IVoiceRecognitionService } from 'vs/platform/voiceRecognition/common/voiceRecognitionService'; import { VoiceRecognitionService } from 'vs/platform/voiceRecognition/node/voiceRecognitionService'; import { VoiceTranscriber } from 'vs/code/node/sharedProcess/contrib/voiceTranscriber'; -import { RawSharedProcessConnection, SharedProcessLifecycle } from 'vs/platform/sharedProcess/common/sharedProcess'; +import { SharedProcessRawConnection, SharedProcessLifecycle } from 'vs/platform/sharedProcess/common/sharedProcess'; class SharedProcessMain extends Disposable implements IClientConnectionFilter { @@ -439,14 +439,14 @@ class SharedProcessMain extends Disposable implements IClientConnectionFilter { }); } - handled(e: MessageEvent): boolean { + handledClientConnection(e: MessageEvent): boolean { // This filter on message port messages will look for // attempts of a window to connect raw to the shared // process to handle these connections separate from // our IPC based protocol. - if (e.data !== RawSharedProcessConnection.response) { + if (e.data !== SharedProcessRawConnection.response) { return false; } diff --git a/src/vs/platform/sharedProcess/common/sharedProcess.ts b/src/vs/platform/sharedProcess/common/sharedProcess.ts index 7e443fffb12..5b7e60efc80 100644 --- a/src/vs/platform/sharedProcess/common/sharedProcess.ts +++ b/src/vs/platform/sharedProcess/common/sharedProcess.ts @@ -9,12 +9,12 @@ export const SharedProcessLifecycle = { initDone: 'vscode:shared-process->electron-main=init-done' }; -export const ChannelSharedProcessConnection = { - request: 'vscode:createChannelSharedProcessConnection', - response: 'vscode:createChannelSharedProcessConnectionResult' +export const SharedProcessChannelConnection = { + request: 'vscode:createSharedProcessChannelConnection', + response: 'vscode:createSharedProcessChannelConnectionResult' }; -export const RawSharedProcessConnection = { - request: 'vscode:createRawSharedProcessConnection', - response: 'vscode:createRawSharedProcessConnectionResult' +export const SharedProcessRawConnection = { + request: 'vscode:createSharedProcessRawConnection', + response: 'vscode:createSharedProcessRawConnectionResult' }; diff --git a/src/vs/platform/sharedProcess/electron-main/sharedProcess.ts b/src/vs/platform/sharedProcess/electron-main/sharedProcess.ts index ec41ba8cc7a..c11fa7d0fc5 100644 --- a/src/vs/platform/sharedProcess/electron-main/sharedProcess.ts +++ b/src/vs/platform/sharedProcess/electron-main/sharedProcess.ts @@ -18,7 +18,7 @@ import { UtilityProcess } from 'vs/platform/utilityProcess/electron-main/utility import { NullTelemetryService } from 'vs/platform/telemetry/common/telemetryUtils'; import { parseSharedProcessDebugPort } from 'vs/platform/environment/node/environmentService'; import { assertIsDefined } from 'vs/base/common/types'; -import { ChannelSharedProcessConnection, RawSharedProcessConnection, SharedProcessLifecycle } from 'vs/platform/sharedProcess/common/sharedProcess'; +import { SharedProcessChannelConnection, SharedProcessRawConnection, SharedProcessLifecycle } from 'vs/platform/sharedProcess/common/sharedProcess'; export class SharedProcess extends Disposable { @@ -43,10 +43,10 @@ export class SharedProcess extends Disposable { private registerListeners(): void { // Shared process channel connections from workbench windows - validatedIpcMain.on(ChannelSharedProcessConnection.request, (e, nonce: string) => this.onWindowConnection(e, nonce, ChannelSharedProcessConnection.response)); + validatedIpcMain.on(SharedProcessChannelConnection.request, (e, nonce: string) => this.onWindowConnection(e, nonce, SharedProcessChannelConnection.response)); // Shared process raw connections from workbench windows - validatedIpcMain.on(RawSharedProcessConnection.request, (e, nonce: string) => this.onWindowConnection(e, nonce, RawSharedProcessConnection.response)); + validatedIpcMain.on(SharedProcessRawConnection.request, (e, nonce: string) => this.onWindowConnection(e, nonce, SharedProcessRawConnection.response)); // Lifecycle this._register(this.lifecycleMainService.onWillShutdown(() => this.onWillShutdown())); diff --git a/src/vs/workbench/services/sharedProcess/electron-sandbox/sharedProcessService.ts b/src/vs/workbench/services/sharedProcess/electron-sandbox/sharedProcessService.ts index 11a2fd6a72b..fffa5df2feb 100644 --- a/src/vs/workbench/services/sharedProcess/electron-sandbox/sharedProcessService.ts +++ b/src/vs/workbench/services/sharedProcess/electron-sandbox/sharedProcessService.ts @@ -8,7 +8,7 @@ import { IChannel, IServerChannel, getDelayedChannel } from 'vs/base/parts/ipc/c import { ILogService } from 'vs/platform/log/common/log'; import { Disposable } from 'vs/base/common/lifecycle'; import { ISharedProcessService } from 'vs/platform/ipc/electron-sandbox/services'; -import { ChannelSharedProcessConnection, RawSharedProcessConnection } from 'vs/platform/sharedProcess/common/sharedProcess'; +import { SharedProcessChannelConnection, SharedProcessRawConnection } from 'vs/platform/sharedProcess/common/sharedProcess'; import { mark } from 'vs/base/common/performance'; import { Barrier, timeout } from 'vs/base/common/async'; import { acquirePort } from 'vs/base/parts/ipc/electron-sandbox/ipc.mp'; @@ -45,7 +45,7 @@ export class SharedProcessService extends Disposable implements ISharedProcessSe // Acquire a message port connected to the shared process mark('code/willConnectSharedProcess'); this.logService.trace('Renderer->SharedProcess#connect: before acquirePort'); - const port = await acquirePort(ChannelSharedProcessConnection.request, ChannelSharedProcessConnection.response); + const port = await acquirePort(SharedProcessChannelConnection.request, SharedProcessChannelConnection.response); mark('code/didConnectSharedProcess'); this.logService.trace('Renderer->SharedProcess#connect: connection established'); @@ -69,11 +69,11 @@ export class SharedProcessService extends Disposable implements ISharedProcessSe async createRawConnection(): Promise { // Await initialization of the shared process - await this.connect(); + await this.withSharedProcessConnection; // Create a new port to the shared process this.logService.trace('Renderer->SharedProcess#createRawConnection: before acquirePort'); - const port = await acquirePort(RawSharedProcessConnection.request, RawSharedProcessConnection.response); + const port = await acquirePort(SharedProcessRawConnection.request, SharedProcessRawConnection.response); this.logService.trace('Renderer->SharedProcess#createRawConnection: connection established'); return port; diff --git a/src/vs/workbench/services/voiceRecognition/electron-sandbox/bufferedVoiceTranscriber.js b/src/vs/workbench/services/voiceRecognition/electron-sandbox/bufferedVoiceTranscriber.js index 3e4d6641c22..7b99644fa3c 100644 --- a/src/vs/workbench/services/voiceRecognition/electron-sandbox/bufferedVoiceTranscriber.js +++ b/src/vs/workbench/services/voiceRecognition/electron-sandbox/bufferedVoiceTranscriber.js @@ -13,7 +13,7 @@ class BufferedVoiceTranscriber extends AudioWorkletProcessor { super(); this.channelCount = 1; - this.bufferTimespan = 4000; + this.bufferTimespan = 2000; this.startTime = undefined; this.allInputFloat32Array = undefined; @@ -27,16 +27,16 @@ class BufferedVoiceTranscriber extends AudioWorkletProcessor { // @ts-ignore const port = this.port; port.onmessage = event => { - if (event.data === 'vscode:transferPortToAudioWorklet') { - this.sharedProcessPort = event.ports[0]; + if (event.data === 'vscode:transferSharedProcessConnection') { + this.sharedProcessConnection = event.ports[0]; - this.sharedProcessPort.onmessage = event => { + this.sharedProcessConnection.onmessage = event => { if (typeof event.data === 'string') { port.postMessage(event.data); } }; - this.sharedProcessPort.start(); + this.sharedProcessConnection.start(); } }; } @@ -56,14 +56,13 @@ class BufferedVoiceTranscriber extends AudioWorkletProcessor { this.currentInputFloat32Arrays.push(inputChannelData.slice(0)); - if (Date.now() - this.startTime > this.bufferTimespan && this.sharedProcessPort) { + if (Date.now() - this.startTime > this.bufferTimespan && this.sharedProcessConnection) { const currentInputFloat32Arrays = this.currentInputFloat32Arrays; this.currentInputFloat32Arrays = []; this.allInputFloat32Array = this.joinFloat32Arrays(this.allInputFloat32Array ? [this.allInputFloat32Array, ...currentInputFloat32Arrays] : currentInputFloat32Arrays); - // @ts-ignore - this.sharedProcessPort.postMessage(this.allInputFloat32Array); + this.sharedProcessConnection.postMessage(this.allInputFloat32Array); this.startTime = Date.now(); } diff --git a/src/vs/workbench/services/voiceRecognition/electron-sandbox/workbenchVoiceRecognitionService.ts b/src/vs/workbench/services/voiceRecognition/electron-sandbox/workbenchVoiceRecognitionService.ts index 523b07d1066..4ccbde62b10 100644 --- a/src/vs/workbench/services/voiceRecognition/electron-sandbox/workbenchVoiceRecognitionService.ts +++ b/src/vs/workbench/services/voiceRecognition/electron-sandbox/workbenchVoiceRecognitionService.ts @@ -50,10 +50,10 @@ class BufferedVoiceTranscriber extends AudioWorkletNode { } async start(token: CancellationToken): Promise { - const rawSharedProcessConnection = await this.sharedProcessService.createRawConnection(); - token.onCancellationRequested(() => rawSharedProcessConnection.close()); + const sharedProcessConnection = await this.sharedProcessService.createRawConnection(); + token.onCancellationRequested(() => sharedProcessConnection.close()); - this.port.postMessage('vscode:transferPortToAudioWorklet', [rawSharedProcessConnection]); + this.port.postMessage('vscode:transferSharedProcessConnection', [sharedProcessConnection]); } }