summaryrefslogtreecommitdiff
path: root/src/cam/capture_script.cpp
blob: 9f22d5f77d3b2c9c787c39693e44d023db4399d3 (plain)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2022, Ideas on Board Oy
 *
 * capture_script.cpp - Capture session configuration script
 */

#include "capture_script.h"

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace libcamera;

CaptureScript::CaptureScript(std::shared_ptr<Camera> camera,
			     const std::string &fileName)
	: camera_(camera), valid_(false)
{
	FILE *fh = fopen(fileName.c_str(), "r");
	if (!fh) {
		int ret = -errno;
		std::cerr << "Failed to open capture script " << fileName
			  << ": " << strerror(-ret) << std::endl;
		return;
	}

	/*
	 * Map the camera's controls to their name so that they can be
	 * easily identified when parsing the script file.
	 */
	for (const auto &[control, info] : camera_->controls())
		controls_[control->name()] = control;

	int ret = parseScript(fh);
	fclose(fh);
	if (ret)
		return;

	valid_ = true;
}

/* Retrieve the control list associated with a frame number. */
const ControlList &CaptureScript::frameControls(unsigned int frame)
{
	static ControlList controls{};

	auto it = frameControls_.find(frame);
	if (it == frameControls_.end())
		return controls;

	return it->second;
}

CaptureScript::EventPtr CaptureScript::nextEvent(yaml_event_type_t expectedType)
{
	EventPtr event(new yaml_event_t);

	if (!yaml_parser_parse(&parser_, event.get()))
		return nullptr;

	if (expectedType != YAML_NO_EVENT && !checkEvent(event, expectedType))
		return nullptr;

	return event;
}

bool CaptureScript::checkEvent(const EventPtr &event, yaml_event_type_t expectedType) const
{
	if (event->type != expectedType) {
		std::cerr << "Capture script error on line " << event->start_mark.line
			  << " column " << event->start_mark.column << ": "
			  << "Expected " << eventTypeName(expectedType)
			  << " event, got " << eventTypeName(event->type)
			  << std::endl;
		return false;
	}

	return true;
}

std::string CaptureScript::eventScalarValue(const EventPtr &event)
{
	return std::string(reinterpret_cast<char *>(event->data.scalar.value),
			   event->data.scalar.length);
}

std::string CaptureScript::eventTypeName(yaml_event_type_t type)
{
	static const std::map<yaml_event_type_t, std::string> typeNames = {
		{ YAML_STREAM_START_EVENT, "stream-start" },
		{ YAML_STREAM_END_EVENT, "stream-end" },
		{ YAML_DOCUMENT_START_EVENT, "document-start" },
		{ YAML_DOCUMENT_END_EVENT, "document-end" },
		{ YAML_ALIAS_EVENT, "alias" },
		{ YAML_SCALAR_EVENT, "scalar" },
		{ YAML_SEQUENCE_START_EVENT, "sequence-start" },
		{ YAML_SEQUENCE_END_EVENT, "sequence-end" },
		{ YAML_MAPPING_START_EVENT, "mapping-start" },
		{ YAML_MAPPING_END_EVENT, "mapping-end" },
	};

	auto it = typeNames.find(type);
	if (it == typeNames.end())
		return "[type " + std::to_string(type) + "]";

	return it->second;
}

int CaptureScript::parseScript(FILE *script)
{
	int ret = yaml_parser_initialize(&parser_);
	if (!ret) {
		std::cerr << "Failed to initialize yaml parser" << std::endl;
		return ret;
	}

	/* Delete the parser upon function exit. */
	struct ParserDeleter {
		ParserDeleter(yaml_parser_t *parser) : parser_(parser) { }
		~ParserDeleter() { yaml_parser_delete(parser_); }
		yaml_parser_t *parser_;
	} deleter(&parser_);

	yaml_parser_set_input_file(&parser_, script);

	EventPtr event = nextEvent(YAML_STREAM_START_EVENT);
	if (!event)
		return -EINVAL;

	event = nextEvent(YAML_DOCUMENT_START_EVENT);
	if (!event)
		return -EINVAL;

	event = nextEvent(YAML_MAPPING_START_EVENT);
	if (!event)
		return -EINVAL;

	while (1) {
		event = nextEvent();
		if (!event)
			return -EINVAL;

		if (event->type == YAML_MAPPING_END_EVENT)
			return 0;

		if (!checkEvent(event, YAML_SCALAR_EVENT))
			return -EINVAL;

		std::string section = eventScalarValue(event);

		if (section == "frames") {
			parseFrames();
		} else {
			std::cerr << "Unsupported section '" << section << "'"
				  << std::endl;
			return -EINVAL;
		}
	}
}

