<div dir="ltr"><div dir="ltr"><div>Hi Paul,</div><div>Thanks for the review.</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Sep 8, 2021 at 9:30 AM <<a href="mailto:paul.elder@ideasonboard.com">paul.elder@ideasonboard.com</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">Hi Vedant,<br>
<br>
On Wed, Sep 01, 2021 at 01:32:17AM +0530, Vedant Paranjape wrote:<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 | 145 +++-------------<br>
> test/gstreamer/gstreamer_test.cpp | 157 ++++++++++++++++++<br>
> test/gstreamer/gstreamer_test.h | 39 +++++<br>
> test/gstreamer/meson.build | 2 +-<br>
> 4 files changed, 221 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>
<br>
For local variables, you just remove the ending underscore altogether<br>
(also the other instances where you do so).<br></blockquote><div><br></div><div>Okay, done !</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">
> + 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..22128c4c<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>
> + 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>
You don't need braces here.<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>
> + 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>
You don't need braces here.<br>
<br>
How come you unref libcameraSrc_ but not the others upon failure?<br>
What's the difference between libcameraSrc_ and the other pipeline<br>
components? Is TestFail both sufficient and necessary to need to free<br>
libcameraSrc_?<br></blockquote><div><br></div><div>This is because the other elements are added to the pipeline, once that is done successfully the pipeline owns them and takes care of their existence.</div><div>So, there's going to be a minor change here though. But, in this case, since create pipeline is called before the elements are added to the pipeline, I need</div><div>to take care of libcameraSrc.</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_deinit();<br>
> +}<br>
> +<br>
> +int GstreamerTest::gstreamer_create_pipeline()<br>
<br>
All of these functions should be camelCase. Also since they're all<br>
members of GstreamerTest, I think you can drop the gstreamer prefix too.<br></blockquote><div><br></div><div>Okay</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>
> + g_autoptr(GstElement) libcameraSrc__ = gst_element_factory_make("libcamerasrc", "libcamera");<br>
> + pipeline_ = gst_pipeline_new("test-pipeline");<br>
> + g_object_ref_sink(libcameraSrc__);<br>
<br>
You can just drop the underscores completely.<br>
<br>
<br>
Hm other than those, I think it's ready.<br>
<br>
<br>
Paul<br>
<br>
> +<br>
> + if (!libcameraSrc__ || !pipeline_) {<br>
> + g_printerr("Unable to create create pipeline %p.%p\n",<br>
> + libcameraSrc__, pipeline_);<br>
> +<br>
> + return TestFail;<br>
> + }<br>
> +<br>
> + libcameraSrc_ = reinterpret_cast<GstElement *>(g_steal_pointer(&libcameraSrc__));<br>
> +<br>
> + return TestPass;<br>
> +}<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>
> 2.25.1<br>
> <br></blockquote><div><br></div><div>Regards,</div><div><i><b>Vedant Paranjape</b></i><br></div></div></div>