summaryrefslogtreecommitdiff
path: root/src/v4l2/v4l2_camera.h
blob: 130995d95eb4bd4c93a366a9cd01ebb267e6980e (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
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 *
 * v4l2_camera.h - V4L2 compatibility camera
 */

#ifndef __V4L2_CAMERA_H__
#define __V4L2_CAMERA_H__

#include <deque>
#include <mutex>
#include <utility>

#include <libcamera/buffer.h>
#include <libcamera/camera.h>
#include <libcamera/file_descriptor.h>
#include <libcamera/framebuffer_allocator.h>

#include "semaphore.h"

using namespace libcamera;

class V4L2Camera
{
public:
	struct Buffer {
		Buffer(unsigned int index, const FrameMetadata &data)
			: index(index), data(data)
		{
		}

		unsigned int index;
		FrameMetadata data;
	};

	V4L2Camera(std::shared_ptr<Camera> camera);
	~V4L2Camera();

	int open();
	void close();
	void getStreamConfig(StreamConfiguration *streamConfig);
	std::vector<Buffer> completedBuffers();

	int configure(StreamConfiguration *streamConfigOut,
		      const Size &size, const PixelFormat &pixelformat,
		      unsigned int bufferCount);

	int allocBuffers(unsigned int count);
	void freeBuffers();
	FileDescriptor getBufferFd(unsigned int index);

	int streamOn();
	int streamOff();

	int qbuf(unsigned int index);

	Semaphore bufferSema_;

private:
	void requestComplete(Request *request);

	std::shared_ptr<Camera> camera_;
	std::unique_ptr<CameraConfiguration> config_;

	bool isRunning_;

	std::mutex bufferLock_;
	FrameBufferAllocator *bufferAllocator_;

	std::deque<std::unique_ptr<Request>> pendingRequests_;
	std::deque<std::unique_ptr<Buffer>> completedBuffers_;
};

#endif /* __V4L2_CAMERA_H__ */
_; } const CameraCapabilities *capabilities() const { return &capabilities_; } const std::shared_ptr<libcamera::Camera> &camera() const { return camera_; } const std::string &maker() const { return maker_; } const std::string &model() const { return model_; } int facing() const { return facing_; } int orientation() const { return orientation_; } unsigned int maxJpegBufferSize() const; void setCallbacks(const camera3_callback_ops_t *callbacks); const camera_metadata_t *getStaticMetadata(); const camera_metadata_t *constructDefaultRequestSettings(int type); int configureStreams(camera3_stream_configuration_t *stream_list); int processCaptureRequest(camera3_capture_request_t *request); void requestComplete(libcamera::Request *request); void streamProcessingComplete(Camera3RequestDescriptor::StreamBuffer *bufferStream, Camera3RequestDescriptor::Status status); protected: std::string logPrefix() const override; private: LIBCAMERA_DISABLE_COPY_AND_MOVE(CameraDevice) CameraDevice(unsigned int id, std::shared_ptr<libcamera::Camera> camera); enum class State { Stopped, Flushing, Running, }; void stop() LIBCAMERA_TSA_EXCLUDES(stateMutex_); std::unique_ptr<HALFrameBuffer> createFrameBuffer(const buffer_handle_t camera3buffer, libcamera::PixelFormat pixelFormat, const libcamera::Size &size); void abortRequest(Camera3RequestDescriptor *descriptor) const; bool isValidRequest(camera3_capture_request_t *request) const; void notifyShutter(uint32_t frameNumber, uint64_t timestamp); void notifyError(uint32_t frameNumber, camera3_stream_t *stream, camera3_error_msg_code code) const; int processControls(Camera3RequestDescriptor *descriptor); void completeDescriptor(Camera3RequestDescriptor *descriptor) LIBCAMERA_TSA_EXCLUDES(descriptorsMutex_); void sendCaptureResults() LIBCAMERA_TSA_REQUIRES(descriptorsMutex_); void setBufferStatus(Camera3RequestDescriptor::StreamBuffer &buffer, Camera3RequestDescriptor::Status status); std::unique_ptr<CameraMetadata> getResultMetadata( const Camera3RequestDescriptor &descriptor) const; unsigned int id_; camera3_device_t camera3Device_; libcamera::Mutex stateMutex_; /* Protects access to the camera state. */ State state_ LIBCAMERA_TSA_GUARDED_BY(stateMutex_); std::shared_ptr<libcamera::Camera> camera_; std::unique_ptr<libcamera::CameraConfiguration> config_; CameraCapabilities capabilities_; std::map<unsigned int, std::unique_ptr<CameraMetadata>> requestTemplates_; const camera3_callback_ops_t *callbacks_; std::vector<CameraStream> streams_; libcamera::Mutex descriptorsMutex_ LIBCAMERA_TSA_ACQUIRED_AFTER(stateMutex_); std::queue<std::unique_ptr<Camera3RequestDescriptor>> descriptors_ LIBCAMERA_TSA_GUARDED_BY(descriptorsMutex_); std::string maker_; std::string model_; int facing_; int orientation_; CameraMetadata lastSettings_; };