blob: 4126e9097e82e8a110286f3acf589f9d7a8a7b68 [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"
29
30namespace v4l2_camera_hal {
31class V4L2Wrapper {
32 public:
33 V4L2Wrapper(const std::string device_path);
34 virtual ~V4L2Wrapper();
35
36 // Connect or disconnect to the device.
37 int Connect();
38 void Disconnect();
39 // Turn the stream on or off.
40 int StreamOn();
41 int StreamOff();
42 // Manage controls.
43 int QueryControl(uint32_t control_id, v4l2_query_ext_ctrl* result);
44 int GetControl(uint32_t control_id, int32_t* value);
45 int SetControl(uint32_t control_id, int32_t desired, int32_t* result);
46 // Manage format.
47 int SetFormat(const default_camera_hal::Stream& stream);
48
49 inline bool connected() { return device_fd_.get() >= 0; }
50
51 private:
52 // Perform an ioctl call in a thread-safe fashion.
53 template <typename T>
54 int IoctlLocked(int request, T data);
55 // Adjust buffers any time a device is connected/reformatted.
56 int SetupBuffers();
57
58 // The camera device path. For example, /dev/video0.
59 const std::string device_path_;
60 // The opened device fd.
61 ScopedFd device_fd_;
62 // Whether or not the device supports the extended control query.
63 bool extended_query_supported_;
64 // The format this device is set up for.
65 std::unique_ptr<StreamFormat> format_;
66 // The maximum number of buffers this device can handle in its current format.
67 uint32_t max_buffers_;
68 // Lock protecting use of the device.
69 std::mutex device_lock_;
70
71 DISALLOW_COPY_AND_ASSIGN(V4L2Wrapper);
72};
73
74} // namespace v4l2_camera_hal
75
76#endif // V4L2_WRAPPER_H