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