blob: a011ae2fe357f2fe995e4de910052929800c52d4 [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"
Iliyan Malchev8951a972011-04-14 16:55:59 -070019//#define LOG_NDEBUG 0
Mathias Agopian65ab4712010-07-14 17:59:35 -070020
21#include <stdio.h>
22#include <sys/types.h>
23#include <pthread.h>
24
25#include <binder/IPCThreadState.h>
26#include <binder/IServiceManager.h>
27#include <binder/MemoryBase.h>
28#include <binder/MemoryHeapBase.h>
29#include <cutils/atomic.h>
Nipun Kwatrab5ca4612010-09-11 19:31:10 -070030#include <cutils/properties.h>
Jamie Gennisbfa33aa2010-12-20 11:51:31 -080031#include <gui/SurfaceTextureClient.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070032#include <hardware/hardware.h>
33#include <media/AudioSystem.h>
34#include <media/mediaplayer.h>
35#include <surfaceflinger/ISurface.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070036#include <utils/Errors.h>
37#include <utils/Log.h>
38#include <utils/String16.h>
39
40#include "CameraService.h"
Iliyan Malchev8951a972011-04-14 16:55:59 -070041#include "CameraHardwareInterface.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070042
43namespace android {
44
45// ----------------------------------------------------------------------------
46// Logging support -- this is for debugging only
47// Use "adb shell dumpsys media.camera -v 1" to change it.
48static volatile int32_t gLogLevel = 0;
49
50#define LOG1(...) LOGD_IF(gLogLevel >= 1, __VA_ARGS__);
51#define LOG2(...) LOGD_IF(gLogLevel >= 2, __VA_ARGS__);
52
53static void setLogLevel(int level) {
54 android_atomic_write(level, &gLogLevel);
55}
56
57// ----------------------------------------------------------------------------
58
59static int getCallingPid() {
60 return IPCThreadState::self()->getCallingPid();
61}
62
63static int getCallingUid() {
64 return IPCThreadState::self()->getCallingUid();
65}
66
67// ----------------------------------------------------------------------------
68
69// This is ugly and only safe if we never re-create the CameraService, but
70// should be ok for now.
71static CameraService *gCameraService;
72
73CameraService::CameraService()
Iliyan Malchev8951a972011-04-14 16:55:59 -070074:mSoundRef(0), mModule(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -070075{
76 LOGI("CameraService started (pid=%d)", getpid());
Mathias Agopian65ab4712010-07-14 17:59:35 -070077 gCameraService = this;
78}
79
Iliyan Malchev8951a972011-04-14 16:55:59 -070080void CameraService::onFirstRef()
81{
82 BnCameraService::onFirstRef();
83
84 if (hw_get_module(CAMERA_HARDWARE_MODULE_ID,
85 (const hw_module_t **)&mModule) < 0) {
86 LOGE("Could not load camera HAL module");
87 mNumberOfCameras = 0;
88 }
89 else {
90 mNumberOfCameras = mModule->get_number_of_cameras();
91 if (mNumberOfCameras > MAX_CAMERAS) {
92 LOGE("Number of cameras(%d) > MAX_CAMERAS(%d).",
93 mNumberOfCameras, MAX_CAMERAS);
94 mNumberOfCameras = MAX_CAMERAS;
95 }
96 for (int i = 0; i < mNumberOfCameras; i++) {
97 setCameraFree(i);
98 }
99 }
100}
101
Mathias Agopian65ab4712010-07-14 17:59:35 -0700102CameraService::~CameraService() {
103 for (int i = 0; i < mNumberOfCameras; i++) {
104 if (mBusy[i]) {
105 LOGE("camera %d is still in use in destructor!", i);
106 }
107 }
108
109 gCameraService = NULL;
110}
111
112int32_t CameraService::getNumberOfCameras() {
113 return mNumberOfCameras;
114}
115
116status_t CameraService::getCameraInfo(int cameraId,
117 struct CameraInfo* cameraInfo) {
Iliyan Malchev8951a972011-04-14 16:55:59 -0700118 if (!mModule) {
119 return NO_INIT;
120 }
121
Mathias Agopian65ab4712010-07-14 17:59:35 -0700122 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
123 return BAD_VALUE;
124 }
125
Iliyan Malchev8951a972011-04-14 16:55:59 -0700126 struct camera_info info;
127 status_t rc = mModule->get_camera_info(cameraId, &info);
128 cameraInfo->facing = info.facing;
129 cameraInfo->orientation = info.orientation;
130 return rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700131}
132
133sp<ICamera> CameraService::connect(
134 const sp<ICameraClient>& cameraClient, int cameraId) {
135 int callingPid = getCallingPid();
136 LOG1("CameraService::connect E (pid %d, id %d)", callingPid, cameraId);
137
Iliyan Malchev8951a972011-04-14 16:55:59 -0700138 if (!mModule) {
139 LOGE("Camera HAL module not loaded");
140 return NULL;
141 }
142
Mathias Agopian65ab4712010-07-14 17:59:35 -0700143 sp<Client> client;
144 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
145 LOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).",
146 callingPid, cameraId);
147 return NULL;
148 }
149
Wu-cheng Lia3355432011-05-20 14:54:25 +0800150 char value[PROPERTY_VALUE_MAX];
151 property_get("sys.secpolicy.camera.disabled", value, "0");
152 if (strcmp(value, "1") == 0) {
153 // Camera is disabled by DevicePolicyManager.
154 LOGI("Camera is disabled. connect X (pid %d) rejected", callingPid);
155 return NULL;
156 }
157
Mathias Agopian65ab4712010-07-14 17:59:35 -0700158 Mutex::Autolock lock(mServiceLock);
159 if (mClient[cameraId] != 0) {
160 client = mClient[cameraId].promote();
161 if (client != 0) {
162 if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) {
163 LOG1("CameraService::connect X (pid %d) (the same client)",
164 callingPid);
165 return client;
166 } else {
167 LOGW("CameraService::connect X (pid %d) rejected (existing client).",
168 callingPid);
169 return NULL;
170 }
171 }
172 mClient[cameraId].clear();
173 }
174
175 if (mBusy[cameraId]) {
176 LOGW("CameraService::connect X (pid %d) rejected"
177 " (camera %d is still busy).", callingPid, cameraId);
178 return NULL;
179 }
180
Iliyan Malchev8951a972011-04-14 16:55:59 -0700181 struct camera_info info;
182 if (mModule->get_camera_info(cameraId, &info) != OK) {
183 LOGE("Invalid camera id %d", cameraId);
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700184 return NULL;
185 }
Iliyan Malchev8951a972011-04-14 16:55:59 -0700186
187 char camera_device_name[10];
188 snprintf(camera_device_name, sizeof(camera_device_name), "%d", cameraId);
189
190 client = new Client(this, cameraClient,
191 new CameraHardwareInterface(&mModule->common,
192 camera_device_name),
193 cameraId, info.facing, callingPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700194 mClient[cameraId] = client;
195 LOG1("CameraService::connect X");
196 return client;
197}
198
199void CameraService::removeClient(const sp<ICameraClient>& cameraClient) {
200 int callingPid = getCallingPid();
201 LOG1("CameraService::removeClient E (pid %d)", callingPid);
202
203 for (int i = 0; i < mNumberOfCameras; i++) {
204 // Declare this before the lock to make absolutely sure the
205 // destructor won't be called with the lock held.
206 sp<Client> client;
207
208 Mutex::Autolock lock(mServiceLock);
209
210 // This happens when we have already disconnected (or this is
211 // just another unused camera).
212 if (mClient[i] == 0) continue;
213
214 // Promote mClient. It can fail if we are called from this path:
215 // Client::~Client() -> disconnect() -> removeClient().
216 client = mClient[i].promote();
217
218 if (client == 0) {
219 mClient[i].clear();
220 continue;
221 }
222
223 if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) {
224 // Found our camera, clear and leave.
225 LOG1("removeClient: clear camera %d", i);
226 mClient[i].clear();
227 break;
228 }
229 }
230
231 LOG1("CameraService::removeClient X (pid %d)", callingPid);
232}
233
234sp<CameraService::Client> CameraService::getClientById(int cameraId) {
235 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
236 return mClient[cameraId].promote();
237}
238
Mathias Agopian65ab4712010-07-14 17:59:35 -0700239status_t CameraService::onTransact(
240 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
241 // Permission checks
242 switch (code) {
243 case BnCameraService::CONNECT:
244 const int pid = getCallingPid();
245 const int self_pid = getpid();
246 if (pid != self_pid) {
247 // we're called from a different process, do the real check
248 if (!checkCallingPermission(
249 String16("android.permission.CAMERA"))) {
250 const int uid = getCallingUid();
251 LOGE("Permission Denial: "
252 "can't use the camera pid=%d, uid=%d", pid, uid);
253 return PERMISSION_DENIED;
254 }
255 }
256 break;
257 }
258
259 return BnCameraService::onTransact(code, data, reply, flags);
260}
261
262// The reason we need this busy bit is a new CameraService::connect() request
263// may come in while the previous Client's destructor has not been run or is
264// still running. If the last strong reference of the previous Client is gone
265// but the destructor has not been finished, we should not allow the new Client
266// to be created because we need to wait for the previous Client to tear down
267// the hardware first.
268void CameraService::setCameraBusy(int cameraId) {
269 android_atomic_write(1, &mBusy[cameraId]);
270}
271
272void CameraService::setCameraFree(int cameraId) {
273 android_atomic_write(0, &mBusy[cameraId]);
274}
275
276// We share the media players for shutter and recording sound for all clients.
277// A reference count is kept to determine when we will actually release the
278// media players.
279
280static MediaPlayer* newMediaPlayer(const char *file) {
281 MediaPlayer* mp = new MediaPlayer();
282 if (mp->setDataSource(file, NULL) == NO_ERROR) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700283 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700284 mp->prepare();
285 } else {
286 LOGE("Failed to load CameraService sounds: %s", file);
287 return NULL;
288 }
289 return mp;
290}
291
292void CameraService::loadSound() {
293 Mutex::Autolock lock(mSoundLock);
294 LOG1("CameraService::loadSound ref=%d", mSoundRef);
295 if (mSoundRef++) return;
296
297 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
298 mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
299}
300
301void CameraService::releaseSound() {
302 Mutex::Autolock lock(mSoundLock);
303 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
304 if (--mSoundRef) return;
305
306 for (int i = 0; i < NUM_SOUNDS; i++) {
307 if (mSoundPlayer[i] != 0) {
308 mSoundPlayer[i]->disconnect();
309 mSoundPlayer[i].clear();
310 }
311 }
312}
313
314void CameraService::playSound(sound_kind kind) {
315 LOG1("playSound(%d)", kind);
316 Mutex::Autolock lock(mSoundLock);
317 sp<MediaPlayer> player = mSoundPlayer[kind];
318 if (player != 0) {
319 // do not play the sound if stream volume is 0
320 // (typically because ringer mode is silent).
321 int index;
Dima Zavinfce7a472011-04-19 22:30:36 -0700322 AudioSystem::getStreamVolumeIndex(AUDIO_STREAM_ENFORCED_AUDIBLE, &index);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700323 if (index != 0) {
324 player->seekTo(0);
325 player->start();
326 }
327 }
328}
329
330// ----------------------------------------------------------------------------
331
332CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700333 const sp<ICameraClient>& cameraClient,
334 const sp<CameraHardwareInterface>& hardware,
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800335 int cameraId, int cameraFacing, int clientPid) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700336 int callingPid = getCallingPid();
337 LOG1("Client::Client E (pid %d)", callingPid);
338
339 mCameraService = cameraService;
340 mCameraClient = cameraClient;
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700341 mHardware = hardware;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700342 mCameraId = cameraId;
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800343 mCameraFacing = cameraFacing;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700344 mClientPid = clientPid;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700345 mMsgEnabled = 0;
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800346 mSurface = 0;
347 mPreviewWindow = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700348 mHardware->setCallbacks(notifyCallback,
349 dataCallback,
350 dataCallbackTimestamp,
351 (void *)cameraId);
352
353 // Enable zoom, error, and focus messages by default
354 enableMsgType(CAMERA_MSG_ERROR |
355 CAMERA_MSG_ZOOM |
356 CAMERA_MSG_FOCUS);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700357
358 // Callback is disabled by default
Iliyan Malchev9e626522011-04-14 16:51:21 -0700359 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800360 mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT);
Nipun Kwatrab5ca4612010-09-11 19:31:10 -0700361 mPlayShutterSound = true;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700362 cameraService->setCameraBusy(cameraId);
363 cameraService->loadSound();
364 LOG1("Client::Client X (pid %d)", callingPid);
365}
366
Mathias Agopian65ab4712010-07-14 17:59:35 -0700367// tear down the client
368CameraService::Client::~Client() {
369 int callingPid = getCallingPid();
370 LOG1("Client::~Client E (pid %d, this %p)", callingPid, this);
371
Mathias Agopian65ab4712010-07-14 17:59:35 -0700372 // set mClientPid to let disconnet() tear down the hardware
373 mClientPid = callingPid;
374 disconnect();
375 mCameraService->releaseSound();
376 LOG1("Client::~Client X (pid %d, this %p)", callingPid, this);
377}
378
379// ----------------------------------------------------------------------------
380
381status_t CameraService::Client::checkPid() const {
382 int callingPid = getCallingPid();
383 if (callingPid == mClientPid) return NO_ERROR;
384
385 LOGW("attempt to use a locked camera from a different process"
386 " (old pid %d, new pid %d)", mClientPid, callingPid);
387 return EBUSY;
388}
389
390status_t CameraService::Client::checkPidAndHardware() const {
391 status_t result = checkPid();
392 if (result != NO_ERROR) return result;
393 if (mHardware == 0) {
394 LOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid());
395 return INVALID_OPERATION;
396 }
397 return NO_ERROR;
398}
399
400status_t CameraService::Client::lock() {
401 int callingPid = getCallingPid();
402 LOG1("lock (pid %d)", callingPid);
403 Mutex::Autolock lock(mLock);
404
405 // lock camera to this client if the the camera is unlocked
406 if (mClientPid == 0) {
407 mClientPid = callingPid;
408 return NO_ERROR;
409 }
410
411 // returns NO_ERROR if the client already owns the camera, EBUSY otherwise
412 return checkPid();
413}
414
415status_t CameraService::Client::unlock() {
416 int callingPid = getCallingPid();
417 LOG1("unlock (pid %d)", callingPid);
418 Mutex::Autolock lock(mLock);
419
420 // allow anyone to use camera (after they lock the camera)
421 status_t result = checkPid();
422 if (result == NO_ERROR) {
423 mClientPid = 0;
424 LOG1("clear mCameraClient (pid %d)", callingPid);
425 // we need to remove the reference to ICameraClient so that when the app
426 // goes away, the reference count goes to 0.
427 mCameraClient.clear();
428 }
429 return result;
430}
431
432// connect a new client to the camera
433status_t CameraService::Client::connect(const sp<ICameraClient>& client) {
434 int callingPid = getCallingPid();
435 LOG1("connect E (pid %d)", callingPid);
436 Mutex::Autolock lock(mLock);
437
438 if (mClientPid != 0 && checkPid() != NO_ERROR) {
439 LOGW("Tried to connect to a locked camera (old pid %d, new pid %d)",
440 mClientPid, callingPid);
441 return EBUSY;
442 }
443
444 if (mCameraClient != 0 && (client->asBinder() == mCameraClient->asBinder())) {
445 LOG1("Connect to the same client");
446 return NO_ERROR;
447 }
448
Iliyan Malchev9e626522011-04-14 16:51:21 -0700449 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700450 mClientPid = callingPid;
451 mCameraClient = client;
452
453 LOG1("connect X (pid %d)", callingPid);
454 return NO_ERROR;
455}
456
457void CameraService::Client::disconnect() {
458 int callingPid = getCallingPid();
459 LOG1("disconnect E (pid %d)", callingPid);
460 Mutex::Autolock lock(mLock);
461
462 if (checkPid() != NO_ERROR) {
463 LOGW("different client - don't disconnect");
464 return;
465 }
466
467 if (mClientPid <= 0) {
468 LOG1("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
469 return;
470 }
471
472 // Make sure disconnect() is done once and once only, whether it is called
473 // from the user directly, or called by the destructor.
474 if (mHardware == 0) return;
475
476 LOG1("hardware teardown");
477 // Before destroying mHardware, we must make sure it's in the
478 // idle state.
479 // Turn off all messages.
480 disableMsgType(CAMERA_MSG_ALL_MSGS);
481 mHardware->stopPreview();
482 mHardware->cancelPicture();
483 // Release the hardware resources.
484 mHardware->release();
Mathias Agopian03dfce92010-12-07 19:38:17 -0800485
Jamie Gennis4b791682010-08-10 16:37:53 -0700486 // Release the held ANativeWindow resources.
487 if (mPreviewWindow != 0) {
488 mPreviewWindow = 0;
489 mHardware->setPreviewWindow(mPreviewWindow);
490 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700491 mHardware.clear();
492
493 mCameraService->removeClient(mCameraClient);
494 mCameraService->setCameraFree(mCameraId);
495
496 LOG1("disconnect X (pid %d)", callingPid);
497}
498
499// ----------------------------------------------------------------------------
500
Jamie Gennis4b791682010-08-10 16:37:53 -0700501// set the Surface that the preview will use
502status_t CameraService::Client::setPreviewDisplay(const sp<Surface>& surface) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700503 LOG1("setPreviewDisplay(%p) (pid %d)", surface.get(), getCallingPid());
504 Mutex::Autolock lock(mLock);
505 status_t result = checkPidAndHardware();
506 if (result != NO_ERROR) return result;
507
508 result = NO_ERROR;
509
510 // return if no change in surface.
Mathias Agopian8b1027d2011-04-05 15:44:20 -0700511 sp<IBinder> binder(surface != 0 ? surface->asBinder() : 0);
512 if (binder == mSurface) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700513 return result;
514 }
515
516 if (mSurface != 0) {
517 LOG1("clearing old preview surface %p", mSurface.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700518 }
Mathias Agopian8b1027d2011-04-05 15:44:20 -0700519 mSurface = binder;
Jamie Gennis4b791682010-08-10 16:37:53 -0700520 mPreviewWindow = surface;
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800521
Mathias Agopian03dfce92010-12-07 19:38:17 -0800522 // If preview has been already started, register preview
Mathias Agopian65ab4712010-07-14 17:59:35 -0700523 // buffers now.
524 if (mHardware->previewEnabled()) {
Mathias Agopian03dfce92010-12-07 19:38:17 -0800525 if (mPreviewWindow != 0) {
Wu-cheng Li012716a2010-10-08 22:04:43 +0800526 native_window_set_buffers_transform(mPreviewWindow.get(),
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800527 mOrientation);
Jamie Gennis4b791682010-08-10 16:37:53 -0700528 result = mHardware->setPreviewWindow(mPreviewWindow);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700529 }
530 }
531
532 return result;
533}
534
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800535// set the SurfaceTexture that the preview will use
536status_t CameraService::Client::setPreviewTexture(
537 const sp<ISurfaceTexture>& surfaceTexture) {
538 LOG1("setPreviewTexture(%p) (pid %d)", surfaceTexture.get(),
539 getCallingPid());
540 Mutex::Autolock lock(mLock);
541 status_t result = checkPidAndHardware();
542 if (result != NO_ERROR) return result;
543
544 // return if no change in surface.
545 // asBinder() is safe on NULL (returns NULL)
546 if (surfaceTexture->asBinder() == mSurface) {
547 return result;
548 }
549
550 if (mSurface != 0) {
551 LOG1("clearing old preview surface %p", mSurface.get());
552 }
553 mSurface = surfaceTexture->asBinder();
554 if (surfaceTexture != 0) {
555 mPreviewWindow = new SurfaceTextureClient(surfaceTexture);
556 } else {
557 mPreviewWindow = 0;
558 }
559
560 // If preview has been already started, set overlay or register preview
561 // buffers now.
562 if (mHardware->previewEnabled()) {
563 // XXX: What if the new preview window is 0?
564 if (mPreviewWindow != 0) {
565 native_window_set_buffers_transform(mPreviewWindow.get(),
566 mOrientation);
567 result = mHardware->setPreviewWindow(mPreviewWindow);
568 }
569 }
570
571 return result;
572}
573
Mathias Agopian65ab4712010-07-14 17:59:35 -0700574// set the preview callback flag to affect how the received frames from
575// preview are handled.
576void CameraService::Client::setPreviewCallbackFlag(int callback_flag) {
577 LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid());
578 Mutex::Autolock lock(mLock);
579 if (checkPidAndHardware() != NO_ERROR) return;
580
581 mPreviewCallbackFlag = callback_flag;
Iliyan Malchev9e626522011-04-14 16:51:21 -0700582 if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
Wu-cheng Li0667de72010-09-03 16:40:32 -0700583 enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
584 } else {
585 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700586 }
587}
588
589// start preview mode
590status_t CameraService::Client::startPreview() {
591 LOG1("startPreview (pid %d)", getCallingPid());
592 return startCameraMode(CAMERA_PREVIEW_MODE);
593}
594
595// start recording mode
596status_t CameraService::Client::startRecording() {
597 LOG1("startRecording (pid %d)", getCallingPid());
598 return startCameraMode(CAMERA_RECORDING_MODE);
599}
600
601// start preview or recording
602status_t CameraService::Client::startCameraMode(camera_mode mode) {
603 LOG1("startCameraMode(%d)", mode);
604 Mutex::Autolock lock(mLock);
605 status_t result = checkPidAndHardware();
606 if (result != NO_ERROR) return result;
607
608 switch(mode) {
609 case CAMERA_PREVIEW_MODE:
Jamie Gennis4b791682010-08-10 16:37:53 -0700610 if (mSurface == 0 && mPreviewWindow == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700611 LOG1("mSurface is not set yet.");
612 // still able to start preview in this case.
613 }
614 return startPreviewMode();
615 case CAMERA_RECORDING_MODE:
Jamie Gennis4b791682010-08-10 16:37:53 -0700616 if (mSurface == 0 && mPreviewWindow == 0) {
617 LOGE("mSurface or mPreviewWindow must be set before startRecordingMode.");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700618 return INVALID_OPERATION;
619 }
620 return startRecordingMode();
621 default:
622 return UNKNOWN_ERROR;
623 }
624}
625
626status_t CameraService::Client::startPreviewMode() {
627 LOG1("startPreviewMode");
628 status_t result = NO_ERROR;
629
630 // if preview has been enabled, nothing needs to be done
631 if (mHardware->previewEnabled()) {
632 return NO_ERROR;
633 }
634
Mathias Agopian03dfce92010-12-07 19:38:17 -0800635 if (mPreviewWindow != 0) {
636 native_window_set_buffers_transform(mPreviewWindow.get(),
637 mOrientation);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700638 }
Mathias Agopian03dfce92010-12-07 19:38:17 -0800639 mHardware->setPreviewWindow(mPreviewWindow);
640 result = mHardware->startPreview();
641
Mathias Agopian65ab4712010-07-14 17:59:35 -0700642 return result;
643}
644
645status_t CameraService::Client::startRecordingMode() {
646 LOG1("startRecordingMode");
647 status_t result = NO_ERROR;
648
649 // if recording has been enabled, nothing needs to be done
650 if (mHardware->recordingEnabled()) {
651 return NO_ERROR;
652 }
653
654 // if preview has not been started, start preview first
655 if (!mHardware->previewEnabled()) {
656 result = startPreviewMode();
657 if (result != NO_ERROR) {
658 return result;
659 }
660 }
661
662 // start recording mode
663 enableMsgType(CAMERA_MSG_VIDEO_FRAME);
664 mCameraService->playSound(SOUND_RECORDING);
665 result = mHardware->startRecording();
666 if (result != NO_ERROR) {
667 LOGE("mHardware->startRecording() failed with status %d", result);
668 }
669 return result;
670}
671
672// stop preview mode
673void CameraService::Client::stopPreview() {
674 LOG1("stopPreview (pid %d)", getCallingPid());
675 Mutex::Autolock lock(mLock);
676 if (checkPidAndHardware() != NO_ERROR) return;
677
Jamie Gennis4b791682010-08-10 16:37:53 -0700678
Mathias Agopian65ab4712010-07-14 17:59:35 -0700679 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
680 mHardware->stopPreview();
681
Mathias Agopian65ab4712010-07-14 17:59:35 -0700682 mPreviewBuffer.clear();
683}
684
685// stop recording mode
686void CameraService::Client::stopRecording() {
687 LOG1("stopRecording (pid %d)", getCallingPid());
688 Mutex::Autolock lock(mLock);
689 if (checkPidAndHardware() != NO_ERROR) return;
690
691 mCameraService->playSound(SOUND_RECORDING);
692 disableMsgType(CAMERA_MSG_VIDEO_FRAME);
693 mHardware->stopRecording();
694
695 mPreviewBuffer.clear();
696}
697
698// release a recording frame
699void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem) {
700 Mutex::Autolock lock(mLock);
701 if (checkPidAndHardware() != NO_ERROR) return;
702 mHardware->releaseRecordingFrame(mem);
703}
704
James Donge2ad6732010-10-18 20:42:51 -0700705status_t CameraService::Client::storeMetaDataInBuffers(bool enabled)
706{
707 LOG1("storeMetaDataInBuffers: %s", enabled? "true": "false");
708 Mutex::Autolock lock(mLock);
709 if (checkPidAndHardware() != NO_ERROR) {
710 return UNKNOWN_ERROR;
711 }
712 return mHardware->storeMetaDataInBuffers(enabled);
713}
714
Mathias Agopian65ab4712010-07-14 17:59:35 -0700715bool CameraService::Client::previewEnabled() {
716 LOG1("previewEnabled (pid %d)", getCallingPid());
717
718 Mutex::Autolock lock(mLock);
719 if (checkPidAndHardware() != NO_ERROR) return false;
720 return mHardware->previewEnabled();
721}
722
723bool CameraService::Client::recordingEnabled() {
724 LOG1("recordingEnabled (pid %d)", getCallingPid());
725
726 Mutex::Autolock lock(mLock);
727 if (checkPidAndHardware() != NO_ERROR) return false;
728 return mHardware->recordingEnabled();
729}
730
731status_t CameraService::Client::autoFocus() {
732 LOG1("autoFocus (pid %d)", getCallingPid());
733
734 Mutex::Autolock lock(mLock);
735 status_t result = checkPidAndHardware();
736 if (result != NO_ERROR) return result;
737
738 return mHardware->autoFocus();
739}
740
741status_t CameraService::Client::cancelAutoFocus() {
742 LOG1("cancelAutoFocus (pid %d)", getCallingPid());
743
744 Mutex::Autolock lock(mLock);
745 status_t result = checkPidAndHardware();
746 if (result != NO_ERROR) return result;
747
748 return mHardware->cancelAutoFocus();
749}
750
751// take a picture - image is returned in callback
James Donge468ac52011-02-17 16:38:06 -0800752status_t CameraService::Client::takePicture(int msgType) {
753 LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700754
755 Mutex::Autolock lock(mLock);
756 status_t result = checkPidAndHardware();
757 if (result != NO_ERROR) return result;
758
James Donge468ac52011-02-17 16:38:06 -0800759 if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
760 (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
761 LOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
762 " cannot be both enabled");
763 return BAD_VALUE;
764 }
765
766 // We only accept picture related message types
767 // and ignore other types of messages for takePicture().
768 int picMsgType = msgType
769 & (CAMERA_MSG_SHUTTER |
770 CAMERA_MSG_POSTVIEW_FRAME |
771 CAMERA_MSG_RAW_IMAGE |
772 CAMERA_MSG_RAW_IMAGE_NOTIFY |
773 CAMERA_MSG_COMPRESSED_IMAGE);
774
775 enableMsgType(picMsgType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700776
777 return mHardware->takePicture();
778}
779
780// set preview/capture parameters - key/value pairs
781status_t CameraService::Client::setParameters(const String8& params) {
782 LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
783
784 Mutex::Autolock lock(mLock);
785 status_t result = checkPidAndHardware();
786 if (result != NO_ERROR) return result;
787
788 CameraParameters p(params);
789 return mHardware->setParameters(p);
790}
791
792// get preview/capture parameters - key/value pairs
793String8 CameraService::Client::getParameters() const {
794 Mutex::Autolock lock(mLock);
795 if (checkPidAndHardware() != NO_ERROR) return String8();
796
797 String8 params(mHardware->getParameters().flatten());
798 LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
799 return params;
800}
801
Nipun Kwatrab5ca4612010-09-11 19:31:10 -0700802// enable shutter sound
803status_t CameraService::Client::enableShutterSound(bool enable) {
804 LOG1("enableShutterSound (pid %d)", getCallingPid());
805
806 status_t result = checkPidAndHardware();
807 if (result != NO_ERROR) return result;
808
809 if (enable) {
810 mPlayShutterSound = true;
811 return OK;
812 }
813
814 // Disabling shutter sound may not be allowed. In that case only
815 // allow the mediaserver process to disable the sound.
816 char value[PROPERTY_VALUE_MAX];
817 property_get("ro.camera.sound.forced", value, "0");
818 if (strcmp(value, "0") != 0) {
819 // Disabling shutter sound is not allowed. Deny if the current
820 // process is not mediaserver.
821 if (getCallingPid() != getpid()) {
822 LOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid());
823 return PERMISSION_DENIED;
824 }
825 }
826
827 mPlayShutterSound = false;
828 return OK;
829}
830
Mathias Agopian65ab4712010-07-14 17:59:35 -0700831status_t CameraService::Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
832 LOG1("sendCommand (pid %d)", getCallingPid());
Wu-cheng Li4a73f3d2010-09-23 17:17:43 -0700833 int orientation;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700834 Mutex::Autolock lock(mLock);
835 status_t result = checkPidAndHardware();
836 if (result != NO_ERROR) return result;
837
838 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
839 // The orientation cannot be set during preview.
840 if (mHardware->previewEnabled()) {
841 return INVALID_OPERATION;
842 }
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800843 // Mirror the preview if the camera is front-facing.
844 orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT);
845 if (orientation == -1) return BAD_VALUE;
846
Wu-cheng Li4a73f3d2010-09-23 17:17:43 -0700847 if (mOrientation != orientation) {
848 mOrientation = orientation;
Wu-cheng Li4a73f3d2010-09-23 17:17:43 -0700849 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700850 return OK;
Nipun Kwatrab5ca4612010-09-11 19:31:10 -0700851 } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) {
852 switch (arg1) {
853 case 0:
854 enableShutterSound(false);
855 break;
856 case 1:
857 enableShutterSound(true);
858 break;
859 default:
860 return BAD_VALUE;
861 }
862 return OK;
Nipun Kwatra3b7b3582010-09-14 16:49:08 -0700863 } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) {
864 mCameraService->playSound(SOUND_RECORDING);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700865 }
866
867 return mHardware->sendCommand(cmd, arg1, arg2);
868}
869
870// ----------------------------------------------------------------------------
871
872void CameraService::Client::enableMsgType(int32_t msgType) {
873 android_atomic_or(msgType, &mMsgEnabled);
874 mHardware->enableMsgType(msgType);
875}
876
877void CameraService::Client::disableMsgType(int32_t msgType) {
878 android_atomic_and(~msgType, &mMsgEnabled);
879 mHardware->disableMsgType(msgType);
880}
881
882#define CHECK_MESSAGE_INTERVAL 10 // 10ms
883bool CameraService::Client::lockIfMessageWanted(int32_t msgType) {
884 int sleepCount = 0;
885 while (mMsgEnabled & msgType) {
886 if (mLock.tryLock() == NO_ERROR) {
887 if (sleepCount > 0) {
888 LOG1("lockIfMessageWanted(%d): waited for %d ms",
889 msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
890 }
891 return true;
892 }
893 if (sleepCount++ == 0) {
894 LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
895 }
896 usleep(CHECK_MESSAGE_INTERVAL * 1000);
897 }
898 LOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
899 return false;
900}
901
902// ----------------------------------------------------------------------------
903
904// Converts from a raw pointer to the client to a strong pointer during a
905// hardware callback. This requires the callbacks only happen when the client
906// is still alive.
907sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user) {
908 sp<Client> client = gCameraService->getClientById((int) user);
909
910 // This could happen if the Client is in the process of shutting down (the
911 // last strong reference is gone, but the destructor hasn't finished
912 // stopping the hardware).
913 if (client == 0) return NULL;
914
915 // The checks below are not necessary and are for debugging only.
916 if (client->mCameraService.get() != gCameraService) {
917 LOGE("mismatch service!");
918 return NULL;
919 }
920
921 if (client->mHardware == 0) {
922 LOGE("mHardware == 0: callback after disconnect()?");
923 return NULL;
924 }
925
926 return client;
927}
928
929// Callback messages can be dispatched to internal handlers or pass to our
930// client's callback functions, depending on the message type.
931//
932// notifyCallback:
933// CAMERA_MSG_SHUTTER handleShutter
934// (others) c->notifyCallback
935// dataCallback:
936// CAMERA_MSG_PREVIEW_FRAME handlePreviewData
937// CAMERA_MSG_POSTVIEW_FRAME handlePostview
938// CAMERA_MSG_RAW_IMAGE handleRawPicture
939// CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture
940// (others) c->dataCallback
941// dataCallbackTimestamp
942// (others) c->dataCallbackTimestamp
943//
944// NOTE: the *Callback functions grab mLock of the client before passing
945// control to handle* functions. So the handle* functions must release the
946// lock before calling the ICameraClient's callbacks, so those callbacks can
947// invoke methods in the Client class again (For example, the preview frame
948// callback may want to releaseRecordingFrame). The handle* functions must
949// release the lock after all accesses to member variables, so it must be
950// handled very carefully.
951
952void CameraService::Client::notifyCallback(int32_t msgType, int32_t ext1,
953 int32_t ext2, void* user) {
954 LOG2("notifyCallback(%d)", msgType);
955
956 sp<Client> client = getClientFromCookie(user);
957 if (client == 0) return;
958 if (!client->lockIfMessageWanted(msgType)) return;
959
960 switch (msgType) {
961 case CAMERA_MSG_SHUTTER:
962 // ext1 is the dimension of the yuv picture.
Iliyan Malchev108dddf2011-03-28 16:10:12 -0700963 client->handleShutter();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700964 break;
965 default:
966 client->handleGenericNotify(msgType, ext1, ext2);
967 break;
968 }
969}
970
971void CameraService::Client::dataCallback(int32_t msgType,
972 const sp<IMemory>& dataPtr, void* user) {
973 LOG2("dataCallback(%d)", msgType);
974
975 sp<Client> client = getClientFromCookie(user);
976 if (client == 0) return;
977 if (!client->lockIfMessageWanted(msgType)) return;
978
979 if (dataPtr == 0) {
980 LOGE("Null data returned in data callback");
981 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
982 return;
983 }
984
985 switch (msgType) {
986 case CAMERA_MSG_PREVIEW_FRAME:
987 client->handlePreviewData(dataPtr);
988 break;
989 case CAMERA_MSG_POSTVIEW_FRAME:
990 client->handlePostview(dataPtr);
991 break;
992 case CAMERA_MSG_RAW_IMAGE:
993 client->handleRawPicture(dataPtr);
994 break;
995 case CAMERA_MSG_COMPRESSED_IMAGE:
996 client->handleCompressedPicture(dataPtr);
997 break;
998 default:
999 client->handleGenericData(msgType, dataPtr);
1000 break;
1001 }
1002}
1003
1004void CameraService::Client::dataCallbackTimestamp(nsecs_t timestamp,
1005 int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
1006 LOG2("dataCallbackTimestamp(%d)", msgType);
1007
1008 sp<Client> client = getClientFromCookie(user);
1009 if (client == 0) return;
James Dong986ef2a2010-12-09 11:08:14 -08001010 if (!client->lockIfMessageWanted(msgType)) return;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001011
1012 if (dataPtr == 0) {
1013 LOGE("Null data returned in data with timestamp callback");
1014 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
1015 return;
1016 }
1017
1018 client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
1019}
1020
1021// snapshot taken callback
Iliyan Malchev108dddf2011-03-28 16:10:12 -07001022void CameraService::Client::handleShutter(void) {
Nipun Kwatrab5ca4612010-09-11 19:31:10 -07001023 if (mPlayShutterSound) {
1024 mCameraService->playSound(SOUND_SHUTTER);
1025 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001026
Mathias Agopian65ab4712010-07-14 17:59:35 -07001027 sp<ICameraClient> c = mCameraClient;
1028 if (c != 0) {
1029 mLock.unlock();
1030 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
1031 if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
1032 }
1033 disableMsgType(CAMERA_MSG_SHUTTER);
1034
Mathias Agopian65ab4712010-07-14 17:59:35 -07001035 mLock.unlock();
1036}
1037
1038// preview callback - frame buffer update
1039void CameraService::Client::handlePreviewData(const sp<IMemory>& mem) {
1040 ssize_t offset;
1041 size_t size;
1042 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1043
Mathias Agopian65ab4712010-07-14 17:59:35 -07001044 // local copy of the callback flags
1045 int flags = mPreviewCallbackFlag;
1046
1047 // is callback enabled?
Iliyan Malchev9e626522011-04-14 16:51:21 -07001048 if (!(flags & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001049 // If the enable bit is off, the copy-out and one-shot bits are ignored
1050 LOG2("frame callback is disabled");
1051 mLock.unlock();
1052 return;
1053 }
1054
1055 // hold a strong pointer to the client
1056 sp<ICameraClient> c = mCameraClient;
1057
1058 // clear callback flags if no client or one-shot mode
Iliyan Malchev9e626522011-04-14 16:51:21 -07001059 if (c == 0 || (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001060 LOG2("Disable preview callback");
Iliyan Malchev9e626522011-04-14 16:51:21 -07001061 mPreviewCallbackFlag &= ~(CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
1062 CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
1063 CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK);
Wu-cheng Li0667de72010-09-03 16:40:32 -07001064 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001065 }
1066
1067 if (c != 0) {
1068 // Is the received frame copied out or not?
Iliyan Malchev9e626522011-04-14 16:51:21 -07001069 if (flags & CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001070 LOG2("frame is copied");
1071 copyFrameAndPostCopiedFrame(c, heap, offset, size);
1072 } else {
1073 LOG2("frame is forwarded");
1074 mLock.unlock();
1075 c->dataCallback(CAMERA_MSG_PREVIEW_FRAME, mem);
1076 }
1077 } else {
1078 mLock.unlock();
1079 }
1080}
1081
1082// picture callback - postview image ready
1083void CameraService::Client::handlePostview(const sp<IMemory>& mem) {
1084 disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
1085
1086 sp<ICameraClient> c = mCameraClient;
1087 mLock.unlock();
1088 if (c != 0) {
1089 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem);
1090 }
1091}
1092
1093// picture callback - raw image ready
1094void CameraService::Client::handleRawPicture(const sp<IMemory>& mem) {
1095 disableMsgType(CAMERA_MSG_RAW_IMAGE);
1096
1097 ssize_t offset;
1098 size_t size;
1099 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1100
Mathias Agopian65ab4712010-07-14 17:59:35 -07001101 sp<ICameraClient> c = mCameraClient;
1102 mLock.unlock();
1103 if (c != 0) {
1104 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem);
1105 }
1106}
1107
1108// picture callback - compressed picture ready
1109void CameraService::Client::handleCompressedPicture(const sp<IMemory>& mem) {
1110 disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
1111
1112 sp<ICameraClient> c = mCameraClient;
1113 mLock.unlock();
1114 if (c != 0) {
1115 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem);
1116 }
1117}
1118
1119
1120void CameraService::Client::handleGenericNotify(int32_t msgType,
1121 int32_t ext1, int32_t ext2) {
1122 sp<ICameraClient> c = mCameraClient;
1123 mLock.unlock();
1124 if (c != 0) {
1125 c->notifyCallback(msgType, ext1, ext2);
1126 }
1127}
1128
1129void CameraService::Client::handleGenericData(int32_t msgType,
1130 const sp<IMemory>& dataPtr) {
1131 sp<ICameraClient> c = mCameraClient;
1132 mLock.unlock();
1133 if (c != 0) {
1134 c->dataCallback(msgType, dataPtr);
1135 }
1136}
1137
1138void CameraService::Client::handleGenericDataTimestamp(nsecs_t timestamp,
1139 int32_t msgType, const sp<IMemory>& dataPtr) {
1140 sp<ICameraClient> c = mCameraClient;
1141 mLock.unlock();
1142 if (c != 0) {
1143 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
1144 }
1145}
1146
1147void CameraService::Client::copyFrameAndPostCopiedFrame(
1148 const sp<ICameraClient>& client, const sp<IMemoryHeap>& heap,
1149 size_t offset, size_t size) {
1150 LOG2("copyFrameAndPostCopiedFrame");
1151 // It is necessary to copy out of pmem before sending this to
1152 // the callback. For efficiency, reuse the same MemoryHeapBase
1153 // provided it's big enough. Don't allocate the memory or
1154 // perform the copy if there's no callback.
1155 // hold the preview lock while we grab a reference to the preview buffer
1156 sp<MemoryHeapBase> previewBuffer;
1157
1158 if (mPreviewBuffer == 0) {
1159 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1160 } else if (size > mPreviewBuffer->virtualSize()) {
1161 mPreviewBuffer.clear();
1162 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1163 }
1164 if (mPreviewBuffer == 0) {
1165 LOGE("failed to allocate space for preview buffer");
1166 mLock.unlock();
1167 return;
1168 }
1169 previewBuffer = mPreviewBuffer;
1170
1171 memcpy(previewBuffer->base(), (uint8_t *)heap->base() + offset, size);
1172
1173 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
1174 if (frame == 0) {
1175 LOGE("failed to allocate space for frame callback");
1176 mLock.unlock();
1177 return;
1178 }
1179
1180 mLock.unlock();
1181 client->dataCallback(CAMERA_MSG_PREVIEW_FRAME, frame);
1182}
1183
Wu-cheng Lie09591e2010-10-14 20:17:44 +08001184int CameraService::Client::getOrientation(int degrees, bool mirror) {
1185 if (!mirror) {
1186 if (degrees == 0) return 0;
1187 else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
1188 else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
1189 else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
1190 } else { // Do mirror (horizontal flip)
1191 if (degrees == 0) { // FLIP_H and ROT_0
1192 return HAL_TRANSFORM_FLIP_H;
1193 } else if (degrees == 90) { // FLIP_H and ROT_90
1194 return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
1195 } else if (degrees == 180) { // FLIP_H and ROT_180
1196 return HAL_TRANSFORM_FLIP_V;
1197 } else if (degrees == 270) { // FLIP_H and ROT_270
1198 return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
1199 }
1200 }
1201 LOGE("Invalid setDisplayOrientation degrees=%d", degrees);
1202 return -1;
1203}
1204
1205
Mathias Agopian65ab4712010-07-14 17:59:35 -07001206// ----------------------------------------------------------------------------
1207
1208static const int kDumpLockRetries = 50;
1209static const int kDumpLockSleep = 60000;
1210
1211static bool tryLock(Mutex& mutex)
1212{
1213 bool locked = false;
1214 for (int i = 0; i < kDumpLockRetries; ++i) {
1215 if (mutex.tryLock() == NO_ERROR) {
1216 locked = true;
1217 break;
1218 }
1219 usleep(kDumpLockSleep);
1220 }
1221 return locked;
1222}
1223
1224status_t CameraService::dump(int fd, const Vector<String16>& args) {
1225 static const char* kDeadlockedString = "CameraService may be deadlocked\n";
1226
1227 const size_t SIZE = 256;
1228 char buffer[SIZE];
1229 String8 result;
1230 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
1231 snprintf(buffer, SIZE, "Permission Denial: "
1232 "can't dump CameraService from pid=%d, uid=%d\n",
1233 getCallingPid(),
1234 getCallingUid());
1235 result.append(buffer);
1236 write(fd, result.string(), result.size());
1237 } else {
1238 bool locked = tryLock(mServiceLock);
1239 // failed to lock - CameraService is probably deadlocked
1240 if (!locked) {
1241 String8 result(kDeadlockedString);
1242 write(fd, result.string(), result.size());
1243 }
1244
1245 bool hasClient = false;
1246 for (int i = 0; i < mNumberOfCameras; i++) {
1247 sp<Client> client = mClient[i].promote();
1248 if (client == 0) continue;
1249 hasClient = true;
1250 sprintf(buffer, "Client[%d] (%p) PID: %d\n",
1251 i,
1252 client->getCameraClient()->asBinder().get(),
1253 client->mClientPid);
1254 result.append(buffer);
1255 write(fd, result.string(), result.size());
1256 client->mHardware->dump(fd, args);
1257 }
1258 if (!hasClient) {
1259 result.append("No camera client yet.\n");
1260 write(fd, result.string(), result.size());
1261 }
1262
1263 if (locked) mServiceLock.unlock();
1264
1265 // change logging level
1266 int n = args.size();
1267 for (int i = 0; i + 1 < n; i++) {
1268 if (args[i] == String16("-v")) {
1269 String8 levelStr(args[i+1]);
1270 int level = atoi(levelStr.string());
1271 sprintf(buffer, "Set Log Level to %d", level);
1272 result.append(buffer);
1273 setLogLevel(level);
1274 }
1275 }
1276 }
1277 return NO_ERROR;
1278}
1279
Mathias Agopian65ab4712010-07-14 17:59:35 -07001280}; // namespace android