Skip to content

Commit 10bb267

Browse files
committed
Fix libcam pixelformat and shutdown messages
1 parent 78a2614 commit 10bb267

3 files changed

Lines changed: 71 additions & 70 deletions

File tree

doc/motion_config.html

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,23 +1250,23 @@ <h3><a name="libcam_params"></a>libcam_params</h3>
12501250
Comma separated list of configuration parameters (aka controls) for the libcamera device.
12511251
</ul>
12521252
<ul>
1253+
<p></p>
1254+
<p></p>
12531255
<i><h4>pixelformat</h4></i>
12541256
User requested pixelformat to use. Default of YU12 is most efficient. Usually
12551257
this parameter is specified as the fourcc character code.
1256-
1257-
<i><h4> Transform(string)</h4></i>
1258-
(These are libcamera transform and rotate options and may not provide result you anticipate.)
1258+
<p></p>
1259+
<i><h4> Orientation(string)</h4></i>
1260+
(These are libcamera orientation options and may not provide result you anticipate.)
12591261
<div><ul>
1260-
<li> Identity</h4></i>
1261-
<li> Rot0</li>
1262-
<li> HFlip</li>
1263-
<li> VFlip</li>
1264-
<li> HVFlip</li>
1265-
<li> Rot180</li>
1266-
<li> Transpose(not currently supported by libcamera)</li>
1267-
<li> Rot270(not currently supported by libcamera)</li>
1268-
<li> Rot90(not currently supported by libcamera)</li>
1269-
<li> Rot180Transpose</li>
1262+
<li> Rotate0</h4></i>
1263+
<li> Rotate0Mirror</li>
1264+
<li> Rotate90</h4></i>
1265+
<li> Rotate90Mirror</li>
1266+
<li> Rotate180</h4></i>
1267+
<li> Rotate180Mirror</li>
1268+
<li> Rotate270</h4></i>
1269+
<li> Rotate270Mirror</li>
12701270
</ul></div>
12711271

12721272
<i><h4> AeEnable(bool)</h4></i>

src/libcam.cpp

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -511,23 +511,23 @@ void cls_libcam:: config_orientation()
511511