int CaptureScript::parseFrames()
{
	EventPtr event = nextEvent(YAML_SEQUENCE_START_EVENT);
	if (!event)
		return -EINVAL;

	while (1) {
		event = nextEvent();
		if (!event)
			return -EINVAL;

		if (event->type == YAML_SEQUENCE_END_EVENT)
			return 0;

		int ret = parseFrame(std::move(event));
		if (ret)
			return ret;
	}
}

int CaptureScript::parseFrame(EventPtr event)
{
	if (!checkEvent(event, YAML_MAPPING_START_EVENT))
		return -EINVAL;

	std::string key = parseScalar();
	if (key.empty())
		return -EINVAL;

	unsigned int frameId = atoi(key.c_str());

	event = nextEvent(YAML_MAPPING_START_EVENT);
	if (!event)
		return -EINVAL;

	ControlList controls{};

	while (1) {
		event = nextEvent();
		if (!event)
			return -EINVAL;

		if (event->type == YAML_MAPPING_END_EVENT)
			break;

		int ret = parseControl(std::move(event), controls);
		if (ret)
			return ret;
	}

	frameControls_[frameId] = std::move(controls);

	event = nextEvent(YAML_MAPPING_END_EVENT);
	if (!event)
		return -EINVAL;

	return 0;
}

int CaptureScript::parseControl(EventPtr event, ControlList &controls)
{
	/* We expect a value after a key. */
	std::string name = eventScalarValue(event);
	if (name.empty())
		return -EINVAL;

	/* If the camera does not support the control just ignore it. */
	auto it = controls_.find(name);
	if (it == controls_.end()) {
		std::cerr << "Unsupported control '" << name << "'" << std::endl;
		return -EINVAL;
	}

	std::string value = parseScalar();
	if (value.empty())
		return -EINVAL;

	const ControlId *controlId = it->second;
	ControlValue val = unpackControl(controlId, value);
	controls.set(controlId->id(), val);

	return 0;
}

std::string CaptureScript::parseScalar()
{
	EventPtr event = nextEvent(YAML_SCALAR_EVENT);
	if (!event)
		return "";

	return eventScalarValue(event);
}

void CaptureScript::unpackFailure(const ControlId *id, const std::string &repr)
{
	static const std::map<unsigned int, const char *> typeNames = {
		{ ControlTypeNone, "none" },
		{ ControlTypeBool, "bool" },
		{ ControlTypeByte, "byte" },
		{ ControlTypeInteger32, "int32" },
		{ ControlTypeInteger64, "int64" },
		{ ControlTypeFloat, "float" },
		{ ControlTypeString, "string" },
		{ ControlTypeRectangle, "Rectangle" },
		{ ControlTypeSize, "Size" },
	};

	const char *typeName;
	auto it = typeNames.find(id->type());
	if (it != typeNames.end())
		typeName = it->second;
	else
		typeName = "unknown";

	std::cerr << "Unsupported control '" << repr << "' for "
		  << typeName << " control " << id->name() << std::endl;
}

