blob: a09e16b3b412ac78c151ba43a837590a9a4dc073 [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>
Jamie Gennisbfa33aa2010-12-20 11:51:31 -080030#include <gui/SurfaceTextureClient.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070031#include <hardware/hardware.h>
32#include <media/AudioSystem.h>
33#include <media/mediaplayer.h>
34#include <surfaceflinger/ISurface.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070035#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 mMsgEnabled = 0;
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800310 mSurface = 0;
311 mPreviewWindow = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700312 mHardware->setCallbacks(notifyCallback,
313 dataCallback,
314 dataCallbackTimestamp,
315 (void *)cameraId);
316
317 // Enable zoom, error, and focus messages by default
318 enableMsgType(CAMERA_MSG_ERROR |
319 CAMERA_MSG_ZOOM |
320 CAMERA_MSG_FOCUS);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700321
322 // Callback is disabled by default
323 mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800324 mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT);
Nipun Kwatrab5ca4612010-09-11 19:31:10 -0700325 mPlayShutterSound = true;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700326 cameraService->setCameraBusy(cameraId);
327 cameraService->loadSound();
328 LOG1("Client::Client X (pid %d)", callingPid);
329}
330
Mathias Agopian65ab4712010-07-14 17:59:35 -0700331// tear down the client
332CameraService::Client::~Client() {
333 int callingPid = getCallingPid();
334 LOG1("Client::~Client E (pid %d, this %p)", callingPid, this);
335
Mathias Agopian65ab4712010-07-14 17:59:35 -0700336 // set mClientPid to let disconnet() tear down the hardware
337 mClientPid = callingPid;
338 disconnect();
339 mCameraService->releaseSound();
340 LOG1("Client::~Client X (pid %d, this %p)", callingPid, this);
341}
342
343// ----------------------------------------------------------------------------
344
345status_t CameraService::Client::checkPid() const {
346 int callingPid = getCallingPid();
347 if (callingPid == mClientPid) return NO_ERROR;
348
349 LOGW("attempt to use a locked camera from a different process"
350 " (old pid %d, new pid %d)", mClientPid, callingPid);
351 return EBUSY;
352}
353
354status_t CameraService::Client::checkPidAndHardware() const {
355 status_t result = checkPid();
356 if (result != NO_ERROR) return result;
357 if (mHardware == 0) {
358 LOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid());
359 return INVALID_OPERATION;
360 }
361 return NO_ERROR;
362}
363
364status_t CameraService::Client::lock() {
365 int callingPid = getCallingPid();
366 LOG1("lock (pid %d)", callingPid);
367 Mutex::Autolock lock(mLock);
368
369 // lock camera to this client if the the camera is unlocked
370 if (mClientPid == 0) {
371 mClientPid = callingPid;
372 return NO_ERROR;
373 }
374
375 // returns NO_ERROR if the client already owns the camera, EBUSY otherwise
376 return checkPid();
377}
378
379status_t CameraService::Client::unlock() {
380 int callingPid = getCallingPid();
381 LOG1("unlock (pid %d)", callingPid);
382 Mutex::Autolock lock(mLock);
383
384 // allow anyone to use camera (after they lock the camera)
385 status_t result = checkPid();
386 if (result == NO_ERROR) {
387 mClientPid = 0;
388 LOG1("clear mCameraClient (pid %d)", callingPid);
389 // we need to remove the reference to ICameraClient so that when the app
390 // goes away, the reference count goes to 0.
391 mCameraClient.clear();
392 }
393 return result;
394}
395
396// connect a new client to the camera
397status_t CameraService::Client::connect(const sp<ICameraClient>& client) {
398 int callingPid = getCallingPid();
399 LOG1("connect E (pid %d)", callingPid);
400 Mutex::Autolock lock(mLock);
401
402 if (mClientPid != 0 && checkPid() != NO_ERROR) {
403 LOGW("Tried to connect to a locked camera (old pid %d, new pid %d)",
404 mClientPid, callingPid);
405 return EBUSY;
406 }
407
408 if (mCameraClient != 0 && (client->asBinder() == mCameraClient->asBinder())) {
409 LOG1("Connect to the same client");
410 return NO_ERROR;
411 }
412
413 mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
414 mClientPid = callingPid;
415 mCameraClient = client;
416
417 LOG1("connect X (pid %d)", callingPid);
418 return NO_ERROR;
419}
420
421void CameraService::Client::disconnect() {
422 int callingPid = getCallingPid();
423 LOG1("disconnect E (pid %d)", callingPid);
424 Mutex::Autolock lock(mLock);
425
426 if (checkPid() != NO_ERROR) {
427 LOGW("different client - don't disconnect");
428 return;
429 }
430
431 if (mClientPid <= 0) {
432 LOG1("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
433 return;
434 }
435
436 // Make sure disconnect() is done once and once only, whether it is called
437 // from the user directly, or called by the destructor.
438 if (mHardware == 0) return;
439
440 LOG1("hardware teardown");
441 // Before destroying mHardware, we must make sure it's in the
442 // idle state.
443 // Turn off all messages.
444 disableMsgType(CAMERA_MSG_ALL_MSGS);
445 mHardware->stopPreview();
446 mHardware->cancelPicture();
447 // Release the hardware resources.
448 mHardware->release();
Mathias Agopian03dfce92010-12-07 19:38:17 -0800449
Jamie Gennis4b791682010-08-10 16:37:53 -0700450 // Release the held ANativeWindow resources.
451 if (mPreviewWindow != 0) {
452 mPreviewWindow = 0;
453 mHardware->setPreviewWindow(mPreviewWindow);
454 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700455 mHardware.clear();
456
457 mCameraService->removeClient(mCameraClient);
458 mCameraService->setCameraFree(mCameraId);
459
460 LOG1("disconnect X (pid %d)", callingPid);
461}
462
463// ----------------------------------------------------------------------------
464
Jamie Gennis4b791682010-08-10 16:37:53 -0700465// set the Surface that the preview will use
466status_t CameraService::Client::setPreviewDisplay(const sp<Surface>& surface) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700467 LOG1("setPreviewDisplay(%p) (pid %d)", surface.get(), getCallingPid());
468 Mutex::Autolock lock(mLock);
469 status_t result = checkPidAndHardware();
470 if (result != NO_ERROR) return result;
471
472 result = NO_ERROR;
473
474 // return if no change in surface.
475 // asBinder() is safe on NULL (returns NULL)
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800476 if (getISurface(surface)->asBinder() == mSurface) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700477 return result;
478 }
479
480 if (mSurface != 0) {
481 LOG1("clearing old preview surface %p", mSurface.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700482 }
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800483 mSurface = getISurface(surface)->asBinder();
Jamie Gennis4b791682010-08-10 16:37:53 -0700484 mPreviewWindow = surface;
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800485
Mathias Agopian03dfce92010-12-07 19:38:17 -0800486 // If preview has been already started, register preview
Mathias Agopian65ab4712010-07-14 17:59:35 -0700487 // buffers now.
488 if (mHardware->previewEnabled()) {
Mathias Agopian03dfce92010-12-07 19:38:17 -0800489 if (mPreviewWindow != 0) {
Wu-cheng Li012716a2010-10-08 22:04:43 +0800490 native_window_set_buffers_transform(mPreviewWindow.get(),
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800491 mOrientation);
Jamie Gennis4b791682010-08-10 16:37:53 -0700492 result = mHardware->setPreviewWindow(mPreviewWindow);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700493 }
494 }
495
496 return result;
497}
498
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800499// set the SurfaceTexture that the preview will use
500status_t CameraService::Client::setPreviewTexture(
501 const sp<ISurfaceTexture>& surfaceTexture) {
502 LOG1("setPreviewTexture(%p) (pid %d)", surfaceTexture.get(),
503 getCallingPid());
504 Mutex::Autolock lock(mLock);
505 status_t result = checkPidAndHardware();
506 if (result != NO_ERROR) return result;
507
508 // return if no change in surface.
509 // asBinder() is safe on NULL (returns NULL)
510 if (surfaceTexture->asBinder() == mSurface) {
511 return result;
512 }
513
514 if (mSurface != 0) {
515 LOG1("clearing old preview surface %p", mSurface.get());
516 }
517 mSurface = surfaceTexture->asBinder();
518 if (surfaceTexture != 0) {
519 mPreviewWindow = new SurfaceTextureClient(surfaceTexture);
520 } else {
521 mPreviewWindow = 0;
522 }
523
524 // If preview has been already started, set overlay or register preview
525 // buffers now.
526 if (mHardware->previewEnabled()) {
527 // XXX: What if the new preview window is 0?
528 if (mPreviewWindow != 0) {
529 native_window_set_buffers_transform(mPreviewWindow.get(),
530 mOrientation);
531 result = mHardware->setPreviewWindow(mPreviewWindow);
532 }
533 }
534
535 return result;
536}
537
Mathias Agopian65ab4712010-07-14 17:59:35 -0700538// set the preview callback flag to affect how the received frames from
539// preview are handled.
540void CameraService::Client::setPreviewCallbackFlag(int callback_flag) {
541 LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid());
542 Mutex::Autolock lock(mLock);
543 if (checkPidAndHardware() != NO_ERROR) return;
544
545 mPreviewCallbackFlag = callback_flag;
Wu-cheng Li0667de72010-09-03 16:40:32 -0700546 if (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ENABLE_MASK) {
547 enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
548 } else {
549 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700550 }
551}
552
553// start preview mode
554status_t CameraService::Client::startPreview() {
555 LOG1("startPreview (pid %d)", getCallingPid());
556 return startCameraMode(CAMERA_PREVIEW_MODE);
557}
558
559// start recording mode
560status_t CameraService::Client::startRecording() {
561 LOG1("startRecording (pid %d)", getCallingPid());
562 return startCameraMode(CAMERA_RECORDING_MODE);
563}
564
565// start preview or recording
566status_t CameraService::Client::startCameraMode(camera_mode mode) {
567 LOG1("startCameraMode(%d)", mode);
568 Mutex::Autolock lock(mLock);
569 status_t result = checkPidAndHardware();
570 if (result != NO_ERROR) return result;
571
572 switch(mode) {
573 case CAMERA_PREVIEW_MODE:
Jamie Gennis4b791682010-08-10 16:37:53 -0700574 if (mSurface == 0 && mPreviewWindow == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700575 LOG1("mSurface is not set yet.");
576 // still able to start preview in this case.
577 }
578 return startPreviewMode();
579 case CAMERA_RECORDING_MODE:
Jamie Gennis4b791682010-08-10 16:37:53 -0700580 if (mSurface == 0 && mPreviewWindow == 0) {
581 LOGE("mSurface or mPreviewWindow must be set before startRecordingMode.");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700582 return INVALID_OPERATION;
583 }
584 return startRecordingMode();
585 default:
586 return UNKNOWN_ERROR;
587 }
588}
589
590status_t CameraService::Client::startPreviewMode() {
591 LOG1("startPreviewMode");
592 status_t result = NO_ERROR;
593
594 // if preview has been enabled, nothing needs to be done
595 if (mHardware->previewEnabled()) {
596 return NO_ERROR;
597 }
598
Mathias Agopian03dfce92010-12-07 19:38:17 -0800599 if (mPreviewWindow != 0) {
600 native_window_set_buffers_transform(mPreviewWindow.get(),
601 mOrientation);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700602 }
Mathias Agopian03dfce92010-12-07 19:38:17 -0800603 mHardware->setPreviewWindow(mPreviewWindow);
604 result = mHardware->startPreview();
605
Mathias Agopian65ab4712010-07-14 17:59:35 -0700606 return result;
607}
608
609status_t CameraService::Client::startRecordingMode() {
610 LOG1("startRecordingMode");
611 status_t result = NO_ERROR;
612
613 // if recording has been enabled, nothing needs to be done
614 if (mHardware->recordingEnabled()) {
615 return NO_ERROR;
616 }
617
618 // if preview has not been started, start preview first
619 if (!mHardware->previewEnabled()) {
620 result = startPreviewMode();
621 if (result != NO_ERROR) {
622 return result;
623 }
624 }
625
626 // start recording mode
627 enableMsgType(CAMERA_MSG_VIDEO_FRAME);
628 mCameraService->playSound(SOUND_RECORDING);
629 result = mHardware->startRecording();
630 if (result != NO_ERROR) {
631 LOGE("mHardware->startRecording() failed with status %d", result);
632 }
633 return result;
634}
635
636// stop preview mode
637void CameraService::Client::stopPreview() {
638 LOG1("stopPreview (pid %d)", getCallingPid());
639 Mutex::Autolock lock(mLock);
640 if (checkPidAndHardware() != NO_ERROR) return;
641
Jamie Gennis4b791682010-08-10 16:37:53 -0700642
Mathias Agopian65ab4712010-07-14 17:59:35 -0700643 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
644 mHardware->stopPreview();
645
Mathias Agopian65ab4712010-07-14 17:59:35 -0700646 mPreviewBuffer.clear();
647}
648
649// stop recording mode
650void CameraService::Client::stopRecording() {
651 LOG1("stopRecording (pid %d)", getCallingPid());
652 Mutex::Autolock lock(mLock);
653 if (checkPidAndHardware() != NO_ERROR) return;
654
655 mCameraService->playSound(SOUND_RECORDING);
656 disableMsgType(CAMERA_MSG_VIDEO_FRAME);
657 mHardware->stopRecording();
658
659 mPreviewBuffer.clear();
660}
661
662// release a recording frame
663void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem) {
664 Mutex::Autolock lock(mLock);
665 if (checkPidAndHardware() != NO_ERROR) return;
666 mHardware->releaseRecordingFrame(mem);
667}
668
James Donge2ad6732010-10-18 20:42:51 -0700669int32_t CameraService::Client::getNumberOfVideoBuffers() const {
670 LOG1("getNumberOfVideoBuffers");
671 Mutex::Autolock lock(mLock);
672 if (checkPidAndHardware() != NO_ERROR) return 0;
673 return mHardware->getNumberOfVideoBuffers();
674}
675
676sp<IMemory> CameraService::Client::getVideoBuffer(int32_t index) const {
677 LOG1("getVideoBuffer: %d", index);
678 Mutex::Autolock lock(mLock);
679 if (checkPidAndHardware() != NO_ERROR) return 0;
680 return mHardware->getVideoBuffer(index);
681}
682
683status_t CameraService::Client::storeMetaDataInBuffers(bool enabled)
684{
685 LOG1("storeMetaDataInBuffers: %s", enabled? "true": "false");
686 Mutex::Autolock lock(mLock);
687 if (checkPidAndHardware() != NO_ERROR) {
688 return UNKNOWN_ERROR;
689 }
690 return mHardware->storeMetaDataInBuffers(enabled);
691}
692
Mathias Agopian65ab4712010-07-14 17:59:35 -0700693bool CameraService::Client::previewEnabled() {
694 LOG1("previewEnabled (pid %d)", getCallingPid());
695
696 Mutex::Autolock lock(mLock);
697 if (checkPidAndHardware() != NO_ERROR) return false;
698 return mHardware->previewEnabled();
699}
700
701bool CameraService::Client::recordingEnabled() {
702 LOG1("recordingEnabled (pid %d)", getCallingPid());
703
704 Mutex::Autolock lock(mLock);
705 if (checkPidAndHardware() != NO_ERROR) return false;
706 return mHardware->recordingEnabled();
707}
708
709status_t CameraService::Client::autoFocus() {
710 LOG1("autoFocus (pid %d)", getCallingPid());
711
712 Mutex::Autolock lock(mLock);
713 status_t result = checkPidAndHardware();
714 if (result != NO_ERROR) return result;
715
716 return mHardware->autoFocus();
717}
718
719status_t CameraService::Client::cancelAutoFocus() {
720 LOG1("cancelAutoFocus (pid %d)", getCallingPid());
721
722 Mutex::Autolock lock(mLock);
723 status_t result = checkPidAndHardware();
724 if (result != NO_ERROR) return result;
725
726 return mHardware->cancelAutoFocus();
727}
728
729// take a picture - image is returned in callback
James Donge468ac52011-02-17 16:38:06 -0800730status_t CameraService::Client::takePicture(int msgType) {
731 LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700732
733 Mutex::Autolock lock(mLock);
734 status_t result = checkPidAndHardware();
735 if (result != NO_ERROR) return result;
736
James Donge468ac52011-02-17 16:38:06 -0800737 if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
738 (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
739 LOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
740 " cannot be both enabled");
741 return BAD_VALUE;
742 }
743
744 // We only accept picture related message types
745 // and ignore other types of messages for takePicture().
746 int picMsgType = msgType
747 & (CAMERA_MSG_SHUTTER |
748 CAMERA_MSG_POSTVIEW_FRAME |
749 CAMERA_MSG_RAW_IMAGE |
750 CAMERA_MSG_RAW_IMAGE_NOTIFY |
751 CAMERA_MSG_COMPRESSED_IMAGE);
752
753 enableMsgType(picMsgType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700754
755 return mHardware->takePicture();
756}
757
758// set preview/capture parameters - key/value pairs
759status_t CameraService::Client::setParameters(const String8& params) {
760 LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
761
762 Mutex::Autolock lock(mLock);
763 status_t result = checkPidAndHardware();
764 if (result != NO_ERROR) return result;
765
766 CameraParameters p(params);
767 return mHardware->setParameters(p);
768}
769
770// get preview/capture parameters - key/value pairs
771String8 CameraService::Client::getParameters() const {
772 Mutex::Autolock lock(mLock);
773 if (checkPidAndHardware() != NO_ERROR) return String8();
774
775 String8 params(mHardware->getParameters().flatten());
776 LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
777 return params;
778}
779
Nipun Kwatrab5ca4612010-09-11 19:31:10 -0700780// enable shutter sound
781status_t CameraService::Client::enableShutterSound(bool enable) {
782 LOG1("enableShutterSound (pid %d)", getCallingPid());
783
784 status_t result = checkPidAndHardware();
785 if (result != NO_ERROR) return result;
786
787 if (enable) {
788 mPlayShutterSound = true;
789 return OK;
790 }
791
792 // Disabling shutter sound may not be allowed. In that case only
793 // allow the mediaserver process to disable the sound.
794 char value[PROPERTY_VALUE_MAX];
795 property_get("ro.camera.sound.forced", value, "0");
796 if (strcmp(value, "0") != 0) {
797 // Disabling shutter sound is not allowed. Deny if the current
798 // process is not mediaserver.
799 if (getCallingPid() != getpid()) {
800 LOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid());
801 return PERMISSION_DENIED;
802 }
803 }
804
805 mPlayShutterSound = false;
806 return OK;
807}
808
Mathias Agopian65ab4712010-07-14 17:59:35 -0700809status_t CameraService::Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
810 LOG1("sendCommand (pid %d)", getCallingPid());
Wu-cheng Li4a73f3d2010-09-23 17:17:43 -0700811 int orientation;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700812 Mutex::Autolock lock(mLock);
813 status_t result = checkPidAndHardware();
814 if (result != NO_ERROR) return result;
815
816 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
817 // The orientation cannot be set during preview.
818 if (mHardware->previewEnabled()) {
819 return INVALID_OPERATION;
820 }
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800821 // Mirror the preview if the camera is front-facing.
822 orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT);
823 if (orientation == -1) return BAD_VALUE;
824
Wu-cheng Li4a73f3d2010-09-23 17:17:43 -0700825 if (mOrientation != orientation) {
826 mOrientation = orientation;
Wu-cheng Li4a73f3d2010-09-23 17:17:43 -0700827 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700828 return OK;
Nipun Kwatrab5ca4612010-09-11 19:31:10 -0700829 } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) {
830 switch (arg1) {
831 case 0:
832 enableShutterSound(false);
833 break;
834 case 1:
835 enableShutterSound(true);
836 break;
837 default:
838 return BAD_VALUE;
839 }
840 return OK;
Nipun Kwatra3b7b3582010-09-14 16:49:08 -0700841 } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) {
842 mCameraService->playSound(SOUND_RECORDING);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700843 }
844
845 return mHardware->sendCommand(cmd, arg1, arg2);
846}
847
848// ----------------------------------------------------------------------------
849
850void CameraService::Client::enableMsgType(int32_t msgType) {
851 android_atomic_or(msgType, &mMsgEnabled);
852 mHardware->enableMsgType(msgType);
853}
854
855void CameraService::Client::disableMsgType(int32_t msgType) {
856 android_atomic_and(~msgType, &mMsgEnabled);
857 mHardware->disableMsgType(msgType);
858}
859
860#define CHECK_MESSAGE_INTERVAL 10 // 10ms
861bool CameraService::Client::lockIfMessageWanted(int32_t msgType) {
862 int sleepCount = 0;
863 while (mMsgEnabled & msgType) {
864 if (mLock.tryLock() == NO_ERROR) {
865 if (sleepCount > 0) {
866 LOG1("lockIfMessageWanted(%d): waited for %d ms",
867 msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
868 }
869 return true;
870 }
871 if (sleepCount++ == 0) {
872 LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
873 }
874 usleep(CHECK_MESSAGE_INTERVAL * 1000);
875 }
876 LOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
877 return false;
878}
879
880// ----------------------------------------------------------------------------
881
882// Converts from a raw pointer to the client to a strong pointer during a
883// hardware callback. This requires the callbacks only happen when the client
884// is still alive.
885sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user) {
886 sp<Client> client = gCameraService->getClientById((int) user);
887
888 // This could happen if the Client is in the process of shutting down (the
889 // last strong reference is gone, but the destructor hasn't finished
890 // stopping the hardware).
891 if (client == 0) return NULL;
892
893 // The checks below are not necessary and are for debugging only.
894 if (client->mCameraService.get() != gCameraService) {
895 LOGE("mismatch service!");
896 return NULL;
897 }
898
899 if (client->mHardware == 0) {
900 LOGE("mHardware == 0: callback after disconnect()?");
901 return NULL;
902 }
903
904 return client;
905}
906
907// Callback messages can be dispatched to internal handlers or pass to our
908// client's callback functions, depending on the message type.
909//
910// notifyCallback:
911// CAMERA_MSG_SHUTTER handleShutter
912// (others) c->notifyCallback
913// dataCallback:
914// CAMERA_MSG_PREVIEW_FRAME handlePreviewData
915// CAMERA_MSG_POSTVIEW_FRAME handlePostview
916// CAMERA_MSG_RAW_IMAGE handleRawPicture
917// CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture
918// (others) c->dataCallback
919// dataCallbackTimestamp
920// (others) c->dataCallbackTimestamp
921//
922// NOTE: the *Callback functions grab mLock of the client before passing
923// control to handle* functions. So the handle* functions must release the
924// lock before calling the ICameraClient's callbacks, so those callbacks can
925// invoke methods in the Client class again (For example, the preview frame
926// callback may want to releaseRecordingFrame). The handle* functions must
927// release the lock after all accesses to member variables, so it must be
928// handled very carefully.
929
930void CameraService::Client::notifyCallback(int32_t msgType, int32_t ext1,
931 int32_t ext2, void* user) {
932 LOG2("notifyCallback(%d)", msgType);
933
934 sp<Client> client = getClientFromCookie(user);
935 if (client == 0) return;
936 if (!client->lockIfMessageWanted(msgType)) return;
937
938 switch (msgType) {
939 case CAMERA_MSG_SHUTTER:
940 // ext1 is the dimension of the yuv picture.
941 client->handleShutter((image_rect_type *)ext1);
942 break;
943 default:
944 client->handleGenericNotify(msgType, ext1, ext2);
945 break;
946 }
947}
948
949void CameraService::Client::dataCallback(int32_t msgType,
950 const sp<IMemory>& dataPtr, void* user) {
951 LOG2("dataCallback(%d)", msgType);
952
953 sp<Client> client = getClientFromCookie(user);
954 if (client == 0) return;
955 if (!client->lockIfMessageWanted(msgType)) return;
956
957 if (dataPtr == 0) {
958 LOGE("Null data returned in data callback");
959 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
960 return;
961 }
962
963 switch (msgType) {
964 case CAMERA_MSG_PREVIEW_FRAME:
965 client->handlePreviewData(dataPtr);
966 break;
967 case CAMERA_MSG_POSTVIEW_FRAME:
968 client->handlePostview(dataPtr);
969 break;
970 case CAMERA_MSG_RAW_IMAGE:
971 client->handleRawPicture(dataPtr);
972 break;
973 case CAMERA_MSG_COMPRESSED_IMAGE:
974 client->handleCompressedPicture(dataPtr);
975 break;
976 default:
977 client->handleGenericData(msgType, dataPtr);
978 break;
979 }
980}
981
982void CameraService::Client::dataCallbackTimestamp(nsecs_t timestamp,
983 int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
984 LOG2("dataCallbackTimestamp(%d)", msgType);
985
986 sp<Client> client = getClientFromCookie(user);
987 if (client == 0) return;
James Dong986ef2a2010-12-09 11:08:14 -0800988 if (!client->lockIfMessageWanted(msgType)) return;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700989
990 if (dataPtr == 0) {
991 LOGE("Null data returned in data with timestamp callback");
992 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
993 return;
994 }
995
996 client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
997}
998
999// snapshot taken callback
1000// "size" is the width and height of yuv picture for registerBuffer.
1001// If it is NULL, use the picture size from parameters.
1002void CameraService::Client::handleShutter(image_rect_type *size) {
Nipun Kwatrab5ca4612010-09-11 19:31:10 -07001003 if (mPlayShutterSound) {
1004 mCameraService->playSound(SOUND_SHUTTER);
1005 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001006
Mathias Agopian65ab4712010-07-14 17:59:35 -07001007 sp<ICameraClient> c = mCameraClient;
1008 if (c != 0) {
1009 mLock.unlock();
1010 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
1011 if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
1012 }
1013 disableMsgType(CAMERA_MSG_SHUTTER);
1014
Mathias Agopian65ab4712010-07-14 17:59:35 -07001015 mLock.unlock();
1016}
1017
1018// preview callback - frame buffer update
1019void CameraService::Client::handlePreviewData(const sp<IMemory>& mem) {
1020 ssize_t offset;
1021 size_t size;
1022 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1023
Mathias Agopian65ab4712010-07-14 17:59:35 -07001024 // local copy of the callback flags
1025 int flags = mPreviewCallbackFlag;
1026
1027 // is callback enabled?
1028 if (!(flags & FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
1029 // If the enable bit is off, the copy-out and one-shot bits are ignored
1030 LOG2("frame callback is disabled");
1031 mLock.unlock();
1032 return;
1033 }
1034
1035 // hold a strong pointer to the client
1036 sp<ICameraClient> c = mCameraClient;
1037
1038 // clear callback flags if no client or one-shot mode
1039 if (c == 0 || (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
1040 LOG2("Disable preview callback");
1041 mPreviewCallbackFlag &= ~(FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
1042 FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
1043 FRAME_CALLBACK_FLAG_ENABLE_MASK);
Wu-cheng Li0667de72010-09-03 16:40:32 -07001044 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001045 }
1046
1047 if (c != 0) {
1048 // Is the received frame copied out or not?
1049 if (flags & FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
1050 LOG2("frame is copied");
1051 copyFrameAndPostCopiedFrame(c, heap, offset, size);
1052 } else {
1053 LOG2("frame is forwarded");
1054 mLock.unlock();
1055 c->dataCallback(CAMERA_MSG_PREVIEW_FRAME, mem);
1056 }
1057 } else {
1058 mLock.unlock();
1059 }
1060}
1061
1062// picture callback - postview image ready
1063void CameraService::Client::handlePostview(const sp<IMemory>& mem) {
1064 disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
1065
1066 sp<ICameraClient> c = mCameraClient;
1067 mLock.unlock();
1068 if (c != 0) {
1069 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem);
1070 }
1071}
1072
1073// picture callback - raw image ready
1074void CameraService::Client::handleRawPicture(const sp<IMemory>& mem) {
1075 disableMsgType(CAMERA_MSG_RAW_IMAGE);
1076
1077 ssize_t offset;
1078 size_t size;
1079 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1080
Mathias Agopian65ab4712010-07-14 17:59:35 -07001081 sp<ICameraClient> c = mCameraClient;
1082 mLock.unlock();
1083 if (c != 0) {
1084 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem);
1085 }
1086}
1087
1088// picture callback - compressed picture ready
1089void CameraService::Client::handleCompressedPicture(const sp<IMemory>& mem) {
1090 disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
1091
1092 sp<ICameraClient> c = mCameraClient;
1093 mLock.unlock();
1094 if (c != 0) {
1095 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem);
1096 }
1097}
1098
1099
1100void CameraService::Client::handleGenericNotify(int32_t msgType,
1101 int32_t ext1, int32_t ext2) {
1102 sp<ICameraClient> c = mCameraClient;
1103 mLock.unlock();
1104 if (c != 0) {
1105 c->notifyCallback(msgType, ext1, ext2);
1106 }
1107}
1108
1109void CameraService::Client::handleGenericData(int32_t msgType,
1110 const sp<IMemory>& dataPtr) {
1111 sp<ICameraClient> c = mCameraClient;
1112 mLock.unlock();
1113 if (c != 0) {
1114 c->dataCallback(msgType, dataPtr);
1115 }
1116}
1117
1118void CameraService::Client::handleGenericDataTimestamp(nsecs_t timestamp,
1119 int32_t msgType, const sp<IMemory>& dataPtr) {
1120 sp<ICameraClient> c = mCameraClient;
1121 mLock.unlock();
1122 if (c != 0) {
1123 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
1124 }
1125}
1126
1127void CameraService::Client::copyFrameAndPostCopiedFrame(
1128 const sp<ICameraClient>& client, const sp<IMemoryHeap>& heap,
1129 size_t offset, size_t size) {
1130 LOG2("copyFrameAndPostCopiedFrame");
1131 // It is necessary to copy out of pmem before sending this to
1132 // the callback. For efficiency, reuse the same MemoryHeapBase
1133 // provided it's big enough. Don't allocate the memory or
1134 // perform the copy if there's no callback.
1135 // hold the preview lock while we grab a reference to the preview buffer
1136 sp<MemoryHeapBase> previewBuffer;
1137
1138 if (mPreviewBuffer == 0) {
1139 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1140 } else if (size > mPreviewBuffer->virtualSize()) {
1141 mPreviewBuffer.clear();
1142 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1143 }
1144 if (mPreviewBuffer == 0) {
1145 LOGE("failed to allocate space for preview buffer");
1146 mLock.unlock();
1147 return;
1148 }
1149 previewBuffer = mPreviewBuffer;
1150
1151 memcpy(previewBuffer->base(), (uint8_t *)heap->base() + offset, size);
1152
1153 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
1154 if (frame == 0) {
1155 LOGE("failed to allocate space for frame callback");
1156 mLock.unlock();
1157 return;
1158 }
1159
1160 mLock.unlock();
1161 client->dataCallback(CAMERA_MSG_PREVIEW_FRAME, frame);
1162}
1163
Wu-cheng Lie09591e2010-10-14 20:17:44 +08001164int CameraService::Client::getOrientation(int degrees, bool mirror) {
1165 if (!mirror) {
1166 if (degrees == 0) return 0;
1167 else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
1168 else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
1169 else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
1170 } else { // Do mirror (horizontal flip)
1171 if (degrees == 0) { // FLIP_H and ROT_0
1172 return HAL_TRANSFORM_FLIP_H;
1173 } else if (degrees == 90) { // FLIP_H and ROT_90
1174 return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
1175 } else if (degrees == 180) { // FLIP_H and ROT_180
1176 return HAL_TRANSFORM_FLIP_V;
1177 } else if (degrees == 270) { // FLIP_H and ROT_270
1178 return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
1179 }
1180 }
1181 LOGE("Invalid setDisplayOrientation degrees=%d", degrees);
1182 return -1;
1183}
1184
1185
Mathias Agopian65ab4712010-07-14 17:59:35 -07001186// ----------------------------------------------------------------------------
1187
1188static const int kDumpLockRetries = 50;
1189static const int kDumpLockSleep = 60000;
1190
1191static bool tryLock(Mutex& mutex)
1192{
1193 bool locked = false;
1194 for (int i = 0; i < kDumpLockRetries; ++i) {
1195 if (mutex.tryLock() == NO_ERROR) {
1196 locked = true;
1197 break;
1198 }
1199 usleep(kDumpLockSleep);
1200 }
1201 return locked;
1202}
1203
1204status_t CameraService::dump(int fd, const Vector<String16>& args) {
1205 static const char* kDeadlockedString = "CameraService may be deadlocked\n";
1206
1207 const size_t SIZE = 256;
1208 char buffer[SIZE];
1209 String8 result;
1210 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
1211 snprintf(buffer, SIZE, "Permission Denial: "
1212 "can't dump CameraService from pid=%d, uid=%d\n",
1213 getCallingPid(),
1214 getCallingUid());
1215 result.append(buffer);
1216 write(fd, result.string(), result.size());
1217 } else {
1218 bool locked = tryLock(mServiceLock);
1219 // failed to lock - CameraService is probably deadlocked
1220 if (!locked) {
1221 String8 result(kDeadlockedString);
1222 write(fd, result.string(), result.size());
1223 }
1224
1225 bool hasClient = false;
1226 for (int i = 0; i < mNumberOfCameras; i++) {
1227 sp<Client> client = mClient[i].promote();
1228 if (client == 0) continue;
1229 hasClient = true;
1230 sprintf(buffer, "Client[%d] (%p) PID: %d\n",
1231 i,
1232 client->getCameraClient()->asBinder().get(),
1233 client->mClientPid);
1234 result.append(buffer);
1235 write(fd, result.string(), result.size());
1236 client->mHardware->dump(fd, args);
1237 }
1238 if (!hasClient) {
1239 result.append("No camera client yet.\n");
1240 write(fd, result.string(), result.size());
1241 }
1242
1243 if (locked) mServiceLock.unlock();
1244
1245 // change logging level
1246 int n = args.size();
1247 for (int i = 0; i + 1 < n; i++) {
1248 if (args[i] == String16("-v")) {
1249 String8 levelStr(args[i+1]);
1250 int level = atoi(levelStr.string());
1251 sprintf(buffer, "Set Log Level to %d", level);
1252 result.append(buffer);
1253 setLogLevel(level);
1254 }
1255 }
1256 }
1257 return NO_ERROR;
1258}
1259
Jamie Gennis4b791682010-08-10 16:37:53 -07001260sp<ISurface> CameraService::getISurface(const sp<Surface>& surface) {
1261 if (surface != 0) {
1262 return surface->getISurface();
1263 } else {
1264 return sp<ISurface>(0);
1265 }
1266}
1267
Mathias Agopian65ab4712010-07-14 17:59:35 -07001268}; // namespace android