blob: 90f8f40bddfe8512e6820842c1504294b0c17be2 [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
23#include "CameraClient.h"
24#include "CameraHardwareInterface.h"
25#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 |
92 CAMERA_MSG_PREVIEW_METADATA | CAMERA_MSG_FOCUS_MOVE);
93
94 LOG1("CameraClient::initialize X (pid %d, id %d)", callingPid, mCameraId);
95 return OK;
96}
97
98
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070099// tear down the client
100CameraClient::~CameraClient() {
101 // this lock should never be NULL
102 Mutex* lock = mCameraService->getClientLockById(mCameraId);
103 lock->lock();
104 mDestructionStarted = true;
105 // client will not be accessed from callback. should unlock to prevent dead-lock in disconnect
106 lock->unlock();
107 int callingPid = getCallingPid();
108 LOG1("CameraClient::~CameraClient E (pid %d, this %p)", callingPid, this);
109
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700110 disconnect();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700111 LOG1("CameraClient::~CameraClient X (pid %d, this %p)", callingPid, this);
112}
113
114status_t CameraClient::dump(int fd, const Vector<String16>& args) {
115 const size_t SIZE = 256;
116 char buffer[SIZE];
117
118 size_t len = snprintf(buffer, SIZE, "Client[%d] (%p) PID: %d\n",
119 mCameraId,
120 getCameraClient()->asBinder().get(),
121 mClientPid);
122 len = (len > SIZE - 1) ? SIZE - 1 : len;
123 write(fd, buffer, len);
124 return mHardware->dump(fd, args);
125}
126
127// ----------------------------------------------------------------------------
128
129status_t CameraClient::checkPid() const {
130 int callingPid = getCallingPid();
Eino-Ville Talvalac0379202012-10-09 22:16:58 -0700131 if (callingPid == mClientPid) return NO_ERROR;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700132
133 ALOGW("attempt to use a locked camera from a different process"
134 " (old pid %d, new pid %d)", mClientPid, callingPid);
135 return EBUSY;
136}
137
138status_t CameraClient::checkPidAndHardware() const {
139 status_t result = checkPid();
140 if (result != NO_ERROR) return result;
141 if (mHardware == 0) {
142 ALOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid());
143 return INVALID_OPERATION;
144 }
145 return NO_ERROR;
146}
147
148status_t CameraClient::lock() {
149 int callingPid = getCallingPid();
150 LOG1("lock (pid %d)", callingPid);
151 Mutex::Autolock lock(mLock);
152
153 // lock camera to this client if the the camera is unlocked
154 if (mClientPid == 0) {
155 mClientPid = callingPid;
156 return NO_ERROR;
157 }
158
159 // returns NO_ERROR if the client already owns the camera, EBUSY otherwise
160 return checkPid();
161}
162
163status_t CameraClient::unlock() {
164 int callingPid = getCallingPid();
165 LOG1("unlock (pid %d)", callingPid);
166 Mutex::Autolock lock(mLock);
167
168 // allow anyone to use camera (after they lock the camera)
169 status_t result = checkPid();
170 if (result == NO_ERROR) {
171 if (mHardware->recordingEnabled()) {
172 ALOGE("Not allowed to unlock camera during recording.");
173 return INVALID_OPERATION;
174 }
175 mClientPid = 0;
176 LOG1("clear mCameraClient (pid %d)", callingPid);
177 // we need to remove the reference to ICameraClient so that when the app
178 // goes away, the reference count goes to 0.
179 mCameraClient.clear();
180 }
181 return result;
182}
183
184// connect a new client to the camera
185status_t CameraClient::connect(const sp<ICameraClient>& client) {
186 int callingPid = getCallingPid();
187 LOG1("connect E (pid %d)", callingPid);
188 Mutex::Autolock lock(mLock);
189
190 if (mClientPid != 0 && checkPid() != NO_ERROR) {
191 ALOGW("Tried to connect to a locked camera (old pid %d, new pid %d)",
192 mClientPid, callingPid);
193 return EBUSY;
194 }
195
196 if (mCameraClient != 0 && (client->asBinder() == mCameraClient->asBinder())) {
197 LOG1("Connect to the same client");
198 return NO_ERROR;
199 }
200
201 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
202 mClientPid = callingPid;
203 mCameraClient = client;
204
205 LOG1("connect X (pid %d)", callingPid);
206 return NO_ERROR;
207}
208
209static void disconnectWindow(const sp<ANativeWindow>& window) {
210 if (window != 0) {
211 status_t result = native_window_api_disconnect(window.get(),
212 NATIVE_WINDOW_API_CAMERA);
213 if (result != NO_ERROR) {
214 ALOGW("native_window_api_disconnect failed: %s (%d)", strerror(-result),
215 result);
216 }
217 }
218}
219
220void CameraClient::disconnect() {
221 int callingPid = getCallingPid();
222 LOG1("disconnect E (pid %d)", callingPid);
223 Mutex::Autolock lock(mLock);
224
Eino-Ville Talvalac0379202012-10-09 22:16:58 -0700225 // Allow both client and the media server to disconnect at all times
226 if (callingPid != mClientPid && callingPid != mServicePid) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700227 ALOGW("different client - don't disconnect");
228 return;
229 }
230
231 if (mClientPid <= 0) {
232 LOG1("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
233 return;
234 }
235
236 // Make sure disconnect() is done once and once only, whether it is called
237 // from the user directly, or called by the destructor.
238 if (mHardware == 0) return;
239
240 LOG1("hardware teardown");
241 // Before destroying mHardware, we must make sure it's in the
242 // idle state.
243 // Turn off all messages.
244 disableMsgType(CAMERA_MSG_ALL_MSGS);
245 mHardware->stopPreview();
246 mHardware->cancelPicture();
247 // Release the hardware resources.
248 mHardware->release();
249
250 // Release the held ANativeWindow resources.
251 if (mPreviewWindow != 0) {
252 disconnectWindow(mPreviewWindow);
253 mPreviewWindow = 0;
254 mHardware->setPreviewWindow(mPreviewWindow);
255 }
256 mHardware.clear();
257
258 CameraService::Client::disconnect();
259
260 LOG1("disconnect X (pid %d)", callingPid);
261}
262
263// ----------------------------------------------------------------------------
264
265status_t CameraClient::setPreviewWindow(const sp<IBinder>& binder,
266 const sp<ANativeWindow>& window) {
267 Mutex::Autolock lock(mLock);
268 status_t result = checkPidAndHardware();
269 if (result != NO_ERROR) return result;
270
271 // return if no change in surface.
272 if (binder == mSurface) {
273 return NO_ERROR;
274 }
275
276 if (window != 0) {
277 result = native_window_api_connect(window.get(), NATIVE_WINDOW_API_CAMERA);
278 if (result != NO_ERROR) {
279 ALOGE("native_window_api_connect failed: %s (%d)", strerror(-result),
280 result);
281 return result;
282 }
283 }
284
285 // If preview has been already started, register preview buffers now.
286 if (mHardware->previewEnabled()) {
287 if (window != 0) {
288 native_window_set_scaling_mode(window.get(),
289 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
290 native_window_set_buffers_transform(window.get(), mOrientation);
291 result = mHardware->setPreviewWindow(window);
292 }
293 }
294
295 if (result == NO_ERROR) {
296 // Everything has succeeded. Disconnect the old window and remember the
297 // new window.
298 disconnectWindow(mPreviewWindow);
299 mSurface = binder;
300 mPreviewWindow = window;
301 } else {
302 // Something went wrong after we connected to the new window, so
303 // disconnect here.
304 disconnectWindow(window);
305 }
306
307 return result;
308}
309
310// set the Surface that the preview will use
311status_t CameraClient::setPreviewDisplay(const sp<Surface>& surface) {
312 LOG1("setPreviewDisplay(%p) (pid %d)", surface.get(), getCallingPid());
313
Mathias Agopian9e1cdea2013-02-19 18:25:33 -0800314 sp<IBinder> binder(surface != 0 ? surface->getIGraphicBufferProducer()->asBinder() : 0);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700315 sp<ANativeWindow> window(surface);
316 return setPreviewWindow(binder, window);
317}
318
Andy McFadden8ba01022012-12-18 09:46:54 -0800319// set the SurfaceTextureClient that the preview will use
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700320status_t CameraClient::setPreviewTexture(
Andy McFadden8ba01022012-12-18 09:46:54 -0800321 const sp<IGraphicBufferProducer>& bufferProducer) {
322 LOG1("setPreviewTexture(%p) (pid %d)", bufferProducer.get(),
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700323 getCallingPid());
324
325 sp<IBinder> binder;
326 sp<ANativeWindow> window;
Andy McFadden8ba01022012-12-18 09:46:54 -0800327 if (bufferProducer != 0) {
328 binder = bufferProducer->asBinder();
Mathias Agopian1a2952a2013-02-14 17:11:27 -0800329 window = new Surface(bufferProducer);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700330 }
331 return setPreviewWindow(binder, window);
332}
333
334// set the preview callback flag to affect how the received frames from
335// preview are handled.
336void CameraClient::setPreviewCallbackFlag(int callback_flag) {
337 LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid());
338 Mutex::Autolock lock(mLock);
339 if (checkPidAndHardware() != NO_ERROR) return;
340
341 mPreviewCallbackFlag = callback_flag;
342 if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
343 enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
344 } else {
345 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
346 }
347}
348
349// start preview mode
350status_t CameraClient::startPreview() {
351 LOG1("startPreview (pid %d)", getCallingPid());
352 return startCameraMode(CAMERA_PREVIEW_MODE);
353}
354
355// start recording mode
356status_t CameraClient::startRecording() {
357 LOG1("startRecording (pid %d)", getCallingPid());
358 return startCameraMode(CAMERA_RECORDING_MODE);
359}
360
361// start preview or recording
362status_t CameraClient::startCameraMode(camera_mode mode) {
363 LOG1("startCameraMode(%d)", mode);
364 Mutex::Autolock lock(mLock);
365 status_t result = checkPidAndHardware();
366 if (result != NO_ERROR) return result;
367
368 switch(mode) {
369 case CAMERA_PREVIEW_MODE:
370 if (mSurface == 0 && mPreviewWindow == 0) {
371 LOG1("mSurface is not set yet.");
372 // still able to start preview in this case.
373 }
374 return startPreviewMode();
375 case CAMERA_RECORDING_MODE:
376 if (mSurface == 0 && mPreviewWindow == 0) {
377 ALOGE("mSurface or mPreviewWindow must be set before startRecordingMode.");
378 return INVALID_OPERATION;
379 }
380 return startRecordingMode();
381 default:
382 return UNKNOWN_ERROR;
383 }
384}
385
386status_t CameraClient::startPreviewMode() {
387 LOG1("startPreviewMode");
388 status_t result = NO_ERROR;
389
390 // if preview has been enabled, nothing needs to be done
391 if (mHardware->previewEnabled()) {
392 return NO_ERROR;
393 }
394
395 if (mPreviewWindow != 0) {
396 native_window_set_scaling_mode(mPreviewWindow.get(),
397 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
398 native_window_set_buffers_transform(mPreviewWindow.get(),
399 mOrientation);
400 }
401 mHardware->setPreviewWindow(mPreviewWindow);
402 result = mHardware->startPreview();
403
404 return result;
405}
406
407status_t CameraClient::startRecordingMode() {
408 LOG1("startRecordingMode");
409 status_t result = NO_ERROR;
410
411 // if recording has been enabled, nothing needs to be done
412 if (mHardware->recordingEnabled()) {
413 return NO_ERROR;
414 }
415
416 // if preview has not been started, start preview first
417 if (!mHardware->previewEnabled()) {
418 result = startPreviewMode();
419 if (result != NO_ERROR) {
420 return result;
421 }
422 }
423
424 // start recording mode
425 enableMsgType(CAMERA_MSG_VIDEO_FRAME);
426 mCameraService->playSound(CameraService::SOUND_RECORDING);
427 result = mHardware->startRecording();
428 if (result != NO_ERROR) {
429 ALOGE("mHardware->startRecording() failed with status %d", result);
430 }
431 return result;
432}
433
434// stop preview mode
435void CameraClient::stopPreview() {
436 LOG1("stopPreview (pid %d)", getCallingPid());
437 Mutex::Autolock lock(mLock);
438 if (checkPidAndHardware() != NO_ERROR) return;
439
440
441 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
442 mHardware->stopPreview();
443
444 mPreviewBuffer.clear();
445}
446
447// stop recording mode
448void CameraClient::stopRecording() {
449 LOG1("stopRecording (pid %d)", getCallingPid());
450 Mutex::Autolock lock(mLock);
451 if (checkPidAndHardware() != NO_ERROR) return;
452
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700453 disableMsgType(CAMERA_MSG_VIDEO_FRAME);
454 mHardware->stopRecording();
Patric Frederiksen35f859b2012-07-10 13:38:35 +0200455 mCameraService->playSound(CameraService::SOUND_RECORDING);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700456
457 mPreviewBuffer.clear();
458}
459
460// release a recording frame
461void CameraClient::releaseRecordingFrame(const sp<IMemory>& mem) {
462 Mutex::Autolock lock(mLock);
463 if (checkPidAndHardware() != NO_ERROR) return;
464 mHardware->releaseRecordingFrame(mem);
465}
466
467status_t CameraClient::storeMetaDataInBuffers(bool enabled)
468{
469 LOG1("storeMetaDataInBuffers: %s", enabled? "true": "false");
470 Mutex::Autolock lock(mLock);
471 if (checkPidAndHardware() != NO_ERROR) {
472 return UNKNOWN_ERROR;
473 }
474 return mHardware->storeMetaDataInBuffers(enabled);
475}
476
477bool CameraClient::previewEnabled() {
478 LOG1("previewEnabled (pid %d)", getCallingPid());
479
480 Mutex::Autolock lock(mLock);
481 if (checkPidAndHardware() != NO_ERROR) return false;
482 return mHardware->previewEnabled();
483}
484
485bool CameraClient::recordingEnabled() {
486 LOG1("recordingEnabled (pid %d)", getCallingPid());
487
488 Mutex::Autolock lock(mLock);
489 if (checkPidAndHardware() != NO_ERROR) return false;
490 return mHardware->recordingEnabled();
491}
492
493status_t CameraClient::autoFocus() {
494 LOG1("autoFocus (pid %d)", getCallingPid());
495
496 Mutex::Autolock lock(mLock);
497 status_t result = checkPidAndHardware();
498 if (result != NO_ERROR) return result;
499
500 return mHardware->autoFocus();
501}
502
503status_t CameraClient::cancelAutoFocus() {
504 LOG1("cancelAutoFocus (pid %d)", getCallingPid());
505
506 Mutex::Autolock lock(mLock);
507 status_t result = checkPidAndHardware();
508 if (result != NO_ERROR) return result;
509
510 return mHardware->cancelAutoFocus();
511}
512
513// take a picture - image is returned in callback
514status_t CameraClient::takePicture(int msgType) {
515 LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType);
516
517 Mutex::Autolock lock(mLock);
518 status_t result = checkPidAndHardware();
519 if (result != NO_ERROR) return result;
520
521 if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
522 (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
523 ALOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
524 " cannot be both enabled");
525 return BAD_VALUE;
526 }
527
528 // We only accept picture related message types
529 // and ignore other types of messages for takePicture().
530 int picMsgType = msgType
531 & (CAMERA_MSG_SHUTTER |
532 CAMERA_MSG_POSTVIEW_FRAME |
533 CAMERA_MSG_RAW_IMAGE |
534 CAMERA_MSG_RAW_IMAGE_NOTIFY |
535 CAMERA_MSG_COMPRESSED_IMAGE);
536
537 enableMsgType(picMsgType);
538
539 return mHardware->takePicture();
540}
541
542// set preview/capture parameters - key/value pairs
543status_t CameraClient::setParameters(const String8& params) {
544 LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
545
546 Mutex::Autolock lock(mLock);
547 status_t result = checkPidAndHardware();
548 if (result != NO_ERROR) return result;
549
550 CameraParameters p(params);
551 return mHardware->setParameters(p);
552}
553
554// get preview/capture parameters - key/value pairs
555String8 CameraClient::getParameters() const {
556 Mutex::Autolock lock(mLock);
557 if (checkPidAndHardware() != NO_ERROR) return String8();
558
559 String8 params(mHardware->getParameters().flatten());
560 LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
561 return params;
562}
563
564// enable shutter sound
565status_t CameraClient::enableShutterSound(bool enable) {
566 LOG1("enableShutterSound (pid %d)", getCallingPid());
567
568 status_t result = checkPidAndHardware();
569 if (result != NO_ERROR) return result;
570
571 if (enable) {
572 mPlayShutterSound = true;
573 return OK;
574 }
575
576 // Disabling shutter sound may not be allowed. In that case only
577 // allow the mediaserver process to disable the sound.
578 char value[PROPERTY_VALUE_MAX];
579 property_get("ro.camera.sound.forced", value, "0");
580 if (strcmp(value, "0") != 0) {
581 // Disabling shutter sound is not allowed. Deny if the current
582 // process is not mediaserver.
583 if (getCallingPid() != getpid()) {
584 ALOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid());
585 return PERMISSION_DENIED;
586 }
587 }
588
589 mPlayShutterSound = false;
590 return OK;
591}
592
593status_t CameraClient::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
594 LOG1("sendCommand (pid %d)", getCallingPid());
595 int orientation;
596 Mutex::Autolock lock(mLock);
597 status_t result = checkPidAndHardware();
598 if (result != NO_ERROR) return result;
599
600 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
601 // Mirror the preview if the camera is front-facing.
602 orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT);
603 if (orientation == -1) return BAD_VALUE;
604
605 if (mOrientation != orientation) {
606 mOrientation = orientation;
607 if (mPreviewWindow != 0) {
608 native_window_set_buffers_transform(mPreviewWindow.get(),
609 mOrientation);
610 }
611 }
612 return OK;
613 } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) {
614 switch (arg1) {
615 case 0:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700616 return enableShutterSound(false);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700617 case 1:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700618 return enableShutterSound(true);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700619 default:
620 return BAD_VALUE;
621 }
622 return OK;
623 } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) {
624 mCameraService->playSound(CameraService::SOUND_RECORDING);
James Dong983cf232012-08-01 16:39:55 -0700625 } else if (cmd == CAMERA_CMD_SET_VIDEO_BUFFER_COUNT) {
626 // Silently ignore this command
627 return INVALID_OPERATION;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700628 } else if (cmd == CAMERA_CMD_PING) {
629 // If mHardware is 0, checkPidAndHardware will return error.
630 return OK;
631 }
632
633 return mHardware->sendCommand(cmd, arg1, arg2);
634}
635
636// ----------------------------------------------------------------------------
637
638void CameraClient::enableMsgType(int32_t msgType) {
639 android_atomic_or(msgType, &mMsgEnabled);
640 mHardware->enableMsgType(msgType);
641}
642
643void CameraClient::disableMsgType(int32_t msgType) {
644 android_atomic_and(~msgType, &mMsgEnabled);
645 mHardware->disableMsgType(msgType);
646}
647
648#define CHECK_MESSAGE_INTERVAL 10 // 10ms
649bool CameraClient::lockIfMessageWanted(int32_t msgType) {
650 int sleepCount = 0;
651 while (mMsgEnabled & msgType) {
652 if (mLock.tryLock() == NO_ERROR) {
653 if (sleepCount > 0) {
654 LOG1("lockIfMessageWanted(%d): waited for %d ms",
655 msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
656 }
657 return true;
658 }
659 if (sleepCount++ == 0) {
660 LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
661 }
662 usleep(CHECK_MESSAGE_INTERVAL * 1000);
663 }
664 ALOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
665 return false;
666}
667
668// Callback messages can be dispatched to internal handlers or pass to our
669// client's callback functions, depending on the message type.
670//
671// notifyCallback:
672// CAMERA_MSG_SHUTTER handleShutter
673// (others) c->notifyCallback
674// dataCallback:
675// CAMERA_MSG_PREVIEW_FRAME handlePreviewData
676// CAMERA_MSG_POSTVIEW_FRAME handlePostview
677// CAMERA_MSG_RAW_IMAGE handleRawPicture
678// CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture
679// (others) c->dataCallback
680// dataCallbackTimestamp
681// (others) c->dataCallbackTimestamp
682//
683// NOTE: the *Callback functions grab mLock of the client before passing
684// control to handle* functions. So the handle* functions must release the
685// lock before calling the ICameraClient's callbacks, so those callbacks can
686// invoke methods in the Client class again (For example, the preview frame
687// callback may want to releaseRecordingFrame). The handle* functions must
688// release the lock after all accesses to member variables, so it must be
689// handled very carefully.
690
691void CameraClient::notifyCallback(int32_t msgType, int32_t ext1,
692 int32_t ext2, void* user) {
693 LOG2("notifyCallback(%d)", msgType);
694
695 Mutex* lock = getClientLockFromCookie(user);
696 if (lock == NULL) return;
697 Mutex::Autolock alock(*lock);
698
699 CameraClient* client =
700 static_cast<CameraClient*>(getClientFromCookie(user));
701 if (client == NULL) return;
702
703 if (!client->lockIfMessageWanted(msgType)) return;
704
705 switch (msgType) {
706 case CAMERA_MSG_SHUTTER:
707 // ext1 is the dimension of the yuv picture.
708 client->handleShutter();
709 break;
710 default:
711 client->handleGenericNotify(msgType, ext1, ext2);
712 break;
713 }
714}
715
716void CameraClient::dataCallback(int32_t msgType,
717 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) {
718 LOG2("dataCallback(%d)", msgType);
719
720 Mutex* lock = getClientLockFromCookie(user);
721 if (lock == NULL) return;
722 Mutex::Autolock alock(*lock);
723
724 CameraClient* client =
725 static_cast<CameraClient*>(getClientFromCookie(user));
726 if (client == NULL) return;
727
728 if (!client->lockIfMessageWanted(msgType)) return;
729 if (dataPtr == 0 && metadata == NULL) {
730 ALOGE("Null data returned in data callback");
731 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
732 return;
733 }
734
735 switch (msgType & ~CAMERA_MSG_PREVIEW_METADATA) {
736 case CAMERA_MSG_PREVIEW_FRAME:
737 client->handlePreviewData(msgType, dataPtr, metadata);
738 break;
739 case CAMERA_MSG_POSTVIEW_FRAME:
740 client->handlePostview(dataPtr);
741 break;
742 case CAMERA_MSG_RAW_IMAGE:
743 client->handleRawPicture(dataPtr);
744 break;
745 case CAMERA_MSG_COMPRESSED_IMAGE:
746 client->handleCompressedPicture(dataPtr);
747 break;
748 default:
749 client->handleGenericData(msgType, dataPtr, metadata);
750 break;
751 }
752}
753
754void CameraClient::dataCallbackTimestamp(nsecs_t timestamp,
755 int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
756 LOG2("dataCallbackTimestamp(%d)", msgType);
757
758 Mutex* lock = getClientLockFromCookie(user);
759 if (lock == NULL) return;
760 Mutex::Autolock alock(*lock);
761
762 CameraClient* client =
763 static_cast<CameraClient*>(getClientFromCookie(user));
764 if (client == NULL) return;
765
766 if (!client->lockIfMessageWanted(msgType)) return;
767
768 if (dataPtr == 0) {
769 ALOGE("Null data returned in data with timestamp callback");
770 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
771 return;
772 }
773
774 client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
775}
776
777// snapshot taken callback
778void CameraClient::handleShutter(void) {
779 if (mPlayShutterSound) {
780 mCameraService->playSound(CameraService::SOUND_SHUTTER);
781 }
782
783 sp<ICameraClient> c = mCameraClient;
784 if (c != 0) {
785 mLock.unlock();
786 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
787 if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
788 }
789 disableMsgType(CAMERA_MSG_SHUTTER);
790
791 mLock.unlock();
792}
793
794// preview callback - frame buffer update
795void CameraClient::handlePreviewData(int32_t msgType,
796 const sp<IMemory>& mem,
797 camera_frame_metadata_t *metadata) {
798 ssize_t offset;
799 size_t size;
800 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
801
802 // local copy of the callback flags
803 int flags = mPreviewCallbackFlag;
804
805 // is callback enabled?
806 if (!(flags & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
807 // If the enable bit is off, the copy-out and one-shot bits are ignored
808 LOG2("frame callback is disabled");
809 mLock.unlock();
810 return;
811 }
812
813 // hold a strong pointer to the client
814 sp<ICameraClient> c = mCameraClient;
815
816 // clear callback flags if no client or one-shot mode
817 if (c == 0 || (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
818 LOG2("Disable preview callback");
819 mPreviewCallbackFlag &= ~(CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
820 CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
821 CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK);
822 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
823 }
824
825 if (c != 0) {
826 // Is the received frame copied out or not?
827 if (flags & CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
828 LOG2("frame is copied");
829 copyFrameAndPostCopiedFrame(msgType, c, heap, offset, size, metadata);
830 } else {
831 LOG2("frame is forwarded");
832 mLock.unlock();
833 c->dataCallback(msgType, mem, metadata);
834 }
835 } else {
836 mLock.unlock();
837 }
838}
839
840// picture callback - postview image ready
841void CameraClient::handlePostview(const sp<IMemory>& mem) {
842 disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
843
844 sp<ICameraClient> c = mCameraClient;
845 mLock.unlock();
846 if (c != 0) {
847 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem, NULL);
848 }
849}
850
851// picture callback - raw image ready
852void CameraClient::handleRawPicture(const sp<IMemory>& mem) {
853 disableMsgType(CAMERA_MSG_RAW_IMAGE);
854
855 ssize_t offset;
856 size_t size;
857 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
858
859 sp<ICameraClient> c = mCameraClient;
860 mLock.unlock();
861 if (c != 0) {
862 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem, NULL);
863 }
864}
865
866// picture callback - compressed picture ready
867void CameraClient::handleCompressedPicture(const sp<IMemory>& mem) {
868 disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
869
870 sp<ICameraClient> c = mCameraClient;
871 mLock.unlock();
872 if (c != 0) {
873 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem, NULL);
874 }
875}
876
877
878void CameraClient::handleGenericNotify(int32_t msgType,
879 int32_t ext1, int32_t ext2) {
880 sp<ICameraClient> c = mCameraClient;
881 mLock.unlock();
882 if (c != 0) {
883 c->notifyCallback(msgType, ext1, ext2);
884 }
885}
886
887void CameraClient::handleGenericData(int32_t msgType,
888 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata) {
889 sp<ICameraClient> c = mCameraClient;
890 mLock.unlock();
891 if (c != 0) {
892 c->dataCallback(msgType, dataPtr, metadata);
893 }
894}
895
896void CameraClient::handleGenericDataTimestamp(nsecs_t timestamp,
897 int32_t msgType, const sp<IMemory>& dataPtr) {
898 sp<ICameraClient> c = mCameraClient;
899 mLock.unlock();
900 if (c != 0) {
901 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
902 }
903}
904
905void CameraClient::copyFrameAndPostCopiedFrame(
906 int32_t msgType, const sp<ICameraClient>& client,
907 const sp<IMemoryHeap>& heap, size_t offset, size_t size,
908 camera_frame_metadata_t *metadata) {
909 LOG2("copyFrameAndPostCopiedFrame");
910 // It is necessary to copy out of pmem before sending this to
911 // the callback. For efficiency, reuse the same MemoryHeapBase
912 // provided it's big enough. Don't allocate the memory or
913 // perform the copy if there's no callback.
914 // hold the preview lock while we grab a reference to the preview buffer
915 sp<MemoryHeapBase> previewBuffer;
916
917 if (mPreviewBuffer == 0) {
918 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
919 } else if (size > mPreviewBuffer->virtualSize()) {
920 mPreviewBuffer.clear();
921 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
922 }
923 if (mPreviewBuffer == 0) {
924 ALOGE("failed to allocate space for preview buffer");
925 mLock.unlock();
926 return;
927 }
928 previewBuffer = mPreviewBuffer;
929
930 memcpy(previewBuffer->base(), (uint8_t *)heap->base() + offset, size);
931
932 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
933 if (frame == 0) {
934 ALOGE("failed to allocate space for frame callback");
935 mLock.unlock();
936 return;
937 }
938
939 mLock.unlock();
940 client->dataCallback(msgType, frame, metadata);
941}
942
943int CameraClient::getOrientation(int degrees, bool mirror) {
944 if (!mirror) {
945 if (degrees == 0) return 0;
946 else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
947 else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
948 else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
949 } else { // Do mirror (horizontal flip)
950 if (degrees == 0) { // FLIP_H and ROT_0
951 return HAL_TRANSFORM_FLIP_H;
952 } else if (degrees == 90) { // FLIP_H and ROT_90
953 return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
954 } else if (degrees == 180) { // FLIP_H and ROT_180
955 return HAL_TRANSFORM_FLIP_V;
956 } else if (degrees == 270) { // FLIP_H and ROT_270
957 return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
958 }
959 }
960 ALOGE("Invalid setDisplayOrientation degrees=%d", degrees);
961 return -1;
962}
963
964}; // namespace android