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 {
data: Bytes::from(frame.data),
pts: frame.pts as _,
key:frame.key == 1,
..Default::default()
});
}

View File

@ -161,7 +161,7 @@ impl Recorder {
})?;
}
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")]
@ -173,7 +173,7 @@ impl Recorder {
})?;
}
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"),
@ -255,6 +255,7 @@ struct HwRecorder {
muxer: Muxer,
ctx: RecorderContext,
written: bool,
key: bool,
start: Instant,
}
@ -273,25 +274,35 @@ impl RecorderApi for HwRecorder {
muxer,
ctx,
written: false,
key: false,
start: Instant::now(),
})
}
fn write_video(&mut self, frame: &EncodedVideoFrame) -> bool {
let ok = self.muxer.write_video(&frame.data, frame.pts).is_ok();
if ok {
self.written = true;
if frame.key {
self.key = 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")]
impl Drop for HwRecorder {
fn drop(&mut self) {
log::info!("DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD {}", self.ctx.filename);
self.muxer.write_tail().ok();
if !self.written || self.start.elapsed().as_secs() < MIN_SECS {
std::fs::remove_file(&self.ctx.filename).ok();
}
log::info!("DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD ok");
}
}