External camera: add device config file

Also remove sizes cannot be cropped from maximal
size.

Bug: 72261897
Change-Id: Icb50cfa58a12e80be3cacc49569fac90be03c8e5
diff --git a/camera/device/3.4/default/ExternalCameraDeviceSession.cpp b/camera/device/3.4/default/ExternalCameraDeviceSession.cpp
index ff55489..a46bcac 100644
--- a/camera/device/3.4/default/ExternalCameraDeviceSession.cpp
+++ b/camera/device/3.4/default/ExternalCameraDeviceSession.cpp
@@ -21,9 +21,7 @@
 #include "ExternalCameraDeviceSession.h"
 
 #include "android-base/macros.h"
-#include "algorithm"
 #include <utils/Timers.h>
-#include <cmath>
 #include <linux/videodev2.h>
 #include <sync/sync.h>
 
@@ -40,98 +38,40 @@
 namespace V3_4 {
 namespace implementation {
 
+namespace {
 // Size of request/result metadata fast message queue. Change to 0 to always use hwbinder buffer.
-static constexpr size_t kMetadataMsgQueueSize = 1 << 20 /* 1MB */;
-const int ExternalCameraDeviceSession::kMaxProcessedStream;
-const int ExternalCameraDeviceSession::kMaxStallStream;
-const Size kMaxVideoSize = {1920, 1088}; // Maybe this should be programmable
-const int kNumVideoBuffers = 4; // number of v4l2 buffers when streaming <= kMaxVideoSize
-const int kNumStillBuffers = 2; // number of v4l2 buffers when streaming > kMaxVideoSize
+static constexpr size_t kMetadataMsgQueueSize = 1 << 18 /* 256kB */;
+
 const int kBadFramesAfterStreamOn = 1; // drop x frames after streamOn to get rid of some initial
                                        // bad frames. TODO: develop a better bad frame detection
                                        // method
 
-// Aspect ratio is defined as width/height here and ExternalCameraDevice
-// will guarantee all supported sizes has width >= height (so aspect ratio >= 1.0)
-#define ASPECT_RATIO(sz) (static_cast<float>((sz).width) / (sz).height)
-const float kMaxAspectRatio = std::numeric_limits<float>::max();
-const float kMinAspectRatio = 1.f;
+} // Anonymous namespace
 
+// Static instances
+const int ExternalCameraDeviceSession::kMaxProcessedStream;
+const int ExternalCameraDeviceSession::kMaxStallStream;
 HandleImporter ExternalCameraDeviceSession::sHandleImporter;
 
-bool isAspectRatioClose(float ar1, float ar2) {
-    const float kAspectRatioMatchThres = 0.025f; // This threshold is good enough to distinguish
-                                                // 4:3/16:9/20:9
-                                                // 1.33 / 1.78 / 2
-    return (std::abs(ar1 - ar2) < kAspectRatioMatchThres);
-}
-
 ExternalCameraDeviceSession::ExternalCameraDeviceSession(
         const sp<ICameraDeviceCallback>& callback,
-        const std::vector<SupportedV4L2Format>& supportedFormats,
+        const ExternalCameraDeviceConfig& cfg,
+        const std::vector<SupportedV4L2Format>& sortedFormats,
+        const CroppingType& croppingType,
         const common::V1_0::helper::CameraMetadata& chars,
         unique_fd v4l2Fd) :
         mCallback(callback),
+        mCfg(cfg),
         mCameraCharacteristics(chars),
+        mSupportedFormats(sortedFormats),
+        mCroppingType(croppingType),
         mV4l2Fd(std::move(v4l2Fd)),
-        mSupportedFormats(sortFormats(supportedFormats)),
-        mCroppingType(initCroppingType(mSupportedFormats)),
         mOutputThread(new OutputThread(this, mCroppingType)),
         mMaxThumbResolution(getMaxThumbResolution()),
         mMaxJpegResolution(getMaxJpegResolution()) {
     mInitFail = initialize();
 }
 
-std::vector<SupportedV4L2Format> ExternalCameraDeviceSession::sortFormats(
-            const std::vector<SupportedV4L2Format>& inFmts) {
-    std::vector<SupportedV4L2Format> fmts = inFmts;
-    std::sort(fmts.begin(), fmts.end(),
-            [](const SupportedV4L2Format& a, const SupportedV4L2Format& b) -> bool {
-                if (a.width == b.width) {
-                    return a.height < b.height;
-                }
-                return a.width < b.width;
-            });
-    return fmts;
-}
-
-CroppingType ExternalCameraDeviceSession::initCroppingType(
-        const std::vector<SupportedV4L2Format>& sortedFmts) {
-    const auto& maxSize = sortedFmts[sortedFmts.size() - 1];
-    float maxSizeAr = ASPECT_RATIO(maxSize);
-    float minAr = kMaxAspectRatio;
-    float maxAr = kMinAspectRatio;
-    for (const auto& fmt : sortedFmts) {
-        float ar = ASPECT_RATIO(fmt);
-        if (ar < minAr) {
-            minAr = ar;
-        }
-        if (ar > maxAr) {
-            maxAr = ar;
-        }
-    }
-
-    CroppingType ct = VERTICAL;
-    if (isAspectRatioClose(maxSizeAr, maxAr)) {
-        // Ex: 16:9 sensor, cropping horizontally to get to 4:3
-        ct = HORIZONTAL;
-    } else if (isAspectRatioClose(maxSizeAr, minAr)) {
-        // Ex: 4:3 sensor, cropping vertically to get to 16:9
-        ct = VERTICAL;
-    } else {
-        ALOGI("%s: camera maxSizeAr %f is not close to minAr %f or maxAr %f",
-                __FUNCTION__, maxSizeAr, minAr, maxAr);
-        if ((maxSizeAr - minAr) < (maxAr - maxSizeAr)) {
-            ct = VERTICAL;
-        } else {
-            ct = HORIZONTAL;
-        }
-    }
-    ALOGI("%s: camera croppingType is %d", __FUNCTION__, ct);
-    return ct;
-}
-
-
 bool ExternalCameraDeviceSession::initialize() {
     if (mV4l2Fd.get() < 0) {
         ALOGE("%s: invalid v4l2 device fd %d!", __FUNCTION__, mV4l2Fd.get());
@@ -1995,8 +1935,8 @@
         return BAD_VALUE;
     }
 
-    uint32_t v4lBufferCount = (v4l2Fmt.width <= kMaxVideoSize.width &&
-            v4l2Fmt.height <= kMaxVideoSize.height) ? kNumVideoBuffers : kNumStillBuffers;
+    uint32_t v4lBufferCount = (fps >= kDefaultFps) ?
+            mCfg.numVideoBuffers : mCfg.numStillBuffers;
     // VIDIOC_REQBUFS: create buffers
     v4l2_requestbuffers req_buffers{};
     req_buffers.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
@@ -2537,113 +2477,6 @@
 #undef ARRAY_SIZE
 #undef UPDATE
 
-V4L2Frame::V4L2Frame(
-        uint32_t w, uint32_t h, uint32_t fourcc,
-        int bufIdx, int fd, uint32_t dataSize) :
-        mWidth(w), mHeight(h), mFourcc(fourcc),
-        mBufferIndex(bufIdx), mFd(fd), mDataSize(dataSize) {}
-
-int V4L2Frame::map(uint8_t** data, size_t* dataSize) {
-    if (data == nullptr || dataSize == nullptr) {
-        ALOGI("%s: V4L2 buffer map bad argument: data %p, dataSize %p",
-                __FUNCTION__, data, dataSize);
-        return -EINVAL;
-    }
-
-    Mutex::Autolock _l(mLock);
-    if (!mMapped) {
-        void* addr = mmap(NULL, mDataSize, PROT_READ, MAP_SHARED, mFd, 0);
-        if (addr == MAP_FAILED) {
-            ALOGE("%s: V4L2 buffer map failed: %s", __FUNCTION__, strerror(errno));
-            return -EINVAL;
-        }
-        mData = static_cast<uint8_t*>(addr);
-        mMapped = true;
-    }
-    *data = mData;
-    *dataSize = mDataSize;
-    ALOGV("%s: V4L map FD %d, data %p size %zu", __FUNCTION__, mFd, mData, mDataSize);
-    return 0;
-}
-
-int V4L2Frame::unmap() {
-    Mutex::Autolock _l(mLock);
-    if (mMapped) {
-        ALOGV("%s: V4L unmap data %p size %zu", __FUNCTION__, mData, mDataSize);
-        if (munmap(mData, mDataSize) != 0) {
-            ALOGE("%s: V4L2 buffer unmap failed: %s", __FUNCTION__, strerror(errno));
-            return -EINVAL;
-        }
-        mMapped = false;
-    }
-    return 0;
-}
-
-V4L2Frame::~V4L2Frame() {
-    unmap();
-}
-
-AllocatedFrame::AllocatedFrame(
-        uint32_t w, uint32_t h) :
-        mWidth(w), mHeight(h), mFourcc(V4L2_PIX_FMT_YUV420) {};
-
-AllocatedFrame::~AllocatedFrame() {}
-
-int AllocatedFrame::allocate(YCbCrLayout* out) {
-    if ((mWidth % 2) || (mHeight % 2)) {
-        ALOGE("%s: bad dimension %dx%d (not multiple of 2)", __FUNCTION__, mWidth, mHeight);
-        return -EINVAL;
-    }
-
-    uint32_t dataSize = mWidth * mHeight * 3 / 2; // YUV420
-    if (mData.size() != dataSize) {
-        mData.resize(dataSize);
-    }
-
-    if (out != nullptr) {
-        out->y = mData.data();
-        out->yStride = mWidth;
-        uint8_t* cbStart = mData.data() + mWidth * mHeight;
-        uint8_t* crStart = cbStart + mWidth * mHeight / 4;
-        out->cb = cbStart;
-        out->cr = crStart;
-        out->cStride = mWidth / 2;
-        out->chromaStep = 1;
-    }
-    return 0;
-}
-
-int AllocatedFrame::getLayout(YCbCrLayout* out) {
-    IMapper::Rect noCrop = {0, 0,
-            static_cast<int32_t>(mWidth),
-            static_cast<int32_t>(mHeight)};
-    return getCroppedLayout(noCrop, out);
-}
-
-int AllocatedFrame::getCroppedLayout(const IMapper::Rect& rect, YCbCrLayout* out) {
-    if (out == nullptr) {
-        ALOGE("%s: null out", __FUNCTION__);
-        return -1;
-    }
-    if ((rect.left + rect.width) > static_cast<int>(mWidth) ||
-        (rect.top + rect.height) > static_cast<int>(mHeight) ||
-            (rect.left % 2) || (rect.top % 2) || (rect.width % 2) || (rect.height % 2)) {
-        ALOGE("%s: bad rect left %d top %d w %d h %d", __FUNCTION__,
-                rect.left, rect.top, rect.width, rect.height);
-        return -1;
-    }
-
-    out->y = mData.data() + mWidth * rect.top + rect.left;
-    out->yStride = mWidth;
-    uint8_t* cbStart = mData.data() + mWidth * mHeight;
-    uint8_t* crStart = cbStart + mWidth * mHeight / 4;
-    out->cb = cbStart + mWidth * rect.top / 4 + rect.left / 2;
-    out->cr = crStart + mWidth * rect.top / 4 + rect.left / 2;
-    out->cStride = mWidth / 2;
-    out->chromaStep = 1;
-    return 0;
-}
-
 }  // namespace implementation
 }  // namespace V3_4
 }  // namespace device