Avichal Rakesh | fcb78cb | 2022-10-27 15:45:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 | |
| 17 | #define LOG_TAG "AidlCameraDeviceCallbacks" |
| 18 | |
| 19 | #include <aidl/AidlCameraDeviceCallbacks.h> |
| 20 | #include <aidl/AidlUtils.h> |
| 21 | #include <aidl/android/frameworks/cameraservice/common/Status.h> |
| 22 | #include <hidl/Utils.h> |
| 23 | #include <utility> |
| 24 | |
| 25 | namespace android::frameworks::cameraservice::device::implementation { |
| 26 | |
| 27 | // VNDK classes |
| 28 | using SCameraMetadata = ::aidl::android::frameworks::cameraservice::device::CameraMetadata; |
| 29 | using SCaptureResultExtras = |
| 30 | ::aidl::android::frameworks::cameraservice::device::CaptureResultExtras; |
| 31 | using SPhysicalCaptureResultInfo = |
| 32 | ::aidl::android::frameworks::cameraservice::device::PhysicalCaptureResultInfo; |
| 33 | using SStatus = ::aidl::android::frameworks::cameraservice::common::Status; |
| 34 | // NDK classes |
| 35 | using UCaptureResultExtras = ::android::hardware::camera2::impl::CaptureResultExtras; |
| 36 | using UPhysicalCaptureResultInfo = ::android::hardware::camera2::impl::PhysicalCaptureResultInfo; |
| 37 | |
| 38 | using ::android::hardware::cameraservice::utils::conversion::aidl::convertToAidl; |
| 39 | using ::android::hardware::cameraservice::utils::conversion::aidl::filterVndkKeys; |
| 40 | |
| 41 | const char *AidlCameraDeviceCallbacks::kResultKey = "CaptureResult"; |
| 42 | |
| 43 | |
| 44 | bool AidlCameraDeviceCallbacks::initializeLooper(int vndkVersion) { |
| 45 | mCbLooper = new ALooper; |
| 46 | mCbLooper->setName("cs-looper"); |
| 47 | status_t err = mCbLooper->start(/*runOnCallingThread*/ false, /*canCallJava*/ false, |
| 48 | PRIORITY_DEFAULT); |
| 49 | if (err !=OK) { |
| 50 | ALOGE("Unable to start camera device callback looper"); |
| 51 | return false; |
| 52 | } |
| 53 | mHandler = new CallbackHandler(this, vndkVersion); |
| 54 | mCbLooper->registerHandler(mHandler); |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | AidlCameraDeviceCallbacks::AidlCameraDeviceCallbacks( |
| 59 | const std::shared_ptr<SICameraDeviceCallback>& base): |
| 60 | mBase(base), mDeathPipe(this, base->asBinder()) {} |
| 61 | |
| 62 | AidlCameraDeviceCallbacks::~AidlCameraDeviceCallbacks() { |
| 63 | if (mCbLooper != nullptr) { |
| 64 | if (mHandler != nullptr) { |
| 65 | mCbLooper->unregisterHandler(mHandler->id()); |
| 66 | } |
| 67 | mCbLooper->stop(); |
| 68 | } |
| 69 | mCbLooper.clear(); |
| 70 | mHandler.clear(); |
| 71 | } |
| 72 | |
| 73 | binder::Status AidlCameraDeviceCallbacks::onDeviceError( |
| 74 | int32_t errorCode, const CaptureResultExtras& resultExtras) { |
| 75 | using hardware::cameraservice::utils::conversion::aidl::convertToAidl; |
| 76 | SCaptureResultExtras cre = convertToAidl(resultExtras); |
| 77 | auto ret = mBase->onDeviceError(convertToAidl(errorCode), cre); |
| 78 | LOG_STATUS_ERROR_IF_NOT_OK(ret, "onDeviceError") |
| 79 | return binder::Status::ok(); |
| 80 | } |
| 81 | |
| 82 | binder::Status AidlCameraDeviceCallbacks::onDeviceIdle() { |
| 83 | auto ret = mBase->onDeviceIdle(); |
| 84 | LOG_STATUS_ERROR_IF_NOT_OK(ret, "onDeviceIdle") |
| 85 | return binder::Status::ok(); |
| 86 | } |
| 87 | |
| 88 | binder::Status AidlCameraDeviceCallbacks::onCaptureStarted( |
| 89 | const CaptureResultExtras& resultExtras, int64_t timestamp) { |
| 90 | using hardware::cameraservice::utils::conversion::aidl::convertToAidl; |
| 91 | SCaptureResultExtras hCaptureResultExtras = convertToAidl(resultExtras); |
| 92 | auto ret = mBase->onCaptureStarted(hCaptureResultExtras, timestamp); |
| 93 | LOG_STATUS_ERROR_IF_NOT_OK(ret, "onCaptureStarted") |
| 94 | return binder::Status::ok(); |
| 95 | } |
| 96 | |
| 97 | void AidlCameraDeviceCallbacks::convertResultMetadataToAidl(const camera_metadata_t* src, |
| 98 | SCaptureMetadataInfo* dst) { |
| 99 | // First try writing to fmq. |
| 100 | size_t metadata_size = get_camera_metadata_size(src); |
| 101 | if ((metadata_size > 0) && |
| 102 | (mCaptureResultMetadataQueue->availableToWrite() > 0)) { |
| 103 | if (mCaptureResultMetadataQueue->write((int8_t *)src, metadata_size)) { |
| 104 | dst->set<SCaptureMetadataInfo::fmqMetadataSize>(metadata_size); |
| 105 | } else { |
| 106 | ALOGW("%s Couldn't use fmq, falling back to hwbinder", __FUNCTION__); |
| 107 | SCameraMetadata metadata; |
| 108 | hardware::cameraservice::utils::conversion::aidl::cloneToAidl(src, &metadata); |
| 109 | dst->set<SCaptureMetadataInfo::metadata>(std::move(metadata)); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void AidlCameraDeviceCallbacks::CallbackHandler::onMessageReceived(const sp<AMessage> &msg) { |
| 115 | sp<RefBase> obj = nullptr; |
| 116 | sp<ResultWrapper> resultWrapper = nullptr; |
| 117 | bool found = false; |
| 118 | switch (msg->what()) { |
| 119 | case kWhatResultReceived: |
| 120 | found = msg->findObject(kResultKey, &obj); |
| 121 | if (!found || obj == nullptr) { |
| 122 | ALOGE("Cannot find result object in callback message"); |
| 123 | return; |
| 124 | } |
| 125 | resultWrapper = static_cast<ResultWrapper*>(obj.get()); |
| 126 | processResultMessage(resultWrapper); |
| 127 | break; |
| 128 | default: |
| 129 | ALOGE("Unknown callback sent"); |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void AidlCameraDeviceCallbacks::CallbackHandler::processResultMessage( |
| 135 | sp<ResultWrapper> &resultWrapper) { |
| 136 | sp<AidlCameraDeviceCallbacks> converter = mConverter.promote(); |
| 137 | if (converter == nullptr) { |
| 138 | ALOGE("Callback wrapper has died, result callback cannot be made"); |
| 139 | return; |
| 140 | } |
| 141 | CameraMetadataNative &result = resultWrapper->mResult; |
| 142 | auto resultExtras = resultWrapper->mResultExtras; |
| 143 | SCaptureResultExtras convResultExtras = |
| 144 | hardware::cameraservice::utils::conversion::aidl::convertToAidl(resultExtras); |
| 145 | |
| 146 | // Convert Metadata into HCameraMetadata; |
| 147 | SCaptureMetadataInfo captureMetadataInfo; |
| 148 | if (filterVndkKeys(mVndkVersion, result, /*isStatic*/false) != OK) { |
| 149 | ALOGE("%s: filtering vndk keys from result failed, not sending onResultReceived callback", |
| 150 | __FUNCTION__); |
| 151 | return; |
| 152 | } |
| 153 | const camera_metadata_t *rawMetadata = result.getAndLock(); |
| 154 | converter->convertResultMetadataToAidl(rawMetadata, &captureMetadataInfo); |
| 155 | result.unlock(rawMetadata); |
| 156 | |
| 157 | auto &physicalCaptureResultInfos = resultWrapper->mPhysicalCaptureResultInfos; |
| 158 | std::vector<SPhysicalCaptureResultInfo> stableCaptureResInfo = |
| 159 | convertToAidl(physicalCaptureResultInfos, converter->mCaptureResultMetadataQueue); |
| 160 | auto ret = converter->mBase->onResultReceived(captureMetadataInfo, |
| 161 | convResultExtras, |
| 162 | stableCaptureResInfo); |
| 163 | |
| 164 | LOG_STATUS_ERROR_IF_NOT_OK(ret, "OnResultReceived") |
| 165 | } |
| 166 | |
| 167 | binder::Status AidlCameraDeviceCallbacks::onResultReceived( |
| 168 | const CameraMetadataNative& result, |
| 169 | const UCaptureResultExtras& resultExtras, |
| 170 | const ::std::vector<UPhysicalCaptureResultInfo>& physicalCaptureResultInfos) { |
| 171 | // Wrap CameraMetadata, resultExtras and physicalCaptureResultInfos in on |
| 172 | // sp<RefBase>-able structure and post it. |
| 173 | sp<ResultWrapper> resultWrapper = new ResultWrapper(const_cast<CameraMetadataNative &>(result), |
| 174 | resultExtras, physicalCaptureResultInfos); |
| 175 | sp<AMessage> msg = new AMessage(kWhatResultReceived, mHandler); |
| 176 | msg->setObject(kResultKey, resultWrapper); |
| 177 | msg->post(); |
| 178 | return binder::Status::ok(); |
| 179 | } |
| 180 | |
| 181 | binder::Status AidlCameraDeviceCallbacks::onPrepared(int32_t) { |
| 182 | // not implemented |
| 183 | return binder::Status::ok(); |
| 184 | } |
| 185 | |
| 186 | binder::Status AidlCameraDeviceCallbacks::onRepeatingRequestError( |
| 187 | int64_t lastFrameNumber, |
| 188 | int32_t repeatingRequestId) { |
| 189 | auto ret = |
| 190 | mBase->onRepeatingRequestError(lastFrameNumber, repeatingRequestId); |
| 191 | LOG_STATUS_ERROR_IF_NOT_OK(ret, "onRepeatingRequestError") |
| 192 | return binder::Status::ok(); |
| 193 | } |
| 194 | binder::Status AidlCameraDeviceCallbacks::onRequestQueueEmpty() { |
| 195 | // not implemented |
| 196 | return binder::Status::ok(); |
| 197 | } |
| 198 | |
| 199 | status_t AidlCameraDeviceCallbacks::linkToDeath(const sp<DeathRecipient>& recipient, |
| 200 | void* cookie, uint32_t flags) { |
| 201 | return mDeathPipe.linkToDeath(recipient, cookie, flags); |
| 202 | } |
| 203 | status_t AidlCameraDeviceCallbacks::unlinkToDeath(const wp<DeathRecipient>& recipient, |
| 204 | void* cookie, |
| 205 | uint32_t flags, |
| 206 | wp<DeathRecipient>* outRecipient) { |
| 207 | return mDeathPipe.unlinkToDeath(recipient, cookie, flags, outRecipient); |
| 208 | } |
| 209 | |
| 210 | } // namespace android::frameworks::cameraservice::device::implementation |