blob: 6419a5ce12657dd9ac75821cc167a30760415c4d [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2**
3** Copyright (C) 2008, The Android Open Source Project
4** Copyright (C) 2008 HTC Inc.
5**
6** Licensed under the Apache License, Version 2.0 (the "License");
7** you may not use this file except in compliance with the License.
8** You may obtain a copy of the License at
9**
10** http://www.apache.org/licenses/LICENSE-2.0
11**
12** Unless required by applicable law or agreed to in writing, software
13** distributed under the License is distributed on an "AS IS" BASIS,
14** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15** See the License for the specific language governing permissions and
16** limitations under the License.
17*/
18
19//#define LOG_NDEBUG 0
20#define LOG_TAG "CameraService"
21#include <utils/Log.h>
22
Mathias Agopian07952722009-05-19 19:08:10 -070023#include <binder/IServiceManager.h>
24#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025#include <utils/String16.h>
26#include <utils/Errors.h>
Mathias Agopian07952722009-05-19 19:08:10 -070027#include <binder/MemoryBase.h>
28#include <binder/MemoryHeapBase.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029#include <ui/ICameraService.h>
30
Jason Samsb18b6912009-03-24 20:21:36 -070031#include <media/mediaplayer.h>
32#include <media/AudioSystem.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033#include "CameraService.h"
34
Chih-Chung Chang6fcba312009-06-24 13:44:37 +080035#include <cutils/atomic.h>
Eric Laurenta7f1e5c2009-03-27 16:27:16 -070036
The Android Open Source Project9066cfe2009-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 Chang1f25ec82009-06-22 16:03:41 +080045#include <signal.h>
The Android Open Source Project9066cfe2009-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
Benny Wongda83f462009-08-12 12:01:27 -050062#define DEBUG_DUMP_POSTVIEW_SNAPSHOT_TO_FILE 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063
64#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
65static int debug_frame_cnt;
66#endif
67
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +080068static int getCallingPid() {
69 return IPCThreadState::self()->getCallingPid();
70}
71
The Android Open Source Project9066cfe2009-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 Chang6fcba312009-06-24 13:44:37 +080085 mUsers = 0;
The Android Open Source Project9066cfe2009-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 Chang1f25ec82009-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 Project9066cfe2009-03-03 19:31:44 -0800100
Chih-Chung Changd5d1ebd2009-06-24 19:59:31 +0800101 Mutex::Autolock lock(mServiceLock);
The Android Open Source Project9066cfe2009-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 Chang1f25ec82009-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 Project9066cfe2009-03-03 19:31:44 -0800111 return currentClient;
112 } else {
Chih-Chung Chang1f25ec82009-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 Chang6fcba312009-06-24 13:44:37 +0800118 if (kill(currentClient->mClientPid, 0) == -1 && errno == ESRCH) {
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800119 LOGD("The old client is dead!");
120 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 return client;
122 }
123 } else {
124 // can't promote, the previous client has died...
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800125 LOGD("New client (pid %d) connecting, old reference was dangling...",
126 callingPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 mClient.clear();
128 }
129 }
130
Chih-Chung Changd5d1ebd2009-06-24 19:59:31 +0800131 if (mUsers > 0) {
132 LOGD("Still have client, rejected");
133 return client;
134 }
135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 // create a new Client object
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800137 client = new Client(this, cameraClient, callingPid);
The Android Open Source Project9066cfe2009-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 Chang1f25ec82009-06-22 16:03:41 +0800144 LOGD("CameraService::connect X");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 return client;
146}
147
148void CameraService::removeClient(const sp<ICameraClient>& cameraClient)
149{
Chih-Chung Chang1f25ec82009-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 Project9066cfe2009-03-03 19:31:44 -0800153 // destructor won't be called with the lock held.
154 sp<Client> client;
155
Chih-Chung Changd5d1ebd2009-06-24 19:59:31 +0800156 Mutex::Autolock lock(mServiceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157
158 if (mClient == 0) {
159 // This happens when we have already disconnected.
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800160 LOGD("removeClient (pid %d): already disconnected", callingPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 return;
162 }
163
Chih-Chung Chang1f25ec82009-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 Project9066cfe2009-03-03 19:31:44 -0800166 client = mClient.promote();
167 if (client == 0) {
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800168 LOGD("removeClient (pid %d): no more strong reference", callingPid);
The Android Open Source Project9066cfe2009-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 Chang1f25ec82009-06-22 16:03:41 +0800175 LOGW("removeClient (pid %d): mClient doesn't match!", callingPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 } else {
177 // okay, good, forget about mClient
178 mClient.clear();
179 }
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800180
181 LOGD("removeClient (pid %d) done", callingPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182}
183
Chih-Chung Chang6fcba312009-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
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700198static sp<MediaPlayer> newMediaPlayer(const char *file)
Jason Samsb18b6912009-03-24 20:21:36 -0700199{
200 sp<MediaPlayer> mp = new MediaPlayer();
201 if (mp->setDataSource(file) == NO_ERROR) {
Eric Laurenta553c252009-07-17 12:17:14 -0700202 mp->setAudioStreamType(AudioSystem::ENFORCED_AUDIBLE);
Jason Samsb18b6912009-03-24 20:21:36 -0700203 mp->prepare();
204 } else {
205 mp.clear();
206 LOGE("Failed to load CameraService sounds.");
207 }
208 return mp;
209}
210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211CameraService::Client::Client(const sp<CameraService>& cameraService,
212 const sp<ICameraClient>& cameraClient, pid_t clientPid)
213{
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800214 int callingPid = getCallingPid();
215 LOGD("Client::Client E (pid %d)", callingPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 mCameraService = cameraService;
217 mCameraClient = cameraClient;
218 mClientPid = clientPid;
219 mHardware = openCameraHardware();
220 mUseOverlay = mHardware->useOverlay();
221
Benny Wongda83f462009-08-12 12:01:27 -0500222 mHardware->setCallbacks(notifyCallback,
223 dataCallback,
224 dataCallbackTimestamp,
225 mCameraService.get());
226
227 // Enable zoom, error, and focus messages by default
228 mHardware->enableMsgType(CAMERA_MSG_ERROR |
229 CAMERA_MSG_ZOOM |
230 CAMERA_MSG_FOCUS);
231
Jason Samsb18b6912009-03-24 20:21:36 -0700232 mMediaPlayerClick = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
233 mMediaPlayerBeep = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
Benny Wong6d2090e2009-07-15 18:44:27 -0500234 mOverlayW = 0;
235 mOverlayH = 0;
Jason Samsb18b6912009-03-24 20:21:36 -0700236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 // Callback is disabled by default
238 mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800239 cameraService->incUsers();
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800240 LOGD("Client::Client X (pid %d)", callingPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241}
242
243status_t CameraService::Client::checkPid()
244{
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800245 int callingPid = getCallingPid();
246 if (mClientPid == callingPid) return NO_ERROR;
247 LOGW("Attempt to use locked camera (client %p) from different process "
248 " (old pid %d, new pid %d)",
249 getCameraClient()->asBinder().get(), mClientPid, callingPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 return -EBUSY;
251}
252
253status_t CameraService::Client::lock()
254{
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800255 int callingPid = getCallingPid();
256 LOGD("lock from pid %d (mClientPid %d)", callingPid, mClientPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 Mutex::Autolock _l(mLock);
258 // lock camera to this client if the the camera is unlocked
259 if (mClientPid == 0) {
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800260 mClientPid = callingPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 return NO_ERROR;
262 }
263 // returns NO_ERROR if the client already owns the camera, -EBUSY otherwise
264 return checkPid();
265}
266
267status_t CameraService::Client::unlock()
268{
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800269 int callingPid = getCallingPid();
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700270 LOGD("unlock from pid %d (mClientPid %d)", callingPid, mClientPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 Mutex::Autolock _l(mLock);
272 // allow anyone to use camera
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 status_t result = checkPid();
James Donge251f322009-04-20 19:35:28 -0700274 if (result == NO_ERROR) {
275 mClientPid = 0;
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800276 LOGD("clear mCameraClient (pid %d)", callingPid);
James Donge251f322009-04-20 19:35:28 -0700277 // we need to remove the reference so that when app goes
278 // away, the reference count goes to 0.
279 mCameraClient.clear();
280 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 return result;
282}
283
284status_t CameraService::Client::connect(const sp<ICameraClient>& client)
285{
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800286 int callingPid = getCallingPid();
287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 // connect a new process to the camera
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800289 LOGD("Client::connect E (pid %d, client %p)", callingPid, client->asBinder().get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290
291 // I hate this hack, but things get really ugly when the media recorder
292 // service is handing back the camera to the app. The ICameraClient
293 // destructor will be called during the same IPC, making it look like
294 // the remote client is trying to disconnect. This hack temporarily
295 // sets the mClientPid to an invalid pid to prevent the hardware from
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800296 // being torn down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 {
298
299 // hold a reference to the old client or we will deadlock if the client is
300 // in the same process and we hold the lock when we remove the reference
301 sp<ICameraClient> oldClient;
302 {
303 Mutex::Autolock _l(mLock);
Chih-Chung Changc8b24082009-06-09 13:56:44 +0800304 if (mClientPid != 0 && checkPid() != NO_ERROR) {
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800305 LOGW("Tried to connect to locked camera (old pid %d, new pid %d)",
306 mClientPid, callingPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 return -EBUSY;
308 }
309 oldClient = mCameraClient;
310
311 // did the client actually change?
Dave Sparksdd158c92009-10-15 10:02:22 -0700312 if ((mCameraClient != NULL) && (client->asBinder() == mCameraClient->asBinder())) {
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800313 LOGD("Connect to the same client");
314 return NO_ERROR;
315 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316
317 mCameraClient = client;
318 mClientPid = -1;
319 mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800320 LOGD("Connect to the new client (pid %d, client %p)",
321 callingPid, mCameraClient->asBinder().get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 }
323
324 }
325 // the old client destructor is called when oldClient goes out of scope
326 // now we set the new PID to lock the interface again
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800327 mClientPid = callingPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328
329 return NO_ERROR;
330}
331
332#if HAVE_ANDROID_OS
333static void *unregister_surface(void *arg)
334{
335 ISurface *surface = (ISurface *)arg;
336 surface->unregisterBuffers();
337 IPCThreadState::self()->flushCommands();
338 return NULL;
339}
340#endif
341
342CameraService::Client::~Client()
343{
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800344 int callingPid = getCallingPid();
345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 // tear down client
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800347 LOGD("Client::~Client E (pid %d, client %p)",
348 callingPid, getCameraClient()->asBinder().get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 if (mSurface != 0 && !mUseOverlay) {
350#if HAVE_ANDROID_OS
351 pthread_t thr;
352 // We unregister the buffers in a different thread because binder does
353 // not let us make sychronous transactions in a binder destructor (that
354 // is, upon our reaching a refcount of zero.)
355 pthread_create(&thr, NULL,
356 unregister_surface,
357 mSurface.get());
358 pthread_join(thr, NULL);
359#else
360 mSurface->unregisterBuffers();
361#endif
362 }
363
Jason Sams403914a2009-03-24 20:36:57 -0700364 if (mMediaPlayerBeep.get() != NULL) {
365 mMediaPlayerBeep->disconnect();
366 mMediaPlayerBeep.clear();
367 }
368 if (mMediaPlayerClick.get() != NULL) {
369 mMediaPlayerClick->disconnect();
370 mMediaPlayerClick.clear();
371 }
Jason Samsb18b6912009-03-24 20:21:36 -0700372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 // make sure we tear down the hardware
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800374 mClientPid = callingPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 disconnect();
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800376 LOGD("Client::~Client X (pid %d)", mClientPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377}
378
379void CameraService::Client::disconnect()
380{
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800381 int callingPid = getCallingPid();
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800382
383 LOGD("Client::disconnect() E (pid %d client %p)",
384 callingPid, getCameraClient()->asBinder().get());
385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 Mutex::Autolock lock(mLock);
387 if (mClientPid <= 0) {
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800388 LOGD("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 return;
390 }
391 if (checkPid() != NO_ERROR) {
Chih-Chung Chang4b4900b2009-06-16 17:15:04 +0800392 LOGD("Different client - don't disconnect");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 return;
394 }
395
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800396 // Make sure disconnect() is done once and once only, whether it is called
397 // from the user directly, or called by the destructor.
398 if (mHardware == 0) return;
399
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800400 LOGD("hardware teardown");
401 // Before destroying mHardware, we must make sure it's in the
402 // idle state.
403 mHardware->stopPreview();
404 // Cancel all picture callbacks.
Benny Wongda83f462009-08-12 12:01:27 -0500405 mHardware->disableMsgType(CAMERA_MSG_SHUTTER |
406 CAMERA_MSG_POSTVIEW_FRAME |
407 CAMERA_MSG_RAW_IMAGE |
408 CAMERA_MSG_COMPRESSED_IMAGE);
409 mHardware->cancelPicture();
410 // Turn off remaining messages.
411 mHardware->disableMsgType(CAMERA_MSG_ALL_MSGS);
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800412 // Release the hardware resources.
413 mHardware->release();
Benny Wong6d2090e2009-07-15 18:44:27 -0500414 // Release the held overlay resources.
415 if (mUseOverlay)
416 {
417 mOverlayRef = 0;
418 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 mHardware.clear();
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800420
Chih-Chung Changd5d1ebd2009-06-24 19:59:31 +0800421 mCameraService->removeClient(mCameraClient);
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800422 mCameraService->decUsers();
423
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800424 LOGD("Client::disconnect() X (pid %d)", callingPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425}
426
427// pass the buffered ISurface to the camera service
428status_t CameraService::Client::setPreviewDisplay(const sp<ISurface>& surface)
429{
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800430 LOGD("setPreviewDisplay(%p) (pid %d)",
431 ((surface == NULL) ? NULL : surface.get()), getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 Mutex::Autolock lock(mLock);
433 status_t result = checkPid();
434 if (result != NO_ERROR) return result;
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 Mutex::Autolock surfaceLock(mSurfaceLock);
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800437 result = NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 // asBinder() is safe on NULL (returns NULL)
439 if (surface->asBinder() != mSurface->asBinder()) {
Benny Wong6d2090e2009-07-15 18:44:27 -0500440 if (mSurface != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 LOGD("clearing old preview surface %p", mSurface.get());
Benny Wong6d2090e2009-07-15 18:44:27 -0500442 if ( !mUseOverlay)
443 {
444 mSurface->unregisterBuffers();
445 }
446 else
447 {
448 // Force the destruction of any previous overlay
449 sp<Overlay> dummy;
450 mHardware->setOverlay( dummy );
451 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 }
453 mSurface = surface;
Benny Wong6d2090e2009-07-15 18:44:27 -0500454 mOverlayRef = 0;
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800455 // If preview has been already started, set overlay or register preview
456 // buffers now.
457 if (mHardware->previewEnabled()) {
458 if (mUseOverlay) {
459 result = setOverlay();
460 } else if (mSurface != 0) {
461 result = registerPreviewBuffers();
462 }
463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 }
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800465 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466}
467
468// set the preview callback flag to affect how the received frames from
469// preview are handled.
470void CameraService::Client::setPreviewCallbackFlag(int callback_flag)
471{
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800472 LOGV("setPreviewCallbackFlag (pid %d)", getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 Mutex::Autolock lock(mLock);
474 if (checkPid() != NO_ERROR) return;
475 mPreviewCallbackFlag = callback_flag;
Benny Wongda83f462009-08-12 12:01:27 -0500476
477 if(mUseOverlay) {
478 if(mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ENABLE_MASK)
479 mHardware->enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
480 else
481 mHardware->disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
482 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483}
484
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800485// start preview mode
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486status_t CameraService::Client::startCameraMode(camera_mode mode)
487{
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800488 int callingPid = getCallingPid();
489
490 LOGD("startCameraMode(%d) (pid %d)", mode, callingPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491
492 /* we cannot call into mHardware with mLock held because
493 * mHardware has callbacks onto us which acquire this lock
494 */
495
496 Mutex::Autolock lock(mLock);
497 status_t result = checkPid();
498 if (result != NO_ERROR) return result;
499
500 if (mHardware == 0) {
501 LOGE("mHardware is NULL, returning.");
502 return INVALID_OPERATION;
503 }
504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 switch(mode) {
506 case CAMERA_RECORDING_MODE:
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800507 if (mSurface == 0) {
508 LOGE("setPreviewDisplay must be called before startRecordingMode.");
509 return INVALID_OPERATION;
510 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 return startRecordingMode();
512
513 default: // CAMERA_PREVIEW_MODE
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800514 if (mSurface == 0) {
515 LOGD("mSurface is not set yet.");
516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 return startPreviewMode();
518 }
519}
520
521status_t CameraService::Client::startRecordingMode()
522{
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800523 LOGD("startRecordingMode (pid %d)", getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524
525 status_t ret = UNKNOWN_ERROR;
526
527 // if preview has not been started, start preview first
528 if (!mHardware->previewEnabled()) {
529 ret = startPreviewMode();
530 if (ret != NO_ERROR) {
531 return ret;
532 }
533 }
534
535 // if recording has been enabled, nothing needs to be done
536 if (mHardware->recordingEnabled()) {
537 return NO_ERROR;
538 }
539
540 // start recording mode
Benny Wongda83f462009-08-12 12:01:27 -0500541 ret = mHardware->startRecording();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 if (ret != NO_ERROR) {
543 LOGE("mHardware->startRecording() failed with status %d", ret);
544 }
545 return ret;
546}
547
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800548status_t CameraService::Client::setOverlay()
549{
550 LOGD("setOverlay");
551 int w, h;
552 CameraParameters params(mHardware->getParameters());
553 params.getPreviewSize(&w, &h);
554
Benny Wong6d2090e2009-07-15 18:44:27 -0500555 if ( w != mOverlayW || h != mOverlayH )
556 {
557 // Force the destruction of any previous overlay
558 sp<Overlay> dummy;
559 mHardware->setOverlay( dummy );
560 mOverlayRef = 0;
561 }
562
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800563 status_t ret = NO_ERROR;
564 if (mSurface != 0) {
Benny Wong6d2090e2009-07-15 18:44:27 -0500565 if (mOverlayRef.get() == NULL) {
Dave Sparks21336402009-10-07 19:18:20 -0700566
567 // FIXME:
568 // Surfaceflinger may hold onto the previous overlay reference for some
569 // time after we try to destroy it. retry a few times. In the future, we
570 // should make the destroy call block, or possibly specify that we can
571 // wait in the createOverlay call if the previous overlay is in the
572 // process of being destroyed.
573 for (int retry = 0; retry < 50; ++retry) {
574 mOverlayRef = mSurface->createOverlay(w, h, OVERLAY_FORMAT_DEFAULT);
575 if (mOverlayRef != NULL) break;
576 LOGD("Overlay create failed - retrying");
577 usleep(20000);
578 }
Benny Wong6d2090e2009-07-15 18:44:27 -0500579 if ( mOverlayRef.get() == NULL )
580 {
581 LOGE("Overlay Creation Failed!");
582 return -EINVAL;
583 }
584 ret = mHardware->setOverlay(new Overlay(mOverlayRef));
585 }
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800586 } else {
587 ret = mHardware->setOverlay(NULL);
588 }
589 if (ret != NO_ERROR) {
590 LOGE("mHardware->setOverlay() failed with status %d\n", ret);
591 }
Benny Wong6d2090e2009-07-15 18:44:27 -0500592
593 mOverlayW = w;
594 mOverlayH = h;
595
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800596 return ret;
597}
598
599status_t CameraService::Client::registerPreviewBuffers()
600{
601 int w, h;
602 CameraParameters params(mHardware->getParameters());
603 params.getPreviewSize(&w, &h);
604
605 uint32_t transform = 0;
606 if (params.getOrientation() ==
607 CameraParameters::CAMERA_ORIENTATION_PORTRAIT) {
608 LOGV("portrait mode");
609 transform = ISurface::BufferHeap::ROT_90;
610 }
611 ISurface::BufferHeap buffers(w, h, w, h,
612 PIXEL_FORMAT_YCbCr_420_SP,
613 transform,
614 0,
615 mHardware->getPreviewHeap());
616
617 status_t ret = mSurface->registerBuffers(buffers);
618 if (ret != NO_ERROR) {
619 LOGE("registerBuffers failed with status %d", ret);
620 }
621 return ret;
622}
623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624status_t CameraService::Client::startPreviewMode()
625{
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800626 LOGD("startPreviewMode (pid %d)", getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627
628 // if preview has been enabled, nothing needs to be done
629 if (mHardware->previewEnabled()) {
630 return NO_ERROR;
631 }
632
633 // start preview mode
634#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
635 debug_frame_cnt = 0;
636#endif
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800637 status_t ret = NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638
639 if (mUseOverlay) {
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800640 // If preview display has been set, set overlay now.
641 if (mSurface != 0) {
642 ret = setOverlay();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 }
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800644 if (ret != NO_ERROR) return ret;
Benny Wongda83f462009-08-12 12:01:27 -0500645 ret = mHardware->startPreview();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 } else {
Benny Wongda83f462009-08-12 12:01:27 -0500647 mHardware->enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
648 ret = mHardware->startPreview();
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800649 if (ret != NO_ERROR) return ret;
650 // If preview display has been set, register preview buffers now.
651 if (mSurface != 0) {
652 // Unregister here because the surface registered with raw heap.
653 mSurface->unregisterBuffers();
654 ret = registerPreviewBuffers();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 }
656 }
657 return ret;
658}
659
660status_t CameraService::Client::startPreview()
661{
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800662 LOGD("startPreview (pid %d)", getCallingPid());
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 return startCameraMode(CAMERA_PREVIEW_MODE);
665}
666
667status_t CameraService::Client::startRecording()
668{
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800669 LOGD("startRecording (pid %d)", getCallingPid());
670
Jason Samsb18b6912009-03-24 20:21:36 -0700671 if (mMediaPlayerBeep.get() != NULL) {
Eric Laurent524dc042009-11-27 05:07:55 -0800672 // do not play record jingle if stream volume is 0
673 // (typically because ringer mode is silent).
674 int index;
675 AudioSystem::getStreamVolumeIndex(AudioSystem::ENFORCED_AUDIBLE, &index);
676 if (index != 0) {
677 mMediaPlayerBeep->seekTo(0);
678 mMediaPlayerBeep->start();
679 }
Jason Samsb18b6912009-03-24 20:21:36 -0700680 }
Benny Wongda83f462009-08-12 12:01:27 -0500681
682 mHardware->enableMsgType(CAMERA_MSG_VIDEO_FRAME);
683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 return startCameraMode(CAMERA_RECORDING_MODE);
685}
686
687// stop preview mode
688void CameraService::Client::stopPreview()
689{
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800690 LOGD("stopPreview (pid %d)", getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691
Dave Sparks05fd0df2009-11-10 17:08:08 -0800692 // hold main lock during state transition
693 {
694 Mutex::Autolock lock(mLock);
695 if (checkPid() != NO_ERROR) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696
Dave Sparks05fd0df2009-11-10 17:08:08 -0800697 if (mHardware == 0) {
698 LOGE("mHardware is NULL, returning.");
699 return;
700 }
701
702 mHardware->stopPreview();
703 mHardware->disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
704 LOGD("stopPreview(), hardware stopped OK");
705
706 if (mSurface != 0 && !mUseOverlay) {
707 mSurface->unregisterBuffers();
708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 }
710
Dave Sparks05fd0df2009-11-10 17:08:08 -0800711 // hold preview buffer lock
712 {
713 Mutex::Autolock lock(mPreviewLock);
714 mPreviewBuffer.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716}
717
718// stop recording mode
719void CameraService::Client::stopRecording()
720{
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800721 LOGD("stopRecording (pid %d)", getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722
Dave Sparks05fd0df2009-11-10 17:08:08 -0800723 // hold main lock during state transition
724 {
725 Mutex::Autolock lock(mLock);
726 if (checkPid() != NO_ERROR) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727
Dave Sparks05fd0df2009-11-10 17:08:08 -0800728 if (mHardware == 0) {
729 LOGE("mHardware is NULL, returning.");
730 return;
731 }
732
733 if (mMediaPlayerBeep.get() != NULL) {
734 mMediaPlayerBeep->seekTo(0);
735 mMediaPlayerBeep->start();
736 }
737
738 mHardware->stopRecording();
739 mHardware->disableMsgType(CAMERA_MSG_VIDEO_FRAME);
740 LOGD("stopRecording(), hardware stopped OK");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 }
742
Dave Sparks05fd0df2009-11-10 17:08:08 -0800743 // hold preview buffer lock
744 {
745 Mutex::Autolock lock(mPreviewLock);
746 mPreviewBuffer.clear();
Jason Samsb18b6912009-03-24 20:21:36 -0700747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748}
749
750// release a recording frame
751void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem)
752{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 Mutex::Autolock lock(mLock);
754 if (checkPid() != NO_ERROR) return;
755
756 if (mHardware == 0) {
757 LOGE("mHardware is NULL, returning.");
758 return;
759 }
760
761 mHardware->releaseRecordingFrame(mem);
762}
763
764bool CameraService::Client::previewEnabled()
765{
766 Mutex::Autolock lock(mLock);
767 if (mHardware == 0) return false;
768 return mHardware->previewEnabled();
769}
770
771bool CameraService::Client::recordingEnabled()
772{
773 Mutex::Autolock lock(mLock);
774 if (mHardware == 0) return false;
775 return mHardware->recordingEnabled();
776}
777
778// Safely retrieves a strong pointer to the client during a hardware callback.
779sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user)
780{
781 sp<Client> client = 0;
782 CameraService *service = static_cast<CameraService*>(user);
783 if (service != NULL) {
Chih-Chung Changd5d1ebd2009-06-24 19:59:31 +0800784 Mutex::Autolock ourLock(service->mServiceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 if (service->mClient != 0) {
786 client = service->mClient.promote();
787 if (client == 0) {
788 LOGE("getClientFromCookie: client appears to have died");
789 service->mClient.clear();
790 }
791 } else {
792 LOGE("getClientFromCookie: got callback but client was NULL");
793 }
794 }
795 return client;
796}
797
798
799#if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE || \
800 DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE || \
801 DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
802static void dump_to_file(const char *fname,
803 uint8_t *buf, uint32_t size)
804{
805 int nw, cnt = 0;
806 uint32_t written = 0;
807
808 LOGD("opening file [%s]\n", fname);
809 int fd = open(fname, O_RDWR | O_CREAT);
810 if (fd < 0) {
811 LOGE("failed to create file [%s]: %s", fname, strerror(errno));
812 return;
813 }
814
815 LOGD("writing %d bytes to file [%s]\n", size, fname);
816 while (written < size) {
817 nw = ::write(fd,
818 buf + written,
819 size - written);
820 if (nw < 0) {
821 LOGE("failed to write to file [%s]: %s",
822 fname, strerror(errno));
823 break;
824 }
825 written += nw;
826 cnt++;
827 }
828 LOGD("done writing %d bytes to file [%s] in %d passes\n",
829 size, fname, cnt);
830 ::close(fd);
831}
832#endif
833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834status_t CameraService::Client::autoFocus()
835{
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800836 LOGD("autoFocus (pid %d)", getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837
838 Mutex::Autolock lock(mLock);
839 status_t result = checkPid();
840 if (result != NO_ERROR) return result;
841
842 if (mHardware == 0) {
843 LOGE("mHardware is NULL, returning.");
844 return INVALID_OPERATION;
845 }
846
Benny Wongda83f462009-08-12 12:01:27 -0500847 return mHardware->autoFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848}
849
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800850status_t CameraService::Client::cancelAutoFocus()
851{
852 LOGD("cancelAutoFocus (pid %d)", getCallingPid());
853
854 Mutex::Autolock lock(mLock);
855 status_t result = checkPid();
856 if (result != NO_ERROR) return result;
857
858 if (mHardware == 0) {
859 LOGE("mHardware is NULL, returning.");
860 return INVALID_OPERATION;
861 }
862
863 return mHardware->cancelAutoFocus();
864}
865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866// take a picture - image is returned in callback
867status_t CameraService::Client::takePicture()
868{
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800869 LOGD("takePicture (pid %d)", getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870
871 Mutex::Autolock lock(mLock);
872 status_t result = checkPid();
873 if (result != NO_ERROR) return result;
874
875 if (mHardware == 0) {
876 LOGE("mHardware is NULL, returning.");
877 return INVALID_OPERATION;
878 }
879
Benny Wongda83f462009-08-12 12:01:27 -0500880 mHardware->enableMsgType(CAMERA_MSG_SHUTTER |
881 CAMERA_MSG_POSTVIEW_FRAME |
882 CAMERA_MSG_RAW_IMAGE |
883 CAMERA_MSG_COMPRESSED_IMAGE);
884
885 return mHardware->takePicture();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886}
887
Benny Wongda83f462009-08-12 12:01:27 -0500888// snapshot taken
Wu-cheng Li4cb04c42009-10-23 17:39:46 +0800889void CameraService::Client::handleShutter(
890 image_rect_type *size // The width and height of yuv picture for
891 // registerBuffer. If this is NULL, use the picture
892 // size from parameters.
893)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894{
Wu-cheng Lia9f95fb2009-03-24 20:39:09 -0700895 // Play shutter sound.
Benny Wongda83f462009-08-12 12:01:27 -0500896 if (mMediaPlayerClick.get() != NULL) {
Eric Laurent524dc042009-11-27 05:07:55 -0800897 // do not play shutter sound if stream volume is 0
898 // (typically because ringer mode is silent).
899 int index;
900 AudioSystem::getStreamVolumeIndex(AudioSystem::ENFORCED_AUDIBLE, &index);
901 if (index != 0) {
902 mMediaPlayerClick->seekTo(0);
903 mMediaPlayerClick->start();
904 }
Wu-cheng Lia9f95fb2009-03-24 20:39:09 -0700905 }
906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 // Screen goes black after the buffer is unregistered.
Benny Wongda83f462009-08-12 12:01:27 -0500908 if (mSurface != 0 && !mUseOverlay) {
909 mSurface->unregisterBuffers();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 }
911
Dave Sparksdd158c92009-10-15 10:02:22 -0700912 sp<ICameraClient> c = mCameraClient;
913 if (c != NULL) {
914 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
915 }
Benny Wongda83f462009-08-12 12:01:27 -0500916 mHardware->disableMsgType(CAMERA_MSG_SHUTTER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917
918 // It takes some time before yuvPicture callback to be called.
919 // Register the buffer for raw image here to reduce latency.
Benny Wongda83f462009-08-12 12:01:27 -0500920 if (mSurface != 0 && !mUseOverlay) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 int w, h;
Benny Wongda83f462009-08-12 12:01:27 -0500922 CameraParameters params(mHardware->getParameters());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 uint32_t transform = 0;
924 if (params.getOrientation() == CameraParameters::CAMERA_ORIENTATION_PORTRAIT) {
925 LOGV("portrait mode");
926 transform = ISurface::BufferHeap::ROT_90;
927 }
Wu-cheng Li4cb04c42009-10-23 17:39:46 +0800928
929 if (size == NULL) {
930 params.getPictureSize(&w, &h);
931 } else {
932 w = size->width;
933 h = size->height;
934 w &= ~1;
935 h &= ~1;
936 LOGD("Snapshot image width=%d, height=%d", w, h);
937 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 ISurface::BufferHeap buffers(w, h, w, h,
Benny Wongda83f462009-08-12 12:01:27 -0500939 PIXEL_FORMAT_YCbCr_420_SP, transform, 0, mHardware->getRawHeap());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940
Benny Wongda83f462009-08-12 12:01:27 -0500941 mSurface->registerBuffers(buffers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943}
944
Benny Wongda83f462009-08-12 12:01:27 -0500945// preview callback - frame buffer update
946void CameraService::Client::handlePreviewData(const sp<IMemory>& mem)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947{
Benny Wongda83f462009-08-12 12:01:27 -0500948 ssize_t offset;
949 size_t size;
950 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
951
952#if DEBUG_HEAP_LEAKS && 0 // debugging
953 if (gWeakHeap == NULL) {
954 if (gWeakHeap != heap) {
955 LOGD("SETTING PREVIEW HEAP");
956 heap->trackMe(true, true);
957 gWeakHeap = heap;
958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 }
Benny Wongda83f462009-08-12 12:01:27 -0500960#endif
961#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
962 {
963 if (debug_frame_cnt++ == DEBUG_DUMP_PREVIEW_FRAME_TO_FILE) {
964 dump_to_file("/data/preview.yuv",
965 (uint8_t *)heap->base() + offset, size);
966 }
967 }
968#endif
969
970 if (!mUseOverlay)
971 {
972 Mutex::Autolock surfaceLock(mSurfaceLock);
973 if (mSurface != NULL) {
974 mSurface->postBuffer(offset);
975 }
976 }
977
Dave Sparksdd158c92009-10-15 10:02:22 -0700978 // local copy of the callback flags
979 int flags = mPreviewCallbackFlag;
980
981 // is callback enabled?
982 if (!(flags & FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
Benny Wongda83f462009-08-12 12:01:27 -0500983 // If the enable bit is off, the copy-out and one-shot bits are ignored
984 LOGV("frame callback is diabled");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 return;
986 }
987
Dave Sparksdd158c92009-10-15 10:02:22 -0700988 // hold a strong pointer to the client
989 sp<ICameraClient> c = mCameraClient;
Benny Wongda83f462009-08-12 12:01:27 -0500990
Dave Sparksdd158c92009-10-15 10:02:22 -0700991 // clear callback flags if no client or one-shot mode
992 if ((c == NULL) || (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
993 LOGV("Disable preview callback");
Benny Wongda83f462009-08-12 12:01:27 -0500994 mPreviewCallbackFlag &= ~(FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
995 FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
996 FRAME_CALLBACK_FLAG_ENABLE_MASK);
Dave Sparksdd158c92009-10-15 10:02:22 -0700997 // TODO: Shouldn't we use this API for non-overlay hardware as well?
Benny Wongda83f462009-08-12 12:01:27 -0500998 if (mUseOverlay)
999 mHardware->disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
1000 }
Dave Sparksdd158c92009-10-15 10:02:22 -07001001
1002 // Is the received frame copied out or not?
1003 if (flags & FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
1004 LOGV("frame is copied");
1005 copyFrameAndPostCopiedFrame(c, heap, offset, size);
1006 } else {
1007 LOGV("frame is forwarded");
1008 c->dataCallback(CAMERA_MSG_PREVIEW_FRAME, mem);
1009 }
Benny Wongda83f462009-08-12 12:01:27 -05001010}
1011
1012// picture callback - postview image ready
1013void CameraService::Client::handlePostview(const sp<IMemory>& mem)
1014{
1015#if DEBUG_DUMP_POSTVIEW_SNAPSHOT_TO_FILE // for testing pursposes only
1016 {
1017 ssize_t offset;
1018 size_t size;
1019 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1020 dump_to_file("/data/postview.yuv",
1021 (uint8_t *)heap->base() + offset, size);
1022 }
1023#endif
1024
Dave Sparksdd158c92009-10-15 10:02:22 -07001025 sp<ICameraClient> c = mCameraClient;
1026 if (c != NULL) {
1027 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem);
1028 }
Benny Wongda83f462009-08-12 12:01:27 -05001029 mHardware->disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
1030}
1031
1032// picture callback - raw image ready
1033void CameraService::Client::handleRawPicture(const sp<IMemory>& mem)
1034{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 ssize_t offset;
1036 size_t size;
1037 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1038#if DEBUG_HEAP_LEAKS && 0 // debugging
1039 gWeakHeap = heap; // debugging
1040#endif
1041
Benny Wongda83f462009-08-12 12:01:27 -05001042 //LOGV("handleRawPicture(%d, %d)", offset, size);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043#if DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE // for testing pursposes only
1044 dump_to_file("/data/photo.yuv",
1045 (uint8_t *)heap->base() + offset, size);
1046#endif
1047
1048 // Put the YUV version of the snapshot in the preview display.
Benny Wongda83f462009-08-12 12:01:27 -05001049 if (mSurface != 0 && !mUseOverlay) {
1050 mSurface->postBuffer(offset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 }
1052
Dave Sparksdd158c92009-10-15 10:02:22 -07001053 sp<ICameraClient> c = mCameraClient;
1054 if (c != NULL) {
1055 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem);
1056 }
Benny Wongda83f462009-08-12 12:01:27 -05001057 mHardware->disableMsgType(CAMERA_MSG_RAW_IMAGE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058}
1059
Benny Wongda83f462009-08-12 12:01:27 -05001060// picture callback - compressed picture ready
1061void CameraService::Client::handleCompressedPicture(const sp<IMemory>& mem)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063#if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE // for testing pursposes only
1064 {
1065 ssize_t offset;
1066 size_t size;
1067 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1068 dump_to_file("/data/photo.jpg",
1069 (uint8_t *)heap->base() + offset, size);
1070 }
1071#endif
1072
Dave Sparksdd158c92009-10-15 10:02:22 -07001073 sp<ICameraClient> c = mCameraClient;
1074 if (c != NULL) {
1075 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem);
1076 }
Benny Wongda83f462009-08-12 12:01:27 -05001077 mHardware->disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078}
1079
Benny Wongda83f462009-08-12 12:01:27 -05001080void CameraService::Client::notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081{
Benny Wongda83f462009-08-12 12:01:27 -05001082 LOGV("notifyCallback(%d)", msgType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083
1084 sp<Client> client = getClientFromCookie(user);
1085 if (client == 0) {
1086 return;
1087 }
1088
Benny Wongda83f462009-08-12 12:01:27 -05001089 switch (msgType) {
1090 case CAMERA_MSG_SHUTTER:
Wu-cheng Li4cb04c42009-10-23 17:39:46 +08001091 // ext1 is the dimension of the yuv picture.
1092 client->handleShutter((image_rect_type *)ext1);
Benny Wongda83f462009-08-12 12:01:27 -05001093 break;
1094 default:
Dave Sparksdd158c92009-10-15 10:02:22 -07001095 sp<ICameraClient> c = client->mCameraClient;
1096 if (c != NULL) {
1097 c->notifyCallback(msgType, ext1, ext2);
1098 }
Benny Wongda83f462009-08-12 12:01:27 -05001099 break;
1100 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101
1102#if DEBUG_CLIENT_REFERENCES
1103 if (client->getStrongCount() == 1) {
Benny Wongda83f462009-08-12 12:01:27 -05001104 LOGE("++++++++++++++++ (NOTIFY CALLBACK) THIS WILL CAUSE A LOCKUP!");
1105 client->printRefs();
1106 }
1107#endif
1108}
1109
1110void CameraService::Client::dataCallback(int32_t msgType, const sp<IMemory>& dataPtr, void* user)
1111{
1112 LOGV("dataCallback(%d)", msgType);
1113
1114 sp<Client> client = getClientFromCookie(user);
1115 if (client == 0) {
1116 return;
1117 }
1118
Dave Sparksdd158c92009-10-15 10:02:22 -07001119 sp<ICameraClient> c = client->mCameraClient;
Benny Wongda83f462009-08-12 12:01:27 -05001120 if (dataPtr == NULL) {
1121 LOGE("Null data returned in data callback");
Dave Sparksdd158c92009-10-15 10:02:22 -07001122 if (c != NULL) {
1123 c->notifyCallback(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
1124 c->dataCallback(msgType, NULL);
1125 }
Benny Wongda83f462009-08-12 12:01:27 -05001126 return;
1127 }
1128
1129 switch (msgType) {
1130 case CAMERA_MSG_PREVIEW_FRAME:
1131 client->handlePreviewData(dataPtr);
1132 break;
1133 case CAMERA_MSG_POSTVIEW_FRAME:
1134 client->handlePostview(dataPtr);
1135 break;
1136 case CAMERA_MSG_RAW_IMAGE:
1137 client->handleRawPicture(dataPtr);
1138 break;
1139 case CAMERA_MSG_COMPRESSED_IMAGE:
1140 client->handleCompressedPicture(dataPtr);
1141 break;
1142 default:
Dave Sparksdd158c92009-10-15 10:02:22 -07001143 if (c != NULL) {
1144 c->dataCallback(msgType, dataPtr);
1145 }
Benny Wongda83f462009-08-12 12:01:27 -05001146 break;
1147 }
1148
1149#if DEBUG_CLIENT_REFERENCES
1150 if (client->getStrongCount() == 1) {
1151 LOGE("++++++++++++++++ (DATA CALLBACK) THIS WILL CAUSE A LOCKUP!");
1152 client->printRefs();
1153 }
1154#endif
1155}
1156
1157void CameraService::Client::dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType,
1158 const sp<IMemory>& dataPtr, void* user)
1159{
1160 LOGV("dataCallbackTimestamp(%d)", msgType);
1161
1162 sp<Client> client = getClientFromCookie(user);
1163 if (client == 0) {
1164 return;
1165 }
Dave Sparksdd158c92009-10-15 10:02:22 -07001166 sp<ICameraClient> c = client->mCameraClient;
Benny Wongda83f462009-08-12 12:01:27 -05001167
1168 if (dataPtr == NULL) {
1169 LOGE("Null data returned in data with timestamp callback");
Dave Sparksdd158c92009-10-15 10:02:22 -07001170 if (c != NULL) {
1171 c->notifyCallback(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
1172 c->dataCallbackTimestamp(0, msgType, NULL);
1173 }
Benny Wongda83f462009-08-12 12:01:27 -05001174 return;
1175 }
1176
Dave Sparksdd158c92009-10-15 10:02:22 -07001177 if (c != NULL) {
1178 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
1179 }
Benny Wongda83f462009-08-12 12:01:27 -05001180
1181#if DEBUG_CLIENT_REFERENCES
1182 if (client->getStrongCount() == 1) {
1183 LOGE("++++++++++++++++ (DATA CALLBACK TIMESTAMP) THIS WILL CAUSE A LOCKUP!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 client->printRefs();
1185 }
1186#endif
1187}
1188
1189// set preview/capture parameters - key/value pairs
1190status_t CameraService::Client::setParameters(const String8& params)
1191{
1192 LOGD("setParameters(%s)", params.string());
1193
1194 Mutex::Autolock lock(mLock);
1195 status_t result = checkPid();
1196 if (result != NO_ERROR) return result;
1197
1198 if (mHardware == 0) {
1199 LOGE("mHardware is NULL, returning.");
1200 return INVALID_OPERATION;
1201 }
1202
1203 CameraParameters p(params);
James Dong6085b4e2009-09-13 17:10:24 -07001204 return mHardware->setParameters(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205}
1206
1207// get preview/capture parameters - key/value pairs
1208String8 CameraService::Client::getParameters() const
1209{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 Mutex::Autolock lock(mLock);
1211
1212 if (mHardware == 0) {
1213 LOGE("mHardware is NULL, returning.");
1214 return String8();
1215 }
1216
Wu-cheng Liab5b4242009-04-22 16:21:26 +08001217 String8 params(mHardware->getParameters().flatten());
1218 LOGD("getParameters(%s)", params.string());
1219 return params;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220}
1221
Wu-cheng Li36f68b82009-09-28 16:14:58 -07001222status_t CameraService::Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
1223{
1224 LOGD("sendCommand (pid %d)", getCallingPid());
1225 Mutex::Autolock lock(mLock);
1226 status_t result = checkPid();
1227 if (result != NO_ERROR) return result;
1228
1229 if (mHardware == 0) {
1230 LOGE("mHardware is NULL, returning.");
1231 return INVALID_OPERATION;
1232 }
1233
1234 return mHardware->sendCommand(cmd, arg1, arg2);
1235}
1236
Dave Sparksdd158c92009-10-15 10:02:22 -07001237void CameraService::Client::copyFrameAndPostCopiedFrame(const sp<ICameraClient>& client,
1238 const sp<IMemoryHeap>& heap, size_t offset, size_t size)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239{
1240 LOGV("copyFrameAndPostCopiedFrame");
1241 // It is necessary to copy out of pmem before sending this to
1242 // the callback. For efficiency, reuse the same MemoryHeapBase
1243 // provided it's big enough. Don't allocate the memory or
1244 // perform the copy if there's no callback.
Dave Sparksc8093c12009-11-06 11:47:13 -08001245
Dave Sparks05fd0df2009-11-10 17:08:08 -08001246 // hold the preview lock while we grab a reference to the preview buffer
Dave Sparksc8093c12009-11-06 11:47:13 -08001247 sp<MemoryHeapBase> previewBuffer;
1248 {
Dave Sparks05fd0df2009-11-10 17:08:08 -08001249 Mutex::Autolock lock(mPreviewLock);
Dave Sparksc8093c12009-11-06 11:47:13 -08001250 if (mPreviewBuffer == 0) {
1251 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1252 } else if (size > mPreviewBuffer->virtualSize()) {
1253 mPreviewBuffer.clear();
1254 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1255 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 if (mPreviewBuffer == 0) {
1257 LOGE("failed to allocate space for preview buffer");
1258 return;
1259 }
Dave Sparksc8093c12009-11-06 11:47:13 -08001260 previewBuffer = mPreviewBuffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 }
Dave Sparksc8093c12009-11-06 11:47:13 -08001262 memcpy(previewBuffer->base(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 (uint8_t *)heap->base() + offset, size);
1264
Dave Sparksc8093c12009-11-06 11:47:13 -08001265 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 if (frame == 0) {
1267 LOGE("failed to allocate space for frame callback");
1268 return;
1269 }
Dave Sparksdd158c92009-10-15 10:02:22 -07001270 client->dataCallback(CAMERA_MSG_PREVIEW_FRAME, frame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271}
1272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273status_t CameraService::dump(int fd, const Vector<String16>& args)
1274{
1275 const size_t SIZE = 256;
1276 char buffer[SIZE];
1277 String8 result;
1278 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
1279 snprintf(buffer, SIZE, "Permission Denial: "
1280 "can't dump CameraService from pid=%d, uid=%d\n",
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +08001281 getCallingPid(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 IPCThreadState::self()->getCallingUid());
1283 result.append(buffer);
1284 write(fd, result.string(), result.size());
1285 } else {
Chih-Chung Changd5d1ebd2009-06-24 19:59:31 +08001286 AutoMutex lock(&mServiceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 if (mClient != 0) {
1288 sp<Client> currentClient = mClient.promote();
1289 sprintf(buffer, "Client (%p) PID: %d\n",
1290 currentClient->getCameraClient()->asBinder().get(),
1291 currentClient->mClientPid);
1292 result.append(buffer);
1293 write(fd, result.string(), result.size());
1294 currentClient->mHardware->dump(fd, args);
1295 } else {
1296 result.append("No camera client yet.\n");
1297 write(fd, result.string(), result.size());
1298 }
1299 }
1300 return NO_ERROR;
1301}
1302
1303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304status_t CameraService::onTransact(
1305 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1306{
1307 // permission checks...
1308 switch (code) {
1309 case BnCameraService::CONNECT:
1310 IPCThreadState* ipc = IPCThreadState::self();
1311 const int pid = ipc->getCallingPid();
1312 const int self_pid = getpid();
1313 if (pid != self_pid) {
1314 // we're called from a different process, do the real check
1315 if (!checkCallingPermission(
1316 String16("android.permission.CAMERA")))
1317 {
1318 const int uid = ipc->getCallingUid();
1319 LOGE("Permission Denial: "
1320 "can't use the camera pid=%d, uid=%d", pid, uid);
1321 return PERMISSION_DENIED;
1322 }
1323 }
1324 break;
1325 }
1326
1327 status_t err = BnCameraService::onTransact(code, data, reply, flags);
1328
Dave Sparksb1f3b8f2009-05-20 20:02:59 -07001329#if DEBUG_HEAP_LEAKS
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 LOGD("+++ onTransact err %d code %d", err, code);
1331
1332 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
1333 // the 'service' command interrogates this binder for its name, and then supplies it
1334 // even for the debugging commands. that means we need to check for it here, using
1335 // ISurfaceComposer (since we delegated the INTERFACE_TRANSACTION handling to
1336 // BnSurfaceComposer before falling through to this code).
1337
1338 LOGD("+++ onTransact code %d", code);
1339
1340 CHECK_INTERFACE(ICameraService, data, reply);
1341
1342 switch(code) {
1343 case 1000:
1344 {
1345 if (gWeakHeap != 0) {
1346 sp<IMemoryHeap> h = gWeakHeap.promote();
1347 IMemoryHeap *p = gWeakHeap.unsafe_get();
1348 LOGD("CHECKING WEAK REFERENCE %p (%p)", h.get(), p);
1349 if (h != 0)
1350 h->printRefs();
1351 bool attempt_to_delete = data.readInt32() == 1;
1352 if (attempt_to_delete) {
1353 // NOT SAFE!
1354 LOGD("DELETING WEAK REFERENCE %p (%p)", h.get(), p);
1355 if (p) delete p;
1356 }
1357 return NO_ERROR;
1358 }
1359 }
1360 break;
1361 default:
1362 break;
1363 }
1364 }
Dave Sparksb1f3b8f2009-05-20 20:02:59 -07001365#endif // DEBUG_HEAP_LEAKS
Dave Sparks7b7225c2009-05-21 09:18:18 -07001366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 return err;
1368}
1369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370}; // namespace android