blob: 1e8c30b727dbcb425441f5e561ecf2b95d576408 [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
150 Mutex::Autolock lock(mServiceLock);
151 if (mClient[cameraId] != 0) {
152 client = mClient[cameraId].promote();
153 if (client != 0) {
154 if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) {
155 LOG1("CameraService::connect X (pid %d) (the same client)",
156 callingPid);
157 return client;
158 } else {
159 LOGW("CameraService::connect X (pid %d) rejected (existing client).",
160 callingPid);
161 return NULL;
162 }
163 }
164 mClient[cameraId].clear();
165 }
166
167 if (mBusy[cameraId]) {
168 LOGW("CameraService::connect X (pid %d) rejected"
169 " (camera %d is still busy).", callingPid, cameraId);
170 return NULL;
171 }
172
Iliyan Malchev8951a972011-04-14 16:55:59 -0700173 struct camera_info info;
174 if (mModule->get_camera_info(cameraId, &info) != OK) {
175 LOGE("Invalid camera id %d", cameraId);
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700176 return NULL;
177 }
Iliyan Malchev8951a972011-04-14 16:55:59 -0700178
179 char camera_device_name[10];
180 snprintf(camera_device_name, sizeof(camera_device_name), "%d", cameraId);
181
182 client = new Client(this, cameraClient,
183 new CameraHardwareInterface(&mModule->common,
184 camera_device_name),
185 cameraId, info.facing, callingPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700186 mClient[cameraId] = client;
187 LOG1("CameraService::connect X");
188 return client;
189}
190
191void CameraService::removeClient(const sp<ICameraClient>& cameraClient) {
192 int callingPid = getCallingPid();
193 LOG1("CameraService::removeClient E (pid %d)", callingPid);
194
195 for (int i = 0; i < mNumberOfCameras; i++) {
196 // Declare this before the lock to make absolutely sure the
197 // destructor won't be called with the lock held.
198 sp<Client> client;
199
200 Mutex::Autolock lock(mServiceLock);
201
202 // This happens when we have already disconnected (or this is
203 // just another unused camera).
204 if (mClient[i] == 0) continue;
205
206 // Promote mClient. It can fail if we are called from this path:
207 // Client::~Client() -> disconnect() -> removeClient().
208 client = mClient[i].promote();
209
210 if (client == 0) {
211 mClient[i].clear();
212 continue;
213 }
214
215 if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) {
216 // Found our camera, clear and leave.
217 LOG1("removeClient: clear camera %d", i);
218 mClient[i].clear();
219 break;
220 }
221 }
222
223 LOG1("CameraService::removeClient X (pid %d)", callingPid);
224}
225
226sp<CameraService::Client> CameraService::getClientById(int cameraId) {
227 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
228 return mClient[cameraId].promote();
229}
230
Mathias Agopian65ab4712010-07-14 17:59:35 -0700231status_t CameraService::onTransact(
232 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
233 // Permission checks
234 switch (code) {
235 case BnCameraService::CONNECT:
236 const int pid = getCallingPid();
237 const int self_pid = getpid();
238 if (pid != self_pid) {
239 // we're called from a different process, do the real check
240 if (!checkCallingPermission(
241 String16("android.permission.CAMERA"))) {
242 const int uid = getCallingUid();
243 LOGE("Permission Denial: "
244 "can't use the camera pid=%d, uid=%d", pid, uid);
245 return PERMISSION_DENIED;
246 }
247 }
248 break;
249 }
250
251 return BnCameraService::onTransact(code, data, reply, flags);
252}
253
254// The reason we need this busy bit is a new CameraService::connect() request
255// may come in while the previous Client's destructor has not been run or is
256// still running. If the last strong reference of the previous Client is gone
257// but the destructor has not been finished, we should not allow the new Client
258// to be created because we need to wait for the previous Client to tear down
259// the hardware first.
260void CameraService::setCameraBusy(int cameraId) {
261 android_atomic_write(1, &mBusy[cameraId]);
262}
263
264void CameraService::setCameraFree(int cameraId) {
265 android_atomic_write(0, &mBusy[cameraId]);
266}
267
268// We share the media players for shutter and recording sound for all clients.
269// A reference count is kept to determine when we will actually release the
270// media players.
271
272static MediaPlayer* newMediaPlayer(const char *file) {
273 MediaPlayer* mp = new MediaPlayer();
274 if (mp->setDataSource(file, NULL) == NO_ERROR) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700275 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700276 mp->prepare();
277 } else {
278 LOGE("Failed to load CameraService sounds: %s", file);
279 return NULL;
280 }
281 return mp;
282}
283
284void CameraService::loadSound() {
285 Mutex::Autolock lock(mSoundLock);
286 LOG1("CameraService::loadSound ref=%d", mSoundRef);
287 if (mSoundRef++) return;
288
289 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
290 mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
291}
292
293void CameraService::releaseSound() {
294 Mutex::Autolock lock(mSoundLock);
295 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
296 if (--mSoundRef) return;
297
298 for (int i = 0; i < NUM_SOUNDS; i++) {
299 if (mSoundPlayer[i] != 0) {
300 mSoundPlayer[i]->disconnect();
301 mSoundPlayer[i].clear();
302 }
303 }
304}
305
306void CameraService::playSound(sound_kind kind) {
307 LOG1("playSound(%d)", kind);
308 Mutex::Autolock lock(mSoundLock);
309 sp<MediaPlayer> player = mSoundPlayer[kind];
310 if (player != 0) {
311 // do not play the sound if stream volume is 0
312 // (typically because ringer mode is silent).
313 int index;
Dima Zavinfce7a472011-04-19 22:30:36 -0700314 AudioSystem::getStreamVolumeIndex(AUDIO_STREAM_ENFORCED_AUDIBLE, &index);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700315 if (index != 0) {
316 player->seekTo(0);
317 player->start();
318 }
319 }
320}
321
322// ----------------------------------------------------------------------------
323
324CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700325 const sp<ICameraClient>& cameraClient,
326 const sp<CameraHardwareInterface>& hardware,
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800327 int cameraId, int cameraFacing, int clientPid) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700328 int callingPid = getCallingPid();
329 LOG1("Client::Client E (pid %d)", callingPid);
330
331 mCameraService = cameraService;
332 mCameraClient = cameraClient;
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700333 mHardware = hardware;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700334 mCameraId = cameraId;
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800335 mCameraFacing = cameraFacing;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700336 mClientPid = clientPid;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700337 mMsgEnabled = 0;
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800338 mSurface = 0;
339 mPreviewWindow = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700340 mHardware->setCallbacks(notifyCallback,
341 dataCallback,
342 dataCallbackTimestamp,
343 (void *)cameraId);
344
345 // Enable zoom, error, and focus messages by default
346 enableMsgType(CAMERA_MSG_ERROR |
347 CAMERA_MSG_ZOOM |
348 CAMERA_MSG_FOCUS);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700349
350 // Callback is disabled by default
Iliyan Malchev9e626522011-04-14 16:51:21 -0700351 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800352 mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT);
Nipun Kwatrab5ca4612010-09-11 19:31:10 -0700353 mPlayShutterSound = true;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700354 cameraService->setCameraBusy(cameraId);
355 cameraService->loadSound();
356 LOG1("Client::Client X (pid %d)", callingPid);
357}
358
Mathias Agopian65ab4712010-07-14 17:59:35 -0700359// tear down the client
360CameraService::Client::~Client() {
361 int callingPid = getCallingPid();
362 LOG1("Client::~Client E (pid %d, this %p)", callingPid, this);
363
Mathias Agopian65ab4712010-07-14 17:59:35 -0700364 // set mClientPid to let disconnet() tear down the hardware
365 mClientPid = callingPid;
366 disconnect();
367 mCameraService->releaseSound();
368 LOG1("Client::~Client X (pid %d, this %p)", callingPid, this);
369}
370
371// ----------------------------------------------------------------------------
372
373status_t CameraService::Client::checkPid() const {
374 int callingPid = getCallingPid();
375 if (callingPid == mClientPid) return NO_ERROR;
376
377 LOGW("attempt to use a locked camera from a different process"
378 " (old pid %d, new pid %d)", mClientPid, callingPid);
379 return EBUSY;
380}
381
382status_t CameraService::Client::checkPidAndHardware() const {
383 status_t result = checkPid();
384 if (result != NO_ERROR) return result;
385 if (mHardware == 0) {
386 LOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid());
387 return INVALID_OPERATION;
388 }
389 return NO_ERROR;
390}
391
392status_t CameraService::Client::lock() {
393 int callingPid = getCallingPid();
394 LOG1("lock (pid %d)", callingPid);
395 Mutex::Autolock lock(mLock);
396
397 // lock camera to this client if the the camera is unlocked
398 if (mClientPid == 0) {
399 mClientPid = callingPid;
400 return NO_ERROR;
401 }
402
403 // returns NO_ERROR if the client already owns the camera, EBUSY otherwise
404 return checkPid();
405}
406
407status_t CameraService::Client::unlock() {
408 int callingPid = getCallingPid();
409 LOG1("unlock (pid %d)", callingPid);
410 Mutex::Autolock lock(mLock);
411
412 // allow anyone to use camera (after they lock the camera)
413 status_t result = checkPid();
414 if (result == NO_ERROR) {
415 mClientPid = 0;
416 LOG1("clear mCameraClient (pid %d)", callingPid);
417 // we need to remove the reference to ICameraClient so that when the app
418 // goes away, the reference count goes to 0.
419 mCameraClient.clear();
420 }
421 return result;
422}
423
424// connect a new client to the camera
425status_t CameraService::Client::connect(const sp<ICameraClient>& client) {
426 int callingPid = getCallingPid();
427 LOG1("connect E (pid %d)", callingPid);
428 Mutex::Autolock lock(mLock);
429
430 if (mClientPid != 0 && checkPid() != NO_ERROR) {
431 LOGW("Tried to connect to a locked camera (old pid %d, new pid %d)",
432 mClientPid, callingPid);
433 return EBUSY;
434 }
435
436 if (mCameraClient != 0 && (client->asBinder() == mCameraClient->asBinder())) {
437 LOG1("Connect to the same client");
438 return NO_ERROR;
439 }
440
Iliyan Malchev9e626522011-04-14 16:51:21 -0700441 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700442 mClientPid = callingPid;
443 mCameraClient = client;
444
445 LOG1("connect X (pid %d)", callingPid);
446 return NO_ERROR;
447}
448
449void CameraService::Client::disconnect() {
450 int callingPid = getCallingPid();
451 LOG1("disconnect E (pid %d)", callingPid);
452 Mutex::Autolock lock(mLock);
453
454 if (checkPid() != NO_ERROR) {
455 LOGW("different client - don't disconnect");
456 return;
457 }
458
459 if (mClientPid <= 0) {
460 LOG1("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
461 return;
462 }
463
464 // Make sure disconnect() is done once and once only, whether it is called
465 // from the user directly, or called by the destructor.
466 if (mHardware == 0) return;
467
468 LOG1("hardware teardown");
469 // Before destroying mHardware, we must make sure it's in the
470 // idle state.
471 // Turn off all messages.
472 disableMsgType(CAMERA_MSG_ALL_MSGS);
473 mHardware->stopPreview();
474 mHardware->cancelPicture();
475 // Release the hardware resources.
476 mHardware->release();
Mathias Agopian03dfce92010-12-07 19:38:17 -0800477
Jamie Gennis4b791682010-08-10 16:37:53 -0700478 // Release the held ANativeWindow resources.
479 if (mPreviewWindow != 0) {
480 mPreviewWindow = 0;
481 mHardware->setPreviewWindow(mPreviewWindow);
482 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700483 mHardware.clear();
484
485 mCameraService->removeClient(mCameraClient);
486 mCameraService->setCameraFree(mCameraId);
487
488 LOG1("disconnect X (pid %d)", callingPid);
489}
490
491// ----------------------------------------------------------------------------
492
Jamie Gennis4b791682010-08-10 16:37:53 -0700493// set the Surface that the preview will use
494status_t CameraService::Client::setPreviewDisplay(const sp<Surface>& surface) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700495 LOG1("setPreviewDisplay(%p) (pid %d)", surface.get(), getCallingPid());
496 Mutex::Autolock lock(mLock);
497 status_t result = checkPidAndHardware();
498 if (result != NO_ERROR) return result;
499
500 result = NO_ERROR;
501
502 // return if no change in surface.
Mathias Agopian8b1027d2011-04-05 15:44:20 -0700503 sp<IBinder> binder(surface != 0 ? surface->asBinder() : 0);
504 if (binder == mSurface) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700505 return result;
506 }
507
508 if (mSurface != 0) {
509 LOG1("clearing old preview surface %p", mSurface.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700510 }
Mathias Agopian8b1027d2011-04-05 15:44:20 -0700511 mSurface = binder;
Jamie Gennis4b791682010-08-10 16:37:53 -0700512 mPreviewWindow = surface;
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800513
Mathias Agopian03dfce92010-12-07 19:38:17 -0800514 // If preview has been already started, register preview
Mathias Agopian65ab4712010-07-14 17:59:35 -0700515 // buffers now.
516 if (mHardware->previewEnabled()) {
Mathias Agopian03dfce92010-12-07 19:38:17 -0800517 if (mPreviewWindow != 0) {
Wu-cheng Li012716a2010-10-08 22:04:43 +0800518 native_window_set_buffers_transform(mPreviewWindow.get(),
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800519 mOrientation);
Jamie Gennis4b791682010-08-10 16:37:53 -0700520 result = mHardware->setPreviewWindow(mPreviewWindow);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700521 }
522 }
523
524 return result;
525}
526
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800527// set the SurfaceTexture that the preview will use
528status_t CameraService::Client::setPreviewTexture(
529 const sp<ISurfaceTexture>& surfaceTexture) {
530 LOG1("setPreviewTexture(%p) (pid %d)", surfaceTexture.get(),
531 getCallingPid());
532 Mutex::Autolock lock(mLock);
533 status_t result = checkPidAndHardware();
534 if (result != NO_ERROR) return result;
535
536 // return if no change in surface.
537 // asBinder() is safe on NULL (returns NULL)
538 if (surfaceTexture->asBinder() == mSurface) {
539 return result;
540 }
541
542 if (mSurface != 0) {
543 LOG1("clearing old preview surface %p", mSurface.get());
544 }
545 mSurface = surfaceTexture->asBinder();
546 if (surfaceTexture != 0) {
547 mPreviewWindow = new SurfaceTextureClient(surfaceTexture);
548 } else {
549 mPreviewWindow = 0;
550 }
551
552 // If preview has been already started, set overlay or register preview
553 // buffers now.
554 if (mHardware->previewEnabled()) {
555 // XXX: What if the new preview window is 0?
556 if (mPreviewWindow != 0) {
557 native_window_set_buffers_transform(mPreviewWindow.get(),
558 mOrientation);
559 result = mHardware->setPreviewWindow(mPreviewWindow);
560 }
561 }
562
563 return result;
564}
565
Mathias Agopian65ab4712010-07-14 17:59:35 -0700566// set the preview callback flag to affect how the received frames from
567// preview are handled.
568void CameraService::Client::setPreviewCallbackFlag(int callback_flag) {
569 LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid());
570 Mutex::Autolock lock(mLock);
571 if (checkPidAndHardware() != NO_ERROR) return;
572
573 mPreviewCallbackFlag = callback_flag;
Iliyan Malchev9e626522011-04-14 16:51:21 -0700574 if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
Wu-cheng Li0667de72010-09-03 16:40:32 -0700575 enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
576 } else {
577 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700578 }
579}
580
581// start preview mode
582status_t CameraService::Client::startPreview() {
583 LOG1("startPreview (pid %d)", getCallingPid());
584 return startCameraMode(CAMERA_PREVIEW_MODE);
585}
586
587// start recording mode
588status_t CameraService::Client::startRecording() {
589 LOG1("startRecording (pid %d)", getCallingPid());
590 return startCameraMode(CAMERA_RECORDING_MODE);
591}
592
593// start preview or recording
594status_t CameraService::Client::startCameraMode(camera_mode mode) {
595 LOG1("startCameraMode(%d)", mode);
596 Mutex::Autolock lock(mLock);
597 status_t result = checkPidAndHardware();
598 if (result != NO_ERROR) return result;
599
600 switch(mode) {
601 case CAMERA_PREVIEW_MODE:
Jamie Gennis4b791682010-08-10 16:37:53 -0700602 if (mSurface == 0 && mPreviewWindow == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700603 LOG1("mSurface is not set yet.");
604 // still able to start preview in this case.
605 }
606 return startPreviewMode();
607 case CAMERA_RECORDING_MODE:
Jamie Gennis4b791682010-08-10 16:37:53 -0700608 if (mSurface == 0 && mPreviewWindow == 0) {
609 LOGE("mSurface or mPreviewWindow must be set before startRecordingMode.");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700610 return INVALID_OPERATION;
611 }
612 return startRecordingMode();
613 default:
614 return UNKNOWN_ERROR;
615 }
616}
617
618status_t CameraService::Client::startPreviewMode() {
619 LOG1("startPreviewMode");
620 status_t result = NO_ERROR;
621
622 // if preview has been enabled, nothing needs to be done
623 if (mHardware->previewEnabled()) {
624 return NO_ERROR;
625 }
626
Mathias Agopian03dfce92010-12-07 19:38:17 -0800627 if (mPreviewWindow != 0) {
628 native_window_set_buffers_transform(mPreviewWindow.get(),
629 mOrientation);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700630 }
Mathias Agopian03dfce92010-12-07 19:38:17 -0800631 mHardware->setPreviewWindow(mPreviewWindow);
632 result = mHardware->startPreview();
633
Mathias Agopian65ab4712010-07-14 17:59:35 -0700634 return result;
635}
636
637status_t CameraService::Client::startRecordingMode() {
638 LOG1("startRecordingMode");
639 status_t result = NO_ERROR;
640
641 // if recording has been enabled, nothing needs to be done
642 if (mHardware->recordingEnabled()) {
643 return NO_ERROR;
644 }
645
646 // if preview has not been started, start preview first
647 if (!mHardware->previewEnabled()) {
648 result = startPreviewMode();
649 if (result != NO_ERROR) {
650 return result;
651 }
652 }
653
654 // start recording mode
655 enableMsgType(CAMERA_MSG_VIDEO_FRAME);
656 mCameraService->playSound(SOUND_RECORDING);
657 result = mHardware->startRecording();
658 if (result != NO_ERROR) {
659 LOGE("mHardware->startRecording() failed with status %d", result);
660 }
661 return result;
662}
663
664// stop preview mode
665void CameraService::Client::stopPreview() {
666 LOG1("stopPreview (pid %d)", getCallingPid());
667 Mutex::Autolock lock(mLock);
668 if (checkPidAndHardware() != NO_ERROR) return;
669
Jamie Gennis4b791682010-08-10 16:37:53 -0700670
Mathias Agopian65ab4712010-07-14 17:59:35 -0700671 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
672 mHardware->stopPreview();
673
Mathias Agopian65ab4712010-07-14 17:59:35 -0700674 mPreviewBuffer.clear();
675}
676
677// stop recording mode
678void CameraService::Client::stopRecording() {
679 LOG1("stopRecording (pid %d)", getCallingPid());
680 Mutex::Autolock lock(mLock);
681 if (checkPidAndHardware() != NO_ERROR) return;
682
683 mCameraService->playSound(SOUND_RECORDING);
684 disableMsgType(CAMERA_MSG_VIDEO_FRAME);
685 mHardware->stopRecording();
686
687 mPreviewBuffer.clear();
688}
689
690// release a recording frame
691void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem) {
692 Mutex::Autolock lock(mLock);
693 if (checkPidAndHardware() != NO_ERROR) return;
694 mHardware->releaseRecordingFrame(mem);
695}
696
James Donge2ad6732010-10-18 20:42:51 -0700697status_t CameraService::Client::storeMetaDataInBuffers(bool enabled)
698{
699 LOG1("storeMetaDataInBuffers: %s", enabled? "true": "false");
700 Mutex::Autolock lock(mLock);
701 if (checkPidAndHardware() != NO_ERROR) {
702 return UNKNOWN_ERROR;
703 }
704 return mHardware->storeMetaDataInBuffers(enabled);
705}
706
Mathias Agopian65ab4712010-07-14 17:59:35 -0700707bool CameraService::Client::previewEnabled() {
708 LOG1("previewEnabled (pid %d)", getCallingPid());
709
710 Mutex::Autolock lock(mLock);
711 if (checkPidAndHardware() != NO_ERROR) return false;
712 return mHardware->previewEnabled();
713}
714
715bool CameraService::Client::recordingEnabled() {
716 LOG1("recordingEnabled (pid %d)", getCallingPid());
717
718 Mutex::Autolock lock(mLock);
719 if (checkPidAndHardware() != NO_ERROR) return false;
720 return mHardware->recordingEnabled();
721}
722
723status_t CameraService::Client::autoFocus() {
724 LOG1("autoFocus (pid %d)", getCallingPid());
725
726 Mutex::Autolock lock(mLock);
727 status_t result = checkPidAndHardware();
728 if (result != NO_ERROR) return result;
729
730 return mHardware->autoFocus();
731}
732
733status_t CameraService::Client::cancelAutoFocus() {
734 LOG1("cancelAutoFocus (pid %d)", getCallingPid());
735
736 Mutex::Autolock lock(mLock);
737 status_t result = checkPidAndHardware();
738 if (result != NO_ERROR) return result;
739
740 return mHardware->cancelAutoFocus();
741}
742
743// take a picture - image is returned in callback
James Donge468ac52011-02-17 16:38:06 -0800744status_t CameraService::Client::takePicture(int msgType) {
745 LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700746
747 Mutex::Autolock lock(mLock);
748 status_t result = checkPidAndHardware();
749 if (result != NO_ERROR) return result;
750
James Donge468ac52011-02-17 16:38:06 -0800751 if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
752 (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
753 LOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
754 " cannot be both enabled");
755 return BAD_VALUE;
756 }
757
758 // We only accept picture related message types
759 // and ignore other types of messages for takePicture().
760 int picMsgType = msgType
761 & (CAMERA_MSG_SHUTTER |
762 CAMERA_MSG_POSTVIEW_FRAME |
763 CAMERA_MSG_RAW_IMAGE |
764 CAMERA_MSG_RAW_IMAGE_NOTIFY |
765 CAMERA_MSG_COMPRESSED_IMAGE);
766
767 enableMsgType(picMsgType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700768
769 return mHardware->takePicture();
770}
771
772// set preview/capture parameters - key/value pairs
773status_t CameraService::Client::setParameters(const String8& params) {
774 LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
775
776 Mutex::Autolock lock(mLock);
777 status_t result = checkPidAndHardware();
778 if (result != NO_ERROR) return result;
779
780 CameraParameters p(params);
781 return mHardware->setParameters(p);
782}
783
784// get preview/capture parameters - key/value pairs
785String8 CameraService::Client::getParameters() const {
786 Mutex::Autolock lock(mLock);
787 if (checkPidAndHardware() != NO_ERROR) return String8();
788
789 String8 params(mHardware->getParameters().flatten());
790 LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
791 return params;
792}
793
Nipun Kwatrab5ca4612010-09-11 19:31:10 -0700794// enable shutter sound
795status_t CameraService::Client::enableShutterSound(bool enable) {
796 LOG1("enableShutterSound (pid %d)", getCallingPid());
797
798 status_t result = checkPidAndHardware();
799 if (result != NO_ERROR) return result;
800
801 if (enable) {
802 mPlayShutterSound = true;
803 return OK;
804 }
805
806 // Disabling shutter sound may not be allowed. In that case only
807 // allow the mediaserver process to disable the sound.
808 char value[PROPERTY_VALUE_MAX];
809 property_get("ro.camera.sound.forced", value, "0");
810 if (strcmp(value, "0") != 0) {
811 // Disabling shutter sound is not allowed. Deny if the current
812 // process is not mediaserver.
813 if (getCallingPid() != getpid()) {
814 LOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid());
815 return PERMISSION_DENIED;
816 }
817 }
818
819 mPlayShutterSound = false;
820 return OK;
821}
822
Mathias Agopian65ab4712010-07-14 17:59:35 -0700823status_t CameraService::Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
824 LOG1("sendCommand (pid %d)", getCallingPid());
Wu-cheng Li4a73f3d2010-09-23 17:17:43 -0700825 int orientation;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700826 Mutex::Autolock lock(mLock);
827 status_t result = checkPidAndHardware();
828 if (result != NO_ERROR) return result;
829
830 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
831 // The orientation cannot be set during preview.
832 if (mHardware->previewEnabled()) {
833 return INVALID_OPERATION;
834 }
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800835 // Mirror the preview if the camera is front-facing.
836 orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT);
837 if (orientation == -1) return BAD_VALUE;
838
Wu-cheng Li4a73f3d2010-09-23 17:17:43 -0700839 if (mOrientation != orientation) {
840 mOrientation = orientation;
Wu-cheng Li4a73f3d2010-09-23 17:17:43 -0700841 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700842 return OK;
Nipun Kwatrab5ca4612010-09-11 19:31:10 -0700843 } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) {
844 switch (arg1) {
845 case 0:
846 enableShutterSound(false);
847 break;
848 case 1:
849 enableShutterSound(true);
850 break;
851 default:
852 return BAD_VALUE;
853 }
854 return OK;
Nipun Kwatra3b7b3582010-09-14 16:49:08 -0700855 } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) {
856 mCameraService->playSound(SOUND_RECORDING);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700857 }
858
859 return mHardware->sendCommand(cmd, arg1, arg2);
860}
861
862// ----------------------------------------------------------------------------
863
864void CameraService::Client::enableMsgType(int32_t msgType) {
865 android_atomic_or(msgType, &mMsgEnabled);
866 mHardware->enableMsgType(msgType);
867}
868
869void CameraService::Client::disableMsgType(int32_t msgType) {
870 android_atomic_and(~msgType, &mMsgEnabled);
871 mHardware->disableMsgType(msgType);
872}
873
874#define CHECK_MESSAGE_INTERVAL 10 // 10ms
875bool CameraService::Client::lockIfMessageWanted(int32_t msgType) {
876 int sleepCount = 0;
877 while (mMsgEnabled & msgType) {
878 if (mLock.tryLock() == NO_ERROR) {
879 if (sleepCount > 0) {
880 LOG1("lockIfMessageWanted(%d): waited for %d ms",
881 msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
882 }
883 return true;
884 }
885 if (sleepCount++ == 0) {
886 LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
887 }
888 usleep(CHECK_MESSAGE_INTERVAL * 1000);
889 }
890 LOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
891 return false;
892}
893
894// ----------------------------------------------------------------------------
895
896// Converts from a raw pointer to the client to a strong pointer during a
897// hardware callback. This requires the callbacks only happen when the client
898// is still alive.
899sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user) {
900 sp<Client> client = gCameraService->getClientById((int) user);
901
902 // This could happen if the Client is in the process of shutting down (the
903 // last strong reference is gone, but the destructor hasn't finished
904 // stopping the hardware).
905 if (client == 0) return NULL;
906
907 // The checks below are not necessary and are for debugging only.
908 if (client->mCameraService.get() != gCameraService) {
909 LOGE("mismatch service!");
910 return NULL;
911 }
912
913 if (client->mHardware == 0) {
914 LOGE("mHardware == 0: callback after disconnect()?");
915 return NULL;
916 }
917
918 return client;
919}
920
921// Callback messages can be dispatched to internal handlers or pass to our
922// client's callback functions, depending on the message type.
923//
924// notifyCallback:
925// CAMERA_MSG_SHUTTER handleShutter
926// (others) c->notifyCallback
927// dataCallback:
928// CAMERA_MSG_PREVIEW_FRAME handlePreviewData
929// CAMERA_MSG_POSTVIEW_FRAME handlePostview
930// CAMERA_MSG_RAW_IMAGE handleRawPicture
931// CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture
932// (others) c->dataCallback
933// dataCallbackTimestamp
934// (others) c->dataCallbackTimestamp
935//
936// NOTE: the *Callback functions grab mLock of the client before passing
937// control to handle* functions. So the handle* functions must release the
938// lock before calling the ICameraClient's callbacks, so those callbacks can
939// invoke methods in the Client class again (For example, the preview frame
940// callback may want to releaseRecordingFrame). The handle* functions must
941// release the lock after all accesses to member variables, so it must be
942// handled very carefully.
943
944void CameraService::Client::notifyCallback(int32_t msgType, int32_t ext1,
945 int32_t ext2, void* user) {
946 LOG2("notifyCallback(%d)", msgType);
947
948 sp<Client> client = getClientFromCookie(user);
949 if (client == 0) return;
950 if (!client->lockIfMessageWanted(msgType)) return;
951
952 switch (msgType) {
953 case CAMERA_MSG_SHUTTER:
954 // ext1 is the dimension of the yuv picture.
Iliyan Malchev108dddf2011-03-28 16:10:12 -0700955 client->handleShutter();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700956 break;
957 default:
958 client->handleGenericNotify(msgType, ext1, ext2);
959 break;
960 }
961}
962
963void CameraService::Client::dataCallback(int32_t msgType,
964 const sp<IMemory>& dataPtr, void* user) {
965 LOG2("dataCallback(%d)", msgType);
966
967 sp<Client> client = getClientFromCookie(user);
968 if (client == 0) return;
969 if (!client->lockIfMessageWanted(msgType)) return;
970
971 if (dataPtr == 0) {
972 LOGE("Null data returned in data callback");
973 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
974 return;
975 }
976
977 switch (msgType) {
978 case CAMERA_MSG_PREVIEW_FRAME:
979 client->handlePreviewData(dataPtr);
980 break;
981 case CAMERA_MSG_POSTVIEW_FRAME:
982 client->handlePostview(dataPtr);
983 break;
984 case CAMERA_MSG_RAW_IMAGE:
985 client->handleRawPicture(dataPtr);
986 break;
987 case CAMERA_MSG_COMPRESSED_IMAGE:
988 client->handleCompressedPicture(dataPtr);
989 break;
990 default:
991 client->handleGenericData(msgType, dataPtr);
992 break;
993 }
994}
995
996void CameraService::Client::dataCallbackTimestamp(nsecs_t timestamp,
997 int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
998 LOG2("dataCallbackTimestamp(%d)", msgType);
999
1000 sp<Client> client = getClientFromCookie(user);
1001 if (client == 0) return;
James Dong986ef2a2010-12-09 11:08:14 -08001002 if (!client->lockIfMessageWanted(msgType)) return;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001003
1004 if (dataPtr == 0) {
1005 LOGE("Null data returned in data with timestamp callback");
1006 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
1007 return;
1008 }
1009
1010 client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
1011}
1012
1013// snapshot taken callback
Iliyan Malchev108dddf2011-03-28 16:10:12 -07001014void CameraService::Client::handleShutter(void) {
Nipun Kwatrab5ca4612010-09-11 19:31:10 -07001015 if (mPlayShutterSound) {
1016 mCameraService->playSound(SOUND_SHUTTER);
1017 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001018
Mathias Agopian65ab4712010-07-14 17:59:35 -07001019 sp<ICameraClient> c = mCameraClient;
1020 if (c != 0) {
1021 mLock.unlock();
1022 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
1023 if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
1024 }
1025 disableMsgType(CAMERA_MSG_SHUTTER);
1026
Mathias Agopian65ab4712010-07-14 17:59:35 -07001027 mLock.unlock();
1028}
1029
1030// preview callback - frame buffer update
1031void CameraService::Client::handlePreviewData(const sp<IMemory>& mem) {
1032 ssize_t offset;
1033 size_t size;
1034 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1035
Mathias Agopian65ab4712010-07-14 17:59:35 -07001036 // local copy of the callback flags
1037 int flags = mPreviewCallbackFlag;
1038
1039 // is callback enabled?
Iliyan Malchev9e626522011-04-14 16:51:21 -07001040 if (!(flags & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001041 // If the enable bit is off, the copy-out and one-shot bits are ignored
1042 LOG2("frame callback is disabled");
1043 mLock.unlock();
1044 return;
1045 }
1046
1047 // hold a strong pointer to the client
1048 sp<ICameraClient> c = mCameraClient;
1049
1050 // clear callback flags if no client or one-shot mode
Iliyan Malchev9e626522011-04-14 16:51:21 -07001051 if (c == 0 || (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001052 LOG2("Disable preview callback");
Iliyan Malchev9e626522011-04-14 16:51:21 -07001053 mPreviewCallbackFlag &= ~(CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
1054 CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
1055 CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK);
Wu-cheng Li0667de72010-09-03 16:40:32 -07001056 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001057 }
1058
1059 if (c != 0) {
1060 // Is the received frame copied out or not?
Iliyan Malchev9e626522011-04-14 16:51:21 -07001061 if (flags & CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001062 LOG2("frame is copied");
1063 copyFrameAndPostCopiedFrame(c, heap, offset, size);
1064 } else {
1065 LOG2("frame is forwarded");
1066 mLock.unlock();
1067 c->dataCallback(CAMERA_MSG_PREVIEW_FRAME, mem);
1068 }
1069 } else {
1070 mLock.unlock();
1071 }
1072}
1073
1074// picture callback - postview image ready
1075void CameraService::Client::handlePostview(const sp<IMemory>& mem) {
1076 disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
1077
1078 sp<ICameraClient> c = mCameraClient;
1079 mLock.unlock();
1080 if (c != 0) {
1081 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem);
1082 }
1083}
1084
1085// picture callback - raw image ready
1086void CameraService::Client::handleRawPicture(const sp<IMemory>& mem) {
1087 disableMsgType(CAMERA_MSG_RAW_IMAGE);
1088
1089 ssize_t offset;
1090 size_t size;
1091 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1092
Mathias Agopian65ab4712010-07-14 17:59:35 -07001093 sp<ICameraClient> c = mCameraClient;
1094 mLock.unlock();
1095 if (c != 0) {
1096 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem);
1097 }
1098}
1099
1100// picture callback - compressed picture ready
1101void CameraService::Client::handleCompressedPicture(const sp<IMemory>& mem) {
1102 disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
1103
1104 sp<ICameraClient> c = mCameraClient;
1105 mLock.unlock();
1106 if (c != 0) {
1107 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem);
1108 }
1109}
1110
1111
1112void CameraService::Client::handleGenericNotify(int32_t msgType,
1113 int32_t ext1, int32_t ext2) {
1114 sp<ICameraClient> c = mCameraClient;
1115 mLock.unlock();
1116 if (c != 0) {
1117 c->notifyCallback(msgType, ext1, ext2);
1118 }
1119}
1120
1121void CameraService::Client::handleGenericData(int32_t msgType,
1122 const sp<IMemory>& dataPtr) {
1123 sp<ICameraClient> c = mCameraClient;
1124 mLock.unlock();
1125 if (c != 0) {
1126 c->dataCallback(msgType, dataPtr);
1127 }
1128}
1129
1130void CameraService::Client::handleGenericDataTimestamp(nsecs_t timestamp,
1131 int32_t msgType, const sp<IMemory>& dataPtr) {
1132 sp<ICameraClient> c = mCameraClient;
1133 mLock.unlock();
1134 if (c != 0) {
1135 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
1136 }
1137}
1138
1139void CameraService::Client::copyFrameAndPostCopiedFrame(
1140 const sp<ICameraClient>& client, const sp<IMemoryHeap>& heap,
1141 size_t offset, size_t size) {
1142 LOG2("copyFrameAndPostCopiedFrame");
1143 // It is necessary to copy out of pmem before sending this to
1144 // the callback. For efficiency, reuse the same MemoryHeapBase
1145 // provided it's big enough. Don't allocate the memory or
1146 // perform the copy if there's no callback.
1147 // hold the preview lock while we grab a reference to the preview buffer
1148 sp<MemoryHeapBase> previewBuffer;
1149
1150 if (mPreviewBuffer == 0) {
1151 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1152 } else if (size > mPreviewBuffer->virtualSize()) {
1153 mPreviewBuffer.clear();
1154 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1155 }
1156 if (mPreviewBuffer == 0) {
1157 LOGE("failed to allocate space for preview buffer");
1158 mLock.unlock();
1159 return;
1160 }
1161 previewBuffer = mPreviewBuffer;
1162
1163 memcpy(previewBuffer->base(), (uint8_t *)heap->base() + offset, size);
1164
1165 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
1166 if (frame == 0) {
1167 LOGE("failed to allocate space for frame callback");
1168 mLock.unlock();
1169 return;
1170 }
1171
1172 mLock.unlock();
1173 client->dataCallback(CAMERA_MSG_PREVIEW_FRAME, frame);
1174}
1175
Wu-cheng Lie09591e2010-10-14 20:17:44 +08001176int CameraService::Client::getOrientation(int degrees, bool mirror) {
1177 if (!mirror) {
1178 if (degrees == 0) return 0;
1179 else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
1180 else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
1181 else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
1182 } else { // Do mirror (horizontal flip)
1183 if (degrees == 0) { // FLIP_H and ROT_0
1184 return HAL_TRANSFORM_FLIP_H;
1185 } else if (degrees == 90) { // FLIP_H and ROT_90
1186 return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
1187 } else if (degrees == 180) { // FLIP_H and ROT_180
1188 return HAL_TRANSFORM_FLIP_V;
1189 } else if (degrees == 270) { // FLIP_H and ROT_270
1190 return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
1191 }
1192 }
1193 LOGE("Invalid setDisplayOrientation degrees=%d", degrees);
1194 return -1;
1195}
1196
1197
Mathias Agopian65ab4712010-07-14 17:59:35 -07001198// ----------------------------------------------------------------------------
1199
1200static const int kDumpLockRetries = 50;
1201static const int kDumpLockSleep = 60000;
1202
1203static bool tryLock(Mutex& mutex)
1204{
1205 bool locked = false;
1206 for (int i = 0; i < kDumpLockRetries; ++i) {
1207 if (mutex.tryLock() == NO_ERROR) {
1208 locked = true;
1209 break;
1210 }
1211 usleep(kDumpLockSleep);
1212 }
1213 return locked;
1214}
1215
1216status_t CameraService::dump(int fd, const Vector<String16>& args) {
1217 static const char* kDeadlockedString = "CameraService may be deadlocked\n";
1218
1219 const size_t SIZE = 256;
1220 char buffer[SIZE];
1221 String8 result;
1222 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
1223 snprintf(buffer, SIZE, "Permission Denial: "
1224 "can't dump CameraService from pid=%d, uid=%d\n",
1225 getCallingPid(),
1226 getCallingUid());
1227 result.append(buffer);
1228 write(fd, result.string(), result.size());
1229 } else {
1230 bool locked = tryLock(mServiceLock);
1231 // failed to lock - CameraService is probably deadlocked
1232 if (!locked) {
1233 String8 result(kDeadlockedString);
1234 write(fd, result.string(), result.size());
1235 }
1236
1237 bool hasClient = false;
1238 for (int i = 0; i < mNumberOfCameras; i++) {
1239 sp<Client> client = mClient[i].promote();
1240 if (client == 0) continue;
1241 hasClient = true;
1242 sprintf(buffer, "Client[%d] (%p) PID: %d\n",
1243 i,
1244 client->getCameraClient()->asBinder().get(),
1245 client->mClientPid);
1246 result.append(buffer);
1247 write(fd, result.string(), result.size());
1248 client->mHardware->dump(fd, args);
1249 }
1250 if (!hasClient) {
1251 result.append("No camera client yet.\n");
1252 write(fd, result.string(), result.size());
1253 }
1254
1255 if (locked) mServiceLock.unlock();
1256
1257 // change logging level
1258 int n = args.size();
1259 for (int i = 0; i + 1 < n; i++) {
1260 if (args[i] == String16("-v")) {
1261 String8 levelStr(args[i+1]);
1262 int level = atoi(levelStr.string());
1263 sprintf(buffer, "Set Log Level to %d", level);
1264 result.append(buffer);
1265 setLogLevel(level);
1266 }
1267 }
1268 }
1269 return NO_ERROR;
1270}
1271
Mathias Agopian65ab4712010-07-14 17:59:35 -07001272}; // namespace android