lox-airplay-sender 0.3.0 → 0.3.2
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/README.md +1 -0
- package/dist/core/audioOut.d.ts +1 -0
- package/dist/core/audioOut.js +26 -4
- package/dist/core/index.js +3 -0
- package/dist/esm/core/audioOut.js +26 -4
- package/dist/esm/core/index.js +3 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/utils/config.js +2 -0
- package/dist/index.js +3 -0
- package/dist/utils/config.d.ts +2 -0
- package/dist/utils/config.js +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -63,6 +63,7 @@ Creates and starts a sender for one AirPlay device. Returns the instance so you
|
|
|
63
63
|
- `device`: `{ event: "device", message: status, detail: { key, desc } }`
|
|
64
64
|
- `buffer`: `{ event: "buffer", message: status }` where status is `buffering|playing|drain|end`
|
|
65
65
|
- `error`: `{ event: "error", message }`
|
|
66
|
+
- `metrics`: `{ event: "metrics", detail }` sync drift snapshots emitted on each sync tick when enabled.
|
|
66
67
|
|
|
67
68
|
### `LoxAirplaySender` methods
|
|
68
69
|
- `sendPcm(chunk: Buffer)`: Push raw PCM audio. If `inputCodec` is `"alac"` you can push ALAC frames.
|
package/dist/core/audioOut.d.ts
CHANGED
package/dist/core/audioOut.js
CHANGED
|
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const node_events_1 = require("node:events");
|
|
7
|
+
const node_perf_hooks_1 = require("node:perf_hooks");
|
|
7
8
|
const config_1 = __importDefault(require("../utils/config"));
|
|
8
9
|
const numUtil_1 = require("../utils/numUtil");
|
|
9
10
|
const SEQ_NUM_WRAP = Math.pow(2, 16);
|
|
@@ -14,7 +15,8 @@ const SEQ_NUM_WRAP = Math.pow(2, 16);
|
|
|
14
15
|
class AudioOut extends node_events_1.EventEmitter {
|
|
15
16
|
lastSeq = -1;
|
|
16
17
|
hasAirTunes = false;
|
|
17
|
-
rtpTimeRef =
|
|
18
|
+
rtpTimeRef = 0;
|
|
19
|
+
monotonicRef = 0;
|
|
18
20
|
startTimeMs;
|
|
19
21
|
latencyFrames = 0;
|
|
20
22
|
latencyApplied = false;
|
|
@@ -29,7 +31,10 @@ class AudioOut extends node_events_1.EventEmitter {
|
|
|
29
31
|
typeof startTimeMs === 'number' && Number.isFinite(startTimeMs)
|
|
30
32
|
? startTimeMs
|
|
31
33
|
: undefined;
|
|
32
|
-
|
|
34
|
+
const wallToMonoOffset = Date.now() - node_perf_hooks_1.performance.now();
|
|
35
|
+
// Anchor the RTP clock to a monotonic base to avoid NTP slews.
|
|
36
|
+
this.rtpTimeRef = (this.startTimeMs ?? Date.now()) - wallToMonoOffset;
|
|
37
|
+
this.monotonicRef = node_perf_hooks_1.performance.now();
|
|
33
38
|
devices.on('airtunes_devices', (hasAirTunes) => {
|
|
34
39
|
this.hasAirTunes = hasAirTunes;
|
|
35
40
|
});
|
|
@@ -42,17 +47,34 @@ class AudioOut extends node_events_1.EventEmitter {
|
|
|
42
47
|
packet.timestamp = (0, numUtil_1.low32)(seq * config_1.default.frames_per_packet + 2 * config_1.default.sampling_rate);
|
|
43
48
|
if (this.hasAirTunes && seq % config_1.default.sync_period === 0) {
|
|
44
49
|
this.emit('need_sync', seq);
|
|
50
|
+
const expectedTimeMs = this.rtpTimeRef +
|
|
51
|
+
((seq * config_1.default.frames_per_packet) / config_1.default.sampling_rate) * 1000;
|
|
52
|
+
const deltaMs = Date.now() - expectedTimeMs;
|
|
53
|
+
this.emit('metrics', { type: 'sync', seq, deltaMs, latencyFrames: this.latencyFrames });
|
|
45
54
|
}
|
|
46
55
|
this.emit('packet', packet);
|
|
47
56
|
packet.release();
|
|
48
57
|
};
|
|
58
|
+
const frameDurationMs = (config_1.default.frames_per_packet / config_1.default.sampling_rate) * 1000;
|
|
49
59
|
const syncAudio = () => {
|
|
50
|
-
const
|
|
60
|
+
const nowMs = node_perf_hooks_1.performance.now();
|
|
61
|
+
const elapsed = nowMs - this.rtpTimeRef;
|
|
51
62
|
if (elapsed < 0) {
|
|
52
63
|
setTimeout(syncAudio, Math.min(config_1.default.stream_latency, Math.abs(elapsed)));
|
|
53
64
|
return;
|
|
54
65
|
}
|
|
55
|
-
|
|
66
|
+
let currentSeq = Math.floor((elapsed * config_1.default.sampling_rate) / (config_1.default.frames_per_packet * 1000));
|
|
67
|
+
// If we're lagging behind significantly, jump forward to avoid long hitches.
|
|
68
|
+
const expectedTimeMs = this.rtpTimeRef + currentSeq * frameDurationMs;
|
|
69
|
+
const deltaMs = nowMs - expectedTimeMs;
|
|
70
|
+
if (deltaMs > config_1.default.jump_forward_threshold_ms) {
|
|
71
|
+
const jumpSeq = Math.ceil((config_1.default.jump_forward_lead_ms * config_1.default.sampling_rate) /
|
|
72
|
+
(config_1.default.frames_per_packet * 1000));
|
|
73
|
+
const newSeq = currentSeq + jumpSeq;
|
|
74
|
+
this.rtpTimeRef = nowMs - newSeq * frameDurationMs;
|
|
75
|
+
this.lastSeq = newSeq - 1;
|
|
76
|
+
currentSeq = newSeq;
|
|
77
|
+
}
|
|
56
78
|
for (let i = this.lastSeq + 1; i <= currentSeq; i += 1) {
|
|
57
79
|
sendPacket(i);
|
|
58
80
|
}
|
package/dist/core/index.js
CHANGED
|
@@ -69,6 +69,9 @@ class AirTunes extends node_stream_1.Duplex {
|
|
|
69
69
|
this.emit('buffer', status);
|
|
70
70
|
});
|
|
71
71
|
audioOut.init(this.devices, this.circularBuffer, options.startTimeMs);
|
|
72
|
+
audioOut.on('metrics', (metrics) => {
|
|
73
|
+
this.emit('metrics', metrics);
|
|
74
|
+
});
|
|
72
75
|
this.circularBuffer.on('drain', () => {
|
|
73
76
|
this.emit('drain');
|
|
74
77
|
});
|
|
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const node_events_1 = require("node:events");
|
|
7
|
+
const node_perf_hooks_1 = require("node:perf_hooks");
|
|
7
8
|
const config_1 = __importDefault(require("../utils/config"));
|
|
8
9
|
const numUtil_1 = require("../utils/numUtil");
|
|
9
10
|
const SEQ_NUM_WRAP = Math.pow(2, 16);
|
|
@@ -14,7 +15,8 @@ const SEQ_NUM_WRAP = Math.pow(2, 16);
|
|
|
14
15
|
class AudioOut extends node_events_1.EventEmitter {
|
|
15
16
|
lastSeq = -1;
|
|
16
17
|
hasAirTunes = false;
|
|
17
|
-
rtpTimeRef =
|
|
18
|
+
rtpTimeRef = 0;
|
|
19
|
+
monotonicRef = 0;
|
|
18
20
|
startTimeMs;
|
|
19
21
|
latencyFrames = 0;
|
|
20
22
|
latencyApplied = false;
|
|
@@ -29,7 +31,10 @@ class AudioOut extends node_events_1.EventEmitter {
|
|
|
29
31
|
typeof startTimeMs === 'number' && Number.isFinite(startTimeMs)
|
|
30
32
|
? startTimeMs
|
|
31
33
|
: undefined;
|
|
32
|
-
|
|
34
|
+
const wallToMonoOffset = Date.now() - node_perf_hooks_1.performance.now();
|
|
35
|
+
// Anchor the RTP clock to a monotonic base to avoid NTP slews.
|
|
36
|
+
this.rtpTimeRef = (this.startTimeMs ?? Date.now()) - wallToMonoOffset;
|
|
37
|
+
this.monotonicRef = node_perf_hooks_1.performance.now();
|
|
33
38
|
devices.on('airtunes_devices', (hasAirTunes) => {
|
|
34
39
|
this.hasAirTunes = hasAirTunes;
|
|
35
40
|
});
|
|
@@ -42,17 +47,34 @@ class AudioOut extends node_events_1.EventEmitter {
|
|
|
42
47
|
packet.timestamp = (0, numUtil_1.low32)(seq * config_1.default.frames_per_packet + 2 * config_1.default.sampling_rate);
|
|
43
48
|
if (this.hasAirTunes && seq % config_1.default.sync_period === 0) {
|
|
44
49
|
this.emit('need_sync', seq);
|
|
50
|
+
const expectedTimeMs = this.rtpTimeRef +
|
|
51
|
+
((seq * config_1.default.frames_per_packet) / config_1.default.sampling_rate) * 1000;
|
|
52
|
+
const deltaMs = Date.now() - expectedTimeMs;
|
|
53
|
+
this.emit('metrics', { type: 'sync', seq, deltaMs, latencyFrames: this.latencyFrames });
|
|
45
54
|
}
|
|
46
55
|
this.emit('packet', packet);
|
|
47
56
|
packet.release();
|
|
48
57
|
};
|
|
58
|
+
const frameDurationMs = (config_1.default.frames_per_packet / config_1.default.sampling_rate) * 1000;
|
|
49
59
|
const syncAudio = () => {
|
|
50
|
-
const
|
|
60
|
+
const nowMs = node_perf_hooks_1.performance.now();
|
|
61
|
+
const elapsed = nowMs - this.rtpTimeRef;
|
|
51
62
|
if (elapsed < 0) {
|
|
52
63
|
setTimeout(syncAudio, Math.min(config_1.default.stream_latency, Math.abs(elapsed)));
|
|
53
64
|
return;
|
|
54
65
|
}
|
|
55
|
-
|
|
66
|
+
let currentSeq = Math.floor((elapsed * config_1.default.sampling_rate) / (config_1.default.frames_per_packet * 1000));
|
|
67
|
+
// If we're lagging behind significantly, jump forward to avoid long hitches.
|
|
68
|
+
const expectedTimeMs = this.rtpTimeRef + currentSeq * frameDurationMs;
|
|
69
|
+
const deltaMs = nowMs - expectedTimeMs;
|
|
70
|
+
if (deltaMs > config_1.default.jump_forward_threshold_ms) {
|
|
71
|
+
const jumpSeq = Math.ceil((config_1.default.jump_forward_lead_ms * config_1.default.sampling_rate) /
|
|
72
|
+
(config_1.default.frames_per_packet * 1000));
|
|
73
|
+
const newSeq = currentSeq + jumpSeq;
|
|
74
|
+
this.rtpTimeRef = nowMs - newSeq * frameDurationMs;
|
|
75
|
+
this.lastSeq = newSeq - 1;
|
|
76
|
+
currentSeq = newSeq;
|
|
77
|
+
}
|
|
56
78
|
for (let i = this.lastSeq + 1; i <= currentSeq; i += 1) {
|
|
57
79
|
sendPacket(i);
|
|
58
80
|
}
|
package/dist/esm/core/index.js
CHANGED
|
@@ -69,6 +69,9 @@ class AirTunes extends node_stream_1.Duplex {
|
|
|
69
69
|
this.emit('buffer', status);
|
|
70
70
|
});
|
|
71
71
|
audioOut.init(this.devices, this.circularBuffer, options.startTimeMs);
|
|
72
|
+
audioOut.on('metrics', (metrics) => {
|
|
73
|
+
this.emit('metrics', metrics);
|
|
74
|
+
});
|
|
72
75
|
this.circularBuffer.on('drain', () => {
|
|
73
76
|
this.emit('drain');
|
|
74
77
|
});
|
package/dist/esm/index.js
CHANGED
|
@@ -84,6 +84,9 @@ class LoxAirplaySender extends node_events_1.EventEmitter {
|
|
|
84
84
|
this.airtunes.on('error', (err) => {
|
|
85
85
|
onEvent?.({ event: 'error', message: err instanceof Error ? err.message : String(err) });
|
|
86
86
|
});
|
|
87
|
+
this.airtunes.on('metrics', (detail) => {
|
|
88
|
+
onEvent?.({ event: 'metrics', detail });
|
|
89
|
+
});
|
|
87
90
|
const dev = this.airtunes.add(options.host, {
|
|
88
91
|
port: options.port,
|
|
89
92
|
name: options.name,
|
package/dist/esm/utils/config.js
CHANGED
|
@@ -27,6 +27,8 @@ exports.config = {
|
|
|
27
27
|
rtsp_retry_jitter_ms: 150,
|
|
28
28
|
control_sync_base_delay_ms: 2,
|
|
29
29
|
control_sync_jitter_ms: 3,
|
|
30
|
+
jump_forward_threshold_ms: 180,
|
|
31
|
+
jump_forward_lead_ms: 220,
|
|
30
32
|
device_magic: (0, numUtil_1.randomInt)(9),
|
|
31
33
|
ntp_epoch: 0x83aa7e80,
|
|
32
34
|
iv_base64: 'ePRBLI0XN5ArFaaz7ncNZw',
|
package/dist/index.js
CHANGED
|
@@ -84,6 +84,9 @@ class LoxAirplaySender extends node_events_1.EventEmitter {
|
|
|
84
84
|
this.airtunes.on('error', (err) => {
|
|
85
85
|
onEvent?.({ event: 'error', message: err instanceof Error ? err.message : String(err) });
|
|
86
86
|
});
|
|
87
|
+
this.airtunes.on('metrics', (detail) => {
|
|
88
|
+
onEvent?.({ event: 'metrics', detail });
|
|
89
|
+
});
|
|
87
90
|
const dev = this.airtunes.add(options.host, {
|
|
88
91
|
port: options.port,
|
|
89
92
|
name: options.name,
|
package/dist/utils/config.d.ts
CHANGED
|
@@ -22,6 +22,8 @@ export interface AirplayConfig {
|
|
|
22
22
|
rtsp_retry_jitter_ms: number;
|
|
23
23
|
control_sync_base_delay_ms: number;
|
|
24
24
|
control_sync_jitter_ms: number;
|
|
25
|
+
jump_forward_threshold_ms: number;
|
|
26
|
+
jump_forward_lead_ms: number;
|
|
25
27
|
device_magic: number;
|
|
26
28
|
ntp_epoch: number;
|
|
27
29
|
iv_base64: string;
|
package/dist/utils/config.js
CHANGED
|
@@ -27,6 +27,8 @@ exports.config = {
|
|
|
27
27
|
rtsp_retry_jitter_ms: 150,
|
|
28
28
|
control_sync_base_delay_ms: 2,
|
|
29
29
|
control_sync_jitter_ms: 3,
|
|
30
|
+
jump_forward_threshold_ms: 180,
|
|
31
|
+
jump_forward_lead_ms: 220,
|
|
30
32
|
device_magic: (0, numUtil_1.randomInt)(9),
|
|
31
33
|
ntp_epoch: 0x83aa7e80,
|
|
32
34
|
iv_base64: 'ePRBLI0XN5ArFaaz7ncNZw',
|