<div dir="ltr"><div dir="ltr">Hi Paul, <br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Aug 10, 2021 at 1:26 PM <<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>
test: gstreamer: Add unit test to test gstreamer single stream working<br>
<br>
On Tue, Aug 10, 2021 at 01:10:04PM +0530, Vedant Paranjape wrote:<br>
> This patch adds unit test to test if single stream using<br>
<br>
s/unit/a/<br>
<br>
I think here it's fine, but since the subject needs to be concice, I<br>
think the second "test" is redundant. "Add test for gstreamer single<br>
stream" maybe is sufficient? Not sure how the others think, maybe this<br>
would be too concise :p<br>
<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>
> ---<br>
>  .../gstreamer_single_stream_test.cpp          | 118 ++++++++++++++++++<br>
>  test/gstreamer/meson.build                    |  15 +++<br>
>  test/meson.build                              |   1 +<br>
>  3 files changed, 134 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..c249a048<br>
> --- /dev/null<br>
> +++ b/test/gstreamer/gstreamer_single_stream_test.cpp<br>
> @@ -0,0 +1,118 @@<br>
> +#include <iostream><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>
> +    GstElement *pipeline, *libcamera_src, *convert0, *sink0;<br>
> +    GstBus *bus;<br>
> +    GstMessage *msg;<br>
> +    GstStateChangeReturn ret;<br>
<br>
<br>
I think these can go in private. Also since they're all member variables<br>
they should have a _ suffix.<br>
<br>
(for the whole file) indentation<br></blockquote><div><br></div><div>Not sure what's wrong with indentation. I have followed how it's there in other tests</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>
> +    int GstreamerPrintError(GstMessage *msg_)<br>
> +    {<br>
> +        int TestResult = TestPass;<br>
> +<br>
> +        if (msg_ != NULL) {<br>
> +            GError *err;<br>
> +            gchar *debug_info;<br>
> +<br>
> +            switch (GST_MESSAGE_TYPE(msg_)) {<br>
> +                case GST_MESSAGE_ERROR:<br>
<br>
>From here,<br>
<br>
> +                    gst_message_parse_error(msg_, &err, &debug_info);<br>
> +                    g_printerr("Error received from element %s: %s\n", GST_OBJECT_NAME(msg_->src), err->message);<br>
> +                    g_printerr("Debugging information: %s\n", debug_info ? debug_info : "none");<br>
> +                    g_clear_error(&err);<br>
> +                    g_free(debug_info);<br>
> +                    TestResult = TestFail;<br>
<br>
to here, I meant this block should be in some print error function.<br>
<br>
> +                    break;<br>
> +                case GST_MESSAGE_EOS:<br>
> +                    g_printerr("End-Of-Stream reached.\n");<br>
<br>
For example, this isn't an error, so this certainly doesn't belong in a<br>
"PrintError" function :D<br>
<br>
> +                    TestResult = TestFail;<br>
> +                    break;<br>
> +                default:<br>
> +                    /* We should not reach here because we only asked for ERRORs and EOS */<br>
> +                    g_printerr("Unexpected message received.\n");<br>
> +                    TestResult = TestFail;<br>
> +                    break;<br>
> +            }<br>
> +        }<br>
> +<br>
> +        return TestResult;<br>
> +    }<br>
> +<br>
> +    int init() override<br>
> +    {<br>
> +        /* Initialize GStreamer */<br>
> +        GError *err_init = nullptr;<br>
> +        if (!gst_init_check(nullptr, nullptr, &err_init)) {<br>
> +            g_printerr ("Could not initialize GStreamer: %s\n", err_init ? err_init->message : "unknown error occurred");<br>
<br>
Break up the line.<br>
<br>
s/occurred//<br>
<br>
<br>
> +            if (err_init) {<br>
> +                g_error_free(err_init);<br>
> +            }<br>
<br>
Don't need the braces.<br>
<br>
> +<br>
> +            return TestFail;<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("autovideosink", "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>
> +            return TestFail;<br>
> +        }<br>
> +<br>
> +        return TestPass;<br>
> +    }<br>
> +<br>
> +    void cleanup() override<br>
> +    {<br>
> +        gst_message_unref(msg);<br>
> +        gst_object_unref(bus);<br>
> +        gst_element_set_state(pipeline, GST_STATE_NULL);<br>
> +        gst_object_unref(pipeline);<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 (1).\n");<br>
<br>
What's this (1) for?<br>
<br>
> +            gst_object_unref(pipeline);<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>
> +            gst_object_unref(pipeline);<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>
> +                                    GstMessageType((uint)GST_MESSAGE_ERROR | (uint)GST_MESSAGE_EOS));<br>
> +<br>
> +        /* Parse error message */<br>
> +        if (GstreamerPrintError(msg) == TestFail) {<br>
> +            return TestFail;<br>
> +        }<br>
<br>
This should be replaced with the switch-case.<br>
<br>
<br>
Otherwise, looks good.<br>
<br>
<br>
Paul<br>
<br>
> +<br>
> +        return TestPass;<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..36643d4a<br>
> --- /dev/null<br>
> +++ b/test/gstreamer/meson.build<br>
> @@ -0,0 +1,15 @@<br>
> +# SPDX-License-Identifier: CC0-1.0<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>
> 2.25.1<br>
>  </blockquote><div> </div><div>Thanks for the review, will do the requested changes.</div><div><br></div><div>Regards,</div><div>Vedant<br></div></div></div>