Skip to content
Draft
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
93 changes: 58 additions & 35 deletions src/state/MediaViewModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import { type MediaDevices } from "./MediaDevices";
import { type Behavior } from "./Behavior";
import { type ObservableScope } from "./ObservableScope";
import { RemoteUserSetting } from "./RemoteUserSettings";

export function observeTrackReference$(
participant: Participant,
Expand Down Expand Up @@ -398,7 +399,6 @@
return this._videoEnabled$;
}

private readonly _cropVideo$ = new BehaviorSubject(true);
/**
* Whether the tile video should be contained inside the tile or be cropped to fit.
*/
Expand All @@ -416,6 +416,7 @@
mxcAvatarUrl$: Behavior<string | undefined>,
public readonly handRaised$: Behavior<Date | null>,
public readonly reaction$: Behavior<ReactionOption | null>,
public readonly _cropVideo$ = new BehaviorSubject(true),

Check failure on line 419 in src/state/MediaViewModel.ts

View workflow job for this annotation

GitHub Actions / Lint, format & type check

Parameter Property name `_cropVideo$` must not have a leading underscore
) {
super(
scope,
Expand Down Expand Up @@ -610,36 +611,7 @@
* The volume to which this participant's audio is set, as a scalar
* multiplier.
*/
public readonly localVolume$ = this.scope.behavior<number>(
merge(
this.locallyMutedToggle$.pipe(map(() => "toggle mute" as const)),
this.localVolumeAdjustment$,
this.localVolumeCommit$.pipe(map(() => "commit" as const)),
).pipe(
accumulate({ volume: 1, committedVolume: 1 }, (state, event) => {
switch (event) {
case "toggle mute":
return {
...state,
volume: state.volume === 0 ? state.committedVolume : 0,
};
case "commit":
// Dragging the slider to zero should have the same effect as
// muting: keep the original committed volume, as if it were never
// dragged
return {
...state,
committedVolume:
state.volume === 0 ? state.committedVolume : state.volume,
};
default:
// Volume adjustment
return { ...state, volume: event };
}
}),
map(({ volume }) => volume),
),
);
public readonly localVolume$: Behavior<number>;

// This private field is used to override the value from the superclass
private __videoEnabled$: Behavior<boolean>;
Expand All @@ -650,9 +622,9 @@
/**
* Whether this participant's audio is disabled.
*/
public readonly locallyMuted$ = this.scope.behavior<boolean>(
this.localVolume$.pipe(map((volume) => volume === 0)),
);
public readonly locallyMuted$: Behavior<boolean>;

private readonly remoteUserSetting: RemoteUserSetting;

public constructor(
scope: ObservableScope,
Expand All @@ -668,6 +640,7 @@
handRaised$: Behavior<Date | null>,
reaction$: Behavior<ReactionOption | null>,
) {
const remoteUserSetting = new RemoteUserSetting(userId);
super(
scope,
id,
Expand All @@ -680,6 +653,7 @@
mxcAvatarUrl$,
handRaised$,
reaction$,
new BehaviorSubject(remoteUserSetting.cropVideo),
);

this.__speaking$ = this.scope.behavior(
Expand All @@ -690,6 +664,47 @@
),
);

this.remoteUserSetting = remoteUserSetting;
const storedVolume = this.remoteUserSetting.getValue().volume;

this.localVolume$ = this.scope.behavior<number>(
merge(
this.locallyMutedToggle$.pipe(map(() => "toggle mute" as const)),
this.localVolumeAdjustment$,
this.localVolumeCommit$.pipe(map(() => "commit" as const)),
).pipe(
accumulate(
{ volume: storedVolume, committedVolume: storedVolume },
(state, event) => {
switch (event) {
case "toggle mute":
return {
...state,
volume: state.volume === 0 ? state.committedVolume : 0,
};
case "commit":
// Dragging the slider to zero should have the same effect as
// muting: keep the original committed volume, as if it were never
// dragged
return {
...state,
committedVolume:
state.volume === 0 ? state.committedVolume : state.volume,
};
default:
// Volume adjustment
return { ...state, volume: event };
}
},
),
map(({ volume }) => volume),
),
);

this.locallyMuted$ = this.scope.behavior<boolean>(
this.localVolume$.pipe(map((volume) => volume === 0)),
);

this.__videoEnabled$ = this.scope.behavior(
pretendToBeDisconnected$.pipe(
switchMap((disconnected) =>
Expand All @@ -708,7 +723,10 @@
switchMap((disconnected) => (disconnected ? of(0) : this.localVolume$)),
this.scope.bind(),
),
]).subscribe(([p, volume]) => p?.setVolume(volume));
]).subscribe(([p, volume]) => {
p?.setVolume(volume);
this.remoteUserSetting.volume = volume;
});
}

public toggleLocallyMuted(): void {
Expand All @@ -723,6 +741,11 @@
this.localVolumeCommit$.next();
}

public toggleFitContain(): void {
super.toggleFitContain();
this.remoteUserSetting.cropVideo = this._cropVideo$.value;
}

public audioStreamStats$ = combineLatest([
this.participant$,
showConnectionStats.value$,
Expand Down
30 changes: 30 additions & 0 deletions src/state/RemoteUserSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Setting } from "../settings/settings";

Check failure on line 1 in src/state/RemoteUserSettings.ts

View workflow job for this annotation

GitHub Actions / Lint, format & type check

Copyright heading is required

export interface RemoteUserSettingData {
volume: number;
cropVideo: boolean;
}

/**
* A set of local modifications for a remote user's media that should persist
* across calls.
*/
export class RemoteUserSetting extends Setting<RemoteUserSettingData> {
constructor(userId: string) {

Check failure on line 13 in src/state/RemoteUserSettings.ts

View workflow job for this annotation

GitHub Actions / Lint, format & type check

Missing accessibility modifier on method definition constructor
super(`remoteusersettings-${userId}`, { volume: 1, cropVideo: true });
}

public set volume(volume: number) {
this.setValue({
...this.getValue(),
volume,
});
}

public set cropVideo(cropVideo: boolean) {
this.setValue({
...this.getValue(),
cropVideo,
});
}
}
Loading