1 /*
2 * Copyright (C) 2012 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 "CameraClient"
18 //#define LOG_NDEBUG 0
19
20 #include <cutils/atomic.h>
21 #include <cutils/properties.h>
22 #include <gui/Surface.h>
23 #include <media/hardware/HardwareAPI.h>
24
25 #include "api1/CameraClient.h"
26 #include "device1/CameraHardwareInterface.h"
27 #include "CameraService.h"
28 #include "utils/CameraThreadState.h"
29
30 namespace android {
31
32 #define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
33 #define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
34
CameraClient(const sp<CameraService> & cameraService,const sp<hardware::ICameraClient> & cameraClient,const String16 & clientPackageName,int cameraId,int cameraFacing,int clientPid,int clientUid,int servicePid)35 CameraClient::CameraClient(const sp<CameraService>& cameraService,
36 const sp<hardware::ICameraClient>& cameraClient,
37 const String16& clientPackageName,
38 int cameraId, int cameraFacing,
39 int clientPid, int clientUid,
40 int servicePid):
41 Client(cameraService, cameraClient, clientPackageName,
42 String8::format("%d", cameraId), cameraId, cameraFacing, clientPid,
43 clientUid, servicePid)
44 {
45 int callingPid = CameraThreadState::getCallingPid();
46 LOG1("CameraClient::CameraClient E (pid %d, id %d)", callingPid, cameraId);
47
48 mHardware = NULL;
49 mMsgEnabled = 0;
50 mSurface = 0;
51 mPreviewWindow = 0;
52 mDestructionStarted = false;
53
54 // Callback is disabled by default
55 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
56 mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT);
57 mPlayShutterSound = true;
58 LOG1("CameraClient::CameraClient X (pid %d, id %d)", callingPid, cameraId);
59 }
60
initialize(sp<CameraProviderManager> manager,const String8 &)61 status_t CameraClient::initialize(sp<CameraProviderManager> manager,
62 const String8& /*monitorTags*/) {
63 int callingPid = CameraThreadState::getCallingPid();
64 status_t res;
65
66 LOG1("CameraClient::initialize E (pid %d, id %d)", callingPid, mCameraId);
67
68 // Verify ops permissions
69 res = startCameraOps();
70 if (res != OK) {
71 return res;
72 }
73
74 char camera_device_name[10];
75 snprintf(camera_device_name, sizeof(camera_device_name), "%d", mCameraId);
76
77 mHardware = new CameraHardwareInterface(camera_device_name);
78 res = mHardware->initialize(manager);
79 if (res != OK) {
80 ALOGE("%s: Camera %d: unable to initialize device: %s (%d)",
81 __FUNCTION__, mCameraId, strerror(-res), res);
82 mHardware.clear();
83 return res;
84 }
85
86 mHardware->setCallbacks(notifyCallback,
87 dataCallback,
88 dataCallbackTimestamp,
89 handleCallbackTimestampBatch,
90 (void *)(uintptr_t)mCameraId);
91
92 // Enable zoom, error, focus, and metadata messages by default
93 enableMsgType(CAMERA_MSG_ERROR | CAMERA_MSG_ZOOM | CAMERA_MSG_FOCUS |
94 CAMERA_MSG_PREVIEW_METADATA | CAMERA_MSG_FOCUS_MOVE);
95
96 LOG1("CameraClient::initialize X (pid %d, id %d)", callingPid, mCameraId);
97 return OK;
98 }
99
100
101 // tear down the client
~CameraClient()102 CameraClient::~CameraClient() {
103 mDestructionStarted = true;
104 int callingPid = CameraThreadState::getCallingPid();
105 LOG1("CameraClient::~CameraClient E (pid %d, this %p)", callingPid, this);
106
107 disconnect();
108 LOG1("CameraClient::~CameraClient X (pid %d, this %p)", callingPid, this);
109 }
110
dump(int fd,const Vector<String16> & args)111 status_t CameraClient::dump(int fd, const Vector<String16>& args) {
112 return BasicClient::dump(fd, args);
113 }
114
dumpClient(int fd,const Vector<String16> & args)115 status_t CameraClient::dumpClient(int fd, const Vector<String16>& args) {
116 const size_t SIZE = 256;
117 char buffer[SIZE];
118
119 size_t len = snprintf(buffer, SIZE, "Client[%d] (%p) with UID %d\n",
120 mCameraId,
121 (getRemoteCallback() != NULL ?
122 IInterface::asBinder(getRemoteCallback()).get() : NULL),
123 mClientUid);
124 len = (len > SIZE - 1) ? SIZE - 1 : len;
125 write(fd, buffer, len);
126
127 len = snprintf(buffer, SIZE, "Latest set parameters:\n");
128 len = (len > SIZE - 1) ? SIZE - 1 : len;
129 write(fd, buffer, len);
130
131 mLatestSetParameters.dump(fd, args);
132
133 const char *enddump = "\n\n";
134 write(fd, enddump, strlen(enddump));
135
136 sp<CameraHardwareInterface> hardware = mHardware;
137 if (hardware != nullptr) {
138 return hardware->dump(fd, args);
139 }
140 ALOGI("%s: camera device closed already, skip dumping", __FUNCTION__);
141 return OK;
142 }
143
144 // ----------------------------------------------------------------------------
145
checkPid() const146 status_t CameraClient::checkPid() const {
147 int callingPid = CameraThreadState::getCallingPid();
148 if (callingPid == mClientPid) return NO_ERROR;
149
150 ALOGW("attempt to use a locked camera from a different process"
151 " (old pid %d, new pid %d)", mClientPid, callingPid);
152 return EBUSY;
153 }
154
checkPidAndHardware() const155 status_t CameraClient::checkPidAndHardware() const {
156 if (mHardware == 0) {
157 ALOGE("attempt to use a camera after disconnect() (pid %d)",
158 CameraThreadState::getCallingPid());
159 return INVALID_OPERATION;
160 }
161 status_t result = checkPid();
162 if (result != NO_ERROR) return result;
163 return NO_ERROR;
164 }
165
lock()166 status_t CameraClient::lock() {
167 int callingPid = CameraThreadState::getCallingPid();
168 LOG1("lock (pid %d)", callingPid);
169 Mutex::Autolock lock(mLock);
170
171 // lock camera to this client if the the camera is unlocked
172 if (mClientPid == 0) {
173 mClientPid = callingPid;
174 return NO_ERROR;
175 }
176
177 // returns NO_ERROR if the client already owns the camera, EBUSY otherwise
178 return checkPid();
179 }
180
unlock()181 status_t CameraClient::unlock() {
182 int callingPid = CameraThreadState::getCallingPid();
183 LOG1("unlock (pid %d)", callingPid);
184 Mutex::Autolock lock(mLock);
185
186 // allow anyone to use camera (after they lock the camera)
187 status_t result = checkPid();
188 if (result == NO_ERROR) {
189 if (mHardware->recordingEnabled()) {
190 ALOGE("Not allowed to unlock camera during recording.");
191 return INVALID_OPERATION;
192 }
193 mClientPid = 0;
194 LOG1("clear mRemoteCallback (pid %d)", callingPid);
195 // we need to remove the reference to ICameraClient so that when the app
196 // goes away, the reference count goes to 0.
197 mRemoteCallback.clear();
198 }
199 return result;
200 }
201
202 // connect a new client to the camera
connect(const sp<hardware::ICameraClient> & client)203 status_t CameraClient::connect(const sp<hardware::ICameraClient>& client) {
204 int callingPid = CameraThreadState::getCallingPid();
205 LOG1("connect E (pid %d)", callingPid);
206 Mutex::Autolock lock(mLock);
207
208 if (mClientPid != 0 && checkPid() != NO_ERROR) {
209 ALOGW("Tried to connect to a locked camera (old pid %d, new pid %d)",
210 mClientPid, callingPid);
211 return EBUSY;
212 }
213
214 if (mRemoteCallback != 0 &&
215 (IInterface::asBinder(client) == IInterface::asBinder(mRemoteCallback))) {
216 LOG1("Connect to the same client");
217 return NO_ERROR;
218 }
219
220 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
221 mClientPid = callingPid;
222 mRemoteCallback = client;
223
224 LOG1("connect X (pid %d)", callingPid);
225 return NO_ERROR;
226 }
227
disconnectWindow(const sp<ANativeWindow> & window)228 static void disconnectWindow(const sp<ANativeWindow>& window) {
229 if (window != 0) {
230 status_t result = native_window_api_disconnect(window.get(),
231 NATIVE_WINDOW_API_CAMERA);
232 if (result != NO_ERROR) {
233 ALOGW("native_window_api_disconnect failed: %s (%d)", strerror(-result),
234 result);
235 }
236 }
237 }
238
disconnect()239 binder::Status CameraClient::disconnect() {
240 int callingPid = CameraThreadState::getCallingPid();
241 LOG1("disconnect E (pid %d)", callingPid);
242 Mutex::Autolock lock(mLock);
243
244 binder::Status res = binder::Status::ok();
245 // Allow both client and the cameraserver to disconnect at all times
246 if (callingPid != mClientPid && callingPid != mServicePid) {
247 ALOGW("different client - don't disconnect");
248 return res;
249 }
250
251 // Make sure disconnect() is done once and once only, whether it is called
252 // from the user directly, or called by the destructor.
253 if (mHardware == 0) return res;
254
255 LOG1("hardware teardown");
256 // Before destroying mHardware, we must make sure it's in the
257 // idle state.
258 // Turn off all messages.
259 disableMsgType(CAMERA_MSG_ALL_MSGS);
260 mHardware->stopPreview();
261 sCameraService->updateProxyDeviceState(
262 hardware::ICameraServiceProxy::CAMERA_STATE_IDLE,
263 mCameraIdStr, mCameraFacing, mClientPackageName,
264 hardware::ICameraServiceProxy::CAMERA_API_LEVEL_1);
265 mHardware->cancelPicture();
266 // Release the hardware resources.
267 mHardware->release();
268
269 // Release the held ANativeWindow resources.
270 if (mPreviewWindow != 0) {
271 disconnectWindow(mPreviewWindow);
272 mPreviewWindow = 0;
273 mHardware->setPreviewWindow(mPreviewWindow);
274 }
275 mHardware.clear();
276
277 CameraService::Client::disconnect();
278
279 LOG1("disconnect X (pid %d)", callingPid);
280
281 return res;
282 }
283
284 // ----------------------------------------------------------------------------
285
setPreviewWindow(const sp<IBinder> & binder,const sp<ANativeWindow> & window)286 status_t CameraClient::setPreviewWindow(const sp<IBinder>& binder,
287 const sp<ANativeWindow>& window) {
288 Mutex::Autolock lock(mLock);
289 status_t result = checkPidAndHardware();
290 if (result != NO_ERROR) return result;
291
292 // return if no change in surface.
293 if (binder == mSurface) {
294 return NO_ERROR;
295 }
296
297 if (window != 0) {
298 result = native_window_api_connect(window.get(), NATIVE_WINDOW_API_CAMERA);
299 if (result != NO_ERROR) {
300 ALOGE("native_window_api_connect failed: %s (%d)", strerror(-result),
301 result);
302 return result;
303 }
304 }
305
306 // If preview has been already started, register preview buffers now.
307 if (mHardware->previewEnabled()) {
308 if (window != 0) {
309 mHardware->setPreviewScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
310 mHardware->setPreviewTransform(mOrientation);
311 result = mHardware->setPreviewWindow(window);
312 }
313 }
314
315 if (result == NO_ERROR) {
316 // Everything has succeeded. Disconnect the old window and remember the
317 // new window.
318 disconnectWindow(mPreviewWindow);
319 mSurface = binder;
320 mPreviewWindow = window;
321 } else {
322 // Something went wrong after we connected to the new window, so
323 // disconnect here.
324 disconnectWindow(window);
325 }
326
327 return result;
328 }
329
330 // set the buffer consumer that the preview will use
setPreviewTarget(const sp<IGraphicBufferProducer> & bufferProducer)331 status_t CameraClient::setPreviewTarget(
332 const sp<IGraphicBufferProducer>& bufferProducer) {
333 LOG1("setPreviewTarget(%p) (pid %d)", bufferProducer.get(),
334 CameraThreadState::getCallingPid());
335
336 sp<IBinder> binder;
337 sp<ANativeWindow> window;
338 if (bufferProducer != 0) {
339 binder = IInterface::asBinder(bufferProducer);
340 // Using controlledByApp flag to ensure that the buffer queue remains in
341 // async mode for the old camera API, where many applications depend
342 // on that behavior.
343 window = new Surface(bufferProducer, /*controlledByApp*/ true);
344 }
345 return setPreviewWindow(binder, window);
346 }
347
348 // set the preview callback flag to affect how the received frames from
349 // preview are handled.
setPreviewCallbackFlag(int callback_flag)350 void CameraClient::setPreviewCallbackFlag(int callback_flag) {
351 LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, CameraThreadState::getCallingPid());
352 Mutex::Autolock lock(mLock);
353 if (checkPidAndHardware() != NO_ERROR) return;
354
355 mPreviewCallbackFlag = callback_flag;
356 if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
357 enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
358 } else {
359 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
360 }
361 }
362
setPreviewCallbackTarget(const sp<IGraphicBufferProducer> & callbackProducer)363 status_t CameraClient::setPreviewCallbackTarget(
364 const sp<IGraphicBufferProducer>& callbackProducer) {
365 (void)callbackProducer;
366 ALOGE("%s: Unimplemented!", __FUNCTION__);
367 return INVALID_OPERATION;
368 }
369
370 // start preview mode
startPreview()371 status_t CameraClient::startPreview() {
372 LOG1("startPreview (pid %d)", CameraThreadState::getCallingPid());
373 return startCameraMode(CAMERA_PREVIEW_MODE);
374 }
375
376 // start recording mode
startRecording()377 status_t CameraClient::startRecording() {
378 LOG1("startRecording (pid %d)", CameraThreadState::getCallingPid());
379 return startCameraMode(CAMERA_RECORDING_MODE);
380 }
381
382 // start preview or recording
startCameraMode(camera_mode mode)383 status_t CameraClient::startCameraMode(camera_mode mode) {
384 LOG1("startCameraMode(%d)", mode);
385 Mutex::Autolock lock(mLock);
386 status_t result = checkPidAndHardware();
387 if (result != NO_ERROR) return result;
388
389 switch(mode) {
390 case CAMERA_PREVIEW_MODE:
391 if (mSurface == 0 && mPreviewWindow == 0) {
392 LOG1("mSurface is not set yet.");
393 // still able to start preview in this case.
394 }
395 return startPreviewMode();
396 case CAMERA_RECORDING_MODE:
397 if (mSurface == 0 && mPreviewWindow == 0) {
398 ALOGE("mSurface or mPreviewWindow must be set before startRecordingMode.");
399 return INVALID_OPERATION;
400 }
401 return startRecordingMode();
402 default:
403 return UNKNOWN_ERROR;
404 }
405 }
406
startPreviewMode()407 status_t CameraClient::startPreviewMode() {
408 LOG1("startPreviewMode");
409 status_t result = NO_ERROR;
410
411 // if preview has been enabled, nothing needs to be done
412 if (mHardware->previewEnabled()) {
413 return NO_ERROR;
414 }
415
416 if (mPreviewWindow != 0) {
417 mHardware->setPreviewScalingMode(
418 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
419 mHardware->setPreviewTransform(mOrientation);
420 }
421 mHardware->setPreviewWindow(mPreviewWindow);
422 result = mHardware->startPreview();
423 if (result == NO_ERROR) {
424 sCameraService->updateProxyDeviceState(
425 hardware::ICameraServiceProxy::CAMERA_STATE_ACTIVE,
426 mCameraIdStr, mCameraFacing, mClientPackageName,
427 hardware::ICameraServiceProxy::CAMERA_API_LEVEL_1);
428 }
429 return result;
430 }
431
startRecordingMode()432 status_t CameraClient::startRecordingMode() {
433 LOG1("startRecordingMode");
434 status_t result = NO_ERROR;
435
436 // if recording has been enabled, nothing needs to be done
437 if (mHardware->recordingEnabled()) {
438 return NO_ERROR;
439 }
440
441 // if preview has not been started, start preview first
442 if (!mHardware->previewEnabled()) {
443 result = startPreviewMode();
444 if (result != NO_ERROR) {
445 return result;
446 }
447 }
448
449 // start recording mode
450 enableMsgType(CAMERA_MSG_VIDEO_FRAME);
451 sCameraService->playSound(CameraService::SOUND_RECORDING_START);
452 result = mHardware->startRecording();
453 if (result != NO_ERROR) {
454 ALOGE("mHardware->startRecording() failed with status %d", result);
455 }
456 return result;
457 }
458
459 // stop preview mode
stopPreview()460 void CameraClient::stopPreview() {
461 LOG1("stopPreview (pid %d)", CameraThreadState::getCallingPid());
462 Mutex::Autolock lock(mLock);
463 if (checkPidAndHardware() != NO_ERROR) return;
464
465
466 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
467 mHardware->stopPreview();
468 sCameraService->updateProxyDeviceState(
469 hardware::ICameraServiceProxy::CAMERA_STATE_IDLE,
470 mCameraIdStr, mCameraFacing, mClientPackageName,
471 hardware::ICameraServiceProxy::CAMERA_API_LEVEL_1);
472 mPreviewBuffer.clear();
473 }
474
475 // stop recording mode
stopRecording()476 void CameraClient::stopRecording() {
477 LOG1("stopRecording (pid %d)", CameraThreadState::getCallingPid());
478 {
479 Mutex::Autolock lock(mLock);
480 if (checkPidAndHardware() != NO_ERROR) return;
481
482 disableMsgType(CAMERA_MSG_VIDEO_FRAME);
483 mHardware->stopRecording();
484 sCameraService->playSound(CameraService::SOUND_RECORDING_STOP);
485
486 mPreviewBuffer.clear();
487 }
488
489 {
490 Mutex::Autolock l(mAvailableCallbackBuffersLock);
491 if (!mAvailableCallbackBuffers.empty()) {
492 mAvailableCallbackBuffers.clear();
493 }
494 }
495 }
496
497 // release a recording frame
releaseRecordingFrame(const sp<IMemory> & mem)498 void CameraClient::releaseRecordingFrame(const sp<IMemory>& mem) {
499 Mutex::Autolock lock(mLock);
500 if (checkPidAndHardware() != NO_ERROR) return;
501 if (mem == nullptr) {
502 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26164272",
503 CameraThreadState::getCallingUid(), nullptr, 0);
504 return;
505 }
506
507 mHardware->releaseRecordingFrame(mem);
508 }
509
releaseRecordingFrameHandle(native_handle_t * handle)510 void CameraClient::releaseRecordingFrameHandle(native_handle_t *handle) {
511 if (handle == nullptr) return;
512 Mutex::Autolock lock(mLock);
513 sp<IMemory> dataPtr;
514 {
515 Mutex::Autolock l(mAvailableCallbackBuffersLock);
516 if (!mAvailableCallbackBuffers.empty()) {
517 dataPtr = mAvailableCallbackBuffers.back();
518 mAvailableCallbackBuffers.pop_back();
519 }
520 }
521
522 if (dataPtr == nullptr) {
523 ALOGE("%s: %d: No callback buffer available. Dropping a native handle.", __FUNCTION__,
524 __LINE__);
525 native_handle_close(handle);
526 native_handle_delete(handle);
527 return;
528 } else if (dataPtr->size() != sizeof(VideoNativeHandleMetadata)) {
529 ALOGE("%s: %d: Callback buffer size doesn't match VideoNativeHandleMetadata", __FUNCTION__,
530 __LINE__);
531 native_handle_close(handle);
532 native_handle_delete(handle);
533 return;
534 }
535
536 if (mHardware != nullptr) {
537 VideoNativeHandleMetadata *metadata = (VideoNativeHandleMetadata*)(dataPtr->pointer());
538 metadata->eType = kMetadataBufferTypeNativeHandleSource;
539 metadata->pHandle = handle;
540 mHardware->releaseRecordingFrame(dataPtr);
541 }
542 }
543
releaseRecordingFrameHandleBatch(const std::vector<native_handle_t * > & handles)544 void CameraClient::releaseRecordingFrameHandleBatch(const std::vector<native_handle_t*>& handles) {
545 Mutex::Autolock lock(mLock);
546 bool disconnected = (mHardware == nullptr);
547 size_t n = handles.size();
548 std::vector<sp<IMemory>> frames;
549 if (!disconnected) {
550 frames.reserve(n);
551 }
552 bool error = false;
553 for (auto& handle : handles) {
554 sp<IMemory> dataPtr;
555 {
556 Mutex::Autolock l(mAvailableCallbackBuffersLock);
557 if (!mAvailableCallbackBuffers.empty()) {
558 dataPtr = mAvailableCallbackBuffers.back();
559 mAvailableCallbackBuffers.pop_back();
560 }
561 }
562
563 if (dataPtr == nullptr) {
564 ALOGE("%s: %d: No callback buffer available. Dropping frames.", __FUNCTION__,
565 __LINE__);
566 error = true;
567 break;
568 } else if (dataPtr->size() != sizeof(VideoNativeHandleMetadata)) {
569 ALOGE("%s: %d: Callback buffer must be VideoNativeHandleMetadata", __FUNCTION__,
570 __LINE__);
571 error = true;
572 break;
573 }
574
575 if (!disconnected) {
576 VideoNativeHandleMetadata *metadata = (VideoNativeHandleMetadata*)(dataPtr->pointer());
577 metadata->eType = kMetadataBufferTypeNativeHandleSource;
578 metadata->pHandle = handle;
579 frames.push_back(dataPtr);
580 }
581 }
582
583 if (error) {
584 for (auto& handle : handles) {
585 native_handle_close(handle);
586 native_handle_delete(handle);
587 }
588 } else if (!disconnected) {
589 mHardware->releaseRecordingFrameBatch(frames);
590 }
591 return;
592 }
593
setVideoBufferMode(int32_t videoBufferMode)594 status_t CameraClient::setVideoBufferMode(int32_t videoBufferMode) {
595 LOG1("setVideoBufferMode: %d", videoBufferMode);
596 bool enableMetadataInBuffers = false;
597
598 if (videoBufferMode == VIDEO_BUFFER_MODE_DATA_CALLBACK_METADATA) {
599 enableMetadataInBuffers = true;
600 } else if (videoBufferMode != VIDEO_BUFFER_MODE_DATA_CALLBACK_YUV) {
601 ALOGE("%s: %d: videoBufferMode %d is not supported.", __FUNCTION__, __LINE__,
602 videoBufferMode);
603 return BAD_VALUE;
604 }
605
606 Mutex::Autolock lock(mLock);
607 if (checkPidAndHardware() != NO_ERROR) {
608 return UNKNOWN_ERROR;
609 }
610
611 return mHardware->storeMetaDataInBuffers(enableMetadataInBuffers);
612 }
613
previewEnabled()614 bool CameraClient::previewEnabled() {
615 LOG1("previewEnabled (pid %d)", CameraThreadState::getCallingPid());
616
617 Mutex::Autolock lock(mLock);
618 if (checkPidAndHardware() != NO_ERROR) return false;
619 return mHardware->previewEnabled();
620 }
621
recordingEnabled()622 bool CameraClient::recordingEnabled() {
623 LOG1("recordingEnabled (pid %d)", CameraThreadState::getCallingPid());
624
625 Mutex::Autolock lock(mLock);
626 if (checkPidAndHardware() != NO_ERROR) return false;
627 return mHardware->recordingEnabled();
628 }
629
autoFocus()630 status_t CameraClient::autoFocus() {
631 LOG1("autoFocus (pid %d)", CameraThreadState::getCallingPid());
632
633 Mutex::Autolock lock(mLock);
634 status_t result = checkPidAndHardware();
635 if (result != NO_ERROR) return result;
636
637 return mHardware->autoFocus();
638 }
639
cancelAutoFocus()640 status_t CameraClient::cancelAutoFocus() {
641 LOG1("cancelAutoFocus (pid %d)", CameraThreadState::getCallingPid());
642
643 Mutex::Autolock lock(mLock);
644 status_t result = checkPidAndHardware();
645 if (result != NO_ERROR) return result;
646
647 return mHardware->cancelAutoFocus();
648 }
649
650 // take a picture - image is returned in callback
takePicture(int msgType)651 status_t CameraClient::takePicture(int msgType) {
652 LOG1("takePicture (pid %d): 0x%x", CameraThreadState::getCallingPid(), msgType);
653
654 Mutex::Autolock lock(mLock);
655 status_t result = checkPidAndHardware();
656 if (result != NO_ERROR) return result;
657
658 if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
659 (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
660 ALOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
661 " cannot be both enabled");
662 return BAD_VALUE;
663 }
664
665 // We only accept picture related message types
666 // and ignore other types of messages for takePicture().
667 int picMsgType = msgType
668 & (CAMERA_MSG_SHUTTER |
669 CAMERA_MSG_POSTVIEW_FRAME |
670 CAMERA_MSG_RAW_IMAGE |
671 CAMERA_MSG_RAW_IMAGE_NOTIFY |
672 CAMERA_MSG_COMPRESSED_IMAGE);
673
674 enableMsgType(picMsgType);
675
676 return mHardware->takePicture();
677 }
678
679 // set preview/capture parameters - key/value pairs
setParameters(const String8 & params)680 status_t CameraClient::setParameters(const String8& params) {
681 LOG1("setParameters (pid %d) (%s)", CameraThreadState::getCallingPid(), params.string());
682
683 Mutex::Autolock lock(mLock);
684 status_t result = checkPidAndHardware();
685 if (result != NO_ERROR) return result;
686
687 mLatestSetParameters = CameraParameters(params);
688 CameraParameters p(params);
689 return mHardware->setParameters(p);
690 }
691
692 // get preview/capture parameters - key/value pairs
getParameters() const693 String8 CameraClient::getParameters() const {
694 Mutex::Autolock lock(mLock);
695 // The camera service can unconditionally get the parameters at all times
696 if (CameraThreadState::getCallingPid() != mServicePid && checkPidAndHardware() != NO_ERROR) {
697 return String8();
698 }
699
700 String8 params(mHardware->getParameters().flatten());
701 LOG1("getParameters (pid %d) (%s)", CameraThreadState::getCallingPid(), params.string());
702 return params;
703 }
704
705 // enable shutter sound
enableShutterSound(bool enable)706 status_t CameraClient::enableShutterSound(bool enable) {
707 LOG1("enableShutterSound (pid %d)", CameraThreadState::getCallingPid());
708
709 status_t result = checkPidAndHardware();
710 if (result != NO_ERROR) return result;
711
712 if (enable) {
713 mPlayShutterSound = true;
714 return OK;
715 }
716
717 mPlayShutterSound = false;
718 return OK;
719 }
720
sendCommand(int32_t cmd,int32_t arg1,int32_t arg2)721 status_t CameraClient::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
722 LOG1("sendCommand (pid %d)", CameraThreadState::getCallingPid());
723 int orientation;
724 Mutex::Autolock lock(mLock);
725 status_t result = checkPidAndHardware();
726 if (result != NO_ERROR) return result;
727
728 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
729 // Mirror the preview if the camera is front-facing.
730 orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT);
731 if (orientation == -1) return BAD_VALUE;
732
733 if (mOrientation != orientation) {
734 mOrientation = orientation;
735 if (mPreviewWindow != 0) {
736 mHardware->setPreviewTransform(mOrientation);
737 }
738 }
739 return OK;
740 } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) {
741 switch (arg1) {
742 case 0:
743 return enableShutterSound(false);
744 case 1:
745 return enableShutterSound(true);
746 default:
747 return BAD_VALUE;
748 }
749 return OK;
750 } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) {
751 sCameraService->playSound(CameraService::SOUND_RECORDING_START);
752 } else if (cmd == CAMERA_CMD_SET_VIDEO_BUFFER_COUNT) {
753 // Silently ignore this command
754 return INVALID_OPERATION;
755 } else if (cmd == CAMERA_CMD_PING) {
756 // If mHardware is 0, checkPidAndHardware will return error.
757 return OK;
758 }
759
760 return mHardware->sendCommand(cmd, arg1, arg2);
761 }
762
763 // ----------------------------------------------------------------------------
764
enableMsgType(int32_t msgType)765 void CameraClient::enableMsgType(int32_t msgType) {
766 android_atomic_or(msgType, &mMsgEnabled);
767 mHardware->enableMsgType(msgType);
768 }
769
disableMsgType(int32_t msgType)770 void CameraClient::disableMsgType(int32_t msgType) {
771 android_atomic_and(~msgType, &mMsgEnabled);
772 mHardware->disableMsgType(msgType);
773 }
774
775 #define CHECK_MESSAGE_INTERVAL 10 // 10ms
lockIfMessageWanted(int32_t msgType)776 bool CameraClient::lockIfMessageWanted(int32_t msgType) {
777 int sleepCount = 0;
778 while (mMsgEnabled & msgType) {
779 if (mLock.tryLock() == NO_ERROR) {
780 if (sleepCount > 0) {
781 LOG1("lockIfMessageWanted(%d): waited for %d ms",
782 msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
783 }
784
785 // If messages are no longer enabled after acquiring lock, release and drop message
786 if ((mMsgEnabled & msgType) == 0) {
787 mLock.unlock();
788 break;
789 }
790
791 return true;
792 }
793 if (sleepCount++ == 0) {
794 LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
795 }
796 usleep(CHECK_MESSAGE_INTERVAL * 1000);
797 }
798 ALOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
799 return false;
800 }
801
getClientFromCookie(void * user)802 sp<CameraClient> CameraClient::getClientFromCookie(void* user) {
803 String8 cameraId = String8::format("%d", (int)(intptr_t) user);
804 auto clientDescriptor = sCameraService->mActiveClientManager.get(cameraId);
805 if (clientDescriptor != nullptr) {
806 return sp<CameraClient>{
807 static_cast<CameraClient*>(clientDescriptor->getValue().get())};
808 }
809 return sp<CameraClient>{nullptr};
810 }
811
812 // Callback messages can be dispatched to internal handlers or pass to our
813 // client's callback functions, depending on the message type.
814 //
815 // notifyCallback:
816 // CAMERA_MSG_SHUTTER handleShutter
817 // (others) c->notifyCallback
818 // dataCallback:
819 // CAMERA_MSG_PREVIEW_FRAME handlePreviewData
820 // CAMERA_MSG_POSTVIEW_FRAME handlePostview
821 // CAMERA_MSG_RAW_IMAGE handleRawPicture
822 // CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture
823 // (others) c->dataCallback
824 // dataCallbackTimestamp
825 // (others) c->dataCallbackTimestamp
826
notifyCallback(int32_t msgType,int32_t ext1,int32_t ext2,void * user)827 void CameraClient::notifyCallback(int32_t msgType, int32_t ext1,
828 int32_t ext2, void* user) {
829 LOG2("notifyCallback(%d)", msgType);
830
831 sp<CameraClient> client = getClientFromCookie(user);
832 if (client.get() == nullptr) return;
833
834 if (!client->lockIfMessageWanted(msgType)) return;
835
836 switch (msgType) {
837 case CAMERA_MSG_SHUTTER:
838 // ext1 is the dimension of the yuv picture.
839 client->handleShutter();
840 break;
841 default:
842 client->handleGenericNotify(msgType, ext1, ext2);
843 break;
844 }
845 }
846
dataCallback(int32_t msgType,const sp<IMemory> & dataPtr,camera_frame_metadata_t * metadata,void * user)847 void CameraClient::dataCallback(int32_t msgType,
848 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) {
849 LOG2("dataCallback(%d)", msgType);
850
851 sp<CameraClient> client = getClientFromCookie(user);
852 if (client.get() == nullptr) return;
853
854 if (!client->lockIfMessageWanted(msgType)) return;
855 if (dataPtr == 0 && metadata == NULL) {
856 ALOGE("Null data returned in data callback");
857 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
858 return;
859 }
860
861 switch (msgType & ~CAMERA_MSG_PREVIEW_METADATA) {
862 case CAMERA_MSG_PREVIEW_FRAME:
863 client->handlePreviewData(msgType, dataPtr, metadata);
864 break;
865 case CAMERA_MSG_POSTVIEW_FRAME:
866 client->handlePostview(dataPtr);
867 break;
868 case CAMERA_MSG_RAW_IMAGE:
869 client->handleRawPicture(dataPtr);
870 break;
871 case CAMERA_MSG_COMPRESSED_IMAGE:
872 client->handleCompressedPicture(dataPtr);
873 break;
874 default:
875 client->handleGenericData(msgType, dataPtr, metadata);
876 break;
877 }
878 }
879
dataCallbackTimestamp(nsecs_t timestamp,int32_t msgType,const sp<IMemory> & dataPtr,void * user)880 void CameraClient::dataCallbackTimestamp(nsecs_t timestamp,
881 int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
882 LOG2("dataCallbackTimestamp(%d)", msgType);
883
884 sp<CameraClient> client = getClientFromCookie(user);
885 if (client.get() == nullptr) return;
886
887 if (!client->lockIfMessageWanted(msgType)) return;
888
889 if (dataPtr == 0) {
890 ALOGE("Null data returned in data with timestamp callback");
891 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
892 return;
893 }
894
895 client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
896 }
897
handleCallbackTimestampBatch(int32_t msgType,const std::vector<HandleTimestampMessage> & msgs,void * user)898 void CameraClient::handleCallbackTimestampBatch(
899 int32_t msgType, const std::vector<HandleTimestampMessage>& msgs, void* user) {
900 LOG2("dataCallbackTimestampBatch");
901 sp<CameraClient> client = getClientFromCookie(user);
902 if (client.get() == nullptr) return;
903 if (!client->lockIfMessageWanted(msgType)) return;
904
905 sp<hardware::ICameraClient> c = client->mRemoteCallback;
906 client->mLock.unlock();
907 if (c != 0 && msgs.size() > 0) {
908 size_t n = msgs.size();
909 std::vector<nsecs_t> timestamps;
910 std::vector<native_handle_t*> handles;
911 timestamps.reserve(n);
912 handles.reserve(n);
913 for (auto& msg : msgs) {
914 native_handle_t* handle = nullptr;
915 if (msg.dataPtr->size() != sizeof(VideoNativeHandleMetadata)) {
916 ALOGE("%s: dataPtr does not contain VideoNativeHandleMetadata!", __FUNCTION__);
917 return;
918 }
919 VideoNativeHandleMetadata *metadata =
920 (VideoNativeHandleMetadata*)(msg.dataPtr->pointer());
921 if (metadata->eType == kMetadataBufferTypeNativeHandleSource) {
922 handle = metadata->pHandle;
923 }
924
925 if (handle == nullptr) {
926 ALOGE("%s: VideoNativeHandleMetadata type mismatch or null handle passed!",
927 __FUNCTION__);
928 return;
929 }
930 {
931 Mutex::Autolock l(client->mAvailableCallbackBuffersLock);
932 client->mAvailableCallbackBuffers.push_back(msg.dataPtr);
933 }
934 timestamps.push_back(msg.timestamp);
935 handles.push_back(handle);
936 }
937 c->recordingFrameHandleCallbackTimestampBatch(timestamps, handles);
938 }
939 }
940
941 // snapshot taken callback
handleShutter(void)942 void CameraClient::handleShutter(void) {
943 if (mPlayShutterSound) {
944 sCameraService->playSound(CameraService::SOUND_SHUTTER);
945 }
946
947 sp<hardware::ICameraClient> c = mRemoteCallback;
948 if (c != 0) {
949 mLock.unlock();
950 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
951 if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
952 }
953 disableMsgType(CAMERA_MSG_SHUTTER);
954
955 // Shutters only happen in response to takePicture, so mark device as
956 // idle now, until preview is restarted
957 sCameraService->updateProxyDeviceState(
958 hardware::ICameraServiceProxy::CAMERA_STATE_IDLE,
959 mCameraIdStr, mCameraFacing, mClientPackageName,
960 hardware::ICameraServiceProxy::CAMERA_API_LEVEL_1);
961
962 mLock.unlock();
963 }
964
965 // preview callback - frame buffer update
handlePreviewData(int32_t msgType,const sp<IMemory> & mem,camera_frame_metadata_t * metadata)966 void CameraClient::handlePreviewData(int32_t msgType,
967 const sp<IMemory>& mem,
968 camera_frame_metadata_t *metadata) {
969 ssize_t offset;
970 size_t size;
971 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
972
973 // local copy of the callback flags
974 int flags = mPreviewCallbackFlag;
975
976 // is callback enabled?
977 if (!(flags & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
978 // If the enable bit is off, the copy-out and one-shot bits are ignored
979 LOG2("frame callback is disabled");
980 mLock.unlock();
981 return;
982 }
983
984 // hold a strong pointer to the client
985 sp<hardware::ICameraClient> c = mRemoteCallback;
986
987 // clear callback flags if no client or one-shot mode
988 if (c == 0 || (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
989 LOG2("Disable preview callback");
990 mPreviewCallbackFlag &= ~(CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
991 CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
992 CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK);
993 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
994 }
995
996 if (c != 0) {
997 // Is the received frame copied out or not?
998 if (flags & CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
999 LOG2("frame is copied");
1000 copyFrameAndPostCopiedFrame(msgType, c, heap, offset, size, metadata);
1001 } else {
1002 LOG2("frame is forwarded");
1003 mLock.unlock();
1004 c->dataCallback(msgType, mem, metadata);
1005 }
1006 } else {
1007 mLock.unlock();
1008 }
1009 }
1010
1011 // picture callback - postview image ready
handlePostview(const sp<IMemory> & mem)1012 void CameraClient::handlePostview(const sp<IMemory>& mem) {
1013 disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
1014
1015 sp<hardware::ICameraClient> c = mRemoteCallback;
1016 mLock.unlock();
1017 if (c != 0) {
1018 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem, NULL);
1019 }
1020 }
1021
1022 // picture callback - raw image ready
handleRawPicture(const sp<IMemory> & mem)1023 void CameraClient::handleRawPicture(const sp<IMemory>& mem) {
1024 disableMsgType(CAMERA_MSG_RAW_IMAGE);
1025
1026 ssize_t offset;
1027 size_t size;
1028 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1029
1030 sp<hardware::ICameraClient> c = mRemoteCallback;
1031 mLock.unlock();
1032 if (c != 0) {
1033 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem, NULL);
1034 }
1035 }
1036
1037 // picture callback - compressed picture ready
handleCompressedPicture(const sp<IMemory> & mem)1038 void CameraClient::handleCompressedPicture(const sp<IMemory>& mem) {
1039 disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
1040
1041 sp<hardware::ICameraClient> c = mRemoteCallback;
1042 mLock.unlock();
1043 if (c != 0) {
1044 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem, NULL);
1045 }
1046 }
1047
1048
handleGenericNotify(int32_t msgType,int32_t ext1,int32_t ext2)1049 void CameraClient::handleGenericNotify(int32_t msgType,
1050 int32_t ext1, int32_t ext2) {
1051 sp<hardware::ICameraClient> c = mRemoteCallback;
1052 mLock.unlock();
1053 if (c != 0) {
1054 c->notifyCallback(msgType, ext1, ext2);
1055 }
1056 }
1057
handleGenericData(int32_t msgType,const sp<IMemory> & dataPtr,camera_frame_metadata_t * metadata)1058 void CameraClient::handleGenericData(int32_t msgType,
1059 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata) {
1060 sp<hardware::ICameraClient> c = mRemoteCallback;
1061 mLock.unlock();
1062 if (c != 0) {
1063 c->dataCallback(msgType, dataPtr, metadata);
1064 }
1065 }
1066
handleGenericDataTimestamp(nsecs_t timestamp,int32_t msgType,const sp<IMemory> & dataPtr)1067 void CameraClient::handleGenericDataTimestamp(nsecs_t timestamp,
1068 int32_t msgType, const sp<IMemory>& dataPtr) {
1069 sp<hardware::ICameraClient> c = mRemoteCallback;
1070 mLock.unlock();
1071 if (c != 0 && dataPtr != nullptr) {
1072 native_handle_t* handle = nullptr;
1073
1074 // Check if dataPtr contains a VideoNativeHandleMetadata.
1075 if (dataPtr->size() == sizeof(VideoNativeHandleMetadata)) {
1076 VideoNativeHandleMetadata *metadata =
1077 (VideoNativeHandleMetadata*)(dataPtr->pointer());
1078 if (metadata->eType == kMetadataBufferTypeNativeHandleSource) {
1079 handle = metadata->pHandle;
1080 }
1081 }
1082
1083 // If dataPtr contains a native handle, send it via recordingFrameHandleCallbackTimestamp.
1084 if (handle != nullptr) {
1085 {
1086 Mutex::Autolock l(mAvailableCallbackBuffersLock);
1087 mAvailableCallbackBuffers.push_back(dataPtr);
1088 }
1089 c->recordingFrameHandleCallbackTimestamp(timestamp, handle);
1090 } else {
1091 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
1092 }
1093 }
1094 }
1095
copyFrameAndPostCopiedFrame(int32_t msgType,const sp<hardware::ICameraClient> & client,const sp<IMemoryHeap> & heap,size_t offset,size_t size,camera_frame_metadata_t * metadata)1096 void CameraClient::copyFrameAndPostCopiedFrame(
1097 int32_t msgType, const sp<hardware::ICameraClient>& client,
1098 const sp<IMemoryHeap>& heap, size_t offset, size_t size,
1099 camera_frame_metadata_t *metadata) {
1100 LOG2("copyFrameAndPostCopiedFrame");
1101 // It is necessary to copy out of pmem before sending this to
1102 // the callback. For efficiency, reuse the same MemoryHeapBase
1103 // provided it's big enough. Don't allocate the memory or
1104 // perform the copy if there's no callback.
1105 // hold the preview lock while we grab a reference to the preview buffer
1106 sp<MemoryHeapBase> previewBuffer;
1107
1108 if (mPreviewBuffer == 0) {
1109 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1110 } else if (size > mPreviewBuffer->virtualSize()) {
1111 mPreviewBuffer.clear();
1112 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1113 }
1114 if (mPreviewBuffer == 0) {
1115 ALOGE("failed to allocate space for preview buffer");
1116 mLock.unlock();
1117 return;
1118 }
1119 previewBuffer = mPreviewBuffer;
1120
1121 void* previewBufferBase = previewBuffer->base();
1122 void* heapBase = heap->base();
1123
1124 if (heapBase == MAP_FAILED) {
1125 ALOGE("%s: Failed to mmap heap for preview frame.", __FUNCTION__);
1126 mLock.unlock();
1127 return;
1128 } else if (previewBufferBase == MAP_FAILED) {
1129 ALOGE("%s: Failed to mmap preview buffer for preview frame.", __FUNCTION__);
1130 mLock.unlock();
1131 return;
1132 }
1133
1134 memcpy(previewBufferBase, (uint8_t *) heapBase + offset, size);
1135
1136 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
1137 if (frame == 0) {
1138 ALOGE("failed to allocate space for frame callback");
1139 mLock.unlock();
1140 return;
1141 }
1142
1143 mLock.unlock();
1144 client->dataCallback(msgType, frame, metadata);
1145 }
1146
getOrientation(int degrees,bool mirror)1147 int CameraClient::getOrientation(int degrees, bool mirror) {
1148 if (!mirror) {
1149 if (degrees == 0) return 0;
1150 else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
1151 else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
1152 else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
1153 } else { // Do mirror (horizontal flip)
1154 if (degrees == 0) { // FLIP_H and ROT_0
1155 return HAL_TRANSFORM_FLIP_H;
1156 } else if (degrees == 90) { // FLIP_H and ROT_90
1157 return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
1158 } else if (degrees == 180) { // FLIP_H and ROT_180
1159 return HAL_TRANSFORM_FLIP_V;
1160 } else if (degrees == 270) { // FLIP_H and ROT_270
1161 return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
1162 }
1163 }
1164 ALOGE("Invalid setDisplayOrientation degrees=%d", degrees);
1165 return -1;
1166 }
1167
setVideoTarget(const sp<IGraphicBufferProducer> & bufferProducer)1168 status_t CameraClient::setVideoTarget(const sp<IGraphicBufferProducer>& bufferProducer) {
1169 (void)bufferProducer;
1170 ALOGE("%s: %d: CameraClient doesn't support setting a video target.", __FUNCTION__, __LINE__);
1171 return INVALID_OPERATION;
1172 }
1173
1174 }; // namespace android
1175