blob: f2f08b3f621023ceb24d58b7da8c047b336e5241 [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() {
47 if (connect_result_ == 0) device_->Disconnect();
48 }
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070049 // Check whether the connection succeeded or not.
50 inline int status() const { return connect_result_; }
51
52 private:
53 std::shared_ptr<V4L2Wrapper> device_;
54 const int connect_result_;
55 };
56
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070057 // Turn the stream on or off.
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070058 virtual int StreamOn();
59 virtual int StreamOff();
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070060 // Manage controls.
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070061 virtual int QueryControl(uint32_t control_id, v4l2_query_ext_ctrl* result);
62 virtual int GetControl(uint32_t control_id, int32_t* value);
63 virtual int SetControl(uint32_t control_id, int32_t desired,
64 int32_t* result = nullptr);
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070065 // Manage format.
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070066 virtual int GetFormats(std::set<uint32_t>* v4l2_formats);
67 virtual int GetFormatFrameSizes(uint32_t v4l2_format,
68 std::set<std::array<int32_t, 2>>* sizes);
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070069 // Durations are returned in ns.
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070070 virtual int GetFormatFrameDurationRange(
71 uint32_t v4l2_format, const std::array<int32_t, 2>& size,
72 std::array<int64_t, 2>* duration_range);
73 virtual int SetFormat(const default_camera_hal::Stream& stream,
74 uint32_t* result_max_buffers);
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -070075 // Manage buffers.
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070076 virtual int EnqueueBuffer(const camera3_stream_buffer_t* camera_buffer);
77 virtual int DequeueBuffer(v4l2_buffer* buffer);
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070078
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070079 private:
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -070080 // Constructor is private to allow failing on bad input.
81 // Use NewV4L2Wrapper instead.
82 V4L2Wrapper(const std::string device_path,
83 std::unique_ptr<V4L2Gralloc> gralloc);
84
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070085 // Connect or disconnect to the device. Access by creating/destroying
86 // a V4L2Wrapper::Connection object.
87 int Connect();
88 void Disconnect();
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070089 // Perform an ioctl call in a thread-safe fashion.
90 template <typename T>
91 int IoctlLocked(int request, T data);
92 // Adjust buffers any time a device is connected/reformatted.
93 int SetupBuffers();
94
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070095 inline bool connected() { return device_fd_.get() >= 0; }
96
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070097 // The camera device path. For example, /dev/video0.
98 const std::string device_path_;
99 // The opened device fd.
100 ScopedFd device_fd_;
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -0700101 // The underlying gralloc module.
102 std::unique_ptr<V4L2Gralloc> gralloc_;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700103 // Whether or not the device supports the extended control query.
104 bool extended_query_supported_;
105 // The format this device is set up for.
106 std::unique_ptr<StreamFormat> format_;
107 // The maximum number of buffers this device can handle in its current format.
108 uint32_t max_buffers_;
109 // Lock protecting use of the device.
110 std::mutex device_lock_;
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700111 // Lock protecting connecting/disconnecting the device.
112 std::mutex connection_lock_;
113 // Reference count connections.
114 int connection_count_;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700115
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700116 friend class Connection;
Ari Hausman-Cohencd9fef62016-07-15 15:54:13 -0700117 friend class V4L2WrapperMock;
118
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -0700119 DISALLOW_COPY_AND_ASSIGN(V4L2Wrapper);
120};
121
122} // namespace v4l2_camera_hal
123
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -0700124#endif // V4L2_CAMERA_HAL_V4L2_WRAPPER_H_