Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 1 | /* |
| 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-Cohen | 3841a7f | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 17 | #ifndef V4L2_CAMERA_HAL_V4L2_WRAPPER_H_ |
| 18 | #define V4L2_CAMERA_HAL_V4L2_WRAPPER_H_ |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 19 | |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 20 | #include <array> |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 21 | #include <memory> |
| 22 | #include <mutex> |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 23 | #include <set> |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 24 | #include <string> |
| 25 | |
| 26 | #include <nativehelper/ScopedFd.h> |
| 27 | |
Ari Hausman-Cohen | 3841a7f | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 28 | #include "common.h" |
| 29 | #include "stream.h" |
| 30 | #include "stream_format.h" |
| 31 | #include "v4l2_gralloc.h" |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 32 | |
| 33 | namespace v4l2_camera_hal { |
| 34 | class V4L2Wrapper { |
| 35 | public: |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 36 | // 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-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 39 | virtual ~V4L2Wrapper(); |
| 40 | |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 41 | // 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-Cohen | 3a4c3bb | 2016-08-01 17:16:01 -0700 | [diff] [blame] | 46 | ~Connection() { |
Ari Hausman-Cohen | 5d75323 | 2016-08-10 14:27:36 -0700 | [diff] [blame^] | 47 | if (connect_result_ == 0) |
| 48 | device_->Disconnect(); |
Ari Hausman-Cohen | 3a4c3bb | 2016-08-01 17:16:01 -0700 | [diff] [blame] | 49 | } |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 50 | // 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-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 58 | // Turn the stream on or off. |
Ari Hausman-Cohen | 3a4c3bb | 2016-08-01 17:16:01 -0700 | [diff] [blame] | 59 | virtual int StreamOn(); |
| 60 | virtual int StreamOff(); |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 61 | // Manage controls. |
Ari Hausman-Cohen | 3a4c3bb | 2016-08-01 17:16:01 -0700 | [diff] [blame] | 62 | 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-Cohen | 5d75323 | 2016-08-10 14:27:36 -0700 | [diff] [blame^] | 64 | virtual int SetControl(uint32_t control_id, |
| 65 | int32_t desired, |
Ari Hausman-Cohen | 3a4c3bb | 2016-08-01 17:16:01 -0700 | [diff] [blame] | 66 | int32_t* result = nullptr); |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 67 | // Manage format. |
Ari Hausman-Cohen | 3a4c3bb | 2016-08-01 17:16:01 -0700 | [diff] [blame] | 68 | 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-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 71 | // Durations are returned in ns. |
Ari Hausman-Cohen | 3a4c3bb | 2016-08-01 17:16:01 -0700 | [diff] [blame] | 72 | virtual int GetFormatFrameDurationRange( |
Ari Hausman-Cohen | 5d75323 | 2016-08-10 14:27:36 -0700 | [diff] [blame^] | 73 | uint32_t v4l2_format, |
| 74 | const std::array<int32_t, 2>& size, |
Ari Hausman-Cohen | 3a4c3bb | 2016-08-01 17:16:01 -0700 | [diff] [blame] | 75 | 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-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 78 | // Manage buffers. |
Ari Hausman-Cohen | 3a4c3bb | 2016-08-01 17:16:01 -0700 | [diff] [blame] | 79 | virtual int EnqueueBuffer(const camera3_stream_buffer_t* camera_buffer); |
| 80 | virtual int DequeueBuffer(v4l2_buffer* buffer); |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 81 | |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 82 | private: |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 83 | // 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-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 88 | // Connect or disconnect to the device. Access by creating/destroying |
| 89 | // a V4L2Wrapper::Connection object. |
| 90 | int Connect(); |
| 91 | void Disconnect(); |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 92 | // 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-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 98 | inline bool connected() { return device_fd_.get() >= 0; } |
| 99 | |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 100 | // 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-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame] | 104 | // The underlying gralloc module. |
| 105 | std::unique_ptr<V4L2Gralloc> gralloc_; |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 106 | // 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-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 114 | // Lock protecting connecting/disconnecting the device. |
| 115 | std::mutex connection_lock_; |
| 116 | // Reference count connections. |
| 117 | int connection_count_; |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 118 | |
Ari Hausman-Cohen | 9e6fd98 | 2016-08-02 16:29:53 -0700 | [diff] [blame] | 119 | friend class Connection; |
Ari Hausman-Cohen | cd9fef6 | 2016-07-15 15:54:13 -0700 | [diff] [blame] | 120 | friend class V4L2WrapperMock; |
| 121 | |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 122 | DISALLOW_COPY_AND_ASSIGN(V4L2Wrapper); |
| 123 | }; |
| 124 | |
| 125 | } // namespace v4l2_camera_hal |
| 126 | |
Ari Hausman-Cohen | 3841a7f | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 127 | #endif // V4L2_CAMERA_HAL_V4L2_WRAPPER_H_ |