From patchwork Thu Dec 20 16:44:44 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 63 Return-Path: Received: from relay10.mail.gandi.net (relay10.mail.gandi.net [217.70.178.230]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 3E2EA60B1F for ; Thu, 20 Dec 2018 17:45:06 +0100 (CET) Received: from w540.lan (2-224-242-101.ip172.fastwebnet.it [2.224.242.101]) (Authenticated sender: jacopo@jmondi.org) by relay10.mail.gandi.net (Postfix) with ESMTPSA id BECF2240017; Thu, 20 Dec 2018 16:45:05 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Thu, 20 Dec 2018 17:44:44 +0100 Message-Id: <1545324285-16730-2-git-send-email-jacopo@jmondi.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1545324285-16730-1-git-send-email-jacopo@jmondi.org> References: <1545324285-16730-1-git-send-email-jacopo@jmondi.org> Subject: [libcamera-devel] [PATCH 1/2] libcamera: Add MediaObject class hierarchy 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, 20 Dec 2018 16:45:06 -0000 Add a class hierarcy to represent all media objects a media graph represents. Add a base MediaObject class, which retains the global unique object id, and define the derived MediaEntity, MediaLink and MediaPad classes. This hierarchy will be used by the MediaDevice objects which represents and handles the media graph. Signed-off-by: Jacopo Mondi --- src/libcamera/include/media_object.h | 120 ++++++++++++++ src/libcamera/media_object.cpp | 302 +++++++++++++++++++++++++++++++++++ src/libcamera/meson.build | 1 + 3 files changed, 423 insertions(+) create mode 100644 src/libcamera/include/media_object.h create mode 100644 src/libcamera/media_object.cpp diff --git a/src/libcamera/include/media_object.h b/src/libcamera/include/media_object.h new file mode 100644 index 0000000..bbacb05 --- /dev/null +++ b/src/libcamera/include/media_object.h @@ -0,0 +1,120 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2018, Google Inc. + * + * media_object.h - Media Device objects: entities, pads and links. + */ +#ifndef __LIBCAMERA_MEDIA_OBJECT_H__ +#define __LIBCAMERA_MEDIA_OBJECT_H__ + +#include +#include +#include +#include + +#include + +namespace libcamera { + +class MediaDevice; +class MediaEntity; + +class MediaObject +{ +public: + MediaObject(unsigned int id) : id_(id) { } + virtual ~MediaObject() { } + + unsigned int id() { return id_; } + +protected: + unsigned int id_; +}; + +class MediaLink : public MediaObject +{ + friend class MediaDevice; + +public: + ~MediaLink() { } + + unsigned int source() { return source_; } + unsigned int sink() { return sink_; } + unsigned int flags() { return flags_; } + void setFlags(unsigned int flags) { flags_ = flags; } + +private: + MediaLink(const struct media_v2_link *link); + MediaLink(const MediaLink &) = delete; + + unsigned int source_; + unsigned int sink_; + unsigned int flags_; +}; + +class MediaPad : public MediaObject +{ + friend class MediaDevice; + +public: + ~MediaPad(); + + unsigned int index() { return index_; } + unsigned int entity() { return entity_; } + unsigned int flags() { return flags_; } + std::vector &links() { return links_; } + + void addLink(MediaLink *link); + +private: + MediaPad(const struct media_v2_pad *pad); + MediaPad(const MediaPad &) = delete; + + unsigned int index_; + unsigned int entity_; + unsigned int flags_; + + std::vector links_; +}; + +class MediaEntity : public MediaObject +{ + friend class MediaDevice; + +public: + bool operator==(unsigned int id) const { return this->id_ == id; } + bool operator!=(unsigned int id) const { return this->id_ != id; } + bool operator==(std::string name) const + { + return !name_.compare(name); + } + + std::string name() { return name_; } + std::vector &sources() { return sources_; } + std::vector &sinks() { return sinks_; } + + MediaPad *getPadByIndex(unsigned int index); + MediaPad *getPadById(unsigned int id); + +private: + MediaEntity(const struct media_v2_entity *entity); + MediaEntity(const MediaEntity &) = delete; + ~MediaEntity(); + + std::string name_; + std::string path_; + + std::vector sources_; + std::vector sinks_; + + int setDevice(const std::string &path); + + void addPad(MediaPad *pad); + + MediaPad *__getPad(std::vector &v, + std::function f); + MediaPad *getPad(std::function f); +}; + +} /* namespace libcamera */ +#endif /* __LIBCAMERA_MEDIA_OBJECT_H__ */ diff --git a/src/libcamera/media_object.cpp b/src/libcamera/media_object.cpp new file mode 100644 index 0000000..740a5fb --- /dev/null +++ b/src/libcamera/media_object.cpp @@ -0,0 +1,302 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2018, Google Inc. + * + * media_object.cpp - Media device objects: entities, pads and links + */ + +#include +#include +#include +#include + +#include +#include +#include + +#include + +#include "log.h" +#include "media_object.h" + +/** + * \file media_object.h + */ +namespace libcamera { + +/** + * \class MediaObject + * \brief Base class for all media object types + * + * Defines a simple base class for all media objects with a simple + * unique id. + */ + +/** + * \fn MediaObject::MediaObject(unsigned int id) + * \brief Construct a MediaObject with id \a id + * \param id The globally unique object's id as returned by MEDIA_IOC_G_TOPOLOGY + */ + +/** + * \fn MediaObject::id() + * \brief Return the object's globally unique id /ref id_ + */ + +/** + * \var MediaObject::id_ + * \brief The MediaObject unique id as returned by MEDIA_IOC_G_TOPOLOGY + */ + +/** + * \class MediaLink + * \brief A Media Link object + * + * A MediaLink object represents a media link between two entities + */ + +/** + * \fn MediaLink::MediaLink(const struct media_v2_link *link) + * \brief Construct a MediaLink with informations from \a link + * \param link The media link representation as returned by + * MEDIA_IOC_G_TOPOLOGY + */ +MediaLink::MediaLink(const struct media_v2_link *link) : MediaObject(link->id), + source_(link->source_id), + sink_(link->sink_id), + flags_(link->flags) { } +/** + * \fn MediaLink::source() + * \brief Return the source pad id + */ + +/** + * \fn MediaLink::sink() + * \brief Return the sink pad id + */ + +/** + * \fn MediaLink::flags() + * \brief Return the link flags + */ + +/** + * \fn MediaLink::setFlags(unsigned int flags) + * \brief Set the link flags to \a flags + * \param flags The flags to be applied to the link + */ + +/** + * \class MediaPad + * \brief Media Pad object + * + * A MediaPad object represents a media pad with its associated links + */ + +/** + * \fn MediaPad::MediaPad(const struct media_v2_pad *pad) + * \brief Create a MediaPad object + * \param mediaPad The media pad representation as returned by + * MEDIA_IOC_G_TOPOLOGY + */ +MediaPad::MediaPad(const struct media_v2_pad *pad) : MediaObject(pad->id), + index_(pad->index), + entity_(pad->entity_id), + flags_(pad->flags) { } +MediaPad::~MediaPad() +{ + links_.clear(); +} + +/** + * \fn MediaPad::index() + * \brief Return the 0-indexed pad index + */ + +/** + * \fn MediaPad::entity() + * \brief Return the entity id this pad belongs to + */ + +/** + * \fn MediaPad::flags() + * \brief Return the pad flags (MEDIA_PAD_FL_*) + */ + +/** + * \fn MediaPad::links() + * \brief Return all outbound and inbound links from/to this pad + */ + +/** + * \fn MediaPad::addLink(MediaLink *link) + * \brief Add a new outbound or inbound link from/to this pad + * \param link The new link to add + */ +void MediaPad::addLink(MediaLink *link) +{ + links_.push_back(link); +} + +/** + * \class MediaEntity + * \brief Media entity object + * + * A MediaEntity object represents a media entity with its id, name and its + * associated pads + */ + +/** + * \fn MediaEntity::operator==(unsigned int id) const + * \brief Compare entities by id (check if they're equal) + * \param id The entity id to compare with + */ + +/** + * \fn MediaEntity::operator!=(unsigned int id) const + * \brief Compare entities by id (check if they're not equal) + * \param id The entity id to compare with + */ + +/** + * \fn MediaEntity::operator==(std::string name) const + * \brief Compare entities by name (check if they're equal) + * \param name The entity name to compare with + */ + +/** + * \fn MediaEntity::name() + * \brief Return the entity name + */ + +/** + * \fn MediaEntity::sources() + * \brief Get all source pads + */ + +/** + * \fn MediaEntity::sinks() + * \brief Get all sink pads + */ + +/** + * \fn MediaEntity::getPadByIndex(unsigned int index) + * \brief Get a pad in this entity by its index + * \param index The pad index (starting from 0) + */ +MediaPad *MediaEntity::getPadByIndex(unsigned int index) +{ + return getPad([&index](MediaPad *p) -> bool { return p->index() == index; }); +} + +/** + * \fn MediaEntity::getPadById(unsigned int id) + * \brief Get a pad in this entity by its id + * \param id The pad globally unique id + */ +MediaPad *MediaEntity::getPadById(unsigned int id) +{ + return getPad([&id](MediaPad *p) -> bool { return p->id() == id; }); +} + +/** + * \fn MediaEntity::MediaEntity(const struct media_v2_entity *entity) + * \brief Construct a MediaEntity with informations from \a entity + * \param entity The media entity representation as returned by + * MEDIA_IOC_G_TOPOLOGY + */ +MediaEntity::MediaEntity(const struct media_v2_entity *entity) : + MediaObject(entity->id), + name_(entity->name) { } + +/** + * \fn MediaEntity::~MediaEntity() + * \brief Release memory for all pads and links + */ +MediaEntity::~MediaEntity() +{ + for (MediaPad *s : sources_) + delete s; + for (MediaPad *s : sinks_) + delete s; + + sources_.clear(); + sinks_.clear(); +} + +/** + * \var MediaEntity::sources_ + * \brief The MediaPad sources vector + */ + +/** + * \var MediaEntity::sinks_ + * \brief The MediaPad sinks vector + */ + +/** + * \fn MediaEntity::setDevice(const std::string &path) + * \brief Set the entity video (sub)device node path + * \param path The video (sub)device node path associated with this entity + */ +int MediaEntity::setDevice(const std::string &path) +{ + /* Make sure the path exists first. */ + struct stat pstat; + int ret = ::stat(const_cast(path.c_str()), &pstat); + if (ret < 0) { + LOG(Error) << "Unable to open: " << path << " : " + << strerror(errno); + return -errno; + } + + path_ = path; + + return 0; +} + +/** + * \fn MediaEntity::addPad(MediaPad *pad) + * \brief Add pad \a pad to \ref sources_ or \ref sinks_ + * \param pad The pad to add + */ +void MediaEntity::addPad(MediaPad *pad) +{ + std::vector *pads = + pad->flags() & MEDIA_PAD_FL_SOURCE ? + &sources_ : &sinks_; + pads->push_back(pad); +} + +/** + * \fn MediaEntity::__getPad(std::vector &v, + * std::function f) + * \brief Find MediaPad the satisfies predicates \a f in the pad vector \v + * \param v The std::vector to search in + * \param f The predicate the pad has to satisfy + */ +MediaPad *MediaEntity::__getPad(std::vector &v, + std::function f) +{ + std::vector::iterator it = v.begin(); + while (it != sources_.end()) { + if (f(*it)) + return *it; + ++it; + } + + return nullptr; +} + +/** + * \fn MediaEntity::getPad(std::function f) + * \brief Run predicate \a f on both \ref sources_ and \ref sinks_ + * \param f The predicate the pad has to satisfy + */ +MediaPad *MediaEntity::getPad(std::function f) +{ + MediaPad *_p = __getPad(sources_, f); + return (_p != nullptr ? _p : __getPad(sinks_, f)); +} + +} /* namespace libcamera */ diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index f632eb5..da06eba 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -1,6 +1,7 @@ libcamera_sources = files([ 'log.cpp', 'main.cpp', + 'media_object.cpp', ]) libcamera_headers = files([