From patchwork Thu Jan 31 23:47:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 468 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 8658160DB4 for ; Fri, 1 Feb 2019 00:47:47 +0100 (CET) Received: from pendragon.ideasonboard.com (85-76-34-136-nat.elisa-mobile.fi [85.76.34.136]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id B8B8C41; Fri, 1 Feb 2019 00:47:45 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1548978467; bh=xFIyg9zzZw2AK7ncALS6AHk2bxVSGkTFDnwm+ZaO8Co=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dAeACrhg3exnB3aGyQhziU9kjF5WFQt8SvEAUQDGtPTFUsYCjt6Y/+Hi+s0eovvZN X3ofSJMdAb9KnsiJysqAvLvTEJPbZrhgwHpabqF5ss51LSO3fw1JupIiXQTbgh4tMk 6adFl1/4/re9zzU1bZijNPuHZGrh3HnW60WBc1iA= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Fri, 1 Feb 2019 01:47:21 +0200 Message-Id: <20190131234721.22606-9-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20190131234721.22606-1-laurent.pinchart@ideasonboard.com> References: <20190131234721.22606-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 8/8] cam: Add --format option to configure a stream X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 31 Jan 2019 23:47:47 -0000 From: Niklas Söderlund Add an option to configure the first stream of a camera from an argument with options and parse the width, height and pixel format from that list. The pixel format is still specified as a integer which should correspond to the kernels FOURCC identifiers. Going forward this should be turned into a string representation and the cam parser should translate between the two. Signed-off-by: Niklas Söderlund Signed-off-by: Laurent Pinchart --- Changes since v1: - Don't rename OptCamera option - Reword the format options help texts - Use the new KeyValueParser integration --- src/cam/main.cpp | 103 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 91 insertions(+), 12 deletions(-) diff --git a/src/cam/main.cpp b/src/cam/main.cpp index 7934d0bf4132..02d9b86f546f 100644 --- a/src/cam/main.cpp +++ b/src/cam/main.cpp @@ -21,6 +21,7 @@ OptionsParser::Options options; enum { OptCamera = 'c', + OptFormat = 'f', OptHelp = 'h', OptList = 'l', }; @@ -35,11 +36,20 @@ void signalHandler(int signal) static int parseOptions(int argc, char *argv[]) { - OptionsParser parser; + KeyValueParser formatKeyValue; + formatKeyValue.addOption("width", OptionInteger, "Width in pixels", + ArgumentRequired); + formatKeyValue.addOption("height", OptionInteger, "Height in pixels", + ArgumentRequired); + formatKeyValue.addOption("pixelformat", OptionInteger, "Pixel format", + ArgumentRequired); + OptionsParser parser; parser.addOption(OptCamera, OptionString, "Specify which camera to operate on", "camera", ArgumentRequired, "camera"); + parser.addOption(OptFormat, &formatKeyValue, + "Set format of the camera's first stream", "format"); parser.addOption(OptHelp, OptionNone, "Display this help message", "help"); parser.addOption(OptList, OptionNone, "List all cameras", "list"); @@ -56,6 +66,47 @@ static int parseOptions(int argc, char *argv[]) return 0; } +int str2uint(std::string str, unsigned int *dest) +{ + if (!dest || str == "" || !sscanf(str.c_str(), "%d", dest)) + return -EINVAL; + return 0; +} + +bool configureStreams(Camera *camera, std::vector &streams) +{ + KeyValueParser::Options format = options[OptFormat]; + + if (streams.size() != 1) { + std::cout << "Camera has " << streams.size() + << " streams, I only know how to work with 1" + << std::endl; + return false; + } + Stream *id = streams.front(); + + std::map config = + camera->streamConfiguration(streams); + + if (format.isSet("width")) + if (str2uint(format["width"], &config[id].width)) + return false; + + if (format.isSet("height")) + if (str2uint(format["height"], &config[id].height)) + return false; + + /* TODO: Translate 4CC string to ID. */ + if (format.isSet("pixelformat")) + if (str2uint(format["pixelformat"], &config[id].pixelFormat)) + return false; + + if (camera->configureStreams(config)) + return false; + + return true; +} + int main(int argc, char **argv) { int ret; @@ -65,6 +116,8 @@ int main(int argc, char **argv) return EXIT_FAILURE; CameraManager *cm = CameraManager::instance(); + std::shared_ptr camera; + std::vector streams; ret = cm->start(); if (ret) { @@ -73,31 +126,57 @@ int main(int argc, char **argv) return EXIT_FAILURE; } + loop = new EventLoop(cm->eventDispatcher()); + + struct sigaction sa = {}; + sa.sa_handler = &signalHandler; + sigaction(SIGINT, &sa, nullptr); + if (options.isSet(OptList)) { std::cout << "Available cameras:" << std::endl; - for (const std::shared_ptr &camera : cm->cameras()) - std::cout << "- " << camera->name() << std::endl; + for (const std::shared_ptr &cam : cm->cameras()) + std::cout << "- " << cam->name() << std::endl; } if (options.isSet(OptCamera)) { - std::shared_ptr cam = cm->get(options[OptCamera]); - - if (cam) { - std::cout << "Using camera " << cam->name() << std::endl; - } else { + camera = cm->get(options[OptCamera]); + if (!camera) { std::cout << "Camera " << options[OptCamera] << " not found" << std::endl; + goto out; + } + + streams = camera->streams(); + + if (camera->acquire()) { + std::cout << "Failed to acquire camera" << std::endl; + goto out; } + + std::cout << "Using camera " << camera->name() << std::endl; } - loop = new EventLoop(cm->eventDispatcher()); + if (options.isSet(OptFormat)) { + if (!camera) { + std::cout << "Can't configure stream, no camera selected" + << std::endl; + goto out_camera; + } - struct sigaction sa = {}; - sa.sa_handler = &signalHandler; - sigaction(SIGINT, &sa, nullptr); + if (!configureStreams(camera.get(), streams)) { + std::cout << "Failed to configure camera" << std::endl; + goto out_camera; + } + } ret = loop->exec(); +out_camera: + if (camera) { + camera->release(); + camera.reset(); + } +out: delete loop; cm->stop();