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
use std::ops::Deref; use super::codec::Codec; use ffi::*; use {format, ChannelLayout}; #[derive(PartialEq, Eq, Copy, Clone)] pub struct Audio { codec: Codec, } impl Audio { pub unsafe fn new(codec: Codec) -> Audio { Audio { codec } } } impl Audio { pub fn rates(&self) -> Option<RateIter> { unsafe { if (*self.as_ptr()).supported_samplerates.is_null() { None } else { Some(RateIter::new((*self.codec.as_ptr()).supported_samplerates)) } } } pub fn formats(&self) -> Option<FormatIter> { unsafe { if (*self.codec.as_ptr()).sample_fmts.is_null() { None } else { Some(FormatIter::new((*self.codec.as_ptr()).sample_fmts)) } } } pub fn channel_layouts(&self) -> Option<ChannelLayoutIter> { unsafe { if (*self.codec.as_ptr()).channel_layouts.is_null() { None } else { Some(ChannelLayoutIter::new( (*self.codec.as_ptr()).channel_layouts, )) } } } } impl Deref for Audio { type Target = Codec; fn deref(&self) -> &Self::Target { &self.codec } } pub struct RateIter { ptr: *const i32, } impl RateIter { pub fn new(ptr: *const i32) -> Self { RateIter { ptr } } } impl Iterator for RateIter { type Item = i32; fn next(&mut self) -> Option<<Self as Iterator>::Item> { unsafe { if *self.ptr == 0 { return None; } let rate = *self.ptr; self.ptr = self.ptr.offset(1); Some(rate) } } } pub struct FormatIter { ptr: *const AVSampleFormat, } impl FormatIter { pub fn new(ptr: *const AVSampleFormat) -> Self { FormatIter { ptr } } } impl Iterator for FormatIter { type Item = format::Sample; fn next(&mut self) -> Option<<Self as Iterator>::Item> { unsafe { if *self.ptr == AVSampleFormat::AV_SAMPLE_FMT_NONE { return None; } let format = (*self.ptr).into(); self.ptr = self.ptr.offset(1); Some(format) } } } pub struct ChannelLayoutIter { ptr: *const u64, } impl ChannelLayoutIter { pub fn new(ptr: *const u64) -> Self { ChannelLayoutIter { ptr } } pub fn best(self, max: i32) -> ChannelLayout { self.fold(ChannelLayout::MONO, |acc, cur| { if cur.channels() > acc.channels() && cur.channels() <= max { cur } else { acc } }) } } impl Iterator for ChannelLayoutIter { type Item = ChannelLayout; fn next(&mut self) -> Option<<Self as Iterator>::Item> { unsafe { if *self.ptr == 0 { return None; } let layout = ChannelLayout::from_bits_truncate(*self.ptr); self.ptr = self.ptr.offset(1); Some(layout) } } }