Skip to content

Commit

Permalink
chore: moved updatePublishOptions to the call instance
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverlaz committed Oct 15, 2024
1 parent c4e828e commit 696a4ef
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 54 deletions.
44 changes: 20 additions & 24 deletions packages/client/src/Call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ export class Call {
*/
private readonly dispatcher = new Dispatcher();

private publishOptions?: PublishOptions;
private statsReporter?: StatsReporter;
private sfuStatsReporter?: SfuStatsReporter;
private dropTimeout: ReturnType<typeof setTimeout> | undefined;
Expand Down Expand Up @@ -1289,19 +1290,12 @@ export class Call {
break;
case TrackType.VIDEO:
const videoStream = this.camera.state.mediaStream;
if (videoStream) {
await this.publishVideoStream(
videoStream,
this.camera.publishOptions,
);
}
if (videoStream) await this.publishVideoStream(videoStream);
break;
case TrackType.SCREEN_SHARE:
const screenShareStream = this.screenShare.state.mediaStream;
if (screenShareStream) {
await this.publishScreenShareStream(screenShareStream, {
screenShareSettings: this.screenShare.getSettings(),
});
await this.publishScreenShareStream(screenShareStream);
}
break;
// screen share audio can't exist without a screen share, so we handle it there
Expand Down Expand Up @@ -1333,12 +1327,8 @@ export class Call {
* The previous video stream will be stopped.
*
* @param videoStream the video stream to publish.
* @param opts the options to use when publishing the stream.
*/
publishVideoStream = async (
videoStream: MediaStream,
opts: PublishOptions = {},
) => {
publishVideoStream = async (videoStream: MediaStream) => {
if (!this.sfuClient) throw new Error(`Call not joined yet.`);
// joining is in progress, and we should wait until the client is ready
await this.sfuClient.joinTask;
Expand All @@ -1360,7 +1350,7 @@ export class Call {
videoStream,
videoTrack,
TrackType.VIDEO,
opts,
this.publishOptions,
);
};

Expand Down Expand Up @@ -1404,12 +1394,8 @@ export class Call {
* The previous screen-share stream will be stopped.
*
* @param screenShareStream the screen-share stream to publish.
* @param opts the options to use when publishing the stream.
*/
publishScreenShareStream = async (
screenShareStream: MediaStream,
opts: PublishOptions = {},
) => {
publishScreenShareStream = async (screenShareStream: MediaStream) => {
if (!this.sfuClient) throw new Error(`Call not joined yet.`);
// joining is in progress, and we should wait until the client is ready
await this.sfuClient.joinTask;
Expand All @@ -1428,6 +1414,9 @@ export class Call {
if (!this.trackPublishOrder.includes(TrackType.SCREEN_SHARE)) {
this.trackPublishOrder.push(TrackType.SCREEN_SHARE);
}
const opts: PublishOptions = {
screenShareSettings: this.screenShare.getSettings(),
};
await this.publisher.publishStream(
screenShareStream,
screenShareTrack,
Expand Down Expand Up @@ -1464,6 +1453,16 @@ export class Call {
await this.publisher?.unpublishStream(trackType, stopTrack);
};

/**
* Updates the preferred publishing options
*
* @internal
* @param options the options to use.
*/
updatePublishOptions(options: PublishOptions) {
this.publishOptions = { ...this.publishOptions, ...options };
}

/**
* Notifies the SFU that a noise cancellation process has started.
*
Expand Down Expand Up @@ -2085,10 +2084,7 @@ export class Call {
this.camera.state.mediaStream &&
!this.publisher?.isPublishing(TrackType.VIDEO)
) {
await this.publishVideoStream(
this.camera.state.mediaStream,
this.camera.publishOptions,
);
await this.publishVideoStream(this.camera.state.mediaStream);
}

// Start camera if backend config specifies, and there is no local setting
Expand Down
24 changes: 4 additions & 20 deletions packages/client/src/devices/CameraManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,14 @@ import { CameraDirection, CameraManagerState } from './CameraManagerState';
import { InputMediaDeviceManager } from './InputMediaDeviceManager';
import { getVideoDevices, getVideoStream } from './devices';
import { TrackType } from '../gen/video/sfu/models/models';
import { PreferredCodec, PublishOptions } from '../types';
import { PreferredCodec } from '../types';

export class CameraManager extends InputMediaDeviceManager<CameraManagerState> {
private targetResolution = {
width: 1280,
height: 720,
};

/**
* The options to use when publishing the video stream.
*
* @internal
*/
publishOptions: PublishOptions | undefined;

/**
* Constructs a new CameraManager.
*
Expand Down Expand Up @@ -86,20 +79,11 @@ export class CameraManager extends InputMediaDeviceManager<CameraManagerState> {
* Sets the preferred codec for encoding the video.
*
* @internal internal use only, not part of the public API.
* @deprecated use {@link call.updatePublishOptions} instead.
* @param codec the codec to use for encoding the video.
*/
setPreferredCodec(codec: PreferredCodec | undefined) {
this.updatePublishOptions({ preferredCodec: codec });
}

/**
* Updates the preferred publish options for the video stream.
*
* @internal
* @param options the options to use.
*/
updatePublishOptions(options: PublishOptions) {
this.publishOptions = { ...this.publishOptions, ...options };
this.call.updatePublishOptions({ preferredCodec: codec });
}

protected getDevices(): Observable<MediaDeviceInfo[]> {
Expand All @@ -121,7 +105,7 @@ export class CameraManager extends InputMediaDeviceManager<CameraManagerState> {
}

protected publishStream(stream: MediaStream): Promise<void> {
return this.call.publishVideoStream(stream, this.publishOptions);
return this.call.publishVideoStream(stream);
}

protected stopPublishStream(stopTracks: boolean): Promise<void> {
Expand Down
4 changes: 1 addition & 3 deletions packages/client/src/devices/ScreenShareManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ export class ScreenShareManager extends InputMediaDeviceManager<
}

protected publishStream(stream: MediaStream): Promise<void> {
return this.call.publishScreenShareStream(stream, {
screenShareSettings: this.state.settings,
});
return this.call.publishScreenShareStream(stream);
}

protected async stopPublishStream(stopTracks: boolean): Promise<void> {
Expand Down
3 changes: 3 additions & 0 deletions sample-apps/react-native/dogfood/src/components/MeetingUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ export const MeetingUI = ({ callId, navigation, route }: Props) => {
const onJoinCallHandler = useCallback(async () => {
try {
setShow('loading');
call?.updatePublishOptions({
preferredCodec: 'vp9',
});
await call?.join({ create: true });
appStoreSetState({ chatLabelNoted: false });
setShow('active-call');
Expand Down
9 changes: 2 additions & 7 deletions sample-apps/react/react-dogfood/components/MeetingUI.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useRouter } from 'next/router';
import { JSX, useCallback, useEffect, useState } from 'react';
import { isFirefox } from 'mobile-device-detect';
import Gleap from 'gleap';
import {
CallingState,
Expand All @@ -23,7 +22,6 @@ import {
import { ActiveCall } from './ActiveCall';
import { Feedback } from './Feedback/Feedback';
import { DefaultAppHeader } from './DefaultAppHeader';
import { useIsProntoEnvironment } from '../context/AppEnvironmentContext';

const contents = {
'error-join': {
Expand All @@ -48,7 +46,6 @@ export const MeetingUI = ({ chatClient, mode }: MeetingUIProps) => {
const { useCallCallingState } = useCallStateHooks();
const callState = useCallCallingState();

const isProntoEnvironment = useIsProntoEnvironment();
const videoCodecOverride = router.query['video_codec'] as
| PreferredCodec
| undefined;
Expand All @@ -65,14 +62,13 @@ export const MeetingUI = ({ chatClient, mode }: MeetingUIProps) => {
if (!fastJoin) setShow('loading');
if (!call) throw new Error('No active call found');
try {
const prontoDefaultCodec =
isProntoEnvironment && !isFirefox ? 'vp9' : 'vp8';
const prontoDefaultCodec = 'vp9';
const preferredCodec = videoCodecOverride || prontoDefaultCodec;
const preferredBitrate = bitrateOverride
? parseInt(bitrateOverride, 10)
: undefined;

call.camera.updatePublishOptions({
call.updatePublishOptions({
preferredCodec,
scalabilityMode,
preferredBitrate,
Expand All @@ -93,7 +89,6 @@ export const MeetingUI = ({ chatClient, mode }: MeetingUIProps) => {
bitrateFactorOverride,
bitrateOverride,
call,
isProntoEnvironment,
scalabilityMode,
videoCodecOverride,
],
Expand Down

0 comments on commit 696a4ef

Please sign in to comment.