blob: e945056c341e3c28d6fcd53b8a64893bef63d24a [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2**
3** Copyright (C) 2008, The Android Open Source Project
4** Copyright (C) 2008 HTC Inc.
5**
6** Licensed under the Apache License, Version 2.0 (the "License");
7** you may not use this file except in compliance with the License.
8** You may obtain a copy of the License at
9**
10** http://www.apache.org/licenses/LICENSE-2.0
11**
12** Unless required by applicable law or agreed to in writing, software
13** distributed under the License is distributed on an "AS IS" BASIS,
14** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15** See the License for the specific language governing permissions and
16** limitations under the License.
17*/
18
19//#define LOG_NDEBUG 0
20#define LOG_TAG "CameraService"
21#include <utils/Log.h>
22
23#include <utils/IServiceManager.h>
24#include <utils/IPCThreadState.h>
25#include <utils/String16.h>
26#include <utils/Errors.h>
27#include <utils/MemoryBase.h>
28#include <utils/MemoryHeapBase.h>
29#include <ui/ICameraService.h>
30
Jason Sams78b877e2009-03-24 20:21:36 -070031#include <media/mediaplayer.h>
32#include <media/AudioSystem.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033#include "CameraService.h"
34
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +080035#include <cutils/atomic.h>
Eric Laurentcbcb00e2009-03-27 16:27:16 -070036#include <cutils/properties.h>
37
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038namespace android {
39
40extern "C" {
41#include <stdio.h>
42#include <sys/types.h>
43#include <sys/stat.h>
44#include <fcntl.h>
45#include <pthread.h>
Chih-Chung Changd98c5162009-06-22 16:03:41 +080046#include <signal.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047}
48
49// When you enable this, as well as DEBUG_REFS=1 and
50// DEBUG_REFS_ENABLED_BY_DEFAULT=0 in libutils/RefBase.cpp, this will track all
51// references to the CameraService::Client in order to catch the case where the
52// client is being destroyed while a callback from the CameraHardwareInterface
53// is outstanding. This is a serious bug because if we make another call into
54// CameraHardwreInterface that itself triggers a callback, we will deadlock.
55
56#define DEBUG_CLIENT_REFERENCES 0
57
58#define PICTURE_TIMEOUT seconds(5)
59
60#define DEBUG_DUMP_PREVIEW_FRAME_TO_FILE 0 /* n-th frame to write */
61#define DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE 0
62#define DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE 0
63
64#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
65static int debug_frame_cnt;
66#endif
67
Chih-Chung Changd98c5162009-06-22 16:03:41 +080068static int getCallingPid() {
69 return IPCThreadState::self()->getCallingPid();
70}
71
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072// ----------------------------------------------------------------------------
73
74void CameraService::instantiate() {
75 defaultServiceManager()->addService(
76 String16("media.camera"), new CameraService());
77}
78
79// ----------------------------------------------------------------------------
80
81CameraService::CameraService() :
82 BnCameraService()
83{
84 LOGI("CameraService started: pid=%d", getpid());
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +080085 mUsers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086}
87
88CameraService::~CameraService()
89{
90 if (mClient != 0) {
91 LOGE("mClient was still connected in destructor!");
92 }
93}
94
95sp<ICamera> CameraService::connect(const sp<ICameraClient>& cameraClient)
96{
Chih-Chung Changd98c5162009-06-22 16:03:41 +080097 int callingPid = getCallingPid();
98 LOGD("CameraService::connect E (pid %d, client %p)", callingPid,
99 cameraClient->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100
101 Mutex::Autolock lock(mLock);
102 sp<Client> client;
103 if (mClient != 0) {
104 sp<Client> currentClient = mClient.promote();
105 if (currentClient != 0) {
106 sp<ICameraClient> currentCameraClient(currentClient->getCameraClient());
107 if (cameraClient->asBinder() == currentCameraClient->asBinder()) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800108 // This is the same client reconnecting...
109 LOGD("CameraService::connect X (pid %d, same client %p) is reconnecting...",
110 callingPid, cameraClient->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 return currentClient;
112 } else {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800113 // It's another client... reject it
114 LOGD("CameraService::connect X (pid %d, new client %p) rejected. "
115 "(old pid %d, old client %p)",
116 callingPid, cameraClient->asBinder().get(),
117 currentClient->mClientPid, currentCameraClient->asBinder().get());
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800118 if (kill(currentClient->mClientPid, 0) == -1 && errno == ESRCH) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800119 LOGD("The old client is dead!");
120 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121 return client;
122 }
123 } else {
124 // can't promote, the previous client has died...
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800125 LOGD("New client (pid %d) connecting, old reference was dangling...",
126 callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127 mClient.clear();
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800128 if (mUsers > 0) {
129 LOGD("Still have client, rejected");
130 return client;
131 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132 }
133 }
134
135 // create a new Client object
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800136 client = new Client(this, cameraClient, callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137 mClient = client;
138#if DEBUG_CLIENT_REFERENCES
139 // Enable tracking for this object, and track increments and decrements of
140 // the refcount.
141 client->trackMe(true, true);
142#endif
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800143 LOGD("CameraService::connect X");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144 return client;
145}
146
147void CameraService::removeClient(const sp<ICameraClient>& cameraClient)
148{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800149 int callingPid = getCallingPid();
150
151 // Declare this outside the lock to make absolutely sure the
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152 // destructor won't be called with the lock held.
153 sp<Client> client;
154
155 Mutex::Autolock lock(mLock);
156
157 if (mClient == 0) {
158 // This happens when we have already disconnected.
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800159 LOGD("removeClient (pid %d): already disconnected", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160 return;
161 }
162
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800163 // Promote mClient. It can fail if we are called from this path:
164 // Client::~Client() -> disconnect() -> removeClient().
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 client = mClient.promote();
166 if (client == 0) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800167 LOGD("removeClient (pid %d): no more strong reference", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168 mClient.clear();
169 return;
170 }
171
172 if (cameraClient->asBinder() != client->getCameraClient()->asBinder()) {
173 // ugh! that's not our client!!
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800174 LOGW("removeClient (pid %d): mClient doesn't match!", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175 } else {
176 // okay, good, forget about mClient
177 mClient.clear();
178 }
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800179
180 LOGD("removeClient (pid %d) done", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181}
182
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800183// The reason we need this count is a new CameraService::connect() request may
184// come in while the previous Client's destructor has not been run or is still
185// running. If the last strong reference of the previous Client is gone but
186// destructor has not been run, we should not allow the new Client to be created
187// because we need to wait for the previous Client to tear down the hardware
188// first.
189void CameraService::incUsers() {
190 android_atomic_inc(&mUsers);
191}
192
193void CameraService::decUsers() {
194 android_atomic_dec(&mUsers);
195}
196
Jason Sams78b877e2009-03-24 20:21:36 -0700197static sp<MediaPlayer> newMediaPlayer(const char *file)
198{
199 sp<MediaPlayer> mp = new MediaPlayer();
200 if (mp->setDataSource(file) == NO_ERROR) {
Eric Laurentcbcb00e2009-03-27 16:27:16 -0700201 char value[PROPERTY_VALUE_MAX];
202 property_get("ro.camera.sound.forced", value, "0");
203 if (atoi(value)) {
204 mp->setAudioStreamType(AudioSystem::ENFORCED_AUDIBLE);
205 } else {
206 mp->setAudioStreamType(AudioSystem::SYSTEM);
207 }
Jason Sams78b877e2009-03-24 20:21:36 -0700208 mp->prepare();
209 } else {
210 mp.clear();
211 LOGE("Failed to load CameraService sounds.");
212 }
213 return mp;
214}
215
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216CameraService::Client::Client(const sp<CameraService>& cameraService,
217 const sp<ICameraClient>& cameraClient, pid_t clientPid)
218{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800219 int callingPid = getCallingPid();
220 LOGD("Client::Client E (pid %d)", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221 mCameraService = cameraService;
222 mCameraClient = cameraClient;
223 mClientPid = clientPid;
224 mHardware = openCameraHardware();
225 mUseOverlay = mHardware->useOverlay();
226
Jason Sams78b877e2009-03-24 20:21:36 -0700227 mMediaPlayerClick = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
228 mMediaPlayerBeep = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
229
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230 // Callback is disabled by default
231 mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800232 cameraService->incUsers();
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800233 LOGD("Client::Client X (pid %d)", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234}
235
236status_t CameraService::Client::checkPid()
237{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800238 int callingPid = getCallingPid();
239 if (mClientPid == callingPid) return NO_ERROR;
240 LOGW("Attempt to use locked camera (client %p) from different process "
241 " (old pid %d, new pid %d)",
242 getCameraClient()->asBinder().get(), mClientPid, callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243 return -EBUSY;
244}
245
246status_t CameraService::Client::lock()
247{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800248 int callingPid = getCallingPid();
249 LOGD("lock from pid %d (mClientPid %d)", callingPid, mClientPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250 Mutex::Autolock _l(mLock);
251 // lock camera to this client if the the camera is unlocked
252 if (mClientPid == 0) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800253 mClientPid = callingPid;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254 return NO_ERROR;
255 }
256 // returns NO_ERROR if the client already owns the camera, -EBUSY otherwise
257 return checkPid();
258}
259
260status_t CameraService::Client::unlock()
261{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800262 int callingPid = getCallingPid();
263 LOGD("unlock from pid %d (mClientPid %d)", callingPid, mClientPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264 Mutex::Autolock _l(mLock);
265 // allow anyone to use camera
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266 status_t result = checkPid();
James Dong0ae13e32009-04-20 19:35:28 -0700267 if (result == NO_ERROR) {
268 mClientPid = 0;
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800269 LOGD("clear mCameraClient (pid %d)", callingPid);
James Dong0ae13e32009-04-20 19:35:28 -0700270 // we need to remove the reference so that when app goes
271 // away, the reference count goes to 0.
272 mCameraClient.clear();
273 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274 return result;
275}
276
277status_t CameraService::Client::connect(const sp<ICameraClient>& client)
278{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800279 int callingPid = getCallingPid();
280
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281 // connect a new process to the camera
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800282 LOGD("Client::connect E (pid %d, client %p)", callingPid, client->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283
284 // I hate this hack, but things get really ugly when the media recorder
285 // service is handing back the camera to the app. The ICameraClient
286 // destructor will be called during the same IPC, making it look like
287 // the remote client is trying to disconnect. This hack temporarily
288 // sets the mClientPid to an invalid pid to prevent the hardware from
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800289 // being torn down.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800290 {
291
292 // hold a reference to the old client or we will deadlock if the client is
293 // in the same process and we hold the lock when we remove the reference
294 sp<ICameraClient> oldClient;
295 {
296 Mutex::Autolock _l(mLock);
Chih-Chung Chang34e5a152009-06-09 13:56:44 +0800297 if (mClientPid != 0 && checkPid() != NO_ERROR) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800298 LOGW("Tried to connect to locked camera (old pid %d, new pid %d)",
299 mClientPid, callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300 return -EBUSY;
301 }
302 oldClient = mCameraClient;
303
304 // did the client actually change?
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800305 if (client->asBinder() == mCameraClient->asBinder()) {
306 LOGD("Connect to the same client");
307 return NO_ERROR;
308 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800309
310 mCameraClient = client;
311 mClientPid = -1;
312 mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800313 LOGD("Connect to the new client (pid %d, client %p)",
314 callingPid, mCameraClient->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315 }
316
317 }
318 // the old client destructor is called when oldClient goes out of scope
319 // now we set the new PID to lock the interface again
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800320 mClientPid = callingPid;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321
322 return NO_ERROR;
323}
324
325#if HAVE_ANDROID_OS
326static void *unregister_surface(void *arg)
327{
328 ISurface *surface = (ISurface *)arg;
329 surface->unregisterBuffers();
330 IPCThreadState::self()->flushCommands();
331 return NULL;
332}
333#endif
334
335CameraService::Client::~Client()
336{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800337 int callingPid = getCallingPid();
338
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800339 // tear down client
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800340 LOGD("Client::~Client E (pid %d, client %p)",
341 callingPid, getCameraClient()->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800342 if (mSurface != 0 && !mUseOverlay) {
343#if HAVE_ANDROID_OS
344 pthread_t thr;
345 // We unregister the buffers in a different thread because binder does
346 // not let us make sychronous transactions in a binder destructor (that
347 // is, upon our reaching a refcount of zero.)
348 pthread_create(&thr, NULL,
349 unregister_surface,
350 mSurface.get());
351 pthread_join(thr, NULL);
352#else
353 mSurface->unregisterBuffers();
354#endif
355 }
356
Jason Sams9e431ac2009-03-24 20:36:57 -0700357 if (mMediaPlayerBeep.get() != NULL) {
358 mMediaPlayerBeep->disconnect();
359 mMediaPlayerBeep.clear();
360 }
361 if (mMediaPlayerClick.get() != NULL) {
362 mMediaPlayerClick->disconnect();
363 mMediaPlayerClick.clear();
364 }
Jason Sams78b877e2009-03-24 20:21:36 -0700365
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800366 // make sure we tear down the hardware
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800367 mClientPid = callingPid;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800368 disconnect();
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800369 LOGD("Client::~Client X (pid %d)", mClientPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800370}
371
372void CameraService::Client::disconnect()
373{
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800374 int callingPid = getCallingPid();
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800375
376 LOGD("Client::disconnect() E (pid %d client %p)",
377 callingPid, getCameraClient()->asBinder().get());
378
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800379 Mutex::Autolock lock(mLock);
380 if (mClientPid <= 0) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800381 LOGD("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800382 return;
383 }
384 if (checkPid() != NO_ERROR) {
Chih-Chung Chang6a5297d2009-06-16 17:15:04 +0800385 LOGD("Different client - don't disconnect");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800386 return;
387 }
388
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800389 // Make sure disconnect() is done once and once only, whether it is called
390 // from the user directly, or called by the destructor.
391 if (mHardware == 0) return;
392
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800393 mCameraService->removeClient(mCameraClient);
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800394
395 LOGD("hardware teardown");
396 // Before destroying mHardware, we must make sure it's in the
397 // idle state.
398 mHardware->stopPreview();
399 // Cancel all picture callbacks.
400 mHardware->cancelPicture(true, true, true);
401 // Release the hardware resources.
402 mHardware->release();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800403 mHardware.clear();
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800404
405 mCameraService->decUsers();
406
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800407 LOGD("Client::disconnect() X (pid %d)", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800408}
409
410// pass the buffered ISurface to the camera service
411status_t CameraService::Client::setPreviewDisplay(const sp<ISurface>& surface)
412{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800413 LOGD("setPreviewDisplay(%p) (pid %d)", surface.get(), getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800414 Mutex::Autolock lock(mLock);
415 status_t result = checkPid();
416 if (result != NO_ERROR) return result;
417 Mutex::Autolock surfaceLock(mSurfaceLock);
418 // asBinder() is safe on NULL (returns NULL)
419 if (surface->asBinder() != mSurface->asBinder()) {
420 if (mSurface != 0 && !mUseOverlay) {
421 LOGD("clearing old preview surface %p", mSurface.get());
422 mSurface->unregisterBuffers();
423 }
424 mSurface = surface;
425 }
426 return NO_ERROR;
427}
428
429// set the preview callback flag to affect how the received frames from
430// preview are handled.
431void CameraService::Client::setPreviewCallbackFlag(int callback_flag)
432{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800433 LOGV("setPreviewCallbackFlag (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800434 Mutex::Autolock lock(mLock);
435 if (checkPid() != NO_ERROR) return;
436 mPreviewCallbackFlag = callback_flag;
437}
438
439// start preview mode, must call setPreviewDisplay first
440status_t CameraService::Client::startCameraMode(camera_mode mode)
441{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800442 int callingPid = getCallingPid();
443
444 LOGD("startCameraMode(%d) (pid %d)", mode, callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445
446 /* we cannot call into mHardware with mLock held because
447 * mHardware has callbacks onto us which acquire this lock
448 */
449
450 Mutex::Autolock lock(mLock);
451 status_t result = checkPid();
452 if (result != NO_ERROR) return result;
453
454 if (mHardware == 0) {
455 LOGE("mHardware is NULL, returning.");
456 return INVALID_OPERATION;
457 }
458
459 if (mSurface == 0) {
460 LOGE("setPreviewDisplay must be called before startCameraMode!");
461 return INVALID_OPERATION;
462 }
463
464 switch(mode) {
465 case CAMERA_RECORDING_MODE:
466 return startRecordingMode();
467
468 default: // CAMERA_PREVIEW_MODE
469 return startPreviewMode();
470 }
471}
472
473status_t CameraService::Client::startRecordingMode()
474{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800475 LOGD("startRecordingMode (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800476
477 status_t ret = UNKNOWN_ERROR;
478
479 // if preview has not been started, start preview first
480 if (!mHardware->previewEnabled()) {
481 ret = startPreviewMode();
482 if (ret != NO_ERROR) {
483 return ret;
484 }
485 }
486
487 // if recording has been enabled, nothing needs to be done
488 if (mHardware->recordingEnabled()) {
489 return NO_ERROR;
490 }
491
492 // start recording mode
493 ret = mHardware->startRecording(recordingCallback,
494 mCameraService.get());
495 if (ret != NO_ERROR) {
496 LOGE("mHardware->startRecording() failed with status %d", ret);
497 }
498 return ret;
499}
500
501status_t CameraService::Client::startPreviewMode()
502{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800503 LOGD("startPreviewMode (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800504
505 // if preview has been enabled, nothing needs to be done
506 if (mHardware->previewEnabled()) {
507 return NO_ERROR;
508 }
509
510 // start preview mode
511#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
512 debug_frame_cnt = 0;
513#endif
514 status_t ret = UNKNOWN_ERROR;
515 int w, h;
516 CameraParameters params(mHardware->getParameters());
517 params.getPreviewSize(&w, &h);
518
519 if (mUseOverlay) {
520 const char *format = params.getPreviewFormat();
521 int fmt;
522 LOGD("Use Overlays");
523 if (!strcmp(format, "yuv422i"))
524 fmt = OVERLAY_FORMAT_YCbCr_422_I;
525 else if (!strcmp(format, "rgb565"))
526 fmt = OVERLAY_FORMAT_RGB_565;
527 else {
528 LOGE("Invalid preview format for overlays");
529 return -EINVAL;
530 }
531 sp<OverlayRef> ref = mSurface->createOverlay(w, h, fmt);
532 ret = mHardware->setOverlay(new Overlay(ref));
533 if (ret != NO_ERROR) {
534 LOGE("mHardware->setOverlay() failed with status %d\n", ret);
535 return ret;
536 }
537 ret = mHardware->startPreview(NULL, mCameraService.get());
538 if (ret != NO_ERROR)
539 LOGE("mHardware->startPreview() failed with status %d\n", ret);
540
541 } else {
542 ret = mHardware->startPreview(previewCallback,
543 mCameraService.get());
544 if (ret == NO_ERROR) {
545
546 mSurface->unregisterBuffers();
547
548 uint32_t transform = 0;
549 if (params.getOrientation() ==
550 CameraParameters::CAMERA_ORIENTATION_PORTRAIT) {
551 LOGV("portrait mode");
552 transform = ISurface::BufferHeap::ROT_90;
553 }
554 ISurface::BufferHeap buffers(w, h, w, h,
555 PIXEL_FORMAT_YCbCr_420_SP,
556 transform,
557 0,
558 mHardware->getPreviewHeap());
559
560 mSurface->registerBuffers(buffers);
561 } else {
562 LOGE("mHardware->startPreview() failed with status %d", ret);
563 }
564 }
565 return ret;
566}
567
568status_t CameraService::Client::startPreview()
569{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800570 LOGD("startPreview (pid %d)", getCallingPid());
571
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800572 return startCameraMode(CAMERA_PREVIEW_MODE);
573}
574
575status_t CameraService::Client::startRecording()
576{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800577 LOGD("startRecording (pid %d)", getCallingPid());
578
Jason Sams78b877e2009-03-24 20:21:36 -0700579 if (mMediaPlayerBeep.get() != NULL) {
Andreas Hubera26e6062009-03-24 20:47:19 -0700580 mMediaPlayerBeep->seekTo(0);
Jason Sams78b877e2009-03-24 20:21:36 -0700581 mMediaPlayerBeep->start();
582 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800583 return startCameraMode(CAMERA_RECORDING_MODE);
584}
585
586// stop preview mode
587void CameraService::Client::stopPreview()
588{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800589 LOGD("stopPreview (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800590
591 Mutex::Autolock lock(mLock);
592 if (checkPid() != NO_ERROR) return;
593
594 if (mHardware == 0) {
595 LOGE("mHardware is NULL, returning.");
596 return;
597 }
598
599 mHardware->stopPreview();
600 LOGD("stopPreview(), hardware stopped OK");
601
602 if (mSurface != 0 && !mUseOverlay) {
603 mSurface->unregisterBuffers();
604 }
605 mPreviewBuffer.clear();
606}
607
608// stop recording mode
609void CameraService::Client::stopRecording()
610{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800611 LOGD("stopRecording (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800612
613 Mutex::Autolock lock(mLock);
614 if (checkPid() != NO_ERROR) return;
615
616 if (mHardware == 0) {
617 LOGE("mHardware is NULL, returning.");
618 return;
619 }
620
Jason Sams78b877e2009-03-24 20:21:36 -0700621 if (mMediaPlayerBeep.get() != NULL) {
Andreas Hubera26e6062009-03-24 20:47:19 -0700622 mMediaPlayerBeep->seekTo(0);
Jason Sams78b877e2009-03-24 20:21:36 -0700623 mMediaPlayerBeep->start();
624 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800625 mHardware->stopRecording();
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800626 LOGD("stopRecording(), hardware stopped OK");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800627 mPreviewBuffer.clear();
628}
629
630// release a recording frame
631void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem)
632{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800633 Mutex::Autolock lock(mLock);
634 if (checkPid() != NO_ERROR) return;
635
636 if (mHardware == 0) {
637 LOGE("mHardware is NULL, returning.");
638 return;
639 }
640
641 mHardware->releaseRecordingFrame(mem);
642}
643
644bool CameraService::Client::previewEnabled()
645{
646 Mutex::Autolock lock(mLock);
647 if (mHardware == 0) return false;
648 return mHardware->previewEnabled();
649}
650
651bool CameraService::Client::recordingEnabled()
652{
653 Mutex::Autolock lock(mLock);
654 if (mHardware == 0) return false;
655 return mHardware->recordingEnabled();
656}
657
658// Safely retrieves a strong pointer to the client during a hardware callback.
659sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user)
660{
661 sp<Client> client = 0;
662 CameraService *service = static_cast<CameraService*>(user);
663 if (service != NULL) {
664 Mutex::Autolock ourLock(service->mLock);
665 if (service->mClient != 0) {
666 client = service->mClient.promote();
667 if (client == 0) {
668 LOGE("getClientFromCookie: client appears to have died");
669 service->mClient.clear();
670 }
671 } else {
672 LOGE("getClientFromCookie: got callback but client was NULL");
673 }
674 }
675 return client;
676}
677
678
679#if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE || \
680 DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE || \
681 DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
682static void dump_to_file(const char *fname,
683 uint8_t *buf, uint32_t size)
684{
685 int nw, cnt = 0;
686 uint32_t written = 0;
687
688 LOGD("opening file [%s]\n", fname);
689 int fd = open(fname, O_RDWR | O_CREAT);
690 if (fd < 0) {
691 LOGE("failed to create file [%s]: %s", fname, strerror(errno));
692 return;
693 }
694
695 LOGD("writing %d bytes to file [%s]\n", size, fname);
696 while (written < size) {
697 nw = ::write(fd,
698 buf + written,
699 size - written);
700 if (nw < 0) {
701 LOGE("failed to write to file [%s]: %s",
702 fname, strerror(errno));
703 break;
704 }
705 written += nw;
706 cnt++;
707 }
708 LOGD("done writing %d bytes to file [%s] in %d passes\n",
709 size, fname, cnt);
710 ::close(fd);
711}
712#endif
713
714// preview callback - frame buffer update
715void CameraService::Client::previewCallback(const sp<IMemory>& mem, void* user)
716{
717 LOGV("previewCallback()");
718 sp<Client> client = getClientFromCookie(user);
719 if (client == 0) {
720 return;
721 }
722
723#if DEBUG_HEAP_LEAKS && 0 // debugging
724 if (gWeakHeap == NULL) {
725 ssize_t offset;
726 size_t size;
727 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
728 if (gWeakHeap != heap) {
729 LOGD("SETTING PREVIEW HEAP");
730 heap->trackMe(true, true);
731 gWeakHeap = heap;
732 }
733 }
734#endif
735
736#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
737 {
738 if (debug_frame_cnt++ == DEBUG_DUMP_PREVIEW_FRAME_TO_FILE) {
739 ssize_t offset;
740 size_t size;
741 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
742 dump_to_file("/data/preview.yuv",
743 (uint8_t *)heap->base() + offset, size);
744 }
745 }
746#endif
747
748 // The strong pointer guarantees the client will exist, but no lock is held.
749 client->postPreviewFrame(mem);
750
751#if DEBUG_CLIENT_REFERENCES
752 //**** if the client's refcount is 1, then we are about to destroy it here,
753 // which is bad--print all refcounts.
754 if (client->getStrongCount() == 1) {
755 LOGE("++++++++++++++++ (PREVIEW) THIS WILL CAUSE A LOCKUP!");
756 client->printRefs();
757 }
758#endif
759}
760
761// recording callback
762void CameraService::Client::recordingCallback(const sp<IMemory>& mem, void* user)
763{
764 LOGV("recordingCallback");
765 sp<Client> client = getClientFromCookie(user);
766 if (client == 0) {
767 return;
768 }
769 // The strong pointer guarantees the client will exist, but no lock is held.
770 client->postRecordingFrame(mem);
771}
772
773// take a picture - image is returned in callback
774status_t CameraService::Client::autoFocus()
775{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800776 LOGD("autoFocus (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800777
778 Mutex::Autolock lock(mLock);
779 status_t result = checkPid();
780 if (result != NO_ERROR) return result;
781
782 if (mHardware == 0) {
783 LOGE("mHardware is NULL, returning.");
784 return INVALID_OPERATION;
785 }
786
787 return mHardware->autoFocus(autoFocusCallback,
788 mCameraService.get());
789}
790
791// take a picture - image is returned in callback
792status_t CameraService::Client::takePicture()
793{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800794 LOGD("takePicture (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800795
796 Mutex::Autolock lock(mLock);
797 status_t result = checkPid();
798 if (result != NO_ERROR) return result;
799
800 if (mHardware == 0) {
801 LOGE("mHardware is NULL, returning.");
802 return INVALID_OPERATION;
803 }
804
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800805 return mHardware->takePicture(shutterCallback,
806 yuvPictureCallback,
807 jpegPictureCallback,
808 mCameraService.get());
809}
810
811// picture callback - snapshot taken
812void CameraService::Client::shutterCallback(void *user)
813{
814 sp<Client> client = getClientFromCookie(user);
815 if (client == 0) {
816 return;
817 }
818
Wu-cheng Liaab44b82009-03-24 20:39:09 -0700819 // Play shutter sound.
820 if (client->mMediaPlayerClick.get() != NULL) {
Andreas Hubera26e6062009-03-24 20:47:19 -0700821 client->mMediaPlayerClick->seekTo(0);
Wu-cheng Liaab44b82009-03-24 20:39:09 -0700822 client->mMediaPlayerClick->start();
823 }
824
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800825 // Screen goes black after the buffer is unregistered.
826 if (client->mSurface != 0 && !client->mUseOverlay) {
827 client->mSurface->unregisterBuffers();
828 }
829
830 client->postShutter();
831
832 // It takes some time before yuvPicture callback to be called.
833 // Register the buffer for raw image here to reduce latency.
834 if (client->mSurface != 0 && !client->mUseOverlay) {
835 int w, h;
836 CameraParameters params(client->mHardware->getParameters());
837 params.getPictureSize(&w, &h);
838 uint32_t transform = 0;
839 if (params.getOrientation() == CameraParameters::CAMERA_ORIENTATION_PORTRAIT) {
840 LOGV("portrait mode");
841 transform = ISurface::BufferHeap::ROT_90;
842 }
843 ISurface::BufferHeap buffers(w, h, w, h,
844 PIXEL_FORMAT_YCbCr_420_SP, transform, 0, client->mHardware->getRawHeap());
845
846 client->mSurface->registerBuffers(buffers);
847 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800848}
849
850// picture callback - raw image ready
851void CameraService::Client::yuvPictureCallback(const sp<IMemory>& mem,
852 void *user)
853{
854 sp<Client> client = getClientFromCookie(user);
855 if (client == 0) {
856 return;
857 }
858 if (mem == NULL) {
859 client->postRaw(NULL);
860 client->postError(UNKNOWN_ERROR);
861 return;
862 }
863
864 ssize_t offset;
865 size_t size;
866 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
867#if DEBUG_HEAP_LEAKS && 0 // debugging
868 gWeakHeap = heap; // debugging
869#endif
870
871 //LOGV("yuvPictureCallback(%d, %d, %p)", offset, size, user);
872#if DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE // for testing pursposes only
873 dump_to_file("/data/photo.yuv",
874 (uint8_t *)heap->base() + offset, size);
875#endif
876
877 // Put the YUV version of the snapshot in the preview display.
878 if (client->mSurface != 0 && !client->mUseOverlay) {
879 client->mSurface->postBuffer(offset);
880 }
881
882 client->postRaw(mem);
883
884#if DEBUG_CLIENT_REFERENCES
885 //**** if the client's refcount is 1, then we are about to destroy it here,
886 // which is bad--print all refcounts.
887 if (client->getStrongCount() == 1) {
888 LOGE("++++++++++++++++ (RAW) THIS WILL CAUSE A LOCKUP!");
889 client->printRefs();
890 }
891#endif
892}
893
894// picture callback - jpeg ready
895void CameraService::Client::jpegPictureCallback(const sp<IMemory>& mem, void *user)
896{
897 sp<Client> client = getClientFromCookie(user);
898 if (client == 0) {
899 return;
900 }
901 if (mem == NULL) {
902 client->postJpeg(NULL);
903 client->postError(UNKNOWN_ERROR);
904 return;
905 }
906
907 /** We absolutely CANNOT call into user code with a lock held **/
908
909#if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE // for testing pursposes only
910 {
911 ssize_t offset;
912 size_t size;
913 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
914 dump_to_file("/data/photo.jpg",
915 (uint8_t *)heap->base() + offset, size);
916 }
917#endif
918
919 client->postJpeg(mem);
920
921#if DEBUG_CLIENT_REFERENCES
922 //**** if the client's refcount is 1, then we are about to destroy it here,
923 // which is bad--print all refcounts.
924 if (client->getStrongCount() == 1) {
925 LOGE("++++++++++++++++ (JPEG) THIS WILL CAUSE A LOCKUP!");
926 client->printRefs();
927 }
928#endif
929}
930
931void CameraService::Client::autoFocusCallback(bool focused, void *user)
932{
933 LOGV("autoFocusCallback");
934
935 sp<Client> client = getClientFromCookie(user);
936 if (client == 0) {
937 return;
938 }
939
940 client->postAutoFocus(focused);
941
942#if DEBUG_CLIENT_REFERENCES
943 if (client->getStrongCount() == 1) {
944 LOGE("++++++++++++++++ (AUTOFOCUS) THIS WILL CAUSE A LOCKUP!");
945 client->printRefs();
946 }
947#endif
948}
949
950// set preview/capture parameters - key/value pairs
951status_t CameraService::Client::setParameters(const String8& params)
952{
953 LOGD("setParameters(%s)", params.string());
954
955 Mutex::Autolock lock(mLock);
956 status_t result = checkPid();
957 if (result != NO_ERROR) return result;
958
959 if (mHardware == 0) {
960 LOGE("mHardware is NULL, returning.");
961 return INVALID_OPERATION;
962 }
963
964 CameraParameters p(params);
965 mHardware->setParameters(p);
966 return NO_ERROR;
967}
968
969// get preview/capture parameters - key/value pairs
970String8 CameraService::Client::getParameters() const
971{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800972 Mutex::Autolock lock(mLock);
973
974 if (mHardware == 0) {
975 LOGE("mHardware is NULL, returning.");
976 return String8();
977 }
978
Wu-cheng Li81d763f2009-04-22 16:21:26 +0800979 String8 params(mHardware->getParameters().flatten());
980 LOGD("getParameters(%s)", params.string());
981 return params;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800982}
983
984void CameraService::Client::postAutoFocus(bool focused)
985{
986 LOGV("postAutoFocus");
Dave Sparks93b94582009-05-07 19:27:32 -0700987 mCameraClient->notifyCallback(CAMERA_MSG_FOCUS, (int32_t)focused, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800988}
989
990void CameraService::Client::postShutter()
991{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800992 LOGD("postShutter");
Dave Sparks93b94582009-05-07 19:27:32 -0700993 mCameraClient->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800994}
995
996void CameraService::Client::postRaw(const sp<IMemory>& mem)
997{
998 LOGD("postRaw");
Dave Sparks93b94582009-05-07 19:27:32 -0700999 mCameraClient->dataCallback(CAMERA_MSG_RAW_IMAGE, mem);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001000}
1001
1002void CameraService::Client::postJpeg(const sp<IMemory>& mem)
1003{
1004 LOGD("postJpeg");
Dave Sparks93b94582009-05-07 19:27:32 -07001005 mCameraClient->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001006}
1007
1008void CameraService::Client::copyFrameAndPostCopiedFrame(sp<IMemoryHeap> heap, size_t offset, size_t size)
1009{
1010 LOGV("copyFrameAndPostCopiedFrame");
1011 // It is necessary to copy out of pmem before sending this to
1012 // the callback. For efficiency, reuse the same MemoryHeapBase
1013 // provided it's big enough. Don't allocate the memory or
1014 // perform the copy if there's no callback.
1015 if (mPreviewBuffer == 0) {
1016 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1017 } else if (size > mPreviewBuffer->virtualSize()) {
1018 mPreviewBuffer.clear();
1019 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1020 if (mPreviewBuffer == 0) {
1021 LOGE("failed to allocate space for preview buffer");
1022 return;
1023 }
1024 }
1025 memcpy(mPreviewBuffer->base(),
1026 (uint8_t *)heap->base() + offset, size);
1027
1028 sp<MemoryBase> frame = new MemoryBase(mPreviewBuffer, 0, size);
1029 if (frame == 0) {
1030 LOGE("failed to allocate space for frame callback");
1031 return;
1032 }
Dave Sparks93b94582009-05-07 19:27:32 -07001033 mCameraClient->dataCallback(CAMERA_MSG_PREVIEW_FRAME, frame);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001034}
1035
1036void CameraService::Client::postRecordingFrame(const sp<IMemory>& frame)
1037{
1038 LOGV("postRecordingFrame");
1039 if (frame == 0) {
1040 LOGW("frame is a null pointer");
1041 return;
1042 }
Dave Sparks93b94582009-05-07 19:27:32 -07001043 mCameraClient->dataCallback(CAMERA_MSG_VIDEO_FRAME, frame);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001044}
1045
1046void CameraService::Client::postPreviewFrame(const sp<IMemory>& mem)
1047{
1048 LOGV("postPreviewFrame");
1049 if (mem == 0) {
1050 LOGW("mem is a null pointer");
1051 return;
1052 }
1053
1054 ssize_t offset;
1055 size_t size;
1056 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1057 {
1058 Mutex::Autolock surfaceLock(mSurfaceLock);
1059 if (mSurface != NULL) {
1060 mSurface->postBuffer(offset);
1061 }
1062 }
1063
1064 // Is the callback enabled or not?
1065 if (!(mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
1066 // If the enable bit is off, the copy-out and one-shot bits are ignored
1067 LOGV("frame callback is diabled");
1068 return;
1069 }
1070
1071 // Is the received frame copied out or not?
1072 if (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
1073 LOGV("frame is copied out");
1074 copyFrameAndPostCopiedFrame(heap, offset, size);
1075 } else {
1076 LOGV("frame is directly sent out without copying");
Dave Sparks93b94582009-05-07 19:27:32 -07001077 mCameraClient->dataCallback(CAMERA_MSG_PREVIEW_FRAME, mem);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001078 }
1079
1080 // Is this is one-shot only?
1081 if (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ONE_SHOT_MASK) {
1082 LOGV("One-shot only, thus clear the bits and disable frame callback");
1083 mPreviewCallbackFlag &= ~(FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
1084 FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
1085 FRAME_CALLBACK_FLAG_ENABLE_MASK);
1086 }
1087}
1088
1089void CameraService::Client::postError(status_t error)
1090{
Dave Sparks93b94582009-05-07 19:27:32 -07001091 mCameraClient->notifyCallback(CAMERA_MSG_ERROR, error, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092}
1093
1094status_t CameraService::dump(int fd, const Vector<String16>& args)
1095{
1096 const size_t SIZE = 256;
1097 char buffer[SIZE];
1098 String8 result;
1099 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
1100 snprintf(buffer, SIZE, "Permission Denial: "
1101 "can't dump CameraService from pid=%d, uid=%d\n",
Chih-Chung Changd98c5162009-06-22 16:03:41 +08001102 getCallingPid(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001103 IPCThreadState::self()->getCallingUid());
1104 result.append(buffer);
1105 write(fd, result.string(), result.size());
1106 } else {
1107 AutoMutex lock(&mLock);
1108 if (mClient != 0) {
1109 sp<Client> currentClient = mClient.promote();
1110 sprintf(buffer, "Client (%p) PID: %d\n",
1111 currentClient->getCameraClient()->asBinder().get(),
1112 currentClient->mClientPid);
1113 result.append(buffer);
1114 write(fd, result.string(), result.size());
1115 currentClient->mHardware->dump(fd, args);
1116 } else {
1117 result.append("No camera client yet.\n");
1118 write(fd, result.string(), result.size());
1119 }
1120 }
1121 return NO_ERROR;
1122}
1123
1124
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001125#define CHECK_INTERFACE(interface, data, reply) \
1126 do { if (!data.enforceInterface(interface::getInterfaceDescriptor())) { \
1127 LOGW("Call incorrectly routed to " #interface); \
1128 return PERMISSION_DENIED; \
1129 } } while (0)
1130
1131status_t CameraService::onTransact(
1132 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1133{
1134 // permission checks...
1135 switch (code) {
1136 case BnCameraService::CONNECT:
1137 IPCThreadState* ipc = IPCThreadState::self();
1138 const int pid = ipc->getCallingPid();
1139 const int self_pid = getpid();
1140 if (pid != self_pid) {
1141 // we're called from a different process, do the real check
1142 if (!checkCallingPermission(
1143 String16("android.permission.CAMERA")))
1144 {
1145 const int uid = ipc->getCallingUid();
1146 LOGE("Permission Denial: "
1147 "can't use the camera pid=%d, uid=%d", pid, uid);
1148 return PERMISSION_DENIED;
1149 }
1150 }
1151 break;
1152 }
1153
1154 status_t err = BnCameraService::onTransact(code, data, reply, flags);
1155
Dave Sparks998b3292009-05-20 20:02:59 -07001156#if DEBUG_HEAP_LEAKS
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001157 LOGD("+++ onTransact err %d code %d", err, code);
1158
1159 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
1160 // the 'service' command interrogates this binder for its name, and then supplies it
1161 // even for the debugging commands. that means we need to check for it here, using
1162 // ISurfaceComposer (since we delegated the INTERFACE_TRANSACTION handling to
1163 // BnSurfaceComposer before falling through to this code).
1164
1165 LOGD("+++ onTransact code %d", code);
1166
1167 CHECK_INTERFACE(ICameraService, data, reply);
1168
1169 switch(code) {
1170 case 1000:
1171 {
1172 if (gWeakHeap != 0) {
1173 sp<IMemoryHeap> h = gWeakHeap.promote();
1174 IMemoryHeap *p = gWeakHeap.unsafe_get();
1175 LOGD("CHECKING WEAK REFERENCE %p (%p)", h.get(), p);
1176 if (h != 0)
1177 h->printRefs();
1178 bool attempt_to_delete = data.readInt32() == 1;
1179 if (attempt_to_delete) {
1180 // NOT SAFE!
1181 LOGD("DELETING WEAK REFERENCE %p (%p)", h.get(), p);
1182 if (p) delete p;
1183 }
1184 return NO_ERROR;
1185 }
1186 }
1187 break;
1188 default:
1189 break;
1190 }
1191 }
Dave Sparks998b3292009-05-20 20:02:59 -07001192#endif // DEBUG_HEAP_LEAKS
Dave Sparksfec880d2009-05-21 09:18:18 -07001193
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001194 return err;
1195}
1196
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001197}; // namespace android