1 /*
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
18 #define ATRACE_TAG ATRACE_TAG_CAMERA
19 #include <log/log.h>
20 
21 #include <inttypes.h>
22 #include "ExternalCameraDeviceSession.h"
23 
24 #include "android-base/macros.h"
25 #include <utils/Timers.h>
26 #include <utils/Trace.h>
27 #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 
33 #include <jpeglib.h>
34 
35 
36 namespace android {
37 namespace hardware {
38 namespace camera {
39 namespace device {
40 namespace V3_4 {
41 namespace implementation {
42 
43 namespace {
44 // Size of request/result metadata fast message queue. Change to 0 to always use hwbinder buffer.
45 static constexpr size_t kMetadataMsgQueueSize = 1 << 18 /* 256kB */;
46 
47 const 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
50 constexpr int MAX_RETRY = 15; // Allow retry some ioctl failures a few times to account for some
51                              // webcam showing temporarily ioctl failures.
52 constexpr int IOCTL_RETRY_SLEEP_US = 33000; // 33ms * MAX_RETRY = 0.5 seconds
53 
54 // Constants for tryLock during dumpstate
55 static constexpr int kDumpLockRetries = 50;
56 static constexpr int kDumpLockSleep = 60000;
57 
tryLock(Mutex & mutex)58 bool tryLock(Mutex& mutex)
59 {
60     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 
tryLock(std::mutex & mutex)71 bool 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 
84 } // Anonymous namespace
85 
86 // Static instances
87 const int ExternalCameraDeviceSession::kMaxProcessedStream;
88 const int ExternalCameraDeviceSession::kMaxStallStream;
89 HandleImporter ExternalCameraDeviceSession::sHandleImporter;
90 
ExternalCameraDeviceSession(const sp<ICameraDeviceCallback> & callback,const ExternalCameraConfig & cfg,const std::vector<SupportedV4L2Format> & sortedFormats,const CroppingType & croppingType,const common::V1_0::helper::CameraMetadata & chars,const std::string & cameraId,unique_fd v4l2Fd)91 ExternalCameraDeviceSession::ExternalCameraDeviceSession(
92         const sp<ICameraDeviceCallback>& callback,
93         const ExternalCameraConfig& cfg,
94         const std::vector<SupportedV4L2Format>& sortedFormats,
95         const CroppingType& croppingType,
96         const common::V1_0::helper::CameraMetadata& chars,
97         const std::string& cameraId,
98         unique_fd v4l2Fd) :
99         mCallback(callback),
100         mCfg(cfg),
101         mCameraCharacteristics(chars),
102         mSupportedFormats(sortedFormats),
103         mCroppingType(croppingType),
104         mCameraId(cameraId),
105         mV4l2Fd(std::move(v4l2Fd)),
106         mMaxThumbResolution(getMaxThumbResolution()),
107         mMaxJpegResolution(getMaxJpegResolution()) {}
108 
initialize()109 bool ExternalCameraDeviceSession::initialize() {
110     if (mV4l2Fd.get() < 0) {
111         ALOGE("%s: invalid v4l2 device fd %d!", __FUNCTION__, mV4l2Fd.get());
112         return true;
113     }
114 
115     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__);
120         mExifMake = "Generic UVC webcam";
121         mExifModel = "Generic UVC webcam";
122     } 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') {
135             mExifMake = "Generic UVC webcam";
136             mExifModel = "Generic UVC webcam";
137         } else {
138             mExifMake = card;
139             mExifModel = card;
140         }
141     }
142 
143     initOutputThread();
144     if (mOutputThread == nullptr) {
145         ALOGE("%s: init OutputThread failed!", __FUNCTION__);
146         return true;
147     }
148     mOutputThread->setExifMakeModel(mExifMake, mExifModel);
149 
150     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     }
162     mResultMetadataQueue = std::make_shared<ResultMetadataQueue>(
163             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 
isInitFailed()174 bool ExternalCameraDeviceSession::isInitFailed() {
175     Mutex::Autolock _l(mLock);
176     if (!mInitialized) {
177         mInitFail = initialize();
178         mInitialized = true;
179     }
180     return mInitFail;
181 }
182 
initOutputThread()183 void ExternalCameraDeviceSession::initOutputThread() {
184     mOutputThread = new OutputThread(this, mCroppingType, mCameraCharacteristics);
185 }
186 
closeOutputThread()187 void ExternalCameraDeviceSession::closeOutputThread() {
188     closeOutputThreadImpl();
189 }
190 
closeOutputThreadImpl()191 void ExternalCameraDeviceSession::closeOutputThreadImpl() {
192     if (mOutputThread) {
193         mOutputThread->flush();
194         mOutputThread->requestExit();
195         mOutputThread->join();
196         mOutputThread.clear();
197     }
198 }
199 
initStatus() const200 Status 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 
~ExternalCameraDeviceSession()210 ExternalCameraDeviceSession::~ExternalCameraDeviceSession() {
211     if (!isClosed()) {
212         ALOGE("ExternalCameraDeviceSession deleted before close!");
213         close(/*callerIsDtor*/true);
214     }
215 }
216 
217 
dumpState(const native_handle_t * handle)218 void 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;
239     {
240         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;
247 
248         if (sessionLocked) {
249             mLock.unlock();
250         }
251     }
252 
253     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 
266     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
272         dprintf(fd, "Current V4L2 format %c%c%c%c %dx%d @ %ffps\n",
273                 streamingFmt.fourcc & 0xFF,
274                 (streamingFmt.fourcc >> 8) & 0xFF,
275                 (streamingFmt.fourcc >> 16) & 0xFF,
276                 (streamingFmt.fourcc >> 24) & 0xFF,
277                 streamingFmt.width, streamingFmt.height,
278                 mV4l2StreamingFps);
279 
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;
302 }
303 
constructDefaultRequestSettings(V3_2::RequestTemplate type,V3_2::ICameraDeviceSession::constructDefaultRequestSettings_cb _hidl_cb)304 Return<void> ExternalCameraDeviceSession::constructDefaultRequestSettings(
305         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 
constructDefaultRequestSettingsRaw(RequestTemplate type,V3_2::CameraMetadata * outMetadata)314 Status ExternalCameraDeviceSession::constructDefaultRequestSettingsRaw(RequestTemplate type,
315         V3_2::CameraMetadata *outMetadata) {
316     CameraMetadata emptyMd;
317     Status status = initStatus();
318     if (status != Status::OK) {
319         return status;
320     }
321 
322     switch (type) {
323         case RequestTemplate::PREVIEW:
324         case RequestTemplate::STILL_CAPTURE:
325         case RequestTemplate::VIDEO_RECORD:
326         case RequestTemplate::VIDEO_SNAPSHOT: {
327             *outMetadata = mDefaultRequests[type];
328             break;
329         }
330         case RequestTemplate::MANUAL:
331         case RequestTemplate::ZERO_SHUTTER_LAG:
332             // Don't support MANUAL, ZSL templates
333             status = Status::ILLEGAL_ARGUMENT;
334             break;
335         default:
336             ALOGE("%s: unknown request template type %d", __FUNCTION__, static_cast<int>(type));
337             status = Status::ILLEGAL_ARGUMENT;
338             break;
339     }
340     return status;
341 }
342 
configureStreams(const V3_2::StreamConfiguration & streams,ICameraDeviceSession::configureStreams_cb _hidl_cb)343 Return<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 
configureStreams_3_3(const V3_2::StreamConfiguration & streams,ICameraDeviceSession::configureStreams_3_3_cb _hidl_cb)360 Return<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 
configureStreams_3_4(const V3_4::StreamConfiguration & requestedConfiguration,ICameraDeviceSession::configureStreams_3_4_cb _hidl_cb)371 Return<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;
376     V3_4::HalStreamConfiguration outStreams;
377     Mutex::Autolock _il(mInterfaceLock);
378 
379     config_v32.operationMode = requestedConfiguration.operationMode;
380     config_v32.streams.resize(requestedConfiguration.streams.size());
381     uint32_t blobBufferSize = 0;
382     int numStallStream = 0;
383     for (size_t i = 0; i < config_v32.streams.size(); i++) {
384         config_v32.streams[i] = requestedConfiguration.streams[i].v3_2;
385         if (config_v32.streams[i].format == PixelFormat::BLOB) {
386             blobBufferSize = requestedConfiguration.streams[i].bufferSize;
387             numStallStream++;
388         }
389     }
390 
391     // 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     }
398 
399     Status status = configureStreams(config_v32, &outStreams_v33, blobBufferSize);
400 
401     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 
getCaptureRequestMetadataQueue(ICameraDeviceSession::getCaptureRequestMetadataQueue_cb _hidl_cb)409 Return<void> ExternalCameraDeviceSession::getCaptureRequestMetadataQueue(
410     ICameraDeviceSession::getCaptureRequestMetadataQueue_cb _hidl_cb) {
411     Mutex::Autolock _il(mInterfaceLock);
412     _hidl_cb(*mRequestMetadataQueue->getDesc());
413     return Void();
414 }
415 
getCaptureResultMetadataQueue(ICameraDeviceSession::getCaptureResultMetadataQueue_cb _hidl_cb)416 Return<void> ExternalCameraDeviceSession::getCaptureResultMetadataQueue(
417     ICameraDeviceSession::getCaptureResultMetadataQueue_cb _hidl_cb) {
418     Mutex::Autolock _il(mInterfaceLock);
419     _hidl_cb(*mResultMetadataQueue->getDesc());
420     return Void();
421 }
422 
processCaptureRequest(const hidl_vec<CaptureRequest> & requests,const hidl_vec<BufferCache> & cachesToRemove,ICameraDeviceSession::processCaptureRequest_cb _hidl_cb)423 Return<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 
processCaptureRequest_3_4(const hidl_vec<V3_4::CaptureRequest> & requests,const hidl_vec<V3_2::BufferCache> & cachesToRemove,ICameraDeviceSession::processCaptureRequest_3_4_cb _hidl_cb)443 Return<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 
flush()463 Return<Status> ExternalCameraDeviceSession::flush() {
464     ATRACE_CALL();
465     Mutex::Autolock _il(mInterfaceLock);
466     Status status = initStatus();
467     if (status != Status::OK) {
468         return status;
469     }
470     mOutputThread->flush();
471     return Status::OK;
472 }
473 
close(bool callerIsDtor)474 Return<void> ExternalCameraDeviceSession::close(bool callerIsDtor) {
475     Mutex::Autolock _il(mInterfaceLock);
476     bool closed = isClosed();
477     if (!closed) {
478         if (callerIsDtor) {
479             closeOutputThreadImpl();
480         } else {
481             closeOutputThread();
482         }
483 
484         Mutex::Autolock _l(mLock);
485         // free all buffers
486         {
487             Mutex::Autolock _l(mCbsLock);
488             for(auto pair : mStreamMap) {
489                 cleanupBuffersLocked(/*Stream ID*/pair.first);
490             }
491         }
492         v4l2StreamOffLocked();
493         ALOGV("%s: closing V4L2 camera FD %d", __FUNCTION__, mV4l2Fd.get());
494         mV4l2Fd.reset();
495         mClosed = true;
496     }
497     return Void();
498 }
499 
importRequestLocked(const CaptureRequest & request,hidl_vec<buffer_handle_t * > & allBufPtrs,hidl_vec<int> & allFences)500 Status 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 
importBuffer(int32_t streamId,uint64_t bufId,buffer_handle_t buf,buffer_handle_t ** outBufPtr,bool allowEmptyBuf)507 Status 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 
importBufferLocked(int32_t streamId,uint64_t bufId,buffer_handle_t buf,buffer_handle_t ** outBufPtr,bool allowEmptyBuf)515 Status ExternalCameraDeviceSession::importBufferLocked(int32_t streamId,
516         uint64_t bufId, buffer_handle_t buf,
517         /*out*/buffer_handle_t** outBufPtr,
518         bool allowEmptyBuf) {
519     return importBufferImpl(
520             mCirculatingBuffers, sHandleImporter, streamId,
521             bufId, buf, outBufPtr, allowEmptyBuf);
522 }
523 
importRequestLockedImpl(const CaptureRequest & request,hidl_vec<buffer_handle_t * > & allBufPtrs,hidl_vec<int> & allFences,bool allowEmptyBuf)524 Status ExternalCameraDeviceSession::importRequestLockedImpl(
525         const CaptureRequest& request,
526         hidl_vec<buffer_handle_t*>& allBufPtrs,
527         hidl_vec<int>& allFences,
528         bool allowEmptyBuf) {
529     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 
547     {
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;
556             }
557         }
558     }
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 
cleanupInflightFences(hidl_vec<int> & allFences,size_t numFences)572 void 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 
waitForV4L2BufferReturnLocked(std::unique_lock<std::mutex> & lk)579 int ExternalCameraDeviceSession::waitForV4L2BufferReturnLocked(std::unique_lock<std::mutex>& lk) {
580     ATRACE_CALL();
581     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 
processOneCaptureRequest(const CaptureRequest & request)597 Status ExternalCameraDeviceSession::processOneCaptureRequest(const CaptureRequest& request)  {
598     ATRACE_CALL();
599     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 
657     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 
705     status = importRequestLocked(request, allBufPtrs, allFences);
706     if (status != Status::OK) {
707         return status;
708     }
709 
710     nsecs_t shutterTs = 0;
711     sp<V4L2Frame> frameIn = dequeueV4l2FrameLocked(&shutterTs);
712     if ( frameIn == nullptr) {
713         ALOGE("%s: V4L2 deque frame failed!", __FUNCTION__);
714         return Status::INTERNAL_ERROR;
715     }
716 
717     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);
723     for (size_t i = 0; i < numOutputBufs; i++) {
724         HalStreamBuffer& halBuf = halReq->buffers[i];
725         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     }
736     {
737         std::lock_guard<std::mutex> lk(mInflightFramesLock);
738         mInflightFrames.insert(halReq->frameNumber);
739     }
740     // Send request to OutputThread for the rest of processing
741     mOutputThread->submitRequest(halReq);
742     mFirstRequest = false;
743     return Status::OK;
744 }
745 
notifyShutter(uint32_t frameNumber,nsecs_t shutterTs)746 void 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 
notifyError(uint32_t frameNumber,int32_t streamId,ErrorCode ec)754 void 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
processCaptureRequestError(const std::shared_ptr<HalRequest> & req,std::vector<NotifyMsg> * outMsgs,std::vector<CaptureResult> * outResults)765 Status ExternalCameraDeviceSession::processCaptureRequestError(
766         const std::shared_ptr<HalRequest>& req,
767         /*out*/std::vector<NotifyMsg>* outMsgs,
768         /*out*/std::vector<CaptureResult>* outResults) {
769     ATRACE_CALL();
770     // Return V4L2 buffer to V4L2 buffer queue
771     sp<V3_4::implementation::V4L2Frame> v4l2Frame =
772             static_cast<V3_4::implementation::V4L2Frame*>(req->frameIn.get());
773     enqueueV4l2Frame(v4l2Frame);
774 
775     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;
783 
784         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     }
792 
793     // Fill output buffers
794     hidl_vec<CaptureResult> results;
795     results.resize(1);
796     CaptureResult& result = results[0];
797     result.frameNumber = req->frameNumber;
798     result.partialResult = 1;
799     result.inputBuffer.streamId = -1;
800     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;
804         result.outputBuffers[i].status = BufferStatus::ERROR;
805         if (req->buffers[i].acquireFence >= 0) {
806             native_handle_t* handle = native_handle_create(/*numFds*/1, /*numInts*/0);
807             handle->data[0] = req->buffers[i].acquireFence;
808             result.outputBuffers[i].releaseFence.setTo(handle, /*shouldOwn*/false);
809         }
810     }
811 
812     // update inflight records
813     {
814         std::lock_guard<std::mutex> lk(mInflightFramesLock);
815         mInflightFrames.erase(req->frameNumber);
816     }
817 
818     if (outResults == nullptr) {
819         // Callback into framework
820         invokeProcessCaptureResultCallback(results, /* tryWriteFmq */true);
821         freeReleaseFences(results);
822     } else {
823         outResults->push_back(result);
824     }
825     return Status::OK;
826 }
827 
processCaptureResult(std::shared_ptr<HalRequest> & req)828 Status ExternalCameraDeviceSession::processCaptureResult(std::shared_ptr<HalRequest>& req) {
829     ATRACE_CALL();
830     // Return V4L2 buffer to V4L2 buffer queue
831     sp<V3_4::implementation::V4L2Frame> v4l2Frame =
832             static_cast<V3_4::implementation::V4L2Frame*>(req->frameIn.get());
833     enqueueV4l2Frame(v4l2Frame);
834 
835     // NotifyShutter
836     notifyShutter(req->frameNumber, req->shutterTs);
837 
838     // Fill output buffers
839     hidl_vec<CaptureResult> results;
840     results.resize(1);
841     CaptureResult& result = results[0];
842     result.frameNumber = req->frameNumber;
843     result.partialResult = 1;
844     result.inputBuffer.streamId = -1;
845     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) {
850             result.outputBuffers[i].status = BufferStatus::ERROR;
851             if (req->buffers[i].acquireFence >= 0) {
852                 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             }
856             notifyError(req->frameNumber, req->buffers[i].streamId, ErrorCode::ERROR_BUFFER);
857         } else {
858             result.outputBuffers[i].status = BufferStatus::OK;
859             // TODO: refactor
860             if (req->buffers[i].acquireFence >= 0) {
861                 native_handle_t* handle = native_handle_create(/*numFds*/1, /*numInts*/0);
862                 handle->data[0] = req->buffers[i].acquireFence;
863                 result.outputBuffers[i].releaseFence.setTo(handle, /*shouldOwn*/false);
864             }
865         }
866     }
867 
868     // Fill capture result metadata
869     fillCaptureResult(req->setting, req->shutterTs);
870     const camera_metadata_t *rawResult = req->setting.getAndLock();
871     V3_2::implementation::convertToHidl(rawResult, &result.result);
872     req->setting.unlock(rawResult);
873 
874     // update inflight records
875     {
876         std::lock_guard<std::mutex> lk(mInflightFramesLock);
877         mInflightFrames.erase(req->frameNumber);
878     }
879 
880     // Callback into framework
881     invokeProcessCaptureResultCallback(results, /* tryWriteFmq */true);
882     freeReleaseFences(results);
883     return Status::OK;
884 }
885 
invokeProcessCaptureResultCallback(hidl_vec<CaptureResult> & results,bool tryWriteFmq)886 void 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     }
912     auto status = mCallback->processCaptureResult(results);
913     if (!status.isOk()) {
914         ALOGE("%s: processCaptureResult ERROR : %s", __FUNCTION__,
915               status.description().c_str());
916     }
917 
918     mProcessCaptureResultLock.unlock();
919 }
920 
OutputThread(wp<OutputThreadInterface> parent,CroppingType ct,const common::V1_0::helper::CameraMetadata & chars)921 ExternalCameraDeviceSession::OutputThread::OutputThread(
922         wp<OutputThreadInterface> parent, CroppingType ct,
923         const common::V1_0::helper::CameraMetadata& chars) :
924         mParent(parent), mCroppingType(ct), mCameraCharacteristics(chars) {}
925 
~OutputThread()926 ExternalCameraDeviceSession::OutputThread::~OutputThread() {}
927 
setExifMakeModel(const std::string & make,const std::string & model)928 void ExternalCameraDeviceSession::OutputThread::setExifMakeModel(
929         const std::string& make, const std::string& model) {
930     mExifMake = make;
931     mExifModel = model;
932 }
933 
cropAndScaleLocked(sp<AllocatedFrame> & in,const Size & outSz,YCbCrLayout * out)934 int ExternalCameraDeviceSession::OutputThread::cropAndScaleLocked(
935         sp<AllocatedFrame>& in, const Size& outSz, YCbCrLayout* out) {
936     Size inSz = {in->mWidth, in->mHeight};
937 
938     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 
1025 
cropAndScaleThumbLocked(sp<AllocatedFrame> & in,const Size & outSz,YCbCrLayout * out)1026 int 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 
1171 /*
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 */
getMaxJpegResolution() const1182 Size 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 
getMaxThumbResolution() const1192 Size ExternalCameraDeviceSession::getMaxThumbResolution() const {
1193     return getMaxThumbnailResolution(mCameraCharacteristics);
1194 }
1195 
getJpegBufferSize(uint32_t width,uint32_t height) const1196 ssize_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 
createJpegLocked(HalStreamBuffer & halBuf,const common::V1_0::helper::CameraMetadata & setting)1236 int ExternalCameraDeviceSession::OutputThread::createJpegLocked(
1237         HalStreamBuffer &halBuf,
1238         const common::V1_0::helper::CameraMetadata& setting)
1239 {
1240     ATRACE_CALL();
1241     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;
1265     bool outputThumbnail = true;
1266 
1267     if (setting.exists(ANDROID_JPEG_QUALITY)) {
1268         camera_metadata_ro_entry entry =
1269             setting.find(ANDROID_JPEG_QUALITY);
1270         jpegQuality = entry.data.u8[0];
1271     } else {
1272         return lfail("%s: ANDROID_JPEG_QUALITY not set",__FUNCTION__);
1273     }
1274 
1275     if (setting.exists(ANDROID_JPEG_THUMBNAIL_QUALITY)) {
1276         camera_metadata_ro_entry entry =
1277             setting.find(ANDROID_JPEG_THUMBNAIL_QUALITY);
1278         thumbQuality = entry.data.u8[0];
1279     } else {
1280         return lfail(
1281             "%s: ANDROID_JPEG_THUMBNAIL_QUALITY not set",
1282             __FUNCTION__);
1283     }
1284 
1285     if (setting.exists(ANDROID_JPEG_THUMBNAIL_SIZE)) {
1286         camera_metadata_ro_entry entry =
1287             setting.find(ANDROID_JPEG_THUMBNAIL_SIZE);
1288         thumbSize = Size { static_cast<uint32_t>(entry.data.i32[0]),
1289                            static_cast<uint32_t>(entry.data.i32[1])
1290         };
1291         if (thumbSize.width == 0 && thumbSize.height == 0) {
1292             outputThumbnail = false;
1293         }
1294     } 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;
1308     const ssize_t maxJpegCodeSize = mBlobBufferSize == 0 ?
1309             parent->getJpegBufferSize(jpegSize.width, jpegSize.height) :
1310             mBlobBufferSize;
1311 
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 */
1322     std::vector<uint8_t> thumbCode(outputThumbnail ? maxThumbCodeSize : 0);
1323 
1324     YCbCrLayout yu12Thumb;
1325     if (outputThumbnail) {
1326         ret = cropAndScaleThumbLocked(mYu12Frame, thumbSize, &yu12Thumb);
1327 
1328         if (ret != 0) {
1329             return lfail(
1330                 "%s: crop and scale thumbnail failed!", __FUNCTION__);
1331         }
1332     }
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 */
1342     if (outputThumbnail) {
1343         ret = encodeJpegYU12(thumbSize, yu12Thumb,
1344                 thumbQuality, 0, 0,
1345                 &thumbCode[0], maxThumbCodeSize, thumbCodeSize);
1346 
1347         if (ret != 0) {
1348             return lfail("%s: thumbnail encodeJpegYU12 failed with %d",__FUNCTION__, ret);
1349         }
1350     }
1351 
1352     /* Combine camera characteristics with request settings to form EXIF
1353      * metadata */
1354     common::V1_0::helper::CameraMetadata meta(mCameraCharacteristics);
1355     meta.append(setting);
1356 
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);
1363     utils->setMake(mExifMake);
1364     utils->setModel(mExifModel);
1365 
1366     ret = utils->generateApp1(outputThumbnail ? &thumbCode[0] : 0, thumbCodeSize);
1367 
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));
1400     if (relFence >= 0) {
1401         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 
threadLoop()1416 bool ExternalCameraDeviceSession::OutputThread::threadLoop() {
1417     std::shared_ptr<HalRequest> req;
1418     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);
1428     if (req == nullptr) {
1429         // No new request, wait again
1430         return true;
1431     }
1432 
1433     auto onDeviceError = [&](auto... args) {
1434         ALOGE(args...);
1435         parent->notifyError(
1436                 req->frameNumber, /*stream*/-1, ErrorCode::ERROR_DEVICE);
1437         signalRequestDone();
1438         return false;
1439     };
1440 
1441     if (req->frameIn->mFourcc != V4L2_PIX_FMT_MJPEG && req->frameIn->mFourcc != V4L2_PIX_FMT_Z16) {
1442         return onDeviceError("%s: do not support V4L2 format %c%c%c%c", __FUNCTION__,
1443                 req->frameIn->mFourcc & 0xFF,
1444                 (req->frameIn->mFourcc >> 8) & 0xFF,
1445                 (req->frameIn->mFourcc >> 16) & 0xFF,
1446                 (req->frameIn->mFourcc >> 24) & 0xFF);
1447     }
1448 
1449     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 
1455     std::unique_lock<std::mutex> lk(mBufferLock);
1456     // 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;
1460     if (req->frameIn->getData(&inData, &inDataSize) != 0) {
1461         lk.unlock();
1462         return onDeviceError("%s: V4L2 buffer map failed", __FUNCTION__);
1463     }
1464 
1465     // TODO: in some special case maybe we can decode jpg directly to gralloc output?
1466     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();
1474 
1475         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;
1485         }
1486     }
1487 
1488     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 
1498     ALOGV("%s processing new request", __FUNCTION__);
1499     const int kSyncWaitTimeoutMs = 500;
1500     for (auto& halBuf : req->buffers) {
1501         if (*(halBuf.bufPtr) == nullptr) {
1502             ALOGW("%s: buffer for stream %d missing", __FUNCTION__, halBuf.streamId);
1503             halBuf.fenceTimeout = true;
1504         } else if (halBuf.acquireFence >= 0) {
1505             int ret = sync_wait(halBuf.acquireFence, kSyncWaitTimeoutMs);
1506             if (ret) {
1507                 halBuf.fenceTimeout = true;
1508             } else {
1509                 ::close(halBuf.acquireFence);
1510                 halBuf.acquireFence = -1;
1511             }
1512         }
1513 
1514         if (halBuf.fenceTimeout) {
1515             continue;
1516         }
1517 
1518         // Gralloc lockYCbCr the buffer
1519         switch (halBuf.format) {
1520             case PixelFormat::BLOB: {
1521                 int ret = createJpegLocked(halBuf, req->setting);
1522 
1523                 if(ret != 0) {
1524                     lk.unlock();
1525                     return onDeviceError("%s: createJpegLocked failed with %d",
1526                           __FUNCTION__, ret);
1527                 }
1528             } break;
1529             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;
1539             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;
1559                 ATRACE_BEGIN("cropAndScaleLocked");
1560                 int ret = cropAndScaleLocked(
1561                         mYu12Frame,
1562                         Size { halBuf.width, halBuf.height },
1563                         &cropAndScaled);
1564                 ATRACE_END();
1565                 if (ret != 0) {
1566                     lk.unlock();
1567                     return onDeviceError("%s: crop and scale failed!", __FUNCTION__);
1568                 }
1569 
1570                 Size sz {halBuf.width, halBuf.height};
1571                 ATRACE_BEGIN("formatConvert");
1572                 ret = formatConvert(cropAndScaled, outLayout, sz, outputFourcc);
1573                 ATRACE_END();
1574                 if (ret != 0) {
1575                     lk.unlock();
1576                     return onDeviceError("%s: format coversion failed!", __FUNCTION__);
1577                 }
1578                 int relFence = sHandleImporter.unlock(*(halBuf.bufPtr));
1579                 if (relFence >= 0) {
1580                     halBuf.acquireFence = relFence;
1581                 }
1582             } break;
1583             default:
1584                 lk.unlock();
1585                 return onDeviceError("%s: unknown output format %x", __FUNCTION__, halBuf.format);
1586         }
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) {
1594         return onDeviceError("%s: failed to process capture result!", __FUNCTION__);
1595     }
1596     signalRequestDone();
1597     return true;
1598 }
1599 
allocateIntermediateBuffers(const Size & v4lSize,const Size & thumbSize,const hidl_vec<Stream> & streams,uint32_t blobBufferSize)1600 Status ExternalCameraDeviceSession::OutputThread::allocateIntermediateBuffers(
1601         const Size& v4lSize, const Size& thumbSize,
1602         const hidl_vec<Stream>& streams,
1603         uint32_t blobBufferSize) {
1604     std::lock_guard<std::mutex> lk(mBufferLock);
1605     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 
1623     // 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 
1636     // 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     }
1672 
1673     mBlobBufferSize = blobBufferSize;
1674     return Status::OK;
1675 }
1676 
clearIntermediateBuffers()1677 void 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 
submitRequest(const std::shared_ptr<HalRequest> & req)1685 Status ExternalCameraDeviceSession::OutputThread::submitRequest(
1686         const std::shared_ptr<HalRequest>& req) {
1687     std::unique_lock<std::mutex> lk(mRequestListLock);
1688     mRequestList.push_back(req);
1689     lk.unlock();
1690     mRequestCond.notify_one();
1691     return Status::OK;
1692 }
1693 
flush()1694 void ExternalCameraDeviceSession::OutputThread::flush() {
1695     ATRACE_CALL();
1696     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);
1703     std::list<std::shared_ptr<HalRequest>> reqs = std::move(mRequestList);
1704     mRequestList.clear();
1705     if (mProcessingRequest) {
1706         std::chrono::seconds timeout = std::chrono::seconds(kFlushWaitTimeoutSec);
1707         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 
1713     ALOGV("%s: flusing inflight requests", __FUNCTION__);
1714     lk.unlock();
1715     for (const auto& req : reqs) {
1716         parent->processCaptureRequestError(req);
1717     }
1718 }
1719 
1720 std::list<std::shared_ptr<HalRequest>>
switchToOffline()1721 ExternalCameraDeviceSession::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 
waitForNextRequest(std::shared_ptr<HalRequest> * out)1746 void ExternalCameraDeviceSession::OutputThread::waitForNextRequest(
1747         std::shared_ptr<HalRequest>* out) {
1748     ATRACE_CALL();
1749     if (out == nullptr) {
1750         ALOGE("%s: out is null", __FUNCTION__);
1751         return;
1752     }
1753 
1754     std::unique_lock<std::mutex> lk(mRequestListLock);
1755     int waitTimes = 0;
1756     while (mRequestList.empty()) {
1757         if (exitPending()) {
1758             return;
1759         }
1760         std::chrono::milliseconds timeout = std::chrono::milliseconds(kReqWaitTimeoutMs);
1761         auto st = mRequestCond.wait_for(lk, timeout);
1762         if (st == std::cv_status::timeout) {
1763             waitTimes++;
1764             if (waitTimes == kReqWaitTimesMax) {
1765                 // no new request, return
1766                 return;
1767             }
1768         }
1769     }
1770     *out = mRequestList.front();
1771     mRequestList.pop_front();
1772     mProcessingRequest = true;
1773     mProcessingFrameNumer = (*out)->frameNumber;
1774 }
1775 
signalRequestDone()1776 void ExternalCameraDeviceSession::OutputThread::signalRequestDone() {
1777     std::unique_lock<std::mutex> lk(mRequestListLock);
1778     mProcessingRequest = false;
1779     mProcessingFrameNumer = 0;
1780     lk.unlock();
1781     mRequestDoneCond.notify_one();
1782 }
1783 
dump(int fd)1784 void 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) {
1793         dprintf(fd, "%d, ", req->frameNumber);
1794     }
1795     dprintf(fd, "\n");
1796 }
1797 
cleanupBuffersLocked(int id)1798 void 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 
updateBufferCaches(const hidl_vec<BufferCache> & cachesToRemove)1806 void ExternalCameraDeviceSession::updateBufferCaches(const hidl_vec<BufferCache>& cachesToRemove) {
1807     Mutex::Autolock _l(mCbsLock);
1808     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 
isSupported(const Stream & stream,const std::vector<SupportedV4L2Format> & supportedFormats,const ExternalCameraConfig & devCfg)1826 bool ExternalCameraDeviceSession::isSupported(const Stream& stream,
1827         const std::vector<SupportedV4L2Format>& supportedFormats,
1828         const ExternalCameraConfig& devCfg) {
1829     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 
1845     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             }
1851             break;
1852         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;
1858         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;
1868         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.
1876     for (const auto& v4l2Fmt : supportedFormats) {
1877         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 
v4l2StreamOffLocked()1885 int 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     }
1898     mV4L2BufferCount = 0;
1899 
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 
setV4l2FpsLocked(double fps)1921 int 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 
configureV4l2StreamLocked(const SupportedV4L2Format & v4l2Fmt,double requestFps)1959 int ExternalCameraDeviceSession::configureV4l2StreamLocked(
1960         const SupportedV4L2Format& v4l2Fmt, double requestFps) {
1961     ATRACE_CALL();
1962     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) {
1976         int numAttempt = 0;
1977         while (ret < 0) {
1978             ALOGW("%s: VIDIOC_S_FMT failed, wait 33ms and try again", __FUNCTION__);
1979             usleep(IOCTL_RETRY_SLEEP_US); // sleep and try again
1980             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         }
1990     }
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);
2009     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;
2016 
2017     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;
2031             }
2032         }
2033         if (fps == 1000.0) {
2034             fps = maxFps;
2035         }
2036     }
2037 
2038     int fpsRet = setV4l2FpsLocked(fps);
2039     if (fpsRet != 0 && fpsRet != -EINVAL) {
2040         ALOGE("%s: set fps failed: %s", __FUNCTION__, strerror(fpsRet));
2041         return fpsRet;
2042     }
2043 
2044     uint32_t v4lBufferCount = (fps >= kDefaultFps) ?
2045             mCfg.numVideoBuffers : mCfg.numStillBuffers;
2046     // 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 
2063     // VIDIOC_QUERYBUF:  get buffer offset in the V4L2 fd
2064     // VIDIOC_QBUF: send buffer to driver
2065     mV4L2BufferCount = req_buffers.count;
2066     for (uint32_t i = 0; i < req_buffers.count; i++) {
2067         v4l2_buffer buffer = {
2068                 .index = i, .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, .memory = V4L2_MEMORY_MMAP};
2069 
2070         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 
2075         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;
2083     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         }
2099     }
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 
2117     ALOGI("%s: start V4L2 streaming %dx%d@%ffps",
2118                 __FUNCTION__, v4l2Fmt.width, v4l2Fmt.height, fps);
2119     mV4l2StreamingFmt = v4l2Fmt;
2120     mV4l2Streaming = true;
2121     return OK;
2122 }
2123 
dequeueV4l2FrameLocked(nsecs_t * shutterTs)2124 sp<V4L2Frame> ExternalCameraDeviceSession::dequeueV4l2FrameLocked(/*out*/nsecs_t* shutterTs) {
2125     ATRACE_CALL();
2126     sp<V4L2Frame> ret = nullptr;
2127 
2128     if (shutterTs == nullptr) {
2129         ALOGE("%s: shutterTs must not be null!", __FUNCTION__);
2130         return ret;
2131     }
2132 
2133     {
2134         std::unique_lock<std::mutex> lk(mV4l2BufferLock);
2135         if (mNumDequeuedV4l2Buffers == mV4L2BufferCount) {
2136             int waitRet = waitForV4L2BufferReturnLocked(lk);
2137             if (waitRet != 0) {
2138                 return ret;
2139             }
2140         }
2141     }
2142 
2143     ATRACE_BEGIN("VIDIOC_DQBUF");
2144     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     }
2151     ATRACE_END();
2152 
2153     if (buffer.index >= mV4L2BufferCount) {
2154         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 
2163     if (buffer.bytesused > mMaxV4L2BufferSize) {
2164         ALOGE("%s: v4l2 buffer bytes used: %u maximum %u", __FUNCTION__, buffer.bytesused,
2165                 mMaxV4L2BufferSize);
2166         return ret;
2167     }
2168 
2169     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 
2178     {
2179         std::lock_guard<std::mutex> lk(mV4l2BufferLock);
2180         mNumDequeuedV4l2Buffers++;
2181     }
2182     return new V4L2Frame(
2183             mV4l2StreamingFmt.width, mV4l2StreamingFmt.height, mV4l2StreamingFmt.fourcc,
2184             buffer.index, mV4l2Fd.get(), buffer.bytesused, buffer.m.offset);
2185 }
2186 
enqueueV4l2Frame(const sp<V4L2Frame> & frame)2187 void ExternalCameraDeviceSession::enqueueV4l2Frame(const sp<V4L2Frame>& frame) {
2188     ATRACE_CALL();
2189     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;
2199     }
2200     ATRACE_END();
2201 
2202     {
2203         std::lock_guard<std::mutex> lk(mV4l2BufferLock);
2204         mNumDequeuedV4l2Buffers--;
2205     }
2206     mV4L2BufferReturned.notify_one();
2207 }
2208 
isStreamCombinationSupported(const V3_2::StreamConfiguration & config,const std::vector<SupportedV4L2Format> & supportedFormats,const ExternalCameraConfig & devCfg)2209 Status ExternalCameraDeviceSession::isStreamCombinationSupported(
2210         const V3_2::StreamConfiguration& config,
2211         const std::vector<SupportedV4L2Format>& supportedFormats,
2212         const ExternalCameraConfig& devCfg) {
2213     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
2227         if (!isSupported(stream, supportedFormats, devCfg)) {
2228             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 
2249     return Status::OK;
2250 }
2251 
configureStreams(const V3_2::StreamConfiguration & config,V3_3::HalStreamConfiguration * out,uint32_t blobBufferSize)2252 Status ExternalCameraDeviceSession::configureStreams(
2253         const V3_2::StreamConfiguration& config,
2254         V3_3::HalStreamConfiguration* out,
2255         uint32_t blobBufferSize) {
2256     ATRACE_CALL();
2257 
2258     Status status = isStreamCombinationSupported(config, mSupportedFormats, mCfg);
2259     if (status != Status::OK) {
2260         return status;
2261     }
2262 
2263     status = initStatus();
2264     if (status != Status::OK) {
2265         return status;
2266     }
2267 
2268 
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         }
2276     }
2277 
2278     Mutex::Autolock _l(mLock);
2279     {
2280         Mutex::Autolock _l(mCbsLock);
2281         // Add new streams
2282         for (const auto& stream : config.streams) {
2283             if (mStreamMap.count(stream.id) == 0) {
2284                 mStreamMap[stream.id] = stream;
2285                 mCirculatingBuffers.emplace(stream.id, CirculatingBuffers{});
2286             }
2287         }
2288 
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             }
2306         }
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);
2314         ALOGI("%s: request stream %dx%d", __FUNCTION__, stream.width, stream.height);
2315         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};
2373     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 
2389     mBlobBufferSize = blobBufferSize;
2390     status = mOutputThread->allocateIntermediateBuffers(v4lSize,
2391                 mMaxThumbResolution, config.streams, blobBufferSize);
2392     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;
2407         out->streams[i].v3_2.maxBuffers  = mV4L2BufferCount;
2408 
2409         switch (config.streams[i].format) {
2410             case PixelFormat::BLOB:
2411             case PixelFormat::YCBCR_420_888:
2412             case PixelFormat::YV12: // Used by SurfaceTexture
2413             case PixelFormat::Y16:
2414                 // 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:
2426                 ALOGE("%s: unsupported format 0x%x", __FUNCTION__, config.streams[i].format);
2427                 return Status::ILLEGAL_ARGUMENT;
2428         }
2429     }
2430 
2431     mFirstRequest = true;
2432     return Status::OK;
2433 }
2434 
isClosed()2435 bool 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)               \
2442 do {                                              \
2443     if ((md).update((tag), (data), (size))) {     \
2444         ALOGE("Update " #tag " failed!");         \
2445         return BAD_VALUE;                         \
2446     }                                             \
2447 } while (0)
2448 
initDefaultRequests()2449 status_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 
2501     const int32_t testPatternModes = ANDROID_SENSOR_TEST_PATTERN_MODE_OFF;
2502     UPDATE(md, ANDROID_SENSOR_TEST_PATTERN_MODE, &testPatternModes, 1);
2503 
2504     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) {
2513         for (const auto& fr : supportedFormat.frameRates) {
2514             int32_t framerateInt = static_cast<int32_t>(fr.getDouble());
2515             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;
2528     int32_t defaultFpsRange[] = {defaultFramerate / 2, defaultFramerate};
2529     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 
2537     auto requestTemplates = hidl_enum_range<RequestTemplate>();
2538     for (RequestTemplate type : requestTemplates) {
2539         ::android::hardware::camera::common::V1_0::helper::CameraMetadata mdCopy = md;
2540         uint8_t intent = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW;
2541         switch (type) {
2542             case RequestTemplate::PREVIEW:
2543                 intent = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW;
2544                 break;
2545             case RequestTemplate::STILL_CAPTURE:
2546                 intent = ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE;
2547                 break;
2548             case RequestTemplate::VIDEO_RECORD:
2549                 intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD;
2550                 break;
2551             case RequestTemplate::VIDEO_SNAPSHOT:
2552                 intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT;
2553                 break;
2554             default:
2555                 ALOGV("%s: unsupported RequestTemplate type %d", __FUNCTION__, type);
2556                 continue;
2557         }
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 
fillCaptureResult(common::V1_0::helper::CameraMetadata & md,nsecs_t timestamp)2571 status_t ExternalCameraDeviceSession::fillCaptureResult(
2572         common::V1_0::helper::CameraMetadata &md, nsecs_t timestamp) {
2573     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             }
2584         }
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 
2598     camera_metadata_ro_entry activeArraySize =
2599             mCameraCharacteristics.find(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE);
2600 
2601     return fillCaptureResultCommon(md, timestamp, activeArraySize);
2602 }
2603 
2604 #undef ARRAY_SIZE
2605 #undef UPDATE
2606 
2607 }  // namespace implementation
2608 }  // namespace V3_4
2609 }  // namespace device
2610 }  // namespace camera
2611 }  // namespace hardware
2612 }  // namespace android
2613