<div dir="auto"><br><br><div class="gmail_quote" dir="auto"><div dir="ltr" class="gmail_attr">On Wed, 1 Sep, 2021, 01:12 Nicolas Dufresne, <<a href="mailto:nicolas@ndufresne.ca">nicolas@ndufresne.ca</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Le mardi 31 août 2021 à 12:07 +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" rel="noreferrer">vedantparanjape160201@gmail.com</a>><br>
> ---<br>
>  .../gstreamer_single_stream_test.cpp          | 145 +++------------<br>
>  test/gstreamer/gstreamer_test.cpp             | 165 ++++++++++++++++++<br>
>  test/gstreamer/gstreamer_test.h               |  39 +++++<br>
>  test/gstreamer/meson.build                    |   2 +-<br>
>  4 files changed, 229 insertions(+), 122 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..c27e4d36 100644<br>
> --- a/test/gstreamer/gstreamer_single_stream_test.cpp<br>
> +++ b/test/gstreamer/gstreamer_single_stream_test.cpp<br>
> @@ -14,104 +14,48 @@<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>
> +             if (status_ != TestPass)<br>
> +                     return status_;<br>
>  <br>
> -                     return TestFail;<br>
> -             }<br>
> +             g_autoptr(GstElement) convert0__ = gst_element_factory_make("videoconvert", "convert0");<br>
> +             g_autoptr(GstElement) sink0__ = gst_element_factory_make("fakesink", "sink0");<br>
> +             g_object_ref_sink(convert0__);<br>
> +             g_object_ref_sink(sink0__);<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>
> +             if (!convert0__ || !sink0__) {<br>
> +                     g_printerr("Not all elements could be created. %p.%p\n",<br>
> +                                convert0__, sink0__);<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>
>  <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_)<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>
> +             convert0_ = reinterpret_cast<GstElement *>(g_steal_pointer(&convert0__));<br>
> +             sink0_ = reinterpret_cast<GstElement *>(g_steal_pointer(&sink0__));<br>
>  <br>
> +             if (gstreamer_create_pipeline() != TestPass)<br>
>                       return TestFail;<br>
> -             }<br>
>  <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 +63,16 @@ 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>
> +             if (gstreamer_start_pipeline() != TestPass)<br>
>                       return TestFail;<br>
> -             }<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>
> +             if (gstreamer_process_event() != TestPass)<br>
> +                     return TestFail;<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..d9a9618a<br>
> --- /dev/null<br>
> +++ b/test/gstreamer/gstreamer_test.cpp<br>
> @@ -0,0 +1,165 @@<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>
> +     GstRegistry *registry = gst_registry_get();<br>
> +     g_autoptr(GstPlugin) plugin = gst_registry_lookup(registry, "libgstlibcamera.so");<br>
> +     if (plugin) {<br>
> +             gst_registry_remove_plugin(registry, plugin);<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>
> +<br>
> +             status_ = TestFail;<br>
> +             return;<br>
> +     }<br>
> +<br>
> +     /* Create the element */<br>
> +     g_autoptr(GstElement) libcameraSrc__ = gst_element_factory_make("libcamerasrc", "libcamera");<br>
> +     g_object_ref_sink(libcameraSrc__);<br>
<br>
I'm a bit insisting, but the base class should not limit to 1 libcamerasrc. One<br>
of the I fixed recently was to driver two cameras concurrently. I was to write a<br>
test when this settle, but it would be quite some work with current case class.<br>
<br>
I would like to suggest a virtual function, create_pipeline(), which delegate<br>
completely the creation of the pipeline.<br></blockquote></div><div dir="auto"><br></div><div dir="auto">Okay so you mean, libcamera element and pipeline making all in one function create_pipeline, as I have actually defined the function below.</div><div dir="auto"><br></div><div class="gmail_quote" dir="auto"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br><br><br></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
> +<br>
> +     if (!libcameraSrc__) {<br>
> +             g_printerr("Unable to create libcamera element\n");<br>
> +<br>
> +             status_ = TestFail;<br>
> +             return;<br>
> +     }<br>
> +<br>
> +     libcameraSrc_ = reinterpret_cast<GstElement *>(g_steal_pointer(&libcameraSrc__));<br>
> +<br>
> +     status_ = TestPass;<br>
> +}<br>
> +<br>
> +GstreamerTest::~GstreamerTest()<br>
> +{<br>
> +     if (pipeline_)<br>
> +             gst_object_unref(pipeline_);<br>
> +     if (status_ == TestFail) {<br>
> +             gst_object_unref(libcameraSrc_);<br>
> +     }<br>
> +<br>
> +     gst_deinit();<br>
> +}<br>
> +<br>
> +int GstreamerTest::gstreamer_create_pipeline()<br>
> +{<br>
> +     pipeline_ = gst_pipeline_new("test-pipeline");<br>
> +<br>
> +     if (!pipeline_) {<br>
> +             g_printerr("Unable to create pipeline\n");<br>
> +<br>
> +             return TestFail;<br>
> +     }<br>
> +<br>
> +     return TestPass;<br>
> +}:la<br>
> +<br>
> +int 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>
> +             return TestFail;<br>
> +     }<br>
> +<br>
> +     return TestPass;<br>
> +}<br>
> +<br>
> +int GstreamerTest::gstreamer_process_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>
> +     gst_element_set_state(pipeline_, GST_STATE_NULL);<br>
> +<br>
> +     /* Parse error message */<br>
> +     if (msg == NULL) {<br>
> +             return TestPass;<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>
> +     return TestFail;<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..cdffdea9<br>
> --- /dev/null<br>
> +++ b/test/gstreamer/gstreamer_test.h<br>
> @@ -0,0 +1,39 @@<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>
> +     virtual ~GstreamerTest();<br>
> +<br>
> +protected:<br>
> +     virtual int gstreamer_create_pipeline();<br>
> +     int gstreamer_start_pipeline();<br>
> +     int gstreamer_process_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>
> 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></div>