blob: 5ab99ba6539d32606d31d72f39aa46f862942b5c [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);
Apurva Rajguru3ba19952012-12-21 17:49:40 -0800696#endif
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700697 }
698
699 return mHardware->sendCommand(cmd, arg1, arg2);
700}
701
702// ----------------------------------------------------------------------------
703
704void CameraClient::enableMsgType(int32_t msgType) {
705 android_atomic_or(msgType, &mMsgEnabled);
706 mHardware->enableMsgType(msgType);
707}
708
709void CameraClient::disableMsgType(int32_t msgType) {
710 android_atomic_and(~msgType, &mMsgEnabled);
711 mHardware->disableMsgType(msgType);
712}
713
714#define CHECK_MESSAGE_INTERVAL 10 // 10ms
715bool CameraClient::lockIfMessageWanted(int32_t msgType) {
716 int sleepCount = 0;
717 while (mMsgEnabled & msgType) {
Alok Kediyaa9b5ad62013-08-11 14:20:01 +0530718#ifdef QCOM_HARDWARE
719 if ((msgType == CAMERA_MSG_PREVIEW_FRAME) &&
720 (mMsgEnabled & CAMERA_MSG_COMPRESSED_IMAGE)) {
721 LOG1("lockIfMessageWanted(%d): Don't try to acquire mlock if "
722 "both Preview and Compressed are enabled", msgType);
723 return false;
724 }
725#endif
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700726 if (mLock.tryLock() == NO_ERROR) {
727 if (sleepCount > 0) {
728 LOG1("lockIfMessageWanted(%d): waited for %d ms",
729 msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
730 }
731 return true;
732 }
733 if (sleepCount++ == 0) {
734 LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
735 }
736 usleep(CHECK_MESSAGE_INTERVAL * 1000);
737 }
738 ALOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
739 return false;
740}
741
742// Callback messages can be dispatched to internal handlers or pass to our
743// client's callback functions, depending on the message type.
744//
745// notifyCallback:
746// CAMERA_MSG_SHUTTER handleShutter
747// (others) c->notifyCallback
748// dataCallback:
749// CAMERA_MSG_PREVIEW_FRAME handlePreviewData
750// CAMERA_MSG_POSTVIEW_FRAME handlePostview
751// CAMERA_MSG_RAW_IMAGE handleRawPicture
752// CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture
753// (others) c->dataCallback
754// dataCallbackTimestamp
755// (others) c->dataCallbackTimestamp
756//
757// NOTE: the *Callback functions grab mLock of the client before passing
758// control to handle* functions. So the handle* functions must release the
759// lock before calling the ICameraClient's callbacks, so those callbacks can
760// invoke methods in the Client class again (For example, the preview frame
761// callback may want to releaseRecordingFrame). The handle* functions must
762// release the lock after all accesses to member variables, so it must be
763// handled very carefully.
764
765void CameraClient::notifyCallback(int32_t msgType, int32_t ext1,
766 int32_t ext2, void* user) {
767 LOG2("notifyCallback(%d)", msgType);
768
Steve Kondik3a85aca2012-12-15 20:29:59 -0800769 // Ignore CAF_RESTART callbacks from Samsung's camera driver
770 if (msgType == CAMERA_MSG_FOCUS && ext1 == 4) {
771 LOG2("Ignore CAF_RESTART callback");
772 return;
773 }
774
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700775 Mutex* lock = getClientLockFromCookie(user);
776 if (lock == NULL) return;
777 Mutex::Autolock alock(*lock);
778
779 CameraClient* client =
780 static_cast<CameraClient*>(getClientFromCookie(user));
781 if (client == NULL) return;
782
783 if (!client->lockIfMessageWanted(msgType)) return;
784
785 switch (msgType) {
786 case CAMERA_MSG_SHUTTER:
787 // ext1 is the dimension of the yuv picture.
788 client->handleShutter();
789 break;
790 default:
791 client->handleGenericNotify(msgType, ext1, ext2);
792 break;
793 }
794}
795
796void CameraClient::dataCallback(int32_t msgType,
797 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) {
798 LOG2("dataCallback(%d)", msgType);
799
800 Mutex* lock = getClientLockFromCookie(user);
801 if (lock == NULL) return;
802 Mutex::Autolock alock(*lock);
803
804 CameraClient* client =
805 static_cast<CameraClient*>(getClientFromCookie(user));
806 if (client == NULL) return;
807
808 if (!client->lockIfMessageWanted(msgType)) return;
809 if (dataPtr == 0 && metadata == NULL) {
810 ALOGE("Null data returned in data callback");
811 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
812 return;
813 }
814
815 switch (msgType & ~CAMERA_MSG_PREVIEW_METADATA) {
816 case CAMERA_MSG_PREVIEW_FRAME:
817 client->handlePreviewData(msgType, dataPtr, metadata);
818 break;
819 case CAMERA_MSG_POSTVIEW_FRAME:
820 client->handlePostview(dataPtr);
821 break;
822 case CAMERA_MSG_RAW_IMAGE:
823 client->handleRawPicture(dataPtr);
824 break;
825 case CAMERA_MSG_COMPRESSED_IMAGE:
826 client->handleCompressedPicture(dataPtr);
827 break;
x01180641c960842011-09-26 19:03:36 -0500828#if defined(OMAP_ICS_CAMERA) || defined(OMAP_ENHANCEMENT_BURST_CAPTURE)
829 case CAMERA_MSG_COMPRESSED_BURST_IMAGE:
830 client->handleCompressedBurstPicture(dataPtr);
831 break;
832#endif
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700833 default:
834 client->handleGenericData(msgType, dataPtr, metadata);
835 break;
836 }
837}
838
839void CameraClient::dataCallbackTimestamp(nsecs_t timestamp,
840 int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
841 LOG2("dataCallbackTimestamp(%d)", msgType);
842
843 Mutex* lock = getClientLockFromCookie(user);
844 if (lock == NULL) return;
845 Mutex::Autolock alock(*lock);
846
847 CameraClient* client =
848 static_cast<CameraClient*>(getClientFromCookie(user));
849 if (client == NULL) return;
850
851 if (!client->lockIfMessageWanted(msgType)) return;
852
853 if (dataPtr == 0) {
854 ALOGE("Null data returned in data with timestamp callback");
855 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
856 return;
857 }
858
859 client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
860}
861
862// snapshot taken callback
863void CameraClient::handleShutter(void) {
864 if (mPlayShutterSound) {
865 mCameraService->playSound(CameraService::SOUND_SHUTTER);
866 }
867
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800868 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700869 if (c != 0) {
870 mLock.unlock();
871 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
872 if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
873 }
874 disableMsgType(CAMERA_MSG_SHUTTER);
875
876 mLock.unlock();
877}
878
879// preview callback - frame buffer update
880void CameraClient::handlePreviewData(int32_t msgType,
881 const sp<IMemory>& mem,
882 camera_frame_metadata_t *metadata) {
883 ssize_t offset;
884 size_t size;
885 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
886
887 // local copy of the callback flags
888 int flags = mPreviewCallbackFlag;
889
890 // is callback enabled?
891 if (!(flags & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
892 // If the enable bit is off, the copy-out and one-shot bits are ignored
893 LOG2("frame callback is disabled");
894 mLock.unlock();
895 return;
896 }
897
898 // hold a strong pointer to the client
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800899 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700900
901 // clear callback flags if no client or one-shot mode
902 if (c == 0 || (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
903 LOG2("Disable preview callback");
904 mPreviewCallbackFlag &= ~(CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
905 CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
906 CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK);
907 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
908 }
909
910 if (c != 0) {
911 // Is the received frame copied out or not?
912 if (flags & CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
913 LOG2("frame is copied");
914 copyFrameAndPostCopiedFrame(msgType, c, heap, offset, size, metadata);
915 } else {
916 LOG2("frame is forwarded");
917 mLock.unlock();
918 c->dataCallback(msgType, mem, metadata);
919 }
920 } else {
921 mLock.unlock();
922 }
923}
924
925// picture callback - postview image ready
926void CameraClient::handlePostview(const sp<IMemory>& mem) {
927 disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
928
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800929 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700930 mLock.unlock();
931 if (c != 0) {
932 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem, NULL);
933 }
934}
935
936// picture callback - raw image ready
937void CameraClient::handleRawPicture(const sp<IMemory>& mem) {
938 disableMsgType(CAMERA_MSG_RAW_IMAGE);
939
940 ssize_t offset;
941 size_t size;
942 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
943
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800944 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700945 mLock.unlock();
946 if (c != 0) {
947 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem, NULL);
948 }
949}
950
951// picture callback - compressed picture ready
952void CameraClient::handleCompressedPicture(const sp<IMemory>& mem) {
Kiran Kumar H Nb589ead2013-06-27 21:32:54 +0530953#ifdef QCOM_HARDWARE
954 if (mBurstCnt)
955 mBurstCnt--;
956
957 if (!mBurstCnt) {
958 LOG1("handleCompressedPicture mBurstCnt = %d", mBurstCnt);
959#endif
960 disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
961#ifdef QCOM_HARDWARE
962 }
963#endif
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700964
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800965 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700966 mLock.unlock();
967 if (c != 0) {
968 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem, NULL);
969 }
970}
971
x01180641c960842011-09-26 19:03:36 -0500972#if defined(OMAP_ICS_CAMERA) || defined(OMAP_ENHANCEMENT_BURST_CAPTURE)
973// burst picture callback - compressed picture ready
974void CameraClient::handleCompressedBurstPicture(const sp<IMemory>& mem) {
975 // Don't disable this message type yet. In this mode takePicture() will
976 // get called only once. When burst finishes this message will get automatically
977 // disabled in the respective call for restarting the preview.
978
979 sp<ICameraClient> c = mCameraClient;
980 mLock.unlock();
981 if (c != 0) {
982 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem, NULL);
983 }
984}
985#endif
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700986
987void CameraClient::handleGenericNotify(int32_t msgType,
988 int32_t ext1, int32_t ext2) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800989 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700990 mLock.unlock();
991 if (c != 0) {
992 c->notifyCallback(msgType, ext1, ext2);
993 }
994}
995
996void CameraClient::handleGenericData(int32_t msgType,
997 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800998 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700999 mLock.unlock();
1000 if (c != 0) {
1001 c->dataCallback(msgType, dataPtr, metadata);
1002 }
1003}
1004
1005void CameraClient::handleGenericDataTimestamp(nsecs_t timestamp,
1006 int32_t msgType, const sp<IMemory>& dataPtr) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -08001007 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001008 mLock.unlock();
1009 if (c != 0) {
1010 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
1011 }
1012}
1013
1014void CameraClient::copyFrameAndPostCopiedFrame(
1015 int32_t msgType, const sp<ICameraClient>& client,
1016 const sp<IMemoryHeap>& heap, size_t offset, size_t size,
1017 camera_frame_metadata_t *metadata) {
1018 LOG2("copyFrameAndPostCopiedFrame");
1019 // It is necessary to copy out of pmem before sending this to
1020 // the callback. For efficiency, reuse the same MemoryHeapBase
1021 // provided it's big enough. Don't allocate the memory or
1022 // perform the copy if there's no callback.
1023 // hold the preview lock while we grab a reference to the preview buffer
1024 sp<MemoryHeapBase> previewBuffer;
1025
1026 if (mPreviewBuffer == 0) {
1027 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1028 } else if (size > mPreviewBuffer->virtualSize()) {
1029 mPreviewBuffer.clear();
1030 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1031 }
1032 if (mPreviewBuffer == 0) {
1033 ALOGE("failed to allocate space for preview buffer");
1034 mLock.unlock();
1035 return;
1036 }
1037 previewBuffer = mPreviewBuffer;
1038
1039 memcpy(previewBuffer->base(), (uint8_t *)heap->base() + offset, size);
1040
1041 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
1042 if (frame == 0) {
1043 ALOGE("failed to allocate space for frame callback");
1044 mLock.unlock();
1045 return;
1046 }
1047
1048 mLock.unlock();
1049 client->dataCallback(msgType, frame, metadata);
1050}
1051
1052int CameraClient::getOrientation(int degrees, bool mirror) {
1053 if (!mirror) {
1054 if (degrees == 0) return 0;
1055 else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
1056 else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
1057 else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
1058 } else { // Do mirror (horizontal flip)
1059 if (degrees == 0) { // FLIP_H and ROT_0
1060 return HAL_TRANSFORM_FLIP_H;
1061 } else if (degrees == 90) { // FLIP_H and ROT_90
1062 return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
1063 } else if (degrees == 180) { // FLIP_H and ROT_180
1064 return HAL_TRANSFORM_FLIP_V;
1065 } else if (degrees == 270) { // FLIP_H and ROT_270
1066 return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
1067 }
1068 }
1069 ALOGE("Invalid setDisplayOrientation degrees=%d", degrees);
1070 return -1;
1071}
1072
1073}; // namespace android