blob: 24112c10a86e7d093e95ac2a21215e02a4c15bfb [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
91 enableMsgType(CAMERA_MSG_ERROR | CAMERA_MSG_ZOOM | CAMERA_MSG_FOCUS |
x01180641c960842011-09-26 19:03:36 -050092 CAMERA_MSG_PREVIEW_METADATA
93#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);
463 mHardware->stopPreview();
464
465 mPreviewBuffer.clear();
466}
467
468// stop recording mode
469void CameraClient::stopRecording() {
470 LOG1("stopRecording (pid %d)", getCallingPid());
471 Mutex::Autolock lock(mLock);
472 if (checkPidAndHardware() != NO_ERROR) return;
473
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700474 disableMsgType(CAMERA_MSG_VIDEO_FRAME);
475 mHardware->stopRecording();
Patric Frederiksen35f859b2012-07-10 13:38:35 +0200476 mCameraService->playSound(CameraService::SOUND_RECORDING);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700477
478 mPreviewBuffer.clear();
479}
480
481// release a recording frame
482void CameraClient::releaseRecordingFrame(const sp<IMemory>& mem) {
483 Mutex::Autolock lock(mLock);
484 if (checkPidAndHardware() != NO_ERROR) return;
485 mHardware->releaseRecordingFrame(mem);
486}
487
488status_t CameraClient::storeMetaDataInBuffers(bool enabled)
489{
490 LOG1("storeMetaDataInBuffers: %s", enabled? "true": "false");
491 Mutex::Autolock lock(mLock);
492 if (checkPidAndHardware() != NO_ERROR) {
493 return UNKNOWN_ERROR;
494 }
495 return mHardware->storeMetaDataInBuffers(enabled);
496}
497
498bool CameraClient::previewEnabled() {
499 LOG1("previewEnabled (pid %d)", getCallingPid());
500
501 Mutex::Autolock lock(mLock);
502 if (checkPidAndHardware() != NO_ERROR) return false;
503 return mHardware->previewEnabled();
504}
505
506bool CameraClient::recordingEnabled() {
507 LOG1("recordingEnabled (pid %d)", getCallingPid());
508
509 Mutex::Autolock lock(mLock);
510 if (checkPidAndHardware() != NO_ERROR) return false;
511 return mHardware->recordingEnabled();
512}
513
514status_t CameraClient::autoFocus() {
515 LOG1("autoFocus (pid %d)", getCallingPid());
516
517 Mutex::Autolock lock(mLock);
518 status_t result = checkPidAndHardware();
519 if (result != NO_ERROR) return result;
520
521 return mHardware->autoFocus();
522}
523
524status_t CameraClient::cancelAutoFocus() {
525 LOG1("cancelAutoFocus (pid %d)", getCallingPid());
526
527 Mutex::Autolock lock(mLock);
528 status_t result = checkPidAndHardware();
529 if (result != NO_ERROR) return result;
530
531 return mHardware->cancelAutoFocus();
532}
533
534// take a picture - image is returned in callback
535status_t CameraClient::takePicture(int msgType) {
536 LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType);
537
538 Mutex::Autolock lock(mLock);
539 status_t result = checkPidAndHardware();
540 if (result != NO_ERROR) return result;
541
542 if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
543 (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
544 ALOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
545 " cannot be both enabled");
546 return BAD_VALUE;
547 }
548
549 // We only accept picture related message types
550 // and ignore other types of messages for takePicture().
551 int picMsgType = msgType
552 & (CAMERA_MSG_SHUTTER |
553 CAMERA_MSG_POSTVIEW_FRAME |
554 CAMERA_MSG_RAW_IMAGE |
555 CAMERA_MSG_RAW_IMAGE_NOTIFY |
x01180641c960842011-09-26 19:03:36 -0500556#if defined(OMAP_ICS_CAMERA) || defined(OMAP_ENHANCEMENT_BURST_CAPTURE)
557 CAMERA_MSG_RAW_BURST |
558#endif
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700559 CAMERA_MSG_COMPRESSED_IMAGE);
560
x01180641c960842011-09-26 19:03:36 -0500561#if defined(OMAP_ICS_CAMERA) || defined(OMAP_ENHANCEMENT_BURST_CAPTURE)
562 picMsgType |= CAMERA_MSG_COMPRESSED_BURST_IMAGE;
563#endif
564
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700565 enableMsgType(picMsgType);
566
567 return mHardware->takePicture();
568}
569
570// set preview/capture parameters - key/value pairs
571status_t CameraClient::setParameters(const String8& params) {
572 LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
573
574 Mutex::Autolock lock(mLock);
575 status_t result = checkPidAndHardware();
576 if (result != NO_ERROR) return result;
577
578 CameraParameters p(params);
579 return mHardware->setParameters(p);
580}
581
582// get preview/capture parameters - key/value pairs
583String8 CameraClient::getParameters() const {
584 Mutex::Autolock lock(mLock);
585 if (checkPidAndHardware() != NO_ERROR) return String8();
586
587 String8 params(mHardware->getParameters().flatten());
588 LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
589 return params;
590}
591
592// enable shutter sound
593status_t CameraClient::enableShutterSound(bool enable) {
594 LOG1("enableShutterSound (pid %d)", getCallingPid());
595
596 status_t result = checkPidAndHardware();
597 if (result != NO_ERROR) return result;
598
599 if (enable) {
600 mPlayShutterSound = true;
601 return OK;
602 }
603
604 // Disabling shutter sound may not be allowed. In that case only
605 // allow the mediaserver process to disable the sound.
606 char value[PROPERTY_VALUE_MAX];
607 property_get("ro.camera.sound.forced", value, "0");
608 if (strcmp(value, "0") != 0) {
609 // Disabling shutter sound is not allowed. Deny if the current
610 // process is not mediaserver.
611 if (getCallingPid() != getpid()) {
612 ALOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid());
613 return PERMISSION_DENIED;
614 }
615 }
616
617 mPlayShutterSound = false;
618 return OK;
619}
620
621status_t CameraClient::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
622 LOG1("sendCommand (pid %d)", getCallingPid());
623 int orientation;
624 Mutex::Autolock lock(mLock);
625 status_t result = checkPidAndHardware();
626 if (result != NO_ERROR) return result;
627
628 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
629 // Mirror the preview if the camera is front-facing.
630 orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT);
631 if (orientation == -1) return BAD_VALUE;
632
633 if (mOrientation != orientation) {
634 mOrientation = orientation;
635 if (mPreviewWindow != 0) {
636 native_window_set_buffers_transform(mPreviewWindow.get(),
637 mOrientation);
638 }
639 }
640 return OK;
641 } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) {
642 switch (arg1) {
643 case 0:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700644 return enableShutterSound(false);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700645 case 1:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700646 return enableShutterSound(true);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700647 default:
648 return BAD_VALUE;
649 }
650 return OK;
651 } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) {
652 mCameraService->playSound(CameraService::SOUND_RECORDING);
James Dong983cf232012-08-01 16:39:55 -0700653 } else if (cmd == CAMERA_CMD_SET_VIDEO_BUFFER_COUNT) {
654 // Silently ignore this command
655 return INVALID_OPERATION;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700656 } else if (cmd == CAMERA_CMD_PING) {
657 // If mHardware is 0, checkPidAndHardware will return error.
658 return OK;
659 }
660
661 return mHardware->sendCommand(cmd, arg1, arg2);
662}
663
664// ----------------------------------------------------------------------------
665
666void CameraClient::enableMsgType(int32_t msgType) {
667 android_atomic_or(msgType, &mMsgEnabled);
668 mHardware->enableMsgType(msgType);
669}
670
671void CameraClient::disableMsgType(int32_t msgType) {
672 android_atomic_and(~msgType, &mMsgEnabled);
673 mHardware->disableMsgType(msgType);
674}
675
676#define CHECK_MESSAGE_INTERVAL 10 // 10ms
677bool CameraClient::lockIfMessageWanted(int32_t msgType) {
678 int sleepCount = 0;
679 while (mMsgEnabled & msgType) {
680 if (mLock.tryLock() == NO_ERROR) {
681 if (sleepCount > 0) {
682 LOG1("lockIfMessageWanted(%d): waited for %d ms",
683 msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
684 }
685 return true;
686 }
687 if (sleepCount++ == 0) {
688 LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
689 }
690 usleep(CHECK_MESSAGE_INTERVAL * 1000);
691 }
692 ALOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
693 return false;
694}
695
696// Callback messages can be dispatched to internal handlers or pass to our
697// client's callback functions, depending on the message type.
698//
699// notifyCallback:
700// CAMERA_MSG_SHUTTER handleShutter
701// (others) c->notifyCallback
702// dataCallback:
703// CAMERA_MSG_PREVIEW_FRAME handlePreviewData
704// CAMERA_MSG_POSTVIEW_FRAME handlePostview
705// CAMERA_MSG_RAW_IMAGE handleRawPicture
706// CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture
707// (others) c->dataCallback
708// dataCallbackTimestamp
709// (others) c->dataCallbackTimestamp
710//
711// NOTE: the *Callback functions grab mLock of the client before passing
712// control to handle* functions. So the handle* functions must release the
713// lock before calling the ICameraClient's callbacks, so those callbacks can
714// invoke methods in the Client class again (For example, the preview frame
715// callback may want to releaseRecordingFrame). The handle* functions must
716// release the lock after all accesses to member variables, so it must be
717// handled very carefully.
718
719void CameraClient::notifyCallback(int32_t msgType, int32_t ext1,
720 int32_t ext2, void* user) {
721 LOG2("notifyCallback(%d)", msgType);
722
Steve Kondik3a85aca2012-12-15 20:29:59 -0800723 // Ignore CAF_RESTART callbacks from Samsung's camera driver
724 if (msgType == CAMERA_MSG_FOCUS && ext1 == 4) {
725 LOG2("Ignore CAF_RESTART callback");
726 return;
727 }
728
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700729 Mutex* lock = getClientLockFromCookie(user);
730 if (lock == NULL) return;
731 Mutex::Autolock alock(*lock);
732
733 CameraClient* client =
734 static_cast<CameraClient*>(getClientFromCookie(user));
735 if (client == NULL) return;
736
737 if (!client->lockIfMessageWanted(msgType)) return;
738
739 switch (msgType) {
740 case CAMERA_MSG_SHUTTER:
741 // ext1 is the dimension of the yuv picture.
742 client->handleShutter();
743 break;
744 default:
745 client->handleGenericNotify(msgType, ext1, ext2);
746 break;
747 }
748}
749
750void CameraClient::dataCallback(int32_t msgType,
751 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) {
752 LOG2("dataCallback(%d)", msgType);
753
754 Mutex* lock = getClientLockFromCookie(user);
755 if (lock == NULL) return;
756 Mutex::Autolock alock(*lock);
757
758 CameraClient* client =
759 static_cast<CameraClient*>(getClientFromCookie(user));
760 if (client == NULL) return;
761
762 if (!client->lockIfMessageWanted(msgType)) return;
763 if (dataPtr == 0 && metadata == NULL) {
764 ALOGE("Null data returned in data callback");
765 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
766 return;
767 }
768
769 switch (msgType & ~CAMERA_MSG_PREVIEW_METADATA) {
770 case CAMERA_MSG_PREVIEW_FRAME:
771 client->handlePreviewData(msgType, dataPtr, metadata);
772 break;
773 case CAMERA_MSG_POSTVIEW_FRAME:
774 client->handlePostview(dataPtr);
775 break;
776 case CAMERA_MSG_RAW_IMAGE:
777 client->handleRawPicture(dataPtr);
778 break;
779 case CAMERA_MSG_COMPRESSED_IMAGE:
780 client->handleCompressedPicture(dataPtr);
781 break;
x01180641c960842011-09-26 19:03:36 -0500782#if defined(OMAP_ICS_CAMERA) || defined(OMAP_ENHANCEMENT_BURST_CAPTURE)
783 case CAMERA_MSG_COMPRESSED_BURST_IMAGE:
784 client->handleCompressedBurstPicture(dataPtr);
785 break;
786#endif
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700787 default:
788 client->handleGenericData(msgType, dataPtr, metadata);
789 break;
790 }
791}
792
793void CameraClient::dataCallbackTimestamp(nsecs_t timestamp,
794 int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
795 LOG2("dataCallbackTimestamp(%d)", msgType);
796
797 Mutex* lock = getClientLockFromCookie(user);
798 if (lock == NULL) return;
799 Mutex::Autolock alock(*lock);
800
801 CameraClient* client =
802 static_cast<CameraClient*>(getClientFromCookie(user));
803 if (client == NULL) return;
804
805 if (!client->lockIfMessageWanted(msgType)) return;
806
807 if (dataPtr == 0) {
808 ALOGE("Null data returned in data with timestamp callback");
809 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
810 return;
811 }
812
813 client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
814}
815
816// snapshot taken callback
817void CameraClient::handleShutter(void) {
818 if (mPlayShutterSound) {
819 mCameraService->playSound(CameraService::SOUND_SHUTTER);
820 }
821
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800822 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700823 if (c != 0) {
824 mLock.unlock();
825 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
826 if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
827 }
828 disableMsgType(CAMERA_MSG_SHUTTER);
829
830 mLock.unlock();
831}
832
833// preview callback - frame buffer update
834void CameraClient::handlePreviewData(int32_t msgType,
835 const sp<IMemory>& mem,
836 camera_frame_metadata_t *metadata) {
837 ssize_t offset;
838 size_t size;
839 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
840
841 // local copy of the callback flags
842 int flags = mPreviewCallbackFlag;
843
844 // is callback enabled?
845 if (!(flags & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
846 // If the enable bit is off, the copy-out and one-shot bits are ignored
847 LOG2("frame callback is disabled");
848 mLock.unlock();
849 return;
850 }
851
852 // hold a strong pointer to the client
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800853 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700854
855 // clear callback flags if no client or one-shot mode
856 if (c == 0 || (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
857 LOG2("Disable preview callback");
858 mPreviewCallbackFlag &= ~(CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
859 CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
860 CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK);
861 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
862 }
863
864 if (c != 0) {
865 // Is the received frame copied out or not?
866 if (flags & CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
867 LOG2("frame is copied");
868 copyFrameAndPostCopiedFrame(msgType, c, heap, offset, size, metadata);
869 } else {
870 LOG2("frame is forwarded");
871 mLock.unlock();
872 c->dataCallback(msgType, mem, metadata);
873 }
874 } else {
875 mLock.unlock();
876 }
877}
878
879// picture callback - postview image ready
880void CameraClient::handlePostview(const sp<IMemory>& mem) {
881 disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
882
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800883 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700884 mLock.unlock();
885 if (c != 0) {
886 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem, NULL);
887 }
888}
889
890// picture callback - raw image ready
891void CameraClient::handleRawPicture(const sp<IMemory>& mem) {
892 disableMsgType(CAMERA_MSG_RAW_IMAGE);
893
894 ssize_t offset;
895 size_t size;
896 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
897
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800898 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700899 mLock.unlock();
900 if (c != 0) {
901 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem, NULL);
902 }
903}
904
905// picture callback - compressed picture ready
906void CameraClient::handleCompressedPicture(const sp<IMemory>& mem) {
907 disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
908
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800909 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700910 mLock.unlock();
911 if (c != 0) {
912 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem, NULL);
913 }
914}
915
x01180641c960842011-09-26 19:03:36 -0500916#if defined(OMAP_ICS_CAMERA) || defined(OMAP_ENHANCEMENT_BURST_CAPTURE)
917// burst picture callback - compressed picture ready
918void CameraClient::handleCompressedBurstPicture(const sp<IMemory>& mem) {
919 // Don't disable this message type yet. In this mode takePicture() will
920 // get called only once. When burst finishes this message will get automatically
921 // disabled in the respective call for restarting the preview.
922
923 sp<ICameraClient> c = mCameraClient;
924 mLock.unlock();
925 if (c != 0) {
926 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem, NULL);
927 }
928}
929#endif
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700930
931void CameraClient::handleGenericNotify(int32_t msgType,
932 int32_t ext1, int32_t ext2) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800933 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700934 mLock.unlock();
935 if (c != 0) {
936 c->notifyCallback(msgType, ext1, ext2);
937 }
938}
939
940void CameraClient::handleGenericData(int32_t msgType,
941 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800942 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700943 mLock.unlock();
944 if (c != 0) {
945 c->dataCallback(msgType, dataPtr, metadata);
946 }
947}
948
949void CameraClient::handleGenericDataTimestamp(nsecs_t timestamp,
950 int32_t msgType, const sp<IMemory>& dataPtr) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800951 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700952 mLock.unlock();
953 if (c != 0) {
954 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
955 }
956}
957
958void CameraClient::copyFrameAndPostCopiedFrame(
959 int32_t msgType, const sp<ICameraClient>& client,
960 const sp<IMemoryHeap>& heap, size_t offset, size_t size,
961 camera_frame_metadata_t *metadata) {
962 LOG2("copyFrameAndPostCopiedFrame");
963 // It is necessary to copy out of pmem before sending this to
964 // the callback. For efficiency, reuse the same MemoryHeapBase
965 // provided it's big enough. Don't allocate the memory or
966 // perform the copy if there's no callback.
967 // hold the preview lock while we grab a reference to the preview buffer
968 sp<MemoryHeapBase> previewBuffer;
969
970 if (mPreviewBuffer == 0) {
971 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
972 } else if (size > mPreviewBuffer->virtualSize()) {
973 mPreviewBuffer.clear();
974 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
975 }
976 if (mPreviewBuffer == 0) {
977 ALOGE("failed to allocate space for preview buffer");
978 mLock.unlock();
979 return;
980 }
981 previewBuffer = mPreviewBuffer;
982
983 memcpy(previewBuffer->base(), (uint8_t *)heap->base() + offset, size);
984
985 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
986 if (frame == 0) {
987 ALOGE("failed to allocate space for frame callback");
988 mLock.unlock();
989 return;
990 }
991
992 mLock.unlock();
993 client->dataCallback(msgType, frame, metadata);
994}
995
996int CameraClient::getOrientation(int degrees, bool mirror) {
997 if (!mirror) {
998 if (degrees == 0) return 0;
999 else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
1000 else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
1001 else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
1002 } else { // Do mirror (horizontal flip)
1003 if (degrees == 0) { // FLIP_H and ROT_0
1004 return HAL_TRANSFORM_FLIP_H;
1005 } else if (degrees == 90) { // FLIP_H and ROT_90
1006 return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
1007 } else if (degrees == 180) { // FLIP_H and ROT_180
1008 return HAL_TRANSFORM_FLIP_V;
1009 } else if (degrees == 270) { // FLIP_H and ROT_270
1010 return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
1011 }
1012 }
1013 ALOGE("Invalid setDisplayOrientation degrees=%d", degrees);
1014 return -1;
1015}
1016
1017}; // namespace android