blob: bd6b60ae049eb05cc4e9aa71dfd712e76c219ef5 [file] [log] [blame]
Igor Murashkine7ee7632013-06-11 18:10:18 -07001/*
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
19// #define LOG_NDEBUG 0
20
21#include <utils/Log.h>
22#include <utils/Trace.h>
23
24#include <cutils/properties.h>
25#include <gui/Surface.h>
26#include "camera2/Parameters.h"
27#include "CameraDeviceClient.h"
28#include "camera2/ProFrameProcessor.h"
29#include "CameraDeviceBase.h"
30#include <camera/photography/CaptureRequest.h>
31
32namespace android {
33using namespace camera2;
34
35CameraDeviceClientBase::CameraDeviceClientBase(
36 const sp<CameraService>& cameraService,
37 const sp<ICameraDeviceCallbacks>& remoteCallback,
38 const String16& clientPackageName,
39 int cameraId,
40 int cameraFacing,
41 int clientPid,
42 uid_t clientUid,
43 int servicePid) :
44 BasicClient(cameraService, remoteCallback->asBinder(), clientPackageName,
45 cameraId, cameraFacing, clientPid, clientUid, servicePid),
46 mRemoteCallback(remoteCallback) {
47}
48void CameraDeviceClientBase::notifyError() {
49 // Thread safe. Don't bother locking.
50 sp<ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
51
52 if (remoteCb != 0) {
53 remoteCb->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
54 }
55}
56
57// Interface used by CameraService
58
59CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService,
60 const sp<ICameraDeviceCallbacks>& remoteCallback,
61 const String16& clientPackageName,
62 int cameraId,
63 int cameraFacing,
64 int clientPid,
65 uid_t clientUid,
66 int servicePid) :
67 Camera2ClientBase(cameraService, remoteCallback, clientPackageName,
68 cameraId, cameraFacing, clientPid, clientUid, servicePid),
69 mRequestIdCounter(0) {
70
71 ATRACE_CALL();
72 ALOGI("CameraDeviceClient %d: Opened", cameraId);
73}
74
75status_t CameraDeviceClient::initialize(camera_module_t *module)
76{
77 ATRACE_CALL();
78 status_t res;
79
80 res = Camera2ClientBase::initialize(module);
81 if (res != OK) {
82 return res;
83 }
84
85 String8 threadName;
86 mFrameProcessor = new ProFrameProcessor(mDevice);
87 threadName = String8::format("CDU-%d-FrameProc", mCameraId);
88 mFrameProcessor->run(threadName.string());
89
90 mFrameProcessor->registerListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
91 FRAME_PROCESSOR_LISTENER_MAX_ID,
92 /*listener*/this);
93
94 return OK;
95}
96
97CameraDeviceClient::~CameraDeviceClient() {
98}
99
100status_t CameraDeviceClient::submitRequest(sp<CaptureRequest> request,
101 bool streaming) {
102 ATRACE_CALL();
103 ALOGV("%s", __FUNCTION__);
104
105 status_t res;
106
107 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
108
109 Mutex::Autolock icl(mBinderSerializationLock);
110
111 if (!mDevice.get()) return DEAD_OBJECT;
112
113 if (request == 0) {
114 ALOGE("%s: Camera %d: Sent null request. Rejecting request.",
115 __FUNCTION__, mCameraId);
116 return BAD_VALUE;
117 }
118
119 CameraMetadata metadata(request->mMetadata);
120
121 if (metadata.isEmpty()) {
122 ALOGE("%s: Camera %d: Sent empty metadata packet. Rejecting request.",
123 __FUNCTION__, mCameraId);
124 return BAD_VALUE;
125 } else if (request->mSurfaceList.size() == 0) {
126 ALOGE("%s: Camera %d: Requests must have at least one surface target. "
127 "Rejecting request.", __FUNCTION__, mCameraId);
128 return BAD_VALUE;
129 }
130
131 if (!enforceRequestPermissions(metadata)) {
132 // Callee logs
133 return PERMISSION_DENIED;
134 }
135
136 /**
137 * Write in the output stream IDs which we calculate from
138 * the capture request's list of surface targets
139 */
140 Vector<uint8_t> outputStreamIds;
141 outputStreamIds.setCapacity(request->mSurfaceList.size());
142 for (size_t i = 0; i < request->mSurfaceList.size(); ++i) {
143 sp<Surface> surface = request->mSurfaceList[i];
144
145 if (surface == 0) continue;
146
147 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
148 int idx = mStreamMap.indexOfKey(gbp->asBinder());
149
150 // Trying to submit request with surface that wasn't created
151 if (idx == NAME_NOT_FOUND) {
152 ALOGE("%s: Camera %d: Tried to submit a request with a surface that"
153 " we have not called createStream on",
154 __FUNCTION__, mCameraId);
155 return BAD_VALUE;
156 }
157
158 int streamId = mStreamMap.valueAt(idx);
159 outputStreamIds.push_back(streamId);
160 ALOGV("%s: Camera %d: Appending output stream %d to request",
161 __FUNCTION__, mCameraId, streamId);
162 }
163
164 metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS, &outputStreamIds[0],
165 outputStreamIds.size());
166
167 // TODO: @hide ANDROID_REQUEST_ID, or use another request token
168 int32_t requestId = mRequestIdCounter++;
169 metadata.update(ANDROID_REQUEST_ID, &requestId, /*size*/1);
170 ALOGV("%s: Camera %d: Submitting request with ID %d",
171 __FUNCTION__, mCameraId, requestId);
172
173 if (streaming) {
174 res = mDevice->setStreamingRequest(metadata);
175 if (res != OK) {
176 ALOGE("%s: Camera %d: Got error %d after trying to set streaming "
177 "request", __FUNCTION__, mCameraId, res);
178 } else {
179 mStreamingRequestList.push_back(mRequestIdCounter);
180 }
181 } else {
182 res = mDevice->capture(metadata);
183 if (res != OK) {
184 ALOGE("%s: Camera %d: Got error %d after trying to set capture",
185 __FUNCTION__, mCameraId, res);
186 }
187 }
188
189 ALOGV("%s: Camera %d: End of function", __FUNCTION__, mCameraId);
190 if (res == OK) {
191 return requestId;
192 }
193
194 return res;
195}
196
197status_t CameraDeviceClient::cancelRequest(int requestId) {
198 ATRACE_CALL();
199 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
200
201 status_t res;
202
203 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
204
205 Mutex::Autolock icl(mBinderSerializationLock);
206
207 if (!mDevice.get()) return DEAD_OBJECT;
208
209 Vector<int>::iterator it, end;
210 for (it = mStreamingRequestList.begin(), end = mStreamingRequestList.end();
211 it != end; ++it) {
212 if (*it == requestId) {
213 break;
214 }
215 }
216
217 if (it == end) {
218 ALOGE("%s: Camera%d: Did not find request id %d in list of streaming "
219 "requests", __FUNCTION__, mCameraId, requestId);
220 return BAD_VALUE;
221 }
222
223 res = mDevice->clearStreamingRequest();
224
225 if (res == OK) {
226 ALOGV("%s: Camera %d: Successfully cleared streaming request",
227 __FUNCTION__, mCameraId);
228 mStreamingRequestList.erase(it);
229 }
230
231 return res;
232}
233
234status_t CameraDeviceClient::deleteStream(int streamId) {
235 ATRACE_CALL();
236 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
237
238 status_t res;
239 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
240
241 Mutex::Autolock icl(mBinderSerializationLock);
242
243 if (!mDevice.get()) return DEAD_OBJECT;
244
245 // Guard against trying to delete non-created streams
246 ssize_t index = NAME_NOT_FOUND;
247 for (size_t i = 0; i < mStreamMap.size(); ++i) {
248 if (streamId == mStreamMap.valueAt(i)) {
249 index = i;
250 break;
251 }
252 }
253
254 if (index == NAME_NOT_FOUND) {
255 ALOGW("%s: Camera %d: Invalid stream ID (%d) specified, no stream "
256 "created yet", __FUNCTION__, mCameraId, streamId);
257 return BAD_VALUE;
258 }
259
260 // Also returns BAD_VALUE if stream ID was not valid
261 res = mDevice->deleteStream(streamId);
262
263 if (res == BAD_VALUE) {
264 ALOGE("%s: Camera %d: Unexpected BAD_VALUE when deleting stream, but we"
265 " already checked and the stream ID (%d) should be valid.",
266 __FUNCTION__, mCameraId, streamId);
267 } else if (res == OK) {
268 mStreamMap.removeItemsAt(index);
269
270 ALOGV("%s: Camera %d: Successfully deleted stream ID (%d)",
271 __FUNCTION__, mCameraId, streamId);
272 }
273
274 return res;
275}
276
277status_t CameraDeviceClient::createStream(int width, int height, int format,
278 const sp<IGraphicBufferProducer>& bufferProducer)
279{
280 ATRACE_CALL();
281 ALOGV("%s (w = %d, h = %d, f = 0x%x)", __FUNCTION__, width, height, format);
282
283 status_t res;
284 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
285
286 Mutex::Autolock icl(mBinderSerializationLock);
287
288 if (!mDevice.get()) return DEAD_OBJECT;
289
290 // Don't create multiple streams for the same target surface
291 {
292 ssize_t index = mStreamMap.indexOfKey(bufferProducer->asBinder());
293 if (index != NAME_NOT_FOUND) {
294 ALOGW("%s: Camera %d: Buffer producer already has a stream for it "
295 "(ID %d)",
296 __FUNCTION__, mCameraId, index);
297 return ALREADY_EXISTS;
298 }
299 }
300
301 sp<IBinder> binder;
302 sp<ANativeWindow> anw;
303 if (bufferProducer != 0) {
304 binder = bufferProducer->asBinder();
305 anw = new Surface(bufferProducer);
306 }
307
308 // TODO: remove w,h,f since we are ignoring them
309
310 if ((res = anw->query(anw.get(), NATIVE_WINDOW_WIDTH, &width)) != OK) {
311 ALOGE("%s: Camera %d: Failed to query Surface width", __FUNCTION__,
312 mCameraId);
313 return res;
314 }
315 if ((res = anw->query(anw.get(), NATIVE_WINDOW_HEIGHT, &height)) != OK) {
316 ALOGE("%s: Camera %d: Failed to query Surface height", __FUNCTION__,
317 mCameraId);
318 return res;
319 }
320 if ((res = anw->query(anw.get(), NATIVE_WINDOW_FORMAT, &format)) != OK) {
321 ALOGE("%s: Camera %d: Failed to query Surface format", __FUNCTION__,
322 mCameraId);
323 return res;
324 }
325
326 // FIXME: remove this override since the default format should be
327 // IMPLEMENTATION_DEFINED. b/9487482
328 if (format != HAL_PIXEL_FORMAT_BLOB &&
329 format != HAL_PIXEL_FORMAT_YCbCr_420_888) {
330 ALOGW("%s: Camera %d: Overriding format 0x%x to IMPLEMENTATION_DEFINED",
331 __FUNCTION__, mCameraId, format);
332 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
333 }
334
335 // TODO: add startConfigure/stopConfigure call to CameraDeviceBase
336 // this will make it so Camera3Device doesn't call configure_streams
337 // after each call, but only once we are done with all.
338
339 int streamId = -1;
Eino-Ville Talvalac7ba4a52013-07-01 09:23:55 -0700340 if (format == HAL_PIXEL_FORMAT_BLOB) {
341 // JPEG buffers need to be sized for maximum possible compressed size
342 CameraMetadata staticInfo = mDevice->info();
343 camera_metadata_entry_t entry = staticInfo.find(ANDROID_JPEG_MAX_SIZE);
344 if (entry.count == 0) {
345 ALOGE("%s: Camera %d: Can't find maximum JPEG size in "
346 "static metadata!", __FUNCTION__, mCameraId);
347 return INVALID_OPERATION;
348 }
349 int32_t maxJpegSize = entry.data.i32[0];
350 res = mDevice->createStream(anw, width, height, format, maxJpegSize,
351 &streamId);
352 } else {
353 // All other streams are a known size
354 res = mDevice->createStream(anw, width, height, format, /*size*/0,
355 &streamId);
356 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700357
358 if (res == OK) {
359 mStreamMap.add(bufferProducer->asBinder(), streamId);
360
361 ALOGV("%s: Camera %d: Successfully created a new stream ID %d",
362 __FUNCTION__, mCameraId, streamId);
363 return streamId;
364 }
365
366 return res;
367}
368
369// Create a request object from a template.
370status_t CameraDeviceClient::createDefaultRequest(int templateId,
371 /*out*/
372 CameraMetadata* request)
373{
374 ATRACE_CALL();
375 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
376
377 status_t res;
378 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
379
380 Mutex::Autolock icl(mBinderSerializationLock);
381
382 if (!mDevice.get()) return DEAD_OBJECT;
383
384 CameraMetadata metadata;
385 if ( (res = mDevice->createDefaultRequest(templateId, &metadata) ) == OK &&
386 request != NULL) {
387
388 request->swap(metadata);
389 }
390
391 return res;
392}
393
394status_t CameraDeviceClient::getCameraInfo(int cameraId,
395 /*out*/
396 camera_metadata** info)
397{
398 ATRACE_CALL();
399 ALOGV("%s", __FUNCTION__);
400
401 status_t res = OK;
402
403 // TODO: remove cameraId. this should be device-specific info, not static.
404 if (cameraId != mCameraId) {
405 return INVALID_OPERATION;
406 }
407
408 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
409
410 Mutex::Autolock icl(mBinderSerializationLock);
411
412 if (!mDevice.get()) return DEAD_OBJECT;
413
414 CameraMetadata deviceInfo = mDevice->info();
415 *info = deviceInfo.release();
416
417 return res;
418}
419
420status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
421 String8 result;
422 result.appendFormat("CameraDeviceClient[%d] (%p) PID: %d, dump:\n",
423 mCameraId,
424 getRemoteCallback()->asBinder().get(),
425 mClientPid);
426 result.append(" State: ");
427
428 // TODO: print dynamic/request section from most recent requests
429 mFrameProcessor->dump(fd, args);
430
431 return dumpDevice(fd, args);
432}
433
434// TODO: refactor the code below this with IProCameraUser.
435// it's 100% copy-pasted, so lets not change it right now to make it easier.
436
437void CameraDeviceClient::detachDevice() {
438 if (mDevice == 0) return;
439
440 ALOGV("Camera %d: Stopping processors", mCameraId);
441
442 mFrameProcessor->removeListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
443 FRAME_PROCESSOR_LISTENER_MAX_ID,
444 /*listener*/this);
445 mFrameProcessor->requestExit();
446 ALOGV("Camera %d: Waiting for threads", mCameraId);
447 mFrameProcessor->join();
448 ALOGV("Camera %d: Disconnecting device", mCameraId);
449
450 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
451 {
452 mDevice->clearStreamingRequest();
453
454 status_t code;
455 if ((code = mDevice->waitUntilDrained()) != OK) {
456 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
457 code);
458 }
459 }
460
461 Camera2ClientBase::detachDevice();
462}
463
464/** Device-related methods */
465void CameraDeviceClient::onFrameAvailable(int32_t frameId,
466 const CameraMetadata& frame) {
467 ATRACE_CALL();
468 ALOGV("%s", __FUNCTION__);
469
470 Mutex::Autolock icl(mBinderSerializationLock);
471 SharedCameraCallbacks::Lock l(mSharedCameraCallbacks);
472
473 if (mRemoteCallback != NULL) {
474 ALOGV("%s: frame = %p ", __FUNCTION__, &frame);
475 mRemoteCallback->onResultReceived(frameId, frame);
476 }
477
478}
479
480// TODO: move to Camera2ClientBase
481bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
482
483 const int pid = IPCThreadState::self()->getCallingPid();
484 const int selfPid = getpid();
485 camera_metadata_entry_t entry;
486
487 /**
488 * Mixin default important security values
489 * - android.led.transmit = defaulted ON
490 */
491 CameraMetadata staticInfo = mDevice->info();
492 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
493 for(size_t i = 0; i < entry.count; ++i) {
494 uint8_t led = entry.data.u8[i];
495
496 switch(led) {
497 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
498 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
499 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
500 metadata.update(ANDROID_LED_TRANSMIT,
501 &transmitDefault, 1);
502 }
503 break;
504 }
505 }
506 }
507
508 // We can do anything!
509 if (pid == selfPid) {
510 return true;
511 }
512
513 /**
514 * Permission check special fields in the request
515 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
516 */
517 entry = metadata.find(ANDROID_LED_TRANSMIT);
518 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
519 String16 permissionString =
520 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
521 if (!checkCallingPermission(permissionString)) {
522 const int uid = IPCThreadState::self()->getCallingUid();
523 ALOGE("Permission Denial: "
524 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
525 return false;
526 }
527 }
528
529 return true;
530}
531
532} // namespace android