mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-03 03:52:50 +08:00
fix: enforce WebRTC microphone mute at gateway
This commit is contained in:
1 parent
e14ddc9465
commit
83a8fd0d5a
5 files changed
+32
-6
No files matched your search
@@ -737,6 +737,7 @@ export class VoiceBridge {
|
||||
connectionId: entry.id,
|
||||
...(entry.webrtcPublicHost ? { publicHost: entry.webrtcPublicHost } : {}),
|
||||
logger: this.logger,
|
||||
microphoneMuted: offer.muted === true,
|
||||
onVoiceFrame: (data) => {
|
||||
const now = Date.now();
|
||||
if (entry.audio.ingressLastAt !== null) entry.audio.ingressMaxGapMs = Math.max(entry.audio.ingressMaxGapMs, now - entry.audio.ingressLastAt);
|
||||
@@ -757,7 +758,7 @@ export class VoiceBridge {
|
||||
});
|
||||
entry.webrtc = peer;
|
||||
try {
|
||||
const answer = await peer.createAnswer(offer);
|
||||
const answer = await peer.createAnswer({ type: offer.type, sdp: offer.sdp });
|
||||
if (entry.webrtc !== peer || entry.ws.readyState !== WebSocket.OPEN) return;
|
||||
sendJson({ type: "webrtcAnswer", payload: { sdp: answer } });
|
||||
this.logger.info({ entryId: entry.id }, "WebRTC audio negotiation completed");
|
||||
@@ -864,6 +865,8 @@ async function handleCommand(
|
||||
}
|
||||
entry.whisperActive = active;
|
||||
sendJson({ type: "whisperTargets", targetIds: [...entry.whisperTargetIds], active: entry.whisperActive });
|
||||
} else if (command.type === "setMicrophoneMuted") {
|
||||
entry.webrtc?.setMicrophoneMuted(command.payload.muted as boolean);
|
||||
}
|
||||
if (command.requestId) sendJson({ type: "commandCompleted", requestId: command.requestId });
|
||||
} catch (error: unknown) {
|
||||
@@ -893,7 +896,8 @@ function parseWebRtcOffer(raw: string): WebRtcSessionDescription | null {
|
||||
if (!isRecord(value) || value.type !== "webrtcOffer" || !isRecord(value.payload) || !isRecord(value.payload.sdp)) return null;
|
||||
const description = value.payload.sdp;
|
||||
if (description.type !== "offer" || typeof description.sdp !== "string" || description.sdp.length > 256 * 1024) return null;
|
||||
return { type: "offer", sdp: description.sdp };
|
||||
if (value.payload.muted !== undefined && typeof value.payload.muted !== "boolean") return null;
|
||||
return { type: "offer", sdp: description.sdp, muted: value.payload.muted === true };
|
||||
}
|
||||
|
||||
function isWebRtcStopMessage(raw: string): boolean {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
export interface ClientCommand {
|
||||
type: "switchChannel" | "sendTextMessage" | "sendServerMessage" | "sendPrivateMessage" | "poke" | "setAway" | "setWhisperTargets" | "setWhisperActive";
|
||||
type: "switchChannel" | "sendTextMessage" | "sendServerMessage" | "sendPrivateMessage" | "poke" | "setAway" | "setWhisperTargets" | "setWhisperActive" | "setMicrophoneMuted";
|
||||
requestId?: string;
|
||||
payload: Record<string, unknown>;
|
||||
}
|
||||
@@ -19,7 +19,7 @@ export function parseClientCommand(raw: string): ClientCommandResult {
|
||||
if (value.requestId !== undefined && (typeof value.requestId !== "string" || value.requestId.length > 64)) {
|
||||
return { error: { code: "INVALID_REQUEST_ID", message: "请求标识无效" } };
|
||||
}
|
||||
const supportedTypes = new Set(["switchChannel", "sendTextMessage", "sendServerMessage", "sendPrivateMessage", "poke", "setAway", "setWhisperTargets", "setWhisperActive"]);
|
||||
const supportedTypes = new Set(["switchChannel", "sendTextMessage", "sendServerMessage", "sendPrivateMessage", "poke", "setAway", "setWhisperTargets", "setWhisperActive", "setMicrophoneMuted"]);
|
||||
if (!supportedTypes.has(value.type)) {
|
||||
return { error: { code: "UNKNOWN_MESSAGE_TYPE", message: "不支持的消息类型" } };
|
||||
}
|
||||
@@ -56,6 +56,9 @@ export function parseClientCommand(raw: string): ClientCommandResult {
|
||||
if (value.type === "setWhisperActive" && typeof value.payload.active !== "boolean") {
|
||||
return { error: { code: "INVALID_WHISPER_STATE", message: "私语状态无效" } };
|
||||
}
|
||||
if (value.type === "setMicrophoneMuted" && typeof value.payload.muted !== "boolean") {
|
||||
return { error: { code: "INVALID_MICROPHONE_STATE", message: "麦克风状态无效" } };
|
||||
}
|
||||
return {
|
||||
type: value.type as ClientCommand["type"],
|
||||
...(typeof value.requestId === "string" ? { requestId: value.requestId } : {}),
|
||||
|
||||
@@ -40,12 +40,14 @@ export interface WebRtcAudioOptions {
|
||||
export interface WebRtcSessionDescription {
|
||||
type: "offer" | "answer";
|
||||
sdp: string;
|
||||
muted?: boolean;
|
||||
}
|
||||
|
||||
export interface WebRtcAudioSessionOptions {
|
||||
connectionId: string;
|
||||
publicHost?: string;
|
||||
logger: LoggerType;
|
||||
microphoneMuted?: boolean;
|
||||
onVoiceFrame: (data: Buffer) => void;
|
||||
onVoiceActivity: (clientIds: number[]) => void;
|
||||
}
|
||||
@@ -73,6 +75,7 @@ export class WebRtcAudioSession {
|
||||
private readonly audioTimer: ReturnType<typeof setInterval>;
|
||||
private readonly activityTimer: ReturnType<typeof setInterval>;
|
||||
private encoder: { encode(data: Buffer): Buffer } | null;
|
||||
private microphoneMuted: boolean;
|
||||
private sequenceNumber = randomInt(0, 65_536);
|
||||
private timestamp = randomInt(0, 0x1_0000_0000) >>> 0;
|
||||
private readonly ssrc = randomInt(1, 0x1_0000_0000) >>> 0;
|
||||
@@ -83,6 +86,7 @@ export class WebRtcAudioSession {
|
||||
this.logger = options.logger.child({ component: "webrtc-audio", connectionId: options.connectionId });
|
||||
this.onVoiceFrame = options.onVoiceFrame;
|
||||
this.onVoiceActivity = options.onVoiceActivity;
|
||||
this.microphoneMuted = options.microphoneMuted === true;
|
||||
this.outgoingTrack = new MediaStreamTrack({ kind: "audio" });
|
||||
|
||||
const iceAdditionalHostAddresses = options.publicHost ? [options.publicHost] : undefined;
|
||||
@@ -98,7 +102,7 @@ export class WebRtcAudioSession {
|
||||
const audio = this.peer.addTransceiver("audio", { direction: "sendrecv" });
|
||||
audio.onTrack.subscribe((track) => {
|
||||
track.onReceiveRtp.subscribe((rtp) => {
|
||||
if (this.closed || !this.opusPayloadTypes.has(rtp.header.payloadType)) return;
|
||||
if (this.closed || this.microphoneMuted || !this.opusPayloadTypes.has(rtp.header.payloadType)) return;
|
||||
const payload = Buffer.from(rtp.payload);
|
||||
// A disabled browser MediaStreamTrack normally still produces RTP
|
||||
// comfort-noise packets. TeamSpeak treats any received voice packet
|
||||
@@ -244,6 +248,10 @@ export class WebRtcAudioSession {
|
||||
this.onVoiceActivity(ids);
|
||||
}
|
||||
|
||||
setMicrophoneMuted(muted: boolean): void {
|
||||
this.microphoneMuted = muted;
|
||||
}
|
||||
|
||||
private hasVoiceEnergy(data: Buffer): boolean {
|
||||
const decoder = this.ingressDecoder;
|
||||
if (!decoder) return false;
|
||||
|
||||
Reference in new issue
Block a user