summaryrefslogtreecommitdiff
path: root/src/v4l2/v4l2_camera_proxy.h
blob: 5aa352c36f6ace62aeac0e7e528ab69ef742bb48 (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
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 *
 * Proxy to V4L2 compatibility camera
 */

#pragma once

#include <linux/videodev2.h>
#include <map>
#include <memory>
#include <set>
#include <sys/types.h>
#include <vector>

#include <libcamera/base/mutex.h>

#include <libcamera/camera.h>

#include "v4l2_camera.h"

class V4L2CameraFile;

class V4L2CameraProxy
{
public:
	V4L2CameraProxy(unsigned int index, std::shared_ptr<libcamera::Camera> camera);

	int open(V4L2CameraFile *file) LIBCAMERA_TSA_EXCLUDES(proxyMutex_);
	void close(V4L2CameraFile *file) LIBCAMERA_TSA_EXCLUDES(proxyMutex_);
	void *mmap(V4L2CameraFile *file, void *addr, size_t length, int prot,
		   int flags, off64_t offset) LIBCAMERA_TSA_EXCLUDES(proxyMutex_);
	int munmap(V4L2CameraFile *file, void *addr, size_t length)
		LIBCAMERA_TSA_EXCLUDES(proxyMutex_);

	int ioctl(V4L2CameraFile *file, unsigned long request, void *arg)
		LIBCAMERA_TSA_EXCLUDES(proxyMutex_);

private:
	bool validateBufferType(uint32_t type);
	bool validateMemoryType(uint32_t memory);
	void setFmtFromConfig(const libcamera::StreamConfiguration &streamConfig);
	void querycap(std::shared_ptr<libcamera::Camera> camera);
	int tryFormat(struct v4l2_format *arg);
	enum v4l2_priority maxPriority();
	void updateBuffers();
	void freeBuffers();

	int vidioc_querycap(V4L2CameraFile *file, struct v4l2_capability *arg);
	int vidioc_enum_framesizes(V4L2CameraFile *file, struct v4l2_frmsizeenum *arg);
	int vidioc_enum_fmt(V4L2CameraFile *file, struct v4l2_fmtdesc *arg);
	int vidioc_g_fmt(V4L2CameraFile *file, struct v4l2_format *arg);
	int vidioc_s_fmt(V4L2CameraFile *file, struct v4l2_format *arg);
	int vidioc_try_fmt(V4L2CameraFile *file, struct v4l2_format *arg);
	int vidioc_g_priority(V4L2CameraFile *file, enum v4l2_priority *arg);
	int vidioc_s_priority(V4L2CameraFile *file, enum v4l2_priority *arg);
	int vidioc_enuminput(V4L2CameraFile *file, struct v4l2_input *arg);
	int vidioc_g_input(V4L2CameraFile *file, int *arg);
	int vidioc_s_input(V4L2CameraFile *file, int *arg);
	int vidioc_reqbufs(V4L2CameraFile *file, struct v4l2_requestbuffers *arg);
	int vidioc_querybuf(V4L2CameraFile *file, struct v4l2_buffer *arg);
	int vidioc_prepare_buf(V4L2CameraFile *file, struct v4l2_buffer *arg);
	int vidioc_qbuf(V4L2CameraFile *file, struct v4l2_buffer *arg);
	int vidioc_dqbuf(V4L2CameraFile *file, struct v4l2_buffer *arg,
			 libcamera::Mutex *lock) LIBCAMERA_TSA_REQUIRES(*lock);
	int vidioc_expbuf(V4L2CameraFile *file, struct v4l2_exportbuffer *arg);
	int vidioc_streamon(V4L2CameraFile *file, int *arg);
	int vidioc_streamoff(V4L2CameraFile *file, int *arg);
	int vidioc_g_parm(V4L2CameraFile *file, struct v4l2_streamparm *arg);
	int vidioc_s_parm(V4L2CameraFile *file, struct v4l2_streamparm *arg);

	bool hasOwnership(V4L2CameraFile *file);
	int acquire(V4L2CameraFile *file);
	void release(V4L2CameraFile *file);

	static const std::set<unsigned long> supportedIoctls_;

	unsigned int refcount_;
	unsigned int index_;

	libcamera::StreamConfiguration streamConfig_;
	unsigned int bufferCount_;
	unsigned int currentBuf_;
	unsigned int sizeimage_;

	struct v4l2_capability capabilities_;
	struct v4l2_pix_format v4l2PixFormat_;
	struct v4l2_fract v4l2TimePerFrame_;

	std::vector<struct v4l2_buffer> buffers_;
	std::map<void *, unsigned int> mmaps_;

	std::set<V4L2CameraFile *> files_;

	std::unique_ptr<V4L2Camera> vcam_;

	/*
	 * This is the exclusive owner of this V4L2CameraProxy instance.
	 * When there is no owner, anybody can call any ioctl before reqbufs.
	 * The first file to call reqbufs with count > 0 or s_fmt will become
	 * the owner, and when the owner calls reqbufs with count = 0 it will
	 * release ownership. Any buffer-related ioctl (except querybuf) or
	 * s_fmt that is called by a non-owner while there exists an owner
	 * will return -EBUSY.
	 */
	V4L2CameraFile *owner_;

	/* This mutex is to serialize access to the proxy. */
	libcamera::Mutex proxyMutex_;
};
hl kwc">std::vector<uint8_t>::const_iterator dataBegin, std::vector<uint8_t>::const_iterator dataEnd, ControlSerializer *cs = nullptr); static T deserialize(const std::vector<uint8_t> &data, const std::vector<int32_t> &fds, ControlSerializer *cs = nullptr); static T deserialize(std::vector<uint8_t>::const_iterator dataBegin, std::vector<uint8_t>::const_iterator dataEnd, std::vector<int32_t>::const_iterator fdsBegin, std::vector<int32_t>::const_iterator fdsEnd, ControlSerializer *cs = nullptr); }; #ifndef __DOXYGEN__ /* * Serialization format for vector of type V: * * 4 bytes - uint32_t Length of vector, in number of elements * * For every element in the vector: * * 4 bytes - uint32_t Size of element, in bytes * 4 bytes - uint32_t Number of fds for the element * X bytes - Serialized element * * \todo Support elements that are references */ template<typename V> class IPADataSerializer<std::vector<V>> { public: static std::tuple<std::vector<uint8_t>, std::vector<int32_t>> serialize(const std::vector<V> &data, ControlSerializer *cs = nullptr) { std::vector<uint8_t> dataVec; std::vector<int32_t> fdsVec; /* Serialize the length. */ uint32_t vecLen = data.size(); appendPOD<uint32_t>(dataVec, vecLen); /* Serialize the members. */ for (auto const &it : data) { std::vector<uint8_t> dvec; std::vector<int32_t> fvec; std::tie(dvec, fvec) = IPADataSerializer<V>::serialize(it, cs); appendPOD<uint32_t>(dataVec, dvec.size()); appendPOD<uint32_t>(dataVec, fvec.size()); dataVec.insert(dataVec.end(), dvec.begin(), dvec.end()); fdsVec.insert(fdsVec.end(), fvec.begin(), fvec.end()); } return { dataVec, fdsVec }; } static std::vector<V> deserialize(std::vector<uint8_t> &data, ControlSerializer *cs = nullptr) { return deserialize(data.cbegin(), data.end(), cs); } static std::vector<V> deserialize(std::vector<uint8_t>::const_iterator dataBegin, std::vector<uint8_t>::const_iterator dataEnd, ControlSerializer *cs = nullptr) { std::vector<int32_t> fds; return deserialize(dataBegin, dataEnd, fds.cbegin(), fds.end(), cs); } static std::vector<V> deserialize(std::vector<uint8_t> &data, std::vector<int32_t> &fds, ControlSerializer *cs = nullptr) { return deserialize(data.cbegin(), data.end(), fds.cbegin(), fds.end(), cs); } static std::vector<V> deserialize(std::vector<uint8_t>::const_iterator dataBegin, std::vector<uint8_t>::const_iterator dataEnd, std::vector<int32_t>::const_iterator fdsBegin, [[maybe_unused]] std::vector<int32_t>::const_iterator fdsEnd, ControlSerializer *cs = nullptr) { uint32_t vecLen = readPOD<uint32_t>(dataBegin, 0, dataEnd); std::vector<V> ret(vecLen); std::vector<uint8_t>::const_iterator dataIter = dataBegin + 4; std::vector<int32_t>::const_iterator fdIter = fdsBegin; for (uint32_t i = 0; i < vecLen; i++) { uint32_t sizeofData = readPOD<uint32_t>(dataIter, 0, dataEnd); uint32_t sizeofFds = readPOD<uint32_t>(dataIter, 4, dataEnd); dataIter += 8; ret[i] = IPADataSerializer<V>::deserialize(dataIter, dataIter + sizeofData, fdIter, fdIter + sizeofFds, cs); dataIter += sizeofData; fdIter += sizeofFds; } return ret; } }; /* * Serialization format for map of key type K and value type V: * * 4 bytes - uint32_t Length of map, in number of pairs * * For every pair in the map: * * 4 bytes - uint32_t Size of key, in bytes * 4 bytes - uint32_t Number of fds for the key * X bytes - Serialized key * 4 bytes - uint32_t Size of value, in bytes * 4 bytes - uint32_t Number of fds for the value * X bytes - Serialized value * * \todo Support keys or values that are references */ template<typename K, typename V> class IPADataSerializer<std::map<K, V>> { public: static std::tuple<std::vector<uint8_t>, std::vector<int32_t>> serialize(const std::map<K, V> &data, ControlSerializer *cs = nullptr) { std::vector<uint8_t> dataVec; std::vector<int32_t> fdsVec; /* Serialize the length. */ uint32_t mapLen = data.size(); appendPOD<uint32_t>(dataVec, mapLen); /* Serialize the members. */ for (auto const &it : data) { std::vector<uint8_t> dvec; std::vector<int32_t> fvec; std::tie(dvec, fvec) = IPADataSerializer<K>::serialize(it.first, cs); appendPOD<uint32_t>(dataVec, dvec.size()); appendPOD<uint32_t>(dataVec, fvec.size()); dataVec.insert(dataVec.end(), dvec.begin(), dvec.end()); fdsVec.insert(fdsVec.end(), fvec.begin(), fvec.end()); std::tie(dvec, fvec) = IPADataSerializer<V>::serialize(it.second, cs); appendPOD<uint32_t>(dataVec, dvec.size()); appendPOD<uint32_t>(dataVec, fvec.size()); dataVec.insert(dataVec.end(), dvec.begin(), dvec.end()); fdsVec.insert(fdsVec.end(), fvec.begin(), fvec.end()); } return { dataVec, fdsVec }; } static std::map<K, V> deserialize(std::vector<uint8_t> &data, ControlSerializer *cs = nullptr) { return deserialize(data.cbegin(), data.end(), cs); } static std::map<K, V> deserialize(std::vector<uint8_t>::const_iterator dataBegin, std::vector<uint8_t>::const_iterator dataEnd, ControlSerializer *cs = nullptr) { std::vector<int32_t> fds; return deserialize(dataBegin, dataEnd, fds.cbegin(), fds.end(), cs); } static std::map<K, V> deserialize(std::vector<uint8_t> &data, std::vector<int32_t> &fds, ControlSerializer *cs = nullptr) { return deserialize(data.cbegin(), data.end(), fds.cbegin(), fds.end(), cs); } static std::map<K, V> deserialize(std::vector<uint8_t>::const_iterator dataBegin, std::vector<uint8_t>::const_iterator dataEnd, std::vector<int32_t>::const_iterator fdsBegin, [[maybe_unused]] std::vector<int32_t>::const_iterator fdsEnd, ControlSerializer *cs = nullptr) { std::map<K, V> ret; uint32_t mapLen = readPOD<uint32_t>(dataBegin, 0, dataEnd); std::vector<uint8_t>::const_iterator dataIter = dataBegin + 4; std::vector<int32_t>::const_iterator fdIter = fdsBegin; for (uint32_t i = 0; i < mapLen; i++) { uint32_t sizeofData = readPOD<uint32_t>(dataIter, 0, dataEnd); uint32_t sizeofFds = readPOD<uint32_t>(dataIter, 4, dataEnd); dataIter += 8; K key = IPADataSerializer<K>::deserialize(dataIter, dataIter + sizeofData, fdIter, fdIter + sizeofFds, cs); dataIter += sizeofData; fdIter += sizeofFds; sizeofData = readPOD<uint32_t>(dataIter, 0, dataEnd); sizeofFds = readPOD<uint32_t>(dataIter, 4, dataEnd); dataIter += 8; const V value = IPADataSerializer<V>::deserialize(dataIter, dataIter + sizeofData, fdIter, fdIter + sizeofFds, cs); ret.insert({ key, value }); dataIter += sizeofData; fdIter += sizeofFds; } return ret; } }; #endif /* __DOXYGEN__ */ } /* namespace libcamera */ #endif /* __LIBCAMERA_INTERNAL_IPA_DATA_SERIALIZER_H__ */