From 148da12eb589f09647a627e13cd4fa3be3b5d50c Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Wed, 14 Sep 2022 20:10:34 -0700 Subject: [PATCH] webrtc: refactor fixes --- plugins/webrtc/.vscode/settings.json | 2 +- plugins/webrtc/package-lock.json | 4 ++-- plugins/webrtc/package.json | 2 +- plugins/webrtc/src/ffmpeg-to-wrtc.ts | 4 +++- plugins/webrtc/src/main.ts | 24 ++++++++++++++--------- plugins/webrtc/src/peerconnection-util.ts | 4 ++-- plugins/webrtc/src/werift-util.ts | 1 + 7 files changed, 25 insertions(+), 16 deletions(-) diff --git a/plugins/webrtc/.vscode/settings.json b/plugins/webrtc/.vscode/settings.json index 77ccdbd6d..25c90c9be 100644 --- a/plugins/webrtc/.vscode/settings.json +++ b/plugins/webrtc/.vscode/settings.json @@ -1,4 +1,4 @@ { - "scrypted.debugHost": "127.0.0.1", + "scrypted.debugHost": "koushik-mac", } \ No newline at end of file diff --git a/plugins/webrtc/package-lock.json b/plugins/webrtc/package-lock.json index 96c3c6684..28f4d6fb0 100644 --- a/plugins/webrtc/package-lock.json +++ b/plugins/webrtc/package-lock.json @@ -1,12 +1,12 @@ { "name": "@scrypted/webrtc", - "version": "0.0.68", + "version": "0.0.69", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@scrypted/webrtc", - "version": "0.0.68", + "version": "0.0.69", "dependencies": { "@koush/werift": "file:../../external/werift/packages/webrtc", "@scrypted/common": "file:../../common", diff --git a/plugins/webrtc/package.json b/plugins/webrtc/package.json index 291fde281..67fb0115c 100644 --- a/plugins/webrtc/package.json +++ b/plugins/webrtc/package.json @@ -1,6 +1,6 @@ { "name": "@scrypted/webrtc", - "version": "0.0.68", + "version": "0.0.69", "scripts": { "scrypted-setup-project": "scrypted-setup-project", "prescrypted-setup-project": "scrypted-package-json", diff --git a/plugins/webrtc/src/ffmpeg-to-wrtc.ts b/plugins/webrtc/src/ffmpeg-to-wrtc.ts index 9c090e6ef..efa32c49b 100644 --- a/plugins/webrtc/src/ffmpeg-to-wrtc.ts +++ b/plugins/webrtc/src/ffmpeg-to-wrtc.ts @@ -400,9 +400,11 @@ export class WebRTCConnectionManagement implements RTCConnectionManagement { const ret = new WebRTCTrack(this, videoTransceiver, audioTransceiver, this.options?.disableIntercom ? undefined : intercom); this.negotiation.then(async () => { + this.console.log('waiting ice connected'); + await waitIceConnected(this.pc); if (ret.removed.finished) return; - await waitIceConnected(this.pc); + this.console.log('done waiting ice connected'); const f = await createTrackForwarder(videoTransceiver, audioTransceiver); waitClosed(this.pc).finally(() => f.kill()); ret.removed.promise.finally(() => f.kill()); diff --git a/plugins/webrtc/src/main.ts b/plugins/webrtc/src/main.ts index 28a2670cf..ad919b652 100644 --- a/plugins/webrtc/src/main.ts +++ b/plugins/webrtc/src/main.ts @@ -6,12 +6,13 @@ import { createBrowserSignalingSession } from "@scrypted/common/src/rtc-connect" import { connectRTCSignalingClients } from '@scrypted/common/src/rtc-signaling'; import { StorageSettings } from '@scrypted/common/src/settings'; import { SettingsMixinDeviceBase, SettingsMixinDeviceOptions } from '@scrypted/common/src/settings-mixin'; +import { sleep } from '@scrypted/common/src/sleep'; import sdk, { BufferConverter, BufferConvertorOptions, DeviceCreator, DeviceCreatorSettings, DeviceProvider, FFmpegInput, HttpRequest, Intercom, MediaObject, MixinProvider, RequestMediaStream, RequestMediaStreamOptions, ResponseMediaStreamOptions, RTCAVSignalingSetup, RTCSessionControl, RTCSignalingChannel, RTCSignalingSession, ScryptedDeviceType, ScryptedInterface, ScryptedMimeTypes, Setting, Settings, SettingValue, VideoCamera } from '@scrypted/sdk'; import crypto from 'crypto'; import net from 'net'; import { DataChannelDebouncer } from './datachannel-debouncer'; import { createRTCPeerConnectionSink, parseOptions, RTC_BRIDGE_NATIVE_ID, WebRTCBridge, WebRTCConnectionManagement } from "./ffmpeg-to-wrtc"; -import { stunIceServers } from './ice-servers'; +import { stunIceServers, stunServer } from './ice-servers'; import { waitClosed, waitConnected, waitIceConnected } from './peerconnection-util'; import { WebRTCCamera } from "./webrtc-camera"; import { WeriftSignalingSession } from './werift-signaling-session'; @@ -301,7 +302,7 @@ export class WebRTCPlugin extends AutoenableMixinProvider implements DeviceCreat } async onConnection(request: HttpRequest, webSocketUrl: string) { - const cleanup = new Deferred(); + const cleanup = new Deferred(); cleanup.promise.catch(e => this.console.log('cleaning up rtc connection:', e.message)); const ws = new WebSocket(webSocketUrl); @@ -325,7 +326,7 @@ export class WebRTCPlugin extends AutoenableMixinProvider implements DeviceCreat const close = () => { const str = 'Connection closed while waiting for message'; reject(new Error(str)); - cleanup.reject(new Error(str)); + cleanup.resolve(str); }; ws.addEventListener('close', close); @@ -343,7 +344,11 @@ export class WebRTCPlugin extends AutoenableMixinProvider implements DeviceCreat this.storageSettings.values.maximumCompatibilityMode, transcodeWidth, sessionSupportsH264High, { setup: { configuration: { - iceServers: stunIceServers, + iceServers: [ + // seemingly this is faster than google which may have throttling on requests? + // unsure, but leaving this as is. + stunServer, + ], }, } }); @@ -358,17 +363,18 @@ export class WebRTCPlugin extends AutoenableMixinProvider implements DeviceCreat const { pc } = connection; const dc = pc.createDataChannel('rpc'); - waitClosed(pc).then(() => cleanup.reject(new Error('peer connection closed'))); + waitClosed(pc).then(() => cleanup.resolve('peer connection closed')); const start = Date.now(); await connection.negotiateRTCSignalingSession(); - await waitIceConnected(pc); + // await waitIceConnected(pc); + // await sleep(5000); // const [dc] = await dcPromise; dc.message.subscribe(message => socket.write(message)); const cp = await client.clientPromise; - cp.on('close', () => cleanup.reject(new Error('socket client closed'))); + cp.on('close', () => cleanup.resolve('socket client closed')); process.send(message, cp); const debouncer = new DataChannelDebouncer({ @@ -378,11 +384,11 @@ export class WebRTCPlugin extends AutoenableMixinProvider implements DeviceCreat socket.destroy(); }); socket.on('data', data => debouncer.send(data)); - socket.on('close', () => cleanup.reject(new Error('socket closed'))); + socket.on('close', () => cleanup.resolve('socket closed')); } catch (e) { console.error("error negotiating browser RTCC signaling", e); - cleanup.reject(e); + cleanup.resolve('error'); throw e; } } diff --git a/plugins/webrtc/src/peerconnection-util.ts b/plugins/webrtc/src/peerconnection-util.ts index 19a9cbe29..8515850f3 100644 --- a/plugins/webrtc/src/peerconnection-util.ts +++ b/plugins/webrtc/src/peerconnection-util.ts @@ -31,7 +31,7 @@ export function waitClosed(pc: RTCPeerConnection) { console.log('iceGatheringStateChange', pc.iceGatheringState); }); pc.iceConnectionStateChange.subscribe(() => { - console.log('iceConnectionStateChange', pc.connectionState, pc.iceConnectionState); + console.log('iceConnectionStateChange', pc.iceConnectionState); if (pc.iceConnectionState === 'disconnected' || pc.iceConnectionState === 'failed' || pc.iceConnectionState === 'closed') { @@ -39,7 +39,7 @@ export function waitClosed(pc: RTCPeerConnection) { } }); pc.connectionStateChange.subscribe(() => { - console.log('connectionStateChange', pc.connectionState, pc.iceConnectionState); + console.log('connectionStateChange', pc.iceConnectionState); if (pc.connectionState === 'closed' || pc.connectionState === 'disconnected' || pc.connectionState === 'failed') { diff --git a/plugins/webrtc/src/werift-util.ts b/plugins/webrtc/src/werift-util.ts index cf1795ffa..624a8f10d 100644 --- a/plugins/webrtc/src/werift-util.ts +++ b/plugins/webrtc/src/werift-util.ts @@ -43,6 +43,7 @@ export function getWeriftIceServers(configuration: RTCConfiguration): RTCIceServ } export function logIsPrivateIceTransport(console: Console, pc: RTCPeerConnection) { + return true; let isPrivate = true; for (const ice of pc.iceTransports) { const [address, port] = ice.connection.remoteAddr;