blob: 97b43a4fe25989d8bfa42e26bcdd6b96911cc487 [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#include <cutils/properties.h>
37
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038namespace android {
39
40extern "C" {
41#include <stdio.h>
42#include <sys/types.h>
43#include <sys/stat.h>
44#include <fcntl.h>
45#include <pthread.h>
Chih-Chung Changd98c5162009-06-22 16:03:41 +080046#include <signal.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047}
48
49// When you enable this, as well as DEBUG_REFS=1 and
50// DEBUG_REFS_ENABLED_BY_DEFAULT=0 in libutils/RefBase.cpp, this will track all
51// references to the CameraService::Client in order to catch the case where the
52// client is being destroyed while a callback from the CameraHardwareInterface
53// is outstanding. This is a serious bug because if we make another call into
54// CameraHardwreInterface that itself triggers a callback, we will deadlock.
55
56#define DEBUG_CLIENT_REFERENCES 0
57
58#define PICTURE_TIMEOUT seconds(5)
59
60#define DEBUG_DUMP_PREVIEW_FRAME_TO_FILE 0 /* n-th frame to write */
61#define DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE 0
62#define DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE 0
63
64#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
65static int debug_frame_cnt;
66#endif
67
Chih-Chung Changd98c5162009-06-22 16:03:41 +080068static int getCallingPid() {
69 return IPCThreadState::self()->getCallingPid();
70}
71
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072// ----------------------------------------------------------------------------
73
74void CameraService::instantiate() {
75 defaultServiceManager()->addService(
76 String16("media.camera"), new CameraService());
77}
78
79// ----------------------------------------------------------------------------
80
81CameraService::CameraService() :
82 BnCameraService()
83{
84 LOGI("CameraService started: pid=%d", getpid());
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +080085 mUsers = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086}
87
88CameraService::~CameraService()
89{
90 if (mClient != 0) {
91 LOGE("mClient was still connected in destructor!");
92 }
93}
94
95sp<ICamera> CameraService::connect(const sp<ICameraClient>& cameraClient)
96{
Chih-Chung Changd98c5162009-06-22 16:03:41 +080097 int callingPid = getCallingPid();
98 LOGD("CameraService::connect E (pid %d, client %p)", callingPid,
99 cameraClient->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800101 Mutex::Autolock lock(mServiceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102 sp<Client> client;
103 if (mClient != 0) {
104 sp<Client> currentClient = mClient.promote();
105 if (currentClient != 0) {
106 sp<ICameraClient> currentCameraClient(currentClient->getCameraClient());
107 if (cameraClient->asBinder() == currentCameraClient->asBinder()) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800108 // This is the same client reconnecting...
109 LOGD("CameraService::connect X (pid %d, same client %p) is reconnecting...",
110 callingPid, cameraClient->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 return currentClient;
112 } else {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800113 // It's another client... reject it
114 LOGD("CameraService::connect X (pid %d, new client %p) rejected. "
115 "(old pid %d, old client %p)",
116 callingPid, cameraClient->asBinder().get(),
117 currentClient->mClientPid, currentCameraClient->asBinder().get());
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800118 if (kill(currentClient->mClientPid, 0) == -1 && errno == ESRCH) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800119 LOGD("The old client is dead!");
120 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121 return client;
122 }
123 } else {
124 // can't promote, the previous client has died...
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800125 LOGD("New client (pid %d) connecting, old reference was dangling...",
126 callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127 mClient.clear();
128 }
129 }
130
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800131 if (mUsers > 0) {
132 LOGD("Still have client, rejected");
133 return client;
134 }
135
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 // create a new Client object
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800137 client = new Client(this, cameraClient, callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138 mClient = client;
139#if DEBUG_CLIENT_REFERENCES
140 // Enable tracking for this object, and track increments and decrements of
141 // the refcount.
142 client->trackMe(true, true);
143#endif
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800144 LOGD("CameraService::connect X");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800145 return client;
146}
147
148void CameraService::removeClient(const sp<ICameraClient>& cameraClient)
149{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800150 int callingPid = getCallingPid();
151
152 // Declare this outside the lock to make absolutely sure the
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153 // destructor won't be called with the lock held.
154 sp<Client> client;
155
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800156 Mutex::Autolock lock(mServiceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157
158 if (mClient == 0) {
159 // This happens when we have already disconnected.
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800160 LOGD("removeClient (pid %d): already disconnected", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161 return;
162 }
163
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800164 // Promote mClient. It can fail if we are called from this path:
165 // Client::~Client() -> disconnect() -> removeClient().
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 client = mClient.promote();
167 if (client == 0) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800168 LOGD("removeClient (pid %d): no more strong reference", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169 mClient.clear();
170 return;
171 }
172
173 if (cameraClient->asBinder() != client->getCameraClient()->asBinder()) {
174 // ugh! that's not our client!!
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800175 LOGW("removeClient (pid %d): mClient doesn't match!", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 } else {
177 // okay, good, forget about mClient
178 mClient.clear();
179 }
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800180
181 LOGD("removeClient (pid %d) done", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182}
183
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800184// The reason we need this count is a new CameraService::connect() request may
185// come in while the previous Client's destructor has not been run or is still
186// running. If the last strong reference of the previous Client is gone but
187// destructor has not been run, we should not allow the new Client to be created
188// because we need to wait for the previous Client to tear down the hardware
189// first.
190void CameraService::incUsers() {
191 android_atomic_inc(&mUsers);
192}
193
194void CameraService::decUsers() {
195 android_atomic_dec(&mUsers);
196}
197
Jason Sams78b877e2009-03-24 20:21:36 -0700198static sp<MediaPlayer> newMediaPlayer(const char *file)
199{
200 sp<MediaPlayer> mp = new MediaPlayer();
201 if (mp->setDataSource(file) == NO_ERROR) {
Eric Laurentcbcb00e2009-03-27 16:27:16 -0700202 char value[PROPERTY_VALUE_MAX];
203 property_get("ro.camera.sound.forced", value, "0");
204 if (atoi(value)) {
205 mp->setAudioStreamType(AudioSystem::ENFORCED_AUDIBLE);
206 } else {
207 mp->setAudioStreamType(AudioSystem::SYSTEM);
208 }
Jason Sams78b877e2009-03-24 20:21:36 -0700209 mp->prepare();
210 } else {
211 mp.clear();
212 LOGE("Failed to load CameraService sounds.");
213 }
214 return mp;
215}
216
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217CameraService::Client::Client(const sp<CameraService>& cameraService,
218 const sp<ICameraClient>& cameraClient, pid_t clientPid)
219{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800220 int callingPid = getCallingPid();
221 LOGD("Client::Client E (pid %d)", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222 mCameraService = cameraService;
223 mCameraClient = cameraClient;
224 mClientPid = clientPid;
225 mHardware = openCameraHardware();
226 mUseOverlay = mHardware->useOverlay();
227
Jason Sams78b877e2009-03-24 20:21:36 -0700228 mMediaPlayerClick = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
229 mMediaPlayerBeep = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
Benny Wong71f77152009-07-15 18:44:27 -0500230 mOverlayW = 0;
231 mOverlayH = 0;
Jason Sams78b877e2009-03-24 20:21:36 -0700232
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233 // Callback is disabled by default
234 mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800235 cameraService->incUsers();
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800236 LOGD("Client::Client X (pid %d)", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237}
238
239status_t CameraService::Client::checkPid()
240{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800241 int callingPid = getCallingPid();
242 if (mClientPid == callingPid) return NO_ERROR;
243 LOGW("Attempt to use locked camera (client %p) from different process "
244 " (old pid %d, new pid %d)",
245 getCameraClient()->asBinder().get(), mClientPid, callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800246 return -EBUSY;
247}
248
249status_t CameraService::Client::lock()
250{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800251 int callingPid = getCallingPid();
252 LOGD("lock from pid %d (mClientPid %d)", callingPid, mClientPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253 Mutex::Autolock _l(mLock);
254 // lock camera to this client if the the camera is unlocked
255 if (mClientPid == 0) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800256 mClientPid = callingPid;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257 return NO_ERROR;
258 }
259 // returns NO_ERROR if the client already owns the camera, -EBUSY otherwise
260 return checkPid();
261}
262
263status_t CameraService::Client::unlock()
264{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800265 int callingPid = getCallingPid();
266 LOGD("unlock from pid %d (mClientPid %d)", callingPid, mClientPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267 Mutex::Autolock _l(mLock);
268 // allow anyone to use camera
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269 status_t result = checkPid();
James Dong0ae13e32009-04-20 19:35:28 -0700270 if (result == NO_ERROR) {
271 mClientPid = 0;
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800272 LOGD("clear mCameraClient (pid %d)", callingPid);
James Dong0ae13e32009-04-20 19:35:28 -0700273 // we need to remove the reference so that when app goes
274 // away, the reference count goes to 0.
275 mCameraClient.clear();
276 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800277 return result;
278}
279
280status_t CameraService::Client::connect(const sp<ICameraClient>& client)
281{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800282 int callingPid = getCallingPid();
283
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284 // connect a new process to the camera
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800285 LOGD("Client::connect E (pid %d, client %p)", callingPid, client->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800286
287 // I hate this hack, but things get really ugly when the media recorder
288 // service is handing back the camera to the app. The ICameraClient
289 // destructor will be called during the same IPC, making it look like
290 // the remote client is trying to disconnect. This hack temporarily
291 // sets the mClientPid to an invalid pid to prevent the hardware from
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800292 // being torn down.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293 {
294
295 // hold a reference to the old client or we will deadlock if the client is
296 // in the same process and we hold the lock when we remove the reference
297 sp<ICameraClient> oldClient;
298 {
299 Mutex::Autolock _l(mLock);
Chih-Chung Chang34e5a152009-06-09 13:56:44 +0800300 if (mClientPid != 0 && checkPid() != NO_ERROR) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800301 LOGW("Tried to connect to locked camera (old pid %d, new pid %d)",
302 mClientPid, callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303 return -EBUSY;
304 }
305 oldClient = mCameraClient;
306
307 // did the client actually change?
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800308 if (client->asBinder() == mCameraClient->asBinder()) {
309 LOGD("Connect to the same client");
310 return NO_ERROR;
311 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312
313 mCameraClient = client;
314 mClientPid = -1;
315 mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800316 LOGD("Connect to the new client (pid %d, client %p)",
317 callingPid, mCameraClient->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318 }
319
320 }
321 // the old client destructor is called when oldClient goes out of scope
322 // now we set the new PID to lock the interface again
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800323 mClientPid = callingPid;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800324
325 return NO_ERROR;
326}
327
328#if HAVE_ANDROID_OS
329static void *unregister_surface(void *arg)
330{
331 ISurface *surface = (ISurface *)arg;
332 surface->unregisterBuffers();
333 IPCThreadState::self()->flushCommands();
334 return NULL;
335}
336#endif
337
338CameraService::Client::~Client()
339{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800340 int callingPid = getCallingPid();
341
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800342 // tear down client
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800343 LOGD("Client::~Client E (pid %d, client %p)",
344 callingPid, getCameraClient()->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345 if (mSurface != 0 && !mUseOverlay) {
346#if HAVE_ANDROID_OS
347 pthread_t thr;
348 // We unregister the buffers in a different thread because binder does
349 // not let us make sychronous transactions in a binder destructor (that
350 // is, upon our reaching a refcount of zero.)
351 pthread_create(&thr, NULL,
352 unregister_surface,
353 mSurface.get());
354 pthread_join(thr, NULL);
355#else
356 mSurface->unregisterBuffers();
357#endif
358 }
359
Jason Sams9e431ac2009-03-24 20:36:57 -0700360 if (mMediaPlayerBeep.get() != NULL) {
361 mMediaPlayerBeep->disconnect();
362 mMediaPlayerBeep.clear();
363 }
364 if (mMediaPlayerClick.get() != NULL) {
365 mMediaPlayerClick->disconnect();
366 mMediaPlayerClick.clear();
367 }
Jason Sams78b877e2009-03-24 20:21:36 -0700368
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800369 // make sure we tear down the hardware
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800370 mClientPid = callingPid;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800371 disconnect();
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800372 LOGD("Client::~Client X (pid %d)", mClientPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800373}
374
375void CameraService::Client::disconnect()
376{
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800377 int callingPid = getCallingPid();
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800378
379 LOGD("Client::disconnect() E (pid %d client %p)",
380 callingPid, getCameraClient()->asBinder().get());
381
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800382 Mutex::Autolock lock(mLock);
383 if (mClientPid <= 0) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800384 LOGD("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800385 return;
386 }
387 if (checkPid() != NO_ERROR) {
Chih-Chung Chang6a5297d2009-06-16 17:15:04 +0800388 LOGD("Different client - don't disconnect");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800389 return;
390 }
391
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800392 // Make sure disconnect() is done once and once only, whether it is called
393 // from the user directly, or called by the destructor.
394 if (mHardware == 0) return;
395
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800396 LOGD("hardware teardown");
397 // Before destroying mHardware, we must make sure it's in the
398 // idle state.
399 mHardware->stopPreview();
400 // Cancel all picture callbacks.
401 mHardware->cancelPicture(true, true, true);
402 // Release the hardware resources.
403 mHardware->release();
Benny Wong71f77152009-07-15 18:44:27 -0500404 // Release the held overlay resources.
405 if (mUseOverlay)
406 {
407 mOverlayRef = 0;
408 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800409 mHardware.clear();
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800410
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800411 mCameraService->removeClient(mCameraClient);
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800412 mCameraService->decUsers();
413
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800414 LOGD("Client::disconnect() X (pid %d)", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800415}
416
417// pass the buffered ISurface to the camera service
418status_t CameraService::Client::setPreviewDisplay(const sp<ISurface>& surface)
419{
Wu-cheng Li988fb622009-06-23 23:37:36 +0800420 LOGD("setPreviewDisplay(%p) (pid %d)",
421 ((surface == NULL) ? NULL : surface.get()), getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800422 Mutex::Autolock lock(mLock);
423 status_t result = checkPid();
424 if (result != NO_ERROR) return result;
Wu-cheng Li988fb622009-06-23 23:37:36 +0800425
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800426 Mutex::Autolock surfaceLock(mSurfaceLock);
Wu-cheng Li988fb622009-06-23 23:37:36 +0800427 result = NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428 // asBinder() is safe on NULL (returns NULL)
429 if (surface->asBinder() != mSurface->asBinder()) {
Benny Wong71f77152009-07-15 18:44:27 -0500430 if (mSurface != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800431 LOGD("clearing old preview surface %p", mSurface.get());
Benny Wong71f77152009-07-15 18:44:27 -0500432 if ( !mUseOverlay)
433 {
434 mSurface->unregisterBuffers();
435 }
436 else
437 {
438 // Force the destruction of any previous overlay
439 sp<Overlay> dummy;
440 mHardware->setOverlay( dummy );
441 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800442 }
443 mSurface = surface;
Benny Wong71f77152009-07-15 18:44:27 -0500444 mOverlayRef = 0;
Wu-cheng Li988fb622009-06-23 23:37:36 +0800445 // If preview has been already started, set overlay or register preview
446 // buffers now.
447 if (mHardware->previewEnabled()) {
448 if (mUseOverlay) {
449 result = setOverlay();
450 } else if (mSurface != 0) {
451 result = registerPreviewBuffers();
452 }
453 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800454 }
Wu-cheng Li988fb622009-06-23 23:37:36 +0800455 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800456}
457
458// set the preview callback flag to affect how the received frames from
459// preview are handled.
460void CameraService::Client::setPreviewCallbackFlag(int callback_flag)
461{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800462 LOGV("setPreviewCallbackFlag (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800463 Mutex::Autolock lock(mLock);
464 if (checkPid() != NO_ERROR) return;
465 mPreviewCallbackFlag = callback_flag;
466}
467
Wu-cheng Li988fb622009-06-23 23:37:36 +0800468// start preview mode
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800469status_t CameraService::Client::startCameraMode(camera_mode mode)
470{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800471 int callingPid = getCallingPid();
472
473 LOGD("startCameraMode(%d) (pid %d)", mode, callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800474
475 /* we cannot call into mHardware with mLock held because
476 * mHardware has callbacks onto us which acquire this lock
477 */
478
479 Mutex::Autolock lock(mLock);
480 status_t result = checkPid();
481 if (result != NO_ERROR) return result;
482
483 if (mHardware == 0) {
484 LOGE("mHardware is NULL, returning.");
485 return INVALID_OPERATION;
486 }
487
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800488 switch(mode) {
489 case CAMERA_RECORDING_MODE:
Wu-cheng Li988fb622009-06-23 23:37:36 +0800490 if (mSurface == 0) {
491 LOGE("setPreviewDisplay must be called before startRecordingMode.");
492 return INVALID_OPERATION;
493 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800494 return startRecordingMode();
495
496 default: // CAMERA_PREVIEW_MODE
Wu-cheng Li988fb622009-06-23 23:37:36 +0800497 if (mSurface == 0) {
498 LOGD("mSurface is not set yet.");
499 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800500 return startPreviewMode();
501 }
502}
503
504status_t CameraService::Client::startRecordingMode()
505{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800506 LOGD("startRecordingMode (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800507
508 status_t ret = UNKNOWN_ERROR;
509
510 // if preview has not been started, start preview first
511 if (!mHardware->previewEnabled()) {
512 ret = startPreviewMode();
513 if (ret != NO_ERROR) {
514 return ret;
515 }
516 }
517
518 // if recording has been enabled, nothing needs to be done
519 if (mHardware->recordingEnabled()) {
520 return NO_ERROR;
521 }
522
523 // start recording mode
Dave Sparksf72d6402009-07-08 15:56:53 -0700524 ret = mHardware->startRecording(recordingCallback, mCameraService.get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800525 if (ret != NO_ERROR) {
526 LOGE("mHardware->startRecording() failed with status %d", ret);
527 }
528 return ret;
529}
530
Wu-cheng Li988fb622009-06-23 23:37:36 +0800531status_t CameraService::Client::setOverlay()
532{
533 LOGD("setOverlay");
534 int w, h;
535 CameraParameters params(mHardware->getParameters());
536 params.getPreviewSize(&w, &h);
537
538 const char *format = params.getPreviewFormat();
539 int fmt;
Benny Wong71f77152009-07-15 18:44:27 -0500540 if (!strcmp(format, "yuv422i-yuyv"))
541 fmt = OVERLAY_FORMAT_YCbYCr_422_I;
Wu-cheng Li988fb622009-06-23 23:37:36 +0800542 else if (!strcmp(format, "rgb565"))
543 fmt = OVERLAY_FORMAT_RGB_565;
544 else {
545 LOGE("Invalid preview format for overlays");
546 return -EINVAL;
547 }
548
Benny Wong71f77152009-07-15 18:44:27 -0500549 if ( w != mOverlayW || h != mOverlayH )
550 {
551 // Force the destruction of any previous overlay
552 sp<Overlay> dummy;
553 mHardware->setOverlay( dummy );
554 mOverlayRef = 0;
555 }
556
Wu-cheng Li988fb622009-06-23 23:37:36 +0800557 status_t ret = NO_ERROR;
558 if (mSurface != 0) {
Benny Wong71f77152009-07-15 18:44:27 -0500559 if (mOverlayRef.get() == NULL) {
560 mOverlayRef = mSurface->createOverlay(w, h, fmt);
561 if ( mOverlayRef.get() == NULL )
562 {
563 LOGE("Overlay Creation Failed!");
564 return -EINVAL;
565 }
566 ret = mHardware->setOverlay(new Overlay(mOverlayRef));
567 }
Wu-cheng Li988fb622009-06-23 23:37:36 +0800568 } else {
569 ret = mHardware->setOverlay(NULL);
570 }
571 if (ret != NO_ERROR) {
572 LOGE("mHardware->setOverlay() failed with status %d\n", ret);
573 }
Benny Wong71f77152009-07-15 18:44:27 -0500574
575 mOverlayW = w;
576 mOverlayH = h;
577
Wu-cheng Li988fb622009-06-23 23:37:36 +0800578 return ret;
579}
580
581status_t CameraService::Client::registerPreviewBuffers()
582{
583 int w, h;
584 CameraParameters params(mHardware->getParameters());
585 params.getPreviewSize(&w, &h);
586
587 uint32_t transform = 0;
588 if (params.getOrientation() ==
589 CameraParameters::CAMERA_ORIENTATION_PORTRAIT) {
590 LOGV("portrait mode");
591 transform = ISurface::BufferHeap::ROT_90;
592 }
593 ISurface::BufferHeap buffers(w, h, w, h,
594 PIXEL_FORMAT_YCbCr_420_SP,
595 transform,
596 0,
597 mHardware->getPreviewHeap());
598
599 status_t ret = mSurface->registerBuffers(buffers);
600 if (ret != NO_ERROR) {
601 LOGE("registerBuffers failed with status %d", ret);
602 }
603 return ret;
604}
605
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800606status_t CameraService::Client::startPreviewMode()
607{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800608 LOGD("startPreviewMode (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800609
610 // if preview has been enabled, nothing needs to be done
611 if (mHardware->previewEnabled()) {
612 return NO_ERROR;
613 }
614
615 // start preview mode
616#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
617 debug_frame_cnt = 0;
618#endif
Wu-cheng Li988fb622009-06-23 23:37:36 +0800619 status_t ret = NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800620
621 if (mUseOverlay) {
Wu-cheng Li988fb622009-06-23 23:37:36 +0800622 // If preview display has been set, set overlay now.
623 if (mSurface != 0) {
624 ret = setOverlay();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800625 }
Wu-cheng Li988fb622009-06-23 23:37:36 +0800626 if (ret != NO_ERROR) return ret;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800627 ret = mHardware->startPreview(NULL, mCameraService.get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628 } else {
629 ret = mHardware->startPreview(previewCallback,
630 mCameraService.get());
Wu-cheng Li988fb622009-06-23 23:37:36 +0800631 if (ret != NO_ERROR) return ret;
632 // If preview display has been set, register preview buffers now.
633 if (mSurface != 0) {
634 // Unregister here because the surface registered with raw heap.
635 mSurface->unregisterBuffers();
636 ret = registerPreviewBuffers();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800637 }
638 }
639 return ret;
640}
641
642status_t CameraService::Client::startPreview()
643{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800644 LOGD("startPreview (pid %d)", getCallingPid());
645
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800646 return startCameraMode(CAMERA_PREVIEW_MODE);
647}
648
649status_t CameraService::Client::startRecording()
650{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800651 LOGD("startRecording (pid %d)", getCallingPid());
652
Jason Sams78b877e2009-03-24 20:21:36 -0700653 if (mMediaPlayerBeep.get() != NULL) {
Andreas Hubera26e6062009-03-24 20:47:19 -0700654 mMediaPlayerBeep->seekTo(0);
Jason Sams78b877e2009-03-24 20:21:36 -0700655 mMediaPlayerBeep->start();
656 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800657 return startCameraMode(CAMERA_RECORDING_MODE);
658}
659
660// stop preview mode
661void CameraService::Client::stopPreview()
662{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800663 LOGD("stopPreview (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800664
665 Mutex::Autolock lock(mLock);
666 if (checkPid() != NO_ERROR) return;
667
668 if (mHardware == 0) {
669 LOGE("mHardware is NULL, returning.");
670 return;
671 }
672
673 mHardware->stopPreview();
674 LOGD("stopPreview(), hardware stopped OK");
675
676 if (mSurface != 0 && !mUseOverlay) {
677 mSurface->unregisterBuffers();
678 }
679 mPreviewBuffer.clear();
680}
681
682// stop recording mode
683void CameraService::Client::stopRecording()
684{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800685 LOGD("stopRecording (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800686
687 Mutex::Autolock lock(mLock);
688 if (checkPid() != NO_ERROR) return;
689
690 if (mHardware == 0) {
691 LOGE("mHardware is NULL, returning.");
692 return;
693 }
694
Jason Sams78b877e2009-03-24 20:21:36 -0700695 if (mMediaPlayerBeep.get() != NULL) {
Andreas Hubera26e6062009-03-24 20:47:19 -0700696 mMediaPlayerBeep->seekTo(0);
Jason Sams78b877e2009-03-24 20:21:36 -0700697 mMediaPlayerBeep->start();
698 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800699 mHardware->stopRecording();
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800700 LOGD("stopRecording(), hardware stopped OK");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800701 mPreviewBuffer.clear();
702}
703
704// release a recording frame
705void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem)
706{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800707 Mutex::Autolock lock(mLock);
708 if (checkPid() != NO_ERROR) return;
709
710 if (mHardware == 0) {
711 LOGE("mHardware is NULL, returning.");
712 return;
713 }
714
715 mHardware->releaseRecordingFrame(mem);
716}
717
718bool CameraService::Client::previewEnabled()
719{
720 Mutex::Autolock lock(mLock);
721 if (mHardware == 0) return false;
722 return mHardware->previewEnabled();
723}
724
725bool CameraService::Client::recordingEnabled()
726{
727 Mutex::Autolock lock(mLock);
728 if (mHardware == 0) return false;
729 return mHardware->recordingEnabled();
730}
731
732// Safely retrieves a strong pointer to the client during a hardware callback.
733sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user)
734{
735 sp<Client> client = 0;
736 CameraService *service = static_cast<CameraService*>(user);
737 if (service != NULL) {
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800738 Mutex::Autolock ourLock(service->mServiceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800739 if (service->mClient != 0) {
740 client = service->mClient.promote();
741 if (client == 0) {
742 LOGE("getClientFromCookie: client appears to have died");
743 service->mClient.clear();
744 }
745 } else {
746 LOGE("getClientFromCookie: got callback but client was NULL");
747 }
748 }
749 return client;
750}
751
752
753#if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE || \
754 DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE || \
755 DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
756static void dump_to_file(const char *fname,
757 uint8_t *buf, uint32_t size)
758{
759 int nw, cnt = 0;
760 uint32_t written = 0;
761
762 LOGD("opening file [%s]\n", fname);
763 int fd = open(fname, O_RDWR | O_CREAT);
764 if (fd < 0) {
765 LOGE("failed to create file [%s]: %s", fname, strerror(errno));
766 return;
767 }
768
769 LOGD("writing %d bytes to file [%s]\n", size, fname);
770 while (written < size) {
771 nw = ::write(fd,
772 buf + written,
773 size - written);
774 if (nw < 0) {
775 LOGE("failed to write to file [%s]: %s",
776 fname, strerror(errno));
777 break;
778 }
779 written += nw;
780 cnt++;
781 }
782 LOGD("done writing %d bytes to file [%s] in %d passes\n",
783 size, fname, cnt);
784 ::close(fd);
785}
786#endif
787
788// preview callback - frame buffer update
789void CameraService::Client::previewCallback(const sp<IMemory>& mem, void* user)
790{
791 LOGV("previewCallback()");
792 sp<Client> client = getClientFromCookie(user);
793 if (client == 0) {
794 return;
795 }
796
797#if DEBUG_HEAP_LEAKS && 0 // debugging
798 if (gWeakHeap == NULL) {
799 ssize_t offset;
800 size_t size;
801 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
802 if (gWeakHeap != heap) {
803 LOGD("SETTING PREVIEW HEAP");
804 heap->trackMe(true, true);
805 gWeakHeap = heap;
806 }
807 }
808#endif
809
810#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
811 {
812 if (debug_frame_cnt++ == DEBUG_DUMP_PREVIEW_FRAME_TO_FILE) {
813 ssize_t offset;
814 size_t size;
815 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
816 dump_to_file("/data/preview.yuv",
817 (uint8_t *)heap->base() + offset, size);
818 }
819 }
820#endif
821
822 // The strong pointer guarantees the client will exist, but no lock is held.
823 client->postPreviewFrame(mem);
824
825#if DEBUG_CLIENT_REFERENCES
826 //**** if the client's refcount is 1, then we are about to destroy it here,
827 // which is bad--print all refcounts.
828 if (client->getStrongCount() == 1) {
829 LOGE("++++++++++++++++ (PREVIEW) THIS WILL CAUSE A LOCKUP!");
830 client->printRefs();
831 }
832#endif
833}
834
835// recording callback
Dave Sparksf72d6402009-07-08 15:56:53 -0700836void CameraService::Client::recordingCallback(nsecs_t timestamp, const sp<IMemory>& mem, void* user)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800837{
838 LOGV("recordingCallback");
839 sp<Client> client = getClientFromCookie(user);
840 if (client == 0) {
841 return;
842 }
843 // The strong pointer guarantees the client will exist, but no lock is held.
Dave Sparksf72d6402009-07-08 15:56:53 -0700844 client->postRecordingFrame(timestamp, mem);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800845}
846
847// take a picture - image is returned in callback
848status_t CameraService::Client::autoFocus()
849{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800850 LOGD("autoFocus (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800851
852 Mutex::Autolock lock(mLock);
853 status_t result = checkPid();
854 if (result != NO_ERROR) return result;
855
856 if (mHardware == 0) {
857 LOGE("mHardware is NULL, returning.");
858 return INVALID_OPERATION;
859 }
860
861 return mHardware->autoFocus(autoFocusCallback,
862 mCameraService.get());
863}
864
865// take a picture - image is returned in callback
866status_t CameraService::Client::takePicture()
867{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800868 LOGD("takePicture (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800869
870 Mutex::Autolock lock(mLock);
871 status_t result = checkPid();
872 if (result != NO_ERROR) return result;
873
874 if (mHardware == 0) {
875 LOGE("mHardware is NULL, returning.");
876 return INVALID_OPERATION;
877 }
878
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800879 return mHardware->takePicture(shutterCallback,
880 yuvPictureCallback,
881 jpegPictureCallback,
882 mCameraService.get());
883}
884
885// picture callback - snapshot taken
886void CameraService::Client::shutterCallback(void *user)
887{
888 sp<Client> client = getClientFromCookie(user);
889 if (client == 0) {
890 return;
891 }
892
Wu-cheng Liaab44b82009-03-24 20:39:09 -0700893 // Play shutter sound.
894 if (client->mMediaPlayerClick.get() != NULL) {
Andreas Hubera26e6062009-03-24 20:47:19 -0700895 client->mMediaPlayerClick->seekTo(0);
Wu-cheng Liaab44b82009-03-24 20:39:09 -0700896 client->mMediaPlayerClick->start();
897 }
898
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800899 // Screen goes black after the buffer is unregistered.
900 if (client->mSurface != 0 && !client->mUseOverlay) {
901 client->mSurface->unregisterBuffers();
902 }
903
904 client->postShutter();
905
906 // It takes some time before yuvPicture callback to be called.
907 // Register the buffer for raw image here to reduce latency.
908 if (client->mSurface != 0 && !client->mUseOverlay) {
909 int w, h;
910 CameraParameters params(client->mHardware->getParameters());
911 params.getPictureSize(&w, &h);
912 uint32_t transform = 0;
913 if (params.getOrientation() == CameraParameters::CAMERA_ORIENTATION_PORTRAIT) {
914 LOGV("portrait mode");
915 transform = ISurface::BufferHeap::ROT_90;
916 }
917 ISurface::BufferHeap buffers(w, h, w, h,
918 PIXEL_FORMAT_YCbCr_420_SP, transform, 0, client->mHardware->getRawHeap());
919
920 client->mSurface->registerBuffers(buffers);
921 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800922}
923
924// picture callback - raw image ready
925void CameraService::Client::yuvPictureCallback(const sp<IMemory>& mem,
926 void *user)
927{
928 sp<Client> client = getClientFromCookie(user);
929 if (client == 0) {
930 return;
931 }
932 if (mem == NULL) {
933 client->postRaw(NULL);
934 client->postError(UNKNOWN_ERROR);
935 return;
936 }
937
938 ssize_t offset;
939 size_t size;
940 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
941#if DEBUG_HEAP_LEAKS && 0 // debugging
942 gWeakHeap = heap; // debugging
943#endif
944
945 //LOGV("yuvPictureCallback(%d, %d, %p)", offset, size, user);
946#if DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE // for testing pursposes only
947 dump_to_file("/data/photo.yuv",
948 (uint8_t *)heap->base() + offset, size);
949#endif
950
951 // Put the YUV version of the snapshot in the preview display.
952 if (client->mSurface != 0 && !client->mUseOverlay) {
953 client->mSurface->postBuffer(offset);
954 }
955
956 client->postRaw(mem);
957
958#if DEBUG_CLIENT_REFERENCES
959 //**** if the client's refcount is 1, then we are about to destroy it here,
960 // which is bad--print all refcounts.
961 if (client->getStrongCount() == 1) {
962 LOGE("++++++++++++++++ (RAW) THIS WILL CAUSE A LOCKUP!");
963 client->printRefs();
964 }
965#endif
966}
967
968// picture callback - jpeg ready
969void CameraService::Client::jpegPictureCallback(const sp<IMemory>& mem, void *user)
970{
971 sp<Client> client = getClientFromCookie(user);
972 if (client == 0) {
973 return;
974 }
975 if (mem == NULL) {
976 client->postJpeg(NULL);
977 client->postError(UNKNOWN_ERROR);
978 return;
979 }
980
981 /** We absolutely CANNOT call into user code with a lock held **/
982
983#if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE // for testing pursposes only
984 {
985 ssize_t offset;
986 size_t size;
987 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
988 dump_to_file("/data/photo.jpg",
989 (uint8_t *)heap->base() + offset, size);
990 }
991#endif
992
993 client->postJpeg(mem);
994
995#if DEBUG_CLIENT_REFERENCES
996 //**** if the client's refcount is 1, then we are about to destroy it here,
997 // which is bad--print all refcounts.
998 if (client->getStrongCount() == 1) {
999 LOGE("++++++++++++++++ (JPEG) THIS WILL CAUSE A LOCKUP!");
1000 client->printRefs();
1001 }
1002#endif
1003}
1004
1005void CameraService::Client::autoFocusCallback(bool focused, void *user)
1006{
1007 LOGV("autoFocusCallback");
1008
1009 sp<Client> client = getClientFromCookie(user);
1010 if (client == 0) {
1011 return;
1012 }
1013
1014 client->postAutoFocus(focused);
1015
1016#if DEBUG_CLIENT_REFERENCES
1017 if (client->getStrongCount() == 1) {
1018 LOGE("++++++++++++++++ (AUTOFOCUS) THIS WILL CAUSE A LOCKUP!");
1019 client->printRefs();
1020 }
1021#endif
1022}
1023
1024// set preview/capture parameters - key/value pairs
1025status_t CameraService::Client::setParameters(const String8& params)
1026{
1027 LOGD("setParameters(%s)", params.string());
1028
1029 Mutex::Autolock lock(mLock);
1030 status_t result = checkPid();
1031 if (result != NO_ERROR) return result;
1032
1033 if (mHardware == 0) {
1034 LOGE("mHardware is NULL, returning.");
1035 return INVALID_OPERATION;
1036 }
1037
1038 CameraParameters p(params);
1039 mHardware->setParameters(p);
1040 return NO_ERROR;
1041}
1042
1043// get preview/capture parameters - key/value pairs
1044String8 CameraService::Client::getParameters() const
1045{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001046 Mutex::Autolock lock(mLock);
1047
1048 if (mHardware == 0) {
1049 LOGE("mHardware is NULL, returning.");
1050 return String8();
1051 }
1052
Wu-cheng Li81d763f2009-04-22 16:21:26 +08001053 String8 params(mHardware->getParameters().flatten());
1054 LOGD("getParameters(%s)", params.string());
1055 return params;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001056}
1057
1058void CameraService::Client::postAutoFocus(bool focused)
1059{
1060 LOGV("postAutoFocus");
Dave Sparks93b94582009-05-07 19:27:32 -07001061 mCameraClient->notifyCallback(CAMERA_MSG_FOCUS, (int32_t)focused, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062}
1063
1064void CameraService::Client::postShutter()
1065{
Chih-Chung Changd98c5162009-06-22 16:03:41 +08001066 LOGD("postShutter");
Dave Sparks93b94582009-05-07 19:27:32 -07001067 mCameraClient->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001068}
1069
1070void CameraService::Client::postRaw(const sp<IMemory>& mem)
1071{
1072 LOGD("postRaw");
Dave Sparks93b94582009-05-07 19:27:32 -07001073 mCameraClient->dataCallback(CAMERA_MSG_RAW_IMAGE, mem);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001074}
1075
1076void CameraService::Client::postJpeg(const sp<IMemory>& mem)
1077{
1078 LOGD("postJpeg");
Dave Sparks93b94582009-05-07 19:27:32 -07001079 mCameraClient->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001080}
1081
1082void CameraService::Client::copyFrameAndPostCopiedFrame(sp<IMemoryHeap> heap, size_t offset, size_t size)
1083{
1084 LOGV("copyFrameAndPostCopiedFrame");
1085 // It is necessary to copy out of pmem before sending this to
1086 // the callback. For efficiency, reuse the same MemoryHeapBase
1087 // provided it's big enough. Don't allocate the memory or
1088 // perform the copy if there's no callback.
1089 if (mPreviewBuffer == 0) {
1090 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1091 } else if (size > mPreviewBuffer->virtualSize()) {
1092 mPreviewBuffer.clear();
1093 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1094 if (mPreviewBuffer == 0) {
1095 LOGE("failed to allocate space for preview buffer");
1096 return;
1097 }
1098 }
1099 memcpy(mPreviewBuffer->base(),
1100 (uint8_t *)heap->base() + offset, size);
1101
1102 sp<MemoryBase> frame = new MemoryBase(mPreviewBuffer, 0, size);
1103 if (frame == 0) {
1104 LOGE("failed to allocate space for frame callback");
1105 return;
1106 }
Dave Sparks93b94582009-05-07 19:27:32 -07001107 mCameraClient->dataCallback(CAMERA_MSG_PREVIEW_FRAME, frame);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001108}
1109
Dave Sparksf72d6402009-07-08 15:56:53 -07001110void CameraService::Client::postRecordingFrame(nsecs_t timestamp, const sp<IMemory>& frame)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001111{
1112 LOGV("postRecordingFrame");
1113 if (frame == 0) {
1114 LOGW("frame is a null pointer");
1115 return;
1116 }
Dave Sparksf72d6402009-07-08 15:56:53 -07001117 mCameraClient->dataCallbackTimestamp(timestamp, CAMERA_MSG_VIDEO_FRAME, frame);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001118}
1119
1120void CameraService::Client::postPreviewFrame(const sp<IMemory>& mem)
1121{
1122 LOGV("postPreviewFrame");
1123 if (mem == 0) {
1124 LOGW("mem is a null pointer");
1125 return;
1126 }
1127
1128 ssize_t offset;
1129 size_t size;
1130 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
Benny Wong71f77152009-07-15 18:44:27 -05001131 if ( !mUseOverlay )
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001132 {
1133 Mutex::Autolock surfaceLock(mSurfaceLock);
1134 if (mSurface != NULL) {
1135 mSurface->postBuffer(offset);
1136 }
1137 }
1138
1139 // Is the callback enabled or not?
1140 if (!(mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
1141 // If the enable bit is off, the copy-out and one-shot bits are ignored
1142 LOGV("frame callback is diabled");
1143 return;
1144 }
1145
1146 // Is the received frame copied out or not?
1147 if (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
1148 LOGV("frame is copied out");
1149 copyFrameAndPostCopiedFrame(heap, offset, size);
1150 } else {
1151 LOGV("frame is directly sent out without copying");
Dave Sparks93b94582009-05-07 19:27:32 -07001152 mCameraClient->dataCallback(CAMERA_MSG_PREVIEW_FRAME, mem);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001153 }
1154
1155 // Is this is one-shot only?
1156 if (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ONE_SHOT_MASK) {
1157 LOGV("One-shot only, thus clear the bits and disable frame callback");
1158 mPreviewCallbackFlag &= ~(FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
1159 FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
1160 FRAME_CALLBACK_FLAG_ENABLE_MASK);
1161 }
1162}
1163
1164void CameraService::Client::postError(status_t error)
1165{
Dave Sparks93b94582009-05-07 19:27:32 -07001166 mCameraClient->notifyCallback(CAMERA_MSG_ERROR, error, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001167}
1168
1169status_t CameraService::dump(int fd, const Vector<String16>& args)
1170{
1171 const size_t SIZE = 256;
1172 char buffer[SIZE];
1173 String8 result;
1174 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
1175 snprintf(buffer, SIZE, "Permission Denial: "
1176 "can't dump CameraService from pid=%d, uid=%d\n",
Chih-Chung Changd98c5162009-06-22 16:03:41 +08001177 getCallingPid(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001178 IPCThreadState::self()->getCallingUid());
1179 result.append(buffer);
1180 write(fd, result.string(), result.size());
1181 } else {
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +08001182 AutoMutex lock(&mServiceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001183 if (mClient != 0) {
1184 sp<Client> currentClient = mClient.promote();
1185 sprintf(buffer, "Client (%p) PID: %d\n",
1186 currentClient->getCameraClient()->asBinder().get(),
1187 currentClient->mClientPid);
1188 result.append(buffer);
1189 write(fd, result.string(), result.size());
1190 currentClient->mHardware->dump(fd, args);
1191 } else {
1192 result.append("No camera client yet.\n");
1193 write(fd, result.string(), result.size());
1194 }
1195 }
1196 return NO_ERROR;
1197}
1198
1199
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001200status_t CameraService::onTransact(
1201 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1202{
1203 // permission checks...
1204 switch (code) {
1205 case BnCameraService::CONNECT:
1206 IPCThreadState* ipc = IPCThreadState::self();
1207 const int pid = ipc->getCallingPid();
1208 const int self_pid = getpid();
1209 if (pid != self_pid) {
1210 // we're called from a different process, do the real check
1211 if (!checkCallingPermission(
1212 String16("android.permission.CAMERA")))
1213 {
1214 const int uid = ipc->getCallingUid();
1215 LOGE("Permission Denial: "
1216 "can't use the camera pid=%d, uid=%d", pid, uid);
1217 return PERMISSION_DENIED;
1218 }
1219 }
1220 break;
1221 }
1222
1223 status_t err = BnCameraService::onTransact(code, data, reply, flags);
1224
Dave Sparks998b3292009-05-20 20:02:59 -07001225#if DEBUG_HEAP_LEAKS
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001226 LOGD("+++ onTransact err %d code %d", err, code);
1227
1228 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
1229 // the 'service' command interrogates this binder for its name, and then supplies it
1230 // even for the debugging commands. that means we need to check for it here, using
1231 // ISurfaceComposer (since we delegated the INTERFACE_TRANSACTION handling to
1232 // BnSurfaceComposer before falling through to this code).
1233
1234 LOGD("+++ onTransact code %d", code);
1235
1236 CHECK_INTERFACE(ICameraService, data, reply);
1237
1238 switch(code) {
1239 case 1000:
1240 {
1241 if (gWeakHeap != 0) {
1242 sp<IMemoryHeap> h = gWeakHeap.promote();
1243 IMemoryHeap *p = gWeakHeap.unsafe_get();
1244 LOGD("CHECKING WEAK REFERENCE %p (%p)", h.get(), p);
1245 if (h != 0)
1246 h->printRefs();
1247 bool attempt_to_delete = data.readInt32() == 1;
1248 if (attempt_to_delete) {
1249 // NOT SAFE!
1250 LOGD("DELETING WEAK REFERENCE %p (%p)", h.get(), p);
1251 if (p) delete p;
1252 }
1253 return NO_ERROR;
1254 }
1255 }
1256 break;
1257 default:
1258 break;
1259 }
1260 }
Dave Sparks998b3292009-05-20 20:02:59 -07001261#endif // DEBUG_HEAP_LEAKS
Dave Sparksfec880d2009-05-21 09:18:18 -07001262
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001263 return err;
1264}
1265
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001266}; // namespace android