blob: 96389dd5d22c813028ca674e3e823c80f0681549 [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");
230
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231 // Callback is disabled by default
232 mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800233 cameraService->incUsers();
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800234 LOGD("Client::Client X (pid %d)", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235}
236
237status_t CameraService::Client::checkPid()
238{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800239 int callingPid = getCallingPid();
240 if (mClientPid == callingPid) return NO_ERROR;
241 LOGW("Attempt to use locked camera (client %p) from different process "
242 " (old pid %d, new pid %d)",
243 getCameraClient()->asBinder().get(), mClientPid, callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244 return -EBUSY;
245}
246
247status_t CameraService::Client::lock()
248{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800249 int callingPid = getCallingPid();
250 LOGD("lock from pid %d (mClientPid %d)", callingPid, mClientPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251 Mutex::Autolock _l(mLock);
252 // lock camera to this client if the the camera is unlocked
253 if (mClientPid == 0) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800254 mClientPid = callingPid;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255 return NO_ERROR;
256 }
257 // returns NO_ERROR if the client already owns the camera, -EBUSY otherwise
258 return checkPid();
259}
260
261status_t CameraService::Client::unlock()
262{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800263 int callingPid = getCallingPid();
264 LOGD("unlock from pid %d (mClientPid %d)", callingPid, mClientPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265 Mutex::Autolock _l(mLock);
266 // allow anyone to use camera
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267 status_t result = checkPid();
James Dong0ae13e32009-04-20 19:35:28 -0700268 if (result == NO_ERROR) {
269 mClientPid = 0;
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800270 LOGD("clear mCameraClient (pid %d)", callingPid);
James Dong0ae13e32009-04-20 19:35:28 -0700271 // we need to remove the reference so that when app goes
272 // away, the reference count goes to 0.
273 mCameraClient.clear();
274 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275 return result;
276}
277
278status_t CameraService::Client::connect(const sp<ICameraClient>& client)
279{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800280 int callingPid = getCallingPid();
281
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800282 // connect a new process to the camera
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800283 LOGD("Client::connect E (pid %d, client %p)", callingPid, client->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284
285 // I hate this hack, but things get really ugly when the media recorder
286 // service is handing back the camera to the app. The ICameraClient
287 // destructor will be called during the same IPC, making it look like
288 // the remote client is trying to disconnect. This hack temporarily
289 // sets the mClientPid to an invalid pid to prevent the hardware from
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800290 // being torn down.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800291 {
292
293 // hold a reference to the old client or we will deadlock if the client is
294 // in the same process and we hold the lock when we remove the reference
295 sp<ICameraClient> oldClient;
296 {
297 Mutex::Autolock _l(mLock);
Chih-Chung Chang34e5a152009-06-09 13:56:44 +0800298 if (mClientPid != 0 && checkPid() != NO_ERROR) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800299 LOGW("Tried to connect to locked camera (old pid %d, new pid %d)",
300 mClientPid, callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301 return -EBUSY;
302 }
303 oldClient = mCameraClient;
304
305 // did the client actually change?
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800306 if (client->asBinder() == mCameraClient->asBinder()) {
307 LOGD("Connect to the same client");
308 return NO_ERROR;
309 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310
311 mCameraClient = client;
312 mClientPid = -1;
313 mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800314 LOGD("Connect to the new client (pid %d, client %p)",
315 callingPid, mCameraClient->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316 }
317
318 }
319 // the old client destructor is called when oldClient goes out of scope
320 // now we set the new PID to lock the interface again
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800321 mClientPid = callingPid;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322
323 return NO_ERROR;
324}
325
326#if HAVE_ANDROID_OS
327static void *unregister_surface(void *arg)
328{
329 ISurface *surface = (ISurface *)arg;
330 surface->unregisterBuffers();
331 IPCThreadState::self()->flushCommands();
332 return NULL;
333}
334#endif
335
336CameraService::Client::~Client()
337{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800338 int callingPid = getCallingPid();
339
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800340 // tear down client
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800341 LOGD("Client::~Client E (pid %d, client %p)",
342 callingPid, getCameraClient()->asBinder().get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800343 if (mSurface != 0 && !mUseOverlay) {
344#if HAVE_ANDROID_OS
345 pthread_t thr;
346 // We unregister the buffers in a different thread because binder does
347 // not let us make sychronous transactions in a binder destructor (that
348 // is, upon our reaching a refcount of zero.)
349 pthread_create(&thr, NULL,
350 unregister_surface,
351 mSurface.get());
352 pthread_join(thr, NULL);
353#else
354 mSurface->unregisterBuffers();
355#endif
356 }
357
Jason Sams9e431ac2009-03-24 20:36:57 -0700358 if (mMediaPlayerBeep.get() != NULL) {
359 mMediaPlayerBeep->disconnect();
360 mMediaPlayerBeep.clear();
361 }
362 if (mMediaPlayerClick.get() != NULL) {
363 mMediaPlayerClick->disconnect();
364 mMediaPlayerClick.clear();
365 }
Jason Sams78b877e2009-03-24 20:21:36 -0700366
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800367 // make sure we tear down the hardware
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800368 mClientPid = callingPid;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800369 disconnect();
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800370 LOGD("Client::~Client X (pid %d)", mClientPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800371}
372
373void CameraService::Client::disconnect()
374{
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800375 int callingPid = getCallingPid();
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800376
377 LOGD("Client::disconnect() E (pid %d client %p)",
378 callingPid, getCameraClient()->asBinder().get());
379
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800380 Mutex::Autolock lock(mLock);
381 if (mClientPid <= 0) {
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800382 LOGD("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800383 return;
384 }
385 if (checkPid() != NO_ERROR) {
Chih-Chung Chang6a5297d2009-06-16 17:15:04 +0800386 LOGD("Different client - don't disconnect");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800387 return;
388 }
389
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800390 // Make sure disconnect() is done once and once only, whether it is called
391 // from the user directly, or called by the destructor.
392 if (mHardware == 0) return;
393
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800394 LOGD("hardware teardown");
395 // Before destroying mHardware, we must make sure it's in the
396 // idle state.
397 mHardware->stopPreview();
398 // Cancel all picture callbacks.
399 mHardware->cancelPicture(true, true, true);
400 // Release the hardware resources.
401 mHardware->release();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800402 mHardware.clear();
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800403
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800404 mCameraService->removeClient(mCameraClient);
Chih-Chung Changfa89f9f2009-06-24 13:44:37 +0800405 mCameraService->decUsers();
406
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800407 LOGD("Client::disconnect() X (pid %d)", callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800408}
409
410// pass the buffered ISurface to the camera service
411status_t CameraService::Client::setPreviewDisplay(const sp<ISurface>& surface)
412{
Wu-cheng Li988fb622009-06-23 23:37:36 +0800413 LOGD("setPreviewDisplay(%p) (pid %d)",
414 ((surface == NULL) ? NULL : surface.get()), getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800415 Mutex::Autolock lock(mLock);
416 status_t result = checkPid();
417 if (result != NO_ERROR) return result;
Wu-cheng Li988fb622009-06-23 23:37:36 +0800418
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800419 Mutex::Autolock surfaceLock(mSurfaceLock);
Wu-cheng Li988fb622009-06-23 23:37:36 +0800420 result = NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800421 // asBinder() is safe on NULL (returns NULL)
422 if (surface->asBinder() != mSurface->asBinder()) {
423 if (mSurface != 0 && !mUseOverlay) {
424 LOGD("clearing old preview surface %p", mSurface.get());
425 mSurface->unregisterBuffers();
426 }
427 mSurface = surface;
Wu-cheng Li988fb622009-06-23 23:37:36 +0800428 // If preview has been already started, set overlay or register preview
429 // buffers now.
430 if (mHardware->previewEnabled()) {
431 if (mUseOverlay) {
432 result = setOverlay();
433 } else if (mSurface != 0) {
434 result = registerPreviewBuffers();
435 }
436 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800437 }
Wu-cheng Li988fb622009-06-23 23:37:36 +0800438 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800439}
440
441// set the preview callback flag to affect how the received frames from
442// preview are handled.
443void CameraService::Client::setPreviewCallbackFlag(int callback_flag)
444{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800445 LOGV("setPreviewCallbackFlag (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800446 Mutex::Autolock lock(mLock);
447 if (checkPid() != NO_ERROR) return;
448 mPreviewCallbackFlag = callback_flag;
449}
450
Wu-cheng Li988fb622009-06-23 23:37:36 +0800451// start preview mode
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800452status_t CameraService::Client::startCameraMode(camera_mode mode)
453{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800454 int callingPid = getCallingPid();
455
456 LOGD("startCameraMode(%d) (pid %d)", mode, callingPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800457
458 /* we cannot call into mHardware with mLock held because
459 * mHardware has callbacks onto us which acquire this lock
460 */
461
462 Mutex::Autolock lock(mLock);
463 status_t result = checkPid();
464 if (result != NO_ERROR) return result;
465
466 if (mHardware == 0) {
467 LOGE("mHardware is NULL, returning.");
468 return INVALID_OPERATION;
469 }
470
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800471 switch(mode) {
472 case CAMERA_RECORDING_MODE:
Wu-cheng Li988fb622009-06-23 23:37:36 +0800473 if (mSurface == 0) {
474 LOGE("setPreviewDisplay must be called before startRecordingMode.");
475 return INVALID_OPERATION;
476 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800477 return startRecordingMode();
478
479 default: // CAMERA_PREVIEW_MODE
Wu-cheng Li988fb622009-06-23 23:37:36 +0800480 if (mSurface == 0) {
481 LOGD("mSurface is not set yet.");
482 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800483 return startPreviewMode();
484 }
485}
486
487status_t CameraService::Client::startRecordingMode()
488{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800489 LOGD("startRecordingMode (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800490
491 status_t ret = UNKNOWN_ERROR;
492
493 // if preview has not been started, start preview first
494 if (!mHardware->previewEnabled()) {
495 ret = startPreviewMode();
496 if (ret != NO_ERROR) {
497 return ret;
498 }
499 }
500
501 // if recording has been enabled, nothing needs to be done
502 if (mHardware->recordingEnabled()) {
503 return NO_ERROR;
504 }
505
506 // start recording mode
Dave Sparksf72d6402009-07-08 15:56:53 -0700507 ret = mHardware->startRecording(recordingCallback, mCameraService.get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800508 if (ret != NO_ERROR) {
509 LOGE("mHardware->startRecording() failed with status %d", ret);
510 }
511 return ret;
512}
513
Wu-cheng Li988fb622009-06-23 23:37:36 +0800514status_t CameraService::Client::setOverlay()
515{
516 LOGD("setOverlay");
517 int w, h;
518 CameraParameters params(mHardware->getParameters());
519 params.getPreviewSize(&w, &h);
520
521 const char *format = params.getPreviewFormat();
522 int fmt;
523 if (!strcmp(format, "yuv422i"))
524 fmt = OVERLAY_FORMAT_YCbCr_422_I;
525 else if (!strcmp(format, "rgb565"))
526 fmt = OVERLAY_FORMAT_RGB_565;
527 else {
528 LOGE("Invalid preview format for overlays");
529 return -EINVAL;
530 }
531
532 status_t ret = NO_ERROR;
533 if (mSurface != 0) {
534 sp<OverlayRef> ref = mSurface->createOverlay(w, h, fmt);
535 ret = mHardware->setOverlay(new Overlay(ref));
536 } else {
537 ret = mHardware->setOverlay(NULL);
538 }
539 if (ret != NO_ERROR) {
540 LOGE("mHardware->setOverlay() failed with status %d\n", ret);
541 }
542 return ret;
543}
544
545status_t CameraService::Client::registerPreviewBuffers()
546{
547 int w, h;
548 CameraParameters params(mHardware->getParameters());
549 params.getPreviewSize(&w, &h);
550
551 uint32_t transform = 0;
552 if (params.getOrientation() ==
553 CameraParameters::CAMERA_ORIENTATION_PORTRAIT) {
554 LOGV("portrait mode");
555 transform = ISurface::BufferHeap::ROT_90;
556 }
557 ISurface::BufferHeap buffers(w, h, w, h,
558 PIXEL_FORMAT_YCbCr_420_SP,
559 transform,
560 0,
561 mHardware->getPreviewHeap());
562
563 status_t ret = mSurface->registerBuffers(buffers);
564 if (ret != NO_ERROR) {
565 LOGE("registerBuffers failed with status %d", ret);
566 }
567 return ret;
568}
569
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800570status_t CameraService::Client::startPreviewMode()
571{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800572 LOGD("startPreviewMode (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800573
574 // if preview has been enabled, nothing needs to be done
575 if (mHardware->previewEnabled()) {
576 return NO_ERROR;
577 }
578
579 // start preview mode
580#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
581 debug_frame_cnt = 0;
582#endif
Wu-cheng Li988fb622009-06-23 23:37:36 +0800583 status_t ret = NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800584
585 if (mUseOverlay) {
Wu-cheng Li988fb622009-06-23 23:37:36 +0800586 // If preview display has been set, set overlay now.
587 if (mSurface != 0) {
588 ret = setOverlay();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800589 }
Wu-cheng Li988fb622009-06-23 23:37:36 +0800590 if (ret != NO_ERROR) return ret;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800591 ret = mHardware->startPreview(NULL, mCameraService.get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800592 } else {
593 ret = mHardware->startPreview(previewCallback,
594 mCameraService.get());
Wu-cheng Li988fb622009-06-23 23:37:36 +0800595 if (ret != NO_ERROR) return ret;
596 // If preview display has been set, register preview buffers now.
597 if (mSurface != 0) {
598 // Unregister here because the surface registered with raw heap.
599 mSurface->unregisterBuffers();
600 ret = registerPreviewBuffers();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800601 }
602 }
603 return ret;
604}
605
606status_t CameraService::Client::startPreview()
607{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800608 LOGD("startPreview (pid %d)", getCallingPid());
609
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800610 return startCameraMode(CAMERA_PREVIEW_MODE);
611}
612
613status_t CameraService::Client::startRecording()
614{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800615 LOGD("startRecording (pid %d)", getCallingPid());
616
Jason Sams78b877e2009-03-24 20:21:36 -0700617 if (mMediaPlayerBeep.get() != NULL) {
Andreas Hubera26e6062009-03-24 20:47:19 -0700618 mMediaPlayerBeep->seekTo(0);
Jason Sams78b877e2009-03-24 20:21:36 -0700619 mMediaPlayerBeep->start();
620 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800621 return startCameraMode(CAMERA_RECORDING_MODE);
622}
623
624// stop preview mode
625void CameraService::Client::stopPreview()
626{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800627 LOGD("stopPreview (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628
629 Mutex::Autolock lock(mLock);
630 if (checkPid() != NO_ERROR) return;
631
632 if (mHardware == 0) {
633 LOGE("mHardware is NULL, returning.");
634 return;
635 }
636
637 mHardware->stopPreview();
638 LOGD("stopPreview(), hardware stopped OK");
639
640 if (mSurface != 0 && !mUseOverlay) {
641 mSurface->unregisterBuffers();
642 }
643 mPreviewBuffer.clear();
644}
645
646// stop recording mode
647void CameraService::Client::stopRecording()
648{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800649 LOGD("stopRecording (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800650
651 Mutex::Autolock lock(mLock);
652 if (checkPid() != NO_ERROR) return;
653
654 if (mHardware == 0) {
655 LOGE("mHardware is NULL, returning.");
656 return;
657 }
658
Jason Sams78b877e2009-03-24 20:21:36 -0700659 if (mMediaPlayerBeep.get() != NULL) {
Andreas Hubera26e6062009-03-24 20:47:19 -0700660 mMediaPlayerBeep->seekTo(0);
Jason Sams78b877e2009-03-24 20:21:36 -0700661 mMediaPlayerBeep->start();
662 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800663 mHardware->stopRecording();
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800664 LOGD("stopRecording(), hardware stopped OK");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800665 mPreviewBuffer.clear();
666}
667
668// release a recording frame
669void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem)
670{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800671 Mutex::Autolock lock(mLock);
672 if (checkPid() != NO_ERROR) return;
673
674 if (mHardware == 0) {
675 LOGE("mHardware is NULL, returning.");
676 return;
677 }
678
679 mHardware->releaseRecordingFrame(mem);
680}
681
682bool CameraService::Client::previewEnabled()
683{
684 Mutex::Autolock lock(mLock);
685 if (mHardware == 0) return false;
686 return mHardware->previewEnabled();
687}
688
689bool CameraService::Client::recordingEnabled()
690{
691 Mutex::Autolock lock(mLock);
692 if (mHardware == 0) return false;
693 return mHardware->recordingEnabled();
694}
695
696// Safely retrieves a strong pointer to the client during a hardware callback.
697sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user)
698{
699 sp<Client> client = 0;
700 CameraService *service = static_cast<CameraService*>(user);
701 if (service != NULL) {
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +0800702 Mutex::Autolock ourLock(service->mServiceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800703 if (service->mClient != 0) {
704 client = service->mClient.promote();
705 if (client == 0) {
706 LOGE("getClientFromCookie: client appears to have died");
707 service->mClient.clear();
708 }
709 } else {
710 LOGE("getClientFromCookie: got callback but client was NULL");
711 }
712 }
713 return client;
714}
715
716
717#if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE || \
718 DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE || \
719 DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
720static void dump_to_file(const char *fname,
721 uint8_t *buf, uint32_t size)
722{
723 int nw, cnt = 0;
724 uint32_t written = 0;
725
726 LOGD("opening file [%s]\n", fname);
727 int fd = open(fname, O_RDWR | O_CREAT);
728 if (fd < 0) {
729 LOGE("failed to create file [%s]: %s", fname, strerror(errno));
730 return;
731 }
732
733 LOGD("writing %d bytes to file [%s]\n", size, fname);
734 while (written < size) {
735 nw = ::write(fd,
736 buf + written,
737 size - written);
738 if (nw < 0) {
739 LOGE("failed to write to file [%s]: %s",
740 fname, strerror(errno));
741 break;
742 }
743 written += nw;
744 cnt++;
745 }
746 LOGD("done writing %d bytes to file [%s] in %d passes\n",
747 size, fname, cnt);
748 ::close(fd);
749}
750#endif
751
752// preview callback - frame buffer update
753void CameraService::Client::previewCallback(const sp<IMemory>& mem, void* user)
754{
755 LOGV("previewCallback()");
756 sp<Client> client = getClientFromCookie(user);
757 if (client == 0) {
758 return;
759 }
760
761#if DEBUG_HEAP_LEAKS && 0 // debugging
762 if (gWeakHeap == NULL) {
763 ssize_t offset;
764 size_t size;
765 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
766 if (gWeakHeap != heap) {
767 LOGD("SETTING PREVIEW HEAP");
768 heap->trackMe(true, true);
769 gWeakHeap = heap;
770 }
771 }
772#endif
773
774#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
775 {
776 if (debug_frame_cnt++ == DEBUG_DUMP_PREVIEW_FRAME_TO_FILE) {
777 ssize_t offset;
778 size_t size;
779 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
780 dump_to_file("/data/preview.yuv",
781 (uint8_t *)heap->base() + offset, size);
782 }
783 }
784#endif
785
786 // The strong pointer guarantees the client will exist, but no lock is held.
787 client->postPreviewFrame(mem);
788
789#if DEBUG_CLIENT_REFERENCES
790 //**** if the client's refcount is 1, then we are about to destroy it here,
791 // which is bad--print all refcounts.
792 if (client->getStrongCount() == 1) {
793 LOGE("++++++++++++++++ (PREVIEW) THIS WILL CAUSE A LOCKUP!");
794 client->printRefs();
795 }
796#endif
797}
798
799// recording callback
Dave Sparksf72d6402009-07-08 15:56:53 -0700800void CameraService::Client::recordingCallback(nsecs_t timestamp, const sp<IMemory>& mem, void* user)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800801{
802 LOGV("recordingCallback");
803 sp<Client> client = getClientFromCookie(user);
804 if (client == 0) {
805 return;
806 }
807 // The strong pointer guarantees the client will exist, but no lock is held.
Dave Sparksf72d6402009-07-08 15:56:53 -0700808 client->postRecordingFrame(timestamp, mem);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800809}
810
811// take a picture - image is returned in callback
812status_t CameraService::Client::autoFocus()
813{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800814 LOGD("autoFocus (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800815
816 Mutex::Autolock lock(mLock);
817 status_t result = checkPid();
818 if (result != NO_ERROR) return result;
819
820 if (mHardware == 0) {
821 LOGE("mHardware is NULL, returning.");
822 return INVALID_OPERATION;
823 }
824
825 return mHardware->autoFocus(autoFocusCallback,
826 mCameraService.get());
827}
828
829// take a picture - image is returned in callback
830status_t CameraService::Client::takePicture()
831{
Chih-Chung Changd98c5162009-06-22 16:03:41 +0800832 LOGD("takePicture (pid %d)", getCallingPid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800833
834 Mutex::Autolock lock(mLock);
835 status_t result = checkPid();
836 if (result != NO_ERROR) return result;
837
838 if (mHardware == 0) {
839 LOGE("mHardware is NULL, returning.");
840 return INVALID_OPERATION;
841 }
842
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800843 return mHardware->takePicture(shutterCallback,
844 yuvPictureCallback,
845 jpegPictureCallback,
846 mCameraService.get());
847}
848
849// picture callback - snapshot taken
850void CameraService::Client::shutterCallback(void *user)
851{
852 sp<Client> client = getClientFromCookie(user);
853 if (client == 0) {
854 return;
855 }
856
Wu-cheng Liaab44b82009-03-24 20:39:09 -0700857 // Play shutter sound.
858 if (client->mMediaPlayerClick.get() != NULL) {
Andreas Hubera26e6062009-03-24 20:47:19 -0700859 client->mMediaPlayerClick->seekTo(0);
Wu-cheng Liaab44b82009-03-24 20:39:09 -0700860 client->mMediaPlayerClick->start();
861 }
862
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800863 // Screen goes black after the buffer is unregistered.
864 if (client->mSurface != 0 && !client->mUseOverlay) {
865 client->mSurface->unregisterBuffers();
866 }
867
868 client->postShutter();
869
870 // It takes some time before yuvPicture callback to be called.
871 // Register the buffer for raw image here to reduce latency.
872 if (client->mSurface != 0 && !client->mUseOverlay) {
873 int w, h;
874 CameraParameters params(client->mHardware->getParameters());
875 params.getPictureSize(&w, &h);
876 uint32_t transform = 0;
877 if (params.getOrientation() == CameraParameters::CAMERA_ORIENTATION_PORTRAIT) {
878 LOGV("portrait mode");
879 transform = ISurface::BufferHeap::ROT_90;
880 }
881 ISurface::BufferHeap buffers(w, h, w, h,
882 PIXEL_FORMAT_YCbCr_420_SP, transform, 0, client->mHardware->getRawHeap());
883
884 client->mSurface->registerBuffers(buffers);
885 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800886}
887
888// picture callback - raw image ready
889void CameraService::Client::yuvPictureCallback(const sp<IMemory>& mem,
890 void *user)
891{
892 sp<Client> client = getClientFromCookie(user);
893 if (client == 0) {
894 return;
895 }
896 if (mem == NULL) {
897 client->postRaw(NULL);
898 client->postError(UNKNOWN_ERROR);
899 return;
900 }
901
902 ssize_t offset;
903 size_t size;
904 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
905#if DEBUG_HEAP_LEAKS && 0 // debugging
906 gWeakHeap = heap; // debugging
907#endif
908
909 //LOGV("yuvPictureCallback(%d, %d, %p)", offset, size, user);
910#if DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE // for testing pursposes only
911 dump_to_file("/data/photo.yuv",
912 (uint8_t *)heap->base() + offset, size);
913#endif
914
915 // Put the YUV version of the snapshot in the preview display.
916 if (client->mSurface != 0 && !client->mUseOverlay) {
917 client->mSurface->postBuffer(offset);
918 }
919
920 client->postRaw(mem);
921
922#if DEBUG_CLIENT_REFERENCES
923 //**** if the client's refcount is 1, then we are about to destroy it here,
924 // which is bad--print all refcounts.
925 if (client->getStrongCount() == 1) {
926 LOGE("++++++++++++++++ (RAW) THIS WILL CAUSE A LOCKUP!");
927 client->printRefs();
928 }
929#endif
930}
931
932// picture callback - jpeg ready
933void CameraService::Client::jpegPictureCallback(const sp<IMemory>& mem, void *user)
934{
935 sp<Client> client = getClientFromCookie(user);
936 if (client == 0) {
937 return;
938 }
939 if (mem == NULL) {
940 client->postJpeg(NULL);
941 client->postError(UNKNOWN_ERROR);
942 return;
943 }
944
945 /** We absolutely CANNOT call into user code with a lock held **/
946
947#if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE // for testing pursposes only
948 {
949 ssize_t offset;
950 size_t size;
951 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
952 dump_to_file("/data/photo.jpg",
953 (uint8_t *)heap->base() + offset, size);
954 }
955#endif
956
957 client->postJpeg(mem);
958
959#if DEBUG_CLIENT_REFERENCES
960 //**** if the client's refcount is 1, then we are about to destroy it here,
961 // which is bad--print all refcounts.
962 if (client->getStrongCount() == 1) {
963 LOGE("++++++++++++++++ (JPEG) THIS WILL CAUSE A LOCKUP!");
964 client->printRefs();
965 }
966#endif
967}
968
969void CameraService::Client::autoFocusCallback(bool focused, void *user)
970{
971 LOGV("autoFocusCallback");
972
973 sp<Client> client = getClientFromCookie(user);
974 if (client == 0) {
975 return;
976 }
977
978 client->postAutoFocus(focused);
979
980#if DEBUG_CLIENT_REFERENCES
981 if (client->getStrongCount() == 1) {
982 LOGE("++++++++++++++++ (AUTOFOCUS) THIS WILL CAUSE A LOCKUP!");
983 client->printRefs();
984 }
985#endif
986}
987
988// set preview/capture parameters - key/value pairs
989status_t CameraService::Client::setParameters(const String8& params)
990{
991 LOGD("setParameters(%s)", params.string());
992
993 Mutex::Autolock lock(mLock);
994 status_t result = checkPid();
995 if (result != NO_ERROR) return result;
996
997 if (mHardware == 0) {
998 LOGE("mHardware is NULL, returning.");
999 return INVALID_OPERATION;
1000 }
1001
1002 CameraParameters p(params);
1003 mHardware->setParameters(p);
1004 return NO_ERROR;
1005}
1006
1007// get preview/capture parameters - key/value pairs
1008String8 CameraService::Client::getParameters() const
1009{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001010 Mutex::Autolock lock(mLock);
1011
1012 if (mHardware == 0) {
1013 LOGE("mHardware is NULL, returning.");
1014 return String8();
1015 }
1016
Wu-cheng Li81d763f2009-04-22 16:21:26 +08001017 String8 params(mHardware->getParameters().flatten());
1018 LOGD("getParameters(%s)", params.string());
1019 return params;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001020}
1021
1022void CameraService::Client::postAutoFocus(bool focused)
1023{
1024 LOGV("postAutoFocus");
Dave Sparks93b94582009-05-07 19:27:32 -07001025 mCameraClient->notifyCallback(CAMERA_MSG_FOCUS, (int32_t)focused, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001026}
1027
1028void CameraService::Client::postShutter()
1029{
Chih-Chung Changd98c5162009-06-22 16:03:41 +08001030 LOGD("postShutter");
Dave Sparks93b94582009-05-07 19:27:32 -07001031 mCameraClient->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001032}
1033
1034void CameraService::Client::postRaw(const sp<IMemory>& mem)
1035{
1036 LOGD("postRaw");
Dave Sparks93b94582009-05-07 19:27:32 -07001037 mCameraClient->dataCallback(CAMERA_MSG_RAW_IMAGE, mem);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001038}
1039
1040void CameraService::Client::postJpeg(const sp<IMemory>& mem)
1041{
1042 LOGD("postJpeg");
Dave Sparks93b94582009-05-07 19:27:32 -07001043 mCameraClient->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001044}
1045
1046void CameraService::Client::copyFrameAndPostCopiedFrame(sp<IMemoryHeap> heap, size_t offset, size_t size)
1047{
1048 LOGV("copyFrameAndPostCopiedFrame");
1049 // It is necessary to copy out of pmem before sending this to
1050 // the callback. For efficiency, reuse the same MemoryHeapBase
1051 // provided it's big enough. Don't allocate the memory or
1052 // perform the copy if there's no callback.
1053 if (mPreviewBuffer == 0) {
1054 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1055 } else if (size > mPreviewBuffer->virtualSize()) {
1056 mPreviewBuffer.clear();
1057 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1058 if (mPreviewBuffer == 0) {
1059 LOGE("failed to allocate space for preview buffer");
1060 return;
1061 }
1062 }
1063 memcpy(mPreviewBuffer->base(),
1064 (uint8_t *)heap->base() + offset, size);
1065
1066 sp<MemoryBase> frame = new MemoryBase(mPreviewBuffer, 0, size);
1067 if (frame == 0) {
1068 LOGE("failed to allocate space for frame callback");
1069 return;
1070 }
Dave Sparks93b94582009-05-07 19:27:32 -07001071 mCameraClient->dataCallback(CAMERA_MSG_PREVIEW_FRAME, frame);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072}
1073
Dave Sparksf72d6402009-07-08 15:56:53 -07001074void CameraService::Client::postRecordingFrame(nsecs_t timestamp, const sp<IMemory>& frame)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001075{
1076 LOGV("postRecordingFrame");
1077 if (frame == 0) {
1078 LOGW("frame is a null pointer");
1079 return;
1080 }
Dave Sparksf72d6402009-07-08 15:56:53 -07001081 mCameraClient->dataCallbackTimestamp(timestamp, CAMERA_MSG_VIDEO_FRAME, frame);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001082}
1083
1084void CameraService::Client::postPreviewFrame(const sp<IMemory>& mem)
1085{
1086 LOGV("postPreviewFrame");
1087 if (mem == 0) {
1088 LOGW("mem is a null pointer");
1089 return;
1090 }
1091
1092 ssize_t offset;
1093 size_t size;
1094 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1095 {
1096 Mutex::Autolock surfaceLock(mSurfaceLock);
1097 if (mSurface != NULL) {
1098 mSurface->postBuffer(offset);
1099 }
1100 }
1101
1102 // Is the callback enabled or not?
1103 if (!(mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
1104 // If the enable bit is off, the copy-out and one-shot bits are ignored
1105 LOGV("frame callback is diabled");
1106 return;
1107 }
1108
1109 // Is the received frame copied out or not?
1110 if (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
1111 LOGV("frame is copied out");
1112 copyFrameAndPostCopiedFrame(heap, offset, size);
1113 } else {
1114 LOGV("frame is directly sent out without copying");
Dave Sparks93b94582009-05-07 19:27:32 -07001115 mCameraClient->dataCallback(CAMERA_MSG_PREVIEW_FRAME, mem);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001116 }
1117
1118 // Is this is one-shot only?
1119 if (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ONE_SHOT_MASK) {
1120 LOGV("One-shot only, thus clear the bits and disable frame callback");
1121 mPreviewCallbackFlag &= ~(FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
1122 FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
1123 FRAME_CALLBACK_FLAG_ENABLE_MASK);
1124 }
1125}
1126
1127void CameraService::Client::postError(status_t error)
1128{
Dave Sparks93b94582009-05-07 19:27:32 -07001129 mCameraClient->notifyCallback(CAMERA_MSG_ERROR, error, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001130}
1131
1132status_t CameraService::dump(int fd, const Vector<String16>& args)
1133{
1134 const size_t SIZE = 256;
1135 char buffer[SIZE];
1136 String8 result;
1137 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
1138 snprintf(buffer, SIZE, "Permission Denial: "
1139 "can't dump CameraService from pid=%d, uid=%d\n",
Chih-Chung Changd98c5162009-06-22 16:03:41 +08001140 getCallingPid(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001141 IPCThreadState::self()->getCallingUid());
1142 result.append(buffer);
1143 write(fd, result.string(), result.size());
1144 } else {
Chih-Chung Changd2d6bc72009-06-24 19:59:31 +08001145 AutoMutex lock(&mServiceLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001146 if (mClient != 0) {
1147 sp<Client> currentClient = mClient.promote();
1148 sprintf(buffer, "Client (%p) PID: %d\n",
1149 currentClient->getCameraClient()->asBinder().get(),
1150 currentClient->mClientPid);
1151 result.append(buffer);
1152 write(fd, result.string(), result.size());
1153 currentClient->mHardware->dump(fd, args);
1154 } else {
1155 result.append("No camera client yet.\n");
1156 write(fd, result.string(), result.size());
1157 }
1158 }
1159 return NO_ERROR;
1160}
1161
1162
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001163status_t CameraService::onTransact(
1164 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1165{
1166 // permission checks...
1167 switch (code) {
1168 case BnCameraService::CONNECT:
1169 IPCThreadState* ipc = IPCThreadState::self();
1170 const int pid = ipc->getCallingPid();
1171 const int self_pid = getpid();
1172 if (pid != self_pid) {
1173 // we're called from a different process, do the real check
1174 if (!checkCallingPermission(
1175 String16("android.permission.CAMERA")))
1176 {
1177 const int uid = ipc->getCallingUid();
1178 LOGE("Permission Denial: "
1179 "can't use the camera pid=%d, uid=%d", pid, uid);
1180 return PERMISSION_DENIED;
1181 }
1182 }
1183 break;
1184 }
1185
1186 status_t err = BnCameraService::onTransact(code, data, reply, flags);
1187
Dave Sparks998b3292009-05-20 20:02:59 -07001188#if DEBUG_HEAP_LEAKS
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189 LOGD("+++ onTransact err %d code %d", err, code);
1190
1191 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
1192 // the 'service' command interrogates this binder for its name, and then supplies it
1193 // even for the debugging commands. that means we need to check for it here, using
1194 // ISurfaceComposer (since we delegated the INTERFACE_TRANSACTION handling to
1195 // BnSurfaceComposer before falling through to this code).
1196
1197 LOGD("+++ onTransact code %d", code);
1198
1199 CHECK_INTERFACE(ICameraService, data, reply);
1200
1201 switch(code) {
1202 case 1000:
1203 {
1204 if (gWeakHeap != 0) {
1205 sp<IMemoryHeap> h = gWeakHeap.promote();
1206 IMemoryHeap *p = gWeakHeap.unsafe_get();
1207 LOGD("CHECKING WEAK REFERENCE %p (%p)", h.get(), p);
1208 if (h != 0)
1209 h->printRefs();
1210 bool attempt_to_delete = data.readInt32() == 1;
1211 if (attempt_to_delete) {
1212 // NOT SAFE!
1213 LOGD("DELETING WEAK REFERENCE %p (%p)", h.get(), p);
1214 if (p) delete p;
1215 }
1216 return NO_ERROR;
1217 }
1218 }
1219 break;
1220 default:
1221 break;
1222 }
1223 }
Dave Sparks998b3292009-05-20 20:02:59 -07001224#endif // DEBUG_HEAP_LEAKS
Dave Sparksfec880d2009-05-21 09:18:18 -07001225
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001226 return err;
1227}
1228
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001229}; // namespace android