blob: 46a8dae31733e93f18bfeb41efa67cf71406c601 [file] [log] [blame]
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001/*
2 * Copyright (C) 2015 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_NDEBUG 0
18#define LOG_TAG "ACameraDevice"
19
Yin-Chia Yehead91462016-01-06 16:45:08 -080020#include <vector>
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080021#include <inttypes.h>
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080022#include <android/hardware/ICameraService.h>
Yin-Chia Yehead91462016-01-06 16:45:08 -080023#include <gui/Surface.h>
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080024#include "ACameraDevice.h"
25#include "ACameraMetadata.h"
26#include "ACaptureRequest.h"
Yin-Chia Yehead91462016-01-06 16:45:08 -080027#include "ACameraCaptureSession.h"
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080028
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -080029#include "ACameraCaptureSession.inc"
30
Jayant Chowdharya8488c92019-06-21 12:45:34 -070031ACameraDevice::~ACameraDevice() {
Jayant Chowdharya8bf1c62019-09-26 08:50:17 -070032 mDevice->stopLooperAndDisconnect();
Jayant Chowdharya8488c92019-06-21 12:45:34 -070033}
34
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080035namespace android {
Jayant Chowdhary6df26072018-11-06 23:55:12 -080036namespace acam {
37
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080038// Static member definitions
Yin-Chia Yehead91462016-01-06 16:45:08 -080039const char* CameraDevice::kContextKey = "Context";
40const char* CameraDevice::kDeviceKey = "Device";
41const char* CameraDevice::kErrorCodeKey = "ErrorCode";
42const char* CameraDevice::kCallbackFpKey = "Callback";
43const char* CameraDevice::kSessionSpKey = "SessionSp";
44const char* CameraDevice::kCaptureRequestKey = "CaptureRequest";
45const char* CameraDevice::kTimeStampKey = "TimeStamp";
46const char* CameraDevice::kCaptureResultKey = "CaptureResult";
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -080047const char* CameraDevice::kPhysicalCaptureResultKey = "PhysicalCaptureResult";
Yin-Chia Yehead91462016-01-06 16:45:08 -080048const char* CameraDevice::kCaptureFailureKey = "CaptureFailure";
49const char* CameraDevice::kSequenceIdKey = "SequenceId";
50const char* CameraDevice::kFrameNumberKey = "FrameNumber";
Yin-Chia Yehe081c592016-03-29 18:26:44 -070051const char* CameraDevice::kAnwKey = "Anw";
Emilian Peevedec62d2019-03-19 17:59:24 -070052const char* CameraDevice::kFailingPhysicalCameraId= "FailingPhysicalCameraId";
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080053
54/**
55 * CameraDevice Implementation
56 */
57CameraDevice::CameraDevice(
58 const char* id,
59 ACameraDevice_StateCallbacks* cb,
Yin-Chia Yehdd045bf2018-08-20 12:39:19 -070060 sp<ACameraMetadata> chars,
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080061 ACameraDevice* wrapper) :
62 mCameraId(id),
63 mAppCallbacks(*cb),
Yin-Chia Yehdd045bf2018-08-20 12:39:19 -070064 mChars(chars),
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080065 mServiceCallback(new ServiceCallback(this)),
66 mWrapper(wrapper),
67 mInError(false),
68 mError(ACAMERA_OK),
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -070069 mIdle(true),
70 mCurrentSession(nullptr) {
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080071 mClosing = false;
72 // Setup looper thread to perfrom device callbacks to app
73 mCbLooper = new ALooper;
74 mCbLooper->setName("C2N-dev-looper");
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -080075 status_t err = mCbLooper->start(
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080076 /*runOnCallingThread*/false,
77 /*canCallJava*/ true,
Yin-Chia Yehead91462016-01-06 16:45:08 -080078 PRIORITY_DEFAULT);
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -080079 if (err != OK) {
80 ALOGE("%s: Unable to start camera device callback looper: %s (%d)",
81 __FUNCTION__, strerror(-err), err);
82 setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_DEVICE);
83 }
Shuzhen Wang6c17e212019-02-19 14:51:47 -080084 mHandler = new CallbackHandler(id);
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080085 mCbLooper->registerHandler(mHandler);
Yin-Chia Yehead91462016-01-06 16:45:08 -080086
Yin-Chia Yeh8aac03f2016-03-03 15:45:23 -080087 const CameraMetadata& metadata = mChars->getInternalData();
88 camera_metadata_ro_entry entry = metadata.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
Yin-Chia Yehead91462016-01-06 16:45:08 -080089 if (entry.count != 1) {
90 ALOGW("%s: bad count %zu for partial result count", __FUNCTION__, entry.count);
91 mPartialResultCount = 1;
92 } else {
93 mPartialResultCount = entry.data.i32[0];
94 }
95
96 entry = metadata.find(ANDROID_LENS_INFO_SHADING_MAP_SIZE);
97 if (entry.count != 2) {
98 ALOGW("%s: bad count %zu for shading map size", __FUNCTION__, entry.count);
99 mShadingMapSize[0] = 0;
100 mShadingMapSize[1] = 0;
101 } else {
102 mShadingMapSize[0] = entry.data.i32[0];
103 mShadingMapSize[1] = entry.data.i32[1];
104 }
Shuzhen Wang6c17e212019-02-19 14:51:47 -0800105
106 size_t physicalIdCnt = 0;
107 const char*const* physicalCameraIds;
108 if (mChars->isLogicalMultiCamera(&physicalIdCnt, &physicalCameraIds)) {
109 for (size_t i = 0; i < physicalIdCnt; i++) {
110 mPhysicalIds.push_back(physicalCameraIds[i]);
111 }
112 }
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800113}
114
Jayant Chowdharya8bf1c62019-09-26 08:50:17 -0700115CameraDevice::~CameraDevice() { }
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800116
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -0700117void
118CameraDevice::postSessionMsgAndCleanup(sp<AMessage>& msg) {
119 msg->post();
120 msg.clear();
121 sp<AMessage> cleanupMsg = new AMessage(kWhatCleanUpSessions, mHandler);
122 cleanupMsg->post();
123}
124
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800125// TODO: cached created request?
126camera_status_t
127CameraDevice::createCaptureRequest(
128 ACameraDevice_request_template templateId,
Shuzhen Wang6c17e212019-02-19 14:51:47 -0800129 const ACameraIdList* physicalIdList,
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800130 ACaptureRequest** request) const {
131 Mutex::Autolock _l(mDeviceLock);
Shuzhen Wang6c17e212019-02-19 14:51:47 -0800132
133 if (physicalIdList != nullptr) {
134 if (physicalIdList->numCameras > static_cast<int>(mPhysicalIds.size())) {
135 ALOGE("%s: physicalIdList size %d exceeds number of available physical cameras %zu",
136 __FUNCTION__, physicalIdList->numCameras, mPhysicalIds.size());
137 return ACAMERA_ERROR_INVALID_PARAMETER;
138 }
139 for (auto i = 0; i < physicalIdList->numCameras; i++) {
140 if (physicalIdList->cameraIds[i] == nullptr) {
141 ALOGE("%s: physicalId is null!", __FUNCTION__);
142 return ACAMERA_ERROR_INVALID_PARAMETER;
143 }
144 if (mPhysicalIds.end() == std::find(
145 mPhysicalIds.begin(), mPhysicalIds.end(), physicalIdList->cameraIds[i])) {
146 ALOGE("%s: Invalid physicalId %s!", __FUNCTION__, physicalIdList->cameraIds[i]);
147 return ACAMERA_ERROR_INVALID_PARAMETER;
148 }
149 }
150 }
151
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800152 camera_status_t ret = checkCameraClosedOrErrorLocked();
153 if (ret != ACAMERA_OK) {
154 return ret;
155 }
156 if (mRemote == nullptr) {
157 return ACAMERA_ERROR_CAMERA_DISCONNECTED;
158 }
159 CameraMetadata rawRequest;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800160 binder::Status remoteRet = mRemote->createDefaultRequest(templateId, &rawRequest);
161 if (remoteRet.serviceSpecificErrorCode() ==
162 hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT) {
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800163 ALOGW("Create capture request failed! template %d is not supported on this device",
164 templateId);
Yin-Chia Yeha22528a2016-05-12 14:03:11 -0700165 return ACAMERA_ERROR_INVALID_PARAMETER;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800166 } else if (!remoteRet.isOk()) {
167 ALOGE("Create capture request failed: %s", remoteRet.toString8().string());
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800168 return ACAMERA_ERROR_UNKNOWN;
169 }
170 ACaptureRequest* outReq = new ACaptureRequest();
171 outReq->settings = new ACameraMetadata(rawRequest.release(), ACameraMetadata::ACM_REQUEST);
Shuzhen Wang6c17e212019-02-19 14:51:47 -0800172 if (physicalIdList != nullptr) {
173 for (auto i = 0; i < physicalIdList->numCameras; i++) {
174 outReq->physicalSettings.emplace(physicalIdList->cameraIds[i],
175 new ACameraMetadata(*(outReq->settings)));
176 }
177 }
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800178 outReq->targets = new ACameraOutputTargets();
179 *request = outReq;
180 return ACAMERA_OK;
181}
182
Yin-Chia Yehead91462016-01-06 16:45:08 -0800183camera_status_t
184CameraDevice::createCaptureSession(
185 const ACaptureSessionOutputContainer* outputs,
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100186 const ACaptureRequest* sessionParameters,
Yin-Chia Yehead91462016-01-06 16:45:08 -0800187 const ACameraCaptureSession_stateCallbacks* callbacks,
188 /*out*/ACameraCaptureSession** session) {
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -0700189 sp<ACameraCaptureSession> currentSession = mCurrentSession.promote();
Yin-Chia Yehead91462016-01-06 16:45:08 -0800190 Mutex::Autolock _l(mDeviceLock);
191 camera_status_t ret = checkCameraClosedOrErrorLocked();
192 if (ret != ACAMERA_OK) {
193 return ret;
194 }
195
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -0700196 if (currentSession != nullptr) {
197 currentSession->closeByDevice();
Yin-Chia Yehead91462016-01-06 16:45:08 -0800198 stopRepeatingLocked();
199 }
200
201 // Create new session
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100202 ret = configureStreamsLocked(outputs, sessionParameters);
Yin-Chia Yehead91462016-01-06 16:45:08 -0800203 if (ret != ACAMERA_OK) {
204 ALOGE("Fail to create new session. cannot configure streams");
205 return ret;
206 }
207
208 ACameraCaptureSession* newSession = new ACameraCaptureSession(
209 mNextSessionId++, outputs, callbacks, this);
210
Yin-Chia Yehead91462016-01-06 16:45:08 -0800211 // set new session as current session
212 newSession->incStrong((void *) ACameraDevice_createCaptureSession);
213 mCurrentSession = newSession;
Yin-Chia Yeh309d05d2016-03-28 10:15:31 -0700214 mFlushing = false;
Yin-Chia Yehead91462016-01-06 16:45:08 -0800215 *session = newSession;
216 return ACAMERA_OK;
217}
218
Shuzhen Wang24810e72019-03-18 10:55:01 -0700219camera_status_t CameraDevice::isSessionConfigurationSupported(
220 const ACaptureSessionOutputContainer* sessionOutputContainer) const {
221 Mutex::Autolock _l(mDeviceLock);
222 camera_status_t ret = checkCameraClosedOrErrorLocked();
223 if (ret != ACAMERA_OK) {
224 return ret;
225 }
226
227 SessionConfiguration sessionConfiguration(0 /*inputWidth*/, 0 /*inputHeight*/,
228 -1 /*inputFormat*/, CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE);
229 for (const auto& output : sessionOutputContainer->mOutputs) {
230 sp<IGraphicBufferProducer> iGBP(nullptr);
231 ret = getIGBPfromAnw(output.mWindow, iGBP);
232 if (ret != ACAMERA_OK) {
233 ALOGE("Camera device %s failed to extract graphic producer from native window",
234 getId());
235 return ret;
236 }
237
238 String16 physicalId16(output.mPhysicalCameraId.c_str());
239 OutputConfiguration outConfig(iGBP, output.mRotation, physicalId16,
240 OutputConfiguration::INVALID_SET_ID, true);
241
242 for (auto& anw : output.mSharedWindows) {
243 ret = getIGBPfromAnw(anw, iGBP);
244 if (ret != ACAMERA_OK) {
245 ALOGE("Camera device %s failed to extract graphic producer from native window",
246 getId());
247 return ret;
248 }
249 outConfig.addGraphicProducer(iGBP);
250 }
251
252 sessionConfiguration.addOutputConfiguration(outConfig);
253 }
254
255 bool supported = false;
256 binder::Status remoteRet = mRemote->isSessionConfigurationSupported(
257 sessionConfiguration, &supported);
258 if (remoteRet.serviceSpecificErrorCode() ==
259 hardware::ICameraService::ERROR_INVALID_OPERATION) {
260 return ACAMERA_ERROR_UNSUPPORTED_OPERATION;
261 } else if (!remoteRet.isOk()) {
262 return ACAMERA_ERROR_UNKNOWN;
263 } else {
264 return supported ? ACAMERA_OK : ACAMERA_ERROR_STREAM_CONFIGURE_FAIL;
265 }
266}
267
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800268camera_status_t CameraDevice::updateOutputConfigurationLocked(ACaptureSessionOutput *output) {
Emilian Peev40ead602017-09-26 15:46:36 +0100269 camera_status_t ret = checkCameraClosedOrErrorLocked();
270 if (ret != ACAMERA_OK) {
271 return ret;
272 }
273
274 if (output == nullptr) {
275 return ACAMERA_ERROR_INVALID_PARAMETER;
276 }
277
278 if (!output->mIsShared) {
279 ALOGE("Error output configuration is not shared");
280 return ACAMERA_ERROR_INVALID_OPERATION;
281 }
282
283 int32_t streamId = -1;
284 for (auto& kvPair : mConfiguredOutputs) {
285 if (kvPair.second.first == output->mWindow) {
286 streamId = kvPair.first;
287 break;
288 }
289 }
290 if (streamId < 0) {
291 ALOGE("Error: Invalid output configuration");
292 return ACAMERA_ERROR_INVALID_PARAMETER;
293 }
294
295 sp<IGraphicBufferProducer> iGBP(nullptr);
296 ret = getIGBPfromAnw(output->mWindow, iGBP);
297 if (ret != ACAMERA_OK) {
298 ALOGE("Camera device %s failed to extract graphic producer from native window",
299 getId());
300 return ret;
301 }
302
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -0800303 String16 physicalId16(output->mPhysicalCameraId.c_str());
304 OutputConfiguration outConfig(iGBP, output->mRotation, physicalId16,
305 OutputConfiguration::INVALID_SET_ID, true);
Emilian Peev40ead602017-09-26 15:46:36 +0100306
307 for (auto& anw : output->mSharedWindows) {
308 ret = getIGBPfromAnw(anw, iGBP);
309 if (ret != ACAMERA_OK) {
310 ALOGE("Camera device %s failed to extract graphic producer from native window",
311 getId());
312 return ret;
313 }
314 outConfig.addGraphicProducer(iGBP);
315 }
316
317 auto remoteRet = mRemote->updateOutputConfiguration(streamId, outConfig);
318 if (!remoteRet.isOk()) {
319 switch (remoteRet.serviceSpecificErrorCode()) {
320 case hardware::ICameraService::ERROR_INVALID_OPERATION:
321 ALOGE("Camera device %s invalid operation: %s", getId(),
322 remoteRet.toString8().string());
323 return ACAMERA_ERROR_INVALID_OPERATION;
324 break;
325 case hardware::ICameraService::ERROR_ALREADY_EXISTS:
326 ALOGE("Camera device %s output surface already exists: %s", getId(),
327 remoteRet.toString8().string());
328 return ACAMERA_ERROR_INVALID_PARAMETER;
329 break;
330 case hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT:
331 ALOGE("Camera device %s invalid input argument: %s", getId(),
332 remoteRet.toString8().string());
333 return ACAMERA_ERROR_INVALID_PARAMETER;
334 break;
335 default:
336 ALOGE("Camera device %s failed to add shared output: %s", getId(),
337 remoteRet.toString8().string());
338 return ACAMERA_ERROR_UNKNOWN;
339 }
340 }
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800341 mConfiguredOutputs[streamId] = std::make_pair(output->mWindow, outConfig);
Emilian Peev40ead602017-09-26 15:46:36 +0100342
343 return ACAMERA_OK;
344}
345
Yin-Chia Yehead91462016-01-06 16:45:08 -0800346camera_status_t
347CameraDevice::allocateCaptureRequest(
348 const ACaptureRequest* request, /*out*/sp<CaptureRequest>& outReq) {
349 camera_status_t ret;
350 sp<CaptureRequest> req(new CaptureRequest());
Shuzhen Wang6c17e212019-02-19 14:51:47 -0800351 req->mPhysicalCameraSettings.push_back({getId(),
Emilian Peevaebbe412018-01-15 13:53:24 +0000352 request->settings->getInternalData()});
Shuzhen Wang6c17e212019-02-19 14:51:47 -0800353 for (auto& entry : request->physicalSettings) {
354 req->mPhysicalCameraSettings.push_back({entry.first,
355 entry.second->getInternalData()});
356 }
Yin-Chia Yehead91462016-01-06 16:45:08 -0800357 req->mIsReprocess = false; // NDK does not support reprocessing yet
Yin-Chia Yehd39b9e32017-10-30 17:39:23 -0700358 req->mContext = request->context;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800359 req->mSurfaceConverted = true; // set to true, and fill in stream/surface idx to speed up IPC
Yin-Chia Yehead91462016-01-06 16:45:08 -0800360
361 for (auto outputTarget : request->targets->mOutputs) {
362 ANativeWindow* anw = outputTarget.mWindow;
363 sp<Surface> surface;
364 ret = getSurfaceFromANativeWindow(anw, surface);
365 if (ret != ACAMERA_OK) {
366 ALOGE("Bad output target in capture request! ret %d", ret);
367 return ret;
368 }
369 req->mSurfaceList.push_back(surface);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800370
371 bool found = false;
372 // lookup stream/surface ID
373 for (const auto& kvPair : mConfiguredOutputs) {
374 int streamId = kvPair.first;
375 const OutputConfiguration& outConfig = kvPair.second.second;
376 const auto& gbps = outConfig.getGraphicBufferProducers();
377 for (int surfaceId = 0; surfaceId < (int) gbps.size(); surfaceId++) {
378 if (gbps[surfaceId] == surface->getIGraphicBufferProducer()) {
379 found = true;
380 req->mStreamIdxList.push_back(streamId);
381 req->mSurfaceIdxList.push_back(surfaceId);
382 break;
383 }
384 }
385 if (found) {
386 break;
387 }
388 }
389 if (!found) {
390 ALOGE("Unconfigured output target %p in capture request!", anw);
391 return ret;
392 }
Yin-Chia Yehead91462016-01-06 16:45:08 -0800393 }
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800394
Yin-Chia Yehead91462016-01-06 16:45:08 -0800395 outReq = req;
396 return ACAMERA_OK;
397}
398
399ACaptureRequest*
Shuzhen Wang6c17e212019-02-19 14:51:47 -0800400CameraDevice::allocateACaptureRequest(sp<CaptureRequest>& req, const std::string& deviceId) {
Yin-Chia Yehead91462016-01-06 16:45:08 -0800401 ACaptureRequest* pRequest = new ACaptureRequest();
Shuzhen Wang6c17e212019-02-19 14:51:47 -0800402 for (auto& entry : req->mPhysicalCameraSettings) {
403 CameraMetadata clone = entry.settings;
404 if (entry.id == deviceId) {
405 pRequest->settings = new ACameraMetadata(clone.release(), ACameraMetadata::ACM_REQUEST);
406 } else {
407 pRequest->physicalSettings.emplace(entry.id,
408 new ACameraMetadata(clone.release(), ACameraMetadata::ACM_REQUEST));
409 }
410 }
Yin-Chia Yehead91462016-01-06 16:45:08 -0800411 pRequest->targets = new ACameraOutputTargets();
412 for (size_t i = 0; i < req->mSurfaceList.size(); i++) {
413 ANativeWindow* anw = static_cast<ANativeWindow*>(req->mSurfaceList[i].get());
414 ACameraOutputTarget outputTarget(anw);
415 pRequest->targets->mOutputs.insert(outputTarget);
416 }
Yin-Chia Yehd39b9e32017-10-30 17:39:23 -0700417 pRequest->context = req->mContext;
Yin-Chia Yehead91462016-01-06 16:45:08 -0800418 return pRequest;
419}
420
421void
422CameraDevice::freeACaptureRequest(ACaptureRequest* req) {
423 if (req == nullptr) {
424 return;
425 }
Yin-Chia Yehdd045bf2018-08-20 12:39:19 -0700426 req->settings.clear();
Shuzhen Wang6c17e212019-02-19 14:51:47 -0800427 req->physicalSettings.clear();
Yin-Chia Yehead91462016-01-06 16:45:08 -0800428 delete req->targets;
429 delete req;
430}
431
432void
433CameraDevice::notifySessionEndOfLifeLocked(ACameraCaptureSession* session) {
434 if (isClosed()) {
435 // Device is closing already. do nothing
436 return;
437 }
438
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -0700439 if (mCurrentSession != session) {
Yin-Chia Yehead91462016-01-06 16:45:08 -0800440 // Session has been replaced by other seesion or device is closed
441 return;
442 }
443 mCurrentSession = nullptr;
444
445 // Should not happen
446 if (!session->mIsClosed) {
447 ALOGE("Error: unclosed session %p reaches end of life!", session);
448 setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_DEVICE);
449 return;
450 }
451
452 // No new session, unconfigure now
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100453 camera_status_t ret = configureStreamsLocked(nullptr, nullptr);
Yin-Chia Yehead91462016-01-06 16:45:08 -0800454 if (ret != ACAMERA_OK) {
455 ALOGE("Unconfigure stream failed. Device might still be configured! ret %d", ret);
456 }
457}
458
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800459void
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -0700460CameraDevice::disconnectLocked(sp<ACameraCaptureSession>& session) {
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800461 if (mClosing.exchange(true)) {
462 // Already closing, just return
463 ALOGW("Camera device %s is already closing.", getId());
464 return;
465 }
466
467 if (mRemote != nullptr) {
468 mRemote->disconnect();
469 }
470 mRemote = nullptr;
Yin-Chia Yehead91462016-01-06 16:45:08 -0800471
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -0700472 if (session != nullptr) {
473 session->closeByDevice();
Yin-Chia Yehead91462016-01-06 16:45:08 -0800474 }
475}
476
477camera_status_t
478CameraDevice::stopRepeatingLocked() {
479 camera_status_t ret = checkCameraClosedOrErrorLocked();
480 if (ret != ACAMERA_OK) {
481 ALOGE("Camera %s stop repeating failed! ret %d", getId(), ret);
482 return ret;
483 }
484 if (mRepeatingSequenceId != REQUEST_ID_NONE) {
485 int repeatingSequenceId = mRepeatingSequenceId;
486 mRepeatingSequenceId = REQUEST_ID_NONE;
487
488 int64_t lastFrameNumber;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800489 binder::Status remoteRet = mRemote->cancelRequest(repeatingSequenceId, &lastFrameNumber);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700490 if (remoteRet.serviceSpecificErrorCode() ==
491 hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT) {
492 ALOGV("Repeating request is already stopped.");
493 return ACAMERA_OK;
494 } else if (!remoteRet.isOk()) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800495 ALOGE("Stop repeating request fails in remote: %s", remoteRet.toString8().string());
Yin-Chia Yehead91462016-01-06 16:45:08 -0800496 return ACAMERA_ERROR_UNKNOWN;
497 }
498 checkRepeatingSequenceCompleteLocked(repeatingSequenceId, lastFrameNumber);
499 }
500 return ACAMERA_OK;
501}
502
503camera_status_t
Yin-Chia Yeh309d05d2016-03-28 10:15:31 -0700504CameraDevice::flushLocked(ACameraCaptureSession* session) {
505 camera_status_t ret = checkCameraClosedOrErrorLocked();
506 if (ret != ACAMERA_OK) {
507 ALOGE("Camera %s abort captures failed! ret %d", getId(), ret);
508 return ret;
509 }
510
511 // This should never happen because creating a new session will close
512 // previous one and thus reject any API call from previous session.
513 // But still good to check here in case something unexpected happen.
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -0700514 if (mCurrentSession != session) {
Yin-Chia Yeh309d05d2016-03-28 10:15:31 -0700515 ALOGE("Camera %s session %p is not current active session!", getId(), session);
516 return ACAMERA_ERROR_INVALID_OPERATION;
517 }
518
519 if (mFlushing) {
520 ALOGW("Camera %s is already aborting captures", getId());
521 return ACAMERA_OK;
522 }
523
524 mFlushing = true;
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -0700525
Yin-Chia Yeh309d05d2016-03-28 10:15:31 -0700526 // Send onActive callback to guarantee there is always active->ready transition
527 sp<AMessage> msg = new AMessage(kWhatSessionStateCb, mHandler);
528 msg->setPointer(kContextKey, session->mUserSessionCallback.context);
529 msg->setObject(kSessionSpKey, session);
530 msg->setPointer(kCallbackFpKey, (void*) session->mUserSessionCallback.onActive);
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -0700531 postSessionMsgAndCleanup(msg);
Yin-Chia Yeh309d05d2016-03-28 10:15:31 -0700532
533 // If device is already idling, send callback and exit early
534 if (mIdle) {
535 sp<AMessage> msg = new AMessage(kWhatSessionStateCb, mHandler);
536 msg->setPointer(kContextKey, session->mUserSessionCallback.context);
537 msg->setObject(kSessionSpKey, session);
538 msg->setPointer(kCallbackFpKey, (void*) session->mUserSessionCallback.onReady);
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -0700539 postSessionMsgAndCleanup(msg);
Yin-Chia Yeh309d05d2016-03-28 10:15:31 -0700540 mFlushing = false;
541 return ACAMERA_OK;
542 }
543
544 int64_t lastFrameNumber;
545 binder::Status remoteRet = mRemote->flush(&lastFrameNumber);
546 if (!remoteRet.isOk()) {
547 ALOGE("Abort captures fails in remote: %s", remoteRet.toString8().string());
548 return ACAMERA_ERROR_UNKNOWN;
549 }
550 if (mRepeatingSequenceId != REQUEST_ID_NONE) {
551 checkRepeatingSequenceCompleteLocked(mRepeatingSequenceId, lastFrameNumber);
552 }
553 return ACAMERA_OK;
554}
555
556camera_status_t
Yin-Chia Yehead91462016-01-06 16:45:08 -0800557CameraDevice::waitUntilIdleLocked() {
558 camera_status_t ret = checkCameraClosedOrErrorLocked();
559 if (ret != ACAMERA_OK) {
560 ALOGE("Wait until camera %s idle failed! ret %d", getId(), ret);
561 return ret;
562 }
563
564 if (mRepeatingSequenceId != REQUEST_ID_NONE) {
565 ALOGE("Camera device %s won't go to idle when there is repeating request!", getId());
566 return ACAMERA_ERROR_INVALID_OPERATION;
567 }
568
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800569 binder::Status remoteRet = mRemote->waitUntilIdle();
570 if (!remoteRet.isOk()) {
571 ALOGE("Camera device %s waitUntilIdle failed: %s", getId(), remoteRet.toString8().string());
Yin-Chia Yehead91462016-01-06 16:45:08 -0800572 // TODO: define a function to convert status_t -> camera_status_t
573 return ACAMERA_ERROR_UNKNOWN;
574 }
575
576 return ACAMERA_OK;
577}
578
579camera_status_t
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700580CameraDevice::getIGBPfromAnw(
581 ANativeWindow* anw,
Yin-Chia Yehead91462016-01-06 16:45:08 -0800582 sp<IGraphicBufferProducer>& out) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800583 sp<Surface> surface;
584 camera_status_t ret = getSurfaceFromANativeWindow(anw, surface);
585 if (ret != ACAMERA_OK) {
586 return ret;
Yin-Chia Yehead91462016-01-06 16:45:08 -0800587 }
Yin-Chia Yehead91462016-01-06 16:45:08 -0800588 out = surface->getIGraphicBufferProducer();
589 return ACAMERA_OK;
590}
591
592camera_status_t
593CameraDevice::getSurfaceFromANativeWindow(
594 ANativeWindow* anw, sp<Surface>& out) {
595 if (anw == nullptr) {
596 ALOGE("Error: output ANativeWindow is null");
597 return ACAMERA_ERROR_INVALID_PARAMETER;
598 }
599 int value;
600 int err = (*anw->query)(anw, NATIVE_WINDOW_CONCRETE_TYPE, &value);
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800601 if (err != OK || value != NATIVE_WINDOW_SURFACE) {
Yin-Chia Yehead91462016-01-06 16:45:08 -0800602 ALOGE("Error: ANativeWindow is not backed by Surface!");
603 return ACAMERA_ERROR_INVALID_PARAMETER;
604 }
605 sp<Surface> surface(static_cast<Surface*>(anw));
606 out = surface;
607 return ACAMERA_OK;
608}
609
610camera_status_t
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100611CameraDevice::configureStreamsLocked(const ACaptureSessionOutputContainer* outputs,
612 const ACaptureRequest* sessionParameters) {
Yin-Chia Yehead91462016-01-06 16:45:08 -0800613 ACaptureSessionOutputContainer emptyOutput;
614 if (outputs == nullptr) {
615 outputs = &emptyOutput;
616 }
617
Yin-Chia Yehead91462016-01-06 16:45:08 -0800618 camera_status_t ret = checkCameraClosedOrErrorLocked();
619 if (ret != ACAMERA_OK) {
620 return ret;
621 }
622
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700623 std::set<std::pair<ANativeWindow*, OutputConfiguration>> outputSet;
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800624 for (const auto& outConfig : outputs->mOutputs) {
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700625 ANativeWindow* anw = outConfig.mWindow;
Yin-Chia Yehead91462016-01-06 16:45:08 -0800626 sp<IGraphicBufferProducer> iGBP(nullptr);
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700627 ret = getIGBPfromAnw(anw, iGBP);
Yin-Chia Yehead91462016-01-06 16:45:08 -0800628 if (ret != ACAMERA_OK) {
629 return ret;
630 }
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -0800631 String16 physicalId16(outConfig.mPhysicalCameraId.c_str());
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700632 outputSet.insert(std::make_pair(
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -0800633 anw, OutputConfiguration(iGBP, outConfig.mRotation, physicalId16,
Emilian Peev40ead602017-09-26 15:46:36 +0100634 OutputConfiguration::INVALID_SET_ID, outConfig.mIsShared)));
Yin-Chia Yehead91462016-01-06 16:45:08 -0800635 }
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700636 auto addSet = outputSet;
Yin-Chia Yehead91462016-01-06 16:45:08 -0800637 std::vector<int> deleteList;
638
639 // Determine which streams need to be created, which to be deleted
640 for (auto& kvPair : mConfiguredOutputs) {
641 int streamId = kvPair.first;
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700642 auto& outputPair = kvPair.second;
643 if (outputSet.count(outputPair) == 0) {
Yin-Chia Yehead91462016-01-06 16:45:08 -0800644 deleteList.push_back(streamId); // Need to delete a no longer needed stream
645 } else {
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700646 addSet.erase(outputPair); // No need to add already existing stream
Yin-Chia Yehead91462016-01-06 16:45:08 -0800647 }
648 }
649
650 ret = stopRepeatingLocked();
651 if (ret != ACAMERA_OK) {
652 ALOGE("Camera device %s stop repeating failed, ret %d", getId(), ret);
653 return ret;
654 }
655
656 ret = waitUntilIdleLocked();
657 if (ret != ACAMERA_OK) {
658 ALOGE("Camera device %s wait until idle failed, ret %d", getId(), ret);
659 return ret;
660 }
661
662 // Send onReady to previous session
663 // CurrentSession will be updated after configureStreamLocked, so here
664 // mCurrentSession is the session to be replaced by a new session
665 if (!mIdle && mCurrentSession != nullptr) {
666 if (mBusySession != mCurrentSession) {
667 ALOGE("Current session != busy session");
668 setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_DEVICE);
669 return ACAMERA_ERROR_CAMERA_DEVICE;
670 }
671 sp<AMessage> msg = new AMessage(kWhatSessionStateCb, mHandler);
672 msg->setPointer(kContextKey, mBusySession->mUserSessionCallback.context);
673 msg->setObject(kSessionSpKey, mBusySession);
674 msg->setPointer(kCallbackFpKey, (void*) mBusySession->mUserSessionCallback.onReady);
675 mBusySession.clear();
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -0700676 postSessionMsgAndCleanup(msg);
Yin-Chia Yehead91462016-01-06 16:45:08 -0800677 }
678 mIdle = true;
679
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800680 binder::Status remoteRet = mRemote->beginConfigure();
681 if (!remoteRet.isOk()) {
682 ALOGE("Camera device %s begin configure failed: %s", getId(), remoteRet.toString8().string());
Yin-Chia Yehead91462016-01-06 16:45:08 -0800683 return ACAMERA_ERROR_UNKNOWN;
684 }
685
686 // delete to-be-deleted streams
687 for (auto streamId : deleteList) {
688 remoteRet = mRemote->deleteStream(streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800689 if (!remoteRet.isOk()) {
690 ALOGE("Camera device %s failed to remove stream %d: %s", getId(), streamId,
691 remoteRet.toString8().string());
Yin-Chia Yehead91462016-01-06 16:45:08 -0800692 return ACAMERA_ERROR_UNKNOWN;
693 }
694 mConfiguredOutputs.erase(streamId);
695 }
696
697 // add new streams
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800698 for (const auto& outputPair : addSet) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800699 int streamId;
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700700 remoteRet = mRemote->createStream(outputPair.second, &streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800701 if (!remoteRet.isOk()) {
702 ALOGE("Camera device %s failed to create stream: %s", getId(),
703 remoteRet.toString8().string());
Yin-Chia Yehead91462016-01-06 16:45:08 -0800704 return ACAMERA_ERROR_UNKNOWN;
705 }
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700706 mConfiguredOutputs.insert(std::make_pair(streamId, outputPair));
Yin-Chia Yehead91462016-01-06 16:45:08 -0800707 }
708
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100709 CameraMetadata params;
710 if ((sessionParameters != nullptr) && (sessionParameters->settings != nullptr)) {
711 params.append(sessionParameters->settings->getInternalData());
712 }
713 remoteRet = mRemote->endConfigure(/*isConstrainedHighSpeed*/ false, params);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800714 if (remoteRet.serviceSpecificErrorCode() == hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT) {
715 ALOGE("Camera device %s cannnot support app output configuration: %s", getId(),
716 remoteRet.toString8().string());
Yin-Chia Yehead91462016-01-06 16:45:08 -0800717 return ACAMERA_ERROR_STREAM_CONFIGURE_FAIL;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800718 } else if (!remoteRet.isOk()) {
719 ALOGE("Camera device %s end configure failed: %s", getId(), remoteRet.toString8().string());
Yin-Chia Yehead91462016-01-06 16:45:08 -0800720 return ACAMERA_ERROR_UNKNOWN;
721 }
722
723 return ACAMERA_OK;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800724}
725
726void
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800727CameraDevice::setRemoteDevice(sp<hardware::camera2::ICameraDeviceUser> remote) {
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800728 Mutex::Autolock _l(mDeviceLock);
729 mRemote = remote;
730}
731
732camera_status_t
733CameraDevice::checkCameraClosedOrErrorLocked() const {
734 if (mRemote == nullptr) {
735 ALOGE("%s: camera device already closed", __FUNCTION__);
736 return ACAMERA_ERROR_CAMERA_DISCONNECTED;
737 }
738 if (mInError) {// triggered by onDeviceError
739 ALOGE("%s: camera device has encountered a serious error", __FUNCTION__);
740 return mError;
741 }
742 return ACAMERA_OK;
743}
744
745void
Yin-Chia Yehead91462016-01-06 16:45:08 -0800746CameraDevice::setCameraDeviceErrorLocked(camera_status_t error) {
747 mInError = true;
748 mError = error;
749 return;
750}
751
752void
753CameraDevice::FrameNumberTracker::updateTracker(int64_t frameNumber, bool isError) {
754 ALOGV("updateTracker frame %" PRId64 " isError %d", frameNumber, isError);
755 if (isError) {
756 mFutureErrorSet.insert(frameNumber);
757 } else if (frameNumber <= mCompletedFrameNumber) {
758 ALOGE("Frame number %" PRId64 " decreased! current fn %" PRId64,
759 frameNumber, mCompletedFrameNumber);
760 return;
761 } else {
762 if (frameNumber != mCompletedFrameNumber + 1) {
763 ALOGE("Frame number out of order. Expect %" PRId64 " but get %" PRId64,
764 mCompletedFrameNumber + 1, frameNumber);
765 // Do not assert as in java implementation
766 }
767 mCompletedFrameNumber = frameNumber;
768 }
769 update();
770}
771
772void
773CameraDevice::FrameNumberTracker::update() {
774 for (auto it = mFutureErrorSet.begin(); it != mFutureErrorSet.end();) {
775 int64_t errorFrameNumber = *it;
776 if (errorFrameNumber == mCompletedFrameNumber + 1) {
777 mCompletedFrameNumber++;
778 it = mFutureErrorSet.erase(it);
779 } else if (errorFrameNumber <= mCompletedFrameNumber) {
780 // This should not happen, but deal with it anyway
781 ALOGE("Completd frame number passed through current frame number!");
782 // erase the old error since it's no longer useful
783 it = mFutureErrorSet.erase(it);
784 } else {
785 // Normal requests hasn't catched up error frames, just break
786 break;
787 }
788 }
789 ALOGV("Update complete frame %" PRId64, mCompletedFrameNumber);
790}
791
792void
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800793CameraDevice::onCaptureErrorLocked(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800794 int32_t errorCode,
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800795 const CaptureResultExtras& resultExtras) {
Yin-Chia Yehead91462016-01-06 16:45:08 -0800796 int sequenceId = resultExtras.requestId;
797 int64_t frameNumber = resultExtras.frameNumber;
798 int32_t burstId = resultExtras.burstId;
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700799 auto it = mSequenceCallbackMap.find(sequenceId);
800 if (it == mSequenceCallbackMap.end()) {
801 ALOGE("%s: Error: capture sequence index %d not found!",
802 __FUNCTION__, sequenceId);
803 setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_SERVICE);
Yin-Chia Yehead91462016-01-06 16:45:08 -0800804 return;
805 }
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700806
807 CallbackHolder cbh = (*it).second;
808 sp<ACameraCaptureSession> session = cbh.mSession;
809 if ((size_t) burstId >= cbh.mRequests.size()) {
810 ALOGE("%s: Error: request index %d out of bound (size %zu)",
811 __FUNCTION__, burstId, cbh.mRequests.size());
812 setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_SERVICE);
813 return;
814 }
815 sp<CaptureRequest> request = cbh.mRequests[burstId];
816
817 // Handle buffer error
818 if (errorCode == hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER) {
819 int32_t streamId = resultExtras.errorStreamId;
820 ACameraCaptureSession_captureCallback_bufferLost onBufferLost =
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -0800821 cbh.mOnCaptureBufferLost;
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700822 auto outputPairIt = mConfiguredOutputs.find(streamId);
823 if (outputPairIt == mConfiguredOutputs.end()) {
824 ALOGE("%s: Error: stream id %d does not exist", __FUNCTION__, streamId);
Yin-Chia Yehead91462016-01-06 16:45:08 -0800825 setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_SERVICE);
826 return;
827 }
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700828
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800829 const auto& gbps = outputPairIt->second.second.getGraphicBufferProducers();
830 for (const auto& outGbp : gbps) {
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800831 for (const auto& surface : request->mSurfaceList) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800832 if (surface->getIGraphicBufferProducer() == outGbp) {
833 ANativeWindow* anw = static_cast<ANativeWindow*>(surface.get());
834 ALOGV("Camera %s Lost output buffer for ANW %p frame %" PRId64,
835 getId(), anw, frameNumber);
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700836
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800837 sp<AMessage> msg = new AMessage(kWhatCaptureBufferLost, mHandler);
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -0800838 msg->setPointer(kContextKey, cbh.mContext);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800839 msg->setObject(kSessionSpKey, session);
840 msg->setPointer(kCallbackFpKey, (void*) onBufferLost);
841 msg->setObject(kCaptureRequestKey, request);
842 msg->setPointer(kAnwKey, (void*) anw);
843 msg->setInt64(kFrameNumberKey, frameNumber);
844 postSessionMsgAndCleanup(msg);
845 }
846 }
847 }
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700848 } else { // Handle other capture failures
849 // Fire capture failure callback if there is one registered
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -0800850 ACameraCaptureSession_captureCallback_failed onError = cbh.mOnCaptureFailed;
Yin-Chia Yehead91462016-01-06 16:45:08 -0800851 sp<CameraCaptureFailure> failure(new CameraCaptureFailure());
852 failure->frameNumber = frameNumber;
853 // TODO: refine this when implementing flush
854 failure->reason = CAPTURE_FAILURE_REASON_ERROR;
855 failure->sequenceId = sequenceId;
856 failure->wasImageCaptured = (errorCode ==
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800857 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT);
Yin-Chia Yehead91462016-01-06 16:45:08 -0800858
Emilian Peevedec62d2019-03-19 17:59:24 -0700859 sp<AMessage> msg = new AMessage(cbh.mIsLogicalCameraCallback ? kWhatLogicalCaptureFail :
860 kWhatCaptureFail, mHandler);
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -0800861 msg->setPointer(kContextKey, cbh.mContext);
Yin-Chia Yehead91462016-01-06 16:45:08 -0800862 msg->setObject(kSessionSpKey, session);
Emilian Peevedec62d2019-03-19 17:59:24 -0700863 if (cbh.mIsLogicalCameraCallback) {
864 if (resultExtras.errorPhysicalCameraId.size() > 0) {
865 String8 cameraId(resultExtras.errorPhysicalCameraId);
866 msg->setString(kFailingPhysicalCameraId, cameraId.string(), cameraId.size());
867 }
868 msg->setPointer(kCallbackFpKey, (void*) cbh.mOnLogicalCameraCaptureFailed);
869 } else {
870 msg->setPointer(kCallbackFpKey, (void*) onError);
871 }
Yin-Chia Yehead91462016-01-06 16:45:08 -0800872 msg->setObject(kCaptureRequestKey, request);
873 msg->setObject(kCaptureFailureKey, failure);
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -0700874 postSessionMsgAndCleanup(msg);
Yin-Chia Yehead91462016-01-06 16:45:08 -0800875
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700876 // Update tracker
877 mFrameNumberTracker.updateTracker(frameNumber, /*isError*/true);
878 checkAndFireSequenceCompleteLocked();
879 }
880 return;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800881}
882
Jayant Chowdharya8bf1c62019-09-26 08:50:17 -0700883void CameraDevice::stopLooperAndDisconnect() {
Jayant Chowdharya8488c92019-06-21 12:45:34 -0700884 Mutex::Autolock _l(mDeviceLock);
Jayant Chowdharya8bf1c62019-09-26 08:50:17 -0700885 sp<ACameraCaptureSession> session = mCurrentSession.promote();
886 if (!isClosed()) {
887 disconnectLocked(session);
888 }
889 mCurrentSession = nullptr;
890
Jayant Chowdharya8488c92019-06-21 12:45:34 -0700891 if (mCbLooper != nullptr) {
892 mCbLooper->unregisterHandler(mHandler->id());
893 mCbLooper->stop();
894 }
895 mCbLooper.clear();
896 mHandler.clear();
897}
898
Shuzhen Wang6c17e212019-02-19 14:51:47 -0800899CameraDevice::CallbackHandler::CallbackHandler(const char* id) : mId(id) {
900}
901
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800902void CameraDevice::CallbackHandler::onMessageReceived(
903 const sp<AMessage> &msg) {
904 switch (msg->what()) {
905 case kWhatOnDisconnected:
906 case kWhatOnError:
Yin-Chia Yehead91462016-01-06 16:45:08 -0800907 case kWhatSessionStateCb:
908 case kWhatCaptureStart:
909 case kWhatCaptureResult:
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -0800910 case kWhatLogicalCaptureResult:
Yin-Chia Yehead91462016-01-06 16:45:08 -0800911 case kWhatCaptureFail:
Emilian Peevedec62d2019-03-19 17:59:24 -0700912 case kWhatLogicalCaptureFail:
Yin-Chia Yehead91462016-01-06 16:45:08 -0800913 case kWhatCaptureSeqEnd:
914 case kWhatCaptureSeqAbort:
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700915 case kWhatCaptureBufferLost:
Yin-Chia Yehead91462016-01-06 16:45:08 -0800916 ALOGV("%s: Received msg %d", __FUNCTION__, msg->what());
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800917 break;
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -0700918 case kWhatCleanUpSessions:
919 mCachedSessions.clear();
920 return;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800921 default:
922 ALOGE("%s:Error: unknown device callback %d", __FUNCTION__, msg->what());
923 return;
924 }
925 // Check the common part of all message
926 void* context;
927 bool found = msg->findPointer(kContextKey, &context);
928 if (!found) {
929 ALOGE("%s: Cannot find callback context!", __FUNCTION__);
930 return;
931 }
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800932 switch (msg->what()) {
933 case kWhatOnDisconnected:
934 {
Yin-Chia Yehead91462016-01-06 16:45:08 -0800935 ACameraDevice* dev;
936 found = msg->findPointer(kDeviceKey, (void**) &dev);
937 if (!found || dev == nullptr) {
938 ALOGE("%s: Cannot find device pointer!", __FUNCTION__);
939 return;
940 }
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800941 ACameraDevice_StateCallback onDisconnected;
Yin-Chia Yehead91462016-01-06 16:45:08 -0800942 found = msg->findPointer(kCallbackFpKey, (void**) &onDisconnected);
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800943 if (!found) {
944 ALOGE("%s: Cannot find onDisconnected!", __FUNCTION__);
945 return;
946 }
Yin-Chia Yehead91462016-01-06 16:45:08 -0800947 if (onDisconnected == nullptr) {
948 return;
949 }
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800950 (*onDisconnected)(context, dev);
951 break;
952 }
953 case kWhatOnError:
954 {
Yin-Chia Yehead91462016-01-06 16:45:08 -0800955 ACameraDevice* dev;
956 found = msg->findPointer(kDeviceKey, (void**) &dev);
957 if (!found || dev == nullptr) {
958 ALOGE("%s: Cannot find device pointer!", __FUNCTION__);
959 return;
960 }
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800961 ACameraDevice_ErrorStateCallback onError;
Yin-Chia Yehead91462016-01-06 16:45:08 -0800962 found = msg->findPointer(kCallbackFpKey, (void**) &onError);
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800963 if (!found) {
964 ALOGE("%s: Cannot find onError!", __FUNCTION__);
965 return;
966 }
967 int errorCode;
968 found = msg->findInt32(kErrorCodeKey, &errorCode);
969 if (!found) {
970 ALOGE("%s: Cannot find error code!", __FUNCTION__);
971 return;
972 }
Yin-Chia Yehead91462016-01-06 16:45:08 -0800973 if (onError == nullptr) {
974 return;
975 }
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800976 (*onError)(context, dev, errorCode);
Yin-Chia Yehead91462016-01-06 16:45:08 -0800977 break;
978 }
979 case kWhatSessionStateCb:
980 case kWhatCaptureStart:
981 case kWhatCaptureResult:
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -0800982 case kWhatLogicalCaptureResult:
Yin-Chia Yehead91462016-01-06 16:45:08 -0800983 case kWhatCaptureFail:
Emilian Peevedec62d2019-03-19 17:59:24 -0700984 case kWhatLogicalCaptureFail:
Yin-Chia Yehead91462016-01-06 16:45:08 -0800985 case kWhatCaptureSeqEnd:
986 case kWhatCaptureSeqAbort:
Yin-Chia Yehe081c592016-03-29 18:26:44 -0700987 case kWhatCaptureBufferLost:
Yin-Chia Yehead91462016-01-06 16:45:08 -0800988 {
989 sp<RefBase> obj;
990 found = msg->findObject(kSessionSpKey, &obj);
991 if (!found || obj == nullptr) {
992 ALOGE("%s: Cannot find session pointer!", __FUNCTION__);
993 return;
994 }
995 sp<ACameraCaptureSession> session(static_cast<ACameraCaptureSession*>(obj.get()));
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -0700996 mCachedSessions.push(session);
Yin-Chia Yehead91462016-01-06 16:45:08 -0800997 sp<CaptureRequest> requestSp = nullptr;
998 switch (msg->what()) {
999 case kWhatCaptureStart:
1000 case kWhatCaptureResult:
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -08001001 case kWhatLogicalCaptureResult:
Yin-Chia Yehead91462016-01-06 16:45:08 -08001002 case kWhatCaptureFail:
Emilian Peevedec62d2019-03-19 17:59:24 -07001003 case kWhatLogicalCaptureFail:
Yin-Chia Yehe081c592016-03-29 18:26:44 -07001004 case kWhatCaptureBufferLost:
Yin-Chia Yehead91462016-01-06 16:45:08 -08001005 found = msg->findObject(kCaptureRequestKey, &obj);
1006 if (!found) {
1007 ALOGE("%s: Cannot find capture request!", __FUNCTION__);
1008 return;
1009 }
1010 requestSp = static_cast<CaptureRequest*>(obj.get());
1011 break;
1012 }
1013
1014 switch (msg->what()) {
1015 case kWhatSessionStateCb:
1016 {
1017 ACameraCaptureSession_stateCallback onState;
1018 found = msg->findPointer(kCallbackFpKey, (void**) &onState);
1019 if (!found) {
1020 ALOGE("%s: Cannot find state callback!", __FUNCTION__);
1021 return;
1022 }
1023 if (onState == nullptr) {
1024 return;
1025 }
1026 (*onState)(context, session.get());
1027 break;
1028 }
1029 case kWhatCaptureStart:
1030 {
1031 ACameraCaptureSession_captureCallback_start onStart;
1032 found = msg->findPointer(kCallbackFpKey, (void**) &onStart);
1033 if (!found) {
1034 ALOGE("%s: Cannot find capture start callback!", __FUNCTION__);
1035 return;
1036 }
1037 if (onStart == nullptr) {
1038 return;
1039 }
1040 int64_t timestamp;
1041 found = msg->findInt64(kTimeStampKey, &timestamp);
1042 if (!found) {
1043 ALOGE("%s: Cannot find timestamp!", __FUNCTION__);
1044 return;
1045 }
Shuzhen Wang6c17e212019-02-19 14:51:47 -08001046 ACaptureRequest* request = allocateACaptureRequest(requestSp, mId);
Yin-Chia Yehead91462016-01-06 16:45:08 -08001047 (*onStart)(context, session.get(), request, timestamp);
1048 freeACaptureRequest(request);
1049 break;
1050 }
1051 case kWhatCaptureResult:
1052 {
1053 ACameraCaptureSession_captureCallback_result onResult;
1054 found = msg->findPointer(kCallbackFpKey, (void**) &onResult);
1055 if (!found) {
1056 ALOGE("%s: Cannot find capture result callback!", __FUNCTION__);
1057 return;
1058 }
1059 if (onResult == nullptr) {
1060 return;
1061 }
1062
1063 found = msg->findObject(kCaptureResultKey, &obj);
1064 if (!found) {
1065 ALOGE("%s: Cannot find capture result!", __FUNCTION__);
1066 return;
1067 }
1068 sp<ACameraMetadata> result(static_cast<ACameraMetadata*>(obj.get()));
Shuzhen Wang6c17e212019-02-19 14:51:47 -08001069 ACaptureRequest* request = allocateACaptureRequest(requestSp, mId);
Yin-Chia Yehead91462016-01-06 16:45:08 -08001070 (*onResult)(context, session.get(), request, result.get());
1071 freeACaptureRequest(request);
1072 break;
1073 }
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -08001074 case kWhatLogicalCaptureResult:
1075 {
1076 ACameraCaptureSession_logicalCamera_captureCallback_result onResult;
1077 found = msg->findPointer(kCallbackFpKey, (void**) &onResult);
1078 if (!found) {
1079 ALOGE("%s: Cannot find logicalCamera capture result callback!",
1080 __FUNCTION__);
1081 return;
1082 }
1083 if (onResult == nullptr) {
1084 return;
1085 }
1086
1087 found = msg->findObject(kCaptureResultKey, &obj);
1088 if (!found) {
1089 ALOGE("%s: Cannot find capture result!", __FUNCTION__);
1090 return;
1091 }
1092 sp<ACameraMetadata> result(static_cast<ACameraMetadata*>(obj.get()));
1093
1094 found = msg->findObject(kPhysicalCaptureResultKey, &obj);
1095 if (!found) {
1096 ALOGE("%s: Cannot find physical capture result!", __FUNCTION__);
1097 return;
1098 }
1099 sp<ACameraPhysicalCaptureResultInfo> physicalResult(
1100 static_cast<ACameraPhysicalCaptureResultInfo*>(obj.get()));
1101 std::vector<PhysicalCaptureResultInfo>& physicalResultInfo =
1102 physicalResult->mPhysicalResultInfo;
1103
1104 std::vector<std::string> physicalCameraIds;
1105 std::vector<sp<ACameraMetadata>> physicalMetadataCopy;
1106 for (size_t i = 0; i < physicalResultInfo.size(); i++) {
1107 String8 physicalId8(physicalResultInfo[i].mPhysicalCameraId);
1108 physicalCameraIds.push_back(physicalId8.c_str());
1109
1110 CameraMetadata clone = physicalResultInfo[i].mPhysicalCameraMetadata;
1111 clone.update(ANDROID_SYNC_FRAME_NUMBER,
1112 &physicalResult->mFrameNumber, /*data_count*/1);
1113 sp<ACameraMetadata> metadata =
1114 new ACameraMetadata(clone.release(), ACameraMetadata::ACM_RESULT);
1115 physicalMetadataCopy.push_back(metadata);
1116 }
1117
1118 std::vector<const char*> physicalCameraIdPtrs;
1119 std::vector<const ACameraMetadata*> physicalMetadataCopyPtrs;
1120 for (size_t i = 0; i < physicalResultInfo.size(); i++) {
1121 physicalCameraIdPtrs.push_back(physicalCameraIds[i].c_str());
1122 physicalMetadataCopyPtrs.push_back(physicalMetadataCopy[i].get());
1123 }
1124
Shuzhen Wang6c17e212019-02-19 14:51:47 -08001125 ACaptureRequest* request = allocateACaptureRequest(requestSp, mId);
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -08001126 (*onResult)(context, session.get(), request, result.get(),
1127 physicalResultInfo.size(), physicalCameraIdPtrs.data(),
1128 physicalMetadataCopyPtrs.data());
1129 freeACaptureRequest(request);
1130 break;
1131 }
Yin-Chia Yehead91462016-01-06 16:45:08 -08001132 case kWhatCaptureFail:
1133 {
1134 ACameraCaptureSession_captureCallback_failed onFail;
1135 found = msg->findPointer(kCallbackFpKey, (void**) &onFail);
1136 if (!found) {
1137 ALOGE("%s: Cannot find capture fail callback!", __FUNCTION__);
1138 return;
1139 }
1140 if (onFail == nullptr) {
1141 return;
1142 }
1143
1144 found = msg->findObject(kCaptureFailureKey, &obj);
1145 if (!found) {
1146 ALOGE("%s: Cannot find capture failure!", __FUNCTION__);
1147 return;
1148 }
1149 sp<CameraCaptureFailure> failureSp(
1150 static_cast<CameraCaptureFailure*>(obj.get()));
1151 ACameraCaptureFailure* failure =
1152 static_cast<ACameraCaptureFailure*>(failureSp.get());
Shuzhen Wang6c17e212019-02-19 14:51:47 -08001153 ACaptureRequest* request = allocateACaptureRequest(requestSp, mId);
Yin-Chia Yehead91462016-01-06 16:45:08 -08001154 (*onFail)(context, session.get(), request, failure);
1155 freeACaptureRequest(request);
Yin-Chia Yehead91462016-01-06 16:45:08 -08001156 break;
1157 }
Emilian Peevedec62d2019-03-19 17:59:24 -07001158 case kWhatLogicalCaptureFail:
1159 {
1160 ACameraCaptureSession_logicalCamera_captureCallback_failed onFail;
1161 found = msg->findPointer(kCallbackFpKey, (void**) &onFail);
1162 if (!found) {
1163 ALOGE("%s: Cannot find capture fail callback!", __FUNCTION__);
1164 return;
1165 }
1166 if (onFail == nullptr) {
1167 return;
1168 }
1169
1170 found = msg->findObject(kCaptureFailureKey, &obj);
1171 if (!found) {
1172 ALOGE("%s: Cannot find capture failure!", __FUNCTION__);
1173 return;
1174 }
1175 sp<CameraCaptureFailure> failureSp(
1176 static_cast<CameraCaptureFailure*>(obj.get()));
1177 ALogicalCameraCaptureFailure failure;
1178 AString physicalCameraId;
1179 found = msg->findString(kFailingPhysicalCameraId, &physicalCameraId);
1180 if (found && !physicalCameraId.empty()) {
1181 failure.physicalCameraId = physicalCameraId.c_str();
1182 } else {
1183 failure.physicalCameraId = nullptr;
1184 }
1185 failure.captureFailure = *failureSp;
1186 ACaptureRequest* request = allocateACaptureRequest(requestSp, mId);
1187 (*onFail)(context, session.get(), request, &failure);
1188 freeACaptureRequest(request);
1189 break;
1190 }
Yin-Chia Yehead91462016-01-06 16:45:08 -08001191 case kWhatCaptureSeqEnd:
1192 {
1193 ACameraCaptureSession_captureCallback_sequenceEnd onSeqEnd;
1194 found = msg->findPointer(kCallbackFpKey, (void**) &onSeqEnd);
1195 if (!found) {
1196 ALOGE("%s: Cannot find sequence end callback!", __FUNCTION__);
1197 return;
1198 }
1199 if (onSeqEnd == nullptr) {
1200 return;
1201 }
1202 int seqId;
1203 found = msg->findInt32(kSequenceIdKey, &seqId);
1204 if (!found) {
1205 ALOGE("%s: Cannot find frame number!", __FUNCTION__);
1206 return;
1207 }
1208 int64_t frameNumber;
1209 found = msg->findInt64(kFrameNumberKey, &frameNumber);
1210 if (!found) {
1211 ALOGE("%s: Cannot find frame number!", __FUNCTION__);
1212 return;
1213 }
1214 (*onSeqEnd)(context, session.get(), seqId, frameNumber);
1215 break;
1216 }
1217 case kWhatCaptureSeqAbort:
1218 {
1219 ACameraCaptureSession_captureCallback_sequenceAbort onSeqAbort;
1220 found = msg->findPointer(kCallbackFpKey, (void**) &onSeqAbort);
1221 if (!found) {
1222 ALOGE("%s: Cannot find sequence end callback!", __FUNCTION__);
1223 return;
1224 }
1225 if (onSeqAbort == nullptr) {
1226 return;
1227 }
1228 int seqId;
1229 found = msg->findInt32(kSequenceIdKey, &seqId);
1230 if (!found) {
1231 ALOGE("%s: Cannot find frame number!", __FUNCTION__);
1232 return;
1233 }
1234 (*onSeqAbort)(context, session.get(), seqId);
1235 break;
1236 }
Yin-Chia Yehe081c592016-03-29 18:26:44 -07001237 case kWhatCaptureBufferLost:
1238 {
1239 ACameraCaptureSession_captureCallback_bufferLost onBufferLost;
1240 found = msg->findPointer(kCallbackFpKey, (void**) &onBufferLost);
1241 if (!found) {
1242 ALOGE("%s: Cannot find buffer lost callback!", __FUNCTION__);
1243 return;
1244 }
1245 if (onBufferLost == nullptr) {
1246 return;
1247 }
1248
1249 ANativeWindow* anw;
1250 found = msg->findPointer(kAnwKey, (void**) &anw);
1251 if (!found) {
1252 ALOGE("%s: Cannot find ANativeWindow!", __FUNCTION__);
1253 return;
1254 }
1255
1256 int64_t frameNumber;
1257 found = msg->findInt64(kFrameNumberKey, &frameNumber);
1258 if (!found) {
1259 ALOGE("%s: Cannot find frame number!", __FUNCTION__);
1260 return;
1261 }
1262
Shuzhen Wang6c17e212019-02-19 14:51:47 -08001263 ACaptureRequest* request = allocateACaptureRequest(requestSp, mId);
Yin-Chia Yehe081c592016-03-29 18:26:44 -07001264 (*onBufferLost)(context, session.get(), request, anw, frameNumber);
1265 freeACaptureRequest(request);
1266 break;
1267 }
Yin-Chia Yehead91462016-01-06 16:45:08 -08001268 }
1269 break;
1270 }
1271 }
1272}
1273
1274CameraDevice::CallbackHolder::CallbackHolder(
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -08001275 sp<ACameraCaptureSession> session,
1276 const Vector<sp<CaptureRequest> >& requests,
1277 bool isRepeating,
1278 ACameraCaptureSession_captureCallbacks* cbs) :
1279 mSession(session), mRequests(requests),
1280 mIsRepeating(isRepeating),
1281 mIsLogicalCameraCallback(false) {
1282 initCaptureCallbacks(cbs);
1283
1284 if (cbs != nullptr) {
1285 mOnCaptureCompleted = cbs->onCaptureCompleted;
Emilian Peevedec62d2019-03-19 17:59:24 -07001286 mOnCaptureFailed = cbs->onCaptureFailed;
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -08001287 }
1288}
1289
1290CameraDevice::CallbackHolder::CallbackHolder(
1291 sp<ACameraCaptureSession> session,
1292 const Vector<sp<CaptureRequest> >& requests,
1293 bool isRepeating,
1294 ACameraCaptureSession_logicalCamera_captureCallbacks* lcbs) :
1295 mSession(session), mRequests(requests),
1296 mIsRepeating(isRepeating),
1297 mIsLogicalCameraCallback(true) {
1298 initCaptureCallbacks(lcbs);
1299
1300 if (lcbs != nullptr) {
1301 mOnLogicalCameraCaptureCompleted = lcbs->onLogicalCameraCaptureCompleted;
Emilian Peevedec62d2019-03-19 17:59:24 -07001302 mOnLogicalCameraCaptureFailed = lcbs->onLogicalCameraCaptureFailed;
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -08001303 }
1304}
Yin-Chia Yehead91462016-01-06 16:45:08 -08001305
1306void
1307CameraDevice::checkRepeatingSequenceCompleteLocked(
1308 const int sequenceId, const int64_t lastFrameNumber) {
1309 ALOGV("Repeating seqId %d lastFrameNumer %" PRId64, sequenceId, lastFrameNumber);
1310 if (lastFrameNumber == NO_FRAMES_CAPTURED) {
1311 if (mSequenceCallbackMap.count(sequenceId) == 0) {
1312 ALOGW("No callback found for sequenceId %d", sequenceId);
1313 return;
1314 }
1315 // remove callback holder from callback map
1316 auto cbIt = mSequenceCallbackMap.find(sequenceId);
1317 CallbackHolder cbh = cbIt->second;
1318 mSequenceCallbackMap.erase(cbIt);
1319 // send seq aborted callback
1320 sp<AMessage> msg = new AMessage(kWhatCaptureSeqAbort, mHandler);
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -08001321 msg->setPointer(kContextKey, cbh.mContext);
Yin-Chia Yehead91462016-01-06 16:45:08 -08001322 msg->setObject(kSessionSpKey, cbh.mSession);
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -08001323 msg->setPointer(kCallbackFpKey, (void*) cbh.mOnCaptureSequenceAborted);
Yin-Chia Yehead91462016-01-06 16:45:08 -08001324 msg->setInt32(kSequenceIdKey, sequenceId);
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -07001325 postSessionMsgAndCleanup(msg);
Yin-Chia Yehead91462016-01-06 16:45:08 -08001326 } else {
1327 // Use mSequenceLastFrameNumberMap to track
1328 mSequenceLastFrameNumberMap.insert(std::make_pair(sequenceId, lastFrameNumber));
1329
1330 // Last frame might have arrived. Check now
1331 checkAndFireSequenceCompleteLocked();
1332 }
1333}
1334
1335void
1336CameraDevice::checkAndFireSequenceCompleteLocked() {
1337 int64_t completedFrameNumber = mFrameNumberTracker.getCompletedFrameNumber();
1338 //std::map<int, int64_t> mSequenceLastFrameNumberMap;
1339 auto it = mSequenceLastFrameNumberMap.begin();
1340 while (it != mSequenceLastFrameNumberMap.end()) {
1341 int sequenceId = it->first;
1342 int64_t lastFrameNumber = it->second;
1343 bool seqCompleted = false;
1344 bool hasCallback = true;
1345
1346 if (mRemote == nullptr) {
1347 ALOGW("Camera %s closed while checking sequence complete", getId());
1348 return;
1349 }
1350
1351 // Check if there is callback for this sequence
1352 // This should not happen because we always register callback (with nullptr inside)
1353 if (mSequenceCallbackMap.count(sequenceId) == 0) {
1354 ALOGW("No callback found for sequenceId %d", sequenceId);
1355 hasCallback = false;
1356 }
1357
1358 if (lastFrameNumber <= completedFrameNumber) {
1359 ALOGV("seq %d reached last frame %" PRId64 ", completed %" PRId64,
1360 sequenceId, lastFrameNumber, completedFrameNumber);
1361 seqCompleted = true;
1362 }
1363
1364 if (seqCompleted && hasCallback) {
1365 // remove callback holder from callback map
1366 auto cbIt = mSequenceCallbackMap.find(sequenceId);
1367 CallbackHolder cbh = cbIt->second;
1368 mSequenceCallbackMap.erase(cbIt);
1369 // send seq complete callback
1370 sp<AMessage> msg = new AMessage(kWhatCaptureSeqEnd, mHandler);
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -08001371 msg->setPointer(kContextKey, cbh.mContext);
Yin-Chia Yehead91462016-01-06 16:45:08 -08001372 msg->setObject(kSessionSpKey, cbh.mSession);
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -08001373 msg->setPointer(kCallbackFpKey, (void*) cbh.mOnCaptureSequenceCompleted);
Yin-Chia Yehead91462016-01-06 16:45:08 -08001374 msg->setInt32(kSequenceIdKey, sequenceId);
1375 msg->setInt64(kFrameNumberKey, lastFrameNumber);
1376
1377 // Clear the session sp before we send out the message
1378 // This will guarantee the rare case where the message is processed
1379 // before cbh goes out of scope and causing we call the session
1380 // destructor while holding device lock
1381 cbh.mSession.clear();
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -07001382 postSessionMsgAndCleanup(msg);
Yin-Chia Yehead91462016-01-06 16:45:08 -08001383 }
1384
1385 // No need to track sequence complete if there is no callback registered
1386 if (seqCompleted || !hasCallback) {
1387 it = mSequenceLastFrameNumberMap.erase(it);
1388 } else {
1389 ++it;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001390 }
1391 }
1392}
1393
1394/**
1395 * Camera service callback implementation
1396 */
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001397binder::Status
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001398CameraDevice::ServiceCallback::onDeviceError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001399 int32_t errorCode,
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001400 const CaptureResultExtras& resultExtras) {
1401 ALOGD("Device error received, code %d, frame number %" PRId64 ", request ID %d, subseq ID %d",
1402 errorCode, resultExtras.frameNumber, resultExtras.requestId, resultExtras.burstId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001403 binder::Status ret = binder::Status::ok();
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001404 sp<CameraDevice> dev = mDevice.promote();
1405 if (dev == nullptr) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001406 return ret; // device has been closed
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001407 }
1408
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -07001409 sp<ACameraCaptureSession> session = dev->mCurrentSession.promote();
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001410 Mutex::Autolock _l(dev->mDeviceLock);
1411 if (dev->mRemote == nullptr) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001412 return ret; // device has been closed
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001413 }
1414 switch (errorCode) {
1415 case ERROR_CAMERA_DISCONNECTED:
1416 {
Yin-Chia Yehead91462016-01-06 16:45:08 -08001417 // Camera is disconnected, close the session and expect no more callbacks
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -07001418 if (session != nullptr) {
1419 session->closeByDevice();
Yin-Chia Yehead91462016-01-06 16:45:08 -08001420 }
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -07001421 dev->mCurrentSession = nullptr;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001422 sp<AMessage> msg = new AMessage(kWhatOnDisconnected, dev->mHandler);
1423 msg->setPointer(kContextKey, dev->mAppCallbacks.context);
1424 msg->setPointer(kDeviceKey, (void*) dev->getWrapper());
Yin-Chia Yehead91462016-01-06 16:45:08 -08001425 msg->setPointer(kCallbackFpKey, (void*) dev->mAppCallbacks.onDisconnected);
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001426 msg->post();
1427 break;
1428 }
1429 default:
1430 ALOGE("Unknown error from camera device: %d", errorCode);
Chih-Hung Hsiehe6a2f212018-10-16 12:16:31 -07001431 [[fallthrough]];
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001432 case ERROR_CAMERA_DEVICE:
1433 case ERROR_CAMERA_SERVICE:
1434 {
Jayant Chowdhary694cd472018-10-16 12:44:58 -07001435 int32_t errorVal = ::ERROR_CAMERA_DEVICE;
1436 // We keep this switch since this block might be encountered with
1437 // more than just 2 states. The default fallthrough could have us
1438 // handling more unmatched error cases.
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001439 switch (errorCode) {
1440 case ERROR_CAMERA_DEVICE:
Yin-Chia Yehead91462016-01-06 16:45:08 -08001441 dev->setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_DEVICE);
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001442 break;
1443 case ERROR_CAMERA_SERVICE:
Yin-Chia Yehead91462016-01-06 16:45:08 -08001444 dev->setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_SERVICE);
Jayant Chowdhary694cd472018-10-16 12:44:58 -07001445 errorVal = ::ERROR_CAMERA_SERVICE;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001446 break;
1447 default:
Yin-Chia Yehead91462016-01-06 16:45:08 -08001448 dev->setCameraDeviceErrorLocked(ACAMERA_ERROR_UNKNOWN);
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001449 break;
1450 }
1451 sp<AMessage> msg = new AMessage(kWhatOnError, dev->mHandler);
1452 msg->setPointer(kContextKey, dev->mAppCallbacks.context);
1453 msg->setPointer(kDeviceKey, (void*) dev->getWrapper());
Yin-Chia Yehead91462016-01-06 16:45:08 -08001454 msg->setPointer(kCallbackFpKey, (void*) dev->mAppCallbacks.onError);
Jayant Chowdhary694cd472018-10-16 12:44:58 -07001455 msg->setInt32(kErrorCodeKey, errorVal);
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001456 msg->post();
1457 break;
1458 }
1459 case ERROR_CAMERA_REQUEST:
1460 case ERROR_CAMERA_RESULT:
1461 case ERROR_CAMERA_BUFFER:
1462 dev->onCaptureErrorLocked(errorCode, resultExtras);
1463 break;
1464 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001465 return ret;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001466}
1467
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001468binder::Status
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001469CameraDevice::ServiceCallback::onDeviceIdle() {
1470 ALOGV("Camera is now idle");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001471 binder::Status ret = binder::Status::ok();
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001472 sp<CameraDevice> dev = mDevice.promote();
1473 if (dev == nullptr) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001474 return ret; // device has been closed
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001475 }
1476
1477 Mutex::Autolock _l(dev->mDeviceLock);
Yin-Chia Yehead91462016-01-06 16:45:08 -08001478 if (dev->isClosed() || dev->mRemote == nullptr) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001479 return ret;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001480 }
Yin-Chia Yehead91462016-01-06 16:45:08 -08001481
1482 if (dev->mIdle) {
1483 // Already in idle state. Possibly other thread did waitUntilIdle
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001484 return ret;
Yin-Chia Yehead91462016-01-06 16:45:08 -08001485 }
1486
1487 if (dev->mCurrentSession != nullptr) {
1488 ALOGE("onDeviceIdle sending state cb");
1489 if (dev->mBusySession != dev->mCurrentSession) {
1490 ALOGE("Current session != busy session");
1491 dev->setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_DEVICE);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001492 return ret;
Yin-Chia Yehead91462016-01-06 16:45:08 -08001493 }
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -07001494
Yin-Chia Yehead91462016-01-06 16:45:08 -08001495 sp<AMessage> msg = new AMessage(kWhatSessionStateCb, dev->mHandler);
1496 msg->setPointer(kContextKey, dev->mBusySession->mUserSessionCallback.context);
1497 msg->setObject(kSessionSpKey, dev->mBusySession);
1498 msg->setPointer(kCallbackFpKey, (void*) dev->mBusySession->mUserSessionCallback.onReady);
1499 // Make sure we clear the sp first so the session destructor can
1500 // only happen on handler thread (where we don't hold device/session lock)
1501 dev->mBusySession.clear();
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -07001502 dev->postSessionMsgAndCleanup(msg);
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001503 }
1504 dev->mIdle = true;
Yin-Chia Yeh309d05d2016-03-28 10:15:31 -07001505 dev->mFlushing = false;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001506 return ret;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001507}
1508
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001509binder::Status
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001510CameraDevice::ServiceCallback::onCaptureStarted(
1511 const CaptureResultExtras& resultExtras,
1512 int64_t timestamp) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001513 binder::Status ret = binder::Status::ok();
1514
Yin-Chia Yehead91462016-01-06 16:45:08 -08001515 sp<CameraDevice> dev = mDevice.promote();
1516 if (dev == nullptr) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001517 return ret; // device has been closed
Yin-Chia Yehead91462016-01-06 16:45:08 -08001518 }
1519 Mutex::Autolock _l(dev->mDeviceLock);
1520 if (dev->isClosed() || dev->mRemote == nullptr) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001521 return ret;
Yin-Chia Yehead91462016-01-06 16:45:08 -08001522 }
1523
1524 int sequenceId = resultExtras.requestId;
Yin-Chia Yehead91462016-01-06 16:45:08 -08001525 int32_t burstId = resultExtras.burstId;
1526
1527 auto it = dev->mSequenceCallbackMap.find(sequenceId);
1528 if (it != dev->mSequenceCallbackMap.end()) {
1529 CallbackHolder cbh = (*it).second;
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -08001530 ACameraCaptureSession_captureCallback_start onStart = cbh.mOnCaptureStarted;
Yin-Chia Yehead91462016-01-06 16:45:08 -08001531 sp<ACameraCaptureSession> session = cbh.mSession;
1532 if ((size_t) burstId >= cbh.mRequests.size()) {
1533 ALOGE("%s: Error: request index %d out of bound (size %zu)",
1534 __FUNCTION__, burstId, cbh.mRequests.size());
1535 dev->setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_SERVICE);
1536 }
1537 sp<CaptureRequest> request = cbh.mRequests[burstId];
1538 sp<AMessage> msg = new AMessage(kWhatCaptureStart, dev->mHandler);
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -08001539 msg->setPointer(kContextKey, cbh.mContext);
Yin-Chia Yehead91462016-01-06 16:45:08 -08001540 msg->setObject(kSessionSpKey, session);
1541 msg->setPointer(kCallbackFpKey, (void*) onStart);
1542 msg->setObject(kCaptureRequestKey, request);
1543 msg->setInt64(kTimeStampKey, timestamp);
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -07001544 dev->postSessionMsgAndCleanup(msg);
Yin-Chia Yehead91462016-01-06 16:45:08 -08001545 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001546 return ret;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001547}
1548
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001549binder::Status
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001550CameraDevice::ServiceCallback::onResultReceived(
1551 const CameraMetadata& metadata,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001552 const CaptureResultExtras& resultExtras,
1553 const std::vector<PhysicalCaptureResultInfo>& physicalResultInfos) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001554 binder::Status ret = binder::Status::ok();
1555
Yin-Chia Yehead91462016-01-06 16:45:08 -08001556 sp<CameraDevice> dev = mDevice.promote();
1557 if (dev == nullptr) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001558 return ret; // device has been closed
Yin-Chia Yehead91462016-01-06 16:45:08 -08001559 }
1560 int sequenceId = resultExtras.requestId;
1561 int64_t frameNumber = resultExtras.frameNumber;
1562 int32_t burstId = resultExtras.burstId;
1563 bool isPartialResult = (resultExtras.partialResultCount < dev->mPartialResultCount);
1564
1565 if (!isPartialResult) {
1566 ALOGV("SeqId %d frame %" PRId64 " result arrive.", sequenceId, frameNumber);
1567 }
1568
1569 Mutex::Autolock _l(dev->mDeviceLock);
1570 if (dev->mRemote == nullptr) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001571 return ret; // device has been disconnected
Yin-Chia Yehead91462016-01-06 16:45:08 -08001572 }
1573
1574 if (dev->isClosed()) {
1575 if (!isPartialResult) {
1576 dev->mFrameNumberTracker.updateTracker(frameNumber, /*isError*/false);
1577 }
1578 // early return to avoid callback sent to closed devices
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001579 return ret;
Yin-Chia Yehead91462016-01-06 16:45:08 -08001580 }
1581
1582 CameraMetadata metadataCopy = metadata;
Yin-Chia Yehead91462016-01-06 16:45:08 -08001583 metadataCopy.update(ANDROID_LENS_INFO_SHADING_MAP_SIZE, dev->mShadingMapSize, /*data_count*/2);
Yin-Chia Yehff2a4952016-04-02 16:31:57 -07001584 metadataCopy.update(ANDROID_SYNC_FRAME_NUMBER, &frameNumber, /*data_count*/1);
Yin-Chia Yehead91462016-01-06 16:45:08 -08001585
1586 auto it = dev->mSequenceCallbackMap.find(sequenceId);
1587 if (it != dev->mSequenceCallbackMap.end()) {
1588 CallbackHolder cbh = (*it).second;
Yin-Chia Yehead91462016-01-06 16:45:08 -08001589 sp<ACameraCaptureSession> session = cbh.mSession;
1590 if ((size_t) burstId >= cbh.mRequests.size()) {
1591 ALOGE("%s: Error: request index %d out of bound (size %zu)",
1592 __FUNCTION__, burstId, cbh.mRequests.size());
1593 dev->setCameraDeviceErrorLocked(ACAMERA_ERROR_CAMERA_SERVICE);
1594 }
1595 sp<CaptureRequest> request = cbh.mRequests[burstId];
1596 sp<ACameraMetadata> result(new ACameraMetadata(
1597 metadataCopy.release(), ACameraMetadata::ACM_RESULT));
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -08001598 sp<ACameraPhysicalCaptureResultInfo> physicalResult(
1599 new ACameraPhysicalCaptureResultInfo(physicalResultInfos, frameNumber));
Yin-Chia Yehead91462016-01-06 16:45:08 -08001600
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -08001601 sp<AMessage> msg = new AMessage(
1602 cbh.mIsLogicalCameraCallback ? kWhatLogicalCaptureResult : kWhatCaptureResult,
1603 dev->mHandler);
1604 msg->setPointer(kContextKey, cbh.mContext);
Yin-Chia Yehead91462016-01-06 16:45:08 -08001605 msg->setObject(kSessionSpKey, session);
Yin-Chia Yehead91462016-01-06 16:45:08 -08001606 msg->setObject(kCaptureRequestKey, request);
1607 msg->setObject(kCaptureResultKey, result);
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -08001608 if (isPartialResult) {
1609 msg->setPointer(kCallbackFpKey,
1610 (void *)cbh.mOnCaptureProgressed);
1611 } else if (cbh.mIsLogicalCameraCallback) {
1612 msg->setPointer(kCallbackFpKey,
1613 (void *)cbh.mOnLogicalCameraCaptureCompleted);
1614 msg->setObject(kPhysicalCaptureResultKey, physicalResult);
1615 } else {
1616 msg->setPointer(kCallbackFpKey,
1617 (void *)cbh.mOnCaptureCompleted);
1618 }
Yin-Chia Yeh6e2353b2017-10-24 16:35:20 -07001619 dev->postSessionMsgAndCleanup(msg);
Yin-Chia Yehead91462016-01-06 16:45:08 -08001620 }
1621
1622 if (!isPartialResult) {
1623 dev->mFrameNumberTracker.updateTracker(frameNumber, /*isError*/false);
1624 dev->checkAndFireSequenceCompleteLocked();
1625 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001626
1627 return ret;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001628}
1629
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001630binder::Status
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001631CameraDevice::ServiceCallback::onPrepared(int) {
1632 // Prepare not yet implemented in NDK
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001633 return binder::Status::ok();
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001634}
1635
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001636binder::Status
Shuzhen Wang9d066012016-09-30 11:30:20 -07001637CameraDevice::ServiceCallback::onRequestQueueEmpty() {
1638 // onRequestQueueEmpty not yet implemented in NDK
1639 return binder::Status::ok();
1640}
1641
1642binder::Status
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -07001643CameraDevice::ServiceCallback::onRepeatingRequestError(
1644 int64_t lastFrameNumber, int32_t stoppedSequenceId) {
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001645 binder::Status ret = binder::Status::ok();
1646
1647 sp<CameraDevice> dev = mDevice.promote();
1648 if (dev == nullptr) {
1649 return ret; // device has been closed
1650 }
1651
1652 Mutex::Autolock _l(dev->mDeviceLock);
1653
1654 int repeatingSequenceId = dev->mRepeatingSequenceId;
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -07001655 if (stoppedSequenceId == repeatingSequenceId) {
1656 dev->mRepeatingSequenceId = REQUEST_ID_NONE;
1657 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001658
1659 dev->checkRepeatingSequenceCompleteLocked(repeatingSequenceId, lastFrameNumber);
1660
1661 return ret;
1662}
1663
Jayant Chowdhary6df26072018-11-06 23:55:12 -08001664} // namespace acam
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001665} // namespace android