@dtelecom/server-sdk-node 0.2.2 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +8 -11
- package/dist/index.js +71 -54
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -515,8 +515,7 @@ declare class AudioFrame {
|
|
|
515
515
|
* AudioSource — feeds PCM16 audio into a local audio track.
|
|
516
516
|
*
|
|
517
517
|
* Handles:
|
|
518
|
-
* -
|
|
519
|
-
* - Opus encoding
|
|
518
|
+
* - Opus encoding (at input sample rate — libopus handles internal resampling)
|
|
520
519
|
* - Frame buffering to ensure exact 20ms frame boundaries
|
|
521
520
|
* - RTP packetization
|
|
522
521
|
*/
|
|
@@ -528,7 +527,7 @@ declare class AudioSource {
|
|
|
528
527
|
private track;
|
|
529
528
|
private sampleBuffer;
|
|
530
529
|
private bufferOffset;
|
|
531
|
-
private readonly
|
|
530
|
+
private readonly frameSize;
|
|
532
531
|
private _onEncodedFrame;
|
|
533
532
|
/**
|
|
534
533
|
* @param sampleRate Input sample rate (e.g. 16000 for STT/TTS)
|
|
@@ -542,8 +541,8 @@ declare class AudioSource {
|
|
|
542
541
|
/**
|
|
543
542
|
* Feed a PCM16 audio frame into the source.
|
|
544
543
|
*
|
|
545
|
-
*
|
|
546
|
-
*
|
|
544
|
+
* Buffered to 20ms boundaries, Opus-encoded at input sample rate,
|
|
545
|
+
* and sent to the track for RTP transmission.
|
|
547
546
|
*/
|
|
548
547
|
captureFrame(frame: AudioFrame): Promise<void>;
|
|
549
548
|
/** Clear any buffered samples */
|
|
@@ -836,16 +835,14 @@ declare class Room extends TypedEmitter<RoomEvents> {
|
|
|
836
835
|
private setupEngineHandlers;
|
|
837
836
|
private setupSignalHandlers;
|
|
838
837
|
private handleParticipantUpdate;
|
|
839
|
-
/** Pending tracks waiting
|
|
838
|
+
/** Pending tracks waiting for participant/publication info to arrive */
|
|
840
839
|
private pendingTracks;
|
|
841
840
|
private handleRemoteTrack;
|
|
842
841
|
/**
|
|
843
|
-
* Match a media track to
|
|
844
|
-
*
|
|
845
|
-
* 1. Try matching by mid (SFU assigns mid in TrackInfo)
|
|
846
|
-
* 2. Fall back to first unassigned audio publication (for SFUs that don't set mid)
|
|
842
|
+
* Match a media track to the correct participant and publication using
|
|
843
|
+
* the SFU-provided participant SID and track SID. No heuristics needed.
|
|
847
844
|
*/
|
|
848
|
-
private
|
|
845
|
+
private matchTrackToParticipant;
|
|
849
846
|
/** Try to match any pending tracks after participant updates arrive */
|
|
850
847
|
private flushPendingTracks;
|
|
851
848
|
private handleDataMessage;
|
package/dist/index.js
CHANGED
|
@@ -1894,19 +1894,23 @@ var AudioFrame = class _AudioFrame {
|
|
|
1894
1894
|
|
|
1895
1895
|
// src/audio/opus-encoder.ts
|
|
1896
1896
|
var log3 = createLogger("OpusEncoder");
|
|
1897
|
-
var OPUS_SAMPLE_RATE = 48e3;
|
|
1898
1897
|
var OPUS_FRAME_DURATION_MS = 20;
|
|
1898
|
+
var OPUS_SAMPLE_RATE = 48e3;
|
|
1899
1899
|
var OPUS_FRAME_SIZE = OPUS_SAMPLE_RATE * OPUS_FRAME_DURATION_MS / 1e3;
|
|
1900
|
+
var VALID_OPUS_RATES = [8e3, 12e3, 16e3, 24e3, 48e3];
|
|
1900
1901
|
var OpusEncoderClass = null;
|
|
1902
|
+
var opusLibraryName = "";
|
|
1901
1903
|
function getOpusEncoder() {
|
|
1902
1904
|
if (!OpusEncoderClass) {
|
|
1903
1905
|
try {
|
|
1904
1906
|
const opus = require("@discordjs/opus");
|
|
1905
1907
|
OpusEncoderClass = opus.OpusEncoder;
|
|
1908
|
+
opusLibraryName = "@discordjs/opus (native)";
|
|
1906
1909
|
} catch {
|
|
1907
1910
|
try {
|
|
1908
1911
|
const OpusScript = require("opusscript");
|
|
1909
1912
|
OpusEncoderClass = OpusScript;
|
|
1913
|
+
opusLibraryName = "opusscript (WASM)";
|
|
1910
1914
|
} catch {
|
|
1911
1915
|
throw new Error(
|
|
1912
1916
|
"No Opus library found. Install @discordjs/opus (recommended) or opusscript (WASM fallback)."
|
|
@@ -1919,32 +1923,45 @@ function getOpusEncoder() {
|
|
|
1919
1923
|
var OpusEncoder = class {
|
|
1920
1924
|
encoder;
|
|
1921
1925
|
channels;
|
|
1926
|
+
/** Number of samples per channel in a 20ms frame at the encoder's sample rate */
|
|
1927
|
+
frameSize;
|
|
1922
1928
|
/**
|
|
1923
1929
|
* Create an Opus encoder.
|
|
1924
|
-
* @param sampleRate
|
|
1930
|
+
* @param sampleRate Sample rate — must be a valid Opus rate (8000, 12000, 16000, 24000, 48000)
|
|
1925
1931
|
* @param channels Number of channels (1 = mono, 2 = stereo)
|
|
1926
1932
|
* @param bitrate Target bitrate in bps (default: 64000)
|
|
1927
1933
|
*/
|
|
1928
|
-
constructor(sampleRate =
|
|
1929
|
-
if (sampleRate
|
|
1930
|
-
throw new Error(`Opus
|
|
1934
|
+
constructor(sampleRate = 48e3, channels = 1, bitrate = 64e3) {
|
|
1935
|
+
if (!VALID_OPUS_RATES.includes(sampleRate)) {
|
|
1936
|
+
throw new Error(`Invalid Opus sample rate: ${sampleRate}Hz. Valid rates: ${VALID_OPUS_RATES.join(", ")}`);
|
|
1931
1937
|
}
|
|
1932
1938
|
this.channels = channels;
|
|
1939
|
+
this.frameSize = sampleRate * OPUS_FRAME_DURATION_MS / 1e3;
|
|
1933
1940
|
const Encoder = getOpusEncoder();
|
|
1934
1941
|
this.encoder = new Encoder(sampleRate, channels);
|
|
1935
1942
|
if (typeof this.encoder.setBitrate === "function") {
|
|
1936
1943
|
this.encoder.setBitrate(bitrate);
|
|
1937
1944
|
}
|
|
1938
|
-
|
|
1945
|
+
if (typeof this.encoder.applyEncoderCTL === "function") {
|
|
1946
|
+
try {
|
|
1947
|
+
this.encoder.applyEncoderCTL(4016, 1);
|
|
1948
|
+
log3.info(`Opus encoder: ${opusLibraryName}, ${sampleRate}Hz, ${channels}ch, ${bitrate / 1e3}kbps, frameSize=${this.frameSize}, DTX=on`);
|
|
1949
|
+
} catch (err) {
|
|
1950
|
+
log3.warn("Failed to enable DTX:", err);
|
|
1951
|
+
log3.info(`Opus encoder: ${opusLibraryName}, ${sampleRate}Hz, ${channels}ch, ${bitrate / 1e3}kbps, frameSize=${this.frameSize}`);
|
|
1952
|
+
}
|
|
1953
|
+
} else {
|
|
1954
|
+
log3.info(`Opus encoder: ${opusLibraryName}, ${sampleRate}Hz, ${channels}ch, ${bitrate / 1e3}kbps, frameSize=${this.frameSize}`);
|
|
1955
|
+
}
|
|
1939
1956
|
}
|
|
1940
1957
|
/**
|
|
1941
1958
|
* Encode a PCM16 frame to Opus.
|
|
1942
|
-
* @param pcm PCM16 samples (Int16Array or Buffer). Must be exactly
|
|
1959
|
+
* @param pcm PCM16 samples (Int16Array or Buffer). Must be exactly frameSize * channels samples.
|
|
1943
1960
|
* @returns Opus-encoded bytes
|
|
1944
1961
|
*/
|
|
1945
1962
|
encode(pcm) {
|
|
1946
1963
|
const buf = pcm instanceof Int16Array ? Buffer.from(pcm.buffer, pcm.byteOffset, pcm.byteLength) : pcm;
|
|
1947
|
-
return this.encoder.encode(buf,
|
|
1964
|
+
return this.encoder.encode(buf, this.frameSize);
|
|
1948
1965
|
}
|
|
1949
1966
|
/** Clean up native resources */
|
|
1950
1967
|
destroy() {
|
|
@@ -2747,6 +2764,16 @@ var RemoteParticipant = class extends TypedEmitter {
|
|
|
2747
2764
|
|
|
2748
2765
|
// src/room.ts
|
|
2749
2766
|
var log9 = createLogger("Room");
|
|
2767
|
+
function unpackStreamId(packed) {
|
|
2768
|
+
const pipeIndex = packed.indexOf("|");
|
|
2769
|
+
if (pipeIndex >= 0) {
|
|
2770
|
+
return {
|
|
2771
|
+
participantSid: packed.substring(0, pipeIndex),
|
|
2772
|
+
trackSid: packed.substring(pipeIndex + 1)
|
|
2773
|
+
};
|
|
2774
|
+
}
|
|
2775
|
+
return { participantSid: packed, trackSid: "" };
|
|
2776
|
+
}
|
|
2750
2777
|
var Room = class extends TypedEmitter {
|
|
2751
2778
|
/** Local participant (this bot) */
|
|
2752
2779
|
localParticipant;
|
|
@@ -2902,56 +2929,49 @@ var Room = class extends TypedEmitter {
|
|
|
2902
2929
|
this.flushPendingTracks();
|
|
2903
2930
|
}
|
|
2904
2931
|
}
|
|
2905
|
-
/** Pending tracks waiting
|
|
2932
|
+
/** Pending tracks waiting for participant/publication info to arrive */
|
|
2906
2933
|
pendingTracks = [];
|
|
2907
2934
|
handleRemoteTrack(mediaTrack, transceiver) {
|
|
2908
|
-
const mid = String(transceiver.mid);
|
|
2909
2935
|
const mediaKind = mediaTrack?.kind;
|
|
2910
2936
|
const me = this.localParticipant?.identity ?? "?";
|
|
2911
2937
|
if (mediaKind !== "audio") {
|
|
2912
|
-
log9.debug(`[${me}] Skipping ${mediaKind} track (
|
|
2938
|
+
log9.debug(`[${me}] Skipping ${mediaKind} track (audio-only SDK)`);
|
|
2939
|
+
return;
|
|
2940
|
+
}
|
|
2941
|
+
const streamId = transceiver.receiver?.remoteStreamId ?? "";
|
|
2942
|
+
const { participantSid, trackSid } = unpackStreamId(streamId);
|
|
2943
|
+
log9.debug(`[${me}] handleRemoteTrack: streamId="${streamId}", participantSid="${participantSid}", trackSid="${trackSid}"`);
|
|
2944
|
+
if (!participantSid || !trackSid) {
|
|
2945
|
+
log9.warn(`[${me}] Cannot match track: missing IDs in streamId "${streamId}"`);
|
|
2913
2946
|
return;
|
|
2914
2947
|
}
|
|
2915
|
-
|
|
2916
|
-
|
|
2917
|
-
|
|
2918
|
-
|
|
2948
|
+
if (!this.matchTrackToParticipant(mediaTrack, participantSid, trackSid)) {
|
|
2949
|
+
log9.debug(`[${me}] Queuing unmatched track (participantSid=${participantSid}, trackSid=${trackSid})`);
|
|
2950
|
+
this.pendingTracks.push({ mediaTrack, participantSid, trackSid });
|
|
2951
|
+
}
|
|
2919
2952
|
}
|
|
2920
2953
|
/**
|
|
2921
|
-
* Match a media track to
|
|
2922
|
-
*
|
|
2923
|
-
* 1. Try matching by mid (SFU assigns mid in TrackInfo)
|
|
2924
|
-
* 2. Fall back to first unassigned audio publication (for SFUs that don't set mid)
|
|
2954
|
+
* Match a media track to the correct participant and publication using
|
|
2955
|
+
* the SFU-provided participant SID and track SID. No heuristics needed.
|
|
2925
2956
|
*/
|
|
2926
|
-
|
|
2957
|
+
matchTrackToParticipant(mediaTrack, participantSid, trackSid) {
|
|
2927
2958
|
const me = this.localParticipant?.identity ?? "?";
|
|
2928
|
-
|
|
2929
|
-
|
|
2930
|
-
|
|
2931
|
-
|
|
2932
|
-
|
|
2933
|
-
|
|
2934
|
-
return true;
|
|
2935
|
-
}
|
|
2936
|
-
}
|
|
2959
|
+
const participant = this.remoteParticipants.get(participantSid);
|
|
2960
|
+
if (!participant) return false;
|
|
2961
|
+
const pub = participant.trackPublications.get(trackSid);
|
|
2962
|
+
if (!pub) return false;
|
|
2963
|
+
if (pub.track) {
|
|
2964
|
+
participant.removeTrack(trackSid);
|
|
2937
2965
|
}
|
|
2938
|
-
|
|
2939
|
-
|
|
2940
|
-
|
|
2941
|
-
log9.debug(`[${me}] Matched mid=${mid} \u2192 ${participant.identity}/${pub.name} (by kind, unassigned)`);
|
|
2942
|
-
participant.addSubscribedTrack(mediaTrack, sid, pub.name);
|
|
2943
|
-
return true;
|
|
2944
|
-
}
|
|
2945
|
-
}
|
|
2946
|
-
}
|
|
2947
|
-
log9.debug(`[${me}] No match for mid=${mid} (pubs: ${[...this.remoteParticipants.values()].map((p) => `${p.identity}=[${[...p.trackPublications.values()].map((t) => `${t.name}:mid=${t.mid}:has=${!!t.track}`).join(",")}]`).join(", ")})`);
|
|
2948
|
-
return false;
|
|
2966
|
+
log9.info(`[${me}] Track matched: ${participant.identity}/${pub.name} (trackSid=${trackSid})`);
|
|
2967
|
+
participant.addSubscribedTrack(mediaTrack, trackSid, pub.name);
|
|
2968
|
+
return true;
|
|
2949
2969
|
}
|
|
2950
2970
|
/** Try to match any pending tracks after participant updates arrive */
|
|
2951
2971
|
flushPendingTracks() {
|
|
2952
2972
|
const remaining = [];
|
|
2953
2973
|
for (const pending of this.pendingTracks) {
|
|
2954
|
-
if (!this.
|
|
2974
|
+
if (!this.matchTrackToParticipant(pending.mediaTrack, pending.participantSid, pending.trackSid)) {
|
|
2955
2975
|
remaining.push(pending);
|
|
2956
2976
|
}
|
|
2957
2977
|
}
|
|
@@ -3024,10 +3044,10 @@ var AudioSource = class {
|
|
|
3024
3044
|
channels;
|
|
3025
3045
|
encoder = null;
|
|
3026
3046
|
track = null;
|
|
3027
|
-
// Buffer for accumulating samples to form exact 20ms frames
|
|
3047
|
+
// Buffer for accumulating samples to form exact 20ms frames
|
|
3028
3048
|
sampleBuffer;
|
|
3029
3049
|
bufferOffset = 0;
|
|
3030
|
-
|
|
3050
|
+
frameSize;
|
|
3031
3051
|
// Callback set by LocalAudioTrack to receive encoded Opus frames
|
|
3032
3052
|
_onEncodedFrame = null;
|
|
3033
3053
|
/**
|
|
@@ -3037,8 +3057,8 @@ var AudioSource = class {
|
|
|
3037
3057
|
constructor(sampleRate, channels = 1) {
|
|
3038
3058
|
this.sampleRate = sampleRate;
|
|
3039
3059
|
this.channels = channels;
|
|
3040
|
-
this.
|
|
3041
|
-
this.sampleBuffer = new Int16Array(this.
|
|
3060
|
+
this.frameSize = sampleRate * 20 / 1e3 * channels;
|
|
3061
|
+
this.sampleBuffer = new Int16Array(this.frameSize);
|
|
3042
3062
|
}
|
|
3043
3063
|
/** Set the callback for encoded Opus frames. Used internally by LocalAudioTrack. */
|
|
3044
3064
|
set onEncodedFrame(cb) {
|
|
@@ -3051,26 +3071,23 @@ var AudioSource = class {
|
|
|
3051
3071
|
/**
|
|
3052
3072
|
* Feed a PCM16 audio frame into the source.
|
|
3053
3073
|
*
|
|
3054
|
-
*
|
|
3055
|
-
*
|
|
3074
|
+
* Buffered to 20ms boundaries, Opus-encoded at input sample rate,
|
|
3075
|
+
* and sent to the track for RTP transmission.
|
|
3056
3076
|
*/
|
|
3057
3077
|
async captureFrame(frame) {
|
|
3058
3078
|
if (!this.encoder) {
|
|
3059
|
-
this.encoder = new OpusEncoder(
|
|
3060
|
-
}
|
|
3061
|
-
let samples = frame.data;
|
|
3062
|
-
if (frame.sampleRate !== OPUS_SAMPLE_RATE) {
|
|
3063
|
-
samples = upsample(frame.data, frame.sampleRate, OPUS_SAMPLE_RATE, this.channels);
|
|
3079
|
+
this.encoder = new OpusEncoder(this.sampleRate, this.channels);
|
|
3064
3080
|
}
|
|
3081
|
+
const samples = frame.data;
|
|
3065
3082
|
let offset = 0;
|
|
3066
3083
|
while (offset < samples.length) {
|
|
3067
|
-
const remaining = this.
|
|
3084
|
+
const remaining = this.frameSize - this.bufferOffset;
|
|
3068
3085
|
const available = samples.length - offset;
|
|
3069
3086
|
const toCopy = Math.min(remaining, available);
|
|
3070
3087
|
this.sampleBuffer.set(samples.subarray(offset, offset + toCopy), this.bufferOffset);
|
|
3071
3088
|
this.bufferOffset += toCopy;
|
|
3072
3089
|
offset += toCopy;
|
|
3073
|
-
if (this.bufferOffset >= this.
|
|
3090
|
+
if (this.bufferOffset >= this.frameSize) {
|
|
3074
3091
|
this.encodeAndSend(this.sampleBuffer);
|
|
3075
3092
|
this.bufferOffset = 0;
|
|
3076
3093
|
}
|