From bc7ec5dcaf44a8eed228a738fb30393a572c75a2 Mon Sep 17 00:00:00 2001 From: Nikola Novakovic Date: Fri, 26 Mar 2021 09:35:22 -1000 Subject: [PATCH] Add support for choosing preferred video codec profile (#183) --- decs.d.ts | 1 + src/stream.ts | 30 +++++++++++++++++++++++++----- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/decs.d.ts b/decs.d.ts index 3de73779..f5488364 100644 --- a/decs.d.ts +++ b/decs.d.ts @@ -99,6 +99,7 @@ interface RTCDtlsTransport { // https://www.w3.org/TR/webrtc/#idl-def-rtcrtpcodeccapability interface RTCRtpCodecCapability { mimeType: string; + sdpFmtpLine?: string; } // https://www.w3.org/TR/webrtc/#idl-def-rtcrtpheaderextensioncapability diff --git a/src/stream.ts b/src/stream.ts index c1818fa5..df4c0db6 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -109,6 +109,7 @@ export interface Constraints extends MediaStreamConstraints { codec: string; simulcast?: boolean; sendEmptyOnMute?: boolean; + preferredCodecProfile?: string; } const defaults = { @@ -253,11 +254,30 @@ export class LocalStream extends MediaStream { if ('setCodecPreferences' in transceiver) { const cap = RTCRtpSender.getCapabilities(kind); if (!cap) return; - const selCodec = cap.codecs.find( - (c) => - c.mimeType.toLowerCase() === `video/${this.constraints.codec.toLowerCase()}` || - c.mimeType.toLowerCase() === `audio/opus`, - ); + + let selCodec: RTCRtpCodecCapability | undefined; + if (this.constraints.preferredCodecProfile && kind === 'video') { + const allCodecProfiles = cap.codecs.filter((c) => c.mimeType.toLowerCase() === `video/${this.constraints.codec.toLowerCase()}`); + if (!allCodecProfiles) { + return; + } + selCodec = allCodecProfiles.find( + (c) => + c.sdpFmtpLine && + c.sdpFmtpLine?.indexOf(`profile-level-id=${this.constraints.preferredCodecProfile}`) >= 0 + ); + if (!selCodec) { + // get first one + selCodec = allCodecProfiles[0]; + + } + } else { + selCodec = cap.codecs.find( + (c) => + c.mimeType.toLowerCase() === `video/${this.constraints.codec.toLowerCase()}` || + c.mimeType.toLowerCase() === `audio/opus` + ); + } if (selCodec) { transceiver.setCodecPreferences([selCodec]); }