2009-03-04 03:31:44 +00:00
|
|
|
/*
|
|
|
|
**
|
|
|
|
** Copyright (C) 2008, The Android Open Source Project
|
|
|
|
** Copyright (C) 2008 HTC Inc.
|
|
|
|
**
|
|
|
|
** Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
** you may not use this file except in compliance with the License.
|
|
|
|
** You may obtain a copy of the License at
|
|
|
|
**
|
|
|
|
** http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
**
|
|
|
|
** Unless required by applicable law or agreed to in writing, software
|
|
|
|
** distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
** See the License for the specific language governing permissions and
|
|
|
|
** limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LOG_TAG "CameraService"
|
|
|
|
#include <utils/Log.h>
|
|
|
|
|
2009-05-20 02:08:10 +00:00
|
|
|
#include <binder/IServiceManager.h>
|
|
|
|
#include <binder/IPCThreadState.h>
|
2009-03-04 03:31:44 +00:00
|
|
|
#include <utils/String16.h>
|
|
|
|
#include <utils/Errors.h>
|
2009-05-20 02:08:10 +00:00
|
|
|
#include <binder/MemoryBase.h>
|
|
|
|
#include <binder/MemoryHeapBase.h>
|
2009-03-04 03:31:44 +00:00
|
|
|
#include <ui/ICameraService.h>
|
|
|
|
|
2009-03-25 03:21:36 +00:00
|
|
|
#include <media/mediaplayer.h>
|
|
|
|
#include <media/AudioSystem.h>
|
2009-03-04 03:31:44 +00:00
|
|
|
#include "CameraService.h"
|
|
|
|
|
2009-06-24 05:44:37 +00:00
|
|
|
#include <cutils/atomic.h>
|
2009-03-27 23:27:16 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
namespace android {
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <pthread.h>
|
2009-06-22 08:03:41 +00:00
|
|
|
#include <signal.h>
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// When you enable this, as well as DEBUG_REFS=1 and
|
|
|
|
// DEBUG_REFS_ENABLED_BY_DEFAULT=0 in libutils/RefBase.cpp, this will track all
|
|
|
|
// references to the CameraService::Client in order to catch the case where the
|
|
|
|
// client is being destroyed while a callback from the CameraHardwareInterface
|
|
|
|
// is outstanding. This is a serious bug because if we make another call into
|
|
|
|
// CameraHardwreInterface that itself triggers a callback, we will deadlock.
|
|
|
|
|
|
|
|
#define DEBUG_CLIENT_REFERENCES 0
|
|
|
|
|
|
|
|
#define PICTURE_TIMEOUT seconds(5)
|
|
|
|
|
|
|
|
#define DEBUG_DUMP_PREVIEW_FRAME_TO_FILE 0 /* n-th frame to write */
|
|
|
|
#define DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE 0
|
|
|
|
#define DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE 0
|
2009-08-12 17:01:27 +00:00
|
|
|
#define DEBUG_DUMP_POSTVIEW_SNAPSHOT_TO_FILE 0
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
|
|
|
|
static int debug_frame_cnt;
|
|
|
|
#endif
|
|
|
|
|
2009-06-22 08:03:41 +00:00
|
|
|
static int getCallingPid() {
|
|
|
|
return IPCThreadState::self()->getCallingPid();
|
|
|
|
}
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void CameraService::instantiate() {
|
|
|
|
defaultServiceManager()->addService(
|
|
|
|
String16("media.camera"), new CameraService());
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
CameraService::CameraService() :
|
|
|
|
BnCameraService()
|
|
|
|
{
|
|
|
|
LOGI("CameraService started: pid=%d", getpid());
|
2009-06-24 05:44:37 +00:00
|
|
|
mUsers = 0;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CameraService::~CameraService()
|
|
|
|
{
|
|
|
|
if (mClient != 0) {
|
|
|
|
LOGE("mClient was still connected in destructor!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sp<ICamera> CameraService::connect(const sp<ICameraClient>& cameraClient)
|
|
|
|
{
|
2009-06-22 08:03:41 +00:00
|
|
|
int callingPid = getCallingPid();
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("CameraService::connect E (pid %d, client %p)", callingPid,
|
2009-06-22 08:03:41 +00:00
|
|
|
cameraClient->asBinder().get());
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-06-24 11:59:31 +00:00
|
|
|
Mutex::Autolock lock(mServiceLock);
|
2009-03-04 03:31:44 +00:00
|
|
|
sp<Client> client;
|
|
|
|
if (mClient != 0) {
|
|
|
|
sp<Client> currentClient = mClient.promote();
|
|
|
|
if (currentClient != 0) {
|
|
|
|
sp<ICameraClient> currentCameraClient(currentClient->getCameraClient());
|
|
|
|
if (cameraClient->asBinder() == currentCameraClient->asBinder()) {
|
2009-06-22 08:03:41 +00:00
|
|
|
// This is the same client reconnecting...
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("CameraService::connect X (pid %d, same client %p) is reconnecting...",
|
2009-06-22 08:03:41 +00:00
|
|
|
callingPid, cameraClient->asBinder().get());
|
2009-03-04 03:31:44 +00:00
|
|
|
return currentClient;
|
|
|
|
} else {
|
2009-06-22 08:03:41 +00:00
|
|
|
// It's another client... reject it
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("CameraService::connect X (pid %d, new client %p) rejected. "
|
2009-06-22 08:03:41 +00:00
|
|
|
"(old pid %d, old client %p)",
|
|
|
|
callingPid, cameraClient->asBinder().get(),
|
|
|
|
currentClient->mClientPid, currentCameraClient->asBinder().get());
|
2009-06-24 05:44:37 +00:00
|
|
|
if (kill(currentClient->mClientPid, 0) == -1 && errno == ESRCH) {
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("The old client is dead!");
|
2009-06-22 08:03:41 +00:00
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
return client;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// can't promote, the previous client has died...
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("New client (pid %d) connecting, old reference was dangling...",
|
2009-06-22 08:03:41 +00:00
|
|
|
callingPid);
|
2009-03-04 03:31:44 +00:00
|
|
|
mClient.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-24 11:59:31 +00:00
|
|
|
if (mUsers > 0) {
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("Still have client, rejected");
|
2009-06-24 11:59:31 +00:00
|
|
|
return client;
|
|
|
|
}
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
// create a new Client object
|
2009-06-22 08:03:41 +00:00
|
|
|
client = new Client(this, cameraClient, callingPid);
|
2009-03-04 03:31:44 +00:00
|
|
|
mClient = client;
|
|
|
|
#if DEBUG_CLIENT_REFERENCES
|
|
|
|
// Enable tracking for this object, and track increments and decrements of
|
|
|
|
// the refcount.
|
|
|
|
client->trackMe(true, true);
|
|
|
|
#endif
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("CameraService::connect X");
|
2009-03-04 03:31:44 +00:00
|
|
|
return client;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CameraService::removeClient(const sp<ICameraClient>& cameraClient)
|
|
|
|
{
|
2009-06-22 08:03:41 +00:00
|
|
|
int callingPid = getCallingPid();
|
|
|
|
|
|
|
|
// Declare this outside the lock to make absolutely sure the
|
2009-03-04 03:31:44 +00:00
|
|
|
// destructor won't be called with the lock held.
|
|
|
|
sp<Client> client;
|
|
|
|
|
2009-06-24 11:59:31 +00:00
|
|
|
Mutex::Autolock lock(mServiceLock);
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
if (mClient == 0) {
|
|
|
|
// This happens when we have already disconnected.
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("removeClient (pid %d): already disconnected", callingPid);
|
2009-03-04 03:31:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-06-22 08:03:41 +00:00
|
|
|
// Promote mClient. It can fail if we are called from this path:
|
|
|
|
// Client::~Client() -> disconnect() -> removeClient().
|
2009-03-04 03:31:44 +00:00
|
|
|
client = mClient.promote();
|
|
|
|
if (client == 0) {
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("removeClient (pid %d): no more strong reference", callingPid);
|
2009-03-04 03:31:44 +00:00
|
|
|
mClient.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cameraClient->asBinder() != client->getCameraClient()->asBinder()) {
|
|
|
|
// ugh! that's not our client!!
|
2009-06-22 08:03:41 +00:00
|
|
|
LOGW("removeClient (pid %d): mClient doesn't match!", callingPid);
|
2009-03-04 03:31:44 +00:00
|
|
|
} else {
|
|
|
|
// okay, good, forget about mClient
|
|
|
|
mClient.clear();
|
|
|
|
}
|
2009-06-22 08:03:41 +00:00
|
|
|
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("removeClient (pid %d) done", callingPid);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-06-24 05:44:37 +00:00
|
|
|
// The reason we need this count is a new CameraService::connect() request may
|
|
|
|
// come in while the previous Client's destructor has not been run or is still
|
|
|
|
// running. If the last strong reference of the previous Client is gone but
|
|
|
|
// destructor has not been run, we should not allow the new Client to be created
|
|
|
|
// because we need to wait for the previous Client to tear down the hardware
|
|
|
|
// first.
|
|
|
|
void CameraService::incUsers() {
|
|
|
|
android_atomic_inc(&mUsers);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CameraService::decUsers() {
|
|
|
|
android_atomic_dec(&mUsers);
|
|
|
|
}
|
|
|
|
|
2009-09-28 23:14:58 +00:00
|
|
|
static sp<MediaPlayer> newMediaPlayer(const char *file)
|
2009-03-25 03:21:36 +00:00
|
|
|
{
|
|
|
|
sp<MediaPlayer> mp = new MediaPlayer();
|
|
|
|
if (mp->setDataSource(file) == NO_ERROR) {
|
2009-07-17 19:17:14 +00:00
|
|
|
mp->setAudioStreamType(AudioSystem::ENFORCED_AUDIBLE);
|
2009-03-25 03:21:36 +00:00
|
|
|
mp->prepare();
|
|
|
|
} else {
|
|
|
|
mp.clear();
|
|
|
|
LOGE("Failed to load CameraService sounds.");
|
|
|
|
}
|
|
|
|
return mp;
|
|
|
|
}
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
CameraService::Client::Client(const sp<CameraService>& cameraService,
|
|
|
|
const sp<ICameraClient>& cameraClient, pid_t clientPid)
|
|
|
|
{
|
2009-06-22 08:03:41 +00:00
|
|
|
int callingPid = getCallingPid();
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("Client::Client E (pid %d)", callingPid);
|
2009-03-04 03:31:44 +00:00
|
|
|
mCameraService = cameraService;
|
|
|
|
mCameraClient = cameraClient;
|
|
|
|
mClientPid = clientPid;
|
|
|
|
mHardware = openCameraHardware();
|
|
|
|
mUseOverlay = mHardware->useOverlay();
|
|
|
|
|
2009-08-12 17:01:27 +00:00
|
|
|
mHardware->setCallbacks(notifyCallback,
|
|
|
|
dataCallback,
|
|
|
|
dataCallbackTimestamp,
|
|
|
|
mCameraService.get());
|
|
|
|
|
|
|
|
// Enable zoom, error, and focus messages by default
|
|
|
|
mHardware->enableMsgType(CAMERA_MSG_ERROR |
|
|
|
|
CAMERA_MSG_ZOOM |
|
|
|
|
CAMERA_MSG_FOCUS);
|
|
|
|
|
2009-03-25 03:21:36 +00:00
|
|
|
mMediaPlayerClick = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
|
|
|
|
mMediaPlayerBeep = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
|
2009-07-15 23:44:27 +00:00
|
|
|
mOverlayW = 0;
|
|
|
|
mOverlayH = 0;
|
2009-03-25 03:21:36 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
// Callback is disabled by default
|
|
|
|
mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
|
2010-01-22 01:31:06 +00:00
|
|
|
mOrientation = 0;
|
2009-06-24 05:44:37 +00:00
|
|
|
cameraService->incUsers();
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("Client::Client X (pid %d)", callingPid);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status_t CameraService::Client::checkPid()
|
|
|
|
{
|
2009-06-22 08:03:41 +00:00
|
|
|
int callingPid = getCallingPid();
|
|
|
|
if (mClientPid == callingPid) return NO_ERROR;
|
|
|
|
LOGW("Attempt to use locked camera (client %p) from different process "
|
|
|
|
" (old pid %d, new pid %d)",
|
|
|
|
getCameraClient()->asBinder().get(), mClientPid, callingPid);
|
2009-03-04 03:31:44 +00:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t CameraService::Client::lock()
|
|
|
|
{
|
2009-06-22 08:03:41 +00:00
|
|
|
int callingPid = getCallingPid();
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("lock from pid %d (mClientPid %d)", callingPid, mClientPid);
|
2009-03-04 03:31:44 +00:00
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
// lock camera to this client if the the camera is unlocked
|
|
|
|
if (mClientPid == 0) {
|
2009-06-22 08:03:41 +00:00
|
|
|
mClientPid = callingPid;
|
2009-03-04 03:31:44 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
// returns NO_ERROR if the client already owns the camera, -EBUSY otherwise
|
|
|
|
return checkPid();
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t CameraService::Client::unlock()
|
|
|
|
{
|
2009-06-22 08:03:41 +00:00
|
|
|
int callingPid = getCallingPid();
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("unlock from pid %d (mClientPid %d)", callingPid, mClientPid);
|
2009-03-04 03:31:44 +00:00
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
// allow anyone to use camera
|
|
|
|
status_t result = checkPid();
|
2009-04-21 02:35:28 +00:00
|
|
|
if (result == NO_ERROR) {
|
|
|
|
mClientPid = 0;
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("clear mCameraClient (pid %d)", callingPid);
|
2009-04-21 02:35:28 +00:00
|
|
|
// we need to remove the reference so that when app goes
|
|
|
|
// away, the reference count goes to 0.
|
|
|
|
mCameraClient.clear();
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t CameraService::Client::connect(const sp<ICameraClient>& client)
|
|
|
|
{
|
2009-06-22 08:03:41 +00:00
|
|
|
int callingPid = getCallingPid();
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
// connect a new process to the camera
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("Client::connect E (pid %d, client %p)", callingPid, client->asBinder().get());
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
// I hate this hack, but things get really ugly when the media recorder
|
|
|
|
// service is handing back the camera to the app. The ICameraClient
|
|
|
|
// destructor will be called during the same IPC, making it look like
|
|
|
|
// the remote client is trying to disconnect. This hack temporarily
|
|
|
|
// sets the mClientPid to an invalid pid to prevent the hardware from
|
2009-06-22 08:03:41 +00:00
|
|
|
// being torn down.
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
// hold a reference to the old client or we will deadlock if the client is
|
|
|
|
// in the same process and we hold the lock when we remove the reference
|
|
|
|
sp<ICameraClient> oldClient;
|
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mLock);
|
2009-06-09 05:56:44 +00:00
|
|
|
if (mClientPid != 0 && checkPid() != NO_ERROR) {
|
2009-06-22 08:03:41 +00:00
|
|
|
LOGW("Tried to connect to locked camera (old pid %d, new pid %d)",
|
|
|
|
mClientPid, callingPid);
|
2009-03-04 03:31:44 +00:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
oldClient = mCameraClient;
|
|
|
|
|
|
|
|
// did the client actually change?
|
2009-10-15 17:02:22 +00:00
|
|
|
if ((mCameraClient != NULL) && (client->asBinder() == mCameraClient->asBinder())) {
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("Connect to the same client");
|
2009-06-22 08:03:41 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
mCameraClient = client;
|
|
|
|
mClientPid = -1;
|
|
|
|
mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("Connect to the new client (pid %d, client %p)",
|
2009-06-22 08:03:41 +00:00
|
|
|
callingPid, mCameraClient->asBinder().get());
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
// the old client destructor is called when oldClient goes out of scope
|
|
|
|
// now we set the new PID to lock the interface again
|
2009-06-22 08:03:41 +00:00
|
|
|
mClientPid = callingPid;
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if HAVE_ANDROID_OS
|
|
|
|
static void *unregister_surface(void *arg)
|
|
|
|
{
|
|
|
|
ISurface *surface = (ISurface *)arg;
|
|
|
|
surface->unregisterBuffers();
|
|
|
|
IPCThreadState::self()->flushCommands();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CameraService::Client::~Client()
|
|
|
|
{
|
2009-06-22 08:03:41 +00:00
|
|
|
int callingPid = getCallingPid();
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
// tear down client
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("Client::~Client E (pid %d, client %p)",
|
2009-06-22 08:03:41 +00:00
|
|
|
callingPid, getCameraClient()->asBinder().get());
|
2009-03-04 03:31:44 +00:00
|
|
|
if (mSurface != 0 && !mUseOverlay) {
|
|
|
|
#if HAVE_ANDROID_OS
|
|
|
|
pthread_t thr;
|
|
|
|
// We unregister the buffers in a different thread because binder does
|
|
|
|
// not let us make sychronous transactions in a binder destructor (that
|
|
|
|
// is, upon our reaching a refcount of zero.)
|
|
|
|
pthread_create(&thr, NULL,
|
|
|
|
unregister_surface,
|
|
|
|
mSurface.get());
|
|
|
|
pthread_join(thr, NULL);
|
|
|
|
#else
|
|
|
|
mSurface->unregisterBuffers();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-03-25 03:36:57 +00:00
|
|
|
if (mMediaPlayerBeep.get() != NULL) {
|
|
|
|
mMediaPlayerBeep->disconnect();
|
|
|
|
mMediaPlayerBeep.clear();
|
|
|
|
}
|
|
|
|
if (mMediaPlayerClick.get() != NULL) {
|
|
|
|
mMediaPlayerClick->disconnect();
|
|
|
|
mMediaPlayerClick.clear();
|
|
|
|
}
|
2009-03-25 03:21:36 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
// make sure we tear down the hardware
|
2009-06-22 08:03:41 +00:00
|
|
|
mClientPid = callingPid;
|
2009-03-04 03:31:44 +00:00
|
|
|
disconnect();
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("Client::~Client X (pid %d)", mClientPid);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CameraService::Client::disconnect()
|
|
|
|
{
|
2009-06-24 05:44:37 +00:00
|
|
|
int callingPid = getCallingPid();
|
2009-06-22 08:03:41 +00:00
|
|
|
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("Client::disconnect() E (pid %d client %p)",
|
2009-06-22 08:03:41 +00:00
|
|
|
callingPid, getCameraClient()->asBinder().get());
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
Mutex::Autolock lock(mLock);
|
|
|
|
if (mClientPid <= 0) {
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
|
2009-03-04 03:31:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (checkPid() != NO_ERROR) {
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("Different client - don't disconnect");
|
2009-03-04 03:31:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-06-24 05:44:37 +00:00
|
|
|
// Make sure disconnect() is done once and once only, whether it is called
|
|
|
|
// from the user directly, or called by the destructor.
|
|
|
|
if (mHardware == 0) return;
|
|
|
|
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("hardware teardown");
|
2009-06-24 05:44:37 +00:00
|
|
|
// Before destroying mHardware, we must make sure it's in the
|
|
|
|
// idle state.
|
|
|
|
mHardware->stopPreview();
|
|
|
|
// Cancel all picture callbacks.
|
2009-08-12 17:01:27 +00:00
|
|
|
mHardware->disableMsgType(CAMERA_MSG_SHUTTER |
|
|
|
|
CAMERA_MSG_POSTVIEW_FRAME |
|
|
|
|
CAMERA_MSG_RAW_IMAGE |
|
|
|
|
CAMERA_MSG_COMPRESSED_IMAGE);
|
|
|
|
mHardware->cancelPicture();
|
|
|
|
// Turn off remaining messages.
|
|
|
|
mHardware->disableMsgType(CAMERA_MSG_ALL_MSGS);
|
2009-06-24 05:44:37 +00:00
|
|
|
// Release the hardware resources.
|
|
|
|
mHardware->release();
|
2009-07-15 23:44:27 +00:00
|
|
|
// Release the held overlay resources.
|
|
|
|
if (mUseOverlay)
|
|
|
|
{
|
|
|
|
mOverlayRef = 0;
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
mHardware.clear();
|
2009-06-24 05:44:37 +00:00
|
|
|
|
2009-06-24 11:59:31 +00:00
|
|
|
mCameraService->removeClient(mCameraClient);
|
2009-06-24 05:44:37 +00:00
|
|
|
mCameraService->decUsers();
|
|
|
|
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("Client::disconnect() X (pid %d)", callingPid);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// pass the buffered ISurface to the camera service
|
|
|
|
status_t CameraService::Client::setPreviewDisplay(const sp<ISurface>& surface)
|
|
|
|
{
|
2010-01-04 16:55:04 +00:00
|
|
|
LOGV("setPreviewDisplay(%p) (pid %d)",
|
2009-06-23 15:37:36 +00:00
|
|
|
((surface == NULL) ? NULL : surface.get()), getCallingPid());
|
2009-03-04 03:31:44 +00:00
|
|
|
Mutex::Autolock lock(mLock);
|
|
|
|
status_t result = checkPid();
|
|
|
|
if (result != NO_ERROR) return result;
|
2009-06-23 15:37:36 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
Mutex::Autolock surfaceLock(mSurfaceLock);
|
2009-06-23 15:37:36 +00:00
|
|
|
result = NO_ERROR;
|
2009-03-04 03:31:44 +00:00
|
|
|
// asBinder() is safe on NULL (returns NULL)
|
|
|
|
if (surface->asBinder() != mSurface->asBinder()) {
|
2009-07-15 23:44:27 +00:00
|
|
|
if (mSurface != 0) {
|
2010-01-04 16:55:04 +00:00
|
|
|
LOGV("clearing old preview surface %p", mSurface.get());
|
2009-07-15 23:44:27 +00:00
|
|
|
if ( !mUseOverlay)
|
|
|
|
{
|
|
|
|
mSurface->unregisterBuffers();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Force the destruction of any previous overlay
|
|
|
|
sp<Overlay> dummy;
|
|
|
|
mHardware->setOverlay( dummy );
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
mSurface = surface;
|
2009-07-15 23:44:27 +00:00
|
|
|
mOverlayRef = 0;
|
2009-06-23 15:37:36 +00:00
|
|
|
// If preview has been already started, set overlay or register preview
|
|
|
|
// buffers now.
|
|
|
|
if (mHardware->previewEnabled()) {
|
|
|
|
if (mUseOverlay) {
|
|
|
|
result = setOverlay();
|
|
|
|
} else if (mSurface != 0) {
|
|
|
|
result = registerPreviewBuffers();
|
|
|
|
}
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2009-06-23 15:37:36 +00:00
|
|
|
return result;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// set the preview callback flag to affect how the received frames from
|
|
|
|
// preview are handled.
|
|
|
|
void CameraService::Client::setPreviewCallbackFlag(int callback_flag)
|
|
|
|
{
|
2009-06-22 08:03:41 +00:00
|
|
|
LOGV("setPreviewCallbackFlag (pid %d)", getCallingPid());
|
2009-03-04 03:31:44 +00:00
|
|
|
Mutex::Autolock lock(mLock);
|
|
|
|
if (checkPid() != NO_ERROR) return;
|
|
|
|
mPreviewCallbackFlag = callback_flag;
|
2009-08-12 17:01:27 +00:00
|
|
|
|
|
|
|
if(mUseOverlay) {
|
|
|
|
if(mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ENABLE_MASK)
|
|
|
|
mHardware->enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
|
|
|
|
else
|
|
|
|
mHardware->disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-06-23 15:37:36 +00:00
|
|
|
// start preview mode
|
2009-03-04 03:31:44 +00:00
|
|
|
status_t CameraService::Client::startCameraMode(camera_mode mode)
|
|
|
|
{
|
2009-06-22 08:03:41 +00:00
|
|
|
int callingPid = getCallingPid();
|
|
|
|
|
2010-01-04 16:55:04 +00:00
|
|
|
LOGV("startCameraMode(%d) (pid %d)", mode, callingPid);
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
/* we cannot call into mHardware with mLock held because
|
|
|
|
* mHardware has callbacks onto us which acquire this lock
|
|
|
|
*/
|
|
|
|
|
|
|
|
Mutex::Autolock lock(mLock);
|
|
|
|
status_t result = checkPid();
|
|
|
|
if (result != NO_ERROR) return result;
|
|
|
|
|
|
|
|
if (mHardware == 0) {
|
|
|
|
LOGE("mHardware is NULL, returning.");
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(mode) {
|
|
|
|
case CAMERA_RECORDING_MODE:
|
2009-06-23 15:37:36 +00:00
|
|
|
if (mSurface == 0) {
|
|
|
|
LOGE("setPreviewDisplay must be called before startRecordingMode.");
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
return startRecordingMode();
|
|
|
|
|
|
|
|
default: // CAMERA_PREVIEW_MODE
|
2009-06-23 15:37:36 +00:00
|
|
|
if (mSurface == 0) {
|
2010-01-04 16:55:04 +00:00
|
|
|
LOGV("mSurface is not set yet.");
|
2009-06-23 15:37:36 +00:00
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
return startPreviewMode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t CameraService::Client::startRecordingMode()
|
|
|
|
{
|
2010-01-04 16:55:04 +00:00
|
|
|
LOGV("startRecordingMode (pid %d)", getCallingPid());
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
status_t ret = UNKNOWN_ERROR;
|
|
|
|
|
|
|
|
// if preview has not been started, start preview first
|
|
|
|
if (!mHardware->previewEnabled()) {
|
|
|
|
ret = startPreviewMode();
|
|
|
|
if (ret != NO_ERROR) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if recording has been enabled, nothing needs to be done
|
|
|
|
if (mHardware->recordingEnabled()) {
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// start recording mode
|
2009-08-12 17:01:27 +00:00
|
|
|
ret = mHardware->startRecording();
|
2009-03-04 03:31:44 +00:00
|
|
|
if (ret != NO_ERROR) {
|
|
|
|
LOGE("mHardware->startRecording() failed with status %d", ret);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-06-23 15:37:36 +00:00
|
|
|
status_t CameraService::Client::setOverlay()
|
|
|
|
{
|
2010-01-04 16:55:04 +00:00
|
|
|
LOGV("setOverlay");
|
2009-06-23 15:37:36 +00:00
|
|
|
int w, h;
|
|
|
|
CameraParameters params(mHardware->getParameters());
|
|
|
|
params.getPreviewSize(&w, &h);
|
|
|
|
|
2009-07-15 23:44:27 +00:00
|
|
|
if ( w != mOverlayW || h != mOverlayH )
|
|
|
|
{
|
|
|
|
// Force the destruction of any previous overlay
|
|
|
|
sp<Overlay> dummy;
|
|
|
|
mHardware->setOverlay( dummy );
|
|
|
|
mOverlayRef = 0;
|
|
|
|
}
|
|
|
|
|
2009-06-23 15:37:36 +00:00
|
|
|
status_t ret = NO_ERROR;
|
|
|
|
if (mSurface != 0) {
|
2009-07-15 23:44:27 +00:00
|
|
|
if (mOverlayRef.get() == NULL) {
|
2009-10-08 02:18:20 +00:00
|
|
|
|
|
|
|
// FIXME:
|
|
|
|
// Surfaceflinger may hold onto the previous overlay reference for some
|
|
|
|
// time after we try to destroy it. retry a few times. In the future, we
|
|
|
|
// should make the destroy call block, or possibly specify that we can
|
|
|
|
// wait in the createOverlay call if the previous overlay is in the
|
|
|
|
// process of being destroyed.
|
|
|
|
for (int retry = 0; retry < 50; ++retry) {
|
2010-01-22 01:31:06 +00:00
|
|
|
mOverlayRef = mSurface->createOverlay(w, h, OVERLAY_FORMAT_DEFAULT,
|
|
|
|
mOrientation);
|
2009-10-08 02:18:20 +00:00
|
|
|
if (mOverlayRef != NULL) break;
|
2010-01-04 16:55:04 +00:00
|
|
|
LOGW("Overlay create failed - retrying");
|
2009-10-08 02:18:20 +00:00
|
|
|
usleep(20000);
|
|
|
|
}
|
2009-07-15 23:44:27 +00:00
|
|
|
if ( mOverlayRef.get() == NULL )
|
|
|
|
{
|
|
|
|
LOGE("Overlay Creation Failed!");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
ret = mHardware->setOverlay(new Overlay(mOverlayRef));
|
|
|
|
}
|
2009-06-23 15:37:36 +00:00
|
|
|
} else {
|
|
|
|
ret = mHardware->setOverlay(NULL);
|
|
|
|
}
|
|
|
|
if (ret != NO_ERROR) {
|
|
|
|
LOGE("mHardware->setOverlay() failed with status %d\n", ret);
|
|
|
|
}
|
2009-07-15 23:44:27 +00:00
|
|
|
|
|
|
|
mOverlayW = w;
|
|
|
|
mOverlayH = h;
|
|
|
|
|
2009-06-23 15:37:36 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t CameraService::Client::registerPreviewBuffers()
|
|
|
|
{
|
|
|
|
int w, h;
|
|
|
|
CameraParameters params(mHardware->getParameters());
|
|
|
|
params.getPreviewSize(&w, &h);
|
|
|
|
|
|
|
|
ISurface::BufferHeap buffers(w, h, w, h,
|
|
|
|
PIXEL_FORMAT_YCbCr_420_SP,
|
2010-01-22 01:31:06 +00:00
|
|
|
mOrientation,
|
2009-06-23 15:37:36 +00:00
|
|
|
0,
|
|
|
|
mHardware->getPreviewHeap());
|
|
|
|
|
|
|
|
status_t ret = mSurface->registerBuffers(buffers);
|
|
|
|
if (ret != NO_ERROR) {
|
|
|
|
LOGE("registerBuffers failed with status %d", ret);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
status_t CameraService::Client::startPreviewMode()
|
|
|
|
{
|
2010-01-04 16:55:04 +00:00
|
|
|
LOGV("startPreviewMode (pid %d)", getCallingPid());
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
// if preview has been enabled, nothing needs to be done
|
|
|
|
if (mHardware->previewEnabled()) {
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// start preview mode
|
|
|
|
#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
|
|
|
|
debug_frame_cnt = 0;
|
|
|
|
#endif
|
2009-06-23 15:37:36 +00:00
|
|
|
status_t ret = NO_ERROR;
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
if (mUseOverlay) {
|
2009-06-23 15:37:36 +00:00
|
|
|
// If preview display has been set, set overlay now.
|
|
|
|
if (mSurface != 0) {
|
|
|
|
ret = setOverlay();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2009-06-23 15:37:36 +00:00
|
|
|
if (ret != NO_ERROR) return ret;
|
2009-08-12 17:01:27 +00:00
|
|
|
ret = mHardware->startPreview();
|
2009-03-04 03:31:44 +00:00
|
|
|
} else {
|
2009-08-12 17:01:27 +00:00
|
|
|
mHardware->enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
|
|
|
|
ret = mHardware->startPreview();
|
2009-06-23 15:37:36 +00:00
|
|
|
if (ret != NO_ERROR) return ret;
|
|
|
|
// If preview display has been set, register preview buffers now.
|
|
|
|
if (mSurface != 0) {
|
|
|
|
// Unregister here because the surface registered with raw heap.
|
|
|
|
mSurface->unregisterBuffers();
|
|
|
|
ret = registerPreviewBuffers();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t CameraService::Client::startPreview()
|
|
|
|
{
|
2010-01-04 16:55:04 +00:00
|
|
|
LOGV("startPreview (pid %d)", getCallingPid());
|
2009-09-28 23:14:58 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
return startCameraMode(CAMERA_PREVIEW_MODE);
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t CameraService::Client::startRecording()
|
|
|
|
{
|
2010-01-04 16:55:04 +00:00
|
|
|
LOGV("startRecording (pid %d)", getCallingPid());
|
2009-06-22 08:03:41 +00:00
|
|
|
|
2009-03-25 03:21:36 +00:00
|
|
|
if (mMediaPlayerBeep.get() != NULL) {
|
2009-11-27 13:07:55 +00:00
|
|
|
// do not play record jingle if stream volume is 0
|
|
|
|
// (typically because ringer mode is silent).
|
|
|
|
int index;
|
|
|
|
AudioSystem::getStreamVolumeIndex(AudioSystem::ENFORCED_AUDIBLE, &index);
|
|
|
|
if (index != 0) {
|
|
|
|
mMediaPlayerBeep->seekTo(0);
|
|
|
|
mMediaPlayerBeep->start();
|
|
|
|
}
|
2009-03-25 03:21:36 +00:00
|
|
|
}
|
2009-08-12 17:01:27 +00:00
|
|
|
|
|
|
|
mHardware->enableMsgType(CAMERA_MSG_VIDEO_FRAME);
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
return startCameraMode(CAMERA_RECORDING_MODE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop preview mode
|
|
|
|
void CameraService::Client::stopPreview()
|
|
|
|
{
|
2010-01-04 16:55:04 +00:00
|
|
|
LOGV("stopPreview (pid %d)", getCallingPid());
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-11-11 01:08:08 +00:00
|
|
|
// hold main lock during state transition
|
|
|
|
{
|
|
|
|
Mutex::Autolock lock(mLock);
|
|
|
|
if (checkPid() != NO_ERROR) return;
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-11-11 01:08:08 +00:00
|
|
|
if (mHardware == 0) {
|
|
|
|
LOGE("mHardware is NULL, returning.");
|
|
|
|
return;
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-11-11 01:08:08 +00:00
|
|
|
mHardware->stopPreview();
|
|
|
|
mHardware->disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
|
2010-01-04 16:55:04 +00:00
|
|
|
LOGV("stopPreview(), hardware stopped OK");
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-11-11 01:08:08 +00:00
|
|
|
if (mSurface != 0 && !mUseOverlay) {
|
|
|
|
mSurface->unregisterBuffers();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// hold preview buffer lock
|
|
|
|
{
|
|
|
|
Mutex::Autolock lock(mPreviewLock);
|
|
|
|
mPreviewBuffer.clear();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop recording mode
|
|
|
|
void CameraService::Client::stopRecording()
|
|
|
|
{
|
2010-01-04 16:55:04 +00:00
|
|
|
LOGV("stopRecording (pid %d)", getCallingPid());
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-11-11 01:08:08 +00:00
|
|
|
// hold main lock during state transition
|
|
|
|
{
|
|
|
|
Mutex::Autolock lock(mLock);
|
|
|
|
if (checkPid() != NO_ERROR) return;
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-11-11 01:08:08 +00:00
|
|
|
if (mHardware == 0) {
|
|
|
|
LOGE("mHardware is NULL, returning.");
|
|
|
|
return;
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-11-11 01:08:08 +00:00
|
|
|
if (mMediaPlayerBeep.get() != NULL) {
|
|
|
|
mMediaPlayerBeep->seekTo(0);
|
|
|
|
mMediaPlayerBeep->start();
|
|
|
|
}
|
2009-08-12 17:01:27 +00:00
|
|
|
|
2009-11-11 01:08:08 +00:00
|
|
|
mHardware->stopRecording();
|
|
|
|
mHardware->disableMsgType(CAMERA_MSG_VIDEO_FRAME);
|
2010-01-04 16:55:04 +00:00
|
|
|
LOGV("stopRecording(), hardware stopped OK");
|
2009-11-11 01:08:08 +00:00
|
|
|
}
|
2009-08-12 17:01:27 +00:00
|
|
|
|
2009-11-11 01:08:08 +00:00
|
|
|
// hold preview buffer lock
|
|
|
|
{
|
|
|
|
Mutex::Autolock lock(mPreviewLock);
|
|
|
|
mPreviewBuffer.clear();
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// release a recording frame
|
|
|
|
void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem)
|
|
|
|
{
|
|
|
|
Mutex::Autolock lock(mLock);
|
|
|
|
if (checkPid() != NO_ERROR) return;
|
|
|
|
|
|
|
|
if (mHardware == 0) {
|
|
|
|
LOGE("mHardware is NULL, returning.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mHardware->releaseRecordingFrame(mem);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CameraService::Client::previewEnabled()
|
|
|
|
{
|
|
|
|
Mutex::Autolock lock(mLock);
|
|
|
|
if (mHardware == 0) return false;
|
|
|
|
return mHardware->previewEnabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CameraService::Client::recordingEnabled()
|
|
|
|
{
|
|
|
|
Mutex::Autolock lock(mLock);
|
|
|
|
if (mHardware == 0) return false;
|
|
|
|
return mHardware->recordingEnabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Safely retrieves a strong pointer to the client during a hardware callback.
|
|
|
|
sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user)
|
|
|
|
{
|
|
|
|
sp<Client> client = 0;
|
|
|
|
CameraService *service = static_cast<CameraService*>(user);
|
|
|
|
if (service != NULL) {
|
2009-06-24 11:59:31 +00:00
|
|
|
Mutex::Autolock ourLock(service->mServiceLock);
|
2009-03-04 03:31:44 +00:00
|
|
|
if (service->mClient != 0) {
|
|
|
|
client = service->mClient.promote();
|
|
|
|
if (client == 0) {
|
|
|
|
LOGE("getClientFromCookie: client appears to have died");
|
|
|
|
service->mClient.clear();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOGE("getClientFromCookie: got callback but client was NULL");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return client;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE || \
|
|
|
|
DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE || \
|
|
|
|
DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
|
|
|
|
static void dump_to_file(const char *fname,
|
|
|
|
uint8_t *buf, uint32_t size)
|
|
|
|
{
|
|
|
|
int nw, cnt = 0;
|
|
|
|
uint32_t written = 0;
|
|
|
|
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("opening file [%s]\n", fname);
|
2009-03-04 03:31:44 +00:00
|
|
|
int fd = open(fname, O_RDWR | O_CREAT);
|
|
|
|
if (fd < 0) {
|
|
|
|
LOGE("failed to create file [%s]: %s", fname, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("writing %d bytes to file [%s]\n", size, fname);
|
2009-03-04 03:31:44 +00:00
|
|
|
while (written < size) {
|
|
|
|
nw = ::write(fd,
|
|
|
|
buf + written,
|
|
|
|
size - written);
|
|
|
|
if (nw < 0) {
|
|
|
|
LOGE("failed to write to file [%s]: %s",
|
|
|
|
fname, strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
written += nw;
|
|
|
|
cnt++;
|
|
|
|
}
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("done writing %d bytes to file [%s] in %d passes\n",
|
2009-03-04 03:31:44 +00:00
|
|
|
size, fname, cnt);
|
|
|
|
::close(fd);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
status_t CameraService::Client::autoFocus()
|
|
|
|
{
|
2010-01-04 16:55:04 +00:00
|
|
|
LOGV("autoFocus (pid %d)", getCallingPid());
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
Mutex::Autolock lock(mLock);
|
|
|
|
status_t result = checkPid();
|
|
|
|
if (result != NO_ERROR) return result;
|
|
|
|
|
|
|
|
if (mHardware == 0) {
|
|
|
|
LOGE("mHardware is NULL, returning.");
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
|
|
|
|
2009-08-12 17:01:27 +00:00
|
|
|
return mHardware->autoFocus();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-09-15 06:51:56 +00:00
|
|
|
status_t CameraService::Client::cancelAutoFocus()
|
|
|
|
{
|
2010-01-04 16:55:04 +00:00
|
|
|
LOGV("cancelAutoFocus (pid %d)", getCallingPid());
|
2009-09-15 06:51:56 +00:00
|
|
|
|
|
|
|
Mutex::Autolock lock(mLock);
|
|
|
|
status_t result = checkPid();
|
|
|
|
if (result != NO_ERROR) return result;
|
|
|
|
|
|
|
|
if (mHardware == 0) {
|
|
|
|
LOGE("mHardware is NULL, returning.");
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mHardware->cancelAutoFocus();
|
|
|
|
}
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
// take a picture - image is returned in callback
|
|
|
|
status_t CameraService::Client::takePicture()
|
|
|
|
{
|
2010-01-04 16:55:04 +00:00
|
|
|
LOGV("takePicture (pid %d)", getCallingPid());
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
Mutex::Autolock lock(mLock);
|
|
|
|
status_t result = checkPid();
|
|
|
|
if (result != NO_ERROR) return result;
|
|
|
|
|
|
|
|
if (mHardware == 0) {
|
|
|
|
LOGE("mHardware is NULL, returning.");
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
|
|
|
|
2009-08-12 17:01:27 +00:00
|
|
|
mHardware->enableMsgType(CAMERA_MSG_SHUTTER |
|
|
|
|
CAMERA_MSG_POSTVIEW_FRAME |
|
|
|
|
CAMERA_MSG_RAW_IMAGE |
|
|
|
|
CAMERA_MSG_COMPRESSED_IMAGE);
|
|
|
|
|
|
|
|
return mHardware->takePicture();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-08-12 17:01:27 +00:00
|
|
|
// snapshot taken
|
2009-10-23 09:39:46 +00:00
|
|
|
void CameraService::Client::handleShutter(
|
|
|
|
image_rect_type *size // The width and height of yuv picture for
|
|
|
|
// registerBuffer. If this is NULL, use the picture
|
|
|
|
// size from parameters.
|
|
|
|
)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-03-25 03:39:09 +00:00
|
|
|
// Play shutter sound.
|
2009-08-12 17:01:27 +00:00
|
|
|
if (mMediaPlayerClick.get() != NULL) {
|
2009-11-27 13:07:55 +00:00
|
|
|
// do not play shutter sound if stream volume is 0
|
|
|
|
// (typically because ringer mode is silent).
|
|
|
|
int index;
|
|
|
|
AudioSystem::getStreamVolumeIndex(AudioSystem::ENFORCED_AUDIBLE, &index);
|
|
|
|
if (index != 0) {
|
|
|
|
mMediaPlayerClick->seekTo(0);
|
|
|
|
mMediaPlayerClick->start();
|
|
|
|
}
|
2009-03-25 03:39:09 +00:00
|
|
|
}
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
// Screen goes black after the buffer is unregistered.
|
2009-08-12 17:01:27 +00:00
|
|
|
if (mSurface != 0 && !mUseOverlay) {
|
|
|
|
mSurface->unregisterBuffers();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-10-15 17:02:22 +00:00
|
|
|
sp<ICameraClient> c = mCameraClient;
|
|
|
|
if (c != NULL) {
|
|
|
|
c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
|
|
|
|
}
|
2009-08-12 17:01:27 +00:00
|
|
|
mHardware->disableMsgType(CAMERA_MSG_SHUTTER);
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
// It takes some time before yuvPicture callback to be called.
|
|
|
|
// Register the buffer for raw image here to reduce latency.
|
2009-08-12 17:01:27 +00:00
|
|
|
if (mSurface != 0 && !mUseOverlay) {
|
2009-03-04 03:31:44 +00:00
|
|
|
int w, h;
|
2009-08-12 17:01:27 +00:00
|
|
|
CameraParameters params(mHardware->getParameters());
|
2009-10-23 09:39:46 +00:00
|
|
|
if (size == NULL) {
|
|
|
|
params.getPictureSize(&w, &h);
|
|
|
|
} else {
|
|
|
|
w = size->width;
|
|
|
|
h = size->height;
|
|
|
|
w &= ~1;
|
|
|
|
h &= ~1;
|
2010-01-04 16:55:04 +00:00
|
|
|
LOGV("Snapshot image width=%d, height=%d", w, h);
|
2009-10-23 09:39:46 +00:00
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
ISurface::BufferHeap buffers(w, h, w, h,
|
2010-01-22 01:31:06 +00:00
|
|
|
PIXEL_FORMAT_YCbCr_420_SP, mOrientation, 0, mHardware->getRawHeap());
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-08-12 17:01:27 +00:00
|
|
|
mSurface->registerBuffers(buffers);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-12 17:01:27 +00:00
|
|
|
// preview callback - frame buffer update
|
|
|
|
void CameraService::Client::handlePreviewData(const sp<IMemory>& mem)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-08-12 17:01:27 +00:00
|
|
|
ssize_t offset;
|
|
|
|
size_t size;
|
|
|
|
sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
|
|
|
|
|
|
|
|
#if DEBUG_HEAP_LEAKS && 0 // debugging
|
|
|
|
if (gWeakHeap == NULL) {
|
|
|
|
if (gWeakHeap != heap) {
|
2010-01-04 16:55:04 +00:00
|
|
|
LOGV("SETTING PREVIEW HEAP");
|
2009-08-12 17:01:27 +00:00
|
|
|
heap->trackMe(true, true);
|
|
|
|
gWeakHeap = heap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE
|
|
|
|
{
|
|
|
|
if (debug_frame_cnt++ == DEBUG_DUMP_PREVIEW_FRAME_TO_FILE) {
|
|
|
|
dump_to_file("/data/preview.yuv",
|
|
|
|
(uint8_t *)heap->base() + offset, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!mUseOverlay)
|
|
|
|
{
|
|
|
|
Mutex::Autolock surfaceLock(mSurfaceLock);
|
|
|
|
if (mSurface != NULL) {
|
|
|
|
mSurface->postBuffer(offset);
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2009-08-12 17:01:27 +00:00
|
|
|
|
2009-10-15 17:02:22 +00:00
|
|
|
// local copy of the callback flags
|
|
|
|
int flags = mPreviewCallbackFlag;
|
|
|
|
|
|
|
|
// is callback enabled?
|
|
|
|
if (!(flags & FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
|
2009-08-12 17:01:27 +00:00
|
|
|
// If the enable bit is off, the copy-out and one-shot bits are ignored
|
|
|
|
LOGV("frame callback is diabled");
|
2009-03-04 03:31:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-10-15 17:02:22 +00:00
|
|
|
// hold a strong pointer to the client
|
|
|
|
sp<ICameraClient> c = mCameraClient;
|
2009-08-12 17:01:27 +00:00
|
|
|
|
2009-10-15 17:02:22 +00:00
|
|
|
// clear callback flags if no client or one-shot mode
|
|
|
|
if ((c == NULL) || (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
|
|
|
|
LOGV("Disable preview callback");
|
2009-08-12 17:01:27 +00:00
|
|
|
mPreviewCallbackFlag &= ~(FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
|
|
|
|
FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
|
|
|
|
FRAME_CALLBACK_FLAG_ENABLE_MASK);
|
2009-10-15 17:02:22 +00:00
|
|
|
// TODO: Shouldn't we use this API for non-overlay hardware as well?
|
2009-08-12 17:01:27 +00:00
|
|
|
if (mUseOverlay)
|
|
|
|
mHardware->disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
|
|
|
|
}
|
2009-10-15 17:02:22 +00:00
|
|
|
|
|
|
|
// Is the received frame copied out or not?
|
|
|
|
if (flags & FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
|
|
|
|
LOGV("frame is copied");
|
|
|
|
copyFrameAndPostCopiedFrame(c, heap, offset, size);
|
|
|
|
} else {
|
|
|
|
LOGV("frame is forwarded");
|
|
|
|
c->dataCallback(CAMERA_MSG_PREVIEW_FRAME, mem);
|
|
|
|
}
|
2009-08-12 17:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// picture callback - postview image ready
|
|
|
|
void CameraService::Client::handlePostview(const sp<IMemory>& mem)
|
|
|
|
{
|
|
|
|
#if DEBUG_DUMP_POSTVIEW_SNAPSHOT_TO_FILE // for testing pursposes only
|
|
|
|
{
|
|
|
|
ssize_t offset;
|
|
|
|
size_t size;
|
|
|
|
sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
|
|
|
|
dump_to_file("/data/postview.yuv",
|
|
|
|
(uint8_t *)heap->base() + offset, size);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-10-15 17:02:22 +00:00
|
|
|
sp<ICameraClient> c = mCameraClient;
|
|
|
|
if (c != NULL) {
|
|
|
|
c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem);
|
|
|
|
}
|
2009-08-12 17:01:27 +00:00
|
|
|
mHardware->disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
// picture callback - raw image ready
|
|
|
|
void CameraService::Client::handleRawPicture(const sp<IMemory>& mem)
|
|
|
|
{
|
2009-03-04 03:31:44 +00:00
|
|
|
ssize_t offset;
|
|
|
|
size_t size;
|
|
|
|
sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
|
|
|
|
#if DEBUG_HEAP_LEAKS && 0 // debugging
|
|
|
|
gWeakHeap = heap; // debugging
|
|
|
|
#endif
|
|
|
|
|
2009-08-12 17:01:27 +00:00
|
|
|
//LOGV("handleRawPicture(%d, %d)", offset, size);
|
2009-03-04 03:31:44 +00:00
|
|
|
#if DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE // for testing pursposes only
|
|
|
|
dump_to_file("/data/photo.yuv",
|
|
|
|
(uint8_t *)heap->base() + offset, size);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Put the YUV version of the snapshot in the preview display.
|
2009-08-12 17:01:27 +00:00
|
|
|
if (mSurface != 0 && !mUseOverlay) {
|
|
|
|
mSurface->postBuffer(offset);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-10-15 17:02:22 +00:00
|
|
|
sp<ICameraClient> c = mCameraClient;
|
|
|
|
if (c != NULL) {
|
|
|
|
c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem);
|
|
|
|
}
|
2009-08-12 17:01:27 +00:00
|
|
|
mHardware->disableMsgType(CAMERA_MSG_RAW_IMAGE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// picture callback - compressed picture ready
|
|
|
|
void CameraService::Client::handleCompressedPicture(const sp<IMemory>& mem)
|
|
|
|
{
|
|
|
|
#if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE // for testing pursposes only
|
|
|
|
{
|
|
|
|
ssize_t offset;
|
|
|
|
size_t size;
|
|
|
|
sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
|
|
|
|
dump_to_file("/data/photo.jpg",
|
|
|
|
(uint8_t *)heap->base() + offset, size);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-10-15 17:02:22 +00:00
|
|
|
sp<ICameraClient> c = mCameraClient;
|
|
|
|
if (c != NULL) {
|
|
|
|
c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem);
|
|
|
|
}
|
2009-08-12 17:01:27 +00:00
|
|
|
mHardware->disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CameraService::Client::notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user)
|
|
|
|
{
|
|
|
|
LOGV("notifyCallback(%d)", msgType);
|
|
|
|
|
|
|
|
sp<Client> client = getClientFromCookie(user);
|
|
|
|
if (client == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (msgType) {
|
|
|
|
case CAMERA_MSG_SHUTTER:
|
2009-10-23 09:39:46 +00:00
|
|
|
// ext1 is the dimension of the yuv picture.
|
|
|
|
client->handleShutter((image_rect_type *)ext1);
|
2009-08-12 17:01:27 +00:00
|
|
|
break;
|
|
|
|
default:
|
2009-10-15 17:02:22 +00:00
|
|
|
sp<ICameraClient> c = client->mCameraClient;
|
|
|
|
if (c != NULL) {
|
|
|
|
c->notifyCallback(msgType, ext1, ext2);
|
|
|
|
}
|
2009-08-12 17:01:27 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
#if DEBUG_CLIENT_REFERENCES
|
|
|
|
if (client->getStrongCount() == 1) {
|
2009-08-12 17:01:27 +00:00
|
|
|
LOGE("++++++++++++++++ (NOTIFY CALLBACK) THIS WILL CAUSE A LOCKUP!");
|
2009-03-04 03:31:44 +00:00
|
|
|
client->printRefs();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-08-12 17:01:27 +00:00
|
|
|
void CameraService::Client::dataCallback(int32_t msgType, const sp<IMemory>& dataPtr, void* user)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-08-12 17:01:27 +00:00
|
|
|
LOGV("dataCallback(%d)", msgType);
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
sp<Client> client = getClientFromCookie(user);
|
|
|
|
if (client == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2009-08-12 17:01:27 +00:00
|
|
|
|
2009-10-15 17:02:22 +00:00
|
|
|
sp<ICameraClient> c = client->mCameraClient;
|
2009-08-12 17:01:27 +00:00
|
|
|
if (dataPtr == NULL) {
|
|
|
|
LOGE("Null data returned in data callback");
|
2009-10-15 17:02:22 +00:00
|
|
|
if (c != NULL) {
|
|
|
|
c->notifyCallback(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
|
|
|
|
c->dataCallback(msgType, NULL);
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-08-12 17:01:27 +00:00
|
|
|
switch (msgType) {
|
|
|
|
case CAMERA_MSG_PREVIEW_FRAME:
|
|
|
|
client->handlePreviewData(dataPtr);
|
|
|
|
break;
|
|
|
|
case CAMERA_MSG_POSTVIEW_FRAME:
|
|
|
|
client->handlePostview(dataPtr);
|
|
|
|
break;
|
|
|
|
case CAMERA_MSG_RAW_IMAGE:
|
|
|
|
client->handleRawPicture(dataPtr);
|
|
|
|
break;
|
|
|
|
case CAMERA_MSG_COMPRESSED_IMAGE:
|
|
|
|
client->handleCompressedPicture(dataPtr);
|
|
|
|
break;
|
|
|
|
default:
|
2009-10-15 17:02:22 +00:00
|
|
|
if (c != NULL) {
|
|
|
|
c->dataCallback(msgType, dataPtr);
|
|
|
|
}
|
2009-08-12 17:01:27 +00:00
|
|
|
break;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG_CLIENT_REFERENCES
|
|
|
|
if (client->getStrongCount() == 1) {
|
2009-08-12 17:01:27 +00:00
|
|
|
LOGE("++++++++++++++++ (DATA CALLBACK) THIS WILL CAUSE A LOCKUP!");
|
2009-03-04 03:31:44 +00:00
|
|
|
client->printRefs();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-08-12 17:01:27 +00:00
|
|
|
void CameraService::Client::dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType,
|
|
|
|
const sp<IMemory>& dataPtr, void* user)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-08-12 17:01:27 +00:00
|
|
|
LOGV("dataCallbackTimestamp(%d)", msgType);
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
sp<Client> client = getClientFromCookie(user);
|
|
|
|
if (client == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2009-10-15 17:02:22 +00:00
|
|
|
sp<ICameraClient> c = client->mCameraClient;
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-08-12 17:01:27 +00:00
|
|
|
if (dataPtr == NULL) {
|
|
|
|
LOGE("Null data returned in data with timestamp callback");
|
2009-10-15 17:02:22 +00:00
|
|
|
if (c != NULL) {
|
|
|
|
c->notifyCallback(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
|
|
|
|
c->dataCallbackTimestamp(0, msgType, NULL);
|
|
|
|
}
|
2009-08-12 17:01:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-10-15 17:02:22 +00:00
|
|
|
if (c != NULL) {
|
|
|
|
c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
#if DEBUG_CLIENT_REFERENCES
|
|
|
|
if (client->getStrongCount() == 1) {
|
2009-08-12 17:01:27 +00:00
|
|
|
LOGE("++++++++++++++++ (DATA CALLBACK TIMESTAMP) THIS WILL CAUSE A LOCKUP!");
|
2009-03-04 03:31:44 +00:00
|
|
|
client->printRefs();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// set preview/capture parameters - key/value pairs
|
|
|
|
status_t CameraService::Client::setParameters(const String8& params)
|
|
|
|
{
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("setParameters(%s)", params.string());
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
Mutex::Autolock lock(mLock);
|
|
|
|
status_t result = checkPid();
|
|
|
|
if (result != NO_ERROR) return result;
|
|
|
|
|
|
|
|
if (mHardware == 0) {
|
|
|
|
LOGE("mHardware is NULL, returning.");
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
CameraParameters p(params);
|
2010-01-22 01:31:06 +00:00
|
|
|
|
2009-09-14 00:10:24 +00:00
|
|
|
return mHardware->setParameters(p);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// get preview/capture parameters - key/value pairs
|
|
|
|
String8 CameraService::Client::getParameters() const
|
|
|
|
{
|
|
|
|
Mutex::Autolock lock(mLock);
|
|
|
|
|
|
|
|
if (mHardware == 0) {
|
|
|
|
LOGE("mHardware is NULL, returning.");
|
|
|
|
return String8();
|
|
|
|
}
|
|
|
|
|
2009-04-22 08:21:26 +00:00
|
|
|
String8 params(mHardware->getParameters().flatten());
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("getParameters(%s)", params.string());
|
2009-04-22 08:21:26 +00:00
|
|
|
return params;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-09-28 23:14:58 +00:00
|
|
|
status_t CameraService::Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
|
|
|
|
{
|
2010-01-04 16:55:04 +00:00
|
|
|
LOGV("sendCommand (pid %d)", getCallingPid());
|
2009-09-28 23:14:58 +00:00
|
|
|
Mutex::Autolock lock(mLock);
|
|
|
|
status_t result = checkPid();
|
|
|
|
if (result != NO_ERROR) return result;
|
|
|
|
|
2010-01-23 01:49:48 +00:00
|
|
|
if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
|
|
|
|
// The orientation cannot be set during preview.
|
|
|
|
if (mHardware->previewEnabled()) {
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
|
|
|
switch (arg1) {
|
|
|
|
case 0:
|
|
|
|
mOrientation = ISurface::BufferHeap::ROT_0;
|
|
|
|
break;
|
|
|
|
case 90:
|
|
|
|
mOrientation = ISurface::BufferHeap::ROT_90;
|
|
|
|
break;
|
|
|
|
case 180:
|
|
|
|
mOrientation = ISurface::BufferHeap::ROT_180;
|
|
|
|
break;
|
|
|
|
case 270:
|
|
|
|
mOrientation = ISurface::BufferHeap::ROT_270;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2009-09-28 23:14:58 +00:00
|
|
|
if (mHardware == 0) {
|
|
|
|
LOGE("mHardware is NULL, returning.");
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mHardware->sendCommand(cmd, arg1, arg2);
|
|
|
|
}
|
|
|
|
|
2009-10-15 17:02:22 +00:00
|
|
|
void CameraService::Client::copyFrameAndPostCopiedFrame(const sp<ICameraClient>& client,
|
|
|
|
const sp<IMemoryHeap>& heap, size_t offset, size_t size)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
|
|
|
LOGV("copyFrameAndPostCopiedFrame");
|
|
|
|
// It is necessary to copy out of pmem before sending this to
|
|
|
|
// the callback. For efficiency, reuse the same MemoryHeapBase
|
|
|
|
// provided it's big enough. Don't allocate the memory or
|
|
|
|
// perform the copy if there's no callback.
|
2009-11-06 19:47:13 +00:00
|
|
|
|
2009-11-11 01:08:08 +00:00
|
|
|
// hold the preview lock while we grab a reference to the preview buffer
|
2009-11-06 19:47:13 +00:00
|
|
|
sp<MemoryHeapBase> previewBuffer;
|
|
|
|
{
|
2009-11-11 01:08:08 +00:00
|
|
|
Mutex::Autolock lock(mPreviewLock);
|
2009-11-06 19:47:13 +00:00
|
|
|
if (mPreviewBuffer == 0) {
|
|
|
|
mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
|
|
|
|
} else if (size > mPreviewBuffer->virtualSize()) {
|
|
|
|
mPreviewBuffer.clear();
|
|
|
|
mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
if (mPreviewBuffer == 0) {
|
|
|
|
LOGE("failed to allocate space for preview buffer");
|
|
|
|
return;
|
|
|
|
}
|
2009-11-06 19:47:13 +00:00
|
|
|
previewBuffer = mPreviewBuffer;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2009-11-06 19:47:13 +00:00
|
|
|
memcpy(previewBuffer->base(),
|
2009-03-04 03:31:44 +00:00
|
|
|
(uint8_t *)heap->base() + offset, size);
|
|
|
|
|
2009-11-06 19:47:13 +00:00
|
|
|
sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
|
2009-03-04 03:31:44 +00:00
|
|
|
if (frame == 0) {
|
|
|
|
LOGE("failed to allocate space for frame callback");
|
|
|
|
return;
|
|
|
|
}
|
2009-10-15 17:02:22 +00:00
|
|
|
client->dataCallback(CAMERA_MSG_PREVIEW_FRAME, frame);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status_t CameraService::dump(int fd, const Vector<String16>& args)
|
|
|
|
{
|
|
|
|
const size_t SIZE = 256;
|
|
|
|
char buffer[SIZE];
|
|
|
|
String8 result;
|
|
|
|
if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
|
|
|
|
snprintf(buffer, SIZE, "Permission Denial: "
|
|
|
|
"can't dump CameraService from pid=%d, uid=%d\n",
|
2009-06-22 08:03:41 +00:00
|
|
|
getCallingPid(),
|
2009-03-04 03:31:44 +00:00
|
|
|
IPCThreadState::self()->getCallingUid());
|
|
|
|
result.append(buffer);
|
|
|
|
write(fd, result.string(), result.size());
|
|
|
|
} else {
|
2009-06-24 11:59:31 +00:00
|
|
|
AutoMutex lock(&mServiceLock);
|
2009-03-04 03:31:44 +00:00
|
|
|
if (mClient != 0) {
|
|
|
|
sp<Client> currentClient = mClient.promote();
|
|
|
|
sprintf(buffer, "Client (%p) PID: %d\n",
|
|
|
|
currentClient->getCameraClient()->asBinder().get(),
|
|
|
|
currentClient->mClientPid);
|
|
|
|
result.append(buffer);
|
|
|
|
write(fd, result.string(), result.size());
|
|
|
|
currentClient->mHardware->dump(fd, args);
|
|
|
|
} else {
|
|
|
|
result.append("No camera client yet.\n");
|
|
|
|
write(fd, result.string(), result.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t CameraService::onTransact(
|
|
|
|
uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
|
|
|
|
{
|
|
|
|
// permission checks...
|
|
|
|
switch (code) {
|
|
|
|
case BnCameraService::CONNECT:
|
|
|
|
IPCThreadState* ipc = IPCThreadState::self();
|
|
|
|
const int pid = ipc->getCallingPid();
|
|
|
|
const int self_pid = getpid();
|
|
|
|
if (pid != self_pid) {
|
|
|
|
// we're called from a different process, do the real check
|
|
|
|
if (!checkCallingPermission(
|
|
|
|
String16("android.permission.CAMERA")))
|
|
|
|
{
|
|
|
|
const int uid = ipc->getCallingUid();
|
|
|
|
LOGE("Permission Denial: "
|
|
|
|
"can't use the camera pid=%d, uid=%d", pid, uid);
|
|
|
|
return PERMISSION_DENIED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t err = BnCameraService::onTransact(code, data, reply, flags);
|
|
|
|
|
2009-05-21 03:02:59 +00:00
|
|
|
#if DEBUG_HEAP_LEAKS
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("+++ onTransact err %d code %d", err, code);
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
|
|
|
|
// the 'service' command interrogates this binder for its name, and then supplies it
|
|
|
|
// even for the debugging commands. that means we need to check for it here, using
|
|
|
|
// ISurfaceComposer (since we delegated the INTERFACE_TRANSACTION handling to
|
|
|
|
// BnSurfaceComposer before falling through to this code).
|
|
|
|
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("+++ onTransact code %d", code);
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
CHECK_INTERFACE(ICameraService, data, reply);
|
|
|
|
|
|
|
|
switch(code) {
|
|
|
|
case 1000:
|
|
|
|
{
|
|
|
|
if (gWeakHeap != 0) {
|
|
|
|
sp<IMemoryHeap> h = gWeakHeap.promote();
|
|
|
|
IMemoryHeap *p = gWeakHeap.unsafe_get();
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("CHECKING WEAK REFERENCE %p (%p)", h.get(), p);
|
2009-03-04 03:31:44 +00:00
|
|
|
if (h != 0)
|
|
|
|
h->printRefs();
|
|
|
|
bool attempt_to_delete = data.readInt32() == 1;
|
|
|
|
if (attempt_to_delete) {
|
|
|
|
// NOT SAFE!
|
2010-01-08 02:48:32 +00:00
|
|
|
LOGV("DELETING WEAK REFERENCE %p (%p)", h.get(), p);
|
2009-03-04 03:31:44 +00:00
|
|
|
if (p) delete p;
|
|
|
|
}
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-05-21 03:02:59 +00:00
|
|
|
#endif // DEBUG_HEAP_LEAKS
|
2009-05-21 16:18:18 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
}; // namespace android
|