crabshell_services/audio/
sound.rs1use std::collections::HashSet;
5use std::io::Read;
6use std::ops::Range;
7use std::path::{Path, PathBuf};
8use std::sync::Arc;
9
10use libpulse_binding::channelmap::{Map, MapDef};
11use libpulse_binding::context;
12use libpulse_binding::operation::{self, Operation};
13use libpulse_binding::proplist::{Proplist, properties};
14use libpulse_binding::sample::{Format, Spec};
15use libpulse_binding::stream::{FlagSet, SeekMode, State, Stream};
16
17use super::{Audio, AudioService, Event, Locked, Pulse, on_pa_thread};
18use crate::file::{MAX_FILE, open_file};
19
20const MAX_CHANNELS: u16 = 8;
22
23#[derive(Debug, PartialEq)]
25pub(super) struct Wav {
26 pub(super) spec: Spec,
27 pub(super) data: Range<usize>,
29}
30
31pub(super) fn parse(bytes: &[u8]) -> Result<Wav, &'static str> {
36 if bytes.len() < 12 || &bytes[..4] != b"RIFF" || &bytes[8..12] != b"WAVE" {
37 return Err("it isn't a WAV file");
38 }
39 let (mut fmt, mut data) = (None, None);
40 let mut at = 12;
41 while let Some(header) = bytes.get(at..at + 8) {
42 let size = u32::from_le_bytes([header[4], header[5], header[6], header[7]]) as usize;
43 let start = at + 8;
44 let end = start.checked_add(size).filter(|&end| end <= bytes.len());
45 match (&header[..4], end) {
46 (b"fmt ", Some(end)) => fmt = Some(&bytes[start..end]),
47 (b"data", Some(end)) => data = Some(start..end),
48 (b"data", None) => return Err("its data runs past the end of the file"),
49 (_, None) => break,
50 _ => {}
51 }
52 at = start + size + size % 2;
54 }
55 let fmt = fmt.ok_or("it has no format chunk")?;
56 let data = data.ok_or("it has no data chunk")?;
57 let field = |at: usize| {
58 fmt
59 .get(at..at + 2)
60 .map(|b| u16::from_le_bytes([b[0], b[1]]))
61 };
62 let (Some(mut tag), Some(channels), Some(bits)) = (field(0), field(2), field(14)) else {
63 return Err("its format chunk is too short");
64 };
65 let rate = fmt
66 .get(4..8)
67 .map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]]))
68 .ok_or("its format chunk is too short")?;
69 if tag == 0xfffe {
70 tag = field(24).ok_or("its extensible format chunk is too short")?;
72 }
73 let format = match (tag, bits) {
76 (1, 8) => Format::U8,
77 (1, 16) => Format::S16le,
78 (1, 24) => Format::S24le,
79 (1, 32) => Format::S32le,
80 (3, 32) => Format::F32le,
81 _ => return Err("it isn't 8, 16, 24 or 32-bit PCM or 32-bit float"),
82 };
83 if !(1..=MAX_CHANNELS).contains(&channels) {
84 return Err("it doesn't have 1 to 8 channels");
85 }
86 let spec = Spec {
87 format,
88 rate,
89 channels: channels as u8,
90 };
91 if !spec.is_valid() {
92 return Err("libpulse doesn't take its rate");
93 }
94 let frame = spec.frame_size();
95 let whole = data.len() - data.len() % frame;
96 Ok(Wav {
97 spec,
98 data: data.start..data.start + whole,
99 })
100}
101
102fn read(path: &Path) -> Result<Vec<u8>, String> {
105 let file = open_file(path).map_err(|e| e.message)?;
106 let mut bytes = Vec::new();
107 file
108 .take(MAX_FILE + 1)
109 .read_to_end(&mut bytes)
110 .map_err(|e| e.to_string())?;
111 if bytes.len() as u64 > MAX_FILE {
112 return Err("it is over 16 MiB".into());
113 }
114 Ok(bytes)
115}
116
117impl Audio {
118 pub fn play_sound(&self, path: impl AsRef<Path>) {
124 let path = path.as_ref();
125 let Some(service) = &self.service else {
126 return tracing::debug!("no audio service: no sound");
127 };
128 let sound = read(path).and_then(|bytes| {
129 let wav = parse(&bytes)?;
130 Ok((bytes, wav))
131 });
132 let (bytes, wav) = match sound {
133 Ok(sound) => sound,
134 Err(why) => return service.refused(path, &why),
135 };
136 let mut pulse = service.pulse.borrow_mut();
137 let played = match pulse.as_mut() {
138 Some(pulse) if pulse.intro.is_some() => pulse.play(bytes, wav),
139 Some(pulse) if pulse.down => Err("there is no audio server".to_owned()),
140 _ if service.shared.config.audio.is_some() => {
143 let mut waiting = service.waiting_sounds.borrow_mut();
144 if waiting.len() < MAX_WAITING {
145 waiting.push((bytes, wav));
146 return;
147 }
148 Err(format!(
149 "{MAX_WAITING} sounds already wait for the audio server"
150 ))
151 }
152 _ => Err("there is no audio server".to_owned()),
153 };
154 if let Err(why) = played {
155 tracing::info!(path = %path.display(), why, "a sound didn't play");
156 }
157 }
158}
159
160impl AudioService {
161 pub(super) fn play_waiting(&self, pulse: &mut Pulse) {
163 for (bytes, wav) in self.waiting_sounds.take() {
164 if let Err(why) = pulse.play(bytes, wav) {
165 tracing::info!(why, "a sound didn't play");
166 }
167 }
168 }
169
170 pub(super) fn drop_waiting(&self) {
172 let dropped = self.waiting_sounds.take().len();
173 if dropped > 0 {
174 tracing::info!(dropped, "sounds waiting for the audio server didn't play");
175 }
176 }
177
178 fn refused(&self, path: &Path, why: &str) {
180 if self.bad_sounds.borrow_mut().insert(path.to_owned()) {
181 tracing::warn!(path = %path.display(), why, "can't play a sound");
182 }
183 }
184}
185
186pub(super) type BadSounds = HashSet<PathBuf>;
188
189const MAX_WAITING: usize = 4;
191
192pub(super) type Waiting = Vec<(Vec<u8>, Wav)>;
194
195pub(super) struct Sound {
197 id: u64,
198 stream: Playing,
199 bytes: Vec<u8>,
200 frame: usize,
202 left: Range<usize>,
204 draining: bool,
205}
206
207struct Playing {
212 stream: Stream,
213 drain: Option<Operation<dyn FnMut(bool)>>,
214}
215
216impl Drop for Playing {
217 fn drop(&mut self) {
218 if let Some(drain) = self.drain.as_mut()
220 && drain.get_state() == operation::State::Running
221 {
222 drain.cancel();
223 }
224 self.stream.set_write_callback(None);
225 self.stream.set_state_callback(None);
226 }
227}
228
229impl Pulse {
230 fn play(&mut self, bytes: Vec<u8>, wav: Wav) -> Result<(), String> {
232 let lock = Locked::new(&mut self.mainloop);
233 let context = self
234 .context
235 .as_mut()
236 .filter(|context| context.get_state() == context::State::Ready)
237 .ok_or("the audio server isn't connected")?;
238 let mut map = Map::default();
239 map
240 .init_auto(wav.spec.channels, MapDef::WAVEEx)
241 .ok_or("libpulse has no channel map for it")?;
242 let mut properties = Proplist::new().ok_or("libpulse made no property list")?;
243 let _ = properties.set_str(properties::MEDIA_ROLE, "event");
244 let mut stream =
245 Stream::new_with_proplist(context, "Sound", &wav.spec, Some(&map), &mut properties)
246 .ok_or("libpulse made no stream")?;
247 stream
248 .connect_playback(None, None, FlagSet::NOFLAGS, None, None)
249 .map_err(|error| format!("the audio server refused it: {error}"))?;
250 let id = self.next_sound;
251 self.next_sound += 1;
252 let (sender, generation) = (Arc::clone(&self.sender), self.generation);
253 let on_write = on_pa_thread(move |_| {
254 let _ = sender.send((generation, Event::Sound(id)));
255 });
256 stream.set_write_callback(Some(Box::new(on_write)));
257 let sender = Arc::clone(&self.sender);
258 let on_state = on_pa_thread(move || {
259 let _ = sender.send((generation, Event::Sound(id)));
260 });
261 stream.set_state_callback(Some(Box::new(on_state)));
262 drop(lock);
263 self.sounds.push(Sound {
264 id,
265 stream: Playing {
266 stream,
267 drain: None,
268 },
269 frame: wav.spec.frame_size(),
270 left: wav.data,
271 bytes,
272 draining: false,
273 });
274 Ok(())
275 }
276
277 pub(super) fn feed_sound(&mut self, id: u64) {
280 let _lock = Locked::new(&mut self.mainloop);
281 let Some(at) = self.sounds.iter().position(|sound| sound.id == id) else {
282 return;
283 };
284 let sound = &mut self.sounds[at];
285 let stream = &mut sound.stream.stream;
286 match stream.get_state() {
287 State::Ready => {}
288 State::Failed | State::Terminated => {
289 tracing::info!("the audio server ended a sound");
290 self.sounds.swap_remove(at);
291 return;
292 }
293 _ => return,
294 }
295 let room = stream.writable_size().unwrap_or(0);
296 let n = sound.left.len().min(room - room % sound.frame);
297 if n > 0 {
298 let chunk = &sound.bytes[sound.left.start..sound.left.start + n];
299 if stream.write(chunk, None, 0, SeekMode::Relative).is_err() {
300 tracing::info!("a sound's stream took no more samples");
301 self.sounds.swap_remove(at);
302 return;
303 }
304 sound.left.start += n;
305 }
306 if sound.left.is_empty() && !sound.draining {
307 sound.draining = true;
308 let (sender, generation) = (Arc::clone(&self.sender), self.generation);
309 let on_drained = on_pa_thread(move |_| {
310 let _ = sender.send((generation, Event::SoundDone(id)));
311 });
312 sound.stream.drain = Some(stream.drain(Some(Box::new(on_drained))));
313 }
314 }
315
316 pub(super) fn sound_done(&mut self, id: u64) {
318 let _lock = Locked::new(&mut self.mainloop);
319 self.sounds.retain(|sound| sound.id != id);
320 }
321}