<div dir="ltr"><div dir="ltr"><div>Hi Nicolas,</div><div>Thanks for the review.</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Aug 31, 2021 at 2:06 AM Nicolas Dufresne <<a href="mailto:nicolas@ndufresne.ca">nicolas@ndufresne.ca</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Thanks for the patch,<br>
<br>
see inline comment for possible improvements.<br>
<br>
Le lundi 30 août 2021 à 02:36 +0530, Vedant Paranjape a écrit :<br>
> Lot of code used in single stream test is boiler plate and common across<br>
> every gstreamer test. Factored out this code into a base class called<br>
> GstreamerTest.<br>
> <br>
> Also updated the gstreamer_single_stream_test to use the GstreamerTest<br>
> base class<br>
> <br>
> Signed-off-by: Vedant Paranjape <<a href="mailto:vedantparanjape160201@gmail.com" target="_blank">vedantparanjape160201@gmail.com</a>><br>
> ---<br>
> .../gstreamer_single_stream_test.cpp | 138 +++------------<br>
> test/gstreamer/gstreamer_test.cpp | 157 ++++++++++++++++++<br>
> test/gstreamer/gstreamer_test.h | 38 +++++<br>
> test/gstreamer/meson.build | 2 +-<br>
> 4 files changed, 216 insertions(+), 119 deletions(-)<br>
> create mode 100644 test/gstreamer/gstreamer_test.cpp<br>
> create mode 100644 test/gstreamer/gstreamer_test.h<br>
> <br>
> diff --git a/test/gstreamer/gstreamer_single_stream_test.cpp b/test/gstreamer/gstreamer_single_stream_test.cpp<br>
> index 4c8d4804..021ac269 100644<br>
> --- a/test/gstreamer/gstreamer_single_stream_test.cpp<br>
> +++ b/test/gstreamer/gstreamer_single_stream_test.cpp<br>
> @@ -14,86 +14,35 @@<br>
> <br>
> #include <gst/gst.h><br>
> <br>
> +#include "gstreamer_test.h"<br>
> #include "test.h"<br>
> <br>
> using namespace std;<br>
> <br>
> -extern "C" {<br>
> -const char *__asan_default_options()<br>
> -{<br>
> - /*<br>
> - * Disable leak detection due to a known global variable initialization<br>
> - * leak in glib's g_quark_init(). This should ideally be handled by<br>
> - * using a suppression file instead of disabling leak detection.<br>
> - */<br>
> - return "detect_leaks=false";<br>
> -}<br>
> -}<br>
> -<br>
> -class GstreamerSingleStreamTest : public Test<br>
> +class GstreamerSingleStreamTest : public GstreamerTest, public Test<br>
> {<br>
> +public:<br>
> + GstreamerSingleStreamTest()<br>
> + : GstreamerTest()<br>
> + {<br>
> + }<br>
> +<br>
> protected:<br>
> int init() override<br>
> {<br>
> - /*<br>
> - * GStreamer by default spawns a process to run the<br>
> - * gst-plugin-scanner helper. If libcamera is compiled with ASan<br>
> - * enabled, and as GStreamer is most likely not, this causes the<br>
> - * ASan link order check to fail when gst-plugin-scanner<br>
> - * dlopen()s the plugin as many libraries will have already been<br>
> - * loaded by then. Fix this issue by disabling spawning of a<br>
> - * child helper process when scanning the build directory for<br>
> - * plugins.<br>
> - */<br>
> - gst_registry_fork_set_enabled(false);<br>
> -<br>
> - /* Initialize GStreamer */<br>
> - g_autoptr(GError) errInit = NULL;<br>
> - if (!gst_init_check(nullptr, nullptr, &errInit)) {<br>
> - g_printerr("Could not initialize GStreamer: %s\n",<br>
> - errInit ? errInit->message : "unknown error");<br>
> -<br>
> - return TestFail;<br>
> - }<br>
> -<br>
> - /*<br>
> - * Remove the system libcamera plugin, if any, and add the<br>
> - * plugin from the build directory.<br>
> - */<br>
> - GstRegistry *registry = gst_registry_get();<br>
> - GstPlugin *plugin = gst_registry_lookup(registry, "libgstlibcamera.so");<br>
> - if (plugin) {<br>
> - gst_registry_remove_plugin(registry, plugin);<br>
> - gst_object_unref(plugin);<br>
> - }<br>
> -<br>
> - std::string path = libcamera::utils::libcameraBuildPath()<br>
> - + "src/gstreamer";<br>
> - if (!gst_registry_scan_path(registry, path.c_str())) {<br>
> - g_printerr("Failed to add plugin to registry\n");<br>
> - gst_deinit();<br>
> - return TestFail;<br>
> - }<br>
> + if (status_ != TestPass)<br>
> + return status_;<br>
> <br>
> - /* Create the elements */<br>
> - libcameraSrc_ = gst_element_factory_make("libcamerasrc", "libcamera");<br>
> convert0_ = gst_element_factory_make("videoconvert", "convert0");<br>
> sink0_ = gst_element_factory_make("fakesink", "sink0");<br>
> <br>
> - /* Create the empty pipeline_ */<br>
> - pipeline_ = gst_pipeline_new("test-pipeline");<br>
> -<br>
> - if (!pipeline_ || !convert0_ || !sink0_ || !libcameraSrc_) {<br>
> - g_printerr("Not all elements could be created. %p.%p.%p.%p\n",<br>
> - pipeline_, convert0_, sink0_, libcameraSrc_);<br>
> - if (pipeline_)<br>
> - gst_object_unref(pipeline_);<br>
> + if (!convert0_ || !sink0_) {<br>
> + g_printerr("Not all elements could be created. %p.%p\n",<br>
> + convert0_, sink0_);<br>
> if (convert0_)<br>
> gst_object_unref(convert0_);<br>
> if (sink0_)<br>
> gst_object_unref(sink0_);<br>
> - if (libcameraSrc_)<br>
> - gst_object_unref(libcameraSrc_);<br>
> gst_deinit();<br>
> <br>
> return TestFail;<br>
> @@ -102,16 +51,8 @@ protected:<br>
> return TestPass;<br>
> }<br>
> <br>
> - void cleanup() override<br>
> - {<br>
> - gst_object_unref(pipeline_);<br>
> - gst_deinit();<br>
> - }<br>
> -<br>
> int run() override<br>
> {<br>
> - GstStateChangeReturn ret;<br>
> -<br>
> /* Build the pipeline */<br>
> gst_bin_add_many(GST_BIN(pipeline_), libcameraSrc_, convert0_, sink0_, NULL);<br>
> if (gst_element_link_many(libcameraSrc_, convert0_, sink0_, NULL) != TRUE) {<br>
> @@ -119,57 +60,18 @@ protected:<br>
> return TestFail;<br>
> }<br>
> <br>
> - /* Start playing */<br>
> - ret = gst_element_set_state(pipeline_, GST_STATE_PLAYING);<br>
> - if (ret == GST_STATE_CHANGE_FAILURE) {<br>
> - g_printerr("Unable to set the pipeline to the playing state.\n");<br>
> - return TestFail;<br>
> - }<br>
> + gstreamer_start_pipeline();<br>
> + if (status_ != TestPass)<br>
> + return status_;<br>
> <br>
> - /* Wait until error or EOS or timeout after 2 seconds */<br>
> - constexpr GstMessageType msgType =<br>
> - static_cast<GstMessageType>(GST_MESSAGE_ERROR | GST_MESSAGE_EOS);<br>
> - constexpr GstClockTime timeout = 2 * GST_SECOND;<br>
> -<br>
> - g_autoptr(GstBus) bus = gst_element_get_bus(pipeline_);<br>
> - g_autoptr(GstMessage) msg = gst_bus_timed_pop_filtered(bus, timeout, msgType);<br>
> -<br>
> - gst_element_set_state(pipeline_, GST_STATE_NULL);<br>
> -<br>
> - /* Parse error message */<br>
> - if (msg == NULL)<br>
> - return TestPass;<br>
> -<br>
> - switch (GST_MESSAGE_TYPE(msg)) {<br>
> - case GST_MESSAGE_ERROR:<br>
> - gstreamer_print_error(msg);<br>
> - break;<br>
> - case GST_MESSAGE_EOS:<br>
> - g_print("End-Of-Stream reached.\n");<br>
> - break;<br>
> - default:<br>
> - g_printerr("Unexpected message received.\n");<br>
> - break;<br>
> - }<br>
> + gstreamer_wait_for_event();<br>
> + if (status_ != TestPass)<br>
> + return status_;<br>
> <br>
> - return TestFail;<br>
> + return TestPass;<br>
> }<br>
> <br>
> private:<br>
> - void gstreamer_print_error(GstMessage *msg)<br>
> - {<br>
> - g_autoptr(GError) err = NULL;<br>
> - g_autofree gchar *debug_info = NULL;<br>
> -<br>
> - gst_message_parse_error(msg, &err, &debug_info);<br>
> - g_printerr("Error received from element %s: %s\n",<br>
> - GST_OBJECT_NAME(msg->src), err->message);<br>
> - g_printerr("Debugging information: %s\n",<br>
> - debug_info ? debug_info : "none");<br>
> - }<br>
> -<br>
> - GstElement *pipeline_;<br>
> - GstElement *libcameraSrc_;<br>
> GstElement *convert0_;<br>
> GstElement *sink0_;<br>
> };<br>
> diff --git a/test/gstreamer/gstreamer_test.cpp b/test/gstreamer/gstreamer_test.cpp<br>
> new file mode 100644<br>
> index 00000000..34608a02<br>
> --- /dev/null<br>
> +++ b/test/gstreamer/gstreamer_test.cpp<br>
> @@ -0,0 +1,157 @@<br>
> +/* SPDX-License-Identifier: GPL-2.0-or-later */<br>
> +/*<br>
> + * Copyright (C) 2021, Vedant Paranjape<br>
> + *<br>
> + * libcamera Gstreamer element API tests<br>
> + */<br>
> +<br>
> +#include "gstreamer_test.h"<br>
> +<br>
> +#include "test.h"<br>
> +<br>
> +using namespace std;<br>
> +<br>
> +extern "C" {<br>
> +const char *__asan_default_options()<br>
> +{<br>
> + /*<br>
> + * Disable leak detection due to a known global variable initialization<br>
> + * leak in glib's g_quark_init(). This should ideally be handled by<br>
> + * using a suppression file instead of disabling leak detection.<br>
> + */<br>
> + return "detect_leaks=false";<br>
> +}<br>
> +}<br>
> +<br>
> +GstreamerTest::GstreamerTest()<br>
> +{<br>
> + /*<br>
> + * GStreamer by default spawns a process to run the<br>
> + * gst-plugin-scanner helper. If libcamera is compiled with ASan<br>
> + * enabled, and as GStreamer is most likely not, this causes the<br>
> + * ASan link order check to fail when gst-plugin-scanner<br>
> + * dlopen()s the plugin as many libraries will have already been<br>
> + * loaded by then. Fix this issue by disabling spawning of a<br>
> + * child helper process when scanning the build directory for<br>
> + * plugins.<br>
> + */<br>
> + gst_registry_fork_set_enabled(false);<br>
> +<br>
> + /* Initialize GStreamer */<br>
> + g_autoptr(GError) errInit = NULL;<br>
> + if (!gst_init_check(nullptr, nullptr, &errInit)) {<br>
> + g_printerr("Could not initialize GStreamer: %s\n",<br>
> + errInit ? errInit->message : "unknown error");<br>
> +<br>
> + status_ = TestFail;<br>
> + return;<br>
> + }<br>
> +<br>
> + /*<br>
> + * Remove the system libcamera plugin, if any, and add the<br>
> + * plugin from the build directory.<br>
> + */<br>
<br>
Indent looks wrong, did you enable the commit hooks ?<br></blockquote><div><br></div><div>Typo on my side. Fixed.</div><div> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> + GstRegistry *registry = gst_registry_get();<br>
> + GstPlugin *plugin = gst_registry_lookup(registry, "libgstlibcamera.so");<br>
> + if (plugin) {<br>
> + gst_registry_remove_plugin(registry, plugin);<br>
> + gst_object_unref(plugin);<br>
<br>
nit: Perhaps use a g_autoptr() instead ?<br>
<br>
> + }<br>
> +<br>
> + std::string path = libcamera::utils::libcameraBuildPath() + "src/gstreamer";<br>
> + if (!gst_registry_scan_path(registry, path.c_str())) {<br>
> + g_printerr("Failed to add plugin to registry\n");<br>
> + gst_deinit();<br>
<br>
Move this in the destructor ? Note that gst_deinit() can only be run once per<br>
process live-time. So if you have multiple test in the same process, make sure<br>
this happens only once.<br>
<br>
> + status_ = TestFail;<br>
> + return;<br>
> + }<br>
> +<br>
> + /* Create the elements */<br>
> + libcameraSrc_ = gst_element_factory_make("libcamerasrc", "libcamera");<br>
> + pipeline_ = gst_pipeline_new("test-pipeline");<br>
<br>
Personally, I would entirely defer this, and create a virtual func to create the<br>
pipeline. This way test can use gst_parse_launch() instead of handcrafting test<br>
pipelines.<br></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> +<br>
> + if (!pipeline_ || !libcameraSrc_) {<br>
> + g_printerr("Not all elements could be created. %p.%p\n",<br>
> + pipeline_, libcameraSrc_);<br>
> + if (pipeline_)<br>
> + gst_object_unref(pipeline_);<br>
> + if (libcameraSrc_)<br>
> + gst_object_unref(libcameraSrc_);<br>
To avoid this overhead, use local g_autoptr() and then blabla_ =<br>
g_steal_pointer() to save it.<br>
> + gst_deinit();<br>
> + status_ = TestFail;<br>
> + return;<br>
> + }<br>
> +<br>
> + status_ = TestPass;<br>
<br>
I peaked at the usage, and I'm not big fan, have you considered adding an init()<br>
function with a proper return value? The C++ way is to use exception, not sure<br>
what the libcamera project thinks about it.<br></blockquote><div><br></div><div>Just did what other base test classes did.</div><div> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> +}<br>
> +<br>
> +GstreamerTest::~GstreamerTest()<br>
> +{<br>
> + gst_object_unref(pipeline_);<br>
> + if (status_ == TestFail) {<br>
> + gst_object_unref(libcameraSrc_);<br>
> + }<br>
> +<br>
> + gst_deinit();<br>
<br>
As you already do this here, and destructor get called regardless if the<br>
constructor worked or not, I think ou can remove all the duplicates in the<br>
constructor.<br>
<br>
> +}<br>
> +<br>
> +void GstreamerTest::gstreamer_start_pipeline()<br>
> +{<br>
> + GstStateChangeReturn ret;<br>
> +<br>
> + /* Start playing */<br>
> + ret = gst_element_set_state(pipeline_, GST_STATE_PLAYING);<br>
> + if (ret == GST_STATE_CHANGE_FAILURE) {<br>
> + g_printerr("Unable to set the pipeline to the playing state.\n");<br>
> + status_ = TestFail;<br>
> + return;<br>
> + }<br>
> +<br>
> + status_ = TestPass;<br>
<br>
Why not use a return value ?<br></blockquote><div><br></div><div>Nice catch, fixed it !</div><div> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> +}<br>
> +<br>
> +void GstreamerTest::gstreamer_wait_for_event()<br>
> +{<br>
> + /* Wait until error or EOS or timeout after 2 seconds */<br>
> + constexpr GstMessageType msgType =<br>
> + static_cast<GstMessageType>(GST_MESSAGE_ERROR | GST_MESSAGE_EOS);<br>
> + constexpr GstClockTime timeout = 2 * GST_SECOND;<br>
> +<br>
> + g_autoptr(GstBus) bus = gst_element_get_bus(pipeline_);<br>
> + g_autoptr(GstMessage) msg = gst_bus_timed_pop_filtered(bus, timeout, msgType);<br>
<br>
Can we make the msgType a optional argument here ? IIRC C++ allow for default<br>
value.<br></blockquote><div><br></div><div> Not sure how to do it, since I am not defining a function.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> +<br>
> + gst_element_set_state(pipeline_, GST_STATE_NULL);<br>
<br>
The method is called "wait_for_event", having it stop the pipeline is a bit<br>
unexpected.<br></blockquote><div><br></div><div>"process_event" sounds much better ?</div><div> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> +<br>
> + /* Parse error message */<br>
> + if (msg == NULL) {<br>
> + status_ = TestPass;<br>
> + return;<br>
> + }<br>
> +<br>
> + switch (GST_MESSAGE_TYPE(msg)) {<br>
> + case GST_MESSAGE_ERROR:<br>
> + gstreamer_print_error(msg);<br>
> + break;<br>
> + case GST_MESSAGE_EOS:<br>
> + g_print("End-Of-Stream reached.\n");<br>
> + break;<br>
> + default:<br>
> + g_printerr("Unexpected message received.\n");<br>
> + break;<br>
> + }<br>
> +<br>
> + status_ = TestPass;<br>
> +}<br>
> +<br>
> +void GstreamerTest::gstreamer_print_error(GstMessage *msg)<br>
> +{<br>
> + g_autoptr(GError) err = NULL;<br>
> + g_autofree gchar *debug_info = NULL;<br>
> +<br>
> + gst_message_parse_error(msg, &err, &debug_info);<br>
> + g_printerr("Error received from element %s: %s\n",<br>
> + GST_OBJECT_NAME(msg->src), err->message);<br>
> + g_printerr("Debugging information: %s\n",<br>
> + debug_info ? debug_info : "none");<br>
> +}<br>
> +<br>
> diff --git a/test/gstreamer/gstreamer_test.h b/test/gstreamer/gstreamer_test.h<br>
> new file mode 100644<br>
> index 00000000..8e117166<br>
> --- /dev/null<br>
> +++ b/test/gstreamer/gstreamer_test.h<br>
> @@ -0,0 +1,38 @@<br>
> +/* SPDX-License-Identifier: GPL-2.0-or-later */<br>
> +/*<br>
> + * Copyright (C) 2021, Vedant Paranjape<br>
> + *<br>
> + * gstreamer_test.cpp - GStreamer test base class<br>
> + */<br>
> +<br>
> +#ifndef __LIBCAMERA_GSTREAMER_TEST_H__<br>
> +#define __LIBCAMERA_GSTREAMER_TEST_H__<br>
> +<br>
> +#include <iostream><br>
> +#include <unistd.h><br>
> +<br>
> +#include <libcamera/base/utils.h><br>
> +<br>
> +#include "libcamera/internal/source_paths.h"<br>
> +<br>
> +#include <gst/gst.h><br>
> +<br>
> +using namespace std;<br>
> +<br>
> +class GstreamerTest<br>
> +{<br>
> +public:<br>
> + GstreamerTest();<br>
> + ~GstreamerTest();<br>
> +<br>
> +protected:<br>
> + void gstreamer_start_pipeline();<br>
> + void gstreamer_wait_for_event();<br>
> + void gstreamer_print_error(GstMessage *msg);<br>
> +<br>
> + GstElement *pipeline_;<br>
> + GstElement *libcameraSrc_;<br>
> + int status_;<br>
> +};<br>
> +<br>
> +#endif /* __LIBCAMERA_GSTREAMER_TEST_H__ */<br>
> \ No newline at end of file<br>
> diff --git a/test/gstreamer/meson.build b/test/gstreamer/meson.build<br>
> index b99aa0da..aca53b92 100644<br>
> --- a/test/gstreamer/meson.build<br>
> +++ b/test/gstreamer/meson.build<br>
> @@ -10,7 +10,7 @@ gstreamer_tests = [<br>
> gstreamer_dep = dependency('gstreamer-1.0', required: true)<br>
> <br>
> foreach t : gstreamer_tests<br>
> - exe = executable(t[0], t[1],<br>
> + exe = executable(t[0], t[1], 'gstreamer_test.cpp',<br>
> dependencies : [libcamera_private, gstreamer_dep],<br>
> link_with : test_libraries,<br>
> include_directories : test_includes_internal)<br>
<br>
<br></blockquote><div><br></div><div>Regards,</div><div><i><b>Vedant Paranjape</b></i><br></div></div></div>