mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-03 12:02:50 +08:00
feat: add Android gateway preview and refactoring plan
This commit is contained in:
1 parent
9e2b9bf564
commit
aeaeb15965
73 files changed
+4347
-24
No files matched your search
@@ -0,0 +1,56 @@
|
||||
import { createRequire } from "node:module";
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
|
||||
type NativeCodec = {
|
||||
encode(pcm: Buffer): Buffer;
|
||||
decode(data: Buffer): Buffer;
|
||||
};
|
||||
|
||||
type ScriptCodec = {
|
||||
encode(pcm: Buffer, frameSize: number): Buffer | Uint8Array;
|
||||
decode(data: Buffer): Buffer | Uint8Array;
|
||||
delete(): void;
|
||||
};
|
||||
|
||||
type OpusScriptModule = {
|
||||
new (sampleRate: number, channels: number, application: number): ScriptCodec;
|
||||
Application: { AUDIO: number };
|
||||
};
|
||||
|
||||
/** Keep the existing native codec on servers; the Android bundle uses the WASM codec. */
|
||||
export class OpusEncoder {
|
||||
private readonly codec: NativeCodec | ScriptCodec;
|
||||
private readonly channels: number;
|
||||
private readonly mobile: boolean;
|
||||
|
||||
constructor(sampleRate: number, channels: number) {
|
||||
this.channels = channels;
|
||||
this.mobile = process.env.WEBSPEAK_MOBILE === "1";
|
||||
if (this.mobile) {
|
||||
const OpusScript = require("opusscript") as OpusScriptModule;
|
||||
this.codec = new OpusScript(sampleRate, channels, OpusScript.Application.AUDIO);
|
||||
} else {
|
||||
const { OpusEncoder: NativeOpusEncoder } = require("@discordjs/opus") as {
|
||||
OpusEncoder: new (rate: number, channelCount: number) => NativeCodec;
|
||||
};
|
||||
this.codec = new NativeOpusEncoder(sampleRate, channels);
|
||||
}
|
||||
}
|
||||
|
||||
encode(pcm: Buffer): Buffer {
|
||||
const encoded = this.mobile
|
||||
? (this.codec as ScriptCodec).encode(pcm, pcm.length / (2 * this.channels))
|
||||
: (this.codec as NativeCodec).encode(pcm);
|
||||
return Buffer.isBuffer(encoded) ? encoded : Buffer.from(encoded);
|
||||
}
|
||||
|
||||
decode(data: Buffer): Buffer {
|
||||
const decoded = this.codec.decode(data);
|
||||
return Buffer.isBuffer(decoded) ? decoded : Buffer.from(decoded);
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
if (this.mobile) (this.codec as ScriptCodec).delete();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
import { WebSocketServer, WebSocket } from "ws";
|
||||
import type { IncomingMessage, Server } from "node:http";
|
||||
import { createRequire } from "node:module";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { isIP } from "node:net";
|
||||
import { identityFromString } from "@echosixhiya/teamspeak-client";
|
||||
@@ -18,11 +17,7 @@ import { WebRtcAudioSession, type WebRtcAudioOptions, type WebRtcAudioStats, typ
|
||||
import { pingTeamSpeakSession } from "./network-probe.js";
|
||||
import type { AccelerationRelayOptions, ConfiguredAccelerationRelay } from "./acceleration-relay.js";
|
||||
import { normalizeScreenShareIceServers, parseScreenShareMessage, type ScreenShareClientMessage, type ScreenShareIceServer, type ScreenSharePeerSignal, type ScreenShareStreamDescription, type ScreenShareViewerDescription } from "./screen-share.js";
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
const { OpusEncoder } = require("@discordjs/opus") as {
|
||||
OpusEncoder: new (sampleRate: number, channels: number) => { encode(pcm: Buffer): Buffer };
|
||||
};
|
||||
import { OpusEncoder } from "./opus-codec.js";
|
||||
|
||||
const HEARTBEAT_INTERVAL_MS = 30_000;
|
||||
const AUDIO_FRAME_BYTES = 1_920;
|
||||
@@ -145,7 +140,7 @@ interface WebClientEntry {
|
||||
members: Map<number, ChannelMember>;
|
||||
avatarCache: Map<string, string | null>;
|
||||
eventLog: ServerEvent[];
|
||||
opusEncoder: { encode(pcm: Buffer): Buffer } | null;
|
||||
opusEncoder: OpusEncoder | null;
|
||||
opusEncoderWarnedAt: number; // Opus 编码器不可用告警的时间戳,用于限流避免反复刷屏
|
||||
whisperTargetIds: Set<number>;
|
||||
whisperActive: boolean;
|
||||
@@ -940,6 +935,7 @@ export class VoiceBridge {
|
||||
clearTimeout(entry.reconnectTimer);
|
||||
entry.reconnectTimer = null;
|
||||
}
|
||||
entry.opusEncoder?.dispose();
|
||||
entry.opusEncoder = null;
|
||||
const webRtc = entry.webrtc;
|
||||
entry.webrtc = null;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { randomInt } from "node:crypto";
|
||||
import { createRequire } from "node:module";
|
||||
import {
|
||||
MediaStreamTrack,
|
||||
RtpHeader,
|
||||
@@ -9,17 +8,10 @@ import {
|
||||
import type { Logger as LoggerType } from "../logger.js";
|
||||
import type { TSVoiceData } from "./ts-client.js";
|
||||
import { WEBRTC_UDP_PORT_RANGE } from "./webrtc-config.js";
|
||||
import { OpusEncoder } from "./opus-codec.js";
|
||||
|
||||
export { DEFAULT_WEBRTC_UDP_PORT_RANGE, WEBRTC_UDP_PORT_RANGE } from "./webrtc-config.js";
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
const { OpusEncoder } = require("@discordjs/opus") as {
|
||||
OpusEncoder: new (sampleRate: number, channels: number) => {
|
||||
decode(data: Buffer): Buffer;
|
||||
encode(data: Buffer): Buffer;
|
||||
};
|
||||
};
|
||||
|
||||
const AUDIO_SAMPLE_RATE = 48_000;
|
||||
const AUDIO_FRAME_SAMPLES = 960;
|
||||
const AUDIO_FRAME_BYTES = AUDIO_FRAME_SAMPLES * 2;
|
||||
@@ -109,7 +101,7 @@ export class WebRtcAudioSession {
|
||||
private readonly onVoiceFrame: (data: Buffer, codec: 4 | 5) => void;
|
||||
private readonly onVoiceActivity: (clientIds: number[]) => void;
|
||||
private readonly outgoingTrack: MediaStreamTrack;
|
||||
private readonly decoderByClient = new Map<number, { decode(data: Buffer): Buffer }>();
|
||||
private readonly decoderByClient = new Map<number, OpusEncoder>();
|
||||
private readonly partialPcmByClient = new Map<number, Buffer>();
|
||||
private readonly pendingFrames = new Map<number, PendingAudioFrame[]>();
|
||||
private readonly memberVolumes = new Map<number, number>();
|
||||
@@ -117,7 +109,7 @@ export class WebRtcAudioSession {
|
||||
private readonly opusPayloadTypes = new Set<number>();
|
||||
private audioTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
private readonly activityTimer: ReturnType<typeof setInterval>;
|
||||
private encoder: { encode(data: Buffer): Buffer } | null;
|
||||
private encoder: OpusEncoder | null;
|
||||
private sequenceNumber = randomInt(0, 65_536);
|
||||
private timestamp = randomInt(0, 0x1_0000_0000) >>> 0;
|
||||
private readonly ssrc = randomInt(1, 0x1_0000_0000) >>> 0;
|
||||
@@ -316,7 +308,9 @@ export class WebRtcAudioSession {
|
||||
this.memberVolumes.clear();
|
||||
this.partialPcmByClient.clear();
|
||||
this.activeSpeakerIds.clear();
|
||||
for (const decoder of this.decoderByClient.values()) decoder.dispose();
|
||||
this.decoderByClient.clear();
|
||||
this.encoder?.dispose();
|
||||
this.encoder = null;
|
||||
this.outgoingTrack.stop();
|
||||
await this.peer.close();
|
||||
|
||||
Reference in new issue
Block a user