Show a patch.

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

{
    "id": 3015,
    "url": "https://patchwork.libcamera.org/api/patches/3015/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/3015/",
    "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": "<20200306202637.525587-10-nicolas@ndufresne.ca>",
    "date": "2020-03-06T20:26:19",
    "name": "[libcamera-devel,v3,09/27] gst: libcamerasrc: Implement selection and acquisition",
    "commit_ref": null,
    "pull_url": null,
    "state": "accepted",
    "archived": false,
    "hash": "b8413c633b8d155c017c486c13452de40dd84f37",
    "submitter": {
        "id": 30,
        "url": "https://patchwork.libcamera.org/api/people/30/?format=api",
        "name": "Nicolas Dufresne",
        "email": "nicolas@ndufresne.ca"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/3015/mbox/",
    "series": [
        {
            "id": 705,
            "url": "https://patchwork.libcamera.org/api/series/705/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=705",
            "date": "2020-03-06T20:26:10",
            "name": "GStreamer Element for libcamera",
            "version": 3,
            "mbox": "https://patchwork.libcamera.org/series/705/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/3015/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/3015/checks/",
    "tags": {},
    "headers": {
        "Return-Path": "<nicolas@ndufresne.ca>",
        "Received": [
            "from bhuna.collabora.co.uk (bhuna.collabora.co.uk [46.235.227.227])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 6914C628CC\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri,  6 Mar 2020 21:27:04 +0100 (CET)",
            "from nicolas-tpx395.localdomain (unknown [IPv6:2610:98:8005::527])\n\t(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256\n\tbits))\n\t(No client certificate requested) (Authenticated sender: nicolas)\n\tby bhuna.collabora.co.uk (Postfix) with ESMTPSA id D6D432972F1;\n\tFri,  6 Mar 2020 20:27:03 +0000 (GMT)"
        ],
        "From": "Nicolas Dufresne <nicolas@ndufresne.ca>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Cc": "Nicolas Dufresne <nicolas.dufresne@collabora.com>,\n\tLaurent Pinchart <laurent.pinchart@ideasonboard.com>",
        "Date": "Fri,  6 Mar 2020 15:26:19 -0500",
        "Message-Id": "<20200306202637.525587-10-nicolas@ndufresne.ca>",
        "X-Mailer": "git-send-email 2.24.1",
        "In-Reply-To": "<20200306202637.525587-1-nicolas@ndufresne.ca>",
        "References": "<20200306202637.525587-1-nicolas@ndufresne.ca>",
        "MIME-Version": "1.0",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [PATCH v3 09/27] gst: libcamerasrc: Implement\n\tselection and acquisition",
        "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>",
        "X-List-Received-Date": "Fri, 06 Mar 2020 20:27:04 -0000"
    },
    "content": "From: Nicolas Dufresne <nicolas.dufresne@collabora.com>\n\nThis adds code to select and acquire a camera. With this, it is now\npossible to run a pipeline like:\n\n   gst-launch-1.0 libcamerasrc ! fakesink\n\nThough no buffer will be streamed yet. In this function, we implement the\nchange_state() virtual method to trigger actions on specific state transitions.\nNote that we also return GST_STATE_CHANGE_NO_PREROLL in\nGST_STATE_CHANGE_READY_TO_PAUSED and GST_STATE_CHANGE_PLAYING_TO_PAUSED\ntransitions as this is required for all live sources.\n\nSigned-off-by: Nicolas Dufresne <nicolas.dufresne@collabora.com>\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n src/gstreamer/gstlibcamerasrc.cpp | 125 ++++++++++++++++++++++++++++++\n 1 file changed, 125 insertions(+)",
    "diff": "diff --git a/src/gstreamer/gstlibcamerasrc.cpp b/src/gstreamer/gstlibcamerasrc.cpp\nindex 3e13a00..fd0eaf5 100644\n--- a/src/gstreamer/gstlibcamerasrc.cpp\n+++ b/src/gstreamer/gstlibcamerasrc.cpp\n@@ -8,16 +8,29 @@\n \n #include \"gstlibcamerasrc.h\"\n \n+#include <libcamera/camera.h>\n+#include <libcamera/camera_manager.h>\n+\n #include \"gstlibcamerapad.h\"\n #include \"gstlibcamera-utils.h\"\n \n+using namespace libcamera;\n+\n GST_DEBUG_CATEGORY_STATIC(source_debug);\n #define GST_CAT_DEFAULT source_debug\n \n+/* Used for C++ object with destructors. */\n+struct GstLibcameraSrcState {\n+\tstd::unique_ptr<CameraManager> cm_;\n+\tstd::shared_ptr<Camera> cam_;\n+};\n+\n struct _GstLibcameraSrc {\n \tGstElement parent;\n \tGstPad *srcpad;\n \tgchar *camera_name;\n+\n+\tGstLibcameraSrcState *state;\n };\n \n enum {\n@@ -41,6 +54,83 @@ GstStaticPadTemplate request_src_template = {\n \t\"src_%s\", GST_PAD_SRC, GST_PAD_REQUEST, TEMPLATE_CAPS\n };\n \n+static bool\n+gst_libcamera_src_open(GstLibcameraSrc *self)\n+{\n+\tstd::unique_ptr<CameraManager> cm = std::make_unique<CameraManager>();\n+\tstd::shared_ptr<Camera> cam;\n+\tgint ret = 0;\n+\n+\tGST_DEBUG_OBJECT(self, \"Opening camera device ...\");\n+\n+\tret = cm->start();\n+\tif (ret) {\n+\t\tGST_ELEMENT_ERROR(self, LIBRARY, INIT,\n+\t\t\t\t  (\"Failed listing cameras.\"),\n+\t\t\t\t  (\"libcamera::CameraMananger::start() failed: %s\", g_strerror(-ret)));\n+\t\treturn false;\n+\t}\n+\n+\tg_autofree gchar *camera_name = nullptr;\n+\t{\n+\t\tGLibLocker lock(GST_OBJECT(self));\n+\t\tif (self->camera_name)\n+\t\t\tcamera_name = g_strdup(self->camera_name);\n+\t}\n+\n+\tif (camera_name) {\n+\t\tcam = cm->get(self->camera_name);\n+\t\tif (!cam) {\n+\t\t\tGST_ELEMENT_ERROR(self, RESOURCE, NOT_FOUND,\n+\t\t\t\t\t  (\"Could not find a camera named '%s'.\", self->camera_name),\n+\t\t\t\t\t  (\"libcamera::CameraMananger::get() returned nullptr\"));\n+\t\t\treturn false;\n+\t\t}\n+\t} else {\n+\t\tif (cm->cameras().empty()) {\n+\t\t\tGST_ELEMENT_ERROR(self, RESOURCE, NOT_FOUND,\n+\t\t\t\t\t  (\"Could not find any supported camera on this system.\"),\n+\t\t\t\t\t  (\"libcamera::CameraMananger::cameras() is empty\"));\n+\t\t\treturn false;\n+\t\t}\n+\t\tcam = cm->cameras()[0];\n+\t}\n+\n+\tGST_INFO_OBJECT(self, \"Using camera named '%s'\", cam->name().c_str());\n+\n+\tret = cam->acquire();\n+\tif (ret) {\n+\t\tGST_ELEMENT_ERROR(self, RESOURCE, BUSY,\n+\t\t\t\t  (\"Camera name '%s' is already in use.\", cam->name().c_str()),\n+\t\t\t\t  (\"libcamera::Camera::acquire() failed: %s\", g_strerror(ret)));\n+\t\treturn false;\n+\t}\n+\n+\t/* No need to lock here, we didn't start our threads yet. */\n+\tself->state->cm_ = std::move(cm);\n+\tself->state->cam_ = cam;\n+\n+\treturn true;\n+}\n+\n+static void\n+gst_libcamera_src_close(GstLibcameraSrc *self)\n+{\n+\tGstLibcameraSrcState *state = self->state;\n+\tgint ret;\n+\n+\tret = state->cam_->release();\n+\tif (ret) {\n+\t\tGST_ELEMENT_WARNING(self, RESOURCE, BUSY,\n+\t\t\t\t    (\"Camera name '%s' is still in use.\", state->cam_->name().c_str()),\n+\t\t\t\t    (\"libcamera::Camera.release() failed: %s\", g_strerror(-ret)));\n+\t}\n+\n+\tstate->cam_.reset();\n+\tstate->cm_->stop();\n+\tstate->cm_.reset();\n+}\n+\n static void\n gst_libcamera_src_set_property(GObject *object, guint prop_id,\n \t\t\t       const GValue *value, GParamSpec *pspec)\n@@ -76,6 +166,36 @@ gst_libcamera_src_get_property(GObject *object, guint prop_id, GValue *value,\n \t}\n }\n \n+static GstStateChangeReturn\n+gst_libcamera_src_change_state(GstElement *element, GstStateChange transition)\n+{\n+\tGstLibcameraSrc *self = GST_LIBCAMERA_SRC(element);\n+\tGstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;\n+\tGstElementClass *klass = GST_ELEMENT_CLASS(gst_libcamera_src_parent_class);\n+\n+\tret = klass->change_state(element, transition);\n+\tif (ret == GST_STATE_CHANGE_FAILURE)\n+\t\treturn ret;\n+\n+\tswitch (transition) {\n+\tcase GST_STATE_CHANGE_NULL_TO_READY:\n+\t\tif (!gst_libcamera_src_open(self))\n+\t\t\treturn GST_STATE_CHANGE_FAILURE;\n+\t\tbreak;\n+\tcase GST_STATE_CHANGE_READY_TO_PAUSED:\n+\tcase GST_STATE_CHANGE_PLAYING_TO_PAUSED:\n+\t\tret = GST_STATE_CHANGE_NO_PREROLL;\n+\t\tbreak;\n+\tcase GST_STATE_CHANGE_READY_TO_NULL:\n+\t\tgst_libcamera_src_close(self);\n+\t\tbreak;\n+\tdefault:\n+\t\tbreak;\n+\t}\n+\n+\treturn ret;\n+}\n+\n static void\n gst_libcamera_src_finalize(GObject *object)\n {\n@@ -83,6 +203,7 @@ gst_libcamera_src_finalize(GObject *object)\n \tGstLibcameraSrc *self = GST_LIBCAMERA_SRC(object);\n \n \tg_free(self->camera_name);\n+\tdelete self->state;\n \n \treturn klass->finalize(object);\n }\n@@ -90,10 +211,12 @@ gst_libcamera_src_finalize(GObject *object)\n static void\n gst_libcamera_src_init(GstLibcameraSrc *self)\n {\n+\tGstLibcameraSrcState *state = new GstLibcameraSrcState();\n \tGstPadTemplate *templ = gst_element_get_pad_template(GST_ELEMENT(self), \"src\");\n \n \tself->srcpad = gst_pad_new_from_template(templ, \"src\");\n \tgst_element_add_pad(GST_ELEMENT(self), self->srcpad);\n+\tself->state = state;\n }\n \n static void\n@@ -106,6 +229,8 @@ gst_libcamera_src_class_init(GstLibcameraSrcClass *klass)\n \tobject_class->get_property = gst_libcamera_src_get_property;\n \tobject_class->finalize = gst_libcamera_src_finalize;\n \n+\telement_class->change_state = gst_libcamera_src_change_state;\n+\n \tgst_element_class_set_metadata(element_class,\n \t\t\t\t       \"libcamera Source\", \"Source/Video\",\n \t\t\t\t       \"Linux Camera source using libcamera\",\n",
    "prefixes": [
        "libcamera-devel",
        "v3",
        "09/27"
    ]
}