moosicbox_audio_output 0.2.0

MoosicBox audio outputs package
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Command-based control interface for audio outputs.
//!
//! This module provides an async command system for controlling audio playback through
//! message passing. Audio outputs can be controlled from different threads or async contexts
//! using [`AudioHandle`], which sends [`AudioCommand`]s and receives [`AudioResponse`]s.

#![allow(clippy::module_name_repetitions)]

use std::fmt;
use thiserror::Error;

/// Commands that can be sent to control audio output.
///
/// These commands are sent through an [`AudioHandle`] to control playback.
#[derive(Debug, Clone)]
pub enum AudioCommand {
    /// Set the volume level (0.0 to 1.0)
    SetVolume(f64),
    /// Pause audio playback
    Pause,
    /// Resume audio playback
    Resume,
    /// Seek to the specified position in seconds
    Seek(f64),
    /// Flush buffered audio data
    Flush,
    /// Reset the audio output to its initial state
    Reset,
}

/// Response returned after executing an audio command.
///
/// Indicates whether the command was processed successfully or failed.
#[derive(Debug, Clone)]
pub enum AudioResponse {
    /// Command executed successfully
    Success,
    /// Command execution failed with error message
    Error(String),
}

/// Message structure for sending commands through channels.
///
/// Contains the command to execute and optionally a channel for receiving the response.
#[derive(Debug)]
pub struct CommandMessage {
    /// The command to execute
    pub command: AudioCommand,
    /// Optional channel for sending back the response
    pub response_sender: Option<flume::Sender<AudioResponse>>,
}

/// Errors that can occur during audio command operations.
///
/// These errors indicate failures in sending commands or receiving responses.
#[derive(Debug, Error)]
pub enum AudioError {
    /// Command execution failed with error message
    #[error("Command error: {0}")]
    Command(String),
    /// Failed to send command through channel
    #[error("Channel send error")]
    ChannelSend,
    /// Failed to receive response through channel
    #[error("Channel receive error")]
    ChannelReceive,
    /// Received unexpected response type
    #[error("Unexpected response type")]
    UnexpectedResponse,
    /// Audio handle is not available
    #[error("Handle not available")]
    HandleNotAvailable,
}

impl From<flume::SendError<CommandMessage>> for AudioError {
    fn from(_: flume::SendError<CommandMessage>) -> Self {
        Self::ChannelSend
    }
}

impl From<flume::TrySendError<CommandMessage>> for AudioError {
    fn from(_: flume::TrySendError<CommandMessage>) -> Self {
        Self::ChannelSend
    }
}

impl From<flume::RecvError> for AudioError {
    fn from(_: flume::RecvError) -> Self {
        Self::ChannelReceive
    }
}

/// Handle for sending commands to an audio output from different threads or async contexts.
///
/// The handle uses channels to communicate with the audio output's command processor,
/// allowing safe control of playback from anywhere in the application.
pub struct AudioHandle {
    command_sender: flume::Sender<CommandMessage>,
}

impl fmt::Debug for AudioHandle {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("AudioHandle")
            .field("command_sender", &"<flume::Sender>")
            .finish()
    }
}

impl Clone for AudioHandle {
    fn clone(&self) -> Self {
        Self {
            command_sender: self.command_sender.clone(),
        }
    }
}

impl AudioHandle {
    /// Creates a new `AudioHandle` with the specified command sender.
    ///
    /// # Arguments
    /// * `command_sender` - Channel for sending commands to the audio output
    #[must_use]
    pub const fn new(command_sender: flume::Sender<CommandMessage>) -> Self {
        Self { command_sender }
    }

    /// Set the volume of the audio output
    ///
    /// # Errors
    ///
    /// * If the command processor fails to send the command
    /// * If the `AudioCommand::SetVolume` command fails to be processed by the command processor
    pub async fn set_volume(&self, volume: f64) -> Result<(), AudioError> {
        self.send_command_with_response(AudioCommand::SetVolume(volume))
            .await?;
        Ok(())
    }

    /// Pause the audio output
    ///
    /// # Errors
    ///
    /// * If the command processor fails to send the command
    /// * If the `AudioCommand::Pause` command fails to be processed by the command processor
    pub async fn pause(&self) -> Result<(), AudioError> {
        self.send_command_with_response(AudioCommand::Pause).await?;
        Ok(())
    }