512512
for (indx=0;indx<params->params_cnt;indx++) {
513513
itm = &params->params_array[indx];
514-
if (itm->param_name == "orientation") {
514+
if (itm->param_name == "Orientation") {
515515
if (itm->param_value == "Rotate0") {
516-
config->orientation = Orientation::Rotate0;
516+
libcam_cfg->orientation = Orientation::Rotate0;
517517
} else if (itm->param_value == "Rotate0Mirror") {
518-
config->orientation = Orientation::Rotate0Mirror;
518+
libcam_cfg->orientation = Orientation::Rotate0Mirror;
519519
} else if (itm->param_value == "Rotate180") {
520-
config->orientation = Orientation::Rotate180;
520+
libcam_cfg->orientation = Orientation::Rotate180;
521521
} else if (itm->param_value == "Rotate180Mirror") {
522-
config->orientation = Orientation::Rotate180Mirror;
522+
libcam_cfg->orientation = Orientation::Rotate180Mirror;
523523
} else if (itm->param_value == "Rotate90") {
524-
config->orientation = Orientation::Rotate90;
524+
libcam_cfg->orientation = Orientation::Rotate90;
525525
} else if (itm->param_value == "Rotate90Mirror") {
526-
config->orientation = Orientation::Rotate90Mirror;
526+
libcam_cfg->orientation = Orientation::Rotate90Mirror;
527527
} else if (itm->param_value == "Rotate270") {
528-
config->orientation = Orientation::Rotate270;
528+
libcam_cfg->orientation = Orientation::Rotate270;
529529
} else if (itm->param_value == "Rotate270Mirror") {
530-
config->orientation = Orientation::Rotate270Mirror;
530+
libcam_cfg->orientation = Orientation::Rotate270Mirror;
531531
} else {
532532
MOTION_LOG(ERR, TYPE_VIDEO, NO_ERRNO
533533
, "Invalid Orientation option: %s."
@@ -536,23 +536,23 @@ void cls_libcam:: config_orientation()
536536
}
537537
}
538538

539-
retcd = config->validate();
539+
retcd = libcam_cfg->validate();
540540
if (retcd == CameraConfiguration::Adjusted) {
541-
if (config->orientation == Orientation::Rotate0) {
541+
if (libcam_cfg->orientation == Orientation::Rotate0) {
542542
adjdesc = "Rotate0";
543-
} else if (config->orientation == Orientation::Rotate0Mirror) {
543+
} else if (libcam_cfg->orientation == Orientation::Rotate0Mirror) {
544544
adjdesc = "Rotate0Mirror";
545-
} else if (config->orientation == Orientation::Rotate90) {
545+
} else if (libcam_cfg->orientation == Orientation::Rotate90) {
546546
adjdesc = "Rotate90";
547-
} else if (config->orientation == Orientation::Rotate90Mirror) {
547+
} else if (libcam_cfg->orientation == Orientation::Rotate90Mirror) {
548548
adjdesc = "Rotate90Mirror";
549-
} else if (config->orientation == Orientation::Rotate180) {
549+
} else if (libcam_cfg->orientation == Orientation::Rotate180) {
550550
adjdesc = "Rotate180";
551-
} else if (config->orientation == Orientation::Rotate180Mirror) {
551+
} else if (libcam_cfg->orientation == Orientation::Rotate180Mirror) {
552552
adjdesc = "Rotate180Mirror";
553-
} else if (config->orientation == Orientation::Rotate270) {
553+
} else if (libcam_cfg->orientation == Orientation::Rotate270) {
554554
adjdesc = "Rotate270";
555-
} else if (config->orientation == Orientation::Rotate270Mirror) {
555+
} else if (libcam_cfg->orientation == Orientation::Rotate270Mirror) {
556556
adjdesc = "Rotate270Mirror";
557557
} else {
558558
adjdesc = "unknown";
@@ -581,30 +581,30 @@ int cls_libcam::start_config()
581581

582582
MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO, "Starting.");
583583

584-
config = camera->generateConfiguration({ StreamRole::Viewfinder });
584+
libcam_cfg = camera->generateConfiguration({ StreamRole::Viewfinder });
585585

586586
pfmt = "YU12";
587587
for (indx=0;indx<params->params_cnt;indx++) {
588588
if (params->params_array[indx].param_name == "pixelformat") {
589-
pixfmt = params->params_array[indx].param_value;
589+
pfmt = params->params_array[indx].param_value;
590590
MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO
591591
, "Using user specified pixelformat %s."
592592
, pfmt.c_str());
593593
}
594594

595595
}
596-
config->at(0).pixelFormat = PixelFormat::fromString(pfmt);
597-
config->at(0).size.width = (uint)cam->cfg->width;
598-
config->at(0).size.height = (uint)cam->cfg->height;
599-
config->at(0).bufferCount = 1;
600-
config->at(0).stride = 0;
596+
libcam_cfg->at(0).pixelFormat = PixelFormat::fromString(pfmt);
597+
libcam_cfg->at(0).size.width = (uint)cam->cfg->width;
598+
libcam_cfg->at(0).size.height = (uint)cam->cfg->height;
599+
libcam_cfg->at(0).bufferCount = 1;
600+
libcam_cfg->at(0).stride = 0;
601601

602-
retcd = config->validate();
602+
retcd = libcam_cfg->validate();
603603
if (retcd == CameraConfiguration::Adjusted) {
604-
if (config->at(0).pixelFormat != PixelFormat::fromString(pixfmt)) {
604+
if (libcam_cfg->at(0).pixelFormat != PixelFormat::fromString(pixfmt)) {
605605
MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO
606606
, "Pixel format was adjusted to %s."
607-
, config->at(0).pixelFormat.toString().c_str());
607+
, libcam_cfg->at(0).pixelFormat.toString().c_str());
608608
} else {
609609
MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO
610610
, "Configuration adjusted.");
@@ -618,26 +618,26 @@ int cls_libcam::start_config()
618618
return -1;
619619
}
620620
sprintf(tmp4cc,"%c%c%c%c"
621-
, config->at(0).pixelFormat.fourcc() >> 0
622-
, config->at(0).pixelFormat.fourcc() >> 8
623-
, config->at(0).pixelFormat.fourcc() >> 16
624-
, config->at(0).pixelFormat.fourcc() >> 24);
621+
, libcam_cfg->at(0).pixelFormat.fourcc() >> 0
622+
, libcam_cfg->at(0).pixelFormat.fourcc() >> 8
623+
, libcam_cfg->at(0).pixelFormat.fourcc() >> 16
624+
, libcam_cfg->at(0).pixelFormat.fourcc() >> 24);
625625
pixfmt = tmp4cc;
626626

627-
if ((config->at(0).size.width != (uint)cam->cfg->width) ||
628-
(config->at(0).size.height != (uint)cam->cfg->height)) {
627+
if ((libcam_cfg->at(0).size.width != (uint)cam->cfg->width) ||
628+
(libcam_cfg->at(0).size.height != (uint)cam->cfg->height)) {
629629
MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO
630630
, "Image size adjusted from %d x %d to %d x %d"
631631
, cam->cfg->width, cam->cfg->height
632-
, config->at(0).size.width, config->at(0).size.height);
632+
, libcam_cfg->at(0).size.width, libcam_cfg->at(0).size.height);
633633
}
634634

635635
MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO
636636
, "Image size %d x %d"
637-
, config->at(0).size.width, config->at(0).size.height);
637+
, libcam_cfg->at(0).size.width, libcam_cfg->at(0).size.height);
638638

