blob: 535f07fa136ef50f4671410d2cb0bf82b8d461c0 [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
2**
3** Copyright (C) 2008, The Android Open Source Project
Mathias Agopian65ab4712010-07-14 17:59:35 -07004**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#define LOG_TAG "CameraService"
19
20#include <stdio.h>
21#include <sys/types.h>
22#include <pthread.h>
23
24#include <binder/IPCThreadState.h>
25#include <binder/IServiceManager.h>
26#include <binder/MemoryBase.h>
27#include <binder/MemoryHeapBase.h>
28#include <cutils/atomic.h>
Nipun Kwatrab5ca4612010-09-11 19:31:10 -070029#include <cutils/properties.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070030#include <hardware/hardware.h>
31#include <media/AudioSystem.h>
32#include <media/mediaplayer.h>
33#include <surfaceflinger/ISurface.h>
34#include <ui/Overlay.h>
35#include <utils/Errors.h>
36#include <utils/Log.h>
37#include <utils/String16.h>
38
39#include "CameraService.h"
40
41namespace android {
42
43// ----------------------------------------------------------------------------
44// Logging support -- this is for debugging only
45// Use "adb shell dumpsys media.camera -v 1" to change it.
46static volatile int32_t gLogLevel = 0;
47
48#define LOG1(...) LOGD_IF(gLogLevel >= 1, __VA_ARGS__);
49#define LOG2(...) LOGD_IF(gLogLevel >= 2, __VA_ARGS__);
50
51static void setLogLevel(int level) {
52 android_atomic_write(level, &gLogLevel);
53}
54
55// ----------------------------------------------------------------------------
56
57static int getCallingPid() {
58 return IPCThreadState::self()->getCallingPid();
59}
60
61static int getCallingUid() {
62 return IPCThreadState::self()->getCallingUid();
63}
64
65// ----------------------------------------------------------------------------
66
67// This is ugly and only safe if we never re-create the CameraService, but
68// should be ok for now.
69static CameraService *gCameraService;
70
71CameraService::CameraService()
72:mSoundRef(0)
73{
74 LOGI("CameraService started (pid=%d)", getpid());
75
76 mNumberOfCameras = HAL_getNumberOfCameras();
77 if (mNumberOfCameras > MAX_CAMERAS) {
78 LOGE("Number of cameras(%d) > MAX_CAMERAS(%d).",
79 mNumberOfCameras, MAX_CAMERAS);
80 mNumberOfCameras = MAX_CAMERAS;
81 }
82
83 for (int i = 0; i < mNumberOfCameras; i++) {
84 setCameraFree(i);
85 }
86
87 gCameraService = this;
88}
89
90CameraService::~CameraService() {
91 for (int i = 0; i < mNumberOfCameras; i++) {
92 if (mBusy[i]) {
93 LOGE("camera %d is still in use in destructor!", i);
94 }
95 }
96
97 gCameraService = NULL;
98}
99
100int32_t CameraService::getNumberOfCameras() {
101 return mNumberOfCameras;
102}
103
104status_t CameraService::getCameraInfo(int cameraId,
105 struct CameraInfo* cameraInfo) {
106 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
107 return BAD_VALUE;
108 }
109
110 HAL_getCameraInfo(cameraId, cameraInfo);
111 return OK;
112}
113
114sp<ICamera> CameraService::connect(
115 const sp<ICameraClient>& cameraClient, int cameraId) {
116 int callingPid = getCallingPid();
117 LOG1("CameraService::connect E (pid %d, id %d)", callingPid, cameraId);
118
119 sp<Client> client;
120 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
121 LOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).",
122 callingPid, cameraId);
123 return NULL;
124 }
125
126 Mutex::Autolock lock(mServiceLock);
127 if (mClient[cameraId] != 0) {
128 client = mClient[cameraId].promote();
129 if (client != 0) {
130 if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) {
131 LOG1("CameraService::connect X (pid %d) (the same client)",
132 callingPid);
133 return client;
134 } else {
135 LOGW("CameraService::connect X (pid %d) rejected (existing client).",
136 callingPid);
137 return NULL;
138 }
139 }
140 mClient[cameraId].clear();
141 }
142
143 if (mBusy[cameraId]) {
144 LOGW("CameraService::connect X (pid %d) rejected"
145 " (camera %d is still busy).", callingPid, cameraId);
146 return NULL;
147 }
148
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700149 sp<CameraHardwareInterface> hardware = HAL_openCameraHardware(cameraId);
150 if (hardware == NULL) {
151 LOGE("Fail to open camera hardware (id=%d)", cameraId);
152 return NULL;
153 }
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800154 CameraInfo info;
155 HAL_getCameraInfo(cameraId, &info);
156 client = new Client(this, cameraClient, hardware, cameraId, info.facing,
157 callingPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700158 mClient[cameraId] = client;
159 LOG1("CameraService::connect X");
160 return client;
161}
162
163void CameraService::removeClient(const sp<ICameraClient>& cameraClient) {
164 int callingPid = getCallingPid();
165 LOG1("CameraService::removeClient E (pid %d)", callingPid);
166
167 for (int i = 0; i < mNumberOfCameras; i++) {
168 // Declare this before the lock to make absolutely sure the
169 // destructor won't be called with the lock held.
170 sp<Client> client;
171
172 Mutex::Autolock lock(mServiceLock);
173
174 // This happens when we have already disconnected (or this is
175 // just another unused camera).
176 if (mClient[i] == 0) continue;
177
178 // Promote mClient. It can fail if we are called from this path:
179 // Client::~Client() -> disconnect() -> removeClient().
180 client = mClient[i].promote();
181
182 if (client == 0) {
183 mClient[i].clear();
184 continue;
185 }
186
187 if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) {
188 // Found our camera, clear and leave.
189 LOG1("removeClient: clear camera %d", i);
190 mClient[i].clear();
191 break;
192 }
193 }
194
195 LOG1("CameraService::removeClient X (pid %d)", callingPid);
196}
197
198sp<CameraService::Client> CameraService::getClientById(int cameraId) {
199 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
200 return mClient[cameraId].promote();
201}
202
Mathias Agopian65ab4712010-07-14 17:59:35 -0700203status_t CameraService::onTransact(
204 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
205 // Permission checks
206 switch (code) {
207 case BnCameraService::CONNECT:
208 const int pid = getCallingPid();
209 const int self_pid = getpid();
210 if (pid != self_pid) {
211 // we're called from a different process, do the real check
212 if (!checkCallingPermission(
213 String16("android.permission.CAMERA"))) {
214 const int uid = getCallingUid();
215 LOGE("Permission Denial: "
216 "can't use the camera pid=%d, uid=%d", pid, uid);
217 return PERMISSION_DENIED;
218 }
219 }
220 break;
221 }
222
223 return BnCameraService::onTransact(code, data, reply, flags);
224}
225
226// The reason we need this busy bit is a new CameraService::connect() request
227// may come in while the previous Client's destructor has not been run or is
228// still running. If the last strong reference of the previous Client is gone
229// but the destructor has not been finished, we should not allow the new Client
230// to be created because we need to wait for the previous Client to tear down
231// the hardware first.
232void CameraService::setCameraBusy(int cameraId) {
233 android_atomic_write(1, &mBusy[cameraId]);
234}
235
236void CameraService::setCameraFree(int cameraId) {
237 android_atomic_write(0, &mBusy[cameraId]);
238}
239
240// We share the media players for shutter and recording sound for all clients.
241// A reference count is kept to determine when we will actually release the
242// media players.
243
244static MediaPlayer* newMediaPlayer(const char *file) {
245 MediaPlayer* mp = new MediaPlayer();
246 if (mp->setDataSource(file, NULL) == NO_ERROR) {
247 mp->setAudioStreamType(AudioSystem::ENFORCED_AUDIBLE);
248 mp->prepare();
249 } else {
250 LOGE("Failed to load CameraService sounds: %s", file);
251 return NULL;
252 }
253 return mp;
254}
255
256void CameraService::loadSound() {
257 Mutex::Autolock lock(mSoundLock);
258 LOG1("CameraService::loadSound ref=%d", mSoundRef);
259 if (mSoundRef++) return;
260
261 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
262 mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
263}
264
265void CameraService::releaseSound() {
266 Mutex::Autolock lock(mSoundLock);
267 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
268 if (--mSoundRef) return;
269
270 for (int i = 0; i < NUM_SOUNDS; i++) {
271 if (mSoundPlayer[i] != 0) {
272 mSoundPlayer[i]->disconnect();
273 mSoundPlayer[i].clear();
274 }
275 }
276}
277
278void CameraService::playSound(sound_kind kind) {
279 LOG1("playSound(%d)", kind);
280 Mutex::Autolock lock(mSoundLock);
281 sp<MediaPlayer> player = mSoundPlayer[kind];
282 if (player != 0) {
283 // do not play the sound if stream volume is 0
284 // (typically because ringer mode is silent).
285 int index;
286 AudioSystem::getStreamVolumeIndex(AudioSystem::ENFORCED_AUDIBLE, &index);
287 if (index != 0) {
288 player->seekTo(0);
289 player->start();
290 }
291 }
292}
293
294// ----------------------------------------------------------------------------
295
296CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700297 const sp<ICameraClient>& cameraClient,
298 const sp<CameraHardwareInterface>& hardware,
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800299 int cameraId, int cameraFacing, int clientPid) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700300 int callingPid = getCallingPid();
301 LOG1("Client::Client E (pid %d)", callingPid);
302
303 mCameraService = cameraService;
304 mCameraClient = cameraClient;
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700305 mHardware = hardware;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700306 mCameraId = cameraId;
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800307 mCameraFacing = cameraFacing;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700308 mClientPid = clientPid;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700309 mUseOverlay = mHardware->useOverlay();
310 mMsgEnabled = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700311 mHardware->setCallbacks(notifyCallback,
312 dataCallback,
313 dataCallbackTimestamp,
314 (void *)cameraId);
315
316 // Enable zoom, error, and focus messages by default
317 enableMsgType(CAMERA_MSG_ERROR |
318 CAMERA_MSG_ZOOM |
319 CAMERA_MSG_FOCUS);
320 mOverlayW = 0;
321 mOverlayH = 0;
322
323 // Callback is disabled by default
324 mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800325 mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT);
Wu-cheng Li4a73f3d2010-09-23 17:17:43 -0700326 mOrientationChanged = false;
Nipun Kwatrab5ca4612010-09-11 19:31:10 -0700327 mPlayShutterSound = true;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700328 cameraService->setCameraBusy(cameraId);
329 cameraService->loadSound();
330 LOG1("Client::Client X (pid %d)", callingPid);
331}
332
333static void *unregister_surface(void *arg) {
334 ISurface *surface = (ISurface *)arg;
335 surface->unregisterBuffers();
336 IPCThreadState::self()->flushCommands();
337 return NULL;
338}
339
340// tear down the client
341CameraService::Client::~Client() {
342 int callingPid = getCallingPid();
343 LOG1("Client::~Client E (pid %d, this %p)", callingPid, this);
344
Mathias Agopian65ab4712010-07-14 17:59:35 -0700345 // set mClientPid to let disconnet() tear down the hardware
346 mClientPid = callingPid;
347 disconnect();
348 mCameraService->releaseSound();
349 LOG1("Client::~Client X (pid %d, this %p)", callingPid, this);
350}
351
352// ----------------------------------------------------------------------------
353
354status_t CameraService::Client::checkPid() const {
355 int callingPid = getCallingPid();
356 if (callingPid == mClientPid) return NO_ERROR;
357
358 LOGW("attempt to use a locked camera from a different process"
359 " (old pid %d, new pid %d)", mClientPid, callingPid);
360 return EBUSY;
361}
362
363status_t CameraService::Client::checkPidAndHardware() const {
364 status_t result = checkPid();
365 if (result != NO_ERROR) return result;
366 if (mHardware == 0) {
367 LOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid());
368 return INVALID_OPERATION;
369 }
370 return NO_ERROR;
371}
372
373status_t CameraService::Client::lock() {
374 int callingPid = getCallingPid();
375 LOG1("lock (pid %d)", callingPid);
376 Mutex::Autolock lock(mLock);
377
378 // lock camera to this client if the the camera is unlocked
379 if (mClientPid == 0) {
380 mClientPid = callingPid;
381 return NO_ERROR;
382 }
383
384 // returns NO_ERROR if the client already owns the camera, EBUSY otherwise
385 return checkPid();
386}
387
388status_t CameraService::Client::unlock() {
389 int callingPid = getCallingPid();
390 LOG1("unlock (pid %d)", callingPid);
391 Mutex::Autolock lock(mLock);
392
393 // allow anyone to use camera (after they lock the camera)
394 status_t result = checkPid();
395 if (result == NO_ERROR) {
396 mClientPid = 0;
397 LOG1("clear mCameraClient (pid %d)", callingPid);
398 // we need to remove the reference to ICameraClient so that when the app
399 // goes away, the reference count goes to 0.
400 mCameraClient.clear();
401 }
402 return result;
403}
404
405// connect a new client to the camera
406status_t CameraService::Client::connect(const sp<ICameraClient>& client) {
407 int callingPid = getCallingPid();
408 LOG1("connect E (pid %d)", callingPid);
409 Mutex::Autolock lock(mLock);
410
411 if (mClientPid != 0 && checkPid() != NO_ERROR) {
412 LOGW("Tried to connect to a locked camera (old pid %d, new pid %d)",
413 mClientPid, callingPid);
414 return EBUSY;
415 }
416
417 if (mCameraClient != 0 && (client->asBinder() == mCameraClient->asBinder())) {
418 LOG1("Connect to the same client");
419 return NO_ERROR;
420 }
421
422 mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
423 mClientPid = callingPid;
424 mCameraClient = client;
425
426 LOG1("connect X (pid %d)", callingPid);
427 return NO_ERROR;
428}
429
430void CameraService::Client::disconnect() {
431 int callingPid = getCallingPid();
432 LOG1("disconnect E (pid %d)", callingPid);
433 Mutex::Autolock lock(mLock);
434
435 if (checkPid() != NO_ERROR) {
436 LOGW("different client - don't disconnect");
437 return;
438 }
439
440 if (mClientPid <= 0) {
441 LOG1("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
442 return;
443 }
444
445 // Make sure disconnect() is done once and once only, whether it is called
446 // from the user directly, or called by the destructor.
447 if (mHardware == 0) return;
448
449 LOG1("hardware teardown");
450 // Before destroying mHardware, we must make sure it's in the
451 // idle state.
452 // Turn off all messages.
453 disableMsgType(CAMERA_MSG_ALL_MSGS);
454 mHardware->stopPreview();
455 mHardware->cancelPicture();
456 // Release the hardware resources.
457 mHardware->release();
458 // Release the held overlay resources.
459 if (mUseOverlay) {
460 mOverlayRef = 0;
461 }
Jamie Gennis4b791682010-08-10 16:37:53 -0700462 // Release the held ANativeWindow resources.
463 if (mPreviewWindow != 0) {
464 mPreviewWindow = 0;
465 mHardware->setPreviewWindow(mPreviewWindow);
466 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700467 mHardware.clear();
468
469 mCameraService->removeClient(mCameraClient);
470 mCameraService->setCameraFree(mCameraId);
471
472 LOG1("disconnect X (pid %d)", callingPid);
473}
474
475// ----------------------------------------------------------------------------
476
Jamie Gennis4b791682010-08-10 16:37:53 -0700477// set the Surface that the preview will use
478status_t CameraService::Client::setPreviewDisplay(const sp<Surface>& surface) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700479 LOG1("setPreviewDisplay(%p) (pid %d)", surface.get(), getCallingPid());
480 Mutex::Autolock lock(mLock);
481 status_t result = checkPidAndHardware();
482 if (result != NO_ERROR) return result;
483
484 result = NO_ERROR;
485
486 // return if no change in surface.
487 // asBinder() is safe on NULL (returns NULL)
Jamie Gennis4b791682010-08-10 16:37:53 -0700488 if (getISurface(surface)->asBinder() == mSurface->asBinder()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700489 return result;
490 }
491
492 if (mSurface != 0) {
493 LOG1("clearing old preview surface %p", mSurface.get());
494 if (mUseOverlay) {
495 // Force the destruction of any previous overlay
496 sp<Overlay> dummy;
497 mHardware->setOverlay(dummy);
Wu-cheng Li4a73f3d2010-09-23 17:17:43 -0700498 mOverlayRef = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700499 }
500 }
Jamie Gennis4b791682010-08-10 16:37:53 -0700501 if (surface != 0) {
502 mSurface = getISurface(surface);
503 } else {
504 mSurface = 0;
505 }
506 mPreviewWindow = surface;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700507 mOverlayRef = 0;
508 // If preview has been already started, set overlay or register preview
509 // buffers now.
510 if (mHardware->previewEnabled()) {
511 if (mUseOverlay) {
512 result = setOverlay();
Jamie Gennis4b791682010-08-10 16:37:53 -0700513 } else if (mPreviewWindow != 0) {
Wu-cheng Li012716a2010-10-08 22:04:43 +0800514 native_window_set_buffers_transform(mPreviewWindow.get(),
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800515 mOrientation);
Jamie Gennis4b791682010-08-10 16:37:53 -0700516 result = mHardware->setPreviewWindow(mPreviewWindow);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700517 }
518 }
519
520 return result;
521}
522
Mathias Agopian65ab4712010-07-14 17:59:35 -0700523status_t CameraService::Client::setOverlay() {
524 int w, h;
525 CameraParameters params(mHardware->getParameters());
526 params.getPreviewSize(&w, &h);
527
Wu-cheng Li4a73f3d2010-09-23 17:17:43 -0700528 if (w != mOverlayW || h != mOverlayH || mOrientationChanged) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700529 // Force the destruction of any previous overlay
530 sp<Overlay> dummy;
531 mHardware->setOverlay(dummy);
532 mOverlayRef = 0;
Wu-cheng Li4a73f3d2010-09-23 17:17:43 -0700533 mOrientationChanged = false;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700534 }
535
536 status_t result = NO_ERROR;
537 if (mSurface == 0) {
538 result = mHardware->setOverlay(NULL);
539 } else {
540 if (mOverlayRef == 0) {
541 // FIXME:
542 // Surfaceflinger may hold onto the previous overlay reference for some
543 // time after we try to destroy it. retry a few times. In the future, we
544 // should make the destroy call block, or possibly specify that we can
545 // wait in the createOverlay call if the previous overlay is in the
546 // process of being destroyed.
547 for (int retry = 0; retry < 50; ++retry) {
548 mOverlayRef = mSurface->createOverlay(w, h, OVERLAY_FORMAT_DEFAULT,
549 mOrientation);
550 if (mOverlayRef != 0) break;
551 LOGW("Overlay create failed - retrying");
552 usleep(20000);
553 }
554 if (mOverlayRef == 0) {
555 LOGE("Overlay Creation Failed!");
556 return -EINVAL;
557 }
558 result = mHardware->setOverlay(new Overlay(mOverlayRef));
559 }
560 }
561 if (result != NO_ERROR) {
562 LOGE("mHardware->setOverlay() failed with status %d\n", result);
563 return result;
564 }
565
566 mOverlayW = w;
567 mOverlayH = h;
568
569 return result;
570}
571
572// set the preview callback flag to affect how the received frames from
573// preview are handled.
574void CameraService::Client::setPreviewCallbackFlag(int callback_flag) {
575 LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid());
576 Mutex::Autolock lock(mLock);
577 if (checkPidAndHardware() != NO_ERROR) return;
578
579 mPreviewCallbackFlag = callback_flag;
Wu-cheng Li0667de72010-09-03 16:40:32 -0700580 if (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ENABLE_MASK) {
581 enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
582 } else {
583 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700584 }
585}
586
587// start preview mode
588status_t CameraService::Client::startPreview() {
589 LOG1("startPreview (pid %d)", getCallingPid());
590 return startCameraMode(CAMERA_PREVIEW_MODE);
591}
592
593// start recording mode
594status_t CameraService::Client::startRecording() {
595 LOG1("startRecording (pid %d)", getCallingPid());
596 return startCameraMode(CAMERA_RECORDING_MODE);
597}
598
599// start preview or recording
600status_t CameraService::Client::startCameraMode(camera_mode mode) {
601 LOG1("startCameraMode(%d)", mode);
602 Mutex::Autolock lock(mLock);
603 status_t result = checkPidAndHardware();
604 if (result != NO_ERROR) return result;
605
606 switch(mode) {
607 case CAMERA_PREVIEW_MODE:
Jamie Gennis4b791682010-08-10 16:37:53 -0700608 if (mSurface == 0 && mPreviewWindow == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700609 LOG1("mSurface is not set yet.");
610 // still able to start preview in this case.
611 }
612 return startPreviewMode();
613 case CAMERA_RECORDING_MODE:
Jamie Gennis4b791682010-08-10 16:37:53 -0700614 if (mSurface == 0 && mPreviewWindow == 0) {
615 LOGE("mSurface or mPreviewWindow must be set before startRecordingMode.");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700616 return INVALID_OPERATION;
617 }
618 return startRecordingMode();
619 default:
620 return UNKNOWN_ERROR;
621 }
622}
623
624status_t CameraService::Client::startPreviewMode() {
625 LOG1("startPreviewMode");
626 status_t result = NO_ERROR;
627
628 // if preview has been enabled, nothing needs to be done
629 if (mHardware->previewEnabled()) {
630 return NO_ERROR;
631 }
632
633 if (mUseOverlay) {
634 // If preview display has been set, set overlay now.
635 if (mSurface != 0) {
636 result = setOverlay();
637 }
638 if (result != NO_ERROR) return result;
639 result = mHardware->startPreview();
640 } else {
Wu-cheng Li012716a2010-10-08 22:04:43 +0800641 if (mPreviewWindow != 0) {
642 native_window_set_buffers_transform(mPreviewWindow.get(),
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800643 mOrientation);
Wu-cheng Li012716a2010-10-08 22:04:43 +0800644 }
Jamie Gennis4b791682010-08-10 16:37:53 -0700645 mHardware->setPreviewWindow(mPreviewWindow);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700646 result = mHardware->startPreview();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700647 }
648 return result;
649}
650
651status_t CameraService::Client::startRecordingMode() {
652 LOG1("startRecordingMode");
653 status_t result = NO_ERROR;
654
655 // if recording has been enabled, nothing needs to be done
656 if (mHardware->recordingEnabled()) {
657 return NO_ERROR;
658 }
659
660 // if preview has not been started, start preview first
661 if (!mHardware->previewEnabled()) {
662 result = startPreviewMode();
663 if (result != NO_ERROR) {
664 return result;
665 }
666 }
667
668 // start recording mode
669 enableMsgType(CAMERA_MSG_VIDEO_FRAME);
670 mCameraService->playSound(SOUND_RECORDING);
671 result = mHardware->startRecording();
672 if (result != NO_ERROR) {
673 LOGE("mHardware->startRecording() failed with status %d", result);
674 }
675 return result;
676}
677
678// stop preview mode
679void CameraService::Client::stopPreview() {
680 LOG1("stopPreview (pid %d)", getCallingPid());
681 Mutex::Autolock lock(mLock);
682 if (checkPidAndHardware() != NO_ERROR) return;
683
Jamie Gennis4b791682010-08-10 16:37:53 -0700684
Mathias Agopian65ab4712010-07-14 17:59:35 -0700685 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
686 mHardware->stopPreview();
687
Mathias Agopian65ab4712010-07-14 17:59:35 -0700688 mPreviewBuffer.clear();
689}
690
691// stop recording mode
692void CameraService::Client::stopRecording() {
693 LOG1("stopRecording (pid %d)", getCallingPid());
694 Mutex::Autolock lock(mLock);
695 if (checkPidAndHardware() != NO_ERROR) return;
696
697 mCameraService->playSound(SOUND_RECORDING);
698 disableMsgType(CAMERA_MSG_VIDEO_FRAME);
699 mHardware->stopRecording();
700
701 mPreviewBuffer.clear();
702}
703
704// release a recording frame
705void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem) {
706 Mutex::Autolock lock(mLock);
707 if (checkPidAndHardware() != NO_ERROR) return;
708 mHardware->releaseRecordingFrame(mem);
709}
710
James Donge2ad6732010-10-18 20:42:51 -0700711int32_t CameraService::Client::getNumberOfVideoBuffers() const {
712 LOG1("getNumberOfVideoBuffers");
713 Mutex::Autolock lock(mLock);
714 if (checkPidAndHardware() != NO_ERROR) return 0;
715 return mHardware->getNumberOfVideoBuffers();
716}
717
718sp<IMemory> CameraService::Client::getVideoBuffer(int32_t index) const {
719 LOG1("getVideoBuffer: %d", index);
720 Mutex::Autolock lock(mLock);
721 if (checkPidAndHardware() != NO_ERROR) return 0;
722 return mHardware->getVideoBuffer(index);
723}
724
725status_t CameraService::Client::storeMetaDataInBuffers(bool enabled)
726{
727 LOG1("storeMetaDataInBuffers: %s", enabled? "true": "false");
728 Mutex::Autolock lock(mLock);
729 if (checkPidAndHardware() != NO_ERROR) {
730 return UNKNOWN_ERROR;
731 }
732 return mHardware->storeMetaDataInBuffers(enabled);
733}
734
Mathias Agopian65ab4712010-07-14 17:59:35 -0700735bool CameraService::Client::previewEnabled() {
736 LOG1("previewEnabled (pid %d)", getCallingPid());
737
738 Mutex::Autolock lock(mLock);
739 if (checkPidAndHardware() != NO_ERROR) return false;
740 return mHardware->previewEnabled();
741}
742
743bool CameraService::Client::recordingEnabled() {
744 LOG1("recordingEnabled (pid %d)", getCallingPid());
745
746 Mutex::Autolock lock(mLock);
747 if (checkPidAndHardware() != NO_ERROR) return false;
748 return mHardware->recordingEnabled();
749}
750
751status_t CameraService::Client::autoFocus() {
752 LOG1("autoFocus (pid %d)", getCallingPid());
753
754 Mutex::Autolock lock(mLock);
755 status_t result = checkPidAndHardware();
756 if (result != NO_ERROR) return result;
757
758 return mHardware->autoFocus();
759}
760
761status_t CameraService::Client::cancelAutoFocus() {
762 LOG1("cancelAutoFocus (pid %d)", getCallingPid());
763
764 Mutex::Autolock lock(mLock);
765 status_t result = checkPidAndHardware();
766 if (result != NO_ERROR) return result;
767
768 return mHardware->cancelAutoFocus();
769}
770
771// take a picture - image is returned in callback
772status_t CameraService::Client::takePicture() {
773 LOG1("takePicture (pid %d)", getCallingPid());
774
775 Mutex::Autolock lock(mLock);
776 status_t result = checkPidAndHardware();
777 if (result != NO_ERROR) return result;
778
779 enableMsgType(CAMERA_MSG_SHUTTER |
780 CAMERA_MSG_POSTVIEW_FRAME |
781 CAMERA_MSG_RAW_IMAGE |
782 CAMERA_MSG_COMPRESSED_IMAGE);
783
784 return mHardware->takePicture();
785}
786
787// set preview/capture parameters - key/value pairs
788status_t CameraService::Client::setParameters(const String8& params) {
789 LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
790
791 Mutex::Autolock lock(mLock);
792 status_t result = checkPidAndHardware();
793 if (result != NO_ERROR) return result;
794
795 CameraParameters p(params);
796 return mHardware->setParameters(p);
797}
798
799// get preview/capture parameters - key/value pairs
800String8 CameraService::Client::getParameters() const {
801 Mutex::Autolock lock(mLock);
802 if (checkPidAndHardware() != NO_ERROR) return String8();
803
804 String8 params(mHardware->getParameters().flatten());
805 LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
806 return params;
807}
808
Nipun Kwatrab5ca4612010-09-11 19:31:10 -0700809// enable shutter sound
810status_t CameraService::Client::enableShutterSound(bool enable) {
811 LOG1("enableShutterSound (pid %d)", getCallingPid());
812
813 status_t result = checkPidAndHardware();
814 if (result != NO_ERROR) return result;
815
816 if (enable) {
817 mPlayShutterSound = true;
818 return OK;
819 }
820
821 // Disabling shutter sound may not be allowed. In that case only
822 // allow the mediaserver process to disable the sound.
823 char value[PROPERTY_VALUE_MAX];
824 property_get("ro.camera.sound.forced", value, "0");
825 if (strcmp(value, "0") != 0) {
826 // Disabling shutter sound is not allowed. Deny if the current
827 // process is not mediaserver.
828 if (getCallingPid() != getpid()) {
829 LOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid());
830 return PERMISSION_DENIED;
831 }
832 }
833
834 mPlayShutterSound = false;
835 return OK;
836}
837
Mathias Agopian65ab4712010-07-14 17:59:35 -0700838status_t CameraService::Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
839 LOG1("sendCommand (pid %d)", getCallingPid());
Wu-cheng Li4a73f3d2010-09-23 17:17:43 -0700840 int orientation;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700841 Mutex::Autolock lock(mLock);
842 status_t result = checkPidAndHardware();
843 if (result != NO_ERROR) return result;
844
845 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
846 // The orientation cannot be set during preview.
847 if (mHardware->previewEnabled()) {
848 return INVALID_OPERATION;
849 }
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800850 // Mirror the preview if the camera is front-facing.
851 orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT);
852 if (orientation == -1) return BAD_VALUE;
853
Wu-cheng Li4a73f3d2010-09-23 17:17:43 -0700854 if (mOrientation != orientation) {
855 mOrientation = orientation;
856 if (mOverlayRef != 0) mOrientationChanged = true;
857 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700858 return OK;
Nipun Kwatrab5ca4612010-09-11 19:31:10 -0700859 } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) {
860 switch (arg1) {
861 case 0:
862 enableShutterSound(false);
863 break;
864 case 1:
865 enableShutterSound(true);
866 break;
867 default:
868 return BAD_VALUE;
869 }
870 return OK;
Nipun Kwatra3b7b3582010-09-14 16:49:08 -0700871 } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) {
872 mCameraService->playSound(SOUND_RECORDING);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700873 }
874
875 return mHardware->sendCommand(cmd, arg1, arg2);
876}
877
878// ----------------------------------------------------------------------------
879
880void CameraService::Client::enableMsgType(int32_t msgType) {
881 android_atomic_or(msgType, &mMsgEnabled);
882 mHardware->enableMsgType(msgType);
883}
884
885void CameraService::Client::disableMsgType(int32_t msgType) {
886 android_atomic_and(~msgType, &mMsgEnabled);
887 mHardware->disableMsgType(msgType);
888}
889
890#define CHECK_MESSAGE_INTERVAL 10 // 10ms
891bool CameraService::Client::lockIfMessageWanted(int32_t msgType) {
892 int sleepCount = 0;
893 while (mMsgEnabled & msgType) {
894 if (mLock.tryLock() == NO_ERROR) {
895 if (sleepCount > 0) {
896 LOG1("lockIfMessageWanted(%d): waited for %d ms",
897 msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
898 }
899 return true;
900 }
901 if (sleepCount++ == 0) {
902 LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
903 }
904 usleep(CHECK_MESSAGE_INTERVAL * 1000);
905 }
906 LOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
907 return false;
908}
909
910// ----------------------------------------------------------------------------
911
912// Converts from a raw pointer to the client to a strong pointer during a
913// hardware callback. This requires the callbacks only happen when the client
914// is still alive.
915sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user) {
916 sp<Client> client = gCameraService->getClientById((int) user);
917
918 // This could happen if the Client is in the process of shutting down (the
919 // last strong reference is gone, but the destructor hasn't finished
920 // stopping the hardware).
921 if (client == 0) return NULL;
922
923 // The checks below are not necessary and are for debugging only.
924 if (client->mCameraService.get() != gCameraService) {
925 LOGE("mismatch service!");
926 return NULL;
927 }
928
929 if (client->mHardware == 0) {
930 LOGE("mHardware == 0: callback after disconnect()?");
931 return NULL;
932 }
933
934 return client;
935}
936
937// Callback messages can be dispatched to internal handlers or pass to our
938// client's callback functions, depending on the message type.
939//
940// notifyCallback:
941// CAMERA_MSG_SHUTTER handleShutter
942// (others) c->notifyCallback
943// dataCallback:
944// CAMERA_MSG_PREVIEW_FRAME handlePreviewData
945// CAMERA_MSG_POSTVIEW_FRAME handlePostview
946// CAMERA_MSG_RAW_IMAGE handleRawPicture
947// CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture
948// (others) c->dataCallback
949// dataCallbackTimestamp
950// (others) c->dataCallbackTimestamp
951//
952// NOTE: the *Callback functions grab mLock of the client before passing
953// control to handle* functions. So the handle* functions must release the
954// lock before calling the ICameraClient's callbacks, so those callbacks can
955// invoke methods in the Client class again (For example, the preview frame
956// callback may want to releaseRecordingFrame). The handle* functions must
957// release the lock after all accesses to member variables, so it must be
958// handled very carefully.
959
960void CameraService::Client::notifyCallback(int32_t msgType, int32_t ext1,
961 int32_t ext2, void* user) {
962 LOG2("notifyCallback(%d)", msgType);
963
964 sp<Client> client = getClientFromCookie(user);
965 if (client == 0) return;
966 if (!client->lockIfMessageWanted(msgType)) return;
967
968 switch (msgType) {
969 case CAMERA_MSG_SHUTTER:
970 // ext1 is the dimension of the yuv picture.
971 client->handleShutter((image_rect_type *)ext1);
972 break;
973 default:
974 client->handleGenericNotify(msgType, ext1, ext2);
975 break;
976 }
977}
978
979void CameraService::Client::dataCallback(int32_t msgType,
980 const sp<IMemory>& dataPtr, void* user) {
981 LOG2("dataCallback(%d)", msgType);
982
983 sp<Client> client = getClientFromCookie(user);
984 if (client == 0) return;
985 if (!client->lockIfMessageWanted(msgType)) return;
986
987 if (dataPtr == 0) {
988 LOGE("Null data returned in data callback");
989 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
990 return;
991 }
992
993 switch (msgType) {
994 case CAMERA_MSG_PREVIEW_FRAME:
995 client->handlePreviewData(dataPtr);
996 break;
997 case CAMERA_MSG_POSTVIEW_FRAME:
998 client->handlePostview(dataPtr);
999 break;
1000 case CAMERA_MSG_RAW_IMAGE:
1001 client->handleRawPicture(dataPtr);
1002 break;
1003 case CAMERA_MSG_COMPRESSED_IMAGE:
1004 client->handleCompressedPicture(dataPtr);
1005 break;
1006 default:
1007 client->handleGenericData(msgType, dataPtr);
1008 break;
1009 }
1010}
1011
1012void CameraService::Client::dataCallbackTimestamp(nsecs_t timestamp,
1013 int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
1014 LOG2("dataCallbackTimestamp(%d)", msgType);
1015
1016 sp<Client> client = getClientFromCookie(user);
1017 if (client == 0) return;
1018 if (!client->lockIfMessageWanted(msgType)) return;
1019
1020 if (dataPtr == 0) {
1021 LOGE("Null data returned in data with timestamp callback");
1022 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
1023 return;
1024 }
1025
1026 client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
1027}
1028
1029// snapshot taken callback
1030// "size" is the width and height of yuv picture for registerBuffer.
1031// If it is NULL, use the picture size from parameters.
1032void CameraService::Client::handleShutter(image_rect_type *size) {
Nipun Kwatrab5ca4612010-09-11 19:31:10 -07001033 if (mPlayShutterSound) {
1034 mCameraService->playSound(SOUND_SHUTTER);
1035 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001036
Mathias Agopian65ab4712010-07-14 17:59:35 -07001037 sp<ICameraClient> c = mCameraClient;
1038 if (c != 0) {
1039 mLock.unlock();
1040 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
1041 if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
1042 }
1043 disableMsgType(CAMERA_MSG_SHUTTER);
1044
1045 // It takes some time before yuvPicture callback to be called.
1046 // Register the buffer for raw image here to reduce latency.
1047 if (mSurface != 0 && !mUseOverlay) {
1048 int w, h;
1049 CameraParameters params(mHardware->getParameters());
1050 if (size == NULL) {
1051 params.getPictureSize(&w, &h);
1052 } else {
1053 w = size->width;
1054 h = size->height;
1055 w &= ~1;
1056 h &= ~1;
1057 LOG1("Snapshot image width=%d, height=%d", w, h);
1058 }
1059 // FIXME: don't use hardcoded format constants here
1060 ISurface::BufferHeap buffers(w, h, w, h,
1061 HAL_PIXEL_FORMAT_YCrCb_420_SP, mOrientation, 0,
1062 mHardware->getRawHeap());
1063
Mathias Agopian65ab4712010-07-14 17:59:35 -07001064 IPCThreadState::self()->flushCommands();
1065 }
1066
1067 mLock.unlock();
1068}
1069
1070// preview callback - frame buffer update
1071void CameraService::Client::handlePreviewData(const sp<IMemory>& mem) {
1072 ssize_t offset;
1073 size_t size;
1074 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1075
Mathias Agopian65ab4712010-07-14 17:59:35 -07001076 // local copy of the callback flags
1077 int flags = mPreviewCallbackFlag;
1078
1079 // is callback enabled?
1080 if (!(flags & FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
1081 // If the enable bit is off, the copy-out and one-shot bits are ignored
1082 LOG2("frame callback is disabled");
1083 mLock.unlock();
1084 return;
1085 }
1086
1087 // hold a strong pointer to the client
1088 sp<ICameraClient> c = mCameraClient;
1089
1090 // clear callback flags if no client or one-shot mode
1091 if (c == 0 || (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
1092 LOG2("Disable preview callback");
1093 mPreviewCallbackFlag &= ~(FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
1094 FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
1095 FRAME_CALLBACK_FLAG_ENABLE_MASK);
Wu-cheng Li0667de72010-09-03 16:40:32 -07001096 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001097 }
1098
1099 if (c != 0) {
1100 // Is the received frame copied out or not?
1101 if (flags & FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
1102 LOG2("frame is copied");
1103 copyFrameAndPostCopiedFrame(c, heap, offset, size);
1104 } else {
1105 LOG2("frame is forwarded");
1106 mLock.unlock();
1107 c->dataCallback(CAMERA_MSG_PREVIEW_FRAME, mem);
1108 }
1109 } else {
1110 mLock.unlock();
1111 }
1112}
1113
1114// picture callback - postview image ready
1115void CameraService::Client::handlePostview(const sp<IMemory>& mem) {
1116 disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
1117
1118 sp<ICameraClient> c = mCameraClient;
1119 mLock.unlock();
1120 if (c != 0) {
1121 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem);
1122 }
1123}
1124
1125// picture callback - raw image ready
1126void CameraService::Client::handleRawPicture(const sp<IMemory>& mem) {
1127 disableMsgType(CAMERA_MSG_RAW_IMAGE);
1128
1129 ssize_t offset;
1130 size_t size;
1131 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1132
Mathias Agopian65ab4712010-07-14 17:59:35 -07001133 sp<ICameraClient> c = mCameraClient;
1134 mLock.unlock();
1135 if (c != 0) {
1136 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem);
1137 }
1138}
1139
1140// picture callback - compressed picture ready
1141void CameraService::Client::handleCompressedPicture(const sp<IMemory>& mem) {
1142 disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
1143
1144 sp<ICameraClient> c = mCameraClient;
1145 mLock.unlock();
1146 if (c != 0) {
1147 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem);
1148 }
1149}
1150
1151
1152void CameraService::Client::handleGenericNotify(int32_t msgType,
1153 int32_t ext1, int32_t ext2) {
1154 sp<ICameraClient> c = mCameraClient;
1155 mLock.unlock();
1156 if (c != 0) {
1157 c->notifyCallback(msgType, ext1, ext2);
1158 }
1159}
1160
1161void CameraService::Client::handleGenericData(int32_t msgType,
1162 const sp<IMemory>& dataPtr) {
1163 sp<ICameraClient> c = mCameraClient;
1164 mLock.unlock();
1165 if (c != 0) {
1166 c->dataCallback(msgType, dataPtr);
1167 }
1168}
1169
1170void CameraService::Client::handleGenericDataTimestamp(nsecs_t timestamp,
1171 int32_t msgType, const sp<IMemory>& dataPtr) {
1172 sp<ICameraClient> c = mCameraClient;
1173 mLock.unlock();
1174 if (c != 0) {
1175 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
1176 }
1177}
1178
1179void CameraService::Client::copyFrameAndPostCopiedFrame(
1180 const sp<ICameraClient>& client, const sp<IMemoryHeap>& heap,
1181 size_t offset, size_t size) {
1182 LOG2("copyFrameAndPostCopiedFrame");
1183 // It is necessary to copy out of pmem before sending this to
1184 // the callback. For efficiency, reuse the same MemoryHeapBase
1185 // provided it's big enough. Don't allocate the memory or
1186 // perform the copy if there's no callback.
1187 // hold the preview lock while we grab a reference to the preview buffer
1188 sp<MemoryHeapBase> previewBuffer;
1189
1190 if (mPreviewBuffer == 0) {
1191 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1192 } else if (size > mPreviewBuffer->virtualSize()) {
1193 mPreviewBuffer.clear();
1194 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1195 }
1196 if (mPreviewBuffer == 0) {
1197 LOGE("failed to allocate space for preview buffer");
1198 mLock.unlock();
1199 return;
1200 }
1201 previewBuffer = mPreviewBuffer;
1202
1203 memcpy(previewBuffer->base(), (uint8_t *)heap->base() + offset, size);
1204
1205 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
1206 if (frame == 0) {
1207 LOGE("failed to allocate space for frame callback");
1208 mLock.unlock();
1209 return;
1210 }
1211
1212 mLock.unlock();
1213 client->dataCallback(CAMERA_MSG_PREVIEW_FRAME, frame);
1214}
1215
Wu-cheng Lie09591e2010-10-14 20:17:44 +08001216int CameraService::Client::getOrientation(int degrees, bool mirror) {
1217 if (!mirror) {
1218 if (degrees == 0) return 0;
1219 else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
1220 else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
1221 else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
1222 } else { // Do mirror (horizontal flip)
1223 if (degrees == 0) { // FLIP_H and ROT_0
1224 return HAL_TRANSFORM_FLIP_H;
1225 } else if (degrees == 90) { // FLIP_H and ROT_90
1226 return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
1227 } else if (degrees == 180) { // FLIP_H and ROT_180
1228 return HAL_TRANSFORM_FLIP_V;
1229 } else if (degrees == 270) { // FLIP_H and ROT_270
1230 return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
1231 }
1232 }
1233 LOGE("Invalid setDisplayOrientation degrees=%d", degrees);
1234 return -1;
1235}
1236
1237
Mathias Agopian65ab4712010-07-14 17:59:35 -07001238// ----------------------------------------------------------------------------
1239
1240static const int kDumpLockRetries = 50;
1241static const int kDumpLockSleep = 60000;
1242
1243static bool tryLock(Mutex& mutex)
1244{
1245 bool locked = false;
1246 for (int i = 0; i < kDumpLockRetries; ++i) {
1247 if (mutex.tryLock() == NO_ERROR) {
1248 locked = true;
1249 break;
1250 }
1251 usleep(kDumpLockSleep);
1252 }
1253 return locked;
1254}
1255
1256status_t CameraService::dump(int fd, const Vector<String16>& args) {
1257 static const char* kDeadlockedString = "CameraService may be deadlocked\n";
1258
1259 const size_t SIZE = 256;
1260 char buffer[SIZE];
1261 String8 result;
1262 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
1263 snprintf(buffer, SIZE, "Permission Denial: "
1264 "can't dump CameraService from pid=%d, uid=%d\n",
1265 getCallingPid(),
1266 getCallingUid());
1267 result.append(buffer);
1268 write(fd, result.string(), result.size());
1269 } else {
1270 bool locked = tryLock(mServiceLock);
1271 // failed to lock - CameraService is probably deadlocked
1272 if (!locked) {
1273 String8 result(kDeadlockedString);
1274 write(fd, result.string(), result.size());
1275 }
1276
1277 bool hasClient = false;
1278 for (int i = 0; i < mNumberOfCameras; i++) {
1279 sp<Client> client = mClient[i].promote();
1280 if (client == 0) continue;
1281 hasClient = true;
1282 sprintf(buffer, "Client[%d] (%p) PID: %d\n",
1283 i,
1284 client->getCameraClient()->asBinder().get(),
1285 client->mClientPid);
1286 result.append(buffer);
1287 write(fd, result.string(), result.size());
1288 client->mHardware->dump(fd, args);
1289 }
1290 if (!hasClient) {
1291 result.append("No camera client yet.\n");
1292 write(fd, result.string(), result.size());
1293 }
1294
1295 if (locked) mServiceLock.unlock();
1296
1297 // change logging level
1298 int n = args.size();
1299 for (int i = 0; i + 1 < n; i++) {
1300 if (args[i] == String16("-v")) {
1301 String8 levelStr(args[i+1]);
1302 int level = atoi(levelStr.string());
1303 sprintf(buffer, "Set Log Level to %d", level);
1304 result.append(buffer);
1305 setLogLevel(level);
1306 }
1307 }
1308 }
1309 return NO_ERROR;
1310}
1311
Jamie Gennis4b791682010-08-10 16:37:53 -07001312sp<ISurface> CameraService::getISurface(const sp<Surface>& surface) {
1313 if (surface != 0) {
1314 return surface->getISurface();
1315 } else {
1316 return sp<ISurface>(0);
1317 }
1318}
1319
Mathias Agopian65ab4712010-07-14 17:59:35 -07001320}; // namespace android