1 use std::convert::TryInto;
2 use std::time::Duration;
3 
4 extern crate oboe;
5 
6 use crate::{
7     BackendSpecificError, BuildStreamError, PauseStreamError, PlayStreamError, StreamError,
8     StreamInstant,
9 };
10 
to_stream_instant(duration: Duration) -> StreamInstant11 pub fn to_stream_instant(duration: Duration) -> StreamInstant {
12     StreamInstant::new(
13         duration.as_secs().try_into().unwrap(),
14         duration.subsec_nanos(),
15     )
16 }
17 
stream_instant<T: oboe::AudioStreamSafe + ?Sized>(stream: &mut T) -> StreamInstant18 pub fn stream_instant<T: oboe::AudioStreamSafe + ?Sized>(stream: &mut T) -> StreamInstant {
19     const CLOCK_MONOTONIC: i32 = 1;
20     let ts = stream
21         .get_timestamp(CLOCK_MONOTONIC)
22         .unwrap_or(oboe::FrameTimestamp {
23             position: 0,
24             timestamp: 0,
25         });
26     to_stream_instant(Duration::from_nanos(ts.timestamp as u64))
27 }
28 
29 impl From<oboe::Error> for StreamError {
from(error: oboe::Error) -> Self30     fn from(error: oboe::Error) -> Self {
31         use self::oboe::Error::*;
32         match error {
33             Disconnected | Unavailable | Closed => Self::DeviceNotAvailable,
34             e => (BackendSpecificError {
35                 description: e.to_string(),
36             })
37             .into(),
38         }
39     }
40 }
41 
42 impl From<oboe::Error> for PlayStreamError {
from(error: oboe::Error) -> Self43     fn from(error: oboe::Error) -> Self {
44         use self::oboe::Error::*;
45         match error {
46             Disconnected | Unavailable | Closed => Self::DeviceNotAvailable,
47             e => (BackendSpecificError {
48                 description: e.to_string(),
49             })
50             .into(),
51         }
52     }
53 }
54 
55 impl From<oboe::Error> for PauseStreamError {
from(error: oboe::Error) -> Self56     fn from(error: oboe::Error) -> Self {
57         use self::oboe::Error::*;
58         match error {
59             Disconnected | Unavailable | Closed => Self::DeviceNotAvailable,
60             e => (BackendSpecificError {
61                 description: e.to_string(),
62             })
63             .into(),
64         }
65     }
66 }
67 
68 impl From<oboe::Error> for BuildStreamError {
from(error: oboe::Error) -> Self69     fn from(error: oboe::Error) -> Self {
70         use self::oboe::Error::*;
71         match error {
72             Disconnected | Unavailable | Closed => Self::DeviceNotAvailable,
73             NoFreeHandles => Self::StreamIdOverflow,
74             InvalidFormat | InvalidRate => Self::StreamConfigNotSupported,
75             IllegalArgument => Self::InvalidArgument,
76             e => (BackendSpecificError {
77                 description: e.to_string(),
78             })
79             .into(),
80         }
81     }
82 }
83