77
88std::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
121135int 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 ;
0 commit comments