Skip to content

Commit

Permalink
webrtc: add connection timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Koushik Dutta authored and Koushik Dutta committed Mar 17, 2024
1 parent d920331 commit 5b1889e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 32 deletions.
24 changes: 11 additions & 13 deletions plugins/webrtc/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 22 additions & 16 deletions plugins/webrtc/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -678,24 +678,11 @@ class WebRTCBridge extends ScryptedDeviceBase implements BufferConverter {
this.toMimeType = ScryptedMimeTypes.RTCConnectionManagement;
}

async convert(data: any, fromMimeType: string, toMimeType: string, options?: MediaObjectOptions): Promise<any> {
async convertInternal(result: ReturnType<typeof zygote>, cleanup: Deferred<string>, data: any, fromMimeType: string, toMimeType: string, options?: MediaObjectOptions): Promise<any> {
const session = data as RTCSignalingSession;
const maximumCompatibilityMode = !!this.plugin.storageSettings.values.maximumCompatibilityMode;
const clientOptions = await legacyGetSignalingSessionOptions(session);

const result = zygote();

const cleanup = new Deferred<string>();

this.plugin.activeConnections++;
result.worker.on('exit', () => {
this.plugin.activeConnections--;
cleanup.resolve('worker exited');
});
cleanup.promise.finally(() => {
result.worker.terminate()
});

const { createConnection } = await result.result;
const connection = await createConnection({}, undefined, session,
maximumCompatibilityMode,
Expand All @@ -711,10 +698,29 @@ class WebRTCBridge extends ScryptedDeviceBase implements BufferConverter {
await connection.negotiateRTCSignalingSession();
await connection.waitConnected();

// await connection.negotiateRTCSignalingSession();

return connection;
}

async convert(data: any, fromMimeType: string, toMimeType: string, options?: MediaObjectOptions): Promise<any> {
const result = zygote();
this.plugin.activeConnections++;
const cleanup = new Deferred<string>();
result.worker.on('exit', () => {
this.plugin.activeConnections--;
cleanup.resolve('worker exited');
});
cleanup.promise.finally(() => {
result.worker.terminate()
});

try {
return await timeoutPromise(2 * 60 * 1000, this.convertInternal(result, cleanup, data, fromMimeType, toMimeType, options));
}
catch (e) {
cleanup.resolve(e.toString());
throw e;
}
}
}

export default WebRTCPlugin;
10 changes: 7 additions & 3 deletions plugins/webrtc/src/peerconnection-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ export function waitIceConnected(pc: RTCPeerConnection) {
}

export function waitClosed(pc: RTCPeerConnection) {
return statePromise(pc.connectionStateChange, () => {
return isPeerConnectionClosed(pc) || isPeerIceConnectionClosed(pc);
})
const connectPromise = statePromise(pc.connectionStateChange, () => {
return isPeerConnectionClosed(pc);
});
const iceConnectPromise = statePromise(pc.iceConnectionStateChange, () => {
return isPeerIceConnectionClosed(pc);
});
return Promise.any([connectPromise, iceConnectPromise]);
}

export function logConnectionState(console: Console, pc: RTCPeerConnection) {
Expand Down

0 comments on commit 5b1889e

Please sign in to comment.