blob: 118249e02fa23c14f289318e0d616674fec110b5 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2**
3** Copyright (C) 2008, The Android Open Source Project
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08004**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080018#define LOG_TAG "CameraService"
19#include <utils/Log.h>
20
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070021#include <binder/IServiceManager.h>
22#include <binder/IPCThreadState.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023#include <utils/String16.h>
24#include <utils/Errors.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070025#include <binder/MemoryBase.h>
26#include <binder/MemoryHeapBase.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080027#include <camera/ICameraService.h>
28#include <surfaceflinger/ISurface.h>
29#include <ui/Overlay.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
Mathias Agopian54ed4f62010-02-16 17:33:37 -080031#include <hardware/hardware.h>
32
Jason Sams78b877e2009-03-24 20:21:36 -070033#include <media/mediaplayer.h>
34#include <media/AudioSystem.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include "CameraService.h"
36
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +080037#include <cutils/atomic.h>
Eric Laurentcbcb00e2009-03-27 16:27:16 -070038
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039namespace android {
40
41extern "C" {
42#include <stdio.h>
43#include <sys/types.h>
44#include <sys/stat.h>
45#include <fcntl.h>
46#include <pthread.h>
Chih-Chung Changd98c5162009-06-22 16:03:41 +080047#include <signal.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048}
49
50// When you enable this, as well as DEBUG_REFS=1 and
51// DEBUG_REFS_ENABLED_BY_DEFAULT=0 in libutils/RefBase.cpp, this will track all
52// references to the CameraService::Client in order to catch the case where the
53// client is being destroyed while a callback from the CameraHardwareInterface
54// is outstanding. This is a serious bug because if we make another call into
55// CameraHardwreInterface that itself triggers a callback, we will deadlock.
56
57#define DEBUG_CLIENT_REFERENCES 0
58
59#define PICTURE_TIMEOUT seconds(5)
60
61#define DEBUG_DUMP_PREVIEW_FRAME_TO_FILE 0 /* n-th frame to write */
62#define DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE 0
63#define DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE 0
Benny Wong4c8fb0a2009-08-12 12:01:27 -050064#define DEBUG_DUMP_POSTVIEW_SNAPSHOT_TO_FILE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065
66#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
67static int debug_frame_cnt;
68#endif
69
Chih-Chung Changd98c5162009-06-22 16:03:41 +080070static int getCallingPid() {
71 return IPCThreadState::self()->getCallingPid();
72}
73
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074// ----------------------------------------------------------------------------
75
76void CameraService::instantiate() {
77 defaultServiceManager()->addService(
78 String16("media.camera"), new CameraService());
79}
80
81// ----------------------------------------------------------------------------
82
83CameraService::CameraService() :
84 BnCameraService()
85{
86 LOGI("CameraService started: pid=%d", getpid());
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +080087 mUsers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088}
89
90CameraService::~CameraService()
91{
92 if (mClient != 0) {
93 LOGE("mClient was still connected in destructor!");
94 }
95}
96
97sp<ICamera> CameraService::connect(const sp<ICameraClient>& cameraClient)
98{
Chih-Chung Changd98c5162009-06-22 16:03:41 +080099 int callingPid = getCallingPid();
Joe Onorato51632e82010-01-07 21:48:32 -0500100 LOGV("CameraService::connect E (pid %d, client %p)", callingPid,
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800101 cameraClient->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800103 Mutex::Autolock lock(mServiceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104 sp<Client> client;
105 if (mClient != 0) {
106 sp<Client> currentClient = mClient.promote();
107 if (currentClient != 0) {
108 sp<ICameraClient> currentCameraClient(currentClient->getCameraClient());
109 if (cameraClient->asBinder() == currentCameraClient->asBinder()) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800110 // This is the same client reconnecting...
Joe Onorato51632e82010-01-07 21:48:32 -0500111 LOGV("CameraService::connect X (pid %d, same client %p) is reconnecting...",
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800112 callingPid, cameraClient->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113 return currentClient;
114 } else {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800115 // It's another client... reject it
Joe Onorato51632e82010-01-07 21:48:32 -0500116 LOGV("CameraService::connect X (pid %d, new client %p) rejected. "
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800117 "(old pid %d, old client %p)",
118 callingPid, cameraClient->asBinder().get(),
119 currentClient->mClientPid, currentCameraClient->asBinder().get());
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800120 if (kill(currentClient->mClientPid, 0) == -1 && errno == ESRCH) {
Joe Onorato51632e82010-01-07 21:48:32 -0500121 LOGV("The old client is dead!");
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800122 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123 return client;
124 }
125 } else {
126 // can't promote, the previous client has died...
Joe Onorato51632e82010-01-07 21:48:32 -0500127 LOGV("New client (pid %d) connecting, old reference was dangling...",
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800128 callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129 mClient.clear();
130 }
131 }
132
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800133 if (mUsers > 0) {
Joe Onorato51632e82010-01-07 21:48:32 -0500134 LOGV("Still have client, rejected");
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800135 return client;
136 }
137
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138 // create a new Client object
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800139 client = new Client(this, cameraClient, callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140 mClient = client;
141#if DEBUG_CLIENT_REFERENCES
142 // Enable tracking for this object, and track increments and decrements of
143 // the refcount.
144 client->trackMe(true, true);
145#endif
Joe Onorato51632e82010-01-07 21:48:32 -0500146 LOGV("CameraService::connect X");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147 return client;
148}
149
150void CameraService::removeClient(const sp<ICameraClient>& cameraClient)
151{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800152 int callingPid = getCallingPid();
153
154 // Declare this outside the lock to make absolutely sure the
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155 // destructor won't be called with the lock held.
156 sp<Client> client;
157
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800158 Mutex::Autolock lock(mServiceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159
160 if (mClient == 0) {
161 // This happens when we have already disconnected.
Joe Onorato51632e82010-01-07 21:48:32 -0500162 LOGV("removeClient (pid %d): already disconnected", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163 return;
164 }
165
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800166 // Promote mClient. It can fail if we are called from this path:
167 // Client::~Client() -> disconnect() -> removeClient().
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168 client = mClient.promote();
169 if (client == 0) {
Joe Onorato51632e82010-01-07 21:48:32 -0500170 LOGV("removeClient (pid %d): no more strong reference", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171 mClient.clear();
172 return;
173 }
174
175 if (cameraClient->asBinder() != client->getCameraClient()->asBinder()) {
176 // ugh! that's not our client!!
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800177 LOGW("removeClient (pid %d): mClient doesn't match!", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178 } else {
179 // okay, good, forget about mClient
180 mClient.clear();
181 }
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800182
Joe Onorato51632e82010-01-07 21:48:32 -0500183 LOGV("removeClient (pid %d) done", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184}
185
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800186// The reason we need this count is a new CameraService::connect() request may
187// come in while the previous Client's destructor has not been run or is still
188// running. If the last strong reference of the previous Client is gone but
189// destructor has not been run, we should not allow the new Client to be created
190// because we need to wait for the previous Client to tear down the hardware
191// first.
192void CameraService::incUsers() {
193 android_atomic_inc(&mUsers);
194}
195
196void CameraService::decUsers() {
197 android_atomic_dec(&mUsers);
198}
199
Wu-cheng Lie6a550d2009-09-28 16:14:58 -0700200static sp<MediaPlayer> newMediaPlayer(const char *file)
Jason Sams78b877e2009-03-24 20:21:36 -0700201{
202 sp<MediaPlayer> mp = new MediaPlayer();
Andreas Huberd4461e72010-01-28 11:19:57 -0800203 if (mp->setDataSource(file, NULL /* headers */) == NO_ERROR) {
Eric Laurent9d91ad52009-07-17 12:17:14 -0700204 mp->setAudioStreamType(AudioSystem::ENFORCED_AUDIBLE);
Jason Sams78b877e2009-03-24 20:21:36 -0700205 mp->prepare();
206 } else {
207 mp.clear();
208 LOGE("Failed to load CameraService sounds.");
209 }
210 return mp;
211}
212
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213CameraService::Client::Client(const sp<CameraService>& cameraService,
214 const sp<ICameraClient>& cameraClient, pid_t clientPid)
215{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800216 int callingPid = getCallingPid();
Joe Onorato51632e82010-01-07 21:48:32 -0500217 LOGV("Client::Client E (pid %d)", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218 mCameraService = cameraService;
219 mCameraClient = cameraClient;
220 mClientPid = clientPid;
221 mHardware = openCameraHardware();
222 mUseOverlay = mHardware->useOverlay();
223
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500224 mHardware->setCallbacks(notifyCallback,
225 dataCallback,
226 dataCallbackTimestamp,
227 mCameraService.get());
228
229 // Enable zoom, error, and focus messages by default
230 mHardware->enableMsgType(CAMERA_MSG_ERROR |
231 CAMERA_MSG_ZOOM |
232 CAMERA_MSG_FOCUS);
233
Jason Sams78b877e2009-03-24 20:21:36 -0700234 mMediaPlayerClick = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
235 mMediaPlayerBeep = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
Benny Wong71f77152009-07-15 18:44:27 -0500236 mOverlayW = 0;
237 mOverlayH = 0;
Jason Sams78b877e2009-03-24 20:21:36 -0700238
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800239 // Callback is disabled by default
240 mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
Chih-Chung Chang52e72002010-01-21 17:31:06 -0800241 mOrientation = 0;
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800242 cameraService->incUsers();
Joe Onorato51632e82010-01-07 21:48:32 -0500243 LOGV("Client::Client X (pid %d)", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244}
245
246status_t CameraService::Client::checkPid()
247{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800248 int callingPid = getCallingPid();
249 if (mClientPid == callingPid) return NO_ERROR;
250 LOGW("Attempt to use locked camera (client %p) from different process "
251 " (old pid %d, new pid %d)",
252 getCameraClient()->asBinder().get(), mClientPid, callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253 return -EBUSY;
254}
255
256status_t CameraService::Client::lock()
257{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800258 int callingPid = getCallingPid();
Joe Onorato51632e82010-01-07 21:48:32 -0500259 LOGV("lock from pid %d (mClientPid %d)", callingPid, mClientPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260 Mutex::Autolock _l(mLock);
261 // lock camera to this client if the the camera is unlocked
262 if (mClientPid == 0) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800263 mClientPid = callingPid;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264 return NO_ERROR;
265 }
266 // returns NO_ERROR if the client already owns the camera, -EBUSY otherwise
267 return checkPid();
268}
269
270status_t CameraService::Client::unlock()
271{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800272 int callingPid = getCallingPid();
Joe Onorato51632e82010-01-07 21:48:32 -0500273 LOGV("unlock from pid %d (mClientPid %d)", callingPid, mClientPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274 Mutex::Autolock _l(mLock);
275 // allow anyone to use camera
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276 status_t result = checkPid();
James Dong0ae13e32009-04-20 19:35:28 -0700277 if (result == NO_ERROR) {
278 mClientPid = 0;
Joe Onorato51632e82010-01-07 21:48:32 -0500279 LOGV("clear mCameraClient (pid %d)", callingPid);
James Dong0ae13e32009-04-20 19:35:28 -0700280 // we need to remove the reference so that when app goes
281 // away, the reference count goes to 0.
282 mCameraClient.clear();
283 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284 return result;
285}
286
287status_t CameraService::Client::connect(const sp<ICameraClient>& client)
288{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800289 int callingPid = getCallingPid();
290
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800291 // connect a new process to the camera
Joe Onorato51632e82010-01-07 21:48:32 -0500292 LOGV("Client::connect E (pid %d, client %p)", callingPid, client->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293
294 // I hate this hack, but things get really ugly when the media recorder
295 // service is handing back the camera to the app. The ICameraClient
296 // destructor will be called during the same IPC, making it look like
297 // the remote client is trying to disconnect. This hack temporarily
298 // sets the mClientPid to an invalid pid to prevent the hardware from
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800299 // being torn down.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300 {
301
302 // hold a reference to the old client or we will deadlock if the client is
303 // in the same process and we hold the lock when we remove the reference
304 sp<ICameraClient> oldClient;
305 {
306 Mutex::Autolock _l(mLock);
Chih-Chung Chang34e5a152009-06-09 13:56:44 +0800307 if (mClientPid != 0 && checkPid() != NO_ERROR) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800308 LOGW("Tried to connect to locked camera (old pid %d, new pid %d)",
309 mClientPid, callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310 return -EBUSY;
311 }
312 oldClient = mCameraClient;
313
314 // did the client actually change?
Dave Sparks393eb792009-10-15 10:02:22 -0700315 if ((mCameraClient != NULL) && (client->asBinder() == mCameraClient->asBinder())) {
Joe Onorato51632e82010-01-07 21:48:32 -0500316 LOGV("Connect to the same client");
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800317 return NO_ERROR;
318 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319
320 mCameraClient = client;
321 mClientPid = -1;
322 mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
Joe Onorato51632e82010-01-07 21:48:32 -0500323 LOGV("Connect to the new client (pid %d, client %p)",
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800324 callingPid, mCameraClient->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325 }
326
327 }
328 // the old client destructor is called when oldClient goes out of scope
329 // now we set the new PID to lock the interface again
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800330 mClientPid = callingPid;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331
332 return NO_ERROR;
333}
334
335#if HAVE_ANDROID_OS
336static void *unregister_surface(void *arg)
337{
338 ISurface *surface = (ISurface *)arg;
339 surface->unregisterBuffers();
340 IPCThreadState::self()->flushCommands();
341 return NULL;
342}
343#endif
344
345CameraService::Client::~Client()
346{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800347 int callingPid = getCallingPid();
348
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349 // tear down client
Joe Onorato51632e82010-01-07 21:48:32 -0500350 LOGV("Client::~Client E (pid %d, client %p)",
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800351 callingPid, getCameraClient()->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800352 if (mSurface != 0 && !mUseOverlay) {
353#if HAVE_ANDROID_OS
354 pthread_t thr;
355 // We unregister the buffers in a different thread because binder does
356 // not let us make sychronous transactions in a binder destructor (that
357 // is, upon our reaching a refcount of zero.)
358 pthread_create(&thr, NULL,
359 unregister_surface,
360 mSurface.get());
361 pthread_join(thr, NULL);
362#else
363 mSurface->unregisterBuffers();
364#endif
365 }
366
Jason Sams9e431ac2009-03-24 20:36:57 -0700367 if (mMediaPlayerBeep.get() != NULL) {
368 mMediaPlayerBeep->disconnect();
369 mMediaPlayerBeep.clear();
370 }
371 if (mMediaPlayerClick.get() != NULL) {
372 mMediaPlayerClick->disconnect();
373 mMediaPlayerClick.clear();
374 }
Jason Sams78b877e2009-03-24 20:21:36 -0700375
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800376 // make sure we tear down the hardware
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800377 mClientPid = callingPid;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800378 disconnect();
Joe Onorato51632e82010-01-07 21:48:32 -0500379 LOGV("Client::~Client X (pid %d)", mClientPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800380}
381
382void CameraService::Client::disconnect()
383{
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800384 int callingPid = getCallingPid();
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800385
Joe Onorato51632e82010-01-07 21:48:32 -0500386 LOGV("Client::disconnect() E (pid %d client %p)",
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800387 callingPid, getCameraClient()->asBinder().get());
388
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800389 Mutex::Autolock lock(mLock);
390 if (mClientPid <= 0) {
Joe Onorato51632e82010-01-07 21:48:32 -0500391 LOGV("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800392 return;
393 }
394 if (checkPid() != NO_ERROR) {
Joe Onorato51632e82010-01-07 21:48:32 -0500395 LOGV("Different client - don't disconnect");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800396 return;
397 }
398
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800399 // Make sure disconnect() is done once and once only, whether it is called
400 // from the user directly, or called by the destructor.
401 if (mHardware == 0) return;
402
Joe Onorato51632e82010-01-07 21:48:32 -0500403 LOGV("hardware teardown");
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800404 // Before destroying mHardware, we must make sure it's in the
405 // idle state.
406 mHardware->stopPreview();
407 // Cancel all picture callbacks.
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500408 mHardware->disableMsgType(CAMERA_MSG_SHUTTER |
409 CAMERA_MSG_POSTVIEW_FRAME |
410 CAMERA_MSG_RAW_IMAGE |
411 CAMERA_MSG_COMPRESSED_IMAGE);
412 mHardware->cancelPicture();
413 // Turn off remaining messages.
414 mHardware->disableMsgType(CAMERA_MSG_ALL_MSGS);
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800415 // Release the hardware resources.
416 mHardware->release();
Benny Wong71f77152009-07-15 18:44:27 -0500417 // Release the held overlay resources.
418 if (mUseOverlay)
419 {
420 mOverlayRef = 0;
421 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800422 mHardware.clear();
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800423
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800424 mCameraService->removeClient(mCameraClient);
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800425 mCameraService->decUsers();
426
Joe Onorato51632e82010-01-07 21:48:32 -0500427 LOGV("Client::disconnect() X (pid %d)", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428}
429
430// pass the buffered ISurface to the camera service
431status_t CameraService::Client::setPreviewDisplay(const sp<ISurface>& surface)
432{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800433 LOGV("setPreviewDisplay(%p) (pid %d)",
Wu-cheng Li988fb622009-06-23 23:37:36 +0800434 ((surface == NULL) ? NULL : surface.get()), getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800435 Mutex::Autolock lock(mLock);
436 status_t result = checkPid();
437 if (result != NO_ERROR) return result;
Wu-cheng Li988fb622009-06-23 23:37:36 +0800438
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800439 Mutex::Autolock surfaceLock(mSurfaceLock);
Wu-cheng Li988fb622009-06-23 23:37:36 +0800440 result = NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800441 // asBinder() is safe on NULL (returns NULL)
442 if (surface->asBinder() != mSurface->asBinder()) {
Benny Wong71f77152009-07-15 18:44:27 -0500443 if (mSurface != 0) {
Dave Sparkse7e93f92010-01-04 08:55:04 -0800444 LOGV("clearing old preview surface %p", mSurface.get());
Benny Wong71f77152009-07-15 18:44:27 -0500445 if ( !mUseOverlay)
446 {
447 mSurface->unregisterBuffers();
448 }
449 else
450 {
451 // Force the destruction of any previous overlay
452 sp<Overlay> dummy;
453 mHardware->setOverlay( dummy );
454 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800455 }
456 mSurface = surface;
Benny Wong71f77152009-07-15 18:44:27 -0500457 mOverlayRef = 0;
Wu-cheng Li988fb622009-06-23 23:37:36 +0800458 // If preview has been already started, set overlay or register preview
459 // buffers now.
460 if (mHardware->previewEnabled()) {
461 if (mUseOverlay) {
462 result = setOverlay();
463 } else if (mSurface != 0) {
464 result = registerPreviewBuffers();
465 }
466 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800467 }
Wu-cheng Li988fb622009-06-23 23:37:36 +0800468 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800469}
470
471// set the preview callback flag to affect how the received frames from
472// preview are handled.
473void CameraService::Client::setPreviewCallbackFlag(int callback_flag)
474{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800475 LOGV("setPreviewCallbackFlag (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800476 Mutex::Autolock lock(mLock);
477 if (checkPid() != NO_ERROR) return;
478 mPreviewCallbackFlag = callback_flag;
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500479
480 if(mUseOverlay) {
481 if(mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ENABLE_MASK)
482 mHardware->enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
483 else
484 mHardware->disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
485 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800486}
487
Wu-cheng Li988fb622009-06-23 23:37:36 +0800488// start preview mode
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800489status_t CameraService::Client::startCameraMode(camera_mode mode)
490{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800491 int callingPid = getCallingPid();
492
Dave Sparkse7e93f92010-01-04 08:55:04 -0800493 LOGV("startCameraMode(%d) (pid %d)", mode, callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800494
495 /* we cannot call into mHardware with mLock held because
496 * mHardware has callbacks onto us which acquire this lock
497 */
498
499 Mutex::Autolock lock(mLock);
500 status_t result = checkPid();
501 if (result != NO_ERROR) return result;
502
503 if (mHardware == 0) {
504 LOGE("mHardware is NULL, returning.");
505 return INVALID_OPERATION;
506 }
507
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800508 switch(mode) {
509 case CAMERA_RECORDING_MODE:
Wu-cheng Li988fb622009-06-23 23:37:36 +0800510 if (mSurface == 0) {
511 LOGE("setPreviewDisplay must be called before startRecordingMode.");
512 return INVALID_OPERATION;
513 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800514 return startRecordingMode();
515
516 default: // CAMERA_PREVIEW_MODE
Wu-cheng Li988fb622009-06-23 23:37:36 +0800517 if (mSurface == 0) {
Dave Sparkse7e93f92010-01-04 08:55:04 -0800518 LOGV("mSurface is not set yet.");
Wu-cheng Li988fb622009-06-23 23:37:36 +0800519 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800520 return startPreviewMode();
521 }
522}
523
524status_t CameraService::Client::startRecordingMode()
525{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800526 LOGV("startRecordingMode (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800527
528 status_t ret = UNKNOWN_ERROR;
529
530 // if preview has not been started, start preview first
531 if (!mHardware->previewEnabled()) {
532 ret = startPreviewMode();
533 if (ret != NO_ERROR) {
534 return ret;
535 }
536 }
537
538 // if recording has been enabled, nothing needs to be done
539 if (mHardware->recordingEnabled()) {
540 return NO_ERROR;
541 }
542
543 // start recording mode
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500544 ret = mHardware->startRecording();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800545 if (ret != NO_ERROR) {
546 LOGE("mHardware->startRecording() failed with status %d", ret);
547 }
548 return ret;
549}
550
Wu-cheng Li988fb622009-06-23 23:37:36 +0800551status_t CameraService::Client::setOverlay()
552{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800553 LOGV("setOverlay");
Wu-cheng Li988fb622009-06-23 23:37:36 +0800554 int w, h;
555 CameraParameters params(mHardware->getParameters());
556 params.getPreviewSize(&w, &h);
557
Benny Wong71f77152009-07-15 18:44:27 -0500558 if ( w != mOverlayW || h != mOverlayH )
559 {
560 // Force the destruction of any previous overlay
561 sp<Overlay> dummy;
562 mHardware->setOverlay( dummy );
563 mOverlayRef = 0;
564 }
565
Wu-cheng Li988fb622009-06-23 23:37:36 +0800566 status_t ret = NO_ERROR;
567 if (mSurface != 0) {
Benny Wong71f77152009-07-15 18:44:27 -0500568 if (mOverlayRef.get() == NULL) {
Dave Sparks587f7832009-10-07 19:18:20 -0700569
570 // FIXME:
571 // Surfaceflinger may hold onto the previous overlay reference for some
572 // time after we try to destroy it. retry a few times. In the future, we
573 // should make the destroy call block, or possibly specify that we can
574 // wait in the createOverlay call if the previous overlay is in the
575 // process of being destroyed.
576 for (int retry = 0; retry < 50; ++retry) {
Chih-Chung Chang52e72002010-01-21 17:31:06 -0800577 mOverlayRef = mSurface->createOverlay(w, h, OVERLAY_FORMAT_DEFAULT,
578 mOrientation);
Dave Sparks587f7832009-10-07 19:18:20 -0700579 if (mOverlayRef != NULL) break;
Dave Sparkse7e93f92010-01-04 08:55:04 -0800580 LOGW("Overlay create failed - retrying");
Dave Sparks587f7832009-10-07 19:18:20 -0700581 usleep(20000);
582 }
Benny Wong71f77152009-07-15 18:44:27 -0500583 if ( mOverlayRef.get() == NULL )
584 {
585 LOGE("Overlay Creation Failed!");
586 return -EINVAL;
587 }
588 ret = mHardware->setOverlay(new Overlay(mOverlayRef));
589 }
Wu-cheng Li988fb622009-06-23 23:37:36 +0800590 } else {
591 ret = mHardware->setOverlay(NULL);
592 }
593 if (ret != NO_ERROR) {
594 LOGE("mHardware->setOverlay() failed with status %d\n", ret);
595 }
Benny Wong71f77152009-07-15 18:44:27 -0500596
597 mOverlayW = w;
598 mOverlayH = h;
599
Wu-cheng Li988fb622009-06-23 23:37:36 +0800600 return ret;
601}
602
603status_t CameraService::Client::registerPreviewBuffers()
604{
605 int w, h;
606 CameraParameters params(mHardware->getParameters());
607 params.getPreviewSize(&w, &h);
608
Mathias Agopian29d17422010-02-16 19:42:32 -0800609 // don't use a hardcoded format here
Wu-cheng Li988fb622009-06-23 23:37:36 +0800610 ISurface::BufferHeap buffers(w, h, w, h,
Mathias Agopian29d17422010-02-16 19:42:32 -0800611 HAL_PIXEL_FORMAT_YCrCb_420_SP,
Chih-Chung Chang52e72002010-01-21 17:31:06 -0800612 mOrientation,
Wu-cheng Li988fb622009-06-23 23:37:36 +0800613 0,
614 mHardware->getPreviewHeap());
615
616 status_t ret = mSurface->registerBuffers(buffers);
617 if (ret != NO_ERROR) {
618 LOGE("registerBuffers failed with status %d", ret);
619 }
620 return ret;
621}
622
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800623status_t CameraService::Client::startPreviewMode()
624{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800625 LOGV("startPreviewMode (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800626
627 // if preview has been enabled, nothing needs to be done
628 if (mHardware->previewEnabled()) {
629 return NO_ERROR;
630 }
631
632 // start preview mode
633#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
634 debug_frame_cnt = 0;
635#endif
Wu-cheng Li988fb622009-06-23 23:37:36 +0800636 status_t ret = NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800637
638 if (mUseOverlay) {
Wu-cheng Li988fb622009-06-23 23:37:36 +0800639 // If preview display has been set, set overlay now.
640 if (mSurface != 0) {
641 ret = setOverlay();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800642 }
Wu-cheng Li988fb622009-06-23 23:37:36 +0800643 if (ret != NO_ERROR) return ret;
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500644 ret = mHardware->startPreview();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800645 } else {
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500646 mHardware->enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
647 ret = mHardware->startPreview();
Wu-cheng Li988fb622009-06-23 23:37:36 +0800648 if (ret != NO_ERROR) return ret;
649 // If preview display has been set, register preview buffers now.
650 if (mSurface != 0) {
651 // Unregister here because the surface registered with raw heap.
652 mSurface->unregisterBuffers();
653 ret = registerPreviewBuffers();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800654 }
655 }
656 return ret;
657}
658
659status_t CameraService::Client::startPreview()
660{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800661 LOGV("startPreview (pid %d)", getCallingPid());
Wu-cheng Lie6a550d2009-09-28 16:14:58 -0700662
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800663 return startCameraMode(CAMERA_PREVIEW_MODE);
664}
665
666status_t CameraService::Client::startRecording()
667{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800668 LOGV("startRecording (pid %d)", getCallingPid());
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800669
Jason Sams78b877e2009-03-24 20:21:36 -0700670 if (mMediaPlayerBeep.get() != NULL) {
Eric Laurent059b4132009-11-27 05:07:55 -0800671 // do not play record jingle if stream volume is 0
672 // (typically because ringer mode is silent).
673 int index;
674 AudioSystem::getStreamVolumeIndex(AudioSystem::ENFORCED_AUDIBLE, &index);
675 if (index != 0) {
676 mMediaPlayerBeep->seekTo(0);
677 mMediaPlayerBeep->start();
678 }
Jason Sams78b877e2009-03-24 20:21:36 -0700679 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500680
681 mHardware->enableMsgType(CAMERA_MSG_VIDEO_FRAME);
682
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800683 return startCameraMode(CAMERA_RECORDING_MODE);
684}
685
686// stop preview mode
687void CameraService::Client::stopPreview()
688{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800689 LOGV("stopPreview (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800690
Dave Sparksff0f38e2009-11-10 17:08:08 -0800691 // hold main lock during state transition
692 {
693 Mutex::Autolock lock(mLock);
694 if (checkPid() != NO_ERROR) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800695
Dave Sparksff0f38e2009-11-10 17:08:08 -0800696 if (mHardware == 0) {
697 LOGE("mHardware is NULL, returning.");
698 return;
699 }
700
701 mHardware->stopPreview();
702 mHardware->disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
Dave Sparkse7e93f92010-01-04 08:55:04 -0800703 LOGV("stopPreview(), hardware stopped OK");
Dave Sparksff0f38e2009-11-10 17:08:08 -0800704
705 if (mSurface != 0 && !mUseOverlay) {
706 mSurface->unregisterBuffers();
707 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800708 }
709
Dave Sparksff0f38e2009-11-10 17:08:08 -0800710 // hold preview buffer lock
711 {
712 Mutex::Autolock lock(mPreviewLock);
713 mPreviewBuffer.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800714 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800715}
716
717// stop recording mode
718void CameraService::Client::stopRecording()
719{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800720 LOGV("stopRecording (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800721
Dave Sparksff0f38e2009-11-10 17:08:08 -0800722 // hold main lock during state transition
723 {
724 Mutex::Autolock lock(mLock);
725 if (checkPid() != NO_ERROR) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800726
Dave Sparksff0f38e2009-11-10 17:08:08 -0800727 if (mHardware == 0) {
728 LOGE("mHardware is NULL, returning.");
729 return;
730 }
731
732 if (mMediaPlayerBeep.get() != NULL) {
733 mMediaPlayerBeep->seekTo(0);
734 mMediaPlayerBeep->start();
735 }
736
737 mHardware->stopRecording();
738 mHardware->disableMsgType(CAMERA_MSG_VIDEO_FRAME);
Dave Sparkse7e93f92010-01-04 08:55:04 -0800739 LOGV("stopRecording(), hardware stopped OK");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800740 }
741
Dave Sparksff0f38e2009-11-10 17:08:08 -0800742 // hold preview buffer lock
743 {
744 Mutex::Autolock lock(mPreviewLock);
745 mPreviewBuffer.clear();
Jason Sams78b877e2009-03-24 20:21:36 -0700746 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800747}
748
749// release a recording frame
750void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem)
751{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800752 Mutex::Autolock lock(mLock);
753 if (checkPid() != NO_ERROR) return;
754
755 if (mHardware == 0) {
756 LOGE("mHardware is NULL, returning.");
757 return;
758 }
759
760 mHardware->releaseRecordingFrame(mem);
761}
762
763bool CameraService::Client::previewEnabled()
764{
765 Mutex::Autolock lock(mLock);
766 if (mHardware == 0) return false;
767 return mHardware->previewEnabled();
768}
769
770bool CameraService::Client::recordingEnabled()
771{
772 Mutex::Autolock lock(mLock);
773 if (mHardware == 0) return false;
774 return mHardware->recordingEnabled();
775}
776
777// Safely retrieves a strong pointer to the client during a hardware callback.
778sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user)
779{
780 sp<Client> client = 0;
781 CameraService *service = static_cast<CameraService*>(user);
782 if (service != NULL) {
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800783 Mutex::Autolock ourLock(service->mServiceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800784 if (service->mClient != 0) {
785 client = service->mClient.promote();
786 if (client == 0) {
787 LOGE("getClientFromCookie: client appears to have died");
788 service->mClient.clear();
789 }
790 } else {
791 LOGE("getClientFromCookie: got callback but client was NULL");
792 }
793 }
794 return client;
795}
796
797
798#if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE || \
799 DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE || \
800 DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
801static void dump_to_file(const char *fname,
802 uint8_t *buf, uint32_t size)
803{
804 int nw, cnt = 0;
805 uint32_t written = 0;
806
Joe Onorato51632e82010-01-07 21:48:32 -0500807 LOGV("opening file [%s]\n", fname);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800808 int fd = open(fname, O_RDWR | O_CREAT);
809 if (fd < 0) {
810 LOGE("failed to create file [%s]: %s", fname, strerror(errno));
811 return;
812 }
813
Joe Onorato51632e82010-01-07 21:48:32 -0500814 LOGV("writing %d bytes to file [%s]\n", size, fname);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800815 while (written < size) {
816 nw = ::write(fd,
817 buf + written,
818 size - written);
819 if (nw < 0) {
820 LOGE("failed to write to file [%s]: %s",
821 fname, strerror(errno));
822 break;
823 }
824 written += nw;
825 cnt++;
826 }
Joe Onorato51632e82010-01-07 21:48:32 -0500827 LOGV("done writing %d bytes to file [%s] in %d passes\n",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800828 size, fname, cnt);
829 ::close(fd);
830}
831#endif
832
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800833status_t CameraService::Client::autoFocus()
834{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800835 LOGV("autoFocus (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800836
837 Mutex::Autolock lock(mLock);
838 status_t result = checkPid();
839 if (result != NO_ERROR) return result;
840
841 if (mHardware == 0) {
842 LOGE("mHardware is NULL, returning.");
843 return INVALID_OPERATION;
844 }
845
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500846 return mHardware->autoFocus();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800847}
848
Chih-Chung Chang00900eb2009-09-15 14:51:56 +0800849status_t CameraService::Client::cancelAutoFocus()
850{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800851 LOGV("cancelAutoFocus (pid %d)", getCallingPid());
Chih-Chung Chang00900eb2009-09-15 14:51:56 +0800852
853 Mutex::Autolock lock(mLock);
854 status_t result = checkPid();
855 if (result != NO_ERROR) return result;
856
857 if (mHardware == 0) {
858 LOGE("mHardware is NULL, returning.");
859 return INVALID_OPERATION;
860 }
861
862 return mHardware->cancelAutoFocus();
863}
864
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800865// take a picture - image is returned in callback
866status_t CameraService::Client::takePicture()
867{
Dave Sparkse7e93f92010-01-04 08:55:04 -0800868 LOGV("takePicture (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800869
870 Mutex::Autolock lock(mLock);
871 status_t result = checkPid();
872 if (result != NO_ERROR) return result;
873
874 if (mHardware == 0) {
875 LOGE("mHardware is NULL, returning.");
876 return INVALID_OPERATION;
877 }
878
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500879 mHardware->enableMsgType(CAMERA_MSG_SHUTTER |
880 CAMERA_MSG_POSTVIEW_FRAME |
881 CAMERA_MSG_RAW_IMAGE |
882 CAMERA_MSG_COMPRESSED_IMAGE);
883
884 return mHardware->takePicture();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800885}
886
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500887// snapshot taken
Wu-cheng Li986e0dc2009-10-23 17:39:46 +0800888void CameraService::Client::handleShutter(
889 image_rect_type *size // The width and height of yuv picture for
890 // registerBuffer. If this is NULL, use the picture
891 // size from parameters.
892)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800893{
Wu-cheng Liaab44b82009-03-24 20:39:09 -0700894 // Play shutter sound.
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500895 if (mMediaPlayerClick.get() != NULL) {
Eric Laurent059b4132009-11-27 05:07:55 -0800896 // do not play shutter sound if stream volume is 0
897 // (typically because ringer mode is silent).
898 int index;
899 AudioSystem::getStreamVolumeIndex(AudioSystem::ENFORCED_AUDIBLE, &index);
900 if (index != 0) {
901 mMediaPlayerClick->seekTo(0);
902 mMediaPlayerClick->start();
903 }
Wu-cheng Liaab44b82009-03-24 20:39:09 -0700904 }
905
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800906 // Screen goes black after the buffer is unregistered.
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500907 if (mSurface != 0 && !mUseOverlay) {
908 mSurface->unregisterBuffers();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800909 }
910
Dave Sparks393eb792009-10-15 10:02:22 -0700911 sp<ICameraClient> c = mCameraClient;
912 if (c != NULL) {
913 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
914 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500915 mHardware->disableMsgType(CAMERA_MSG_SHUTTER);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800916
917 // It takes some time before yuvPicture callback to be called.
918 // Register the buffer for raw image here to reduce latency.
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500919 if (mSurface != 0 && !mUseOverlay) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800920 int w, h;
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500921 CameraParameters params(mHardware->getParameters());
Wu-cheng Li986e0dc2009-10-23 17:39:46 +0800922 if (size == NULL) {
923 params.getPictureSize(&w, &h);
924 } else {
925 w = size->width;
926 h = size->height;
927 w &= ~1;
928 h &= ~1;
Dave Sparkse7e93f92010-01-04 08:55:04 -0800929 LOGV("Snapshot image width=%d, height=%d", w, h);
Wu-cheng Li986e0dc2009-10-23 17:39:46 +0800930 }
Mathias Agopian29d17422010-02-16 19:42:32 -0800931 // FIXME: don't use hardcoded format constants here
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800932 ISurface::BufferHeap buffers(w, h, w, h,
Mathias Agopian29d17422010-02-16 19:42:32 -0800933 HAL_PIXEL_FORMAT_YCrCb_420_SP, mOrientation, 0,
Mathias Agopian54ed4f62010-02-16 17:33:37 -0800934 mHardware->getRawHeap());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800935
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500936 mSurface->registerBuffers(buffers);
Chih-Chung Changb6aef252010-07-01 21:06:45 +0800937 IPCThreadState::self()->flushCommands();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800938 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800939}
940
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500941// preview callback - frame buffer update
942void CameraService::Client::handlePreviewData(const sp<IMemory>& mem)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800943{
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500944 ssize_t offset;
945 size_t size;
946 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
947
948#if DEBUG_HEAP_LEAKS && 0 // debugging
949 if (gWeakHeap == NULL) {
950 if (gWeakHeap != heap) {
Dave Sparkse7e93f92010-01-04 08:55:04 -0800951 LOGV("SETTING PREVIEW HEAP");
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500952 heap->trackMe(true, true);
953 gWeakHeap = heap;
954 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800955 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500956#endif
957#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
958 {
959 if (debug_frame_cnt++ == DEBUG_DUMP_PREVIEW_FRAME_TO_FILE) {
960 dump_to_file("/data/preview.yuv",
961 (uint8_t *)heap->base() + offset, size);
962 }
963 }
964#endif
965
966 if (!mUseOverlay)
967 {
968 Mutex::Autolock surfaceLock(mSurfaceLock);
969 if (mSurface != NULL) {
970 mSurface->postBuffer(offset);
971 }
972 }
973
Dave Sparks393eb792009-10-15 10:02:22 -0700974 // local copy of the callback flags
975 int flags = mPreviewCallbackFlag;
976
977 // is callback enabled?
978 if (!(flags & FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500979 // If the enable bit is off, the copy-out and one-shot bits are ignored
980 LOGV("frame callback is diabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800981 return;
982 }
983
Dave Sparks393eb792009-10-15 10:02:22 -0700984 // hold a strong pointer to the client
985 sp<ICameraClient> c = mCameraClient;
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500986
Dave Sparks393eb792009-10-15 10:02:22 -0700987 // clear callback flags if no client or one-shot mode
988 if ((c == NULL) || (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
989 LOGV("Disable preview callback");
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500990 mPreviewCallbackFlag &= ~(FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
991 FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
992 FRAME_CALLBACK_FLAG_ENABLE_MASK);
Dave Sparks393eb792009-10-15 10:02:22 -0700993 // TODO: Shouldn't we use this API for non-overlay hardware as well?
Benny Wong4c8fb0a2009-08-12 12:01:27 -0500994 if (mUseOverlay)
995 mHardware->disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
996 }
Dave Sparks393eb792009-10-15 10:02:22 -0700997
998 // Is the received frame copied out or not?
999 if (flags & FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
1000 LOGV("frame is copied");
1001 copyFrameAndPostCopiedFrame(c, heap, offset, size);
1002 } else {
1003 LOGV("frame is forwarded");
1004 c->dataCallback(CAMERA_MSG_PREVIEW_FRAME, mem);
1005 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001006}
1007
1008// picture callback - postview image ready
1009void CameraService::Client::handlePostview(const sp<IMemory>& mem)
1010{
1011#if DEBUG_DUMP_POSTVIEW_SNAPSHOT_TO_FILE // for testing pursposes only
1012 {
1013 ssize_t offset;
1014 size_t size;
1015 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1016 dump_to_file("/data/postview.yuv",
1017 (uint8_t *)heap->base() + offset, size);
1018 }
1019#endif
1020
Dave Sparks393eb792009-10-15 10:02:22 -07001021 sp<ICameraClient> c = mCameraClient;
1022 if (c != NULL) {
1023 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem);
1024 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001025 mHardware->disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
1026}
1027
1028// picture callback - raw image ready
1029void CameraService::Client::handleRawPicture(const sp<IMemory>& mem)
1030{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001031 ssize_t offset;
1032 size_t size;
1033 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1034#if DEBUG_HEAP_LEAKS && 0 // debugging
1035 gWeakHeap = heap; // debugging
1036#endif
1037
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001038 //LOGV("handleRawPicture(%d, %d)", offset, size);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001039#if DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE // for testing pursposes only
1040 dump_to_file("/data/photo.yuv",
1041 (uint8_t *)heap->base() + offset, size);
1042#endif
1043
1044 // Put the YUV version of the snapshot in the preview display.
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001045 if (mSurface != 0 && !mUseOverlay) {
1046 mSurface->postBuffer(offset);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001047 }
1048
Dave Sparks393eb792009-10-15 10:02:22 -07001049 sp<ICameraClient> c = mCameraClient;
1050 if (c != NULL) {
1051 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem);
1052 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001053 mHardware->disableMsgType(CAMERA_MSG_RAW_IMAGE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001054}
1055
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001056// picture callback - compressed picture ready
1057void CameraService::Client::handleCompressedPicture(const sp<IMemory>& mem)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001058{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059#if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE // for testing pursposes only
1060 {
1061 ssize_t offset;
1062 size_t size;
1063 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1064 dump_to_file("/data/photo.jpg",
1065 (uint8_t *)heap->base() + offset, size);
1066 }
1067#endif
1068
Dave Sparks393eb792009-10-15 10:02:22 -07001069 sp<ICameraClient> c = mCameraClient;
1070 if (c != NULL) {
1071 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem);
1072 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001073 mHardware->disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001074}
1075
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001076void CameraService::Client::notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077{
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001078 LOGV("notifyCallback(%d)", msgType);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001079
1080 sp<Client> client = getClientFromCookie(user);
1081 if (client == 0) {
1082 return;
1083 }
1084
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001085 switch (msgType) {
1086 case CAMERA_MSG_SHUTTER:
Wu-cheng Li986e0dc2009-10-23 17:39:46 +08001087 // ext1 is the dimension of the yuv picture.
1088 client->handleShutter((image_rect_type *)ext1);
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001089 break;
1090 default:
Dave Sparks393eb792009-10-15 10:02:22 -07001091 sp<ICameraClient> c = client->mCameraClient;
1092 if (c != NULL) {
1093 c->notifyCallback(msgType, ext1, ext2);
1094 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001095 break;
1096 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001097
1098#if DEBUG_CLIENT_REFERENCES
1099 if (client->getStrongCount() == 1) {
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001100 LOGE("++++++++++++++++ (NOTIFY CALLBACK) THIS WILL CAUSE A LOCKUP!");
1101 client->printRefs();
1102 }
1103#endif
1104}
1105
1106void CameraService::Client::dataCallback(int32_t msgType, const sp<IMemory>& dataPtr, void* user)
1107{
1108 LOGV("dataCallback(%d)", msgType);
1109
1110 sp<Client> client = getClientFromCookie(user);
1111 if (client == 0) {
1112 return;
1113 }
1114
Dave Sparks393eb792009-10-15 10:02:22 -07001115 sp<ICameraClient> c = client->mCameraClient;
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001116 if (dataPtr == NULL) {
1117 LOGE("Null data returned in data callback");
Dave Sparks393eb792009-10-15 10:02:22 -07001118 if (c != NULL) {
1119 c->notifyCallback(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
1120 c->dataCallback(msgType, NULL);
1121 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001122 return;
1123 }
1124
1125 switch (msgType) {
1126 case CAMERA_MSG_PREVIEW_FRAME:
1127 client->handlePreviewData(dataPtr);
1128 break;
1129 case CAMERA_MSG_POSTVIEW_FRAME:
1130 client->handlePostview(dataPtr);
1131 break;
1132 case CAMERA_MSG_RAW_IMAGE:
1133 client->handleRawPicture(dataPtr);
1134 break;
1135 case CAMERA_MSG_COMPRESSED_IMAGE:
1136 client->handleCompressedPicture(dataPtr);
1137 break;
1138 default:
Dave Sparks393eb792009-10-15 10:02:22 -07001139 if (c != NULL) {
1140 c->dataCallback(msgType, dataPtr);
1141 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001142 break;
1143 }
1144
1145#if DEBUG_CLIENT_REFERENCES
1146 if (client->getStrongCount() == 1) {
1147 LOGE("++++++++++++++++ (DATA CALLBACK) THIS WILL CAUSE A LOCKUP!");
1148 client->printRefs();
1149 }
1150#endif
1151}
1152
1153void CameraService::Client::dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType,
1154 const sp<IMemory>& dataPtr, void* user)
1155{
1156 LOGV("dataCallbackTimestamp(%d)", msgType);
1157
1158 sp<Client> client = getClientFromCookie(user);
1159 if (client == 0) {
1160 return;
1161 }
Dave Sparks393eb792009-10-15 10:02:22 -07001162 sp<ICameraClient> c = client->mCameraClient;
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001163
1164 if (dataPtr == NULL) {
1165 LOGE("Null data returned in data with timestamp callback");
Dave Sparks393eb792009-10-15 10:02:22 -07001166 if (c != NULL) {
1167 c->notifyCallback(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
1168 c->dataCallbackTimestamp(0, msgType, NULL);
1169 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001170 return;
1171 }
1172
Dave Sparks393eb792009-10-15 10:02:22 -07001173 if (c != NULL) {
1174 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
1175 }
Benny Wong4c8fb0a2009-08-12 12:01:27 -05001176
1177#if DEBUG_CLIENT_REFERENCES
1178 if (client->getStrongCount() == 1) {
1179 LOGE("++++++++++++++++ (DATA CALLBACK TIMESTAMP) THIS WILL CAUSE A LOCKUP!");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001180 client->printRefs();
1181 }
1182#endif
1183}
1184
1185// set preview/capture parameters - key/value pairs
1186status_t CameraService::Client::setParameters(const String8& params)
1187{
Joe Onorato51632e82010-01-07 21:48:32 -05001188 LOGV("setParameters(%s)", params.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189
1190 Mutex::Autolock lock(mLock);
1191 status_t result = checkPid();
1192 if (result != NO_ERROR) return result;
1193
1194 if (mHardware == 0) {
1195 LOGE("mHardware is NULL, returning.");
1196 return INVALID_OPERATION;
1197 }
1198
1199 CameraParameters p(params);
Chih-Chung Chang52e72002010-01-21 17:31:06 -08001200
James Dong102f7772009-09-13 17:10:24 -07001201 return mHardware->setParameters(p);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001202}
1203
1204// get preview/capture parameters - key/value pairs
1205String8 CameraService::Client::getParameters() const
1206{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001207 Mutex::Autolock lock(mLock);
1208
1209 if (mHardware == 0) {
1210 LOGE("mHardware is NULL, returning.");
1211 return String8();
1212 }
1213
Wu-cheng Li81d763f2009-04-22 16:21:26 +08001214 String8 params(mHardware->getParameters().flatten());
Joe Onorato51632e82010-01-07 21:48:32 -05001215 LOGV("getParameters(%s)", params.string());
Wu-cheng Li81d763f2009-04-22 16:21:26 +08001216 return params;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001217}
1218
Wu-cheng Lie6a550d2009-09-28 16:14:58 -07001219status_t CameraService::Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
1220{
Dave Sparkse7e93f92010-01-04 08:55:04 -08001221 LOGV("sendCommand (pid %d)", getCallingPid());
Wu-cheng Lie6a550d2009-09-28 16:14:58 -07001222 Mutex::Autolock lock(mLock);
1223 status_t result = checkPid();
1224 if (result != NO_ERROR) return result;
1225
Chih-Chung Changf091e832010-01-22 17:49:48 -08001226 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
1227 // The orientation cannot be set during preview.
1228 if (mHardware->previewEnabled()) {
1229 return INVALID_OPERATION;
1230 }
1231 switch (arg1) {
1232 case 0:
1233 mOrientation = ISurface::BufferHeap::ROT_0;
1234 break;
1235 case 90:
1236 mOrientation = ISurface::BufferHeap::ROT_90;
1237 break;
1238 case 180:
1239 mOrientation = ISurface::BufferHeap::ROT_180;
1240 break;
1241 case 270:
1242 mOrientation = ISurface::BufferHeap::ROT_270;
1243 break;
1244 default:
1245 return BAD_VALUE;
1246 }
1247 return OK;
1248 }
1249
Wu-cheng Lie6a550d2009-09-28 16:14:58 -07001250 if (mHardware == 0) {
1251 LOGE("mHardware is NULL, returning.");
1252 return INVALID_OPERATION;
1253 }
1254
1255 return mHardware->sendCommand(cmd, arg1, arg2);
1256}
1257
Dave Sparks393eb792009-10-15 10:02:22 -07001258void CameraService::Client::copyFrameAndPostCopiedFrame(const sp<ICameraClient>& client,
1259 const sp<IMemoryHeap>& heap, size_t offset, size_t size)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001260{
1261 LOGV("copyFrameAndPostCopiedFrame");
1262 // It is necessary to copy out of pmem before sending this to
1263 // the callback. For efficiency, reuse the same MemoryHeapBase
1264 // provided it's big enough. Don't allocate the memory or
1265 // perform the copy if there's no callback.
Dave Sparks23c21ba2009-11-06 11:47:13 -08001266
Dave Sparksff0f38e2009-11-10 17:08:08 -08001267 // hold the preview lock while we grab a reference to the preview buffer
Dave Sparks23c21ba2009-11-06 11:47:13 -08001268 sp<MemoryHeapBase> previewBuffer;
1269 {
Dave Sparksff0f38e2009-11-10 17:08:08 -08001270 Mutex::Autolock lock(mPreviewLock);
Dave Sparks23c21ba2009-11-06 11:47:13 -08001271 if (mPreviewBuffer == 0) {
1272 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1273 } else if (size > mPreviewBuffer->virtualSize()) {
1274 mPreviewBuffer.clear();
1275 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1276 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001277 if (mPreviewBuffer == 0) {
1278 LOGE("failed to allocate space for preview buffer");
1279 return;
1280 }
Dave Sparks23c21ba2009-11-06 11:47:13 -08001281 previewBuffer = mPreviewBuffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001282 }
Dave Sparks23c21ba2009-11-06 11:47:13 -08001283 memcpy(previewBuffer->base(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001284 (uint8_t *)heap->base() + offset, size);
1285
Dave Sparks23c21ba2009-11-06 11:47:13 -08001286 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001287 if (frame == 0) {
1288 LOGE("failed to allocate space for frame callback");
1289 return;
1290 }
Dave Sparks393eb792009-10-15 10:02:22 -07001291 client->dataCallback(CAMERA_MSG_PREVIEW_FRAME, frame);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001292}
1293
Chih-Chung Changf0252f22010-03-05 11:33:03 -08001294static const int kDumpLockRetries = 50;
1295static const int kDumpLockSleep = 60000;
1296
1297static bool tryLock(Mutex& mutex)
1298{
1299 bool locked = false;
1300 for (int i = 0; i < kDumpLockRetries; ++i) {
1301 if (mutex.tryLock() == NO_ERROR) {
1302 locked = true;
1303 break;
1304 }
1305 usleep(kDumpLockSleep);
1306 }
1307 return locked;
1308}
1309
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001310status_t CameraService::dump(int fd, const Vector<String16>& args)
1311{
Chih-Chung Changf0252f22010-03-05 11:33:03 -08001312 static const char* kDeadlockedString = "CameraService may be deadlocked\n";
1313
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001314 const size_t SIZE = 256;
1315 char buffer[SIZE];
1316 String8 result;
1317 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
1318 snprintf(buffer, SIZE, "Permission Denial: "
1319 "can't dump CameraService from pid=%d, uid=%d\n",
Chih-Chung Changd98c5162009-06-22 16:03:41 +08001320 getCallingPid(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001321 IPCThreadState::self()->getCallingUid());
1322 result.append(buffer);
1323 write(fd, result.string(), result.size());
1324 } else {
Chih-Chung Changf0252f22010-03-05 11:33:03 -08001325 bool locked = tryLock(mServiceLock);
1326 // failed to lock - CameraService is probably deadlocked
1327 if (!locked) {
1328 String8 result(kDeadlockedString);
1329 write(fd, result.string(), result.size());
1330 }
1331
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001332 if (mClient != 0) {
1333 sp<Client> currentClient = mClient.promote();
1334 sprintf(buffer, "Client (%p) PID: %d\n",
1335 currentClient->getCameraClient()->asBinder().get(),
1336 currentClient->mClientPid);
1337 result.append(buffer);
1338 write(fd, result.string(), result.size());
1339 currentClient->mHardware->dump(fd, args);
1340 } else {
1341 result.append("No camera client yet.\n");
1342 write(fd, result.string(), result.size());
1343 }
Chih-Chung Changf0252f22010-03-05 11:33:03 -08001344
1345 if (locked) mServiceLock.unlock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001346 }
1347 return NO_ERROR;
1348}
1349
1350
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001351status_t CameraService::onTransact(
1352 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1353{
1354 // permission checks...
1355 switch (code) {
1356 case BnCameraService::CONNECT:
1357 IPCThreadState* ipc = IPCThreadState::self();
1358 const int pid = ipc->getCallingPid();
1359 const int self_pid = getpid();
1360 if (pid != self_pid) {
1361 // we're called from a different process, do the real check
1362 if (!checkCallingPermission(
1363 String16("android.permission.CAMERA")))
1364 {
1365 const int uid = ipc->getCallingUid();
1366 LOGE("Permission Denial: "
1367 "can't use the camera pid=%d, uid=%d", pid, uid);
1368 return PERMISSION_DENIED;
1369 }
1370 }
1371 break;
1372 }
1373
1374 status_t err = BnCameraService::onTransact(code, data, reply, flags);
1375
Dave Sparks998b3292009-05-20 20:02:59 -07001376#if DEBUG_HEAP_LEAKS
Joe Onorato51632e82010-01-07 21:48:32 -05001377 LOGV("+++ onTransact err %d code %d", err, code);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001378
1379 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
1380 // the 'service' command interrogates this binder for its name, and then supplies it
1381 // even for the debugging commands. that means we need to check for it here, using
1382 // ISurfaceComposer (since we delegated the INTERFACE_TRANSACTION handling to
1383 // BnSurfaceComposer before falling through to this code).
1384
Joe Onorato51632e82010-01-07 21:48:32 -05001385 LOGV("+++ onTransact code %d", code);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001386
1387 CHECK_INTERFACE(ICameraService, data, reply);
1388
1389 switch(code) {
1390 case 1000:
1391 {
1392 if (gWeakHeap != 0) {
1393 sp<IMemoryHeap> h = gWeakHeap.promote();
1394 IMemoryHeap *p = gWeakHeap.unsafe_get();
Joe Onorato51632e82010-01-07 21:48:32 -05001395 LOGV("CHECKING WEAK REFERENCE %p (%p)", h.get(), p);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001396 if (h != 0)
1397 h->printRefs();
1398 bool attempt_to_delete = data.readInt32() == 1;
1399 if (attempt_to_delete) {
1400 // NOT SAFE!
Joe Onorato51632e82010-01-07 21:48:32 -05001401 LOGV("DELETING WEAK REFERENCE %p (%p)", h.get(), p);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001402 if (p) delete p;
1403 }
1404 return NO_ERROR;
1405 }
1406 }
1407 break;
1408 default:
1409 break;
1410 }
1411 }
Dave Sparks998b3292009-05-20 20:02:59 -07001412#endif // DEBUG_HEAP_LEAKS
Dave Sparksfec880d2009-05-21 09:18:18 -07001413
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001414 return err;
1415}
1416
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001417}; // namespace android