-
-
Notifications
You must be signed in to change notification settings - Fork 305
Replace airtunes lib by airplay-sender #2439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
854c119
b404209
1189e33
3db9f26
0f0e2e7
76b975c
8826bc1
8c26bdd
3e91e74
a560324
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,53 +18,79 @@ async function setValue(device, deviceFeature, value, options) { | |
| } | ||
|
|
||
| if (deviceFeature.type === DEVICE_FEATURE_TYPES.MUSIC.PLAY_NOTIFICATION) { | ||
| const client = new this.Airtunes(); | ||
| const airplayDevice = client.add(ipAddress, { | ||
| volume: options?.volume || 70, | ||
| }); | ||
| const MAX_NOTIFICATION_DURATION_MS = 5 * 60 * 1000; // 5 minutes max | ||
| let decodeProcess; | ||
| let killTimer; | ||
|
|
||
| client.on('buffer', (event) => { | ||
| if (event === 'end') { | ||
| logger.debug('Playback ended, waiting for AirTunes devices'); | ||
| setTimeout(() => { | ||
| client.stopAll(() => { | ||
| if (decodeProcess) { | ||
| decodeProcess.kill(); | ||
| } | ||
| }); | ||
| }, 5000); | ||
| const cleanup = () => { | ||
| clearTimeout(killTimer); | ||
| if (decodeProcess) { | ||
| decodeProcess.kill(); | ||
| decodeProcess = null; | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| const sender = this.airplaySender( | ||
| { | ||
| host: ipAddress, | ||
| airplay2: true, | ||
| volume: options?.volume ?? 70, | ||
| }, | ||
| async (event) => { | ||
| if (event.event === 'device' && event.message === 'ready') { | ||
| decodeProcess = this.childProcess.spawn('ffmpeg', [ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any risk that this process could run indefinitely?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it can happen if there is a deconnection during url reading. I added a global timeout after 5 minutes (we can adjust, I don't know user's notification duration). In this case execFile is not possible, file is send in realtime using chunks to homepods, execFile needs to buffer all file in memory |
||
| '-re', | ||
| '-i', | ||
| value, | ||
| '-acodec', | ||
| 'pcm_s16le', | ||
| '-f', | ||
| 's16le', // PCM 16bits, little-endian | ||
| '-ar', | ||
| '44100', // Sampling rate | ||
| '-ac', | ||
| 2, // Stereo | ||
| 'pipe:1', // Output on stdout | ||
| ]); | ||
|
|
||
| airplayDevice.on('status', async (status) => { | ||
| if (status === 'ready') { | ||
| decodeProcess = this.childProcess.spawn('ffmpeg', [ | ||
| '-i', | ||
| value, | ||
| '-acodec', | ||
| 'pcm_s16le', | ||
| '-f', | ||
| 's16le', // PCM 16bits, little-endian | ||
| '-ar', | ||
| '44100', // Sampling rate | ||
| '-ac', | ||
| 2, // Stereo | ||
| 'pipe:1', // Output on stdout | ||
| ]); | ||
| decodeProcess.stdout.pipe(client); | ||
| killTimer = setTimeout(() => { | ||
| logger.warn('ffmpeg exceeded max notification duration, killing process'); | ||
| cleanup(); | ||
| sender.stop(); | ||
| }, MAX_NOTIFICATION_DURATION_MS); | ||
|
|
||
| // detect if ffmpeg was not spawned correctly | ||
| decodeProcess.stderr.setEncoding('utf8'); | ||
| decodeProcess.stderr.on('data', (data) => { | ||
| if (/^execvp\(\)/.test(data)) { | ||
| decodeProcess.on('error', (err) => { | ||
| logger.error('Failed to start ffmpeg'); | ||
| logger.error(`stderr: ${data}`); | ||
| client.stopAll(() => {}); | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
| logger.error(err); | ||
| cleanup(); | ||
| sender.stop(); | ||
| }); | ||
|
|
||
| decodeProcess.stdout.on('data', (chunk) => sender.sendPcm(chunk)); | ||
| decodeProcess.stdout.on('end', () => { | ||
| clearTimeout(killTimer); | ||
| setTimeout(() => { | ||
| sender.stop(); | ||
| }, 7000); | ||
| }); | ||
|
|
||
| // detect if ffmpeg was not spawned correctly | ||
| decodeProcess.stderr.setEncoding('utf8'); | ||
| decodeProcess.stderr.on('data', (data) => { | ||
| if (/^execvp\(\)/.test(data)) { | ||
| logger.error('Failed to start ffmpeg'); | ||
| logger.error(`stderr: ${data}`); | ||
| cleanup(); | ||
| sender.stop(); | ||
| } | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| if (event.event === 'buffer' && event.message === 'end') { | ||
| cleanup(); | ||
| } | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.