libcamera/test/gstreamer/gstreamer_device_provider_test.cpp
Laurent Pinchart 03526e58d1 tests: gstreamer: Fix compiler error with gcc 8.4.0
The provider g_autoptr variable introduced by commit adb1bbb748
("tests: gstreamer: Test cameras' enumeration from GstDeviceProvider")
is left uninitialized when declared. The cleanup function could thus get
called on an unitialized variable if the scope was exited before the
variable gets initialized. This can't occur here, but gcc 8.4.0 still
complains about it:

/usr/include/glib-2.0/glib/gmacros.h: In member function ‘virtual int GstreamerDeviceProviderTest::run()’:
/usr/include/glib-2.0/glib/gmacros.h:1049:27: error: ‘provider’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
     { if (_ptr) (cleanup) ((ParentName *) _ptr); }                                                              \
                           ^
../test/gstreamer/gstreamer_device_provider_test.cpp:37:32: note: ‘provider’ was declared here
   g_autoptr(GstDeviceProvider) provider;

Silence the error by initializing the variable to NULL at declaration
time. This is a good practice in any case, as later refactoring could
otherwise introduce a scope exit before initialization.

Fixes: adb1bbb748 ("tests: gstreamer: Test cameras' enumeration from GstDeviceProvider")
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Umang Jain <umang.jain@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
2023-07-11 15:58:06 +01:00

77 lines
1.6 KiB
C++

/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright (C) 2023, Umang Jain <umang.jain@ideasonboard.com>
*
* gstreamer_single_stream_test.cpp - GStreamer single stream capture test
*/
#include <vector>
#include <libcamera/libcamera.h>
#include <gst/gst.h>
#include "gstreamer_test.h"
#include "test.h"
using namespace std;
class GstreamerDeviceProviderTest : public GstreamerTest, public Test
{
public:
GstreamerDeviceProviderTest()
: GstreamerTest()
{
}
protected:
int init() override
{
if (status_ != TestPass)
return status_;
return TestPass;
}
int run() override
{
g_autoptr(GstDeviceProvider) provider = NULL;
GList *devices, *l;
std::vector<std::string> cameraNames;
std::unique_ptr<libcamera::CameraManager> cm;
cm = std::make_unique<libcamera::CameraManager>();
cm->start();
for (auto &camera : cm->cameras())
cameraNames.push_back(camera->id());
cm->stop();
cm.reset();
provider = gst_device_provider_factory_get_by_name("libcameraprovider");
devices = gst_device_provider_get_devices(provider);
for (l = devices; l != NULL; l = g_list_next(l)) {
GstDevice *device = GST_DEVICE(l->data);
g_autofree gchar *gst_name;
bool matched = false;
g_autoptr(GstElement) element = gst_device_create_element(device, NULL);
g_object_get(element, "camera-name", &gst_name, NULL);
for (auto name : cameraNames) {
if (strcmp(name.c_str(), gst_name) == 0) {
matched = true;
break;
}
}
if (!matched)
return TestFail;
}
g_list_free_full(devices, (GDestroyNotify)gst_object_unref);
return TestPass;
}
};
TEST_REGISTER(GstreamerDeviceProviderTest)