    /// Resume the audio output
    ///
    /// # Errors
    ///
    /// * If the command processor fails to send the command
    /// * If the `AudioCommand::Resume` command fails to be processed by the command processor
    pub async fn resume(&self) -> Result<(), AudioError> {
        self.send_command_with_response(AudioCommand::Resume)
            .await?;
        Ok(())
    }

    /// Seek to the specified position in the audio output
    ///
    /// # Errors
    ///
    /// * If the command processor fails to send the command
    /// * If the `AudioCommand::Seek` command fails to be processed by the command processor
    pub async fn seek(&self, position: f64) -> Result<(), AudioError> {
        self.send_command_with_response(AudioCommand::Seek(position))
            .await?;
        Ok(())
    }

    /// Flush the audio output
    ///
    /// # Errors
    ///
    /// * If the command processor fails to send the command
    /// * If the `AudioCommand::Flush` command fails to be processed by the command processor
    pub async fn flush(&self) -> Result<(), AudioError> {
        self.send_command_with_response(AudioCommand::Flush).await?;
        Ok(())
    }

    /// Reset the audio output
    ///
    /// # Errors
    ///
    /// * If the command processor fails to send the command
    /// * If the `AudioCommand::Reset` command fails to be processed by the command processor
    pub async fn reset(&self) -> Result<(), AudioError> {
        self.send_command_with_response(AudioCommand::Reset).await?;
        Ok(())
    }

    /// Immediately set the volume of the audio output
    ///
    /// This is a fire-and-forget command that does not wait for a response.
    /// It is intended to be used in situations where the caller wants to set the volume of the audio output
    /// immediately, without waiting for the response from the command processor.
    ///
    /// # Errors
    ///
    /// * If the command processor fails to send the command
    pub fn set_volume_immediate(&self, volume: f64) -> Result<(), AudioError> {
        self.send_command_fire_and_forget(AudioCommand::SetVolume(volume))
    }

    /// Immediately pause the audio output
    ///
    /// This is a fire-and-forget command that does not wait for a response.
    /// It is intended to be used in situations where the caller wants to pause the audio output
    /// immediately, without waiting for the response from the command processor.
    ///
    /// # Errors
    ///
    /// * If the command processor fails to send the command
    pub fn pause_immediate(&self) -> Result<(), AudioError> {
        self.send_command_fire_and_forget(AudioCommand::Pause)
    }

    /// Immediately resume the audio output
    ///
    /// This is a fire-and-forget command that does not wait for a response.
    /// It is intended to be used in situations where the caller wants to resume the audio output
    /// immediately, without waiting for the response from the command processor.
    ///
    /// # Errors
    ///
    /// * If the command processor fails to send the command
    pub fn resume_immediate(&self) -> Result<(), AudioError> {
        self.send_command_fire_and_forget(AudioCommand::Resume)
    }

    async fn send_command_with_response(
        &self,
        command: AudioCommand,
    ) -> Result<AudioResponse, AudioError> {
        let (response_tx, response_rx) = flume::bounded(1);
        self.command_sender
            .send_async(CommandMessage {
                command,
                response_sender: Some(response_tx),
            })
            .await?;

        response_rx.recv_async().await.map_err(AudioError::from)
    }

    fn send_command_fire_and_forget(&self, command: AudioCommand) -> Result<(), AudioError> {
        self.command_sender
            .try_send(CommandMessage {
                command,
                response_sender: None,
            })
            .map_err(AudioError::from)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test_log::test]
    fn test_audio_handle_fire_and_forget_success() {
        let (tx, rx) = flume::bounded(10);
        let handle = AudioHandle::new(tx);

        let result = handle.set_volume_immediate(0.5);
        assert!(result.is_ok());

        let msg = rx.try_recv().unwrap();
        assert!(matches!(msg.command, AudioCommand::SetVolume(v) if (v - 0.5).abs() < 0.001));
        assert!(msg.response_sender.is_none());
    }

    #[test_log::test]
    fn test_audio_handle_pause_immediate() {
        let (tx, rx) = flume::bounded(10);
        let handle = AudioHandle::new(tx);

        let result = handle.pause_immediate();
        assert!(result.is_ok());

        let msg = rx.try_recv().unwrap();
        assert!(matches!(msg.command, AudioCommand::Pause));
        assert!(msg.response_sender.is_none());
    }

    #[test_log::test]
    fn test_audio_handle_resume_immediate() {
        let (tx, rx) = flume::bounded(10);
        let handle = AudioHandle::new(tx);

        let result = handle.resume_immediate();
        assert!(result.is_ok());

        let msg = rx.try_recv().unwrap();
        assert!(matches!(msg.command, AudioCommand::Resume));
        assert!(msg.response_sender.is_none());
    }

