• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <hardware/camera.h>
17 
18 #include <hidl/AidlCameraDeviceCallbacks.h>
19 #include <hidl/Utils.h>
20 #include <aidl/AidlUtils.h>
21 
22 namespace android {
23 namespace frameworks {
24 namespace cameraservice {
25 namespace device {
26 namespace V2_0 {
27 namespace implementation {
28 
29 using hardware::hidl_vec;
30 using HCaptureResultExtras = android::frameworks::cameraservice::device::V2_0::CaptureResultExtras;
31 using HPhysicalCaptureResultInfo = android::frameworks::cameraservice::device::V2_0::PhysicalCaptureResultInfo;
32 using HCameraMetadata = android::frameworks::cameraservice::device::V2_0::CameraMetadata;
33 
34 const char *H2BCameraDeviceCallbacks::kResultKey = "CaptureResult";
35 
H2BCameraDeviceCallbacks(const sp<HalInterface> & base)36 H2BCameraDeviceCallbacks::H2BCameraDeviceCallbacks(const sp<HalInterface>& base) : CBase(base) { }
37 
initializeLooper(int vndkVersion)38 bool H2BCameraDeviceCallbacks::initializeLooper(int vndkVersion) {
39     mCbLooper = new ALooper;
40     mCbLooper->setName("cs-looper");
41     status_t err = mCbLooper->start(/*runOnCallingThread*/ false, /*canCallJava*/ false,
42                                     PRIORITY_DEFAULT);
43     if (err !=OK) {
44         ALOGE("Unable to start camera device callback looper");
45         return false;
46     }
47     mHandler = new CallbackHandler(this, vndkVersion);
48     mCbLooper->registerHandler(mHandler);
49     return true;
50 }
51 
~H2BCameraDeviceCallbacks()52 H2BCameraDeviceCallbacks::~H2BCameraDeviceCallbacks() {
53     if (mCbLooper != nullptr) {
54         if (mHandler != nullptr) {
55             mCbLooper->unregisterHandler(mHandler->id());
56         }
57         mCbLooper->stop();
58     }
59     mCbLooper.clear();
60     mHandler.clear();
61 }
62 
onDeviceError(int32_t errorCode,const CaptureResultExtras & resultExtras)63 binder::Status H2BCameraDeviceCallbacks::onDeviceError(
64     int32_t errorCode, const CaptureResultExtras& resultExtras) {
65     using hardware::cameraservice::utils::conversion::convertToHidl;
66     HCaptureResultExtras hCaptureResultExtras = convertToHidl(resultExtras);
67     auto ret = mBase->onDeviceError(convertToHidl(errorCode), hCaptureResultExtras);
68     if (!ret.isOk()) {
69         ALOGE("%s OnDeviceError callback failed due to %s",__FUNCTION__,
70               ret.description().c_str());
71     }
72     return binder::Status::ok();
73 }
74 
onDeviceIdle()75 binder::Status H2BCameraDeviceCallbacks::onDeviceIdle() {
76     auto ret = mBase->onDeviceIdle();
77     if (!ret.isOk()) {
78           ALOGE("%s OnDeviceIdle callback failed due to %s",__FUNCTION__,
79                 ret.description().c_str());
80     }
81     return binder::Status::ok();
82 }
83 
onCaptureStarted(const CaptureResultExtras & resultExtras,int64_t timestamp)84 binder::Status H2BCameraDeviceCallbacks::onCaptureStarted(
85     const CaptureResultExtras& resultExtras, int64_t timestamp) {
86     using hardware::cameraservice::utils::conversion::convertToHidl;
87     HCaptureResultExtras hCaptureResultExtras = convertToHidl(resultExtras);
88     auto ret = mBase->onCaptureStarted(hCaptureResultExtras, timestamp);
89     if (!ret.isOk()) {
90         ALOGE("%s OnCaptureCallback failed due to %s",__FUNCTION__,
91               ret.description().c_str());
92     }
93     return binder::Status::ok();
94 }
95 
convertResultMetadataToHidl(const camera_metadata_t * rawMetadata,FmqSizeOrMetadata * hResultMetadata)96 void H2BCameraDeviceCallbacks::convertResultMetadataToHidl(const camera_metadata_t *rawMetadata,
97                                                            FmqSizeOrMetadata *hResultMetadata) {
98     // First try writing to fmq.
99     size_t metadata_size = get_camera_metadata_size(rawMetadata);
100     if ((metadata_size > 0) &&
101         (mCaptureResultMetadataQueue->availableToWrite() > 0)) {
102         if (mCaptureResultMetadataQueue->write((uint8_t *)rawMetadata, metadata_size)) {
103             hResultMetadata->fmqMetadataSize(metadata_size);
104         } else {
105             ALOGW("%s Couldn't use fmq, falling back to hwbinder", __FUNCTION__);
106             HCameraMetadata metadata;
107             hardware::cameraservice::utils::conversion::convertToHidl(rawMetadata, &metadata);
108             hResultMetadata->metadata(std::move(metadata));
109         }
110     }
111 }
112 
onMessageReceived(const sp<AMessage> & msg)113 void H2BCameraDeviceCallbacks::CallbackHandler::onMessageReceived(const sp<AMessage> &msg) {
114     sp<RefBase> obj = nullptr;
115     sp<ResultWrapper> resultWrapper = nullptr;
116     bool found = false;
117     switch (msg->what()) {
118         case kWhatResultReceived:
119             found = msg->findObject(kResultKey, &obj);
120             if (!found || obj == nullptr) {
121                 ALOGE("Cannot find result object in callback message");
122                 return;
123             }
124             resultWrapper = static_cast<ResultWrapper *>(obj.get());
125             processResultMessage(resultWrapper);
126             break;
127         default:
128             ALOGE("Unknown callback sent");
129             break;
130     }
131     return;
132 }
133 
processResultMessage(sp<ResultWrapper> & resultWrapper)134 void H2BCameraDeviceCallbacks::CallbackHandler::processResultMessage(
135     sp<ResultWrapper> &resultWrapper) {
136     sp<H2BCameraDeviceCallbacks> 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     HCaptureResultExtras hResultExtras =
144             hardware::cameraservice::utils::conversion::convertToHidl(resultExtras);
145 
146     // Convert Metadata into HCameraMetadata;
147     FmqSizeOrMetadata hResult;
148     using hardware::cameraservice::utils::conversion::aidl::filterVndkKeys;
149     if (filterVndkKeys(mVndkVersion, result, /*isStatic*/false) != OK) {
150         ALOGE("%s: filtering vndk keys from result failed, not sending onResultReceived callback",
151                 __FUNCTION__);
152         return;
153     }
154     const camera_metadata_t *rawMetadata = result.getAndLock();
155     converter->convertResultMetadataToHidl(rawMetadata, &hResult);
156     result.unlock(rawMetadata);
157     auto &physicalCaptureResultInfos = resultWrapper->mPhysicalCaptureResultInfos;
158     hidl_vec<HPhysicalCaptureResultInfo> hPhysicalCaptureResultInfos =
159             hardware::cameraservice::utils::conversion::convertToHidl(
160                     physicalCaptureResultInfos, converter->mCaptureResultMetadataQueue);
161     auto ret = converter->mBase->onResultReceived(hResult, hResultExtras,
162                                                   hPhysicalCaptureResultInfos);
163     if (!ret.isOk()) {
164           ALOGE("%s OnResultReceived callback failed due to %s",__FUNCTION__,
165                 ret.description().c_str());
166     }
167 }
168 
onResultReceived(const CameraMetadataNative & result,const CaptureResultExtras & resultExtras,const::std::vector<PhysicalCaptureResultInfo> & physicalCaptureResultInfos)169 binder::Status H2BCameraDeviceCallbacks::onResultReceived(
170     const CameraMetadataNative& result,
171     const CaptureResultExtras& resultExtras,
172     const ::std::vector<PhysicalCaptureResultInfo>& physicalCaptureResultInfos) {
173     // Wrap CameraMetadata, resultExtras and physicalCaptureResultInfos in on
174     // sp<RefBase>-able structure and post it.
175     sp<ResultWrapper> resultWrapper = new ResultWrapper(const_cast<CameraMetadataNative &>(result),
176                                                         resultExtras, physicalCaptureResultInfos);
177     sp<AMessage> msg = new AMessage(kWhatResultReceived, mHandler);
178     msg->setObject(kResultKey, resultWrapper);
179     msg->post();
180     return binder::Status::ok();
181 }
182 
onPrepared(int32_t streamId)183 binder::Status H2BCameraDeviceCallbacks::onPrepared(int32_t streamId) {
184     // not implemented
185     // To silence Wunused-parameter.
186     (void) streamId;
187     return binder::Status::ok();
188 }
189 
onRepeatingRequestError(int64_t lastFrameNumber,int32_t repeatingRequestId)190 binder::Status H2BCameraDeviceCallbacks::onRepeatingRequestError(
191     int64_t lastFrameNumber,
192     int32_t repeatingRequestId) {
193     auto ret =
194         mBase->onRepeatingRequestError(lastFrameNumber, repeatingRequestId);
195     if (!ret.isOk()) {
196         ALOGE("%s OnRepeatingRequestEror callback failed due to %s",__FUNCTION__,
197               ret.description().c_str());
198     }
199     return binder::Status::ok();
200 }
201 
onRequestQueueEmpty()202 binder::Status H2BCameraDeviceCallbacks::onRequestQueueEmpty() {
203     // not implemented
204     return binder::Status::ok();
205 }
206 
207 } // implementation
208 } // V2_0
209 } // device
210 } // cameraservice
211 } // frameworks
212 } // android
213