From 0ab73d70395747c31c15557501e27d13bf278418 Mon Sep 17 00:00:00 2001 From: Jacopo Mondi Date: Mon, 7 Oct 2024 11:19:51 +0200 Subject: WIP Signed-off-by: Jacopo Mondi --- include/libcamera/internal/yaml_emitter.h | 98 ++++++++++++++++- src/libcamera/pipeline_handler.cpp | 57 ++++++---- src/libcamera/yaml_emitter.cpp | 176 ++++++++++++++++++++++++------ 3 files changed, 273 insertions(+), 58 deletions(-) diff --git a/include/libcamera/internal/yaml_emitter.h b/include/libcamera/internal/yaml_emitter.h index f8df5265..14ea8241 100644 --- a/include/libcamera/internal/yaml_emitter.h +++ b/include/libcamera/internal/yaml_emitter.h @@ -9,7 +9,9 @@ #include #include +#include #include +#include #include #include @@ -21,15 +23,105 @@ namespace libcamera { class File; class YamlEvent; +class YamlEmitter; +class YamlDict; +class YamlList; + +class YamlOutput +{ +protected: + enum class YamlType { + Root, + Scalar, + Dict, + List, + }; + +public: + virtual ~YamlOutput() {} + virtual void close() {} + + std::unique_ptr dict(); + std::unique_ptr list(); + + YamlType type() { return type_; } + +protected: + YamlOutput(YamlEmitter *emitter, YamlType type) + : emitter_(emitter), type_(type) + { + } + + int emitScalar(std::string_view scalar); + int emitMappingStart(); + int emitMappingEnd(); + int emitSequenceStart(); + int emitSequenceEnd(); + + YamlEmitter *emitter_; + YamlType type_; +}; + +class YamlRoot : public YamlOutput +{ +public: + ~YamlRoot() {} + + std::unique_ptr list(); + std::unique_ptr dict(); + void scalar(std::string_view scalar); + +private: + friend class YamlEmitter; + + YamlRoot(YamlEmitter *emitter) + : YamlOutput(emitter, YamlType::Root) + { + emitterRoot_ = std::unique_ptr(emitter); + } + + std::unique_ptr emitterRoot_; +}; + +class YamlList : public YamlOutput +{ +public: + ~YamlList(); + + std::unique_ptr list(); + std::unique_ptr dict(); + void scalar(std::string_view scalar); + +private: + friend class YamlOutput; + + YamlList(YamlEmitter *emitter); +}; + + +class YamlDict : public YamlOutput +{ +public: + ~YamlDict(); + + void scalar(std::string_view key, std::string_view value); + std::unique_ptr list(std::string_view key); + std::unique_ptr dict(std::string_view key); + +private: + friend class YamlOutput; + + YamlDict(YamlEmitter *emitter); +}; + class YamlEmitter final { public: ~YamlEmitter(); - static std::unique_ptr create(std::string &path); + static std::unique_ptr root(std::string &path); - int emit(std::map); - int emit(const std::string &scalar); + int emit(YamlEvent *event); private: LIBCAMERA_DISABLE_COPY(YamlEmitter); diff --git a/src/libcamera/pipeline_handler.cpp b/src/libcamera/pipeline_handler.cpp index 5fd55edb..7e1c4f3f 100644 --- a/src/libcamera/pipeline_handler.cpp +++ b/src/libcamera/pipeline_handler.cpp @@ -788,40 +788,51 @@ void PipelineHandler::disconnect() * \return The CameraManager for this pipeline handler */ -void PipelineHandler::dumpConfiguration(const std::set &streams, - const Orientation &orientation) +void PipelineHandler::dumpConfiguration([[maybe_unused]] const std::set &streams, + [[maybe_unused]] const Orientation &orientation) { std::string path("/tmp/yaml_emitter.yaml"); - auto emitter = YamlEmitter::create(path); - std::map map = { { "cane", "gatto" }, }; - emitter->emit(map); + auto root = YamlEmitter::root(path); - if (!dumpCaptureScript_) - return; - - std::stringstream ss; - ss << "configuration:" << std::endl; - ss << " orientation: " << orientation << std::endl; + auto rootDict = root->dict(); + auto config = rootDict->dict("configuration"); + config->scalar("orientation", "front"); - /* TODO Dump Sensor configuration */ + auto yamlStreams = config->list("streams"); - ss << " streams:" << std::endl; for (const auto &stream : streams) { const StreamConfiguration &streamConfig = stream->configuration(); - ss << " - pixelFormat: " << streamConfig.pixelFormat << std::endl; - ss << " size: " << streamConfig.size << std::endl; - ss << " stride: " << streamConfig.stride << std::endl; - ss << " frameSize: " << streamConfig.frameSize << std::endl; - ss << " bufferCount: " << streamConfig.bufferCount << std::endl; + auto yamlStream = yamlStreams->dict(); + + yamlStream->scalar("pixelformat", "YUYV"); + yamlStream->scalar("size", "1920x1080"); + if (streamConfig.colorSpace) - ss << " colorSpace: " << streamConfig.colorSpace->toString() << std::endl; + LOG(Error); } - dumpCaptureScript_->write(ss.str().c_str(), ss.str().size()); +#if 0 + auto yamlStreams = std::unique_ptr(config->list("streams")); - std::string str = "frames:\n"; - dumpCaptureScript_->write(str.c_str(), str.size()); - dumpCaptureScript_->flush(); + /* TODO Dump Sensor configuration */ + + for (const auto &stream : streams) { + const StreamConfiguration &streamConfig = stream->configuration(); + auto yamlStream = std::unique_ptr(yamlStreams->dict("")); + + yamlStream->append("pixelformat", "YUYV"); + yamlStream->append("size", "1920x1080"); + + //ss << " - pixelFormat: " << streamConfig.pixelFormat << std::endl; + //ss << " size: " << streamConfig.size << std::endl; + //ss << " stride: " << streamConfig.stride << std::endl; + //ss << " frameSize: " << streamConfig.frameSize << std::endl; + //ss << " bufferCount: " << streamConfig.bufferCount << std::endl; + if (streamConfig.colorSpace) + LOG(Error); + // ss << " colorSpace: " << streamConfig.colorSpace->toString() << std::endl; + } +#endif } void PipelineHandler::dumpRequest(Request *request, DumpMode mode) diff --git a/src/libcamera/yaml_emitter.cpp b/src/libcamera/yaml_emitter.cpp index 5683d1d4..0da0fe68 100644 --- a/src/libcamera/yaml_emitter.cpp +++ b/src/libcamera/yaml_emitter.cpp @@ -68,6 +68,141 @@ std::unique_ptr YamlEvent::create() return std::unique_ptr(new YamlEvent(), Deleter()); } +std::unique_ptr YamlOutput::dict() +{ + return std::unique_ptr(new YamlDict(emitter_)); +} + +std::unique_ptr YamlOutput::list() +{ + return std::unique_ptr(new YamlList(emitter_)); +} + +int YamlOutput::emitScalar(std::string_view scalar) +{ + LOG(Error) << "SCALAR: " << scalar; + + std::unique_ptr event = YamlEvent::create(); + + const unsigned char *value = reinterpret_cast + (scalar.data()); + yaml_scalar_event_initialize(event->event(), NULL, NULL, value, + scalar.length(), true, false, + YAML_PLAIN_SCALAR_STYLE); + return emitter_->emit(event.get()); +} + +int YamlOutput::emitMappingStart() +{ + LOG(Error) << "MAP START"; + std::unique_ptr event = YamlEvent::create(); + yaml_mapping_start_event_initialize(event->event(), NULL, NULL, + true, YAML_BLOCK_MAPPING_STYLE); + return emitter_->emit(event.get()); +} + +int YamlOutput::emitMappingEnd() +{ + LOG(Error) << "MAP END"; + std::unique_ptr event = YamlEvent::create(); + yaml_mapping_end_event_initialize(event->event()); + return emitter_->emit(event.get()); +} + +int YamlOutput::emitSequenceStart() +{ + LOG(Error) << "LIST START"; + std::unique_ptr event = YamlEvent::create(); + yaml_sequence_start_event_initialize(event->event(), NULL, NULL, true, + YAML_BLOCK_SEQUENCE_STYLE); + return emitter_->emit(event.get()); +} + +int YamlOutput::emitSequenceEnd() +{ + LOG(Error) << "LIST END"; + std::unique_ptr event = YamlEvent::create(); + yaml_sequence_end_event_initialize(event->event()); + return emitter_->emit(event.get()); +} + +std::unique_ptr YamlRoot::dict() +{ + emitMappingStart(); + + return YamlOutput::dict(); +} + +std::unique_ptr YamlRoot::list() +{ + emitSequenceStart(); + + return YamlOutput::list(); + +} + +YamlList::YamlList(YamlEmitter *emitter) + : YamlOutput(emitter, YamlType::List) +{ +} + +YamlList::~YamlList() +{ + emitSequenceEnd(); +} + +void YamlList::scalar(std::string_view scalar) +{ + emitScalar(scalar); +} + +std::unique_ptr YamlList::list() +{ + emitSequenceStart(); + + return YamlOutput::list(); +} + +std::unique_ptr YamlList::dict() +{ + emitMappingStart(); + + return YamlOutput::dict(); +} + +YamlDict::YamlDict(YamlEmitter *emitter) + : YamlOutput(emitter, YamlType::Dict) +{ +} + +YamlDict::~YamlDict() +{ + LOG(Error) << "~Dict"; + emitMappingEnd(); +} + +void YamlDict::scalar(std::string_view key, std::string_view value) +{ + emitScalar(key); + emitScalar(value); +} + +std::unique_ptr YamlDict::list(std::string_view key) +{ + emitScalar(key); + emitSequenceStart(); + + return YamlOutput::list(); +} + +std::unique_ptr YamlDict::dict(std::string_view key) +{ + emitScalar(key); + emitMappingStart(); + + return YamlOutput::dict(); +} + YamlEmitter::Emitter::~Emitter() { yaml_emitter_delete(&emitter_); @@ -118,9 +253,11 @@ YamlEmitter::~YamlEmitter() { std::unique_ptr event = YamlEvent::create(); + LOG(Error) << "DOC END"; yaml_document_end_event_initialize(event->event(), 0); emitter_.emit(event.get()); + LOG(Error) << "STREAM END"; yaml_stream_end_event_initialize(event->event()); emitter_.emit(event.get()); } @@ -131,15 +268,17 @@ void YamlEmitter::init() std::unique_ptr event = YamlEvent::create(); + LOG(Error) << "STREAM START"; yaml_stream_start_event_initialize(event->event(), YAML_UTF8_ENCODING); emitter_.emit(event.get()); + LOG(Error) << "DOC START"; yaml_document_start_event_initialize(event->event(), NULL, NULL, NULL, 0); emitter_.emit(event.get()); } -std::unique_ptr YamlEmitter::create(std::string &path) +std::unique_ptr YamlEmitter::root(std::string &path) { YamlEmitter *emitter = new YamlEmitter(); @@ -148,40 +287,13 @@ std::unique_ptr YamlEmitter::create(std::string &path) emitter->init(); - return std::unique_ptr(emitter); + YamlRoot *root = new YamlRoot(emitter); + return std::unique_ptr(root); } -int YamlEmitter::emit(std::map map) +int YamlEmitter::emit(YamlEvent *event) { - std::unique_ptr event = YamlEvent::create(); - - auto iter = map.begin(); - - yaml_mapping_start_event_initialize(event->event(), NULL, NULL, - true, YAML_BLOCK_MAPPING_STYLE); - emitter_.emit(event.get()); - - emit(iter->first); - emit(iter->second); - - yaml_mapping_end_event_initialize(event->event()); - emitter_.emit(event.get()); - - return 0; -} - -int YamlEmitter::emit(const std::string &scalar) -{ - const unsigned char *value = reinterpret_cast - (scalar.c_str()); - std::unique_ptr event = YamlEvent::create(); - - yaml_scalar_event_initialize(event->event(), NULL, NULL, value, - scalar.length(), true, false, - YAML_PLAIN_SCALAR_STYLE); - emitter_.emit(event.get()); - - return 0; + return emitter_.emit(event); } } /* namespace libcamera */ -- cgit v1.2.1