blob: d557794be6be83b6d38b962571a8ffe9091101e1 [file] [log] [blame]
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001/*
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/properties.h>
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070021#include <gui/Surface.h>
22
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070023#include "api1/CameraClient.h"
24#include "device1/CameraHardwareInterface.h"
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070025#include "CameraService.h"
26
27namespace android {
28
29#define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
30#define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
31
32static int getCallingPid() {
33 return IPCThreadState::self()->getCallingPid();
34}
35
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070036CameraClient::CameraClient(const sp<CameraService>& cameraService,
37 const sp<ICameraClient>& cameraClient,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080038 const String16& clientPackageName,
39 int cameraId, int cameraFacing,
40 int clientPid, int clientUid,
41 int servicePid):
42 Client(cameraService, cameraClient, clientPackageName,
43 cameraId, cameraFacing, clientPid, clientUid, servicePid)
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070044{
45 int callingPid = getCallingPid();
46 LOG1("CameraClient::CameraClient E (pid %d, id %d)", callingPid, cameraId);
47
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070048 mHardware = NULL;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070049 mMsgEnabled = 0;
50 mSurface = 0;
51 mPreviewWindow = 0;
52 mDestructionStarted = false;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070053
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
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070061status_t CameraClient::initialize(camera_module_t *module) {
62 int callingPid = getCallingPid();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080063 status_t res;
64
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070065 LOG1("CameraClient::initialize E (pid %d, id %d)", callingPid, mCameraId);
66
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080067 // Verify ops permissions
68 res = startCameraOps();
69 if (res != OK) {
70 return res;
71 }
72
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070073 char camera_device_name[10];
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070074 snprintf(camera_device_name, sizeof(camera_device_name), "%d", mCameraId);
75
76 mHardware = new CameraHardwareInterface(camera_device_name);
77 res = mHardware->initialize(&module->common);
78 if (res != OK) {
79 ALOGE("%s: Camera %d: unable to initialize device: %s (%d)",
80 __FUNCTION__, mCameraId, strerror(-res), res);
Igor Murashkin44f120f2012-10-09 14:45:37 -070081 mHardware.clear();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070082 return NO_INIT;
83 }
84
85 mHardware->setCallbacks(notifyCallback,
86 dataCallback,
87 dataCallbackTimestamp,
88 (void *)mCameraId);
89
90 // Enable zoom, error, focus, and metadata messages by default
Apurva Rajguru96db5f22012-07-12 11:34:12 -070091 enableMsgType(CAMERA_MSG_ERROR | CAMERA_MSG_ZOOM | CAMERA_MSG_FOCUS
Apurva Rajguru96db5f22012-07-12 11:34:12 -070092 | CAMERA_MSG_PREVIEW_METADATA
x01180641c960842011-09-26 19:03:36 -050093#ifndef OMAP_ICS_CAMERA
94 | CAMERA_MSG_FOCUS_MOVE
95#endif
96 );
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070097
98 LOG1("CameraClient::initialize X (pid %d, id %d)", callingPid, mCameraId);
99 return OK;
100}
101
102
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700103// tear down the client
104CameraClient::~CameraClient() {
105 // this lock should never be NULL
106 Mutex* lock = mCameraService->getClientLockById(mCameraId);
107 lock->lock();
108 mDestructionStarted = true;
109 // client will not be accessed from callback. should unlock to prevent dead-lock in disconnect
110 lock->unlock();
111 int callingPid = getCallingPid();
112 LOG1("CameraClient::~CameraClient E (pid %d, this %p)", callingPid, this);
113
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700114 disconnect();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700115 LOG1("CameraClient::~CameraClient X (pid %d, this %p)", callingPid, this);
116}
117
118status_t CameraClient::dump(int fd, const Vector<String16>& args) {
119 const size_t SIZE = 256;
120 char buffer[SIZE];
121
122 size_t len = snprintf(buffer, SIZE, "Client[%d] (%p) PID: %d\n",
123 mCameraId,
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800124 getRemoteCallback()->asBinder().get(),
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700125 mClientPid);
126 len = (len > SIZE - 1) ? SIZE - 1 : len;
127 write(fd, buffer, len);
128 return mHardware->dump(fd, args);
129}
130
131// ----------------------------------------------------------------------------
132
133status_t CameraClient::checkPid() const {
134 int callingPid = getCallingPid();
Eino-Ville Talvalac0379202012-10-09 22:16:58 -0700135 if (callingPid == mClientPid) return NO_ERROR;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700136
137 ALOGW("attempt to use a locked camera from a different process"
138 " (old pid %d, new pid %d)", mClientPid, callingPid);
139 return EBUSY;
140}
141
142status_t CameraClient::checkPidAndHardware() const {
143 status_t result = checkPid();
144 if (result != NO_ERROR) return result;
145 if (mHardware == 0) {
146 ALOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid());
147 return INVALID_OPERATION;
148 }
149 return NO_ERROR;
150}
151
152status_t CameraClient::lock() {
153 int callingPid = getCallingPid();
154 LOG1("lock (pid %d)", callingPid);
155 Mutex::Autolock lock(mLock);
156
157 // lock camera to this client if the the camera is unlocked
158 if (mClientPid == 0) {
159 mClientPid = callingPid;
160 return NO_ERROR;
161 }
162
163 // returns NO_ERROR if the client already owns the camera, EBUSY otherwise
164 return checkPid();
165}
166
167status_t CameraClient::unlock() {
168 int callingPid = getCallingPid();
169 LOG1("unlock (pid %d)", callingPid);
170 Mutex::Autolock lock(mLock);
171
172 // allow anyone to use camera (after they lock the camera)
173 status_t result = checkPid();
174 if (result == NO_ERROR) {
175 if (mHardware->recordingEnabled()) {
176 ALOGE("Not allowed to unlock camera during recording.");
177 return INVALID_OPERATION;
178 }
179 mClientPid = 0;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800180 LOG1("clear mRemoteCallback (pid %d)", callingPid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700181 // we need to remove the reference to ICameraClient so that when the app
182 // goes away, the reference count goes to 0.
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800183 mRemoteCallback.clear();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700184 }
185 return result;
186}
187
188// connect a new client to the camera
189status_t CameraClient::connect(const sp<ICameraClient>& client) {
190 int callingPid = getCallingPid();
191 LOG1("connect E (pid %d)", callingPid);
192 Mutex::Autolock lock(mLock);
193
194 if (mClientPid != 0 && checkPid() != NO_ERROR) {
195 ALOGW("Tried to connect to a locked camera (old pid %d, new pid %d)",
196 mClientPid, callingPid);
197 return EBUSY;
198 }
199
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800200 if (mRemoteCallback != 0 &&
201 (client->asBinder() == mRemoteCallback->asBinder())) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700202 LOG1("Connect to the same client");
203 return NO_ERROR;
204 }
205
206 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
207 mClientPid = callingPid;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800208 mRemoteCallback = client;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700209
210 LOG1("connect X (pid %d)", callingPid);
211 return NO_ERROR;
212}
213
214static void disconnectWindow(const sp<ANativeWindow>& window) {
215 if (window != 0) {
216 status_t result = native_window_api_disconnect(window.get(),
217 NATIVE_WINDOW_API_CAMERA);
218 if (result != NO_ERROR) {
219 ALOGW("native_window_api_disconnect failed: %s (%d)", strerror(-result),
220 result);
221 }
222 }
223}
224
225void CameraClient::disconnect() {
226 int callingPid = getCallingPid();
227 LOG1("disconnect E (pid %d)", callingPid);
228 Mutex::Autolock lock(mLock);
229
Eino-Ville Talvalac0379202012-10-09 22:16:58 -0700230 // Allow both client and the media server to disconnect at all times
231 if (callingPid != mClientPid && callingPid != mServicePid) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700232 ALOGW("different client - don't disconnect");
233 return;
234 }
235
236 if (mClientPid <= 0) {
237 LOG1("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
238 return;
239 }
240
241 // Make sure disconnect() is done once and once only, whether it is called
242 // from the user directly, or called by the destructor.
243 if (mHardware == 0) return;
244
245 LOG1("hardware teardown");
246 // Before destroying mHardware, we must make sure it's in the
247 // idle state.
248 // Turn off all messages.
249 disableMsgType(CAMERA_MSG_ALL_MSGS);
250 mHardware->stopPreview();
251 mHardware->cancelPicture();
252 // Release the hardware resources.
253 mHardware->release();
254
255 // Release the held ANativeWindow resources.
256 if (mPreviewWindow != 0) {
257 disconnectWindow(mPreviewWindow);
258 mPreviewWindow = 0;
259 mHardware->setPreviewWindow(mPreviewWindow);
260 }
261 mHardware.clear();
262
263 CameraService::Client::disconnect();
264
265 LOG1("disconnect X (pid %d)", callingPid);
266}
267
268// ----------------------------------------------------------------------------
269
270status_t CameraClient::setPreviewWindow(const sp<IBinder>& binder,
271 const sp<ANativeWindow>& window) {
272 Mutex::Autolock lock(mLock);
273 status_t result = checkPidAndHardware();
274 if (result != NO_ERROR) return result;
275
276 // return if no change in surface.
277 if (binder == mSurface) {
278 return NO_ERROR;
279 }
280
281 if (window != 0) {
282 result = native_window_api_connect(window.get(), NATIVE_WINDOW_API_CAMERA);
283 if (result != NO_ERROR) {
284 ALOGE("native_window_api_connect failed: %s (%d)", strerror(-result),
285 result);
286 return result;
287 }
288 }
289
290 // If preview has been already started, register preview buffers now.
291 if (mHardware->previewEnabled()) {
292 if (window != 0) {
293 native_window_set_scaling_mode(window.get(),
294 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
295 native_window_set_buffers_transform(window.get(), mOrientation);
296 result = mHardware->setPreviewWindow(window);
297 }
298 }
299
300 if (result == NO_ERROR) {
301 // Everything has succeeded. Disconnect the old window and remember the
302 // new window.
303 disconnectWindow(mPreviewWindow);
304 mSurface = binder;
305 mPreviewWindow = window;
306 } else {
307 // Something went wrong after we connected to the new window, so
308 // disconnect here.
309 disconnectWindow(window);
310 }
311
312 return result;
313}
314
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700315// set the buffer consumer that the preview will use
316status_t CameraClient::setPreviewTarget(
Andy McFadden8ba01022012-12-18 09:46:54 -0800317 const sp<IGraphicBufferProducer>& bufferProducer) {
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700318 LOG1("setPreviewTarget(%p) (pid %d)", bufferProducer.get(),
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700319 getCallingPid());
320
321 sp<IBinder> binder;
322 sp<ANativeWindow> window;
Andy McFadden8ba01022012-12-18 09:46:54 -0800323 if (bufferProducer != 0) {
324 binder = bufferProducer->asBinder();
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700325 // Using controlledByApp flag to ensure that the buffer queue remains in
326 // async mode for the old camera API, where many applications depend
327 // on that behavior.
328 window = new Surface(bufferProducer, /*controlledByApp*/ true);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700329 }
330 return setPreviewWindow(binder, window);
331}
332
333// set the preview callback flag to affect how the received frames from
334// preview are handled.
335void CameraClient::setPreviewCallbackFlag(int callback_flag) {
336 LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid());
337 Mutex::Autolock lock(mLock);
338 if (checkPidAndHardware() != NO_ERROR) return;
339
340 mPreviewCallbackFlag = callback_flag;
341 if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
342 enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
343 } else {
344 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
345 }
346}
347
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700348status_t CameraClient::setPreviewCallbackTarget(
349 const sp<IGraphicBufferProducer>& callbackProducer) {
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700350 (void)callbackProducer;
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700351 ALOGE("%s: Unimplemented!", __FUNCTION__);
352 return INVALID_OPERATION;
353}
354
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700355// start preview mode
356status_t CameraClient::startPreview() {
357 LOG1("startPreview (pid %d)", getCallingPid());
358 return startCameraMode(CAMERA_PREVIEW_MODE);
359}
360
361// start recording mode
362status_t CameraClient::startRecording() {
363 LOG1("startRecording (pid %d)", getCallingPid());
364 return startCameraMode(CAMERA_RECORDING_MODE);
365}
366
367// start preview or recording
368status_t CameraClient::startCameraMode(camera_mode mode) {
369 LOG1("startCameraMode(%d)", mode);
370 Mutex::Autolock lock(mLock);
371 status_t result = checkPidAndHardware();
372 if (result != NO_ERROR) return result;
373
374 switch(mode) {
375 case CAMERA_PREVIEW_MODE:
376 if (mSurface == 0 && mPreviewWindow == 0) {
377 LOG1("mSurface is not set yet.");
378 // still able to start preview in this case.
379 }
380 return startPreviewMode();
381 case CAMERA_RECORDING_MODE:
382 if (mSurface == 0 && mPreviewWindow == 0) {
383 ALOGE("mSurface or mPreviewWindow must be set before startRecordingMode.");
384 return INVALID_OPERATION;
385 }
386 return startRecordingMode();
387 default:
388 return UNKNOWN_ERROR;
389 }
390}
391
392status_t CameraClient::startPreviewMode() {
393 LOG1("startPreviewMode");
394 status_t result = NO_ERROR;
395
396 // if preview has been enabled, nothing needs to be done
397 if (mHardware->previewEnabled()) {
398 return NO_ERROR;
399 }
400
401 if (mPreviewWindow != 0) {
402 native_window_set_scaling_mode(mPreviewWindow.get(),
403 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
404 native_window_set_buffers_transform(mPreviewWindow.get(),
405 mOrientation);
406 }
x01180641c960842011-09-26 19:03:36 -0500407
408#if defined(OMAP_ICS_CAMERA) || defined(OMAP_ENHANCEMENT_BURST_CAPTURE)
409 disableMsgType(CAMERA_MSG_COMPRESSED_BURST_IMAGE);
410#endif
411
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700412 mHardware->setPreviewWindow(mPreviewWindow);
413 result = mHardware->startPreview();
414
415 return result;
416}
417
418status_t CameraClient::startRecordingMode() {
419 LOG1("startRecordingMode");
420 status_t result = NO_ERROR;
421
422 // if recording has been enabled, nothing needs to be done
423 if (mHardware->recordingEnabled()) {
424 return NO_ERROR;
425 }
426
427 // if preview has not been started, start preview first
428 if (!mHardware->previewEnabled()) {
429 result = startPreviewMode();
430 if (result != NO_ERROR) {
431 return result;
432 }
433 }
434
435 // start recording mode
436 enableMsgType(CAMERA_MSG_VIDEO_FRAME);
437 mCameraService->playSound(CameraService::SOUND_RECORDING);
438 result = mHardware->startRecording();
439 if (result != NO_ERROR) {
440 ALOGE("mHardware->startRecording() failed with status %d", result);
441 }
442 return result;
443}
444
445// stop preview mode
446void CameraClient::stopPreview() {
447 LOG1("stopPreview (pid %d)", getCallingPid());
448 Mutex::Autolock lock(mLock);
449 if (checkPidAndHardware() != NO_ERROR) return;
450
x01180641c960842011-09-26 19:03:36 -0500451#ifdef OMAP_ENHANCEMENT
452 // According to framework documentation, preview needs
453 // to be started for image capture. This will make sure
454 // that image capture related messages get disabled if
455 // not done already in their respective handlers.
456 // If these messages come when in the midddle of
457 // stopping preview we will deadlock the system in
458 // lockIfMessageWanted().
459 disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
460#endif
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700461
462 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
Alok Kediya53fb8702013-08-11 14:47:14 +0530463#ifdef QCOM_HARDWARE
464 //Disable picture related message types
465 ALOGI("stopPreview: Disable picture related messages");
466 int picMsgType = 0;
467 picMsgType = (CAMERA_MSG_SHUTTER |
468 CAMERA_MSG_POSTVIEW_FRAME |
469 CAMERA_MSG_RAW_IMAGE |
470 CAMERA_MSG_RAW_IMAGE_NOTIFY |
471 CAMERA_MSG_COMPRESSED_IMAGE);
472 disableMsgType(picMsgType);
473#endif
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700474 mHardware->stopPreview();
475
476 mPreviewBuffer.clear();
477}
478
479// stop recording mode
480void CameraClient::stopRecording() {
481 LOG1("stopRecording (pid %d)", getCallingPid());
482 Mutex::Autolock lock(mLock);
483 if (checkPidAndHardware() != NO_ERROR) return;
484
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700485 disableMsgType(CAMERA_MSG_VIDEO_FRAME);
Alok Kediya53fb8702013-08-11 14:47:14 +0530486#ifdef QCOM_HARDWARE
487 //Disable picture related message types
488 ALOGI("stopRecording: Disable picture related messages");
489 int picMsgType = 0;
490 picMsgType = (CAMERA_MSG_SHUTTER |
491 CAMERA_MSG_POSTVIEW_FRAME |
492 CAMERA_MSG_RAW_IMAGE |
493 CAMERA_MSG_RAW_IMAGE_NOTIFY |
494 CAMERA_MSG_COMPRESSED_IMAGE);
495 disableMsgType(picMsgType);
496#endif
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700497 mHardware->stopRecording();
Patric Frederiksen35f859b2012-07-10 13:38:35 +0200498 mCameraService->playSound(CameraService::SOUND_RECORDING);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700499
500 mPreviewBuffer.clear();
501}
502
503// release a recording frame
504void CameraClient::releaseRecordingFrame(const sp<IMemory>& mem) {
505 Mutex::Autolock lock(mLock);
506 if (checkPidAndHardware() != NO_ERROR) return;
507 mHardware->releaseRecordingFrame(mem);
508}
509
510status_t CameraClient::storeMetaDataInBuffers(bool enabled)
511{
512 LOG1("storeMetaDataInBuffers: %s", enabled? "true": "false");
513 Mutex::Autolock lock(mLock);
514 if (checkPidAndHardware() != NO_ERROR) {
515 return UNKNOWN_ERROR;
516 }
517 return mHardware->storeMetaDataInBuffers(enabled);
518}
519
520bool CameraClient::previewEnabled() {
521 LOG1("previewEnabled (pid %d)", getCallingPid());
522
523 Mutex::Autolock lock(mLock);
524 if (checkPidAndHardware() != NO_ERROR) return false;
525 return mHardware->previewEnabled();
526}
527
528bool CameraClient::recordingEnabled() {
529 LOG1("recordingEnabled (pid %d)", getCallingPid());
530
531 Mutex::Autolock lock(mLock);
532 if (checkPidAndHardware() != NO_ERROR) return false;
533 return mHardware->recordingEnabled();
534}
535
536status_t CameraClient::autoFocus() {
537 LOG1("autoFocus (pid %d)", getCallingPid());
538
539 Mutex::Autolock lock(mLock);
540 status_t result = checkPidAndHardware();
541 if (result != NO_ERROR) return result;
542
543 return mHardware->autoFocus();
544}
545
546status_t CameraClient::cancelAutoFocus() {
547 LOG1("cancelAutoFocus (pid %d)", getCallingPid());
548
549 Mutex::Autolock lock(mLock);
550 status_t result = checkPidAndHardware();
551 if (result != NO_ERROR) return result;
552
553 return mHardware->cancelAutoFocus();
554}
555
556// take a picture - image is returned in callback
557status_t CameraClient::takePicture(int msgType) {
558 LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType);
559
560 Mutex::Autolock lock(mLock);
561 status_t result = checkPidAndHardware();
562 if (result != NO_ERROR) return result;
563
564 if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
565 (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
566 ALOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
567 " cannot be both enabled");
568 return BAD_VALUE;
569 }
570
571 // We only accept picture related message types
572 // and ignore other types of messages for takePicture().
573 int picMsgType = msgType
574 & (CAMERA_MSG_SHUTTER |
575 CAMERA_MSG_POSTVIEW_FRAME |
576 CAMERA_MSG_RAW_IMAGE |
577 CAMERA_MSG_RAW_IMAGE_NOTIFY |
x01180641c960842011-09-26 19:03:36 -0500578#if defined(OMAP_ICS_CAMERA) || defined(OMAP_ENHANCEMENT_BURST_CAPTURE)
579 CAMERA_MSG_RAW_BURST |
580#endif
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700581 CAMERA_MSG_COMPRESSED_IMAGE);
582
x01180641c960842011-09-26 19:03:36 -0500583#if defined(OMAP_ICS_CAMERA) || defined(OMAP_ENHANCEMENT_BURST_CAPTURE)
584 picMsgType |= CAMERA_MSG_COMPRESSED_BURST_IMAGE;
585#endif
586
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700587 enableMsgType(picMsgType);
Kiran Kumar H Nb589ead2013-06-27 21:32:54 +0530588#ifdef QCOM_HARDWARE
589 mBurstCnt = mHardware->getParameters().getInt("num-snaps-per-shutter");
590 if(mBurstCnt <= 0)
591 mBurstCnt = 1;
592 LOG1("mBurstCnt = %d", mBurstCnt);
593#endif
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700594
595 return mHardware->takePicture();
596}
597
598// set preview/capture parameters - key/value pairs
599status_t CameraClient::setParameters(const String8& params) {
600 LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
601
602 Mutex::Autolock lock(mLock);
603 status_t result = checkPidAndHardware();
604 if (result != NO_ERROR) return result;
605
606 CameraParameters p(params);
607 return mHardware->setParameters(p);
608}
609
610// get preview/capture parameters - key/value pairs
611String8 CameraClient::getParameters() const {
612 Mutex::Autolock lock(mLock);
613 if (checkPidAndHardware() != NO_ERROR) return String8();
614
615 String8 params(mHardware->getParameters().flatten());
616 LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
617 return params;
618}
619
620// enable shutter sound
621status_t CameraClient::enableShutterSound(bool enable) {
622 LOG1("enableShutterSound (pid %d)", getCallingPid());
623
624 status_t result = checkPidAndHardware();
625 if (result != NO_ERROR) return result;
626
627 if (enable) {
628 mPlayShutterSound = true;
629 return OK;
630 }
631
632 // Disabling shutter sound may not be allowed. In that case only
633 // allow the mediaserver process to disable the sound.
634 char value[PROPERTY_VALUE_MAX];
635 property_get("ro.camera.sound.forced", value, "0");
636 if (strcmp(value, "0") != 0) {
637 // Disabling shutter sound is not allowed. Deny if the current
638 // process is not mediaserver.
639 if (getCallingPid() != getpid()) {
640 ALOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid());
641 return PERMISSION_DENIED;
642 }
643 }
644
645 mPlayShutterSound = false;
646 return OK;
647}
648
649status_t CameraClient::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
650 LOG1("sendCommand (pid %d)", getCallingPid());
651 int orientation;
652 Mutex::Autolock lock(mLock);
653 status_t result = checkPidAndHardware();
654 if (result != NO_ERROR) return result;
655
656 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
657 // Mirror the preview if the camera is front-facing.
658 orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT);
659 if (orientation == -1) return BAD_VALUE;
660
661 if (mOrientation != orientation) {
662 mOrientation = orientation;
663 if (mPreviewWindow != 0) {
664 native_window_set_buffers_transform(mPreviewWindow.get(),
665 mOrientation);
666 }
667 }
668 return OK;
669 } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) {
670 switch (arg1) {
671 case 0:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700672 return enableShutterSound(false);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700673 case 1:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700674 return enableShutterSound(true);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700675 default:
676 return BAD_VALUE;
677 }
678 return OK;
679 } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) {
680 mCameraService->playSound(CameraService::SOUND_RECORDING);
James Dong983cf232012-08-01 16:39:55 -0700681 } else if (cmd == CAMERA_CMD_SET_VIDEO_BUFFER_COUNT) {
682 // Silently ignore this command
683 return INVALID_OPERATION;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700684 } else if (cmd == CAMERA_CMD_PING) {
685 // If mHardware is 0, checkPidAndHardware will return error.
686 return OK;
Apurva Rajguru3ba19952012-12-21 17:49:40 -0800687#ifdef QCOM_HARDWARE
688 } else if (cmd == CAMERA_CMD_HISTOGRAM_ON) {
689 enableMsgType(CAMERA_MSG_STATS_DATA);
690 } else if (cmd == CAMERA_CMD_HISTOGRAM_OFF) {
691 disableMsgType(CAMERA_MSG_STATS_DATA);
Emilian Peev02965372013-04-18 14:46:13 +0300692 } else if (cmd == CAMERA_CMD_METADATA_ON) {
693 enableMsgType(CAMERA_MSG_META_DATA);
694 } else if (cmd == CAMERA_CMD_METADATA_OFF) {
695 disableMsgType(CAMERA_MSG_META_DATA);
Emilian Peev5018d392013-08-14 17:03:36 +0300696 } else if ( cmd == CAMERA_CMD_LONGSHOT_ON ) {
697 mLongshotEnabled = true;
698 } else if ( cmd == CAMERA_CMD_LONGSHOT_OFF ) {
699 mLongshotEnabled = false;
700 disableMsgType(CAMERA_MSG_SHUTTER);
701 disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
Apurva Rajguru3ba19952012-12-21 17:49:40 -0800702#endif
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700703 }
704
705 return mHardware->sendCommand(cmd, arg1, arg2);
706}
707
708// ----------------------------------------------------------------------------
709
710void CameraClient::enableMsgType(int32_t msgType) {
711 android_atomic_or(msgType, &mMsgEnabled);
712 mHardware->enableMsgType(msgType);
713}
714
715void CameraClient::disableMsgType(int32_t msgType) {
716 android_atomic_and(~msgType, &mMsgEnabled);
717 mHardware->disableMsgType(msgType);
718}
719
720#define CHECK_MESSAGE_INTERVAL 10 // 10ms
721bool CameraClient::lockIfMessageWanted(int32_t msgType) {
722 int sleepCount = 0;
723 while (mMsgEnabled & msgType) {
Alok Kediyaa9b5ad62013-08-11 14:20:01 +0530724#ifdef QCOM_HARDWARE
725 if ((msgType == CAMERA_MSG_PREVIEW_FRAME) &&
726 (mMsgEnabled & CAMERA_MSG_COMPRESSED_IMAGE)) {
727 LOG1("lockIfMessageWanted(%d): Don't try to acquire mlock if "
728 "both Preview and Compressed are enabled", msgType);
729 return false;
730 }
731#endif
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700732 if (mLock.tryLock() == NO_ERROR) {
733 if (sleepCount > 0) {
734 LOG1("lockIfMessageWanted(%d): waited for %d ms",
735 msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
736 }
737 return true;
738 }
739 if (sleepCount++ == 0) {
740 LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
741 }
742 usleep(CHECK_MESSAGE_INTERVAL * 1000);
743 }
744 ALOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
745 return false;
746}
747
748// Callback messages can be dispatched to internal handlers or pass to our
749// client's callback functions, depending on the message type.
750//
751// notifyCallback:
752// CAMERA_MSG_SHUTTER handleShutter
753// (others) c->notifyCallback
754// dataCallback:
755// CAMERA_MSG_PREVIEW_FRAME handlePreviewData
756// CAMERA_MSG_POSTVIEW_FRAME handlePostview
757// CAMERA_MSG_RAW_IMAGE handleRawPicture
758// CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture
759// (others) c->dataCallback
760// dataCallbackTimestamp
761// (others) c->dataCallbackTimestamp
762//
763// NOTE: the *Callback functions grab mLock of the client before passing
764// control to handle* functions. So the handle* functions must release the
765// lock before calling the ICameraClient's callbacks, so those callbacks can
766// invoke methods in the Client class again (For example, the preview frame
767// callback may want to releaseRecordingFrame). The handle* functions must
768// release the lock after all accesses to member variables, so it must be
769// handled very carefully.
770
771void CameraClient::notifyCallback(int32_t msgType, int32_t ext1,
772 int32_t ext2, void* user) {
773 LOG2("notifyCallback(%d)", msgType);
774
Steve Kondik3a85aca2012-12-15 20:29:59 -0800775 // Ignore CAF_RESTART callbacks from Samsung's camera driver
776 if (msgType == CAMERA_MSG_FOCUS && ext1 == 4) {
777 LOG2("Ignore CAF_RESTART callback");
778 return;
779 }
780
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700781 Mutex* lock = getClientLockFromCookie(user);
782 if (lock == NULL) return;
783 Mutex::Autolock alock(*lock);
784
785 CameraClient* client =
786 static_cast<CameraClient*>(getClientFromCookie(user));
787 if (client == NULL) return;
788
789 if (!client->lockIfMessageWanted(msgType)) return;
790
791 switch (msgType) {
792 case CAMERA_MSG_SHUTTER:
793 // ext1 is the dimension of the yuv picture.
794 client->handleShutter();
795 break;
796 default:
797 client->handleGenericNotify(msgType, ext1, ext2);
798 break;
799 }
800}
801
802void CameraClient::dataCallback(int32_t msgType,
803 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) {
804 LOG2("dataCallback(%d)", msgType);
805
806 Mutex* lock = getClientLockFromCookie(user);
807 if (lock == NULL) return;
808 Mutex::Autolock alock(*lock);
809
810 CameraClient* client =
811 static_cast<CameraClient*>(getClientFromCookie(user));
812 if (client == NULL) return;
813
814 if (!client->lockIfMessageWanted(msgType)) return;
815 if (dataPtr == 0 && metadata == NULL) {
816 ALOGE("Null data returned in data callback");
817 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
818 return;
819 }
820
821 switch (msgType & ~CAMERA_MSG_PREVIEW_METADATA) {
822 case CAMERA_MSG_PREVIEW_FRAME:
823 client->handlePreviewData(msgType, dataPtr, metadata);
824 break;
825 case CAMERA_MSG_POSTVIEW_FRAME:
826 client->handlePostview(dataPtr);
827 break;
828 case CAMERA_MSG_RAW_IMAGE:
829 client->handleRawPicture(dataPtr);
830 break;
831 case CAMERA_MSG_COMPRESSED_IMAGE:
832 client->handleCompressedPicture(dataPtr);
833 break;
x01180641c960842011-09-26 19:03:36 -0500834#if defined(OMAP_ICS_CAMERA) || defined(OMAP_ENHANCEMENT_BURST_CAPTURE)
835 case CAMERA_MSG_COMPRESSED_BURST_IMAGE:
836 client->handleCompressedBurstPicture(dataPtr);
837 break;
838#endif
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700839 default:
840 client->handleGenericData(msgType, dataPtr, metadata);
841 break;
842 }
843}
844
845void CameraClient::dataCallbackTimestamp(nsecs_t timestamp,
846 int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
847 LOG2("dataCallbackTimestamp(%d)", msgType);
848
849 Mutex* lock = getClientLockFromCookie(user);
850 if (lock == NULL) return;
851 Mutex::Autolock alock(*lock);
852
853 CameraClient* client =
854 static_cast<CameraClient*>(getClientFromCookie(user));
855 if (client == NULL) return;
856
857 if (!client->lockIfMessageWanted(msgType)) return;
858
859 if (dataPtr == 0) {
860 ALOGE("Null data returned in data with timestamp callback");
861 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
862 return;
863 }
864
865 client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
866}
867
868// snapshot taken callback
869void CameraClient::handleShutter(void) {
870 if (mPlayShutterSound) {
871 mCameraService->playSound(CameraService::SOUND_SHUTTER);
872 }
873
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800874 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700875 if (c != 0) {
876 mLock.unlock();
877 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
878 if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
879 }
Emilian Peev5018d392013-08-14 17:03:36 +0300880 if ( !mLongshotEnabled ) {
881 disableMsgType(CAMERA_MSG_SHUTTER);
882 }
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700883
884 mLock.unlock();
885}
886
887// preview callback - frame buffer update
888void CameraClient::handlePreviewData(int32_t msgType,
889 const sp<IMemory>& mem,
890 camera_frame_metadata_t *metadata) {
891 ssize_t offset;
892 size_t size;
893 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
894
895 // local copy of the callback flags
896 int flags = mPreviewCallbackFlag;
897
898 // is callback enabled?
899 if (!(flags & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
900 // If the enable bit is off, the copy-out and one-shot bits are ignored
901 LOG2("frame callback is disabled");
902 mLock.unlock();
903 return;
904 }
905
906 // hold a strong pointer to the client
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800907 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700908
909 // clear callback flags if no client or one-shot mode
910 if (c == 0 || (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
911 LOG2("Disable preview callback");
912 mPreviewCallbackFlag &= ~(CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
913 CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
914 CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK);
915 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
916 }
917
918 if (c != 0) {
919 // Is the received frame copied out or not?
920 if (flags & CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
921 LOG2("frame is copied");
922 copyFrameAndPostCopiedFrame(msgType, c, heap, offset, size, metadata);
923 } else {
924 LOG2("frame is forwarded");
925 mLock.unlock();
926 c->dataCallback(msgType, mem, metadata);
927 }
928 } else {
929 mLock.unlock();
930 }
931}
932
933// picture callback - postview image ready
934void CameraClient::handlePostview(const sp<IMemory>& mem) {
935 disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
936
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800937 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700938 mLock.unlock();
939 if (c != 0) {
940 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem, NULL);
941 }
942}
943
944// picture callback - raw image ready
945void CameraClient::handleRawPicture(const sp<IMemory>& mem) {
946 disableMsgType(CAMERA_MSG_RAW_IMAGE);
947
948 ssize_t offset;
949 size_t size;
950 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
951
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800952 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700953 mLock.unlock();
954 if (c != 0) {
955 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem, NULL);
956 }
957}
958
959// picture callback - compressed picture ready
960void CameraClient::handleCompressedPicture(const sp<IMemory>& mem) {
Kiran Kumar H Nb589ead2013-06-27 21:32:54 +0530961#ifdef QCOM_HARDWARE
962 if (mBurstCnt)
963 mBurstCnt--;
964
Emilian Peev5018d392013-08-14 17:03:36 +0300965 if (!mBurstCnt && !mLongshotEnabled) {
Kiran Kumar H Nb589ead2013-06-27 21:32:54 +0530966 LOG1("handleCompressedPicture mBurstCnt = %d", mBurstCnt);
967#endif
968 disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
969#ifdef QCOM_HARDWARE
970 }
971#endif
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700972
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800973 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700974 mLock.unlock();
975 if (c != 0) {
976 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem, NULL);
977 }
978}
979
x01180641c960842011-09-26 19:03:36 -0500980#if defined(OMAP_ICS_CAMERA) || defined(OMAP_ENHANCEMENT_BURST_CAPTURE)
981// burst picture callback - compressed picture ready
982void CameraClient::handleCompressedBurstPicture(const sp<IMemory>& mem) {
983 // Don't disable this message type yet. In this mode takePicture() will
984 // get called only once. When burst finishes this message will get automatically
985 // disabled in the respective call for restarting the preview.
986
987 sp<ICameraClient> c = mCameraClient;
988 mLock.unlock();
989 if (c != 0) {
990 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem, NULL);
991 }
992}
993#endif
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700994
995void CameraClient::handleGenericNotify(int32_t msgType,
996 int32_t ext1, int32_t ext2) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800997 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700998 mLock.unlock();
999 if (c != 0) {
1000 c->notifyCallback(msgType, ext1, ext2);
1001 }
1002}
1003
1004void CameraClient::handleGenericData(int32_t msgType,
1005 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -08001006 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001007 mLock.unlock();
1008 if (c != 0) {
1009 c->dataCallback(msgType, dataPtr, metadata);
1010 }
1011}
1012
1013void CameraClient::handleGenericDataTimestamp(nsecs_t timestamp,
1014 int32_t msgType, const sp<IMemory>& dataPtr) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -08001015 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001016 mLock.unlock();
1017 if (c != 0) {
1018 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
1019 }
1020}
1021
1022void CameraClient::copyFrameAndPostCopiedFrame(
1023 int32_t msgType, const sp<ICameraClient>& client,
1024 const sp<IMemoryHeap>& heap, size_t offset, size_t size,
1025 camera_frame_metadata_t *metadata) {
1026 LOG2("copyFrameAndPostCopiedFrame");
1027 // It is necessary to copy out of pmem before sending this to
1028 // the callback. For efficiency, reuse the same MemoryHeapBase
1029 // provided it's big enough. Don't allocate the memory or
1030 // perform the copy if there's no callback.
1031 // hold the preview lock while we grab a reference to the preview buffer
1032 sp<MemoryHeapBase> previewBuffer;
1033
1034 if (mPreviewBuffer == 0) {
1035 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1036 } else if (size > mPreviewBuffer->virtualSize()) {
1037 mPreviewBuffer.clear();
1038 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1039 }
1040 if (mPreviewBuffer == 0) {
1041 ALOGE("failed to allocate space for preview buffer");
1042 mLock.unlock();
1043 return;
1044 }
1045 previewBuffer = mPreviewBuffer;
1046
1047 memcpy(previewBuffer->base(), (uint8_t *)heap->base() + offset, size);
1048
1049 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
1050 if (frame == 0) {
1051 ALOGE("failed to allocate space for frame callback");
1052 mLock.unlock();
1053 return;
1054 }
1055
1056 mLock.unlock();
1057 client->dataCallback(msgType, frame, metadata);
1058}
1059
1060int CameraClient::getOrientation(int degrees, bool mirror) {
1061 if (!mirror) {
1062 if (degrees == 0) return 0;
1063 else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
1064 else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
1065 else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
1066 } else { // Do mirror (horizontal flip)
1067 if (degrees == 0) { // FLIP_H and ROT_0
1068 return HAL_TRANSFORM_FLIP_H;
1069 } else if (degrees == 90) { // FLIP_H and ROT_90
1070 return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
1071 } else if (degrees == 180) { // FLIP_H and ROT_180
1072 return HAL_TRANSFORM_FLIP_V;
1073 } else if (degrees == 270) { // FLIP_H and ROT_270
1074 return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
1075 }
1076 }
1077 ALOGE("Invalid setDisplayOrientation degrees=%d", degrees);
1078 return -1;
1079}
1080
1081}; // namespace android