Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 "CameraDeviceClient" |
| 18 | #define ATRACE_TAG ATRACE_TAG_CAMERA |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 19 | //#define LOG_NDEBUG 0 |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 20 | |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 21 | #include <cutils/properties.h> |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 22 | #include <utils/Log.h> |
| 23 | #include <utils/Trace.h> |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 24 | #include <gui/Surface.h> |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 25 | #include <camera/camera2/CaptureRequest.h> |
Ruben Brunk | 5698d44 | 2014-06-18 10:39:40 -0700 | [diff] [blame] | 26 | #include <camera/CameraUtils.h> |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 27 | |
| 28 | #include "common/CameraDeviceBase.h" |
| 29 | #include "api2/CameraDeviceClient.h" |
| 30 | |
| 31 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | using namespace camera2; |
| 35 | |
| 36 | CameraDeviceClientBase::CameraDeviceClientBase( |
| 37 | const sp<CameraService>& cameraService, |
| 38 | const sp<ICameraDeviceCallbacks>& remoteCallback, |
| 39 | const String16& clientPackageName, |
| 40 | int cameraId, |
| 41 | int cameraFacing, |
| 42 | int clientPid, |
| 43 | uid_t clientUid, |
| 44 | int servicePid) : |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 45 | BasicClient(cameraService, |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 46 | IInterface::asBinder(remoteCallback), |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 47 | clientPackageName, |
| 48 | cameraId, |
| 49 | cameraFacing, |
| 50 | clientPid, |
| 51 | clientUid, |
| 52 | servicePid), |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 53 | mRemoteCallback(remoteCallback) { |
| 54 | } |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 55 | |
| 56 | // Interface used by CameraService |
| 57 | |
| 58 | CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService, |
| 59 | const sp<ICameraDeviceCallbacks>& remoteCallback, |
| 60 | const String16& clientPackageName, |
| 61 | int cameraId, |
| 62 | int cameraFacing, |
| 63 | int clientPid, |
| 64 | uid_t clientUid, |
| 65 | int servicePid) : |
| 66 | Camera2ClientBase(cameraService, remoteCallback, clientPackageName, |
| 67 | cameraId, cameraFacing, clientPid, clientUid, servicePid), |
| 68 | mRequestIdCounter(0) { |
| 69 | |
| 70 | ATRACE_CALL(); |
| 71 | ALOGI("CameraDeviceClient %d: Opened", cameraId); |
| 72 | } |
| 73 | |
| 74 | status_t CameraDeviceClient::initialize(camera_module_t *module) |
| 75 | { |
| 76 | ATRACE_CALL(); |
| 77 | status_t res; |
| 78 | |
| 79 | res = Camera2ClientBase::initialize(module); |
| 80 | if (res != OK) { |
| 81 | return res; |
| 82 | } |
| 83 | |
| 84 | String8 threadName; |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 85 | mFrameProcessor = new FrameProcessorBase(mDevice); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 86 | threadName = String8::format("CDU-%d-FrameProc", mCameraId); |
| 87 | mFrameProcessor->run(threadName.string()); |
| 88 | |
| 89 | mFrameProcessor->registerListener(FRAME_PROCESSOR_LISTENER_MIN_ID, |
| 90 | FRAME_PROCESSOR_LISTENER_MAX_ID, |
Eino-Ville Talvala | 184dfe4 | 2013-11-07 15:13:16 -0800 | [diff] [blame] | 91 | /*listener*/this, |
Zhijun He | 25a0aef | 2014-06-25 11:40:02 -0700 | [diff] [blame] | 92 | /*sendPartials*/true); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 93 | |
| 94 | return OK; |
| 95 | } |
| 96 | |
| 97 | CameraDeviceClient::~CameraDeviceClient() { |
| 98 | } |
| 99 | |
| 100 | status_t CameraDeviceClient::submitRequest(sp<CaptureRequest> request, |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 101 | bool streaming, |
| 102 | /*out*/ |
| 103 | int64_t* lastFrameNumber) { |
| 104 | List<sp<CaptureRequest> > requestList; |
| 105 | requestList.push_back(request); |
| 106 | return submitRequestList(requestList, streaming, lastFrameNumber); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 109 | status_t CameraDeviceClient::submitRequestList(List<sp<CaptureRequest> > requests, |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 110 | bool streaming, int64_t* lastFrameNumber) { |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 111 | ATRACE_CALL(); |
Mark Salyzyn | 5046841 | 2014-06-18 16:33:43 -0700 | [diff] [blame] | 112 | ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size()); |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 113 | |
| 114 | status_t res; |
| 115 | if ( (res = checkPid(__FUNCTION__) ) != OK) return res; |
| 116 | |
| 117 | Mutex::Autolock icl(mBinderSerializationLock); |
| 118 | |
| 119 | if (!mDevice.get()) return DEAD_OBJECT; |
| 120 | |
| 121 | if (requests.empty()) { |
| 122 | ALOGE("%s: Camera %d: Sent null request. Rejecting request.", |
| 123 | __FUNCTION__, mCameraId); |
| 124 | return BAD_VALUE; |
| 125 | } |
| 126 | |
| 127 | List<const CameraMetadata> metadataRequestList; |
| 128 | int32_t requestId = mRequestIdCounter; |
| 129 | uint32_t loopCounter = 0; |
| 130 | |
| 131 | for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end(); ++it) { |
| 132 | sp<CaptureRequest> request = *it; |
| 133 | if (request == 0) { |
| 134 | ALOGE("%s: Camera %d: Sent null request.", |
| 135 | __FUNCTION__, mCameraId); |
| 136 | return BAD_VALUE; |
| 137 | } |
| 138 | |
| 139 | CameraMetadata metadata(request->mMetadata); |
| 140 | if (metadata.isEmpty()) { |
| 141 | ALOGE("%s: Camera %d: Sent empty metadata packet. Rejecting request.", |
| 142 | __FUNCTION__, mCameraId); |
| 143 | return BAD_VALUE; |
| 144 | } else if (request->mSurfaceList.isEmpty()) { |
| 145 | ALOGE("%s: Camera %d: Requests must have at least one surface target. " |
| 146 | "Rejecting request.", __FUNCTION__, mCameraId); |
| 147 | return BAD_VALUE; |
| 148 | } |
| 149 | |
| 150 | if (!enforceRequestPermissions(metadata)) { |
| 151 | // Callee logs |
| 152 | return PERMISSION_DENIED; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Write in the output stream IDs which we calculate from |
| 157 | * the capture request's list of surface targets |
| 158 | */ |
| 159 | Vector<int32_t> outputStreamIds; |
| 160 | outputStreamIds.setCapacity(request->mSurfaceList.size()); |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 161 | for (size_t i = 0; i < request->mSurfaceList.size(); ++i) { |
| 162 | sp<Surface> surface = request->mSurfaceList[i]; |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 163 | if (surface == 0) continue; |
| 164 | |
| 165 | sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer(); |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 166 | int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp)); |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 167 | |
| 168 | // Trying to submit request with surface that wasn't created |
| 169 | if (idx == NAME_NOT_FOUND) { |
| 170 | ALOGE("%s: Camera %d: Tried to submit a request with a surface that" |
| 171 | " we have not called createStream on", |
| 172 | __FUNCTION__, mCameraId); |
| 173 | return BAD_VALUE; |
| 174 | } |
| 175 | |
| 176 | int streamId = mStreamMap.valueAt(idx); |
| 177 | outputStreamIds.push_back(streamId); |
| 178 | ALOGV("%s: Camera %d: Appending output stream %d to request", |
| 179 | __FUNCTION__, mCameraId, streamId); |
| 180 | } |
| 181 | |
| 182 | metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS, &outputStreamIds[0], |
| 183 | outputStreamIds.size()); |
| 184 | |
| 185 | metadata.update(ANDROID_REQUEST_ID, &requestId, /*size*/1); |
| 186 | loopCounter++; // loopCounter starts from 1 |
Mark Salyzyn | 5046841 | 2014-06-18 16:33:43 -0700 | [diff] [blame] | 187 | ALOGV("%s: Camera %d: Creating request with ID %d (%d of %zu)", |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 188 | __FUNCTION__, mCameraId, requestId, loopCounter, requests.size()); |
| 189 | |
| 190 | metadataRequestList.push_back(metadata); |
| 191 | } |
| 192 | mRequestIdCounter++; |
| 193 | |
| 194 | if (streaming) { |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 195 | res = mDevice->setStreamingRequestList(metadataRequestList, lastFrameNumber); |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 196 | if (res != OK) { |
| 197 | ALOGE("%s: Camera %d: Got error %d after trying to set streaming " |
| 198 | "request", __FUNCTION__, mCameraId, res); |
| 199 | } else { |
| 200 | mStreamingRequestList.push_back(requestId); |
| 201 | } |
| 202 | } else { |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 203 | res = mDevice->captureList(metadataRequestList, lastFrameNumber); |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 204 | if (res != OK) { |
| 205 | ALOGE("%s: Camera %d: Got error %d after trying to set capture", |
| 206 | __FUNCTION__, mCameraId, res); |
| 207 | } |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 208 | ALOGV("%s: requestId = %d ", __FUNCTION__, requestId); |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | ALOGV("%s: Camera %d: End of function", __FUNCTION__, mCameraId); |
| 212 | if (res == OK) { |
| 213 | return requestId; |
| 214 | } |
| 215 | |
| 216 | return res; |
| 217 | } |
| 218 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 219 | status_t CameraDeviceClient::cancelRequest(int requestId, int64_t* lastFrameNumber) { |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 220 | ATRACE_CALL(); |
| 221 | ALOGV("%s, requestId = %d", __FUNCTION__, requestId); |
| 222 | |
| 223 | status_t res; |
| 224 | |
| 225 | if ( (res = checkPid(__FUNCTION__) ) != OK) return res; |
| 226 | |
| 227 | Mutex::Autolock icl(mBinderSerializationLock); |
| 228 | |
| 229 | if (!mDevice.get()) return DEAD_OBJECT; |
| 230 | |
| 231 | Vector<int>::iterator it, end; |
| 232 | for (it = mStreamingRequestList.begin(), end = mStreamingRequestList.end(); |
| 233 | it != end; ++it) { |
| 234 | if (*it == requestId) { |
| 235 | break; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | if (it == end) { |
| 240 | ALOGE("%s: Camera%d: Did not find request id %d in list of streaming " |
| 241 | "requests", __FUNCTION__, mCameraId, requestId); |
| 242 | return BAD_VALUE; |
| 243 | } |
| 244 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 245 | res = mDevice->clearStreamingRequest(lastFrameNumber); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 246 | |
| 247 | if (res == OK) { |
| 248 | ALOGV("%s: Camera %d: Successfully cleared streaming request", |
| 249 | __FUNCTION__, mCameraId); |
| 250 | mStreamingRequestList.erase(it); |
| 251 | } |
| 252 | |
| 253 | return res; |
| 254 | } |
| 255 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 256 | status_t CameraDeviceClient::beginConfigure() { |
| 257 | // TODO: Implement this. |
| 258 | ALOGE("%s: Not implemented yet.", __FUNCTION__); |
| 259 | return OK; |
| 260 | } |
| 261 | |
| 262 | status_t CameraDeviceClient::endConfigure() { |
Igor Murashkin | e2d167e | 2014-08-19 16:19:59 -0700 | [diff] [blame] | 263 | ALOGV("%s: ending configure (%zu streams)", |
| 264 | __FUNCTION__, mStreamMap.size()); |
| 265 | |
| 266 | status_t res; |
| 267 | if ( (res = checkPid(__FUNCTION__) ) != OK) return res; |
| 268 | |
| 269 | Mutex::Autolock icl(mBinderSerializationLock); |
| 270 | |
| 271 | if (!mDevice.get()) return DEAD_OBJECT; |
| 272 | |
| 273 | return mDevice->configureStreams(); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 276 | status_t CameraDeviceClient::deleteStream(int streamId) { |
| 277 | ATRACE_CALL(); |
| 278 | ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId); |
| 279 | |
| 280 | status_t res; |
| 281 | if ( (res = checkPid(__FUNCTION__) ) != OK) return res; |
| 282 | |
| 283 | Mutex::Autolock icl(mBinderSerializationLock); |
| 284 | |
| 285 | if (!mDevice.get()) return DEAD_OBJECT; |
| 286 | |
| 287 | // Guard against trying to delete non-created streams |
| 288 | ssize_t index = NAME_NOT_FOUND; |
| 289 | for (size_t i = 0; i < mStreamMap.size(); ++i) { |
| 290 | if (streamId == mStreamMap.valueAt(i)) { |
| 291 | index = i; |
| 292 | break; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | if (index == NAME_NOT_FOUND) { |
| 297 | ALOGW("%s: Camera %d: Invalid stream ID (%d) specified, no stream " |
| 298 | "created yet", __FUNCTION__, mCameraId, streamId); |
| 299 | return BAD_VALUE; |
| 300 | } |
| 301 | |
| 302 | // Also returns BAD_VALUE if stream ID was not valid |
| 303 | res = mDevice->deleteStream(streamId); |
| 304 | |
| 305 | if (res == BAD_VALUE) { |
| 306 | ALOGE("%s: Camera %d: Unexpected BAD_VALUE when deleting stream, but we" |
| 307 | " already checked and the stream ID (%d) should be valid.", |
| 308 | __FUNCTION__, mCameraId, streamId); |
| 309 | } else if (res == OK) { |
| 310 | mStreamMap.removeItemsAt(index); |
| 311 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | return res; |
| 315 | } |
| 316 | |
| 317 | status_t CameraDeviceClient::createStream(int width, int height, int format, |
| 318 | const sp<IGraphicBufferProducer>& bufferProducer) |
| 319 | { |
| 320 | ATRACE_CALL(); |
| 321 | ALOGV("%s (w = %d, h = %d, f = 0x%x)", __FUNCTION__, width, height, format); |
| 322 | |
| 323 | status_t res; |
| 324 | if ( (res = checkPid(__FUNCTION__) ) != OK) return res; |
| 325 | |
| 326 | Mutex::Autolock icl(mBinderSerializationLock); |
| 327 | |
Yin-Chia Yeh | 89f14da | 2014-06-10 16:05:44 -0700 | [diff] [blame] | 328 | if (bufferProducer == NULL) { |
| 329 | ALOGE("%s: bufferProducer must not be null", __FUNCTION__); |
| 330 | return BAD_VALUE; |
| 331 | } |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 332 | if (!mDevice.get()) return DEAD_OBJECT; |
| 333 | |
| 334 | // Don't create multiple streams for the same target surface |
| 335 | { |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 336 | ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer)); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 337 | if (index != NAME_NOT_FOUND) { |
| 338 | ALOGW("%s: Camera %d: Buffer producer already has a stream for it " |
Colin Cross | e5729fa | 2014-03-21 15:04:25 -0700 | [diff] [blame] | 339 | "(ID %zd)", |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 340 | __FUNCTION__, mCameraId, index); |
| 341 | return ALREADY_EXISTS; |
| 342 | } |
| 343 | } |
| 344 | |
Eino-Ville Talvala | 1da3b60 | 2013-09-26 15:28:55 -0700 | [diff] [blame] | 345 | // HACK b/10949105 |
| 346 | // Query consumer usage bits to set async operation mode for |
| 347 | // GLConsumer using controlledByApp parameter. |
| 348 | bool useAsync = false; |
| 349 | int32_t consumerUsage; |
| 350 | if ((res = bufferProducer->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, |
| 351 | &consumerUsage)) != OK) { |
| 352 | ALOGE("%s: Camera %d: Failed to query consumer usage", __FUNCTION__, |
| 353 | mCameraId); |
| 354 | return res; |
| 355 | } |
| 356 | if (consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) { |
| 357 | ALOGW("%s: Camera %d: Forcing asynchronous mode for stream", |
| 358 | __FUNCTION__, mCameraId); |
| 359 | useAsync = true; |
| 360 | } |
| 361 | |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 362 | sp<IBinder> binder = IInterface::asBinder(bufferProducer); |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 363 | sp<ANativeWindow> anw = new Surface(bufferProducer, useAsync); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 364 | |
| 365 | // TODO: remove w,h,f since we are ignoring them |
| 366 | |
| 367 | if ((res = anw->query(anw.get(), NATIVE_WINDOW_WIDTH, &width)) != OK) { |
| 368 | ALOGE("%s: Camera %d: Failed to query Surface width", __FUNCTION__, |
| 369 | mCameraId); |
| 370 | return res; |
| 371 | } |
| 372 | if ((res = anw->query(anw.get(), NATIVE_WINDOW_HEIGHT, &height)) != OK) { |
| 373 | ALOGE("%s: Camera %d: Failed to query Surface height", __FUNCTION__, |
| 374 | mCameraId); |
| 375 | return res; |
| 376 | } |
| 377 | if ((res = anw->query(anw.get(), NATIVE_WINDOW_FORMAT, &format)) != OK) { |
| 378 | ALOGE("%s: Camera %d: Failed to query Surface format", __FUNCTION__, |
| 379 | mCameraId); |
| 380 | return res; |
| 381 | } |
| 382 | |
| 383 | // FIXME: remove this override since the default format should be |
| 384 | // IMPLEMENTATION_DEFINED. b/9487482 |
Igor Murashkin | 1581101 | 2013-07-29 12:25:59 -0700 | [diff] [blame] | 385 | if (format >= HAL_PIXEL_FORMAT_RGBA_8888 && |
| 386 | format <= HAL_PIXEL_FORMAT_BGRA_8888) { |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 387 | ALOGW("%s: Camera %d: Overriding format 0x%x to IMPLEMENTATION_DEFINED", |
| 388 | __FUNCTION__, mCameraId, format); |
| 389 | format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED; |
| 390 | } |
| 391 | |
| 392 | // TODO: add startConfigure/stopConfigure call to CameraDeviceBase |
| 393 | // this will make it so Camera3Device doesn't call configure_streams |
| 394 | // after each call, but only once we are done with all. |
| 395 | |
| 396 | int streamId = -1; |
Zhijun He | 28c9b6f | 2014-08-08 12:00:47 -0700 | [diff] [blame] | 397 | res = mDevice->createStream(anw, width, height, format, &streamId); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 398 | |
| 399 | if (res == OK) { |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 400 | mStreamMap.add(binder, streamId); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 401 | |
| 402 | ALOGV("%s: Camera %d: Successfully created a new stream ID %d", |
| 403 | __FUNCTION__, mCameraId, streamId); |
Igor Murashkin | f8b2a6f | 2013-09-17 17:03:28 -0700 | [diff] [blame] | 404 | |
| 405 | /** |
| 406 | * Set the stream transform flags to automatically |
| 407 | * rotate the camera stream for preview use cases. |
| 408 | */ |
| 409 | int32_t transform = 0; |
| 410 | res = getRotationTransformLocked(&transform); |
| 411 | |
| 412 | if (res != OK) { |
| 413 | // Error logged by getRotationTransformLocked. |
| 414 | return res; |
| 415 | } |
| 416 | |
| 417 | res = mDevice->setStreamTransform(streamId, transform); |
| 418 | if (res != OK) { |
| 419 | ALOGE("%s: Failed to set stream transform (stream id %d)", |
| 420 | __FUNCTION__, streamId); |
| 421 | return res; |
| 422 | } |
| 423 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 424 | return streamId; |
| 425 | } |
| 426 | |
| 427 | return res; |
| 428 | } |
| 429 | |
| 430 | // Create a request object from a template. |
| 431 | status_t CameraDeviceClient::createDefaultRequest(int templateId, |
| 432 | /*out*/ |
| 433 | CameraMetadata* request) |
| 434 | { |
| 435 | ATRACE_CALL(); |
| 436 | ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId); |
| 437 | |
| 438 | status_t res; |
| 439 | if ( (res = checkPid(__FUNCTION__) ) != OK) return res; |
| 440 | |
| 441 | Mutex::Autolock icl(mBinderSerializationLock); |
| 442 | |
| 443 | if (!mDevice.get()) return DEAD_OBJECT; |
| 444 | |
| 445 | CameraMetadata metadata; |
| 446 | if ( (res = mDevice->createDefaultRequest(templateId, &metadata) ) == OK && |
| 447 | request != NULL) { |
| 448 | |
| 449 | request->swap(metadata); |
| 450 | } |
| 451 | |
| 452 | return res; |
| 453 | } |
| 454 | |
Igor Murashkin | 099b457 | 2013-07-12 17:52:16 -0700 | [diff] [blame] | 455 | status_t CameraDeviceClient::getCameraInfo(/*out*/CameraMetadata* info) |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 456 | { |
| 457 | ATRACE_CALL(); |
| 458 | ALOGV("%s", __FUNCTION__); |
| 459 | |
| 460 | status_t res = OK; |
| 461 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 462 | if ( (res = checkPid(__FUNCTION__) ) != OK) return res; |
| 463 | |
| 464 | Mutex::Autolock icl(mBinderSerializationLock); |
| 465 | |
| 466 | if (!mDevice.get()) return DEAD_OBJECT; |
| 467 | |
Igor Murashkin | 099b457 | 2013-07-12 17:52:16 -0700 | [diff] [blame] | 468 | if (info != NULL) { |
| 469 | *info = mDevice->info(); // static camera metadata |
| 470 | // TODO: merge with device-specific camera metadata |
| 471 | } |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 472 | |
| 473 | return res; |
| 474 | } |
| 475 | |
Zhijun He | 2ab500c | 2013-07-23 08:02:53 -0700 | [diff] [blame] | 476 | status_t CameraDeviceClient::waitUntilIdle() |
| 477 | { |
| 478 | ATRACE_CALL(); |
| 479 | ALOGV("%s", __FUNCTION__); |
| 480 | |
| 481 | status_t res = OK; |
| 482 | if ( (res = checkPid(__FUNCTION__) ) != OK) return res; |
| 483 | |
| 484 | Mutex::Autolock icl(mBinderSerializationLock); |
| 485 | |
| 486 | if (!mDevice.get()) return DEAD_OBJECT; |
| 487 | |
| 488 | // FIXME: Also need check repeating burst. |
| 489 | if (!mStreamingRequestList.isEmpty()) { |
| 490 | ALOGE("%s: Camera %d: Try to waitUntilIdle when there are active streaming requests", |
| 491 | __FUNCTION__, mCameraId); |
| 492 | return INVALID_OPERATION; |
| 493 | } |
| 494 | res = mDevice->waitUntilDrained(); |
| 495 | ALOGV("%s Done", __FUNCTION__); |
| 496 | |
| 497 | return res; |
| 498 | } |
| 499 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 500 | status_t CameraDeviceClient::flush(int64_t* lastFrameNumber) { |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 501 | ATRACE_CALL(); |
| 502 | ALOGV("%s", __FUNCTION__); |
| 503 | |
| 504 | status_t res = OK; |
| 505 | if ( (res = checkPid(__FUNCTION__) ) != OK) return res; |
| 506 | |
| 507 | Mutex::Autolock icl(mBinderSerializationLock); |
| 508 | |
| 509 | if (!mDevice.get()) return DEAD_OBJECT; |
| 510 | |
Jianing Wei | 3c76fa3 | 2014-04-21 11:34:34 -0700 | [diff] [blame] | 511 | mStreamingRequestList.clear(); |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 512 | return mDevice->flush(lastFrameNumber); |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 515 | status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) { |
| 516 | String8 result; |
Eino-Ville Talvala | 67489d2 | 2014-09-18 15:52:02 -0700 | [diff] [blame] | 517 | result.appendFormat("CameraDeviceClient[%d] (%p) dump:\n", |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 518 | mCameraId, |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 519 | (getRemoteCallback() != NULL ? |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 520 | IInterface::asBinder(getRemoteCallback()).get() : NULL) ); |
Eino-Ville Talvala | 67489d2 | 2014-09-18 15:52:02 -0700 | [diff] [blame] | 521 | result.appendFormat(" Current client: %s (PID %d, UID %u)\n", |
| 522 | String8(mClientPackageName).string(), |
| 523 | mClientPid, mClientUid); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 524 | |
Eino-Ville Talvala | 67489d2 | 2014-09-18 15:52:02 -0700 | [diff] [blame] | 525 | result.append(" State:\n"); |
| 526 | result.appendFormat(" Request ID counter: %d\n", mRequestIdCounter); |
| 527 | if (!mStreamMap.isEmpty()) { |
| 528 | result.append(" Current stream IDs:\n"); |
| 529 | for (size_t i = 0; i < mStreamMap.size(); i++) { |
| 530 | result.appendFormat(" Stream %d\n", mStreamMap.valueAt(i)); |
| 531 | } |
| 532 | } else { |
| 533 | result.append(" No streams configured.\n"); |
| 534 | } |
| 535 | write(fd, result.string(), result.size()); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 536 | // TODO: print dynamic/request section from most recent requests |
| 537 | mFrameProcessor->dump(fd, args); |
| 538 | |
| 539 | return dumpDevice(fd, args); |
| 540 | } |
| 541 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 542 | void CameraDeviceClient::notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode, |
| 543 | const CaptureResultExtras& resultExtras) { |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 544 | // Thread safe. Don't bother locking. |
| 545 | sp<ICameraDeviceCallbacks> remoteCb = getRemoteCallback(); |
| 546 | |
| 547 | if (remoteCb != 0) { |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 548 | remoteCb->onDeviceError(errorCode, resultExtras); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | |
| 552 | void CameraDeviceClient::notifyIdle() { |
| 553 | // Thread safe. Don't bother locking. |
| 554 | sp<ICameraDeviceCallbacks> remoteCb = getRemoteCallback(); |
| 555 | |
| 556 | if (remoteCb != 0) { |
| 557 | remoteCb->onDeviceIdle(); |
| 558 | } |
| 559 | } |
| 560 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 561 | void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras, |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 562 | nsecs_t timestamp) { |
| 563 | // Thread safe. Don't bother locking. |
| 564 | sp<ICameraDeviceCallbacks> remoteCb = getRemoteCallback(); |
| 565 | if (remoteCb != 0) { |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 566 | remoteCb->onCaptureStarted(resultExtras, timestamp); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 570 | // TODO: refactor the code below this with IProCameraUser. |
| 571 | // it's 100% copy-pasted, so lets not change it right now to make it easier. |
| 572 | |
| 573 | void CameraDeviceClient::detachDevice() { |
| 574 | if (mDevice == 0) return; |
| 575 | |
| 576 | ALOGV("Camera %d: Stopping processors", mCameraId); |
| 577 | |
| 578 | mFrameProcessor->removeListener(FRAME_PROCESSOR_LISTENER_MIN_ID, |
| 579 | FRAME_PROCESSOR_LISTENER_MAX_ID, |
| 580 | /*listener*/this); |
| 581 | mFrameProcessor->requestExit(); |
| 582 | ALOGV("Camera %d: Waiting for threads", mCameraId); |
| 583 | mFrameProcessor->join(); |
| 584 | ALOGV("Camera %d: Disconnecting device", mCameraId); |
| 585 | |
| 586 | // WORKAROUND: HAL refuses to disconnect while there's streams in flight |
| 587 | { |
| 588 | mDevice->clearStreamingRequest(); |
| 589 | |
| 590 | status_t code; |
| 591 | if ((code = mDevice->waitUntilDrained()) != OK) { |
| 592 | ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__, |
| 593 | code); |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | Camera2ClientBase::detachDevice(); |
| 598 | } |
| 599 | |
| 600 | /** Device-related methods */ |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 601 | void CameraDeviceClient::onResultAvailable(const CaptureResult& result) { |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 602 | ATRACE_CALL(); |
| 603 | ALOGV("%s", __FUNCTION__); |
| 604 | |
Igor Murashkin | 4fb55c1 | 2013-08-29 17:43:01 -0700 | [diff] [blame] | 605 | // Thread-safe. No lock necessary. |
| 606 | sp<ICameraDeviceCallbacks> remoteCb = mRemoteCallback; |
| 607 | if (remoteCb != NULL) { |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 608 | remoteCb->onResultReceived(result.mMetadata, result.mResultExtras); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 609 | } |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | // TODO: move to Camera2ClientBase |
| 613 | bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) { |
| 614 | |
| 615 | const int pid = IPCThreadState::self()->getCallingPid(); |
| 616 | const int selfPid = getpid(); |
| 617 | camera_metadata_entry_t entry; |
| 618 | |
| 619 | /** |
| 620 | * Mixin default important security values |
| 621 | * - android.led.transmit = defaulted ON |
| 622 | */ |
| 623 | CameraMetadata staticInfo = mDevice->info(); |
| 624 | entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS); |
| 625 | for(size_t i = 0; i < entry.count; ++i) { |
| 626 | uint8_t led = entry.data.u8[i]; |
| 627 | |
| 628 | switch(led) { |
| 629 | case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: { |
| 630 | uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON; |
| 631 | if (!metadata.exists(ANDROID_LED_TRANSMIT)) { |
| 632 | metadata.update(ANDROID_LED_TRANSMIT, |
| 633 | &transmitDefault, 1); |
| 634 | } |
| 635 | break; |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | // We can do anything! |
| 641 | if (pid == selfPid) { |
| 642 | return true; |
| 643 | } |
| 644 | |
| 645 | /** |
| 646 | * Permission check special fields in the request |
| 647 | * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT |
| 648 | */ |
| 649 | entry = metadata.find(ANDROID_LED_TRANSMIT); |
| 650 | if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) { |
| 651 | String16 permissionString = |
| 652 | String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED"); |
| 653 | if (!checkCallingPermission(permissionString)) { |
| 654 | const int uid = IPCThreadState::self()->getCallingUid(); |
| 655 | ALOGE("Permission Denial: " |
| 656 | "can't disable transmit LED pid=%d, uid=%d", pid, uid); |
| 657 | return false; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | return true; |
| 662 | } |
| 663 | |
Igor Murashkin | f8b2a6f | 2013-09-17 17:03:28 -0700 | [diff] [blame] | 664 | status_t CameraDeviceClient::getRotationTransformLocked(int32_t* transform) { |
| 665 | ALOGV("%s: begin", __FUNCTION__); |
| 666 | |
Igor Murashkin | f8b2a6f | 2013-09-17 17:03:28 -0700 | [diff] [blame] | 667 | const CameraMetadata& staticInfo = mDevice->info(); |
Ruben Brunk | 5698d44 | 2014-06-18 10:39:40 -0700 | [diff] [blame] | 668 | return CameraUtils::getRotationTransform(staticInfo, transform); |
Igor Murashkin | f8b2a6f | 2013-09-17 17:03:28 -0700 | [diff] [blame] | 669 | } |
| 670 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 671 | } // namespace android |