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 | |
| 17 | #ifndef V4L2_WRAPPER_H |
| 18 | #define V4L2_WRAPPER_H |
| 19 | |
| 20 | #include <memory> |
| 21 | #include <mutex> |
| 22 | #include <string> |
| 23 | |
| 24 | #include <nativehelper/ScopedFd.h> |
| 25 | |
| 26 | #include "Common.h" |
| 27 | #include "Stream.h" |
| 28 | #include "StreamFormat.h" |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame^] | 29 | #include "V4L2Gralloc.h" |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 30 | |
| 31 | namespace v4l2_camera_hal { |
| 32 | class V4L2Wrapper { |
| 33 | public: |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame^] | 34 | // Use this method to create V4L2Wrapper objects. Functionally equivalent |
| 35 | // to "new V4L2Wrapper", except that it may return nullptr in case of failure. |
| 36 | static V4L2Wrapper* NewV4L2Wrapper(const std::string device_path); |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 37 | virtual ~V4L2Wrapper(); |
| 38 | |
| 39 | // Connect or disconnect to the device. |
| 40 | int Connect(); |
| 41 | void Disconnect(); |
| 42 | // Turn the stream on or off. |
| 43 | int StreamOn(); |
| 44 | int StreamOff(); |
| 45 | // Manage controls. |
| 46 | int QueryControl(uint32_t control_id, v4l2_query_ext_ctrl* result); |
| 47 | int GetControl(uint32_t control_id, int32_t* value); |
| 48 | int SetControl(uint32_t control_id, int32_t desired, int32_t* result); |
| 49 | // Manage format. |
| 50 | int SetFormat(const default_camera_hal::Stream& stream); |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame^] | 51 | // Manage buffers. |
| 52 | int EnqueueBuffer(const camera3_stream_buffer_t* camera_buffer); |
| 53 | int DequeueBuffer(v4l2_buffer* buffer); |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 54 | |
| 55 | inline bool connected() { return device_fd_.get() >= 0; } |
| 56 | |
| 57 | private: |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame^] | 58 | // Constructor is private to allow failing on bad input. |
| 59 | // Use NewV4L2Wrapper instead. |
| 60 | V4L2Wrapper(const std::string device_path, |
| 61 | std::unique_ptr<V4L2Gralloc> gralloc); |
| 62 | |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 63 | // Perform an ioctl call in a thread-safe fashion. |
| 64 | template <typename T> |
| 65 | int IoctlLocked(int request, T data); |
| 66 | // Adjust buffers any time a device is connected/reformatted. |
| 67 | int SetupBuffers(); |
| 68 | |
| 69 | // The camera device path. For example, /dev/video0. |
| 70 | const std::string device_path_; |
| 71 | // The opened device fd. |
| 72 | ScopedFd device_fd_; |
Ari Hausman-Cohen | 4ab4962 | 2016-07-21 14:33:54 -0700 | [diff] [blame^] | 73 | // The underlying gralloc module. |
| 74 | std::unique_ptr<V4L2Gralloc> gralloc_; |
Ari Hausman-Cohen | c17fd09 | 2016-07-18 10:13:26 -0700 | [diff] [blame] | 75 | // Whether or not the device supports the extended control query. |
| 76 | bool extended_query_supported_; |
| 77 | // The format this device is set up for. |
| 78 | std::unique_ptr<StreamFormat> format_; |
| 79 | // The maximum number of buffers this device can handle in its current format. |
| 80 | uint32_t max_buffers_; |
| 81 | // Lock protecting use of the device. |
| 82 | std::mutex device_lock_; |
| 83 | |
| 84 | DISALLOW_COPY_AND_ASSIGN(V4L2Wrapper); |
| 85 | }; |
| 86 | |
| 87 | } // namespace v4l2_camera_hal |
| 88 | |
| 89 | #endif // V4L2_WRAPPER_H |