/src/android/

1' href='#n1'>1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2021, Vedant Paranjape
 *
 * libcamera Gstreamer element API tests
 */

#include <libcamera/libcamera.h>

#include <libcamera/base/utils.h>

#if HAVE_ASAN
#include <sanitizer/asan_interface.h>
#endif

#include "gstreamer_test.h"

#include "test.h"

using namespace std;

#if HAVE_ASAN
extern "C" {
const char *__asan_default_options()
{
	/*
	 * Disable leak detection due to a known global variable initialization
	 * leak in glib's g_quark_init(). This should ideally be handled by
	 * using a suppression file instead of disabling leak detection.
	 */
	return "detect_leaks=false";
}
}
#endif

GstreamerTest::GstreamerTest(unsigned int numStreams)
	: pipeline_(nullptr), libcameraSrc_(nullptr)
{
	/*
	 * GStreamer by default spawns a process to run the gst-plugin-scanner
	 * helper. If libcamera is compiled with ASan enabled, and as GStreamer
	 * is most likely not, this causes the ASan link order check to fail
	 * when gst-plugin-scanner dlopen()s the plugin as many libraries will
	 * have already been loaded by then. Fix this issue by disabling
	 * spawning of a child helper process when scanning the build directory
	 * for plugins.
	 */
	gst_registry_fork_set_enabled(false);

	/* Initialize GStreamer */
	g_autoptr(GError) errInit = NULL;
	if (!gst_init_check(nullptr, nullptr, &errInit)) {
		g_printerr("Could not initialize GStreamer: %s\n",
			   errInit ? errInit->message : "unknown error");

		status_ = TestFail;
		return;
	}

	/*
	 * Atleast one camera should be available with numStreams streams,
	 * otherwise skip the test entirely.
	 */
	if (!checkMinCameraStreamsAndSetCameraName(numStreams)) {
		status_ = TestSkip;
		return;
	}

	status_ = TestPass;
}

bool GstreamerTest::checkMinCameraStreamsAndSetCameraName(unsigned int numStreams)
{
	libcamera::CameraManager cm;
	bool cameraFound = false;

	cm.start();

	for (auto &camera : cm.cameras()) {
		if (camera->streams().size() < numStreams)
			continue;

		cameraFound = true;
		cameraName_ = camera->id();
		break;
	}

	cm.stop();

	return cameraFound;
}

GstreamerTest::~GstreamerTest()
{
	g_clear_object(&pipeline_);
	g_clear_object(&libcameraSrc_);

	gst_deinit();
}

int GstreamerTest::createPipeline()
{
	libcameraSrc_ = gst_element_factory_make("libcamerasrc", "libcamera");
	pipeline_ = gst_pipeline_new("test-pipeline");

	if (!libcameraSrc_ || !pipeline_) {
		g_printerr("Unable to create pipeline %p.%p\n",
			   libcameraSrc_, pipeline_);

		return TestFail;
	}

	g_object_set(libcameraSrc_, "camera-name", cameraName_.c_str(), NULL);