blob: 355763e6512c9fa40618fddba8f77f686017a79a [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2**
3** Copyright (C) 2008, The Android Open Source Project
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004**
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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018#define LOG_TAG "CameraService"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019
Chih-Chung Change25cc652010-05-06 16:36:58 +080020#include <stdio.h>
21#include <sys/types.h>
22#include <pthread.h>
23
Mathias Agopian07952722009-05-19 19:08:10 -070024#include <binder/IPCThreadState.h>
Chih-Chung Change25cc652010-05-06 16:36:58 +080025#include <binder/IServiceManager.h>
Mathias Agopian07952722009-05-19 19:08:10 -070026#include <binder/MemoryBase.h>
27#include <binder/MemoryHeapBase.h>
Chih-Chung Change25cc652010-05-06 16:36:58 +080028#include <cutils/atomic.h>
Nipun Kwatra088146a2010-09-11 19:31:10 -070029#include <cutils/properties.h>
Chih-Chung Change25cc652010-05-06 16:36:58 +080030#include <hardware/hardware.h>
31#include <media/AudioSystem.h>
32#include <media/mediaplayer.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080033#include <surfaceflinger/ISurface.h>
34#include <ui/Overlay.h>
Chih-Chung Change25cc652010-05-06 16:36:58 +080035#include <utils/Errors.h>
36#include <utils/Log.h>
37#include <utils/String16.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
39#include "CameraService.h"
Eric Laurenta7f1e5c2009-03-27 16:27:16 -070040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041namespace android {
42
Chih-Chung Change25cc652010-05-06 16:36:58 +080043// ----------------------------------------------------------------------------
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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053}
54
Chih-Chung Change25cc652010-05-06 16:36:58 +080055// ----------------------------------------------------------------------------
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +080057static int getCallingPid() {
58 return IPCThreadState::self()->getCallingPid();
59}
60
Chih-Chung Change25cc652010-05-06 16:36:58 +080061static int getCallingUid() {
62 return IPCThreadState::self()->getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063}
64
65// ----------------------------------------------------------------------------
66
Chih-Chung Change25cc652010-05-06 16:36:58 +080067// This is ugly and only safe if we never re-create the CameraService, but
68// should be ok for now.
69static CameraService *gCameraService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070
Chih-Chung Change25cc652010-05-06 16:36:58 +080071CameraService::CameraService()
72:mSoundRef(0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073{
Chih-Chung Change25cc652010-05-06 16:36:58 +080074 LOGI("CameraService started (pid=%d)", getpid());
75
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +080076 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++) {
Chih-Chung Change25cc652010-05-06 16:36:58 +080084 setCameraFree(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 }
Chih-Chung Change25cc652010-05-06 16:36:58 +080086
87 gCameraService = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088}
89
Chih-Chung Change25cc652010-05-06 16:36:58 +080090CameraService::~CameraService() {
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +080091 for (int i = 0; i < mNumberOfCameras; i++) {
Chih-Chung Change25cc652010-05-06 16:36:58 +080092 if (mBusy[i]) {
93 LOGE("camera %d is still in use in destructor!", i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 }
95 }
96
Chih-Chung Change25cc652010-05-06 16:36:58 +080097 gCameraService = NULL;
98}
99
100int32_t CameraService::getNumberOfCameras() {
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800101 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;
Chih-Chung Change25cc652010-05-06 16:36:58 +0800112}
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;
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800120 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
Chih-Chung Change25cc652010-05-06 16:36:58 +0800121 LOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).",
122 callingPid, cameraId);
123 return NULL;
Chih-Chung Changd5d1ebd2009-06-24 19:59:31 +0800124 }
125
Chih-Chung Change25cc652010-05-06 16:36:58 +0800126 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 Lie7044382010-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 }
154 client = new Client(this, cameraClient, hardware, cameraId, callingPid);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800155 mClient[cameraId] = client;
156 LOG1("CameraService::connect X");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 return client;
158}
159
Chih-Chung Change25cc652010-05-06 16:36:58 +0800160void CameraService::removeClient(const sp<ICameraClient>& cameraClient) {
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800161 int callingPid = getCallingPid();
Chih-Chung Change25cc652010-05-06 16:36:58 +0800162 LOG1("CameraService::removeClient E (pid %d)", callingPid);
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800163
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800164 for (int i = 0; i < mNumberOfCameras; i++) {
Chih-Chung Change25cc652010-05-06 16:36:58 +0800165 // Declare this before the lock to make absolutely sure the
166 // destructor won't be called with the lock held.
167 sp<Client> client;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168
Chih-Chung Change25cc652010-05-06 16:36:58 +0800169 Mutex::Autolock lock(mServiceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170
Chih-Chung Change25cc652010-05-06 16:36:58 +0800171 // This happens when we have already disconnected (or this is
172 // just another unused camera).
173 if (mClient[i] == 0) continue;
174
175 // Promote mClient. It can fail if we are called from this path:
176 // Client::~Client() -> disconnect() -> removeClient().
177 client = mClient[i].promote();
178
179 if (client == 0) {
180 mClient[i].clear();
181 continue;
182 }
183
184 if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) {
185 // Found our camera, clear and leave.
186 LOG1("removeClient: clear camera %d", i);
187 mClient[i].clear();
188 break;
189 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 }
191
Chih-Chung Change25cc652010-05-06 16:36:58 +0800192 LOG1("CameraService::removeClient X (pid %d)", callingPid);
193}
194
195sp<CameraService::Client> CameraService::getClientById(int cameraId) {
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800196 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
Chih-Chung Change25cc652010-05-06 16:36:58 +0800197 return mClient[cameraId].promote();
198}
199
Chih-Chung Change25cc652010-05-06 16:36:58 +0800200status_t CameraService::onTransact(
201 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
202 // Permission checks
203 switch (code) {
204 case BnCameraService::CONNECT:
205 const int pid = getCallingPid();
206 const int self_pid = getpid();
207 if (pid != self_pid) {
208 // we're called from a different process, do the real check
209 if (!checkCallingPermission(
210 String16("android.permission.CAMERA"))) {
211 const int uid = getCallingUid();
212 LOGE("Permission Denial: "
213 "can't use the camera pid=%d, uid=%d", pid, uid);
214 return PERMISSION_DENIED;
215 }
216 }
217 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 }
219
Chih-Chung Change25cc652010-05-06 16:36:58 +0800220 return BnCameraService::onTransact(code, data, reply, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221}
222
Chih-Chung Change25cc652010-05-06 16:36:58 +0800223// The reason we need this busy bit is a new CameraService::connect() request
224// may come in while the previous Client's destructor has not been run or is
225// still running. If the last strong reference of the previous Client is gone
226// but the destructor has not been finished, we should not allow the new Client
227// to be created because we need to wait for the previous Client to tear down
228// the hardware first.
229void CameraService::setCameraBusy(int cameraId) {
230 android_atomic_write(1, &mBusy[cameraId]);
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800231}
232
Chih-Chung Change25cc652010-05-06 16:36:58 +0800233void CameraService::setCameraFree(int cameraId) {
234 android_atomic_write(0, &mBusy[cameraId]);
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800235}
236
Chih-Chung Change25cc652010-05-06 16:36:58 +0800237// We share the media players for shutter and recording sound for all clients.
238// A reference count is kept to determine when we will actually release the
239// media players.
240
241static MediaPlayer* newMediaPlayer(const char *file) {
242 MediaPlayer* mp = new MediaPlayer();
243 if (mp->setDataSource(file, NULL) == NO_ERROR) {
Eric Laurenta553c252009-07-17 12:17:14 -0700244 mp->setAudioStreamType(AudioSystem::ENFORCED_AUDIBLE);
Jason Samsb18b6912009-03-24 20:21:36 -0700245 mp->prepare();
246 } else {
Chih-Chung Change25cc652010-05-06 16:36:58 +0800247 LOGE("Failed to load CameraService sounds: %s", file);
248 return NULL;
Jason Samsb18b6912009-03-24 20:21:36 -0700249 }
250 return mp;
251}
252
Chih-Chung Change25cc652010-05-06 16:36:58 +0800253void CameraService::loadSound() {
254 Mutex::Autolock lock(mSoundLock);
255 LOG1("CameraService::loadSound ref=%d", mSoundRef);
256 if (mSoundRef++) return;
257
258 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
259 mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
260}
261
262void CameraService::releaseSound() {
263 Mutex::Autolock lock(mSoundLock);
264 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
265 if (--mSoundRef) return;
266
267 for (int i = 0; i < NUM_SOUNDS; i++) {
268 if (mSoundPlayer[i] != 0) {
269 mSoundPlayer[i]->disconnect();
270 mSoundPlayer[i].clear();
271 }
272 }
273}
274
275void CameraService::playSound(sound_kind kind) {
276 LOG1("playSound(%d)", kind);
277 Mutex::Autolock lock(mSoundLock);
278 sp<MediaPlayer> player = mSoundPlayer[kind];
279 if (player != 0) {
280 // do not play the sound if stream volume is 0
281 // (typically because ringer mode is silent).
282 int index;
283 AudioSystem::getStreamVolumeIndex(AudioSystem::ENFORCED_AUDIBLE, &index);
284 if (index != 0) {
285 player->seekTo(0);
286 player->start();
287 }
288 }
289}
290
291// ----------------------------------------------------------------------------
292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lie7044382010-08-17 15:45:37 -0700294 const sp<ICameraClient>& cameraClient,
295 const sp<CameraHardwareInterface>& hardware,
296 int cameraId, int clientPid) {
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800297 int callingPid = getCallingPid();
Chih-Chung Change25cc652010-05-06 16:36:58 +0800298 LOG1("Client::Client E (pid %d)", callingPid);
299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 mCameraService = cameraService;
301 mCameraClient = cameraClient;
Wu-cheng Lie7044382010-08-17 15:45:37 -0700302 mHardware = hardware;
Chih-Chung Change25cc652010-05-06 16:36:58 +0800303 mCameraId = cameraId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 mClientPid = clientPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 mUseOverlay = mHardware->useOverlay();
Chih-Chung Change25cc652010-05-06 16:36:58 +0800306 mMsgEnabled = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307
Benny Wongda83f462009-08-12 12:01:27 -0500308 mHardware->setCallbacks(notifyCallback,
309 dataCallback,
310 dataCallbackTimestamp,
Chih-Chung Change25cc652010-05-06 16:36:58 +0800311 (void *)cameraId);
Benny Wongda83f462009-08-12 12:01:27 -0500312
313 // Enable zoom, error, and focus messages by default
Chih-Chung Change25cc652010-05-06 16:36:58 +0800314 enableMsgType(CAMERA_MSG_ERROR |
315 CAMERA_MSG_ZOOM |
316 CAMERA_MSG_FOCUS);
Benny Wong6d2090e2009-07-15 18:44:27 -0500317 mOverlayW = 0;
318 mOverlayH = 0;
Jason Samsb18b6912009-03-24 20:21:36 -0700319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 // Callback is disabled by default
321 mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800322 mOrientation = 0;
Nipun Kwatra088146a2010-09-11 19:31:10 -0700323 mPlayShutterSound = true;
Chih-Chung Change25cc652010-05-06 16:36:58 +0800324 cameraService->setCameraBusy(cameraId);
325 cameraService->loadSound();
326 LOG1("Client::Client X (pid %d)", callingPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327}
328
Chih-Chung Change25cc652010-05-06 16:36:58 +0800329static void *unregister_surface(void *arg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 ISurface *surface = (ISurface *)arg;
331 surface->unregisterBuffers();
332 IPCThreadState::self()->flushCommands();
333 return NULL;
334}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335
Chih-Chung Change25cc652010-05-06 16:36:58 +0800336// tear down the client
337CameraService::Client::~Client() {
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800338 int callingPid = getCallingPid();
Chih-Chung Change25cc652010-05-06 16:36:58 +0800339 LOG1("Client::~Client E (pid %d, this %p)", callingPid, this);
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800340
Chih-Chung Change25cc652010-05-06 16:36:58 +0800341 // set mClientPid to let disconnet() tear down the hardware
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800342 mClientPid = callingPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 disconnect();
Chih-Chung Change25cc652010-05-06 16:36:58 +0800344 mCameraService->releaseSound();
345 LOG1("Client::~Client X (pid %d, this %p)", callingPid, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346}
347
Chih-Chung Change25cc652010-05-06 16:36:58 +0800348// ----------------------------------------------------------------------------
349
350status_t CameraService::Client::checkPid() const {
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800351 int callingPid = getCallingPid();
Chih-Chung Change25cc652010-05-06 16:36:58 +0800352 if (callingPid == mClientPid) return NO_ERROR;
James Dong71d714c2010-06-09 15:57:48 -0700353
Chih-Chung Change25cc652010-05-06 16:36:58 +0800354 LOGW("attempt to use a locked camera from a different process"
355 " (old pid %d, new pid %d)", mClientPid, callingPid);
356 return EBUSY;
357}
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800358
Chih-Chung Change25cc652010-05-06 16:36:58 +0800359status_t CameraService::Client::checkPidAndHardware() const {
360 status_t result = checkPid();
361 if (result != NO_ERROR) return result;
362 if (mHardware == 0) {
363 LOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid());
364 return INVALID_OPERATION;
365 }
366 return NO_ERROR;
367}
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800368
Chih-Chung Change25cc652010-05-06 16:36:58 +0800369status_t CameraService::Client::lock() {
370 int callingPid = getCallingPid();
371 LOG1("lock (pid %d)", callingPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800373
374 // lock camera to this client if the the camera is unlocked
375 if (mClientPid == 0) {
376 mClientPid = callingPid;
377 return NO_ERROR;
378 }
379
380 // returns NO_ERROR if the client already owns the camera, EBUSY otherwise
381 return checkPid();
382}
383
384status_t CameraService::Client::unlock() {
385 int callingPid = getCallingPid();
386 LOG1("unlock (pid %d)", callingPid);
387 Mutex::Autolock lock(mLock);
388
389 // allow anyone to use camera (after they lock the camera)
390 status_t result = checkPid();
391 if (result == NO_ERROR) {
392 mClientPid = 0;
393 LOG1("clear mCameraClient (pid %d)", callingPid);
394 // we need to remove the reference to ICameraClient so that when the app
395 // goes away, the reference count goes to 0.
396 mCameraClient.clear();
397 }
398 return result;
399}
400
401// connect a new client to the camera
402status_t CameraService::Client::connect(const sp<ICameraClient>& client) {
403 int callingPid = getCallingPid();
404 LOG1("connect E (pid %d)", callingPid);
405 Mutex::Autolock lock(mLock);
406
407 if (mClientPid != 0 && checkPid() != NO_ERROR) {
408 LOGW("Tried to connect to a locked camera (old pid %d, new pid %d)",
409 mClientPid, callingPid);
410 return EBUSY;
411 }
412
413 if (mCameraClient != 0 && (client->asBinder() == mCameraClient->asBinder())) {
414 LOG1("Connect to the same client");
415 return NO_ERROR;
416 }
417
418 mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
419 mClientPid = callingPid;
420 mCameraClient = client;
421
422 LOG1("connect X (pid %d)", callingPid);
423 return NO_ERROR;
424}
425
426void CameraService::Client::disconnect() {
427 int callingPid = getCallingPid();
428 LOG1("disconnect E (pid %d)", callingPid);
429 Mutex::Autolock lock(mLock);
430
431 if (checkPid() != NO_ERROR) {
432 LOGW("different client - don't disconnect");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 return;
434 }
Chih-Chung Change25cc652010-05-06 16:36:58 +0800435
436 if (mClientPid <= 0) {
437 LOG1("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 return;
439 }
440
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800441 // Make sure disconnect() is done once and once only, whether it is called
442 // from the user directly, or called by the destructor.
443 if (mHardware == 0) return;
444
Chih-Chung Change25cc652010-05-06 16:36:58 +0800445 LOG1("hardware teardown");
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800446 // Before destroying mHardware, we must make sure it's in the
447 // idle state.
Chih-Chung Change25cc652010-05-06 16:36:58 +0800448 // Turn off all messages.
449 disableMsgType(CAMERA_MSG_ALL_MSGS);
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800450 mHardware->stopPreview();
Benny Wongda83f462009-08-12 12:01:27 -0500451 mHardware->cancelPicture();
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800452 // Release the hardware resources.
453 mHardware->release();
Benny Wong6d2090e2009-07-15 18:44:27 -0500454 // Release the held overlay resources.
Chih-Chung Change25cc652010-05-06 16:36:58 +0800455 if (mUseOverlay) {
Benny Wong6d2090e2009-07-15 18:44:27 -0500456 mOverlayRef = 0;
457 }
Jamie Gennis85cfdd02010-08-10 16:37:53 -0700458 // Release the held ANativeWindow resources.
459 if (mPreviewWindow != 0) {
460 mPreviewWindow = 0;
461 mHardware->setPreviewWindow(mPreviewWindow);
462 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 mHardware.clear();
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800464
Chih-Chung Changd5d1ebd2009-06-24 19:59:31 +0800465 mCameraService->removeClient(mCameraClient);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800466 mCameraService->setCameraFree(mCameraId);
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800467
Chih-Chung Change25cc652010-05-06 16:36:58 +0800468 LOG1("disconnect X (pid %d)", callingPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469}
470
Chih-Chung Change25cc652010-05-06 16:36:58 +0800471// ----------------------------------------------------------------------------
472
Jamie Gennis85cfdd02010-08-10 16:37:53 -0700473// set the Surface that the preview will use
474status_t CameraService::Client::setPreviewDisplay(const sp<Surface>& surface) {
Chih-Chung Change25cc652010-05-06 16:36:58 +0800475 LOG1("setPreviewDisplay(%p) (pid %d)", surface.get(), getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800477 status_t result = checkPidAndHardware();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 if (result != NO_ERROR) return result;
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800479
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800480 result = NO_ERROR;
Chih-Chung Change25cc652010-05-06 16:36:58 +0800481
482 // return if no change in surface.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 // asBinder() is safe on NULL (returns NULL)
Jamie Gennis85cfdd02010-08-10 16:37:53 -0700484 if (getISurface(surface)->asBinder() == mSurface->asBinder()) {
Chih-Chung Change25cc652010-05-06 16:36:58 +0800485 return result;
486 }
487
488 if (mSurface != 0) {
489 LOG1("clearing old preview surface %p", mSurface.get());
490 if (mUseOverlay) {
491 // Force the destruction of any previous overlay
492 sp<Overlay> dummy;
493 mHardware->setOverlay(dummy);
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800494 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 }
Jamie Gennis85cfdd02010-08-10 16:37:53 -0700496 if (surface != 0) {
497 mSurface = getISurface(surface);
498 } else {
499 mSurface = 0;
500 }
501 mPreviewWindow = surface;
Chih-Chung Change25cc652010-05-06 16:36:58 +0800502 mOverlayRef = 0;
503 // If preview has been already started, set overlay or register preview
504 // buffers now.
505 if (mHardware->previewEnabled()) {
506 if (mUseOverlay) {
507 result = setOverlay();
Jamie Gennis85cfdd02010-08-10 16:37:53 -0700508 } else if (mPreviewWindow != 0) {
509 result = mHardware->setPreviewWindow(mPreviewWindow);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800510 }
511 }
512
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800513 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514}
515
Chih-Chung Change25cc652010-05-06 16:36:58 +0800516status_t CameraService::Client::setOverlay() {
517 int w, h;
518 CameraParameters params(mHardware->getParameters());
519 params.getPreviewSize(&w, &h);
520
521 if (w != mOverlayW || h != mOverlayH) {
522 // Force the destruction of any previous overlay
523 sp<Overlay> dummy;
524 mHardware->setOverlay(dummy);
525 mOverlayRef = 0;
526 }
527
528 status_t result = NO_ERROR;
529 if (mSurface == 0) {
530 result = mHardware->setOverlay(NULL);
531 } else {
532 if (mOverlayRef == 0) {
533 // FIXME:
534 // Surfaceflinger may hold onto the previous overlay reference for some
535 // time after we try to destroy it. retry a few times. In the future, we
536 // should make the destroy call block, or possibly specify that we can
537 // wait in the createOverlay call if the previous overlay is in the
538 // process of being destroyed.
539 for (int retry = 0; retry < 50; ++retry) {
540 mOverlayRef = mSurface->createOverlay(w, h, OVERLAY_FORMAT_DEFAULT,
541 mOrientation);
542 if (mOverlayRef != 0) break;
543 LOGW("Overlay create failed - retrying");
544 usleep(20000);
545 }
546 if (mOverlayRef == 0) {
547 LOGE("Overlay Creation Failed!");
548 return -EINVAL;
549 }
550 result = mHardware->setOverlay(new Overlay(mOverlayRef));
551 }
552 }
553 if (result != NO_ERROR) {
554 LOGE("mHardware->setOverlay() failed with status %d\n", result);
555 return result;
556 }
557
558 mOverlayW = w;
559 mOverlayH = h;
560
561 return result;
562}
563
564// set the preview callback flag to affect how the received frames from
565// preview are handled.
566void CameraService::Client::setPreviewCallbackFlag(int callback_flag) {
567 LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid());
568 Mutex::Autolock lock(mLock);
569 if (checkPidAndHardware() != NO_ERROR) return;
570
571 mPreviewCallbackFlag = callback_flag;
Wu-cheng Lid02c8122010-09-03 16:40:32 -0700572 if (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ENABLE_MASK) {
573 enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
574 } else {
575 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800576 }
577}
578
579// start preview mode
580status_t CameraService::Client::startPreview() {
581 LOG1("startPreview (pid %d)", getCallingPid());
582 return startCameraMode(CAMERA_PREVIEW_MODE);
583}
584
585// start recording mode
586status_t CameraService::Client::startRecording() {
587 LOG1("startRecording (pid %d)", getCallingPid());
588 return startCameraMode(CAMERA_RECORDING_MODE);
589}
590
591// start preview or recording
592status_t CameraService::Client::startCameraMode(camera_mode mode) {
593 LOG1("startCameraMode(%d)", mode);
594 Mutex::Autolock lock(mLock);
595 status_t result = checkPidAndHardware();
596 if (result != NO_ERROR) return result;
597
598 switch(mode) {
599 case CAMERA_PREVIEW_MODE:
Jamie Gennis85cfdd02010-08-10 16:37:53 -0700600 if (mSurface == 0 && mPreviewWindow == 0) {
Chih-Chung Change25cc652010-05-06 16:36:58 +0800601 LOG1("mSurface is not set yet.");
602 // still able to start preview in this case.
603 }
604 return startPreviewMode();
605 case CAMERA_RECORDING_MODE:
Jamie Gennis85cfdd02010-08-10 16:37:53 -0700606 if (mSurface == 0 && mPreviewWindow == 0) {
607 LOGE("mSurface or mPreviewWindow must be set before startRecordingMode.");
Chih-Chung Change25cc652010-05-06 16:36:58 +0800608 return INVALID_OPERATION;
609 }
610 return startRecordingMode();
611 default:
612 return UNKNOWN_ERROR;
613 }
614}
615
616status_t CameraService::Client::startPreviewMode() {
617 LOG1("startPreviewMode");
618 status_t result = NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619
620 // if preview has been enabled, nothing needs to be done
621 if (mHardware->previewEnabled()) {
622 return NO_ERROR;
623 }
624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 if (mUseOverlay) {
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800626 // If preview display has been set, set overlay now.
627 if (mSurface != 0) {
Chih-Chung Change25cc652010-05-06 16:36:58 +0800628 result = setOverlay();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 }
Chih-Chung Change25cc652010-05-06 16:36:58 +0800630 if (result != NO_ERROR) return result;
631 result = mHardware->startPreview();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 } else {
Jamie Gennis85cfdd02010-08-10 16:37:53 -0700633 // XXX: Set the orientation of the ANativeWindow.
634 mHardware->setPreviewWindow(mPreviewWindow);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800635 result = mHardware->startPreview();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 }
Chih-Chung Change25cc652010-05-06 16:36:58 +0800637 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638}
639
Chih-Chung Change25cc652010-05-06 16:36:58 +0800640status_t CameraService::Client::startRecordingMode() {
641 LOG1("startRecordingMode");
642 status_t result = NO_ERROR;
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700643
Chih-Chung Change25cc652010-05-06 16:36:58 +0800644 // if recording has been enabled, nothing needs to be done
645 if (mHardware->recordingEnabled()) {
646 return NO_ERROR;
647 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648
Chih-Chung Change25cc652010-05-06 16:36:58 +0800649 // if preview has not been started, start preview first
650 if (!mHardware->previewEnabled()) {
651 result = startPreviewMode();
652 if (result != NO_ERROR) {
653 return result;
Eric Laurent524dc042009-11-27 05:07:55 -0800654 }
Jason Samsb18b6912009-03-24 20:21:36 -0700655 }
Benny Wongda83f462009-08-12 12:01:27 -0500656
Chih-Chung Change25cc652010-05-06 16:36:58 +0800657 // start recording mode
658 enableMsgType(CAMERA_MSG_VIDEO_FRAME);
659 mCameraService->playSound(SOUND_RECORDING);
660 result = mHardware->startRecording();
661 if (result != NO_ERROR) {
662 LOGE("mHardware->startRecording() failed with status %d", result);
663 }
664 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665}
666
667// stop preview mode
Chih-Chung Change25cc652010-05-06 16:36:58 +0800668void CameraService::Client::stopPreview() {
669 LOG1("stopPreview (pid %d)", getCallingPid());
670 Mutex::Autolock lock(mLock);
671 if (checkPidAndHardware() != NO_ERROR) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672
Jamie Gennis85cfdd02010-08-10 16:37:53 -0700673
Chih-Chung Change25cc652010-05-06 16:36:58 +0800674 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
675 mHardware->stopPreview();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676
Chih-Chung Change25cc652010-05-06 16:36:58 +0800677 mPreviewBuffer.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678}
679
680// stop recording mode
Chih-Chung Change25cc652010-05-06 16:36:58 +0800681void CameraService::Client::stopRecording() {
682 LOG1("stopRecording (pid %d)", getCallingPid());
683 Mutex::Autolock lock(mLock);
684 if (checkPidAndHardware() != NO_ERROR) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685
Chih-Chung Change25cc652010-05-06 16:36:58 +0800686 mCameraService->playSound(SOUND_RECORDING);
687 disableMsgType(CAMERA_MSG_VIDEO_FRAME);
688 mHardware->stopRecording();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689
Chih-Chung Change25cc652010-05-06 16:36:58 +0800690 mPreviewBuffer.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691}
692
693// release a recording frame
Chih-Chung Change25cc652010-05-06 16:36:58 +0800694void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800696 if (checkPidAndHardware() != NO_ERROR) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 mHardware->releaseRecordingFrame(mem);
698}
699
Chih-Chung Change25cc652010-05-06 16:36:58 +0800700bool CameraService::Client::previewEnabled() {
701 LOG1("previewEnabled (pid %d)", getCallingPid());
702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800704 if (checkPidAndHardware() != NO_ERROR) return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 return mHardware->previewEnabled();
706}
707
Chih-Chung Change25cc652010-05-06 16:36:58 +0800708bool CameraService::Client::recordingEnabled() {
709 LOG1("recordingEnabled (pid %d)", getCallingPid());
710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800712 if (checkPidAndHardware() != NO_ERROR) return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 return mHardware->recordingEnabled();
714}
715
Chih-Chung Change25cc652010-05-06 16:36:58 +0800716status_t CameraService::Client::autoFocus() {
717 LOG1("autoFocus (pid %d)", getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718
719 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800720 status_t result = checkPidAndHardware();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 if (result != NO_ERROR) return result;
722
Benny Wongda83f462009-08-12 12:01:27 -0500723 return mHardware->autoFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724}
725
Chih-Chung Change25cc652010-05-06 16:36:58 +0800726status_t CameraService::Client::cancelAutoFocus() {
727 LOG1("cancelAutoFocus (pid %d)", getCallingPid());
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800728
729 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800730 status_t result = checkPidAndHardware();
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800731 if (result != NO_ERROR) return result;
732
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800733 return mHardware->cancelAutoFocus();
734}
735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736// take a picture - image is returned in callback
Chih-Chung Change25cc652010-05-06 16:36:58 +0800737status_t CameraService::Client::takePicture() {
738 LOG1("takePicture (pid %d)", getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739
740 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800741 status_t result = checkPidAndHardware();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 if (result != NO_ERROR) return result;
743
Chih-Chung Change25cc652010-05-06 16:36:58 +0800744 enableMsgType(CAMERA_MSG_SHUTTER |
745 CAMERA_MSG_POSTVIEW_FRAME |
746 CAMERA_MSG_RAW_IMAGE |
747 CAMERA_MSG_COMPRESSED_IMAGE);
Benny Wongda83f462009-08-12 12:01:27 -0500748
749 return mHardware->takePicture();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750}
751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752// set preview/capture parameters - key/value pairs
Chih-Chung Change25cc652010-05-06 16:36:58 +0800753status_t CameraService::Client::setParameters(const String8& params) {
754 LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755
756 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800757 status_t result = checkPidAndHardware();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 if (result != NO_ERROR) return result;
759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 CameraParameters p(params);
James Dong6085b4e2009-09-13 17:10:24 -0700761 return mHardware->setParameters(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762}
763
764// get preview/capture parameters - key/value pairs
Chih-Chung Change25cc652010-05-06 16:36:58 +0800765String8 CameraService::Client::getParameters() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800767 if (checkPidAndHardware() != NO_ERROR) return String8();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768
Wu-cheng Liab5b4242009-04-22 16:21:26 +0800769 String8 params(mHardware->getParameters().flatten());
Chih-Chung Change25cc652010-05-06 16:36:58 +0800770 LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
Wu-cheng Liab5b4242009-04-22 16:21:26 +0800771 return params;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772}
773
Nipun Kwatra088146a2010-09-11 19:31:10 -0700774// enable shutter sound
775status_t CameraService::Client::enableShutterSound(bool enable) {
776 LOG1("enableShutterSound (pid %d)", getCallingPid());
777
778 status_t result = checkPidAndHardware();
779 if (result != NO_ERROR) return result;
780
781 if (enable) {
782 mPlayShutterSound = true;
783 return OK;
784 }
785
786 // Disabling shutter sound may not be allowed. In that case only
787 // allow the mediaserver process to disable the sound.
788 char value[PROPERTY_VALUE_MAX];
789 property_get("ro.camera.sound.forced", value, "0");
790 if (strcmp(value, "0") != 0) {
791 // Disabling shutter sound is not allowed. Deny if the current
792 // process is not mediaserver.
793 if (getCallingPid() != getpid()) {
794 LOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid());
795 return PERMISSION_DENIED;
796 }
797 }
798
799 mPlayShutterSound = false;
800 return OK;
801}
802
Chih-Chung Change25cc652010-05-06 16:36:58 +0800803status_t CameraService::Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
804 LOG1("sendCommand (pid %d)", getCallingPid());
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700805 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800806 status_t result = checkPidAndHardware();
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700807 if (result != NO_ERROR) return result;
808
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800809 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
810 // The orientation cannot be set during preview.
811 if (mHardware->previewEnabled()) {
812 return INVALID_OPERATION;
813 }
814 switch (arg1) {
815 case 0:
816 mOrientation = ISurface::BufferHeap::ROT_0;
817 break;
818 case 90:
819 mOrientation = ISurface::BufferHeap::ROT_90;
820 break;
821 case 180:
822 mOrientation = ISurface::BufferHeap::ROT_180;
823 break;
824 case 270:
825 mOrientation = ISurface::BufferHeap::ROT_270;
826 break;
827 default:
828 return BAD_VALUE;
829 }
830 return OK;
Nipun Kwatra088146a2010-09-11 19:31:10 -0700831 } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) {
832 switch (arg1) {
833 case 0:
834 enableShutterSound(false);
835 break;
836 case 1:
837 enableShutterSound(true);
838 break;
839 default:
840 return BAD_VALUE;
841 }
842 return OK;
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800843 }
844
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700845 return mHardware->sendCommand(cmd, arg1, arg2);
846}
847
Chih-Chung Change25cc652010-05-06 16:36:58 +0800848// ----------------------------------------------------------------------------
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;
988 if (!client->lockIfMessageWanted(msgType)) return;
989
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 Kwatra088146a2010-09-11 19:31:10 -07001003 if (mPlayShutterSound) {
1004 mCameraService->playSound(SOUND_SHUTTER);
1005 }
Chih-Chung Change25cc652010-05-06 16:36:58 +08001006
Chih-Chung Change25cc652010-05-06 16:36:58 +08001007 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
1015 // It takes some time before yuvPicture callback to be called.
1016 // Register the buffer for raw image here to reduce latency.
1017 if (mSurface != 0 && !mUseOverlay) {
1018 int w, h;
1019 CameraParameters params(mHardware->getParameters());
1020 if (size == NULL) {
1021 params.getPictureSize(&w, &h);
1022 } else {
1023 w = size->width;
1024 h = size->height;
1025 w &= ~1;
1026 h &= ~1;
1027 LOG1("Snapshot image width=%d, height=%d", w, h);
1028 }
1029 // FIXME: don't use hardcoded format constants here
1030 ISurface::BufferHeap buffers(w, h, w, h,
1031 HAL_PIXEL_FORMAT_YCrCb_420_SP, mOrientation, 0,
1032 mHardware->getRawHeap());
1033
Chih-Chung Chang3ef6ebe2010-07-02 07:48:03 -07001034 IPCThreadState::self()->flushCommands();
Chih-Chung Change25cc652010-05-06 16:36:58 +08001035 }
1036
1037 mLock.unlock();
1038}
1039
1040// preview callback - frame buffer update
1041void CameraService::Client::handlePreviewData(const sp<IMemory>& mem) {
1042 ssize_t offset;
1043 size_t size;
1044 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1045
Chih-Chung Change25cc652010-05-06 16:36:58 +08001046 // local copy of the callback flags
1047 int flags = mPreviewCallbackFlag;
1048
1049 // is callback enabled?
1050 if (!(flags & FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
1051 // If the enable bit is off, the copy-out and one-shot bits are ignored
1052 LOG2("frame callback is disabled");
1053 mLock.unlock();
1054 return;
1055 }
1056
1057 // hold a strong pointer to the client
1058 sp<ICameraClient> c = mCameraClient;
1059
1060 // clear callback flags if no client or one-shot mode
1061 if (c == 0 || (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
1062 LOG2("Disable preview callback");
1063 mPreviewCallbackFlag &= ~(FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
1064 FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
1065 FRAME_CALLBACK_FLAG_ENABLE_MASK);
Wu-cheng Lid02c8122010-09-03 16:40:32 -07001066 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
Chih-Chung Change25cc652010-05-06 16:36:58 +08001067 }
1068
1069 if (c != 0) {
1070 // Is the received frame copied out or not?
1071 if (flags & FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
1072 LOG2("frame is copied");
1073 copyFrameAndPostCopiedFrame(c, heap, offset, size);
1074 } else {
1075 LOG2("frame is forwarded");
1076 mLock.unlock();
1077 c->dataCallback(CAMERA_MSG_PREVIEW_FRAME, mem);
1078 }
1079 } else {
1080 mLock.unlock();
1081 }
1082}
1083
1084// picture callback - postview image ready
1085void CameraService::Client::handlePostview(const sp<IMemory>& mem) {
1086 disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
1087
1088 sp<ICameraClient> c = mCameraClient;
1089 mLock.unlock();
1090 if (c != 0) {
1091 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem);
1092 }
1093}
1094
1095// picture callback - raw image ready
1096void CameraService::Client::handleRawPicture(const sp<IMemory>& mem) {
1097 disableMsgType(CAMERA_MSG_RAW_IMAGE);
1098
1099 ssize_t offset;
1100 size_t size;
1101 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1102
Chih-Chung Change25cc652010-05-06 16:36:58 +08001103 sp<ICameraClient> c = mCameraClient;
1104 mLock.unlock();
1105 if (c != 0) {
1106 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem);
1107 }
1108}
1109
1110// picture callback - compressed picture ready
1111void CameraService::Client::handleCompressedPicture(const sp<IMemory>& mem) {
1112 disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
1113
1114 sp<ICameraClient> c = mCameraClient;
1115 mLock.unlock();
1116 if (c != 0) {
1117 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem);
1118 }
1119}
1120
1121
1122void CameraService::Client::handleGenericNotify(int32_t msgType,
1123 int32_t ext1, int32_t ext2) {
1124 sp<ICameraClient> c = mCameraClient;
1125 mLock.unlock();
1126 if (c != 0) {
1127 c->notifyCallback(msgType, ext1, ext2);
1128 }
1129}
1130
1131void CameraService::Client::handleGenericData(int32_t msgType,
1132 const sp<IMemory>& dataPtr) {
1133 sp<ICameraClient> c = mCameraClient;
1134 mLock.unlock();
1135 if (c != 0) {
1136 c->dataCallback(msgType, dataPtr);
1137 }
1138}
1139
1140void CameraService::Client::handleGenericDataTimestamp(nsecs_t timestamp,
1141 int32_t msgType, const sp<IMemory>& dataPtr) {
1142 sp<ICameraClient> c = mCameraClient;
1143 mLock.unlock();
1144 if (c != 0) {
1145 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
1146 }
1147}
1148
1149void CameraService::Client::copyFrameAndPostCopiedFrame(
1150 const sp<ICameraClient>& client, const sp<IMemoryHeap>& heap,
1151 size_t offset, size_t size) {
1152 LOG2("copyFrameAndPostCopiedFrame");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 // It is necessary to copy out of pmem before sending this to
1154 // the callback. For efficiency, reuse the same MemoryHeapBase
1155 // provided it's big enough. Don't allocate the memory or
1156 // perform the copy if there's no callback.
Dave Sparks05fd0df2009-11-10 17:08:08 -08001157 // hold the preview lock while we grab a reference to the preview buffer
Dave Sparksc8093c12009-11-06 11:47:13 -08001158 sp<MemoryHeapBase> previewBuffer;
Chih-Chung Change25cc652010-05-06 16:36:58 +08001159
1160 if (mPreviewBuffer == 0) {
1161 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1162 } else if (size > mPreviewBuffer->virtualSize()) {
1163 mPreviewBuffer.clear();
1164 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 }
Chih-Chung Change25cc652010-05-06 16:36:58 +08001166 if (mPreviewBuffer == 0) {
1167 LOGE("failed to allocate space for preview buffer");
1168 mLock.unlock();
1169 return;
1170 }
1171 previewBuffer = mPreviewBuffer;
1172
1173 memcpy(previewBuffer->base(), (uint8_t *)heap->base() + offset, size);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174
Dave Sparksc8093c12009-11-06 11:47:13 -08001175 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 if (frame == 0) {
1177 LOGE("failed to allocate space for frame callback");
Chih-Chung Change25cc652010-05-06 16:36:58 +08001178 mLock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 return;
1180 }
Chih-Chung Change25cc652010-05-06 16:36:58 +08001181
1182 mLock.unlock();
Dave Sparksdd158c92009-10-15 10:02:22 -07001183 client->dataCallback(CAMERA_MSG_PREVIEW_FRAME, frame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184}
1185
Chih-Chung Change25cc652010-05-06 16:36:58 +08001186// ----------------------------------------------------------------------------
1187
Chih-Chung Change86ae1b2010-03-05 11:33:03 -08001188static 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
Chih-Chung Change25cc652010-05-06 16:36:58 +08001204status_t CameraService::dump(int fd, const Vector<String16>& args) {
Chih-Chung Change86ae1b2010-03-05 11:33:03 -08001205 static const char* kDeadlockedString = "CameraService may be deadlocked\n";
1206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 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",
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +08001213 getCallingPid(),
Chih-Chung Change25cc652010-05-06 16:36:58 +08001214 getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 result.append(buffer);
1216 write(fd, result.string(), result.size());
1217 } else {
Chih-Chung Change86ae1b2010-03-05 11:33:03 -08001218 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
Chih-Chung Change25cc652010-05-06 16:36:58 +08001225 bool hasClient = false;
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +08001226 for (int i = 0; i < mNumberOfCameras; i++) {
Chih-Chung Change25cc652010-05-06 16:36:58 +08001227 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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 result.append(buffer);
1235 write(fd, result.string(), result.size());
Chih-Chung Change25cc652010-05-06 16:36:58 +08001236 client->mHardware->dump(fd, args);
1237 }
1238 if (!hasClient) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 result.append("No camera client yet.\n");
1240 write(fd, result.string(), result.size());
1241 }
Chih-Chung Change86ae1b2010-03-05 11:33:03 -08001242
1243 if (locked) mServiceLock.unlock();
Chih-Chung Change25cc652010-05-06 16:36:58 +08001244
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 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 }
1257 return NO_ERROR;
1258}
1259
Jamie Gennis85cfdd02010-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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268}; // namespace android