blob: 9ebefeb9f2b47e9c49213158c2264c40744bef5d [file] [log] [blame]
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001/*
2 * Copyright 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#include "v4l2_camera.h"
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070018
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070019#include <fcntl.h>
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070020#include <linux/videodev2.h>
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070021#include <sys/types.h>
22#include <sys/stat.h>
23
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070024#include <cstdlib>
25
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070026#include <camera/CameraMetadata.h>
27#include <hardware/camera3.h>
28
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070029#include "common.h"
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070030#include "metadata/metadata_common.h"
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070031#include "stream_format.h"
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070032#include "v4l2_metadata_factory.h"
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070033
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070034#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
35
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070036namespace v4l2_camera_hal {
37
Ari Hausman-Cohendde80172016-07-01 16:20:36 -070038// Helper function for managing metadata.
39static std::vector<int32_t> getMetadataKeys(
40 const camera_metadata_t* metadata) {
41 std::vector<int32_t> keys;
42 size_t num_entries = get_camera_metadata_entry_count(metadata);
43 for (size_t i = 0; i < num_entries; ++i) {
44 camera_metadata_ro_entry_t entry;
45 get_camera_metadata_ro_entry(metadata, i, &entry);
46 keys.push_back(entry.tag);
47 }
48 return keys;
49}
50
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -070051V4L2Camera* V4L2Camera::NewV4L2Camera(int id, const std::string path) {
52 HAL_LOG_ENTER();
53
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070054 std::shared_ptr<V4L2Wrapper> v4l2_wrapper(V4L2Wrapper::NewV4L2Wrapper(path));
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -070055 if (!v4l2_wrapper) {
56 HAL_LOGE("Failed to initialize V4L2 wrapper.");
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -070057 return nullptr;
58 }
59
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070060 std::unique_ptr<Metadata> metadata;
61 int res = GetV4L2Metadata(v4l2_wrapper, &metadata);
62 if (res) {
63 HAL_LOGE("Failed to initialize V4L2 metadata: %d", res);
64 return nullptr;
65 }
66
67 return new V4L2Camera(id, std::move(v4l2_wrapper), std::move(metadata));
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -070068}
69
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070070V4L2Camera::V4L2Camera(int id, std::shared_ptr<V4L2Wrapper> v4l2_wrapper,
71 std::unique_ptr<Metadata> metadata)
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070072 : default_camera_hal::Camera(id),
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070073 device_(std::move(v4l2_wrapper)),
74 metadata_(std::move(metadata)),
75 max_input_streams_(0),
76 max_output_streams_({{0, 0, 0}}) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070077 HAL_LOG_ENTER();
78}
79
80V4L2Camera::~V4L2Camera() {
81 HAL_LOG_ENTER();
82}
83
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070084int V4L2Camera::connect() {
85 HAL_LOG_ENTER();
86
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070087 if (connection_) {
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070088 HAL_LOGE("Already connected. Please disconnect and try again.");
89 return -EIO;
90 }
91
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070092 connection_.reset(new V4L2Wrapper::Connection(device_));
93 if (connection_->status()) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -070094 HAL_LOGE("Failed to connect to device.");
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070095 return connection_->status();
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070096 }
97
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070098 // TODO(b/29185945): confirm this is a supported device.
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070099 // This is checked by the HAL, but the device at |device_|'s path may
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700100 // not be the same one that was there when the HAL was loaded.
101 // (Alternatively, better hotplugging support may make this unecessary
102 // by disabling cameras that get disconnected and checking newly connected
103 // cameras, so connect() is never called on an unsupported camera)
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700104
105 // TODO(b/29158098): Inform service of any flashes that are no longer available
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700106 // because this camera is in use.
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -0700107 return 0;
108}
109
110void V4L2Camera::disconnect() {
111 HAL_LOG_ENTER();
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700112
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700113 connection_.reset();
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700114
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700115 // TODO(b/29158098): Inform service of any flashes that are available again
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700116 // because this camera is no longer in use.
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -0700117}
118
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700119int V4L2Camera::initStaticInfo(android::CameraMetadata* out) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700120 HAL_LOG_ENTER();
121
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700122 int res = metadata_->FillStaticMetadata(out);
123 if (res) {
124 HAL_LOGE("Failed to get static metadata.");
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700125 return res;
126 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700127
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700128 // Extract max streams for use in verifying stream configs.
129 res = SingleTagValue(*out, ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS,
130 &max_input_streams_);
131 if (res) {
132 HAL_LOGE("Failed to get max num input streams from static metadata.");
133 return res;
134 }
135 res = SingleTagValue(*out, ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS,
136 &max_output_streams_);
137 if (res) {
138 HAL_LOGE("Failed to get max num output streams from static metadata.");
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700139 return res;
140 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700141
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700142 return 0;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700143}
144
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700145int V4L2Camera::initTemplate(int type, android::CameraMetadata* out) {
146 HAL_LOG_ENTER();
147
148 return metadata_->GetRequestTemplate(type, out);
149}
150
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700151void V4L2Camera::initDeviceInfo(camera_info_t* info) {
152 HAL_LOG_ENTER();
153
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700154 // TODO(b/31044975): move this into device interface.
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700155 // For now, just constants.
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700156 info->resource_cost = 100;
157 info->conflicting_devices = nullptr;
158 info->conflicting_devices_length = 0;
159}
160
161int V4L2Camera::initDevice() {
162 HAL_LOG_ENTER();
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700163 // Nothing to do.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700164 return 0;
165}
166
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700167int V4L2Camera::enqueueBuffer(const camera3_stream_buffer_t* camera_buffer) {
168 HAL_LOG_ENTER();
169
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700170 int res = device_->EnqueueBuffer(camera_buffer);
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700171 if (res) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700172 HAL_LOGE("Device failed to enqueue buffer.");
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700173 return res;
174 }
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700175
176 // Turn on the stream.
177 // TODO(b/29334616): Lock around stream on/off access, only start stream
178 // if not already on. (For now, since it's synchronous, it will always be
179 // turned off before another call to this function).
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700180 res = device_->StreamOn();
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700181 if (res) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700182 HAL_LOGE("Device failed to turn on stream.");
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700183 return res;
184 }
185
186 // TODO(b/29334616): Enqueueing and dequeueing should be separate worker
187 // threads, not in the same function.
188
189 // Dequeue the buffer.
190 v4l2_buffer result_buffer;
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700191 res = device_->DequeueBuffer(&result_buffer);
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700192 if (res) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700193 HAL_LOGE("Device failed to dequeue buffer.");
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700194 return res;
195 }
196
197 // All done, cleanup.
198 // TODO(b/29334616): Lock around stream on/off access, only stop stream if
199 // buffer queue is empty (synchronously, there's only ever 1 buffer in the
200 // queue at a time, so this is safe).
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700201 res = device_->StreamOff();
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700202 if (res) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700203 HAL_LOGE("Device failed to turn off stream.");
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700204 return res;
205 }
206
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700207 return 0;
208}
209
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700210int V4L2Camera::getResultSettings(android::CameraMetadata* metadata,
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700211 uint64_t* timestamp) {
212 HAL_LOG_ENTER();
213
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700214 // Get the results.
215 int res = metadata_->FillResultMetadata(metadata);
216 if (res) {
217 HAL_LOGE("Failed to fill result metadata.");
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700218 return res;
219 }
220
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700221 // Extract the timestamp.
222 int64_t frame_time = 0;
223 res = SingleTagValue(*metadata, ANDROID_SENSOR_TIMESTAMP, &frame_time);
224 if (res) {
225 HAL_LOGE("Failed to extract timestamp from result metadata");
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700226 return res;
227 }
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700228 *timestamp = static_cast<uint64_t>(frame_time);
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700229
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700230 return 0;
231}
232
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700233bool V4L2Camera::isSupportedStreamSet(default_camera_hal::Stream** streams,
234 int count, uint32_t mode) {
235 HAL_LOG_ENTER();
236
237 if (mode != CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE) {
238 HAL_LOGE("Unsupported stream configuration mode: %d", mode);
239 return false;
240 }
241
242 // This should be checked by the caller, but put here as a sanity check.
243 if (count < 1) {
244 HAL_LOGE("Must request at least 1 stream");
245 return false;
246 }
247
248 // Count the number of streams of each type.
249 int32_t num_input = 0;
250 int32_t num_raw = 0;
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700251 int32_t num_stalling = 0;
252 int32_t num_non_stalling = 0;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700253 for (int i = 0; i < count; ++i) {
254 default_camera_hal::Stream* stream = streams[i];
255
256 if (stream->isInputType()) {
257 ++num_input;
258 }
259
260 if (stream->isOutputType()) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700261 StreamFormat format(*stream);
262 switch (format.Category()) {
263 case kFormatCategoryRaw:
264 ++num_raw;
265 case kFormatCategoryStalling:
266 ++num_stalling;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700267 break;
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700268 case kFormatCategoryNonStalling:
269 ++num_non_stalling;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700270 break;
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700271 case kFormatCategoryUnknown: // Fall through.
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700272 default:
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700273 HAL_LOGE("Unsupported format for stream %d: %d", i, stream->getFormat());
274 return false;
275 }
276 }
277 }
278
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700279 if (num_input > max_input_streams_ ||
280 num_raw > max_output_streams_[0] ||
281 num_non_stalling > max_output_streams_[1] ||
282 num_stalling > max_output_streams_[2]) {
283 HAL_LOGE("Invalid stream configuration: %d input, %d RAW, %d non-stalling, "
284 "%d stalling (max supported: %d input, %d RAW, %d non-stalling, "
285 "%d stalling)", max_input_streams_, max_output_streams_[0],
286 max_output_streams_[1], max_output_streams_[2], num_input,
287 num_raw, num_non_stalling, num_stalling);
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700288 return false;
289 }
290
291 // TODO(b/29939583): The above logic should be all that's necessary,
292 // but V4L2 doesn't actually support more than 1 stream at a time. So for now,
293 // if not all streams are the same format and size, error. Note that this
294 // means the HAL is not spec-compliant; the requested streams are technically
295 // valid and it is not technically allowed to error once it has reached this
296 // point.
297 int format = streams[0]->getFormat();
298 uint32_t width = streams[0]->getWidth();
299 uint32_t height = streams[0]->getHeight();
300 for (int i = 1; i < count; ++i) {
301 const default_camera_hal::Stream* stream = streams[i];
302 if (stream->getFormat() != format || stream->getWidth() != width ||
303 stream->getHeight() != height) {
304 HAL_LOGE("V4L2 only supports 1 stream configuration at a time "
305 "(stream 0 is format %d, width %u, height %u, "
306 "stream %d is format %d, width %u, height %u).",
307 format, width, height, i, stream->getFormat(),
308 stream->getWidth(), stream->getHeight());
309 return false;
310 }
311 }
312
313 return true;
314}
315
316int V4L2Camera::setupStream(default_camera_hal::Stream* stream,
317 uint32_t* max_buffers) {
318 HAL_LOG_ENTER();
319
320 if (stream->getRotation() != CAMERA3_STREAM_ROTATION_0) {
321 HAL_LOGE("Rotation %d not supported", stream->getRotation());
322 return -EINVAL;
323 }
324
325 // Doesn't matter what was requested, we always use dataspace V0_JFIF.
326 // Note: according to camera3.h, this isn't allowed, but etalvala@google.com
327 // claims it's underdocumented; the implementation lets the HAL overwrite it.
328 stream->setDataSpace(HAL_DATASPACE_V0_JFIF);
329
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700330 int res = device_->SetFormat(*stream, max_buffers);
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700331 if (res) {
332 HAL_LOGE("Failed to set device to correct format for stream.");
333 return res;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700334 }
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700335 // Sanity check.
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700336 if (*max_buffers < 1) {
337 HAL_LOGE("Setting format resulted in an invalid maximum of %u buffers.",
338 *max_buffers);
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700339 return -ENODEV;
340 }
341
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700342 return 0;
343}
344
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700345bool V4L2Camera::isValidCaptureSettings(const android::CameraMetadata& settings) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700346 HAL_LOG_ENTER();
347
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700348 return metadata_->IsValidRequest(settings);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700349}
350
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700351int V4L2Camera::setSettings(const android::CameraMetadata& new_settings) {
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700352 HAL_LOG_ENTER();
353
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700354 return metadata_->SetRequestSettings(new_settings);
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700355}
356
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -0700357} // namespace v4l2_camera_hal