Skip to content

Commit d566406

Browse files
committed
feat: add flag to disable adaptive stream
1 parent e1a1e5e commit d566406

File tree

7 files changed

+38
-30
lines changed

7 files changed

+38
-30
lines changed

src/args.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ struct Args {
1818
bool no_audio = false;
1919
bool hw_accel = false;
2020
bool use_libcamera = false;
21+
bool fixed_resolution = false;
2122
uint32_t format = V4L2_PIX_FMT_MJPEG;
2223
std::string v4l2_format = "mjpeg";
2324
std::string device = "/dev/video0";

src/customized_video_encoder_factory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ std::unique_ptr<webrtc::VideoEncoder>
4949
CustomizedVideoEncoderFactory::CreateVideoEncoder(const webrtc::SdpVideoFormat &format) {
5050
if (absl::EqualsIgnoreCase(format.name, cricket::kH264CodecName)) {
5151
if (args_.hw_accel) {
52-
return V4l2H264Encoder::Create();
52+
return V4l2H264Encoder::Create(args_);
5353
} else {
5454
return webrtc::H264Encoder::Create(cricket::VideoCodec(format));
5555
}

src/parser.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ void Parser::ParseArgs(int argc, char *argv[], Args &args) {
4040
("use_libcamera", bpo::bool_switch()->default_value(args.use_libcamera),
4141
"Read YUV420 from the camera via libcamera, the `device` and `v4l2_format` "
4242
"flags will be suspended")
43+
("fixed_resolution", bpo::bool_switch()->default_value(args.fixed_resolution),
44+
"Disable adaptive resolution scaling and keep a fixed resolution.")
4345
("no_audio", bpo::bool_switch()->default_value(args.no_audio), "Run without audio source")
4446
("uid", bpo::value<std::string>()->default_value(args.uid),
4547
"Set the unique id to identify the device")
@@ -108,6 +110,7 @@ void Parser::ParseArgs(int argc, char *argv[], Args &args) {
108110
SetIfExists(vm, "record_path", args.record_path);
109111

110112
args.use_libcamera = vm["use_libcamera"].as<bool>();
113+
args.fixed_resolution = vm["fixed_resolution"].as<bool>();
111114
args.no_audio = vm["no_audio"].as<bool>();
112115
args.hw_accel = vm["hw_accel"].as<bool>();
113116

src/track/scale_track_source.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ void ScaleTrackSource::OnFrameCaptured(rtc::scoped_refptr<webrtc::VideoFrameBuff
3838
timestamp_aligner.TranslateTimestamp(timestamp_us, rtc::TimeMicros());
3939

4040
int adapted_width, adapted_height, crop_width, crop_height, crop_x, crop_y;
41-
if (!AdaptFrame(width, height, timestamp_us, &adapted_width, &adapted_height, &crop_width,
42-
&crop_height, &crop_x, &crop_y)) {
41+
if (capturer->config().fixed_resolution) {
42+
adapted_width = width;
43+
adapted_height = height;
44+
} else if (!AdaptFrame(width, height, timestamp_us, &adapted_width, &adapted_height,
45+
&crop_width, &crop_height, &crop_x, &crop_y)) {
4346
return;
4447
}
4548

src/track/v4l2dma_track_source.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,28 @@ void V4l2DmaTrackSource::OnFrameCaptured(V4l2Buffer decoded_buffer) {
3636
const int64_t translated_timestamp_us =
3737
timestamp_aligner.TranslateTimestamp(timestamp_us, rtc::TimeMicros());
3838

39-
int adapted_width, adapted_height, crop_width, crop_height, crop_x, crop_y;
40-
if (!AdaptFrame(width, height, timestamp_us, &adapted_width, &adapted_height, &crop_width,
41-
&crop_height, &crop_x, &crop_y)) {
42-
return;
43-
}
39+
if (capturer->config().fixed_resolution) {
40+
auto dst_buffer = V4l2FrameBuffer::Create(config_width_, config_height_, decoded_buffer,
41+
V4L2_PIX_FMT_YUV420);
42+
OnFrame(webrtc::VideoFrame::Builder()
43+
.set_video_frame_buffer(dst_buffer)
44+
.set_rotation(webrtc::kVideoRotation_0)
45+
.set_timestamp_us(translated_timestamp_us)
46+
.build());
47+
} else {
48+
int adapted_width, adapted_height, crop_width, crop_height, crop_x, crop_y;
49+
if (!AdaptFrame(width, height, timestamp_us, &adapted_width, &adapted_height, &crop_width,
50+
&crop_height, &crop_x, &crop_y)) {
51+
return;
52+
}
4453

45-
if (adapted_width != config_width_ || adapted_height != config_height_) {
46-
config_width_ = adapted_width;
47-
config_height_ = adapted_height;
48-
scaler =
49-
V4l2Scaler::Create(width, height, config_width_, config_height_, is_dma_src_, true);
50-
}
54+
if (!scaler || adapted_width != config_width_ || adapted_height != config_height_) {
55+
config_width_ = adapted_width;
56+
config_height_ = adapted_height;
57+
scaler =
58+
V4l2Scaler::Create(width, height, config_width_, config_height_, is_dma_src_, true);
59+
}
5160

52-
if (scaler) {
5361
scaler->EmplaceBuffer(
5462
decoded_buffer, [this, translated_timestamp_us](V4l2Buffer scaled_buffer) {
5563
auto dst_buffer = V4l2FrameBuffer::Create(config_width_, config_height_,
@@ -61,13 +69,5 @@ void V4l2DmaTrackSource::OnFrameCaptured(V4l2Buffer decoded_buffer) {
6169
.set_timestamp_us(translated_timestamp_us)
6270
.build());
6371
});
64-
} else {
65-
auto dst_buffer = V4l2FrameBuffer::Create(config_width_, config_height_, decoded_buffer,
66-
V4L2_PIX_FMT_YUV420);
67-
OnFrame(webrtc::VideoFrame::Builder()
68-
.set_video_frame_buffer(dst_buffer)
69-
.set_rotation(webrtc::kVideoRotation_0)
70-
.set_timestamp_us(translated_timestamp_us)
71-
.build());
7272
}
7373
}

src/v4l2_codecs/v4l2_h264_encoder.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
#include "common/logging.h"
33
#include "common/v4l2_frame_buffer.h"
44

5-
std::unique_ptr<webrtc::VideoEncoder> V4l2H264Encoder::Create() {
6-
return std::make_unique<V4l2H264Encoder>();
5+
std::unique_ptr<webrtc::VideoEncoder> V4l2H264Encoder::Create(Args args) {
6+
return std::make_unique<V4l2H264Encoder>(args);
77
}
88

9-
V4l2H264Encoder::V4l2H264Encoder()
10-
: fps_adjuster_(30),
11-
is_dma_(true),
9+
V4l2H264Encoder::V4l2H264Encoder(Args args)
10+
: fps_adjuster_(args.fps),
11+
is_dma_(!args.fixed_resolution),
1212
bitrate_adjuster_(.85, 1),
1313
callback_(nullptr) {}
1414

src/v4l2_codecs/v4l2_h264_encoder.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
#include <common_video/include/bitrate_adjuster.h>
77
#include <modules/video_coding/codecs/h264/include/h264.h>
88

9+
#include "args.h"
910
#include "v4l2_codecs/v4l2_encoder.h"
1011

1112
class V4l2H264Encoder : public webrtc::VideoEncoder {
1213
public:
13-
static std::unique_ptr<webrtc::VideoEncoder> Create();
14-
V4l2H264Encoder();
14+
static std::unique_ptr<webrtc::VideoEncoder> Create(Args args);
15+
V4l2H264Encoder(Args args);
1516

1617
int32_t InitEncode(const webrtc::VideoCodec *codec_settings,
1718
const VideoEncoder::Settings &settings) override;

0 commit comments

Comments
 (0)