-
Notifications
You must be signed in to change notification settings - Fork 57
/
audiodecoder.rs
86 lines (72 loc) · 2.45 KB
/
audiodecoder.rs
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
// Copyright 2015 The Servo Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use codecs::aac::AacHeaders;
use codecs::vorbis::{self, VorbisHeaders};
use libc::c_int;
#[cfg(feature="ffmpeg")]
use codecs::libavcodec;
#[cfg(target_os="macos")]
use platform;
pub trait AudioHeaders {
fn vorbis_headers<'a>(&'a self) -> Option<&'a VorbisHeaders> {
None
}
fn aac_headers<'a>(&'a self) -> Option<&'a AacHeaders> {
None
}
}
pub trait AudioDecoderInfo {
fn create_decoder(self: Box<Self>) -> Box<AudioDecoder + 'static>;
}
pub trait AudioDecoder {
fn decode(&mut self, data: &[u8]) -> Result<(),()>;
fn decoded_samples<'a>(&'a mut self) -> Result<Box<DecodedAudioSamples + 'a>,()>;
fn acknowledge(&mut self, sample_count: c_int);
}
pub trait DecodedAudioSamples {
fn samples<'a>(&'a self, channel: i32) -> Option<&'a [f32]>;
}
/// For codecs that require no headers, or as a placeholder.
#[derive(Copy, Clone)]
pub struct EmptyAudioHeadersImpl;
impl AudioHeaders for EmptyAudioHeadersImpl {}
#[allow(missing_copy_implementations)]
pub struct RegisteredAudioDecoder {
pub id: [u8; 4],
pub constructor: extern "Rust" fn(headers: &AudioHeaders, sample_rate: f64, channels: u16)
-> Box<AudioDecoderInfo + 'static>,
}
impl RegisteredAudioDecoder {
pub fn get(codec_id: &[u8]) -> Result<&'static RegisteredAudioDecoder,()> {
for decoder in AUDIO_DECODERS.iter() {
if decoder.id == codec_id {
return Ok(decoder)
}
}
Err(())
}
pub fn new(&self, headers: &AudioHeaders, sample_rate: f64, channels: u16)
-> Box<AudioDecoderInfo + 'static> {
(self.constructor)(headers, sample_rate, channels)
}
pub fn id(&self) -> [u8; 4] {
self.id
}
}
pub static AUDIO_DECODERS: [RegisteredAudioDecoder;
1 +
cfg!(target_os="macos") as usize +
cfg!(feature="ffmpeg") as usize
] = [
vorbis::AUDIO_DECODER,
#[cfg(target_os="macos")]
platform::macos::audiounit::AUDIO_DECODER,
#[cfg(feature="ffmpeg")]
libavcodec::AUDIO_DECODER,
];