blob: 521f835cd4f2a21ca990cad86db742dcb6dc0a02 [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/stat.h>
Ari Hausman-Cohen9430ad92016-08-24 14:00:32 -070022#include <sys/types.h>
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070023
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.
Ari Hausman-Cohen9430ad92016-08-24 14:00:32 -070039static std::vector<int32_t> getMetadataKeys(const camera_metadata_t* metadata) {
Ari Hausman-Cohendde80172016-07-01 16:20:36 -070040 std::vector<int32_t> keys;
41 size_t num_entries = get_camera_metadata_entry_count(metadata);
42 for (size_t i = 0; i < num_entries; ++i) {
43 camera_metadata_ro_entry_t entry;
44 get_camera_metadata_ro_entry(metadata, i, &entry);
45 keys.push_back(entry.tag);
46 }
47 return keys;
48}
49
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -070050V4L2Camera* V4L2Camera::NewV4L2Camera(int id, const std::string path) {
51 HAL_LOG_ENTER();
52
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070053 std::shared_ptr<V4L2Wrapper> v4l2_wrapper(V4L2Wrapper::NewV4L2Wrapper(path));
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -070054 if (!v4l2_wrapper) {
55 HAL_LOGE("Failed to initialize V4L2 wrapper.");
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -070056 return nullptr;
57 }
58
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070059 std::unique_ptr<Metadata> metadata;
60 int res = GetV4L2Metadata(v4l2_wrapper, &metadata);
61 if (res) {
62 HAL_LOGE("Failed to initialize V4L2 metadata: %d", res);
63 return nullptr;
64 }
65
66 return new V4L2Camera(id, std::move(v4l2_wrapper), std::move(metadata));
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -070067}
68
Ari Hausman-Cohen9430ad92016-08-24 14:00:32 -070069V4L2Camera::V4L2Camera(int id,
70 std::shared_ptr<V4L2Wrapper> v4l2_wrapper,
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070071 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
Ari Hausman-Cohen9430ad92016-08-24 14:00:32 -0700105 // TODO(b/29158098): Inform service of any flashes that are no longer
106 // available 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.
Ari Hausman-Cohen9430ad92016-08-24 14:00:32 -0700129 res = SingleTagValue(
130 *out, ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS, &max_input_streams_);
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700131 if (res) {
132 HAL_LOGE("Failed to get max num input streams from static metadata.");
133 return res;
134 }
Ari Hausman-Cohen9430ad92016-08-24 14:00:32 -0700135 res = SingleTagValue(
136 *out, ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS, &max_output_streams_);
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700137 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,
Ari Hausman-Cohen9430ad92016-08-24 14:00:32 -0700234 int count,
235 uint32_t mode) {
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700236 HAL_LOG_ENTER();
237
238 if (mode != CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE) {
239 HAL_LOGE("Unsupported stream configuration mode: %d", mode);
240 return false;
241 }
242
243 // This should be checked by the caller, but put here as a sanity check.
244 if (count < 1) {
245 HAL_LOGE("Must request at least 1 stream");
246 return false;
247 }
248
249 // Count the number of streams of each type.
250 int32_t num_input = 0;
251 int32_t num_raw = 0;
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700252 int32_t num_stalling = 0;
253 int32_t num_non_stalling = 0;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700254 for (int i = 0; i < count; ++i) {
255 default_camera_hal::Stream* stream = streams[i];
256
257 if (stream->isInputType()) {
258 ++num_input;
259 }
260
261 if (stream->isOutputType()) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700262 StreamFormat format(*stream);
263 switch (format.Category()) {
264 case kFormatCategoryRaw:
265 ++num_raw;
266 case kFormatCategoryStalling:
267 ++num_stalling;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700268 break;
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700269 case kFormatCategoryNonStalling:
270 ++num_non_stalling;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700271 break;
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700272 case kFormatCategoryUnknown: // Fall through.
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700273 default:
Ari Hausman-Cohen9430ad92016-08-24 14:00:32 -0700274 HAL_LOGE(
275 "Unsupported format for stream %d: %d", i, stream->getFormat());
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700276 return false;
277 }
278 }
279 }
280
Ari Hausman-Cohen9430ad92016-08-24 14:00:32 -0700281 if (num_input > max_input_streams_ || num_raw > max_output_streams_[0] ||
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700282 num_non_stalling > max_output_streams_[1] ||
283 num_stalling > max_output_streams_[2]) {
Ari Hausman-Cohen9430ad92016-08-24 14:00:32 -0700284 HAL_LOGE(
285 "Invalid stream configuration: %d input, %d RAW, %d non-stalling, "
286 "%d stalling (max supported: %d input, %d RAW, %d non-stalling, "
287 "%d stalling)",
288 max_input_streams_,
289 max_output_streams_[0],
290 max_output_streams_[1],
291 max_output_streams_[2],
292 num_input,
293 num_raw,
294 num_non_stalling,
295 num_stalling);
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700296 return false;
297 }
298
299 // TODO(b/29939583): The above logic should be all that's necessary,
300 // but V4L2 doesn't actually support more than 1 stream at a time. So for now,
301 // if not all streams are the same format and size, error. Note that this
302 // means the HAL is not spec-compliant; the requested streams are technically
303 // valid and it is not technically allowed to error once it has reached this
304 // point.
305 int format = streams[0]->getFormat();
306 uint32_t width = streams[0]->getWidth();
307 uint32_t height = streams[0]->getHeight();
308 for (int i = 1; i < count; ++i) {
309 const default_camera_hal::Stream* stream = streams[i];
310 if (stream->getFormat() != format || stream->getWidth() != width ||
311 stream->getHeight() != height) {
Ari Hausman-Cohen9430ad92016-08-24 14:00:32 -0700312 HAL_LOGE(
313 "V4L2 only supports 1 stream configuration at a time "
314 "(stream 0 is format %d, width %u, height %u, "
315 "stream %d is format %d, width %u, height %u).",
316 format,
317 width,
318 height,
319 i,
320 stream->getFormat(),
321 stream->getWidth(),
322 stream->getHeight());
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700323 return false;
324 }
325 }
326
327 return true;
328}
329
330int V4L2Camera::setupStream(default_camera_hal::Stream* stream,
331 uint32_t* max_buffers) {
332 HAL_LOG_ENTER();
333
334 if (stream->getRotation() != CAMERA3_STREAM_ROTATION_0) {
335 HAL_LOGE("Rotation %d not supported", stream->getRotation());
336 return -EINVAL;
337 }
338
339 // Doesn't matter what was requested, we always use dataspace V0_JFIF.
340 // Note: according to camera3.h, this isn't allowed, but etalvala@google.com
341 // claims it's underdocumented; the implementation lets the HAL overwrite it.
342 stream->setDataSpace(HAL_DATASPACE_V0_JFIF);
343
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700344 int res = device_->SetFormat(*stream, max_buffers);
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700345 if (res) {
346 HAL_LOGE("Failed to set device to correct format for stream.");
347 return res;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700348 }
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700349 // Sanity check.
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700350 if (*max_buffers < 1) {
351 HAL_LOGE("Setting format resulted in an invalid maximum of %u buffers.",
352 *max_buffers);
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700353 return -ENODEV;
354 }
355
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700356 return 0;
357}
358
Ari Hausman-Cohen9430ad92016-08-24 14:00:32 -0700359bool V4L2Camera::isValidCaptureSettings(
360 const android::CameraMetadata& settings) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700361 HAL_LOG_ENTER();
362
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700363 return metadata_->IsValidRequest(settings);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700364}
365
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700366int V4L2Camera::setSettings(const android::CameraMetadata& new_settings) {
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700367 HAL_LOG_ENTER();
368
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700369 return metadata_->SetRequestSettings(new_settings);
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700370}
371
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -0700372} // namespace v4l2_camera_hal