blob: 405d4715a6d4b865b3c608344e477f2bee79bbec [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>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028#include <ui/ICameraService.h>
29
Jason Sams78b877e2009-03-24 20:21:36 -070030#include <media/mediaplayer.h>
31#include <media/AudioSystem.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032#include "CameraService.h"
33
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +080034#include <cutils/atomic.h>
Eric Laurentcbcb00e2009-03-27 16:27:16 -070035
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036namespace android {
37
38extern "C" {
39#include <stdio.h>
40#include <sys/types.h>
41#include <sys/stat.h>
42#include <fcntl.h>
43#include <pthread.h>
Chih-Chung Changd98c5162009-06-22 16:03:41 +080044#include <signal.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045}
46
47// When you enable this, as well as DEBUG_REFS=1 and
48// DEBUG_REFS_ENABLED_BY_DEFAULT=0 in libutils/RefBase.cpp, this will track all
49// references to the CameraService::Client in order to catch the case where the
50// client is being destroyed while a callback from the CameraHardwareInterface
51// is outstanding. This is a serious bug because if we make another call into
52// CameraHardwreInterface that itself triggers a callback, we will deadlock.
53
54#define DEBUG_CLIENT_REFERENCES 0
55
56#define PICTURE_TIMEOUT seconds(5)
57
58#define DEBUG_DUMP_PREVIEW_FRAME_TO_FILE 0 /* n-th frame to write */
59#define DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE 0
60#define DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE 0
Benny Wong4c8fb0a2009-08-12 12:01:27 -050061#define DEBUG_DUMP_POSTVIEW_SNAPSHOT_TO_FILE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
63#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
64static int debug_frame_cnt;
65#endif
66
Chih-Chung Changd98c5162009-06-22 16:03:41 +080067static int getCallingPid() {
68 return IPCThreadState::self()->getCallingPid();
69}
70
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071// ----------------------------------------------------------------------------
72
73void CameraService::instantiate() {
74 defaultServiceManager()->addService(
75 String16("media.camera"), new CameraService());
76}
77
78// ----------------------------------------------------------------------------
79
80CameraService::CameraService() :
81 BnCameraService()
82{
83 LOGI("CameraService started: pid=%d", getpid());
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +080084 mUsers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085}
86
87CameraService::~CameraService()
88{
89 if (mClient != 0) {
90 LOGE("mClient was still connected in destructor!");
91 }
92}
93
94sp<ICamera> CameraService::connect(const sp<ICameraClient>& cameraClient)
95{
Chih-Chung Changd98c5162009-06-22 16:03:41 +080096 int callingPid = getCallingPid();
Joe Onorato51632e82010-01-07 21:48:32 -050097 LOGV("CameraService::connect E (pid %d, client %p)", callingPid,
Chih-Chung Changd98c5162009-06-22 16:03:41 +080098 cameraClient->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800100 Mutex::Autolock lock(mServiceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101 sp<Client> client;
102 if (mClient != 0) {
103 sp<Client> currentClient = mClient.promote();
104 if (currentClient != 0) {
105 sp<ICameraClient> currentCameraClient(currentClient->getCameraClient());
106 if (cameraClient->asBinder() == currentCameraClient->asBinder()) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800107 // This is the same client reconnecting...
Joe Onorato51632e82010-01-07 21:48:32 -0500108 LOGV("CameraService::connect X (pid %d, same client %p) is reconnecting...",
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800109 callingPid, cameraClient->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110 return currentClient;
111 } else {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800112 // It's another client... reject it
Joe Onorato51632e82010-01-07 21:48:32 -0500113 LOGV("CameraService::connect X (pid %d, new client %p) rejected. "
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800114 "(old pid %d, old client %p)",
115 callingPid, cameraClient->asBinder().get(),
116 currentClient->mClientPid, currentCameraClient->asBinder().get());
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800117 if (kill(currentClient->mClientPid, 0) == -1 && errno == ESRCH) {
Joe Onorato51632e82010-01-07 21:48:32 -0500118 LOGV("The old client is dead!");
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800119 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120 return client;
121 }
122 } else {
123 // can't promote, the previous client has died...
Joe Onorato51632e82010-01-07 21:48:32 -0500124 LOGV("New client (pid %d) connecting, old reference was dangling...",
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800125 callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800126 mClient.clear();
127 }
128 }
129
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800130 if (mUsers > 0) {
Joe Onorato51632e82010-01-07 21:48:32 -0500131 LOGV("Still have client, rejected");
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800132 return client;
133 }
134
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135 // 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
Joe Onorato51632e82010-01-07 21:48:32 -0500143 LOGV("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
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800155 Mutex::Autolock lock(mServiceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156
157 if (mClient == 0) {
158 // This happens when we have already disconnected.
Joe Onorato51632e82010-01-07 21:48:32 -0500159 LOGV("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) {
Joe Onorato51632e82010-01-07 21:48:32 -0500167 LOGV("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
Joe Onorato51632e82010-01-07 21:48:32 -0500180 LOGV("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
Wu-cheng Lie6a550d2009-09-28 16:14:58 -0700197static sp<MediaPlayer> newMediaPlayer(const char *file)
Jason Sams78b877e2009-03-24 20:21:36 -0700198{
199 sp<MediaPlayer> mp = new MediaPlayer();
Andreas Huberd4461e72010-01-28 11:19:57 -0800200 if (mp->setDataSource(file, NULL /* headers */) == NO_ERROR) {
Eric Laurent9d91ad52009-07-17 12:17:14 -0700201 mp->setAudioStreamType(AudioSystem::ENFORCED_AUDIBLE);
Jason Sams78b877e2009-03-24 20:21:36 -0700202 mp->prepare();
203 } else {
204 mp.clear();
205 LOGE("Failed to load CameraService sounds.");
206 }
207 return mp;
208}
209
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210CameraService::Client::Client(const sp<CameraService>& cameraService,
211 const sp<ICameraClient>& cameraClient, pid_t clientPid)
212{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800213 int callingPid = getCallingPid();
Joe Onorato51632e82010-01-07 21:48:32 -0500214 LOGV("Client::Client E (pid %d)", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215 mCameraService = cameraService;
216 mCameraClient = cameraClient;
217 mClientPid = clientPid;
218 mHardware = openCameraHardware();
219 mUseOverlay = mHardware->useOverlay();
220
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500221 mHardware->setCallbacks(notifyCallback,
222 dataCallback,
223 dataCallbackTimestamp,
224 mCameraService.get());
225
226 // Enable zoom, error, and focus messages by default
227 mHardware->enableMsgType(CAMERA_MSG_ERROR |
228 CAMERA_MSG_ZOOM |
229 CAMERA_MSG_FOCUS);
230
Jason Sams78b877e2009-03-24 20:21:36 -0700231 mMediaPlayerClick = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
232 mMediaPlayerBeep = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
Benny Wong71f77152009-07-15 18:44:27 -0500233 mOverlayW = 0;
234 mOverlayH = 0;
Jason Sams78b877e2009-03-24 20:21:36 -0700235
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236 // Callback is disabled by default
237 mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
Chih-Chung Chang52e72002010-01-21 17:31:06 -0800238 mOrientation = 0;
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800239 cameraService->incUsers();
Joe Onorato51632e82010-01-07 21:48:32 -0500240 LOGV("Client::Client X (pid %d)", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241}
242
243status_t CameraService::Client::checkPid()
244{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800245 int callingPid = getCallingPid();
246 if (mClientPid == callingPid) return NO_ERROR;
247 LOGW("Attempt to use locked camera (client %p) from different process "
248 " (old pid %d, new pid %d)",
249 getCameraClient()->asBinder().get(), mClientPid, callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250 return -EBUSY;
251}
252
253status_t CameraService::Client::lock()
254{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800255 int callingPid = getCallingPid();
Joe Onorato51632e82010-01-07 21:48:32 -0500256 LOGV("lock from pid %d (mClientPid %d)", callingPid, mClientPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257 Mutex::Autolock _l(mLock);
258 // lock camera to this client if the the camera is unlocked
259 if (mClientPid == 0) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800260 mClientPid = callingPid;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261 return NO_ERROR;
262 }
263 // returns NO_ERROR if the client already owns the camera, -EBUSY otherwise
264 return checkPid();
265}
266
267status_t CameraService::Client::unlock()
268{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800269 int callingPid = getCallingPid();
Joe Onorato51632e82010-01-07 21:48:32 -0500270 LOGV("unlock from pid %d (mClientPid %d)", callingPid, mClientPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271 Mutex::Autolock _l(mLock);
272 // allow anyone to use camera
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273 status_t result = checkPid();
James Dong0ae13e32009-04-20 19:35:28 -0700274 if (result == NO_ERROR) {
275 mClientPid = 0;
Joe Onorato51632e82010-01-07 21:48:32 -0500276 LOGV("clear mCameraClient (pid %d)", callingPid);
James Dong0ae13e32009-04-20 19:35:28 -0700277 // we need to remove the reference so that when app goes
278 // away, the reference count goes to 0.
279 mCameraClient.clear();
280 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281 return result;
282}
283
284status_t CameraService::Client::connect(const sp<ICameraClient>& client)
285{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800286 int callingPid = getCallingPid();
287
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288 // connect a new process to the camera
Joe Onorato51632e82010-01-07 21:48:32 -0500289 LOGV("Client::connect E (pid %d, client %p)", callingPid, client->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800290
291 // I hate this hack, but things get really ugly when the media recorder
292 // service is handing back the camera to the app. The ICameraClient
293 // destructor will be called during the same IPC, making it look like
294 // the remote client is trying to disconnect. This hack temporarily
295 // sets the mClientPid to an invalid pid to prevent the hardware from
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800296 // being torn down.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800297 {
298
299 // hold a reference to the old client or we will deadlock if the client is
300 // in the same process and we hold the lock when we remove the reference
301 sp<ICameraClient> oldClient;
302 {
303 Mutex::Autolock _l(mLock);
Chih-Chung Chang34e5a152009-06-09 13:56:44 +0800304 if (mClientPid != 0 && checkPid() != NO_ERROR) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800305 LOGW("Tried to connect to locked camera (old pid %d, new pid %d)",
306 mClientPid, callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307 return -EBUSY;
308 }
309 oldClient = mCameraClient;
310
311 // did the client actually change?
Dave Sparks393eb792009-10-15 10:02:22 -0700312 if ((mCameraClient != NULL) && (client->asBinder() == mCameraClient->asBinder())) {
Joe Onorato51632e82010-01-07 21:48:32 -0500313 LOGV("Connect to the same client");
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800314 return NO_ERROR;
315 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316
317 mCameraClient = client;
318 mClientPid = -1;
319 mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
Joe Onorato51632e82010-01-07 21:48:32 -0500320 LOGV("Connect to the new client (pid %d, client %p)",
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800321 callingPid, mCameraClient->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322 }
323
324 }
325 // the old client destructor is called when oldClient goes out of scope
326 // now we set the new PID to lock the interface again
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800327 mClientPid = callingPid;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328
329 return NO_ERROR;
330}
331
332#if HAVE_ANDROID_OS
333static void *unregister_surface(void *arg)
334{
335 ISurface *surface = (ISurface *)arg;
336 surface->unregisterBuffers();
337 IPCThreadState::self()->flushCommands();
338 return NULL;
339}
340#endif
341
342CameraService::Client::~Client()
343{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800344 int callingPid = getCallingPid();
345
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346 // tear down client
Joe Onorato51632e82010-01-07 21:48:32 -0500347 LOGV("Client::~Client E (pid %d, client %p)",
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800348 callingPid, getCameraClient()->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349 if (mSurface != 0 && !mUseOverlay) {
350#if HAVE_ANDROID_OS
351 pthread_t thr;
352 // We unregister the buffers in a different thread because binder does
353 // not let us make sychronous transactions in a binder destructor (that
354 // is, upon our reaching a refcount of zero.)
355 pthread_create(&thr, NULL,
356 unregister_surface,
357 mSurface.get());
358 pthread_join(thr, NULL);
359#else
360 mSurface->unregisterBuffers();
361#endif
362 }
363
Jason Sams9e431ac2009-03-24 20:36:57 -0700364 if (mMediaPlayerBeep.get() != NULL) {
365 mMediaPlayerBeep->disconnect();
366 mMediaPlayerBeep.clear();
367 }
368 if (mMediaPlayerClick.get() != NULL) {
369 mMediaPlayerClick->disconnect();
370 mMediaPlayerClick.clear();
371 }
Jason Sams78b877e2009-03-24 20:21:36 -0700372
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800373 // make sure we tear down the hardware
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800374 mClientPid = callingPid;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800375 disconnect();
Joe Onorato51632e82010-01-07 21:48:32 -0500376 LOGV("Client::~Client X (pid %d)", mClientPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800377}
378
379void CameraService::Client::disconnect()
380{
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800381 int callingPid = getCallingPid();
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800382
Joe Onorato51632e82010-01-07 21:48:32 -0500383 LOGV("Client::disconnect() E (pid %d client %p)",
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800384 callingPid, getCameraClient()->asBinder().get());
385
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800386 Mutex::Autolock lock(mLock);
387 if (mClientPid <= 0) {
Joe Onorato51632e82010-01-07 21:48:32 -0500388 LOGV("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800389 return;
390 }
391 if (checkPid() != NO_ERROR) {
Joe Onorato51632e82010-01-07 21:48:32 -0500392 LOGV("Different client - don't disconnect");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800393 return;
394 }
395
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800396 // Make sure disconnect() is done once and once only, whether it is called
397 // from the user directly, or called by the destructor.
398 if (mHardware == 0) return;
399
Joe Onorato51632e82010-01-07 21:48:32 -0500400 LOGV("hardware teardown");
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800401 // Before destroying mHardware, we must make sure it's in the
402 // idle state.
403 mHardware->stopPreview();
404 // Cancel all picture callbacks.
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500405 mHardware->disableMsgType(CAMERA_MSG_SHUTTER |
406 CAMERA_MSG_POSTVIEW_FRAME |
407 CAMERA_MSG_RAW_IMAGE |
408 CAMERA_MSG_COMPRESSED_IMAGE);
409 mHardware->cancelPicture();
410 // Turn off remaining messages.
411 mHardware->disableMsgType(CAMERA_MSG_ALL_MSGS);
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800412 // Release the hardware resources.
413 mHardware->release();
Benny Wong71f77152009-07-15 18:44:27 -0500414 // Release the held overlay resources.
415 if (mUseOverlay)
416 {
417 mOverlayRef = 0;
418 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800419 mHardware.clear();
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800420
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800421 mCameraService->removeClient(mCameraClient);
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800422 mCameraService->decUsers();
423
Joe Onorato51632e82010-01-07 21:48:32 -0500424 LOGV("Client::disconnect() X (pid %d)", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800425}
426
427// pass the buffered ISurface to the camera service
428status_t CameraService::Client::setPreviewDisplay(const sp<ISurface>& surface)
429{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800430 LOGV("setPreviewDisplay(%p) (pid %d)",
Wu-cheng Li988fb622009-06-23 23:37:36 +0800431 ((surface == NULL) ? NULL : surface.get()), getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800432 Mutex::Autolock lock(mLock);
433 status_t result = checkPid();
434 if (result != NO_ERROR) return result;
Wu-cheng Li988fb622009-06-23 23:37:36 +0800435
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800436 Mutex::Autolock surfaceLock(mSurfaceLock);
Wu-cheng Li988fb622009-06-23 23:37:36 +0800437 result = NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800438 // asBinder() is safe on NULL (returns NULL)
439 if (surface->asBinder() != mSurface->asBinder()) {
Benny Wong71f77152009-07-15 18:44:27 -0500440 if (mSurface != 0) {
Dave Sparkse7e93f92010-01-04 08:55:04 -0800441 LOGV("clearing old preview surface %p", mSurface.get());
Benny Wong71f77152009-07-15 18:44:27 -0500442 if ( !mUseOverlay)
443 {
444 mSurface->unregisterBuffers();
445 }
446 else
447 {
448 // Force the destruction of any previous overlay
449 sp<Overlay> dummy;
450 mHardware->setOverlay( dummy );
451 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800452 }
453 mSurface = surface;
Benny Wong71f77152009-07-15 18:44:27 -0500454 mOverlayRef = 0;
Wu-cheng Li988fb622009-06-23 23:37:36 +0800455 // If preview has been already started, set overlay or register preview
456 // buffers now.
457 if (mHardware->previewEnabled()) {
458 if (mUseOverlay) {
459 result = setOverlay();
460 } else if (mSurface != 0) {
461 result = registerPreviewBuffers();
462 }
463 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800464 }
Wu-cheng Li988fb622009-06-23 23:37:36 +0800465 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800466}
467
468// set the preview callback flag to affect how the received frames from
469// preview are handled.
470void CameraService::Client::setPreviewCallbackFlag(int callback_flag)
471{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800472 LOGV("setPreviewCallbackFlag (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800473 Mutex::Autolock lock(mLock);
474 if (checkPid() != NO_ERROR) return;
475 mPreviewCallbackFlag = callback_flag;
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500476
477 if(mUseOverlay) {
478 if(mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ENABLE_MASK)
479 mHardware->enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
480 else
481 mHardware->disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
482 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800483}
484
Wu-cheng Li988fb622009-06-23 23:37:36 +0800485// start preview mode
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800486status_t CameraService::Client::startCameraMode(camera_mode mode)
487{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800488 int callingPid = getCallingPid();
489
Dave Sparkse7e93f92010-01-04 08:55:04 -0800490 LOGV("startCameraMode(%d) (pid %d)", mode, callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800491
492 /* we cannot call into mHardware with mLock held because
493 * mHardware has callbacks onto us which acquire this lock
494 */
495
496 Mutex::Autolock lock(mLock);
497 status_t result = checkPid();
498 if (result != NO_ERROR) return result;
499
500 if (mHardware == 0) {
501 LOGE("mHardware is NULL, returning.");
502 return INVALID_OPERATION;
503 }
504
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800505 switch(mode) {
506 case CAMERA_RECORDING_MODE:
Wu-cheng Li988fb622009-06-23 23:37:36 +0800507 if (mSurface == 0) {
508 LOGE("setPreviewDisplay must be called before startRecordingMode.");
509 return INVALID_OPERATION;
510 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800511 return startRecordingMode();
512
513 default: // CAMERA_PREVIEW_MODE
Wu-cheng Li988fb622009-06-23 23:37:36 +0800514 if (mSurface == 0) {
Dave Sparkse7e93f92010-01-04 08:55:04 -0800515 LOGV("mSurface is not set yet.");
Wu-cheng Li988fb622009-06-23 23:37:36 +0800516 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800517 return startPreviewMode();
518 }
519}
520
521status_t CameraService::Client::startRecordingMode()
522{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800523 LOGV("startRecordingMode (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800524
525 status_t ret = UNKNOWN_ERROR;
526
527 // if preview has not been started, start preview first
528 if (!mHardware->previewEnabled()) {
529 ret = startPreviewMode();
530 if (ret != NO_ERROR) {
531 return ret;
532 }
533 }
534
535 // if recording has been enabled, nothing needs to be done
536 if (mHardware->recordingEnabled()) {
537 return NO_ERROR;
538 }
539
540 // start recording mode
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500541 ret = mHardware->startRecording();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800542 if (ret != NO_ERROR) {
543 LOGE("mHardware->startRecording() failed with status %d", ret);
544 }
545 return ret;
546}
547
Wu-cheng Li988fb622009-06-23 23:37:36 +0800548status_t CameraService::Client::setOverlay()
549{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800550 LOGV("setOverlay");
Wu-cheng Li988fb622009-06-23 23:37:36 +0800551 int w, h;
552 CameraParameters params(mHardware->getParameters());
553 params.getPreviewSize(&w, &h);
554
Benny Wong71f77152009-07-15 18:44:27 -0500555 if ( w != mOverlayW || h != mOverlayH )
556 {
557 // Force the destruction of any previous overlay
558 sp<Overlay> dummy;
559 mHardware->setOverlay( dummy );
560 mOverlayRef = 0;
561 }
562
Wu-cheng Li988fb622009-06-23 23:37:36 +0800563 status_t ret = NO_ERROR;
564 if (mSurface != 0) {
Benny Wong71f77152009-07-15 18:44:27 -0500565 if (mOverlayRef.get() == NULL) {
Dave Sparks587f7832009-10-07 19:18:20 -0700566
567 // FIXME:
568 // Surfaceflinger may hold onto the previous overlay reference for some
569 // time after we try to destroy it. retry a few times. In the future, we
570 // should make the destroy call block, or possibly specify that we can
571 // wait in the createOverlay call if the previous overlay is in the
572 // process of being destroyed.
573 for (int retry = 0; retry < 50; ++retry) {
Chih-Chung Chang52e72002010-01-21 17:31:06 -0800574 mOverlayRef = mSurface->createOverlay(w, h, OVERLAY_FORMAT_DEFAULT,
575 mOrientation);
Dave Sparks587f7832009-10-07 19:18:20 -0700576 if (mOverlayRef != NULL) break;
Dave Sparkse7e93f92010-01-04 08:55:04 -0800577 LOGW("Overlay create failed - retrying");
Dave Sparks587f7832009-10-07 19:18:20 -0700578 usleep(20000);
579 }
Benny Wong71f77152009-07-15 18:44:27 -0500580 if ( mOverlayRef.get() == NULL )
581 {
582 LOGE("Overlay Creation Failed!");
583 return -EINVAL;
584 }
585 ret = mHardware->setOverlay(new Overlay(mOverlayRef));
586 }
Wu-cheng Li988fb622009-06-23 23:37:36 +0800587 } else {
588 ret = mHardware->setOverlay(NULL);
589 }
590 if (ret != NO_ERROR) {
591 LOGE("mHardware->setOverlay() failed with status %d\n", ret);
592 }
Benny Wong71f77152009-07-15 18:44:27 -0500593
594 mOverlayW = w;
595 mOverlayH = h;
596
Wu-cheng Li988fb622009-06-23 23:37:36 +0800597 return ret;
598}
599
600status_t CameraService::Client::registerPreviewBuffers()
601{
602 int w, h;
603 CameraParameters params(mHardware->getParameters());
604 params.getPreviewSize(&w, &h);
605
Wu-cheng Li988fb622009-06-23 23:37:36 +0800606 ISurface::BufferHeap buffers(w, h, w, h,
607 PIXEL_FORMAT_YCbCr_420_SP,
Chih-Chung Chang52e72002010-01-21 17:31:06 -0800608 mOrientation,
Wu-cheng Li988fb622009-06-23 23:37:36 +0800609 0,
610 mHardware->getPreviewHeap());
611
612 status_t ret = mSurface->registerBuffers(buffers);
613 if (ret != NO_ERROR) {
614 LOGE("registerBuffers failed with status %d", ret);
615 }
616 return ret;
617}
618
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800619status_t CameraService::Client::startPreviewMode()
620{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800621 LOGV("startPreviewMode (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800622
623 // if preview has been enabled, nothing needs to be done
624 if (mHardware->previewEnabled()) {
625 return NO_ERROR;
626 }
627
628 // start preview mode
629#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
630 debug_frame_cnt = 0;
631#endif
Wu-cheng Li988fb622009-06-23 23:37:36 +0800632 status_t ret = NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800633
634 if (mUseOverlay) {
Wu-cheng Li988fb622009-06-23 23:37:36 +0800635 // If preview display has been set, set overlay now.
636 if (mSurface != 0) {
637 ret = setOverlay();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800638 }
Wu-cheng Li988fb622009-06-23 23:37:36 +0800639 if (ret != NO_ERROR) return ret;
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500640 ret = mHardware->startPreview();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800641 } else {
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500642 mHardware->enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
643 ret = mHardware->startPreview();
Wu-cheng Li988fb622009-06-23 23:37:36 +0800644 if (ret != NO_ERROR) return ret;
645 // If preview display has been set, register preview buffers now.
646 if (mSurface != 0) {
647 // Unregister here because the surface registered with raw heap.
648 mSurface->unregisterBuffers();
649 ret = registerPreviewBuffers();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800650 }
651 }
652 return ret;
653}
654
655status_t CameraService::Client::startPreview()
656{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800657 LOGV("startPreview (pid %d)", getCallingPid());
Wu-cheng Lie6a550d2009-09-28 16:14:58 -0700658
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800659 return startCameraMode(CAMERA_PREVIEW_MODE);
660}
661
662status_t CameraService::Client::startRecording()
663{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800664 LOGV("startRecording (pid %d)", getCallingPid());
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800665
Jason Sams78b877e2009-03-24 20:21:36 -0700666 if (mMediaPlayerBeep.get() != NULL) {
Eric Laurent059b4132009-11-27 05:07:55 -0800667 // do not play record jingle if stream volume is 0
668 // (typically because ringer mode is silent).
669 int index;
670 AudioSystem::getStreamVolumeIndex(AudioSystem::ENFORCED_AUDIBLE, &index);
671 if (index != 0) {
672 mMediaPlayerBeep->seekTo(0);
673 mMediaPlayerBeep->start();
674 }
Jason Sams78b877e2009-03-24 20:21:36 -0700675 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500676
677 mHardware->enableMsgType(CAMERA_MSG_VIDEO_FRAME);
678
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800679 return startCameraMode(CAMERA_RECORDING_MODE);
680}
681
682// stop preview mode
683void CameraService::Client::stopPreview()
684{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800685 LOGV("stopPreview (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800686
Dave Sparksff0f38e2009-11-10 17:08:08 -0800687 // hold main lock during state transition
688 {
689 Mutex::Autolock lock(mLock);
690 if (checkPid() != NO_ERROR) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800691
Dave Sparksff0f38e2009-11-10 17:08:08 -0800692 if (mHardware == 0) {
693 LOGE("mHardware is NULL, returning.");
694 return;
695 }
696
697 mHardware->stopPreview();
698 mHardware->disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
Dave Sparkse7e93f92010-01-04 08:55:04 -0800699 LOGV("stopPreview(), hardware stopped OK");
Dave Sparksff0f38e2009-11-10 17:08:08 -0800700
701 if (mSurface != 0 && !mUseOverlay) {
702 mSurface->unregisterBuffers();
703 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800704 }
705
Dave Sparksff0f38e2009-11-10 17:08:08 -0800706 // hold preview buffer lock
707 {
708 Mutex::Autolock lock(mPreviewLock);
709 mPreviewBuffer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800710 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800711}
712
713// stop recording mode
714void CameraService::Client::stopRecording()
715{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800716 LOGV("stopRecording (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800717
Dave Sparksff0f38e2009-11-10 17:08:08 -0800718 // hold main lock during state transition
719 {
720 Mutex::Autolock lock(mLock);
721 if (checkPid() != NO_ERROR) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800722
Dave Sparksff0f38e2009-11-10 17:08:08 -0800723 if (mHardware == 0) {
724 LOGE("mHardware is NULL, returning.");
725 return;
726 }
727
728 if (mMediaPlayerBeep.get() != NULL) {
729 mMediaPlayerBeep->seekTo(0);
730 mMediaPlayerBeep->start();
731 }
732
733 mHardware->stopRecording();
734 mHardware->disableMsgType(CAMERA_MSG_VIDEO_FRAME);
Dave Sparkse7e93f92010-01-04 08:55:04 -0800735 LOGV("stopRecording(), hardware stopped OK");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800736 }
737
Dave Sparksff0f38e2009-11-10 17:08:08 -0800738 // hold preview buffer lock
739 {
740 Mutex::Autolock lock(mPreviewLock);
741 mPreviewBuffer.clear();
Jason Sams78b877e2009-03-24 20:21:36 -0700742 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800743}
744
745// release a recording frame
746void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem)
747{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800748 Mutex::Autolock lock(mLock);
749 if (checkPid() != NO_ERROR) return;
750
751 if (mHardware == 0) {
752 LOGE("mHardware is NULL, returning.");
753 return;
754 }
755
756 mHardware->releaseRecordingFrame(mem);
757}
758
759bool CameraService::Client::previewEnabled()
760{
761 Mutex::Autolock lock(mLock);
762 if (mHardware == 0) return false;
763 return mHardware->previewEnabled();
764}
765
766bool CameraService::Client::recordingEnabled()
767{
768 Mutex::Autolock lock(mLock);
769 if (mHardware == 0) return false;
770 return mHardware->recordingEnabled();
771}
772
773// Safely retrieves a strong pointer to the client during a hardware callback.
774sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user)
775{
776 sp<Client> client = 0;
777 CameraService *service = static_cast<CameraService*>(user);
778 if (service != NULL) {
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800779 Mutex::Autolock ourLock(service->mServiceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800780 if (service->mClient != 0) {
781 client = service->mClient.promote();
782 if (client == 0) {
783 LOGE("getClientFromCookie: client appears to have died");
784 service->mClient.clear();
785 }
786 } else {
787 LOGE("getClientFromCookie: got callback but client was NULL");
788 }
789 }
790 return client;
791}
792
793
794#if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE || \
795 DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE || \
796 DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
797static void dump_to_file(const char *fname,
798 uint8_t *buf, uint32_t size)
799{
800 int nw, cnt = 0;
801 uint32_t written = 0;
802
Joe Onorato51632e82010-01-07 21:48:32 -0500803 LOGV("opening file [%s]\n", fname);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800804 int fd = open(fname, O_RDWR | O_CREAT);
805 if (fd < 0) {
806 LOGE("failed to create file [%s]: %s", fname, strerror(errno));
807 return;
808 }
809
Joe Onorato51632e82010-01-07 21:48:32 -0500810 LOGV("writing %d bytes to file [%s]\n", size, fname);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800811 while (written < size) {
812 nw = ::write(fd,
813 buf + written,
814 size - written);
815 if (nw < 0) {
816 LOGE("failed to write to file [%s]: %s",
817 fname, strerror(errno));
818 break;
819 }
820 written += nw;
821 cnt++;
822 }
Joe Onorato51632e82010-01-07 21:48:32 -0500823 LOGV("done writing %d bytes to file [%s] in %d passes\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800824 size, fname, cnt);
825 ::close(fd);
826}
827#endif
828
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800829status_t CameraService::Client::autoFocus()
830{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800831 LOGV("autoFocus (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800832
833 Mutex::Autolock lock(mLock);
834 status_t result = checkPid();
835 if (result != NO_ERROR) return result;
836
837 if (mHardware == 0) {
838 LOGE("mHardware is NULL, returning.");
839 return INVALID_OPERATION;
840 }
841
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500842 return mHardware->autoFocus();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800843}
844
Chih-Chung Chang00900eb2009-09-15 14:51:56 +0800845status_t CameraService::Client::cancelAutoFocus()
846{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800847 LOGV("cancelAutoFocus (pid %d)", getCallingPid());
Chih-Chung Chang00900eb2009-09-15 14:51:56 +0800848
849 Mutex::Autolock lock(mLock);
850 status_t result = checkPid();
851 if (result != NO_ERROR) return result;
852
853 if (mHardware == 0) {
854 LOGE("mHardware is NULL, returning.");
855 return INVALID_OPERATION;
856 }
857
858 return mHardware->cancelAutoFocus();
859}
860
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800861// take a picture - image is returned in callback
862status_t CameraService::Client::takePicture()
863{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800864 LOGV("takePicture (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800865
866 Mutex::Autolock lock(mLock);
867 status_t result = checkPid();
868 if (result != NO_ERROR) return result;
869
870 if (mHardware == 0) {
871 LOGE("mHardware is NULL, returning.");
872 return INVALID_OPERATION;
873 }
874
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500875 mHardware->enableMsgType(CAMERA_MSG_SHUTTER |
876 CAMERA_MSG_POSTVIEW_FRAME |
877 CAMERA_MSG_RAW_IMAGE |
878 CAMERA_MSG_COMPRESSED_IMAGE);
879
880 return mHardware->takePicture();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800881}
882
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500883// snapshot taken
Wu-cheng Li986e0dc2009-10-23 17:39:46 +0800884void CameraService::Client::handleShutter(
885 image_rect_type *size // The width and height of yuv picture for
886 // registerBuffer. If this is NULL, use the picture
887 // size from parameters.
888)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800889{
Wu-cheng Liaab44b82009-03-24 20:39:09 -0700890 // Play shutter sound.
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500891 if (mMediaPlayerClick.get() != NULL) {
Eric Laurent059b4132009-11-27 05:07:55 -0800892 // do not play shutter sound if stream volume is 0
893 // (typically because ringer mode is silent).
894 int index;
895 AudioSystem::getStreamVolumeIndex(AudioSystem::ENFORCED_AUDIBLE, &index);
896 if (index != 0) {
897 mMediaPlayerClick->seekTo(0);
898 mMediaPlayerClick->start();
899 }
Wu-cheng Liaab44b82009-03-24 20:39:09 -0700900 }
901
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800902 // Screen goes black after the buffer is unregistered.
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500903 if (mSurface != 0 && !mUseOverlay) {
904 mSurface->unregisterBuffers();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800905 }
906
Dave Sparks393eb792009-10-15 10:02:22 -0700907 sp<ICameraClient> c = mCameraClient;
908 if (c != NULL) {
909 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
910 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500911 mHardware->disableMsgType(CAMERA_MSG_SHUTTER);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800912
913 // It takes some time before yuvPicture callback to be called.
914 // Register the buffer for raw image here to reduce latency.
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500915 if (mSurface != 0 && !mUseOverlay) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800916 int w, h;
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500917 CameraParameters params(mHardware->getParameters());
Wu-cheng Li986e0dc2009-10-23 17:39:46 +0800918 if (size == NULL) {
919 params.getPictureSize(&w, &h);
920 } else {
921 w = size->width;
922 h = size->height;
923 w &= ~1;
924 h &= ~1;
Dave Sparkse7e93f92010-01-04 08:55:04 -0800925 LOGV("Snapshot image width=%d, height=%d", w, h);
Wu-cheng Li986e0dc2009-10-23 17:39:46 +0800926 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800927 ISurface::BufferHeap buffers(w, h, w, h,
Chih-Chung Chang52e72002010-01-21 17:31:06 -0800928 PIXEL_FORMAT_YCbCr_420_SP, mOrientation, 0, mHardware->getRawHeap());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800929
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500930 mSurface->registerBuffers(buffers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800931 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800932}
933
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500934// preview callback - frame buffer update
935void CameraService::Client::handlePreviewData(const sp<IMemory>& mem)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800936{
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500937 ssize_t offset;
938 size_t size;
939 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
940
941#if DEBUG_HEAP_LEAKS && 0 // debugging
942 if (gWeakHeap == NULL) {
943 if (gWeakHeap != heap) {
Dave Sparkse7e93f92010-01-04 08:55:04 -0800944 LOGV("SETTING PREVIEW HEAP");
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500945 heap->trackMe(true, true);
946 gWeakHeap = heap;
947 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800948 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500949#endif
950#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
951 {
952 if (debug_frame_cnt++ == DEBUG_DUMP_PREVIEW_FRAME_TO_FILE) {
953 dump_to_file("/data/preview.yuv",
954 (uint8_t *)heap->base() + offset, size);
955 }
956 }
957#endif
958
959 if (!mUseOverlay)
960 {
961 Mutex::Autolock surfaceLock(mSurfaceLock);
962 if (mSurface != NULL) {
963 mSurface->postBuffer(offset);
964 }
965 }
966
Dave Sparks393eb792009-10-15 10:02:22 -0700967 // local copy of the callback flags
968 int flags = mPreviewCallbackFlag;
969
970 // is callback enabled?
971 if (!(flags & FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500972 // If the enable bit is off, the copy-out and one-shot bits are ignored
973 LOGV("frame callback is diabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800974 return;
975 }
976
Dave Sparks393eb792009-10-15 10:02:22 -0700977 // hold a strong pointer to the client
978 sp<ICameraClient> c = mCameraClient;
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500979
Dave Sparks393eb792009-10-15 10:02:22 -0700980 // clear callback flags if no client or one-shot mode
981 if ((c == NULL) || (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
982 LOGV("Disable preview callback");
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500983 mPreviewCallbackFlag &= ~(FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
984 FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
985 FRAME_CALLBACK_FLAG_ENABLE_MASK);
Dave Sparks393eb792009-10-15 10:02:22 -0700986 // TODO: Shouldn't we use this API for non-overlay hardware as well?
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500987 if (mUseOverlay)
988 mHardware->disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
989 }
Dave Sparks393eb792009-10-15 10:02:22 -0700990
991 // Is the received frame copied out or not?
992 if (flags & FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
993 LOGV("frame is copied");
994 copyFrameAndPostCopiedFrame(c, heap, offset, size);
995 } else {
996 LOGV("frame is forwarded");
997 c->dataCallback(CAMERA_MSG_PREVIEW_FRAME, mem);
998 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500999}
1000
1001// picture callback - postview image ready
1002void CameraService::Client::handlePostview(const sp<IMemory>& mem)
1003{
1004#if DEBUG_DUMP_POSTVIEW_SNAPSHOT_TO_FILE // for testing pursposes only
1005 {
1006 ssize_t offset;
1007 size_t size;
1008 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1009 dump_to_file("/data/postview.yuv",
1010 (uint8_t *)heap->base() + offset, size);
1011 }
1012#endif
1013
Dave Sparks393eb792009-10-15 10:02:22 -07001014 sp<ICameraClient> c = mCameraClient;
1015 if (c != NULL) {
1016 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem);
1017 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001018 mHardware->disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
1019}
1020
1021// picture callback - raw image ready
1022void CameraService::Client::handleRawPicture(const sp<IMemory>& mem)
1023{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001024 ssize_t offset;
1025 size_t size;
1026 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1027#if DEBUG_HEAP_LEAKS && 0 // debugging
1028 gWeakHeap = heap; // debugging
1029#endif
1030
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001031 //LOGV("handleRawPicture(%d, %d)", offset, size);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001032#if DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE // for testing pursposes only
1033 dump_to_file("/data/photo.yuv",
1034 (uint8_t *)heap->base() + offset, size);
1035#endif
1036
1037 // Put the YUV version of the snapshot in the preview display.
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001038 if (mSurface != 0 && !mUseOverlay) {
1039 mSurface->postBuffer(offset);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001040 }
1041
Dave Sparks393eb792009-10-15 10:02:22 -07001042 sp<ICameraClient> c = mCameraClient;
1043 if (c != NULL) {
1044 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem);
1045 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001046 mHardware->disableMsgType(CAMERA_MSG_RAW_IMAGE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001047}
1048
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001049// picture callback - compressed picture ready
1050void CameraService::Client::handleCompressedPicture(const sp<IMemory>& mem)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001051{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001052#if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE // for testing pursposes only
1053 {
1054 ssize_t offset;
1055 size_t size;
1056 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1057 dump_to_file("/data/photo.jpg",
1058 (uint8_t *)heap->base() + offset, size);
1059 }
1060#endif
1061
Dave Sparks393eb792009-10-15 10:02:22 -07001062 sp<ICameraClient> c = mCameraClient;
1063 if (c != NULL) {
1064 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem);
1065 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001066 mHardware->disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067}
1068
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001069void CameraService::Client::notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001070{
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001071 LOGV("notifyCallback(%d)", msgType);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072
1073 sp<Client> client = getClientFromCookie(user);
1074 if (client == 0) {
1075 return;
1076 }
1077
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001078 switch (msgType) {
1079 case CAMERA_MSG_SHUTTER:
Wu-cheng Li986e0dc2009-10-23 17:39:46 +08001080 // ext1 is the dimension of the yuv picture.
1081 client->handleShutter((image_rect_type *)ext1);
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001082 break;
1083 default:
Dave Sparks393eb792009-10-15 10:02:22 -07001084 sp<ICameraClient> c = client->mCameraClient;
1085 if (c != NULL) {
1086 c->notifyCallback(msgType, ext1, ext2);
1087 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001088 break;
1089 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001090
1091#if DEBUG_CLIENT_REFERENCES
1092 if (client->getStrongCount() == 1) {
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001093 LOGE("++++++++++++++++ (NOTIFY CALLBACK) THIS WILL CAUSE A LOCKUP!");
1094 client->printRefs();
1095 }
1096#endif
1097}
1098
1099void CameraService::Client::dataCallback(int32_t msgType, const sp<IMemory>& dataPtr, void* user)
1100{
1101 LOGV("dataCallback(%d)", msgType);
1102
1103 sp<Client> client = getClientFromCookie(user);
1104 if (client == 0) {
1105 return;
1106 }
1107
Dave Sparks393eb792009-10-15 10:02:22 -07001108 sp<ICameraClient> c = client->mCameraClient;
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001109 if (dataPtr == NULL) {
1110 LOGE("Null data returned in data callback");
Dave Sparks393eb792009-10-15 10:02:22 -07001111 if (c != NULL) {
1112 c->notifyCallback(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
1113 c->dataCallback(msgType, NULL);
1114 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001115 return;
1116 }
1117
1118 switch (msgType) {
1119 case CAMERA_MSG_PREVIEW_FRAME:
1120 client->handlePreviewData(dataPtr);
1121 break;
1122 case CAMERA_MSG_POSTVIEW_FRAME:
1123 client->handlePostview(dataPtr);
1124 break;
1125 case CAMERA_MSG_RAW_IMAGE:
1126 client->handleRawPicture(dataPtr);
1127 break;
1128 case CAMERA_MSG_COMPRESSED_IMAGE:
1129 client->handleCompressedPicture(dataPtr);
1130 break;
1131 default:
Dave Sparks393eb792009-10-15 10:02:22 -07001132 if (c != NULL) {
1133 c->dataCallback(msgType, dataPtr);
1134 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001135 break;
1136 }
1137
1138#if DEBUG_CLIENT_REFERENCES
1139 if (client->getStrongCount() == 1) {
1140 LOGE("++++++++++++++++ (DATA CALLBACK) THIS WILL CAUSE A LOCKUP!");
1141 client->printRefs();
1142 }
1143#endif
1144}
1145
1146void CameraService::Client::dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType,
1147 const sp<IMemory>& dataPtr, void* user)
1148{
1149 LOGV("dataCallbackTimestamp(%d)", msgType);
1150
1151 sp<Client> client = getClientFromCookie(user);
1152 if (client == 0) {
1153 return;
1154 }
Dave Sparks393eb792009-10-15 10:02:22 -07001155 sp<ICameraClient> c = client->mCameraClient;
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001156
1157 if (dataPtr == NULL) {
1158 LOGE("Null data returned in data with timestamp callback");
Dave Sparks393eb792009-10-15 10:02:22 -07001159 if (c != NULL) {
1160 c->notifyCallback(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
1161 c->dataCallbackTimestamp(0, msgType, NULL);
1162 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001163 return;
1164 }
1165
Dave Sparks393eb792009-10-15 10:02:22 -07001166 if (c != NULL) {
1167 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
1168 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001169
1170#if DEBUG_CLIENT_REFERENCES
1171 if (client->getStrongCount() == 1) {
1172 LOGE("++++++++++++++++ (DATA CALLBACK TIMESTAMP) THIS WILL CAUSE A LOCKUP!");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001173 client->printRefs();
1174 }
1175#endif
1176}
1177
1178// set preview/capture parameters - key/value pairs
1179status_t CameraService::Client::setParameters(const String8& params)
1180{
Joe Onorato51632e82010-01-07 21:48:32 -05001181 LOGV("setParameters(%s)", params.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001182
1183 Mutex::Autolock lock(mLock);
1184 status_t result = checkPid();
1185 if (result != NO_ERROR) return result;
1186
1187 if (mHardware == 0) {
1188 LOGE("mHardware is NULL, returning.");
1189 return INVALID_OPERATION;
1190 }
1191
1192 CameraParameters p(params);
Chih-Chung Chang52e72002010-01-21 17:31:06 -08001193
James Dong102f7772009-09-13 17:10:24 -07001194 return mHardware->setParameters(p);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001195}
1196
1197// get preview/capture parameters - key/value pairs
1198String8 CameraService::Client::getParameters() const
1199{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001200 Mutex::Autolock lock(mLock);
1201
1202 if (mHardware == 0) {
1203 LOGE("mHardware is NULL, returning.");
1204 return String8();
1205 }
1206
Wu-cheng Li81d763f2009-04-22 16:21:26 +08001207 String8 params(mHardware->getParameters().flatten());
Joe Onorato51632e82010-01-07 21:48:32 -05001208 LOGV("getParameters(%s)", params.string());
Wu-cheng Li81d763f2009-04-22 16:21:26 +08001209 return params;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001210}
1211
Wu-cheng Lie6a550d2009-09-28 16:14:58 -07001212status_t CameraService::Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
1213{
Dave Sparkse7e93f92010-01-04 08:55:04 -08001214 LOGV("sendCommand (pid %d)", getCallingPid());
Wu-cheng Lie6a550d2009-09-28 16:14:58 -07001215 Mutex::Autolock lock(mLock);
1216 status_t result = checkPid();
1217 if (result != NO_ERROR) return result;
1218
Chih-Chung Changf091e832010-01-22 17:49:48 -08001219 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
1220 // The orientation cannot be set during preview.
1221 if (mHardware->previewEnabled()) {
1222 return INVALID_OPERATION;
1223 }
1224 switch (arg1) {
1225 case 0:
1226 mOrientation = ISurface::BufferHeap::ROT_0;
1227 break;
1228 case 90:
1229 mOrientation = ISurface::BufferHeap::ROT_90;
1230 break;
1231 case 180:
1232 mOrientation = ISurface::BufferHeap::ROT_180;
1233 break;
1234 case 270:
1235 mOrientation = ISurface::BufferHeap::ROT_270;
1236 break;
1237 default:
1238 return BAD_VALUE;
1239 }
1240 return OK;
1241 }
1242
Wu-cheng Lie6a550d2009-09-28 16:14:58 -07001243 if (mHardware == 0) {
1244 LOGE("mHardware is NULL, returning.");
1245 return INVALID_OPERATION;
1246 }
1247
1248 return mHardware->sendCommand(cmd, arg1, arg2);
1249}
1250
Dave Sparks393eb792009-10-15 10:02:22 -07001251void CameraService::Client::copyFrameAndPostCopiedFrame(const sp<ICameraClient>& client,
1252 const sp<IMemoryHeap>& heap, size_t offset, size_t size)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001253{
1254 LOGV("copyFrameAndPostCopiedFrame");
1255 // It is necessary to copy out of pmem before sending this to
1256 // the callback. For efficiency, reuse the same MemoryHeapBase
1257 // provided it's big enough. Don't allocate the memory or
1258 // perform the copy if there's no callback.
Dave Sparks23c21ba2009-11-06 11:47:13 -08001259
Dave Sparksff0f38e2009-11-10 17:08:08 -08001260 // hold the preview lock while we grab a reference to the preview buffer
Dave Sparks23c21ba2009-11-06 11:47:13 -08001261 sp<MemoryHeapBase> previewBuffer;
1262 {
Dave Sparksff0f38e2009-11-10 17:08:08 -08001263 Mutex::Autolock lock(mPreviewLock);
Dave Sparks23c21ba2009-11-06 11:47:13 -08001264 if (mPreviewBuffer == 0) {
1265 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1266 } else if (size > mPreviewBuffer->virtualSize()) {
1267 mPreviewBuffer.clear();
1268 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1269 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001270 if (mPreviewBuffer == 0) {
1271 LOGE("failed to allocate space for preview buffer");
1272 return;
1273 }
Dave Sparks23c21ba2009-11-06 11:47:13 -08001274 previewBuffer = mPreviewBuffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001275 }
Dave Sparks23c21ba2009-11-06 11:47:13 -08001276 memcpy(previewBuffer->base(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001277 (uint8_t *)heap->base() + offset, size);
1278
Dave Sparks23c21ba2009-11-06 11:47:13 -08001279 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001280 if (frame == 0) {
1281 LOGE("failed to allocate space for frame callback");
1282 return;
1283 }
Dave Sparks393eb792009-10-15 10:02:22 -07001284 client->dataCallback(CAMERA_MSG_PREVIEW_FRAME, frame);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001285}
1286
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001287status_t CameraService::dump(int fd, const Vector<String16>& args)
1288{
1289 const size_t SIZE = 256;
1290 char buffer[SIZE];
1291 String8 result;
1292 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
1293 snprintf(buffer, SIZE, "Permission Denial: "
1294 "can't dump CameraService from pid=%d, uid=%d\n",
Chih-Chung Changd98c5162009-06-22 16:03:41 +08001295 getCallingPid(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001296 IPCThreadState::self()->getCallingUid());
1297 result.append(buffer);
1298 write(fd, result.string(), result.size());
1299 } else {
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +08001300 AutoMutex lock(&mServiceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301 if (mClient != 0) {
1302 sp<Client> currentClient = mClient.promote();
1303 sprintf(buffer, "Client (%p) PID: %d\n",
1304 currentClient->getCameraClient()->asBinder().get(),
1305 currentClient->mClientPid);
1306 result.append(buffer);
1307 write(fd, result.string(), result.size());
1308 currentClient->mHardware->dump(fd, args);
1309 } else {
1310 result.append("No camera client yet.\n");
1311 write(fd, result.string(), result.size());
1312 }
1313 }
1314 return NO_ERROR;
1315}
1316
1317
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001318status_t CameraService::onTransact(
1319 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1320{
1321 // permission checks...
1322 switch (code) {
1323 case BnCameraService::CONNECT:
1324 IPCThreadState* ipc = IPCThreadState::self();
1325 const int pid = ipc->getCallingPid();
1326 const int self_pid = getpid();
1327 if (pid != self_pid) {
1328 // we're called from a different process, do the real check
1329 if (!checkCallingPermission(
1330 String16("android.permission.CAMERA")))
1331 {
1332 const int uid = ipc->getCallingUid();
1333 LOGE("Permission Denial: "
1334 "can't use the camera pid=%d, uid=%d", pid, uid);
1335 return PERMISSION_DENIED;
1336 }
1337 }
1338 break;
1339 }
1340
1341 status_t err = BnCameraService::onTransact(code, data, reply, flags);
1342
Dave Sparks998b3292009-05-20 20:02:59 -07001343#if DEBUG_HEAP_LEAKS
Joe Onorato51632e82010-01-07 21:48:32 -05001344 LOGV("+++ onTransact err %d code %d", err, code);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001345
1346 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
1347 // the 'service' command interrogates this binder for its name, and then supplies it
1348 // even for the debugging commands. that means we need to check for it here, using
1349 // ISurfaceComposer (since we delegated the INTERFACE_TRANSACTION handling to
1350 // BnSurfaceComposer before falling through to this code).
1351
Joe Onorato51632e82010-01-07 21:48:32 -05001352 LOGV("+++ onTransact code %d", code);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001353
1354 CHECK_INTERFACE(ICameraService, data, reply);
1355
1356 switch(code) {
1357 case 1000:
1358 {
1359 if (gWeakHeap != 0) {
1360 sp<IMemoryHeap> h = gWeakHeap.promote();
1361 IMemoryHeap *p = gWeakHeap.unsafe_get();
Joe Onorato51632e82010-01-07 21:48:32 -05001362 LOGV("CHECKING WEAK REFERENCE %p (%p)", h.get(), p);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001363 if (h != 0)
1364 h->printRefs();
1365 bool attempt_to_delete = data.readInt32() == 1;
1366 if (attempt_to_delete) {
1367 // NOT SAFE!
Joe Onorato51632e82010-01-07 21:48:32 -05001368 LOGV("DELETING WEAK REFERENCE %p (%p)", h.get(), p);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001369 if (p) delete p;
1370 }
1371 return NO_ERROR;
1372 }
1373 }
1374 break;
1375 default:
1376 break;
1377 }
1378 }
Dave Sparks998b3292009-05-20 20:02:59 -07001379#endif // DEBUG_HEAP_LEAKS
Dave Sparksfec880d2009-05-21 09:18:18 -07001380
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001381 return err;
1382}
1383
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001384}; // namespace android