summaryrefslogtreecommitdiff
path: root/src/v4l2/v4l2_camera.h
blob: f760316c854fba2f0f9bab376a865fc2f30a65aa (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
/* 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 <linux/videodev2.h>
#include <mutex>

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

#include "semaphore.h"

using namespace libcamera;

class V4L2FrameMetadata
{
public:
	V4L2FrameMetadata(Buffer *buffer);

	int index() const { return index_; }

	unsigned int bytesused() const { return bytesused_; }
	uint64_t timestamp() const { return timestamp_; }
	unsigned int sequence() const { return sequence_; }

	Buffer::Status status() const { return status_; }

private:
	int index_;

	unsigned int bytesused_;
	uint64_t timestamp_;
	unsigned int sequence_;

	Buffer::Status status_;
};

class V4L2Camera : public Object
{
public:
	V4L2Camera(std::shared_ptr<Camera> camera);
	~V4L2Camera();

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

	int configure(StreamConfiguration *streamConfigOut,
		      const Size &size, 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_;

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

#endif /* __V4L2_CAMERA_H__ */