blob: 113499a89d88e4d1063ddb1f3d528d175772131f [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
20#include <memory>
21#include <mutex>
22#include <string>
23
24#include <nativehelper/ScopedFd.h>
25
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070026#include "common.h"
27#include "stream.h"
28#include "stream_format.h"
29#include "v4l2_gralloc.h"
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070030
31namespace v4l2_camera_hal {
32class V4L2Wrapper {
33 public:
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -070034 // 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-Cohenc17fd092016-07-18 10:13:26 -070037 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);
Ari Hausman-Cohen99f3ea02016-08-02 10:47:07 -070048 int SetControl(uint32_t control_id, int32_t desired,
49 int32_t* result = nullptr);
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070050 // Manage format.
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -070051 int SetFormat(const default_camera_hal::Stream& stream,
52 uint32_t* result_max_buffers);
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -070053 // Manage buffers.
54 int EnqueueBuffer(const camera3_stream_buffer_t* camera_buffer);
55 int DequeueBuffer(v4l2_buffer* buffer);
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070056
57 inline bool connected() { return device_fd_.get() >= 0; }
58
59 private:
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -070060 // Constructor is private to allow failing on bad input.
61 // Use NewV4L2Wrapper instead.
62 V4L2Wrapper(const std::string device_path,
63 std::unique_ptr<V4L2Gralloc> gralloc);
64
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070065 // Perform an ioctl call in a thread-safe fashion.
66 template <typename T>
67 int IoctlLocked(int request, T data);
68 // Adjust buffers any time a device is connected/reformatted.
69 int SetupBuffers();
70
71 // The camera device path. For example, /dev/video0.
72 const std::string device_path_;
73 // The opened device fd.
74 ScopedFd device_fd_;
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -070075 // The underlying gralloc module.
76 std::unique_ptr<V4L2Gralloc> gralloc_;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070077 // Whether or not the device supports the extended control query.
78 bool extended_query_supported_;
79 // The format this device is set up for.
80 std::unique_ptr<StreamFormat> format_;
81 // The maximum number of buffers this device can handle in its current format.
82 uint32_t max_buffers_;
83 // Lock protecting use of the device.
84 std::mutex device_lock_;
85
Ari Hausman-Cohencd9fef62016-07-15 15:54:13 -070086 friend class V4L2WrapperMock;
87
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070088 DISALLOW_COPY_AND_ASSIGN(V4L2Wrapper);
89};
90
91} // namespace v4l2_camera_hal
92
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070093#endif // V4L2_CAMERA_HAL_V4L2_WRAPPER_H_