639-
cam->imgs.width = (int)config->at(0).size.width;
640-
cam->imgs.height = (int)config->at(0).size.height;
639+
cam->imgs.width = (int)libcam_cfg->at(0).size.width;
640+
cam->imgs.height = (int)libcam_cfg->at(0).size.height;
641641
cam->imgs.size_norm = (cam->imgs.width * cam->imgs.height * 3) / 2;
642642
cam->imgs.motionsize = cam->imgs.width * cam->imgs.height;
643643

@@ -646,7 +646,7 @@ int cls_libcam::start_config()
646646
log_draft();
647647

648648
config_orientation();
649-
camera->configure(config.get());
649+
camera->configure(libcam_cfg.get());
650650

651651
MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO, "Finished.");
652652

@@ -680,7 +680,7 @@ int cls_libcam::start_req()
680680
camera->requestCompleted.connect(this, &cls_libcam::req_complete);
681681
frmbuf = std::make_unique<FrameBufferAllocator>(camera);
682682

683-
retcd = frmbuf->allocate(config->at(0).stream());
683+
retcd = frmbuf->allocate(libcam_cfg->at(0).stream());
684684
if (retcd < 0) {
685685
MOTION_LOG(ERR, TYPE_VIDEO, NO_ERRNO
686686
, "Buffer allocation error.");
@@ -694,7 +694,7 @@ int cls_libcam::start_req()
694694
return -1;
695695
}
696696

697-
Stream *stream = config->at(0).stream();
697+
Stream *stream = libcam_cfg->at(0).stream();
698698
const std::vector<std::unique_ptr<FrameBuffer>> &buffers =
699699
frmbuf->buffers(stream);
700700
const std::unique_ptr<FrameBuffer> &buffer = buffers[0];
@@ -752,6 +752,7 @@ int cls_libcam::start_capture()
752752
return -1;
753753
}
754754
mydelete(camctrls);
755+
started_cam = true;
755756

756757
for (std::unique_ptr<Request> &request : requests) {
757758
retcd = req_add(request.get());
@@ -760,6 +761,7 @@ int cls_libcam::start_capture()
760761
, "Failed to queue request.");
761762
if (started_cam) {
762763
camera->stop();
764+
started_cam = false;
763765
}
764766
return -1;
765767
}
@@ -803,45 +805,44 @@ int cls_libcam::libcam_start()
803805
cam->watchdog = cam->cfg->watchdog_tmo;
804806
SLEEP(1,0);
805807

806-
started_cam = true;
807-
808808
MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO, "Camera started");
809809

810810
return 0;
811811
}
812812

813813
void cls_libcam::libcam_stop()
814814
{
815+
int indx;
815816
mydelete(convert);
816817
mydelete(params);
817818

818-
if (started_aqr) {
819-
camera->stop();
820-
}
821-
822819
if (started_req) {
823-
camera->requestCompleted.disconnect(this, &cls_libcam::req_complete);
824820
while (req_queue.empty() == false) {
825821
req_queue.pop();
826822
}
823+
camera->requestCompleted.disconnect(this, &cls_libcam::req_complete);
824+
frmbuf->free(libcam_cfg->at(0).stream());
825+
frmbuf.reset();
826+
for (indx=0;indx<requests.size();indx++) {
827+
requests[indx].reset();
828+
}
827829
requests.clear();
828-
829-
frmbuf->free(config->at(0).stream());
830+
munmap(membuf.buf, membuf.bufsz);
831+
started_req = false;
830832
}
831833

832-
if (started_aqr){
834+
if (started_cam) {
835+
camera->stop();
833836
camera->release();
837+
camera.reset();
838+
started_cam = false;
834839
}
835840

836841
if (started_mgr) {
837842
cam_mgr->stop();
843+
started_mgr = false;
838844
}
839845

840-
started_cam = false;
841-
started_mgr = false;
842-
started_aqr = false;
843-
started_req = false;
844-
845846
cam->device_status = STATUS_CLOSED;
846847
MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO, "Camera stopped.");
847848
}

src/libcam.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
std::unique_ptr<libcamera::CameraManager> cam_mgr;
4747
std::shared_ptr<libcamera::Camera> camera;
48-
std::unique_ptr<libcamera::CameraConfiguration> config;
48+
std::unique_ptr<libcamera::CameraConfiguration> libcam_cfg;
4949
std::unique_ptr<libcamera::FrameBufferAllocator> frmbuf;
5050
std::vector<std::unique_ptr<libcamera::Request>> requests;
5151

0 commit comments

Comments
 (0)