[libcamera-devel,27/30] cam: Allow specifying directories in the --file option
diff mbox series

Message ID 20210707021941.20804-28-laurent.pinchart@ideasonboard.com
State Superseded
Headers show
Series
  • Multi-camera support in the cam application
Related show

Commit Message

Laurent Pinchart July 7, 2021, 2:19 a.m. UTC
The value of the --file option is the full name of the file to which
captured frames are written. To write files to a specific directory with
the default naming scheme, the "frame-#.bin" name has to appear at the
end of the file name. Simplify usage of the option by allowing --file to
specify a directory only. If the file name ends with a '/', the default
"frame-#.bin" file name is automatically appended.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/cam/buffer_writer.cpp | 7 ++++++-
 src/cam/buffer_writer.h   | 2 +-
 src/cam/main.cpp          | 5 ++++-
 3 files changed, 11 insertions(+), 3 deletions(-)

Comments

Kieran Bingham July 12, 2021, 4:07 p.m. UTC | #1
On 07/07/2021 03:19, Laurent Pinchart wrote:
> The value of the --file option is the full name of the file to which
> captured frames are written. To write files to a specific directory with
> the default naming scheme, the "frame-#.bin" name has to appear at the
> end of the file name. Simplify usage of the option by allowing --file to
> specify a directory only. If the file name ends with a '/', the default
> "frame-#.bin" file name is automatically appended.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

I wonder if the camera id should be encoded by the # as well as the
stream name ... but maybe that's not needed if multiple cameras will
always write to a separate directory...

Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>


> ---
>  src/cam/buffer_writer.cpp | 7 ++++++-
>  src/cam/buffer_writer.h   | 2 +-
>  src/cam/main.cpp          | 5 ++++-
>  3 files changed, 11 insertions(+), 3 deletions(-)
> 
> diff --git a/src/cam/buffer_writer.cpp b/src/cam/buffer_writer.cpp
> index f2b21aef3362..a7648a92fc92 100644
> --- a/src/cam/buffer_writer.cpp
> +++ b/src/cam/buffer_writer.cpp
> @@ -49,7 +49,12 @@ int BufferWriter::write(FrameBuffer *buffer, const std::string &streamName)
>  	size_t pos;
>  	int fd, ret = 0;
>  
> -	filename = pattern_;
> +	if (!pattern_.empty())
> +		filename = pattern_;
> +
> +	if (filename.empty() || filename.back() == '/')
> +		filename += "frame-#.bin";
> +
>  	pos = filename.find_first_of('#');
>  	if (pos != std::string::npos) {
>  		std::stringstream ss;
> diff --git a/src/cam/buffer_writer.h b/src/cam/buffer_writer.h
> index 604ce8702ec7..7626de42d369 100644
> --- a/src/cam/buffer_writer.h
> +++ b/src/cam/buffer_writer.h
> @@ -15,7 +15,7 @@
>  class BufferWriter
>  {
>  public:
> -	BufferWriter(const std::string &pattern = "frame-#.bin");
> +	BufferWriter(const std::string &pattern = "");
>  	~BufferWriter();
>  
>  	void mapBuffer(libcamera::FrameBuffer *buffer);
> diff --git a/src/cam/main.cpp b/src/cam/main.cpp
> index a3f19d0fa61b..6d7d45e11499 100644
> --- a/src/cam/main.cpp
> +++ b/src/cam/main.cpp
> @@ -118,7 +118,10 @@ int CamApp::parseOptions(int argc, char *argv[])
>  			 "capture", ArgumentOptional, "count");
>  	parser.addOption(OptFile, OptionString,
>  			 "Write captured frames to disk\n"
> -			 "The first '#' character in the file name is expanded to the stream name and frame sequence number.\n"
> +			 "If the file name ends with a '/', it sets the directory in which\n"
> +			 "to write files, using the default file name. Otherwise it sets the\n"
> +			 "full file path and name. The first '#' character in the file name\n"
> +			 "is expanded to the stream name and frame sequence number.\n"
>  			 "The default file name is 'frame-#.bin'.",
>  			 "file", ArgumentOptional, "filename");
>  	parser.addOption(OptStream, &streamKeyValue,
>

Patch
diff mbox series

diff --git a/src/cam/buffer_writer.cpp b/src/cam/buffer_writer.cpp
index f2b21aef3362..a7648a92fc92 100644
--- a/src/cam/buffer_writer.cpp
+++ b/src/cam/buffer_writer.cpp
@@ -49,7 +49,12 @@  int BufferWriter::write(FrameBuffer *buffer, const std::string &streamName)
 	size_t pos;
 	int fd, ret = 0;
 
-	filename = pattern_;
+	if (!pattern_.empty())
+		filename = pattern_;
+
+	if (filename.empty() || filename.back() == '/')
+		filename += "frame-#.bin";
+
 	pos = filename.find_first_of('#');
 	if (pos != std::string::npos) {
 		std::stringstream ss;
diff --git a/src/cam/buffer_writer.h b/src/cam/buffer_writer.h
index 604ce8702ec7..7626de42d369 100644
--- a/src/cam/buffer_writer.h
+++ b/src/cam/buffer_writer.h
@@ -15,7 +15,7 @@ 
 class BufferWriter
 {
 public:
-	BufferWriter(const std::string &pattern = "frame-#.bin");
+	BufferWriter(const std::string &pattern = "");
 	~BufferWriter();
 
 	void mapBuffer(libcamera::FrameBuffer *buffer);
diff --git a/src/cam/main.cpp b/src/cam/main.cpp
index a3f19d0fa61b..6d7d45e11499 100644
--- a/src/cam/main.cpp
+++ b/src/cam/main.cpp
@@ -118,7 +118,10 @@  int CamApp::parseOptions(int argc, char *argv[])
 			 "capture", ArgumentOptional, "count");
 	parser.addOption(OptFile, OptionString,
 			 "Write captured frames to disk\n"
-			 "The first '#' character in the file name is expanded to the stream name and frame sequence number.\n"
+			 "If the file name ends with a '/', it sets the directory in which\n"
+			 "to write files, using the default file name. Otherwise it sets the\n"
+			 "full file path and name. The first '#' character in the file name\n"
+			 "is expanded to the stream name and frame sequence number.\n"
 			 "The default file name is 'frame-#.bin'.",
 			 "file", ArgumentOptional, "filename");
 	parser.addOption(OptStream, &streamKeyValue,