ControlValue CaptureScript::unpackControl(const ControlId *id,
					  const std::string &repr)
{
	ControlValue value{};

	switch (id->type()) {
	case ControlTypeNone:
		break;
	case ControlTypeBool: {
		bool val;

		if (repr == "true") {
			val = true;
		} else if (repr == "false") {
			val = false;
		} else {
			unpackFailure(id, repr);
			return value;
		}

		value.set<bool>(val);
		break;
	}
	case ControlTypeByte: {
		uint8_t val = strtol(repr.c_str(), NULL, 10);
		value.set<uint8_t>(val);
		break;
	}
	case ControlTypeInteger32: {
		int32_t val = strtol(repr.c_str(), NULL, 10);
		value.set<int32_t>(val);
		break;
	}
	case ControlTypeInteger64: {
		int64_t val = strtoll(repr.c_str(), NULL, 10);
		value.set<int64_t>(val);
		break;
	}
	case ControlTypeFloat: {
		float val = strtof(repr.c_str(), NULL);
		value.set<float>(val);
		break;
	}
	case ControlTypeString: {
		value.set<std::string>(repr);
		break;
	}
	case ControlTypeRectangle:
		/* \todo Parse rectangles. */
		break;
	case ControlTypeSize:
		/* \todo Parse Sizes. */
		break;
	}

	return value;
}
ndler::exportFrameBuffers() * \brief Allocate and export buffers for \a stream * \param[in] camera The camera * \param[in] stream The stream to allocate buffers for * \param[out] buffers Array of buffers successfully allocated * * This method allocates buffers for the \a stream from the devices associated * with the stream in the corresponding pipeline handler. Those buffers shall be * suitable to be added to a Request for the stream, and shall be mappable to * the CPU through their associated dmabufs with mmap(). * * The method may only be called after the Camera has been configured and before * it gets started, or after it gets stopped. It shall be called only for * streams that are part of the active camera configuration. * * The only intended caller is Camera::exportFrameBuffers(). * * \context This function is called from the CameraManager thread. * * \return The number of allocated buffers on success or a negative error code * otherwise */ /** * \fn PipelineHandler::start() * \brief Start capturing from a group of streams * \param[in] camera The camera to start * * Start the group of streams that have been configured for capture by * \a configure(). The intended caller of this method is the Camera class which * will in turn be called from the application to indicate that it has * configured the streams and is ready to capture. * * \context This function is called from the CameraManager thread. * * \return 0 on success or a negative error code otherwise */ /** * \fn PipelineHandler::stop() * \brief Stop capturing from all running streams * \param[in] camera The camera to stop * * This method stops capturing and processing requests immediately. All pending * requests are cancelled and complete immediately in an error state. * * \context This function is called from the CameraManager thread. */ /** * \fn PipelineHandler::queueRequest() * \brief Queue a request to the camera * \param[in] camera The camera to queue the request to * \param[in] request The request to queue * * This method queues a capture request to the pipeline handler for processing. * The request is first added to the internal list of queued requests, and * then passed to the pipeline handler with a call to queueRequestDevice(). * * Keeping track of queued requests ensures automatic completion of all requests * when the pipeline handler is stopped with stop(). Request completion shall be * signalled by the pipeline handler using the completeRequest() method. * * \context This function is called from the CameraManager thread. * * \return 0 on success or a negative error code otherwise */ int PipelineHandler::queueRequest(Camera *camera, Request *request) { CameraData *data = cameraData(camera); data->queuedRequests_.push_back(request); int ret = queueRequestDevice(camera, request); if (ret) data->queuedRequests_.remove(request); return ret; } /** * \fn PipelineHandler::queueRequestDevice() * \brief Queue a request to the device * \param[in] camera The camera to queue the request to * \param[in] request The request to queue * * This method queues a capture request to the device for processing. The * request contains a set of buffers associated with streams and a set of * parameters. The pipeline handler shall program the device to ensure that the * parameters will be applied to the frames captured in the buffers provided in * the request. * * \context This function is called from the CameraManager thread. * * \return 0 on success or a negative error code otherwise */ /** * \brief Complete a buffer for a request * \param[in] camera The camera the request belongs to * \param[in] request The request the buffer belongs to * \param[in] buffer The buffer that has completed * * This method shall be called by pipeline handlers to signal completion of the * \a buffer part of the \a request. It notifies applications of buffer * completion and updates the request's internal buffer tracking. The request * is not completed automatically when the last buffer completes to give * pipeline handlers a chance to perform any operation that may still be * needed. They shall complete requests explicitly with completeRequest(). * * \context This function shall be called from the CameraManager thread. * * \return True if all buffers contained in the request have completed, false * otherwise */ bool PipelineHandler::completeBuffer(Camera *camera, Request *request, FrameBuffer *buffer) { camera->bufferCompleted.emit(request, buffer); return request->completeBuffer(buffer); } /** * \brief Signal request completion * \param[in] camera The camera that the request belongs to * \param[in] request The request that has completed * * The pipeline handler shall call this method to notify the \a camera that the * request has completed. The request is deleted and shall not be accessed once * this method returns. * * This method ensures that requests will be returned to the application in * submission order, the pipeline handler may call it on any complete request * without any ordering constraint. * * \context This function shall be called from the CameraManager thread. */ void PipelineHandler::completeRequest(Camera *camera, Request *request) { request->complete(); CameraData *data = cameraData(camera); while (!data->queuedRequests_.empty()) { Request *req = data->queuedRequests_.front(); if (req->status() == Request::RequestPending) break; ASSERT(!req->hasPendingBuffers()); data->queuedRequests_.pop_front(); camera->requestComplete(req); } } /** * \brief Register a camera to the camera manager and pipeline handler * \param[in] camera The camera to be added * \param[in] data Pipeline-specific data for the camera * \param[in] devnum Device number of the camera (optional) * * This method is called by pipeline handlers to register the cameras they * handle with the camera manager. It associates the pipeline-specific \a data * with the camera, for later retrieval with cameraData(). Ownership of \a data * is transferred to the PipelineHandler. * * \a devnum is the device number (as returned by makedev) that the \a camera * is to be associated with. This is for the V4L2 compatibility layer to map * device nodes to Camera instances based on the device number * registered by this method in \a devnum. * * \context This function shall be called from the CameraManager thread. */ void PipelineHandler::registerCamera(std::shared_ptr<Camera> camera, std::unique_ptr<CameraData> data, dev_t devnum) { data->camera_ = camera.get(); cameraData_[camera.get()] = std::move(data); cameras_.push_back(camera); manager_->addCamera(std::move(camera), devnum); } /** * \brief Enable hotplug handling for a media device * \param[in] media The media device * * This function enables hotplug handling, and especially hot-unplug handling, * of the \a media device. It shall be called by pipeline handlers for all the * media devices that can be disconnected. * * When a media device passed to this function is later unplugged, the pipeline * handler gets notified and automatically disconnects all the cameras it has * registered without requiring any manual intervention. */ void PipelineHandler::hotplugMediaDevice(MediaDevice *media) { media->disconnected.connect(this, &PipelineHandler::mediaDeviceDisconnected); } /** * \brief Slot for the MediaDevice disconnected signal */ void PipelineHandler::mediaDeviceDisconnected(MediaDevice *media) { media->disconnected.disconnect(this); if (cameras_.empty()) return; disconnect(); } /** * \brief Device disconnection handler * * This virtual function is called to notify the pipeline handler that the * device it handles has been disconnected. It notifies all cameras created by * the pipeline handler that they have been disconnected, and unregisters them * from the camera manager. * * The function can be overloaded by pipeline handlers to perform custom * operations at disconnection time. Any overloaded version shall call the * PipelineHandler::disconnect() base function for proper hot-unplug operation. */ void PipelineHandler::disconnect() { for (std::weak_ptr<Camera> ptr : cameras_) { std::shared_ptr<Camera> camera = ptr.lock(); if (!camera) continue; camera->disconnect(); manager_->removeCamera(camera.get()); } cameras_.clear(); } /** * \brief Retrieve the pipeline-specific data associated with a Camera * \param[in] camera The camera whose data to retrieve * \return A pointer to the pipeline-specific data passed to registerCamera(). * The returned pointer is a borrowed reference and is guaranteed to remain * valid until the pipeline handler is destroyed. It shall not be deleted * manually by the caller. */ CameraData *PipelineHandler::cameraData(const Camera *camera) { ASSERT(cameraData_.count(camera)); return cameraData_[camera].get(); } /** * \var PipelineHandler::manager_ * \brief The Camera manager associated with the pipeline handler * * The camera manager pointer is stored in the pipeline handler for the * convenience of pipeline handler implementations. It remains valid and * constant for the whole lifetime of the pipeline handler. */ /** * \fn PipelineHandler::name() * \brief Retrieve the pipeline handler name * \context This function shall be \threadsafe. * \return The pipeline handler name */ /** * \class PipelineHandlerFactory * \brief Registration of PipelineHandler classes and creation of instances