ensure first mux frame is key frame

Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
21pages 2022-09-21 13:27:18 +08:00
parent 9489877c78
commit eff5dd2e03
2 changed files with 18 additions and 6 deletions

View File

@ -94,6 +94,7 @@ impl EncoderApi for HwEncoder {
frames.push(EncodedVideoFrame { frames.push(EncodedVideoFrame {
data: Bytes::from(frame.data), data: Bytes::from(frame.data),
pts: frame.pts as _, pts: frame.pts as _,
key:frame.key == 1,
..Default::default() ..Default::default()
}); });
} }

View File

@ -161,7 +161,7 @@ impl Recorder {
})?; })?;
} }
if self.ctx.codec_id == RecodeCodecID::H264 { if self.ctx.codec_id == RecodeCodecID::H264 {
h264s.frames.last().map(|f| self.write_video(f)); h264s.frames.iter().map(|f| self.write_video(f)).count();
} }
} }
#[cfg(feature = "hwcodec")] #[cfg(feature = "hwcodec")]
@ -173,7 +173,7 @@ impl Recorder {
})?; })?;
} }
if self.ctx.codec_id == RecodeCodecID::H265 { if self.ctx.codec_id == RecodeCodecID::H265 {
h265s.frames.last().map(|f| self.write_video(f)); h265s.frames.iter().map(|f| self.write_video(f)).count();
} }
} }
_ => bail!("unsupported frame type"), _ => bail!("unsupported frame type"),
@ -255,6 +255,7 @@ struct HwRecorder {
muxer: Muxer, muxer: Muxer,
ctx: RecorderContext, ctx: RecorderContext,
written: bool, written: bool,
key: bool,
start: Instant, start: Instant,
} }
@ -273,25 +274,35 @@ impl RecorderApi for HwRecorder {
muxer, muxer,
ctx, ctx,
written: false, written: false,
key: false,
start: Instant::now(), start: Instant::now(),
}) })
} }
fn write_video(&mut self, frame: &EncodedVideoFrame) -> bool { fn write_video(&mut self, frame: &EncodedVideoFrame) -> bool {
let ok = self.muxer.write_video(&frame.data, frame.pts).is_ok(); if frame.key {
if ok { self.key = true;
self.written = true; }
if self.key {
let ok = self.muxer.write_video(&frame.data, frame.key).is_ok();
if ok {
self.written = true;
}
ok
} else {
false
} }
ok
} }
} }
#[cfg(feature = "hwcodec")] #[cfg(feature = "hwcodec")]
impl Drop for HwRecorder { impl Drop for HwRecorder {
fn drop(&mut self) { fn drop(&mut self) {
log::info!("DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD {}", self.ctx.filename);
self.muxer.write_tail().ok(); self.muxer.write_tail().ok();
if !self.written || self.start.elapsed().as_secs() < MIN_SECS { if !self.written || self.start.elapsed().as_secs() < MIN_SECS {
std::fs::remove_file(&self.ctx.filename).ok(); std::fs::remove_file(&self.ctx.filename).ok();
} }
log::info!("DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD ok");
} }
} }