encoder: add ability to request keyframes from livestream encoders (#38172)

* add param that requests keyframe

* set keyframe request to false on cleanup

* clean

* timing and errors

* remove other changes

* clean diff

* clean diff

* clean up live adaptation params and functions

* better function names

* fix syntax

* remove unused var

* clean diff
This commit is contained in:
stef
2026-06-15 17:48:13 -04:00
committed by GitHub
parent fafcee04f4
commit 0240a62fd9
10 changed files with 50 additions and 16 deletions
+1
View File
@@ -27,6 +27,7 @@ public:
virtual void encoder_open() = 0;
virtual void encoder_close() = 0;
virtual void set_bitrate(int bitrate) = 0;
virtual void request_keyframe() = 0;
void publisher_publish(int segment_num, uint32_t idx, VisionIpcBufExtra &extra, unsigned int flags, kj::ArrayPtr<capnp::byte> header, kj::ArrayPtr<capnp::byte> dat);
+4
View File
@@ -76,6 +76,10 @@ void FfmpegEncoder::set_bitrate(int bitrate) {
LOGE("adaptive bitrate is not supported for ffmpeg encoder %s", encoder_info.publish_name);
}
void FfmpegEncoder::request_keyframe() {
LOGE("keyframe request is not supported for ffmpeg encoder %s", encoder_info.publish_name);
}
int FfmpegEncoder::encode_frame(VisionBuf* buf, VisionIpcBufExtra *extra) {
assert(buf->width == this->in_width);
assert(buf->height == this->in_height);
+1
View File
@@ -22,6 +22,7 @@ public:
void encoder_open();
void encoder_close();
void set_bitrate(int bitrate);
void request_keyframe();
private:
int segment_num = -1;
+12 -2
View File
@@ -156,7 +156,6 @@ V4LEncoder::V4LEncoder(const EncoderInfo &encoder_info, int in_width, int in_hei
EncoderSettings encoder_settings = encoder_info.get_settings(in_width);
current_bitrate = encoder_settings.bitrate;
adaptive_bitrate = encoder_info.adaptive_bitrate;
bool is_h265 = encoder_settings.encode_type == cereal::EncodeIndex::Type::FULL_H_E_V_C;
struct v4l2_format fmt_out = {
@@ -307,7 +306,7 @@ void V4LEncoder::encoder_close() {
}
void V4LEncoder::set_bitrate(int bitrate) {
if (!adaptive_bitrate || bitrate == current_bitrate) return;
if (bitrate == current_bitrate) return;
if (bitrate <= 0) {
LOGE("invalid livestream encoder bitrate %d", bitrate);
return;
@@ -325,6 +324,17 @@ void V4LEncoder::set_bitrate(int bitrate) {
current_bitrate = bitrate;
}
void V4LEncoder::request_keyframe() {
struct v4l2_control ctrl = {
.id = V4L2_CID_MPEG_VIDC_VIDEO_REQUEST_IFRAME,
.value = 1,
};
if (util::safe_ioctl(fd, VIDIOC_S_CTRL, &ctrl) == -1) {
LOGE("failed to request keyframe for %s", encoder_info.publish_name);
}
}
V4LEncoder::~V4LEncoder() {
encoder_close();
v4l2_buf_type buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
+1 -1
View File
@@ -14,6 +14,7 @@ public:
void encoder_open();
void encoder_close();
void set_bitrate(int bitrate);
void request_keyframe();
private:
int fd;
@@ -22,7 +23,6 @@ private:
int segment_num = -1;
int counter = 0;
int current_bitrate = -1;
bool adaptive_bitrate;
SafeQueue<VisionIpcBufExtra> extras;