Skip to content

Commit

Permalink
Fix trackProcessor creation from LocalParticipant.createTracks (#1247)
Browse files Browse the repository at this point in the history
* Fix trackProcessor creation from LocalParticipant.createTracks

* Create sour-starfishes-act.md
  • Loading branch information
lukasIO authored Sep 11, 2024
1 parent bf7fb5d commit 260a5e8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/sour-starfishes-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"livekit-client": patch
---

Fix trackProcessor creation from LocalParticipant.createTracks
15 changes: 8 additions & 7 deletions src/room/participant/LocalParticipant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ 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 @@ -40,7 +41,6 @@ import type {
VideoCaptureOptions,
} from '../track/options';
import { ScreenSharePresets, VideoPresets, isBackupCodec } from '../track/options';
import type { TrackProcessor } from '../track/processor/types';
import {
constraintsForOptions,
getLogContextFromTrack,
Expand Down Expand Up @@ -486,6 +486,9 @@ export default class LocalParticipant extends Participant {
* @returns
*/
async createTracks(options?: CreateLocalTracksOptions): Promise<LocalTrack[]> {
options ??= {};
const { audioProcessor, videoProcessor } = extractProcessorsFromOptions(options);

const mergedOptions = mergeDefaultOptions(
options,
this.roomOptions?.audioCaptureDefaults,
Expand Down Expand Up @@ -540,12 +543,10 @@ export default class LocalParticipant extends Participant {
track.setAudioContext(this.audioContext);
}
track.mediaStream = stream;
if (trackOptions.processor) {
if (track instanceof LocalAudioTrack) {
await track.setProcessor(trackOptions.processor as TrackProcessor<Track.Kind.Audio>);
} else {
await track.setProcessor(trackOptions.processor as TrackProcessor<Track.Kind.Video>);
}
if (track instanceof LocalAudioTrack && audioProcessor) {
await track.setProcessor(audioProcessor);
} else if (track instanceof LocalVideoTrack && videoProcessor) {
await track.setProcessor(videoProcessor);
}
return track;
}),
Expand Down
35 changes: 27 additions & 8 deletions src/room/track/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,32 @@ import type {
VideoCaptureOptions,
} from './options';
import { ScreenSharePresets } from './options';
import type { TrackProcessor } from './processor/types';
import type {
AudioProcessorOptions,
TrackProcessor,
VideoProcessorOptions,
} from './processor/types';
import {
constraintsForOptions,
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 All @@ -35,6 +54,7 @@ export async function createLocalTracks(
options.audio ??= true;
options.video ??= true;

const { audioProcessor, videoProcessor } = extractProcessorsFromOptions(options);
const opts = mergeDefaultOptions(options, audioDefaults, videoDefaults);
const constraints = constraintsForOptions(opts);

Expand All @@ -55,7 +75,7 @@ export async function createLocalTracks(
return Promise.all(
stream.getTracks().map(async (mediaStreamTrack) => {
const isAudio = mediaStreamTrack.kind === 'audio';
let trackOptions = isAudio ? options!.audio : options!.video;
let trackOptions = isAudio ? opts!.audio : opts!.video;
if (typeof trackOptions === 'boolean' || !trackOptions) {
trackOptions = {};
}
Expand All @@ -80,13 +100,12 @@ export async function createLocalTracks(
track.source = Track.Source.Microphone;
}
track.mediaStream = stream;
if (trackOptions.processor) {
if (track instanceof LocalAudioTrack) {
await track.setProcessor(trackOptions.processor as TrackProcessor<Track.Kind.Audio>);
} else if (track instanceof LocalVideoTrack) {
await track.setProcessor(trackOptions.processor as TrackProcessor<Track.Kind.Video>);
}
if (track instanceof LocalAudioTrack && audioProcessor) {
await track.setProcessor(audioProcessor);
} else if (track instanceof LocalVideoTrack && videoProcessor) {
await track.setProcessor(videoProcessor);
}

return track;
}),
);
Expand Down

0 comments on commit 260a5e8

Please sign in to comment.