@hiroosy/baileys-lite 0.3.7 → 0.3.8
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/package.json +1 -1
- package/src/voip/app-data/WaAppDataStream.js +0 -39
- package/src/voip/call/WaCallManager.js +4 -32
- package/src/voip/call/WaCallMediaSession.js +35 -309
- package/src/voip/index.js +75 -18
- package/src/voip/media/WaAudioEngine.js +150 -122
- package/src/voip/media/WaVideoEngine.js +7 -22
- package/src/voip/media/mlow-codec.js +7 -141
- package/src/voip/relay/WaManualRelay.js +91 -94
- package/src/voip/relay/dtls/handshake.js +4 -42
- package/src/voip/relay/sctp/association.js +89 -54
- package/src/voip/relay/stun.js +6 -6
- package/src/voip/types.js +1 -1
- package/src/voip/voipClient.js +3 -1
|
@@ -16,12 +16,10 @@ import { WaAudioEngine } from '../media/WaAudioEngine.js';
|
|
|
16
16
|
import { WaVideoEngine } from '../media/WaVideoEngine.js';
|
|
17
17
|
import { parseRelayFromAck } from '../relay/relay-ack.js';
|
|
18
18
|
import { isRtpPacket, isStunPacket } from '../relay/stun.js';
|
|
19
|
-
import { deriveWasmRelayStreamSsrcs } from '../crypto/ssrc.js';
|
|
20
19
|
import { WaManualRelay } from '../relay/WaManualRelay.js';
|
|
21
20
|
import { buildAcceptReceiptStanza, buildAcceptStanza, buildMuteV2Stanza, buildPreacceptStanza, buildRaiseHandStanza, buildRejectStanza, buildRelaylatencyForwardStanza, buildRelayLatencyStanza, buildTerminateStanza, buildTransportStanza, buildVideoStateStanza, decryptCallKey, extractNodeInfo, extractRelayEndpoints, needsDecryption, parseMuteV2, parseRaiseHandState, VideoDecAccept, VideoDecRequest, VideoState } from '../signaling/signaling.js';
|
|
22
21
|
import { CallDirection, CallMediaType, CallState, EndCallReason, PayloadType, SRTP_AUTH_TAG_LEN, SRTP_RECV_AUTH_TAG_LEN, SRTP_SEND_AUTH_TAG_LEN } from '../types.js';
|
|
23
22
|
|
|
24
|
-
|
|
25
23
|
export class WaCallMediaSession {
|
|
26
24
|
info;
|
|
27
25
|
deps;
|
|
@@ -58,10 +56,9 @@ export class WaCallMediaSession {
|
|
|
58
56
|
selfEchoCount = 0;
|
|
59
57
|
lastRecvSeq = -1;
|
|
60
58
|
recvSeqGaps = 0;
|
|
61
|
-
lastCapturedAtMs = undefined;
|
|
62
|
-
qLog = { n: 0, tiny: 0, sent: 0, octets: 0, retx: 0, rx: 0, plc: 0, late: 0, fec: 0, errors: 0 };
|
|
63
59
|
actualPeerSsrc = null;
|
|
64
60
|
ssrcResubscribed = false;
|
|
61
|
+
reconnecting = false;
|
|
65
62
|
|
|
66
63
|
videoSsrc = 0;
|
|
67
64
|
videoRtpStream = null;
|
|
@@ -116,10 +113,32 @@ export class WaCallMediaSession {
|
|
|
116
113
|
this.sctpRelay.on('relay_receive', (relayInfo) => {
|
|
117
114
|
this.onRelayData(relayInfo.data);
|
|
118
115
|
});
|
|
116
|
+
this.sctpRelay.on('relay_lost', (detail) => {
|
|
117
|
+
if (!detail.anyOpen)
|
|
118
|
+
this.markReconnecting({ attempt: 0, max: detail.max });
|
|
119
|
+
});
|
|
120
|
+
this.sctpRelay.on('relay_retrying', (detail) => {
|
|
121
|
+
if (!detail.anyOpen)
|
|
122
|
+
this.markReconnecting({ attempt: detail.attempt, max: detail.max });
|
|
123
|
+
});
|
|
124
|
+
this.sctpRelay.on('relay_recovered', () => {
|
|
125
|
+
if (!this.reconnecting || !this.sctpRelay.hasConnection())
|
|
126
|
+
return;
|
|
127
|
+
this.reconnecting = false;
|
|
128
|
+
this.delegate.emitReconnected(this.info);
|
|
129
|
+
});
|
|
130
|
+
this.sctpRelay.on('relay_failed', (detail) => {
|
|
131
|
+
if (!detail.anyOpen)
|
|
132
|
+
this.endCall(EndCallReason.Failed).catch(() => { });
|
|
133
|
+
});
|
|
119
134
|
}
|
|
120
135
|
get callId() {
|
|
121
136
|
return this.info.callId;
|
|
122
137
|
}
|
|
138
|
+
markReconnecting(detail) {
|
|
139
|
+
this.reconnecting = true;
|
|
140
|
+
this.delegate.emitReconnecting(this.info, detail);
|
|
141
|
+
}
|
|
123
142
|
onDecodedAudio = (pcm) => {
|
|
124
143
|
this.audioEngine.onPlaybackData(pcm);
|
|
125
144
|
this.delegate.emitInboundAudio?.(this.info, pcm);
|
|
@@ -131,31 +150,11 @@ export class WaCallMediaSession {
|
|
|
131
150
|
this.selfSsrcJid = this.ensureDeviceJid(selfLid);
|
|
132
151
|
const peerSsrc = generateSecureSsrc(this.info.callId, this.ensureDeviceJid(peerJid));
|
|
133
152
|
this.peerSsrcs = [peerSsrc];
|
|
134
|
-
this.logger.media('call media initialized', {
|
|
135
|
-
callId: this.info.callId,
|
|
136
|
-
selfSsrc: `0x${ssrc.toString(16).toUpperCase()}`,
|
|
137
|
-
peerSsrc: `0x${peerSsrc.toString(16).toUpperCase()}`
|
|
138
|
-
});
|
|
139
|
-
this.logger.warn('[DIAG] initMedia called', {
|
|
140
|
-
callId: this.info.callId,
|
|
141
|
-
selfSsrc: `0x${ssrc.toString(16).toUpperCase()}`,
|
|
142
|
-
peerSsrc: `0x${peerSsrc.toString(16).toUpperCase()}`,
|
|
143
|
-
videoEngineIsFresh: this.videoEngine?.proc === null
|
|
144
|
-
});
|
|
145
153
|
this.opusCodec = await MLowCodec.create({ logger: this.logger.child({ component: 'mlow' }) });
|
|
146
154
|
if (this.info.mediaType === CallMediaType.Video) {
|
|
147
|
-
|
|
148
155
|
this.videoSsrc = generateSecureSsrc(this.info.callId, this.ensureDeviceJid(selfLid), 2);
|
|
149
156
|
this.videoRtpStream = new VideoRtpStream(this.videoSsrc, videoRtpDurationSamples(this.videoEngine.frameDurationMs));
|
|
150
157
|
this.videoAssembler = new H264AccessUnitAssembler();
|
|
151
|
-
this.logger.media('video media initialized', {
|
|
152
|
-
callId: this.info.callId,
|
|
153
|
-
videoSsrc: `0x${this.videoSsrc.toString(16).toUpperCase()}`
|
|
154
|
-
});
|
|
155
|
-
this.logger.warn('[DIAG] video media initialized', {
|
|
156
|
-
callId: this.info.callId,
|
|
157
|
-
videoSsrc: `0x${this.videoSsrc.toString(16).toUpperCase()}`
|
|
158
|
-
});
|
|
159
158
|
}
|
|
160
159
|
}
|
|
161
160
|
resetOutgoingFlags() {
|
|
@@ -207,7 +206,6 @@ export class WaCallMediaSession {
|
|
|
207
206
|
if (this.info.relayData) {
|
|
208
207
|
await this.connectRelays(this.info.relayData.endpoints);
|
|
209
208
|
}
|
|
210
|
-
this.logger.media('call accepted', { callId });
|
|
211
209
|
}
|
|
212
210
|
async rejectCall(reason = EndCallReason.Declined) {
|
|
213
211
|
this.info.applyTransition({ type: 'local_rejected', reason });
|
|
@@ -255,15 +253,13 @@ export class WaCallMediaSession {
|
|
|
255
253
|
}
|
|
256
254
|
async loadAudio(audioPath) {
|
|
257
255
|
this.audioEngine.setLoopMode(false);
|
|
258
|
-
await this.audioEngine.
|
|
256
|
+
await this.audioEngine.loadSource(audioPath);
|
|
259
257
|
this.resetEncodeState();
|
|
260
|
-
this.logger.media('audio loaded for call', { callId: this.info.callId });
|
|
261
258
|
}
|
|
262
259
|
setExternalAudioMode(enabled) {
|
|
263
260
|
this.audioEngine.setExternalMode(enabled);
|
|
264
261
|
if (enabled) {
|
|
265
262
|
this.resetEncodeState();
|
|
266
|
-
this.logger.debug('external audio mode enabled', { callId: this.info.callId });
|
|
267
263
|
}
|
|
268
264
|
}
|
|
269
265
|
feedLiveAudio(data) {
|
|
@@ -344,9 +340,6 @@ export class WaCallMediaSession {
|
|
|
344
340
|
const recvKeying = derivePerJidSrtpKey(peerCallKey, this.ensureDeviceJid(peerJid));
|
|
345
341
|
this.srtpSession = new SrtpSession(sendKeying, recvKeying, SRTP_SEND_AUTH_TAG_LEN, SRTP_RECV_AUTH_TAG_LEN);
|
|
346
342
|
srtpFromPeerKey = true;
|
|
347
|
-
this.logger.debug('srtp re-initialized with peer call_key', {
|
|
348
|
-
callId: this.info.callId
|
|
349
|
-
});
|
|
350
343
|
}
|
|
351
344
|
catch (err) {
|
|
352
345
|
this.logger.error('per-jid srtp re-derivation failed', {
|
|
@@ -367,9 +360,7 @@ export class WaCallMediaSession {
|
|
|
367
360
|
this.info.applyTransition({ type: 'remote_accepted' });
|
|
368
361
|
this.delegate.emitState(this.info);
|
|
369
362
|
}
|
|
370
|
-
catch
|
|
371
|
-
this.logger.trace('call transition skipped', { message: toError(err).message });
|
|
372
|
-
}
|
|
363
|
+
catch { }
|
|
373
364
|
const meId = this.deps.authClient.getCurrentCredentials()?.meJid ?? '';
|
|
374
365
|
const meLid = this.deps.authClient.getCurrentCredentials()?.meLid;
|
|
375
366
|
const ourJid = meLid || meId;
|
|
@@ -379,22 +370,12 @@ export class WaCallMediaSession {
|
|
|
379
370
|
const acceptingDeviceJid = peerJid;
|
|
380
371
|
this.acceptedByJid = acceptingDeviceJid;
|
|
381
372
|
if (this.actualPeerSsrc !== null) {
|
|
382
|
-
|
|
383
|
-
this.logger.debug('accept keeping actual peer ssrc', {
|
|
384
|
-
callId,
|
|
385
|
-
actualPeerSsrc: `0x${this.actualPeerSsrc.toString(16)}`,
|
|
386
|
-
calculatedJid
|
|
387
|
-
});
|
|
373
|
+
this.ensureDeviceJid(acceptingDeviceJid);
|
|
388
374
|
}
|
|
389
375
|
else {
|
|
390
376
|
const peerDeviceJidForSsrc = this.ensureDeviceJid(acceptingDeviceJid);
|
|
391
377
|
const acceptSsrc = generateSecureSsrc(callId, peerDeviceJidForSsrc);
|
|
392
378
|
this.peerSsrcs = [acceptSsrc];
|
|
393
|
-
this.logger.debug('accept ssrc assigned', {
|
|
394
|
-
callId,
|
|
395
|
-
jid: peerDeviceJidForSsrc,
|
|
396
|
-
ssrc: `0x${acceptSsrc.toString(16)}`
|
|
397
|
-
});
|
|
398
379
|
}
|
|
399
380
|
this.sctpRelay.setSubscriptionSsrc(this.peerSsrcs[0] ?? 0);
|
|
400
381
|
this.sctpRelay.resendSubscriptions();
|
|
@@ -457,20 +438,14 @@ export class WaCallMediaSession {
|
|
|
457
438
|
try {
|
|
458
439
|
this.info.applyTransition({ type: 'media_connected' });
|
|
459
440
|
this.delegate.emitState(this.info);
|
|
460
|
-
const MEDIA_START_DELAY_MS = 3000;
|
|
461
441
|
setTimeout(() => {
|
|
462
|
-
this.logger.warn('[DIAG] delayed startMediaFlow firing (path B)', {
|
|
463
|
-
callId: this.info.callId, state: this.info.stateData.state
|
|
464
|
-
});
|
|
465
442
|
if (this.info.stateData.state === CallState.Ended) {
|
|
466
443
|
return;
|
|
467
444
|
}
|
|
468
445
|
this.startMediaFlow();
|
|
469
|
-
},
|
|
470
|
-
}
|
|
471
|
-
catch (err) {
|
|
472
|
-
this.logger.trace('call transition skipped', { message: toError(err).message });
|
|
446
|
+
}, 3000);
|
|
473
447
|
}
|
|
448
|
+
catch { }
|
|
474
449
|
}
|
|
475
450
|
else if (this.info.relayData) {
|
|
476
451
|
await this.connectRelays(this.info.relayData.endpoints);
|
|
@@ -556,11 +531,6 @@ export class WaCallMediaSession {
|
|
|
556
531
|
peerPid,
|
|
557
532
|
hbhKey
|
|
558
533
|
};
|
|
559
|
-
this.logger.debug('offer ack relays parsed', {
|
|
560
|
-
callId: this.info.callId,
|
|
561
|
-
relayCount: relays.length,
|
|
562
|
-
participantCount: participantJids.length
|
|
563
|
-
});
|
|
564
534
|
const callKey = this.info.encryptionKey;
|
|
565
535
|
if (participantJids.length > 0) {
|
|
566
536
|
const meLid = this.deps.authClient.getCurrentCredentials()?.meLid;
|
|
@@ -590,11 +560,6 @@ export class WaCallMediaSession {
|
|
|
590
560
|
if (callKey) {
|
|
591
561
|
this.initSrtpKeys();
|
|
592
562
|
}
|
|
593
|
-
else {
|
|
594
|
-
this.logger.media('no call_key, srtp not initialized', {
|
|
595
|
-
callId: this.info.callId
|
|
596
|
-
});
|
|
597
|
-
}
|
|
598
563
|
}
|
|
599
564
|
if (this.info.isInitiator && !this.outgoingPreacceptSent) {
|
|
600
565
|
try {
|
|
@@ -664,10 +629,6 @@ export class WaCallMediaSession {
|
|
|
664
629
|
}
|
|
665
630
|
if (electedRelayIdx !== undefined) {
|
|
666
631
|
this.info.electedRelayIdx = electedRelayIdx;
|
|
667
|
-
this.logger.debug('elected relay index', {
|
|
668
|
-
callId: this.info.callId,
|
|
669
|
-
electedRelayIdx
|
|
670
|
-
});
|
|
671
632
|
}
|
|
672
633
|
}
|
|
673
634
|
async handleCallMuteV2(node, peerJid) {
|
|
@@ -824,13 +785,7 @@ export class WaCallMediaSession {
|
|
|
824
785
|
this.delegate.emitCallReaction?.(this.info, reaction);
|
|
825
786
|
}
|
|
826
787
|
}
|
|
827
|
-
catch
|
|
828
|
-
this.logger.debug('app data packet dropped', {
|
|
829
|
-
callId: this.info.callId,
|
|
830
|
-
payloadType,
|
|
831
|
-
message: toError(err).message
|
|
832
|
-
});
|
|
833
|
-
}
|
|
788
|
+
catch { }
|
|
834
789
|
}
|
|
835
790
|
|
|
836
791
|
async handleCallVideo(node) {
|
|
@@ -854,7 +809,6 @@ export class WaCallMediaSession {
|
|
|
854
809
|
this.peerVideoActive = this.peerVideoJids.size > 0;
|
|
855
810
|
return { before, after: this.peerVideoJids.size };
|
|
856
811
|
};
|
|
857
|
-
this.logger.media('inbound video state', { callId, state });
|
|
858
812
|
switch (state) {
|
|
859
813
|
case VideoState.UpgradeRequest:
|
|
860
814
|
case VideoState.UpgradeRequestV2: {
|
|
@@ -956,11 +910,6 @@ export class WaCallMediaSession {
|
|
|
956
910
|
if (this.sctpRelay.hasConnection()) {
|
|
957
911
|
this.videoEngine.start();
|
|
958
912
|
}
|
|
959
|
-
this.logger.media('video enabled mid-call', {
|
|
960
|
-
callId: this.info.callId,
|
|
961
|
-
videoSsrc: `0x${this.videoSsrc.toString(16).toUpperCase()}`,
|
|
962
|
-
source: this.videoEngine.sourceKind
|
|
963
|
-
});
|
|
964
913
|
}
|
|
965
914
|
|
|
966
915
|
async swapVideoSource(videoPath) {
|
|
@@ -971,7 +920,6 @@ export class WaCallMediaSession {
|
|
|
971
920
|
await this.videoEngine.loadVideoFile(videoPath);
|
|
972
921
|
this.audioEngine.setLoopMode(true);
|
|
973
922
|
this.videoEngine.start();
|
|
974
|
-
this.logger.media('video source swapped', { callId: this.info.callId, videoPath });
|
|
975
923
|
}
|
|
976
924
|
|
|
977
925
|
async stopVideoMidCall({ keepSource = false } = {}) {
|
|
@@ -996,7 +944,6 @@ export class WaCallMediaSession {
|
|
|
996
944
|
this.videoRtcpSession = null;
|
|
997
945
|
this.videoAssembler = null;
|
|
998
946
|
this.info.mediaType = CallMediaType.Audio;
|
|
999
|
-
this.logger.media('video disabled mid-call', { callId: this.info.callId, keptSource: keepSource });
|
|
1000
947
|
}
|
|
1001
948
|
handleCallTerminate() {
|
|
1002
949
|
try {
|
|
@@ -1005,46 +952,15 @@ export class WaCallMediaSession {
|
|
|
1005
952
|
reason: EndCallReason.UserEnded
|
|
1006
953
|
});
|
|
1007
954
|
}
|
|
1008
|
-
catch
|
|
1009
|
-
this.logger.trace('call transition skipped', { message: toError(err).message });
|
|
1010
|
-
}
|
|
955
|
+
catch { }
|
|
1011
956
|
this.delegate.emitEnded(this.info);
|
|
1012
957
|
this.delegate.emitState(this.info);
|
|
1013
958
|
this.cleanup();
|
|
1014
959
|
}
|
|
1015
960
|
sendCapturedAudio(data) {
|
|
1016
|
-
const nowMs = Date.now();
|
|
1017
|
-
if (this.lastCapturedAtMs !== undefined) {
|
|
1018
|
-
const gapMs = nowMs - this.lastCapturedAtMs;
|
|
1019
|
-
|
|
1020
|
-
if (gapMs > 300) {
|
|
1021
|
-
this.logger.warn('captured audio frame gap larger than expected', {
|
|
1022
|
-
callId: this.info.callId,
|
|
1023
|
-
gapMs,
|
|
1024
|
-
audioSendCount: this.audioSendCount,
|
|
1025
|
-
sctpBacklog: this.sctpRelay.getSendBacklog()
|
|
1026
|
-
});
|
|
1027
|
-
}
|
|
1028
|
-
}
|
|
1029
|
-
this.lastCapturedAtMs = nowMs;
|
|
1030
961
|
const hasRelay = this.sctpRelay.hasConnection();
|
|
1031
962
|
if (!this.rtpSession || !this.srtpSession || !this.opusCodec || !hasRelay) {
|
|
1032
963
|
this.audioDropCount++;
|
|
1033
|
-
if (this.audioDropCount === 1 || this.audioDropCount % 500 === 0) {
|
|
1034
|
-
const missing = [
|
|
1035
|
-
!this.rtpSession && 'rtpSession',
|
|
1036
|
-
!this.srtpSession && 'srtpSession',
|
|
1037
|
-
!this.opusCodec && 'opusCodec',
|
|
1038
|
-
!hasRelay && 'relayConnection'
|
|
1039
|
-
]
|
|
1040
|
-
.filter(Boolean)
|
|
1041
|
-
.join(', ');
|
|
1042
|
-
this.logger.media('audio dropped', {
|
|
1043
|
-
callId: this.info.callId,
|
|
1044
|
-
dropCount: this.audioDropCount,
|
|
1045
|
-
missing
|
|
1046
|
-
});
|
|
1047
|
-
}
|
|
1048
964
|
return;
|
|
1049
965
|
}
|
|
1050
966
|
for (let i = 0; i < data.length; i++) {
|
|
@@ -1075,8 +991,6 @@ export class WaCallMediaSession {
|
|
|
1075
991
|
this.encodeBufferPos = 0;
|
|
1076
992
|
try {
|
|
1077
993
|
const opusFrame = this.opusCodec.encode(frameData);
|
|
1078
|
-
if (opusFrame.length <= 18)
|
|
1079
|
-
this.qLog.tiny++;
|
|
1080
994
|
this.sendOpusFrame(opusFrame, false);
|
|
1081
995
|
this.realAudioSendCount++;
|
|
1082
996
|
}
|
|
@@ -1105,16 +1019,6 @@ export class WaCallMediaSession {
|
|
|
1105
1019
|
const hasRelay = this.sctpRelay.hasConnection();
|
|
1106
1020
|
if (!this.videoRtpStream || !this.videoSrtpSession || !hasRelay) {
|
|
1107
1021
|
this.videoDropCount++;
|
|
1108
|
-
if (this.videoDropCount === 1 || this.videoDropCount % 150 === 0) {
|
|
1109
|
-
const missing = [
|
|
1110
|
-
!this.videoRtpStream && 'videoRtpStream',
|
|
1111
|
-
!this.videoSrtpSession && 'videoSrtpSession',
|
|
1112
|
-
!hasRelay && 'relayConnection'
|
|
1113
|
-
].filter(Boolean).join(', ');
|
|
1114
|
-
this.logger.media('video access unit dropped', {
|
|
1115
|
-
callId: this.info.callId, dropCount: this.videoDropCount, missing
|
|
1116
|
-
});
|
|
1117
|
-
}
|
|
1118
1022
|
return;
|
|
1119
1023
|
}
|
|
1120
1024
|
try {
|
|
@@ -1149,13 +1053,6 @@ export class WaCallMediaSession {
|
|
|
1149
1053
|
this.videoSendCount++;
|
|
1150
1054
|
if (idr)
|
|
1151
1055
|
this.videoIdrSendCount++;
|
|
1152
|
-
if (idr || this.videoSendCount === 1 || this.videoSendCount % 150 === 0) {
|
|
1153
|
-
this.logger.media('video sent', {
|
|
1154
|
-
callId: this.info.callId, sendCount: this.videoSendCount,
|
|
1155
|
-
idrSentTotal: this.videoIdrSendCount,
|
|
1156
|
-
auBytes: au.length, packets: chunks.length, idr
|
|
1157
|
-
});
|
|
1158
|
-
}
|
|
1159
1056
|
}
|
|
1160
1057
|
catch (err) {
|
|
1161
1058
|
this.logger.error('error sending video', {
|
|
@@ -1164,21 +1061,6 @@ export class WaCallMediaSession {
|
|
|
1164
1061
|
}
|
|
1165
1062
|
}
|
|
1166
1063
|
cleanup() {
|
|
1167
|
-
this.logger.warn('[DIAG] cleanup entered', { callId: this.info.callId, state: this.info.stateData.state });
|
|
1168
|
-
const opusStats = this.opusCodec?.getStats();
|
|
1169
|
-
this.logger.media('call stats', {
|
|
1170
|
-
callId: this.info.callId,
|
|
1171
|
-
relayPackets: this.relayPacketCount,
|
|
1172
|
-
recvOk: this.audioRecvCount,
|
|
1173
|
-
srtpErrors: this.srtpErrorCount,
|
|
1174
|
-
sent: this.audioSendCount,
|
|
1175
|
-
dropped: this.audioDropCount,
|
|
1176
|
-
opusOk: opusStats?.success ?? 0,
|
|
1177
|
-
opusErr: opusStats?.errors ?? 0,
|
|
1178
|
-
videoSent: this.videoSendCount,
|
|
1179
|
-
videoIdrSent: this.videoIdrSendCount,
|
|
1180
|
-
videoDropped: this.videoDropCount
|
|
1181
|
-
});
|
|
1182
1064
|
this.audioEngine.setOnAudioFinished(null);
|
|
1183
1065
|
this.audioEngine.stop();
|
|
1184
1066
|
this.videoEngine.stop();
|
|
@@ -1268,16 +1150,6 @@ export class WaCallMediaSession {
|
|
|
1268
1150
|
this.sctpRelay.broadcast(toArrayBuffer(srtpData));
|
|
1269
1151
|
this.audioSendCount++;
|
|
1270
1152
|
this.audioOctetsSent = (this.audioOctetsSent + rtpPayload.length) >>> 0;
|
|
1271
|
-
if (this.audioSendCount === 1 || this.audioSendCount % 500 === 0) {
|
|
1272
|
-
this.logger.media('audio sent', {
|
|
1273
|
-
callId: this.info.callId,
|
|
1274
|
-
sendCount: this.audioSendCount,
|
|
1275
|
-
opusBytes: opusFrame.length,
|
|
1276
|
-
srtpBytes: srtpData.length,
|
|
1277
|
-
silence: isSilence,
|
|
1278
|
-
sctpBacklog: this.sctpRelay.getSendBacklog()
|
|
1279
|
-
});
|
|
1280
|
-
}
|
|
1281
1153
|
}
|
|
1282
1154
|
catch (err) {
|
|
1283
1155
|
this.logger.error('error sending audio', {
|
|
@@ -1294,7 +1166,6 @@ export class WaCallMediaSession {
|
|
|
1294
1166
|
initSrtpKeys() {
|
|
1295
1167
|
const callKey = this.info.encryptionKey;
|
|
1296
1168
|
if (!callKey) {
|
|
1297
|
-
this.logger.media('no call_key, srtp not initialized', { callId: this.info.callId });
|
|
1298
1169
|
return;
|
|
1299
1170
|
}
|
|
1300
1171
|
const meLid = this.deps.authClient.getCurrentCredentials()?.meLid;
|
|
@@ -1326,23 +1197,12 @@ export class WaCallMediaSession {
|
|
|
1326
1197
|
this.trackPeerAppDataSsrcs([peerDeviceJid, ...participants.filter((jid) => toUserJid(jid) !== ourBase)]);
|
|
1327
1198
|
this.audioRtcpSession = new SrtcpSendContext(sendKeying);
|
|
1328
1199
|
if (this.info.mediaType === CallMediaType.Video) {
|
|
1329
|
-
|
|
1330
1200
|
this.videoSrtpSession = new SrtpSession(sendKeying, recvKeying, SRTP_SEND_AUTH_TAG_LEN, SRTP_RECV_AUTH_TAG_LEN);
|
|
1331
1201
|
this.videoRtcpSession = new SrtcpSendContext(sendKeying);
|
|
1332
1202
|
}
|
|
1333
1203
|
this.startRtcpSenderReports();
|
|
1334
|
-
this.logger.debug('srtp per-jid keys initialized', {
|
|
1335
|
-
callId: this.info.callId,
|
|
1336
|
-
sendJid: ourDeviceJid,
|
|
1337
|
-
recvJid: peerDeviceJid
|
|
1338
|
-
});
|
|
1339
|
-
}
|
|
1340
|
-
catch (err) {
|
|
1341
|
-
this.logger.debug('srtp key derivation failed', {
|
|
1342
|
-
callId: this.info.callId,
|
|
1343
|
-
message: toError(err).message
|
|
1344
|
-
});
|
|
1345
1204
|
}
|
|
1205
|
+
catch { }
|
|
1346
1206
|
}
|
|
1347
1207
|
|
|
1348
1208
|
startRtcpSenderReports() {
|
|
@@ -1355,8 +1215,6 @@ export class WaCallMediaSession {
|
|
|
1355
1215
|
this.rtcpTimer = setInterval(() => {
|
|
1356
1216
|
if (!this.sctpRelay.hasConnection())
|
|
1357
1217
|
return;
|
|
1358
|
-
if (++this.qLog.n % 3 === 0)
|
|
1359
|
-
this.logVoipQuality();
|
|
1360
1218
|
const nowMs = Date.now();
|
|
1361
1219
|
try {
|
|
1362
1220
|
if (this.audioRtcpSession && this.selfSsrc) {
|
|
@@ -1379,27 +1237,8 @@ export class WaCallMediaSession {
|
|
|
1379
1237
|
const protectedPacket = this.videoRtcpSession.protect(this.videoSsrc, plain);
|
|
1380
1238
|
this.sctpRelay.broadcast(toArrayBuffer(protectedPacket));
|
|
1381
1239
|
}
|
|
1382
|
-
|
|
1383
|
-
if (this.mediaStartedAtMs) {
|
|
1384
|
-
const wallElapsedMs = nowMs - this.mediaStartedAtMs;
|
|
1385
|
-
const audioElapsedMs = this.audioSendCount * 60;
|
|
1386
|
-
const videoElapsedMs = this.videoSendCount * (1000 / this.videoEngine.frameRate);
|
|
1387
|
-
this.logger.media('av sync check', {
|
|
1388
|
-
callId: this.info.callId,
|
|
1389
|
-
wallElapsedMs,
|
|
1390
|
-
audioElapsedMs,
|
|
1391
|
-
videoElapsedMs,
|
|
1392
|
-
audioMinusWallMs: audioElapsedMs - wallElapsedMs,
|
|
1393
|
-
videoMinusWallMs: Math.round(videoElapsedMs - wallElapsedMs)
|
|
1394
|
-
});
|
|
1395
|
-
}
|
|
1396
|
-
}
|
|
1397
|
-
catch (err) {
|
|
1398
|
-
this.logger.debug('rtcp sender report failed', {
|
|
1399
|
-
callId: this.info.callId,
|
|
1400
|
-
message: toError(err).message
|
|
1401
|
-
});
|
|
1402
1240
|
}
|
|
1241
|
+
catch { }
|
|
1403
1242
|
}, 1500);
|
|
1404
1243
|
}
|
|
1405
1244
|
resetEncodeState() {
|
|
@@ -1412,29 +1251,17 @@ export class WaCallMediaSession {
|
|
|
1412
1251
|
try {
|
|
1413
1252
|
this.info.applyTransition({ type: 'media_connected' });
|
|
1414
1253
|
this.delegate.emitState(this.info);
|
|
1415
|
-
this.logger.media('relay connected', {
|
|
1416
|
-
callId: this.info.callId, acceptedByJid: this.acceptedByJid ?? null
|
|
1417
|
-
});
|
|
1418
1254
|
if (!this.acceptedByJid) {
|
|
1419
|
-
this.logger.media('relay connected before accept, deferring media start', {
|
|
1420
|
-
callId: this.info.callId
|
|
1421
|
-
});
|
|
1422
1255
|
return;
|
|
1423
1256
|
}
|
|
1424
|
-
const MEDIA_START_DELAY_MS = 3000;
|
|
1425
1257
|
setTimeout(() => {
|
|
1426
|
-
this.logger.warn('[DIAG] delayed startMediaFlow firing (path A)', {
|
|
1427
|
-
callId: this.info.callId, state: this.info.stateData.state
|
|
1428
|
-
});
|
|
1429
1258
|
if (this.info.stateData.state === CallState.Ended) {
|
|
1430
1259
|
return;
|
|
1431
1260
|
}
|
|
1432
1261
|
this.startMediaFlow();
|
|
1433
|
-
},
|
|
1434
|
-
}
|
|
1435
|
-
catch (err) {
|
|
1436
|
-
this.logger.trace('call transition skipped', { message: toError(err).message });
|
|
1262
|
+
}, 3000);
|
|
1437
1263
|
}
|
|
1264
|
+
catch { }
|
|
1438
1265
|
}
|
|
1439
1266
|
}
|
|
1440
1267
|
onRelayData(data) {
|
|
@@ -1508,89 +1335,12 @@ export class WaCallMediaSession {
|
|
|
1508
1335
|
const lossPct = pkts + gaps > 0 ? Math.min(100, Math.round((100 * gaps) / (pkts + gaps))) : 0;
|
|
1509
1336
|
this.opusCodec.setExpectedPacketLossPercent(lossPct);
|
|
1510
1337
|
}
|
|
1511
|
-
if (this.audioRecvCount % 1500 === 0) {
|
|
1512
|
-
const stats = this.opusCodec.getStats();
|
|
1513
|
-
|
|
1514
|
-
this.logger.trace('audio recv stats', {
|
|
1515
|
-
callId: this.info.callId,
|
|
1516
|
-
recvCount: this.audioRecvCount,
|
|
1517
|
-
real: this.recvRealCount,
|
|
1518
|
-
dtx: this.recvDtxCount,
|
|
1519
|
-
decodeOk: stats.success,
|
|
1520
|
-
decodeErr: stats.errors,
|
|
1521
|
-
plc: stats.plc,
|
|
1522
|
-
fec: stats.fec,
|
|
1523
|
-
late: stats.late
|
|
1524
|
-
});
|
|
1525
|
-
}
|
|
1526
1338
|
}
|
|
1527
1339
|
catch (err) {
|
|
1528
1340
|
this.srtpErrorCount++;
|
|
1529
|
-
if (this.srtpErrorCount <= 5) {
|
|
1530
|
-
const ssrc = data.length >= 12 ? readUInt32BE(data, 8) : 0;
|
|
1531
|
-
this.logger.debug('srtp recv error', {
|
|
1532
|
-
callId: this.info.callId,
|
|
1533
|
-
errorCount: this.srtpErrorCount,
|
|
1534
|
-
message: toError(err).message,
|
|
1535
|
-
ssrc: `0x${ssrc.toString(16)}`
|
|
1536
|
-
});
|
|
1537
|
-
}
|
|
1538
1341
|
}
|
|
1539
1342
|
}
|
|
1540
1343
|
|
|
1541
|
-
logVoipQuality() {
|
|
1542
|
-
try {
|
|
1543
|
-
const q = this.qLog;
|
|
1544
|
-
const pace = this.audioEngine.takePacingStats();
|
|
1545
|
-
const bl = this.sctpRelay.getSendBacklog?.() ?? {};
|
|
1546
|
-
const sent = this.audioSendCount - q.sent;
|
|
1547
|
-
const octets = ((this.audioOctetsSent - q.octets) >>> 0);
|
|
1548
|
-
const rs = this.opusCodec?.getStats() ?? { late: 0, plc: 0, fec: 0, errors: 0 };
|
|
1549
|
-
const line = {
|
|
1550
|
-
callId: this.info.callId,
|
|
1551
|
-
tSec: this.mediaStartedAtMs ? Math.round((Date.now() - this.mediaStartedAtMs) / 1000) : 0,
|
|
1552
|
-
txFrames: sent,
|
|
1553
|
-
txAvgB: sent > 0 ? Math.round(octets / sent) : 0,
|
|
1554
|
-
txTinyPct: sent > 0 ? Math.round((100 * (this.qLog.tiny)) / sent) : 0,
|
|
1555
|
-
tickLate90: pace.late90,
|
|
1556
|
-
tickLate200: pace.late200,
|
|
1557
|
-
tickBurst: pace.burst20,
|
|
1558
|
-
tickMaxGapMs: pace.maxGap,
|
|
1559
|
-
loopP50ms: pace.loopP50,
|
|
1560
|
-
loopP99ms: pace.loopP99,
|
|
1561
|
-
loopMaxMs: pace.loopMax,
|
|
1562
|
-
sctpInFlight: bl.inFlight ?? 0,
|
|
1563
|
-
sctpQueuedAudio: bl.queuedAudio ?? 0,
|
|
1564
|
-
sctpRetx: (bl.retransmits ?? 0) - q.retx,
|
|
1565
|
-
aLagMs: this.mediaStartedAtMs ? this.audioSendCount * 60 - (Date.now() - this.mediaStartedAtMs) : 0,
|
|
1566
|
-
vLagMs: this.mediaStartedAtMs && this.videoSendCount > 0 && this.videoEngine?.frameRate
|
|
1567
|
-
? Math.round(this.videoSendCount * (1000 / this.videoEngine.frameRate) - (Date.now() - this.mediaStartedAtMs))
|
|
1568
|
-
: 0,
|
|
1569
|
-
vRestarts: this.videoEngine?._restartCount ?? 0,
|
|
1570
|
-
relayRx: this.relayPacketCount,
|
|
1571
|
-
srtpErr: this.srtpErrorCount,
|
|
1572
|
-
stun: this.sctpRelay.getRxStunStats?.() ?? {},
|
|
1573
|
-
rxPkts: this.audioRecvCount - q.rx,
|
|
1574
|
-
rxLate: rs.late - q.late,
|
|
1575
|
-
rxPlc: rs.plc - q.plc,
|
|
1576
|
-
rxFec: rs.fec - q.fec,
|
|
1577
|
-
rxDecErr: rs.errors - q.errors
|
|
1578
|
-
};
|
|
1579
|
-
this.logger.warn('[VOIPQ] ' + JSON.stringify(line));
|
|
1580
|
-
q.sent = this.audioSendCount;
|
|
1581
|
-
q.octets = this.audioOctetsSent;
|
|
1582
|
-
q.tiny = 0;
|
|
1583
|
-
q.retx = bl.retransmits ?? 0;
|
|
1584
|
-
q.rx = this.audioRecvCount;
|
|
1585
|
-
q.late = rs.late;
|
|
1586
|
-
q.plc = rs.plc;
|
|
1587
|
-
q.fec = rs.fec;
|
|
1588
|
-
q.errors = rs.errors;
|
|
1589
|
-
}
|
|
1590
|
-
catch (err) {
|
|
1591
|
-
this.logger.trace('voip quality log failed', { message: toError(err).message });
|
|
1592
|
-
}
|
|
1593
|
-
}
|
|
1594
1344
|
onRelayVideoData(data) {
|
|
1595
1345
|
if (!this.videoSrtpSession || !this.videoAssembler)
|
|
1596
1346
|
return;
|
|
@@ -1603,19 +1353,9 @@ export class WaCallMediaSession {
|
|
|
1603
1353
|
}
|
|
1604
1354
|
catch (err) {
|
|
1605
1355
|
this.srtpErrorCount++;
|
|
1606
|
-
if (this.srtpErrorCount <= 5) {
|
|
1607
|
-
this.logger.debug('video srtp recv error', {
|
|
1608
|
-
callId: this.info.callId,
|
|
1609
|
-
message: toError(err).message
|
|
1610
|
-
});
|
|
1611
|
-
}
|
|
1612
1356
|
}
|
|
1613
1357
|
}
|
|
1614
1358
|
async connectRelays(endpoints) {
|
|
1615
|
-
this.logger.debug('connecting relays', {
|
|
1616
|
-
callId: this.info.callId,
|
|
1617
|
-
endpointCount: endpoints.length
|
|
1618
|
-
});
|
|
1619
1359
|
const seen = new Set();
|
|
1620
1360
|
const uniqueEndpoints = [];
|
|
1621
1361
|
for (const ep of endpoints) {
|
|
@@ -1663,16 +1403,7 @@ export class WaCallMediaSession {
|
|
|
1663
1403
|
this.logger.error('no relay configs', { callId: this.info.callId });
|
|
1664
1404
|
return;
|
|
1665
1405
|
}
|
|
1666
|
-
this.
|
|
1667
|
-
callId: this.info.callId,
|
|
1668
|
-
inbound,
|
|
1669
|
-
candidateCount: usable.length,
|
|
1670
|
-
chosen: relays[0].name,
|
|
1671
|
-
isFna: relays[0].isFna
|
|
1672
|
-
});
|
|
1673
|
-
if (this.selfSsrcJid) {
|
|
1674
|
-
this.sctpRelay.setStreamSsrcs(deriveWasmRelayStreamSsrcs(this.info.callId, this.selfSsrcJid));
|
|
1675
|
-
}
|
|
1406
|
+
this.sctpRelay.setParticipantIds(this.info.relayData?.selfPid, this.info.relayData?.peerPid);
|
|
1676
1407
|
this.sctpRelay.setSsrc(this.selfSsrc);
|
|
1677
1408
|
if (this.info.mediaType === CallMediaType.Video) {
|
|
1678
1409
|
this.sctpRelay.setVideoSsrc(this.videoSsrc);
|
|
@@ -1680,10 +1411,6 @@ export class WaCallMediaSession {
|
|
|
1680
1411
|
this.sctpRelay.setSubscriptionSsrc(this.peerSsrcs[0] ?? 0);
|
|
1681
1412
|
try {
|
|
1682
1413
|
await this.sctpRelay.configureRelays(relays);
|
|
1683
|
-
this.logger.debug('sctp relays configured', {
|
|
1684
|
-
callId: this.info.callId,
|
|
1685
|
-
connected: this.sctpRelay.getConnectedCount()
|
|
1686
|
-
});
|
|
1687
1414
|
}
|
|
1688
1415
|
catch (err) {
|
|
1689
1416
|
this.logger.error('sctp relay error', {
|
|
@@ -1695,7 +1422,6 @@ export class WaCallMediaSession {
|
|
|
1695
1422
|
startMediaFlow() {
|
|
1696
1423
|
this.mediaStartedAtMs = Date.now();
|
|
1697
1424
|
this.mediaFlowStarted = true;
|
|
1698
|
-
this.logger.warn('[DIAG] startMediaFlow entered', { callId: this.info.callId });
|
|
1699
1425
|
this.resetEncodeState();
|
|
1700
1426
|
this.audioEngine.startPlayback();
|
|
1701
1427
|
this.audioEngine.startCapture();
|