335 lines
7.0 KiB
Rust
335 lines
7.0 KiB
Rust
use anyhow::{
|
|
anyhow,
|
|
Result,
|
|
};
|
|
|
|
use byteorder::{
|
|
ByteOrder,
|
|
LittleEndian,
|
|
};
|
|
|
|
// This crate flitters between being very convenient and being a type labyrinth.
|
|
|
|
use ffmpeg_next::{
|
|
decoder::Audio as DecodeContext,
|
|
format::context::Input as DemuxContext,
|
|
software::resampling::Context as ResamplingContext,
|
|
util::{
|
|
channel_layout::ChannelLayout,
|
|
format::sample::{
|
|
self,
|
|
Sample,
|
|
},
|
|
frame::Audio as FfAudioFrame,
|
|
},
|
|
};
|
|
|
|
pub const SAMPLE_RATE: u32 = 48000;
|
|
|
|
pub struct AudioFrame {
|
|
frame: FfAudioFrame,
|
|
}
|
|
|
|
impl AudioFrame {
|
|
pub fn data (&self) -> &[u8] {
|
|
// Hard-coded because I'm only support f32 interleaved stereo
|
|
&self.frame.data (0) [0..self.frame.samples () * 4 * 2]
|
|
}
|
|
|
|
pub fn rate (&self) -> u32 {
|
|
self.frame.rate ()
|
|
}
|
|
|
|
pub fn samples (&self) -> usize {
|
|
self.frame.samples ()
|
|
}
|
|
}
|
|
|
|
impl From <FfAudioFrame> for AudioFrame {
|
|
fn from (frame: FfAudioFrame) -> Self {
|
|
Self {
|
|
frame,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive (Default)]
|
|
pub struct PcmBuffers {
|
|
buffers: Vec <Vec <u8>>,
|
|
|
|
// Always points into the first buffer, if any
|
|
consumer_cursor: usize,
|
|
}
|
|
|
|
impl PcmBuffers {
|
|
pub fn samples_available (&self) -> usize {
|
|
(self.buffers.iter ().map (|b| b.len ()).sum::<usize> () - self.consumer_cursor) / 8
|
|
}
|
|
|
|
#[warn(unused_must_use)]
|
|
pub fn consume_exact (&mut self, data: &mut [f32]) -> bool {
|
|
if data.len () > self.samples_available () {
|
|
return false;
|
|
}
|
|
|
|
for x in data {
|
|
if self.consumer_cursor >= self.buffers [0].len () {
|
|
self.buffers.remove (0);
|
|
self.consumer_cursor = 0;
|
|
}
|
|
|
|
*x = LittleEndian::read_f32 (&self.buffers [0][self.consumer_cursor..]);
|
|
self.consumer_cursor += 4;
|
|
}
|
|
|
|
true
|
|
}
|
|
|
|
pub fn produce_bytes (&mut self, new_buffer: Vec <u8>) {
|
|
self.buffers.push (new_buffer);
|
|
}
|
|
}
|
|
|
|
pub struct Decoder <T> {
|
|
demuxer: T,
|
|
|
|
decoder: DecodeContext,
|
|
resampler: ResamplingContext,
|
|
|
|
dummy_frame: Option <FfAudioFrame>,
|
|
}
|
|
|
|
impl <T: Demuxer> Decoder <T> {
|
|
pub fn new (demuxer: T, codec: ffmpeg_next::codec::Context) -> Result <Self> {
|
|
let decoder = codec.decoder ().audio ()?;
|
|
let resampler = decoder.resampler (
|
|
Sample::F32 (sample::Type::Packed),
|
|
ChannelLayout::STEREO,
|
|
48000,
|
|
)?;
|
|
|
|
Ok (Self {
|
|
demuxer,
|
|
decoder,
|
|
resampler,
|
|
|
|
dummy_frame: None,
|
|
})
|
|
}
|
|
|
|
fn new_frame () -> FfAudioFrame {
|
|
let mut x = FfAudioFrame::empty ();
|
|
x.set_channel_layout (ChannelLayout::STEREO);
|
|
x.set_format (Sample::F32 (sample::Type::Packed));
|
|
x
|
|
}
|
|
|
|
pub fn next (&mut self) -> Result <Option <AudioFrame>> {
|
|
Ok (if let Some (frame) = self.pump ()? {
|
|
assert_eq! (frame.rate (), 48000);
|
|
assert! (frame.samples () > 0);
|
|
|
|
Some (frame)
|
|
}
|
|
else {
|
|
None
|
|
})
|
|
}
|
|
|
|
fn pump (&mut self) -> Result <Option <AudioFrame>> {
|
|
loop {
|
|
match self.pump_resampler ()? {
|
|
Some (x) => {
|
|
// tracing::trace! ("Pumped resampler");
|
|
return Ok (Some (x));
|
|
},
|
|
None => (),
|
|
}
|
|
|
|
match self.pump_decoder ()? {
|
|
Some (x) => {
|
|
// tracing::trace! ("Pumped decoder");
|
|
return Ok (Some (x));
|
|
},
|
|
None => (),
|
|
}
|
|
|
|
if self.pump_demuxer ()? {
|
|
// tracing::trace! ("Pumped demuxer");
|
|
continue;
|
|
}
|
|
else {
|
|
break;
|
|
}
|
|
}
|
|
|
|
Ok (None)
|
|
}
|
|
|
|
pub fn pump_resampler (&mut self) -> Result <Option <AudioFrame>> {
|
|
let frame_src = match self.dummy_frame.as_ref () {
|
|
None => return Ok (None),
|
|
Some (x) => x,
|
|
};
|
|
|
|
let mut frame_resampled = Self::new_frame ();
|
|
|
|
let _rc = self.resampler.run (&frame_src, &mut frame_resampled)?;
|
|
// dbg! (&frame_resampled, rc);
|
|
|
|
Ok (if frame_resampled.samples () > 0 {
|
|
// tracing::trace! ("Pulled from resampler FIFO");
|
|
Some (frame_resampled.into ())
|
|
}
|
|
else {
|
|
None
|
|
})
|
|
}
|
|
|
|
pub fn pump_decoder (&mut self) -> Result <Option <AudioFrame>> {
|
|
let mut frame_src = FfAudioFrame::empty ();
|
|
if let Err (_) = self.decoder.receive_frame (&mut frame_src) {
|
|
return Ok (None);
|
|
};
|
|
|
|
if self.dummy_frame.is_none () {
|
|
let mut dummy_frame = FfAudioFrame::new (
|
|
frame_src.format (),
|
|
0,
|
|
frame_src.channel_layout (),
|
|
);
|
|
dummy_frame.set_rate (frame_src.rate ());
|
|
self.dummy_frame = Some (dummy_frame);
|
|
}
|
|
|
|
let nb_output_samples = frame_src.samples ();
|
|
|
|
let mut frame_resampled = FfAudioFrame::new (
|
|
Sample::F32 (sample::Type::Packed),
|
|
nb_output_samples,
|
|
ChannelLayout::STEREO
|
|
);
|
|
|
|
self.resampler.run (&frame_src, &mut frame_resampled)?;
|
|
Ok (Some (frame_resampled.into ()))
|
|
}
|
|
|
|
pub fn pump_demuxer (&mut self) -> Result <bool> {
|
|
let packet = self.demuxer.pump ()?;
|
|
let packet = match packet {
|
|
Some (x) => x,
|
|
None => return Ok (false),
|
|
};
|
|
|
|
self.decoder.send_packet (&packet)?;
|
|
Ok (true)
|
|
}
|
|
}
|
|
|
|
pub trait Demuxer {
|
|
fn pump (&mut self) -> Result <Option <ffmpeg_next::Packet>>;
|
|
}
|
|
|
|
pub struct FfmpegDemuxer {
|
|
input_ctx: DemuxContext,
|
|
best_stream_idx: usize,
|
|
}
|
|
|
|
impl FfmpegDemuxer {
|
|
pub fn new (filename: &str) -> Result <(Self, ffmpeg_next::codec::Context)> {
|
|
let input_ctx = ffmpeg_next::format::input (&filename)?;
|
|
let stream = input_ctx
|
|
.streams ()
|
|
.best (ffmpeg_next::media::Type::Audio)
|
|
.ok_or_else (|| anyhow! ("can't find good audio stream"))?;
|
|
let best_stream_idx = stream.index ();
|
|
|
|
let codec = stream.codec ();
|
|
|
|
Ok ((Self {
|
|
input_ctx,
|
|
best_stream_idx,
|
|
}, codec))
|
|
}
|
|
}
|
|
|
|
impl Demuxer for FfmpegDemuxer {
|
|
fn pump (&mut self) -> Result <Option <ffmpeg_next::Packet>> {
|
|
while let Some ((stream, packet)) = self.input_ctx.packets ().next () {
|
|
if stream.index () == self.best_stream_idx {
|
|
return Ok (Some (packet));
|
|
}
|
|
}
|
|
|
|
Ok (None)
|
|
}
|
|
}
|
|
|
|
use crate::net_reader::NetReader;
|
|
|
|
pub struct HttpOggDemuxer {
|
|
ogg_rdr: ogg::reading::PacketReader <std::io::BufReader <NetReader>>,
|
|
}
|
|
|
|
impl HttpOggDemuxer {
|
|
pub fn new (url: String) -> Result <(Self, ffmpeg_next::codec::Context)> {
|
|
let net_rdr = NetReader::new (url)?;
|
|
let buf_rdr = std::io::BufReader::new (net_rdr);
|
|
let mut ogg_rdr = ogg::reading::PacketReader::new (buf_rdr);
|
|
|
|
let codec = {
|
|
// Open a local file as a placeholder for figuring out the remote file's codec
|
|
let input_ctx = ffmpeg_next::format::input (&"test.ogg")?;
|
|
let stream = input_ctx
|
|
.streams ()
|
|
.best (ffmpeg_next::media::Type::Audio)
|
|
.ok_or_else (|| anyhow! ("can't find good audio stream"))?;
|
|
|
|
stream.codec ()
|
|
};
|
|
|
|
Ok ((Self {
|
|
ogg_rdr,
|
|
}, codec))
|
|
}
|
|
}
|
|
|
|
impl Demuxer for HttpOggDemuxer {
|
|
fn pump (&mut self) -> Result <Option <ffmpeg_next::Packet>> {
|
|
let packet = match self.ogg_rdr.read_packet () {
|
|
Err (e) => {
|
|
tracing::error! ("Ogg read error: {:?}", e);
|
|
return Ok (None);
|
|
},
|
|
Ok (x) => x,
|
|
};
|
|
let packet = match packet {
|
|
None => return Ok (None),
|
|
Some (x) => x,
|
|
};
|
|
|
|
Ok (Some (ffmpeg_next::Packet::copy (&packet.data)))
|
|
}
|
|
}
|
|
|
|
#[cfg (test)]
|
|
mod test {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn pcm_buffer () {
|
|
let mut x = PcmBuffers::default ();
|
|
|
|
x.produce (vec! [0.0, 1.0, 2.0, 3.0]);
|
|
x.produce (vec! [4.0, 5.0, 6.0, 7.0]);
|
|
|
|
let mut data = vec! [0.0; 3];
|
|
|
|
assert! (x.consume_exact (&mut data));
|
|
assert_eq! (&data, &[0.0, 1.0, 2.0]);
|
|
|
|
assert! (x.consume_exact (&mut data));
|
|
assert_eq! (&data, &[3.0, 4.0, 5.0]);
|
|
}
|
|
}
|