From patchwork Fri Dec 28 07:57:40 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 91 Return-Path: Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net [217.70.183.201]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id E61FE600CC for ; Fri, 28 Dec 2018 08:57:58 +0100 (CET) X-Originating-IP: 2.224.242.101 Received: from uno.lan (2-224-242-101.ip172.fastwebnet.it [2.224.242.101]) (Authenticated sender: jacopo@jmondi.org) by relay8-d.mail.gandi.net (Postfix) with ESMTPSA id 75FCC1BF206; Fri, 28 Dec 2018 07:57:58 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Fri, 28 Dec 2018 08:57:40 +0100 Message-Id: <20181228075743.28637-2-jacopo@jmondi.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20181228075743.28637-1-jacopo@jmondi.org> References: <20181228075743.28637-1-jacopo@jmondi.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH RESEND v2 1/4] 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: Fri, 28 Dec 2018 07:57:59 -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 | 117 +++++++++++ src/libcamera/media_object.cpp | 302 +++++++++++++++++++++++++++ src/libcamera/meson.build | 1 + 3 files changed, 420 insertions(+) create mode 100644 src/libcamera/include/media_object.h create mode 100644 src/libcamera/media_object.cpp -- 2.20.1 diff --git a/src/libcamera/include/media_object.h b/src/libcamera/include/media_object.h new file mode 100644 index 0000000..99913eb --- /dev/null +++ b/src/libcamera/include/media_object.h @@ -0,0 +1,117 @@ +/* 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() const { return id_; } + +protected: + unsigned int id_; +}; + +class MediaLink : public MediaObject +{ + friend class MediaDevice; + +public: + ~MediaLink() { } + + unsigned int source() const { return source_; } + unsigned int sink() const { return sink_; } + unsigned int flags() const { 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() const { return index_; } + unsigned int entity() const { return entity_; } + unsigned int flags() const { return flags_; } + const std::vector &links() const { 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_ == name; } + + const std::string name() const { return name_; } + const std::vector &sources() const { return sources_; } + const std::vector &sinks() const { return sinks_; } + + const MediaPad *getPadByIndex(unsigned int index); + const MediaPad *getPadById(unsigned int id); + +private: + MediaEntity(const struct media_v2_entity *entity); + MediaEntity(const MediaEntity &) = delete; + ~MediaEntity(); + + std::string name_; + std::string devnode_; + + std::vector sources_; + std::vector sinks_; + + int setDevice(const std::string &path); + + void addPad(MediaPad *pad); + + const MediaPad *__getPad(std::vector &v, + std::function f); + const 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..65ed421 --- /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) + */ +const 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 + */ +const 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; + } + + devnode_ = 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 + */ +const MediaPad *MediaEntity::__getPad(std::vector &v, + std::function f) +{ + auto 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 + */ +const MediaPad *MediaEntity::getPad(std::function f) +{ + const MediaPad *_p = __getPad(sources_, f); + return (_p ? _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([ From patchwork Fri Dec 28 07:57:41 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 92 Return-Path: Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net [217.70.183.201]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 8A8DA600CC for ; Fri, 28 Dec 2018 08:57:59 +0100 (CET) X-Originating-IP: 2.224.242.101 Received: from uno.lan (2-224-242-101.ip172.fastwebnet.it [2.224.242.101]) (Authenticated sender: jacopo@jmondi.org) by relay8-d.mail.gandi.net (Postfix) with ESMTPSA id 16B5E1BF207; Fri, 28 Dec 2018 07:57:58 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Fri, 28 Dec 2018 08:57:41 +0100 Message-Id: <20181228075743.28637-3-jacopo@jmondi.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20181228075743.28637-1-jacopo@jmondi.org> References: <20181228075743.28637-1-jacopo@jmondi.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH RESEND v2 2/4] libcamera: Add MediaDevice class 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: Fri, 28 Dec 2018 07:57:59 -0000 The MediaDevice object implements handling and configuration of the media graph associated with a V4L2 media device. The class allows enumeration of all pads, links and entities registered in the media graph, and provides methods to setup and reset media links. Signed-off-by: Jacopo Mondi --- src/libcamera/include/media_device.h | 71 ++++ src/libcamera/media_device.cpp | 596 +++++++++++++++++++++++++++ src/libcamera/meson.build | 1 + 3 files changed, 668 insertions(+) create mode 100644 src/libcamera/include/media_device.h create mode 100644 src/libcamera/media_device.cpp -- 2.20.1 diff --git a/src/libcamera/include/media_device.h b/src/libcamera/include/media_device.h new file mode 100644 index 0000000..642eea9 --- /dev/null +++ b/src/libcamera/include/media_device.h @@ -0,0 +1,71 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2018, Google Inc. + * + * media_device.h - Media device handler + */ +#ifndef __LIBCAMERA_MEDIA_DEVICE_H__ +#define __LIBCAMERA_MEDIA_DEVICE_H__ + +#include +#include +#include +#include + +#include + +#include "log.h" +#include "media_object.h" + +namespace libcamera { + +class MediaDevice +{ +public: + MediaDevice() : fd_(-1) { }; + ~MediaDevice(); + + const std::string name() const { return name_; } + const std::string path() const { return path_; } + + int open(const std::string &path); + int close(); + int enumerate(std::map &entitiesMap); + void dumpGraph(std::ostream &os); + + int resetLinks(); + int link(const std::string &source, unsigned int sourceIdx, + const std::string &sink, unsigned int sinkIdx, + unsigned int flags); + +private: + /** The media device file descriptor */ + int fd_; + /** The media device name as returned by MEDIA_IOC_DEVICE_INFO */ + std::string name_; + /** The media device path */ + std::string path_; + + std::map mediaObjects_; + MediaObject *getObject(unsigned int id); + void addObject(MediaObject *obj); + void deleteObjects(); + + std::vector entities_; + MediaEntity *getEntityByName(const std::string &name); + + int enumerateEntities(std::map &entitiesMap, + struct media_v2_topology &topology); + int enumeratePads(struct media_v2_topology &topology); + int enumerateLinks(struct media_v2_topology &topology); + + int setupLink(const MediaPad *source, const MediaPad *sink, + MediaLink *link, unsigned int flags); + + void dumpLocal(MediaEntity *e, MediaPad *p, std::ostream &os); + void dumpRemote(MediaLink *l, std::ostream &os); + void dumpLink(MediaLink *l, std::ostream &os); +}; + +} /* namespace libcamera */ +#endif /* __LIBCAMERA_MEDIA_DEVICE_H__ */ diff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp new file mode 100644 index 0000000..b98d62f --- /dev/null +++ b/src/libcamera/media_device.cpp @@ -0,0 +1,596 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2018, Google Inc. + * + * media_device.cpp - Media device handler + */ + +#include +#include +#include +#include +#include + +#include +#include + +#include + +#include "log.h" +#include "media_device.h" + +/** + * \file media_device.h + */ +namespace libcamera { + +/** + * \class MediaDevice + * \brief Media device handler + * + * MediaDevice handles the media graph associated with a V4L2 media device. + */ + +/** + * \fn MediaDevice::~MediaDevice() + * \brief Close the media device file descriptor and release entities + */ +MediaDevice::~MediaDevice() +{ + if (fd_ > -1) + ::close(fd_); + deleteObjects(); +} + +/** + * \fn MediaDevice::name() + * \brief Return the media device name + */ + +/** + * \fn MediaDevice::path() + * \brief Return the media device path node associated with this MediaDevice + */ + +/** + * \fn MediaDevice::deleteObjects() + * \brief Delete all registered entities in the MediaDevice object + */ +void MediaDevice::deleteObjects() +{ + for (auto const &e : mediaObjects_) + delete e.second; + + mediaObjects_.clear(); + entities_.clear(); +} + +/** + * \fn int MediaDevice::open(std::string) + * \brief Open a media device and initialize its components. + * \param path The media device path + */ +int MediaDevice::open(const std::string &path) +{ + fd_ = ::open(path.c_str(), O_RDWR); + if (fd_ < 0) { + LOG(Error) << "Failed to open media device at " << path + << ": " << strerror(errno); + return -errno; + } + path_ = path; + + struct media_device_info info = { }; + int ret = ioctl(fd_, MEDIA_IOC_DEVICE_INFO, &info); + if (ret) { + LOG(Error) << "Failed to get media device info " + << ": " << strerror(errno); + return -errno; + } + + name_ = info.driver; + + return 0; +} + +/** + * \fn MediaDevice::close() + * \brief Close the file descriptor associated with the media device + */ +int MediaDevice::close() +{ + if (fd_ > -1) + return ::close(fd_); + + return 0; +} + +void MediaDevice::addObject(MediaObject *obj) +{ + + if (mediaObjects_.find(obj->id()) != mediaObjects_.end()) { + LOG(Error) << "Element with id " << obj->id() + << " already enumerated."; + return; + } + + mediaObjects_[obj->id()] = obj; +} + +MediaObject *MediaDevice::getObject(unsigned int id) +{ + auto it = mediaObjects_.find(id); + return (it == mediaObjects_.end()) ? + nullptr : it->second; +} + +/** + * \fn MediaDevice::getEntityByName(std::string) + * \brief Return entity with name \a name + * \param name The entity name + */ +MediaEntity *MediaDevice::getEntityByName(const std::string &name) +{ + auto it = entities_.begin(); + + while (it != entities_.end()) { + MediaEntity *e = *it; + if (!(e->name().compare(name))) + return e; + it++; + } + + return nullptr; +} + +/** + * \fn MediaDevice::enumerateLinks(struct media_v2_topology &topology) + * \brief Enumerate all links in the system and associate them with their + * source and sink pads + * \param topology The media topology as returned by MEDIA_IOC_G_TOPOLOGY + */ +int MediaDevice::enumerateLinks(struct media_v2_topology &topology) +{ + struct media_v2_link *link = reinterpret_cast + (topology.ptr_links); + + for (unsigned int i = 0; i < topology.num_links; i++, link++) { + /* + * Skip links between entities and interfaces: we only care + * about pad-2-pad links here. + */ + if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) == + MEDIA_LNK_FL_INTERFACE_LINK) + continue; + + MediaLink *mediaLink = new MediaLink(link); + addObject(mediaLink); + + /* Store reference to this mediaLink in the link's source pad. */ + MediaPad *mediaPad = dynamic_cast + (getObject(mediaLink->source())); + if (!mediaPad) { + LOG(Error) << "Failed to find pad with id: " + << mediaLink->source(); + return -ENODEV; + } + mediaPad->addLink(mediaLink); + + /* Store reference to this mediaLink in the link's sink pad. */ + mediaPad = dynamic_cast(getObject(mediaLink->sink())); + if (!mediaPad) { + LOG(Error) << "Failed to find pad with id: " + << mediaLink->sink(); + return -ENODEV; + } + mediaPad->addLink(mediaLink); + } + + return 0; +} + +/** + * \fn MediaDevice::enumeratePads(struct media_v2_topology &topology) + * \brief Enumerate all pads in the system and associate them with the + * entity they belong to + * \param topology The media topology as returned by MEDIA_IOC_G_TOPOLOGY + */ +int MediaDevice::enumeratePads(struct media_v2_topology &topology) +{ + struct media_v2_pad *pad = reinterpret_cast + (topology.ptr_pads); + + for (unsigned int i = 0; i < topology.num_pads; i++, pad++) { + MediaPad *mediaPad = new MediaPad(pad); + addObject(mediaPad); + + /* Store a reference to this MediaPad in pad's entity. */ + MediaEntity *mediaEntity = dynamic_cast + (getObject(mediaPad->entity())); + if (!mediaEntity) { + LOG(Error) << "Failed to find entity with id: " + << mediaPad->entity(); + return -ENODEV; + } + + mediaEntity->addPad(mediaPad); + } + + return 0; +} + +/** + * \fn MediaDevice::enumerateEntities(std::map &, + * struct media_v2_topology &topology) + * \brief Enumerate and initialize entities in the media graph + * \param entitiesMap Map entities names to their video (sub)device node + * \param topology The media topology as returned by MEDIA_IOC_G_TOPOLOGY + * + * Associate the video (sub)device path to the entity name as returned by + * MEDIA_IOC_G_TOPOLOGY + */ +int MediaDevice::enumerateEntities(std::map &entitiesMap, + struct media_v2_topology &topology) +{ + struct media_v2_entity *entities = reinterpret_cast + (topology.ptr_entities); + + for (unsigned int i = 0; i < topology.num_entities; ++i) { + auto it = entitiesMap.find(entities[i].name); + if (it == entitiesMap.end()) { + LOG(Error) << "Entity " << entities[i].name + << " not found in media entities map"; + return -ENOENT; + } + + MediaEntity *entity = new MediaEntity(&entities[i]); + if (entity->setDevice(it->second)) { + delete entity; + goto delete_entities; + } + + addObject(entity); + entities_.push_back(entity); + } + + return 0; + +delete_entities: + deleteObjects(); + + return -errno; +} + +/** + * \fn MediaDevice::enumerate(std::map) + * \brief Enumerate the media graph topology + * \param entitiesMap Map entities names to their video (sub)device node + * FIXME: this is statically provided by the caller at the moment. + * + * This functions enumerates all media objects, registered in the media graph, + * through the MEDIA_IOC_G_TOPOLOGY ioctl. For each returned entity, + * it creates and store its representation for later reuse. + */ +int MediaDevice::enumerate(std::map &entitiesMap) +{ + struct media_v2_topology topology = { }; + unsigned int num_interfaces; + unsigned int num_links; + unsigned int num_pads; + unsigned int num_ent; + + do { + num_ent = topology.num_entities; + num_pads = topology.num_pads; + num_links = topology.num_links; + num_interfaces = topology.num_interfaces; + + /* Call G_TOPOLOGY the first time here to enumerate .*/ + if (ioctl(fd_, MEDIA_IOC_G_TOPOLOGY, &topology)) { + LOG(Error) << "Failed to enumerate media topology on" + << path_ << ": " << strerror(errno); + return -errno; + } + + /* + * Repeat the call until we don't get a 'stable' number + * of media objects. + */ + } while (num_ent != topology.num_entities || + num_pads != topology.num_pads || + num_links != topology.num_links || + num_interfaces != topology.num_interfaces); + + auto *_ptr_e = new media_v2_entity[topology.num_entities]; + topology.ptr_entities = reinterpret_cast<__u64>(_ptr_e); + + auto *_ptr_p = new media_v2_pad[topology.num_pads]; + topology.ptr_pads = reinterpret_cast<__u64>(_ptr_p); + + auto *_ptr_l = new media_v2_link[topology.num_links]; + topology.ptr_links = reinterpret_cast<__u64>(_ptr_l); + + /* Call G_TOPOLOGY again, this time with memory reserved. */ + int ret = ioctl(fd_, MEDIA_IOC_G_TOPOLOGY, &topology); + if (ret < 0) { + LOG(Error) << "Failed to enumerate media topology on " << path_ + << ": " << strerror(errno); + ret = -errno; + goto error_free_mem; + } + + ret = enumerateEntities(entitiesMap, topology); + if (ret) + goto error_free_mem; + + ret = enumeratePads(topology); + if (ret) + goto error_free_objs; + + ret = enumerateLinks(topology); + if (ret) + goto error_free_objs; + + delete[] _ptr_e; + delete[] _ptr_p; + delete[] _ptr_l; + + return 0; + +error_free_objs: + deleteObjects(); + +error_free_mem: + delete[] _ptr_e; + delete[] _ptr_p; + delete[] _ptr_l; + + return ret; +} + +void MediaDevice::dumpLocal(MediaEntity *e, MediaPad *p, std::ostream &os) +{ + os << "\t \"" << e->name() << "\"[" + << p->index() << "]"; +} + +void MediaDevice::dumpRemote(MediaLink *l, std::ostream &os) +{ + + MediaPad *remotePad = dynamic_cast + (getObject(l->sink())); + if (!remotePad) + return; + + MediaEntity *remoteEntity = dynamic_cast + (getObject(remotePad->entity())); + if (!remoteEntity) + return; + + os << "\"" << remoteEntity->name() << "\"[" + << remotePad->index() << "]"; +} + +void MediaDevice::dumpLink(MediaLink *l, std::ostream &os) +{ + unsigned int flags = l->flags(); + + os << " ["; + if (flags) { + os << (flags & MEDIA_LNK_FL_ENABLED ? "ENABLED," : "") + << (flags & MEDIA_LNK_FL_IMMUTABLE ? "IMMUTABLE" : ""); + } + os << "]\n"; +} + +/** + * \fn MediaDevice::dumpGraph(std::ostream) + * \brief Dump the media device topology in textual form to an output stream + * \param os The output stream where to append the printed topology to + */ +void MediaDevice::dumpGraph(std::ostream &os) +{ + os << "\n" << name_ << " - " << path_ << "\n\n"; + + for (auto const &e : entities_) { + os << "\"" << e->name() << "\"\n"; + + for (auto const &p : e->sinks()) { + os << " [" << p->index() << "]" << ": Sink\n"; + for (auto const &l : p->links()) { + dumpLocal(e, p, os); + os << " <- "; + dumpRemote(l, os); + dumpLink(l, os); + } + os << "\n"; + } + + for (auto const &p : e->sources()) { + os << " [" << p->index() << "]" << ": Source\n"; + for (auto const &l : p->links()) { + dumpLocal(e, p, os); + os << " -> "; + dumpRemote(l, os); + dumpLink(l, os); + } + os << "\n"; + } + } +} + +/** + * \fn MediaDevice::setupLink(MediaPad *source, MediaPad *sink) + * \brief Apply \a flags to the link between \a source and \a sink pads + * \param source The source MediaPad + * \param sink The sink MediaPad + * \param link The MediaLink to operate on + * \param flags Flags to be applied to the link (MEDIA_LNK_FL_*) + */ +int MediaDevice::setupLink(const MediaPad *source, const MediaPad *sink, + MediaLink *link, unsigned int flags) +{ + struct media_link_desc linkDesc = { }; + + linkDesc.source.entity = source->entity(); + linkDesc.source.index = source->index(); + linkDesc.source.flags = MEDIA_PAD_FL_SOURCE; + + linkDesc.sink.entity = sink->entity(); + linkDesc.sink.index = sink->index(); + linkDesc.sink.flags = MEDIA_PAD_FL_SINK; + + linkDesc.flags = flags; + + if (ioctl(fd_, MEDIA_IOC_SETUP_LINK, &linkDesc)) { + LOG(Error) << "Failed to setup link: " + << strerror(errno); + return -errno; + } + + link->setFlags(0); + + return 0; +} + +/** + * \fn MediaDevice::resetLinks() + * \brief Reset all links on the media graph + * + * Walk all registered entities, and disable all links from their + * source pads to other pads. + */ +int MediaDevice::resetLinks() +{ + for (MediaEntity *e : entities_) { + for (MediaPad *sourcePad : e->sources()) { + for (MediaLink *l : sourcePad->links()) { + /* + * Do not reset links that are not enabled + * or immutable. + */ + if (l->flags() & MEDIA_LNK_FL_IMMUTABLE) + continue; + + if (!(l->flags() & MEDIA_LNK_FL_ENABLED)) + continue; + + /* Get the remote sink pad. */ + MediaPad *sinkPad = dynamic_cast + (getObject(l->sink())); + if (!sinkPad) + return -ENOENT; + + /* Also get entity to make sure IDs are ok. */ + MediaEntity *sinkEntity = + dynamic_cast + (getObject(sinkPad->entity())); + if (!sinkEntity) + return -ENOENT; + + int ret = setupLink(sourcePad, sinkPad, l, 0); + if (ret) { + LOG(Error) << "Link reset failed: " + << e->name() << "[" + << sourcePad->index() + << "] -> " + << sinkEntity->name() << "[" + << sinkPad->index() << "]"; + return ret; + } + + LOG(Info) << "Link reset: " + << e->name() << "[" + << sourcePad->index() + << "] -> " + << sinkEntity->name() << "[" + << sinkPad->index() << "]"; + } + } + } + + return 0; +} + +/** + * \fn MediaDevice::link(std::string, unsigned int, std::string, unsigned int) + * \brief Setup a link identified by the entities name and their source and + * sink pad indexes + * \param source The source entity name + * \param sourceIdx The source pad index + * \param sink The sink entity name + * \param sinkIdx The sink pad index + * \param flags The link setup flag (see MEDIA_LNK_FL_*) + */ +int MediaDevice::link(const std::string &source, unsigned int sourceIdx, + const std::string &sink, unsigned int sinkIdx, + unsigned int flags) +{ + + /* Make sure the supplied link is well formed. */ + MediaEntity *sourceEntity = getEntityByName(source); + if (!sourceEntity) { + LOG(Error) << "Entity name: " << source << "not found"; + return -ENOENT; + } + + MediaEntity *sinkEntity = getEntityByName(sink); + if (!sinkEntity) { + LOG(Error) << "Entity name: " << source << "not found"; + return -ENOENT; + } + + const MediaPad *sourcePad = sourceEntity->getPadByIndex(sourceIdx); + if (!sourcePad) { + LOG(Error) << "Pad " << sourceIdx << "not found in entity " + << sourceEntity->name(); + return -ENOENT; + } + + const MediaPad *sinkPad = sinkEntity->getPadByIndex(sinkIdx); + if (!sinkPad) { + LOG(Error) << "Pad " << sinkIdx << "not found in entity " + << sinkEntity->name(); + return -ENOENT; + } + + /* + * Walk all links in the source and search for an entry matching the + * pad ids. If none, the requested link does not exists. + */ + MediaLink *validLink = nullptr; + for (MediaLink *link : sourcePad->links()) { + if (link->source() != sourcePad->id()) + continue; + + if (link->sink() != sinkPad->id()) + continue; + + validLink = link; + break; + } + + if (!validLink) { + LOG(Error) << "Link not found" + << "\"" << sourceEntity->name() << "\"[" + << sourcePad->index() << "] -> " + << "\"" << sinkEntity->name() << "\"[" + << sinkPad->index() << "]"; + return -EINVAL; + } + + int ret = setupLink(sourcePad, sinkPad, validLink, flags); + if (ret) + return ret; + + LOG(Info) << "Setup link: " + << "\"" << sourceEntity->name() << "\"[" + << sourcePad->index() << "] -> " + << "\"" << sinkEntity->name() << "\"[" + << sinkPad->index() << "]" + << " [" << flags << "]"; + + return 0; +} + +} /* namespace libcamera */ diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index da06eba..4cac687 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -2,6 +2,7 @@ libcamera_sources = files([ 'log.cpp', 'main.cpp', 'media_object.cpp', + 'media_device.cpp', ]) libcamera_headers = files([ From patchwork Fri Dec 28 07:57:42 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 93 Return-Path: Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net [217.70.183.201]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 2DB9860B32 for ; Fri, 28 Dec 2018 08:58:00 +0100 (CET) X-Originating-IP: 2.224.242.101 Received: from uno.lan (2-224-242-101.ip172.fastwebnet.it [2.224.242.101]) (Authenticated sender: jacopo@jmondi.org) by relay8-d.mail.gandi.net (Postfix) with ESMTPSA id BE7E81BF206; Fri, 28 Dec 2018 07:57:59 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Fri, 28 Dec 2018 08:57:42 +0100 Message-Id: <20181228075743.28637-4-jacopo@jmondi.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20181228075743.28637-1-jacopo@jmondi.org> References: <20181228075743.28637-1-jacopo@jmondi.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH RESEND v2 3/4] libcamera: media device: Add MediaDeviceDesc 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: Fri, 28 Dec 2018 07:58:00 -0000 Add the MediaDeviceDesc class which represents the description of a media graph and the entities it contains. Signed-off-by: Jacopo Mondi --- src/libcamera/include/media_device.h | 42 ++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) -- 2.20.1 diff --git a/src/libcamera/include/media_device.h b/src/libcamera/include/media_device.h index 642eea9..e1b26e2 100644 --- a/src/libcamera/include/media_device.h +++ b/src/libcamera/include/media_device.h @@ -19,6 +19,48 @@ namespace libcamera { +/** + * \class MediaDeviceDesc + * \brief Collects the representation of a media device + * + * The MediaDeviceDesc class allows components of the library to create + * a description of a media device, specifying a driver name and a list of + * entity names contained in it. + */ +class MediaDeviceDesc +{ +public: + /** + * \brief Construct a representation of a media device handled + * by driver \a driver's name + * \param driver The name of the driver that controls the media graph + * as returned by MEDIA_IOC_DEVICE_INFO + */ + MediaDeviceDesc(const std::string &driver) : driver_(driver) { } + + /** + * \brief Add an entity name to the list of entities handled in the + * media graph. + * \param entity The name of the entity to add + */ + void addEntity(const std::string entity) { entities_.push_back(entity); } + + /** + * \brief Return the name of the driver that controls the media graph + * as returned by MEDIA_IOC_DEVICE_INFO + */ + const std::string driver() { return driver_; } + + /** + * \brief Return the list of entities names + */ + std::vector &entities() { return entities_; } + +private: + std::string driver_; + std::vector entities_; +}; + class MediaDevice { public: From patchwork Fri Dec 28 07:57:43 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 94 Return-Path: Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net [217.70.183.201]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C121360B32 for ; Fri, 28 Dec 2018 08:58:00 +0100 (CET) X-Originating-IP: 2.224.242.101 Received: from uno.lan (2-224-242-101.ip172.fastwebnet.it [2.224.242.101]) (Authenticated sender: jacopo@jmondi.org) by relay8-d.mail.gandi.net (Postfix) with ESMTPSA id 5CB9B1BF206; Fri, 28 Dec 2018 07:58:00 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Fri, 28 Dec 2018 08:57:43 +0100 Message-Id: <20181228075743.28637-5-jacopo@jmondi.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20181228075743.28637-1-jacopo@jmondi.org> References: <20181228075743.28637-1-jacopo@jmondi.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH RESEND v2 4/4] libcamera: media device: Add 'match()' method 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: Fri, 28 Dec 2018 07:58:00 -0000 Add 'match()' method to the MediaDevice class. The method allows matching the media device against a MediaDeviceDesc. Signed-off-by: Jacopo Mondi --- src/libcamera/include/media_device.h | 2 ++ src/libcamera/media_device.cpp | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) -- 2.20.1 diff --git a/src/libcamera/include/media_device.h b/src/libcamera/include/media_device.h index e1b26e2..9f6d091 100644 --- a/src/libcamera/include/media_device.h +++ b/src/libcamera/include/media_device.h @@ -75,6 +75,8 @@ public: int enumerate(std::map &entitiesMap); void dumpGraph(std::ostream &os); + bool match(MediaDeviceDesc &desc); + int resetLinks(); int link(const std::string &source, unsigned int sourceIdx, const std::string &sink, unsigned int sinkIdx, diff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp index b98d62f..a961506 100644 --- a/src/libcamera/media_device.cpp +++ b/src/libcamera/media_device.cpp @@ -453,6 +453,25 @@ int MediaDevice::setupLink(const MediaPad *source, const MediaPad *sink, return 0; } +/** + * \brief Match the media device represented by this object against + * a media device description \a desc + * \param desc The media device description to math this MediaDevice against + */ +bool MediaDevice::match(MediaDeviceDesc &desc) +{ + /* TODO: add more fields to MediaDeviceDesc and match them + * against media_device_info fields if required */ + if (name_ != desc.driver()) + return false; + + for (std::string &e : desc.entities()) + if (!getEntityByName(e)) + return false; + + return true; +} + /** * \fn MediaDevice::resetLinks() * \brief Reset all links on the media graph