Skip to main content

livekit/platform_audio/
error.rs

1// Copyright 2026 LiveKit, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Error types for platform audio operations.
16
17use std::fmt;
18
19/// Errors that can occur during audio operations.
20#[derive(Debug, Clone, PartialEq, Eq)]
21pub enum AudioError {
22    /// Platform ADM could not be initialized.
23    ///
24    /// This can happen if:
25    /// - No audio devices are available
26    /// - Audio permissions are not granted
27    /// - Platform audio subsystem is unavailable
28    PlatformInitFailed,
29
30    /// The specified device index is invalid.
31    ///
32    /// Device indices are 0-based and must be less than the device count.
33    InvalidDeviceIndex,
34
35    /// The specified device GUID was not found.
36    ///
37    /// The device may have been disconnected or the GUID may be invalid.
38    DeviceNotFound,
39
40    /// An audio operation failed.
41    OperationFailed(String),
42}
43
44impl fmt::Display for AudioError {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        match self {
47            AudioError::PlatformInitFailed => {
48                write!(f, "Failed to initialize platform audio")
49            }
50            AudioError::InvalidDeviceIndex => write!(f, "Invalid device index"),
51            AudioError::DeviceNotFound => write!(f, "Device not found"),
52            AudioError::OperationFailed(msg) => write!(f, "Audio operation failed: {}", msg),
53        }
54    }
55}
56
57impl std::error::Error for AudioError {}
58
59/// Result type for audio operations.
60pub type AudioResult<T> = Result<T, AudioError>;
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn audio_error_display() {
68        let err = AudioError::PlatformInitFailed;
69        let msg = format!("{}", err);
70        assert!(msg.contains("platform audio"));
71
72        let err = AudioError::InvalidDeviceIndex;
73        let msg = format!("{}", err);
74        assert!(msg.contains("Invalid device index"));
75
76        let err = AudioError::OperationFailed("test message".to_string());
77        let msg = format!("{}", err);
78        assert!(msg.contains("test message"));
79    }
80
81    #[test]
82    fn audio_error_debug() {
83        let err = AudioError::PlatformInitFailed;
84        let debug_str = format!("{:?}", err);
85        assert!(debug_str.contains("PlatformInitFailed"));
86
87        let err = AudioError::InvalidDeviceIndex;
88        let debug_str = format!("{:?}", err);
89        assert!(debug_str.contains("InvalidDeviceIndex"));
90    }
91
92    #[test]
93    fn audio_error_equality() {
94        assert_eq!(AudioError::PlatformInitFailed, AudioError::PlatformInitFailed);
95        assert_eq!(AudioError::InvalidDeviceIndex, AudioError::InvalidDeviceIndex);
96        assert_eq!(
97            AudioError::OperationFailed("a".to_string()),
98            AudioError::OperationFailed("a".to_string())
99        );
100        assert_ne!(
101            AudioError::OperationFailed("a".to_string()),
102            AudioError::OperationFailed("b".to_string())
103        );
104    }
105
106    #[test]
107    fn audio_error_clone() {
108        let err = AudioError::OperationFailed("test".to_string());
109        let cloned = err.clone();
110        assert_eq!(err, cloned);
111    }
112
113    #[test]
114    fn audio_error_is_std_error() {
115        let err: Box<dyn std::error::Error> = Box::new(AudioError::InvalidDeviceIndex);
116        assert!(err.to_string().contains("Invalid device index"));
117    }
118
119    #[test]
120    fn audio_result_ok() {
121        let result: AudioResult<i32> = Ok(42);
122        assert!(result.is_ok());
123        assert_eq!(result.unwrap(), 42);
124    }
125
126    #[test]
127    fn audio_result_err() {
128        let result: AudioResult<i32> = Err(AudioError::InvalidDeviceIndex);
129        assert!(result.is_err());
130        assert_eq!(result.unwrap_err(), AudioError::InvalidDeviceIndex);
131    }
132}