👕 refactor: use decoder module
parent
722821e2c4
commit
096b5aeb65
|
@ -75,10 +75,10 @@ pub struct SharedState {
|
|||
}
|
||||
|
||||
pub struct Decoder {
|
||||
input_ctx: DemuxContext,
|
||||
pub input_ctx: DemuxContext,
|
||||
best_stream_idx: usize,
|
||||
decoder: DecodeContext,
|
||||
resampler: ResamplingContext,
|
||||
pub decoder: DecodeContext,
|
||||
pub resampler: ResamplingContext,
|
||||
}
|
||||
|
||||
impl Decoder {
|
||||
|
|
81
src/main.rs
81
src/main.rs
|
@ -40,26 +40,7 @@ fn main () -> Result <()> {
|
|||
let thread_decoder = thread::spawn (move|| {
|
||||
let (lock, cvar) = &*pair2;
|
||||
|
||||
let mut 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 mut decoder = stream.codec ().decoder ().audio ()?;
|
||||
let mut 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,
|
||||
)?;
|
||||
|
||||
let mut frame_src = ffmpeg_next::util::frame::Audio::empty ();
|
||||
let mut packets = input_ctx.packets ()
|
||||
//.skip (15800)
|
||||
;
|
||||
let mut decoder = decoder::Decoder::new (&filename)?;
|
||||
|
||||
'decoder_thread: loop {
|
||||
// tracing::trace! ("decode thread parking");
|
||||
|
@ -77,74 +58,28 @@ fn main () -> Result <()> {
|
|||
|
||||
let pcm_buffers = &mut decoder_state.pcm_buffers;
|
||||
|
||||
'fill_buffer: while pcm_buffers.samples_available () < 48_000 {
|
||||
while pcm_buffers.samples_available () < 48_000 {
|
||||
// tracing::trace! ("Decoder is trying to work...");
|
||||
|
||||
match resampler.delay () {
|
||||
Some (x) if x.milliseconds > 500 => {
|
||||
// tracing::trace! ("flushing resampler");
|
||||
let mut frame_resampled = ffmpeg_next::util::frame::Audio::empty ();
|
||||
|
||||
if resampler.flush (&mut frame_resampled).is_ok () {
|
||||
pcm_buffers.produce_bytes (frame_resampled.data (0));
|
||||
continue 'fill_buffer;
|
||||
}
|
||||
else {
|
||||
// tracing::warn! ("resampler flushed out a zero-length frame?");
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
if ! decoder.fill_buffer (pcm_buffers)? {
|
||||
tracing::info! ("Decoder thread is out of work, quitting");
|
||||
break;
|
||||
}
|
||||
|
||||
if decoder.receive_frame (&mut frame_src).is_ok () {
|
||||
//eprintln! ("decoder.receive_frame");
|
||||
let mut frame_resampled = ffmpeg_next::util::frame::Audio::empty ();
|
||||
resampler.run (&frame_src, &mut frame_resampled)?;
|
||||
pcm_buffers.produce_bytes (frame_resampled.data (0));
|
||||
continue 'fill_buffer;
|
||||
}
|
||||
|
||||
//eprintln! ("Decoder is dry, fetching a new packet...");
|
||||
|
||||
while let Some ((stream, packet)) = packets.next () {
|
||||
if stream.index () == best_stream_idx {
|
||||
//eprintln! ("decoder.send_packet");
|
||||
decoder.send_packet (&packet)?;
|
||||
continue 'fill_buffer;
|
||||
}
|
||||
}
|
||||
|
||||
//eprintln! ("Decoder ran out of work");
|
||||
|
||||
if resampler.delay ().is_some () {
|
||||
tracing::trace! ("flushing resampler");
|
||||
let mut frame_resampled = ffmpeg_next::util::frame::Audio::empty ();
|
||||
|
||||
if resampler.flush (&mut frame_resampled).is_ok () {
|
||||
//eprintln! ("resampler.flush");
|
||||
pcm_buffers.produce_bytes (frame_resampled.data (0));
|
||||
continue 'fill_buffer;
|
||||
}
|
||||
}
|
||||
|
||||
tracing::info! ("Decoder thread is out of work, quitting");
|
||||
break 'decoder_thread;
|
||||
}
|
||||
}
|
||||
|
||||
tracing::debug! ("Dropping resampler...");
|
||||
|
||||
drop (resampler);
|
||||
drop (decoder.resampler);
|
||||
sleep (Duration::from_secs (3));
|
||||
|
||||
tracing::debug! ("Dropping decoder...");
|
||||
|
||||
drop (decoder);
|
||||
drop (decoder.decoder);
|
||||
sleep (Duration::from_secs (3));
|
||||
|
||||
tracing::debug! ("Dropping input_ctx...");
|
||||
|
||||
drop (input_ctx);
|
||||
drop (decoder.input_ctx);
|
||||
sleep (Duration::from_secs (3));
|
||||
|
||||
Ok::<_, anyhow::Error> (())
|
||||
|
|
Loading…
Reference in New Issue