blob: 86a6fac270094dfe8291cf871f92932865e7991d [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#define LOG_TAG "CameraService"
20#include <utils/Log.h>
21
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070022#include <binder/IServiceManager.h>
23#include <binder/IPCThreadState.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024#include <utils/String16.h>
25#include <utils/Errors.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070026#include <binder/MemoryBase.h>
27#include <binder/MemoryHeapBase.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080028#include <camera/ICameraService.h>
29#include <surfaceflinger/ISurface.h>
30#include <ui/Overlay.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031
Jason Sams78b877e2009-03-24 20:21:36 -070032#include <media/mediaplayer.h>
33#include <media/AudioSystem.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034#include "CameraService.h"
35
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +080036#include <cutils/atomic.h>
Eric Laurentcbcb00e2009-03-27 16:27:16 -070037
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
Benny Wong4c8fb0a2009-08-12 12:01:27 -050063#define DEBUG_DUMP_POSTVIEW_SNAPSHOT_TO_FILE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064
65#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
66static int debug_frame_cnt;
67#endif
68
Chih-Chung Changd98c5162009-06-22 16:03:41 +080069static int getCallingPid() {
70 return IPCThreadState::self()->getCallingPid();
71}
72
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073// ----------------------------------------------------------------------------
74
75void CameraService::instantiate() {
76 defaultServiceManager()->addService(
77 String16("media.camera"), new CameraService());
78}
79
80// ----------------------------------------------------------------------------
81
82CameraService::CameraService() :
83 BnCameraService()
84{
85 LOGI("CameraService started: pid=%d", getpid());
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +080086 mUsers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087}
88
89CameraService::~CameraService()
90{
91 if (mClient != 0) {
92 LOGE("mClient was still connected in destructor!");
93 }
94}
95
96sp<ICamera> CameraService::connect(const sp<ICameraClient>& cameraClient)
97{
Chih-Chung Changd98c5162009-06-22 16:03:41 +080098 int callingPid = getCallingPid();
Joe Onorato51632e82010-01-07 21:48:32 -050099 LOGV("CameraService::connect E (pid %d, client %p)", callingPid,
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800100 cameraClient->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800102 Mutex::Autolock lock(mServiceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103 sp<Client> client;
104 if (mClient != 0) {
105 sp<Client> currentClient = mClient.promote();
106 if (currentClient != 0) {
107 sp<ICameraClient> currentCameraClient(currentClient->getCameraClient());
108 if (cameraClient->asBinder() == currentCameraClient->asBinder()) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800109 // This is the same client reconnecting...
Joe Onorato51632e82010-01-07 21:48:32 -0500110 LOGV("CameraService::connect X (pid %d, same client %p) is reconnecting...",
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800111 callingPid, cameraClient->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112 return currentClient;
113 } else {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800114 // It's another client... reject it
Joe Onorato51632e82010-01-07 21:48:32 -0500115 LOGV("CameraService::connect X (pid %d, new client %p) rejected. "
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800116 "(old pid %d, old client %p)",
117 callingPid, cameraClient->asBinder().get(),
118 currentClient->mClientPid, currentCameraClient->asBinder().get());
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800119 if (kill(currentClient->mClientPid, 0) == -1 && errno == ESRCH) {
Joe Onorato51632e82010-01-07 21:48:32 -0500120 LOGV("The old client is dead!");
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800121 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122 return client;
123 }
124 } else {
125 // can't promote, the previous client has died...
Joe Onorato51632e82010-01-07 21:48:32 -0500126 LOGV("New client (pid %d) connecting, old reference was dangling...",
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800127 callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128 mClient.clear();
129 }
130 }
131
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800132 if (mUsers > 0) {
Joe Onorato51632e82010-01-07 21:48:32 -0500133 LOGV("Still have client, rejected");
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800134 return client;
135 }
136
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137 // create a new Client object
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800138 client = new Client(this, cameraClient, callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 mClient = client;
140#if DEBUG_CLIENT_REFERENCES
141 // Enable tracking for this object, and track increments and decrements of
142 // the refcount.
143 client->trackMe(true, true);
144#endif
Joe Onorato51632e82010-01-07 21:48:32 -0500145 LOGV("CameraService::connect X");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146 return client;
147}
148
149void CameraService::removeClient(const sp<ICameraClient>& cameraClient)
150{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800151 int callingPid = getCallingPid();
152
153 // Declare this outside the lock to make absolutely sure the
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 // destructor won't be called with the lock held.
155 sp<Client> client;
156
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800157 Mutex::Autolock lock(mServiceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158
159 if (mClient == 0) {
160 // This happens when we have already disconnected.
Joe Onorato51632e82010-01-07 21:48:32 -0500161 LOGV("removeClient (pid %d): already disconnected", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 return;
163 }
164
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800165 // Promote mClient. It can fail if we are called from this path:
166 // Client::~Client() -> disconnect() -> removeClient().
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167 client = mClient.promote();
168 if (client == 0) {
Joe Onorato51632e82010-01-07 21:48:32 -0500169 LOGV("removeClient (pid %d): no more strong reference", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170 mClient.clear();
171 return;
172 }
173
174 if (cameraClient->asBinder() != client->getCameraClient()->asBinder()) {
175 // ugh! that's not our client!!
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800176 LOGW("removeClient (pid %d): mClient doesn't match!", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 } else {
178 // okay, good, forget about mClient
179 mClient.clear();
180 }
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800181
Joe Onorato51632e82010-01-07 21:48:32 -0500182 LOGV("removeClient (pid %d) done", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183}
184
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800185// The reason we need this count is a new CameraService::connect() request may
186// come in while the previous Client's destructor has not been run or is still
187// running. If the last strong reference of the previous Client is gone but
188// destructor has not been run, we should not allow the new Client to be created
189// because we need to wait for the previous Client to tear down the hardware
190// first.
191void CameraService::incUsers() {
192 android_atomic_inc(&mUsers);
193}
194
195void CameraService::decUsers() {
196 android_atomic_dec(&mUsers);
197}
198
Wu-cheng Lie6a550d2009-09-28 16:14:58 -0700199static sp<MediaPlayer> newMediaPlayer(const char *file)
Jason Sams78b877e2009-03-24 20:21:36 -0700200{
201 sp<MediaPlayer> mp = new MediaPlayer();
Andreas Huberd4461e72010-01-28 11:19:57 -0800202 if (mp->setDataSource(file, NULL /* headers */) == NO_ERROR) {
Eric Laurent9d91ad52009-07-17 12:17:14 -0700203 mp->setAudioStreamType(AudioSystem::ENFORCED_AUDIBLE);
Jason Sams78b877e2009-03-24 20:21:36 -0700204 mp->prepare();
205 } else {
206 mp.clear();
207 LOGE("Failed to load CameraService sounds.");
208 }
209 return mp;
210}
211
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212CameraService::Client::Client(const sp<CameraService>& cameraService,
213 const sp<ICameraClient>& cameraClient, pid_t clientPid)
214{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800215 int callingPid = getCallingPid();
Joe Onorato51632e82010-01-07 21:48:32 -0500216 LOGV("Client::Client E (pid %d)", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217 mCameraService = cameraService;
218 mCameraClient = cameraClient;
219 mClientPid = clientPid;
220 mHardware = openCameraHardware();
221 mUseOverlay = mHardware->useOverlay();
222
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500223 mHardware->setCallbacks(notifyCallback,
224 dataCallback,
225 dataCallbackTimestamp,
226 mCameraService.get());
227
228 // Enable zoom, error, and focus messages by default
229 mHardware->enableMsgType(CAMERA_MSG_ERROR |
230 CAMERA_MSG_ZOOM |
231 CAMERA_MSG_FOCUS);
232
Jason Sams78b877e2009-03-24 20:21:36 -0700233 mMediaPlayerClick = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
234 mMediaPlayerBeep = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
Benny Wong71f77152009-07-15 18:44:27 -0500235 mOverlayW = 0;
236 mOverlayH = 0;
Jason Sams78b877e2009-03-24 20:21:36 -0700237
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238 // Callback is disabled by default
239 mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
Chih-Chung Chang52e72002010-01-21 17:31:06 -0800240 mOrientation = 0;
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800241 cameraService->incUsers();
Joe Onorato51632e82010-01-07 21:48:32 -0500242 LOGV("Client::Client X (pid %d)", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243}
244
245status_t CameraService::Client::checkPid()
246{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800247 int callingPid = getCallingPid();
248 if (mClientPid == callingPid) return NO_ERROR;
249 LOGW("Attempt to use locked camera (client %p) from different process "
250 " (old pid %d, new pid %d)",
251 getCameraClient()->asBinder().get(), mClientPid, callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252 return -EBUSY;
253}
254
255status_t CameraService::Client::lock()
256{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800257 int callingPid = getCallingPid();
Joe Onorato51632e82010-01-07 21:48:32 -0500258 LOGV("lock from pid %d (mClientPid %d)", callingPid, mClientPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259 Mutex::Autolock _l(mLock);
260 // lock camera to this client if the the camera is unlocked
261 if (mClientPid == 0) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800262 mClientPid = callingPid;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263 return NO_ERROR;
264 }
265 // returns NO_ERROR if the client already owns the camera, -EBUSY otherwise
266 return checkPid();
267}
268
269status_t CameraService::Client::unlock()
270{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800271 int callingPid = getCallingPid();
Joe Onorato51632e82010-01-07 21:48:32 -0500272 LOGV("unlock from pid %d (mClientPid %d)", callingPid, mClientPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273 Mutex::Autolock _l(mLock);
274 // allow anyone to use camera
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275 status_t result = checkPid();
James Dong0ae13e32009-04-20 19:35:28 -0700276 if (result == NO_ERROR) {
277 mClientPid = 0;
Joe Onorato51632e82010-01-07 21:48:32 -0500278 LOGV("clear mCameraClient (pid %d)", callingPid);
James Dong0ae13e32009-04-20 19:35:28 -0700279 // we need to remove the reference so that when app goes
280 // away, the reference count goes to 0.
281 mCameraClient.clear();
282 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283 return result;
284}
285
286status_t CameraService::Client::connect(const sp<ICameraClient>& client)
287{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800288 int callingPid = getCallingPid();
289
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800290 // connect a new process to the camera
Joe Onorato51632e82010-01-07 21:48:32 -0500291 LOGV("Client::connect E (pid %d, client %p)", callingPid, client->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292
293 // I hate this hack, but things get really ugly when the media recorder
294 // service is handing back the camera to the app. The ICameraClient
295 // destructor will be called during the same IPC, making it look like
296 // the remote client is trying to disconnect. This hack temporarily
297 // sets the mClientPid to an invalid pid to prevent the hardware from
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800298 // being torn down.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299 {
300
301 // hold a reference to the old client or we will deadlock if the client is
302 // in the same process and we hold the lock when we remove the reference
303 sp<ICameraClient> oldClient;
304 {
305 Mutex::Autolock _l(mLock);
Chih-Chung Chang34e5a152009-06-09 13:56:44 +0800306 if (mClientPid != 0 && checkPid() != NO_ERROR) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800307 LOGW("Tried to connect to locked camera (old pid %d, new pid %d)",
308 mClientPid, callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800309 return -EBUSY;
310 }
311 oldClient = mCameraClient;
312
313 // did the client actually change?
Dave Sparks393eb792009-10-15 10:02:22 -0700314 if ((mCameraClient != NULL) && (client->asBinder() == mCameraClient->asBinder())) {
Joe Onorato51632e82010-01-07 21:48:32 -0500315 LOGV("Connect to the same client");
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800316 return NO_ERROR;
317 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318
319 mCameraClient = client;
320 mClientPid = -1;
321 mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
Joe Onorato51632e82010-01-07 21:48:32 -0500322 LOGV("Connect to the new client (pid %d, client %p)",
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800323 callingPid, mCameraClient->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800324 }
325
326 }
327 // the old client destructor is called when oldClient goes out of scope
328 // now we set the new PID to lock the interface again
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800329 mClientPid = callingPid;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330
331 return NO_ERROR;
332}
333
334#if HAVE_ANDROID_OS
335static void *unregister_surface(void *arg)
336{
337 ISurface *surface = (ISurface *)arg;
338 surface->unregisterBuffers();
339 IPCThreadState::self()->flushCommands();
340 return NULL;
341}
342#endif
343
344CameraService::Client::~Client()
345{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800346 int callingPid = getCallingPid();
347
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800348 // tear down client
Joe Onorato51632e82010-01-07 21:48:32 -0500349 LOGV("Client::~Client E (pid %d, client %p)",
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800350 callingPid, getCameraClient()->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800351 if (mSurface != 0 && !mUseOverlay) {
352#if HAVE_ANDROID_OS
353 pthread_t thr;
354 // We unregister the buffers in a different thread because binder does
355 // not let us make sychronous transactions in a binder destructor (that
356 // is, upon our reaching a refcount of zero.)
357 pthread_create(&thr, NULL,
358 unregister_surface,
359 mSurface.get());
360 pthread_join(thr, NULL);
361#else
362 mSurface->unregisterBuffers();
363#endif
364 }
365
Jason Sams9e431ac2009-03-24 20:36:57 -0700366 if (mMediaPlayerBeep.get() != NULL) {
367 mMediaPlayerBeep->disconnect();
368 mMediaPlayerBeep.clear();
369 }
370 if (mMediaPlayerClick.get() != NULL) {
371 mMediaPlayerClick->disconnect();
372 mMediaPlayerClick.clear();
373 }
Jason Sams78b877e2009-03-24 20:21:36 -0700374
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800375 // make sure we tear down the hardware
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800376 mClientPid = callingPid;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800377 disconnect();
Joe Onorato51632e82010-01-07 21:48:32 -0500378 LOGV("Client::~Client X (pid %d)", mClientPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800379}
380
381void CameraService::Client::disconnect()
382{
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800383 int callingPid = getCallingPid();
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800384
Joe Onorato51632e82010-01-07 21:48:32 -0500385 LOGV("Client::disconnect() E (pid %d client %p)",
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800386 callingPid, getCameraClient()->asBinder().get());
387
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800388 Mutex::Autolock lock(mLock);
389 if (mClientPid <= 0) {
Joe Onorato51632e82010-01-07 21:48:32 -0500390 LOGV("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800391 return;
392 }
393 if (checkPid() != NO_ERROR) {
Joe Onorato51632e82010-01-07 21:48:32 -0500394 LOGV("Different client - don't disconnect");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800395 return;
396 }
397
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800398 // Make sure disconnect() is done once and once only, whether it is called
399 // from the user directly, or called by the destructor.
400 if (mHardware == 0) return;
401
Joe Onorato51632e82010-01-07 21:48:32 -0500402 LOGV("hardware teardown");
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800403 // Before destroying mHardware, we must make sure it's in the
404 // idle state.
405 mHardware->stopPreview();
406 // Cancel all picture callbacks.
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500407 mHardware->disableMsgType(CAMERA_MSG_SHUTTER |
408 CAMERA_MSG_POSTVIEW_FRAME |
409 CAMERA_MSG_RAW_IMAGE |
410 CAMERA_MSG_COMPRESSED_IMAGE);
411 mHardware->cancelPicture();
412 // Turn off remaining messages.
413 mHardware->disableMsgType(CAMERA_MSG_ALL_MSGS);
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800414 // Release the hardware resources.
415 mHardware->release();
Benny Wong71f77152009-07-15 18:44:27 -0500416 // Release the held overlay resources.
417 if (mUseOverlay)
418 {
419 mOverlayRef = 0;
420 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800421 mHardware.clear();
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800422
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800423 mCameraService->removeClient(mCameraClient);
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800424 mCameraService->decUsers();
425
Joe Onorato51632e82010-01-07 21:48:32 -0500426 LOGV("Client::disconnect() X (pid %d)", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800427}
428
429// pass the buffered ISurface to the camera service
430status_t CameraService::Client::setPreviewDisplay(const sp<ISurface>& surface)
431{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800432 LOGV("setPreviewDisplay(%p) (pid %d)",
Wu-cheng Li988fb622009-06-23 23:37:36 +0800433 ((surface == NULL) ? NULL : surface.get()), getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800434 Mutex::Autolock lock(mLock);
435 status_t result = checkPid();
436 if (result != NO_ERROR) return result;
Wu-cheng Li988fb622009-06-23 23:37:36 +0800437
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800438 Mutex::Autolock surfaceLock(mSurfaceLock);
Wu-cheng Li988fb622009-06-23 23:37:36 +0800439 result = NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800440 // asBinder() is safe on NULL (returns NULL)
441 if (surface->asBinder() != mSurface->asBinder()) {
Benny Wong71f77152009-07-15 18:44:27 -0500442 if (mSurface != 0) {
Dave Sparkse7e93f92010-01-04 08:55:04 -0800443 LOGV("clearing old preview surface %p", mSurface.get());
Benny Wong71f77152009-07-15 18:44:27 -0500444 if ( !mUseOverlay)
445 {
446 mSurface->unregisterBuffers();
447 }
448 else
449 {
450 // Force the destruction of any previous overlay
451 sp<Overlay> dummy;
452 mHardware->setOverlay( dummy );
453 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800454 }
455 mSurface = surface;
Benny Wong71f77152009-07-15 18:44:27 -0500456 mOverlayRef = 0;
Wu-cheng Li988fb622009-06-23 23:37:36 +0800457 // If preview has been already started, set overlay or register preview
458 // buffers now.
459 if (mHardware->previewEnabled()) {
460 if (mUseOverlay) {
461 result = setOverlay();
462 } else if (mSurface != 0) {
463 result = registerPreviewBuffers();
464 }
465 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800466 }
Wu-cheng Li988fb622009-06-23 23:37:36 +0800467 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800468}
469
470// set the preview callback flag to affect how the received frames from
471// preview are handled.
472void CameraService::Client::setPreviewCallbackFlag(int callback_flag)
473{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800474 LOGV("setPreviewCallbackFlag (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800475 Mutex::Autolock lock(mLock);
476 if (checkPid() != NO_ERROR) return;
477 mPreviewCallbackFlag = callback_flag;
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500478
479 if(mUseOverlay) {
480 if(mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ENABLE_MASK)
481 mHardware->enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
482 else
483 mHardware->disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
484 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800485}
486
Wu-cheng Li988fb622009-06-23 23:37:36 +0800487// start preview mode
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800488status_t CameraService::Client::startCameraMode(camera_mode mode)
489{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800490 int callingPid = getCallingPid();
491
Dave Sparkse7e93f92010-01-04 08:55:04 -0800492 LOGV("startCameraMode(%d) (pid %d)", mode, callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800493
494 /* we cannot call into mHardware with mLock held because
495 * mHardware has callbacks onto us which acquire this lock
496 */
497
498 Mutex::Autolock lock(mLock);
499 status_t result = checkPid();
500 if (result != NO_ERROR) return result;
501
502 if (mHardware == 0) {
503 LOGE("mHardware is NULL, returning.");
504 return INVALID_OPERATION;
505 }
506
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800507 switch(mode) {
508 case CAMERA_RECORDING_MODE:
Wu-cheng Li988fb622009-06-23 23:37:36 +0800509 if (mSurface == 0) {
510 LOGE("setPreviewDisplay must be called before startRecordingMode.");
511 return INVALID_OPERATION;
512 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800513 return startRecordingMode();
514
515 default: // CAMERA_PREVIEW_MODE
Wu-cheng Li988fb622009-06-23 23:37:36 +0800516 if (mSurface == 0) {
Dave Sparkse7e93f92010-01-04 08:55:04 -0800517 LOGV("mSurface is not set yet.");
Wu-cheng Li988fb622009-06-23 23:37:36 +0800518 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800519 return startPreviewMode();
520 }
521}
522
523status_t CameraService::Client::startRecordingMode()
524{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800525 LOGV("startRecordingMode (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800526
527 status_t ret = UNKNOWN_ERROR;
528
529 // if preview has not been started, start preview first
530 if (!mHardware->previewEnabled()) {
531 ret = startPreviewMode();
532 if (ret != NO_ERROR) {
533 return ret;
534 }
535 }
536
537 // if recording has been enabled, nothing needs to be done
538 if (mHardware->recordingEnabled()) {
539 return NO_ERROR;
540 }
541
542 // start recording mode
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500543 ret = mHardware->startRecording();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800544 if (ret != NO_ERROR) {
545 LOGE("mHardware->startRecording() failed with status %d", ret);
546 }
547 return ret;
548}
549
Wu-cheng Li988fb622009-06-23 23:37:36 +0800550status_t CameraService::Client::setOverlay()
551{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800552 LOGV("setOverlay");
Wu-cheng Li988fb622009-06-23 23:37:36 +0800553 int w, h;
554 CameraParameters params(mHardware->getParameters());
555 params.getPreviewSize(&w, &h);
556
Benny Wong71f77152009-07-15 18:44:27 -0500557 if ( w != mOverlayW || h != mOverlayH )
558 {
559 // Force the destruction of any previous overlay
560 sp<Overlay> dummy;
561 mHardware->setOverlay( dummy );
562 mOverlayRef = 0;
563 }
564
Wu-cheng Li988fb622009-06-23 23:37:36 +0800565 status_t ret = NO_ERROR;
566 if (mSurface != 0) {
Benny Wong71f77152009-07-15 18:44:27 -0500567 if (mOverlayRef.get() == NULL) {
Dave Sparks587f7832009-10-07 19:18:20 -0700568
569 // FIXME:
570 // Surfaceflinger may hold onto the previous overlay reference for some
571 // time after we try to destroy it. retry a few times. In the future, we
572 // should make the destroy call block, or possibly specify that we can
573 // wait in the createOverlay call if the previous overlay is in the
574 // process of being destroyed.
575 for (int retry = 0; retry < 50; ++retry) {
Chih-Chung Chang52e72002010-01-21 17:31:06 -0800576 mOverlayRef = mSurface->createOverlay(w, h, OVERLAY_FORMAT_DEFAULT,
577 mOrientation);
Dave Sparks587f7832009-10-07 19:18:20 -0700578 if (mOverlayRef != NULL) break;
Dave Sparkse7e93f92010-01-04 08:55:04 -0800579 LOGW("Overlay create failed - retrying");
Dave Sparks587f7832009-10-07 19:18:20 -0700580 usleep(20000);
581 }
Benny Wong71f77152009-07-15 18:44:27 -0500582 if ( mOverlayRef.get() == NULL )
583 {
584 LOGE("Overlay Creation Failed!");
585 return -EINVAL;
586 }
587 ret = mHardware->setOverlay(new Overlay(mOverlayRef));
588 }
Wu-cheng Li988fb622009-06-23 23:37:36 +0800589 } else {
590 ret = mHardware->setOverlay(NULL);
591 }
592 if (ret != NO_ERROR) {
593 LOGE("mHardware->setOverlay() failed with status %d\n", ret);
594 }
Benny Wong71f77152009-07-15 18:44:27 -0500595
596 mOverlayW = w;
597 mOverlayH = h;
598
Wu-cheng Li988fb622009-06-23 23:37:36 +0800599 return ret;
600}
601
602status_t CameraService::Client::registerPreviewBuffers()
603{
604 int w, h;
605 CameraParameters params(mHardware->getParameters());
606 params.getPreviewSize(&w, &h);
607
Wu-cheng Li988fb622009-06-23 23:37:36 +0800608 ISurface::BufferHeap buffers(w, h, w, h,
609 PIXEL_FORMAT_YCbCr_420_SP,
Chih-Chung Chang52e72002010-01-21 17:31:06 -0800610 mOrientation,
Wu-cheng Li988fb622009-06-23 23:37:36 +0800611 0,
612 mHardware->getPreviewHeap());
613
614 status_t ret = mSurface->registerBuffers(buffers);
615 if (ret != NO_ERROR) {
616 LOGE("registerBuffers failed with status %d", ret);
617 }
618 return ret;
619}
620
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800621status_t CameraService::Client::startPreviewMode()
622{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800623 LOGV("startPreviewMode (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800624
625 // if preview has been enabled, nothing needs to be done
626 if (mHardware->previewEnabled()) {
627 return NO_ERROR;
628 }
629
630 // start preview mode
631#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
632 debug_frame_cnt = 0;
633#endif
Wu-cheng Li988fb622009-06-23 23:37:36 +0800634 status_t ret = NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800635
636 if (mUseOverlay) {
Wu-cheng Li988fb622009-06-23 23:37:36 +0800637 // If preview display has been set, set overlay now.
638 if (mSurface != 0) {
639 ret = setOverlay();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800640 }
Wu-cheng Li988fb622009-06-23 23:37:36 +0800641 if (ret != NO_ERROR) return ret;
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500642 ret = mHardware->startPreview();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800643 } else {
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500644 mHardware->enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
645 ret = mHardware->startPreview();
Wu-cheng Li988fb622009-06-23 23:37:36 +0800646 if (ret != NO_ERROR) return ret;
647 // If preview display has been set, register preview buffers now.
648 if (mSurface != 0) {
649 // Unregister here because the surface registered with raw heap.
650 mSurface->unregisterBuffers();
651 ret = registerPreviewBuffers();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800652 }
653 }
654 return ret;
655}
656
657status_t CameraService::Client::startPreview()
658{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800659 LOGV("startPreview (pid %d)", getCallingPid());
Wu-cheng Lie6a550d2009-09-28 16:14:58 -0700660
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800661 return startCameraMode(CAMERA_PREVIEW_MODE);
662}
663
664status_t CameraService::Client::startRecording()
665{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800666 LOGV("startRecording (pid %d)", getCallingPid());
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800667
Jason Sams78b877e2009-03-24 20:21:36 -0700668 if (mMediaPlayerBeep.get() != NULL) {
Eric Laurent059b4132009-11-27 05:07:55 -0800669 // do not play record jingle if stream volume is 0
670 // (typically because ringer mode is silent).
671 int index;
672 AudioSystem::getStreamVolumeIndex(AudioSystem::ENFORCED_AUDIBLE, &index);
673 if (index != 0) {
674 mMediaPlayerBeep->seekTo(0);
675 mMediaPlayerBeep->start();
676 }
Jason Sams78b877e2009-03-24 20:21:36 -0700677 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500678
679 mHardware->enableMsgType(CAMERA_MSG_VIDEO_FRAME);
680
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800681 return startCameraMode(CAMERA_RECORDING_MODE);
682}
683
684// stop preview mode
685void CameraService::Client::stopPreview()
686{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800687 LOGV("stopPreview (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800688
Dave Sparksff0f38e2009-11-10 17:08:08 -0800689 // hold main lock during state transition
690 {
691 Mutex::Autolock lock(mLock);
692 if (checkPid() != NO_ERROR) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800693
Dave Sparksff0f38e2009-11-10 17:08:08 -0800694 if (mHardware == 0) {
695 LOGE("mHardware is NULL, returning.");
696 return;
697 }
698
699 mHardware->stopPreview();
700 mHardware->disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
Dave Sparkse7e93f92010-01-04 08:55:04 -0800701 LOGV("stopPreview(), hardware stopped OK");
Dave Sparksff0f38e2009-11-10 17:08:08 -0800702
703 if (mSurface != 0 && !mUseOverlay) {
704 mSurface->unregisterBuffers();
705 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800706 }
707
Dave Sparksff0f38e2009-11-10 17:08:08 -0800708 // hold preview buffer lock
709 {
710 Mutex::Autolock lock(mPreviewLock);
711 mPreviewBuffer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800712 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800713}
714
715// stop recording mode
716void CameraService::Client::stopRecording()
717{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800718 LOGV("stopRecording (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800719
Dave Sparksff0f38e2009-11-10 17:08:08 -0800720 // hold main lock during state transition
721 {
722 Mutex::Autolock lock(mLock);
723 if (checkPid() != NO_ERROR) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800724
Dave Sparksff0f38e2009-11-10 17:08:08 -0800725 if (mHardware == 0) {
726 LOGE("mHardware is NULL, returning.");
727 return;
728 }
729
730 if (mMediaPlayerBeep.get() != NULL) {
731 mMediaPlayerBeep->seekTo(0);
732 mMediaPlayerBeep->start();
733 }
734
735 mHardware->stopRecording();
736 mHardware->disableMsgType(CAMERA_MSG_VIDEO_FRAME);
Dave Sparkse7e93f92010-01-04 08:55:04 -0800737 LOGV("stopRecording(), hardware stopped OK");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800738 }
739
Dave Sparksff0f38e2009-11-10 17:08:08 -0800740 // hold preview buffer lock
741 {
742 Mutex::Autolock lock(mPreviewLock);
743 mPreviewBuffer.clear();
Jason Sams78b877e2009-03-24 20:21:36 -0700744 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800745}
746
747// release a recording frame
748void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem)
749{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800750 Mutex::Autolock lock(mLock);
751 if (checkPid() != NO_ERROR) return;
752
753 if (mHardware == 0) {
754 LOGE("mHardware is NULL, returning.");
755 return;
756 }
757
758 mHardware->releaseRecordingFrame(mem);
759}
760
761bool CameraService::Client::previewEnabled()
762{
763 Mutex::Autolock lock(mLock);
764 if (mHardware == 0) return false;
765 return mHardware->previewEnabled();
766}
767
768bool CameraService::Client::recordingEnabled()
769{
770 Mutex::Autolock lock(mLock);
771 if (mHardware == 0) return false;
772 return mHardware->recordingEnabled();
773}
774
775// Safely retrieves a strong pointer to the client during a hardware callback.
776sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user)
777{
778 sp<Client> client = 0;
779 CameraService *service = static_cast<CameraService*>(user);
780 if (service != NULL) {
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800781 Mutex::Autolock ourLock(service->mServiceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800782 if (service->mClient != 0) {
783 client = service->mClient.promote();
784 if (client == 0) {
785 LOGE("getClientFromCookie: client appears to have died");
786 service->mClient.clear();
787 }
788 } else {
789 LOGE("getClientFromCookie: got callback but client was NULL");
790 }
791 }
792 return client;
793}
794
795
796#if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE || \
797 DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE || \
798 DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
799static void dump_to_file(const char *fname,
800 uint8_t *buf, uint32_t size)
801{
802 int nw, cnt = 0;
803 uint32_t written = 0;
804
Joe Onorato51632e82010-01-07 21:48:32 -0500805 LOGV("opening file [%s]\n", fname);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800806 int fd = open(fname, O_RDWR | O_CREAT);
807 if (fd < 0) {
808 LOGE("failed to create file [%s]: %s", fname, strerror(errno));
809 return;
810 }
811
Joe Onorato51632e82010-01-07 21:48:32 -0500812 LOGV("writing %d bytes to file [%s]\n", size, fname);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800813 while (written < size) {
814 nw = ::write(fd,
815 buf + written,
816 size - written);
817 if (nw < 0) {
818 LOGE("failed to write to file [%s]: %s",
819 fname, strerror(errno));
820 break;
821 }
822 written += nw;
823 cnt++;
824 }
Joe Onorato51632e82010-01-07 21:48:32 -0500825 LOGV("done writing %d bytes to file [%s] in %d passes\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800826 size, fname, cnt);
827 ::close(fd);
828}
829#endif
830
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800831status_t CameraService::Client::autoFocus()
832{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800833 LOGV("autoFocus (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800834
835 Mutex::Autolock lock(mLock);
836 status_t result = checkPid();
837 if (result != NO_ERROR) return result;
838
839 if (mHardware == 0) {
840 LOGE("mHardware is NULL, returning.");
841 return INVALID_OPERATION;
842 }
843
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500844 return mHardware->autoFocus();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800845}
846
Chih-Chung Chang00900eb2009-09-15 14:51:56 +0800847status_t CameraService::Client::cancelAutoFocus()
848{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800849 LOGV("cancelAutoFocus (pid %d)", getCallingPid());
Chih-Chung Chang00900eb2009-09-15 14:51:56 +0800850
851 Mutex::Autolock lock(mLock);
852 status_t result = checkPid();
853 if (result != NO_ERROR) return result;
854
855 if (mHardware == 0) {
856 LOGE("mHardware is NULL, returning.");
857 return INVALID_OPERATION;
858 }
859
860 return mHardware->cancelAutoFocus();
861}
862
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800863// take a picture - image is returned in callback
864status_t CameraService::Client::takePicture()
865{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800866 LOGV("takePicture (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800867
868 Mutex::Autolock lock(mLock);
869 status_t result = checkPid();
870 if (result != NO_ERROR) return result;
871
872 if (mHardware == 0) {
873 LOGE("mHardware is NULL, returning.");
874 return INVALID_OPERATION;
875 }
876
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500877 mHardware->enableMsgType(CAMERA_MSG_SHUTTER |
878 CAMERA_MSG_POSTVIEW_FRAME |
879 CAMERA_MSG_RAW_IMAGE |
880 CAMERA_MSG_COMPRESSED_IMAGE);
881
882 return mHardware->takePicture();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800883}
884
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500885// snapshot taken
Wu-cheng Li986e0dc2009-10-23 17:39:46 +0800886void CameraService::Client::handleShutter(
887 image_rect_type *size // The width and height of yuv picture for
888 // registerBuffer. If this is NULL, use the picture
889 // size from parameters.
890)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800891{
Wu-cheng Liaab44b82009-03-24 20:39:09 -0700892 // Play shutter sound.
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500893 if (mMediaPlayerClick.get() != NULL) {
Eric Laurent059b4132009-11-27 05:07:55 -0800894 // do not play shutter sound if stream volume is 0
895 // (typically because ringer mode is silent).
896 int index;
897 AudioSystem::getStreamVolumeIndex(AudioSystem::ENFORCED_AUDIBLE, &index);
898 if (index != 0) {
899 mMediaPlayerClick->seekTo(0);
900 mMediaPlayerClick->start();
901 }
Wu-cheng Liaab44b82009-03-24 20:39:09 -0700902 }
903
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800904 // Screen goes black after the buffer is unregistered.
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500905 if (mSurface != 0 && !mUseOverlay) {
906 mSurface->unregisterBuffers();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800907 }
908
Dave Sparks393eb792009-10-15 10:02:22 -0700909 sp<ICameraClient> c = mCameraClient;
910 if (c != NULL) {
911 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
912 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500913 mHardware->disableMsgType(CAMERA_MSG_SHUTTER);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800914
915 // It takes some time before yuvPicture callback to be called.
916 // Register the buffer for raw image here to reduce latency.
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500917 if (mSurface != 0 && !mUseOverlay) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800918 int w, h;
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500919 CameraParameters params(mHardware->getParameters());
Wu-cheng Li986e0dc2009-10-23 17:39:46 +0800920 if (size == NULL) {
921 params.getPictureSize(&w, &h);
922 } else {
923 w = size->width;
924 h = size->height;
925 w &= ~1;
926 h &= ~1;
Dave Sparkse7e93f92010-01-04 08:55:04 -0800927 LOGV("Snapshot image width=%d, height=%d", w, h);
Wu-cheng Li986e0dc2009-10-23 17:39:46 +0800928 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800929 ISurface::BufferHeap buffers(w, h, w, h,
Chih-Chung Chang52e72002010-01-21 17:31:06 -0800930 PIXEL_FORMAT_YCbCr_420_SP, mOrientation, 0, mHardware->getRawHeap());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800931
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500932 mSurface->registerBuffers(buffers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800933 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800934}
935
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500936// preview callback - frame buffer update
937void CameraService::Client::handlePreviewData(const sp<IMemory>& mem)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800938{
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500939 ssize_t offset;
940 size_t size;
941 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
942
943#if DEBUG_HEAP_LEAKS && 0 // debugging
944 if (gWeakHeap == NULL) {
945 if (gWeakHeap != heap) {
Dave Sparkse7e93f92010-01-04 08:55:04 -0800946 LOGV("SETTING PREVIEW HEAP");
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500947 heap->trackMe(true, true);
948 gWeakHeap = heap;
949 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800950 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500951#endif
952#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
953 {
954 if (debug_frame_cnt++ == DEBUG_DUMP_PREVIEW_FRAME_TO_FILE) {
955 dump_to_file("/data/preview.yuv",
956 (uint8_t *)heap->base() + offset, size);
957 }
958 }
959#endif
960
961 if (!mUseOverlay)
962 {
963 Mutex::Autolock surfaceLock(mSurfaceLock);
964 if (mSurface != NULL) {
965 mSurface->postBuffer(offset);
966 }
967 }
968
Dave Sparks393eb792009-10-15 10:02:22 -0700969 // local copy of the callback flags
970 int flags = mPreviewCallbackFlag;
971
972 // is callback enabled?
973 if (!(flags & FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500974 // If the enable bit is off, the copy-out and one-shot bits are ignored
975 LOGV("frame callback is diabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800976 return;
977 }
978
Dave Sparks393eb792009-10-15 10:02:22 -0700979 // hold a strong pointer to the client
980 sp<ICameraClient> c = mCameraClient;
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500981
Dave Sparks393eb792009-10-15 10:02:22 -0700982 // clear callback flags if no client or one-shot mode
983 if ((c == NULL) || (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
984 LOGV("Disable preview callback");
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500985 mPreviewCallbackFlag &= ~(FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
986 FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
987 FRAME_CALLBACK_FLAG_ENABLE_MASK);
Dave Sparks393eb792009-10-15 10:02:22 -0700988 // TODO: Shouldn't we use this API for non-overlay hardware as well?
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500989 if (mUseOverlay)
990 mHardware->disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
991 }
Dave Sparks393eb792009-10-15 10:02:22 -0700992
993 // Is the received frame copied out or not?
994 if (flags & FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
995 LOGV("frame is copied");
996 copyFrameAndPostCopiedFrame(c, heap, offset, size);
997 } else {
998 LOGV("frame is forwarded");
999 c->dataCallback(CAMERA_MSG_PREVIEW_FRAME, mem);
1000 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001001}
1002
1003// picture callback - postview image ready
1004void CameraService::Client::handlePostview(const sp<IMemory>& mem)
1005{
1006#if DEBUG_DUMP_POSTVIEW_SNAPSHOT_TO_FILE // for testing pursposes only
1007 {
1008 ssize_t offset;
1009 size_t size;
1010 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1011 dump_to_file("/data/postview.yuv",
1012 (uint8_t *)heap->base() + offset, size);
1013 }
1014#endif
1015
Dave Sparks393eb792009-10-15 10:02:22 -07001016 sp<ICameraClient> c = mCameraClient;
1017 if (c != NULL) {
1018 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem);
1019 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001020 mHardware->disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
1021}
1022
1023// picture callback - raw image ready
1024void CameraService::Client::handleRawPicture(const sp<IMemory>& mem)
1025{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001026 ssize_t offset;
1027 size_t size;
1028 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1029#if DEBUG_HEAP_LEAKS && 0 // debugging
1030 gWeakHeap = heap; // debugging
1031#endif
1032
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001033 //LOGV("handleRawPicture(%d, %d)", offset, size);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001034#if DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE // for testing pursposes only
1035 dump_to_file("/data/photo.yuv",
1036 (uint8_t *)heap->base() + offset, size);
1037#endif
1038
1039 // Put the YUV version of the snapshot in the preview display.
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001040 if (mSurface != 0 && !mUseOverlay) {
1041 mSurface->postBuffer(offset);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001042 }
1043
Dave Sparks393eb792009-10-15 10:02:22 -07001044 sp<ICameraClient> c = mCameraClient;
1045 if (c != NULL) {
1046 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem);
1047 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001048 mHardware->disableMsgType(CAMERA_MSG_RAW_IMAGE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001049}
1050
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001051// picture callback - compressed picture ready
1052void CameraService::Client::handleCompressedPicture(const sp<IMemory>& mem)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001053{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001054#if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE // for testing pursposes only
1055 {
1056 ssize_t offset;
1057 size_t size;
1058 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1059 dump_to_file("/data/photo.jpg",
1060 (uint8_t *)heap->base() + offset, size);
1061 }
1062#endif
1063
Dave Sparks393eb792009-10-15 10:02:22 -07001064 sp<ICameraClient> c = mCameraClient;
1065 if (c != NULL) {
1066 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem);
1067 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001068 mHardware->disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069}
1070
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001071void CameraService::Client::notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072{
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001073 LOGV("notifyCallback(%d)", msgType);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001074
1075 sp<Client> client = getClientFromCookie(user);
1076 if (client == 0) {
1077 return;
1078 }
1079
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001080 switch (msgType) {
1081 case CAMERA_MSG_SHUTTER:
Wu-cheng Li986e0dc2009-10-23 17:39:46 +08001082 // ext1 is the dimension of the yuv picture.
1083 client->handleShutter((image_rect_type *)ext1);
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001084 break;
1085 default:
Dave Sparks393eb792009-10-15 10:02:22 -07001086 sp<ICameraClient> c = client->mCameraClient;
1087 if (c != NULL) {
1088 c->notifyCallback(msgType, ext1, ext2);
1089 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001090 break;
1091 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092
1093#if DEBUG_CLIENT_REFERENCES
1094 if (client->getStrongCount() == 1) {
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001095 LOGE("++++++++++++++++ (NOTIFY CALLBACK) THIS WILL CAUSE A LOCKUP!");
1096 client->printRefs();
1097 }
1098#endif
1099}
1100
1101void CameraService::Client::dataCallback(int32_t msgType, const sp<IMemory>& dataPtr, void* user)
1102{
1103 LOGV("dataCallback(%d)", msgType);
1104
1105 sp<Client> client = getClientFromCookie(user);
1106 if (client == 0) {
1107 return;
1108 }
1109
Dave Sparks393eb792009-10-15 10:02:22 -07001110 sp<ICameraClient> c = client->mCameraClient;
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001111 if (dataPtr == NULL) {
1112 LOGE("Null data returned in data callback");
Dave Sparks393eb792009-10-15 10:02:22 -07001113 if (c != NULL) {
1114 c->notifyCallback(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
1115 c->dataCallback(msgType, NULL);
1116 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001117 return;
1118 }
1119
1120 switch (msgType) {
1121 case CAMERA_MSG_PREVIEW_FRAME:
1122 client->handlePreviewData(dataPtr);
1123 break;
1124 case CAMERA_MSG_POSTVIEW_FRAME:
1125 client->handlePostview(dataPtr);
1126 break;
1127 case CAMERA_MSG_RAW_IMAGE:
1128 client->handleRawPicture(dataPtr);
1129 break;
1130 case CAMERA_MSG_COMPRESSED_IMAGE:
1131 client->handleCompressedPicture(dataPtr);
1132 break;
1133 default:
Dave Sparks393eb792009-10-15 10:02:22 -07001134 if (c != NULL) {
1135 c->dataCallback(msgType, dataPtr);
1136 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001137 break;
1138 }
1139
1140#if DEBUG_CLIENT_REFERENCES
1141 if (client->getStrongCount() == 1) {
1142 LOGE("++++++++++++++++ (DATA CALLBACK) THIS WILL CAUSE A LOCKUP!");
1143 client->printRefs();
1144 }
1145#endif
1146}
1147
1148void CameraService::Client::dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType,
1149 const sp<IMemory>& dataPtr, void* user)
1150{
1151 LOGV("dataCallbackTimestamp(%d)", msgType);
1152
1153 sp<Client> client = getClientFromCookie(user);
1154 if (client == 0) {
1155 return;
1156 }
Dave Sparks393eb792009-10-15 10:02:22 -07001157 sp<ICameraClient> c = client->mCameraClient;
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001158
1159 if (dataPtr == NULL) {
1160 LOGE("Null data returned in data with timestamp callback");
Dave Sparks393eb792009-10-15 10:02:22 -07001161 if (c != NULL) {
1162 c->notifyCallback(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
1163 c->dataCallbackTimestamp(0, msgType, NULL);
1164 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001165 return;
1166 }
1167
Dave Sparks393eb792009-10-15 10:02:22 -07001168 if (c != NULL) {
1169 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
1170 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001171
1172#if DEBUG_CLIENT_REFERENCES
1173 if (client->getStrongCount() == 1) {
1174 LOGE("++++++++++++++++ (DATA CALLBACK TIMESTAMP) THIS WILL CAUSE A LOCKUP!");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001175 client->printRefs();
1176 }
1177#endif
1178}
1179
1180// set preview/capture parameters - key/value pairs
1181status_t CameraService::Client::setParameters(const String8& params)
1182{
Joe Onorato51632e82010-01-07 21:48:32 -05001183 LOGV("setParameters(%s)", params.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184
1185 Mutex::Autolock lock(mLock);
1186 status_t result = checkPid();
1187 if (result != NO_ERROR) return result;
1188
1189 if (mHardware == 0) {
1190 LOGE("mHardware is NULL, returning.");
1191 return INVALID_OPERATION;
1192 }
1193
1194 CameraParameters p(params);
Chih-Chung Chang52e72002010-01-21 17:31:06 -08001195
James Dong102f7772009-09-13 17:10:24 -07001196 return mHardware->setParameters(p);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001197}
1198
1199// get preview/capture parameters - key/value pairs
1200String8 CameraService::Client::getParameters() const
1201{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001202 Mutex::Autolock lock(mLock);
1203
1204 if (mHardware == 0) {
1205 LOGE("mHardware is NULL, returning.");
1206 return String8();
1207 }
1208
Wu-cheng Li81d763f2009-04-22 16:21:26 +08001209 String8 params(mHardware->getParameters().flatten());
Joe Onorato51632e82010-01-07 21:48:32 -05001210 LOGV("getParameters(%s)", params.string());
Wu-cheng Li81d763f2009-04-22 16:21:26 +08001211 return params;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001212}
1213
Wu-cheng Lie6a550d2009-09-28 16:14:58 -07001214status_t CameraService::Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
1215{
Dave Sparkse7e93f92010-01-04 08:55:04 -08001216 LOGV("sendCommand (pid %d)", getCallingPid());
Wu-cheng Lie6a550d2009-09-28 16:14:58 -07001217 Mutex::Autolock lock(mLock);
1218 status_t result = checkPid();
1219 if (result != NO_ERROR) return result;
1220
Chih-Chung Changf091e832010-01-22 17:49:48 -08001221 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
1222 // The orientation cannot be set during preview.
1223 if (mHardware->previewEnabled()) {
1224 return INVALID_OPERATION;
1225 }
1226 switch (arg1) {
1227 case 0:
1228 mOrientation = ISurface::BufferHeap::ROT_0;
1229 break;
1230 case 90:
1231 mOrientation = ISurface::BufferHeap::ROT_90;
1232 break;
1233 case 180:
1234 mOrientation = ISurface::BufferHeap::ROT_180;
1235 break;
1236 case 270:
1237 mOrientation = ISurface::BufferHeap::ROT_270;
1238 break;
1239 default:
1240 return BAD_VALUE;
1241 }
1242 return OK;
1243 }
1244
Wu-cheng Lie6a550d2009-09-28 16:14:58 -07001245 if (mHardware == 0) {
1246 LOGE("mHardware is NULL, returning.");
1247 return INVALID_OPERATION;
1248 }
1249
1250 return mHardware->sendCommand(cmd, arg1, arg2);
1251}
1252
Dave Sparks393eb792009-10-15 10:02:22 -07001253void CameraService::Client::copyFrameAndPostCopiedFrame(const sp<ICameraClient>& client,
1254 const sp<IMemoryHeap>& heap, size_t offset, size_t size)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001255{
1256 LOGV("copyFrameAndPostCopiedFrame");
1257 // It is necessary to copy out of pmem before sending this to
1258 // the callback. For efficiency, reuse the same MemoryHeapBase
1259 // provided it's big enough. Don't allocate the memory or
1260 // perform the copy if there's no callback.
Dave Sparks23c21ba2009-11-06 11:47:13 -08001261
Dave Sparksff0f38e2009-11-10 17:08:08 -08001262 // hold the preview lock while we grab a reference to the preview buffer
Dave Sparks23c21ba2009-11-06 11:47:13 -08001263 sp<MemoryHeapBase> previewBuffer;
1264 {
Dave Sparksff0f38e2009-11-10 17:08:08 -08001265 Mutex::Autolock lock(mPreviewLock);
Dave Sparks23c21ba2009-11-06 11:47:13 -08001266 if (mPreviewBuffer == 0) {
1267 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1268 } else if (size > mPreviewBuffer->virtualSize()) {
1269 mPreviewBuffer.clear();
1270 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1271 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001272 if (mPreviewBuffer == 0) {
1273 LOGE("failed to allocate space for preview buffer");
1274 return;
1275 }
Dave Sparks23c21ba2009-11-06 11:47:13 -08001276 previewBuffer = mPreviewBuffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001277 }
Dave Sparks23c21ba2009-11-06 11:47:13 -08001278 memcpy(previewBuffer->base(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001279 (uint8_t *)heap->base() + offset, size);
1280
Dave Sparks23c21ba2009-11-06 11:47:13 -08001281 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001282 if (frame == 0) {
1283 LOGE("failed to allocate space for frame callback");
1284 return;
1285 }
Dave Sparks393eb792009-10-15 10:02:22 -07001286 client->dataCallback(CAMERA_MSG_PREVIEW_FRAME, frame);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001287}
1288
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001289status_t CameraService::dump(int fd, const Vector<String16>& args)
1290{
1291 const size_t SIZE = 256;
1292 char buffer[SIZE];
1293 String8 result;
1294 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
1295 snprintf(buffer, SIZE, "Permission Denial: "
1296 "can't dump CameraService from pid=%d, uid=%d\n",
Chih-Chung Changd98c5162009-06-22 16:03:41 +08001297 getCallingPid(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001298 IPCThreadState::self()->getCallingUid());
1299 result.append(buffer);
1300 write(fd, result.string(), result.size());
1301 } else {
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +08001302 AutoMutex lock(&mServiceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001303 if (mClient != 0) {
1304 sp<Client> currentClient = mClient.promote();
1305 sprintf(buffer, "Client (%p) PID: %d\n",
1306 currentClient->getCameraClient()->asBinder().get(),
1307 currentClient->mClientPid);
1308 result.append(buffer);
1309 write(fd, result.string(), result.size());
1310 currentClient->mHardware->dump(fd, args);
1311 } else {
1312 result.append("No camera client yet.\n");
1313 write(fd, result.string(), result.size());
1314 }
1315 }
1316 return NO_ERROR;
1317}
1318
1319
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001320status_t CameraService::onTransact(
1321 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1322{
1323 // permission checks...
1324 switch (code) {
1325 case BnCameraService::CONNECT:
1326 IPCThreadState* ipc = IPCThreadState::self();
1327 const int pid = ipc->getCallingPid();
1328 const int self_pid = getpid();
1329 if (pid != self_pid) {
1330 // we're called from a different process, do the real check
1331 if (!checkCallingPermission(
1332 String16("android.permission.CAMERA")))
1333 {
1334 const int uid = ipc->getCallingUid();
1335 LOGE("Permission Denial: "
1336 "can't use the camera pid=%d, uid=%d", pid, uid);
1337 return PERMISSION_DENIED;
1338 }
1339 }
1340 break;
1341 }
1342
1343 status_t err = BnCameraService::onTransact(code, data, reply, flags);
1344
Dave Sparks998b3292009-05-20 20:02:59 -07001345#if DEBUG_HEAP_LEAKS
Joe Onorato51632e82010-01-07 21:48:32 -05001346 LOGV("+++ onTransact err %d code %d", err, code);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001347
1348 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
1349 // the 'service' command interrogates this binder for its name, and then supplies it
1350 // even for the debugging commands. that means we need to check for it here, using
1351 // ISurfaceComposer (since we delegated the INTERFACE_TRANSACTION handling to
1352 // BnSurfaceComposer before falling through to this code).
1353
Joe Onorato51632e82010-01-07 21:48:32 -05001354 LOGV("+++ onTransact code %d", code);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001355
1356 CHECK_INTERFACE(ICameraService, data, reply);
1357
1358 switch(code) {
1359 case 1000:
1360 {
1361 if (gWeakHeap != 0) {
1362 sp<IMemoryHeap> h = gWeakHeap.promote();
1363 IMemoryHeap *p = gWeakHeap.unsafe_get();
Joe Onorato51632e82010-01-07 21:48:32 -05001364 LOGV("CHECKING WEAK REFERENCE %p (%p)", h.get(), p);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001365 if (h != 0)
1366 h->printRefs();
1367 bool attempt_to_delete = data.readInt32() == 1;
1368 if (attempt_to_delete) {
1369 // NOT SAFE!
Joe Onorato51632e82010-01-07 21:48:32 -05001370 LOGV("DELETING WEAK REFERENCE %p (%p)", h.get(), p);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001371 if (p) delete p;
1372 }
1373 return NO_ERROR;
1374 }
1375 }
1376 break;
1377 default:
1378 break;
1379 }
1380 }
Dave Sparks998b3292009-05-20 20:02:59 -07001381#endif // DEBUG_HEAP_LEAKS
Dave Sparksfec880d2009-05-21 09:18:18 -07001382
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001383 return err;
1384}
1385
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001386}; // namespace android