Skip to content

Commit 0739286

Browse files
committed
fix: controls are set miss its type
1 parent 86b1020 commit 0739286

File tree

6 files changed

+97
-100
lines changed

6 files changed

+97
-100
lines changed

src/args.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ struct Args {
6666
float sharpness = 1.0f;
6767
float contrast = 1.0f;
6868
float brightness = 0.0f;
69-
float saturation = 0.0f;
69+
float saturation = 1.0f;
7070
float ev = 0.0f;
7171
std::string shutter_ = "0";
7272
TimeVal<std::chrono::microseconds> shutter;

src/capturer/libcamera_capturer.cpp

Lines changed: 89 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,99 @@
77

88
std::shared_ptr<LibcameraCapturer> LibcameraCapturer::Create(Args args) {
99
auto ptr = std::make_shared<LibcameraCapturer>(args);
10-
ptr->Init(args.cameraId);
11-
ptr->SetFps(args.fps).SetRotation(args.rotation).SetResolution(args.width, args.height);
10+
ptr->InitCamera(args.cameraId);
11+
ptr->InitControls(args);
12+
ptr->SetFps(args.fps)
13+
.SetRotation(args.rotation)
14+
.SetResolution(args.width, args.height)
15+
.StartCapture();
16+
return ptr;
17+
}
18+
19+
LibcameraCapturer::LibcameraCapturer(Args args)
20+
: buffer_count_(2),
21+
format_(args.format),
22+
config_(args) {}
23+
24+
LibcameraCapturer::~LibcameraCapturer() {
25+
camera_->stop();
26+
allocator_->free(stream_);
27+
allocator_.reset();
28+
camera_config_.reset();
29+
camera_->release();
30+
camera_.reset();
31+
cm_->stop();
32+
}
1233

34+
void LibcameraCapturer::InitCamera(int cameraId) {
35+
cm_ = std::make_unique<libcamera::CameraManager>();
36+
int ret = cm_->start();
37+
if (ret) {
38+
throw std::runtime_error("Failed to start camera manager");
39+
}
40+
41+
auto cameras = cm_->cameras();
42+
if (cameras.size() == 0) {
43+
throw std::runtime_error("No camera is available via libcamera.");
44+
}
45+
46+
if (cameraId >= cameras.size()) {
47+
throw std::runtime_error("Selected camera is not available.");
48+
}
49+
50+
std::string const &cam_id = cameras[cameraId]->id();
51+
INFO_PRINT("camera id: %s", cam_id.c_str());
52+
camera_ = cm_->get(cam_id);
53+
camera_->acquire();
54+
camera_config_ = camera_->generateConfiguration({libcamera::StreamRole::VideoRecording});
55+
}
56+
57+
void LibcameraCapturer::InitControls(Args args) {
1358
if (args.gain) {
14-
ptr->SetControls(libcamera::controls::ANALOGUE_GAIN_MODE,
15-
libcamera::controls::AnalogueGainModeManual)
16-
.SetControls(libcamera::controls::ANALOGUE_GAIN, args.gain);
59+
SetControls(libcamera::controls::ANALOGUE_GAIN_MODE,
60+
libcamera::controls::AnalogueGainModeManual);
61+
controls_.set(libcamera::controls::ANALOGUE_GAIN, args.gain);
1762
}
1863

19-
ptr->SetControls(libcamera::controls::SHARPNESS, args.sharpness)
20-
.SetControls(libcamera::controls::CONTRAST, args.contrast)
21-
.SetControls(libcamera::controls::BRIGHTNESS, args.brightness)
22-
.SetControls(libcamera::controls::SATURATION, args.saturation)
23-
.SetControls(libcamera::controls::EXPOSURE_VALUE, args.ev)
24-
.SetControls(libcamera::controls::EXPOSURE_TIME,
25-
args.shutter.get<std::chrono::microseconds>())
26-
.SetControls(libcamera::controls::AE_METERING_MODE, args.ae_metering_mode)
27-
.SetControls(libcamera::controls::AE_EXPOSURE_MODE, args.ae_mode)
28-
.SetControls(libcamera::controls::AWB_MODE, args.awb_mode)
29-
.SetControls(libcamera::controls::COLOUR_GAINS,
30-
libcamera::Span<const float, 2>({args.awb_gain_r, args.awb_gain_b}))
31-
.SetControls(libcamera::controls::draft::NOISE_REDUCTION_MODE, args.denoise_mode);
64+
SetControls(libcamera::controls::SHARPNESS, args.sharpness);
65+
SetControls(libcamera::controls::CONTRAST, args.contrast);
66+
SetControls(libcamera::controls::BRIGHTNESS, args.brightness);
67+
SetControls(libcamera::controls::SATURATION, args.saturation);
68+
SetControls(libcamera::controls::EXPOSURE_VALUE, args.ev);
69+
70+
if (controls_.contains(libcamera::controls::EXPOSURE_TIME)) {
71+
controls_.set(libcamera::controls::EXPOSURE_TIME,
72+
args.shutter.get<std::chrono::microseconds>());
73+
}
74+
75+
SetControls(libcamera::controls::AE_METERING_MODE, args.ae_metering_mode);
76+
SetControls(libcamera::controls::AE_EXPOSURE_MODE, args.ae_mode);
77+
SetControls(libcamera::controls::AWB_MODE, args.awb_mode);
78+
SetControls(libcamera::controls::draft::NOISE_REDUCTION_MODE, args.denoise_mode);
79+
80+
if (!controls_.get(libcamera::controls::ColourGains)) {
81+
controls_.set(libcamera::controls::ColourGains,
82+
libcamera::Span<const float, 2>({args.awb_gain_r, args.awb_gain_b}));
83+
}
3284

3385
if (args.af_mode == -1) {
3486
if (args.lens_position || args.set_default_lens_position) {
3587
args.af_mode = libcamera::controls::AfModeManual;
3688
} else {
37-
if (ptr->camera_->controls().find(libcamera::controls::AF_MODE) !=
38-
ptr->camera_->controls().end() &&
39-
ptr->camera_->controls().count(&libcamera::controls::AfMode) > 0) {
89+
if (camera_->controls().find(libcamera::controls::AF_MODE) !=
90+
camera_->controls().end() &&
91+
camera_->controls().count(&libcamera::controls::AfMode) > 0) {
4092
args.af_mode =
41-
ptr->camera_->controls().at(&libcamera::controls::AfMode).max().get<int>();
93+
camera_->controls().at(&libcamera::controls::AfMode).max().get<int>();
4294
}
4395
}
4496
}
45-
ptr->SetControls(libcamera::controls::AF_MODE, args.af_mode)
46-
.SetControls(libcamera::controls::AF_RANGE, args.af_range_mode)
47-
.SetControls(libcamera::controls::AF_SPEED, args.af_speed_mode);
97+
SetControls(libcamera::controls::AF_MODE, args.af_mode);
98+
SetControls(libcamera::controls::AF_RANGE, args.af_range_mode);
99+
SetControls(libcamera::controls::AF_SPEED, args.af_speed_mode);
48100

49101
if (args.af_window_width != 0 && args.af_window_height != 0) {
50-
libcamera::Rectangle sensor_area = ptr->camera_->controls()
102+
libcamera::Rectangle sensor_area = camera_->controls()
51103
.at(&libcamera::controls::ScalerCrop)
52104
.max()
53105
.get<libcamera::Rectangle>();
@@ -59,63 +111,25 @@ std::shared_ptr<LibcameraCapturer> LibcameraCapturer::Create(Args args) {
59111
afwindows_rectangle[0] = libcamera::Rectangle(x, y, w, h);
60112
afwindows_rectangle[0].translateBy(sensor_area.topLeft());
61113

62-
ptr->SetControls(libcamera::controls::AF_METERING, libcamera::controls::AfMeteringWindows)
63-
.SetControls(libcamera::controls::AF_WINDOWS,
64-
libcamera::Span<const libcamera::Rectangle>(afwindows_rectangle));
114+
SetControls(libcamera::controls::AF_METERING, libcamera::controls::AfMeteringWindows);
115+
116+
if (controls_.contains(libcamera::controls::AF_WINDOWS)) {
117+
controls_.set(libcamera::controls::AF_WINDOWS,
118+
libcamera::Span<const libcamera::Rectangle>(afwindows_rectangle));
119+
}
65120
}
66121

67122
if (args.af_mode == libcamera::controls::AfModeEnum::AfModeAuto) {
68-
ptr->SetControls(libcamera::controls::AF_TRIGGER, libcamera::controls::AfTriggerStart);
123+
SetControls(libcamera::controls::AF_TRIGGER, libcamera::controls::AfTriggerStart);
69124
} else if (args.lens_position || args.set_default_lens_position) {
70125
float f;
71126
if (args.lens_position) {
72127
f = args.lens_position.value();
73128
} else {
74-
f = ptr->camera_->controls().at(&libcamera::controls::LensPosition).def().get<float>();
129+
f = camera_->controls().at(&libcamera::controls::LensPosition).def().get<float>();
75130
}
76-
ptr->SetControls(libcamera::controls::LENS_POSITION, f);
77-
}
78-
79-
ptr->StartCapture();
80-
return ptr;
81-
}
82-
83-
LibcameraCapturer::LibcameraCapturer(Args args)
84-
: buffer_count_(2),
85-
format_(args.format),
86-
config_(args) {}
87-
88-
void LibcameraCapturer::Init(int cameraId) {
89-
cm_ = std::make_unique<libcamera::CameraManager>();
90-
int ret = cm_->start();
91-
if (ret) {
92-
throw std::runtime_error("Failed to start camera manager");
93-
}
94-
95-
auto cameras = cm_->cameras();
96-
if (cameras.size() == 0) {
97-
throw std::runtime_error("No camera is available via libcamera.");
98-
}
99-
100-
if (cameraId >= cameras.size()) {
101-
throw std::runtime_error("Selected camera is not available.");
131+
SetControls(libcamera::controls::LENS_POSITION, f);
102132
}
103-
104-
std::string const &cam_id = cameras[cameraId]->id();
105-
INFO_PRINT("camera id: %s", cam_id.c_str());
106-
camera_ = cm_->get(cam_id);
107-
camera_->acquire();
108-
camera_config_ = camera_->generateConfiguration({libcamera::StreamRole::VideoRecording});
109-
}
110-
111-
LibcameraCapturer::~LibcameraCapturer() {
112-
camera_->stop();
113-
allocator_->free(stream_);
114-
allocator_.reset();
115-
camera_config_.reset();
116-
camera_->release();
117-
camera_.reset();
118-
cm_->stop();
119133
}
120134

121135
int LibcameraCapturer::fps() const { return fps_; }
@@ -175,15 +189,11 @@ LibcameraCapturer &LibcameraCapturer::SetFps(int fps) {
175189
return *this;
176190
}
177191

178-
LibcameraCapturer &LibcameraCapturer::SetControls(int key, ControlValue value) {
192+
LibcameraCapturer &LibcameraCapturer::SetControls(int key, int value) {
179193
std::lock_guard<std::mutex> lock(control_mutex_);
180194

181195
if (controls_.contains(key) && camera_->controls().count(key) > 0) {
182-
std::visit(
183-
[&](auto &&v) {
184-
controls_.set(key, v);
185-
},
186-
value);
196+
controls_.set(key, value);
187197
}
188198

189199
return *this;

src/capturer/libcamera_capturer.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class LibcameraCapturer : public VideoCapturer {
2626
uint32_t format() const override;
2727
Args config() const override;
2828

29-
LibcameraCapturer &SetControls(int key, ControlValue value) override;
29+
LibcameraCapturer &SetControls(int key, int value) override;
3030
rtc::scoped_refptr<webrtc::I420BufferInterface> GetI420Frame() override;
3131
void StartCapture() override;
3232

@@ -56,7 +56,8 @@ class LibcameraCapturer : public VideoCapturer {
5656
LibcameraCapturer &SetFps(int fps) override;
5757
LibcameraCapturer &SetRotation(int angle) override;
5858

59-
void Init(int deviceId);
59+
void InitCamera(int deviceId);
60+
void InitControls(Args arg);
6061
void AllocateBuffer();
6162
void RequestComplete(libcamera::Request *request);
6263
};

src/capturer/v4l2_capturer.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,8 @@ void V4L2Capturer::CaptureImage() {
152152
}
153153
}
154154

155-
V4L2Capturer &V4L2Capturer::SetControls(int key, ControlValue value) {
156-
std::visit(overloaded{[&](int v) {
157-
V4L2Util::SetExtCtrl(fd_, key, v);
158-
},
159-
[&](auto &&v) {
160-
std::cerr << "Unsupported control value type: " << typeid(v).name()
161-
<< "\n";
162-
}},
163-
value);
155+
V4L2Capturer &V4L2Capturer::SetControls(int key, int value) {
156+
V4L2Util::SetExtCtrl(fd_, key, value);
164157
return *this;
165158
}
166159

src/capturer/v4l2_capturer.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313

1414
class V4L2Capturer : public VideoCapturer {
1515
public:
16-
template <class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
17-
template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
18-
1916
static std::shared_ptr<V4L2Capturer> Create(Args args);
2017

2118
V4L2Capturer(Args args);
@@ -28,7 +25,7 @@ class V4L2Capturer : public VideoCapturer {
2825
Args config() const override;
2926
void StartCapture() override;
3027

31-
V4L2Capturer &SetControls(int key, ControlValue value) override;
28+
V4L2Capturer &SetControls(int key, int value) override;
3229
rtc::scoped_refptr<webrtc::I420BufferInterface> GetI420Frame() override;
3330

3431
private:

src/capturer/video_capturer.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313

1414
class VideoCapturer {
1515
public:
16-
using ControlValue =
17-
std::variant<int, signed long, float, bool, libcamera::Span<const float, 2UL>,
18-
libcamera::Span<const libcamera::Rectangle>>;
19-
2016
VideoCapturer() = default;
2117
~VideoCapturer() {
2218
raw_buffer_subject_.UnSubscribe();
@@ -35,7 +31,7 @@ class VideoCapturer {
3531
virtual VideoCapturer &SetResolution(int width, int height) = 0;
3632
virtual VideoCapturer &SetFps(int fps) = 0;
3733
virtual VideoCapturer &SetRotation(int angle) = 0;
38-
virtual VideoCapturer &SetControls(int key, ControlValue value) = 0;
34+
virtual VideoCapturer &SetControls(int key, int value) = 0;
3935

4036
std::shared_ptr<Observable<V4L2Buffer>> AsRawBufferObservable() {
4137
return raw_buffer_subject_.AsObservable();

0 commit comments

Comments
 (0)