162 lines
3.9 KiB
Rust
162 lines
3.9 KiB
Rust
use std::{
|
|
io::Cursor,
|
|
};
|
|
|
|
use anyhow::{
|
|
anyhow,
|
|
Result,
|
|
};
|
|
|
|
use byteorder::{
|
|
LittleEndian,
|
|
ReadBytesExt,
|
|
};
|
|
|
|
// 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,
|
|
};
|
|
|
|
pub const SAMPLE_RATE: u32 = 48000;
|
|
|
|
#[derive (Default)]
|
|
pub struct PcmBuffers {
|
|
buffers: Vec <Vec <f32>>,
|
|
|
|
// 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
|
|
}
|
|
|
|
#[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 = self.buffers [0][self.consumer_cursor];
|
|
self.consumer_cursor += 1;
|
|
}
|
|
|
|
true
|
|
}
|
|
|
|
pub fn produce (&mut self, new_buffer: Vec <f32>) {
|
|
self.buffers.push (new_buffer);
|
|
}
|
|
|
|
pub fn produce_bytes (&mut self, new_buffer: &[u8]) {
|
|
let mut b = vec! [0.0f32; new_buffer.len () / 4];
|
|
let mut rdr = Cursor::new (new_buffer);
|
|
|
|
rdr.read_f32_into::<LittleEndian> (&mut b).unwrap ();
|
|
|
|
self.produce (b);
|
|
}
|
|
}
|
|
|
|
#[derive (Default)]
|
|
pub struct SharedState {
|
|
pub pcm_buffers: PcmBuffers,
|
|
pub quit: bool,
|
|
}
|
|
|
|
pub struct Decoder {
|
|
input_ctx: DemuxContext,
|
|
best_stream_idx: usize,
|
|
decoder: DecodeContext,
|
|
resampler: ResamplingContext,
|
|
}
|
|
|
|
impl Decoder {
|
|
pub fn new (filename: &str) -> Result <Self> {
|
|
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 decoder = stream.codec ().decoder ().audio ()?;
|
|
let resampler = decoder.resampler (
|
|
ffmpeg_next::util::format::sample::Sample::F32 (
|
|
ffmpeg_next::util::format::sample::Type::Packed,
|
|
),
|
|
ffmpeg_next::util::channel_layout::ChannelLayout::STEREO,
|
|
48000,
|
|
)?;
|
|
|
|
Ok (Self {
|
|
input_ctx,
|
|
best_stream_idx,
|
|
decoder,
|
|
resampler,
|
|
})
|
|
}
|
|
|
|
pub fn fill_buffer (&mut self, pcm_buffers: &mut PcmBuffers) -> Result <bool> {
|
|
match self.resampler.delay () {
|
|
Some (x) if x.milliseconds > 500 => {
|
|
// tracing::trace! ("flushing resampler");
|
|
let mut frame_resampled = ffmpeg_next::util::frame::Audio::empty ();
|
|
|
|
if self.resampler.flush (&mut frame_resampled).is_ok () {
|
|
pcm_buffers.produce_bytes (frame_resampled.data (0));
|
|
return Ok (true);
|
|
}
|
|
else {
|
|
// tracing::warn! ("resampler flushed out a zero-length frame?");
|
|
}
|
|
},
|
|
_ => {},
|
|
}
|
|
|
|
let mut frame_src = ffmpeg_next::util::frame::Audio::empty ();
|
|
if self.decoder.receive_frame (&mut frame_src).is_ok () {
|
|
//eprintln! ("decoder.receive_frame");
|
|
let mut frame_resampled = ffmpeg_next::util::frame::Audio::empty ();
|
|
self.resampler.run (&frame_src, &mut frame_resampled)?;
|
|
pcm_buffers.produce_bytes (frame_resampled.data (0));
|
|
return Ok (true);
|
|
}
|
|
|
|
//eprintln! ("Decoder is dry, fetching a new packet...");
|
|
|
|
while let Some ((stream, packet)) = self.input_ctx.packets ().next () {
|
|
if stream.index () == self.best_stream_idx {
|
|
//eprintln! ("decoder.send_packet");
|
|
self.decoder.send_packet (&packet)?;
|
|
return Ok (true);
|
|
}
|
|
}
|
|
|
|
//eprintln! ("Decoder ran out of work");
|
|
|
|
if self.resampler.delay ().is_some () {
|
|
tracing::trace! ("flushing resampler");
|
|
let mut frame_resampled = ffmpeg_next::util::frame::Audio::empty ();
|
|
|
|
if self.resampler.flush (&mut frame_resampled).is_ok () {
|
|
//eprintln! ("resampler.flush");
|
|
pcm_buffers.produce_bytes (frame_resampled.data (0));
|
|
return Ok (true);
|
|
}
|
|
}
|
|
|
|
Ok (false)
|
|
}
|
|
}
|