    #[test_log::test]
    fn test_audio_handle_fire_and_forget_channel_full() {
        let (tx, _rx) = flume::bounded(0);
        let handle = AudioHandle::new(tx);

        let result = handle.set_volume_immediate(0.5);
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), AudioError::ChannelSend));
    }

    #[test_log::test(switchy_async::test)]
    async fn test_audio_handle_set_volume() {
        let (tx, rx) = flume::bounded(10);
        let handle = AudioHandle::new(tx);

        // Spawn a task to respond
        switchy_async::task::spawn(async move {
            let msg = rx.recv_async().await.unwrap();
            if let Some(resp_tx) = msg.response_sender {
                resp_tx.send_async(AudioResponse::Success).await.unwrap();
            }
        });

        let result = handle.set_volume(0.5).await;
        assert!(result.is_ok());
    }

    #[test_log::test(switchy_async::test)]
    async fn test_audio_handle_pause() {
        let (tx, rx) = flume::bounded(10);
        let handle = AudioHandle::new(tx);

        switchy_async::task::spawn(async move {
            let msg = rx.recv_async().await.unwrap();
            if let Some(resp_tx) = msg.response_sender {
                resp_tx.send_async(AudioResponse::Success).await.unwrap();
            }
        });

        let result = handle.pause().await;
        assert!(result.is_ok());
    }

    #[test_log::test(switchy_async::test)]
    async fn test_audio_handle_resume() {
        let (tx, rx) = flume::bounded(10);
        let handle = AudioHandle::new(tx);

        switchy_async::task::spawn(async move {
            let msg = rx.recv_async().await.unwrap();
            if let Some(resp_tx) = msg.response_sender {
                resp_tx.send_async(AudioResponse::Success).await.unwrap();
            }
        });

        let result = handle.resume().await;
        assert!(result.is_ok());
    }

    #[test_log::test(switchy_async::test)]
    async fn test_audio_handle_seek() {
        let (tx, rx) = flume::bounded(10);
        let handle = AudioHandle::new(tx);

        switchy_async::task::spawn(async move {
            let msg = rx.recv_async().await.unwrap();
            assert!(matches!(msg.command, AudioCommand::Seek(pos) if (pos - 10.5).abs() < 0.001));
            if let Some(resp_tx) = msg.response_sender {
                resp_tx.send_async(AudioResponse::Success).await.unwrap();
            }
        });

        let result = handle.seek(10.5).await;
        assert!(result.is_ok());
    }

    #[test_log::test(switchy_async::test)]
    async fn test_audio_handle_flush() {
        let (tx, rx) = flume::bounded(10);
        let handle = AudioHandle::new(tx);

        switchy_async::task::spawn(async move {
            let msg = rx.recv_async().await.unwrap();
            assert!(matches!(msg.command, AudioCommand::Flush));
            if let Some(resp_tx) = msg.response_sender {
                resp_tx.send_async(AudioResponse::Success).await.unwrap();
            }
        });

        let result = handle.flush().await;
        assert!(result.is_ok());
    }

    #[test_log::test(switchy_async::test)]
    async fn test_audio_handle_reset() {
        let (tx, rx) = flume::bounded(10);
        let handle = AudioHandle::new(tx);

        switchy_async::task::spawn(async move {
            let msg = rx.recv_async().await.unwrap();
            assert!(matches!(msg.command, AudioCommand::Reset));
            if let Some(resp_tx) = msg.response_sender {
                resp_tx.send_async(AudioResponse::Success).await.unwrap();
            }
        });

        let result = handle.reset().await;
        assert!(result.is_ok());
    }

    #[test_log::test(switchy_async::test)]
    async fn test_audio_handle_channel_closed() {
        let (tx, rx) = flume::bounded(10);
        let handle = AudioHandle::new(tx);

        drop(rx);

        let result = handle.set_volume(0.5).await;
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), AudioError::ChannelSend));
    }

    #[test_log::test(switchy_async::test)]
    async fn test_audio_handle_response_channel_closed() {
        let (tx, rx) = flume::bounded(10);
        let handle = AudioHandle::new(tx);

        switchy_async::task::spawn(async move {
            let _msg = rx.recv_async().await.unwrap();
            // Don't send response, just drop the response sender
        });

        let result = handle.set_volume(0.5).await;
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), AudioError::ChannelReceive));
    }
}