From 2beec2bcb64b7c5a1ddfd445f08ff0afe4fa7f2a Mon Sep 17 00:00:00 2001 From: lukasIO Date: Thu, 28 Nov 2024 15:27:35 +0100 Subject: [PATCH] Fix processor passing in CreateLocalTracks options (#1329) * Fix processor passing in CreateLocalTracks options * Create red-tools-juggle.md * fix circular dependency --- .changeset/red-tools-juggle.md | 5 +++ src/room/participant/LocalParticipant.ts | 7 ++-- src/room/track/create.ts | 21 +---------- src/room/track/utils.ts | 44 +++++++++++++++++++----- 4 files changed, 46 insertions(+), 31 deletions(-) create mode 100644 .changeset/red-tools-juggle.md diff --git a/.changeset/red-tools-juggle.md b/.changeset/red-tools-juggle.md new file mode 100644 index 0000000000..76749a4837 --- /dev/null +++ b/.changeset/red-tools-juggle.md @@ -0,0 +1,5 @@ +--- +"livekit-client": patch +--- + +Fix processor passing in CreateLocalTracks options diff --git a/src/room/participant/LocalParticipant.ts b/src/room/participant/LocalParticipant.ts index 93049f5e7f..014f0d4bd4 100644 --- a/src/room/participant/LocalParticipant.ts +++ b/src/room/participant/LocalParticipant.ts @@ -44,7 +44,6 @@ import LocalTrack from '../track/LocalTrack'; import LocalTrackPublication from '../track/LocalTrackPublication'; import LocalVideoTrack, { videoLayersFromEncodings } from '../track/LocalVideoTrack'; import { Track } from '../track/Track'; -import { extractProcessorsFromOptions } from '../track/create'; import type { AudioCaptureOptions, BackupVideoCodec, @@ -56,6 +55,7 @@ import type { import { ScreenSharePresets, VideoPresets, isBackupCodec } from '../track/options'; import { constraintsForOptions, + extractProcessorsFromOptions, getLogContextFromTrack, mergeDefaultOptions, mimeTypeToVideoCodecString, @@ -563,10 +563,11 @@ export default class LocalParticipant extends Participant { */ async createTracks(options?: CreateLocalTracksOptions): Promise { options ??= {}; - const { audioProcessor, videoProcessor } = extractProcessorsFromOptions(options); + const { audioProcessor, videoProcessor, optionsWithoutProcessor } = + extractProcessorsFromOptions(options); const mergedOptions = mergeDefaultOptions( - options, + optionsWithoutProcessor, this.roomOptions?.audioCaptureDefaults, this.roomOptions?.videoCaptureDefaults, ); diff --git a/src/room/track/create.ts b/src/room/track/create.ts index 8914bdf91f..3a57834082 100644 --- a/src/room/track/create.ts +++ b/src/room/track/create.ts @@ -14,32 +14,13 @@ import type { VideoCaptureOptions, } from './options'; import { ScreenSharePresets } from './options'; -import type { - AudioProcessorOptions, - TrackProcessor, - VideoProcessorOptions, -} from './processor/types'; import { constraintsForOptions, + extractProcessorsFromOptions, mergeDefaultOptions, screenCaptureToDisplayMediaStreamOptions, } from './utils'; -/** @internal */ -export function extractProcessorsFromOptions(options: CreateLocalTracksOptions) { - let audioProcessor: TrackProcessor | undefined; - let videoProcessor: TrackProcessor | undefined; - - if (typeof options.audio === 'object' && options.audio.processor) { - audioProcessor = options.audio.processor; - } - if (typeof options.video === 'object' && options.video.processor) { - videoProcessor = options.video.processor; - } - - return { audioProcessor, videoProcessor }; -} - /** * Creates a local video and audio track at the same time. When acquiring both * audio and video tracks together, it'll display a single permission prompt to diff --git a/src/room/track/utils.ts b/src/room/track/utils.ts index 82e6122c05..534353f6c2 100644 --- a/src/room/track/utils.ts +++ b/src/room/track/utils.ts @@ -1,4 +1,5 @@ import { TrackPublishedResponse } from '@livekit/protocol'; +import type { AudioProcessorOptions, TrackProcessor, VideoProcessorOptions } from '../..'; import { cloneDeep } from '../../utils/cloneDeep'; import { isSafari, sleep } from '../utils'; import { Track } from './Track'; @@ -17,24 +18,33 @@ export function mergeDefaultOptions( audioDefaults?: AudioCaptureOptions, videoDefaults?: VideoCaptureOptions, ): CreateLocalTracksOptions { - const opts: CreateLocalTracksOptions = cloneDeep(options) ?? {}; - if (opts.audio === true) opts.audio = {}; - if (opts.video === true) opts.video = {}; + const { optionsWithoutProcessor, audioProcessor, videoProcessor } = extractProcessorsFromOptions( + options ?? {}, + ); + const clonedOptions: CreateLocalTracksOptions = cloneDeep(optionsWithoutProcessor) ?? {}; + if (clonedOptions.audio === true) clonedOptions.audio = {}; + if (clonedOptions.video === true) clonedOptions.video = {}; // use defaults - if (opts.audio) { + if (clonedOptions.audio) { mergeObjectWithoutOverwriting( - opts.audio as Record, + clonedOptions.audio as Record, audioDefaults as Record, ); + if (audioProcessor) { + clonedOptions.audio.processor = audioProcessor; + } } - if (opts.video) { + if (clonedOptions.video) { mergeObjectWithoutOverwriting( - opts.video as Record, + clonedOptions.video as Record, videoDefaults as Record, ); + if (videoProcessor) { + clonedOptions.video.processor = videoProcessor; + } } - return opts; + return clonedOptions; } function mergeObjectWithoutOverwriting( @@ -260,3 +270,21 @@ export function diffAttributes( return diff; } + +/** @internal */ +export function extractProcessorsFromOptions(options: CreateLocalTracksOptions) { + const newOptions = { ...options }; + let audioProcessor: TrackProcessor | undefined; + let videoProcessor: TrackProcessor | undefined; + + if (typeof newOptions.audio === 'object' && newOptions.audio.processor) { + audioProcessor = newOptions.audio.processor; + newOptions.audio.processor = undefined; + } + if (typeof newOptions.video === 'object' && newOptions.video.processor) { + videoProcessor = newOptions.video.processor; + newOptions.video.processor = undefined; + } + + return { audioProcessor, videoProcessor, optionsWithoutProcessor: newOptions }; +}