blob: 1d429738f5b226cb0716cc56550956131fe2c997 [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
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-Cohen4ab49622016-07-21 14:33:54 -070029#include "V4L2Gralloc.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);
48 int SetControl(uint32_t control_id, int32_t desired, int32_t* result);
49 // Manage format.
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -070050 int SetFormat(const default_camera_hal::Stream& stream,
51 uint32_t* result_max_buffers);
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -070052 // Manage buffers.
53 int EnqueueBuffer(const camera3_stream_buffer_t* camera_buffer);
54 int DequeueBuffer(v4l2_buffer* buffer);
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070055
56 inline bool connected() { return device_fd_.get() >= 0; }
57
58 private:
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -070059 // Constructor is private to allow failing on bad input.
60 // Use NewV4L2Wrapper instead.
61 V4L2Wrapper(const std::string device_path,
62 std::unique_ptr<V4L2Gralloc> gralloc);
63
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070064 // Perform an ioctl call in a thread-safe fashion.
65 template <typename T>
66 int IoctlLocked(int request, T data);
67 // Adjust buffers any time a device is connected/reformatted.
68 int SetupBuffers();
69
70 // The camera device path. For example, /dev/video0.
71 const std::string device_path_;
72 // The opened device fd.
73 ScopedFd device_fd_;
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -070074 // The underlying gralloc module.
75 std::unique_ptr<V4L2Gralloc> gralloc_;
Ari Hausman-Cohenc17fd092016-07-18 10:13:26 -070076 // Whether or not the device supports the extended control query.
77 bool extended_query_supported_;
78 // The format this device is set up for.
79 std::unique_ptr<StreamFormat> format_;
80 // The maximum number of buffers this device can handle in its current format.
81 uint32_t max_buffers_;
82 // Lock protecting use of the device.
83 std::mutex device_lock_;
84
85 DISALLOW_COPY_AND_ASSIGN(V4L2Wrapper);
86};
87
88} // namespace v4l2_camera_hal
89
90#endif // V4L2_WRAPPER_H