blob: 3798c14f8914625e11f7d08ee3daf30b4046157c [file] [log] [blame]
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070017#ifndef V4L2_CAMERA_HAL_V4L2_WRAPPER_H_
18#define V4L2_CAMERA_HAL_V4L2_WRAPPER_H_
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070019
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070020#include <array>
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070021#include <memory>
22#include <mutex>
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070023#include <set>
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070024#include <string>
25
26#include <nativehelper/ScopedFd.h>
27
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070028#include "common.h"
29#include "stream.h"
30#include "stream_format.h"
31#include "v4l2_gralloc.h"
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070032
33namespace v4l2_camera_hal {
34class V4L2Wrapper {
35 public:
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -070036 // Use this method to create V4L2Wrapper objects. Functionally equivalent
37 // to "new V4L2Wrapper", except that it may return nullptr in case of failure.
38 static V4L2Wrapper* NewV4L2Wrapper(const std::string device_path);
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070039 virtual ~V4L2Wrapper();
40
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070041 // Helper class to ensure all opened connections are closed.
42 class Connection {
43 public:
44 Connection(std::shared_ptr<V4L2Wrapper> device)
45 : device_(std::move(device)), connect_result_(device_->Connect()) {}
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070046 ~Connection() {
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -070047 if (connect_result_ == 0)
48 device_->Disconnect();
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070049 }
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070050 // Check whether the connection succeeded or not.
51 inline int status() const { return connect_result_; }
52
53 private:
54 std::shared_ptr<V4L2Wrapper> device_;
55 const int connect_result_;
56 };
57
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070058 // Turn the stream on or off.
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070059 virtual int StreamOn();
60 virtual int StreamOff();
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070061 // Manage controls.
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070062 virtual int QueryControl(uint32_t control_id, v4l2_query_ext_ctrl* result);
63 virtual int GetControl(uint32_t control_id, int32_t* value);
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -070064 virtual int SetControl(uint32_t control_id,
65 int32_t desired,
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070066 int32_t* result = nullptr);
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070067 // Manage format.
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070068 virtual int GetFormats(std::set<uint32_t>* v4l2_formats);
69 virtual int GetFormatFrameSizes(uint32_t v4l2_format,
70 std::set<std::array<int32_t, 2>>* sizes);
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070071 // Durations are returned in ns.
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070072 virtual int GetFormatFrameDurationRange(
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -070073 uint32_t v4l2_format,
74 const std::array<int32_t, 2>& size,
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070075 std::array<int64_t, 2>* duration_range);
76 virtual int SetFormat(const default_camera_hal::Stream& stream,
77 uint32_t* result_max_buffers);
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -070078 // Manage buffers.
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070079 virtual int EnqueueBuffer(const camera3_stream_buffer_t* camera_buffer);
80 virtual int DequeueBuffer(v4l2_buffer* buffer);
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070081
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070082 private:
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -070083 // Constructor is private to allow failing on bad input.
84 // Use NewV4L2Wrapper instead.
85 V4L2Wrapper(const std::string device_path,
86 std::unique_ptr<V4L2Gralloc> gralloc);
87
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070088 // Connect or disconnect to the device. Access by creating/destroying
89 // a V4L2Wrapper::Connection object.
90 int Connect();
91 void Disconnect();
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070092 // Perform an ioctl call in a thread-safe fashion.
93 template <typename T>
94 int IoctlLocked(int request, T data);
95 // Adjust buffers any time a device is connected/reformatted.
96 int SetupBuffers();
97
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070098 inline bool connected() { return device_fd_.get() >= 0; }
99
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700100 // The camera device path. For example, /dev/video0.
101 const std::string device_path_;
102 // The opened device fd.
103 ScopedFd device_fd_;
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700104 // The underlying gralloc module.
105 std::unique_ptr<V4L2Gralloc> gralloc_;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700106 // Whether or not the device supports the extended control query.
107 bool extended_query_supported_;
108 // The format this device is set up for.
109 std::unique_ptr<StreamFormat> format_;
110 // The maximum number of buffers this device can handle in its current format.
111 uint32_t max_buffers_;
112 // Lock protecting use of the device.
113 std::mutex device_lock_;
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700114 // Lock protecting connecting/disconnecting the device.
115 std::mutex connection_lock_;
116 // Reference count connections.
117 int connection_count_;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700118
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700119 friend class Connection;
Ari Hausman-Cohencd9fef62016-07-15 15:54:13 -0700120 friend class V4L2WrapperMock;
121
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700122 DISALLOW_COPY_AND_ASSIGN(V4L2Wrapper);
123};
124
125} // namespace v4l2_camera_hal
126
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -0700127#endif // V4L2_CAMERA_HAL_V4L2_WRAPPER_H_