<div dir="ltr"><div>Hi Laurent,</div><div>I tried doing this, it threw a compiler error: <a href="https://paste.debian.net/1207499/">https://paste.debian.net/1207499/</a></div><div>I assume it must have something to do with vars being defined in a class. And g_autoptr uses the cleanup attribute.</div><div><br></div><div>Regards,</div><div>Vedant<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Aug 13, 2021 at 5:26 PM Laurent Pinchart <<a href="mailto:laurent.pinchart@ideasonboard.com">laurent.pinchart@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 Fri, Aug 13, 2021 at 11:44:19AM +0530, Vedant Paranjape wrote:<br>
> On Fri, Aug 13, 2021 at 5:55 AM Laurent Pinchart wrote:<br>
> > On Thu, Aug 12, 2021 at 04:10:30PM +0530, Vedant Paranjape wrote:<br>
> > > This patch adds a test to test if single stream using<br>
> > > libcamera's gstreamer element works.<br>
> > ><br>
> > > Signed-off-by: Vedant Paranjape <<a href="mailto:vedantparanjape160201@gmail.com" target="_blank">vedantparanjape160201@gmail.com</a>><br>
> > > Reviewed-by: Paul Elder <<a href="mailto:paul.elder@ideasonboard.com" target="_blank">paul.elder@ideasonboard.com</a>><br>
> > > ---<br>
> > >  .../gstreamer_single_stream_test.cpp          | 134 ++++++++++++++++++<br>
> > >  test/gstreamer/meson.build                    |  19 +++<br>
> > >  test/meson.build                              |   1 +<br>
> > >  3 files changed, 154 insertions(+)<br>
> > >  create mode 100644 test/gstreamer/gstreamer_single_stream_test.cpp<br>
> > >  create mode 100644 test/gstreamer/meson.build<br>
> > ><br>
> > > diff --git a/test/gstreamer/gstreamer_single_stream_test.cpp b/test/gstreamer/gstreamer_single_stream_test.cpp<br>
> > > new file mode 100644<br>
> > > index 00000000..040b1e7e<br>
> > > --- /dev/null<br>
> > > +++ b/test/gstreamer/gstreamer_single_stream_test.cpp<br>
> > > @@ -0,0 +1,134 @@<br>
> > > +/* SPDX-License-Identifier: GPL-2.0-or-later */<br>
> > > +/*<br>
> > > + * Copyright (C) 2021, Vedant Paranjape<br>
> > > + *<br>
> > > + * ipa_interface_test.cpp - Test the IPA interface<br>
> > > + */<br>
> > > +<br>
> > > +#include <iostream><br>
> > > +<br>
> > > +#include <libcamera/base/utils.h><br>
> > > +<br>
> > > +#include <gst/gst.h><br>
> > > +<br>
> > > +#include "test.h"<br>
> > > +<br>
> > > +using namespace std;<br>
> > > +<br>
> > > +class GstreamerSingleStreamTest : public Test<br>
> > > +{<br>
> > > +protected:<br>
> > > +     int init() override<br>
> > > +     {<br>
> > > +             /* Initialize GStreamer */<br>
> > > +             GError *err_init = nullptr;<br>
> ><br>
> > Let's use the libcamera coding style, this should be errInit. Same for<br>
> > libcamera_src_.<br>
> ><br>
> > > +             if (!gst_init_check(nullptr, nullptr, &err_init)) {<br>
> > > +                     g_printerr("Could not initialize GStreamer: %s\n",<br>
> > > +                                err_init ? err_init->message : "unknown error");<br>
> > > +                     if (err_init)<br>
> > > +                             g_error_free(err_init);<br>
> > > +<br>
> > > +                     return TestFail;<br>
> > > +             }<br>
> > > +<br>
> > > +             if (!gst_registry_scan_path(gst_registry_get(),<br>
> > > +<br>
> >  std::string(libcamera::utils::libcameraBuildPath() +<br>
> > > +                                                     "src/gstreamer")<br>
> > > +                                                 .c_str())) {<br>
> ><br>
> > Let's use a local variable to make this a bit more readable.<br>
> ><br>
> >                 std::string path = libcamera::utils::libcameraBuildPath()<br>
> >                                  + "src/gstreamer";<br>
> >                 if (!gst_registry_scan_path(gst_registry_get(), path.c_str()) {<br>
> ><br>
> > > +                     g_printerr("Failed to add plugin to registry\n");<br>
> ><br>
> > You need to call gst_deinit() here.<br>
> ><br>
> > > +                     return TestFail;<br>
> > > +             }<br>
> ><br>
> > This won't work if the libcamerasrc plugin is already installed on the<br>
> > system, gst_registry_scan_path() will not add anything in that case. You<br>
> > need to remove the plugin first. I've tested the following code:<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>
> ><br>
> > > +<br>
> > > +             /* Create the elements */<br>
> > > +             libcamera_src_ = gst_element_factory_make("libcamerasrc", "libcamera");<br>
> > > +             convert0_ = gst_element_factory_make("videoconvert", "convert0");<br>
> > > +             sink0_ = gst_element_factory_make("fakevideosink", "sink0");<br>
> > > +<br>
> > > +             /* Create the empty pipeline_ */<br>
> > > +             pipeline_ = gst_pipeline_new("test-pipeline");<br>
> > > +<br>
> > > +             if (!pipeline_ || !convert0_ || !sink0_ || !libcamera_src_) {<br>
> > > +                     g_printerr("Not all elements could be created.\n");<br>
> ><br>
> > cleanup() isn't called if init() fails, so you need to unref pipeline_,<br>
> > but also libcamera_src_, convert0_ and sink0_ here. The last three<br>
> > elements don't need to be unrefered in cleanup(), because<br>
> > gst_bin_add_many() makes the pipeline take ownership of them.<br>
> <br>
> I could make all those pointers g_autoptr, right ?<br>
<br>
I think so, at least if they can be helpful, I'm not a glib specialist<br>
:-)<br>
<br>
> > gst_deinit() is also needed here.<br>
> ><br>
> > > +                     return TestFail;<br>
> > > +             }<br>
> > > +<br>
> > > +             return TestPass;<br>
> > > +     }<br>
> > > +<br>
> > > +     void cleanup() override<br>
> > > +     {<br>
> > > +             gst_object_unref(bus_);<br>
> > > +             gst_element_set_state(pipeline_, GST_STATE_NULL);<br>
> ><br>
> > I'd move this call to the run() function, just before the<br>
> > /* Parse error message */ comment, as it shouldn't be executed if the<br>
> > pipeline isn't playing.<br>
> ><br>
> > > +             gst_object_unref(pipeline_);<br>
> > > +             gst_deinit();<br>
> > > +     }<br>
> > > +<br>
> > > +     int run() override<br>
> > > +     {<br>
> > > +             /* Build the pipeline */<br>
> > > +             gst_bin_add_many(GST_BIN(pipeline_), libcamera_src_, convert0_, sink0_, NULL);<br>
> > > +             if (gst_element_link_many(libcamera_src_, convert0_, sink0_, NULL) != TRUE) {<br>
> > > +                     g_printerr("Elements could not be linked.\n");<br>
> > > +                     gst_object_unref(pipeline_);<br>
> ><br>
> > This will cause a double unref, as cleanup() will be called.<br>
> ><br>
> > > +                     return TestFail;<br>
> > > +             }<br>
> > > +<br>
> > > +             /* Start playing */<br>
> > > +             ret_ = gst_element_set_state(pipeline_, GST_STATE_PLAYING);<br>
> ><br>
> > This can be a local variable.<br>
> ><br>
> > > +             if (ret_ == GST_STATE_CHANGE_FAILURE) {<br>
> > > +                     g_printerr("Unable to set the pipeline to the playing state.\n");<br>
> > > +                     gst_object_unref(pipeline_);<br>
> ><br>
> > Same here, double unref.<br>
> ><br>
> > > +                     return TestFail;<br>
> > > +             }<br>
> > > +<br>
> > > +             /* Wait until error or EOS or timeout after 2 seconds */<br>
> > > +             GstClockTime timeout = 2000000000;<br>
> > > +             bus_ = gst_element_get_bus(pipeline_);<br>
> > > +             msg_ = gst_bus_timed_pop_filtered(bus_, timeout,<br>
> ><br>
> > Both bus and msg can be local variables. Otherwise, for bus_, you'd have<br>
> > to initialize it to nullptr, or cleanup() will crash if one of the above<br>
> > two conditions is false.<br>
> ><br>
> > > +<br>
> >  GstMessageType((uint)GST_MESSAGE_ERROR | (uint)GST_MESSAGE_EOS));<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>
> > > +                     return TestFail;<br>
> > > +                     break;<br>
> > > +             case GST_MESSAGE_EOS:<br>
> > > +                     g_print("End-Of-Stream reached.\n");<br>
> > > +                     return TestFail;<br>
> > > +                     break;<br>
> > > +             default:<br>
> > > +                     g_printerr("Unexpected message received.\n");<br>
> > > +                     return TestFail;<br>
> > > +                     break;<br>
> > > +             }<br>
> ><br>
> > A blank line would be nice here.<br>
> ><br>
> > > +             gst_message_unref(msg_);<br>
> ><br>
> > This will never be called, as all the cases above return. Furthermore,<br>
> > msg_ is leaked in all those cases. I'd remove the return statements, and<br>
> > turn the next line into<br>
> ><br>
> >                 return TestFail;<br>
> ><br>
> > > +<br>
> > > +             return TestPass;<br>
> > > +     }<br>
> > > +<br>
> > > +private:<br>
> > > +     GstElement *pipeline_, *libcamera_src_, *convert0_, *sink0_;<br>
> ><br>
> > One variable per line please:<br>
> ><br>
> >         GstElement *pipeline_;<br>
> >         GstElement *libcamera_src_;<br>
> >         GstElement *convert0_;<br>
> >         GstElement *sink0_;<br>
> ><br>
> > > +     GstBus *bus_;<br>
> > > +     GstMessage *msg_;<br>
> > > +     GstStateChangeReturn ret_;<br>
> > > +<br>
> > > +     void gstreamer_print_error(GstMessage *msg)<br>
> > > +     {<br>
> > > +             GError *err;<br>
> > > +             gchar *debug_info;<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>
> > > +             g_clear_error(&err);<br>
> > > +             g_free(debug_info);<br>
> > > +     }<br>
> ><br>
> > We usually put functions before variables.<br>
> ><br>
> > > +};<br>
> > > +<br>
> > > +TEST_REGISTER(GstreamerSingleStreamTest)<br>
> > > diff --git a/test/gstreamer/meson.build b/test/gstreamer/meson.build<br>
> > > new file mode 100644<br>
> > > index 00000000..b99aa0da<br>
> > > --- /dev/null<br>
> > > +++ b/test/gstreamer/meson.build<br>
> > > @@ -0,0 +1,19 @@<br>
> > > +# SPDX-License-Identifier: CC0-1.0<br>
> > > +<br>
> > > +if not gst_enabled<br>
> > > +    subdir_done()<br>
> > > +endif<br>
> > > +<br>
> > > +gstreamer_tests = [<br>
> > > +    ['single_stream_test',   'gstreamer_single_stream_test.cpp'],<br>
> > > +]<br>
> > > +gstreamer_dep = dependency('gstreamer-1.0', required: true)<br>
> > > +<br>
> > > +foreach t : gstreamer_tests<br>
> > > +    exe = executable(t[0], t[1],<br>
> > > +                     dependencies : [libcamera_private, gstreamer_dep],<br>
> > > +                     link_with : test_libraries,<br>
> > > +                     include_directories : test_includes_internal)<br>
> > > +<br>
> > > +    test(t[0], exe, suite : 'gstreamer', is_parallel : false)<br>
> > > +endforeach<br>
> > > diff --git a/test/meson.build b/test/meson.build<br>
> > > index 3bceb5df..d0466f17 100644<br>
> > > --- a/test/meson.build<br>
> > > +++ b/test/meson.build<br>
> > > @@ -11,6 +11,7 @@ subdir('libtest')<br>
> > ><br>
> > >  subdir('camera')<br>
> > >  subdir('controls')<br>
> > > +subdir('gstreamer')<br>
> > >  subdir('ipa')<br>
> > >  subdir('ipc')<br>
> > >  subdir('log')<br>
<br>
-- <br>
Regards,<br>
<br>
Laurent Pinchart<br>
</blockquote></div>