From 005efbfe82161ca56c50b6cc71ca96499a650eb3 Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Thu, 10 Oct 2024 10:27:07 -0700 Subject: [PATCH] webrtc: add flag that forces opus audio --- plugins/webrtc/.vscode/settings.json | 2 +- plugins/webrtc/package-lock.json | 4 +- plugins/webrtc/package.json | 2 +- plugins/webrtc/src/ffmpeg-to-wrtc.ts | 8 ++-- plugins/webrtc/src/main.ts | 46 ++++++++++++-------- plugins/webrtc/src/webrtc-required-codecs.ts | 8 ++++ 6 files changed, 46 insertions(+), 24 deletions(-) diff --git a/plugins/webrtc/.vscode/settings.json b/plugins/webrtc/.vscode/settings.json index 77ccdbd6d..a620593fa 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": "scrypted-nvr", } \ No newline at end of file diff --git a/plugins/webrtc/package-lock.json b/plugins/webrtc/package-lock.json index d9a13bfcb..adc0c9657 100644 --- a/plugins/webrtc/package-lock.json +++ b/plugins/webrtc/package-lock.json @@ -1,12 +1,12 @@ { "name": "@scrypted/webrtc", - "version": "0.2.52", + "version": "0.2.53", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@scrypted/webrtc", - "version": "0.2.52", + "version": "0.2.53", "dependencies": { "@scrypted/common": "file:../../common", "@scrypted/sdk": "file:../../sdk", diff --git a/plugins/webrtc/package.json b/plugins/webrtc/package.json index b499e6214..c211bcc47 100644 --- a/plugins/webrtc/package.json +++ b/plugins/webrtc/package.json @@ -1,6 +1,6 @@ { "name": "@scrypted/webrtc", - "version": "0.2.52", + "version": "0.2.53", "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 ab19575ef..8961b2259 100644 --- a/plugins/webrtc/src/ffmpeg-to-wrtc.ts +++ b/plugins/webrtc/src/ffmpeg-to-wrtc.ts @@ -3,7 +3,7 @@ import { MediaStreamTrack, PeerConfig, RTCPeerConnection, RTCRtpCodecParameters, import { Deferred } from "@scrypted/common/src/deferred"; import sdk, { FFmpegInput, FFmpegTranscodeStream, Intercom, MediaObject, MediaStreamDestination, MediaStreamFeedback, RequestMediaStream, RTCAVSignalingSetup, RTCConnectionManagement, RTCInputMediaObjectTrack, RTCOutputMediaObjectTrack, RTCSignalingOptions, RTCSignalingSession, ScryptedDevice, ScryptedMimeTypes } from "@scrypted/sdk"; import { ScryptedSessionControl } from "./session-control"; -import { requiredAudioCodecs, requiredVideoCodec } from "./webrtc-required-codecs"; +import { opusAudioCodecOnly, requiredAudioCodecs, requiredVideoCodec } from "./webrtc-required-codecs"; import { logIsLocalIceTransport } from "./werift-util"; import { addVideoFilterArguments } from "@scrypted/common/src/ffmpeg-helpers"; @@ -481,6 +481,7 @@ export class WebRTCConnectionManagement implements RTCConnectionManagement { closed = false; constructor(public console: Console, public clientSession: RTCSignalingSession, + public requireOpus: boolean, public maximumCompatibilityMode: boolean, public clientOptions: RTCSignalingOptions, public options: { @@ -494,7 +495,7 @@ export class WebRTCConnectionManagement implements RTCConnectionManagement { // the cameras and alexa targets will also provide externally reachable addresses. codecs: { audio: [ - ...requiredAudioCodecs, + ...(requireOpus ? opusAudioCodecOnly : requiredAudioCodecs), ], video: [ requiredVideoCodec, @@ -660,6 +661,7 @@ export async function createRTCPeerConnectionSink( console: Console, intercom: ScryptedDevice & Intercom, mo: MediaObject, + requireOpus: boolean, maximumCompatibilityMode: boolean, configuration: RTCConfiguration, weriftConfiguration: Partial, @@ -668,7 +670,7 @@ export async function createRTCPeerConnectionSink( const clientOptions = await legacyGetSignalingSessionOptions(clientSignalingSession); // console.log('remote options', clientOptions); - const connection = new WebRTCConnectionManagement(console, clientSignalingSession, maximumCompatibilityMode, clientOptions, { + const connection = new WebRTCConnectionManagement(console, clientSignalingSession, requireOpus, maximumCompatibilityMode, clientOptions, { configuration, weriftConfiguration, }); diff --git a/plugins/webrtc/src/main.ts b/plugins/webrtc/src/main.ts index c2ab5a437..eae35d522 100644 --- a/plugins/webrtc/src/main.ts +++ b/plugins/webrtc/src/main.ts @@ -54,6 +54,7 @@ class WebRTCMixin extends SettingsMixinDeviceBase(); cleanup.promise.catch(e => this.console.log('cleaning up rtc connection:', e.message)); - const connection = new WebRTCConnectionManagement(console, clientSession, maximumCompatibilityMode, clientOptions, options); + const connection = new WebRTCConnectionManagement(console, clientSession, this.storageSettings.values.requireOpus, maximumCompatibilityMode, clientOptions, options); cleanup.promise.finally(() => connection.close().catch(() => { })); const { pc } = connection; waitClosed(pc).then(() => cleanup.resolve('peer connection closed')); diff --git a/plugins/webrtc/src/webrtc-required-codecs.ts b/plugins/webrtc/src/webrtc-required-codecs.ts index f45f285b3..dab8030d5 100644 --- a/plugins/webrtc/src/webrtc-required-codecs.ts +++ b/plugins/webrtc/src/webrtc-required-codecs.ts @@ -41,6 +41,14 @@ export const requiredAudioCodecs = [ }), ]; +export const opusAudioCodecOnly = [ + new RTCRtpCodecParameters({ + mimeType: "audio/opus", + clockRate: 48000, + channels: 2, + payloadType: 111, + }), +]; export function getAudioCodec(outputCodecParameters: RTCRtpCodecParameters) { if (outputCodecParameters.name === 'PCMA') {