♻️ refactor

main
_ 2021-11-14 18:12:07 +00:00
parent 57c53a31dd
commit 99b3bfe231
1 changed files with 19 additions and 29 deletions

View File

@ -91,24 +91,19 @@ impl PcmBuffers {
} }
pub struct Decoder { pub struct Decoder {
input_ctx: DemuxContext, demuxer: FfmpegDemuxer,
decoder: DecodeContext, decoder: DecodeContext,
resampler: ResamplingContext, resampler: ResamplingContext,
best_stream_idx: usize,
dummy_frame: Option <FfAudioFrame>, dummy_frame: Option <FfAudioFrame>,
} }
impl Decoder { impl Decoder {
pub fn new (filename: &str) -> Result <Self> { pub fn new (filename: &str) -> Result <Self> {
let input_ctx = ffmpeg_next::format::input (&filename)?; let (demuxer, codec) = FfmpegDemuxer::new (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 decoder = codec.decoder ().audio ()?;
let resampler = decoder.resampler ( let resampler = decoder.resampler (
Sample::F32 (sample::Type::Packed), Sample::F32 (sample::Type::Packed),
ChannelLayout::STEREO, ChannelLayout::STEREO,
@ -116,11 +111,10 @@ impl Decoder {
)?; )?;
Ok (Self { Ok (Self {
input_ctx, demuxer,
decoder, decoder,
resampler, resampler,
best_stream_idx,
dummy_frame: None, dummy_frame: None,
}) })
} }
@ -223,15 +217,14 @@ impl Decoder {
} }
pub fn pump_demuxer (&mut self) -> Result <bool> { pub fn pump_demuxer (&mut self) -> Result <bool> {
while let Some ((stream, packet)) = self.input_ctx.packets ().next () { let packet = self.demuxer.pump ()?;
if stream.index () == self.best_stream_idx { let packet = match packet {
// tracing::trace! ("demuxed packet"); Some (x) => x,
self.decoder.send_packet (&packet)?; None => return Ok (false),
return Ok (true); };
}
}
Ok (false) self.decoder.send_packet (&packet)?;
Ok (true)
} }
} }
@ -242,11 +235,10 @@ trait Demuxer {
struct FfmpegDemuxer { struct FfmpegDemuxer {
input_ctx: DemuxContext, input_ctx: DemuxContext,
best_stream_idx: usize, best_stream_idx: usize,
decoder: DecodeContext,
} }
impl FfmpegDemuxer { impl FfmpegDemuxer {
pub fn new (filename: &str) -> Result <Self> { pub fn new (filename: &str) -> Result <(Self, ffmpeg_next::codec::Context)> {
let input_ctx = ffmpeg_next::format::input (&filename)?; let input_ctx = ffmpeg_next::format::input (&filename)?;
let stream = input_ctx let stream = input_ctx
.streams () .streams ()
@ -254,27 +246,25 @@ impl FfmpegDemuxer {
.ok_or_else (|| anyhow! ("can't find good audio stream"))?; .ok_or_else (|| anyhow! ("can't find good audio stream"))?;
let best_stream_idx = stream.index (); let best_stream_idx = stream.index ();
let decoder = stream.codec ().decoder ().audio ()?; let codec = stream.codec ();
Ok (Self { Ok ((Self {
input_ctx, input_ctx,
best_stream_idx, best_stream_idx,
decoder, }, codec))
})
} }
} }
impl FfmpegDemuxer { impl FfmpegDemuxer {
fn pump (&mut self) -> Result <bool> { fn pump (&mut self) -> Result <Option <ffmpeg_next::Packet>> {
while let Some ((stream, packet)) = self.input_ctx.packets ().next () { while let Some ((stream, packet)) = self.input_ctx.packets ().next () {
if stream.index () == self.best_stream_idx { if stream.index () == self.best_stream_idx {
// tracing::trace! ("demuxed packet"); // tracing::trace! ("demuxed packet");
self.decoder.send_packet (&packet)?; return Ok (Some (packet));
return Ok (true);
} }
} }
Ok (false) Ok (None)
} }
} }