blob: 5f8674219cbe127d42bd4210536de99ed738a1fe [file] [log] [blame]
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001/*
2 * Copyright (C) 2018 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#define LOG_TAG "ExtCamDevSsn@3.4"
17//#define LOG_NDEBUG 0
Yin-Chia Yehe99cf202018-02-28 11:30:38 -080018#define ATRACE_TAG ATRACE_TAG_CAMERA
Yin-Chia Yeh19030592017-10-19 17:30:11 -070019#include <log/log.h>
20
21#include <inttypes.h>
22#include "ExternalCameraDeviceSession.h"
23
24#include "android-base/macros.h"
Yin-Chia Yeh19030592017-10-19 17:30:11 -070025#include <utils/Timers.h>
Yin-Chia Yehe99cf202018-02-28 11:30:38 -080026#include <utils/Trace.h>
Yin-Chia Yeh19030592017-10-19 17:30:11 -070027#include <linux/videodev2.h>
28#include <sync/sync.h>
29
30#define HAVE_JPEG // required for libyuv.h to export MJPEG decode APIs
31#include <libyuv.h>
32
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -080033#include <jpeglib.h>
34
35
Yin-Chia Yeh19030592017-10-19 17:30:11 -070036namespace android {
37namespace hardware {
38namespace camera {
39namespace device {
40namespace V3_4 {
41namespace implementation {
42
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -080043namespace {
Yin-Chia Yeh19030592017-10-19 17:30:11 -070044// Size of request/result metadata fast message queue. Change to 0 to always use hwbinder buffer.
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -080045static constexpr size_t kMetadataMsgQueueSize = 1 << 18 /* 256kB */;
46
Yin-Chia Yeh19030592017-10-19 17:30:11 -070047const int kBadFramesAfterStreamOn = 1; // drop x frames after streamOn to get rid of some initial
48 // bad frames. TODO: develop a better bad frame detection
49 // method
Yin-Chia Yeh8b699aa2018-02-28 15:58:54 -080050constexpr int MAX_RETRY = 15; // Allow retry some ioctl failures a few times to account for some
51 // webcam showing temporarily ioctl failures.
Yin-Chia Yeh2d61bfd2018-03-14 13:50:23 -070052constexpr int IOCTL_RETRY_SLEEP_US = 33000; // 33ms * MAX_RETRY = 0.5 seconds
Yin-Chia Yeh19030592017-10-19 17:30:11 -070053
Yin-Chia Yeh94f52a32018-03-07 14:46:51 -080054// Constants for tryLock during dumpstate
55static constexpr int kDumpLockRetries = 50;
56static constexpr int kDumpLockSleep = 60000;
57
Yin-Chia Yeh4a3393c2018-02-14 12:47:16 -080058bool tryLock(Mutex& mutex)
59{
Yin-Chia Yeh4a3393c2018-02-14 12:47:16 -080060 bool locked = false;
61 for (int i = 0; i < kDumpLockRetries; ++i) {
62 if (mutex.tryLock() == NO_ERROR) {
63 locked = true;
64 break;
65 }
66 usleep(kDumpLockSleep);
67 }
68 return locked;
69}
70
Yin-Chia Yeh94f52a32018-03-07 14:46:51 -080071bool tryLock(std::mutex& mutex)
72{
73 bool locked = false;
74 for (int i = 0; i < kDumpLockRetries; ++i) {
75 if (mutex.try_lock()) {
76 locked = true;
77 break;
78 }
79 usleep(kDumpLockSleep);
80 }
81 return locked;
82}
83
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -080084} // Anonymous namespace
Yin-Chia Yeh19030592017-10-19 17:30:11 -070085
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -080086// Static instances
87const int ExternalCameraDeviceSession::kMaxProcessedStream;
88const int ExternalCameraDeviceSession::kMaxStallStream;
Yin-Chia Yeh19030592017-10-19 17:30:11 -070089HandleImporter ExternalCameraDeviceSession::sHandleImporter;
90
Yin-Chia Yeh19030592017-10-19 17:30:11 -070091ExternalCameraDeviceSession::ExternalCameraDeviceSession(
92 const sp<ICameraDeviceCallback>& callback,
Yin-Chia Yeh17982492018-02-05 17:41:01 -080093 const ExternalCameraConfig& cfg,
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -080094 const std::vector<SupportedV4L2Format>& sortedFormats,
95 const CroppingType& croppingType,
Yin-Chia Yeh19030592017-10-19 17:30:11 -070096 const common::V1_0::helper::CameraMetadata& chars,
Yin-Chia Yeh4a3393c2018-02-14 12:47:16 -080097 const std::string& cameraId,
Yin-Chia Yeh19030592017-10-19 17:30:11 -070098 unique_fd v4l2Fd) :
99 mCallback(callback),
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -0800100 mCfg(cfg),
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700101 mCameraCharacteristics(chars),
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -0800102 mSupportedFormats(sortedFormats),
103 mCroppingType(croppingType),
Yin-Chia Yeh4a3393c2018-02-14 12:47:16 -0800104 mCameraId(cameraId),
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700105 mV4l2Fd(std::move(v4l2Fd)),
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -0800106 mMaxThumbResolution(getMaxThumbResolution()),
Yin-Chia Yehee238402018-11-04 16:30:11 -0800107 mMaxJpegResolution(getMaxJpegResolution()) {}
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700108
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700109bool ExternalCameraDeviceSession::initialize() {
110 if (mV4l2Fd.get() < 0) {
111 ALOGE("%s: invalid v4l2 device fd %d!", __FUNCTION__, mV4l2Fd.get());
112 return true;
113 }
114
Yin-Chia Yeh2d61bfd2018-03-14 13:50:23 -0700115 struct v4l2_capability capability;
116 int ret = ioctl(mV4l2Fd.get(), VIDIOC_QUERYCAP, &capability);
117 std::string make, model;
118 if (ret < 0) {
119 ALOGW("%s v4l2 QUERYCAP failed", __FUNCTION__);
Yin-Chia Yeh5dab7282020-01-21 10:08:12 -0800120 mExifMake = "Generic UVC webcam";
121 mExifModel = "Generic UVC webcam";
Yin-Chia Yeh2d61bfd2018-03-14 13:50:23 -0700122 } else {
123 // capability.card is UTF-8 encoded
124 char card[32];
125 int j = 0;
126 for (int i = 0; i < 32; i++) {
127 if (capability.card[i] < 128) {
128 card[j++] = capability.card[i];
129 }
130 if (capability.card[i] == '\0') {
131 break;
132 }
133 }
134 if (j == 0 || card[j - 1] != '\0') {
Yin-Chia Yeh5dab7282020-01-21 10:08:12 -0800135 mExifMake = "Generic UVC webcam";
136 mExifModel = "Generic UVC webcam";
Yin-Chia Yeh2d61bfd2018-03-14 13:50:23 -0700137 } else {
Yin-Chia Yeh5dab7282020-01-21 10:08:12 -0800138 mExifMake = card;
139 mExifModel = card;
Yin-Chia Yeh2d61bfd2018-03-14 13:50:23 -0700140 }
141 }
Yin-Chia Yehee238402018-11-04 16:30:11 -0800142
143 initOutputThread();
144 if (mOutputThread == nullptr) {
145 ALOGE("%s: init OutputThread failed!", __FUNCTION__);
146 return true;
147 }
Yin-Chia Yeh5dab7282020-01-21 10:08:12 -0800148 mOutputThread->setExifMakeModel(mExifMake, mExifModel);
Yin-Chia Yeh2d61bfd2018-03-14 13:50:23 -0700149
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700150 status_t status = initDefaultRequests();
151 if (status != OK) {
152 ALOGE("%s: init default requests failed!", __FUNCTION__);
153 return true;
154 }
155
156 mRequestMetadataQueue = std::make_unique<RequestMetadataQueue>(
157 kMetadataMsgQueueSize, false /* non blocking */);
158 if (!mRequestMetadataQueue->isValid()) {
159 ALOGE("%s: invalid request fmq", __FUNCTION__);
160 return true;
161 }
Yin-Chia Yeh5dab7282020-01-21 10:08:12 -0800162 mResultMetadataQueue = std::make_shared<ResultMetadataQueue>(
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700163 kMetadataMsgQueueSize, false /* non blocking */);
164 if (!mResultMetadataQueue->isValid()) {
165 ALOGE("%s: invalid result fmq", __FUNCTION__);
166 return true;
167 }
168
169 // TODO: check is PRIORITY_DISPLAY enough?
170 mOutputThread->run("ExtCamOut", PRIORITY_DISPLAY);
171 return false;
172}
173
Yin-Chia Yehee238402018-11-04 16:30:11 -0800174bool ExternalCameraDeviceSession::isInitFailed() {
175 Mutex::Autolock _l(mLock);
176 if (!mInitialized) {
177 mInitFail = initialize();
178 mInitialized = true;
179 }
180 return mInitFail;
181}
182
183void ExternalCameraDeviceSession::initOutputThread() {
Yin-Chia Yeh5dab7282020-01-21 10:08:12 -0800184 mOutputThread = new OutputThread(this, mCroppingType, mCameraCharacteristics);
Yin-Chia Yehee238402018-11-04 16:30:11 -0800185}
186
187void ExternalCameraDeviceSession::closeOutputThread() {
188 closeOutputThreadImpl();
189}
190
191void ExternalCameraDeviceSession::closeOutputThreadImpl() {
192 if (mOutputThread) {
193 mOutputThread->flush();
194 mOutputThread->requestExit();
195 mOutputThread->join();
196 mOutputThread.clear();
197 }
198}
199
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700200Status ExternalCameraDeviceSession::initStatus() const {
201 Mutex::Autolock _l(mLock);
202 Status status = Status::OK;
203 if (mInitFail || mClosed) {
204 ALOGI("%s: sesssion initFailed %d closed %d", __FUNCTION__, mInitFail, mClosed);
205 status = Status::INTERNAL_ERROR;
206 }
207 return status;
208}
209
210ExternalCameraDeviceSession::~ExternalCameraDeviceSession() {
211 if (!isClosed()) {
212 ALOGE("ExternalCameraDeviceSession deleted before close!");
Yin-Chia Yehee238402018-11-04 16:30:11 -0800213 close(/*callerIsDtor*/true);
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700214 }
215}
216
Yin-Chia Yeh4a3393c2018-02-14 12:47:16 -0800217
218void ExternalCameraDeviceSession::dumpState(const native_handle_t* handle) {
219 if (handle->numFds != 1 || handle->numInts != 0) {
220 ALOGE("%s: handle must contain 1 FD and 0 integers! Got %d FDs and %d ints",
221 __FUNCTION__, handle->numFds, handle->numInts);
222 return;
223 }
224 int fd = handle->data[0];
225
226 bool intfLocked = tryLock(mInterfaceLock);
227 if (!intfLocked) {
228 dprintf(fd, "!! ExternalCameraDeviceSession interface may be deadlocked !!\n");
229 }
230
231 if (isClosed()) {
232 dprintf(fd, "External camera %s is closed\n", mCameraId.c_str());
233 return;
234 }
235
236 bool streaming = false;
237 size_t v4L2BufferCount = 0;
238 SupportedV4L2Format streamingFmt;
Yin-Chia Yeh4a3393c2018-02-14 12:47:16 -0800239 {
Yin-Chia Yeh4a3393c2018-02-14 12:47:16 -0800240 bool sessionLocked = tryLock(mLock);
241 if (!sessionLocked) {
242 dprintf(fd, "!! ExternalCameraDeviceSession mLock may be deadlocked !!\n");
243 }
244 streaming = mV4l2Streaming;
245 streamingFmt = mV4l2StreamingFmt;
246 v4L2BufferCount = mV4L2BufferCount;
Yin-Chia Yeh94f52a32018-03-07 14:46:51 -0800247
Yin-Chia Yeh4a3393c2018-02-14 12:47:16 -0800248 if (sessionLocked) {
249 mLock.unlock();
250 }
251 }
252
Yin-Chia Yeh94f52a32018-03-07 14:46:51 -0800253 std::unordered_set<uint32_t> inflightFrames;
254 {
255 bool iffLocked = tryLock(mInflightFramesLock);
256 if (!iffLocked) {
257 dprintf(fd,
258 "!! ExternalCameraDeviceSession mInflightFramesLock may be deadlocked !!\n");
259 }
260 inflightFrames = mInflightFrames;
261 if (iffLocked) {
262 mInflightFramesLock.unlock();
263 }
264 }
265
Yin-Chia Yeh4a3393c2018-02-14 12:47:16 -0800266 dprintf(fd, "External camera %s V4L2 FD %d, cropping type %s, %s\n",
267 mCameraId.c_str(), mV4l2Fd.get(),
268 (mCroppingType == VERTICAL) ? "vertical" : "horizontal",
269 streaming ? "streaming" : "not streaming");
270 if (streaming) {
271 // TODO: dump fps later
Yin-Chia Yeh3aa9ae92018-02-23 17:21:51 -0800272 dprintf(fd, "Current V4L2 format %c%c%c%c %dx%d @ %ffps\n",
Yin-Chia Yeh4a3393c2018-02-14 12:47:16 -0800273 streamingFmt.fourcc & 0xFF,
274 (streamingFmt.fourcc >> 8) & 0xFF,
275 (streamingFmt.fourcc >> 16) & 0xFF,
276 (streamingFmt.fourcc >> 24) & 0xFF,
Yin-Chia Yeh3aa9ae92018-02-23 17:21:51 -0800277 streamingFmt.width, streamingFmt.height,
278 mV4l2StreamingFps);
Yin-Chia Yeh4a3393c2018-02-14 12:47:16 -0800279
280 size_t numDequeuedV4l2Buffers = 0;
281 {
282 std::lock_guard<std::mutex> lk(mV4l2BufferLock);
283 numDequeuedV4l2Buffers = mNumDequeuedV4l2Buffers;
284 }
285 dprintf(fd, "V4L2 buffer queue size %zu, dequeued %zu\n",
286 v4L2BufferCount, numDequeuedV4l2Buffers);
287 }
288
289 dprintf(fd, "In-flight frames (not sorted):");
290 for (const auto& frameNumber : inflightFrames) {
291 dprintf(fd, "%d, ", frameNumber);
292 }
293 dprintf(fd, "\n");
294 mOutputThread->dump(fd);
295 dprintf(fd, "\n");
296
297 if (intfLocked) {
298 mInterfaceLock.unlock();
299 }
300
301 return;
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700302}
303
304Return<void> ExternalCameraDeviceSession::constructDefaultRequestSettings(
Eino-Ville Talvala658d30d2018-01-18 12:55:07 -0800305 V3_2::RequestTemplate type,
306 V3_2::ICameraDeviceSession::constructDefaultRequestSettings_cb _hidl_cb) {
307 V3_2::CameraMetadata outMetadata;
308 Status status = constructDefaultRequestSettingsRaw(
309 static_cast<RequestTemplate>(type), &outMetadata);
310 _hidl_cb(status, outMetadata);
311 return Void();
312}
313
Eino-Ville Talvala658d30d2018-01-18 12:55:07 -0800314Status ExternalCameraDeviceSession::constructDefaultRequestSettingsRaw(RequestTemplate type,
315 V3_2::CameraMetadata *outMetadata) {
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700316 CameraMetadata emptyMd;
317 Status status = initStatus();
318 if (status != Status::OK) {
Eino-Ville Talvala658d30d2018-01-18 12:55:07 -0800319 return status;
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700320 }
321
322 switch (type) {
323 case RequestTemplate::PREVIEW:
324 case RequestTemplate::STILL_CAPTURE:
325 case RequestTemplate::VIDEO_RECORD:
Eino-Ville Talvala658d30d2018-01-18 12:55:07 -0800326 case RequestTemplate::VIDEO_SNAPSHOT: {
327 *outMetadata = mDefaultRequests[type];
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700328 break;
Eino-Ville Talvala658d30d2018-01-18 12:55:07 -0800329 }
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700330 case RequestTemplate::MANUAL:
331 case RequestTemplate::ZERO_SHUTTER_LAG:
Eino-Ville Talvala0a2a9fc2018-02-05 16:27:15 -0800332 // Don't support MANUAL, ZSL templates
Eino-Ville Talvala658d30d2018-01-18 12:55:07 -0800333 status = Status::ILLEGAL_ARGUMENT;
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700334 break;
335 default:
336 ALOGE("%s: unknown request template type %d", __FUNCTION__, static_cast<int>(type));
Eino-Ville Talvala658d30d2018-01-18 12:55:07 -0800337 status = Status::ILLEGAL_ARGUMENT;
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700338 break;
339 }
Eino-Ville Talvala658d30d2018-01-18 12:55:07 -0800340 return status;
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700341}
342
343Return<void> ExternalCameraDeviceSession::configureStreams(
344 const V3_2::StreamConfiguration& streams,
345 ICameraDeviceSession::configureStreams_cb _hidl_cb) {
346 V3_2::HalStreamConfiguration outStreams;
347 V3_3::HalStreamConfiguration outStreams_v33;
348 Mutex::Autolock _il(mInterfaceLock);
349
350 Status status = configureStreams(streams, &outStreams_v33);
351 size_t size = outStreams_v33.streams.size();
352 outStreams.streams.resize(size);
353 for (size_t i = 0; i < size; i++) {
354 outStreams.streams[i] = outStreams_v33.streams[i].v3_2;
355 }
356 _hidl_cb(status, outStreams);
357 return Void();
358}
359
360Return<void> ExternalCameraDeviceSession::configureStreams_3_3(
361 const V3_2::StreamConfiguration& streams,
362 ICameraDeviceSession::configureStreams_3_3_cb _hidl_cb) {
363 V3_3::HalStreamConfiguration outStreams;
364 Mutex::Autolock _il(mInterfaceLock);
365
366 Status status = configureStreams(streams, &outStreams);
367 _hidl_cb(status, outStreams);
368 return Void();
369}
370
371Return<void> ExternalCameraDeviceSession::configureStreams_3_4(
372 const V3_4::StreamConfiguration& requestedConfiguration,
373 ICameraDeviceSession::configureStreams_3_4_cb _hidl_cb) {
374 V3_2::StreamConfiguration config_v32;
375 V3_3::HalStreamConfiguration outStreams_v33;
Yin-Chia Yeh4c641182018-08-06 15:01:20 -0700376 V3_4::HalStreamConfiguration outStreams;
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700377 Mutex::Autolock _il(mInterfaceLock);
378
379 config_v32.operationMode = requestedConfiguration.operationMode;
380 config_v32.streams.resize(requestedConfiguration.streams.size());
Yin-Chia Yeh4c641182018-08-06 15:01:20 -0700381 uint32_t blobBufferSize = 0;
382 int numStallStream = 0;
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700383 for (size_t i = 0; i < config_v32.streams.size(); i++) {
384 config_v32.streams[i] = requestedConfiguration.streams[i].v3_2;
Yin-Chia Yeh4c641182018-08-06 15:01:20 -0700385 if (config_v32.streams[i].format == PixelFormat::BLOB) {
386 blobBufferSize = requestedConfiguration.streams[i].bufferSize;
387 numStallStream++;
388 }
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700389 }
390
Yin-Chia Yeh4c641182018-08-06 15:01:20 -0700391 // Fail early if there are multiple BLOB streams
392 if (numStallStream > kMaxStallStream) {
393 ALOGE("%s: too many stall streams (expect <= %d, got %d)", __FUNCTION__,
394 kMaxStallStream, numStallStream);
395 _hidl_cb(Status::ILLEGAL_ARGUMENT, outStreams);
396 return Void();
397 }
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700398
Yin-Chia Yeh4c641182018-08-06 15:01:20 -0700399 Status status = configureStreams(config_v32, &outStreams_v33, blobBufferSize);
400
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700401 outStreams.streams.resize(outStreams_v33.streams.size());
402 for (size_t i = 0; i < outStreams.streams.size(); i++) {
403 outStreams.streams[i].v3_3 = outStreams_v33.streams[i];
404 }
405 _hidl_cb(status, outStreams);
406 return Void();
407}
408
409Return<void> ExternalCameraDeviceSession::getCaptureRequestMetadataQueue(
410 ICameraDeviceSession::getCaptureRequestMetadataQueue_cb _hidl_cb) {
411 Mutex::Autolock _il(mInterfaceLock);
412 _hidl_cb(*mRequestMetadataQueue->getDesc());
413 return Void();
414}
415
416Return<void> ExternalCameraDeviceSession::getCaptureResultMetadataQueue(
417 ICameraDeviceSession::getCaptureResultMetadataQueue_cb _hidl_cb) {
418 Mutex::Autolock _il(mInterfaceLock);
419 _hidl_cb(*mResultMetadataQueue->getDesc());
420 return Void();
421}
422
423Return<void> ExternalCameraDeviceSession::processCaptureRequest(
424 const hidl_vec<CaptureRequest>& requests,
425 const hidl_vec<BufferCache>& cachesToRemove,
426 ICameraDeviceSession::processCaptureRequest_cb _hidl_cb) {
427 Mutex::Autolock _il(mInterfaceLock);
428 updateBufferCaches(cachesToRemove);
429
430 uint32_t numRequestProcessed = 0;
431 Status s = Status::OK;
432 for (size_t i = 0; i < requests.size(); i++, numRequestProcessed++) {
433 s = processOneCaptureRequest(requests[i]);
434 if (s != Status::OK) {
435 break;
436 }
437 }
438
439 _hidl_cb(s, numRequestProcessed);
440 return Void();
441}
442
443Return<void> ExternalCameraDeviceSession::processCaptureRequest_3_4(
444 const hidl_vec<V3_4::CaptureRequest>& requests,
445 const hidl_vec<V3_2::BufferCache>& cachesToRemove,
446 ICameraDeviceSession::processCaptureRequest_3_4_cb _hidl_cb) {
447 Mutex::Autolock _il(mInterfaceLock);
448 updateBufferCaches(cachesToRemove);
449
450 uint32_t numRequestProcessed = 0;
451 Status s = Status::OK;
452 for (size_t i = 0; i < requests.size(); i++, numRequestProcessed++) {
453 s = processOneCaptureRequest(requests[i].v3_2);
454 if (s != Status::OK) {
455 break;
456 }
457 }
458
459 _hidl_cb(s, numRequestProcessed);
460 return Void();
461}
462
463Return<Status> ExternalCameraDeviceSession::flush() {
Yin-Chia Yehe99cf202018-02-28 11:30:38 -0800464 ATRACE_CALL();
Yin-Chia Yeh190e5602018-02-13 17:43:13 -0800465 Mutex::Autolock _il(mInterfaceLock);
466 Status status = initStatus();
467 if (status != Status::OK) {
468 return status;
469 }
470 mOutputThread->flush();
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700471 return Status::OK;
472}
473
Yin-Chia Yehee238402018-11-04 16:30:11 -0800474Return<void> ExternalCameraDeviceSession::close(bool callerIsDtor) {
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700475 Mutex::Autolock _il(mInterfaceLock);
Yin-Chia Yeh4a3393c2018-02-14 12:47:16 -0800476 bool closed = isClosed();
477 if (!closed) {
Yin-Chia Yehee238402018-11-04 16:30:11 -0800478 if (callerIsDtor) {
479 closeOutputThreadImpl();
480 } else {
481 closeOutputThread();
482 }
Yin-Chia Yeh4a3393c2018-02-14 12:47:16 -0800483
484 Mutex::Autolock _l(mLock);
485 // free all buffers
Yin-Chia Yehee238402018-11-04 16:30:11 -0800486 {
487 Mutex::Autolock _l(mCbsLock);
488 for(auto pair : mStreamMap) {
489 cleanupBuffersLocked(/*Stream ID*/pair.first);
490 }
Yin-Chia Yeh4a3393c2018-02-14 12:47:16 -0800491 }
492 v4l2StreamOffLocked();
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700493 ALOGV("%s: closing V4L2 camera FD %d", __FUNCTION__, mV4l2Fd.get());
494 mV4l2Fd.reset();
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700495 mClosed = true;
496 }
497 return Void();
498}
499
Yin-Chia Yehee238402018-11-04 16:30:11 -0800500Status ExternalCameraDeviceSession::importRequestLocked(
501 const CaptureRequest& request,
502 hidl_vec<buffer_handle_t*>& allBufPtrs,
503 hidl_vec<int>& allFences) {
504 return importRequestLockedImpl(request, allBufPtrs, allFences);
505}
506
507Status ExternalCameraDeviceSession::importBuffer(int32_t streamId,
508 uint64_t bufId, buffer_handle_t buf,
509 /*out*/buffer_handle_t** outBufPtr,
510 bool allowEmptyBuf) {
511 Mutex::Autolock _l(mCbsLock);
512 return importBufferLocked(streamId, bufId, buf, outBufPtr, allowEmptyBuf);
513}
514
515Status ExternalCameraDeviceSession::importBufferLocked(int32_t streamId,
516 uint64_t bufId, buffer_handle_t buf,
517 /*out*/buffer_handle_t** outBufPtr,
518 bool allowEmptyBuf) {
Yin-Chia Yeh5dab7282020-01-21 10:08:12 -0800519 return importBufferImpl(
520 mCirculatingBuffers, sHandleImporter, streamId,
521 bufId, buf, outBufPtr, allowEmptyBuf);
Yin-Chia Yehee238402018-11-04 16:30:11 -0800522}
523
524Status ExternalCameraDeviceSession::importRequestLockedImpl(
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700525 const CaptureRequest& request,
526 hidl_vec<buffer_handle_t*>& allBufPtrs,
Yin-Chia Yehee238402018-11-04 16:30:11 -0800527 hidl_vec<int>& allFences,
528 bool allowEmptyBuf) {
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700529 size_t numOutputBufs = request.outputBuffers.size();
530 size_t numBufs = numOutputBufs;
531 // Validate all I/O buffers
532 hidl_vec<buffer_handle_t> allBufs;
533 hidl_vec<uint64_t> allBufIds;
534 allBufs.resize(numBufs);
535 allBufIds.resize(numBufs);
536 allBufPtrs.resize(numBufs);
537 allFences.resize(numBufs);
538 std::vector<int32_t> streamIds(numBufs);
539
540 for (size_t i = 0; i < numOutputBufs; i++) {
541 allBufs[i] = request.outputBuffers[i].buffer.getNativeHandle();
542 allBufIds[i] = request.outputBuffers[i].bufferId;
543 allBufPtrs[i] = &allBufs[i];
544 streamIds[i] = request.outputBuffers[i].streamId;
545 }
546
Yin-Chia Yehee238402018-11-04 16:30:11 -0800547 {
548 Mutex::Autolock _l(mCbsLock);
549 for (size_t i = 0; i < numBufs; i++) {
550 Status st = importBufferLocked(
551 streamIds[i], allBufIds[i], allBufs[i], &allBufPtrs[i],
552 allowEmptyBuf);
553 if (st != Status::OK) {
554 // Detailed error logs printed in importBuffer
555 return st;
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700556 }
557 }
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700558 }
559
560 // All buffers are imported. Now validate output buffer acquire fences
561 for (size_t i = 0; i < numOutputBufs; i++) {
562 if (!sHandleImporter.importFence(
563 request.outputBuffers[i].acquireFence, allFences[i])) {
564 ALOGE("%s: output buffer %zu acquire fence is invalid", __FUNCTION__, i);
565 cleanupInflightFences(allFences, i);
566 return Status::INTERNAL_ERROR;
567 }
568 }
569 return Status::OK;
570}
571
572void ExternalCameraDeviceSession::cleanupInflightFences(
573 hidl_vec<int>& allFences, size_t numFences) {
574 for (size_t j = 0; j < numFences; j++) {
575 sHandleImporter.closeFence(allFences[j]);
576 }
577}
578
Yin-Chia Yeh3aa9ae92018-02-23 17:21:51 -0800579int ExternalCameraDeviceSession::waitForV4L2BufferReturnLocked(std::unique_lock<std::mutex>& lk) {
Yin-Chia Yeh94f52a32018-03-07 14:46:51 -0800580 ATRACE_CALL();
Yin-Chia Yeh3aa9ae92018-02-23 17:21:51 -0800581 std::chrono::seconds timeout = std::chrono::seconds(kBufferWaitTimeoutSec);
582 mLock.unlock();
583 auto st = mV4L2BufferReturned.wait_for(lk, timeout);
584 // Here we introduce a order where mV4l2BufferLock is acquired before mLock, while
585 // the normal lock acquisition order is reversed. This is fine because in most of
586 // cases we are protected by mInterfaceLock. The only thread that can cause deadlock
587 // is the OutputThread, where we do need to make sure we don't acquire mLock then
588 // mV4l2BufferLock
589 mLock.lock();
590 if (st == std::cv_status::timeout) {
591 ALOGE("%s: wait for V4L2 buffer return timeout!", __FUNCTION__);
592 return -1;
593 }
594 return 0;
595}
596
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700597Status ExternalCameraDeviceSession::processOneCaptureRequest(const CaptureRequest& request) {
Yin-Chia Yehe99cf202018-02-28 11:30:38 -0800598 ATRACE_CALL();
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700599 Status status = initStatus();
600 if (status != Status::OK) {
601 return status;
602 }
603
604 if (request.inputBuffer.streamId != -1) {
605 ALOGE("%s: external camera does not support reprocessing!", __FUNCTION__);
606 return Status::ILLEGAL_ARGUMENT;
607 }
608
609 Mutex::Autolock _l(mLock);
610 if (!mV4l2Streaming) {
611 ALOGE("%s: cannot process request in streamOff state!", __FUNCTION__);
612 return Status::INTERNAL_ERROR;
613 }
614
615 const camera_metadata_t *rawSettings = nullptr;
616 bool converted = true;
617 CameraMetadata settingsFmq; // settings from FMQ
618 if (request.fmqSettingsSize > 0) {
619 // non-blocking read; client must write metadata before calling
620 // processOneCaptureRequest
621 settingsFmq.resize(request.fmqSettingsSize);
622 bool read = mRequestMetadataQueue->read(settingsFmq.data(), request.fmqSettingsSize);
623 if (read) {
624 converted = V3_2::implementation::convertFromHidl(settingsFmq, &rawSettings);
625 } else {
626 ALOGE("%s: capture request settings metadata couldn't be read from fmq!", __FUNCTION__);
627 converted = false;
628 }
629 } else {
630 converted = V3_2::implementation::convertFromHidl(request.settings, &rawSettings);
631 }
632
633 if (converted && rawSettings != nullptr) {
634 mLatestReqSetting = rawSettings;
635 }
636
637 if (!converted) {
638 ALOGE("%s: capture request settings metadata is corrupt!", __FUNCTION__);
639 return Status::ILLEGAL_ARGUMENT;
640 }
641
642 if (mFirstRequest && rawSettings == nullptr) {
643 ALOGE("%s: capture request settings must not be null for first request!",
644 __FUNCTION__);
645 return Status::ILLEGAL_ARGUMENT;
646 }
647
648 hidl_vec<buffer_handle_t*> allBufPtrs;
649 hidl_vec<int> allFences;
650 size_t numOutputBufs = request.outputBuffers.size();
651
652 if (numOutputBufs == 0) {
653 ALOGE("%s: capture request must have at least one output buffer!", __FUNCTION__);
654 return Status::ILLEGAL_ARGUMENT;
655 }
656
Yin-Chia Yeh3aa9ae92018-02-23 17:21:51 -0800657 camera_metadata_entry fpsRange = mLatestReqSetting.find(ANDROID_CONTROL_AE_TARGET_FPS_RANGE);
658 if (fpsRange.count == 2) {
659 double requestFpsMax = fpsRange.data.i32[1];
660 double closestFps = 0.0;
661 double fpsError = 1000.0;
662 bool fpsSupported = false;
663 for (const auto& fr : mV4l2StreamingFmt.frameRates) {
664 double f = fr.getDouble();
665 if (std::fabs(requestFpsMax - f) < 1.0) {
666 fpsSupported = true;
667 break;
668 }
669 if (std::fabs(requestFpsMax - f) < fpsError) {
670 fpsError = std::fabs(requestFpsMax - f);
671 closestFps = f;
672 }
673 }
674 if (!fpsSupported) {
675 /* This can happen in a few scenarios:
676 * 1. The application is sending a FPS range not supported by the configured outputs.
677 * 2. The application is sending a valid FPS range for all cofigured outputs, but
678 * the selected V4L2 size can only run at slower speed. This should be very rare
679 * though: for this to happen a sensor needs to support at least 3 different aspect
680 * ratio outputs, and when (at least) two outputs are both not the main aspect ratio
681 * of the webcam, a third size that's larger might be picked and runs into this
682 * issue.
683 */
684 ALOGW("%s: cannot reach fps %d! Will do %f instead",
685 __FUNCTION__, fpsRange.data.i32[1], closestFps);
686 requestFpsMax = closestFps;
687 }
688
689 if (requestFpsMax != mV4l2StreamingFps) {
690 {
691 std::unique_lock<std::mutex> lk(mV4l2BufferLock);
692 while (mNumDequeuedV4l2Buffers != 0) {
693 // Wait until pipeline is idle before reconfigure stream
694 int waitRet = waitForV4L2BufferReturnLocked(lk);
695 if (waitRet != 0) {
696 ALOGE("%s: wait for pipeline idle failed!", __FUNCTION__);
697 return Status::INTERNAL_ERROR;
698 }
699 }
700 }
701 configureV4l2StreamLocked(mV4l2StreamingFmt, requestFpsMax);
702 }
703 }
704
Yin-Chia Yehee238402018-11-04 16:30:11 -0800705 status = importRequestLocked(request, allBufPtrs, allFences);
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700706 if (status != Status::OK) {
707 return status;
708 }
709
Yin-Chia Yeh4a3393c2018-02-14 12:47:16 -0800710 nsecs_t shutterTs = 0;
711 sp<V4L2Frame> frameIn = dequeueV4l2FrameLocked(&shutterTs);
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700712 if ( frameIn == nullptr) {
713 ALOGE("%s: V4L2 deque frame failed!", __FUNCTION__);
714 return Status::INTERNAL_ERROR;
715 }
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700716
Yin-Chia Yehe086fb72018-02-16 14:54:04 -0800717 std::shared_ptr<HalRequest> halReq = std::make_shared<HalRequest>();
718 halReq->frameNumber = request.frameNumber;
719 halReq->setting = mLatestReqSetting;
720 halReq->frameIn = frameIn;
721 halReq->shutterTs = shutterTs;
722 halReq->buffers.resize(numOutputBufs);
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700723 for (size_t i = 0; i < numOutputBufs; i++) {
Yin-Chia Yehe086fb72018-02-16 14:54:04 -0800724 HalStreamBuffer& halBuf = halReq->buffers[i];
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700725 int streamId = halBuf.streamId = request.outputBuffers[i].streamId;
726 halBuf.bufferId = request.outputBuffers[i].bufferId;
727 const Stream& stream = mStreamMap[streamId];
728 halBuf.width = stream.width;
729 halBuf.height = stream.height;
730 halBuf.format = stream.format;
731 halBuf.usage = stream.usage;
732 halBuf.bufPtr = allBufPtrs[i];
733 halBuf.acquireFence = allFences[i];
734 halBuf.fenceTimeout = false;
735 }
Yin-Chia Yeh94f52a32018-03-07 14:46:51 -0800736 {
737 std::lock_guard<std::mutex> lk(mInflightFramesLock);
738 mInflightFrames.insert(halReq->frameNumber);
739 }
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700740 // Send request to OutputThread for the rest of processing
741 mOutputThread->submitRequest(halReq);
742 mFirstRequest = false;
743 return Status::OK;
744}
745
746void ExternalCameraDeviceSession::notifyShutter(uint32_t frameNumber, nsecs_t shutterTs) {
747 NotifyMsg msg;
748 msg.type = MsgType::SHUTTER;
749 msg.msg.shutter.frameNumber = frameNumber;
750 msg.msg.shutter.timestamp = shutterTs;
751 mCallback->notify({msg});
752}
753
754void ExternalCameraDeviceSession::notifyError(
755 uint32_t frameNumber, int32_t streamId, ErrorCode ec) {
756 NotifyMsg msg;
757 msg.type = MsgType::ERROR;
758 msg.msg.error.frameNumber = frameNumber;
759 msg.msg.error.errorStreamId = streamId;
760 msg.msg.error.errorCode = ec;
761 mCallback->notify({msg});
762}
763
764//TODO: refactor with processCaptureResult
Yin-Chia Yehe086fb72018-02-16 14:54:04 -0800765Status ExternalCameraDeviceSession::processCaptureRequestError(
Yin-Chia Yeh5dab7282020-01-21 10:08:12 -0800766 const std::shared_ptr<HalRequest>& req,
767 /*out*/std::vector<NotifyMsg>* outMsgs,
768 /*out*/std::vector<CaptureResult>* outResults) {
Yin-Chia Yehe99cf202018-02-28 11:30:38 -0800769 ATRACE_CALL();
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700770 // Return V4L2 buffer to V4L2 buffer queue
Yin-Chia Yeh5dab7282020-01-21 10:08:12 -0800771 sp<V3_4::implementation::V4L2Frame> v4l2Frame =
772 static_cast<V3_4::implementation::V4L2Frame*>(req->frameIn.get());
773 enqueueV4l2Frame(v4l2Frame);
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700774
Yin-Chia Yeh5dab7282020-01-21 10:08:12 -0800775 if (outMsgs == nullptr) {
776 notifyShutter(req->frameNumber, req->shutterTs);
777 notifyError(/*frameNum*/req->frameNumber, /*stream*/-1, ErrorCode::ERROR_REQUEST);
778 } else {
779 NotifyMsg shutter;
780 shutter.type = MsgType::SHUTTER;
781 shutter.msg.shutter.frameNumber = req->frameNumber;
782 shutter.msg.shutter.timestamp = req->shutterTs;
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700783
Yin-Chia Yeh5dab7282020-01-21 10:08:12 -0800784 NotifyMsg error;
785 error.type = MsgType::ERROR;
786 error.msg.error.frameNumber = req->frameNumber;
787 error.msg.error.errorStreamId = -1;
788 error.msg.error.errorCode = ErrorCode::ERROR_REQUEST;
789 outMsgs->push_back(shutter);
790 outMsgs->push_back(error);
791 }
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700792
793 // Fill output buffers
794 hidl_vec<CaptureResult> results;
795 results.resize(1);
796 CaptureResult& result = results[0];
Yin-Chia Yehe086fb72018-02-16 14:54:04 -0800797 result.frameNumber = req->frameNumber;
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700798 result.partialResult = 1;
799 result.inputBuffer.streamId = -1;
Yin-Chia Yehe086fb72018-02-16 14:54:04 -0800800 result.outputBuffers.resize(req->buffers.size());
801 for (size_t i = 0; i < req->buffers.size(); i++) {
802 result.outputBuffers[i].streamId = req->buffers[i].streamId;
803 result.outputBuffers[i].bufferId = req->buffers[i].bufferId;
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700804 result.outputBuffers[i].status = BufferStatus::ERROR;
Yin-Chia Yehe086fb72018-02-16 14:54:04 -0800805 if (req->buffers[i].acquireFence >= 0) {
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700806 native_handle_t* handle = native_handle_create(/*numFds*/1, /*numInts*/0);
Yin-Chia Yehe086fb72018-02-16 14:54:04 -0800807 handle->data[0] = req->buffers[i].acquireFence;
Yin-Chia Yeh1e089662018-02-02 15:57:58 -0800808 result.outputBuffers[i].releaseFence.setTo(handle, /*shouldOwn*/false);
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700809 }
810 }
811
812 // update inflight records
813 {
Yin-Chia Yeh94f52a32018-03-07 14:46:51 -0800814 std::lock_guard<std::mutex> lk(mInflightFramesLock);
Yin-Chia Yehe086fb72018-02-16 14:54:04 -0800815 mInflightFrames.erase(req->frameNumber);
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700816 }
817
Yin-Chia Yeh5dab7282020-01-21 10:08:12 -0800818 if (outResults == nullptr) {
819 // Callback into framework
820 invokeProcessCaptureResultCallback(results, /* tryWriteFmq */true);
821 freeReleaseFences(results);
822 } else {
823 outResults->push_back(result);
824 }
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700825 return Status::OK;
826}
827
Yin-Chia Yehe086fb72018-02-16 14:54:04 -0800828Status ExternalCameraDeviceSession::processCaptureResult(std::shared_ptr<HalRequest>& req) {
Yin-Chia Yehe99cf202018-02-28 11:30:38 -0800829 ATRACE_CALL();
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700830 // Return V4L2 buffer to V4L2 buffer queue
Yin-Chia Yeh5dab7282020-01-21 10:08:12 -0800831 sp<V3_4::implementation::V4L2Frame> v4l2Frame =
832 static_cast<V3_4::implementation::V4L2Frame*>(req->frameIn.get());
833 enqueueV4l2Frame(v4l2Frame);
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700834
835 // NotifyShutter
Yin-Chia Yehe086fb72018-02-16 14:54:04 -0800836 notifyShutter(req->frameNumber, req->shutterTs);
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700837
838 // Fill output buffers
839 hidl_vec<CaptureResult> results;
840 results.resize(1);
841 CaptureResult& result = results[0];
Yin-Chia Yehe086fb72018-02-16 14:54:04 -0800842 result.frameNumber = req->frameNumber;
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700843 result.partialResult = 1;
844 result.inputBuffer.streamId = -1;
Yin-Chia Yehe086fb72018-02-16 14:54:04 -0800845 result.outputBuffers.resize(req->buffers.size());
846 for (size_t i = 0; i < req->buffers.size(); i++) {
847 result.outputBuffers[i].streamId = req->buffers[i].streamId;
848 result.outputBuffers[i].bufferId = req->buffers[i].bufferId;
849 if (req->buffers[i].fenceTimeout) {
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700850 result.outputBuffers[i].status = BufferStatus::ERROR;
Yin-Chia Yeh965d9dd2019-03-14 14:21:01 -0700851 if (req->buffers[i].acquireFence >= 0) {
Yin-Chia Yehee238402018-11-04 16:30:11 -0800852 native_handle_t* handle = native_handle_create(/*numFds*/1, /*numInts*/0);
853 handle->data[0] = req->buffers[i].acquireFence;
854 result.outputBuffers[i].releaseFence.setTo(handle, /*shouldOwn*/false);
855 }
Yin-Chia Yehe086fb72018-02-16 14:54:04 -0800856 notifyError(req->frameNumber, req->buffers[i].streamId, ErrorCode::ERROR_BUFFER);
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700857 } else {
858 result.outputBuffers[i].status = BufferStatus::OK;
859 // TODO: refactor
Yin-Chia Yeh965d9dd2019-03-14 14:21:01 -0700860 if (req->buffers[i].acquireFence >= 0) {
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700861 native_handle_t* handle = native_handle_create(/*numFds*/1, /*numInts*/0);
Yin-Chia Yehe086fb72018-02-16 14:54:04 -0800862 handle->data[0] = req->buffers[i].acquireFence;
Yin-Chia Yeh1e089662018-02-02 15:57:58 -0800863 result.outputBuffers[i].releaseFence.setTo(handle, /*shouldOwn*/false);
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700864 }
865 }
866 }
867
868 // Fill capture result metadata
Yin-Chia Yehe086fb72018-02-16 14:54:04 -0800869 fillCaptureResult(req->setting, req->shutterTs);
870 const camera_metadata_t *rawResult = req->setting.getAndLock();
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700871 V3_2::implementation::convertToHidl(rawResult, &result.result);
Yin-Chia Yehe086fb72018-02-16 14:54:04 -0800872 req->setting.unlock(rawResult);
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700873
874 // update inflight records
875 {
Yin-Chia Yeh94f52a32018-03-07 14:46:51 -0800876 std::lock_guard<std::mutex> lk(mInflightFramesLock);
Yin-Chia Yehe086fb72018-02-16 14:54:04 -0800877 mInflightFrames.erase(req->frameNumber);
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700878 }
879
880 // Callback into framework
881 invokeProcessCaptureResultCallback(results, /* tryWriteFmq */true);
882 freeReleaseFences(results);
883 return Status::OK;
884}
885
886void ExternalCameraDeviceSession::invokeProcessCaptureResultCallback(
887 hidl_vec<CaptureResult> &results, bool tryWriteFmq) {
888 if (mProcessCaptureResultLock.tryLock() != OK) {
889 const nsecs_t NS_TO_SECOND = 1000000000;
890 ALOGV("%s: previous call is not finished! waiting 1s...", __FUNCTION__);
891 if (mProcessCaptureResultLock.timedLock(/* 1s */NS_TO_SECOND) != OK) {
892 ALOGE("%s: cannot acquire lock in 1s, cannot proceed",
893 __FUNCTION__);
894 return;
895 }
896 }
897 if (tryWriteFmq && mResultMetadataQueue->availableToWrite() > 0) {
898 for (CaptureResult &result : results) {
899 if (result.result.size() > 0) {
900 if (mResultMetadataQueue->write(result.result.data(), result.result.size())) {
901 result.fmqResultSize = result.result.size();
902 result.result.resize(0);
903 } else {
904 ALOGW("%s: couldn't utilize fmq, fall back to hwbinder", __FUNCTION__);
905 result.fmqResultSize = 0;
906 }
907 } else {
908 result.fmqResultSize = 0;
909 }
910 }
911 }
Yuriy Romanenko9cdd6f92018-01-31 15:59:20 -0800912 auto status = mCallback->processCaptureResult(results);
913 if (!status.isOk()) {
914 ALOGE("%s: processCaptureResult ERROR : %s", __FUNCTION__,
915 status.description().c_str());
916 }
917
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700918 mProcessCaptureResultLock.unlock();
919}
920
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700921ExternalCameraDeviceSession::OutputThread::OutputThread(
Yin-Chia Yeh5dab7282020-01-21 10:08:12 -0800922 wp<OutputThreadInterface> parent, CroppingType ct,
923 const common::V1_0::helper::CameraMetadata& chars) :
924 mParent(parent), mCroppingType(ct), mCameraCharacteristics(chars) {}
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700925
926ExternalCameraDeviceSession::OutputThread::~OutputThread() {}
927
Yin-Chia Yeh2d61bfd2018-03-14 13:50:23 -0700928void ExternalCameraDeviceSession::OutputThread::setExifMakeModel(
929 const std::string& make, const std::string& model) {
930 mExifMake = make;
931 mExifModel = model;
932}
933
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700934int ExternalCameraDeviceSession::OutputThread::cropAndScaleLocked(
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -0800935 sp<AllocatedFrame>& in, const Size& outSz, YCbCrLayout* out) {
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700936 Size inSz = {in->mWidth, in->mHeight};
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -0800937
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700938 int ret;
939 if (inSz == outSz) {
940 ret = in->getLayout(out);
941 if (ret != 0) {
942 ALOGE("%s: failed to get input image layout", __FUNCTION__);
943 return ret;
944 }
945 return ret;
946 }
947
948 // Cropping to output aspect ratio
949 IMapper::Rect inputCrop;
950 ret = getCropRect(mCroppingType, inSz, outSz, &inputCrop);
951 if (ret != 0) {
952 ALOGE("%s: failed to compute crop rect for output size %dx%d",
953 __FUNCTION__, outSz.width, outSz.height);
954 return ret;
955 }
956
957 YCbCrLayout croppedLayout;
958 ret = in->getCroppedLayout(inputCrop, &croppedLayout);
959 if (ret != 0) {
960 ALOGE("%s: failed to crop input image %dx%d to output size %dx%d",
961 __FUNCTION__, inSz.width, inSz.height, outSz.width, outSz.height);
962 return ret;
963 }
964
965 if ((mCroppingType == VERTICAL && inSz.width == outSz.width) ||
966 (mCroppingType == HORIZONTAL && inSz.height == outSz.height)) {
967 // No scale is needed
968 *out = croppedLayout;
969 return 0;
970 }
971
972 auto it = mScaledYu12Frames.find(outSz);
973 sp<AllocatedFrame> scaledYu12Buf;
974 if (it != mScaledYu12Frames.end()) {
975 scaledYu12Buf = it->second;
976 } else {
977 it = mIntermediateBuffers.find(outSz);
978 if (it == mIntermediateBuffers.end()) {
979 ALOGE("%s: failed to find intermediate buffer size %dx%d",
980 __FUNCTION__, outSz.width, outSz.height);
981 return -1;
982 }
983 scaledYu12Buf = it->second;
984 }
985 // Scale
986 YCbCrLayout outLayout;
987 ret = scaledYu12Buf->getLayout(&outLayout);
988 if (ret != 0) {
989 ALOGE("%s: failed to get output buffer layout", __FUNCTION__);
990 return ret;
991 }
992
993 ret = libyuv::I420Scale(
994 static_cast<uint8_t*>(croppedLayout.y),
995 croppedLayout.yStride,
996 static_cast<uint8_t*>(croppedLayout.cb),
997 croppedLayout.cStride,
998 static_cast<uint8_t*>(croppedLayout.cr),
999 croppedLayout.cStride,
1000 inputCrop.width,
1001 inputCrop.height,
1002 static_cast<uint8_t*>(outLayout.y),
1003 outLayout.yStride,
1004 static_cast<uint8_t*>(outLayout.cb),
1005 outLayout.cStride,
1006 static_cast<uint8_t*>(outLayout.cr),
1007 outLayout.cStride,
1008 outSz.width,
1009 outSz.height,
1010 // TODO: b/72261744 see if we can use better filter without losing too much perf
1011 libyuv::FilterMode::kFilterNone);
1012
1013 if (ret != 0) {
1014 ALOGE("%s: failed to scale buffer from %dx%d to %dx%d. Ret %d",
1015 __FUNCTION__, inputCrop.width, inputCrop.height,
1016 outSz.width, outSz.height, ret);
1017 return ret;
1018 }
1019
1020 *out = outLayout;
1021 mScaledYu12Frames.insert({outSz, scaledYu12Buf});
1022 return 0;
1023}
1024
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08001025
1026int ExternalCameraDeviceSession::OutputThread::cropAndScaleThumbLocked(
1027 sp<AllocatedFrame>& in, const Size &outSz, YCbCrLayout* out) {
1028 Size inSz {in->mWidth, in->mHeight};
1029
1030 if ((outSz.width * outSz.height) >
1031 (mYu12ThumbFrame->mWidth * mYu12ThumbFrame->mHeight)) {
1032 ALOGE("%s: Requested thumbnail size too big (%d,%d) > (%d,%d)",
1033 __FUNCTION__, outSz.width, outSz.height,
1034 mYu12ThumbFrame->mWidth, mYu12ThumbFrame->mHeight);
1035 return -1;
1036 }
1037
1038 int ret;
1039
1040 /* This will crop-and-zoom the input YUV frame to the thumbnail size
1041 * Based on the following logic:
1042 * 1) Square pixels come in, square pixels come out, therefore single
1043 * scale factor is computed to either make input bigger or smaller
1044 * depending on if we are upscaling or downscaling
1045 * 2) That single scale factor would either make height too tall or width
1046 * too wide so we need to crop the input either horizontally or vertically
1047 * but not both
1048 */
1049
1050 /* Convert the input and output dimensions into floats for ease of math */
1051 float fWin = static_cast<float>(inSz.width);
1052 float fHin = static_cast<float>(inSz.height);
1053 float fWout = static_cast<float>(outSz.width);
1054 float fHout = static_cast<float>(outSz.height);
1055
1056 /* Compute the one scale factor from (1) above, it will be the smaller of
1057 * the two possibilities. */
1058 float scaleFactor = std::min( fHin / fHout, fWin / fWout );
1059
1060 /* Since we are crop-and-zooming (as opposed to letter/pillar boxing) we can
1061 * simply multiply the output by our scaleFactor to get the cropped input
1062 * size. Note that at least one of {fWcrop, fHcrop} is going to wind up
1063 * being {fWin, fHin} respectively because fHout or fWout cancels out the
1064 * scaleFactor calculation above.
1065 *
1066 * Specifically:
1067 * if ( fHin / fHout ) < ( fWin / fWout ) we crop the sides off
1068 * input, in which case
1069 * scaleFactor = fHin / fHout
1070 * fWcrop = fHin / fHout * fWout
1071 * fHcrop = fHin
1072 *
1073 * Note that fWcrop <= fWin ( because ( fHin / fHout ) * fWout < fWin, which
1074 * is just the inequality above with both sides multiplied by fWout
1075 *
1076 * on the other hand if ( fWin / fWout ) < ( fHin / fHout) we crop the top
1077 * and the bottom off of input, and
1078 * scaleFactor = fWin / fWout
1079 * fWcrop = fWin
1080 * fHCrop = fWin / fWout * fHout
1081 */
1082 float fWcrop = scaleFactor * fWout;
1083 float fHcrop = scaleFactor * fHout;
1084
1085 /* Convert to integer and truncate to an even number */
1086 Size cropSz = { 2*static_cast<uint32_t>(fWcrop/2.0f),
1087 2*static_cast<uint32_t>(fHcrop/2.0f) };
1088
1089 /* Convert to a centered rectange with even top/left */
1090 IMapper::Rect inputCrop {
1091 2*static_cast<int32_t>((inSz.width - cropSz.width)/4),
1092 2*static_cast<int32_t>((inSz.height - cropSz.height)/4),
1093 static_cast<int32_t>(cropSz.width),
1094 static_cast<int32_t>(cropSz.height) };
1095
1096 if ((inputCrop.top < 0) ||
1097 (inputCrop.top >= static_cast<int32_t>(inSz.height)) ||
1098 (inputCrop.left < 0) ||
1099 (inputCrop.left >= static_cast<int32_t>(inSz.width)) ||
1100 (inputCrop.width <= 0) ||
1101 (inputCrop.width + inputCrop.left > static_cast<int32_t>(inSz.width)) ||
1102 (inputCrop.height <= 0) ||
1103 (inputCrop.height + inputCrop.top > static_cast<int32_t>(inSz.height)))
1104 {
1105 ALOGE("%s: came up with really wrong crop rectangle",__FUNCTION__);
1106 ALOGE("%s: input layout %dx%d to for output size %dx%d",
1107 __FUNCTION__, inSz.width, inSz.height, outSz.width, outSz.height);
1108 ALOGE("%s: computed input crop +%d,+%d %dx%d",
1109 __FUNCTION__, inputCrop.left, inputCrop.top,
1110 inputCrop.width, inputCrop.height);
1111 return -1;
1112 }
1113
1114 YCbCrLayout inputLayout;
1115 ret = in->getCroppedLayout(inputCrop, &inputLayout);
1116 if (ret != 0) {
1117 ALOGE("%s: failed to crop input layout %dx%d to for output size %dx%d",
1118 __FUNCTION__, inSz.width, inSz.height, outSz.width, outSz.height);
1119 ALOGE("%s: computed input crop +%d,+%d %dx%d",
1120 __FUNCTION__, inputCrop.left, inputCrop.top,
1121 inputCrop.width, inputCrop.height);
1122 return ret;
1123 }
1124 ALOGV("%s: crop input layout %dx%d to for output size %dx%d",
1125 __FUNCTION__, inSz.width, inSz.height, outSz.width, outSz.height);
1126 ALOGV("%s: computed input crop +%d,+%d %dx%d",
1127 __FUNCTION__, inputCrop.left, inputCrop.top,
1128 inputCrop.width, inputCrop.height);
1129
1130
1131 // Scale
1132 YCbCrLayout outFullLayout;
1133
1134 ret = mYu12ThumbFrame->getLayout(&outFullLayout);
1135 if (ret != 0) {
1136 ALOGE("%s: failed to get output buffer layout", __FUNCTION__);
1137 return ret;
1138 }
1139
1140
1141 ret = libyuv::I420Scale(
1142 static_cast<uint8_t*>(inputLayout.y),
1143 inputLayout.yStride,
1144 static_cast<uint8_t*>(inputLayout.cb),
1145 inputLayout.cStride,
1146 static_cast<uint8_t*>(inputLayout.cr),
1147 inputLayout.cStride,
1148 inputCrop.width,
1149 inputCrop.height,
1150 static_cast<uint8_t*>(outFullLayout.y),
1151 outFullLayout.yStride,
1152 static_cast<uint8_t*>(outFullLayout.cb),
1153 outFullLayout.cStride,
1154 static_cast<uint8_t*>(outFullLayout.cr),
1155 outFullLayout.cStride,
1156 outSz.width,
1157 outSz.height,
1158 libyuv::FilterMode::kFilterNone);
1159
1160 if (ret != 0) {
1161 ALOGE("%s: failed to scale buffer from %dx%d to %dx%d. Ret %d",
1162 __FUNCTION__, inputCrop.width, inputCrop.height,
1163 outSz.width, outSz.height, ret);
1164 return ret;
1165 }
1166
1167 *out = outFullLayout;
1168 return 0;
1169}
1170
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08001171/*
1172 * TODO: There needs to be a mechanism to discover allocated buffer size
1173 * in the HAL.
1174 *
1175 * This is very fragile because it is duplicated computation from:
1176 * frameworks/av/services/camera/libcameraservice/device3/Camera3Device.cpp
1177 *
1178 */
1179
1180/* This assumes mSupportedFormats have all been declared as supporting
1181 * HAL_PIXEL_FORMAT_BLOB to the framework */
1182Size ExternalCameraDeviceSession::getMaxJpegResolution() const {
1183 Size ret { 0, 0 };
1184 for(auto & fmt : mSupportedFormats) {
1185 if(fmt.width * fmt.height > ret.width * ret.height) {
1186 ret = Size { fmt.width, fmt.height };
1187 }
1188 }
1189 return ret;
1190}
1191
1192Size ExternalCameraDeviceSession::getMaxThumbResolution() const {
Yin-Chia Yeh5dab7282020-01-21 10:08:12 -08001193 return getMaxThumbnailResolution(mCameraCharacteristics);
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08001194}
1195
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08001196ssize_t ExternalCameraDeviceSession::getJpegBufferSize(
1197 uint32_t width, uint32_t height) const {
1198 // Constant from camera3.h
1199 const ssize_t kMinJpegBufferSize = 256 * 1024 + sizeof(CameraBlob);
1200 // Get max jpeg size (area-wise).
1201 if (mMaxJpegResolution.width == 0) {
1202 ALOGE("%s: Do not have a single supported JPEG stream",
1203 __FUNCTION__);
1204 return BAD_VALUE;
1205 }
1206
1207 // Get max jpeg buffer size
1208 ssize_t maxJpegBufferSize = 0;
1209 camera_metadata_ro_entry jpegBufMaxSize =
1210 mCameraCharacteristics.find(ANDROID_JPEG_MAX_SIZE);
1211 if (jpegBufMaxSize.count == 0) {
1212 ALOGE("%s: Can't find maximum JPEG size in static metadata!",
1213 __FUNCTION__);
1214 return BAD_VALUE;
1215 }
1216 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
1217
1218 if (maxJpegBufferSize <= kMinJpegBufferSize) {
1219 ALOGE("%s: ANDROID_JPEG_MAX_SIZE (%zd) <= kMinJpegBufferSize (%zd)",
1220 __FUNCTION__, maxJpegBufferSize, kMinJpegBufferSize);
1221 return BAD_VALUE;
1222 }
1223
1224 // Calculate final jpeg buffer size for the given resolution.
1225 float scaleFactor = ((float) (width * height)) /
1226 (mMaxJpegResolution.width * mMaxJpegResolution.height);
1227 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
1228 kMinJpegBufferSize;
1229 if (jpegBufferSize > maxJpegBufferSize) {
1230 jpegBufferSize = maxJpegBufferSize;
1231 }
1232
1233 return jpegBufferSize;
1234}
1235
1236int ExternalCameraDeviceSession::OutputThread::createJpegLocked(
1237 HalStreamBuffer &halBuf,
Yin-Chia Yeh5dab7282020-01-21 10:08:12 -08001238 const common::V1_0::helper::CameraMetadata& setting)
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08001239{
Yin-Chia Yehe99cf202018-02-28 11:30:38 -08001240 ATRACE_CALL();
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08001241 int ret;
1242 auto lfail = [&](auto... args) {
1243 ALOGE(args...);
1244
1245 return 1;
1246 };
1247 auto parent = mParent.promote();
1248 if (parent == nullptr) {
1249 ALOGE("%s: session has been disconnected!", __FUNCTION__);
1250 return 1;
1251 }
1252
1253 ALOGV("%s: HAL buffer sid: %d bid: %" PRIu64 " w: %u h: %u",
1254 __FUNCTION__, halBuf.streamId, static_cast<uint64_t>(halBuf.bufferId),
1255 halBuf.width, halBuf.height);
1256 ALOGV("%s: HAL buffer fmt: %x usage: %" PRIx64 " ptr: %p",
1257 __FUNCTION__, halBuf.format, static_cast<uint64_t>(halBuf.usage),
1258 halBuf.bufPtr);
1259 ALOGV("%s: YV12 buffer %d x %d",
1260 __FUNCTION__,
1261 mYu12Frame->mWidth, mYu12Frame->mHeight);
1262
1263 int jpegQuality, thumbQuality;
1264 Size thumbSize;
Yin-Chia Yeh8b699aa2018-02-28 15:58:54 -08001265 bool outputThumbnail = true;
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08001266
Yin-Chia Yeh5dab7282020-01-21 10:08:12 -08001267 if (setting.exists(ANDROID_JPEG_QUALITY)) {
1268 camera_metadata_ro_entry entry =
1269 setting.find(ANDROID_JPEG_QUALITY);
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08001270 jpegQuality = entry.data.u8[0];
1271 } else {
1272 return lfail("%s: ANDROID_JPEG_QUALITY not set",__FUNCTION__);
1273 }
1274
Yin-Chia Yeh5dab7282020-01-21 10:08:12 -08001275 if (setting.exists(ANDROID_JPEG_THUMBNAIL_QUALITY)) {
1276 camera_metadata_ro_entry entry =
1277 setting.find(ANDROID_JPEG_THUMBNAIL_QUALITY);
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08001278 thumbQuality = entry.data.u8[0];
1279 } else {
1280 return lfail(
1281 "%s: ANDROID_JPEG_THUMBNAIL_QUALITY not set",
1282 __FUNCTION__);
1283 }
1284
Yin-Chia Yeh5dab7282020-01-21 10:08:12 -08001285 if (setting.exists(ANDROID_JPEG_THUMBNAIL_SIZE)) {
1286 camera_metadata_ro_entry entry =
1287 setting.find(ANDROID_JPEG_THUMBNAIL_SIZE);
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08001288 thumbSize = Size { static_cast<uint32_t>(entry.data.i32[0]),
1289 static_cast<uint32_t>(entry.data.i32[1])
1290 };
Yin-Chia Yeh8b699aa2018-02-28 15:58:54 -08001291 if (thumbSize.width == 0 && thumbSize.height == 0) {
1292 outputThumbnail = false;
1293 }
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08001294 } else {
1295 return lfail(
1296 "%s: ANDROID_JPEG_THUMBNAIL_SIZE not set", __FUNCTION__);
1297 }
1298
1299 /* Cropped and scaled YU12 buffer for main and thumbnail */
1300 YCbCrLayout yu12Main;
1301 Size jpegSize { halBuf.width, halBuf.height };
1302
1303 /* Compute temporary buffer sizes accounting for the following:
1304 * thumbnail can't exceed APP1 size of 64K
1305 * main image needs to hold APP1, headers, and at most a poorly
1306 * compressed image */
1307 const ssize_t maxThumbCodeSize = 64 * 1024;
Yin-Chia Yeh4c641182018-08-06 15:01:20 -07001308 const ssize_t maxJpegCodeSize = mBlobBufferSize == 0 ?
1309 parent->getJpegBufferSize(jpegSize.width, jpegSize.height) :
1310 mBlobBufferSize;
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08001311
1312 /* Check that getJpegBufferSize did not return an error */
1313 if (maxJpegCodeSize < 0) {
1314 return lfail(
1315 "%s: getJpegBufferSize returned %zd",__FUNCTION__,maxJpegCodeSize);
1316 }
1317
1318
1319 /* Hold actual thumbnail and main image code sizes */
1320 size_t thumbCodeSize = 0, jpegCodeSize = 0;
1321 /* Temporary thumbnail code buffer */
Yin-Chia Yeh8b699aa2018-02-28 15:58:54 -08001322 std::vector<uint8_t> thumbCode(outputThumbnail ? maxThumbCodeSize : 0);
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08001323
1324 YCbCrLayout yu12Thumb;
Yin-Chia Yeh8b699aa2018-02-28 15:58:54 -08001325 if (outputThumbnail) {
1326 ret = cropAndScaleThumbLocked(mYu12Frame, thumbSize, &yu12Thumb);
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08001327
Yin-Chia Yeh8b699aa2018-02-28 15:58:54 -08001328 if (ret != 0) {
1329 return lfail(
1330 "%s: crop and scale thumbnail failed!", __FUNCTION__);
1331 }
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08001332 }
1333
1334 /* Scale and crop main jpeg */
1335 ret = cropAndScaleLocked(mYu12Frame, jpegSize, &yu12Main);
1336
1337 if (ret != 0) {
1338 return lfail("%s: crop and scale main failed!", __FUNCTION__);
1339 }
1340
1341 /* Encode the thumbnail image */
Yin-Chia Yeh8b699aa2018-02-28 15:58:54 -08001342 if (outputThumbnail) {
1343 ret = encodeJpegYU12(thumbSize, yu12Thumb,
1344 thumbQuality, 0, 0,
1345 &thumbCode[0], maxThumbCodeSize, thumbCodeSize);
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08001346
Yin-Chia Yeh8b699aa2018-02-28 15:58:54 -08001347 if (ret != 0) {
1348 return lfail("%s: thumbnail encodeJpegYU12 failed with %d",__FUNCTION__, ret);
1349 }
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08001350 }
1351
1352 /* Combine camera characteristics with request settings to form EXIF
1353 * metadata */
Yin-Chia Yeh5dab7282020-01-21 10:08:12 -08001354 common::V1_0::helper::CameraMetadata meta(mCameraCharacteristics);
1355 meta.append(setting);
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08001356
1357 /* Generate EXIF object */
1358 std::unique_ptr<ExifUtils> utils(ExifUtils::create());
1359 /* Make sure it's initialized */
1360 utils->initialize();
1361
1362 utils->setFromMetadata(meta, jpegSize.width, jpegSize.height);
Yin-Chia Yeh2d61bfd2018-03-14 13:50:23 -07001363 utils->setMake(mExifMake);
1364 utils->setModel(mExifModel);
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08001365
Yin-Chia Yeh8b699aa2018-02-28 15:58:54 -08001366 ret = utils->generateApp1(outputThumbnail ? &thumbCode[0] : 0, thumbCodeSize);
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08001367
1368 if (!ret) {
1369 return lfail("%s: generating APP1 failed", __FUNCTION__);
1370 }
1371
1372 /* Get internal buffer */
1373 size_t exifDataSize = utils->getApp1Length();
1374 const uint8_t* exifData = utils->getApp1Buffer();
1375
1376 /* Lock the HAL jpeg code buffer */
1377 void *bufPtr = sHandleImporter.lock(
1378 *(halBuf.bufPtr), halBuf.usage, maxJpegCodeSize);
1379
1380 if (!bufPtr) {
1381 return lfail("%s: could not lock %zu bytes", __FUNCTION__, maxJpegCodeSize);
1382 }
1383
1384 /* Encode the main jpeg image */
1385 ret = encodeJpegYU12(jpegSize, yu12Main,
1386 jpegQuality, exifData, exifDataSize,
1387 bufPtr, maxJpegCodeSize, jpegCodeSize);
1388
1389 /* TODO: Not sure this belongs here, maybe better to pass jpegCodeSize out
1390 * and do this when returning buffer to parent */
1391 CameraBlob blob { CameraBlobId::JPEG, static_cast<uint32_t>(jpegCodeSize) };
1392 void *blobDst =
1393 reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(bufPtr) +
1394 maxJpegCodeSize -
1395 sizeof(CameraBlob));
1396 memcpy(blobDst, &blob, sizeof(CameraBlob));
1397
1398 /* Unlock the HAL jpeg code buffer */
1399 int relFence = sHandleImporter.unlock(*(halBuf.bufPtr));
Yin-Chia Yeh965d9dd2019-03-14 14:21:01 -07001400 if (relFence >= 0) {
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08001401 halBuf.acquireFence = relFence;
1402 }
1403
1404 /* Check if our JPEG actually succeeded */
1405 if (ret != 0) {
1406 return lfail(
1407 "%s: encodeJpegYU12 failed with %d",__FUNCTION__, ret);
1408 }
1409
1410 ALOGV("%s: encoded JPEG (ret:%d) with Q:%d max size: %zu",
1411 __FUNCTION__, ret, jpegQuality, maxJpegCodeSize);
1412
1413 return 0;
1414}
1415
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001416bool ExternalCameraDeviceSession::OutputThread::threadLoop() {
Yin-Chia Yehe086fb72018-02-16 14:54:04 -08001417 std::shared_ptr<HalRequest> req;
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001418 auto parent = mParent.promote();
1419 if (parent == nullptr) {
1420 ALOGE("%s: session has been disconnected!", __FUNCTION__);
1421 return false;
1422 }
1423
1424 // TODO: maybe we need to setup a sensor thread to dq/enq v4l frames
1425 // regularly to prevent v4l buffer queue filled with stale buffers
1426 // when app doesn't program a preveiw request
1427 waitForNextRequest(&req);
Yin-Chia Yehe086fb72018-02-16 14:54:04 -08001428 if (req == nullptr) {
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001429 // No new request, wait again
1430 return true;
1431 }
1432
Yin-Chia Yeh190e5602018-02-13 17:43:13 -08001433 auto onDeviceError = [&](auto... args) {
1434 ALOGE(args...);
1435 parent->notifyError(
Yin-Chia Yehe086fb72018-02-16 14:54:04 -08001436 req->frameNumber, /*stream*/-1, ErrorCode::ERROR_DEVICE);
Yin-Chia Yeh190e5602018-02-13 17:43:13 -08001437 signalRequestDone();
1438 return false;
1439 };
1440
Emil Jahshaneed00402018-12-11 15:15:17 +02001441 if (req->frameIn->mFourcc != V4L2_PIX_FMT_MJPEG && req->frameIn->mFourcc != V4L2_PIX_FMT_Z16) {
Yin-Chia Yeh190e5602018-02-13 17:43:13 -08001442 return onDeviceError("%s: do not support V4L2 format %c%c%c%c", __FUNCTION__,
Yin-Chia Yehe086fb72018-02-16 14:54:04 -08001443 req->frameIn->mFourcc & 0xFF,
1444 (req->frameIn->mFourcc >> 8) & 0xFF,
1445 (req->frameIn->mFourcc >> 16) & 0xFF,
1446 (req->frameIn->mFourcc >> 24) & 0xFF);
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001447 }
1448
Yin-Chia Yehee238402018-11-04 16:30:11 -08001449 int res = requestBufferStart(req->buffers);
1450 if (res != 0) {
1451 ALOGE("%s: send BufferRequest failed! res %d", __FUNCTION__, res);
1452 return onDeviceError("%s: failed to send buffer request!", __FUNCTION__);
1453 }
1454
Yin-Chia Yeh190e5602018-02-13 17:43:13 -08001455 std::unique_lock<std::mutex> lk(mBufferLock);
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001456 // Convert input V4L2 frame to YU12 of the same size
1457 // TODO: see if we can save some computation by converting to YV12 here
1458 uint8_t* inData;
1459 size_t inDataSize;
Yin-Chia Yeh5dab7282020-01-21 10:08:12 -08001460 if (req->frameIn->getData(&inData, &inDataSize) != 0) {
chenhg06ced052018-10-10 17:07:21 -07001461 lk.unlock();
1462 return onDeviceError("%s: V4L2 buffer map failed", __FUNCTION__);
1463 }
1464
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001465 // TODO: in some special case maybe we can decode jpg directly to gralloc output?
Emil Jahshaneed00402018-12-11 15:15:17 +02001466 if (req->frameIn->mFourcc == V4L2_PIX_FMT_MJPEG) {
1467 ATRACE_BEGIN("MJPGtoI420");
1468 int res = libyuv::MJPGToI420(
1469 inData, inDataSize, static_cast<uint8_t*>(mYu12FrameLayout.y), mYu12FrameLayout.yStride,
1470 static_cast<uint8_t*>(mYu12FrameLayout.cb), mYu12FrameLayout.cStride,
1471 static_cast<uint8_t*>(mYu12FrameLayout.cr), mYu12FrameLayout.cStride,
1472 mYu12Frame->mWidth, mYu12Frame->mHeight, mYu12Frame->mWidth, mYu12Frame->mHeight);
1473 ATRACE_END();
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001474
Emil Jahshaneed00402018-12-11 15:15:17 +02001475 if (res != 0) {
1476 // For some webcam, the first few V4L2 frames might be malformed...
1477 ALOGE("%s: Convert V4L2 frame to YU12 failed! res %d", __FUNCTION__, res);
1478 lk.unlock();
1479 Status st = parent->processCaptureRequestError(req);
1480 if (st != Status::OK) {
1481 return onDeviceError("%s: failed to process capture request error!", __FUNCTION__);
1482 }
1483 signalRequestDone();
1484 return true;
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001485 }
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001486 }
1487
Yin-Chia Yehee238402018-11-04 16:30:11 -08001488 ATRACE_BEGIN("Wait for BufferRequest done");
1489 res = waitForBufferRequestDone(&req->buffers);
1490 ATRACE_END();
1491
1492 if (res != 0) {
1493 ALOGE("%s: wait for BufferRequest done failed! res %d", __FUNCTION__, res);
1494 lk.unlock();
1495 return onDeviceError("%s: failed to process buffer request error!", __FUNCTION__);
1496 }
1497
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001498 ALOGV("%s processing new request", __FUNCTION__);
1499 const int kSyncWaitTimeoutMs = 500;
Yin-Chia Yehe086fb72018-02-16 14:54:04 -08001500 for (auto& halBuf : req->buffers) {
Yin-Chia Yehee238402018-11-04 16:30:11 -08001501 if (*(halBuf.bufPtr) == nullptr) {
1502 ALOGW("%s: buffer for stream %d missing", __FUNCTION__, halBuf.streamId);
1503 halBuf.fenceTimeout = true;
Yin-Chia Yeh965d9dd2019-03-14 14:21:01 -07001504 } else if (halBuf.acquireFence >= 0) {
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001505 int ret = sync_wait(halBuf.acquireFence, kSyncWaitTimeoutMs);
1506 if (ret) {
1507 halBuf.fenceTimeout = true;
1508 } else {
1509 ::close(halBuf.acquireFence);
Yin-Chia Yeh1e089662018-02-02 15:57:58 -08001510 halBuf.acquireFence = -1;
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001511 }
1512 }
1513
1514 if (halBuf.fenceTimeout) {
1515 continue;
1516 }
1517
1518 // Gralloc lockYCbCr the buffer
1519 switch (halBuf.format) {
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08001520 case PixelFormat::BLOB: {
Yin-Chia Yeh5dab7282020-01-21 10:08:12 -08001521 int ret = createJpegLocked(halBuf, req->setting);
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08001522
1523 if(ret != 0) {
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08001524 lk.unlock();
Yin-Chia Yeh190e5602018-02-13 17:43:13 -08001525 return onDeviceError("%s: createJpegLocked failed with %d",
1526 __FUNCTION__, ret);
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08001527 }
1528 } break;
Emil Jahshaneed00402018-12-11 15:15:17 +02001529 case PixelFormat::Y16: {
1530 void* outLayout = sHandleImporter.lock(*(halBuf.bufPtr), halBuf.usage, inDataSize);
1531
1532 std::memcpy(outLayout, inData, inDataSize);
1533
1534 int relFence = sHandleImporter.unlock(*(halBuf.bufPtr));
1535 if (relFence >= 0) {
1536 halBuf.acquireFence = relFence;
1537 }
1538 } break;
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001539 case PixelFormat::YCBCR_420_888:
1540 case PixelFormat::YV12: {
1541 IMapper::Rect outRect {0, 0,
1542 static_cast<int32_t>(halBuf.width),
1543 static_cast<int32_t>(halBuf.height)};
1544 YCbCrLayout outLayout = sHandleImporter.lockYCbCr(
1545 *(halBuf.bufPtr), halBuf.usage, outRect);
1546 ALOGV("%s: outLayout y %p cb %p cr %p y_str %d c_str %d c_step %d",
1547 __FUNCTION__, outLayout.y, outLayout.cb, outLayout.cr,
1548 outLayout.yStride, outLayout.cStride, outLayout.chromaStep);
1549
1550 // Convert to output buffer size/format
1551 uint32_t outputFourcc = getFourCcFromLayout(outLayout);
1552 ALOGV("%s: converting to format %c%c%c%c", __FUNCTION__,
1553 outputFourcc & 0xFF,
1554 (outputFourcc >> 8) & 0xFF,
1555 (outputFourcc >> 16) & 0xFF,
1556 (outputFourcc >> 24) & 0xFF);
1557
1558 YCbCrLayout cropAndScaled;
Yin-Chia Yehe99cf202018-02-28 11:30:38 -08001559 ATRACE_BEGIN("cropAndScaleLocked");
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001560 int ret = cropAndScaleLocked(
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08001561 mYu12Frame,
1562 Size { halBuf.width, halBuf.height },
1563 &cropAndScaled);
Yin-Chia Yehe99cf202018-02-28 11:30:38 -08001564 ATRACE_END();
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001565 if (ret != 0) {
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001566 lk.unlock();
Yin-Chia Yeh190e5602018-02-13 17:43:13 -08001567 return onDeviceError("%s: crop and scale failed!", __FUNCTION__);
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001568 }
1569
1570 Size sz {halBuf.width, halBuf.height};
Yin-Chia Yeh5dab7282020-01-21 10:08:12 -08001571 ATRACE_BEGIN("formatConvert");
1572 ret = formatConvert(cropAndScaled, outLayout, sz, outputFourcc);
Yin-Chia Yehe99cf202018-02-28 11:30:38 -08001573 ATRACE_END();
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001574 if (ret != 0) {
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001575 lk.unlock();
Yin-Chia Yeh190e5602018-02-13 17:43:13 -08001576 return onDeviceError("%s: format coversion failed!", __FUNCTION__);
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001577 }
1578 int relFence = sHandleImporter.unlock(*(halBuf.bufPtr));
Yin-Chia Yeh965d9dd2019-03-14 14:21:01 -07001579 if (relFence >= 0) {
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001580 halBuf.acquireFence = relFence;
1581 }
1582 } break;
1583 default:
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001584 lk.unlock();
Yin-Chia Yeh190e5602018-02-13 17:43:13 -08001585 return onDeviceError("%s: unknown output format %x", __FUNCTION__, halBuf.format);
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001586 }
1587 } // for each buffer
1588 mScaledYu12Frames.clear();
1589
1590 // Don't hold the lock while calling back to parent
1591 lk.unlock();
1592 Status st = parent->processCaptureResult(req);
1593 if (st != Status::OK) {
Yin-Chia Yeh190e5602018-02-13 17:43:13 -08001594 return onDeviceError("%s: failed to process capture result!", __FUNCTION__);
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001595 }
Yin-Chia Yeh190e5602018-02-13 17:43:13 -08001596 signalRequestDone();
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001597 return true;
1598}
1599
1600Status ExternalCameraDeviceSession::OutputThread::allocateIntermediateBuffers(
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08001601 const Size& v4lSize, const Size& thumbSize,
Yin-Chia Yeh4c641182018-08-06 15:01:20 -07001602 const hidl_vec<Stream>& streams,
1603 uint32_t blobBufferSize) {
Yin-Chia Yeh190e5602018-02-13 17:43:13 -08001604 std::lock_guard<std::mutex> lk(mBufferLock);
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001605 if (mScaledYu12Frames.size() != 0) {
1606 ALOGE("%s: intermediate buffer pool has %zu inflight buffers! (expect 0)",
1607 __FUNCTION__, mScaledYu12Frames.size());
1608 return Status::INTERNAL_ERROR;
1609 }
1610
1611 // Allocating intermediate YU12 frame
1612 if (mYu12Frame == nullptr || mYu12Frame->mWidth != v4lSize.width ||
1613 mYu12Frame->mHeight != v4lSize.height) {
1614 mYu12Frame.clear();
1615 mYu12Frame = new AllocatedFrame(v4lSize.width, v4lSize.height);
1616 int ret = mYu12Frame->allocate(&mYu12FrameLayout);
1617 if (ret != 0) {
1618 ALOGE("%s: allocating YU12 frame failed!", __FUNCTION__);
1619 return Status::INTERNAL_ERROR;
1620 }
1621 }
1622
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08001623 // Allocating intermediate YU12 thumbnail frame
1624 if (mYu12ThumbFrame == nullptr ||
1625 mYu12ThumbFrame->mWidth != thumbSize.width ||
1626 mYu12ThumbFrame->mHeight != thumbSize.height) {
1627 mYu12ThumbFrame.clear();
1628 mYu12ThumbFrame = new AllocatedFrame(thumbSize.width, thumbSize.height);
1629 int ret = mYu12ThumbFrame->allocate(&mYu12ThumbFrameLayout);
1630 if (ret != 0) {
1631 ALOGE("%s: allocating YU12 thumb frame failed!", __FUNCTION__);
1632 return Status::INTERNAL_ERROR;
1633 }
1634 }
1635
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001636 // Allocating scaled buffers
1637 for (const auto& stream : streams) {
1638 Size sz = {stream.width, stream.height};
1639 if (sz == v4lSize) {
1640 continue; // Don't need an intermediate buffer same size as v4lBuffer
1641 }
1642 if (mIntermediateBuffers.count(sz) == 0) {
1643 // Create new intermediate buffer
1644 sp<AllocatedFrame> buf = new AllocatedFrame(stream.width, stream.height);
1645 int ret = buf->allocate();
1646 if (ret != 0) {
1647 ALOGE("%s: allocating intermediate YU12 frame %dx%d failed!",
1648 __FUNCTION__, stream.width, stream.height);
1649 return Status::INTERNAL_ERROR;
1650 }
1651 mIntermediateBuffers[sz] = buf;
1652 }
1653 }
1654
1655 // Remove unconfigured buffers
1656 auto it = mIntermediateBuffers.begin();
1657 while (it != mIntermediateBuffers.end()) {
1658 bool configured = false;
1659 auto sz = it->first;
1660 for (const auto& stream : streams) {
1661 if (stream.width == sz.width && stream.height == sz.height) {
1662 configured = true;
1663 break;
1664 }
1665 }
1666 if (configured) {
1667 it++;
1668 } else {
1669 it = mIntermediateBuffers.erase(it);
1670 }
1671 }
Yin-Chia Yeh4c641182018-08-06 15:01:20 -07001672
1673 mBlobBufferSize = blobBufferSize;
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001674 return Status::OK;
1675}
1676
Yin-Chia Yeh5dab7282020-01-21 10:08:12 -08001677void ExternalCameraDeviceSession::OutputThread::clearIntermediateBuffers() {
1678 std::lock_guard<std::mutex> lk(mBufferLock);
1679 mYu12Frame.clear();
1680 mYu12ThumbFrame.clear();
1681 mIntermediateBuffers.clear();
1682 mBlobBufferSize = 0;
1683}
1684
Yin-Chia Yehe086fb72018-02-16 14:54:04 -08001685Status ExternalCameraDeviceSession::OutputThread::submitRequest(
1686 const std::shared_ptr<HalRequest>& req) {
Yin-Chia Yeh190e5602018-02-13 17:43:13 -08001687 std::unique_lock<std::mutex> lk(mRequestListLock);
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001688 mRequestList.push_back(req);
Yin-Chia Yeh190e5602018-02-13 17:43:13 -08001689 lk.unlock();
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001690 mRequestCond.notify_one();
1691 return Status::OK;
1692}
1693
1694void ExternalCameraDeviceSession::OutputThread::flush() {
Yin-Chia Yehe99cf202018-02-28 11:30:38 -08001695 ATRACE_CALL();
Yin-Chia Yeh190e5602018-02-13 17:43:13 -08001696 auto parent = mParent.promote();
1697 if (parent == nullptr) {
1698 ALOGE("%s: session has been disconnected!", __FUNCTION__);
1699 return;
1700 }
1701
1702 std::unique_lock<std::mutex> lk(mRequestListLock);
Yin-Chia Yehe086fb72018-02-16 14:54:04 -08001703 std::list<std::shared_ptr<HalRequest>> reqs = std::move(mRequestList);
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001704 mRequestList.clear();
Yin-Chia Yeh190e5602018-02-13 17:43:13 -08001705 if (mProcessingRequest) {
Yin-Chia Yeh4a3393c2018-02-14 12:47:16 -08001706 std::chrono::seconds timeout = std::chrono::seconds(kFlushWaitTimeoutSec);
Yin-Chia Yeh190e5602018-02-13 17:43:13 -08001707 auto st = mRequestDoneCond.wait_for(lk, timeout);
1708 if (st == std::cv_status::timeout) {
1709 ALOGE("%s: wait for inflight request finish timeout!", __FUNCTION__);
1710 }
1711 }
1712
Yin-Chia Yehe99cf202018-02-28 11:30:38 -08001713 ALOGV("%s: flusing inflight requests", __FUNCTION__);
Yin-Chia Yeh190e5602018-02-13 17:43:13 -08001714 lk.unlock();
1715 for (const auto& req : reqs) {
1716 parent->processCaptureRequestError(req);
1717 }
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001718}
1719
Yin-Chia Yeh5dab7282020-01-21 10:08:12 -08001720std::list<std::shared_ptr<HalRequest>>
1721ExternalCameraDeviceSession::OutputThread::switchToOffline() {
1722 ATRACE_CALL();
1723 std::list<std::shared_ptr<HalRequest>> emptyList;
1724 auto parent = mParent.promote();
1725 if (parent == nullptr) {
1726 ALOGE("%s: session has been disconnected!", __FUNCTION__);
1727 return emptyList;
1728 }
1729
1730 std::unique_lock<std::mutex> lk(mRequestListLock);
1731 std::list<std::shared_ptr<HalRequest>> reqs = std::move(mRequestList);
1732 mRequestList.clear();
1733 if (mProcessingRequest) {
1734 std::chrono::seconds timeout = std::chrono::seconds(kFlushWaitTimeoutSec);
1735 auto st = mRequestDoneCond.wait_for(lk, timeout);
1736 if (st == std::cv_status::timeout) {
1737 ALOGE("%s: wait for inflight request finish timeout!", __FUNCTION__);
1738 }
1739 }
1740 lk.unlock();
1741 clearIntermediateBuffers();
1742 ALOGV("%s: returning %zu request for offline processing", __FUNCTION__, reqs.size());
1743 return reqs;
1744}
1745
Yin-Chia Yehe086fb72018-02-16 14:54:04 -08001746void ExternalCameraDeviceSession::OutputThread::waitForNextRequest(
1747 std::shared_ptr<HalRequest>* out) {
Yin-Chia Yehe99cf202018-02-28 11:30:38 -08001748 ATRACE_CALL();
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001749 if (out == nullptr) {
1750 ALOGE("%s: out is null", __FUNCTION__);
1751 return;
1752 }
1753
Yin-Chia Yeh190e5602018-02-13 17:43:13 -08001754 std::unique_lock<std::mutex> lk(mRequestListLock);
Yin-Chia Yeh4a3393c2018-02-14 12:47:16 -08001755 int waitTimes = 0;
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001756 while (mRequestList.empty()) {
Yin-Chia Yeh4a3393c2018-02-14 12:47:16 -08001757 if (exitPending()) {
1758 return;
1759 }
1760 std::chrono::milliseconds timeout = std::chrono::milliseconds(kReqWaitTimeoutMs);
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001761 auto st = mRequestCond.wait_for(lk, timeout);
1762 if (st == std::cv_status::timeout) {
Yin-Chia Yeh4a3393c2018-02-14 12:47:16 -08001763 waitTimes++;
1764 if (waitTimes == kReqWaitTimesMax) {
1765 // no new request, return
1766 return;
1767 }
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001768 }
1769 }
1770 *out = mRequestList.front();
1771 mRequestList.pop_front();
Yin-Chia Yeh190e5602018-02-13 17:43:13 -08001772 mProcessingRequest = true;
Yin-Chia Yehe086fb72018-02-16 14:54:04 -08001773 mProcessingFrameNumer = (*out)->frameNumber;
Yin-Chia Yeh190e5602018-02-13 17:43:13 -08001774}
1775
1776void ExternalCameraDeviceSession::OutputThread::signalRequestDone() {
1777 std::unique_lock<std::mutex> lk(mRequestListLock);
1778 mProcessingRequest = false;
Yin-Chia Yeh4a3393c2018-02-14 12:47:16 -08001779 mProcessingFrameNumer = 0;
Yin-Chia Yeh190e5602018-02-13 17:43:13 -08001780 lk.unlock();
1781 mRequestDoneCond.notify_one();
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001782}
1783
Yin-Chia Yeh4a3393c2018-02-14 12:47:16 -08001784void ExternalCameraDeviceSession::OutputThread::dump(int fd) {
1785 std::lock_guard<std::mutex> lk(mRequestListLock);
1786 if (mProcessingRequest) {
1787 dprintf(fd, "OutputThread processing frame %d\n", mProcessingFrameNumer);
1788 } else {
1789 dprintf(fd, "OutputThread not processing any frames\n");
1790 }
1791 dprintf(fd, "OutputThread request list contains frame: ");
1792 for (const auto& req : mRequestList) {
Yin-Chia Yehe086fb72018-02-16 14:54:04 -08001793 dprintf(fd, "%d, ", req->frameNumber);
Yin-Chia Yeh4a3393c2018-02-14 12:47:16 -08001794 }
1795 dprintf(fd, "\n");
1796}
1797
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001798void ExternalCameraDeviceSession::cleanupBuffersLocked(int id) {
1799 for (auto& pair : mCirculatingBuffers.at(id)) {
1800 sHandleImporter.freeBuffer(pair.second);
1801 }
1802 mCirculatingBuffers[id].clear();
1803 mCirculatingBuffers.erase(id);
1804}
1805
1806void ExternalCameraDeviceSession::updateBufferCaches(const hidl_vec<BufferCache>& cachesToRemove) {
Yin-Chia Yehee238402018-11-04 16:30:11 -08001807 Mutex::Autolock _l(mCbsLock);
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001808 for (auto& cache : cachesToRemove) {
1809 auto cbsIt = mCirculatingBuffers.find(cache.streamId);
1810 if (cbsIt == mCirculatingBuffers.end()) {
1811 // The stream could have been removed
1812 continue;
1813 }
1814 CirculatingBuffers& cbs = cbsIt->second;
1815 auto it = cbs.find(cache.bufferId);
1816 if (it != cbs.end()) {
1817 sHandleImporter.freeBuffer(it->second);
1818 cbs.erase(it);
1819 } else {
1820 ALOGE("%s: stream %d buffer %" PRIu64 " is not cached",
1821 __FUNCTION__, cache.streamId, cache.bufferId);
1822 }
1823 }
1824}
1825
Emilian Peev40a8c6e2018-10-29 09:35:21 +00001826bool ExternalCameraDeviceSession::isSupported(const Stream& stream,
Emil Jahshaneed00402018-12-11 15:15:17 +02001827 const std::vector<SupportedV4L2Format>& supportedFormats,
1828 const ExternalCameraConfig& devCfg) {
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001829 int32_t ds = static_cast<int32_t>(stream.dataSpace);
1830 PixelFormat fmt = stream.format;
1831 uint32_t width = stream.width;
1832 uint32_t height = stream.height;
1833 // TODO: check usage flags
1834
1835 if (stream.streamType != StreamType::OUTPUT) {
1836 ALOGE("%s: does not support non-output stream type", __FUNCTION__);
1837 return false;
1838 }
1839
1840 if (stream.rotation != StreamRotation::ROTATION_0) {
1841 ALOGE("%s: does not support stream rotation", __FUNCTION__);
1842 return false;
1843 }
1844
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001845 switch (fmt) {
1846 case PixelFormat::BLOB:
1847 if (ds != static_cast<int32_t>(Dataspace::V0_JFIF)) {
1848 ALOGI("%s: BLOB format does not support dataSpace %x", __FUNCTION__, ds);
1849 return false;
1850 }
Chih-Hung Hsieh5daaea72018-10-16 14:22:12 -07001851 break;
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001852 case PixelFormat::IMPLEMENTATION_DEFINED:
1853 case PixelFormat::YCBCR_420_888:
1854 case PixelFormat::YV12:
1855 // TODO: check what dataspace we can support here.
1856 // intentional no-ops.
1857 break;
Emil Jahshaneed00402018-12-11 15:15:17 +02001858 case PixelFormat::Y16:
1859 if (!devCfg.depthEnabled) {
1860 ALOGI("%s: Depth is not Enabled", __FUNCTION__);
1861 return false;
1862 }
1863 if (!(ds & Dataspace::DEPTH)) {
1864 ALOGI("%s: Y16 supports only dataSpace DEPTH", __FUNCTION__);
1865 return false;
1866 }
1867 break;
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001868 default:
1869 ALOGI("%s: does not support format %x", __FUNCTION__, fmt);
1870 return false;
1871 }
1872
1873 // Assume we can convert any V4L2 format to any of supported output format for now, i.e,
1874 // ignoring v4l2Fmt.fourcc for now. Might need more subtle check if we support more v4l format
1875 // in the futrue.
Emilian Peev40a8c6e2018-10-29 09:35:21 +00001876 for (const auto& v4l2Fmt : supportedFormats) {
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001877 if (width == v4l2Fmt.width && height == v4l2Fmt.height) {
1878 return true;
1879 }
1880 }
1881 ALOGI("%s: resolution %dx%d is not supported", __FUNCTION__, width, height);
1882 return false;
1883}
1884
1885int ExternalCameraDeviceSession::v4l2StreamOffLocked() {
1886 if (!mV4l2Streaming) {
1887 return OK;
1888 }
1889
1890 {
1891 std::lock_guard<std::mutex> lk(mV4l2BufferLock);
1892 if (mNumDequeuedV4l2Buffers != 0) {
1893 ALOGE("%s: there are %zu inflight V4L buffers",
1894 __FUNCTION__, mNumDequeuedV4l2Buffers);
1895 return -1;
1896 }
1897 }
Yuriy Romanenko9cdd6f92018-01-31 15:59:20 -08001898 mV4L2BufferCount = 0;
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001899
1900 // VIDIOC_STREAMOFF
1901 v4l2_buf_type capture_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1902 if (TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_STREAMOFF, &capture_type)) < 0) {
1903 ALOGE("%s: STREAMOFF failed: %s", __FUNCTION__, strerror(errno));
1904 return -errno;
1905 }
1906
1907 // VIDIOC_REQBUFS: clear buffers
1908 v4l2_requestbuffers req_buffers{};
1909 req_buffers.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1910 req_buffers.memory = V4L2_MEMORY_MMAP;
1911 req_buffers.count = 0;
1912 if (TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_REQBUFS, &req_buffers)) < 0) {
1913 ALOGE("%s: REQBUFS failed: %s", __FUNCTION__, strerror(errno));
1914 return -errno;
1915 }
1916
1917 mV4l2Streaming = false;
1918 return OK;
1919}
1920
Yin-Chia Yeh3aa9ae92018-02-23 17:21:51 -08001921int ExternalCameraDeviceSession::setV4l2FpsLocked(double fps) {
1922 // VIDIOC_G_PARM/VIDIOC_S_PARM: set fps
1923 v4l2_streamparm streamparm = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
1924 // The following line checks that the driver knows about framerate get/set.
1925 int ret = TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_G_PARM, &streamparm));
1926 if (ret != 0) {
1927 if (errno == -EINVAL) {
1928 ALOGW("%s: device does not support VIDIOC_G_PARM", __FUNCTION__);
1929 }
1930 return -errno;
1931 }
1932 // Now check if the device is able to accept a capture framerate set.
1933 if (!(streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME)) {
1934 ALOGW("%s: device does not support V4L2_CAP_TIMEPERFRAME", __FUNCTION__);
1935 return -EINVAL;
1936 }
1937
1938 // fps is float, approximate by a fraction.
1939 const int kFrameRatePrecision = 10000;
1940 streamparm.parm.capture.timeperframe.numerator = kFrameRatePrecision;
1941 streamparm.parm.capture.timeperframe.denominator =
1942 (fps * kFrameRatePrecision);
1943
1944 if (TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_S_PARM, &streamparm)) < 0) {
1945 ALOGE("%s: failed to set framerate to %f: %s", __FUNCTION__, fps, strerror(errno));
1946 return -1;
1947 }
1948
1949 double retFps = streamparm.parm.capture.timeperframe.denominator /
1950 static_cast<double>(streamparm.parm.capture.timeperframe.numerator);
1951 if (std::fabs(fps - retFps) > 1.0) {
1952 ALOGE("%s: expect fps %f, got %f instead", __FUNCTION__, fps, retFps);
1953 return -1;
1954 }
1955 mV4l2StreamingFps = fps;
1956 return 0;
1957}
1958
1959int ExternalCameraDeviceSession::configureV4l2StreamLocked(
1960 const SupportedV4L2Format& v4l2Fmt, double requestFps) {
Yin-Chia Yehe99cf202018-02-28 11:30:38 -08001961 ATRACE_CALL();
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001962 int ret = v4l2StreamOffLocked();
1963 if (ret != OK) {
1964 ALOGE("%s: stop v4l2 streaming failed: ret %d", __FUNCTION__, ret);
1965 return ret;
1966 }
1967
1968 // VIDIOC_S_FMT w/h/fmt
1969 v4l2_format fmt;
1970 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1971 fmt.fmt.pix.width = v4l2Fmt.width;
1972 fmt.fmt.pix.height = v4l2Fmt.height;
1973 fmt.fmt.pix.pixelformat = v4l2Fmt.fourcc;
1974 ret = TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_S_FMT, &fmt));
1975 if (ret < 0) {
Yin-Chia Yeh8b699aa2018-02-28 15:58:54 -08001976 int numAttempt = 0;
1977 while (ret < 0) {
1978 ALOGW("%s: VIDIOC_S_FMT failed, wait 33ms and try again", __FUNCTION__);
Yin-Chia Yeh2d61bfd2018-03-14 13:50:23 -07001979 usleep(IOCTL_RETRY_SLEEP_US); // sleep and try again
Yin-Chia Yeh8b699aa2018-02-28 15:58:54 -08001980 ret = TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_S_FMT, &fmt));
1981 if (numAttempt == MAX_RETRY) {
1982 break;
1983 }
1984 numAttempt++;
1985 }
1986 if (ret < 0) {
1987 ALOGE("%s: S_FMT ioctl failed: %s", __FUNCTION__, strerror(errno));
1988 return -errno;
1989 }
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001990 }
1991
1992 if (v4l2Fmt.width != fmt.fmt.pix.width || v4l2Fmt.height != fmt.fmt.pix.height ||
1993 v4l2Fmt.fourcc != fmt.fmt.pix.pixelformat) {
1994 ALOGE("%s: S_FMT expect %c%c%c%c %dx%d, got %c%c%c%c %dx%d instead!", __FUNCTION__,
1995 v4l2Fmt.fourcc & 0xFF,
1996 (v4l2Fmt.fourcc >> 8) & 0xFF,
1997 (v4l2Fmt.fourcc >> 16) & 0xFF,
1998 (v4l2Fmt.fourcc >> 24) & 0xFF,
1999 v4l2Fmt.width, v4l2Fmt.height,
2000 fmt.fmt.pix.pixelformat & 0xFF,
2001 (fmt.fmt.pix.pixelformat >> 8) & 0xFF,
2002 (fmt.fmt.pix.pixelformat >> 16) & 0xFF,
2003 (fmt.fmt.pix.pixelformat >> 24) & 0xFF,
2004 fmt.fmt.pix.width, fmt.fmt.pix.height);
2005 return -EINVAL;
2006 }
2007 uint32_t bufferSize = fmt.fmt.pix.sizeimage;
2008 ALOGI("%s: V4L2 buffer size is %d", __FUNCTION__, bufferSize);
Emilian Peevbc0e1652018-04-03 13:28:46 +01002009 uint32_t expectedMaxBufferSize = kMaxBytesPerPixel * fmt.fmt.pix.width * fmt.fmt.pix.height;
2010 if ((bufferSize == 0) || (bufferSize > expectedMaxBufferSize)) {
2011 ALOGE("%s: V4L2 buffer size: %u looks invalid. Expected maximum size: %u", __FUNCTION__,
2012 bufferSize, expectedMaxBufferSize);
2013 return -EINVAL;
2014 }
2015 mMaxV4L2BufferSize = bufferSize;
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002016
Yin-Chia Yeh3aa9ae92018-02-23 17:21:51 -08002017 const double kDefaultFps = 30.0;
2018 double fps = 1000.0;
2019 if (requestFps != 0.0) {
2020 fps = requestFps;
2021 } else {
2022 double maxFps = -1.0;
2023 // Try to pick the slowest fps that is at least 30
2024 for (const auto& fr : v4l2Fmt.frameRates) {
2025 double f = fr.getDouble();
2026 if (maxFps < f) {
2027 maxFps = f;
2028 }
2029 if (f >= kDefaultFps && f < fps) {
2030 fps = f;
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002031 }
2032 }
Yin-Chia Yeh3aa9ae92018-02-23 17:21:51 -08002033 if (fps == 1000.0) {
2034 fps = maxFps;
2035 }
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002036 }
Yin-Chia Yeh3aa9ae92018-02-23 17:21:51 -08002037
2038 int fpsRet = setV4l2FpsLocked(fps);
2039 if (fpsRet != 0 && fpsRet != -EINVAL) {
2040 ALOGE("%s: set fps failed: %s", __FUNCTION__, strerror(fpsRet));
2041 return fpsRet;
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002042 }
2043
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -08002044 uint32_t v4lBufferCount = (fps >= kDefaultFps) ?
2045 mCfg.numVideoBuffers : mCfg.numStillBuffers;
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002046 // VIDIOC_REQBUFS: create buffers
2047 v4l2_requestbuffers req_buffers{};
2048 req_buffers.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2049 req_buffers.memory = V4L2_MEMORY_MMAP;
2050 req_buffers.count = v4lBufferCount;
2051 if (TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_REQBUFS, &req_buffers)) < 0) {
2052 ALOGE("%s: VIDIOC_REQBUFS failed: %s", __FUNCTION__, strerror(errno));
2053 return -errno;
2054 }
2055
2056 // Driver can indeed return more buffer if it needs more to operate
2057 if (req_buffers.count < v4lBufferCount) {
2058 ALOGE("%s: VIDIOC_REQBUFS expected %d buffers, got %d instead",
2059 __FUNCTION__, v4lBufferCount, req_buffers.count);
2060 return NO_MEMORY;
2061 }
2062
Yuriy Romanenko9cdd6f92018-01-31 15:59:20 -08002063 // VIDIOC_QUERYBUF: get buffer offset in the V4L2 fd
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002064 // VIDIOC_QBUF: send buffer to driver
Yuriy Romanenko9cdd6f92018-01-31 15:59:20 -08002065 mV4L2BufferCount = req_buffers.count;
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002066 for (uint32_t i = 0; i < req_buffers.count; i++) {
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002067 v4l2_buffer buffer = {
Nick Desaulnierse1e932e2019-10-11 13:40:19 -07002068 .index = i, .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, .memory = V4L2_MEMORY_MMAP};
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002069
Yuriy Romanenko9cdd6f92018-01-31 15:59:20 -08002070 if (TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_QUERYBUF, &buffer)) < 0) {
2071 ALOGE("%s: QUERYBUF %d failed: %s", __FUNCTION__, i, strerror(errno));
2072 return -errno;
2073 }
2074
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002075 if (TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_QBUF, &buffer)) < 0) {
2076 ALOGE("%s: QBUF %d failed: %s", __FUNCTION__, i, strerror(errno));
2077 return -errno;
2078 }
2079 }
2080
2081 // VIDIOC_STREAMON: start streaming
2082 v4l2_buf_type capture_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Yin-Chia Yeh8b699aa2018-02-28 15:58:54 -08002083 ret = TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_STREAMON, &capture_type));
2084 if (ret < 0) {
2085 int numAttempt = 0;
2086 while (ret < 0) {
2087 ALOGW("%s: VIDIOC_STREAMON failed, wait 33ms and try again", __FUNCTION__);
2088 usleep(IOCTL_RETRY_SLEEP_US); // sleep 100 ms and try again
2089 ret = TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_STREAMON, &capture_type));
2090 if (numAttempt == MAX_RETRY) {
2091 break;
2092 }
2093 numAttempt++;
2094 }
2095 if (ret < 0) {
2096 ALOGE("%s: VIDIOC_STREAMON ioctl failed: %s", __FUNCTION__, strerror(errno));
2097 return -errno;
2098 }
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002099 }
2100
2101 // Swallow first few frames after streamOn to account for bad frames from some devices
2102 for (int i = 0; i < kBadFramesAfterStreamOn; i++) {
2103 v4l2_buffer buffer{};
2104 buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2105 buffer.memory = V4L2_MEMORY_MMAP;
2106 if (TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_DQBUF, &buffer)) < 0) {
2107 ALOGE("%s: DQBUF fails: %s", __FUNCTION__, strerror(errno));
2108 return -errno;
2109 }
2110
2111 if (TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_QBUF, &buffer)) < 0) {
2112 ALOGE("%s: QBUF index %d fails: %s", __FUNCTION__, buffer.index, strerror(errno));
2113 return -errno;
2114 }
2115 }
2116
Yin-Chia Yeh8b699aa2018-02-28 15:58:54 -08002117 ALOGI("%s: start V4L2 streaming %dx%d@%ffps",
2118 __FUNCTION__, v4l2Fmt.width, v4l2Fmt.height, fps);
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002119 mV4l2StreamingFmt = v4l2Fmt;
2120 mV4l2Streaming = true;
2121 return OK;
2122}
2123
Yin-Chia Yeh4a3393c2018-02-14 12:47:16 -08002124sp<V4L2Frame> ExternalCameraDeviceSession::dequeueV4l2FrameLocked(/*out*/nsecs_t* shutterTs) {
Yin-Chia Yehe99cf202018-02-28 11:30:38 -08002125 ATRACE_CALL();
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002126 sp<V4L2Frame> ret = nullptr;
2127
Yin-Chia Yeh4a3393c2018-02-14 12:47:16 -08002128 if (shutterTs == nullptr) {
2129 ALOGE("%s: shutterTs must not be null!", __FUNCTION__);
2130 return ret;
2131 }
2132
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002133 {
2134 std::unique_lock<std::mutex> lk(mV4l2BufferLock);
Yuriy Romanenko9cdd6f92018-01-31 15:59:20 -08002135 if (mNumDequeuedV4l2Buffers == mV4L2BufferCount) {
Yin-Chia Yeh3aa9ae92018-02-23 17:21:51 -08002136 int waitRet = waitForV4L2BufferReturnLocked(lk);
2137 if (waitRet != 0) {
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002138 return ret;
2139 }
2140 }
2141 }
2142
Yin-Chia Yeh94f52a32018-03-07 14:46:51 -08002143 ATRACE_BEGIN("VIDIOC_DQBUF");
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002144 v4l2_buffer buffer{};
2145 buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2146 buffer.memory = V4L2_MEMORY_MMAP;
2147 if (TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_DQBUF, &buffer)) < 0) {
2148 ALOGE("%s: DQBUF fails: %s", __FUNCTION__, strerror(errno));
2149 return ret;
2150 }
Yin-Chia Yeh94f52a32018-03-07 14:46:51 -08002151 ATRACE_END();
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002152
Yuriy Romanenko9cdd6f92018-01-31 15:59:20 -08002153 if (buffer.index >= mV4L2BufferCount) {
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002154 ALOGE("%s: Invalid buffer id: %d", __FUNCTION__, buffer.index);
2155 return ret;
2156 }
2157
2158 if (buffer.flags & V4L2_BUF_FLAG_ERROR) {
2159 ALOGE("%s: v4l2 buf error! buf flag 0x%x", __FUNCTION__, buffer.flags);
2160 // TODO: try to dequeue again
2161 }
2162
Emilian Peevbc0e1652018-04-03 13:28:46 +01002163 if (buffer.bytesused > mMaxV4L2BufferSize) {
2164 ALOGE("%s: v4l2 buffer bytes used: %u maximum %u", __FUNCTION__, buffer.bytesused,
2165 mMaxV4L2BufferSize);
2166 return ret;
2167 }
2168
Yin-Chia Yeh4a3393c2018-02-14 12:47:16 -08002169 if (buffer.flags & V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC) {
2170 // Ideally we should also check for V4L2_BUF_FLAG_TSTAMP_SRC_SOE, but
2171 // even V4L2_BUF_FLAG_TSTAMP_SRC_EOF is better than capture a timestamp now
2172 *shutterTs = static_cast<nsecs_t>(buffer.timestamp.tv_sec)*1000000000LL +
2173 buffer.timestamp.tv_usec * 1000LL;
2174 } else {
2175 *shutterTs = systemTime(SYSTEM_TIME_MONOTONIC);
2176 }
2177
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002178 {
2179 std::lock_guard<std::mutex> lk(mV4l2BufferLock);
2180 mNumDequeuedV4l2Buffers++;
2181 }
2182 return new V4L2Frame(
2183 mV4l2StreamingFmt.width, mV4l2StreamingFmt.height, mV4l2StreamingFmt.fourcc,
Yuriy Romanenko9cdd6f92018-01-31 15:59:20 -08002184 buffer.index, mV4l2Fd.get(), buffer.bytesused, buffer.m.offset);
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002185}
2186
2187void ExternalCameraDeviceSession::enqueueV4l2Frame(const sp<V4L2Frame>& frame) {
Yin-Chia Yehe99cf202018-02-28 11:30:38 -08002188 ATRACE_CALL();
Yin-Chia Yeh94f52a32018-03-07 14:46:51 -08002189 frame->unmap();
2190 ATRACE_BEGIN("VIDIOC_QBUF");
2191 v4l2_buffer buffer{};
2192 buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2193 buffer.memory = V4L2_MEMORY_MMAP;
2194 buffer.index = frame->mBufferIndex;
2195 if (TEMP_FAILURE_RETRY(ioctl(mV4l2Fd.get(), VIDIOC_QBUF, &buffer)) < 0) {
2196 ALOGE("%s: QBUF index %d fails: %s", __FUNCTION__,
2197 frame->mBufferIndex, strerror(errno));
2198 return;
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002199 }
Yin-Chia Yeh94f52a32018-03-07 14:46:51 -08002200 ATRACE_END();
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002201
2202 {
2203 std::lock_guard<std::mutex> lk(mV4l2BufferLock);
2204 mNumDequeuedV4l2Buffers--;
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002205 }
Yin-Chia Yeh190e5602018-02-13 17:43:13 -08002206 mV4L2BufferReturned.notify_one();
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002207}
2208
Emilian Peev40a8c6e2018-10-29 09:35:21 +00002209Status ExternalCameraDeviceSession::isStreamCombinationSupported(
Yin-Chia Yeh4c641182018-08-06 15:01:20 -07002210 const V3_2::StreamConfiguration& config,
Emil Jahshaneed00402018-12-11 15:15:17 +02002211 const std::vector<SupportedV4L2Format>& supportedFormats,
2212 const ExternalCameraConfig& devCfg) {
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002213 if (config.operationMode != StreamConfigurationMode::NORMAL_MODE) {
2214 ALOGE("%s: unsupported operation mode: %d", __FUNCTION__, config.operationMode);
2215 return Status::ILLEGAL_ARGUMENT;
2216 }
2217
2218 if (config.streams.size() == 0) {
2219 ALOGE("%s: cannot configure zero stream", __FUNCTION__);
2220 return Status::ILLEGAL_ARGUMENT;
2221 }
2222
2223 int numProcessedStream = 0;
2224 int numStallStream = 0;
2225 for (const auto& stream : config.streams) {
2226 // Check if the format/width/height combo is supported
Emil Jahshaneed00402018-12-11 15:15:17 +02002227 if (!isSupported(stream, supportedFormats, devCfg)) {
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002228 return Status::ILLEGAL_ARGUMENT;
2229 }
2230 if (stream.format == PixelFormat::BLOB) {
2231 numStallStream++;
2232 } else {
2233 numProcessedStream++;
2234 }
2235 }
2236
2237 if (numProcessedStream > kMaxProcessedStream) {
2238 ALOGE("%s: too many processed streams (expect <= %d, got %d)", __FUNCTION__,
2239 kMaxProcessedStream, numProcessedStream);
2240 return Status::ILLEGAL_ARGUMENT;
2241 }
2242
2243 if (numStallStream > kMaxStallStream) {
2244 ALOGE("%s: too many stall streams (expect <= %d, got %d)", __FUNCTION__,
2245 kMaxStallStream, numStallStream);
2246 return Status::ILLEGAL_ARGUMENT;
2247 }
2248
Emilian Peev40a8c6e2018-10-29 09:35:21 +00002249 return Status::OK;
2250}
2251
2252Status ExternalCameraDeviceSession::configureStreams(
2253 const V3_2::StreamConfiguration& config,
2254 V3_3::HalStreamConfiguration* out,
2255 uint32_t blobBufferSize) {
2256 ATRACE_CALL();
2257
Emil Jahshaneed00402018-12-11 15:15:17 +02002258 Status status = isStreamCombinationSupported(config, mSupportedFormats, mCfg);
Emilian Peev40a8c6e2018-10-29 09:35:21 +00002259 if (status != Status::OK) {
2260 return status;
2261 }
2262
2263 status = initStatus();
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002264 if (status != Status::OK) {
2265 return status;
2266 }
2267
Yin-Chia Yeh94f52a32018-03-07 14:46:51 -08002268
2269 {
2270 std::lock_guard<std::mutex> lk(mInflightFramesLock);
2271 if (!mInflightFrames.empty()) {
2272 ALOGE("%s: trying to configureStreams while there are still %zu inflight frames!",
2273 __FUNCTION__, mInflightFrames.size());
2274 return Status::INTERNAL_ERROR;
2275 }
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002276 }
2277
Yin-Chia Yeh94f52a32018-03-07 14:46:51 -08002278 Mutex::Autolock _l(mLock);
Yin-Chia Yehee238402018-11-04 16:30:11 -08002279 {
2280 Mutex::Autolock _l(mCbsLock);
2281 // Add new streams
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002282 for (const auto& stream : config.streams) {
Yin-Chia Yehee238402018-11-04 16:30:11 -08002283 if (mStreamMap.count(stream.id) == 0) {
2284 mStreamMap[stream.id] = stream;
2285 mCirculatingBuffers.emplace(stream.id, CirculatingBuffers{});
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002286 }
2287 }
Yin-Chia Yehee238402018-11-04 16:30:11 -08002288
2289 // Cleanup removed streams
2290 for(auto it = mStreamMap.begin(); it != mStreamMap.end();) {
2291 int id = it->first;
2292 bool found = false;
2293 for (const auto& stream : config.streams) {
2294 if (id == stream.id) {
2295 found = true;
2296 break;
2297 }
2298 }
2299 if (!found) {
2300 // Unmap all buffers of deleted stream
2301 cleanupBuffersLocked(id);
2302 it = mStreamMap.erase(it);
2303 } else {
2304 ++it;
2305 }
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002306 }
2307 }
2308
2309 // Now select a V4L2 format to produce all output streams
2310 float desiredAr = (mCroppingType == VERTICAL) ? kMaxAspectRatio : kMinAspectRatio;
2311 uint32_t maxDim = 0;
2312 for (const auto& stream : config.streams) {
2313 float aspectRatio = ASPECT_RATIO(stream);
Yin-Chia Yehc15a1ca2018-03-02 14:16:52 -08002314 ALOGI("%s: request stream %dx%d", __FUNCTION__, stream.width, stream.height);
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002315 if ((mCroppingType == VERTICAL && aspectRatio < desiredAr) ||
2316 (mCroppingType == HORIZONTAL && aspectRatio > desiredAr)) {
2317 desiredAr = aspectRatio;
2318 }
2319
2320 // The dimension that's not cropped
2321 uint32_t dim = (mCroppingType == VERTICAL) ? stream.width : stream.height;
2322 if (dim > maxDim) {
2323 maxDim = dim;
2324 }
2325 }
2326 // Find the smallest format that matches the desired aspect ratio and is wide/high enough
2327 SupportedV4L2Format v4l2Fmt {.width = 0, .height = 0};
2328 for (const auto& fmt : mSupportedFormats) {
2329 uint32_t dim = (mCroppingType == VERTICAL) ? fmt.width : fmt.height;
2330 if (dim >= maxDim) {
2331 float aspectRatio = ASPECT_RATIO(fmt);
2332 if (isAspectRatioClose(aspectRatio, desiredAr)) {
2333 v4l2Fmt = fmt;
2334 // since mSupportedFormats is sorted by width then height, the first matching fmt
2335 // will be the smallest one with matching aspect ratio
2336 break;
2337 }
2338 }
2339 }
2340 if (v4l2Fmt.width == 0) {
2341 // Cannot find exact good aspect ratio candidate, try to find a close one
2342 for (const auto& fmt : mSupportedFormats) {
2343 uint32_t dim = (mCroppingType == VERTICAL) ? fmt.width : fmt.height;
2344 if (dim >= maxDim) {
2345 float aspectRatio = ASPECT_RATIO(fmt);
2346 if ((mCroppingType == VERTICAL && aspectRatio < desiredAr) ||
2347 (mCroppingType == HORIZONTAL && aspectRatio > desiredAr)) {
2348 v4l2Fmt = fmt;
2349 break;
2350 }
2351 }
2352 }
2353 }
2354
2355 if (v4l2Fmt.width == 0) {
2356 ALOGE("%s: unable to find a resolution matching (%s at least %d, aspect ratio %f)"
2357 , __FUNCTION__, (mCroppingType == VERTICAL) ? "width" : "height",
2358 maxDim, desiredAr);
2359 return Status::ILLEGAL_ARGUMENT;
2360 }
2361
2362 if (configureV4l2StreamLocked(v4l2Fmt) != 0) {
2363 ALOGE("V4L configuration failed!, format:%c%c%c%c, w %d, h %d",
2364 v4l2Fmt.fourcc & 0xFF,
2365 (v4l2Fmt.fourcc >> 8) & 0xFF,
2366 (v4l2Fmt.fourcc >> 16) & 0xFF,
2367 (v4l2Fmt.fourcc >> 24) & 0xFF,
2368 v4l2Fmt.width, v4l2Fmt.height);
2369 return Status::INTERNAL_ERROR;
2370 }
2371
2372 Size v4lSize = {v4l2Fmt.width, v4l2Fmt.height};
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08002373 Size thumbSize { 0, 0 };
2374 camera_metadata_ro_entry entry =
2375 mCameraCharacteristics.find(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES);
2376 for(uint32_t i = 0; i < entry.count; i += 2) {
2377 Size sz { static_cast<uint32_t>(entry.data.i32[i]),
2378 static_cast<uint32_t>(entry.data.i32[i+1]) };
2379 if(sz.width * sz.height > thumbSize.width * thumbSize.height) {
2380 thumbSize = sz;
2381 }
2382 }
2383
2384 if (thumbSize.width * thumbSize.height == 0) {
2385 ALOGE("%s: non-zero thumbnail size not available", __FUNCTION__);
2386 return Status::INTERNAL_ERROR;
2387 }
2388
Yin-Chia Yeh5dab7282020-01-21 10:08:12 -08002389 mBlobBufferSize = blobBufferSize;
Yuriy Romanenkoe932f1b2018-01-19 16:12:00 -08002390 status = mOutputThread->allocateIntermediateBuffers(v4lSize,
Yin-Chia Yeh4c641182018-08-06 15:01:20 -07002391 mMaxThumbResolution, config.streams, blobBufferSize);
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002392 if (status != Status::OK) {
2393 ALOGE("%s: allocating intermediate buffers failed!", __FUNCTION__);
2394 return status;
2395 }
2396
2397 out->streams.resize(config.streams.size());
2398 for (size_t i = 0; i < config.streams.size(); i++) {
2399 out->streams[i].overrideDataSpace = config.streams[i].dataSpace;
2400 out->streams[i].v3_2.id = config.streams[i].id;
2401 // TODO: double check should we add those CAMERA flags
2402 mStreamMap[config.streams[i].id].usage =
2403 out->streams[i].v3_2.producerUsage = config.streams[i].usage |
2404 BufferUsage::CPU_WRITE_OFTEN |
2405 BufferUsage::CAMERA_OUTPUT;
2406 out->streams[i].v3_2.consumerUsage = 0;
Yuriy Romanenko9cdd6f92018-01-31 15:59:20 -08002407 out->streams[i].v3_2.maxBuffers = mV4L2BufferCount;
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002408
2409 switch (config.streams[i].format) {
2410 case PixelFormat::BLOB:
2411 case PixelFormat::YCBCR_420_888:
Yin-Chia Yehfb1c1542018-01-24 15:46:36 -08002412 case PixelFormat::YV12: // Used by SurfaceTexture
Emil Jahshaneed00402018-12-11 15:15:17 +02002413 case PixelFormat::Y16:
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002414 // No override
2415 out->streams[i].v3_2.overrideFormat = config.streams[i].format;
2416 break;
2417 case PixelFormat::IMPLEMENTATION_DEFINED:
2418 // Override based on VIDEO or not
2419 out->streams[i].v3_2.overrideFormat =
2420 (config.streams[i].usage & BufferUsage::VIDEO_ENCODER) ?
2421 PixelFormat::YCBCR_420_888 : PixelFormat::YV12;
2422 // Save overridden formt in mStreamMap
2423 mStreamMap[config.streams[i].id].format = out->streams[i].v3_2.overrideFormat;
2424 break;
2425 default:
Yin-Chia Yehfb1c1542018-01-24 15:46:36 -08002426 ALOGE("%s: unsupported format 0x%x", __FUNCTION__, config.streams[i].format);
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002427 return Status::ILLEGAL_ARGUMENT;
2428 }
2429 }
2430
2431 mFirstRequest = true;
2432 return Status::OK;
2433}
2434
2435bool ExternalCameraDeviceSession::isClosed() {
2436 Mutex::Autolock _l(mLock);
2437 return mClosed;
2438}
2439
2440#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
2441#define UPDATE(md, tag, data, size) \
2442do { \
2443 if ((md).update((tag), (data), (size))) { \
2444 ALOGE("Update " #tag " failed!"); \
2445 return BAD_VALUE; \
2446 } \
2447} while (0)
2448
2449status_t ExternalCameraDeviceSession::initDefaultRequests() {
2450 ::android::hardware::camera::common::V1_0::helper::CameraMetadata md;
2451
2452 const uint8_t aberrationMode = ANDROID_COLOR_CORRECTION_ABERRATION_MODE_OFF;
2453 UPDATE(md, ANDROID_COLOR_CORRECTION_ABERRATION_MODE, &aberrationMode, 1);
2454
2455 const int32_t exposureCompensation = 0;
2456 UPDATE(md, ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION, &exposureCompensation, 1);
2457
2458 const uint8_t videoStabilizationMode = ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF;
2459 UPDATE(md, ANDROID_CONTROL_VIDEO_STABILIZATION_MODE, &videoStabilizationMode, 1);
2460
2461 const uint8_t awbMode = ANDROID_CONTROL_AWB_MODE_AUTO;
2462 UPDATE(md, ANDROID_CONTROL_AWB_MODE, &awbMode, 1);
2463
2464 const uint8_t aeMode = ANDROID_CONTROL_AE_MODE_ON;
2465 UPDATE(md, ANDROID_CONTROL_AE_MODE, &aeMode, 1);
2466
2467 const uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
2468 UPDATE(md, ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
2469
2470 const uint8_t afMode = ANDROID_CONTROL_AF_MODE_AUTO;
2471 UPDATE(md, ANDROID_CONTROL_AF_MODE, &afMode, 1);
2472
2473 const uint8_t afTrigger = ANDROID_CONTROL_AF_TRIGGER_IDLE;
2474 UPDATE(md, ANDROID_CONTROL_AF_TRIGGER, &afTrigger, 1);
2475
2476 const uint8_t sceneMode = ANDROID_CONTROL_SCENE_MODE_DISABLED;
2477 UPDATE(md, ANDROID_CONTROL_SCENE_MODE, &sceneMode, 1);
2478
2479 const uint8_t effectMode = ANDROID_CONTROL_EFFECT_MODE_OFF;
2480 UPDATE(md, ANDROID_CONTROL_EFFECT_MODE, &effectMode, 1);
2481
2482 const uint8_t flashMode = ANDROID_FLASH_MODE_OFF;
2483 UPDATE(md, ANDROID_FLASH_MODE, &flashMode, 1);
2484
2485 const int32_t thumbnailSize[] = {240, 180};
2486 UPDATE(md, ANDROID_JPEG_THUMBNAIL_SIZE, thumbnailSize, 2);
2487
2488 const uint8_t jpegQuality = 90;
2489 UPDATE(md, ANDROID_JPEG_QUALITY, &jpegQuality, 1);
2490 UPDATE(md, ANDROID_JPEG_THUMBNAIL_QUALITY, &jpegQuality, 1);
2491
2492 const int32_t jpegOrientation = 0;
2493 UPDATE(md, ANDROID_JPEG_ORIENTATION, &jpegOrientation, 1);
2494
2495 const uint8_t oisMode = ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF;
2496 UPDATE(md, ANDROID_LENS_OPTICAL_STABILIZATION_MODE, &oisMode, 1);
2497
2498 const uint8_t nrMode = ANDROID_NOISE_REDUCTION_MODE_OFF;
2499 UPDATE(md, ANDROID_NOISE_REDUCTION_MODE, &nrMode, 1);
2500
Yin-Chia Yehc15a1ca2018-03-02 14:16:52 -08002501 const int32_t testPatternModes = ANDROID_SENSOR_TEST_PATTERN_MODE_OFF;
2502 UPDATE(md, ANDROID_SENSOR_TEST_PATTERN_MODE, &testPatternModes, 1);
2503
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002504 const uint8_t fdMode = ANDROID_STATISTICS_FACE_DETECT_MODE_OFF;
2505 UPDATE(md, ANDROID_STATISTICS_FACE_DETECT_MODE, &fdMode, 1);
2506
2507 const uint8_t hotpixelMode = ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF;
2508 UPDATE(md, ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE, &hotpixelMode, 1);
2509
2510 bool support30Fps = false;
2511 int32_t maxFps = std::numeric_limits<int32_t>::min();
2512 for (const auto& supportedFormat : mSupportedFormats) {
Yin-Chia Yeh134093a2018-02-12 14:05:48 -08002513 for (const auto& fr : supportedFormat.frameRates) {
2514 int32_t framerateInt = static_cast<int32_t>(fr.getDouble());
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002515 if (maxFps < framerateInt) {
2516 maxFps = framerateInt;
2517 }
2518 if (framerateInt == 30) {
2519 support30Fps = true;
2520 break;
2521 }
2522 }
2523 if (support30Fps) {
2524 break;
2525 }
2526 }
2527 int32_t defaultFramerate = support30Fps ? 30 : maxFps;
Yin-Chia Yehc15a1ca2018-03-02 14:16:52 -08002528 int32_t defaultFpsRange[] = {defaultFramerate / 2, defaultFramerate};
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002529 UPDATE(md, ANDROID_CONTROL_AE_TARGET_FPS_RANGE, defaultFpsRange, ARRAY_SIZE(defaultFpsRange));
2530
2531 uint8_t antibandingMode = ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO;
2532 UPDATE(md, ANDROID_CONTROL_AE_ANTIBANDING_MODE, &antibandingMode, 1);
2533
2534 const uint8_t controlMode = ANDROID_CONTROL_MODE_AUTO;
2535 UPDATE(md, ANDROID_CONTROL_MODE, &controlMode, 1);
2536
Steven Morelandc90461c2018-05-01 16:54:09 -07002537 auto requestTemplates = hidl_enum_range<RequestTemplate>();
Eino-Ville Talvala658d30d2018-01-18 12:55:07 -08002538 for (RequestTemplate type : requestTemplates) {
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002539 ::android::hardware::camera::common::V1_0::helper::CameraMetadata mdCopy = md;
2540 uint8_t intent = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW;
2541 switch (type) {
Eino-Ville Talvala658d30d2018-01-18 12:55:07 -08002542 case RequestTemplate::PREVIEW:
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002543 intent = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW;
2544 break;
Eino-Ville Talvala658d30d2018-01-18 12:55:07 -08002545 case RequestTemplate::STILL_CAPTURE:
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002546 intent = ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE;
2547 break;
Eino-Ville Talvala658d30d2018-01-18 12:55:07 -08002548 case RequestTemplate::VIDEO_RECORD:
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002549 intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD;
2550 break;
Eino-Ville Talvala658d30d2018-01-18 12:55:07 -08002551 case RequestTemplate::VIDEO_SNAPSHOT:
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002552 intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT;
2553 break;
2554 default:
Yuriy Romanenko083de0c2018-01-26 16:05:54 -08002555 ALOGV("%s: unsupported RequestTemplate type %d", __FUNCTION__, type);
2556 continue;
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002557 }
2558 UPDATE(mdCopy, ANDROID_CONTROL_CAPTURE_INTENT, &intent, 1);
2559
2560 camera_metadata_t* rawMd = mdCopy.release();
2561 CameraMetadata hidlMd;
2562 hidlMd.setToExternal(
2563 (uint8_t*) rawMd, get_camera_metadata_size(rawMd));
2564 mDefaultRequests[type] = hidlMd;
2565 free_camera_metadata(rawMd);
2566 }
2567
2568 return OK;
2569}
2570
2571status_t ExternalCameraDeviceSession::fillCaptureResult(
2572 common::V1_0::helper::CameraMetadata &md, nsecs_t timestamp) {
Yin-Chia Yeh94f52a32018-03-07 14:46:51 -08002573 bool afTrigger = false;
2574 {
2575 std::lock_guard<std::mutex> lk(mAfTriggerLock);
2576 afTrigger = mAfTrigger;
2577 if (md.exists(ANDROID_CONTROL_AF_TRIGGER)) {
2578 camera_metadata_entry entry = md.find(ANDROID_CONTROL_AF_TRIGGER);
2579 if (entry.data.u8[0] == ANDROID_CONTROL_AF_TRIGGER_START) {
2580 mAfTrigger = afTrigger = true;
2581 } else if (entry.data.u8[0] == ANDROID_CONTROL_AF_TRIGGER_CANCEL) {
2582 mAfTrigger = afTrigger = false;
2583 }
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002584 }
2585 }
2586
2587 // For USB camera, the USB camera handles everything and we don't have control
2588 // over AF. We only simply fake the AF metadata based on the request
2589 // received here.
2590 uint8_t afState;
2591 if (afTrigger) {
2592 afState = ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED;
2593 } else {
2594 afState = ANDROID_CONTROL_AF_STATE_INACTIVE;
2595 }
2596 UPDATE(md, ANDROID_CONTROL_AF_STATE, &afState, 1);
2597
Yin-Chia Yeh5dab7282020-01-21 10:08:12 -08002598 camera_metadata_ro_entry activeArraySize =
2599 mCameraCharacteristics.find(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE);
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002600
Yin-Chia Yeh5dab7282020-01-21 10:08:12 -08002601 return fillCaptureResultCommon(md, timestamp, activeArraySize);
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002602}
2603
2604#undef ARRAY_SIZE
2605#undef UPDATE
2606
Yin-Chia Yeh19030592017-10-19 17:30:11 -07002607} // namespace implementation
2608} // namespace V3_4
2609} // namespace device
2610} // namespace camera
2611} // namespace hardware
2612} // namespace android