| Lift freeq's AV media plane out of sleek 90f8b89 nandi 19d ago | 1 | use std::convert::TryInto; |
| 2 | use std::time::Duration; |
| 3 | |
| 4 | extern crate ndk; |
| 5 | |
| 6 | use crate::{ |
| 7 | BackendSpecificError, BuildStreamError, PauseStreamError, PlayStreamError, StreamError, |
| 8 | StreamInstant, |
| 9 | }; |
| 10 | |
| 11 | 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 | |
| 18 | pub fn stream_instant(stream: &ndk::audio::AudioStream) -> StreamInstant { |
| 19 | let ts = stream |
| 20 | .timestamp(ndk::audio::Clockid::Monotonic) |
| 21 | .unwrap_or(ndk::audio::Timestamp { |
| 22 | frame_position: 0, |
| 23 | time_nanoseconds: 0, |
| 24 | }); |
| 25 | to_stream_instant(Duration::from_nanos(ts.time_nanoseconds as u64)) |
| 26 | } |
| 27 | |
| 28 | impl From<ndk::audio::AudioError> for StreamError { |
| 29 | fn from(error: ndk::audio::AudioError) -> Self { |
| 30 | use self::ndk::audio::AudioError::*; |
| 31 | match error { |
| 32 | Disconnected | Unavailable => Self::DeviceNotAvailable, |
| 33 | e => (BackendSpecificError { |
| 34 | description: e.to_string(), |
| 35 | }) |
| 36 | .into(), |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | impl From<ndk::audio::AudioError> for PlayStreamError { |
| 42 | fn from(error: ndk::audio::AudioError) -> Self { |
| 43 | use self::ndk::audio::AudioError::*; |
| 44 | match error { |
| 45 | Disconnected | Unavailable => Self::DeviceNotAvailable, |
| 46 | e => (BackendSpecificError { |
| 47 | description: e.to_string(), |
| 48 | }) |
| 49 | .into(), |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | impl From<ndk::audio::AudioError> for PauseStreamError { |
| 55 | fn from(error: ndk::audio::AudioError) -> Self { |
| 56 | use self::ndk::audio::AudioError::*; |
| 57 | match error { |
| 58 | Disconnected | Unavailable => Self::DeviceNotAvailable, |
| 59 | e => (BackendSpecificError { |
| 60 | description: e.to_string(), |
| 61 | }) |
| 62 | .into(), |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | impl From<ndk::audio::AudioError> for BuildStreamError { |
| 68 | fn from(error: ndk::audio::AudioError) -> Self { |
| 69 | use self::ndk::audio::AudioError::*; |
| 70 | match error { |
| 71 | Disconnected | Unavailable => Self::DeviceNotAvailable, |
| 72 | NoFreeHandles => Self::StreamIdOverflow, |
| 73 | InvalidFormat | InvalidRate => Self::StreamConfigNotSupported, |
| 74 | IllegalArgument => Self::InvalidArgument, |
| 75 | e => (BackendSpecificError { |
| 76 | description: e.to_string(), |
| 77 | }) |
| 78 | .into(), |
| 79 | } |
| 80 | } |
| 81 | } |