Add seperation of voice channels, creation of new ones, and move around users

This commit is contained in:
2026-03-30 02:11:39 +02:00
parent 83694570e3
commit 727059fb52
19 changed files with 614 additions and 50 deletions

View File

@@ -91,4 +91,8 @@ export class VoiceConnectionFacade {
stopVoiceHeartbeat(): void {
this.realtime.stopVoiceHeartbeat();
}
syncOutgoingVoiceRouting(allowedPeerIds: string[]): void {
this.realtime.syncOutgoingVoiceRouting(allowedPeerIds);
}
}

View File

@@ -3,8 +3,14 @@ import {
effect,
inject
} from '@angular/core';
import { Store } from '@ngrx/store';
import { STORAGE_KEY_USER_VOLUMES } from '../../../core/constants';
import { ScreenShareFacade } from '../../../domains/screen-share';
import { User } from '../../../shared-kernel';
import {
selectAllUsers,
selectCurrentUser
} from '../../../store/users/users.selectors';
import { VoiceConnectionFacade } from './voice-connection.facade';
export interface PlaybackOptions {
@@ -34,8 +40,11 @@ interface PeerAudioPipeline {
@Injectable({ providedIn: 'root' })
export class VoicePlaybackService {
private readonly store = inject(Store);
private readonly voiceConnection = inject(VoiceConnectionFacade);
private readonly screenShare = inject(ScreenShareFacade);
private readonly allUsers = this.store.selectSignal(selectAllUsers);
private readonly currentUser = this.store.selectSignal(selectCurrentUser);
private peerPipelines = new Map<string, PeerAudioPipeline>();
private pendingRemoteStreams = new Map<string, MediaStream>();
@@ -64,6 +73,11 @@ export class VoicePlaybackService {
void this.applyEffectiveOutputDeviceToAllPipelines();
});
effect(() => {
this.syncOutgoingVoiceRouting();
this.recalcAllGains();
});
this.voiceConnection.onRemoteStream.subscribe(({ peerId }) => {
const voiceStream = this.voiceConnection.getRemoteVoiceStream(peerId);
@@ -305,7 +319,7 @@ export class VoicePlaybackService {
if (!pipeline)
return;
if (this.deafened || this.captureEchoSuppressed || this.isUserMuted(peerId)) {
if (this.deafened || this.captureEchoSuppressed || this.isUserMuted(peerId) || !this.isPeerInCurrentVoiceRoom(peerId)) {
pipeline.gainNode.gain.value = 0;
return;
}
@@ -320,6 +334,61 @@ export class VoicePlaybackService {
this.peerPipelines.forEach((_pipeline, peerId) => this.applyGain(peerId));
}
private isPeerInCurrentVoiceRoom(peerId: string): boolean {
const localVoiceState = this.currentUser()?.voiceState;
if (!localVoiceState?.isConnected || !localVoiceState.roomId || !localVoiceState.serverId) {
return false;
}
const remoteVoiceState = this.findUserForPeer(peerId)?.voiceState;
return !!remoteVoiceState?.isConnected
&& remoteVoiceState.roomId === localVoiceState.roomId
&& remoteVoiceState.serverId === localVoiceState.serverId;
}
private findUserForPeer(peerId: string): User | undefined {
return this.allUsers().find((user) => user.id === peerId || user.oderId === peerId || user.peerId === peerId);
}
private syncOutgoingVoiceRouting(): void {
const localVoiceState = this.currentUser()?.voiceState;
if (!localVoiceState?.isConnected || !localVoiceState.roomId || !localVoiceState.serverId) {
this.voiceConnection.syncOutgoingVoiceRouting([]);
return;
}
const allowedPeerIds = new Set<string>();
for (const user of this.allUsers()) {
const voiceState = user.voiceState;
if (
!voiceState?.isConnected
|| voiceState.roomId !== localVoiceState.roomId
|| voiceState.serverId !== localVoiceState.serverId
) {
continue;
}
if (user.id) {
allowedPeerIds.add(user.id);
}
if (user.oderId) {
allowedPeerIds.add(user.oderId);
}
if (user.peerId) {
allowedPeerIds.add(user.peerId);
}
}
this.voiceConnection.syncOutgoingVoiceRouting(Array.from(allowedPeerIds));
}
private persistVolumes(): void {
try {
const data: Record<string, { volume: number; muted: boolean }> = {};