| 1 | //! Linux notification and ringtone playback through the system audio output. |
| 2 | use std::io::Cursor; |
| 3 | use std::sync::atomic::{AtomicBool, Ordering}; |
| 4 | use std::sync::{Arc, Mutex}; |
| 5 | use std::time::Duration; |
| 6 | |
| 7 | use rodio::cpal::traits::{DeviceTrait, HostTrait}; |
| 8 | |
| 9 | const SOUND_BYTES: &[u8] = include_bytes!("../../../../app/assets/sounds/notification.mp3"); |
| 10 | const POLL: Duration = Duration::from_millis(20); |
| 11 | static SOUND_OUTPUT: Mutex<Option<String>> = Mutex::new(None); |
| 12 | |
| 13 | pub fn set_sound_output(device_key: Option<String>) { |
| 14 | *SOUND_OUTPUT.lock().unwrap_or_else(|p| p.into_inner()) = device_key; |
| 15 | } |
| 16 | |
| 17 | fn open_sink() -> Result<rodio::MixerDeviceSink, String> { |
| 18 | let chosen = SOUND_OUTPUT |
| 19 | .lock() |
| 20 | .unwrap_or_else(|p| p.into_inner()) |
| 21 | .clone(); |
| 22 | let device = chosen.and_then(|key| { |
| 23 | rodio::cpal::default_host() |
| 24 | .output_devices() |
| 25 | .ok()? |
| 26 | .find(|device| { |
| 27 | device.id().is_ok_and(|id| id.1 == key) |
| 28 | || device |
| 29 | .description() |
| 30 | .is_ok_and(|description| description.name() == key) |
| 31 | }) |
| 32 | }); |
| 33 | if let Some(device) = device { |
| 34 | if let Ok(sink) = |
| 35 | rodio::DeviceSinkBuilder::from_device(device).and_then(|builder| builder.open_stream()) |
| 36 | { |
| 37 | return Ok(sink); |
| 38 | } |
| 39 | } |
| 40 | rodio::DeviceSinkBuilder::open_default_sink() |
| 41 | .map_err(|e| format!("cannot open audio output: {e}")) |
| 42 | } |
| 43 | |
| 44 | fn play(bytes: Vec<u8>, looped: bool, stop: Arc<AtomicBool>) -> Result<(), String> { |
| 45 | if stop.load(Ordering::SeqCst) { |
| 46 | return Ok(()); |
| 47 | } |
| 48 | let mut sink = open_sink()?; |
| 49 | sink.log_on_drop(false); |
| 50 | loop { |
| 51 | if stop.load(Ordering::SeqCst) { |
| 52 | return Ok(()); |
| 53 | } |
| 54 | let player = rodio::play(sink.mixer(), Cursor::new(bytes.clone())) |
| 55 | .map_err(|e| format!("cannot decode notification sound: {e}"))?; |
| 56 | while !player.empty() { |
| 57 | if stop.load(Ordering::SeqCst) { |
| 58 | player.stop(); |
| 59 | return Ok(()); |
| 60 | } |
| 61 | std::thread::sleep(POLL); |
| 62 | } |
| 63 | if !looped { |
| 64 | return Ok(()); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | pub fn play_mp3_until(bytes: Vec<u8>, looped: bool, stop: Arc<AtomicBool>) { |
| 70 | std::thread::spawn(move || { |
| 71 | if let Err(error) = play(bytes, looped, stop) { |
| 72 | crate::diagnostics::record(format!("sound: {error}")); |
| 73 | } |
| 74 | }); |
| 75 | } |
| 76 | |
| 77 | pub fn play_mp3(bytes: Vec<u8>) { |
| 78 | play_mp3_until(bytes, false, Arc::new(AtomicBool::new(false))); |
| 79 | } |
| 80 | |
| 81 | pub fn play_sound() { |
| 82 | play_mp3(SOUND_BYTES.to_vec()); |
| 83 | } |
| 84 | |
| 85 | #[cfg(test)] |
| 86 | mod tests { |
| 87 | use super::*; |
| 88 | |
| 89 | #[test] |
| 90 | fn bundled_notification_decodes_to_audible_samples() { |
| 91 | let decoded = rodio::Decoder::try_from(Cursor::new(SOUND_BYTES)).unwrap(); |
| 92 | assert!(decoded.take(48000).any(|sample| sample.abs() > 0.001)); |
| 93 | } |
| 94 | |
| 95 | #[test] |
| 96 | fn cancelled_playback_does_not_open_a_device_or_decode_audio() { |
| 97 | assert!(play(Vec::new(), true, Arc::new(AtomicBool::new(true))).is_ok()); |
| 98 | } |
| 99 | |
| 100 | #[test] |
| 101 | #[ignore = "plays a short notification on the real default audio output"] |
| 102 | fn notification_plays_on_default_output() { |
| 103 | play( |
| 104 | SOUND_BYTES.to_vec(), |
| 105 | false, |
| 106 | Arc::new(AtomicBool::new(false)), |
| 107 | ) |
| 108 | .unwrap(); |
| 109 | } |
| 110 | } |