@basmilius/apple-raop 0.6.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-present Bas Milius
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.
@@ -0,0 +1,8 @@
1
+ import { type MediaMetadata } from "./types";
2
+ export declare const MAX_PACKETS_COMPENSATE = 3;
3
+ export declare const PACKET_BACKLOG_SIZE = 1e3;
4
+ export declare const SLOW_WARNING_THRESHOLD = 5;
5
+ export declare const FRAMES_PER_PACKET = 352;
6
+ export declare const MISSING_METADATA: MediaMetadata;
7
+ export declare const EMPTY_METADATA: MediaMetadata;
8
+ export declare const SUPPORTED_ENCRYPTIONS: unknown;
@@ -0,0 +1,12 @@
1
+ import { EventEmitter } from "node:events";
2
+ import { PacketFifo } from "./packets";
3
+ import type { StreamContext } from "./types";
4
+ export default class ControlClient extends EventEmitter {
5
+ #private;
6
+ constructor(context: StreamContext, packetBacklog: PacketFifo);
7
+ get port(): number;
8
+ bind(localIp: string, port: number): Promise<void>;
9
+ close(): void;
10
+ start(remoteAddr: string): void;
11
+ stop(): void;
12
+ }
@@ -0,0 +1,8 @@
1
+ export * from "./types";
2
+ export * from "./packets";
3
+ export * from "./utils";
4
+ export { default as ControlClient } from "./controlClient";
5
+ export { default as RtspClient } from "./rtspClient";
6
+ export { default as Statistics } from "./statistics";
7
+ export { default as StreamClient } from "./streamClient";
8
+ export { RaopClient, type StreamOptions } from "./raop";
package/dist/index.js ADDED
@@ -0,0 +1,1034 @@
1
+ // src/types.ts
2
+ var EncryptionType;
3
+ ((EncryptionType2) => {
4
+ EncryptionType2[EncryptionType2["Unknown"] = 0] = "Unknown";
5
+ EncryptionType2[EncryptionType2["Unencrypted"] = 1] = "Unencrypted";
6
+ EncryptionType2[EncryptionType2["MFiSAP"] = 2] = "MFiSAP";
7
+ })(EncryptionType ||= {});
8
+ var MetadataType;
9
+ ((MetadataType2) => {
10
+ MetadataType2[MetadataType2["NotSupported"] = 0] = "NotSupported";
11
+ MetadataType2[MetadataType2["Text"] = 1] = "Text";
12
+ MetadataType2[MetadataType2["Artwork"] = 2] = "Artwork";
13
+ MetadataType2[MetadataType2["Progress"] = 4] = "Progress";
14
+ })(MetadataType ||= {});
15
+ // src/packets.ts
16
+ class PacketFifo {
17
+ #maxSize;
18
+ #packets = new Map;
19
+ #order = [];
20
+ constructor(maxSize) {
21
+ this.#maxSize = maxSize;
22
+ }
23
+ get(seqno) {
24
+ return this.#packets.get(seqno);
25
+ }
26
+ set(seqno, packet) {
27
+ if (this.#packets.has(seqno)) {
28
+ return;
29
+ }
30
+ this.#packets.set(seqno, packet);
31
+ this.#order.push(seqno);
32
+ while (this.#order.length > this.#maxSize) {
33
+ const oldest = this.#order.shift();
34
+ if (oldest !== undefined) {
35
+ this.#packets.delete(oldest);
36
+ }
37
+ }
38
+ }
39
+ has(seqno) {
40
+ return this.#packets.has(seqno);
41
+ }
42
+ clear() {
43
+ this.#packets.clear();
44
+ this.#order.length = 0;
45
+ }
46
+ }
47
+ var AudioPacketHeader = {
48
+ encode(header, payloadType, seqno, timestamp, ssrc) {
49
+ const packet = Buffer.alloc(12);
50
+ packet.writeUInt8(header, 0);
51
+ packet.writeUInt8(payloadType, 1);
52
+ packet.writeUInt16BE(seqno, 2);
53
+ packet.writeUInt32BE(timestamp, 4);
54
+ packet.writeUInt32BE(ssrc, 8);
55
+ return packet;
56
+ }
57
+ };
58
+ var SyncPacket = {
59
+ encode(header, payloadType, seqno, rtpTimestamp, ntpSec, ntpFrac, rtpTimestampNow) {
60
+ const packet = Buffer.alloc(20);
61
+ packet.writeUInt8(header, 0);
62
+ packet.writeUInt8(payloadType, 1);
63
+ packet.writeUInt16BE(seqno, 2);
64
+ packet.writeUInt32BE(rtpTimestamp, 4);
65
+ packet.writeUInt32BE(ntpSec, 8);
66
+ packet.writeUInt32BE(ntpFrac, 12);
67
+ packet.writeUInt32BE(rtpTimestampNow, 16);
68
+ return packet;
69
+ }
70
+ };
71
+ function decodeRetransmitRequest(data) {
72
+ return {
73
+ lostSeqno: data.readUInt16BE(4),
74
+ lostPackets: data.readUInt16BE(6)
75
+ };
76
+ }
77
+ // src/utils.ts
78
+ function pctToDbfs(volume) {
79
+ if (volume <= 0)
80
+ return -144;
81
+ if (volume >= 100)
82
+ return 0;
83
+ return 20 * Math.log10(volume / 100);
84
+ }
85
+ function getEncryptionTypes(properties) {
86
+ const et = properties.get("et");
87
+ if (!et)
88
+ return 0 /* Unknown */;
89
+ let types = 0 /* Unknown */;
90
+ for (const t of et.split(",")) {
91
+ const num = parseInt(t.trim(), 10);
92
+ if (num === 0)
93
+ types |= 1 /* Unencrypted */;
94
+ if (num === 1)
95
+ types |= 2 /* MFiSAP */;
96
+ }
97
+ return types;
98
+ }
99
+ function getMetadataTypes(properties) {
100
+ const md = properties.get("md");
101
+ if (!md)
102
+ return 0 /* NotSupported */;
103
+ let types = 0 /* NotSupported */;
104
+ for (const t of md.split(",")) {
105
+ const num = parseInt(t.trim(), 10);
106
+ if (num === 0)
107
+ types |= 1 /* Text */;
108
+ if (num === 1)
109
+ types |= 2 /* Artwork */;
110
+ if (num === 2)
111
+ types |= 4 /* Progress */;
112
+ }
113
+ return types;
114
+ }
115
+ function getAudioProperties(properties) {
116
+ const sr = parseInt(properties.get("sr") ?? "44100", 10);
117
+ const ch = parseInt(properties.get("ch") ?? "2", 10);
118
+ const ss = parseInt(properties.get("ss") ?? "16", 10);
119
+ return [sr, ch, ss / 8];
120
+ }
121
+ // src/controlClient.ts
122
+ import { createSocket } from "node:dgram";
123
+ import { EventEmitter } from "node:events";
124
+ import { NTP } from "@basmilius/apple-encoding";
125
+ function ntpFromTs(timestamp, sampleRate) {
126
+ const seconds = Math.floor(timestamp / sampleRate);
127
+ const fraction = timestamp % sampleRate * 4294967295 / sampleRate;
128
+ return BigInt(seconds) << 32n | BigInt(Math.floor(fraction));
129
+ }
130
+
131
+ class ControlClient extends EventEmitter {
132
+ #transport;
133
+ #context;
134
+ #packetBacklog;
135
+ #syncTask;
136
+ #abortController;
137
+ #localPort;
138
+ constructor(context, packetBacklog) {
139
+ super();
140
+ this.#context = context;
141
+ this.#packetBacklog = packetBacklog;
142
+ }
143
+ get port() {
144
+ return this.#localPort ?? 0;
145
+ }
146
+ async bind(localIp, port) {
147
+ return new Promise((resolve, reject) => {
148
+ this.#transport = createSocket("udp4");
149
+ this.#transport.on("error", (err) => {
150
+ console.error("Control connection error:", err);
151
+ reject(err);
152
+ });
153
+ this.#transport.on("message", (data, rinfo) => {
154
+ this.#onMessage(data, rinfo);
155
+ });
156
+ this.#transport.on("listening", () => {
157
+ const address = this.#transport.address();
158
+ this.#localPort = address.port;
159
+ resolve();
160
+ });
161
+ this.#transport.bind(port, localIp);
162
+ });
163
+ }
164
+ close() {
165
+ this.stop();
166
+ if (this.#transport) {
167
+ this.#transport.close();
168
+ this.#transport = undefined;
169
+ }
170
+ }
171
+ start(remoteAddr) {
172
+ if (this.#syncTask) {
173
+ throw new Error("Already running");
174
+ }
175
+ this.#abortController = new AbortController;
176
+ this.#startSyncTask(remoteAddr, this.#context.controlPort);
177
+ }
178
+ stop() {
179
+ if (this.#abortController) {
180
+ this.#abortController.abort();
181
+ this.#abortController = undefined;
182
+ }
183
+ if (this.#syncTask) {
184
+ clearInterval(this.#syncTask);
185
+ this.#syncTask = undefined;
186
+ }
187
+ }
188
+ #startSyncTask(addr, port) {
189
+ let firstPacket = true;
190
+ const sendSync = () => {
191
+ if (!this.#transport)
192
+ return;
193
+ const currentTime = ntpFromTs(this.#context.headTs, this.#context.sampleRate);
194
+ const [currentSec, currentFrac] = NTP.parts(currentTime);
195
+ const packet = SyncPacket.encode(firstPacket ? 144 : 128, 212, 7, this.#context.headTs - this.#context.latency, currentSec, currentFrac, this.#context.headTs);
196
+ firstPacket = false;
197
+ this.#transport.send(packet, port, addr);
198
+ };
199
+ sendSync();
200
+ this.#syncTask = setInterval(sendSync, 1000);
201
+ }
202
+ #onMessage(data, rinfo) {
203
+ const actualType = data[1] & 127;
204
+ if (actualType === 85) {
205
+ this.#retransmitLostPackets(decodeRetransmitRequest(data), rinfo);
206
+ } else {
207
+ console.debug("Received unhandled control data from", rinfo, data);
208
+ }
209
+ }
210
+ #retransmitLostPackets(request, addr) {
211
+ for (let i = 0;i < request.lostPackets; i++) {
212
+ const seqno = request.lostSeqno + i;
213
+ if (this.#packetBacklog.has(seqno)) {
214
+ const packet = this.#packetBacklog.get(seqno);
215
+ const originalSeqno = packet.subarray(2, 4);
216
+ const resp = Buffer.concat([Buffer.from([128, 214]), originalSeqno, packet]);
217
+ if (this.#transport) {
218
+ this.#transport.send(resp, addr.port, addr.address);
219
+ }
220
+ } else {
221
+ console.debug(`Packet ${seqno} not in backlog`);
222
+ }
223
+ }
224
+ }
225
+ }
226
+ // src/rtspClient.ts
227
+ import { createHash } from "node:crypto";
228
+ import { generateActiveRemoteId, generateDacpId, generateSessionId } from "@basmilius/apple-airplay";
229
+ import { Connection, HTTP_TIMEOUT } from "@basmilius/apple-common";
230
+ import { DAAP, Plist, RTSP } from "@basmilius/apple-encoding";
231
+ var USER_AGENT = "AirPlay/550.10";
232
+ var FRAMES_PER_PACKET = 352;
233
+ var AUTH_SETUP_UNENCRYPTED = Buffer.from([1]);
234
+ var CURVE25519_PUB_KEY = Buffer.from([
235
+ 89,
236
+ 2,
237
+ 237,
238
+ 233,
239
+ 13,
240
+ 78,
241
+ 242,
242
+ 189,
243
+ 76,
244
+ 182,
245
+ 138,
246
+ 99,
247
+ 48,
248
+ 3,
249
+ 130,
250
+ 7,
251
+ 169,
252
+ 77,
253
+ 189,
254
+ 80,
255
+ 216,
256
+ 170,
257
+ 70,
258
+ 91,
259
+ 93,
260
+ 140,
261
+ 1,
262
+ 42,
263
+ 12,
264
+ 126,
265
+ 29,
266
+ 78
267
+ ]);
268
+ function getDigestPayload(method, uri, info) {
269
+ const ha1 = createHash("md5").update(`${info.username}:${info.realm}:${info.password}`).digest("hex");
270
+ const ha2 = createHash("md5").update(`${method}:${uri}`).digest("hex");
271
+ const response = createHash("md5").update(`${ha1}:${info.nonce}:${ha2}`).digest("hex");
272
+ return `Digest username="${info.username}", realm="${info.realm}", nonce="${info.nonce}", uri="${uri}", response="${response}"`;
273
+ }
274
+ function generateRandomSessionId() {
275
+ return Math.floor(Math.random() * 4294967295);
276
+ }
277
+ function buildAnnouncePayload(options) {
278
+ return [
279
+ "v=0",
280
+ `o=iTunes ${options.sessionId} 0 IN IP4 ${options.localIp}`,
281
+ "s=iTunes",
282
+ `c=IN IP4 ${options.remoteIp}`,
283
+ "t=0 0",
284
+ "m=audio 0 RTP/AVP 96",
285
+ `a=rtpmap:96 L16/${options.sampleRate}/${options.channels}`,
286
+ `a=fmtp:96 ${FRAMES_PER_PACKET} 0 ${options.bitsPerChannel} 40 10 14 ${options.channels} 255 0 0 ${options.sampleRate}`
287
+ ].join(`\r
288
+ `) + `\r
289
+ `;
290
+ }
291
+
292
+ class RtspClient extends Connection {
293
+ get activeRemoteId() {
294
+ return this.#activeRemoteId;
295
+ }
296
+ get dacpId() {
297
+ return this.#dacpId;
298
+ }
299
+ get rtspSessionId() {
300
+ return this.#rtspSessionId;
301
+ }
302
+ get sessionId() {
303
+ return this.#sessionId;
304
+ }
305
+ get uri() {
306
+ return `rtsp://${this.connection.localIp}/${this.#sessionId}`;
307
+ }
308
+ get connection() {
309
+ return {
310
+ localIp: this.#localIp,
311
+ remoteIp: this.address
312
+ };
313
+ }
314
+ #activeRemoteId;
315
+ #dacpId;
316
+ #rtspSessionId;
317
+ #sessionId;
318
+ #localIp = "0.0.0.0";
319
+ #buffer = Buffer.alloc(0);
320
+ #cseq = 0;
321
+ #digestInfo;
322
+ #requests = new Map;
323
+ constructor(context, address, port) {
324
+ super(context, address, port);
325
+ this.#activeRemoteId = generateActiveRemoteId();
326
+ this.#dacpId = generateDacpId();
327
+ this.#rtspSessionId = generateSessionId();
328
+ this.#sessionId = generateRandomSessionId();
329
+ this.on("close", this.#onClose.bind(this));
330
+ this.on("data", this.#onData.bind(this));
331
+ this.on("error", this.#onError.bind(this));
332
+ this.on("timeout", this.#onTimeout.bind(this));
333
+ this.on("connect", this.#onConnect.bind(this));
334
+ }
335
+ async info() {
336
+ try {
337
+ const response = await this.#exchange("GET", "/info", {
338
+ allowError: true
339
+ });
340
+ if (response.ok) {
341
+ const buffer = Buffer.from(await response.arrayBuffer());
342
+ if (buffer.length > 0) {
343
+ try {
344
+ return Plist.parse(buffer.buffer);
345
+ } catch {
346
+ return {};
347
+ }
348
+ }
349
+ }
350
+ return {};
351
+ } catch {
352
+ return {};
353
+ }
354
+ }
355
+ async authSetup() {
356
+ const body = Buffer.concat([AUTH_SETUP_UNENCRYPTED, CURVE25519_PUB_KEY]);
357
+ await this.#exchange("POST", "/auth-setup", {
358
+ contentType: "application/octet-stream",
359
+ body,
360
+ protocol: "HTTP/1.1"
361
+ });
362
+ }
363
+ async announce(bytesPerChannel, channels, sampleRate, password) {
364
+ const body = buildAnnouncePayload({
365
+ sessionId: this.#sessionId,
366
+ localIp: this.connection.localIp,
367
+ remoteIp: this.connection.remoteIp,
368
+ bitsPerChannel: 8 * bytesPerChannel,
369
+ channels,
370
+ sampleRate
371
+ });
372
+ let response = await this.#exchange("ANNOUNCE", undefined, {
373
+ contentType: "application/sdp",
374
+ body,
375
+ allowError: !!password
376
+ });
377
+ if (response.status === 401 && password) {
378
+ const wwwAuthenticate = response.headers.get("www-authenticate");
379
+ if (wwwAuthenticate) {
380
+ const parts = wwwAuthenticate.split('"');
381
+ if (parts.length >= 5) {
382
+ this.#digestInfo = {
383
+ username: "pyatv",
384
+ realm: parts[1],
385
+ password,
386
+ nonce: parts[3]
387
+ };
388
+ response = await this.#exchange("ANNOUNCE", undefined, {
389
+ contentType: "application/sdp",
390
+ body
391
+ });
392
+ }
393
+ }
394
+ }
395
+ return response;
396
+ }
397
+ async setup(headers, body) {
398
+ return await this.#exchange("SETUP", undefined, { headers, body });
399
+ }
400
+ async record(headers) {
401
+ await this.#exchange("RECORD", undefined, { headers });
402
+ }
403
+ async flush(options) {
404
+ await this.#exchange("FLUSH", undefined, { headers: options.headers });
405
+ }
406
+ async setParameter(name, value) {
407
+ await this.#exchange("SET_PARAMETER", undefined, {
408
+ contentType: "text/parameters",
409
+ body: `${name}: ${value}`
410
+ });
411
+ }
412
+ async setMetadata(session, rtpseq, rtptime, metadata) {
413
+ const daapData = DAAP.encodeTrackMetadata({
414
+ title: metadata.title,
415
+ artist: metadata.artist,
416
+ album: metadata.album,
417
+ duration: metadata.duration
418
+ });
419
+ await this.#exchange("SET_PARAMETER", undefined, {
420
+ contentType: "application/x-dmap-tagged",
421
+ headers: {
422
+ Session: session,
423
+ "RTP-Info": `seq=${rtpseq};rtptime=${rtptime}`
424
+ },
425
+ body: daapData
426
+ });
427
+ }
428
+ async setArtwork(session, rtpseq, rtptime, artwork) {
429
+ let contentType = "image/jpeg";
430
+ if (artwork[0] === 137 && artwork[1] === 80) {
431
+ contentType = "image/png";
432
+ }
433
+ await this.#exchange("SET_PARAMETER", undefined, {
434
+ contentType,
435
+ headers: {
436
+ Session: session,
437
+ "RTP-Info": `seq=${rtpseq};rtptime=${rtptime}`
438
+ },
439
+ body: artwork
440
+ });
441
+ }
442
+ async feedback(allowError = false) {
443
+ return await this.#exchange("POST", "/feedback", { allowError });
444
+ }
445
+ async teardown(session) {
446
+ await this.#exchange("TEARDOWN", undefined, {
447
+ headers: { Session: session }
448
+ });
449
+ }
450
+ async#exchange(method, uri, options = {}) {
451
+ const {
452
+ contentType,
453
+ headers: extraHeaders = {},
454
+ allowError = false,
455
+ protocol = "RTSP/1.0",
456
+ timeout = HTTP_TIMEOUT
457
+ } = options;
458
+ let { body } = options;
459
+ const cseq = this.#cseq++;
460
+ const targetUri = uri ?? this.uri;
461
+ const headers = {
462
+ CSeq: cseq,
463
+ "DACP-ID": this.#dacpId,
464
+ "Active-Remote": this.#activeRemoteId,
465
+ "Client-Instance": this.#dacpId,
466
+ "User-Agent": USER_AGENT
467
+ };
468
+ if (this.#digestInfo) {
469
+ headers["Authorization"] = getDigestPayload(method, targetUri, this.#digestInfo);
470
+ }
471
+ Object.assign(headers, extraHeaders);
472
+ if (body && typeof body === "object" && !Buffer.isBuffer(body)) {
473
+ headers["Content-Type"] = "application/x-apple-binary-plist";
474
+ body = Buffer.from(Plist.serialize(body));
475
+ } else if (contentType) {
476
+ headers["Content-Type"] = contentType;
477
+ }
478
+ let bodyBuffer;
479
+ if (body) {
480
+ bodyBuffer = typeof body === "string" ? Buffer.from(body) : body;
481
+ headers["Content-Length"] = bodyBuffer.length;
482
+ } else {
483
+ headers["Content-Length"] = 0;
484
+ }
485
+ const headerLines = [
486
+ `${method} ${targetUri} ${protocol}`,
487
+ ...Object.entries(headers).map(([k, v]) => `${k}: ${v}`),
488
+ "",
489
+ ""
490
+ ].join(`\r
491
+ `);
492
+ const data = bodyBuffer ? Buffer.concat([Buffer.from(headerLines), bodyBuffer]) : Buffer.from(headerLines);
493
+ this.context.logger.net("[rtsp]", method, targetUri, `cseq=${cseq}`);
494
+ return new Promise((resolve, reject) => {
495
+ this.#requests.set(cseq, { resolve, reject });
496
+ const timer = setTimeout(() => {
497
+ this.#requests.delete(cseq);
498
+ reject(new Error(`No response to CSeq ${cseq} (${targetUri})`));
499
+ }, timeout);
500
+ this.write(data).catch((err) => {
501
+ clearTimeout(timer);
502
+ this.#requests.delete(cseq);
503
+ reject(err);
504
+ });
505
+ const originalResolve = resolve;
506
+ this.#requests.set(cseq, {
507
+ resolve: (response) => {
508
+ clearTimeout(timer);
509
+ if (!allowError && !response.ok) {
510
+ reject(new Error(`RTSP error: ${response.status} ${response.statusText}`));
511
+ } else {
512
+ originalResolve(response);
513
+ }
514
+ },
515
+ reject: (error) => {
516
+ clearTimeout(timer);
517
+ reject(error);
518
+ }
519
+ });
520
+ });
521
+ }
522
+ #onConnect() {
523
+ this.#localIp = "0.0.0.0";
524
+ }
525
+ #onClose() {
526
+ this.#buffer = Buffer.alloc(0);
527
+ for (const [cseq, { reject }] of this.#requests) {
528
+ reject(new Error("Connection closed"));
529
+ this.#requests.delete(cseq);
530
+ }
531
+ this.context.logger.net("[rtsp]", "#onClose()");
532
+ }
533
+ #onData(data) {
534
+ try {
535
+ this.#buffer = Buffer.concat([this.#buffer, data]);
536
+ while (this.#buffer.byteLength > 0) {
537
+ const result = RTSP.makeResponse(this.#buffer);
538
+ if (result === null) {
539
+ return;
540
+ }
541
+ this.#buffer = this.#buffer.subarray(result.responseLength);
542
+ const cseqHeader = result.response.headers.get("CSeq");
543
+ const cseq = cseqHeader ? parseInt(cseqHeader, 10) : -1;
544
+ if (this.#requests.has(cseq)) {
545
+ const { resolve } = this.#requests.get(cseq);
546
+ this.#requests.delete(cseq);
547
+ resolve(result.response);
548
+ } else {
549
+ this.context.logger.warn("[rtsp]", `Unexpected response for CSeq ${cseq}`);
550
+ }
551
+ }
552
+ } catch (err) {
553
+ this.context.logger.error("[rtsp]", "#onData()", err);
554
+ this.emit("error", err);
555
+ }
556
+ }
557
+ #onError(err) {
558
+ for (const [cseq, { reject }] of this.#requests) {
559
+ reject(err);
560
+ this.#requests.delete(cseq);
561
+ }
562
+ this.context.logger.error("[rtsp]", "#onError()", err);
563
+ }
564
+ #onTimeout() {
565
+ const err = new Error("Connection timed out");
566
+ for (const [cseq, { reject }] of this.#requests) {
567
+ reject(err);
568
+ this.#requests.delete(cseq);
569
+ }
570
+ this.context.logger.net("[rtsp]", "#onTimeout()");
571
+ }
572
+ }
573
+ // src/statistics.ts
574
+ class Statistics {
575
+ sampleRate;
576
+ startTimeNs;
577
+ intervalTime;
578
+ totalFrames = 0;
579
+ intervalFrames = 0;
580
+ constructor(sampleRate) {
581
+ this.sampleRate = sampleRate;
582
+ this.startTimeNs = process.hrtime.bigint();
583
+ this.intervalTime = performance.now();
584
+ }
585
+ get expectedFrameCount() {
586
+ const elapsedNs = Number(process.hrtime.bigint() - this.startTimeNs);
587
+ return Math.floor(elapsedNs / (1e9 / this.sampleRate));
588
+ }
589
+ get framesBehind() {
590
+ return this.expectedFrameCount - this.totalFrames;
591
+ }
592
+ get intervalCompleted() {
593
+ return this.intervalFrames >= this.sampleRate;
594
+ }
595
+ tick(sentFrames) {
596
+ this.totalFrames += sentFrames;
597
+ this.intervalFrames += sentFrames;
598
+ }
599
+ newInterval() {
600
+ const endTime = performance.now();
601
+ const diff = (endTime - this.intervalTime) / 1000;
602
+ this.intervalTime = endTime;
603
+ const frames = this.intervalFrames;
604
+ this.intervalFrames = 0;
605
+ return [diff, frames];
606
+ }
607
+ }
608
+ // src/streamClient.ts
609
+ import { createSocket as createSocket2 } from "node:dgram";
610
+ import { EventEmitter as EventEmitter2 } from "node:events";
611
+ import { waitFor } from "@basmilius/apple-common";
612
+
613
+ // src/const.ts
614
+ var MAX_PACKETS_COMPENSATE = 3;
615
+ var PACKET_BACKLOG_SIZE = 1000;
616
+ var SLOW_WARNING_THRESHOLD = 5;
617
+ var FRAMES_PER_PACKET2 = 352;
618
+ var MISSING_METADATA = {
619
+ title: "Streaming with apple-raop",
620
+ artist: "apple-raop",
621
+ album: "AirPlay",
622
+ duration: 0
623
+ };
624
+ var EMPTY_METADATA = {
625
+ title: "",
626
+ artist: "",
627
+ album: "",
628
+ duration: 0
629
+ };
630
+ var SUPPORTED_ENCRYPTIONS = 1 /* Unencrypted */ | 2 /* MFiSAP */;
631
+
632
+ // src/streamClient.ts
633
+ class StreamClient extends EventEmitter2 {
634
+ get info() {
635
+ return this.#info;
636
+ }
637
+ get playbackInfo() {
638
+ return {
639
+ metadata: this.#isMetadataEmpty(this.#metadata) ? MISSING_METADATA : this.#metadata,
640
+ position: this.#streamContext.position
641
+ };
642
+ }
643
+ get #requiresAuthSetup() {
644
+ const modelName = this.#properties.get("am") ?? "";
645
+ return (this.#encryptionTypes & 2 /* MFiSAP */) !== 0 && modelName.startsWith("AirPort");
646
+ }
647
+ #context;
648
+ #rtsp;
649
+ #streamContext;
650
+ #settings;
651
+ #protocol;
652
+ #packetBacklog;
653
+ #timingServer;
654
+ #controlClient;
655
+ #encryptionTypes = 0 /* Unknown */;
656
+ #metadataTypes = 0 /* NotSupported */;
657
+ #metadata = EMPTY_METADATA;
658
+ #info = {};
659
+ #properties = new Map;
660
+ #isPlaying = false;
661
+ constructor(context, rtsp, streamContext, protocol, settings, timingServer) {
662
+ super();
663
+ this.#context = context;
664
+ this.#rtsp = rtsp;
665
+ this.#streamContext = streamContext;
666
+ this.#protocol = protocol;
667
+ this.#settings = settings;
668
+ this.#packetBacklog = new PacketFifo(PACKET_BACKLOG_SIZE);
669
+ this.#timingServer = timingServer;
670
+ }
671
+ close() {
672
+ this.#protocol.teardown();
673
+ this.#controlClient?.close();
674
+ }
675
+ async initialize(properties) {
676
+ this.#properties = properties;
677
+ this.#encryptionTypes = getEncryptionTypes(properties);
678
+ this.#metadataTypes = getMetadataTypes(properties);
679
+ this.#context.logger.info(`Initializing RTSP with encryption=${this.#encryptionTypes}, metadata=${this.#metadataTypes}`);
680
+ const intersection = this.#encryptionTypes & SUPPORTED_ENCRYPTIONS;
681
+ if (!intersection || intersection === 0 /* Unknown */) {
682
+ this.#context.logger.debug("No supported encryption type, continuing anyway");
683
+ }
684
+ this.#updateOutputProperties(properties);
685
+ this.#controlClient = new ControlClient(this.#streamContext, this.#packetBacklog);
686
+ await this.#controlClient.bind(this.#rtsp.connection.localIp, this.#settings.protocols.raop.controlPort);
687
+ this.#context.logger.debug(`Local ports: control=${this.#controlClient.port}, timing=${this.#timingServer.port}`);
688
+ const info = await this.#rtsp.info();
689
+ Object.assign(this.#info, info);
690
+ this.#context.logger.debug("Updated info parameters to:", this.#info);
691
+ if (this.#requiresAuthSetup) {
692
+ await this.#rtsp.authSetup();
693
+ }
694
+ await this.#protocol.setup(this.#timingServer.port, this.#controlClient.port);
695
+ }
696
+ stop() {
697
+ this.#context.logger.debug("Stopping audio playback");
698
+ this.#isPlaying = false;
699
+ }
700
+ async setVolume(volume) {
701
+ await this.#rtsp.setParameter("volume", String(volume));
702
+ this.#streamContext.volume = volume;
703
+ }
704
+ async sendAudio(source, metadata = EMPTY_METADATA, volume) {
705
+ if (!this.#controlClient) {
706
+ throw new Error("Not initialized");
707
+ }
708
+ this.#streamContext.reset();
709
+ let transport;
710
+ try {
711
+ transport = createSocket2("udp4");
712
+ await new Promise((resolve) => {
713
+ transport.connect(this.#streamContext.serverPort, this.#rtsp.connection.remoteIp, resolve);
714
+ });
715
+ this.#controlClient.start(this.#rtsp.connection.remoteIp);
716
+ if ((this.#metadataTypes & 4 /* Progress */) !== 0) {
717
+ const start = this.#streamContext.rtptime;
718
+ const now = this.#streamContext.rtptime;
719
+ const end = start + source.duration * this.#streamContext.sampleRate;
720
+ await this.#rtsp.setParameter("progress", `${start}/${now}/${end}`);
721
+ }
722
+ this.#metadata = metadata;
723
+ if ((this.#metadataTypes & 1 /* Text */) !== 0) {
724
+ this.#context.logger.debug("Playing with metadata:", this.playbackInfo.metadata);
725
+ await this.#rtsp.setMetadata(this.#streamContext.rtspSession, this.#streamContext.rtpseq, this.#streamContext.rtptime, this.playbackInfo.metadata);
726
+ }
727
+ if ((this.#metadataTypes & 2 /* Artwork */) !== 0 && metadata.artwork) {
728
+ this.#context.logger.debug(`Sending ${metadata.artwork.length} bytes artwork`);
729
+ await this.#rtsp.setArtwork(this.#streamContext.rtspSession, this.#streamContext.rtpseq, this.#streamContext.rtptime, metadata.artwork);
730
+ }
731
+ await this.#protocol.startFeedback();
732
+ this.emit("playing", this.playbackInfo);
733
+ await this.#rtsp.record({
734
+ Range: "npt=0-",
735
+ Session: this.#streamContext.rtspSession,
736
+ "RTP-Info": `seq=${this.#streamContext.rtpseq};rtptime=${this.#streamContext.rtptime}`
737
+ });
738
+ await this.#rtsp.flush({
739
+ headers: {
740
+ Range: "npt=0-",
741
+ Session: this.#streamContext.rtspSession,
742
+ "RTP-Info": `seq=${this.#streamContext.rtpseq};rtptime=${this.#streamContext.rtptime}`
743
+ }
744
+ });
745
+ if (volume !== undefined) {
746
+ await this.setVolume(pctToDbfs(volume));
747
+ }
748
+ await this.#streamData(source, transport);
749
+ } catch (err) {
750
+ this.#context.logger.error("An error occurred during streaming.", err);
751
+ throw new Error(`An error occurred during streaming: ${err}`);
752
+ } finally {
753
+ this.#packetBacklog.clear();
754
+ if (transport) {
755
+ await this.#rtsp.teardown(this.#streamContext.rtspSession);
756
+ transport.close();
757
+ }
758
+ this.#protocol.teardown();
759
+ this.close();
760
+ this.emit("stopped");
761
+ }
762
+ }
763
+ async#streamData(source, transport) {
764
+ const stats = new Statistics(this.#streamContext.sampleRate);
765
+ const initialTime = performance.now();
766
+ let prevSlowSeqno = null;
767
+ let numberSlowSeqno = 0;
768
+ this.#isPlaying = true;
769
+ while (this.#isPlaying) {
770
+ const currentSeqno = this.#streamContext.rtpseq - 1;
771
+ const numSent = await this.#sendPacket(source, stats.totalFrames === 0, transport);
772
+ if (numSent === 0) {
773
+ break;
774
+ }
775
+ stats.tick(numSent);
776
+ const framesBehind = stats.framesBehind;
777
+ if (framesBehind >= FRAMES_PER_PACKET2) {
778
+ const maxPackets = Math.min(Math.floor(framesBehind / FRAMES_PER_PACKET2), MAX_PACKETS_COMPENSATE);
779
+ this.#context.logger.debug(`Compensating with ${maxPackets} packets (${framesBehind} frames behind)`);
780
+ const [sentFrames, hasMorePackets] = await this.#sendNumberOfPackets(source, transport, maxPackets);
781
+ stats.tick(sentFrames);
782
+ if (!hasMorePackets) {
783
+ break;
784
+ }
785
+ }
786
+ if (stats.intervalCompleted) {
787
+ const [intervalTime, intervalFrames] = stats.newInterval();
788
+ this.#context.logger.debug(`Sent ${intervalFrames} frames in ${intervalTime.toFixed(3)}s (current frames: ${stats.totalFrames}, expected: ${stats.expectedFrameCount})`);
789
+ }
790
+ const absTimeStream = stats.totalFrames / this.#streamContext.sampleRate;
791
+ const relToStart = (performance.now() - initialTime) / 1000;
792
+ const diff = absTimeStream - relToStart;
793
+ if (diff > 0) {
794
+ numberSlowSeqno = 0;
795
+ await waitFor(diff * 1000);
796
+ } else {
797
+ if (prevSlowSeqno === currentSeqno - 1) {
798
+ numberSlowSeqno++;
799
+ }
800
+ if (numberSlowSeqno >= SLOW_WARNING_THRESHOLD) {
801
+ this.#context.logger.warn(`Too slow to keep up for seqno ${currentSeqno} (${absTimeStream.toFixed(3)} vs ${relToStart.toFixed(3)} => ${diff.toFixed(3)})`);
802
+ } else {
803
+ this.#context.logger.debug(`Too slow to keep up for seqno ${currentSeqno} (${absTimeStream.toFixed(3)} vs ${relToStart.toFixed(3)} => ${diff.toFixed(3)})`);
804
+ }
805
+ prevSlowSeqno = currentSeqno;
806
+ }
807
+ }
808
+ const elapsedNs = Number(process.hrtime.bigint() - stats.startTimeNs);
809
+ this.#context.logger.debug(`Audio finished sending in ${(elapsedNs / 1e9).toFixed(3)}s`);
810
+ }
811
+ async#sendPacket(source, firstPacket, transport) {
812
+ if (this.#streamContext.paddingSent >= this.#streamContext.latency) {
813
+ return 0;
814
+ }
815
+ let frames = await source.readFrames(FRAMES_PER_PACKET2);
816
+ if (!frames) {
817
+ frames = Buffer.alloc(this.#streamContext.packetSize);
818
+ this.#streamContext.paddingSent += Math.floor(frames.length / this.#streamContext.frameSize);
819
+ } else if (frames.length !== this.#streamContext.packetSize) {
820
+ const padded = Buffer.alloc(this.#streamContext.packetSize);
821
+ frames.copy(padded);
822
+ frames = padded;
823
+ }
824
+ const header = AudioPacketHeader.encode(128, firstPacket ? 224 : 96, this.#streamContext.rtpseq, this.#streamContext.headTs, this.#rtsp.sessionId);
825
+ const [rtpseq, packet] = await this.#protocol.sendAudioPacket(transport, header, frames);
826
+ this.#packetBacklog.set(rtpseq, packet);
827
+ this.#streamContext.rtpseq = (this.#streamContext.rtpseq + 1) % 2 ** 16;
828
+ this.#streamContext.headTs += Math.floor(frames.length / this.#streamContext.frameSize);
829
+ return Math.floor(frames.length / this.#streamContext.frameSize);
830
+ }
831
+ async#sendNumberOfPackets(source, transport, count) {
832
+ let totalFrames = 0;
833
+ for (let i = 0;i < count; i++) {
834
+ const sent = await this.#sendPacket(source, false, transport);
835
+ totalFrames += sent;
836
+ if (sent === 0) {
837
+ return [totalFrames, false];
838
+ }
839
+ }
840
+ return [totalFrames, true];
841
+ }
842
+ #isMetadataEmpty(metadata) {
843
+ return metadata.title === "" && metadata.artist === "" && metadata.album === "" && metadata.duration === 0;
844
+ }
845
+ #updateOutputProperties(properties) {
846
+ const [sampleRate, channels, bytesPerChannel] = getAudioProperties(properties);
847
+ this.#streamContext.sampleRate = sampleRate;
848
+ this.#streamContext.channels = channels;
849
+ this.#streamContext.bytesPerChannel = bytesPerChannel;
850
+ this.#context.logger.debug(`Update play settings to ${sampleRate}/${channels}/${bytesPerChannel * 8}bit`);
851
+ }
852
+ }
853
+ // src/raop.ts
854
+ import { EventEmitter as EventEmitter3 } from "node:events";
855
+ import { Context, Discovery } from "@basmilius/apple-common";
856
+ var SAMPLE_RATE = 44100;
857
+ var CHANNELS = 2;
858
+ var BYTES_PER_CHANNEL = 2;
859
+ var FRAMES_PER_PACKET3 = 352;
860
+
861
+ class RaopClient extends EventEmitter3 {
862
+ get context() {
863
+ return this.#context;
864
+ }
865
+ get deviceId() {
866
+ return this.#discoveryResult.id;
867
+ }
868
+ get address() {
869
+ return this.#discoveryResult.address;
870
+ }
871
+ get modelName() {
872
+ return this.#discoveryResult.modelName;
873
+ }
874
+ get info() {
875
+ return this.#streamClient.info;
876
+ }
877
+ #context;
878
+ #rtsp;
879
+ #streamClient;
880
+ #discoveryResult;
881
+ constructor(context, rtsp, streamClient, discoveryResult) {
882
+ super();
883
+ this.#context = context;
884
+ this.#rtsp = rtsp;
885
+ this.#streamClient = streamClient;
886
+ this.#discoveryResult = discoveryResult;
887
+ this.#streamClient.on("playing", (info) => this.emit("playing", info));
888
+ this.#streamClient.on("stopped", () => this.emit("stopped"));
889
+ }
890
+ async stream(source, options = {}) {
891
+ await source.start();
892
+ try {
893
+ await this.#streamClient.sendAudio(source, options.metadata, options.volume);
894
+ } finally {
895
+ await source.stop();
896
+ }
897
+ }
898
+ stop() {
899
+ this.#streamClient.stop();
900
+ }
901
+ async setVolume(volume) {
902
+ await this.#streamClient.setVolume(volume);
903
+ }
904
+ async close() {
905
+ this.#streamClient.close();
906
+ await this.#rtsp.disconnect();
907
+ }
908
+ static async create(discoveryResult, timingServer) {
909
+ const context = new Context(discoveryResult.id);
910
+ const rtsp = new RtspClient(context, discoveryResult.address, discoveryResult.service.port);
911
+ await rtsp.connect();
912
+ const streamContext = createStreamContext();
913
+ streamContext.rtspSession = rtsp.rtspSessionId;
914
+ const protocol = new RaopStreamProtocol(rtsp, streamContext);
915
+ const settings = {
916
+ protocols: {
917
+ raop: {
918
+ controlPort: 0,
919
+ timingPort: 0
920
+ }
921
+ }
922
+ };
923
+ const streamClient = new StreamClient(context, rtsp, streamContext, protocol, settings, timingServer);
924
+ const properties = new Map(Object.entries(discoveryResult.txt));
925
+ await streamClient.initialize(properties);
926
+ return new RaopClient(context, rtsp, streamClient, discoveryResult);
927
+ }
928
+ static async discover(deviceId, timingServer) {
929
+ const discovery = Discovery.raop();
930
+ const result = await discovery.findUntil(deviceId);
931
+ return RaopClient.create(result, timingServer);
932
+ }
933
+ }
934
+
935
+ class RaopStreamProtocol {
936
+ #rtsp;
937
+ #streamContext;
938
+ #feedbackInterval;
939
+ constructor(rtsp, streamContext) {
940
+ this.#rtsp = rtsp;
941
+ this.#streamContext = streamContext;
942
+ }
943
+ async setup(timingPort, controlPort) {
944
+ await this.#rtsp.announce(this.#streamContext.bytesPerChannel, this.#streamContext.channels, this.#streamContext.sampleRate);
945
+ const transport = [
946
+ "RTP/AVP/UDP",
947
+ "unicast",
948
+ "interleaved=0-1",
949
+ "mode=record",
950
+ `control_port=${controlPort}`,
951
+ `timing_port=${timingPort}`
952
+ ].filter(Boolean).join(";");
953
+ const response = await this.#rtsp.setup({
954
+ Transport: transport
955
+ });
956
+ const transportHeader = response.headers.get("Transport");
957
+ if (!transportHeader) {
958
+ return;
959
+ }
960
+ const serverPortMatch = transportHeader.match(/server_port=(\d+)/);
961
+ if (serverPortMatch) {
962
+ this.#streamContext.serverPort = parseInt(serverPortMatch[1], 10);
963
+ }
964
+ const controlPortMatch = transportHeader.match(/control_port=(\d+)/);
965
+ if (controlPortMatch) {
966
+ this.#streamContext.controlPort = parseInt(controlPortMatch[1], 10);
967
+ }
968
+ }
969
+ async startFeedback() {
970
+ this.#feedbackInterval = setInterval(async () => {
971
+ try {
972
+ await this.#rtsp.feedback(true);
973
+ } catch {}
974
+ }, 2000);
975
+ }
976
+ async sendAudioPacket(transport, header, audio) {
977
+ const packet = Buffer.concat([header, audio]);
978
+ const seqno = header.readUInt16BE(2);
979
+ await new Promise((resolve, reject) => {
980
+ transport.send(packet, (err) => err ? reject(err) : resolve());
981
+ });
982
+ return [seqno, packet];
983
+ }
984
+ teardown() {
985
+ if (!this.#feedbackInterval) {
986
+ return;
987
+ }
988
+ clearInterval(this.#feedbackInterval);
989
+ this.#feedbackInterval = undefined;
990
+ }
991
+ }
992
+ function createStreamContext() {
993
+ return {
994
+ sampleRate: SAMPLE_RATE,
995
+ channels: CHANNELS,
996
+ bytesPerChannel: BYTES_PER_CHANNEL,
997
+ rtpseq: Math.floor(Math.random() * 65536),
998
+ rtptime: Math.floor(Math.random() * 4294967295),
999
+ headTs: 0,
1000
+ latency: Math.floor(SAMPLE_RATE * 2),
1001
+ serverPort: 0,
1002
+ controlPort: 0,
1003
+ rtspSession: "",
1004
+ volume: -20,
1005
+ position: 0,
1006
+ packetSize: FRAMES_PER_PACKET3 * CHANNELS * BYTES_PER_CHANNEL,
1007
+ frameSize: CHANNELS * BYTES_PER_CHANNEL,
1008
+ paddingSent: 0,
1009
+ reset() {
1010
+ this.rtpseq = Math.floor(Math.random() * 65536);
1011
+ this.rtptime = Math.floor(Math.random() * 4294967295);
1012
+ this.headTs = this.rtptime;
1013
+ this.paddingSent = 0;
1014
+ this.position = 0;
1015
+ }
1016
+ };
1017
+ }
1018
+ export {
1019
+ pctToDbfs,
1020
+ getMetadataTypes,
1021
+ getEncryptionTypes,
1022
+ getAudioProperties,
1023
+ decodeRetransmitRequest,
1024
+ SyncPacket,
1025
+ StreamClient,
1026
+ Statistics,
1027
+ RtspClient,
1028
+ RaopClient,
1029
+ PacketFifo,
1030
+ MetadataType,
1031
+ EncryptionType,
1032
+ ControlClient,
1033
+ AudioPacketHeader
1034
+ };
@@ -0,0 +1,19 @@
1
+ export declare class PacketFifo {
2
+ #private;
3
+ constructor(maxSize: number);
4
+ get(seqno: number): Buffer | undefined;
5
+ set(seqno: number, packet: Buffer): void;
6
+ has(seqno: number): boolean;
7
+ clear(): void;
8
+ }
9
+ export declare const AudioPacketHeader: {
10
+ encode(header: number, payloadType: number, seqno: number, timestamp: number, ssrc: number): Buffer;
11
+ };
12
+ export declare const SyncPacket: {
13
+ encode(header: number, payloadType: number, seqno: number, rtpTimestamp: number, ntpSec: number, ntpFrac: number, rtpTimestampNow: number): Buffer;
14
+ };
15
+ export type RetransmitRequest = {
16
+ readonly lostSeqno: number;
17
+ readonly lostPackets: number;
18
+ };
19
+ export declare function decodeRetransmitRequest(data: Buffer): RetransmitRequest;
package/dist/raop.d.ts ADDED
@@ -0,0 +1,26 @@
1
+ import { EventEmitter } from "node:events";
2
+ import { type AudioSource, Context, type DiscoveryResult, TimingServer } from "@basmilius/apple-common";
3
+ import type { MediaMetadata, PlaybackInfo } from "./types";
4
+ export type EventMap = {
5
+ readonly playing: [playbackInfo: PlaybackInfo];
6
+ readonly stopped: [];
7
+ };
8
+ export type StreamOptions = {
9
+ readonly metadata?: MediaMetadata;
10
+ readonly volume?: number;
11
+ };
12
+ export declare class RaopClient extends EventEmitter<EventMap> {
13
+ #private;
14
+ get context(): Context;
15
+ get deviceId(): string;
16
+ get address(): string;
17
+ get modelName(): string;
18
+ get info(): Record<string, unknown>;
19
+ private constructor();
20
+ stream(source: AudioSource, options?: StreamOptions): Promise<void>;
21
+ stop(): void;
22
+ setVolume(volume: number): Promise<void>;
23
+ close(): Promise<void>;
24
+ static create(discoveryResult: DiscoveryResult, timingServer: TimingServer): Promise<RaopClient>;
25
+ static discover(deviceId: string, timingServer: TimingServer): Promise<RaopClient>;
26
+ }
@@ -0,0 +1,28 @@
1
+ import { Connection, type Context } from "@basmilius/apple-common";
2
+ import type { MediaMetadata } from "./types";
3
+ export default class RtspClient extends Connection<{}> {
4
+ #private;
5
+ get activeRemoteId(): string;
6
+ get dacpId(): string;
7
+ get rtspSessionId(): string;
8
+ get sessionId(): number;
9
+ get uri(): string;
10
+ get connection(): {
11
+ localIp: string;
12
+ remoteIp: string;
13
+ };
14
+ constructor(context: Context, address: string, port: number);
15
+ info(): Promise<Record<string, unknown>>;
16
+ authSetup(): Promise<void>;
17
+ announce(bytesPerChannel: number, channels: number, sampleRate: number, password?: string): Promise<Response>;
18
+ setup(headers?: Record<string, string>, body?: Buffer | string | Record<string, unknown>): Promise<Response>;
19
+ record(headers?: Record<string, string>): Promise<void>;
20
+ flush(options: {
21
+ headers: Record<string, string>;
22
+ }): Promise<void>;
23
+ setParameter(name: string, value: string): Promise<void>;
24
+ setMetadata(session: string, rtpseq: number, rtptime: number, metadata: MediaMetadata): Promise<void>;
25
+ setArtwork(session: string, rtpseq: number, rtptime: number, artwork: Buffer): Promise<void>;
26
+ feedback(allowError?: boolean): Promise<Response>;
27
+ teardown(session: string): Promise<void>;
28
+ }
@@ -0,0 +1,13 @@
1
+ export default class Statistics {
2
+ readonly sampleRate: number;
3
+ readonly startTimeNs: bigint;
4
+ intervalTime: number;
5
+ totalFrames: number;
6
+ intervalFrames: number;
7
+ constructor(sampleRate: number);
8
+ get expectedFrameCount(): number;
9
+ get framesBehind(): number;
10
+ get intervalCompleted(): boolean;
11
+ tick(sentFrames: number): void;
12
+ newInterval(): [number, number];
13
+ }
@@ -0,0 +1,19 @@
1
+ import { EventEmitter } from "node:events";
2
+ import { type AudioSource, type Context, type TimingServer } from "@basmilius/apple-common";
3
+ import { type MediaMetadata, type PlaybackInfo, type Settings, type StreamContext, type StreamProtocol } from "./types";
4
+ import RtspClient from "./rtspClient";
5
+ export type EventMap = {
6
+ readonly playing: [playbackInfo: PlaybackInfo];
7
+ readonly stopped: [];
8
+ };
9
+ export default class StreamClient extends EventEmitter<EventMap> {
10
+ #private;
11
+ get info(): Record<string, unknown>;
12
+ get playbackInfo(): PlaybackInfo;
13
+ constructor(context: Context, rtsp: RtspClient, streamContext: StreamContext, protocol: StreamProtocol, settings: Settings, timingServer: TimingServer);
14
+ close(): void;
15
+ initialize(properties: Map<string, string>): Promise<void>;
16
+ stop(): void;
17
+ setVolume(volume: number): Promise<void>;
18
+ sendAudio(source: AudioSource, metadata?: MediaMetadata, volume?: number): Promise<void>;
19
+ }
@@ -0,0 +1,68 @@
1
+ import type { Socket as UdpSocket } from 'node:dgram';
2
+
3
+ export type MediaMetadata = {
4
+ readonly title: string;
5
+ readonly artist: string;
6
+ readonly album: string;
7
+ readonly duration: number;
8
+ readonly artwork?: Buffer;
9
+ }
10
+
11
+ export type PlaybackInfo = {
12
+ readonly metadata: MediaMetadata;
13
+ readonly position: number;
14
+ }
15
+
16
+ export type StreamContext = {
17
+ sampleRate: number;
18
+ channels: number;
19
+ bytesPerChannel: number;
20
+ rtpseq: number;
21
+ rtptime: number;
22
+ headTs: number;
23
+ latency: number;
24
+ serverPort: number;
25
+ controlPort: number;
26
+ rtspSession: string;
27
+ volume: number;
28
+ position: number;
29
+ packetSize: number;
30
+ frameSize: number;
31
+ paddingSent: number;
32
+
33
+ reset(): void;
34
+ }
35
+
36
+ export interface StreamProtocol {
37
+ setup(timingPort: number, controlPort: number): Promise<void>;
38
+ startFeedback(): Promise<void>;
39
+ sendAudioPacket(transport: UdpSocket, header: Buffer, audio: Buffer): Promise<[number, Buffer]>;
40
+ teardown(): void;
41
+ }
42
+
43
+ export interface Settings {
44
+ protocols: {
45
+ raop: {
46
+ controlPort: number;
47
+ timingPort: number;
48
+ };
49
+ };
50
+ }
51
+
52
+ export enum EncryptionType {
53
+ Unknown = 0,
54
+ Unencrypted = 1 << 0,
55
+ MFiSAP = 1 << 1
56
+ }
57
+
58
+ export enum MetadataType {
59
+ NotSupported = 0,
60
+ Text = 1 << 0,
61
+ Artwork = 1 << 1,
62
+ Progress = 1 << 2
63
+ }
64
+
65
+ export interface RaopListener {
66
+ playing(playbackInfo: PlaybackInfo): void;
67
+ stopped(): void;
68
+ }
@@ -0,0 +1,5 @@
1
+ import { EncryptionType, MetadataType } from "./types";
2
+ export declare function pctToDbfs(volume: number): number;
3
+ export declare function getEncryptionTypes(properties: Map<string, string>): EncryptionType;
4
+ export declare function getMetadataTypes(properties: Map<string, string>): MetadataType;
5
+ export declare function getAudioProperties(properties: Map<string, string>): [number, number, number];
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@basmilius/apple-raop",
3
+ "description": "Implementation of Apple's RAOP protocol in Node.js.",
4
+ "version": "0.6.0",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "author": {
8
+ "name": "Bas Milius",
9
+ "email": "bas@mili.us",
10
+ "url": "https://bas.dev"
11
+ },
12
+ "keywords": [
13
+ "apple",
14
+ "raop",
15
+ "airplay",
16
+ "tv",
17
+ "apple tv",
18
+ "homekit"
19
+ ],
20
+ "files": [
21
+ "dist",
22
+ "LICENSE"
23
+ ],
24
+ "publishConfig": {
25
+ "access": "public",
26
+ "provenance": true
27
+ },
28
+ "scripts": {
29
+ "build": "tsc && bun -b build.ts",
30
+ "watch:test": "bun --watch test.ts"
31
+ },
32
+ "main": "./dist/index.js",
33
+ "types": "./dist/index.d.ts",
34
+ "typings": "./dist/index.d.ts",
35
+ "sideEffects": false,
36
+ "exports": {
37
+ ".": {
38
+ "types": "./dist/index.d.ts",
39
+ "default": "./dist/index.js"
40
+ }
41
+ },
42
+ "dependencies": {
43
+ "@basmilius/apple-airplay": "0.6.0",
44
+ "@basmilius/apple-common": "0.6.0",
45
+ "@basmilius/apple-encoding": "0.6.0",
46
+ "@basmilius/apple-encryption": "0.6.0"
47
+ },
48
+ "devDependencies": {
49
+ "@basmilius/tools": "^2.23.0",
50
+ "@types/bun": "^1.3.8"
51
+ }
52
+ }