@zelynskybail/baileys 2.6.31 → 2.6.35
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/BUILD_STATUS.md +17 -18
- package/CHANGELOG.md +21 -0
- package/LICENSE-JAPOFC-VOIP +21 -0
- package/NOTICE.md +3 -0
- package/README.md +53 -8
- package/THIRD_PARTY_NOTICES.md +12 -0
- package/lib/Utils/ffmpeg-path.js +20 -0
- package/lib/VoIP/audio-feeder.d.ts +20 -0
- package/lib/VoIP/audio-feeder.js +208 -0
- package/lib/VoIP/call-recorder.d.ts +13 -0
- package/lib/VoIP/call-recorder.js +77 -0
- package/lib/VoIP/group-call.d.ts +105 -0
- package/lib/VoIP/group-call.js +718 -0
- package/lib/VoIP/index.d.ts +5 -0
- package/lib/VoIP/index.js +18 -0
- package/lib/VoIP/relay-transport.d.ts +43 -0
- package/lib/VoIP/relay-transport.js +563 -0
- package/lib/VoIP/signaling.d.ts +26 -0
- package/lib/VoIP/signaling.js +662 -0
- package/lib/VoIP/types.d.ts +69 -0
- package/lib/VoIP/types.js +19 -0
- package/lib/VoIP/wasm-engine.d.ts +120 -0
- package/lib/VoIP/wasm-engine.js +1405 -0
- package/lib/VoIP/worker-bootstrap.d.ts +4 -0
- package/lib/VoIP/worker-bootstrap.js +1063 -0
- package/lib/assets/wasm/integrity.json +25 -0
- package/lib/assets/wasm/loader.d.ts +5 -0
- package/lib/assets/wasm/loader.js +10 -0
- package/lib/assets/wasm/whatsapp.wasm +0 -0
- package/lib/assets/wasm/worker-modules.d.ts +5 -0
- package/lib/assets/wasm/worker-modules.js +318 -0
- package/package.json +16 -6
- package/scripts/check-publish-eligibility.mjs +13 -0
- package/scripts/fetch-wasm-resources.mjs +350 -0
- package/scripts/verify.mjs +56 -0
package/BUILD_STATUS.md
CHANGED
|
@@ -1,21 +1,20 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Build Status — 2.6.35
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
## Added and fixed
|
|
4
|
+
- Bundled the supplied WhatsApp Web WASM resource snapshot: `loader.js`, `worker-modules.js`, `whatsapp.wasm`, and `integrity.json` (client revision 1048638736; refresh with `npm run voip:fetch-wasm`).
|
|
5
|
+
- Fixed false-positive worker-bundle loader detection that skipped the standalone WASM loader.
|
|
6
|
+
- Fixed worker-bootstrap lookup for the package `lib/assets/wasm` resource directory.
|
|
7
|
+
- Updated the WASM fetch script to create its destination directory and validate the loader module definition.
|
|
8
|
+
- Release verification checks presence, size, and SHA-256 for all manifest-listed WASM assets; Node test suite includes WASM integrity and loader-detection regression tests.
|
|
9
|
+
- Preserved existing Zelynsky messaging, VoIP, CallManager, and group-call APIs.
|
|
4
10
|
|
|
5
|
-
##
|
|
6
|
-
-
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
- Newsletter, groups, communities, business, privacy, profile, message edit/delete and status protocol methods present in the supplied core.
|
|
11
|
-
- Call event/reject/link/privacy helpers. Optional outbound 1:1 voice call client is exposed through `VoipClient` using its own linked-device session.
|
|
12
|
-
- Group Status helper for text, image, video, audio and sticker, backed by the core `swgc` path.
|
|
13
|
-
- VCardBuilder alias over the built-in contact-card builder.
|
|
14
|
-
- No automatic newsletter/channel follow or invite acceptance on connect.
|
|
11
|
+
## Runtime requirements
|
|
12
|
+
- Node.js >=20.
|
|
13
|
+
- `@roamhq/wrtc` optional native dependency for WebRTC relay transport.
|
|
14
|
+
- The package includes a pinned WASM resource snapshot. Run `npm run voip:fetch-wasm` to refresh it from WhatsApp Web.
|
|
15
|
+
- `ffmpeg` available on PATH for file-based audio sources.
|
|
15
16
|
|
|
16
|
-
##
|
|
17
|
-
-
|
|
18
|
-
-
|
|
19
|
-
- Group
|
|
20
|
-
- Runtime install, live pairing, media relay, and end-to-end tests have not been performed in this environment.
|
|
21
|
-
- Before public npm publication, review every upstream license and preserve notices. The bundled MessageBuilder license restricts redistribution as a standalone product; it is bundled as a module with the original watermark retained, but permission should be confirmed for public distribution.
|
|
17
|
+
## Status
|
|
18
|
+
- Package version and release scripts updated to 2.6.35.
|
|
19
|
+
- Static verification and package tests must pass before release.
|
|
20
|
+
- Group-call interoperability has not been tested against a live WhatsApp account in this build environment. Treat group calling as experimental.
|
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,24 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 2.6.35
|
|
4
|
+
|
|
5
|
+
- Bundled the supplied WhatsApp Web runtime snapshot (`loader.js`, `worker-modules.js`, `whatsapp.wasm`) with its SHA-256 integrity manifest; the refresh script can update it from WhatsApp Web.
|
|
6
|
+
- Fixed WASM loader detection: worker bundle references no longer incorrectly suppress loading the standalone loader module.
|
|
7
|
+
- Fixed worker-bootstrap resource lookup to search the package `lib/assets/wasm` directory passed by the WASM engine.
|
|
8
|
+
- Made `voip:fetch-wasm` create its destination directory before downloading and validate that the fetched loader actually defines the requested module.
|
|
9
|
+
- Strengthened release verification to validate all bundled WASM resource sizes and SHA-256 hashes; added loader-detection and bundled-resource regression tests.
|
|
10
|
+
- Preserved the existing Zelynsky APIs and experimental CallManager/group-call implementation.
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
## 2.6.34
|
|
14
|
+
|
|
15
|
+
- Added experimental JAPofc 2.4.7-new based shared-socket VoIP engine.
|
|
16
|
+
- Added incoming call offer handling, inbound group-call join, outbound group-call initiation, participant controls, audio feeder, relay transport, WASM engine, call recovery and WAV recording.
|
|
17
|
+
- Preserved the existing Zelynsky `CallManager` and separate-session `VoipClient` API.
|
|
18
|
+
- Added `attachVoip`, `GroupVoipClient`, `ActiveCall`, and `CallState` exports.
|
|
19
|
+
- Added WASM resource fetch script and optional `@roamhq/wrtc` dependency.
|
|
20
|
+
- Updated README and attribution notices.
|
|
21
|
+
|
|
1
22
|
## 2.6.31
|
|
2
23
|
|
|
3
24
|
- VoIP: persistent authDir (no re-QR after first pair)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) J.AP (JAPofc)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/NOTICE.md
CHANGED
|
@@ -7,3 +7,6 @@ The bundled `lib/Modded/MessageBuilder.js` is the user-supplied NIXCODE MessageB
|
|
|
7
7
|
The optional VoIP engine is a separate MIT-licensed dependency and is loaded only when requested.
|
|
8
8
|
|
|
9
9
|
This project is independent and is not affiliated with or endorsed by WhatsApp or Meta Platforms, Inc.
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
The `lib/assets/wasm/` directory includes a pinned WhatsApp Web runtime resource snapshot (`whatsapp.wasm`, `worker-modules.js`, and `loader.js`) supplied separately for this integration. These generated WhatsApp Web resources are third-party runtime assets and are not original Zelynsky or JAPofc source code; the JAPofc MIT license does not relicense WhatsApp/Meta assets. The bundled integrity manifest records their SHA-256 hashes and source URLs. The refresh script downloads replacement resources from WhatsApp Web.
|
package/README.md
CHANGED
|
@@ -123,28 +123,73 @@ await store.close()
|
|
|
123
123
|
|
|
124
124
|
Tulisnya atomik, tapi ini untuk metadata kecil. Jangan dipakai sebagai database.
|
|
125
125
|
|
|
126
|
-
## Panggilan suara
|
|
126
|
+
## Panggilan suara dan Group Call (experimental)
|
|
127
|
+
|
|
128
|
+
Adapter VoIP lama tetap tersedia dan tidak berubah:
|
|
127
129
|
|
|
128
130
|
```js
|
|
129
131
|
import { VoipClient } from '@zelynskybail/baileys'
|
|
130
132
|
|
|
131
133
|
const voip = new VoipClient({ authDir: './voip-session' })
|
|
132
134
|
await voip.connect()
|
|
133
|
-
|
|
134
135
|
const call = await voip.call('628123456789', {
|
|
135
136
|
audioSource: './audio.mp3',
|
|
136
137
|
durationMs: 30000
|
|
137
138
|
})
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
Untuk engine VoIP berbasis socket utama (port JAPofc 2.4.7-new):
|
|
142
|
+
|
|
143
|
+
```js
|
|
144
|
+
import { attachVoip } from '@zelynskybail/baileys/calls'
|
|
145
|
+
|
|
146
|
+
const voip = await attachVoip(sock, {
|
|
147
|
+
autoReject: false,
|
|
148
|
+
autoJoinGroup: false,
|
|
149
|
+
offerTtlMs: 45000
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
voip.on('incoming-call', info => {
|
|
153
|
+
console.log('Panggilan masuk:', info)
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
const pending = voip.getPendingCalls()
|
|
157
|
+
// Panggilan grup masuk yang masih pending:
|
|
158
|
+
const groupOffer = pending.find(call => call.isGroupCall)
|
|
159
|
+
if (groupOffer) {
|
|
160
|
+
const call = await voip.joinGroupCall(groupOffer.callId, {
|
|
161
|
+
audioSource: './playlist.mp3',
|
|
162
|
+
durationMs: 10 * 60 * 1000,
|
|
163
|
+
chatName: 'KuzeMD Music'
|
|
164
|
+
})
|
|
165
|
+
call.on('connected', () => console.log('Bot masuk group call'))
|
|
166
|
+
call.on('ended', reason => console.log('Group call selesai:', reason))
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
API group-call:
|
|
171
|
+
- `attachVoip(sock, config)` / `new CallManager(sock).attachVoip(config)`
|
|
172
|
+
- `getPendingCalls()`, `rejectCall(callId, from)`, `answerCall(callId, options)`
|
|
173
|
+
- `joinGroupCall(callId, options)`, `startGroupCall(groupJid, participants, options)`
|
|
174
|
+
- `rejoinGroupCall(overrides)`, `inviteToGroupCall(number)`, `removeGroupParticipant(jid)`
|
|
175
|
+
- `setAudioSource(source)`, `getActiveCall()`, `isBusy()`, `getStats()`, `recoverCall(options)`
|
|
176
|
+
- `call.setAudioSource(source)`, `call.end()`, `call.mute(boolean)`, `call.waitForEnd()`
|
|
177
|
+
|
|
178
|
+
Engine group-call ini masih **experimental** dan membutuhkan resource WhatsApp Web WASM serta native WebRTC. Paket npm membundel snapshot resource WASM yang checksum-nya diverifikasi saat build. Pasang `@roamhq/wrtc` yang cocok dengan platform Node.js lu:
|
|
179
|
+
|
|
180
|
+
```sh
|
|
181
|
+
npm i @roamhq/wrtc
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
Untuk memperbarui snapshot WASM dari endpoint WhatsApp Web, jalankan:
|
|
138
185
|
|
|
139
|
-
|
|
140
|
-
|
|
186
|
+
```sh
|
|
187
|
+
npm run voip:fetch-wasm
|
|
141
188
|
```
|
|
142
189
|
|
|
143
|
-
|
|
144
|
-
Yang belum: media RTC masuk, video call, panggilan grup.
|
|
145
|
-
`ffmpeg` hanya perlu kalau sumber audionya file.
|
|
190
|
+
Perintah tersebut otomatis membuat `lib/assets/wasm/` jika belum ada, mengunduh worker, loader, dan WASM yang saling cocok, lalu menulis `integrity.json`. Worker engine memuat loader dari `lib/assets/wasm/loader.js`; loader tidak boleh dilewati hanya karena nama modulnya disebut sebagai dependency di worker bundle.
|
|
146
191
|
|
|
147
|
-
|
|
192
|
+
`ffmpeg` diperlukan untuk memutar sumber audio berbentuk file. `attachVoip()` menggunakan socket Baileys yang sudah login; ia tidak membuat sesi linked-device kedua. Uji di akun/grup percobaan terlebih dahulu. API yang tersedia tidak menjamin WhatsApp akan menerima setiap panggilan atau bahwa koneksi RTC stabil di semua platform.
|
|
148
193
|
|
|
149
194
|
## Profil performa
|
|
150
195
|
|
package/THIRD_PARTY_NOTICES.md
CHANGED
|
@@ -6,3 +6,15 @@
|
|
|
6
6
|
- **baileys-caller** — optional outbound audio calling adapter, authored by ShellTear and distributed under MIT by the npm fork used by this project. It is loaded dynamically and its license applies to that dependency.
|
|
7
7
|
|
|
8
8
|
No third-party code is represented as wholly original ZelynskyBail work.
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
## JAPofc VoIP implementation
|
|
12
|
+
|
|
13
|
+
The experimental shared-socket VoIP engine under `lib/VoIP/group-call.js` and its supporting modules (`audio-feeder.js`, `call-recorder.js`, `relay-transport.js`, `signaling.js`, `types.js`, `wasm-engine.js`, and `worker-bootstrap.js`) are ported from `@japofc/baileys` version `2.4.7-new`, authored by J.AP (JAPofc).
|
|
14
|
+
|
|
15
|
+
The source project declares the MIT License. The port retains the original JAPofc source attribution/comments. This notice is provided in addition to the upstream Baileys and other third-party notices already included with this package.
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
## Bundled WhatsApp Web WASM resources
|
|
19
|
+
|
|
20
|
+
`lib/assets/wasm/whatsapp.wasm`, `worker-modules.js`, and `loader.js` are generated WhatsApp Web runtime resources, provided as a pinned compatibility snapshot for the experimental VoIP engine. They are not covered by the JAPofc MIT license. Their hashes, source URLs, and client revision are recorded in `lib/assets/wasm/integrity.json`. Use of these resources remains subject to the applicable WhatsApp/Meta terms.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
|
|
3
|
+
export async function resolveFfmpegPath() {
|
|
4
|
+
const override = process.env.FFMPEG_PATH;
|
|
5
|
+
if (override && existsSync(override)) return override;
|
|
6
|
+
|
|
7
|
+
try {
|
|
8
|
+
const mod = await import('ffmpeg-static');
|
|
9
|
+
const candidate = mod.default || mod.path || mod;
|
|
10
|
+
if (typeof candidate === 'string' && candidate && existsSync(candidate)) return candidate;
|
|
11
|
+
} catch {}
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
const mod = await import('@ffmpeg-installer/ffmpeg');
|
|
15
|
+
const candidate = mod.path || mod.default?.path;
|
|
16
|
+
if (typeof candidate === 'string' && candidate && existsSync(candidate)) return candidate;
|
|
17
|
+
} catch {}
|
|
18
|
+
|
|
19
|
+
return 'ffmpeg';
|
|
20
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export declare class AudioFeeder {
|
|
2
|
+
#private;
|
|
3
|
+
private readonly sampleRate;
|
|
4
|
+
private readonly channels;
|
|
5
|
+
private readonly framesPerChunk;
|
|
6
|
+
private readonly onChunk;
|
|
7
|
+
private readonly source;
|
|
8
|
+
droppedChunks: number;
|
|
9
|
+
underflowChunks: number;
|
|
10
|
+
bytesProduced: number;
|
|
11
|
+
chunksEmitted: number;
|
|
12
|
+
constructor(sampleRate: number, channels: number, framesPerChunk: number, onChunk: (chunk: Float32Array) => void, source?: string | {
|
|
13
|
+
data: Buffer;
|
|
14
|
+
ext?: string;
|
|
15
|
+
});
|
|
16
|
+
start: () => void;
|
|
17
|
+
/** Swap the decode source mid-stream; returns true when the running pipeline was restarted with the new source. */
|
|
18
|
+
swapSource: (source: string | { data: Buffer; ext?: string }) => boolean;
|
|
19
|
+
stop: () => void;
|
|
20
|
+
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* lib/VoIP/audio-feeder.js
|
|
3
|
+
* Author: J.AP (@japofc/baileys)
|
|
4
|
+
*
|
|
5
|
+
* Decodes an audio `source` to f32le PCM with ffmpeg and meters fixed-size
|
|
6
|
+
* frames to the WASM uplink at real-time cadence. Supports file paths, `lavfi:`
|
|
7
|
+
* generators, in-memory `{ data: Buffer, ext }`, and a silent default. A small
|
|
8
|
+
* warm-up buffer smooths startup; back-pressure pauses ffmpeg when the queue
|
|
9
|
+
* fills; underflows emit silence so the uplink never stalls.
|
|
10
|
+
*/
|
|
11
|
+
import { spawn } from 'node:child_process';
|
|
12
|
+
import { randomBytes } from 'node:crypto';
|
|
13
|
+
import { unlinkSync, writeFileSync } from 'node:fs';
|
|
14
|
+
import { tmpdir } from 'node:os';
|
|
15
|
+
import { join } from 'node:path';
|
|
16
|
+
|
|
17
|
+
const LOW_WATERMARK_CHUNKS = 16;
|
|
18
|
+
const MAX_QUEUED_CHUNKS = 1024;
|
|
19
|
+
const WARMUP_MS = 500;
|
|
20
|
+
|
|
21
|
+
export class AudioFeeder {
|
|
22
|
+
// public counters / config
|
|
23
|
+
sampleRate;
|
|
24
|
+
channels;
|
|
25
|
+
framesPerChunk;
|
|
26
|
+
onChunk;
|
|
27
|
+
source;
|
|
28
|
+
droppedChunks = 0;
|
|
29
|
+
underflowChunks = 0;
|
|
30
|
+
bytesProduced = 0;
|
|
31
|
+
chunksEmitted = 0;
|
|
32
|
+
|
|
33
|
+
// internal state
|
|
34
|
+
#proc = null;
|
|
35
|
+
#pending = Buffer.alloc(0);
|
|
36
|
+
#tempFile = null;
|
|
37
|
+
#queue = [];
|
|
38
|
+
#emitTimer = null;
|
|
39
|
+
#nextEmitAtMs = 0;
|
|
40
|
+
#warmupUntilMs = 0;
|
|
41
|
+
#starting = false; // async binary resolution in flight
|
|
42
|
+
#started = false; // start() called, not yet stop()ed (survives decoder death)
|
|
43
|
+
#generation = 0; // bumped on stop() to cancel a pending spawn
|
|
44
|
+
|
|
45
|
+
constructor(sampleRate, channels, framesPerChunk, onChunk, source = 'silence') {
|
|
46
|
+
this.sampleRate = sampleRate;
|
|
47
|
+
this.channels = channels;
|
|
48
|
+
this.framesPerChunk = framesPerChunk;
|
|
49
|
+
this.onChunk = onChunk;
|
|
50
|
+
this.source = source;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
start = () => {
|
|
54
|
+
if (this.#proc || this.#starting) return;
|
|
55
|
+
this.#started = true;
|
|
56
|
+
this.#starting = true;
|
|
57
|
+
const generation = ++this.#generation;
|
|
58
|
+
// resolve the ffmpeg binary (ffmpeg-static / installer / PATH / env), then spawn
|
|
59
|
+
void import('../Utils/ffmpeg-path.js')
|
|
60
|
+
.then(({ resolveFfmpegPath }) => resolveFfmpegPath())
|
|
61
|
+
.then((bin) => {
|
|
62
|
+
this.#starting = false;
|
|
63
|
+
if (generation === this.#generation) this.#spawnDecoder(bin ?? 'ffmpeg');
|
|
64
|
+
})
|
|
65
|
+
.catch(() => {
|
|
66
|
+
this.#starting = false;
|
|
67
|
+
if (generation === this.#generation) this.#spawnDecoder('ffmpeg');
|
|
68
|
+
});
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Swap the audio source mid-stream (greeting → menu → hold music). When
|
|
73
|
+
* running, restarts the decoder with the new source and returns true; when
|
|
74
|
+
* not started, just stores it for the next start() and returns false.
|
|
75
|
+
*/
|
|
76
|
+
swapSource = (source) => {
|
|
77
|
+
const wasRunning = this.#started;
|
|
78
|
+
this.source = source;
|
|
79
|
+
if (!wasRunning) return false;
|
|
80
|
+
this.stop();
|
|
81
|
+
this.start();
|
|
82
|
+
return true;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
stop = () => {
|
|
86
|
+
this.#started = false;
|
|
87
|
+
this.#generation += 1; // cancel a spawn still resolving its binary
|
|
88
|
+
this.#starting = false;
|
|
89
|
+
if (this.#emitTimer) {
|
|
90
|
+
clearTimeout(this.#emitTimer);
|
|
91
|
+
this.#emitTimer = null;
|
|
92
|
+
}
|
|
93
|
+
this.#proc?.kill('SIGTERM');
|
|
94
|
+
this.#proc = null;
|
|
95
|
+
this.#pending = Buffer.alloc(0);
|
|
96
|
+
this.#queue = [];
|
|
97
|
+
this.#warmupUntilMs = 0;
|
|
98
|
+
this.#cleanupTempFile();
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
#cleanupTempFile = () => {
|
|
102
|
+
if (!this.#tempFile) return;
|
|
103
|
+
try { unlinkSync(this.#tempFile); } catch { /* best effort */ }
|
|
104
|
+
this.#tempFile = null;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
/** Build the ffmpeg input args for the current source. */
|
|
108
|
+
#inputArgs = () => {
|
|
109
|
+
if (!this.source || this.source === 'silence') {
|
|
110
|
+
return ['-f', 'lavfi', '-i', `aevalsrc=0:d=3600:s=${this.sampleRate}`];
|
|
111
|
+
}
|
|
112
|
+
if (this.source && typeof this.source === 'object' && Buffer.isBuffer(this.source.data)) {
|
|
113
|
+
const ext = String(this.source.ext || 'ogg').replace(/[^a-z0-9]/gi, '') || 'ogg';
|
|
114
|
+
const tmp = join(tmpdir(), `jap-voip-${Date.now()}-${randomBytes(4).toString('hex')}.${ext}`);
|
|
115
|
+
writeFileSync(tmp, this.source.data);
|
|
116
|
+
this.#tempFile = tmp;
|
|
117
|
+
return ['-i', tmp];
|
|
118
|
+
}
|
|
119
|
+
if (typeof this.source === 'string' && this.source.startsWith('lavfi:')) {
|
|
120
|
+
return ['-f', 'lavfi', '-i', this.source.slice('lavfi:'.length)];
|
|
121
|
+
}
|
|
122
|
+
return ['-i', this.source];
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
#spawnDecoder = (ffmpegBin) => {
|
|
126
|
+
if (this.#proc) return;
|
|
127
|
+
const chunkSamples = this.framesPerChunk * this.channels;
|
|
128
|
+
const chunkBytes = chunkSamples * Float32Array.BYTES_PER_ELEMENT;
|
|
129
|
+
const chunkIntervalMs = (this.framesPerChunk / this.sampleRate) * 1000;
|
|
130
|
+
|
|
131
|
+
this.#proc = spawn(ffmpegBin, [
|
|
132
|
+
'-hide_banner',
|
|
133
|
+
'-loglevel', 'error',
|
|
134
|
+
'-thread_queue_size', '512',
|
|
135
|
+
...this.#inputArgs(),
|
|
136
|
+
'-f', 'f32le',
|
|
137
|
+
'-ac', String(this.channels),
|
|
138
|
+
'-ar', String(this.sampleRate),
|
|
139
|
+
'pipe:1'
|
|
140
|
+
]);
|
|
141
|
+
|
|
142
|
+
this.#proc.stdout.on('data', (chunk) => this.#ingest(chunk, chunkBytes, chunkSamples));
|
|
143
|
+
this.#proc.on('error', (err) => {
|
|
144
|
+
if (err?.code === 'ENOENT') {
|
|
145
|
+
process.stderr.write('[AudioFeeder] ffmpeg binary not found — install ffmpeg to feed call audio (or use audioSource: "silence").\n');
|
|
146
|
+
}
|
|
147
|
+
this.#proc = null;
|
|
148
|
+
});
|
|
149
|
+
this.#proc.stderr.on('data', (chunk) => process.stderr.write(`[AudioFeeder] ${chunk.toString().trim()}\n`));
|
|
150
|
+
this.#proc.on('exit', (code) => {
|
|
151
|
+
if (code !== 0 && code !== null) process.stderr.write(`[AudioFeeder] ffmpeg exited with code=${code}\n`);
|
|
152
|
+
this.#proc = null;
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
this.#nextEmitAtMs = 0;
|
|
156
|
+
this.#warmupUntilMs = Date.now() + WARMUP_MS;
|
|
157
|
+
this.#scheduleNext(chunkSamples, chunkIntervalMs);
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
/** Slice raw ffmpeg output into fixed frames, applying back-pressure. */
|
|
161
|
+
#ingest = (chunk, chunkBytes, chunkSamples) => {
|
|
162
|
+
this.#pending = Buffer.concat([this.#pending, chunk]);
|
|
163
|
+
while (this.#pending.length >= chunkBytes) {
|
|
164
|
+
if (this.#queue.length >= MAX_QUEUED_CHUNKS) {
|
|
165
|
+
this.#proc?.stdout.pause();
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
const frame = this.#pending.subarray(0, chunkBytes);
|
|
169
|
+
this.#pending = this.#pending.subarray(chunkBytes);
|
|
170
|
+
const out = new Float32Array(chunkSamples);
|
|
171
|
+
out.set(new Float32Array(frame.buffer, frame.byteOffset, chunkSamples));
|
|
172
|
+
this.bytesProduced += chunkBytes;
|
|
173
|
+
this.#queue.push(out);
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
#scheduleNext = (chunkSamples, chunkIntervalMs) => {
|
|
178
|
+
if (!this.#proc) return;
|
|
179
|
+
const now = Date.now();
|
|
180
|
+
if (this.#nextEmitAtMs === 0) this.#nextEmitAtMs = now;
|
|
181
|
+
const delayMs = Math.max(0, this.#nextEmitAtMs - now);
|
|
182
|
+
this.#emitTimer = setTimeout(() => {
|
|
183
|
+
this.#emitTimer = null;
|
|
184
|
+
// during warm-up, wait for the queue to fill before metering out
|
|
185
|
+
if (this.#queue.length < LOW_WATERMARK_CHUNKS && Date.now() < this.#warmupUntilMs) {
|
|
186
|
+
this.#nextEmitAtMs = Date.now() + 10;
|
|
187
|
+
this.#scheduleNext(chunkSamples, chunkIntervalMs);
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
this.#flushOne(chunkSamples);
|
|
191
|
+
this.#nextEmitAtMs += chunkIntervalMs;
|
|
192
|
+
this.#scheduleNext(chunkSamples, chunkIntervalMs);
|
|
193
|
+
}, delayMs);
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
#flushOne = (chunkSamples) => {
|
|
197
|
+
let next = this.#queue.shift();
|
|
198
|
+
if (!next) {
|
|
199
|
+
next = new Float32Array(chunkSamples); // underflow → silence
|
|
200
|
+
this.underflowChunks += 1;
|
|
201
|
+
}
|
|
202
|
+
this.chunksEmitted += 1;
|
|
203
|
+
this.onChunk(next);
|
|
204
|
+
if (this.#proc?.stdout.isPaused() && this.#queue.length <= MAX_QUEUED_CHUNKS / 4) {
|
|
205
|
+
this.#proc.stdout.resume();
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JAP@Add --- Type declarations for the VoIP WAV recorder.
|
|
3
|
+
*/
|
|
4
|
+
export interface WavRecorderOptions {
|
|
5
|
+
sampleRate?: number;
|
|
6
|
+
channels?: number;
|
|
7
|
+
}
|
|
8
|
+
export interface WavRecorder {
|
|
9
|
+
write(pcm: Float32Array): void;
|
|
10
|
+
close(): void;
|
|
11
|
+
readonly bytesWritten: number;
|
|
12
|
+
}
|
|
13
|
+
export declare const createWavRecorder: (filePath: string, opts?: WavRecorderOptions) => WavRecorder;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* lib/VoIP/call-recorder.js
|
|
3
|
+
* Author: J.AP (@japofc/baileys)
|
|
4
|
+
*
|
|
5
|
+
* Minimal, dependency-free WAV recorder. Collects the Float32 PCM chunks
|
|
6
|
+
* emitted by an ActiveCall 'audio' event and writes a canonical 16-bit PCM
|
|
7
|
+
* .wav. The WASM engine doesn't report the playback rate, so 16 kHz mono is the
|
|
8
|
+
* default (the rate the capture path was validated at); pass `sampleRate`
|
|
9
|
+
* explicitly if recordings sound pitched.
|
|
10
|
+
*/
|
|
11
|
+
import { openSync, writeSync, closeSync } from 'node:fs';
|
|
12
|
+
|
|
13
|
+
/** Convert a float sample in [-1, 1] to a signed 16-bit integer. */
|
|
14
|
+
const toInt16 = (sample) => {
|
|
15
|
+
const s = Math.max(-1, Math.min(1, sample));
|
|
16
|
+
return Math.round(s * (s < 0 ? 0x8000 : 0x7fff));
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/** Build the 44-byte canonical WAV header (sizes patched on close). */
|
|
20
|
+
const buildHeader = (sampleRate, channels) => {
|
|
21
|
+
const h = Buffer.alloc(44);
|
|
22
|
+
h.write('RIFF', 0);
|
|
23
|
+
h.write('WAVE', 8);
|
|
24
|
+
h.write('fmt ', 12);
|
|
25
|
+
h.writeUInt32LE(16, 16); // fmt chunk size
|
|
26
|
+
h.writeUInt16LE(1, 20); // PCM
|
|
27
|
+
h.writeUInt16LE(channels, 22);
|
|
28
|
+
h.writeUInt32LE(sampleRate, 24);
|
|
29
|
+
h.writeUInt32LE(sampleRate * channels * 2, 28); // byte rate
|
|
30
|
+
h.writeUInt16LE(channels * 2, 32); // block align
|
|
31
|
+
h.writeUInt16LE(16, 34); // bits per sample
|
|
32
|
+
h.write('data', 36);
|
|
33
|
+
return h;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export const createWavRecorder = (filePath, { sampleRate = 16000, channels = 1 } = {}) => {
|
|
37
|
+
if (!filePath || typeof filePath !== 'string') {
|
|
38
|
+
throw new TypeError('createWavRecorder(filePath) requires a file path string');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const fd = openSync(filePath, 'w');
|
|
42
|
+
writeSync(fd, buildHeader(sampleRate, channels));
|
|
43
|
+
|
|
44
|
+
let dataBytes = 0;
|
|
45
|
+
let closed = false;
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
/** Append one Float32Array PCM chunk (interleaved when multi-channel). */
|
|
49
|
+
write(pcm) {
|
|
50
|
+
if (closed) throw new Error('WAV recorder is already closed');
|
|
51
|
+
if (!pcm?.length) return;
|
|
52
|
+
const out = Buffer.allocUnsafe(pcm.length * 2);
|
|
53
|
+
for (let i = 0; i < pcm.length; i++) out.writeInt16LE(toInt16(pcm[i]), i * 2);
|
|
54
|
+
writeSync(fd, out);
|
|
55
|
+
dataBytes += out.length;
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
/** Patch the RIFF/data sizes and close. Idempotent. */
|
|
59
|
+
close() {
|
|
60
|
+
if (closed) return;
|
|
61
|
+
closed = true;
|
|
62
|
+
try {
|
|
63
|
+
const sizes = Buffer.alloc(8);
|
|
64
|
+
sizes.writeUInt32LE(36 + dataBytes, 0); // RIFF chunk size
|
|
65
|
+
sizes.writeUInt32LE(dataBytes, 4); // data chunk size
|
|
66
|
+
writeSync(fd, sizes, 0, 4, 4); // patch RIFF size @ offset 4
|
|
67
|
+
writeSync(fd, sizes, 4, 4, 40); // patch data size @ offset 40
|
|
68
|
+
} finally {
|
|
69
|
+
closeSync(fd);
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
get bytesWritten() {
|
|
74
|
+
return dataBytes;
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
};
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
export { CallState } from './types.js';
|
|
3
|
+
export { createWavRecorder } from './call-recorder.js';
|
|
4
|
+
export type { WavRecorder, WavRecorderOptions } from './call-recorder.js';
|
|
5
|
+
|
|
6
|
+
export interface VoipClientConfig {
|
|
7
|
+
/** Pre-built WASM engine (tests + custom stacks). */
|
|
8
|
+
engine?: any;
|
|
9
|
+
/** Pre-built relay transport. */
|
|
10
|
+
relay?: any;
|
|
11
|
+
/** Path to the VOIP wasm resources directory. */
|
|
12
|
+
resourcesPath?: string;
|
|
13
|
+
/** Relay-health watchdog poll interval in ms (default 5000; 0 disables). */
|
|
14
|
+
watchdogIntervalMs?: number;
|
|
15
|
+
/** Consecutive empty-relay polls before recovery kicks in (default 3). */
|
|
16
|
+
watchdogMaxSilent?: number;
|
|
17
|
+
/**
|
|
18
|
+
* Recovery budget per call (default 3): after this many consecutive failed
|
|
19
|
+
* recoveries the client emits 'call-unrecoverable' and force-ends the call
|
|
20
|
+
* instead of retrying forever.
|
|
21
|
+
*/
|
|
22
|
+
watchdogMaxRecoveries?: number;
|
|
23
|
+
[key: string]: any;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface AnswerCallOptions {
|
|
27
|
+
audioSource?: string;
|
|
28
|
+
durationMs?: number;
|
|
29
|
+
isMicEnabled?: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface JoinGroupCallOptions {
|
|
33
|
+
audioSource?: string;
|
|
34
|
+
durationMs?: number;
|
|
35
|
+
hasVideo?: boolean;
|
|
36
|
+
joinAndAccept?: boolean;
|
|
37
|
+
chatName?: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface StartGroupCallOptions {
|
|
41
|
+
audioSource?: string;
|
|
42
|
+
durationMs?: number;
|
|
43
|
+
useVideo?: boolean;
|
|
44
|
+
chatName?: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface RecoverCallOptions {
|
|
48
|
+
peerJid?: string;
|
|
49
|
+
callId?: string;
|
|
50
|
+
retryCount?: number;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** A live (or recently ended) voice/video call. Emits 'audio' with PCM chunks. */
|
|
54
|
+
export class ActiveCall extends EventEmitter {
|
|
55
|
+
constructor(callId: string, engine: any, durationMs: number, meta?: Record<string, any>);
|
|
56
|
+
readonly state: any;
|
|
57
|
+
startedAt: number;
|
|
58
|
+
end(): any;
|
|
59
|
+
mute(muted: boolean): any;
|
|
60
|
+
invite(pnUserJid: string, lidUserJid?: string, deviceJids?: string[]): any;
|
|
61
|
+
removeParticipant(peerJid: string): any;
|
|
62
|
+
requestMute(peerJid: string): any;
|
|
63
|
+
react(reaction: string): any;
|
|
64
|
+
setHandRaised(raised: boolean): any;
|
|
65
|
+
getDurationMs(): number;
|
|
66
|
+
recordToFile(filePath: string, opts?: Record<string, any>): any;
|
|
67
|
+
/**
|
|
68
|
+
* Swap what the call is playing WITHOUT hanging up: a file path/URL,
|
|
69
|
+
* `{ data: Buffer, ext }`, a `lavfi:` graph, or `"silence"`. Returns true
|
|
70
|
+
* when a live decoder was swapped, false when audio capture has not
|
|
71
|
+
* started yet (the source is remembered for when it does).
|
|
72
|
+
*/
|
|
73
|
+
setAudioSource(source: string | { data: Buffer; ext?: string }): boolean;
|
|
74
|
+
waitForEnd(): Promise<any>;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Audio-only WhatsApp voice-call client (WASM call stack + WebRTC relay).
|
|
79
|
+
* Emits 'incoming-call' / 'outgoing-call' / 'call-ended'.
|
|
80
|
+
*/
|
|
81
|
+
export class VoipClient extends EventEmitter {
|
|
82
|
+
constructor(config?: VoipClientConfig);
|
|
83
|
+
connectWithSocket(existingSock: any): Promise<void>;
|
|
84
|
+
isBusy(): boolean;
|
|
85
|
+
getActiveCall(): ActiveCall | null;
|
|
86
|
+
/** Swap the active call's uplink audio source mid-call. Throws when no call is active. */
|
|
87
|
+
setAudioSource(source: string | { data: Buffer; ext?: string }): boolean;
|
|
88
|
+
getPendingCalls(): any[];
|
|
89
|
+
rejectCall(callId: string, callFrom: string, options?: { text?: string }): Promise<any>;
|
|
90
|
+
answerCall(callId: string, options?: AnswerCallOptions): Promise<any>;
|
|
91
|
+
joinGroupCall(callId: string, options?: JoinGroupCallOptions): Promise<any>;
|
|
92
|
+
startGroupCall(groupJid: string, participants?: string[], options?: StartGroupCallOptions): Promise<any>;
|
|
93
|
+
rejoinGroupCall(overrides?: Record<string, any>): Promise<any>;
|
|
94
|
+
inviteToGroupCall(phoneNumber: string, deviceJids?: string[]): Promise<any>;
|
|
95
|
+
removeGroupParticipant(peerJid: string): Promise<any>;
|
|
96
|
+
previewCallLink(token: string, opts?: Record<string, any>): any;
|
|
97
|
+
joinCallLink(): any;
|
|
98
|
+
getStats(): any;
|
|
99
|
+
recoverCall(options?: RecoverCallOptions): Promise<any>;
|
|
100
|
+
call(phoneNumber: string, opts?: Record<string, any>): Promise<any>;
|
|
101
|
+
disconnect(): any;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Instantiate VoipClient on an active socket (also sets `sock.voip`). */
|
|
105
|
+
export declare const attachVoip: (sock: any, config?: VoipClientConfig) => Promise<VoipClient>;
|