Patch Detail
Show a patch.
GET /api/1.1/patches/115/?format=api
{ "id": 115, "url": "https://patchwork.libcamera.org/api/1.1/patches/115/?format=api", "web_url": "https://patchwork.libcamera.org/patch/115/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/1.1/projects/1/?format=api", "name": "libcamera", "link_name": "libcamera", "list_id": "libcamera_core", "list_email": "libcamera-devel@lists.libcamera.org", "web_url": "", "scm_url": "", "webscm_url": "" }, "msgid": "<20181231092752.5018-3-jacopo@jmondi.org>", "date": "2018-12-31T09:27:51", "name": "[libcamera-devel,v4,2/3] libcamera: Add MediaDevice class", "commit_ref": null, "pull_url": null, "state": "superseded", "archived": false, "hash": "f4705e798b165c2ce023bcdf8e69f0745775edb1", "submitter": { "id": 3, "url": "https://patchwork.libcamera.org/api/1.1/people/3/?format=api", "name": "Jacopo Mondi", "email": "jacopo@jmondi.org" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/115/mbox/", "series": [ { "id": 44, "url": "https://patchwork.libcamera.org/api/1.1/series/44/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=44", "date": "2018-12-31T09:27:49", "name": "MediaDevice class and MediaObject classes", "version": 4, "mbox": "https://patchwork.libcamera.org/series/44/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/115/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/115/checks/", "tags": {}, "headers": { "Return-Path": "<jacopo@jmondi.org>", "Received": [ "from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net\n\t[217.70.183.198])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id B34EE60B30\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 31 Dec 2018 10:27:58 +0100 (CET)", "from uno.homenet.telecomitalia.it\n\t(host54-51-dynamic.16-87-r.retail.telecomitalia.it [87.16.51.54])\n\t(Authenticated sender: jacopo@jmondi.org)\n\tby relay6-d.mail.gandi.net (Postfix) with ESMTPSA id A0292C000F;\n\tMon, 31 Dec 2018 09:27:57 +0000 (UTC)" ], "X-Originating-IP": "87.16.51.54", "From": "Jacopo Mondi <jacopo@jmondi.org>", "To": "libcamera-devel@lists.libcamera.org", "Date": "Mon, 31 Dec 2018 10:27:51 +0100", "Message-Id": "<20181231092752.5018-3-jacopo@jmondi.org>", "X-Mailer": "git-send-email 2.20.1", "In-Reply-To": "<20181231092752.5018-1-jacopo@jmondi.org>", "References": "<20181231092752.5018-1-jacopo@jmondi.org>", "MIME-Version": "1.0", "Content-Transfer-Encoding": "8bit", "Subject": "[libcamera-devel] [PATCH v4 2/3] libcamera: Add MediaDevice class", "X-BeenThere": "libcamera-devel@lists.libcamera.org", "X-Mailman-Version": "2.1.23", "Precedence": "list", "List-Id": "<libcamera-devel.lists.libcamera.org>", "List-Unsubscribe": "<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>", "List-Archive": "<https://lists.libcamera.org/pipermail/libcamera-devel/>", "List-Post": "<mailto:libcamera-devel@lists.libcamera.org>", "List-Help": "<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>", "List-Subscribe": "<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>", "X-List-Received-Date": "Mon, 31 Dec 2018 09:27:58 -0000" }, "content": "The MediaDevice object implements handling and configuration of the media\ngraph associated with a media device.\n\nThe class allows enumeration of all pads, links and entities registered in\nthe media graph.\n\nSigned-off-by: Jacopo Mondi <jacopo@jmondi.org>\n---\n src/libcamera/include/media_device.h | 61 +++++\n src/libcamera/media_device.cpp | 362 +++++++++++++++++++++++++++\n src/libcamera/meson.build | 2 +\n 3 files changed, 425 insertions(+)\n create mode 100644 src/libcamera/include/media_device.h\n create mode 100644 src/libcamera/media_device.cpp", "diff": "diff --git a/src/libcamera/include/media_device.h b/src/libcamera/include/media_device.h\nnew file mode 100644\nindex 0000000..11eae5c\n--- /dev/null\n+++ b/src/libcamera/include/media_device.h\n@@ -0,0 +1,61 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2018, Google Inc.\n+ *\n+ * media_device.h - Media device handler\n+ */\n+#ifndef __LIBCAMERA_MEDIA_DEVICE_H__\n+#define __LIBCAMERA_MEDIA_DEVICE_H__\n+\n+#include <map>\n+#include <sstream>\n+#include <string>\n+#include <vector>\n+\n+#include <linux/media.h>\n+\n+#include \"media_object.h\"\n+\n+namespace libcamera {\n+\n+class MediaDevice\n+{\n+public:\n+\tMediaDevice() : fd_(-1) { };\n+\t~MediaDevice();\n+\n+\tconst std::string driver() const { return driver_; }\n+\tconst std::string devnode() const { return devnode_; }\n+\n+\tint open(const std::string &devnode);\n+\tvoid close();\n+\n+\tconst std::vector<MediaEntity *> &entities() const { return entities_; }\n+\n+\tint populate();\n+\n+private:\n+\tstd::string driver_;\n+\tstd::string devnode_;\n+\tint fd_;\n+\n+\t/*\n+\t * Global map of media objects (entities, pads, links) associated to\n+\t * their globally unique id.\n+\t */\n+\tstd::map<unsigned int, MediaObject *> objects_;\n+\tMediaObject *getObject(unsigned int id);\n+\tint addObject(MediaObject *obj);\n+\tvoid deleteObjects();\n+\n+\t/* Global list of media entities in the media graph: lookup by name. */\n+\tstd::vector<MediaEntity *> entities_;\n+\tMediaEntity *getEntityByName(const std::string &name);\n+\n+\tvoid populateEntities(const struct media_v2_topology &topology);\n+\tint populatePads(const struct media_v2_topology &topology);\n+\tint populateLinks(const struct media_v2_topology &topology);\n+};\n+\n+} /* namespace libcamera */\n+#endif /* __LIBCAMERA_MEDIA_DEVICE_H__ */\ndiff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp\nnew file mode 100644\nindex 0000000..2a293bd\n--- /dev/null\n+++ b/src/libcamera/media_device.cpp\n@@ -0,0 +1,362 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2018, Google Inc.\n+ *\n+ * media_device.cpp - Media device handler\n+ */\n+\n+#include <errno.h>\n+#include <fcntl.h>\n+#include <string.h>\n+#include <sys/ioctl.h>\n+#include <unistd.h>\n+\n+#include <string>\n+#include <vector>\n+\n+#include <linux/media.h>\n+\n+#include \"log.h\"\n+#include \"media_device.h\"\n+\n+/**\n+ * \\file media_device.h\n+ */\n+namespace libcamera {\n+\n+/**\n+ * \\class MediaDevice\n+ * \\brief Media device handler\n+ *\n+ * Represents the graph of media objects associated with a media device as\n+ * exposed by the kernel through the media controller APIs.\n+ *\n+ * The caller is responsible for opening the MediaDevice explicitly before\n+ * operating on it, and shall close it when not needed anymore, as access\n+ * to the MediaDevice is exclusive.\n+ *\n+ * A MediaDevice is created empty and gets populated by inspecting the media\n+ * graph topology using the MEDIA_IOC_G_TOPOLOGY ioctls. Representation\n+ * of each entity, pad and link described are created using MediaObject\n+ * derived classes.\n+ *\n+ * All MediaObject are stored in a global pool, where they could be retrieved\n+ * from by their globally unique id.\n+ *\n+ * References to MediaEntity registered in the graph are stored in a vector\n+ * to allow easier by-name lookup, and the list of MediaEntities is accessible.\n+ */\n+\n+/**\n+ * \\brief Close the media device file descriptor and delete all object\n+ */\n+MediaDevice::~MediaDevice()\n+{\n+\tif (fd_ != -1)\n+\t\t::close(fd_);\n+\tdeleteObjects();\n+}\n+\n+/**\n+ * \\fn MediaDevice::driver()\n+ * \\brief Retrieve the media device driver name\n+ * \\return The name of the kernel driver that handles the MediaDevice\n+ */\n+\n+/**\n+ * \\fn MediaDevice::devnode()\n+ * \\brief Retrieve the media device device node path\n+ * \\return The MediaDevice devnode path\n+ */\n+\n+/**\n+ * \\brief Delete all media objects in the MediaDevice.\n+ *\n+ * Delete all MediaEntities; entities will then delete their pads,\n+ * and each source pad will delete links.\n+ *\n+ * After this function has been called, the media graph will be unpopulated\n+ * and its media objects deleted. The media device has to be populated\n+ * before it could be used again.\n+ */\n+void MediaDevice::deleteObjects()\n+{\n+\tfor (auto const &o : objects_)\n+\t\tdelete o.second;\n+\n+\tobjects_.clear();\n+\tentities_.clear();\n+}\n+\n+/**\n+ * \\brief Open a media device and retrieve informations from it\n+ * \\param devnode The media device node path\n+ *\n+ * The function fails if the media device is already open or if either\n+ * open or the media device information retrieval operations fail.\n+ *\n+ * \\return 0 for success or a negative error number otherwise\n+ */\n+int MediaDevice::open(const std::string &devnode)\n+{\n+\tif (fd_ != -1) {\n+\t\tLOG(Error) << \"MediaDevice already open\";\n+\t\treturn -EBUSY;\n+\t}\n+\n+\tint ret = ::open(devnode.c_str(), O_RDWR);\n+\tif (ret < 0) {\n+\t\tret = -errno;\n+\t\tLOG(Error) << \"Failed to open media device at \" << devnode\n+\t\t\t << \": \" << strerror(-ret);\n+\t\treturn ret;\n+\t}\n+\tfd_ = ret;\n+\tdevnode_ = devnode;\n+\n+\tstruct media_device_info info = { };\n+\tret = ioctl(fd_, MEDIA_IOC_DEVICE_INFO, &info);\n+\tif (ret) {\n+\t\tret = -errno;\n+\t\tLOG(Error) << \"Failed to get media device info \"\n+\t\t\t << \": \" << strerror(-ret);\n+\t\treturn ret;\n+\t}\n+\n+\tdriver_ = info.driver;\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * \\brief Close the file descriptor associated with the media device.\n+ *\n+ * After this function has been called, for the MediaDevice to be operated on,\n+ * the caller shall open it again.\n+ */\n+void MediaDevice::close()\n+{\n+\tif (fd_ == -1)\n+\t\treturn;\n+\n+\t::close(fd_);\n+\tfd_ = -1;\n+}\n+\n+/**\n+ * \\fn MediaDevice::entities()\n+ * \\brief Retrieve the list of entities in the media graph\n+ * \\return The list of MediaEntities registered in the MediaDevice\n+ */\n+\n+/*\n+ * Add a new object to the global objects pool and fail if the object\n+ * has already been registered.\n+ */\n+int MediaDevice::addObject(MediaObject *obj)\n+{\n+\n+\tif (objects_.find(obj->id()) != objects_.end()) {\n+\t\tLOG(Error) << \"Element with id \" << obj->id()\n+\t\t\t << \" already enumerated.\";\n+\t\treturn -EEXIST;\n+\t}\n+\n+\tobjects_[obj->id()] = obj;\n+\n+\treturn 0;\n+}\n+\n+/*\n+ * MediaObject pool lookup by id.\n+ */\n+MediaObject *MediaDevice::getObject(unsigned int id)\n+{\n+\tauto it = objects_.find(id);\n+\treturn (it == objects_.end()) ? nullptr : it->second;\n+}\n+\n+/**\n+ * \\brief Return the MediaEntity with name \\a name\n+ * \\param name The entity name\n+ * \\return The entity with \\a name\n+ * \\return nullptr if no entity with \\a name is found\n+ */\n+MediaEntity *MediaDevice::getEntityByName(const std::string &name)\n+{\n+\tfor (MediaEntity *e : entities_)\n+\t\tif (e->name() == name)\n+\t\t\treturn e;\n+\n+\treturn nullptr;\n+}\n+\n+int MediaDevice::populateLinks(const struct media_v2_topology &topology)\n+{\n+\tmedia_v2_link *mediaLinks = reinterpret_cast<media_v2_link *>\n+\t\t\t\t (topology.ptr_links);\n+\n+\tfor (unsigned int i = 0; i < topology.num_links; ++i) {\n+\t\t/*\n+\t\t * Skip links between entities and interfaces: we only care\n+\t\t * about pad-2-pad links here.\n+\t\t */\n+\t\tif ((mediaLinks[i].flags & MEDIA_LNK_FL_LINK_TYPE) ==\n+\t\t MEDIA_LNK_FL_INTERFACE_LINK)\n+\t\t\tcontinue;\n+\n+\t\t/* Store references to source and sink pads in the link. */\n+\t\tunsigned int source_id = mediaLinks[i].source_id;\n+\t\tMediaPad *source = dynamic_cast<MediaPad *>\n+\t\t\t\t (getObject(source_id));\n+\t\tif (!source) {\n+\t\t\tLOG(Error) << \"Failed to find pad with id: \"\n+\t\t\t\t << source_id;\n+\t\t\treturn -ENODEV;\n+\t\t}\n+\n+\t\tunsigned int sink_id = mediaLinks[i].sink_id;\n+\t\tMediaPad *sink = dynamic_cast<MediaPad *>\n+\t\t\t\t (getObject(sink_id));\n+\t\tif (!sink) {\n+\t\t\tLOG(Error) << \"Failed to find pad with id: \"\n+\t\t\t\t << sink_id;\n+\t\t\treturn -ENODEV;\n+\t\t}\n+\n+\t\tMediaLink *link = new MediaLink(&mediaLinks[i], source, sink);\n+\t\tsource->addLink(link);\n+\t\tsink->addLink(link);\n+\n+\t\taddObject(link);\n+\t}\n+\n+\treturn 0;\n+}\n+\n+int MediaDevice::populatePads(const struct media_v2_topology &topology)\n+{\n+\tmedia_v2_pad *mediaPads = reinterpret_cast<media_v2_pad *>\n+\t\t\t\t (topology.ptr_pads);\n+\n+\tfor (unsigned int i = 0; i < topology.num_pads; ++i) {\n+\t\tunsigned int entity_id = mediaPads[i].entity_id;\n+\n+\t\t/* Store a reference to this MediaPad in entity. */\n+\t\tMediaEntity *mediaEntity = dynamic_cast<MediaEntity *>\n+\t\t\t\t\t (getObject(entity_id));\n+\t\tif (!mediaEntity) {\n+\t\t\tLOG(Error) << \"Failed to find entity with id: \"\n+\t\t\t\t << entity_id;\n+\t\t\treturn -ENODEV;\n+\t\t}\n+\n+\t\tMediaPad *pad = new MediaPad(&mediaPads[i], mediaEntity);\n+\t\tmediaEntity->addPad(pad);\n+\n+\t\taddObject(pad);\n+\t}\n+\n+\treturn 0;\n+}\n+\n+/*\n+ * For each entity in the media graph create a MediaEntity and store a\n+ * reference in the MediaObject global pool and in the global vector of\n+ * entities.\n+ */\n+void MediaDevice::populateEntities(const struct media_v2_topology &topology)\n+{\n+\tmedia_v2_entity *mediaEntities = reinterpret_cast<media_v2_entity *>\n+\t\t\t\t\t (topology.ptr_entities);\n+\n+\tfor (unsigned int i = 0; i < topology.num_entities; ++i) {\n+\t\tMediaEntity *entity = new MediaEntity(&mediaEntities[i]);\n+\n+\t\taddObject(entity);\n+\t\tentities_.push_back(entity);\n+\t}\n+}\n+\n+/**\n+ * \\brief Populate the media graph with media objects\n+ *\n+ * This function enumerates all media objects in the media device graph and\n+ * creates their MediaObject representations. All entities, pads and links are\n+ * stored as MediaEntity, MediaPad and MediaLink respectively, with cross-\n+ * references between objects. Interfaces are not processed.\n+ *\n+ * MediaEntities are stored in a global list in the MediaDevice itself to ease\n+ * lookup, while MediaPads are accessible from the MediaEntity they belong\n+ * to only and MediaLinks from the MediaPad they connect.\n+ *\n+ * \\return 0 on success, a negative error code otherwise\n+ */\n+int MediaDevice::populate()\n+{\n+\tstruct media_v2_topology topology;\n+\tstruct media_v2_entity *ents;\n+\tstruct media_v2_link *links;\n+\tstruct media_v2_pad *pads;\n+\tint ret;\n+\n+\t/*\n+\t * Keep calling G_TOPOLOGY until the version number stays stable.\n+\t */\n+\twhile (true) {\n+\t\ttopology = {};\n+\n+\t\tret = ioctl(fd_, MEDIA_IOC_G_TOPOLOGY, &topology);\n+\t\tif (ret < 0) {\n+\t\t\tret = -errno;\n+\t\t\tLOG(Error) << \"Failed to enumerate topology: \"\n+\t\t\t\t << strerror(-ret);\n+\t\t\treturn ret;\n+\t\t}\n+\n+\t\t__u64 version = topology.topology_version;\n+\n+\t\tents = new media_v2_entity[topology.num_entities];\n+\t\tlinks = new media_v2_link[topology.num_links];\n+\t\tpads = new media_v2_pad[topology.num_pads];\n+\n+\t\ttopology.ptr_entities = reinterpret_cast<__u64>(ents);\n+\t\ttopology.ptr_links = reinterpret_cast<__u64>(links);\n+\t\ttopology.ptr_pads = reinterpret_cast<__u64>(pads);\n+\n+\t\tret = ioctl(fd_, MEDIA_IOC_G_TOPOLOGY, &topology);\n+\t\tif (ret < 0) {\n+\t\t\tret = -errno;\n+\t\t\tLOG(Error) << \"Failed to enumerate topology: \"\n+\t\t\t\t << strerror(-ret);\n+\t\t\tgoto error_free_objs;\n+\t\t}\n+\n+\t\tif (version == topology.topology_version)\n+\t\t\tbreak;\n+\n+\t\tdelete[] links;\n+\t\tdelete[] ents;\n+\t\tdelete[] pads;\n+\t}\n+\n+\t/* Populate entities, pads and links. */\n+\tpopulateEntities(topology);\n+\n+\tret = populatePads(topology);\n+\tif (ret)\n+\t\tgoto error_free_objs;\n+\n+\tret = populateLinks(topology);\n+error_free_objs:\n+\tif (ret)\n+\t\tdeleteObjects();\n+\n+\tdelete[] links;\n+\tdelete[] ents;\n+\tdelete[] pads;\n+\n+\treturn ret;\n+}\n+\n+} /* namespace libcamera */\ndiff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\nindex 2985d40..2ff5bb5 100644\n--- a/src/libcamera/meson.build\n+++ b/src/libcamera/meson.build\n@@ -4,6 +4,7 @@ libcamera_sources = files([\n 'device_enumerator.cpp',\n 'log.cpp',\n 'main.cpp',\n+ 'media_device.cpp',\n 'media_object.cpp',\n 'pipeline_handler.cpp',\n ])\n@@ -11,6 +12,7 @@ libcamera_sources = files([\n libcamera_headers = files([\n 'include/device_enumerator.h',\n 'include/log.h',\n+ 'include/media_device.h',\n 'include/media_object.h',\n 'include/pipeline_handler.h',\n 'include/utils.h',\n", "prefixes": [ "libcamera-devel", "v4", "2/3" ] }