@dtelecom/server-sdk-node 0.1.1 → 0.2.0
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 +46 -3
- package/dist/index.js +224 -52
- package/dist/index.js.map +1 -1
- package/package.json +4 -3
package/dist/index.d.ts
CHANGED
|
@@ -328,6 +328,11 @@ declare const StreamStateUpdate: {
|
|
|
328
328
|
*
|
|
329
329
|
* Handles binary protobuf signaling: SignalRequest (client→server)
|
|
330
330
|
* and SignalResponse (server→client).
|
|
331
|
+
*
|
|
332
|
+
* Keepalive: sends application-level protobuf pings at the interval specified
|
|
333
|
+
* by the server's JoinResponse (pingInterval / pingTimeout). This matches
|
|
334
|
+
* the official client-sdk-js behaviour. If no pong is received within
|
|
335
|
+
* pingTimeout seconds the connection is considered dead.
|
|
331
336
|
*/
|
|
332
337
|
|
|
333
338
|
interface SignalOptions {
|
|
@@ -356,9 +361,17 @@ interface SignalEvents {
|
|
|
356
361
|
}
|
|
357
362
|
declare class SignalClient extends TypedEmitter<SignalEvents> {
|
|
358
363
|
private ws;
|
|
359
|
-
private pingInterval;
|
|
360
364
|
private _isConnected;
|
|
361
365
|
private joinResponse;
|
|
366
|
+
/** App-level ping interval timer (sends ping every pingInterval seconds). */
|
|
367
|
+
private pingIntervalTimer;
|
|
368
|
+
/** App-level pong timeout timer (fires if no pong within pingTimeout seconds). */
|
|
369
|
+
private pingTimeoutTimer;
|
|
370
|
+
/** Server-provided ping config (seconds). */
|
|
371
|
+
private pingIntervalS;
|
|
372
|
+
private pingTimeoutS;
|
|
373
|
+
/** RTT measured from last ping/pong round-trip. */
|
|
374
|
+
private rtt;
|
|
362
375
|
get isConnected(): boolean;
|
|
363
376
|
/**
|
|
364
377
|
* Connect to the dTelecom SFU signaling server.
|
|
@@ -384,8 +397,17 @@ declare class SignalClient extends TypedEmitter<SignalEvents> {
|
|
|
384
397
|
private buildUrl;
|
|
385
398
|
private sendRequest;
|
|
386
399
|
private handleResponse;
|
|
387
|
-
|
|
388
|
-
|
|
400
|
+
/**
|
|
401
|
+
* Send app-level ping (both legacy and new format, matching client-sdk-js).
|
|
402
|
+
* Proto: SignalRequest.ping (field 14) + SignalRequest.ping_req (field 16).
|
|
403
|
+
*/
|
|
404
|
+
private sendPing;
|
|
405
|
+
/** Start sending pings at pingIntervalS and watching for pong timeout. */
|
|
406
|
+
private startPingInterval;
|
|
407
|
+
/** Stop ping interval + timeout timers. */
|
|
408
|
+
private stopPingInterval;
|
|
409
|
+
/** Reset the pong-timeout timer. Called when we receive a pong. */
|
|
410
|
+
private resetPingTimeout;
|
|
389
411
|
}
|
|
390
412
|
|
|
391
413
|
/**
|
|
@@ -432,6 +454,12 @@ declare class RTCEngine extends TypedEmitter<EngineEvents> {
|
|
|
432
454
|
get lossyDataChannel(): RTCDataChannel | null;
|
|
433
455
|
constructor();
|
|
434
456
|
connect(url: string, token: string, options?: EngineOptions): Promise<JoinResponse>;
|
|
457
|
+
/**
|
|
458
|
+
* Wait for the primary PeerConnection ICE to reach "connected".
|
|
459
|
+
* When subscriberPrimary=true the subscriber PC is primary;
|
|
460
|
+
* otherwise the publisher PC is primary.
|
|
461
|
+
*/
|
|
462
|
+
private waitForPrimaryConnected;
|
|
435
463
|
addTransceiver(track: MediaStreamTrack): Promise<RTCRtpTransceiver>;
|
|
436
464
|
requestPublishTrack(cid: string, name: string, type: TrackType, source: TrackSource, options?: {
|
|
437
465
|
disableDtx?: boolean;
|
|
@@ -579,6 +607,8 @@ declare class TrackPublication extends TypedEmitter<TrackPublicationEvents> {
|
|
|
579
607
|
source: TrackSource;
|
|
580
608
|
mimeType: string;
|
|
581
609
|
muted: boolean;
|
|
610
|
+
/** Media ID assigned by SFU — used to match WebRTC transceivers to publications */
|
|
611
|
+
mid: string;
|
|
582
612
|
constructor(info: TrackInfo);
|
|
583
613
|
updateInfo(info: TrackInfo): void;
|
|
584
614
|
}
|
|
@@ -779,6 +809,8 @@ declare class Room extends TypedEmitter<RoomEvents> {
|
|
|
779
809
|
private _isConnected;
|
|
780
810
|
private activeSpeakers;
|
|
781
811
|
private roomInfo;
|
|
812
|
+
/** Identity-prefixed logger for debugging multi-Room scenarios */
|
|
813
|
+
private log;
|
|
782
814
|
constructor();
|
|
783
815
|
get isConnected(): boolean;
|
|
784
816
|
/**
|
|
@@ -801,7 +833,18 @@ declare class Room extends TypedEmitter<RoomEvents> {
|
|
|
801
833
|
private setupEngineHandlers;
|
|
802
834
|
private setupSignalHandlers;
|
|
803
835
|
private handleParticipantUpdate;
|
|
836
|
+
/** Pending tracks waiting to be matched to a participant publication */
|
|
837
|
+
private pendingTracks;
|
|
804
838
|
private handleRemoteTrack;
|
|
839
|
+
/**
|
|
840
|
+
* Match a media track to a participant publication.
|
|
841
|
+
* Strategy:
|
|
842
|
+
* 1. Try matching by mid (SFU assigns mid in TrackInfo)
|
|
843
|
+
* 2. Fall back to first unassigned audio publication (for SFUs that don't set mid)
|
|
844
|
+
*/
|
|
845
|
+
private matchTrack;
|
|
846
|
+
/** Try to match any pending tracks after participant updates arrive */
|
|
847
|
+
private flushPendingTracks;
|
|
805
848
|
private handleDataMessage;
|
|
806
849
|
private handleSpeakersChanged;
|
|
807
850
|
private getOrCreateParticipant;
|
package/dist/index.js
CHANGED
|
@@ -888,7 +888,13 @@ var SignalRequest = {
|
|
|
888
888
|
writeMessage(writer, 8, (w) => LeaveRequest.encode(message.leave, w));
|
|
889
889
|
}
|
|
890
890
|
if (message.ping !== void 0) {
|
|
891
|
-
writer.uint32(
|
|
891
|
+
writer.uint32(112).int64(message.ping);
|
|
892
|
+
}
|
|
893
|
+
if (message.pingReq !== void 0) {
|
|
894
|
+
const nested = writer.uint32(130).fork();
|
|
895
|
+
nested.uint32(8).int64(message.pingReq.timestamp);
|
|
896
|
+
nested.uint32(16).int64(message.pingReq.rtt);
|
|
897
|
+
nested.ldelim();
|
|
892
898
|
}
|
|
893
899
|
return writer;
|
|
894
900
|
}
|
|
@@ -946,6 +952,26 @@ var SignalResponse = {
|
|
|
946
952
|
case 18:
|
|
947
953
|
message.pong = reader.int64();
|
|
948
954
|
break;
|
|
955
|
+
case 20: {
|
|
956
|
+
const pongEnd = reader.pos + reader.uint32();
|
|
957
|
+
const pongResp = { lastPingTimestamp: 0, timestamp: 0 };
|
|
958
|
+
while (reader.pos < pongEnd) {
|
|
959
|
+
const pongTag = reader.uint32();
|
|
960
|
+
switch (pongTag >>> 3) {
|
|
961
|
+
case 1:
|
|
962
|
+
pongResp.lastPingTimestamp = reader.int64();
|
|
963
|
+
break;
|
|
964
|
+
case 2:
|
|
965
|
+
pongResp.timestamp = reader.int64();
|
|
966
|
+
break;
|
|
967
|
+
default:
|
|
968
|
+
reader.skipType(pongTag & 7);
|
|
969
|
+
break;
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
message.pongResp = pongResp;
|
|
973
|
+
break;
|
|
974
|
+
}
|
|
949
975
|
default:
|
|
950
976
|
reader.skipType(tag & 7);
|
|
951
977
|
break;
|
|
@@ -1154,9 +1180,17 @@ var SDK_NAME = "node";
|
|
|
1154
1180
|
var SDK_VERSION = "0.1.0";
|
|
1155
1181
|
var SignalClient = class extends TypedEmitter {
|
|
1156
1182
|
ws = null;
|
|
1157
|
-
pingInterval = null;
|
|
1158
1183
|
_isConnected = false;
|
|
1159
1184
|
joinResponse = null;
|
|
1185
|
+
/** App-level ping interval timer (sends ping every pingInterval seconds). */
|
|
1186
|
+
pingIntervalTimer = null;
|
|
1187
|
+
/** App-level pong timeout timer (fires if no pong within pingTimeout seconds). */
|
|
1188
|
+
pingTimeoutTimer = null;
|
|
1189
|
+
/** Server-provided ping config (seconds). */
|
|
1190
|
+
pingIntervalS = 0;
|
|
1191
|
+
pingTimeoutS = 0;
|
|
1192
|
+
/** RTT measured from last ping/pong round-trip. */
|
|
1193
|
+
rtt = 0;
|
|
1160
1194
|
get isConnected() {
|
|
1161
1195
|
return this._isConnected;
|
|
1162
1196
|
}
|
|
@@ -1198,8 +1232,11 @@ var SignalClient = class extends TypedEmitter {
|
|
|
1198
1232
|
clearTimeout(timeout);
|
|
1199
1233
|
this.joinResponse = response.join;
|
|
1200
1234
|
this._isConnected = true;
|
|
1201
|
-
|
|
1202
|
-
|
|
1235
|
+
this.pingTimeoutS = response.join.pingTimeout;
|
|
1236
|
+
this.pingIntervalS = response.join.pingInterval;
|
|
1237
|
+
if (this.pingTimeoutS > 0) {
|
|
1238
|
+
log.debug(`Ping config: interval=${this.pingIntervalS}s, timeout=${this.pingTimeoutS}s`);
|
|
1239
|
+
this.startPingInterval();
|
|
1203
1240
|
}
|
|
1204
1241
|
resolve(response.join);
|
|
1205
1242
|
}
|
|
@@ -1216,7 +1253,7 @@ var SignalClient = class extends TypedEmitter {
|
|
|
1216
1253
|
this.ws.on("close", (code, reason) => {
|
|
1217
1254
|
clearTimeout(timeout);
|
|
1218
1255
|
this._isConnected = false;
|
|
1219
|
-
this.
|
|
1256
|
+
this.stopPingInterval();
|
|
1220
1257
|
const reasonStr = reason?.toString() || `code ${code}`;
|
|
1221
1258
|
log.info(`WebSocket closed: ${reasonStr}`);
|
|
1222
1259
|
this.emit("close", reasonStr);
|
|
@@ -1260,7 +1297,7 @@ var SignalClient = class extends TypedEmitter {
|
|
|
1260
1297
|
}
|
|
1261
1298
|
/** Close the WebSocket connection */
|
|
1262
1299
|
close() {
|
|
1263
|
-
this.
|
|
1300
|
+
this.stopPingInterval();
|
|
1264
1301
|
this._isConnected = false;
|
|
1265
1302
|
if (this.ws) {
|
|
1266
1303
|
this.ws.removeAllListeners();
|
|
@@ -1305,6 +1342,10 @@ var SignalClient = class extends TypedEmitter {
|
|
|
1305
1342
|
}
|
|
1306
1343
|
}
|
|
1307
1344
|
handleResponse(response) {
|
|
1345
|
+
const types = Object.keys(response).filter((k) => response[k] !== void 0 && response[k] !== null);
|
|
1346
|
+
if (types.length > 0) {
|
|
1347
|
+
log.debug(`Signal response: ${types.join(", ")}`);
|
|
1348
|
+
}
|
|
1308
1349
|
if (response.offer) {
|
|
1309
1350
|
log.debug(`Received OFFER from server (type=${response.offer.type})`);
|
|
1310
1351
|
this.emit("offer", response.offer);
|
|
@@ -1348,21 +1389,63 @@ var SignalClient = class extends TypedEmitter {
|
|
|
1348
1389
|
if (response.mute) {
|
|
1349
1390
|
}
|
|
1350
1391
|
if (response.pong !== void 0) {
|
|
1392
|
+
this.resetPingTimeout();
|
|
1351
1393
|
}
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
const intervalMs = intervalSec * 1e3;
|
|
1356
|
-
this.pingInterval = setInterval(() => {
|
|
1357
|
-
this.sendRequest({ ping: Date.now() });
|
|
1358
|
-
}, intervalMs);
|
|
1359
|
-
}
|
|
1360
|
-
stopPing() {
|
|
1361
|
-
if (this.pingInterval) {
|
|
1362
|
-
clearInterval(this.pingInterval);
|
|
1363
|
-
this.pingInterval = null;
|
|
1394
|
+
if (response.pongResp) {
|
|
1395
|
+
this.rtt = Date.now() - response.pongResp.lastPingTimestamp;
|
|
1396
|
+
this.resetPingTimeout();
|
|
1364
1397
|
}
|
|
1365
1398
|
}
|
|
1399
|
+
/**
|
|
1400
|
+
* Send app-level ping (both legacy and new format, matching client-sdk-js).
|
|
1401
|
+
* Proto: SignalRequest.ping (field 14) + SignalRequest.ping_req (field 16).
|
|
1402
|
+
*/
|
|
1403
|
+
sendPing() {
|
|
1404
|
+
const now = Date.now();
|
|
1405
|
+
this.sendRequest({ ping: now });
|
|
1406
|
+
this.sendRequest({ pingReq: { timestamp: now, rtt: this.rtt } });
|
|
1407
|
+
}
|
|
1408
|
+
/** Start sending pings at pingIntervalS and watching for pong timeout. */
|
|
1409
|
+
startPingInterval() {
|
|
1410
|
+
this.stopPingInterval();
|
|
1411
|
+
this.resetPingTimeout();
|
|
1412
|
+
if (!this.pingIntervalS) return;
|
|
1413
|
+
this.pingIntervalTimer = setInterval(() => {
|
|
1414
|
+
this.sendPing();
|
|
1415
|
+
}, this.pingIntervalS * 1e3);
|
|
1416
|
+
}
|
|
1417
|
+
/** Stop ping interval + timeout timers. */
|
|
1418
|
+
stopPingInterval() {
|
|
1419
|
+
if (this.pingIntervalTimer) {
|
|
1420
|
+
clearInterval(this.pingIntervalTimer);
|
|
1421
|
+
this.pingIntervalTimer = null;
|
|
1422
|
+
}
|
|
1423
|
+
if (this.pingTimeoutTimer) {
|
|
1424
|
+
clearTimeout(this.pingTimeoutTimer);
|
|
1425
|
+
this.pingTimeoutTimer = null;
|
|
1426
|
+
}
|
|
1427
|
+
}
|
|
1428
|
+
/** Reset the pong-timeout timer. Called when we receive a pong. */
|
|
1429
|
+
resetPingTimeout() {
|
|
1430
|
+
if (this.pingTimeoutTimer) {
|
|
1431
|
+
clearTimeout(this.pingTimeoutTimer);
|
|
1432
|
+
this.pingTimeoutTimer = null;
|
|
1433
|
+
}
|
|
1434
|
+
if (!this.pingTimeoutS) return;
|
|
1435
|
+
this.pingTimeoutTimer = setTimeout(() => {
|
|
1436
|
+
this.pingTimeoutTimer = null;
|
|
1437
|
+
if (this._isConnected) {
|
|
1438
|
+
log.warn(`Ping timeout \u2014 no pong received within ${this.pingTimeoutS}s`);
|
|
1439
|
+
this._isConnected = false;
|
|
1440
|
+
if (this.ws) {
|
|
1441
|
+
this.ws.removeAllListeners();
|
|
1442
|
+
this.ws.close();
|
|
1443
|
+
this.ws = null;
|
|
1444
|
+
}
|
|
1445
|
+
this.emit("close", "ping_timeout");
|
|
1446
|
+
}
|
|
1447
|
+
}, this.pingTimeoutS * 1e3);
|
|
1448
|
+
}
|
|
1366
1449
|
};
|
|
1367
1450
|
|
|
1368
1451
|
// src/engine.ts
|
|
@@ -1418,8 +1501,39 @@ var RTCEngine = class extends TypedEmitter {
|
|
|
1418
1501
|
if (!this.subscriberPrimary) {
|
|
1419
1502
|
await this.negotiate();
|
|
1420
1503
|
}
|
|
1504
|
+
await this.waitForPrimaryConnected();
|
|
1421
1505
|
return joinResponse;
|
|
1422
1506
|
}
|
|
1507
|
+
/**
|
|
1508
|
+
* Wait for the primary PeerConnection ICE to reach "connected".
|
|
1509
|
+
* When subscriberPrimary=true the subscriber PC is primary;
|
|
1510
|
+
* otherwise the publisher PC is primary.
|
|
1511
|
+
*/
|
|
1512
|
+
waitForPrimaryConnected(timeoutMs = 15e3) {
|
|
1513
|
+
const pc = this.subscriberPrimary ? this.subscriber : this.publisher;
|
|
1514
|
+
if (!pc) return Promise.resolve();
|
|
1515
|
+
const iceState = pc.iceConnectionState;
|
|
1516
|
+
if (iceState === "connected" || iceState === "completed") {
|
|
1517
|
+
return Promise.resolve();
|
|
1518
|
+
}
|
|
1519
|
+
return new Promise((resolve, reject) => {
|
|
1520
|
+
const timeout = setTimeout(() => {
|
|
1521
|
+
reject(new Error(
|
|
1522
|
+
`Primary PC ICE timed out after ${timeoutMs}ms (state: ${pc.iceConnectionState})`
|
|
1523
|
+
));
|
|
1524
|
+
}, timeoutMs);
|
|
1525
|
+
const handler = (state) => {
|
|
1526
|
+
if (state === "connected" || state === "completed") {
|
|
1527
|
+
clearTimeout(timeout);
|
|
1528
|
+
resolve();
|
|
1529
|
+
} else if (state === "failed") {
|
|
1530
|
+
clearTimeout(timeout);
|
|
1531
|
+
reject(new Error("Primary PC ICE failed"));
|
|
1532
|
+
}
|
|
1533
|
+
};
|
|
1534
|
+
pc.iceConnectionStateChange.subscribe(handler);
|
|
1535
|
+
});
|
|
1536
|
+
}
|
|
1423
1537
|
async addTransceiver(track) {
|
|
1424
1538
|
if (!this.publisher) {
|
|
1425
1539
|
throw new Error("Publisher PC not initialized");
|
|
@@ -1645,6 +1759,7 @@ var RTCEngine = class extends TypedEmitter {
|
|
|
1645
1759
|
await this.subscriber.setRemoteDescription(
|
|
1646
1760
|
new import_werift.RTCSessionDescription(sd.sdp, sd.type)
|
|
1647
1761
|
);
|
|
1762
|
+
this.flushPendingCandidates(1 /* SUBSCRIBER */);
|
|
1648
1763
|
const answer = await this.subscriber.createAnswer();
|
|
1649
1764
|
await this.subscriber.setLocalDescription(answer);
|
|
1650
1765
|
this.signal.sendAnswer({
|
|
@@ -1699,10 +1814,8 @@ var RTCEngine = class extends TypedEmitter {
|
|
|
1699
1814
|
this.disconnect();
|
|
1700
1815
|
});
|
|
1701
1816
|
this.signal.on("close", (reason) => {
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
this.emit("disconnected", reason);
|
|
1705
|
-
}
|
|
1817
|
+
this._isConnected = false;
|
|
1818
|
+
this.emit("disconnected", reason);
|
|
1706
1819
|
});
|
|
1707
1820
|
}
|
|
1708
1821
|
flushPendingCandidates(target) {
|
|
@@ -2224,6 +2337,8 @@ var TrackPublication = class extends TypedEmitter {
|
|
|
2224
2337
|
source;
|
|
2225
2338
|
mimeType;
|
|
2226
2339
|
muted;
|
|
2340
|
+
/** Media ID assigned by SFU — used to match WebRTC transceivers to publications */
|
|
2341
|
+
mid;
|
|
2227
2342
|
constructor(info) {
|
|
2228
2343
|
super();
|
|
2229
2344
|
this.sid = info.sid;
|
|
@@ -2232,6 +2347,7 @@ var TrackPublication = class extends TypedEmitter {
|
|
|
2232
2347
|
this.source = info.source;
|
|
2233
2348
|
this.mimeType = info.mimeType;
|
|
2234
2349
|
this.muted = info.muted;
|
|
2350
|
+
this.mid = info.mid;
|
|
2235
2351
|
}
|
|
2236
2352
|
updateInfo(info) {
|
|
2237
2353
|
const wasMuted = this.muted;
|
|
@@ -2241,6 +2357,7 @@ var TrackPublication = class extends TypedEmitter {
|
|
|
2241
2357
|
this.source = info.source;
|
|
2242
2358
|
this.mimeType = info.mimeType;
|
|
2243
2359
|
this.muted = info.muted;
|
|
2360
|
+
this.mid = info.mid;
|
|
2244
2361
|
if (wasMuted !== info.muted) {
|
|
2245
2362
|
this.emit(info.muted ? "muted" : "unmuted");
|
|
2246
2363
|
}
|
|
@@ -2540,6 +2657,7 @@ var RemoteParticipant = class extends TypedEmitter {
|
|
|
2540
2657
|
const activeSids = /* @__PURE__ */ new Set();
|
|
2541
2658
|
for (const trackInfo of info.tracks) {
|
|
2542
2659
|
activeSids.add(trackInfo.sid);
|
|
2660
|
+
log8.debug(`Track info for ${this.identity}: sid=${trackInfo.sid}, type=${trackInfo.type}, mid=${trackInfo.mid}`);
|
|
2543
2661
|
const existing = this._trackPublications.get(trackInfo.sid);
|
|
2544
2662
|
if (existing) {
|
|
2545
2663
|
existing.updateInfo(trackInfo);
|
|
@@ -2633,6 +2751,8 @@ var Room = class extends TypedEmitter {
|
|
|
2633
2751
|
_isConnected = false;
|
|
2634
2752
|
activeSpeakers = [];
|
|
2635
2753
|
roomInfo = null;
|
|
2754
|
+
/** Identity-prefixed logger for debugging multi-Room scenarios */
|
|
2755
|
+
log = log9;
|
|
2636
2756
|
constructor() {
|
|
2637
2757
|
super();
|
|
2638
2758
|
this.engine = new RTCEngine();
|
|
@@ -2657,7 +2777,8 @@ var Room = class extends TypedEmitter {
|
|
|
2657
2777
|
this.setupEngineHandlers();
|
|
2658
2778
|
this.setupSignalHandlers();
|
|
2659
2779
|
this._isConnected = true;
|
|
2660
|
-
|
|
2780
|
+
this.log = log9;
|
|
2781
|
+
log9.info(`[${this.localParticipant.identity}] Connected to room "${this.name}"`);
|
|
2661
2782
|
}
|
|
2662
2783
|
/**
|
|
2663
2784
|
* Disconnect from the room.
|
|
@@ -2744,6 +2865,7 @@ var Room = class extends TypedEmitter {
|
|
|
2744
2865
|
});
|
|
2745
2866
|
}
|
|
2746
2867
|
handleParticipantUpdate(update) {
|
|
2868
|
+
const me = this.localParticipant?.identity ?? "?";
|
|
2747
2869
|
for (const info of update.participants) {
|
|
2748
2870
|
if (info.sid === this.localParticipant.sid || info.identity === this.localParticipant.identity) {
|
|
2749
2871
|
this.localParticipant.updateInfo(info);
|
|
@@ -2754,36 +2876,78 @@ var Room = class extends TypedEmitter {
|
|
|
2754
2876
|
if (participant) {
|
|
2755
2877
|
participant.destroy();
|
|
2756
2878
|
this.remoteParticipants.delete(info.sid);
|
|
2757
|
-
log9.info(`Participant disconnected: ${participant.identity}`);
|
|
2879
|
+
log9.info(`[${me}] Participant disconnected: ${participant.identity}`);
|
|
2758
2880
|
this.emit("participantDisconnected", participant);
|
|
2759
2881
|
}
|
|
2760
2882
|
} else {
|
|
2761
|
-
const isNew =
|
|
2762
|
-
const participant = this.getOrCreateParticipant(info);
|
|
2883
|
+
const { participant, isNew } = this.getOrCreateParticipant(info);
|
|
2763
2884
|
if (isNew) {
|
|
2764
|
-
log9.info(`Participant connected: ${participant.identity}`);
|
|
2885
|
+
log9.info(`[${me}] Participant connected: ${participant.identity}`);
|
|
2765
2886
|
this.emit("participantConnected", participant);
|
|
2766
2887
|
}
|
|
2767
2888
|
}
|
|
2768
2889
|
}
|
|
2890
|
+
if (this.pendingTracks.length > 0) {
|
|
2891
|
+
this.flushPendingTracks();
|
|
2892
|
+
}
|
|
2769
2893
|
}
|
|
2894
|
+
/** Pending tracks waiting to be matched to a participant publication */
|
|
2895
|
+
pendingTracks = [];
|
|
2770
2896
|
handleRemoteTrack(mediaTrack, transceiver) {
|
|
2771
|
-
const mid = transceiver.mid;
|
|
2897
|
+
const mid = String(transceiver.mid);
|
|
2772
2898
|
const mediaKind = mediaTrack?.kind;
|
|
2773
|
-
const
|
|
2899
|
+
const me = this.localParticipant?.identity ?? "?";
|
|
2900
|
+
if (mediaKind !== "audio") {
|
|
2901
|
+
log9.debug(`[${me}] Skipping ${mediaKind} track (mid=${mid}, audio-only SDK)`);
|
|
2902
|
+
return;
|
|
2903
|
+
}
|
|
2904
|
+
log9.debug(`[${me}] handleRemoteTrack: mid=${mid}, kind=${mediaKind}`);
|
|
2905
|
+
if (this.matchTrack(mediaTrack, mid)) return;
|
|
2906
|
+
log9.debug(`[${me}] Queuing unmatched audio track (mid=${mid})`);
|
|
2907
|
+
this.pendingTracks.push({ mediaTrack, mid });
|
|
2908
|
+
}
|
|
2909
|
+
/**
|
|
2910
|
+
* Match a media track to a participant publication.
|
|
2911
|
+
* Strategy:
|
|
2912
|
+
* 1. Try matching by mid (SFU assigns mid in TrackInfo)
|
|
2913
|
+
* 2. Fall back to first unassigned audio publication (for SFUs that don't set mid)
|
|
2914
|
+
*/
|
|
2915
|
+
matchTrack(mediaTrack, mid) {
|
|
2916
|
+
const me = this.localParticipant?.identity ?? "?";
|
|
2774
2917
|
for (const participant of this.remoteParticipants.values()) {
|
|
2775
2918
|
for (const [sid, pub] of participant.trackPublications) {
|
|
2776
|
-
if (
|
|
2777
|
-
|
|
2778
|
-
|
|
2779
|
-
return;
|
|
2780
|
-
}
|
|
2919
|
+
if (pub.mid === mid && pub.kind === 0 /* AUDIO */) {
|
|
2920
|
+
log9.debug(`[${me}] Matched mid=${mid} \u2192 ${participant.identity}/${pub.name} (by mid)`);
|
|
2921
|
+
if (pub.track) participant.removeTrack(sid);
|
|
2781
2922
|
participant.addSubscribedTrack(mediaTrack, sid, pub.name);
|
|
2782
|
-
return;
|
|
2923
|
+
return true;
|
|
2783
2924
|
}
|
|
2784
2925
|
}
|
|
2785
2926
|
}
|
|
2786
|
-
|
|
2927
|
+
for (const participant of this.remoteParticipants.values()) {
|
|
2928
|
+
for (const [sid, pub] of participant.trackPublications) {
|
|
2929
|
+
if (!pub.track && pub.kind === 0 /* AUDIO */) {
|
|
2930
|
+
log9.debug(`[${me}] Matched mid=${mid} \u2192 ${participant.identity}/${pub.name} (by kind, unassigned)`);
|
|
2931
|
+
participant.addSubscribedTrack(mediaTrack, sid, pub.name);
|
|
2932
|
+
return true;
|
|
2933
|
+
}
|
|
2934
|
+
}
|
|
2935
|
+
}
|
|
2936
|
+
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(", ")})`);
|
|
2937
|
+
return false;
|
|
2938
|
+
}
|
|
2939
|
+
/** Try to match any pending tracks after participant updates arrive */
|
|
2940
|
+
flushPendingTracks() {
|
|
2941
|
+
const remaining = [];
|
|
2942
|
+
for (const pending of this.pendingTracks) {
|
|
2943
|
+
if (!this.matchTrack(pending.mediaTrack, pending.mid)) {
|
|
2944
|
+
remaining.push(pending);
|
|
2945
|
+
}
|
|
2946
|
+
}
|
|
2947
|
+
if (this.pendingTracks.length !== remaining.length) {
|
|
2948
|
+
log9.debug(`Matched ${this.pendingTracks.length - remaining.length} pending tracks`);
|
|
2949
|
+
}
|
|
2950
|
+
this.pendingTracks = remaining;
|
|
2787
2951
|
}
|
|
2788
2952
|
handleDataMessage(data, kind) {
|
|
2789
2953
|
try {
|
|
@@ -2814,23 +2978,31 @@ var Room = class extends TypedEmitter {
|
|
|
2814
2978
|
let participant = this.remoteParticipants.get(info.sid);
|
|
2815
2979
|
if (participant) {
|
|
2816
2980
|
participant.updateInfo(info);
|
|
2817
|
-
|
|
2818
|
-
|
|
2819
|
-
|
|
2820
|
-
|
|
2821
|
-
this.
|
|
2822
|
-
|
|
2823
|
-
|
|
2824
|
-
|
|
2825
|
-
}
|
|
2826
|
-
participant.on("trackPublished", (pub) => {
|
|
2827
|
-
this.emit("trackPublished", pub, participant);
|
|
2828
|
-
});
|
|
2829
|
-
participant.on("trackUnpublished", (pub) => {
|
|
2830
|
-
this.emit("trackUnpublished", pub, participant);
|
|
2831
|
-
});
|
|
2981
|
+
return { participant, isNew: false };
|
|
2982
|
+
}
|
|
2983
|
+
for (const [oldSid, existing] of this.remoteParticipants) {
|
|
2984
|
+
if (existing.identity === info.identity) {
|
|
2985
|
+
this.remoteParticipants.delete(oldSid);
|
|
2986
|
+
existing.updateInfo(info);
|
|
2987
|
+
this.remoteParticipants.set(info.sid, existing);
|
|
2988
|
+
return { participant: existing, isNew: false };
|
|
2989
|
+
}
|
|
2832
2990
|
}
|
|
2833
|
-
|
|
2991
|
+
participant = new RemoteParticipant(info);
|
|
2992
|
+
this.remoteParticipants.set(info.sid, participant);
|
|
2993
|
+
participant.on("trackSubscribed", (track, pub) => {
|
|
2994
|
+
this.emit("trackSubscribed", track, pub, participant);
|
|
2995
|
+
});
|
|
2996
|
+
participant.on("trackUnsubscribed", (track, pub) => {
|
|
2997
|
+
this.emit("trackUnsubscribed", track, pub, participant);
|
|
2998
|
+
});
|
|
2999
|
+
participant.on("trackPublished", (pub) => {
|
|
3000
|
+
this.emit("trackPublished", pub, participant);
|
|
3001
|
+
});
|
|
3002
|
+
participant.on("trackUnpublished", (pub) => {
|
|
3003
|
+
this.emit("trackUnpublished", pub, participant);
|
|
3004
|
+
});
|
|
3005
|
+
return { participant, isNew: true };
|
|
2834
3006
|
}
|
|
2835
3007
|
};
|
|
2836
3008
|
|