blob: e7c1fbb3a21f1703cd75ff1a3e9ccf3e826c1803 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2**
3** Copyright (C) 2008, The Android Open Source Project
4** Copyright (C) 2008 HTC Inc.
5**
6** Licensed under the Apache License, Version 2.0 (the "License");
7** you may not use this file except in compliance with the License.
8** You may obtain a copy of the License at
9**
10** http://www.apache.org/licenses/LICENSE-2.0
11**
12** Unless required by applicable law or agreed to in writing, software
13** distributed under the License is distributed on an "AS IS" BASIS,
14** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15** See the License for the specific language governing permissions and
16** limitations under the License.
17*/
18
19//#define LOG_NDEBUG 0
20#define LOG_TAG "CameraService"
21#include <utils/Log.h>
22
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070023#include <binder/IServiceManager.h>
24#include <binder/IPCThreadState.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <utils/String16.h>
26#include <utils/Errors.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070027#include <binder/MemoryBase.h>
28#include <binder/MemoryHeapBase.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029#include <ui/ICameraService.h>
30
Jason Sams78b877e2009-03-24 20:21:36 -070031#include <media/mediaplayer.h>
32#include <media/AudioSystem.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033#include "CameraService.h"
34
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +080035#include <cutils/atomic.h>
Eric Laurentcbcb00e2009-03-27 16:27:16 -070036
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037namespace android {
38
39extern "C" {
40#include <stdio.h>
41#include <sys/types.h>
42#include <sys/stat.h>
43#include <fcntl.h>
44#include <pthread.h>
Chih-Chung Changd98c5162009-06-22 16:03:41 +080045#include <signal.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046}
47
48// When you enable this, as well as DEBUG_REFS=1 and
49// DEBUG_REFS_ENABLED_BY_DEFAULT=0 in libutils/RefBase.cpp, this will track all
50// references to the CameraService::Client in order to catch the case where the
51// client is being destroyed while a callback from the CameraHardwareInterface
52// is outstanding. This is a serious bug because if we make another call into
53// CameraHardwreInterface that itself triggers a callback, we will deadlock.
54
55#define DEBUG_CLIENT_REFERENCES 0
56
57#define PICTURE_TIMEOUT seconds(5)
58
59#define DEBUG_DUMP_PREVIEW_FRAME_TO_FILE 0 /* n-th frame to write */
60#define DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE 0
61#define DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE 0
62
63#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
64static int debug_frame_cnt;
65#endif
66
Chih-Chung Changd98c5162009-06-22 16:03:41 +080067static int getCallingPid() {
68 return IPCThreadState::self()->getCallingPid();
69}
70
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071// ----------------------------------------------------------------------------
72
73void CameraService::instantiate() {
74 defaultServiceManager()->addService(
75 String16("media.camera"), new CameraService());
76}
77
78// ----------------------------------------------------------------------------
79
80CameraService::CameraService() :
81 BnCameraService()
82{
83 LOGI("CameraService started: pid=%d", getpid());
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +080084 mUsers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085}
86
87CameraService::~CameraService()
88{
89 if (mClient != 0) {
90 LOGE("mClient was still connected in destructor!");
91 }
92}
93
94sp<ICamera> CameraService::connect(const sp<ICameraClient>& cameraClient)
95{
Chih-Chung Changd98c5162009-06-22 16:03:41 +080096 int callingPid = getCallingPid();
97 LOGD("CameraService::connect E (pid %d, client %p)", callingPid,
98 cameraClient->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800100 Mutex::Autolock lock(mServiceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101 sp<Client> client;
102 if (mClient != 0) {
103 sp<Client> currentClient = mClient.promote();
104 if (currentClient != 0) {
105 sp<ICameraClient> currentCameraClient(currentClient->getCameraClient());
106 if (cameraClient->asBinder() == currentCameraClient->asBinder()) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800107 // This is the same client reconnecting...
108 LOGD("CameraService::connect X (pid %d, same client %p) is reconnecting...",
109 callingPid, cameraClient->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110 return currentClient;
111 } else {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800112 // It's another client... reject it
113 LOGD("CameraService::connect X (pid %d, new client %p) rejected. "
114 "(old pid %d, old client %p)",
115 callingPid, cameraClient->asBinder().get(),
116 currentClient->mClientPid, currentCameraClient->asBinder().get());
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800117 if (kill(currentClient->mClientPid, 0) == -1 && errno == ESRCH) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800118 LOGD("The old client is dead!");
119 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120 return client;
121 }
122 } else {
123 // can't promote, the previous client has died...
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800124 LOGD("New client (pid %d) connecting, old reference was dangling...",
125 callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800126 mClient.clear();
127 }
128 }
129
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800130 if (mUsers > 0) {
131 LOGD("Still have client, rejected");
132 return client;
133 }
134
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135 // create a new Client object
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800136 client = new Client(this, cameraClient, callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137 mClient = client;
138#if DEBUG_CLIENT_REFERENCES
139 // Enable tracking for this object, and track increments and decrements of
140 // the refcount.
141 client->trackMe(true, true);
142#endif
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800143 LOGD("CameraService::connect X");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144 return client;
145}
146
147void CameraService::removeClient(const sp<ICameraClient>& cameraClient)
148{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800149 int callingPid = getCallingPid();
150
151 // Declare this outside the lock to make absolutely sure the
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152 // destructor won't be called with the lock held.
153 sp<Client> client;
154
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800155 Mutex::Autolock lock(mServiceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156
157 if (mClient == 0) {
158 // This happens when we have already disconnected.
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800159 LOGD("removeClient (pid %d): already disconnected", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160 return;
161 }
162
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800163 // Promote mClient. It can fail if we are called from this path:
164 // Client::~Client() -> disconnect() -> removeClient().
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 client = mClient.promote();
166 if (client == 0) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800167 LOGD("removeClient (pid %d): no more strong reference", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168 mClient.clear();
169 return;
170 }
171
172 if (cameraClient->asBinder() != client->getCameraClient()->asBinder()) {
173 // ugh! that's not our client!!
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800174 LOGW("removeClient (pid %d): mClient doesn't match!", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175 } else {
176 // okay, good, forget about mClient
177 mClient.clear();
178 }
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800179
180 LOGD("removeClient (pid %d) done", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181}
182
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800183// The reason we need this count is a new CameraService::connect() request may
184// come in while the previous Client's destructor has not been run or is still
185// running. If the last strong reference of the previous Client is gone but
186// destructor has not been run, we should not allow the new Client to be created
187// because we need to wait for the previous Client to tear down the hardware
188// first.
189void CameraService::incUsers() {
190 android_atomic_inc(&mUsers);
191}
192
193void CameraService::decUsers() {
194 android_atomic_dec(&mUsers);
195}
196
Jason Sams78b877e2009-03-24 20:21:36 -0700197static sp<MediaPlayer> newMediaPlayer(const char *file)
198{
199 sp<MediaPlayer> mp = new MediaPlayer();
200 if (mp->setDataSource(file) == NO_ERROR) {
Eric Laurent9d91ad52009-07-17 12:17:14 -0700201 mp->setAudioStreamType(AudioSystem::ENFORCED_AUDIBLE);
Jason Sams78b877e2009-03-24 20:21:36 -0700202 mp->prepare();
203 } else {
204 mp.clear();
205 LOGE("Failed to load CameraService sounds.");
206 }
207 return mp;
208}
209
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210CameraService::Client::Client(const sp<CameraService>& cameraService,
211 const sp<ICameraClient>& cameraClient, pid_t clientPid)
212{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800213 int callingPid = getCallingPid();
214 LOGD("Client::Client E (pid %d)", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215 mCameraService = cameraService;
216 mCameraClient = cameraClient;
217 mClientPid = clientPid;
218 mHardware = openCameraHardware();
219 mUseOverlay = mHardware->useOverlay();
220
Jason Sams78b877e2009-03-24 20:21:36 -0700221 mMediaPlayerClick = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
222 mMediaPlayerBeep = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
Benny Wong71f77152009-07-15 18:44:27 -0500223 mOverlayW = 0;
224 mOverlayH = 0;
Jason Sams78b877e2009-03-24 20:21:36 -0700225
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226 // Callback is disabled by default
227 mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800228 cameraService->incUsers();
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800229 LOGD("Client::Client X (pid %d)", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230}
231
232status_t CameraService::Client::checkPid()
233{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800234 int callingPid = getCallingPid();
235 if (mClientPid == callingPid) return NO_ERROR;
236 LOGW("Attempt to use locked camera (client %p) from different process "
237 " (old pid %d, new pid %d)",
238 getCameraClient()->asBinder().get(), mClientPid, callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800239 return -EBUSY;
240}
241
242status_t CameraService::Client::lock()
243{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800244 int callingPid = getCallingPid();
245 LOGD("lock from pid %d (mClientPid %d)", callingPid, mClientPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800246 Mutex::Autolock _l(mLock);
247 // lock camera to this client if the the camera is unlocked
248 if (mClientPid == 0) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800249 mClientPid = callingPid;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250 return NO_ERROR;
251 }
252 // returns NO_ERROR if the client already owns the camera, -EBUSY otherwise
253 return checkPid();
254}
255
256status_t CameraService::Client::unlock()
257{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800258 int callingPid = getCallingPid();
259 LOGD("unlock from pid %d (mClientPid %d)", callingPid, mClientPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260 Mutex::Autolock _l(mLock);
261 // allow anyone to use camera
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262 status_t result = checkPid();
James Dong0ae13e32009-04-20 19:35:28 -0700263 if (result == NO_ERROR) {
264 mClientPid = 0;
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800265 LOGD("clear mCameraClient (pid %d)", callingPid);
James Dong0ae13e32009-04-20 19:35:28 -0700266 // we need to remove the reference so that when app goes
267 // away, the reference count goes to 0.
268 mCameraClient.clear();
269 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270 return result;
271}
272
273status_t CameraService::Client::connect(const sp<ICameraClient>& client)
274{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800275 int callingPid = getCallingPid();
276
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800277 // connect a new process to the camera
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800278 LOGD("Client::connect E (pid %d, client %p)", callingPid, client->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279
280 // I hate this hack, but things get really ugly when the media recorder
281 // service is handing back the camera to the app. The ICameraClient
282 // destructor will be called during the same IPC, making it look like
283 // the remote client is trying to disconnect. This hack temporarily
284 // sets the mClientPid to an invalid pid to prevent the hardware from
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800285 // being torn down.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800286 {
287
288 // hold a reference to the old client or we will deadlock if the client is
289 // in the same process and we hold the lock when we remove the reference
290 sp<ICameraClient> oldClient;
291 {
292 Mutex::Autolock _l(mLock);
Chih-Chung Chang34e5a152009-06-09 13:56:44 +0800293 if (mClientPid != 0 && checkPid() != NO_ERROR) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800294 LOGW("Tried to connect to locked camera (old pid %d, new pid %d)",
295 mClientPid, callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296 return -EBUSY;
297 }
298 oldClient = mCameraClient;
299
300 // did the client actually change?
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800301 if (client->asBinder() == mCameraClient->asBinder()) {
302 LOGD("Connect to the same client");
303 return NO_ERROR;
304 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305
306 mCameraClient = client;
307 mClientPid = -1;
308 mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800309 LOGD("Connect to the new client (pid %d, client %p)",
310 callingPid, mCameraClient->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311 }
312
313 }
314 // the old client destructor is called when oldClient goes out of scope
315 // now we set the new PID to lock the interface again
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800316 mClientPid = callingPid;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800317
318 return NO_ERROR;
319}
320
321#if HAVE_ANDROID_OS
322static void *unregister_surface(void *arg)
323{
324 ISurface *surface = (ISurface *)arg;
325 surface->unregisterBuffers();
326 IPCThreadState::self()->flushCommands();
327 return NULL;
328}
329#endif
330
331CameraService::Client::~Client()
332{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800333 int callingPid = getCallingPid();
334
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335 // tear down client
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800336 LOGD("Client::~Client E (pid %d, client %p)",
337 callingPid, getCameraClient()->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338 if (mSurface != 0 && !mUseOverlay) {
339#if HAVE_ANDROID_OS
340 pthread_t thr;
341 // We unregister the buffers in a different thread because binder does
342 // not let us make sychronous transactions in a binder destructor (that
343 // is, upon our reaching a refcount of zero.)
344 pthread_create(&thr, NULL,
345 unregister_surface,
346 mSurface.get());
347 pthread_join(thr, NULL);
348#else
349 mSurface->unregisterBuffers();
350#endif
351 }
352
Jason Sams9e431ac2009-03-24 20:36:57 -0700353 if (mMediaPlayerBeep.get() != NULL) {
354 mMediaPlayerBeep->disconnect();
355 mMediaPlayerBeep.clear();
356 }
357 if (mMediaPlayerClick.get() != NULL) {
358 mMediaPlayerClick->disconnect();
359 mMediaPlayerClick.clear();
360 }
Jason Sams78b877e2009-03-24 20:21:36 -0700361
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800362 // make sure we tear down the hardware
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800363 mClientPid = callingPid;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800364 disconnect();
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800365 LOGD("Client::~Client X (pid %d)", mClientPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800366}
367
368void CameraService::Client::disconnect()
369{
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800370 int callingPid = getCallingPid();
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800371
372 LOGD("Client::disconnect() E (pid %d client %p)",
373 callingPid, getCameraClient()->asBinder().get());
374
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800375 Mutex::Autolock lock(mLock);
376 if (mClientPid <= 0) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800377 LOGD("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800378 return;
379 }
380 if (checkPid() != NO_ERROR) {
Chih-Chung Chang6a5297d2009-06-16 17:15:04 +0800381 LOGD("Different client - don't disconnect");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800382 return;
383 }
384
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800385 // Make sure disconnect() is done once and once only, whether it is called
386 // from the user directly, or called by the destructor.
387 if (mHardware == 0) return;
388
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800389 LOGD("hardware teardown");
390 // Before destroying mHardware, we must make sure it's in the
391 // idle state.
392 mHardware->stopPreview();
393 // Cancel all picture callbacks.
394 mHardware->cancelPicture(true, true, true);
395 // Release the hardware resources.
396 mHardware->release();
Benny Wong71f77152009-07-15 18:44:27 -0500397 // Release the held overlay resources.
398 if (mUseOverlay)
399 {
400 mOverlayRef = 0;
401 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800402 mHardware.clear();
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800403
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800404 mCameraService->removeClient(mCameraClient);
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800405 mCameraService->decUsers();
406
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800407 LOGD("Client::disconnect() X (pid %d)", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800408}
409
410// pass the buffered ISurface to the camera service
411status_t CameraService::Client::setPreviewDisplay(const sp<ISurface>& surface)
412{
Wu-cheng Li988fb622009-06-23 23:37:36 +0800413 LOGD("setPreviewDisplay(%p) (pid %d)",
414 ((surface == NULL) ? NULL : surface.get()), getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800415 Mutex::Autolock lock(mLock);
416 status_t result = checkPid();
417 if (result != NO_ERROR) return result;
Wu-cheng Li988fb622009-06-23 23:37:36 +0800418
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800419 Mutex::Autolock surfaceLock(mSurfaceLock);
Wu-cheng Li988fb622009-06-23 23:37:36 +0800420 result = NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800421 // asBinder() is safe on NULL (returns NULL)
422 if (surface->asBinder() != mSurface->asBinder()) {
Benny Wong71f77152009-07-15 18:44:27 -0500423 if (mSurface != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800424 LOGD("clearing old preview surface %p", mSurface.get());
Benny Wong71f77152009-07-15 18:44:27 -0500425 if ( !mUseOverlay)
426 {
427 mSurface->unregisterBuffers();
428 }
429 else
430 {
431 // Force the destruction of any previous overlay
432 sp<Overlay> dummy;
433 mHardware->setOverlay( dummy );
434 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800435 }
436 mSurface = surface;
Benny Wong71f77152009-07-15 18:44:27 -0500437 mOverlayRef = 0;
Wu-cheng Li988fb622009-06-23 23:37:36 +0800438 // If preview has been already started, set overlay or register preview
439 // buffers now.
440 if (mHardware->previewEnabled()) {
441 if (mUseOverlay) {
442 result = setOverlay();
443 } else if (mSurface != 0) {
444 result = registerPreviewBuffers();
445 }
446 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800447 }
Wu-cheng Li988fb622009-06-23 23:37:36 +0800448 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800449}
450
451// set the preview callback flag to affect how the received frames from
452// preview are handled.
453void CameraService::Client::setPreviewCallbackFlag(int callback_flag)
454{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800455 LOGV("setPreviewCallbackFlag (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800456 Mutex::Autolock lock(mLock);
457 if (checkPid() != NO_ERROR) return;
458 mPreviewCallbackFlag = callback_flag;
459}
460
Wu-cheng Li988fb622009-06-23 23:37:36 +0800461// start preview mode
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800462status_t CameraService::Client::startCameraMode(camera_mode mode)
463{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800464 int callingPid = getCallingPid();
465
466 LOGD("startCameraMode(%d) (pid %d)", mode, callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800467
468 /* we cannot call into mHardware with mLock held because
469 * mHardware has callbacks onto us which acquire this lock
470 */
471
472 Mutex::Autolock lock(mLock);
473 status_t result = checkPid();
474 if (result != NO_ERROR) return result;
475
476 if (mHardware == 0) {
477 LOGE("mHardware is NULL, returning.");
478 return INVALID_OPERATION;
479 }
480
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800481 switch(mode) {
482 case CAMERA_RECORDING_MODE:
Wu-cheng Li988fb622009-06-23 23:37:36 +0800483 if (mSurface == 0) {
484 LOGE("setPreviewDisplay must be called before startRecordingMode.");
485 return INVALID_OPERATION;
486 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800487 return startRecordingMode();
488
489 default: // CAMERA_PREVIEW_MODE
Wu-cheng Li988fb622009-06-23 23:37:36 +0800490 if (mSurface == 0) {
491 LOGD("mSurface is not set yet.");
492 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800493 return startPreviewMode();
494 }
495}
496
497status_t CameraService::Client::startRecordingMode()
498{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800499 LOGD("startRecordingMode (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800500
501 status_t ret = UNKNOWN_ERROR;
502
503 // if preview has not been started, start preview first
504 if (!mHardware->previewEnabled()) {
505 ret = startPreviewMode();
506 if (ret != NO_ERROR) {
507 return ret;
508 }
509 }
510
511 // if recording has been enabled, nothing needs to be done
512 if (mHardware->recordingEnabled()) {
513 return NO_ERROR;
514 }
515
516 // start recording mode
Dave Sparksf72d6402009-07-08 15:56:53 -0700517 ret = mHardware->startRecording(recordingCallback, mCameraService.get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800518 if (ret != NO_ERROR) {
519 LOGE("mHardware->startRecording() failed with status %d", ret);
520 }
521 return ret;
522}
523
Wu-cheng Li988fb622009-06-23 23:37:36 +0800524status_t CameraService::Client::setOverlay()
525{
526 LOGD("setOverlay");
527 int w, h;
528 CameraParameters params(mHardware->getParameters());
529 params.getPreviewSize(&w, &h);
530
531 const char *format = params.getPreviewFormat();
532 int fmt;
Benny Wong71f77152009-07-15 18:44:27 -0500533 if (!strcmp(format, "yuv422i-yuyv"))
534 fmt = OVERLAY_FORMAT_YCbYCr_422_I;
Wu-cheng Li988fb622009-06-23 23:37:36 +0800535 else if (!strcmp(format, "rgb565"))
536 fmt = OVERLAY_FORMAT_RGB_565;
537 else {
538 LOGE("Invalid preview format for overlays");
539 return -EINVAL;
540 }
541
Benny Wong71f77152009-07-15 18:44:27 -0500542 if ( w != mOverlayW || h != mOverlayH )
543 {
544 // Force the destruction of any previous overlay
545 sp<Overlay> dummy;
546 mHardware->setOverlay( dummy );
547 mOverlayRef = 0;
548 }
549
Wu-cheng Li988fb622009-06-23 23:37:36 +0800550 status_t ret = NO_ERROR;
551 if (mSurface != 0) {
Benny Wong71f77152009-07-15 18:44:27 -0500552 if (mOverlayRef.get() == NULL) {
553 mOverlayRef = mSurface->createOverlay(w, h, fmt);
554 if ( mOverlayRef.get() == NULL )
555 {
556 LOGE("Overlay Creation Failed!");
557 return -EINVAL;
558 }
559 ret = mHardware->setOverlay(new Overlay(mOverlayRef));
560 }
Wu-cheng Li988fb622009-06-23 23:37:36 +0800561 } else {
562 ret = mHardware->setOverlay(NULL);
563 }
564 if (ret != NO_ERROR) {
565 LOGE("mHardware->setOverlay() failed with status %d\n", ret);
566 }
Benny Wong71f77152009-07-15 18:44:27 -0500567
568 mOverlayW = w;
569 mOverlayH = h;
570
Wu-cheng Li988fb622009-06-23 23:37:36 +0800571 return ret;
572}
573
574status_t CameraService::Client::registerPreviewBuffers()
575{
576 int w, h;
577 CameraParameters params(mHardware->getParameters());
578 params.getPreviewSize(&w, &h);
579
580 uint32_t transform = 0;
581 if (params.getOrientation() ==
582 CameraParameters::CAMERA_ORIENTATION_PORTRAIT) {
583 LOGV("portrait mode");
584 transform = ISurface::BufferHeap::ROT_90;
585 }
586 ISurface::BufferHeap buffers(w, h, w, h,
587 PIXEL_FORMAT_YCbCr_420_SP,
588 transform,
589 0,
590 mHardware->getPreviewHeap());
591
592 status_t ret = mSurface->registerBuffers(buffers);
593 if (ret != NO_ERROR) {
594 LOGE("registerBuffers failed with status %d", ret);
595 }
596 return ret;
597}
598
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800599status_t CameraService::Client::startPreviewMode()
600{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800601 LOGD("startPreviewMode (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800602
603 // if preview has been enabled, nothing needs to be done
604 if (mHardware->previewEnabled()) {
605 return NO_ERROR;
606 }
607
608 // start preview mode
609#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
610 debug_frame_cnt = 0;
611#endif
Wu-cheng Li988fb622009-06-23 23:37:36 +0800612 status_t ret = NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800613
614 if (mUseOverlay) {
Wu-cheng Li988fb622009-06-23 23:37:36 +0800615 // If preview display has been set, set overlay now.
616 if (mSurface != 0) {
617 ret = setOverlay();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800618 }
Wu-cheng Li988fb622009-06-23 23:37:36 +0800619 if (ret != NO_ERROR) return ret;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800620 ret = mHardware->startPreview(NULL, mCameraService.get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800621 } else {
622 ret = mHardware->startPreview(previewCallback,
623 mCameraService.get());
Wu-cheng Li988fb622009-06-23 23:37:36 +0800624 if (ret != NO_ERROR) return ret;
625 // If preview display has been set, register preview buffers now.
626 if (mSurface != 0) {
627 // Unregister here because the surface registered with raw heap.
628 mSurface->unregisterBuffers();
629 ret = registerPreviewBuffers();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800630 }
631 }
632 return ret;
633}
634
635status_t CameraService::Client::startPreview()
636{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800637 LOGD("startPreview (pid %d)", getCallingPid());
638
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800639 return startCameraMode(CAMERA_PREVIEW_MODE);
640}
641
642status_t CameraService::Client::startRecording()
643{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800644 LOGD("startRecording (pid %d)", getCallingPid());
645
Jason Sams78b877e2009-03-24 20:21:36 -0700646 if (mMediaPlayerBeep.get() != NULL) {
Andreas Hubera26e6062009-03-24 20:47:19 -0700647 mMediaPlayerBeep->seekTo(0);
Jason Sams78b877e2009-03-24 20:21:36 -0700648 mMediaPlayerBeep->start();
649 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800650 return startCameraMode(CAMERA_RECORDING_MODE);
651}
652
653// stop preview mode
654void CameraService::Client::stopPreview()
655{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800656 LOGD("stopPreview (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800657
658 Mutex::Autolock lock(mLock);
659 if (checkPid() != NO_ERROR) return;
660
661 if (mHardware == 0) {
662 LOGE("mHardware is NULL, returning.");
663 return;
664 }
665
666 mHardware->stopPreview();
667 LOGD("stopPreview(), hardware stopped OK");
668
669 if (mSurface != 0 && !mUseOverlay) {
670 mSurface->unregisterBuffers();
671 }
672 mPreviewBuffer.clear();
673}
674
675// stop recording mode
676void CameraService::Client::stopRecording()
677{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800678 LOGD("stopRecording (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800679
680 Mutex::Autolock lock(mLock);
681 if (checkPid() != NO_ERROR) return;
682
683 if (mHardware == 0) {
684 LOGE("mHardware is NULL, returning.");
685 return;
686 }
687
Jason Sams78b877e2009-03-24 20:21:36 -0700688 if (mMediaPlayerBeep.get() != NULL) {
Andreas Hubera26e6062009-03-24 20:47:19 -0700689 mMediaPlayerBeep->seekTo(0);
Jason Sams78b877e2009-03-24 20:21:36 -0700690 mMediaPlayerBeep->start();
691 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800692 mHardware->stopRecording();
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800693 LOGD("stopRecording(), hardware stopped OK");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800694 mPreviewBuffer.clear();
695}
696
697// release a recording frame
698void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem)
699{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800700 Mutex::Autolock lock(mLock);
701 if (checkPid() != NO_ERROR) return;
702
703 if (mHardware == 0) {
704 LOGE("mHardware is NULL, returning.");
705 return;
706 }
707
708 mHardware->releaseRecordingFrame(mem);
709}
710
711bool CameraService::Client::previewEnabled()
712{
713 Mutex::Autolock lock(mLock);
714 if (mHardware == 0) return false;
715 return mHardware->previewEnabled();
716}
717
718bool CameraService::Client::recordingEnabled()
719{
720 Mutex::Autolock lock(mLock);
721 if (mHardware == 0) return false;
722 return mHardware->recordingEnabled();
723}
724
725// Safely retrieves a strong pointer to the client during a hardware callback.
726sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user)
727{
728 sp<Client> client = 0;
729 CameraService *service = static_cast<CameraService*>(user);
730 if (service != NULL) {
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800731 Mutex::Autolock ourLock(service->mServiceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800732 if (service->mClient != 0) {
733 client = service->mClient.promote();
734 if (client == 0) {
735 LOGE("getClientFromCookie: client appears to have died");
736 service->mClient.clear();
737 }
738 } else {
739 LOGE("getClientFromCookie: got callback but client was NULL");
740 }
741 }
742 return client;
743}
744
745
746#if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE || \
747 DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE || \
748 DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
749static void dump_to_file(const char *fname,
750 uint8_t *buf, uint32_t size)
751{
752 int nw, cnt = 0;
753 uint32_t written = 0;
754
755 LOGD("opening file [%s]\n", fname);
756 int fd = open(fname, O_RDWR | O_CREAT);
757 if (fd < 0) {
758 LOGE("failed to create file [%s]: %s", fname, strerror(errno));
759 return;
760 }
761
762 LOGD("writing %d bytes to file [%s]\n", size, fname);
763 while (written < size) {
764 nw = ::write(fd,
765 buf + written,
766 size - written);
767 if (nw < 0) {
768 LOGE("failed to write to file [%s]: %s",
769 fname, strerror(errno));
770 break;
771 }
772 written += nw;
773 cnt++;
774 }
775 LOGD("done writing %d bytes to file [%s] in %d passes\n",
776 size, fname, cnt);
777 ::close(fd);
778}
779#endif
780
781// preview callback - frame buffer update
782void CameraService::Client::previewCallback(const sp<IMemory>& mem, void* user)
783{
784 LOGV("previewCallback()");
785 sp<Client> client = getClientFromCookie(user);
786 if (client == 0) {
787 return;
788 }
789
790#if DEBUG_HEAP_LEAKS && 0 // debugging
791 if (gWeakHeap == NULL) {
792 ssize_t offset;
793 size_t size;
794 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
795 if (gWeakHeap != heap) {
796 LOGD("SETTING PREVIEW HEAP");
797 heap->trackMe(true, true);
798 gWeakHeap = heap;
799 }
800 }
801#endif
802
803#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
804 {
805 if (debug_frame_cnt++ == DEBUG_DUMP_PREVIEW_FRAME_TO_FILE) {
806 ssize_t offset;
807 size_t size;
808 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
809 dump_to_file("/data/preview.yuv",
810 (uint8_t *)heap->base() + offset, size);
811 }
812 }
813#endif
814
815 // The strong pointer guarantees the client will exist, but no lock is held.
816 client->postPreviewFrame(mem);
817
818#if DEBUG_CLIENT_REFERENCES
819 //**** if the client's refcount is 1, then we are about to destroy it here,
820 // which is bad--print all refcounts.
821 if (client->getStrongCount() == 1) {
822 LOGE("++++++++++++++++ (PREVIEW) THIS WILL CAUSE A LOCKUP!");
823 client->printRefs();
824 }
825#endif
826}
827
828// recording callback
Dave Sparksf72d6402009-07-08 15:56:53 -0700829void CameraService::Client::recordingCallback(nsecs_t timestamp, const sp<IMemory>& mem, void* user)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800830{
831 LOGV("recordingCallback");
832 sp<Client> client = getClientFromCookie(user);
833 if (client == 0) {
834 return;
835 }
836 // The strong pointer guarantees the client will exist, but no lock is held.
Dave Sparksf72d6402009-07-08 15:56:53 -0700837 client->postRecordingFrame(timestamp, mem);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800838}
839
840// take a picture - image is returned in callback
841status_t CameraService::Client::autoFocus()
842{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800843 LOGD("autoFocus (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800844
845 Mutex::Autolock lock(mLock);
846 status_t result = checkPid();
847 if (result != NO_ERROR) return result;
848
849 if (mHardware == 0) {
850 LOGE("mHardware is NULL, returning.");
851 return INVALID_OPERATION;
852 }
853
854 return mHardware->autoFocus(autoFocusCallback,
855 mCameraService.get());
856}
857
858// take a picture - image is returned in callback
859status_t CameraService::Client::takePicture()
860{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800861 LOGD("takePicture (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800862
863 Mutex::Autolock lock(mLock);
864 status_t result = checkPid();
865 if (result != NO_ERROR) return result;
866
867 if (mHardware == 0) {
868 LOGE("mHardware is NULL, returning.");
869 return INVALID_OPERATION;
870 }
871
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800872 return mHardware->takePicture(shutterCallback,
873 yuvPictureCallback,
874 jpegPictureCallback,
875 mCameraService.get());
876}
877
878// picture callback - snapshot taken
879void CameraService::Client::shutterCallback(void *user)
880{
881 sp<Client> client = getClientFromCookie(user);
882 if (client == 0) {
883 return;
884 }
885
Wu-cheng Liaab44b82009-03-24 20:39:09 -0700886 // Play shutter sound.
887 if (client->mMediaPlayerClick.get() != NULL) {
Andreas Hubera26e6062009-03-24 20:47:19 -0700888 client->mMediaPlayerClick->seekTo(0);
Wu-cheng Liaab44b82009-03-24 20:39:09 -0700889 client->mMediaPlayerClick->start();
890 }
891
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800892 // Screen goes black after the buffer is unregistered.
893 if (client->mSurface != 0 && !client->mUseOverlay) {
894 client->mSurface->unregisterBuffers();
895 }
896
897 client->postShutter();
898
899 // It takes some time before yuvPicture callback to be called.
900 // Register the buffer for raw image here to reduce latency.
901 if (client->mSurface != 0 && !client->mUseOverlay) {
902 int w, h;
903 CameraParameters params(client->mHardware->getParameters());
904 params.getPictureSize(&w, &h);
905 uint32_t transform = 0;
906 if (params.getOrientation() == CameraParameters::CAMERA_ORIENTATION_PORTRAIT) {
907 LOGV("portrait mode");
908 transform = ISurface::BufferHeap::ROT_90;
909 }
910 ISurface::BufferHeap buffers(w, h, w, h,
911 PIXEL_FORMAT_YCbCr_420_SP, transform, 0, client->mHardware->getRawHeap());
912
913 client->mSurface->registerBuffers(buffers);
914 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800915}
916
917// picture callback - raw image ready
918void CameraService::Client::yuvPictureCallback(const sp<IMemory>& mem,
919 void *user)
920{
921 sp<Client> client = getClientFromCookie(user);
922 if (client == 0) {
923 return;
924 }
925 if (mem == NULL) {
926 client->postRaw(NULL);
927 client->postError(UNKNOWN_ERROR);
928 return;
929 }
930
931 ssize_t offset;
932 size_t size;
933 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
934#if DEBUG_HEAP_LEAKS && 0 // debugging
935 gWeakHeap = heap; // debugging
936#endif
937
938 //LOGV("yuvPictureCallback(%d, %d, %p)", offset, size, user);
939#if DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE // for testing pursposes only
940 dump_to_file("/data/photo.yuv",
941 (uint8_t *)heap->base() + offset, size);
942#endif
943
944 // Put the YUV version of the snapshot in the preview display.
945 if (client->mSurface != 0 && !client->mUseOverlay) {
946 client->mSurface->postBuffer(offset);
947 }
948
949 client->postRaw(mem);
950
951#if DEBUG_CLIENT_REFERENCES
952 //**** if the client's refcount is 1, then we are about to destroy it here,
953 // which is bad--print all refcounts.
954 if (client->getStrongCount() == 1) {
955 LOGE("++++++++++++++++ (RAW) THIS WILL CAUSE A LOCKUP!");
956 client->printRefs();
957 }
958#endif
959}
960
961// picture callback - jpeg ready
962void CameraService::Client::jpegPictureCallback(const sp<IMemory>& mem, void *user)
963{
964 sp<Client> client = getClientFromCookie(user);
965 if (client == 0) {
966 return;
967 }
968 if (mem == NULL) {
969 client->postJpeg(NULL);
970 client->postError(UNKNOWN_ERROR);
971 return;
972 }
973
974 /** We absolutely CANNOT call into user code with a lock held **/
975
976#if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE // for testing pursposes only
977 {
978 ssize_t offset;
979 size_t size;
980 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
981 dump_to_file("/data/photo.jpg",
982 (uint8_t *)heap->base() + offset, size);
983 }
984#endif
985
986 client->postJpeg(mem);
987
988#if DEBUG_CLIENT_REFERENCES
989 //**** if the client's refcount is 1, then we are about to destroy it here,
990 // which is bad--print all refcounts.
991 if (client->getStrongCount() == 1) {
992 LOGE("++++++++++++++++ (JPEG) THIS WILL CAUSE A LOCKUP!");
993 client->printRefs();
994 }
995#endif
996}
997
998void CameraService::Client::autoFocusCallback(bool focused, void *user)
999{
1000 LOGV("autoFocusCallback");
1001
1002 sp<Client> client = getClientFromCookie(user);
1003 if (client == 0) {
1004 return;
1005 }
1006
1007 client->postAutoFocus(focused);
1008
1009#if DEBUG_CLIENT_REFERENCES
1010 if (client->getStrongCount() == 1) {
1011 LOGE("++++++++++++++++ (AUTOFOCUS) THIS WILL CAUSE A LOCKUP!");
1012 client->printRefs();
1013 }
1014#endif
1015}
1016
1017// set preview/capture parameters - key/value pairs
1018status_t CameraService::Client::setParameters(const String8& params)
1019{
1020 LOGD("setParameters(%s)", params.string());
1021
1022 Mutex::Autolock lock(mLock);
1023 status_t result = checkPid();
1024 if (result != NO_ERROR) return result;
1025
1026 if (mHardware == 0) {
1027 LOGE("mHardware is NULL, returning.");
1028 return INVALID_OPERATION;
1029 }
1030
1031 CameraParameters p(params);
1032 mHardware->setParameters(p);
1033 return NO_ERROR;
1034}
1035
1036// get preview/capture parameters - key/value pairs
1037String8 CameraService::Client::getParameters() const
1038{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001039 Mutex::Autolock lock(mLock);
1040
1041 if (mHardware == 0) {
1042 LOGE("mHardware is NULL, returning.");
1043 return String8();
1044 }
1045
Wu-cheng Li81d763f2009-04-22 16:21:26 +08001046 String8 params(mHardware->getParameters().flatten());
1047 LOGD("getParameters(%s)", params.string());
1048 return params;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001049}
1050
1051void CameraService::Client::postAutoFocus(bool focused)
1052{
1053 LOGV("postAutoFocus");
Dave Sparks93b94582009-05-07 19:27:32 -07001054 mCameraClient->notifyCallback(CAMERA_MSG_FOCUS, (int32_t)focused, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001055}
1056
1057void CameraService::Client::postShutter()
1058{
Chih-Chung Changd98c5162009-06-22 16:03:41 +08001059 LOGD("postShutter");
Dave Sparks93b94582009-05-07 19:27:32 -07001060 mCameraClient->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001061}
1062
1063void CameraService::Client::postRaw(const sp<IMemory>& mem)
1064{
1065 LOGD("postRaw");
Dave Sparks93b94582009-05-07 19:27:32 -07001066 mCameraClient->dataCallback(CAMERA_MSG_RAW_IMAGE, mem);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067}
1068
1069void CameraService::Client::postJpeg(const sp<IMemory>& mem)
1070{
1071 LOGD("postJpeg");
Dave Sparks93b94582009-05-07 19:27:32 -07001072 mCameraClient->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001073}
1074
1075void CameraService::Client::copyFrameAndPostCopiedFrame(sp<IMemoryHeap> heap, size_t offset, size_t size)
1076{
1077 LOGV("copyFrameAndPostCopiedFrame");
1078 // It is necessary to copy out of pmem before sending this to
1079 // the callback. For efficiency, reuse the same MemoryHeapBase
1080 // provided it's big enough. Don't allocate the memory or
1081 // perform the copy if there's no callback.
1082 if (mPreviewBuffer == 0) {
1083 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1084 } else if (size > mPreviewBuffer->virtualSize()) {
1085 mPreviewBuffer.clear();
1086 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1087 if (mPreviewBuffer == 0) {
1088 LOGE("failed to allocate space for preview buffer");
1089 return;
1090 }
1091 }
1092 memcpy(mPreviewBuffer->base(),
1093 (uint8_t *)heap->base() + offset, size);
1094
1095 sp<MemoryBase> frame = new MemoryBase(mPreviewBuffer, 0, size);
1096 if (frame == 0) {
1097 LOGE("failed to allocate space for frame callback");
1098 return;
1099 }
Dave Sparks93b94582009-05-07 19:27:32 -07001100 mCameraClient->dataCallback(CAMERA_MSG_PREVIEW_FRAME, frame);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001101}
1102
Dave Sparksf72d6402009-07-08 15:56:53 -07001103void CameraService::Client::postRecordingFrame(nsecs_t timestamp, const sp<IMemory>& frame)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001104{
1105 LOGV("postRecordingFrame");
1106 if (frame == 0) {
1107 LOGW("frame is a null pointer");
1108 return;
1109 }
Dave Sparksf72d6402009-07-08 15:56:53 -07001110 mCameraClient->dataCallbackTimestamp(timestamp, CAMERA_MSG_VIDEO_FRAME, frame);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001111}
1112
1113void CameraService::Client::postPreviewFrame(const sp<IMemory>& mem)
1114{
1115 LOGV("postPreviewFrame");
1116 if (mem == 0) {
1117 LOGW("mem is a null pointer");
1118 return;
1119 }
1120
1121 ssize_t offset;
1122 size_t size;
1123 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
Benny Wong71f77152009-07-15 18:44:27 -05001124 if ( !mUseOverlay )
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001125 {
1126 Mutex::Autolock surfaceLock(mSurfaceLock);
1127 if (mSurface != NULL) {
1128 mSurface->postBuffer(offset);
1129 }
1130 }
1131
1132 // Is the callback enabled or not?
1133 if (!(mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
1134 // If the enable bit is off, the copy-out and one-shot bits are ignored
1135 LOGV("frame callback is diabled");
1136 return;
1137 }
1138
1139 // Is the received frame copied out or not?
1140 if (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
1141 LOGV("frame is copied out");
1142 copyFrameAndPostCopiedFrame(heap, offset, size);
1143 } else {
1144 LOGV("frame is directly sent out without copying");
Dave Sparks93b94582009-05-07 19:27:32 -07001145 mCameraClient->dataCallback(CAMERA_MSG_PREVIEW_FRAME, mem);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001146 }
1147
1148 // Is this is one-shot only?
1149 if (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ONE_SHOT_MASK) {
1150 LOGV("One-shot only, thus clear the bits and disable frame callback");
1151 mPreviewCallbackFlag &= ~(FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
1152 FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
1153 FRAME_CALLBACK_FLAG_ENABLE_MASK);
1154 }
1155}
1156
1157void CameraService::Client::postError(status_t error)
1158{
Dave Sparks93b94582009-05-07 19:27:32 -07001159 mCameraClient->notifyCallback(CAMERA_MSG_ERROR, error, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001160}
1161
1162status_t CameraService::dump(int fd, const Vector<String16>& args)
1163{
1164 const size_t SIZE = 256;
1165 char buffer[SIZE];
1166 String8 result;
1167 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
1168 snprintf(buffer, SIZE, "Permission Denial: "
1169 "can't dump CameraService from pid=%d, uid=%d\n",
Chih-Chung Changd98c5162009-06-22 16:03:41 +08001170 getCallingPid(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001171 IPCThreadState::self()->getCallingUid());
1172 result.append(buffer);
1173 write(fd, result.string(), result.size());
1174 } else {
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +08001175 AutoMutex lock(&mServiceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001176 if (mClient != 0) {
1177 sp<Client> currentClient = mClient.promote();
1178 sprintf(buffer, "Client (%p) PID: %d\n",
1179 currentClient->getCameraClient()->asBinder().get(),
1180 currentClient->mClientPid);
1181 result.append(buffer);
1182 write(fd, result.string(), result.size());
1183 currentClient->mHardware->dump(fd, args);
1184 } else {
1185 result.append("No camera client yet.\n");
1186 write(fd, result.string(), result.size());
1187 }
1188 }
1189 return NO_ERROR;
1190}
1191
1192
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001193status_t CameraService::onTransact(
1194 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1195{
1196 // permission checks...
1197 switch (code) {
1198 case BnCameraService::CONNECT:
1199 IPCThreadState* ipc = IPCThreadState::self();
1200 const int pid = ipc->getCallingPid();
1201 const int self_pid = getpid();
1202 if (pid != self_pid) {
1203 // we're called from a different process, do the real check
1204 if (!checkCallingPermission(
1205 String16("android.permission.CAMERA")))
1206 {
1207 const int uid = ipc->getCallingUid();
1208 LOGE("Permission Denial: "
1209 "can't use the camera pid=%d, uid=%d", pid, uid);
1210 return PERMISSION_DENIED;
1211 }
1212 }
1213 break;
1214 }
1215
1216 status_t err = BnCameraService::onTransact(code, data, reply, flags);
1217
Dave Sparks998b3292009-05-20 20:02:59 -07001218#if DEBUG_HEAP_LEAKS
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001219 LOGD("+++ onTransact err %d code %d", err, code);
1220
1221 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
1222 // the 'service' command interrogates this binder for its name, and then supplies it
1223 // even for the debugging commands. that means we need to check for it here, using
1224 // ISurfaceComposer (since we delegated the INTERFACE_TRANSACTION handling to
1225 // BnSurfaceComposer before falling through to this code).
1226
1227 LOGD("+++ onTransact code %d", code);
1228
1229 CHECK_INTERFACE(ICameraService, data, reply);
1230
1231 switch(code) {
1232 case 1000:
1233 {
1234 if (gWeakHeap != 0) {
1235 sp<IMemoryHeap> h = gWeakHeap.promote();
1236 IMemoryHeap *p = gWeakHeap.unsafe_get();
1237 LOGD("CHECKING WEAK REFERENCE %p (%p)", h.get(), p);
1238 if (h != 0)
1239 h->printRefs();
1240 bool attempt_to_delete = data.readInt32() == 1;
1241 if (attempt_to_delete) {
1242 // NOT SAFE!
1243 LOGD("DELETING WEAK REFERENCE %p (%p)", h.get(), p);
1244 if (p) delete p;
1245 }
1246 return NO_ERROR;
1247 }
1248 }
1249 break;
1250 default:
1251 break;
1252 }
1253 }
Dave Sparks998b3292009-05-20 20:02:59 -07001254#endif // DEBUG_HEAP_LEAKS
Dave Sparksfec880d2009-05-21 09:18:18 -07001255
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001256 return err;
1257}
1258
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001259}; // namespace android