[{"id":3972,"web_url":"https://patchwork.libcamera.org/comment/3972/","msgid":"<20200306204104.GU4878@pendragon.ideasonboard.com>","date":"2020-03-06T20:41:04","subject":"Re: [libcamera-devel] [PATCH v3 15/27] gst: libcamerasrc: Implement\n\tminimal caps negotiation","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Nicolas,\n\nThank you for the patch.\n\nOn Fri, Mar 06, 2020 at 03:26:25PM -0500, Nicolas Dufresne wrote:\n> From: Nicolas Dufresne <nicolas.dufresne@collabora.com>\n> \n> This is not expected to work in every possible cases, but should be sufficient as\n> an initial implementation. What it does is that it turns the StreamFormats into\n> caps and queries downstream caps with that as a filter.\n> \n> The result is the subset of caps that can be used. We then keep the first\n> structure in that result and fixate using the default values found in\n> StreamConfiguration as a default in case a range is available.\n> \n> We then validate this configuration and turn the potentially modified\n> configuration into caps that we push downstream. Note that we trust the order\n> in StreamFormats as being sorted best first, but this is not currently in\n> libcamera. A todo has been added in the head of this file as a reminder to fix\n> that in the core.\n> \n> Signed-off-by: Nicolas Dufresne <nicolas.dufresne@collabora.com>\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> ---\n>  src/gstreamer/gstlibcamerasrc.cpp | 81 +++++++++++++++++++++++++++++++\n>  1 file changed, 81 insertions(+)\n> \n> diff --git a/src/gstreamer/gstlibcamerasrc.cpp b/src/gstreamer/gstlibcamerasrc.cpp\n> index 1680314..95af141 100644\n> --- a/src/gstreamer/gstlibcamerasrc.cpp\n> +++ b/src/gstreamer/gstlibcamerasrc.cpp\n> @@ -6,6 +6,12 @@\n>   * gstlibcamerasrc.cpp - GStreamer Capture Element\n>   */\n>  \n> +/**\n> + * \\todo libcamera UVC drivers picks the lowest possible resolution first, this\n> + * should be fixed so that we get a decent resolution and framerate for the\n> + * role by default.\n> + */\n> +\n>  #include \"gstlibcamerasrc.h\"\n>  \n>  #include <vector>\n> @@ -25,6 +31,7 @@ GST_DEBUG_CATEGORY_STATIC(source_debug);\n>  struct GstLibcameraSrcState {\n>  \tstd::unique_ptr<CameraManager> cm_;\n>  \tstd::shared_ptr<Camera> cam_;\n> +\tstd::unique_ptr<CameraConfiguration> config_;\n>  \tstd::vector<GstPad *> srcpads_;\n>  };\n>  \n> @@ -133,16 +140,90 @@ gst_libcamera_src_task_enter(GstTask *task, GThread *thread, gpointer user_data)\n>  \tGstLibcameraSrc *self = GST_LIBCAMERA_SRC(user_data);\n>  \tGLibRecLocker lock(&self->stream_lock);\n>  \tGstLibcameraSrcState *state = self->state;\n> +\tGstFlowReturn flow_ret = GST_FLOW_OK;\n> +\tgint ret;\n>  \n>  \tGST_DEBUG_OBJECT(self, \"Streaming thread has started\");\n>  \n>  \tguint group_id = gst_util_group_id_next();\n> +\tStreamRoles roles;\n>  \tfor (GstPad *srcpad : state->srcpads_) {\n>  \t\t/* Create stream-id and push stream-start. */\n>  \t\tg_autofree gchar *stream_id = gst_pad_create_stream_id(srcpad, GST_ELEMENT(self), nullptr);\n>  \t\tGstEvent *event = gst_event_new_stream_start(stream_id);\n>  \t\tgst_event_set_group_id(event, group_id);\n>  \t\tgst_pad_push_event(srcpad, event);\n> +\n> +\t\t/* Collect the streams roles for the next iteration. */\n> +\t\troles.push_back(gst_libcamera_pad_get_role(srcpad));\n> +\t}\n> +\n> +\t/* Generate the stream configurations, there should be one per pad. */\n> +\tstate->config_ = state->cam_->generateConfiguration(roles);\n> +\t/*\n> +\t * \\todo Check if camera may increase or decrease the number of streams\n> +\t * regardless of the number of roles.\n> +\t */\n> +\tg_assert(state->config_->size() == state->srcpads_.size());\n> +\n> +\tfor (gsize i = 0; i < state->srcpads_.size(); i++) {\n> +\t\tGstPad *srcpad = state->srcpads_[i];\n> +\t\tStreamConfiguration &stream_cfg = state->config_->at(i);\n> +\n> +\t\t/* Retrieve the supported caps. */\n> +\t\tg_autoptr(GstCaps) filter = gst_libcamera_stream_formats_to_caps(stream_cfg.formats());\n> +\t\tg_autoptr(GstCaps) caps = gst_pad_peer_query_caps(srcpad, filter);\n> +\t\tif (gst_caps_is_empty(caps)) {\n> +\t\t\tflow_ret = GST_FLOW_NOT_NEGOTIATED;\n> +\t\t\tbreak;\n> +\t\t}\n> +\n> +\t\t/* Fixate caps and configure the stream. */\n> +\t\tcaps = gst_caps_make_writable(caps);\n> +\t\tgst_libcamera_configure_stream_from_caps(stream_cfg, caps);\n> +\t}\n> +\n> +\tif (flow_ret != GST_FLOW_OK)\n> +\t\tgoto done;\n> +\n> +\t/* Validate the configuration. */\n> +\tif (state->config_->validate() == CameraConfiguration::Invalid) {\n> +\t\tflow_ret = GST_FLOW_NOT_NEGOTIATED;\n> +\t\tgoto done;\n> +\t}\n> +\n> +\t/*\n> +\t * Regardless if it has been modified, create clean caps and push the\n> +\t * caps event. Downstream will decide if the caps are acceptable.\n> +\t */\n> +\tfor (gsize i = 0; i < state->srcpads_.size(); i++) {\n> +\t\tGstPad *srcpad = state->srcpads_[i];\n> +\t\tconst StreamConfiguration &stream_cfg = state->config_->at(i);\n> +\n> +\t\tg_autoptr(GstCaps) caps = gst_libcamera_stream_configuration_to_caps(stream_cfg);\n> +\t\tif (!gst_pad_push_event(srcpad, gst_event_new_caps(caps))) {\n> +\t\t\tflow_ret = GST_FLOW_NOT_NEGOTIATED;\n> +\t\t\tbreak;\n> +\t\t}\n> +\t}\n> +\n> +\tret = state->cam_->configure(state->config_.get());\n> +\tif (ret) {\n> +\t\tGST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,\n> +\t\t\t\t  (\"Failed to configure camera: %s\", g_strerror(-ret)),\n> +\t\t\t\t  (\"Camera::configure() failed with error code %i\", ret));\n> +\t\tgst_task_stop(task);\n> +\t\treturn;\n> +\t}\n> +\n> +done:\n> +\tswitch (flow_ret) {\n> +\tcase GST_FLOW_NOT_NEGOTIATED:\n> +\t\tGST_ELEMENT_FLOW_ERROR(self, flow_ret);\n> +\t\tgst_task_stop(task);\n> +\t\tbreak;\n> +\tdefault:\n> +\t\tbreak;\n>  \t}\n>  }\n>","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 15D6F60424\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri,  6 Mar 2020 21:41:08 +0100 (CET)","from pendragon.ideasonboard.com (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 7D2EE24B;\n\tFri,  6 Mar 2020 21:41:07 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1583527267;\n\tbh=sg3D2Q46dy9sxb4vrNQHHwxcc0CFdEgdiZtFI7CmXbc=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=GUsOopHeYjSDBBElg/0dmlUKUBGTadPMJebpzT6ZlMtisISdtUSP6sHKLQ9A4XG69\n\tpSK17n1PuHEqD3x2bnR1Z0KUwgrGpAZUV7oRdLiqVFm0b+JerKBVYkzD5hDyxkfh9C\n\tpxYgDvPB6SzScSDjP7CKqLNWgeVbSwaha69y+WgU=","Date":"Fri, 6 Mar 2020 22:41:04 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Nicolas Dufresne <nicolas@ndufresne.ca>","Cc":"libcamera-devel@lists.libcamera.org,\n\tNicolas Dufresne <nicolas.dufresne@collabora.com>","Message-ID":"<20200306204104.GU4878@pendragon.ideasonboard.com>","References":"<20200306202637.525587-1-nicolas@ndufresne.ca>\n\t<20200306202637.525587-16-nicolas@ndufresne.ca>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20200306202637.525587-16-nicolas@ndufresne.ca>","User-Agent":"Mutt/1.10.1 (2018-07-13)","Subject":"Re: [libcamera-devel] [PATCH v3 15/27] gst: libcamerasrc: Implement\n\tminimal caps negotiation","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:41:08 -0000"}}]