From e7a87197509896862c639476b330007cfa78bdf1 Mon Sep 17 00:00:00 2001 From: Oliver Lazoroski Date: Fri, 22 Nov 2024 16:03:35 +0100 Subject: [PATCH] fix: force single codec - reads the data from the RTCRtpSender when available --- packages/client/src/rtc/Publisher.ts | 11 ++++++++--- packages/client/src/rtc/codecs.ts | 10 +++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/packages/client/src/rtc/Publisher.ts b/packages/client/src/rtc/Publisher.ts index f479101e53..15d51192b5 100644 --- a/packages/client/src/rtc/Publisher.ts +++ b/packages/client/src/rtc/Publisher.ts @@ -459,9 +459,14 @@ export class Publisher { private getCodecPreferences = ( trackType: TrackType, preferredCodec?: string, + codecPreferencesSource?: 'sender' | 'receiver', ) => { if (trackType === TrackType.VIDEO) { - return getPreferredCodecs('video', preferredCodec || 'vp8'); + return getPreferredCodecs( + 'video', + preferredCodec || 'vp8', + codecPreferencesSource, + ); } if (trackType === TrackType.AUDIO) { const defaultAudioCodec = this.isRedEnabled ? 'red' : 'opus'; @@ -575,8 +580,8 @@ export class Publisher { const opts = this.publishOptsForTrack.get(trackType); if (!opts || !opts.forceSingleCodec) return sdp; - const codec = opts.forceCodec || opts.preferredCodec; - const orderedCodecs = this.getCodecPreferences(trackType, codec); + const codec = opts.forceCodec || getOptimalVideoCodec(opts.preferredCodec); + const orderedCodecs = this.getCodecPreferences(trackType, codec, 'sender'); if (!orderedCodecs || orderedCodecs.length === 0) return sdp; const transceiver = this.transceiverCache.get(trackType); diff --git a/packages/client/src/rtc/codecs.ts b/packages/client/src/rtc/codecs.ts index 01a8707f9d..cda7cefd58 100644 --- a/packages/client/src/rtc/codecs.ts +++ b/packages/client/src/rtc/codecs.ts @@ -9,15 +9,19 @@ import type { PreferredCodec } from '../types'; * @param kind the kind of codec to get. * @param preferredCodec the codec to prioritize (vp8, h264, vp9, av1...). * @param codecToRemove the codec to exclude from the list. + * @param codecPreferencesSource the source of the codec preferences. */ export const getPreferredCodecs = ( kind: 'audio' | 'video', preferredCodec: string, codecToRemove?: string, -): RTCRtpCodecCapability[] | undefined => { - if (!('getCapabilities' in RTCRtpReceiver)) return; + codecPreferencesSource: 'sender' | 'receiver' = 'receiver', +): RTCRtpCodec[] | undefined => { + const source = + codecPreferencesSource === 'receiver' ? RTCRtpReceiver : RTCRtpSender; + if (!('getCapabilities' in source)) return; - const capabilities = RTCRtpReceiver.getCapabilities(kind); + const capabilities = source.getCapabilities(kind); if (!capabilities) return; const preferred: RTCRtpCodecCapability[] = [];