{"id":23960,"url":"https://patchwork.libcamera.org/api/1.1/patches/23960/?format=json","web_url":"https://patchwork.libcamera.org/patch/23960/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20250725-multicontext-v1-6-ea558291e101@ideasonboard.com>","date":"2025-07-25T10:33:51","name":"[6/9] libcamera: media-device: Introduce SharedMediaDevice","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"7e8bc802d085d34b7d07d41bf6fde5fb18ad27e3","submitter":{"id":143,"url":"https://patchwork.libcamera.org/api/1.1/people/143/?format=json","name":"Jacopo Mondi","email":"jacopo.mondi@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/23960/mbox/","series":[{"id":5329,"url":"https://patchwork.libcamera.org/api/1.1/series/5329/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=5329","date":"2025-07-25T10:33:45","name":"libcamera: Support for multi-context operations","version":1,"mbox":"https://patchwork.libcamera.org/series/5329/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/23960/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/23960/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id D35ABC3237\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 25 Jul 2025 10:34:14 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 79AF0690DC;\n\tFri, 25 Jul 2025 12:34:09 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id B3E14690BC\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 25 Jul 2025 12:33:59 +0200 (CEST)","from [192.168.0.172] (mob-5-90-139-29.net.vodafone.it\n\t[5.90.139.29])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id DF72178E;\n\tFri, 25 Jul 2025 12:33:19 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"OkqJBlUT\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1753439600;\n\tbh=MheqSlSDoWQPQzlMwHnXKCZq6yEzivBzqHP37UxYB2Y=;\n\th=From:Date:Subject:References:In-Reply-To:To:Cc:From;\n\tb=OkqJBlUTUoCxcnxqaO+g/AoqKU8GBgg/swmziWgALM1tlaffNLQgHQid8QH/iifgq\n\tlm6GJZkpL5x8ZllQ6SXxDvV41R3oZoy9m/sLCoeLFGIkx4tY3iFeimXqoSxI/gUBfv\n\t5WP0++Z0HEiRdmf67IfIMbFOxzcUTUUgibhEo/4k=","From":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>","Date":"Fri, 25 Jul 2025 12:33:51 +0200","Subject":"[PATCH 6/9] libcamera: media-device: Introduce SharedMediaDevice","MIME-Version":"1.0","Content-Type":"text/plain; charset=\"utf-8\"","Content-Transfer-Encoding":"7bit","Message-Id":"<20250725-multicontext-v1-6-ea558291e101@ideasonboard.com>","References":"<20250725-multicontext-v1-0-ea558291e101@ideasonboard.com>","In-Reply-To":"<20250725-multicontext-v1-0-ea558291e101@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>","X-Mailer":"b4 0.14.2","X-Developer-Signature":"v=1; a=openpgp-sha256; l=9954;\n\ti=jacopo.mondi@ideasonboard.com; h=from:subject:message-id;\n\tbh=MheqSlSDoWQPQzlMwHnXKCZq6yEzivBzqHP37UxYB2Y=;\n\tb=owEBbQKS/ZANAwAKAXI0Bo8WoVY8AcsmYgBog12UFPasn238Sp/uOOJcU82+qewXNDgbdEoEX\n\txUbsUBVRFqJAjMEAAEKAB0WIQS1xD1IgJogio9YOMByNAaPFqFWPAUCaINdlAAKCRByNAaPFqFW\n\tPAG9D/44+42lD1nKTlxPur9EmawIO7/bcUpbKVt4e3FniiVdO1oVr6MI9AN/B2uluazHuHzhYLs\n\t5KQx6QZkwLw6T20qJRHReUnF3TbP8sCNnvBVJ598xalRcSIcYHYSCoom/Mn5ta/vE8csWER3sey\n\tfZvu66oqohukfXBXcxLgoImRAZb/nk8MRrJ4h08WFc1tCZPR10RFkNeM/kX+F8PhKLGgiDkPdL8\n\t4iqFB5o7WMoPxNH4TnZkkF8ELCMNliULlBxgkrtKpvub8LDygrioQn4KD9n4aBWYA+Q/cGNbhQf\n\toI2oFM4Wk+xv6k3EsOFGZeLsE4d08RN1OcJblbmyxnW1icAEf5N7/zT5ZwpxFQ5abo/ZTneIjrj\n\tA8Ymxrt5cxLVIogWbtaHPrKWp8RvrrjhqXdMzqf87ToPKTyeWeDpYJMqHcfdSpDvYC8uuBYrRai\n\tzzu0DvmQC1PZYjO8OZnAPfrRd6yp1EuNhH8/6SMifYgeO+slkO1WvLmMxbnloyWj0PcurAA5D0q\n\toYFYF8+hECCcXCJnVdjzy3hhbtNktkCajEYXabR5HhkshBxlvV9PegkNd+ieN7aKMpWqqCRuUV5\n\tSdRi4GAFqfeJg1x3ccEQ23vQvQ8kqEpEKp3lNMJAzTMeoThN91caz8Qk1xngtPJStM57bNawnER\n\trdZnnkDFxeGyTdw==","X-Developer-Key":"i=jacopo.mondi@ideasonboard.com; a=openpgp;\n\tfpr=72392EDC88144A65C701EA9BA5826A2587AD026B","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Introduce the SharedMediaDevice class derived from the MediaDevice\nclass, that represents a media device that supports multi-context\noperations.\n\nDisallow direct construction of MediaDevice and introduce the\nMediaDeviceFactory class that instantiate the correct class from the\nMediaDevice class hierarchy inspecting the newly introduced\nmedia_device_info.flags field.\n\nThe new SharedMediaDevice class represents a media device that can\nbe concurrently accessed by different processes, for this reason\noverload the acquire/lock operations to be effectively nops.\n\nSigned-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n---\n include/libcamera/internal/media_device.h |  39 +++++++--\n src/libcamera/device_enumerator.cpp       |   8 +-\n src/libcamera/media_device.cpp            | 139 ++++++++++++++++++++++++++++++\n 3 files changed, 178 insertions(+), 8 deletions(-)","diff":"diff --git a/include/libcamera/internal/media_device.h b/include/libcamera/internal/media_device.h\nindex b3a48b98d64b019fb42c5224d29105e1d57a3e3c..ea0e9d66009933bbb9613d27423d2f951520c887 100644\n--- a/include/libcamera/internal/media_device.h\n+++ b/include/libcamera/internal/media_device.h\n@@ -21,18 +21,24 @@\n \n namespace libcamera {\n \n+class MediaDeviceFactory\n+{\n+public:\n+\tstatic std::unique_ptr<MediaDevice>\n+\tcreateMediaDevice(const std::string &deviceNode);\n+};\n+\n class MediaDevice : protected Loggable\n {\n public:\n-\tMediaDevice(const std::string &deviceNode);\n-\t~MediaDevice();\n+\tvirtual ~MediaDevice();\n \n-\tbool acquire();\n-\tvoid release();\n-\tbool busy() const { return acquired_; }\n+\tvirtual bool acquire();\n+\tvirtual void release();\n+\tvirtual bool busy() const { return acquired_; }\n \n-\tbool lock();\n-\tvoid unlock();\n+\tvirtual bool lock();\n+\tvirtual void unlock();\n \n \tint populate();\n \tbool isValid() const { return valid_; }\n@@ -58,9 +64,12 @@ public:\n \tstd::vector<MediaEntity *> locateEntities(unsigned int function);\n \n protected:\n+\tMediaDevice(const std::string &deviceNode);\n \tstd::string logPrefix() const override;\n \n private:\n+\tfriend class MediaDeviceFactory;\n+\n \tint open();\n \tvoid close();\n \n@@ -92,4 +101,20 @@ private:\n \tstd::vector<MediaEntity *> entities_;\n };\n \n+class SharedMediaDevice : public MediaDevice\n+{\n+public:\n+\tbool acquire() override;\n+\tvoid release() override;\n+\tbool busy() const override { return false; }\n+\n+\tbool lock() override;\n+\tvoid unlock() override;\n+\n+private:\n+\tfriend class MediaDeviceFactory;\n+\n+\tSharedMediaDevice(const std::string &deviceNode);\n+};\n+\n } /* namespace libcamera */\ndiff --git a/src/libcamera/device_enumerator.cpp b/src/libcamera/device_enumerator.cpp\nindex ae17862f676310ef568e5331106ed661e84b5130..e73bb0050843c5281986e8e05d2013102905d270 100644\n--- a/src/libcamera/device_enumerator.cpp\n+++ b/src/libcamera/device_enumerator.cpp\n@@ -217,7 +217,13 @@ DeviceEnumerator::~DeviceEnumerator()\n  */\n std::unique_ptr<MediaDevice> DeviceEnumerator::createDevice(const std::string &deviceNode)\n {\n-\tstd::unique_ptr<MediaDevice> media = std::make_unique<MediaDevice>(deviceNode);\n+\tstd::unique_ptr<MediaDevice> media = MediaDeviceFactory::createMediaDevice(deviceNode);\n+\tif (!media) {\n+\t\tLOG(DeviceEnumerator, Info)\n+\t\t\t<< \"Unable to create a media device for \" << deviceNode\n+\t\t\t<< \", skipping\";\n+\t\treturn nullptr;\n+\t}\n \n \tint ret = media->populate();\n \tif (ret < 0) {\ndiff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp\nindex 353f34a81eca600a4a35594d782d3dc5dd690bdf..70d073f964bde0236585de083baca4cd9c30d193 100644\n--- a/src/libcamera/media_device.cpp\n+++ b/src/libcamera/media_device.cpp\n@@ -31,6 +31,56 @@ namespace libcamera {\n \n LOG_DEFINE_CATEGORY(MediaDevice)\n \n+/**\n+ * \\class MediaDeviceFactory\n+ * \\brief Factory class that instantiates a MediaDevice or SharedMediaDevice\n+ *\n+ * The MediaDevice and SharedMediaDevice classes cannot be instantiated\n+ * directly, but instead the MediaDeviceFactory shall be used to create\n+ * an instance of these classes using the MediaDeviceFactory::createMediaDevice\n+ * function.\n+ */\n+\n+/**\n+ * \\brief Create an instance of the MediaDevice class hierarchy\n+ * \\param[in] deviceNode The media device node path\n+ *\n+ * If a media device supports multi-context operations and advertise it through\n+ * the presence of the MEDIA_DEVICE_FL_CONTEXT flag in the\n+ * media_device_info.flags field an instance of SharedMediaDevice will be\n+ * created and returned. If instead the media device doesn's support\n+ * multi-context operations an instance of the MediaDevice class is created and\n+ * returned.\n+ *\n+ * \\return A unique_ptr<> that wraps an instance of SharedMediaDevice or\n+ * MediaDevice\n+ */\n+std::unique_ptr<MediaDevice>\n+MediaDeviceFactory::createMediaDevice(const std::string &deviceNode)\n+{\n+\t/*\n+\t * Inspect the media device info flags to decide which class to\n+\t * instantiate.\n+\t */\n+\tauto fd = UniqueFD(::open(deviceNode.c_str(), O_RDWR | O_CLOEXEC));\n+\tif (!fd.isValid())\n+\t\treturn {};\n+\n+\tstruct media_device_info info = {};\n+\tint ret = ioctl(fd.get(), MEDIA_IOC_DEVICE_INFO, &info);\n+\tif (ret) {\n+\t\tLOG(MediaDevice, Error)\n+\t\t\t<< \"Failed to get media device info \" << strerror(-ret);\n+\t\treturn {};\n+\t}\n+\n+\tif (info.flags & MEDIA_DEVICE_FL_CONTEXT)\n+\t\treturn std::unique_ptr<SharedMediaDevice>\n+\t\t\t(new SharedMediaDevice(deviceNode));\n+\n+\treturn std::unique_ptr<MediaDevice>(new MediaDevice(deviceNode));\n+}\n+\n /**\n  * \\class MediaDevice\n  * \\brief The MediaDevice represents a Media Controller device with its full\n@@ -40,6 +90,9 @@ LOG_DEFINE_CATEGORY(MediaDevice)\n  * created, and that association is kept for the lifetime of the MediaDevice\n  * instance.\n  *\n+ * Instances of MediaDevice are created by the MediaDeviceFactory\n+ * createMediaDevice() function and cannot be directly instantiated.\n+ *\n  * The instance is created with an empty media graph. Before performing any\n  * other operation, it must be populate by calling populate(). Instances of\n  * MediaEntity, MediaPad and MediaLink are created to model the media graph,\n@@ -56,6 +109,30 @@ LOG_DEFINE_CATEGORY(MediaDevice)\n  * managers to claim media devices they support during enumeration.\n  */\n \n+/**\n+ * \\class SharedMediaDevice\n+ * \\brief The SharedMediaDevice represents a Media Controller device that\n+ * supports multi-context operations\n+ *\n+ * \\sa MediaDevice\n+ * \\sa MediaDeviceFactory\n+ *\n+ * A SharedMediaDevice is associated with a media controller device node that\n+ * supports multi-context operations. Compared to the MediaDevice base class a\n+ * SharedMediaDevice allows multiple open() calls to happen and is never\n+ * uniquely owned as each time the media device gets open a new context gets\n+ * created. This means that the acquire() and release() operations are\n+ * effectively nop (and as a consequence busy() always return false).\n+ *\n+ * Instances of MediaDevice are created by the MediaDeviceFactory\n+ * createMediaDevice() function and cannot be directly instantiated.\n+ *\n+ * Similarly to the MediaDevice class, instances of SharedMediaDevice are\n+ * created with an empty media graph and needs to be populated using the\n+ * populate() function. Media entities enumerated in the graph can be accessed\n+ * through the same functions as the ones provided by the MediaDevice class.\n+ */\n+\n /**\n  * \\brief Construct a MediaDevice\n  * \\param[in] deviceNode The media device node path\n@@ -68,6 +145,17 @@ MediaDevice::MediaDevice(const std::string &deviceNode)\n {\n }\n \n+/**\n+ * \\brief Construct a SharedMediaDevice\n+ * \\param[in] deviceNode The media device node path\n+ *\n+ * \\copydoc MediaDevice::MediaDevice\n+ */\n+SharedMediaDevice::SharedMediaDevice(const std::string &deviceNode)\n+\t: MediaDevice(deviceNode)\n+{\n+}\n+\n MediaDevice::~MediaDevice()\n {\n \tfd_.reset();\n@@ -113,6 +201,19 @@ bool MediaDevice::acquire()\n \treturn true;\n }\n \n+/**\n+ * \\brief Acquiring a SharedMediaDevice is a nop\n+ *\n+ * A SharedMediaDevice is designed to be opened multiple times. Acquiring\n+ * a SharedMediaDevice is effectively a nop.\n+ *\n+ * \\return Always return true\n+ */\n+bool SharedMediaDevice::acquire()\n+{\n+\treturn true;\n+}\n+\n /**\n  * \\brief Release a device previously claimed for exclusive use\n  * \\sa acquire(), busy()\n@@ -123,6 +224,15 @@ void MediaDevice::release()\n \tacquired_ = false;\n }\n \n+/**\n+ * \\brief Releasing a SharedMediaDevice is a nop\n+ *\n+ * As acquiring a SharedMediaDevice is a nop, releasing it is a nop as well.\n+ */\n+void SharedMediaDevice::release()\n+{\n+}\n+\n /**\n  * \\brief Lock the device to prevent it from being used by other instances of\n  * libcamera\n@@ -151,6 +261,19 @@ bool MediaDevice::lock()\n \treturn true;\n }\n \n+/**\n+ * \\brief Locking a SharedMediaDevice is a nop\n+ *\n+ * As SharedMediaDevice is designed to be opened multiple times locking\n+ * a SharedMediaDevice is effectively a nop.\n+ *\n+ * \\return Always return true\n+ */\n+bool SharedMediaDevice::lock()\n+{\n+\treturn true;\n+}\n+\n /**\n  * \\brief Unlock the device and free it for use for libcamera instances\n  *\n@@ -168,6 +291,15 @@ void MediaDevice::unlock()\n \tstd::ignore = lockf(fd_.get(), F_ULOCK, 0);\n }\n \n+/**\n+ * \\brief Unlocking a SharedMediaDevice is a nop\n+ *\n+ * As locking a SharedMediaDevice is a nop, unlocking it is a nop as well.\n+ */\n+void SharedMediaDevice::unlock()\n+{\n+}\n+\n /**\n  * \\fn MediaDevice::busy()\n  * \\brief Check if a device is in use\n@@ -176,6 +308,13 @@ void MediaDevice::unlock()\n  * \\sa acquire(), release()\n  */\n \n+/**\n+ * \\fn SharedMediaDevice::busy()\n+ * \\brief As SharedMediaDevice is designed to be opened multiple times, for this\n+ * reason it is never marked as busy\n+ * \\return Always returns false\n+ */\n+\n /**\n  * \\brief Populate the MediaDevice with device information and media objects\n  *\n","prefixes":["6/9"]}