blob: 26901829e932a6e2a914b9fddfd077ea75154328 [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-03-03 19:31:44 -080019#define LOG_TAG "CameraService"
20#include <utils/Log.h>
21
Mathias Agopian07952722009-05-19 19:08:10 -070022#include <binder/IServiceManager.h>
23#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024#include <utils/String16.h>
25#include <utils/Errors.h>
Mathias Agopian07952722009-05-19 19:08:10 -070026#include <binder/MemoryBase.h>
27#include <binder/MemoryHeapBase.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080028#include <camera/ICameraService.h>
29#include <surfaceflinger/ISurface.h>
30#include <ui/Overlay.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
Mathias Agopian8f2423e2010-02-16 17:33:37 -080032#include <hardware/hardware.h>
33
Jason Samsb18b6912009-03-24 20:21:36 -070034#include <media/mediaplayer.h>
35#include <media/AudioSystem.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036#include "CameraService.h"
37
Chih-Chung Chang6fcba312009-06-24 13:44:37 +080038#include <cutils/atomic.h>
Eric Laurenta7f1e5c2009-03-27 16:27:16 -070039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040namespace android {
41
42extern "C" {
43#include <stdio.h>
44#include <sys/types.h>
45#include <sys/stat.h>
46#include <fcntl.h>
47#include <pthread.h>
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +080048#include <signal.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049}
50
51// When you enable this, as well as DEBUG_REFS=1 and
52// DEBUG_REFS_ENABLED_BY_DEFAULT=0 in libutils/RefBase.cpp, this will track all
53// references to the CameraService::Client in order to catch the case where the
54// client is being destroyed while a callback from the CameraHardwareInterface
55// is outstanding. This is a serious bug because if we make another call into
56// CameraHardwreInterface that itself triggers a callback, we will deadlock.
57
58#define DEBUG_CLIENT_REFERENCES 0
59
60#define PICTURE_TIMEOUT seconds(5)
61
62#define DEBUG_DUMP_PREVIEW_FRAME_TO_FILE 0 /* n-th frame to write */
63#define DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE 0
64#define DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE 0
Benny Wongda83f462009-08-12 12:01:27 -050065#define DEBUG_DUMP_POSTVIEW_SNAPSHOT_TO_FILE 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066
67#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
68static int debug_frame_cnt;
69#endif
70
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +080071static int getCallingPid() {
72 return IPCThreadState::self()->getCallingPid();
73}
74
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075// ----------------------------------------------------------------------------
76
77void CameraService::instantiate() {
78 defaultServiceManager()->addService(
79 String16("media.camera"), new CameraService());
80}
81
82// ----------------------------------------------------------------------------
83
84CameraService::CameraService() :
85 BnCameraService()
86{
87 LOGI("CameraService started: pid=%d", getpid());
Chih-Chung Chang6fcba312009-06-24 13:44:37 +080088 mUsers = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089}
90
91CameraService::~CameraService()
92{
93 if (mClient != 0) {
94 LOGE("mClient was still connected in destructor!");
95 }
96}
97
98sp<ICamera> CameraService::connect(const sp<ICameraClient>& cameraClient)
99{
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800100 int callingPid = getCallingPid();
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500101 LOGV("CameraService::connect E (pid %d, client %p)", callingPid,
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800102 cameraClient->asBinder().get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103
Chih-Chung Changd5d1ebd2009-06-24 19:59:31 +0800104 Mutex::Autolock lock(mServiceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 sp<Client> client;
106 if (mClient != 0) {
107 sp<Client> currentClient = mClient.promote();
108 if (currentClient != 0) {
109 sp<ICameraClient> currentCameraClient(currentClient->getCameraClient());
110 if (cameraClient->asBinder() == currentCameraClient->asBinder()) {
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800111 // This is the same client reconnecting...
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500112 LOGV("CameraService::connect X (pid %d, same client %p) is reconnecting...",
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800113 callingPid, cameraClient->asBinder().get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 return currentClient;
115 } else {
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800116 // It's another client... reject it
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500117 LOGV("CameraService::connect X (pid %d, new client %p) rejected. "
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800118 "(old pid %d, old client %p)",
119 callingPid, cameraClient->asBinder().get(),
120 currentClient->mClientPid, currentCameraClient->asBinder().get());
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800121 if (kill(currentClient->mClientPid, 0) == -1 && errno == ESRCH) {
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500122 LOGV("The old client is dead!");
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800123 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 return client;
125 }
126 } else {
127 // can't promote, the previous client has died...
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500128 LOGV("New client (pid %d) connecting, old reference was dangling...",
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800129 callingPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 mClient.clear();
131 }
132 }
133
Chih-Chung Changd5d1ebd2009-06-24 19:59:31 +0800134 if (mUsers > 0) {
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500135 LOGV("Still have client, rejected");
Chih-Chung Changd5d1ebd2009-06-24 19:59:31 +0800136 return client;
137 }
138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 // create a new Client object
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800140 client = new Client(this, cameraClient, callingPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 mClient = client;
142#if DEBUG_CLIENT_REFERENCES
143 // Enable tracking for this object, and track increments and decrements of
144 // the refcount.
145 client->trackMe(true, true);
146#endif
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500147 LOGV("CameraService::connect X");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 return client;
149}
150
151void CameraService::removeClient(const sp<ICameraClient>& cameraClient)
152{
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800153 int callingPid = getCallingPid();
154
155 // Declare this outside the lock to make absolutely sure the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 // destructor won't be called with the lock held.
157 sp<Client> client;
158
Chih-Chung Changd5d1ebd2009-06-24 19:59:31 +0800159 Mutex::Autolock lock(mServiceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160
161 if (mClient == 0) {
162 // This happens when we have already disconnected.
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500163 LOGV("removeClient (pid %d): already disconnected", callingPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 return;
165 }
166
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800167 // Promote mClient. It can fail if we are called from this path:
168 // Client::~Client() -> disconnect() -> removeClient().
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 client = mClient.promote();
170 if (client == 0) {
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500171 LOGV("removeClient (pid %d): no more strong reference", callingPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 mClient.clear();
173 return;
174 }
175
176 if (cameraClient->asBinder() != client->getCameraClient()->asBinder()) {
177 // ugh! that's not our client!!
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800178 LOGW("removeClient (pid %d): mClient doesn't match!", callingPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 } else {
180 // okay, good, forget about mClient
181 mClient.clear();
182 }
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800183
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500184 LOGV("removeClient (pid %d) done", callingPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185}
186
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800187// The reason we need this count is a new CameraService::connect() request may
188// come in while the previous Client's destructor has not been run or is still
189// running. If the last strong reference of the previous Client is gone but
190// destructor has not been run, we should not allow the new Client to be created
191// because we need to wait for the previous Client to tear down the hardware
192// first.
193void CameraService::incUsers() {
194 android_atomic_inc(&mUsers);
195}
196
197void CameraService::decUsers() {
198 android_atomic_dec(&mUsers);
199}
200
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700201static sp<MediaPlayer> newMediaPlayer(const char *file)
Jason Samsb18b6912009-03-24 20:21:36 -0700202{
203 sp<MediaPlayer> mp = new MediaPlayer();
Andreas Huber25643002010-01-28 11:19:57 -0800204 if (mp->setDataSource(file, NULL /* headers */) == NO_ERROR) {
Eric Laurenta553c252009-07-17 12:17:14 -0700205 mp->setAudioStreamType(AudioSystem::ENFORCED_AUDIBLE);
Jason Samsb18b6912009-03-24 20:21:36 -0700206 mp->prepare();
207 } else {
208 mp.clear();
209 LOGE("Failed to load CameraService sounds.");
210 }
211 return mp;
212}
213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214CameraService::Client::Client(const sp<CameraService>& cameraService,
215 const sp<ICameraClient>& cameraClient, pid_t clientPid)
216{
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800217 int callingPid = getCallingPid();
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500218 LOGV("Client::Client E (pid %d)", callingPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 mCameraService = cameraService;
220 mCameraClient = cameraClient;
221 mClientPid = clientPid;
222 mHardware = openCameraHardware();
223 mUseOverlay = mHardware->useOverlay();
224
Benny Wongda83f462009-08-12 12:01:27 -0500225 mHardware->setCallbacks(notifyCallback,
226 dataCallback,
227 dataCallbackTimestamp,
228 mCameraService.get());
229
230 // Enable zoom, error, and focus messages by default
231 mHardware->enableMsgType(CAMERA_MSG_ERROR |
232 CAMERA_MSG_ZOOM |
233 CAMERA_MSG_FOCUS);
234
Jason Samsb18b6912009-03-24 20:21:36 -0700235 mMediaPlayerClick = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
236 mMediaPlayerBeep = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
Benny Wong6d2090e2009-07-15 18:44:27 -0500237 mOverlayW = 0;
238 mOverlayH = 0;
Jason Samsb18b6912009-03-24 20:21:36 -0700239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 // Callback is disabled by default
241 mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800242 mOrientation = 0;
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800243 cameraService->incUsers();
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500244 LOGV("Client::Client X (pid %d)", callingPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245}
246
247status_t CameraService::Client::checkPid()
248{
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800249 int callingPid = getCallingPid();
250 if (mClientPid == callingPid) return NO_ERROR;
251 LOGW("Attempt to use locked camera (client %p) from different process "
252 " (old pid %d, new pid %d)",
253 getCameraClient()->asBinder().get(), mClientPid, callingPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 return -EBUSY;
255}
256
257status_t CameraService::Client::lock()
258{
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800259 int callingPid = getCallingPid();
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500260 LOGV("lock from pid %d (mClientPid %d)", callingPid, mClientPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 Mutex::Autolock _l(mLock);
262 // lock camera to this client if the the camera is unlocked
263 if (mClientPid == 0) {
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800264 mClientPid = callingPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 return NO_ERROR;
266 }
267 // returns NO_ERROR if the client already owns the camera, -EBUSY otherwise
268 return checkPid();
269}
270
271status_t CameraService::Client::unlock()
272{
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800273 int callingPid = getCallingPid();
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500274 LOGV("unlock from pid %d (mClientPid %d)", callingPid, mClientPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 Mutex::Autolock _l(mLock);
276 // allow anyone to use camera
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 status_t result = checkPid();
James Donge251f322009-04-20 19:35:28 -0700278 if (result == NO_ERROR) {
279 mClientPid = 0;
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500280 LOGV("clear mCameraClient (pid %d)", callingPid);
James Donge251f322009-04-20 19:35:28 -0700281 // we need to remove the reference so that when app goes
282 // away, the reference count goes to 0.
283 mCameraClient.clear();
284 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 return result;
286}
287
288status_t CameraService::Client::connect(const sp<ICameraClient>& client)
289{
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800290 int callingPid = getCallingPid();
291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 // connect a new process to the camera
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500293 LOGV("Client::connect E (pid %d, client %p)", callingPid, client->asBinder().get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294
295 // I hate this hack, but things get really ugly when the media recorder
296 // service is handing back the camera to the app. The ICameraClient
297 // destructor will be called during the same IPC, making it look like
298 // the remote client is trying to disconnect. This hack temporarily
299 // sets the mClientPid to an invalid pid to prevent the hardware from
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800300 // being torn down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 {
302
303 // hold a reference to the old client or we will deadlock if the client is
304 // in the same process and we hold the lock when we remove the reference
305 sp<ICameraClient> oldClient;
306 {
307 Mutex::Autolock _l(mLock);
Chih-Chung Changc8b24082009-06-09 13:56:44 +0800308 if (mClientPid != 0 && checkPid() != NO_ERROR) {
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800309 LOGW("Tried to connect to locked camera (old pid %d, new pid %d)",
310 mClientPid, callingPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 return -EBUSY;
312 }
313 oldClient = mCameraClient;
314
315 // did the client actually change?
Dave Sparksdd158c92009-10-15 10:02:22 -0700316 if ((mCameraClient != NULL) && (client->asBinder() == mCameraClient->asBinder())) {
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500317 LOGV("Connect to the same client");
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800318 return NO_ERROR;
319 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320
321 mCameraClient = client;
322 mClientPid = -1;
323 mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500324 LOGV("Connect to the new client (pid %d, client %p)",
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800325 callingPid, mCameraClient->asBinder().get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 }
327
328 }
329 // the old client destructor is called when oldClient goes out of scope
330 // now we set the new PID to lock the interface again
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800331 mClientPid = callingPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332
333 return NO_ERROR;
334}
335
336#if HAVE_ANDROID_OS
337static void *unregister_surface(void *arg)
338{
339 ISurface *surface = (ISurface *)arg;
340 surface->unregisterBuffers();
341 IPCThreadState::self()->flushCommands();
342 return NULL;
343}
344#endif
345
346CameraService::Client::~Client()
347{
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800348 int callingPid = getCallingPid();
349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 // tear down client
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500351 LOGV("Client::~Client E (pid %d, client %p)",
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800352 callingPid, getCameraClient()->asBinder().get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 if (mSurface != 0 && !mUseOverlay) {
354#if HAVE_ANDROID_OS
355 pthread_t thr;
356 // We unregister the buffers in a different thread because binder does
357 // not let us make sychronous transactions in a binder destructor (that
358 // is, upon our reaching a refcount of zero.)
359 pthread_create(&thr, NULL,
360 unregister_surface,
361 mSurface.get());
362 pthread_join(thr, NULL);
363#else
364 mSurface->unregisterBuffers();
365#endif
366 }
367
Jason Sams403914a2009-03-24 20:36:57 -0700368 if (mMediaPlayerBeep.get() != NULL) {
369 mMediaPlayerBeep->disconnect();
370 mMediaPlayerBeep.clear();
371 }
372 if (mMediaPlayerClick.get() != NULL) {
373 mMediaPlayerClick->disconnect();
374 mMediaPlayerClick.clear();
375 }
Jason Samsb18b6912009-03-24 20:21:36 -0700376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 // make sure we tear down the hardware
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800378 mClientPid = callingPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 disconnect();
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500380 LOGV("Client::~Client X (pid %d)", mClientPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381}
382
383void CameraService::Client::disconnect()
384{
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800385 int callingPid = getCallingPid();
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800386
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500387 LOGV("Client::disconnect() E (pid %d client %p)",
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800388 callingPid, getCameraClient()->asBinder().get());
389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 Mutex::Autolock lock(mLock);
391 if (mClientPid <= 0) {
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500392 LOGV("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 return;
394 }
395 if (checkPid() != NO_ERROR) {
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500396 LOGV("Different client - don't disconnect");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 return;
398 }
399
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800400 // Make sure disconnect() is done once and once only, whether it is called
401 // from the user directly, or called by the destructor.
402 if (mHardware == 0) return;
403
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500404 LOGV("hardware teardown");
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800405 // Before destroying mHardware, we must make sure it's in the
406 // idle state.
407 mHardware->stopPreview();
408 // Cancel all picture callbacks.
Benny Wongda83f462009-08-12 12:01:27 -0500409 mHardware->disableMsgType(CAMERA_MSG_SHUTTER |
410 CAMERA_MSG_POSTVIEW_FRAME |
411 CAMERA_MSG_RAW_IMAGE |
412 CAMERA_MSG_COMPRESSED_IMAGE);
413 mHardware->cancelPicture();
414 // Turn off remaining messages.
415 mHardware->disableMsgType(CAMERA_MSG_ALL_MSGS);
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800416 // Release the hardware resources.
417 mHardware->release();
Benny Wong6d2090e2009-07-15 18:44:27 -0500418 // Release the held overlay resources.
419 if (mUseOverlay)
420 {
421 mOverlayRef = 0;
422 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 mHardware.clear();
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800424
Chih-Chung Changd5d1ebd2009-06-24 19:59:31 +0800425 mCameraService->removeClient(mCameraClient);
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800426 mCameraService->decUsers();
427
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500428 LOGV("Client::disconnect() X (pid %d)", callingPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429}
430
431// pass the buffered ISurface to the camera service
432status_t CameraService::Client::setPreviewDisplay(const sp<ISurface>& surface)
433{
Dave Sparksc95a1762010-01-04 08:55:04 -0800434 LOGV("setPreviewDisplay(%p) (pid %d)",
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800435 ((surface == NULL) ? NULL : surface.get()), getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 Mutex::Autolock lock(mLock);
437 status_t result = checkPid();
438 if (result != NO_ERROR) return result;
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 Mutex::Autolock surfaceLock(mSurfaceLock);
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800441 result = NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 // asBinder() is safe on NULL (returns NULL)
443 if (surface->asBinder() != mSurface->asBinder()) {
Benny Wong6d2090e2009-07-15 18:44:27 -0500444 if (mSurface != 0) {
Dave Sparksc95a1762010-01-04 08:55:04 -0800445 LOGV("clearing old preview surface %p", mSurface.get());
Benny Wong6d2090e2009-07-15 18:44:27 -0500446 if ( !mUseOverlay)
447 {
448 mSurface->unregisterBuffers();
449 }
450 else
451 {
452 // Force the destruction of any previous overlay
453 sp<Overlay> dummy;
454 mHardware->setOverlay( dummy );
455 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 }
457 mSurface = surface;
Benny Wong6d2090e2009-07-15 18:44:27 -0500458 mOverlayRef = 0;
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800459 // If preview has been already started, set overlay or register preview
460 // buffers now.
461 if (mHardware->previewEnabled()) {
462 if (mUseOverlay) {
463 result = setOverlay();
464 } else if (mSurface != 0) {
465 result = registerPreviewBuffers();
466 }
467 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 }
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800469 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470}
471
472// set the preview callback flag to affect how the received frames from
473// preview are handled.
474void CameraService::Client::setPreviewCallbackFlag(int callback_flag)
475{
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800476 LOGV("setPreviewCallbackFlag (pid %d)", getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 Mutex::Autolock lock(mLock);
478 if (checkPid() != NO_ERROR) return;
479 mPreviewCallbackFlag = callback_flag;
Benny Wongda83f462009-08-12 12:01:27 -0500480
481 if(mUseOverlay) {
482 if(mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ENABLE_MASK)
483 mHardware->enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
484 else
485 mHardware->disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487}
488
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800489// start preview mode
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490status_t CameraService::Client::startCameraMode(camera_mode mode)
491{
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800492 int callingPid = getCallingPid();
493
Dave Sparksc95a1762010-01-04 08:55:04 -0800494 LOGV("startCameraMode(%d) (pid %d)", mode, callingPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495
496 /* we cannot call into mHardware with mLock held because
497 * mHardware has callbacks onto us which acquire this lock
498 */
499
500 Mutex::Autolock lock(mLock);
501 status_t result = checkPid();
502 if (result != NO_ERROR) return result;
503
504 if (mHardware == 0) {
505 LOGE("mHardware is NULL, returning.");
506 return INVALID_OPERATION;
507 }
508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 switch(mode) {
510 case CAMERA_RECORDING_MODE:
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800511 if (mSurface == 0) {
512 LOGE("setPreviewDisplay must be called before startRecordingMode.");
513 return INVALID_OPERATION;
514 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 return startRecordingMode();
516
517 default: // CAMERA_PREVIEW_MODE
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800518 if (mSurface == 0) {
Dave Sparksc95a1762010-01-04 08:55:04 -0800519 LOGV("mSurface is not set yet.");
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 return startPreviewMode();
522 }
523}
524
525status_t CameraService::Client::startRecordingMode()
526{
Dave Sparksc95a1762010-01-04 08:55:04 -0800527 LOGV("startRecordingMode (pid %d)", getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528
529 status_t ret = UNKNOWN_ERROR;
530
531 // if preview has not been started, start preview first
532 if (!mHardware->previewEnabled()) {
533 ret = startPreviewMode();
534 if (ret != NO_ERROR) {
535 return ret;
536 }
537 }
538
539 // if recording has been enabled, nothing needs to be done
540 if (mHardware->recordingEnabled()) {
541 return NO_ERROR;
542 }
543
544 // start recording mode
Benny Wongda83f462009-08-12 12:01:27 -0500545 ret = mHardware->startRecording();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 if (ret != NO_ERROR) {
547 LOGE("mHardware->startRecording() failed with status %d", ret);
548 }
549 return ret;
550}
551
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800552status_t CameraService::Client::setOverlay()
553{
Dave Sparksc95a1762010-01-04 08:55:04 -0800554 LOGV("setOverlay");
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800555 int w, h;
556 CameraParameters params(mHardware->getParameters());
557 params.getPreviewSize(&w, &h);
558
Benny Wong6d2090e2009-07-15 18:44:27 -0500559 if ( w != mOverlayW || h != mOverlayH )
560 {
561 // Force the destruction of any previous overlay
562 sp<Overlay> dummy;
563 mHardware->setOverlay( dummy );
564 mOverlayRef = 0;
565 }
566
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800567 status_t ret = NO_ERROR;
568 if (mSurface != 0) {
Benny Wong6d2090e2009-07-15 18:44:27 -0500569 if (mOverlayRef.get() == NULL) {
Dave Sparks21336402009-10-07 19:18:20 -0700570
571 // FIXME:
572 // Surfaceflinger may hold onto the previous overlay reference for some
573 // time after we try to destroy it. retry a few times. In the future, we
574 // should make the destroy call block, or possibly specify that we can
575 // wait in the createOverlay call if the previous overlay is in the
576 // process of being destroyed.
577 for (int retry = 0; retry < 50; ++retry) {
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800578 mOverlayRef = mSurface->createOverlay(w, h, OVERLAY_FORMAT_DEFAULT,
579 mOrientation);
Dave Sparks21336402009-10-07 19:18:20 -0700580 if (mOverlayRef != NULL) break;
Dave Sparksc95a1762010-01-04 08:55:04 -0800581 LOGW("Overlay create failed - retrying");
Dave Sparks21336402009-10-07 19:18:20 -0700582 usleep(20000);
583 }
Benny Wong6d2090e2009-07-15 18:44:27 -0500584 if ( mOverlayRef.get() == NULL )
585 {
586 LOGE("Overlay Creation Failed!");
587 return -EINVAL;
588 }
589 ret = mHardware->setOverlay(new Overlay(mOverlayRef));
590 }
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800591 } else {
592 ret = mHardware->setOverlay(NULL);
593 }
594 if (ret != NO_ERROR) {
595 LOGE("mHardware->setOverlay() failed with status %d\n", ret);
596 }
Benny Wong6d2090e2009-07-15 18:44:27 -0500597
598 mOverlayW = w;
599 mOverlayH = h;
600
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800601 return ret;
602}
603
604status_t CameraService::Client::registerPreviewBuffers()
605{
606 int w, h;
607 CameraParameters params(mHardware->getParameters());
608 params.getPreviewSize(&w, &h);
609
Mathias Agopian5a487122010-02-16 19:42:32 -0800610 // don't use a hardcoded format here
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800611 ISurface::BufferHeap buffers(w, h, w, h,
Mathias Agopian5a487122010-02-16 19:42:32 -0800612 HAL_PIXEL_FORMAT_YCrCb_420_SP,
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800613 mOrientation,
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800614 0,
615 mHardware->getPreviewHeap());
616
617 status_t ret = mSurface->registerBuffers(buffers);
618 if (ret != NO_ERROR) {
619 LOGE("registerBuffers failed with status %d", ret);
620 }
621 return ret;
622}
623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624status_t CameraService::Client::startPreviewMode()
625{
Dave Sparksc95a1762010-01-04 08:55:04 -0800626 LOGV("startPreviewMode (pid %d)", getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627
628 // if preview has been enabled, nothing needs to be done
629 if (mHardware->previewEnabled()) {
630 return NO_ERROR;
631 }
632
633 // start preview mode
634#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
635 debug_frame_cnt = 0;
636#endif
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800637 status_t ret = NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638
639 if (mUseOverlay) {
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800640 // If preview display has been set, set overlay now.
641 if (mSurface != 0) {
642 ret = setOverlay();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 }
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800644 if (ret != NO_ERROR) return ret;
Benny Wongda83f462009-08-12 12:01:27 -0500645 ret = mHardware->startPreview();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 } else {
Benny Wongda83f462009-08-12 12:01:27 -0500647 mHardware->enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
648 ret = mHardware->startPreview();
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800649 if (ret != NO_ERROR) return ret;
650 // If preview display has been set, register preview buffers now.
651 if (mSurface != 0) {
652 // Unregister here because the surface registered with raw heap.
653 mSurface->unregisterBuffers();
654 ret = registerPreviewBuffers();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 }
656 }
657 return ret;
658}
659
660status_t CameraService::Client::startPreview()
661{
Dave Sparksc95a1762010-01-04 08:55:04 -0800662 LOGV("startPreview (pid %d)", getCallingPid());
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 return startCameraMode(CAMERA_PREVIEW_MODE);
665}
666
667status_t CameraService::Client::startRecording()
668{
Dave Sparksc95a1762010-01-04 08:55:04 -0800669 LOGV("startRecording (pid %d)", getCallingPid());
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800670
Jason Samsb18b6912009-03-24 20:21:36 -0700671 if (mMediaPlayerBeep.get() != NULL) {
Eric Laurent524dc042009-11-27 05:07:55 -0800672 // do not play record jingle if stream volume is 0
673 // (typically because ringer mode is silent).
674 int index;
675 AudioSystem::getStreamVolumeIndex(AudioSystem::ENFORCED_AUDIBLE, &index);
676 if (index != 0) {
677 mMediaPlayerBeep->seekTo(0);
678 mMediaPlayerBeep->start();
679 }
Jason Samsb18b6912009-03-24 20:21:36 -0700680 }
Benny Wongda83f462009-08-12 12:01:27 -0500681
682 mHardware->enableMsgType(CAMERA_MSG_VIDEO_FRAME);
683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 return startCameraMode(CAMERA_RECORDING_MODE);
685}
686
687// stop preview mode
688void CameraService::Client::stopPreview()
689{
Dave Sparksc95a1762010-01-04 08:55:04 -0800690 LOGV("stopPreview (pid %d)", getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691
Dave Sparks05fd0df2009-11-10 17:08:08 -0800692 // hold main lock during state transition
693 {
694 Mutex::Autolock lock(mLock);
695 if (checkPid() != NO_ERROR) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696
Dave Sparks05fd0df2009-11-10 17:08:08 -0800697 if (mHardware == 0) {
698 LOGE("mHardware is NULL, returning.");
699 return;
700 }
701
702 mHardware->stopPreview();
703 mHardware->disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
Dave Sparksc95a1762010-01-04 08:55:04 -0800704 LOGV("stopPreview(), hardware stopped OK");
Dave Sparks05fd0df2009-11-10 17:08:08 -0800705
706 if (mSurface != 0 && !mUseOverlay) {
707 mSurface->unregisterBuffers();
708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 }
710
Dave Sparks05fd0df2009-11-10 17:08:08 -0800711 // hold preview buffer lock
712 {
713 Mutex::Autolock lock(mPreviewLock);
714 mPreviewBuffer.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716}
717
718// stop recording mode
719void CameraService::Client::stopRecording()
720{
Dave Sparksc95a1762010-01-04 08:55:04 -0800721 LOGV("stopRecording (pid %d)", getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722
Dave Sparks05fd0df2009-11-10 17:08:08 -0800723 // hold main lock during state transition
724 {
725 Mutex::Autolock lock(mLock);
726 if (checkPid() != NO_ERROR) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727
Dave Sparks05fd0df2009-11-10 17:08:08 -0800728 if (mHardware == 0) {
729 LOGE("mHardware is NULL, returning.");
730 return;
731 }
732
733 if (mMediaPlayerBeep.get() != NULL) {
734 mMediaPlayerBeep->seekTo(0);
735 mMediaPlayerBeep->start();
736 }
737
738 mHardware->stopRecording();
739 mHardware->disableMsgType(CAMERA_MSG_VIDEO_FRAME);
Dave Sparksc95a1762010-01-04 08:55:04 -0800740 LOGV("stopRecording(), hardware stopped OK");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 }
742
Dave Sparks05fd0df2009-11-10 17:08:08 -0800743 // hold preview buffer lock
744 {
745 Mutex::Autolock lock(mPreviewLock);
746 mPreviewBuffer.clear();
Jason Samsb18b6912009-03-24 20:21:36 -0700747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748}
749
750// release a recording frame
751void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem)
752{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 Mutex::Autolock lock(mLock);
754 if (checkPid() != NO_ERROR) return;
755
756 if (mHardware == 0) {
757 LOGE("mHardware is NULL, returning.");
758 return;
759 }
760
761 mHardware->releaseRecordingFrame(mem);
762}
763
764bool CameraService::Client::previewEnabled()
765{
766 Mutex::Autolock lock(mLock);
767 if (mHardware == 0) return false;
768 return mHardware->previewEnabled();
769}
770
771bool CameraService::Client::recordingEnabled()
772{
773 Mutex::Autolock lock(mLock);
774 if (mHardware == 0) return false;
775 return mHardware->recordingEnabled();
776}
777
778// Safely retrieves a strong pointer to the client during a hardware callback.
779sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user)
780{
781 sp<Client> client = 0;
782 CameraService *service = static_cast<CameraService*>(user);
783 if (service != NULL) {
Chih-Chung Changd5d1ebd2009-06-24 19:59:31 +0800784 Mutex::Autolock ourLock(service->mServiceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 if (service->mClient != 0) {
786 client = service->mClient.promote();
787 if (client == 0) {
788 LOGE("getClientFromCookie: client appears to have died");
789 service->mClient.clear();
790 }
791 } else {
792 LOGE("getClientFromCookie: got callback but client was NULL");
793 }
794 }
795 return client;
796}
797
798
799#if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE || \
800 DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE || \
801 DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
802static void dump_to_file(const char *fname,
803 uint8_t *buf, uint32_t size)
804{
805 int nw, cnt = 0;
806 uint32_t written = 0;
807
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500808 LOGV("opening file [%s]\n", fname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 int fd = open(fname, O_RDWR | O_CREAT);
810 if (fd < 0) {
811 LOGE("failed to create file [%s]: %s", fname, strerror(errno));
812 return;
813 }
814
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500815 LOGV("writing %d bytes to file [%s]\n", size, fname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 while (written < size) {
817 nw = ::write(fd,
818 buf + written,
819 size - written);
820 if (nw < 0) {
821 LOGE("failed to write to file [%s]: %s",
822 fname, strerror(errno));
823 break;
824 }
825 written += nw;
826 cnt++;
827 }
Joe Onoratof5d95cb2010-01-07 21:48:32 -0500828 LOGV("done writing %d bytes to file [%s] in %d passes\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 size, fname, cnt);
830 ::close(fd);
831}
832#endif
833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834status_t CameraService::Client::autoFocus()
835{
Dave Sparksc95a1762010-01-04 08:55:04 -0800836 LOGV("autoFocus (pid %d)", getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837
838 Mutex::Autolock lock(mLock);
839 status_t result = checkPid();
840 if (result != NO_ERROR) return result;
841
842 if (mHardware == 0) {
843 LOGE("mHardware is NULL, returning.");
844 return INVALID_OPERATION;
845 }
846
Benny Wongda83f462009-08-12 12:01:27 -0500847 return mHardware->autoFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848}
849
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800850status_t CameraService::Client::cancelAutoFocus()
851{
Dave Sparksc95a1762010-01-04 08:55:04 -0800852 LOGV("cancelAutoFocus (pid %d)", getCallingPid());
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800853
854 Mutex::Autolock lock(mLock);
855 status_t result = checkPid();
856 if (result != NO_ERROR) return result;
857
858 if (mHardware == 0) {
859 LOGE("mHardware is NULL, returning.");
860 return INVALID_OPERATION;
861 }
862
863 return mHardware->cancelAutoFocus();
864}
865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866// take a picture - image is returned in callback
867status_t CameraService::Client::takePicture()
868{
Dave Sparksc95a1762010-01-04 08:55:04 -0800869 LOGV("takePicture (pid %d)", getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870
871 Mutex::Autolock lock(mLock);
872 status_t result = checkPid();
873 if (result != NO_ERROR) return result;
874
875 if (mHardware == 0) {
876 LOGE("mHardware is NULL, returning.");
877 return INVALID_OPERATION;
878 }
879
Benny Wongda83f462009-08-12 12:01:27 -0500880 mHardware->enableMsgType(CAMERA_MSG_SHUTTER |
881 CAMERA_MSG_POSTVIEW_FRAME |
882 CAMERA_MSG_RAW_IMAGE |
883 CAMERA_MSG_COMPRESSED_IMAGE);
884
885 return mHardware->takePicture();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886}
887
Benny Wongda83f462009-08-12 12:01:27 -0500888// snapshot taken
Wu-cheng Li4cb04c42009-10-23 17:39:46 +0800889void CameraService::Client::handleShutter(
890 image_rect_type *size // The width and height of yuv picture for
891 // registerBuffer. If this is NULL, use the picture
892 // size from parameters.
893)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894{
Wu-cheng Lia9f95fb2009-03-24 20:39:09 -0700895 // Play shutter sound.
Benny Wongda83f462009-08-12 12:01:27 -0500896 if (mMediaPlayerClick.get() != NULL) {
Eric Laurent524dc042009-11-27 05:07:55 -0800897 // do not play shutter sound if stream volume is 0
898 // (typically because ringer mode is silent).
899 int index;
900 AudioSystem::getStreamVolumeIndex(AudioSystem::ENFORCED_AUDIBLE, &index);
901 if (index != 0) {
902 mMediaPlayerClick->seekTo(0);
903 mMediaPlayerClick->start();
904 }
Wu-cheng Lia9f95fb2009-03-24 20:39:09 -0700905 }
906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 // Screen goes black after the buffer is unregistered.
Benny Wongda83f462009-08-12 12:01:27 -0500908 if (mSurface != 0 && !mUseOverlay) {
909 mSurface->unregisterBuffers();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 }
911
Dave Sparksdd158c92009-10-15 10:02:22 -0700912 sp<ICameraClient> c = mCameraClient;
913 if (c != NULL) {
914 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
915 }
Benny Wongda83f462009-08-12 12:01:27 -0500916 mHardware->disableMsgType(CAMERA_MSG_SHUTTER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917
918 // It takes some time before yuvPicture callback to be called.
919 // Register the buffer for raw image here to reduce latency.
Benny Wongda83f462009-08-12 12:01:27 -0500920 if (mSurface != 0 && !mUseOverlay) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 int w, h;
Benny Wongda83f462009-08-12 12:01:27 -0500922 CameraParameters params(mHardware->getParameters());
Wu-cheng Li4cb04c42009-10-23 17:39:46 +0800923 if (size == NULL) {
924 params.getPictureSize(&w, &h);
925 } else {
926 w = size->width;
927 h = size->height;
928 w &= ~1;
929 h &= ~1;
Dave Sparksc95a1762010-01-04 08:55:04 -0800930 LOGV("Snapshot image width=%d, height=%d", w, h);
Wu-cheng Li4cb04c42009-10-23 17:39:46 +0800931 }
Mathias Agopian5a487122010-02-16 19:42:32 -0800932 // FIXME: don't use hardcoded format constants here
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 ISurface::BufferHeap buffers(w, h, w, h,
Mathias Agopian5a487122010-02-16 19:42:32 -0800934 HAL_PIXEL_FORMAT_YCrCb_420_SP, mOrientation, 0,
Mathias Agopian8f2423e2010-02-16 17:33:37 -0800935 mHardware->getRawHeap());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936
Benny Wongda83f462009-08-12 12:01:27 -0500937 mSurface->registerBuffers(buffers);
Chih-Chung Changfe1bac62010-07-01 21:06:45 +0800938 IPCThreadState::self()->flushCommands();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940}
941
Benny Wongda83f462009-08-12 12:01:27 -0500942// preview callback - frame buffer update
943void CameraService::Client::handlePreviewData(const sp<IMemory>& mem)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944{
Benny Wongda83f462009-08-12 12:01:27 -0500945 ssize_t offset;
946 size_t size;
947 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
948
949#if DEBUG_HEAP_LEAKS && 0 // debugging
950 if (gWeakHeap == NULL) {
951 if (gWeakHeap != heap) {
Dave Sparksc95a1762010-01-04 08:55:04 -0800952 LOGV("SETTING PREVIEW HEAP");
Benny Wongda83f462009-08-12 12:01:27 -0500953 heap->trackMe(true, true);
954 gWeakHeap = heap;
955 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 }
Benny Wongda83f462009-08-12 12:01:27 -0500957#endif
958#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
959 {
960 if (debug_frame_cnt++ == DEBUG_DUMP_PREVIEW_FRAME_TO_FILE) {
961 dump_to_file("/data/preview.yuv",
962 (uint8_t *)heap->base() + offset, size);
963 }
964 }
965#endif
966
967 if (!mUseOverlay)
968 {
969 Mutex::Autolock surfaceLock(mSurfaceLock);
970 if (mSurface != NULL) {
971 mSurface->postBuffer(offset);
972 }
973 }
974
Dave Sparksdd158c92009-10-15 10:02:22 -0700975 // local copy of the callback flags
976 int flags = mPreviewCallbackFlag;
977
978 // is callback enabled?
979 if (!(flags & FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
Benny Wongda83f462009-08-12 12:01:27 -0500980 // If the enable bit is off, the copy-out and one-shot bits are ignored
981 LOGV("frame callback is diabled");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 return;
983 }
984
Dave Sparksdd158c92009-10-15 10:02:22 -0700985 // hold a strong pointer to the client
986 sp<ICameraClient> c = mCameraClient;
Benny Wongda83f462009-08-12 12:01:27 -0500987
Dave Sparksdd158c92009-10-15 10:02:22 -0700988 // clear callback flags if no client or one-shot mode
989 if ((c == NULL) || (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
990 LOGV("Disable preview callback");
Benny Wongda83f462009-08-12 12:01:27 -0500991 mPreviewCallbackFlag &= ~(FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
992 FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
993 FRAME_CALLBACK_FLAG_ENABLE_MASK);
Dave Sparksdd158c92009-10-15 10:02:22 -0700994 // TODO: Shouldn't we use this API for non-overlay hardware as well?
Benny Wongda83f462009-08-12 12:01:27 -0500995 if (mUseOverlay)
996 mHardware->disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
997 }
Dave Sparksdd158c92009-10-15 10:02:22 -0700998
999 // Is the received frame copied out or not?
1000 if (flags & FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
1001 LOGV("frame is copied");
1002 copyFrameAndPostCopiedFrame(c, heap, offset, size);
1003 } else {
1004 LOGV("frame is forwarded");
1005 c->dataCallback(CAMERA_MSG_PREVIEW_FRAME, mem);
1006 }
Benny Wongda83f462009-08-12 12:01:27 -05001007}
1008
1009// picture callback - postview image ready
1010void CameraService::Client::handlePostview(const sp<IMemory>& mem)
1011{
1012#if DEBUG_DUMP_POSTVIEW_SNAPSHOT_TO_FILE // for testing pursposes only
1013 {
1014 ssize_t offset;
1015 size_t size;
1016 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1017 dump_to_file("/data/postview.yuv",
1018 (uint8_t *)heap->base() + offset, size);
1019 }
1020#endif
1021
Dave Sparksdd158c92009-10-15 10:02:22 -07001022 sp<ICameraClient> c = mCameraClient;
1023 if (c != NULL) {
1024 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem);
1025 }
Benny Wongda83f462009-08-12 12:01:27 -05001026 mHardware->disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
1027}
1028
1029// picture callback - raw image ready
1030void CameraService::Client::handleRawPicture(const sp<IMemory>& mem)
1031{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 ssize_t offset;
1033 size_t size;
1034 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1035#if DEBUG_HEAP_LEAKS && 0 // debugging
1036 gWeakHeap = heap; // debugging
1037#endif
1038
Benny Wongda83f462009-08-12 12:01:27 -05001039 //LOGV("handleRawPicture(%d, %d)", offset, size);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040#if DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE // for testing pursposes only
1041 dump_to_file("/data/photo.yuv",
1042 (uint8_t *)heap->base() + offset, size);
1043#endif
1044
1045 // Put the YUV version of the snapshot in the preview display.
Benny Wongda83f462009-08-12 12:01:27 -05001046 if (mSurface != 0 && !mUseOverlay) {
1047 mSurface->postBuffer(offset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 }
1049
Dave Sparksdd158c92009-10-15 10:02:22 -07001050 sp<ICameraClient> c = mCameraClient;
1051 if (c != NULL) {
1052 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem);
1053 }
Benny Wongda83f462009-08-12 12:01:27 -05001054 mHardware->disableMsgType(CAMERA_MSG_RAW_IMAGE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055}
1056
Benny Wongda83f462009-08-12 12:01:27 -05001057// picture callback - compressed picture ready
1058void CameraService::Client::handleCompressedPicture(const sp<IMemory>& mem)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060#if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE // for testing pursposes only
1061 {
1062 ssize_t offset;
1063 size_t size;
1064 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1065 dump_to_file("/data/photo.jpg",
1066 (uint8_t *)heap->base() + offset, size);
1067 }
1068#endif
1069
Dave Sparksdd158c92009-10-15 10:02:22 -07001070 sp<ICameraClient> c = mCameraClient;
1071 if (c != NULL) {
1072 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem);
1073 }
Benny Wongda83f462009-08-12 12:01:27 -05001074 mHardware->disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075}
1076
Benny Wongda83f462009-08-12 12:01:27 -05001077void CameraService::Client::notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078{
Benny Wongda83f462009-08-12 12:01:27 -05001079 LOGV("notifyCallback(%d)", msgType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080
1081 sp<Client> client = getClientFromCookie(user);
1082 if (client == 0) {
1083 return;
1084 }
1085
Benny Wongda83f462009-08-12 12:01:27 -05001086 switch (msgType) {
1087 case CAMERA_MSG_SHUTTER:
Wu-cheng Li4cb04c42009-10-23 17:39:46 +08001088 // ext1 is the dimension of the yuv picture.
1089 client->handleShutter((image_rect_type *)ext1);
Benny Wongda83f462009-08-12 12:01:27 -05001090 break;
1091 default:
Dave Sparksdd158c92009-10-15 10:02:22 -07001092 sp<ICameraClient> c = client->mCameraClient;
1093 if (c != NULL) {
1094 c->notifyCallback(msgType, ext1, ext2);
1095 }
Benny Wongda83f462009-08-12 12:01:27 -05001096 break;
1097 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098
1099#if DEBUG_CLIENT_REFERENCES
1100 if (client->getStrongCount() == 1) {
Benny Wongda83f462009-08-12 12:01:27 -05001101 LOGE("++++++++++++++++ (NOTIFY CALLBACK) THIS WILL CAUSE A LOCKUP!");
1102 client->printRefs();
1103 }
1104#endif
1105}
1106
1107void CameraService::Client::dataCallback(int32_t msgType, const sp<IMemory>& dataPtr, void* user)
1108{
1109 LOGV("dataCallback(%d)", msgType);
1110
1111 sp<Client> client = getClientFromCookie(user);
1112 if (client == 0) {
1113 return;
1114 }
1115
Dave Sparksdd158c92009-10-15 10:02:22 -07001116 sp<ICameraClient> c = client->mCameraClient;
Benny Wongda83f462009-08-12 12:01:27 -05001117 if (dataPtr == NULL) {
1118 LOGE("Null data returned in data callback");
Dave Sparksdd158c92009-10-15 10:02:22 -07001119 if (c != NULL) {
1120 c->notifyCallback(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
1121 c->dataCallback(msgType, NULL);
1122 }
Benny Wongda83f462009-08-12 12:01:27 -05001123 return;
1124 }
1125
1126 switch (msgType) {
1127 case CAMERA_MSG_PREVIEW_FRAME:
1128 client->handlePreviewData(dataPtr);
1129 break;
1130 case CAMERA_MSG_POSTVIEW_FRAME:
1131 client->handlePostview(dataPtr);
1132 break;
1133 case CAMERA_MSG_RAW_IMAGE:
1134 client->handleRawPicture(dataPtr);
1135 break;
1136 case CAMERA_MSG_COMPRESSED_IMAGE:
1137 client->handleCompressedPicture(dataPtr);
1138 break;
1139 default:
Dave Sparksdd158c92009-10-15 10:02:22 -07001140 if (c != NULL) {
1141 c->dataCallback(msgType, dataPtr);
1142 }
Benny Wongda83f462009-08-12 12:01:27 -05001143 break;
1144 }
1145
1146#if DEBUG_CLIENT_REFERENCES
1147 if (client->getStrongCount() == 1) {
1148 LOGE("++++++++++++++++ (DATA CALLBACK) THIS WILL CAUSE A LOCKUP!");
1149 client->printRefs();
1150 }
1151#endif
1152}
1153
1154void CameraService::Client::dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType,
1155 const sp<IMemory>& dataPtr, void* user)
1156{
1157 LOGV("dataCallbackTimestamp(%d)", msgType);
1158
1159 sp<Client> client = getClientFromCookie(user);
1160 if (client == 0) {
1161 return;
1162 }
Dave Sparksdd158c92009-10-15 10:02:22 -07001163 sp<ICameraClient> c = client->mCameraClient;
Benny Wongda83f462009-08-12 12:01:27 -05001164
1165 if (dataPtr == NULL) {
1166 LOGE("Null data returned in data with timestamp callback");
Dave Sparksdd158c92009-10-15 10:02:22 -07001167 if (c != NULL) {
1168 c->notifyCallback(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
1169 c->dataCallbackTimestamp(0, msgType, NULL);
1170 }
Benny Wongda83f462009-08-12 12:01:27 -05001171 return;
1172 }
1173
Dave Sparksdd158c92009-10-15 10:02:22 -07001174 if (c != NULL) {
1175 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
1176 }
Benny Wongda83f462009-08-12 12:01:27 -05001177
1178#if DEBUG_CLIENT_REFERENCES
1179 if (client->getStrongCount() == 1) {
1180 LOGE("++++++++++++++++ (DATA CALLBACK TIMESTAMP) THIS WILL CAUSE A LOCKUP!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 client->printRefs();
1182 }
1183#endif
1184}
1185
1186// set preview/capture parameters - key/value pairs
1187status_t CameraService::Client::setParameters(const String8& params)
1188{
Joe Onoratof5d95cb2010-01-07 21:48:32 -05001189 LOGV("setParameters(%s)", params.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190
1191 Mutex::Autolock lock(mLock);
1192 status_t result = checkPid();
1193 if (result != NO_ERROR) return result;
1194
1195 if (mHardware == 0) {
1196 LOGE("mHardware is NULL, returning.");
1197 return INVALID_OPERATION;
1198 }
1199
1200 CameraParameters p(params);
Chih-Chung Change1ceec22010-01-21 17:31:06 -08001201
James Dong6085b4e2009-09-13 17:10:24 -07001202 return mHardware->setParameters(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203}
1204
1205// get preview/capture parameters - key/value pairs
1206String8 CameraService::Client::getParameters() const
1207{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 Mutex::Autolock lock(mLock);
1209
1210 if (mHardware == 0) {
1211 LOGE("mHardware is NULL, returning.");
1212 return String8();
1213 }
1214
Wu-cheng Liab5b4242009-04-22 16:21:26 +08001215 String8 params(mHardware->getParameters().flatten());
Joe Onoratof5d95cb2010-01-07 21:48:32 -05001216 LOGV("getParameters(%s)", params.string());
Wu-cheng Liab5b4242009-04-22 16:21:26 +08001217 return params;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218}
1219
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001220status_t CameraService::Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
1221{
Dave Sparksc95a1762010-01-04 08:55:04 -08001222 LOGV("sendCommand (pid %d)", getCallingPid());
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001223 Mutex::Autolock lock(mLock);
1224 status_t result = checkPid();
1225 if (result != NO_ERROR) return result;
1226
Chih-Chung Changd1d77062010-01-22 17:49:48 -08001227 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
1228 // The orientation cannot be set during preview.
1229 if (mHardware->previewEnabled()) {
1230 return INVALID_OPERATION;
1231 }
1232 switch (arg1) {
1233 case 0:
1234 mOrientation = ISurface::BufferHeap::ROT_0;
1235 break;
1236 case 90:
1237 mOrientation = ISurface::BufferHeap::ROT_90;
1238 break;
1239 case 180:
1240 mOrientation = ISurface::BufferHeap::ROT_180;
1241 break;
1242 case 270:
1243 mOrientation = ISurface::BufferHeap::ROT_270;
1244 break;
1245 default:
1246 return BAD_VALUE;
1247 }
1248 return OK;
1249 }
1250
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001251 if (mHardware == 0) {
1252 LOGE("mHardware is NULL, returning.");
1253 return INVALID_OPERATION;
1254 }
1255
1256 return mHardware->sendCommand(cmd, arg1, arg2);
1257}
1258
Dave Sparksdd158c92009-10-15 10:02:22 -07001259void CameraService::Client::copyFrameAndPostCopiedFrame(const sp<ICameraClient>& client,
1260 const sp<IMemoryHeap>& heap, size_t offset, size_t size)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261{
1262 LOGV("copyFrameAndPostCopiedFrame");
1263 // It is necessary to copy out of pmem before sending this to
1264 // the callback. For efficiency, reuse the same MemoryHeapBase
1265 // provided it's big enough. Don't allocate the memory or
1266 // perform the copy if there's no callback.
Dave Sparksc8093c12009-11-06 11:47:13 -08001267
Dave Sparks05fd0df2009-11-10 17:08:08 -08001268 // hold the preview lock while we grab a reference to the preview buffer
Dave Sparksc8093c12009-11-06 11:47:13 -08001269 sp<MemoryHeapBase> previewBuffer;
1270 {
Dave Sparks05fd0df2009-11-10 17:08:08 -08001271 Mutex::Autolock lock(mPreviewLock);
Dave Sparksc8093c12009-11-06 11:47:13 -08001272 if (mPreviewBuffer == 0) {
1273 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1274 } else if (size > mPreviewBuffer->virtualSize()) {
1275 mPreviewBuffer.clear();
1276 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1277 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 if (mPreviewBuffer == 0) {
1279 LOGE("failed to allocate space for preview buffer");
1280 return;
1281 }
Dave Sparksc8093c12009-11-06 11:47:13 -08001282 previewBuffer = mPreviewBuffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 }
Dave Sparksc8093c12009-11-06 11:47:13 -08001284 memcpy(previewBuffer->base(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 (uint8_t *)heap->base() + offset, size);
1286
Dave Sparksc8093c12009-11-06 11:47:13 -08001287 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 if (frame == 0) {
1289 LOGE("failed to allocate space for frame callback");
1290 return;
1291 }
Dave Sparksdd158c92009-10-15 10:02:22 -07001292 client->dataCallback(CAMERA_MSG_PREVIEW_FRAME, frame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293}
1294
Chih-Chung Change86ae1b2010-03-05 11:33:03 -08001295static const int kDumpLockRetries = 50;
1296static const int kDumpLockSleep = 60000;
1297
1298static bool tryLock(Mutex& mutex)
1299{
1300 bool locked = false;
1301 for (int i = 0; i < kDumpLockRetries; ++i) {
1302 if (mutex.tryLock() == NO_ERROR) {
1303 locked = true;
1304 break;
1305 }
1306 usleep(kDumpLockSleep);
1307 }
1308 return locked;
1309}
1310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311status_t CameraService::dump(int fd, const Vector<String16>& args)
1312{
Chih-Chung Change86ae1b2010-03-05 11:33:03 -08001313 static const char* kDeadlockedString = "CameraService may be deadlocked\n";
1314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 const size_t SIZE = 256;
1316 char buffer[SIZE];
1317 String8 result;
1318 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
1319 snprintf(buffer, SIZE, "Permission Denial: "
1320 "can't dump CameraService from pid=%d, uid=%d\n",
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +08001321 getCallingPid(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 IPCThreadState::self()->getCallingUid());
1323 result.append(buffer);
1324 write(fd, result.string(), result.size());
1325 } else {
Chih-Chung Change86ae1b2010-03-05 11:33:03 -08001326 bool locked = tryLock(mServiceLock);
1327 // failed to lock - CameraService is probably deadlocked
1328 if (!locked) {
1329 String8 result(kDeadlockedString);
1330 write(fd, result.string(), result.size());
1331 }
1332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 if (mClient != 0) {
1334 sp<Client> currentClient = mClient.promote();
1335 sprintf(buffer, "Client (%p) PID: %d\n",
1336 currentClient->getCameraClient()->asBinder().get(),
1337 currentClient->mClientPid);
1338 result.append(buffer);
1339 write(fd, result.string(), result.size());
1340 currentClient->mHardware->dump(fd, args);
1341 } else {
1342 result.append("No camera client yet.\n");
1343 write(fd, result.string(), result.size());
1344 }
Chih-Chung Change86ae1b2010-03-05 11:33:03 -08001345
1346 if (locked) mServiceLock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 }
1348 return NO_ERROR;
1349}
1350
1351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352status_t CameraService::onTransact(
1353 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1354{
1355 // permission checks...
1356 switch (code) {
1357 case BnCameraService::CONNECT:
1358 IPCThreadState* ipc = IPCThreadState::self();
1359 const int pid = ipc->getCallingPid();
1360 const int self_pid = getpid();
1361 if (pid != self_pid) {
1362 // we're called from a different process, do the real check
1363 if (!checkCallingPermission(
1364 String16("android.permission.CAMERA")))
1365 {
1366 const int uid = ipc->getCallingUid();
1367 LOGE("Permission Denial: "
1368 "can't use the camera pid=%d, uid=%d", pid, uid);
1369 return PERMISSION_DENIED;
1370 }
1371 }
1372 break;
1373 }
1374
1375 status_t err = BnCameraService::onTransact(code, data, reply, flags);
1376
Dave Sparksb1f3b8f2009-05-20 20:02:59 -07001377#if DEBUG_HEAP_LEAKS
Joe Onoratof5d95cb2010-01-07 21:48:32 -05001378 LOGV("+++ onTransact err %d code %d", err, code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379
1380 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
1381 // the 'service' command interrogates this binder for its name, and then supplies it
1382 // even for the debugging commands. that means we need to check for it here, using
1383 // ISurfaceComposer (since we delegated the INTERFACE_TRANSACTION handling to
1384 // BnSurfaceComposer before falling through to this code).
1385
Joe Onoratof5d95cb2010-01-07 21:48:32 -05001386 LOGV("+++ onTransact code %d", code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387
1388 CHECK_INTERFACE(ICameraService, data, reply);
1389
1390 switch(code) {
1391 case 1000:
1392 {
1393 if (gWeakHeap != 0) {
1394 sp<IMemoryHeap> h = gWeakHeap.promote();
1395 IMemoryHeap *p = gWeakHeap.unsafe_get();
Joe Onoratof5d95cb2010-01-07 21:48:32 -05001396 LOGV("CHECKING WEAK REFERENCE %p (%p)", h.get(), p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 if (h != 0)
1398 h->printRefs();
1399 bool attempt_to_delete = data.readInt32() == 1;
1400 if (attempt_to_delete) {
1401 // NOT SAFE!
Joe Onoratof5d95cb2010-01-07 21:48:32 -05001402 LOGV("DELETING WEAK REFERENCE %p (%p)", h.get(), p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 if (p) delete p;
1404 }
1405 return NO_ERROR;
1406 }
1407 }
1408 break;
1409 default:
1410 break;
1411 }
1412 }
Dave Sparksb1f3b8f2009-05-20 20:02:59 -07001413#endif // DEBUG_HEAP_LEAKS
Dave Sparks7b7225c2009-05-21 09:18:18 -07001414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 return err;
1416}
1417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418}; // namespace android