@hiroosy/baileys-lite 0.0.4 → 0.3.1
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/LICENSE +21 -0
- package/README.md +658 -10
- package/WAProto/index.js +2 -2558
- package/engine-requirements.js +3 -4
- package/package.json +8 -3
- package/src/binary/wa-binary.js +3 -25
- package/src/constants.js +7 -15
- package/src/defaults.js +2 -5
- package/src/foundation/ai-rich.js +530 -0
- package/src/foundation/audio-duration.js +43 -103
- package/src/foundation/audio-samples.js +19 -37
- package/src/foundation/boom.js +0 -7
- package/src/foundation/cache.js +4 -26
- package/src/foundation/concurrency.js +1 -19
- package/src/foundation/curve25519.js +7 -39
- package/src/foundation/link-preview.js +15 -57
- package/src/foundation/logger.js +0 -11
- package/src/foundation/long.js +4 -14
- package/src/foundation/protobuf-minimal.js +21 -53
- package/src/foundation/qrcode-terminal.js +273 -0
- package/src/foundation/sql-database.js +716 -0
- package/src/foundation/wa-crypto.js +2 -31
- package/src/foundation/zip.js +25 -41
- package/src/index.js +8 -18
- package/src/signal/libsignal.js +23 -78
- package/src/signal/signal-group.js +0 -35
- package/src/signal/signal-repository.js +10 -29
- package/src/socket/business-communities.js +12 -31
- package/src/socket/chats.js +5 -8
- package/src/socket/groups-newsletter.js +18 -17
- package/src/socket/messages-recv.js +81 -127
- package/src/socket/messages-send.js +372 -139
- package/src/socket/socket-core.js +227 -157
- package/src/socket/usync.js +2 -6
- package/src/socket-client/native-websocket.js +344 -0
- package/src/socket-client/websocket-client.js +84 -38
- package/src/utils/auth-state-core.js +14 -56
- package/src/utils/auth-state-storage.js +7 -42
- package/src/utils/chat-sync.js +4 -26
- package/src/utils/media.js +15 -71
- package/src/utils/message-compose.js +208 -85
- package/src/utils/message-processing.js +10 -65
- package/src/utils/username.js +19 -0
- package/src/utils/wa-protocol-core.js +71 -127
- package/src/vendor/jpeg-js/decoder.js +75 -136
- package/src/vendor/jpeg-js/encoder.js +179 -264
- package/src/vendor/pngjs/bitmapper.js +5 -27
- package/src/vendor/pngjs/bitpacker.js +4 -12
- package/src/vendor/pngjs/constants.js +3 -11
- package/src/vendor/pngjs/crc.js +0 -6
- package/src/vendor/pngjs/filter-pack.js +1 -7
- package/src/vendor/pngjs/filter-parse-sync.js +0 -6
- package/src/vendor/pngjs/filter-parse.js +1 -11
- package/src/vendor/pngjs/format-normaliser.js +5 -11
- package/src/vendor/pngjs/interlace.js +7 -24
- package/src/vendor/pngjs/packer-sync.js +0 -11
- package/src/vendor/pngjs/packer.js +7 -15
- package/src/vendor/pngjs/paeth-predictor.js +0 -6
- package/src/vendor/pngjs/parser-sync.js +0 -8
- package/src/vendor/pngjs/parser.js +5 -29
- package/src/vendor/pngjs/png-sync.js +0 -7
- package/src/vendor/pngjs/sync-inflate.js +7 -16
- package/src/vendor/pngjs/sync-reader.js +4 -11
- package/src/voip/WaVoipCoordinator.js +158 -0
- package/src/voip/app-data/WaAppDataStream.js +193 -0
- package/src/voip/app-data/protocol.js +172 -0
- package/src/voip/bytes.js +63 -0
- package/src/voip/call/WaCallManager.js +442 -0
- package/src/voip/call/WaCallMediaSession.js +1705 -0
- package/src/voip/call/call-state.js +203 -0
- package/src/voip/crypto/encryption.js +13 -0
- package/src/voip/crypto/primitives.js +21 -0
- package/src/voip/crypto/rtcp.js +66 -0
- package/src/voip/crypto/srtp.js +180 -0
- package/src/voip/crypto/ssrc.js +15 -0
- package/src/voip/index.js +628 -0
- package/src/voip/media/WaAudioEngine.js +486 -0
- package/src/voip/media/WaVideoEngine.js +275 -0
- package/src/voip/media/audio-codec.js +0 -0
- package/src/voip/media/audio-reorder.js +107 -0
- package/src/voip/media/h264.js +246 -0
- package/src/voip/media/mlow-codec.js +465 -0
- package/src/voip/media/rtcp.js +68 -0
- package/src/voip/media/rtp.js +242 -0
- package/src/voip/protobuf.js +30 -0
- package/src/voip/relay/WaManualRelay.js +609 -0
- package/src/voip/relay/datachannel/pre-negotiated.js +25 -0
- package/src/voip/relay/dtls/aead.js +46 -0
- package/src/voip/relay/dtls/cert-builder.js +102 -0
- package/src/voip/relay/dtls/der.js +98 -0
- package/src/voip/relay/dtls/ecdhe.js +37 -0
- package/src/voip/relay/dtls/handshake-framing.js +100 -0
- package/src/voip/relay/dtls/handshake-messages.js +158 -0
- package/src/voip/relay/dtls/handshake.js +582 -0
- package/src/voip/relay/dtls/prf.js +39 -0
- package/src/voip/relay/dtls/record.js +56 -0
- package/src/voip/relay/relay-ack.js +141 -0
- package/src/voip/relay/sctp/association.js +434 -0
- package/src/voip/relay/sctp/crc32c.js +21 -0
- package/src/voip/relay/sctp/wire.js +226 -0
- package/src/voip/relay/stun.js +463 -0
- package/src/voip/shim/baileys-resolve.js +7 -0
- package/src/voip/shim/core.js +66 -0
- package/src/voip/shim/crypto.js +6 -0
- package/src/voip/shim/proto.js +10 -0
- package/src/voip/shim/protocol.js +130 -0
- package/src/voip/shim/transport.js +82 -0
- package/src/voip/shim/util.js +207 -0
- package/src/voip/signaling/bridge.js +102 -0
- package/src/voip/signaling/screen-share.js +60 -0
- package/src/voip/signaling/signaling.js +491 -0
- package/src/voip/types.js +69 -0
- package/src/voip/voip-deps.js +243 -0
- package/src/voip/voipClient.js +314 -0
- package/src/wam/wam-constants.js +0 -1
- package/src/wam/wam.js +2 -7
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { WA_MESSAGE_TAGS } from './shim/protocol.js';
|
|
2
|
+
import { WaCallManager } from './call/WaCallManager.js';
|
|
3
|
+
import { routeCallAck, routeCallReceipt, routeCallStanza } from './signaling/bridge.js';
|
|
4
|
+
export class WaVoipCoordinator {
|
|
5
|
+
manager;
|
|
6
|
+
deps;
|
|
7
|
+
logger;
|
|
8
|
+
unregisterHandlers = [];
|
|
9
|
+
constructor(ctx, options = {}) {
|
|
10
|
+
this.deps = ctx.deps;
|
|
11
|
+
this.logger = ctx.logger.child({ scope: 'voip' }, { level: options.logLevel });
|
|
12
|
+
this.manager = new WaCallManager({
|
|
13
|
+
deps: ctx.deps,
|
|
14
|
+
stores: ctx.stores,
|
|
15
|
+
logger: this.logger,
|
|
16
|
+
maxConcurrentCalls: options.maxConcurrentCalls
|
|
17
|
+
});
|
|
18
|
+
this.registerIncomingHandlers(ctx);
|
|
19
|
+
this.wireClientEvents(ctx);
|
|
20
|
+
}
|
|
21
|
+
async startCall(options) {
|
|
22
|
+
return this.manager.startCall(options);
|
|
23
|
+
}
|
|
24
|
+
async acceptCall(callId) {
|
|
25
|
+
return this.manager.acceptCall(callId);
|
|
26
|
+
}
|
|
27
|
+
async rejectCall(callId, reason) {
|
|
28
|
+
return this.manager.rejectCall(callId, reason);
|
|
29
|
+
}
|
|
30
|
+
async endCall(callId, reason) {
|
|
31
|
+
return this.manager.endCall(callId, reason);
|
|
32
|
+
}
|
|
33
|
+
async loadAudio(callId, audioPath) {
|
|
34
|
+
return this.manager.loadAudio(callId, audioPath);
|
|
35
|
+
}
|
|
36
|
+
async loadVideo(callId, videoPath) {
|
|
37
|
+
return this.manager.loadVideo(callId, videoPath);
|
|
38
|
+
}
|
|
39
|
+
async enableVideoMidCall(callId) {
|
|
40
|
+
return this.manager.enableVideoMidCall(callId);
|
|
41
|
+
}
|
|
42
|
+
async disableVideoMidCall(callId, options) {
|
|
43
|
+
return this.manager.disableVideoMidCall(callId, options);
|
|
44
|
+
}
|
|
45
|
+
async startVideoMidCall(callId) {
|
|
46
|
+
return this.manager.startVideoMidCall(callId);
|
|
47
|
+
}
|
|
48
|
+
async stopVideoMidCall(callId, options) {
|
|
49
|
+
return this.manager.stopVideoMidCall(callId, options);
|
|
50
|
+
}
|
|
51
|
+
async swapVideoSource(callId, videoPath) {
|
|
52
|
+
return this.manager.swapVideoSource(callId, videoPath);
|
|
53
|
+
}
|
|
54
|
+
setMute(callId, muted) {
|
|
55
|
+
this.manager.setMute(callId, muted);
|
|
56
|
+
}
|
|
57
|
+
async setHandRaised(callId, raised) {
|
|
58
|
+
return this.manager.setHandRaised(callId, raised);
|
|
59
|
+
}
|
|
60
|
+
async setScreenShare(callId, sharing) {
|
|
61
|
+
return this.manager.setScreenShare(callId, sharing);
|
|
62
|
+
}
|
|
63
|
+
sendReaction(callId, reaction) {
|
|
64
|
+
return this.manager.sendReaction(callId, reaction);
|
|
65
|
+
}
|
|
66
|
+
setExternalAudioMode(callId, enabled) {
|
|
67
|
+
this.manager.setExternalAudioMode(callId, enabled);
|
|
68
|
+
}
|
|
69
|
+
feedLiveAudio(callId, data) {
|
|
70
|
+
return this.manager.feedLiveAudio(callId, data);
|
|
71
|
+
}
|
|
72
|
+
getLiveBufferMs(callId) {
|
|
73
|
+
return this.manager.getLiveBufferMs(callId);
|
|
74
|
+
}
|
|
75
|
+
getFeedWatermarksMs() {
|
|
76
|
+
return this.manager.getFeedWatermarksMs();
|
|
77
|
+
}
|
|
78
|
+
getCall(callId) {
|
|
79
|
+
return this.manager.getCall(callId);
|
|
80
|
+
}
|
|
81
|
+
getCalls() {
|
|
82
|
+
return this.manager.getCalls();
|
|
83
|
+
}
|
|
84
|
+
on(event, listener) {
|
|
85
|
+
this.manager.on(event, listener);
|
|
86
|
+
return this;
|
|
87
|
+
}
|
|
88
|
+
off(event, listener) {
|
|
89
|
+
this.manager.off(event, listener);
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
once(event, listener) {
|
|
93
|
+
this.manager.once(event, listener);
|
|
94
|
+
return this;
|
|
95
|
+
}
|
|
96
|
+
dispose() {
|
|
97
|
+
for (const unregister of this.unregisterHandlers.splice(0)) {
|
|
98
|
+
unregister();
|
|
99
|
+
}
|
|
100
|
+
this.manager.destroy();
|
|
101
|
+
}
|
|
102
|
+
registerIncomingHandlers(ctx) {
|
|
103
|
+
this.unregisterHandlers.push(ctx.registerIncomingHandler({
|
|
104
|
+
tag: 'call',
|
|
105
|
+
prepend: true,
|
|
106
|
+
handler: async (node) => {
|
|
107
|
+
const tag = await routeCallStanza(this.manager, this.deps, node, this.logger);
|
|
108
|
+
return tag !== null;
|
|
109
|
+
}
|
|
110
|
+
}), ctx.registerIncomingHandler({
|
|
111
|
+
tag: WA_MESSAGE_TAGS.ACK,
|
|
112
|
+
prepend: true,
|
|
113
|
+
handler: async (node) => {
|
|
114
|
+
if (node.attrs.class !== 'call') {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
await routeCallAck(this.manager, node);
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
}), ctx.registerIncomingHandler({
|
|
121
|
+
tag: WA_MESSAGE_TAGS.RECEIPT,
|
|
122
|
+
prepend: true,
|
|
123
|
+
handler: async (node) => routeCallReceipt(this.deps, node)
|
|
124
|
+
}));
|
|
125
|
+
}
|
|
126
|
+
wireClientEvents(ctx) {
|
|
127
|
+
this.manager.on('call_state', (call) => {
|
|
128
|
+
ctx.emit('voip_call_state', call);
|
|
129
|
+
});
|
|
130
|
+
this.manager.on('call_incoming', (call) => {
|
|
131
|
+
ctx.emit('voip_call_incoming', call);
|
|
132
|
+
});
|
|
133
|
+
this.manager.on('call_ended', (call) => {
|
|
134
|
+
ctx.emit('voip_call_ended', call);
|
|
135
|
+
});
|
|
136
|
+
this.manager.on('call_inbound_audio', (call, pcm) => {
|
|
137
|
+
ctx.emit('voip_call_inbound_audio', { call, pcm });
|
|
138
|
+
});
|
|
139
|
+
this.manager.on('call_outbound_audio_finished', (call) => {
|
|
140
|
+
ctx.emit('voip_call_outbound_audio_finished', call);
|
|
141
|
+
});
|
|
142
|
+
this.manager.on('call_peer_mute', (call, muted) => {
|
|
143
|
+
ctx.emit('voip_call_peer_mute', { call, muted });
|
|
144
|
+
});
|
|
145
|
+
this.manager.on('call_hand_raise', (call, participantJid, raised) => {
|
|
146
|
+
ctx.emit('voip_call_hand_raise', { call, participantJid, raised });
|
|
147
|
+
});
|
|
148
|
+
this.manager.on('call_reaction', (call, reaction) => {
|
|
149
|
+
ctx.emit('voip_call_reaction', { call, reaction });
|
|
150
|
+
});
|
|
151
|
+
this.manager.on('call_screen_share', (call, share) => {
|
|
152
|
+
ctx.emit('voip_call_screen_share', { call, share });
|
|
153
|
+
});
|
|
154
|
+
this.manager.on('call_error', (error) => {
|
|
155
|
+
ctx.emit('voip_call_error', error);
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { toError } from '../shim/util.js';
|
|
2
|
+
import { randomInt } from '../crypto/primitives.js';
|
|
3
|
+
import { RtpHeader, RtpPacket } from '../media/rtp.js';
|
|
4
|
+
import { decodeAppDataPayload, encodeReactionPayload } from './protocol.js';
|
|
5
|
+
|
|
6
|
+
const DEFAULT_RETRANSMISSION_INTERVAL_MS = 60;
|
|
7
|
+
const DEFAULT_CLEAR_INTERVAL_MS = 600;
|
|
8
|
+
|
|
9
|
+
const MAX_TRACKED_TRANSACTIONS = 64;
|
|
10
|
+
|
|
11
|
+
export const WA_APP_DATA_PAYLOAD_TYPE = 119;
|
|
12
|
+
|
|
13
|
+
export class WaAppDataStream {
|
|
14
|
+
ssrc;
|
|
15
|
+
logger;
|
|
16
|
+
sendPacket;
|
|
17
|
+
retransmissionIntervalMs;
|
|
18
|
+
clearIntervalMs;
|
|
19
|
+
sequenceNumber = randomInt(0, 65_536);
|
|
20
|
+
|
|
21
|
+
timestamp = randomInt(0, 0xffffffff);
|
|
22
|
+
outgoing = null;
|
|
23
|
+
retransmitTimer = null;
|
|
24
|
+
|
|
25
|
+
seenTransactions = new Set();
|
|
26
|
+
|
|
27
|
+
nextTransactionId = 1n;
|
|
28
|
+
learnedPayloadType;
|
|
29
|
+
configuredPayloadType;
|
|
30
|
+
sframeRequired = false;
|
|
31
|
+
sframeProtect = null;
|
|
32
|
+
txReactionCount = 0;
|
|
33
|
+
txReactionErrorCount = 0;
|
|
34
|
+
rxReactionCount = 0;
|
|
35
|
+
constructor(options) {
|
|
36
|
+
this.logger = options.logger;
|
|
37
|
+
this.ssrc = options.ssrc;
|
|
38
|
+
this.sendPacket = options.sendPacket;
|
|
39
|
+
this.configuredPayloadType = options.payloadType ?? null;
|
|
40
|
+
this.learnedPayloadType = null;
|
|
41
|
+
this.retransmissionIntervalMs =
|
|
42
|
+
options.retransmissionIntervalMs ?? DEFAULT_RETRANSMISSION_INTERVAL_MS;
|
|
43
|
+
this.clearIntervalMs = options.clearIntervalMs ?? DEFAULT_CLEAR_INTERVAL_MS;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
get payloadType() {
|
|
47
|
+
return this.configuredPayloadType ?? WA_APP_DATA_PAYLOAD_TYPE;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
get peerPayloadType() {
|
|
51
|
+
return this.learnedPayloadType;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
observeInboundPayloadType(payloadType) {
|
|
55
|
+
if (this.configuredPayloadType !== null || this.learnedPayloadType === payloadType)
|
|
56
|
+
return;
|
|
57
|
+
this.learnedPayloadType = payloadType;
|
|
58
|
+
this.logger.debug('app data payload type learned from peer', {
|
|
59
|
+
payloadType,
|
|
60
|
+
ssrc: `0x${this.ssrc.toString(16)}`
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
setSframe(required, protect) {
|
|
65
|
+
this.sframeRequired = required;
|
|
66
|
+
this.sframeProtect = protect;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
sendReaction(reaction) {
|
|
70
|
+
const transactionId = this.nextTransactionId++;
|
|
71
|
+
const payload = encodeReactionPayload({ transactionId, reaction });
|
|
72
|
+
const clearAt = Date.now() + this.clearIntervalMs;
|
|
73
|
+
this.outgoing = { payload, transactionId, reaction, clearAt };
|
|
74
|
+
this.txReactionCount++;
|
|
75
|
+
const sent = this.transmit();
|
|
76
|
+
this.armRetransmission();
|
|
77
|
+
this.logger.debug('call reaction queued', {
|
|
78
|
+
reaction,
|
|
79
|
+
transactionId: transactionId.toString(),
|
|
80
|
+
ssrc: `0x${this.ssrc.toString(16)}`,
|
|
81
|
+
firstAttemptSent: sent,
|
|
82
|
+
total: this.txReactionCount
|
|
83
|
+
});
|
|
84
|
+
return sent;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
receive(payload, ssrc) {
|
|
88
|
+
if (payload.length === 0)
|
|
89
|
+
return EMPTY_REACTIONS;
|
|
90
|
+
const decoded = decodeAppDataPayload(payload);
|
|
91
|
+
if (!decoded) {
|
|
92
|
+
this.logger.debug('app data payload not understood', {
|
|
93
|
+
ssrc: `0x${this.ssrc.toString(16)}`,
|
|
94
|
+
bytes: payload.length,
|
|
95
|
+
sframeRequired: this.sframeRequired,
|
|
96
|
+
sframeKeyed: this.sframeProtect !== null
|
|
97
|
+
});
|
|
98
|
+
return EMPTY_REACTIONS;
|
|
99
|
+
}
|
|
100
|
+
if (decoded.truncated) {
|
|
101
|
+
this.logger.debug('app data payload carried more messages than are read', {
|
|
102
|
+
ssrc: `0x${ssrc.toString(16)}`,
|
|
103
|
+
read: decoded.items.length
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
const fresh = [];
|
|
107
|
+
for (const item of decoded.items) {
|
|
108
|
+
const reaction = item.reaction;
|
|
109
|
+
if (!reaction)
|
|
110
|
+
continue;
|
|
111
|
+
const key = `${ssrc}:${reaction.transactionId}`;
|
|
112
|
+
if (this.seenTransactions.has(key))
|
|
113
|
+
continue;
|
|
114
|
+
this.rememberTransaction(key);
|
|
115
|
+
this.rxReactionCount++;
|
|
116
|
+
fresh.push(reaction);
|
|
117
|
+
}
|
|
118
|
+
if (fresh.length > 0) {
|
|
119
|
+
this.logger.debug('call reaction received', {
|
|
120
|
+
shape: decoded.shape,
|
|
121
|
+
count: fresh.length,
|
|
122
|
+
total: this.rxReactionCount
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
return fresh;
|
|
126
|
+
}
|
|
127
|
+
close() {
|
|
128
|
+
this.clearOutgoing();
|
|
129
|
+
this.seenTransactions.clear();
|
|
130
|
+
}
|
|
131
|
+
rememberTransaction(key) {
|
|
132
|
+
if (this.seenTransactions.size >= MAX_TRACKED_TRANSACTIONS) {
|
|
133
|
+
const oldest = this.seenTransactions.values().next().value;
|
|
134
|
+
if (oldest !== undefined)
|
|
135
|
+
this.seenTransactions.delete(oldest);
|
|
136
|
+
}
|
|
137
|
+
this.seenTransactions.add(key);
|
|
138
|
+
}
|
|
139
|
+
armRetransmission() {
|
|
140
|
+
if (this.retransmitTimer)
|
|
141
|
+
return;
|
|
142
|
+
this.retransmitTimer = setInterval(() => {
|
|
143
|
+
this.onRetransmissionTick();
|
|
144
|
+
}, this.retransmissionIntervalMs);
|
|
145
|
+
this.retransmitTimer.unref?.();
|
|
146
|
+
}
|
|
147
|
+
onRetransmissionTick() {
|
|
148
|
+
const outgoing = this.outgoing;
|
|
149
|
+
if (!outgoing) {
|
|
150
|
+
this.clearOutgoing();
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
if (Date.now() >= outgoing.clearAt) {
|
|
154
|
+
this.logger.debug('call reaction cleared from send buffer', {
|
|
155
|
+
transactionId: outgoing.transactionId.toString()
|
|
156
|
+
});
|
|
157
|
+
this.clearOutgoing();
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
this.transmit();
|
|
161
|
+
}
|
|
162
|
+
clearOutgoing() {
|
|
163
|
+
this.outgoing = null;
|
|
164
|
+
if (this.retransmitTimer) {
|
|
165
|
+
clearInterval(this.retransmitTimer);
|
|
166
|
+
this.retransmitTimer = null;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
transmit() {
|
|
170
|
+
const outgoing = this.outgoing;
|
|
171
|
+
if (!outgoing)
|
|
172
|
+
return false;
|
|
173
|
+
const payloadType = this.payloadType;
|
|
174
|
+
try {
|
|
175
|
+
const header = new RtpHeader(payloadType, this.sequenceNumber, this.timestamp, this.ssrc);
|
|
176
|
+
this.sequenceNumber = (this.sequenceNumber + 1) & 0xffff;
|
|
177
|
+
const payload = this.sframeProtect
|
|
178
|
+
? this.sframeProtect(outgoing.payload)
|
|
179
|
+
: outgoing.payload;
|
|
180
|
+
return this.sendPacket(new RtpPacket(header, payload));
|
|
181
|
+
}
|
|
182
|
+
catch (err) {
|
|
183
|
+
this.txReactionErrorCount++;
|
|
184
|
+
this.logger.debug('failed to send app data rtp packet', {
|
|
185
|
+
ssrc: `0x${this.ssrc.toString(16)}`,
|
|
186
|
+
errors: this.txReactionErrorCount,
|
|
187
|
+
message: toError(err).message
|
|
188
|
+
});
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
const EMPTY_REACTIONS = [];
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { concatBytes, EMPTY_BYTES, TEXT_DECODER, TEXT_ENCODER } from '../bytes.js';
|
|
2
|
+
import { encodeProtoLengthDelimited, encodeProtoVarintField } from '../protobuf.js';
|
|
3
|
+
|
|
4
|
+
const APP_DATA_PAYLOADS_MESSAGES = 1;
|
|
5
|
+
const APP_DATA_MESSAGE_REACTION_INFO = 1;
|
|
6
|
+
const APP_DATA_MESSAGE_AR_EFFECT_INFO = 3;
|
|
7
|
+
const REACTION_INFO_TRANSACTION_ID = 1;
|
|
8
|
+
const REACTION_INFO_REACTION = 2;
|
|
9
|
+
const AR_EFFECT_INFO_TRANSACTION_ID = 1;
|
|
10
|
+
const AR_EFFECT_INFO_AR_EFFECT_ID = 2;
|
|
11
|
+
const WIRE_TYPE_VARINT = 0;
|
|
12
|
+
const WIRE_TYPE_FIXED64 = 1;
|
|
13
|
+
const WIRE_TYPE_LENGTH_DELIMITED = 2;
|
|
14
|
+
const WIRE_TYPE_FIXED32 = 5;
|
|
15
|
+
const VARINT_CONTINUATION = 0x80;
|
|
16
|
+
const VARINT_PAYLOAD_MASK = 0x7f;
|
|
17
|
+
const VARINT_SHIFT = 7n;
|
|
18
|
+
|
|
19
|
+
const MAX_MESSAGES_PER_PAYLOAD = 32;
|
|
20
|
+
function readVarint(data, offset) {
|
|
21
|
+
let value = 0n;
|
|
22
|
+
let shift = 0n;
|
|
23
|
+
let cursor = offset;
|
|
24
|
+
while (cursor < data.length) {
|
|
25
|
+
const byte = data[cursor++];
|
|
26
|
+
value |= BigInt(byte & VARINT_PAYLOAD_MASK) << shift;
|
|
27
|
+
if ((byte & VARINT_CONTINUATION) === 0)
|
|
28
|
+
return { value, next: cursor };
|
|
29
|
+
shift += VARINT_SHIFT;
|
|
30
|
+
if (shift > 63n)
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
throw new Error('truncated protobuf varint');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function* readFields(data) {
|
|
37
|
+
let offset = 0;
|
|
38
|
+
while (offset < data.length) {
|
|
39
|
+
const tag = readVarint(data, offset);
|
|
40
|
+
offset = tag.next;
|
|
41
|
+
const fieldNumber = Number(tag.value >> 3n);
|
|
42
|
+
const wireType = Number(tag.value & 0x7n);
|
|
43
|
+
if (fieldNumber === 0)
|
|
44
|
+
throw new Error('invalid protobuf field number 0');
|
|
45
|
+
if (wireType === WIRE_TYPE_VARINT) {
|
|
46
|
+
const read = readVarint(data, offset);
|
|
47
|
+
offset = read.next;
|
|
48
|
+
yield { fieldNumber, wireType, varint: read.value, bytes: EMPTY_BYTES };
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (wireType === WIRE_TYPE_LENGTH_DELIMITED) {
|
|
52
|
+
const length = readVarint(data, offset);
|
|
53
|
+
const start = length.next;
|
|
54
|
+
const end = start + Number(length.value);
|
|
55
|
+
if (end > data.length)
|
|
56
|
+
throw new Error('truncated protobuf length-delimited field');
|
|
57
|
+
offset = end;
|
|
58
|
+
yield { fieldNumber, wireType, varint: 0n, bytes: data.subarray(start, end) };
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (wireType === WIRE_TYPE_FIXED64) {
|
|
62
|
+
offset += 8;
|
|
63
|
+
}
|
|
64
|
+
else if (wireType === WIRE_TYPE_FIXED32) {
|
|
65
|
+
offset += 4;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
throw new Error(`unsupported protobuf wire type ${wireType}`);
|
|
69
|
+
}
|
|
70
|
+
if (offset > data.length)
|
|
71
|
+
throw new Error('truncated protobuf fixed-width field');
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function encodeReactionPayload(reaction) {
|
|
76
|
+
const reactionInfo = concatBytes([
|
|
77
|
+
encodeProtoVarintField(REACTION_INFO_TRANSACTION_ID, reaction.transactionId),
|
|
78
|
+
encodeProtoLengthDelimited(REACTION_INFO_REACTION, TEXT_ENCODER.encode(reaction.reaction))
|
|
79
|
+
]);
|
|
80
|
+
const message = encodeProtoLengthDelimited(APP_DATA_MESSAGE_REACTION_INFO, reactionInfo);
|
|
81
|
+
return encodeProtoLengthDelimited(APP_DATA_PAYLOADS_MESSAGES, message);
|
|
82
|
+
}
|
|
83
|
+
function decodeReactionInfo(data, requireTransactionId) {
|
|
84
|
+
let transactionId = null;
|
|
85
|
+
let reaction = null;
|
|
86
|
+
for (const field of readFields(data)) {
|
|
87
|
+
if (field.fieldNumber === REACTION_INFO_TRANSACTION_ID && field.wireType === 0) {
|
|
88
|
+
transactionId = field.varint;
|
|
89
|
+
}
|
|
90
|
+
else if (field.fieldNumber === REACTION_INFO_REACTION && field.wireType === 2) {
|
|
91
|
+
reaction = TEXT_DECODER.decode(field.bytes);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (reaction === null)
|
|
95
|
+
return null;
|
|
96
|
+
if (transactionId === null && requireTransactionId)
|
|
97
|
+
return null;
|
|
98
|
+
return { transactionId: transactionId ?? 0n, reaction };
|
|
99
|
+
}
|
|
100
|
+
function decodeArEffectInfo(data, requireTransactionId) {
|
|
101
|
+
let transactionId = null;
|
|
102
|
+
let arEffectId = null;
|
|
103
|
+
for (const field of readFields(data)) {
|
|
104
|
+
if (field.fieldNumber === AR_EFFECT_INFO_TRANSACTION_ID && field.wireType === 0) {
|
|
105
|
+
transactionId = field.varint;
|
|
106
|
+
}
|
|
107
|
+
else if (field.fieldNumber === AR_EFFECT_INFO_AR_EFFECT_ID && field.wireType === 2) {
|
|
108
|
+
arEffectId = TEXT_DECODER.decode(field.bytes);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (arEffectId === null)
|
|
112
|
+
return null;
|
|
113
|
+
if (transactionId === null && requireTransactionId)
|
|
114
|
+
return null;
|
|
115
|
+
return { transactionId: transactionId ?? 0n, arEffectId };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function decodeAppDataMessage(data, strict) {
|
|
119
|
+
let reaction = null;
|
|
120
|
+
let arEffect = null;
|
|
121
|
+
for (const field of readFields(data)) {
|
|
122
|
+
if (field.wireType !== WIRE_TYPE_LENGTH_DELIMITED)
|
|
123
|
+
continue;
|
|
124
|
+
if (field.fieldNumber === APP_DATA_MESSAGE_REACTION_INFO) {
|
|
125
|
+
reaction = decodeReactionInfo(field.bytes, strict);
|
|
126
|
+
}
|
|
127
|
+
else if (field.fieldNumber === APP_DATA_MESSAGE_AR_EFFECT_INFO) {
|
|
128
|
+
arEffect = decodeArEffectInfo(field.bytes, strict);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (reaction)
|
|
132
|
+
return arEffect ? { reaction, arEffect } : { reaction };
|
|
133
|
+
return arEffect ? { arEffect } : null;
|
|
134
|
+
}
|
|
135
|
+
function decodeAsPayloads(data) {
|
|
136
|
+
const items = [];
|
|
137
|
+
let truncated = false;
|
|
138
|
+
for (const field of readFields(data)) {
|
|
139
|
+
if (field.fieldNumber !== APP_DATA_PAYLOADS_MESSAGES)
|
|
140
|
+
continue;
|
|
141
|
+
if (field.wireType !== WIRE_TYPE_LENGTH_DELIMITED)
|
|
142
|
+
continue;
|
|
143
|
+
if (items.length >= MAX_MESSAGES_PER_PAYLOAD) {
|
|
144
|
+
truncated = true;
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
const item = decodeAppDataMessage(field.bytes, false);
|
|
148
|
+
if (item)
|
|
149
|
+
items.push(item);
|
|
150
|
+
}
|
|
151
|
+
return { items, truncated };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function decodeAppDataPayload(data) {
|
|
155
|
+
try {
|
|
156
|
+
const nested = decodeAsPayloads(data);
|
|
157
|
+
if (nested.items.length > 0) {
|
|
158
|
+
return { shape: 'payloads', items: nested.items, truncated: nested.truncated };
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
}
|
|
163
|
+
try {
|
|
164
|
+
const flat = decodeAppDataMessage(data, true);
|
|
165
|
+
if (flat)
|
|
166
|
+
return { shape: 'message', items: [flat], truncated: false };
|
|
167
|
+
}
|
|
168
|
+
catch {
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { base64ToBytes, bytesToBase64, bytesToHex, concatBytes, EMPTY_BYTES, TEXT_DECODER, TEXT_ENCODER, toBytesView } from './shim/util.js';
|
|
2
|
+
export { base64ToBytes, bytesToBase64, bytesToHex, concatBytes, EMPTY_BYTES, TEXT_DECODER, TEXT_ENCODER, toBytesView };
|
|
3
|
+
function ensureBounds(buf, offset, size) {
|
|
4
|
+
if (!Number.isInteger(offset) || offset < 0 || offset + size > buf.length) {
|
|
5
|
+
throw new RangeError(`byte access out of range: offset ${offset}, size ${size}, length ${buf.length}`);
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export function readUInt16BE(buf, offset) {
|
|
9
|
+
ensureBounds(buf, offset, 2);
|
|
10
|
+
return (buf[offset] << 8) | buf[offset + 1];
|
|
11
|
+
}
|
|
12
|
+
export function readUInt32BE(buf, offset) {
|
|
13
|
+
ensureBounds(buf, offset, 4);
|
|
14
|
+
return (((buf[offset] << 24) |
|
|
15
|
+
(buf[offset + 1] << 16) |
|
|
16
|
+
(buf[offset + 2] << 8) |
|
|
17
|
+
buf[offset + 3]) >>>
|
|
18
|
+
0);
|
|
19
|
+
}
|
|
20
|
+
export function readUInt32LE(buf, offset) {
|
|
21
|
+
ensureBounds(buf, offset, 4);
|
|
22
|
+
return ((buf[offset] |
|
|
23
|
+
(buf[offset + 1] << 8) |
|
|
24
|
+
(buf[offset + 2] << 16) |
|
|
25
|
+
(buf[offset + 3] << 24)) >>>
|
|
26
|
+
0);
|
|
27
|
+
}
|
|
28
|
+
export function readBigUInt64BE(buf, offset) {
|
|
29
|
+
const hi = readUInt32BE(buf, offset);
|
|
30
|
+
const lo = readUInt32BE(buf, offset + 4);
|
|
31
|
+
return (BigInt(hi) << 32n) | BigInt(lo);
|
|
32
|
+
}
|
|
33
|
+
export function writeUInt16BE(buf, value, offset) {
|
|
34
|
+
ensureBounds(buf, offset, 2);
|
|
35
|
+
buf[offset] = (value >> 8) & 0xff;
|
|
36
|
+
buf[offset + 1] = value & 0xff;
|
|
37
|
+
}
|
|
38
|
+
export function writeUInt32BE(buf, value, offset) {
|
|
39
|
+
ensureBounds(buf, offset, 4);
|
|
40
|
+
buf[offset] = (value >> 24) & 0xff;
|
|
41
|
+
buf[offset + 1] = (value >> 16) & 0xff;
|
|
42
|
+
buf[offset + 2] = (value >> 8) & 0xff;
|
|
43
|
+
buf[offset + 3] = value & 0xff;
|
|
44
|
+
}
|
|
45
|
+
export function writeUInt32LE(buf, value, offset) {
|
|
46
|
+
ensureBounds(buf, offset, 4);
|
|
47
|
+
buf[offset] = value & 0xff;
|
|
48
|
+
buf[offset + 1] = (value >> 8) & 0xff;
|
|
49
|
+
buf[offset + 2] = (value >> 16) & 0xff;
|
|
50
|
+
buf[offset + 3] = (value >> 24) & 0xff;
|
|
51
|
+
}
|
|
52
|
+
export function writeBigUInt64BE(buf, value, offset) {
|
|
53
|
+
writeUInt32BE(buf, Number((value >> 32n) & 0xffffffffn), offset);
|
|
54
|
+
writeUInt32BE(buf, Number(value & 0xffffffffn), offset + 4);
|
|
55
|
+
}
|
|
56
|
+
export function toArrayBuffer(bytes) {
|
|
57
|
+
if (bytes.byteOffset === 0 &&
|
|
58
|
+
bytes.byteLength === bytes.buffer.byteLength &&
|
|
59
|
+
bytes.buffer instanceof ArrayBuffer) {
|
|
60
|
+
return bytes.buffer;
|
|
61
|
+
}
|
|
62
|
+
return bytes.slice().buffer;
|
|
63
|
+
}
|