Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix processor passing in CreateLocalTracks options #1329

Merged
merged 4 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/red-tools-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"livekit-client": patch
---

Fix processor passing in CreateLocalTracks options
7 changes: 4 additions & 3 deletions src/room/participant/LocalParticipant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -56,6 +55,7 @@ import type {
import { ScreenSharePresets, VideoPresets, isBackupCodec } from '../track/options';
import {
constraintsForOptions,
extractProcessorsFromOptions,
getLogContextFromTrack,
mergeDefaultOptions,
mimeTypeToVideoCodecString,
Expand Down Expand Up @@ -563,10 +563,11 @@ export default class LocalParticipant extends Participant {
*/
async createTracks(options?: CreateLocalTracksOptions): Promise<LocalTrack[]> {
options ??= {};
const { audioProcessor, videoProcessor } = extractProcessorsFromOptions(options);
const { audioProcessor, videoProcessor, optionsWithoutProcessor } =
extractProcessorsFromOptions(options);

const mergedOptions = mergeDefaultOptions(
options,
optionsWithoutProcessor,
this.roomOptions?.audioCaptureDefaults,
this.roomOptions?.videoCaptureDefaults,
);
Expand Down
21 changes: 1 addition & 20 deletions src/room/track/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Track.Kind.Audio, AudioProcessorOptions> | undefined;
let videoProcessor: TrackProcessor<Track.Kind.Video, VideoProcessorOptions> | 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
Expand Down
44 changes: 36 additions & 8 deletions src/room/track/utils.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<string, unknown>,
clonedOptions.audio as Record<string, unknown>,
audioDefaults as Record<string, unknown>,
);
if (audioProcessor) {
clonedOptions.audio.processor = audioProcessor;
}
}
if (opts.video) {
if (clonedOptions.video) {
mergeObjectWithoutOverwriting(
opts.video as Record<string, unknown>,
clonedOptions.video as Record<string, unknown>,
videoDefaults as Record<string, unknown>,
);
if (videoProcessor) {
clonedOptions.video.processor = videoProcessor;
}
}
return opts;
return clonedOptions;
}

function mergeObjectWithoutOverwriting(
Expand Down Expand Up @@ -260,3 +270,21 @@ export function diffAttributes(

return diff;
}

/** @internal */
export function extractProcessorsFromOptions(options: CreateLocalTracksOptions) {
const newOptions = { ...options };
let audioProcessor: TrackProcessor<Track.Kind.Audio, AudioProcessorOptions> | undefined;
let videoProcessor: TrackProcessor<Track.Kind.Video, VideoProcessorOptions> | 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 };
}
Loading