Show a patch.

GET /api/patches/188/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 188,
    "url": "https://patchwork.libcamera.org/api/patches/188/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/188/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/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": "<20190108204241.10674-3-jacopo@jmondi.org>",
    "date": "2019-01-08T20:42:40",
    "name": "[libcamera-devel,v3,2/3] libcamera: Add MediaDevice class",
    "commit_ref": null,
    "pull_url": null,
    "state": "accepted",
    "archived": false,
    "hash": "e205d0f5e0badff2cd4535ea110b52b468b9c5a4",
    "submitter": {
        "id": 3,
        "url": "https://patchwork.libcamera.org/api/people/3/?format=api",
        "name": "Jacopo Mondi",
        "email": "jacopo@jmondi.org"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/188/mbox/",
    "series": [
        {
            "id": 62,
            "url": "https://patchwork.libcamera.org/api/series/62/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=62",
            "date": "2019-01-08T20:42:38",
            "name": "MediaDevice class and MediaObject classes",
            "version": 3,
            "mbox": "https://patchwork.libcamera.org/series/62/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/188/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/188/checks/",
    "tags": {},
    "headers": {
        "Return-Path": "<jacopo@jmondi.org>",
        "Received": [
            "from relay3-d.mail.gandi.net (relay3-d.mail.gandi.net\n\t[217.70.183.195])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 3604D60B30\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  8 Jan 2019 21:42:47 +0100 (CET)",
            "from uno.lan (2-224-242-101.ip172.fastwebnet.it [2.224.242.101])\n\t(Authenticated sender: jacopo@jmondi.org)\n\tby relay3-d.mail.gandi.net (Postfix) with ESMTPSA id BCE8160003;\n\tTue,  8 Jan 2019 20:42:46 +0000 (UTC)"
        ],
        "X-Originating-IP": "2.224.242.101",
        "From": "Jacopo Mondi <jacopo@jmondi.org>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Date": "Tue,  8 Jan 2019 21:42:40 +0100",
        "Message-Id": "<20190108204241.10674-3-jacopo@jmondi.org>",
        "X-Mailer": "git-send-email 2.20.1",
        "In-Reply-To": "<20190108204241.10674-1-jacopo@jmondi.org>",
        "References": "<20190108204241.10674-1-jacopo@jmondi.org>",
        "MIME-Version": "1.0",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [PATCH v3 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": "Tue, 08 Jan 2019 20:42:47 -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       | 370 +++++++++++++++++++++++++++\n src/libcamera/meson.build            |   2 +\n 3 files changed, 433 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..6a9260e\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+\tint 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..497b12d\n--- /dev/null\n+++ b/src/libcamera/media_device.cpp\n@@ -0,0 +1,370 @@\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+ * MediaDevice represents the graph of media objects associated with a\n+ * media device as 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 Return the driver name that handles the media graph this object\n+ * represents.\n+ */\n+\n+/**\n+ * \\fn MediaDevice::devnode()\n+ * \\brief Return the media device devnode node associated with this MediaDevice.\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 &e : entities_)\n+\t\tdelete e;\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 Returns 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+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 Return the list of MediaEntity references.\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+int 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+\treturn 0;\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 Return 0 on success or a negative error code on error.\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_mem;\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+\tret = populateEntities(topology);\n+\tif (ret)\n+\t\tgoto error_free_mem;\n+\n+\tret = populatePads(topology);\n+\tif (ret)\n+\t\tgoto error_free_objs;\n+\n+\tret = populateLinks(topology);\n+\tif (ret)\n+\t\tgoto error_free_objs;\n+\n+\tdelete[] links;\n+\tdelete[] ents;\n+\tdelete[] pads;\n+\n+\treturn 0;\n+\n+error_free_objs:\n+\tdeleteObjects();\n+\n+error_free_mem:\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 01d321c..39a0464 100644\n--- a/src/libcamera/meson.build\n+++ b/src/libcamera/meson.build\n@@ -2,10 +2,12 @@ libcamera_sources = files([\n     'log.cpp',\n     'main.cpp',\n     'media_object.cpp',\n+    'media_device.cpp',\n ])\n \n libcamera_headers = files([\n     'include/log.h',\n+    'include/media_device.h',\n     'include/media_object.h',\n     'include/utils.h',\n ])\n",
    "prefixes": [
        "libcamera-devel",
        "v3",
        "2/3"
    ]
}