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"
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
|
2009-05-20 02:08:10 +00:00
|
|
|
#include <binder/IPCThreadState.h>
|
2010-05-06 08:36:58 +00:00
|
|
|
#include <binder/IServiceManager.h>
|
2009-05-20 02:08:10 +00:00
|
|
|
#include <binder/MemoryBase.h>
|
|
|
|
#include <binder/MemoryHeapBase.h>
|
2010-05-06 08:36:58 +00:00
|
|
|
#include <cutils/atomic.h>
|
|
|
|
#include <hardware/hardware.h>
|
|
|
|
#include <media/AudioSystem.h>
|
|
|
|
#include <media/mediaplayer.h>
|
2010-02-10 01:46:37 +00:00
|
|
|
#include <surfaceflinger/ISurface.h>
|
|
|
|
#include <ui/Overlay.h>
|
2010-05-06 08:36:58 +00:00
|
|
|
#include <utils/Errors.h>
|
|
|
|
#include <utils/Log.h>
|
|
|
|
#include <utils/String16.h>
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
#include "CameraService.h"
|
2010-05-10 06:36:24 +00:00
|
|
|
#ifdef INCLUDE_CAMERA_STUB
|
2010-05-06 08:36:58 +00:00
|
|
|
#include "CameraHardwareStub.h"
|
|
|
|
#endif
|
2009-03-27 23:27:16 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
namespace android {
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
/* This determines the number of cameras available */
|
2010-05-10 06:36:24 +00:00
|
|
|
#if defined(INCLUDE_CAMERA_HARDWARE) && defined(INCLUDE_CAMERA_STUB)
|
2010-05-06 08:36:58 +00:00
|
|
|
#define NUM_CAMERAS 2
|
2010-05-10 06:36:24 +00:00
|
|
|
#elif defined(INCLUDE_CAMERA_HARDWARE) || defined(INCLUDE_CAMERA_STUB)
|
2010-05-06 08:36:58 +00:00
|
|
|
#define NUM_CAMERAS 1
|
|
|
|
#else
|
|
|
|
#error "Should have at least one camera"
|
|
|
|
#endif
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
/* Make sure we have enough array space allocated */
|
|
|
|
#if NUM_CAMERAS > MAX_CAMERAS
|
|
|
|
#error "Need to increase MAX_CAMERAS"
|
|
|
|
#endif
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
/* This defines the "open" function for each camera */
|
|
|
|
extern "C" typedef sp<CameraHardwareInterface> (*OpenCameraHardwareFunction)();
|
|
|
|
static OpenCameraHardwareFunction sOpenCameraTable[] = {
|
2010-05-10 06:36:24 +00:00
|
|
|
#ifdef INCLUDE_CAMERA_HARDWARE
|
2010-05-06 08:36:58 +00:00
|
|
|
&openCameraHardware,
|
|
|
|
#endif
|
2010-05-10 06:36:24 +00:00
|
|
|
#ifdef INCLUDE_CAMERA_STUB
|
2010-05-06 08:36:58 +00:00
|
|
|
&openCameraHardwareStub,
|
|
|
|
#endif
|
|
|
|
};
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Logging support -- this is for debugging only
|
|
|
|
// Use "adb shell dumpsys media.camera -v 1" to change it.
|
|
|
|
static volatile int32_t gLogLevel = 0;
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
#define LOG1(...) LOGD_IF(gLogLevel >= 1, __VA_ARGS__);
|
|
|
|
#define LOG2(...) LOGD_IF(gLogLevel >= 2, __VA_ARGS__);
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
static void setLogLevel(int level) {
|
|
|
|
android_atomic_write(level, &gLogLevel);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-06-22 08:03:41 +00:00
|
|
|
static int getCallingPid() {
|
|
|
|
return IPCThreadState::self()->getCallingPid();
|
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
static int getCallingUid() {
|
|
|
|
return IPCThreadState::self()->getCallingUid();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
// This is ugly and only safe if we never re-create the CameraService, but
|
|
|
|
// should be ok for now.
|
|
|
|
static CameraService *gCameraService;
|
|
|
|
|
|
|
|
CameraService::CameraService()
|
|
|
|
:mSoundRef(0)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2010-05-06 08:36:58 +00:00
|
|
|
LOGI("CameraService started (pid=%d)", getpid());
|
|
|
|
|
|
|
|
for (int i = 0; i < NUM_CAMERAS; i++) {
|
|
|
|
setCameraFree(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
gCameraService = this;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
CameraService::~CameraService() {
|
|
|
|
for (int i = 0; i < NUM_CAMERAS; i++) {
|
|
|
|
if (mBusy[i]) {
|
|
|
|
LOGE("camera %d is still in use in destructor!", i);
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2010-05-06 08:36:58 +00:00
|
|
|
|
|
|
|
gCameraService = NULL;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
int32_t CameraService::getNumberOfCameras() {
|
|
|
|
return NUM_CAMERAS;
|
|
|
|
}
|
|
|
|
|
|
|
|
sp<ICamera> CameraService::connect(
|
|
|
|
const sp<ICameraClient>& cameraClient, int cameraId) {
|
2009-06-22 08:03:41 +00:00
|
|
|
int callingPid = getCallingPid();
|
2010-05-06 08:36:58 +00:00
|
|
|
LOG1("CameraService::connect E (pid %d, id %d)", callingPid, cameraId);
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
sp<Client> client;
|
2010-05-06 08:36:58 +00:00
|
|
|
if (cameraId < 0 || cameraId >= NUM_CAMERAS) {
|
|
|
|
LOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).",
|
|
|
|
callingPid, cameraId);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Mutex::Autolock lock(mServiceLock);
|
|
|
|
if (mClient[cameraId] != 0) {
|
|
|
|
client = mClient[cameraId].promote();
|
|
|
|
if (client != 0) {
|
|
|
|
if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) {
|
|
|
|
LOG1("CameraService::connect X (pid %d) (the same client)",
|
|
|
|
callingPid);
|
2009-03-04 03:31:44 +00:00
|
|
|
return client;
|
2010-05-06 08:36:58 +00:00
|
|
|
} else {
|
|
|
|
LOGW("CameraService::connect X (pid %d) rejected (existing client).",
|
2009-06-22 08:03:41 +00:00
|
|
|
callingPid);
|
2010-05-06 08:36:58 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2010-05-06 08:36:58 +00:00
|
|
|
mClient[cameraId].clear();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
if (mBusy[cameraId]) {
|
|
|
|
LOGW("CameraService::connect X (pid %d) rejected"
|
|
|
|
" (camera %d is still busy).", callingPid, cameraId);
|
|
|
|
return NULL;
|
2009-06-24 11:59:31 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
client = new Client(this, cameraClient, cameraId, callingPid);
|
|
|
|
mClient[cameraId] = client;
|
|
|
|
LOG1("CameraService::connect X");
|
2009-03-04 03:31:44 +00:00
|
|
|
return client;
|
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
void CameraService::removeClient(const sp<ICameraClient>& cameraClient) {
|
2009-06-22 08:03:41 +00:00
|
|
|
int callingPid = getCallingPid();
|
2010-05-06 08:36:58 +00:00
|
|
|
LOG1("CameraService::removeClient E (pid %d)", callingPid);
|
2009-06-22 08:03:41 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
for (int i = 0; i < NUM_CAMERAS; i++) {
|
|
|
|
// Declare this before the lock to make absolutely sure the
|
|
|
|
// destructor won't be called with the lock held.
|
|
|
|
sp<Client> client;
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
Mutex::Autolock lock(mServiceLock);
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
// This happens when we have already disconnected (or this is
|
|
|
|
// just another unused camera).
|
|
|
|
if (mClient[i] == 0) continue;
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
// Promote mClient. It can fail if we are called from this path:
|
|
|
|
// Client::~Client() -> disconnect() -> removeClient().
|
|
|
|
client = mClient[i].promote();
|
|
|
|
|
|
|
|
if (client == 0) {
|
|
|
|
mClient[i].clear();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) {
|
|
|
|
// Found our camera, clear and leave.
|
|
|
|
LOG1("removeClient: clear camera %d", i);
|
|
|
|
mClient[i].clear();
|
|
|
|
break;
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
LOG1("CameraService::removeClient X (pid %d)", callingPid);
|
|
|
|
}
|
|
|
|
|
|
|
|
sp<CameraService::Client> CameraService::getClientById(int cameraId) {
|
|
|
|
if (cameraId < 0 || cameraId >= NUM_CAMERAS) return NULL;
|
|
|
|
return mClient[cameraId].promote();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CameraService::instantiate() {
|
|
|
|
defaultServiceManager()->addService(String16("media.camera"),
|
|
|
|
new CameraService());
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t CameraService::onTransact(
|
|
|
|
uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
|
|
|
|
// Permission checks
|
|
|
|
switch (code) {
|
|
|
|
case BnCameraService::CONNECT:
|
|
|
|
const int pid = 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 = getCallingUid();
|
|
|
|
LOGE("Permission Denial: "
|
|
|
|
"can't use the camera pid=%d, uid=%d", pid, uid);
|
|
|
|
return PERMISSION_DENIED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2009-06-22 08:03:41 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
return BnCameraService::onTransact(code, data, reply, flags);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
// The reason we need this busy bit 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 the destructor has not been finished, 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::setCameraBusy(int cameraId) {
|
|
|
|
android_atomic_write(1, &mBusy[cameraId]);
|
2009-06-24 05:44:37 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
void CameraService::setCameraFree(int cameraId) {
|
|
|
|
android_atomic_write(0, &mBusy[cameraId]);
|
2009-06-24 05:44:37 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
// We share the media players for shutter and recording sound for all clients.
|
|
|
|
// A reference count is kept to determine when we will actually release the
|
|
|
|
// media players.
|
|
|
|
|
|
|
|
static MediaPlayer* newMediaPlayer(const char *file) {
|
|
|
|
MediaPlayer* mp = new MediaPlayer();
|
|
|
|
if (mp->setDataSource(file, NULL) == 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 {
|
2010-05-06 08:36:58 +00:00
|
|
|
LOGE("Failed to load CameraService sounds: %s", file);
|
|
|
|
return NULL;
|
2009-03-25 03:21:36 +00:00
|
|
|
}
|
|
|
|
return mp;
|
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
void CameraService::loadSound() {
|
|
|
|
Mutex::Autolock lock(mSoundLock);
|
|
|
|
LOG1("CameraService::loadSound ref=%d", mSoundRef);
|
|
|
|
if (mSoundRef++) return;
|
|
|
|
|
|
|
|
mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
|
|
|
|
mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CameraService::releaseSound() {
|
|
|
|
Mutex::Autolock lock(mSoundLock);
|
|
|
|
LOG1("CameraService::releaseSound ref=%d", mSoundRef);
|
|
|
|
if (--mSoundRef) return;
|
|
|
|
|
|
|
|
for (int i = 0; i < NUM_SOUNDS; i++) {
|
|
|
|
if (mSoundPlayer[i] != 0) {
|
|
|
|
mSoundPlayer[i]->disconnect();
|
|
|
|
mSoundPlayer[i].clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CameraService::playSound(sound_kind kind) {
|
|
|
|
LOG1("playSound(%d)", kind);
|
|
|
|
Mutex::Autolock lock(mSoundLock);
|
|
|
|
sp<MediaPlayer> player = mSoundPlayer[kind];
|
|
|
|
if (player != 0) {
|
|
|
|
// do not play the sound if stream volume is 0
|
|
|
|
// (typically because ringer mode is silent).
|
|
|
|
int index;
|
|
|
|
AudioSystem::getStreamVolumeIndex(AudioSystem::ENFORCED_AUDIBLE, &index);
|
|
|
|
if (index != 0) {
|
|
|
|
player->seekTo(0);
|
|
|
|
player->start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
CameraService::Client::Client(const sp<CameraService>& cameraService,
|
2010-05-06 08:36:58 +00:00
|
|
|
const sp<ICameraClient>& cameraClient, int cameraId, int clientPid) {
|
2009-06-22 08:03:41 +00:00
|
|
|
int callingPid = getCallingPid();
|
2010-05-06 08:36:58 +00:00
|
|
|
LOG1("Client::Client E (pid %d)", callingPid);
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
mCameraService = cameraService;
|
|
|
|
mCameraClient = cameraClient;
|
2010-05-06 08:36:58 +00:00
|
|
|
mCameraId = cameraId;
|
2009-03-04 03:31:44 +00:00
|
|
|
mClientPid = clientPid;
|
2010-05-06 08:36:58 +00:00
|
|
|
|
|
|
|
mHardware = sOpenCameraTable[cameraId]();
|
2009-03-04 03:31:44 +00:00
|
|
|
mUseOverlay = mHardware->useOverlay();
|
2010-05-06 08:36:58 +00:00
|
|
|
mMsgEnabled = 0;
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-08-12 17:01:27 +00:00
|
|
|
mHardware->setCallbacks(notifyCallback,
|
|
|
|
dataCallback,
|
|
|
|
dataCallbackTimestamp,
|
2010-05-06 08:36:58 +00:00
|
|
|
(void *)cameraId);
|
2009-08-12 17:01:27 +00:00
|
|
|
|
|
|
|
// Enable zoom, error, and focus messages by default
|
2010-05-06 08:36:58 +00:00
|
|
|
enableMsgType(CAMERA_MSG_ERROR |
|
|
|
|
CAMERA_MSG_ZOOM |
|
|
|
|
CAMERA_MSG_FOCUS);
|
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;
|
2010-05-06 08:36:58 +00:00
|
|
|
cameraService->setCameraBusy(cameraId);
|
|
|
|
cameraService->loadSound();
|
|
|
|
LOG1("Client::Client X (pid %d)", callingPid);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
static void *unregister_surface(void *arg) {
|
|
|
|
ISurface *surface = (ISurface *)arg;
|
|
|
|
surface->unregisterBuffers();
|
|
|
|
IPCThreadState::self()->flushCommands();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// tear down the client
|
|
|
|
CameraService::Client::~Client() {
|
2009-06-22 08:03:41 +00:00
|
|
|
int callingPid = getCallingPid();
|
2010-05-06 08:36:58 +00:00
|
|
|
LOG1("Client::~Client E (pid %d, this %p)", callingPid, this);
|
|
|
|
|
|
|
|
if (mSurface != 0 && !mUseOverlay) {
|
|
|
|
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, // attr
|
|
|
|
unregister_surface,
|
|
|
|
mSurface.get());
|
|
|
|
pthread_join(thr, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// set mClientPid to let disconnet() tear down the hardware
|
|
|
|
mClientPid = callingPid;
|
|
|
|
disconnect();
|
|
|
|
mCameraService->releaseSound();
|
|
|
|
LOG1("Client::~Client X (pid %d, this %p)", callingPid, this);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
status_t CameraService::Client::checkPid() const {
|
2009-06-22 08:03:41 +00:00
|
|
|
int callingPid = getCallingPid();
|
2010-05-06 08:36:58 +00:00
|
|
|
if (callingPid == mClientPid) return NO_ERROR;
|
|
|
|
if (callingPid == getpid()) {
|
|
|
|
LOGW("FIXME: use camera from mediaserver without permission.");
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
LOGW("attempt to use a locked camera from a different process"
|
|
|
|
" (old pid %d, new pid %d)", mClientPid, callingPid);
|
|
|
|
return EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t CameraService::Client::checkPidAndHardware() const {
|
|
|
|
status_t result = checkPid();
|
|
|
|
if (result != NO_ERROR) return result;
|
|
|
|
if (mHardware == 0) {
|
|
|
|
LOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid());
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t CameraService::Client::lock() {
|
|
|
|
int callingPid = getCallingPid();
|
|
|
|
LOG1("lock (pid %d)", callingPid);
|
|
|
|
Mutex::Autolock lock(mLock);
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
// 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;
|
|
|
|
}
|
2010-05-06 08:36:58 +00:00
|
|
|
|
|
|
|
// returns NO_ERROR if the client already owns the camera, EBUSY otherwise
|
2009-03-04 03:31:44 +00:00
|
|
|
return checkPid();
|
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
status_t CameraService::Client::unlock() {
|
2009-06-22 08:03:41 +00:00
|
|
|
int callingPid = getCallingPid();
|
2010-05-06 08:36:58 +00:00
|
|
|
LOG1("unlock (pid %d)", callingPid);
|
|
|
|
Mutex::Autolock lock(mLock);
|
|
|
|
|
|
|
|
// allow anyone to use camera (after they lock the camera)
|
2009-03-04 03:31:44 +00:00
|
|
|
status_t result = checkPid();
|
2009-04-21 02:35:28 +00:00
|
|
|
if (result == NO_ERROR) {
|
|
|
|
mClientPid = 0;
|
2010-05-06 08:36:58 +00:00
|
|
|
LOG1("clear mCameraClient (pid %d)", callingPid);
|
|
|
|
// we need to remove the reference to ICameraClient so that when the app
|
|
|
|
// goes away, the reference count goes to 0.
|
2009-04-21 02:35:28 +00:00
|
|
|
mCameraClient.clear();
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
// connect a new client to the camera
|
|
|
|
status_t CameraService::Client::connect(const sp<ICameraClient>& client) {
|
2009-06-22 08:03:41 +00:00
|
|
|
int callingPid = getCallingPid();
|
2010-05-06 08:36:58 +00:00
|
|
|
LOG1("connect E (pid %d)", callingPid);
|
|
|
|
Mutex::Autolock lock(mLock);
|
2009-06-22 08:03:41 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
if (mClientPid != 0 && checkPid() != NO_ERROR) {
|
|
|
|
LOGW("Tried to connect to a locked camera (old pid %d, new pid %d)",
|
|
|
|
mClientPid, callingPid);
|
|
|
|
return EBUSY;
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
if (mCameraClient != 0 && (client->asBinder() == mCameraClient->asBinder())) {
|
|
|
|
LOG1("Connect to the same client");
|
|
|
|
return NO_ERROR;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2010-05-06 08:36:58 +00:00
|
|
|
|
|
|
|
mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
|
2009-06-22 08:03:41 +00:00
|
|
|
mClientPid = callingPid;
|
2010-05-06 08:36:58 +00:00
|
|
|
mCameraClient = client;
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
LOG1("connect X (pid %d)", callingPid);
|
2009-03-04 03:31:44 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
void CameraService::Client::disconnect() {
|
2009-06-22 08:03:41 +00:00
|
|
|
int callingPid = getCallingPid();
|
2010-05-06 08:36:58 +00:00
|
|
|
LOG1("disconnect E (pid %d)", callingPid);
|
|
|
|
Mutex::Autolock lock(mLock);
|
2009-06-22 08:03:41 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
if (checkPid() != NO_ERROR) {
|
|
|
|
LOGW("different client - don't disconnect");
|
|
|
|
return;
|
2009-03-25 03:36:57 +00:00
|
|
|
}
|
2009-03-25 03:21:36 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
if (mClientPid <= 0) {
|
2010-05-06 08:36:58 +00:00
|
|
|
LOG1("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
|
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-05-06 08:36:58 +00:00
|
|
|
LOG1("hardware teardown");
|
2009-06-24 05:44:37 +00:00
|
|
|
// Before destroying mHardware, we must make sure it's in the
|
|
|
|
// idle state.
|
2010-05-06 08:36:58 +00:00
|
|
|
// Turn off all messages.
|
|
|
|
disableMsgType(CAMERA_MSG_ALL_MSGS);
|
2009-06-24 05:44:37 +00:00
|
|
|
mHardware->stopPreview();
|
2009-08-12 17:01:27 +00:00
|
|
|
mHardware->cancelPicture();
|
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.
|
2010-05-06 08:36:58 +00:00
|
|
|
if (mUseOverlay) {
|
2009-07-15 23:44:27 +00:00
|
|
|
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);
|
2010-05-06 08:36:58 +00:00
|
|
|
mCameraService->setCameraFree(mCameraId);
|
2009-06-24 05:44:37 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
LOG1("disconnect X (pid %d)", callingPid);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// set the ISurface that the preview will use
|
|
|
|
status_t CameraService::Client::setPreviewDisplay(const sp<ISurface>& surface) {
|
|
|
|
LOG1("setPreviewDisplay(%p) (pid %d)", surface.get(), getCallingPid());
|
2009-03-04 03:31:44 +00:00
|
|
|
Mutex::Autolock lock(mLock);
|
2010-05-06 08:36:58 +00:00
|
|
|
status_t result = checkPidAndHardware();
|
2009-03-04 03:31:44 +00:00
|
|
|
if (result != NO_ERROR) return result;
|
2009-06-23 15:37:36 +00:00
|
|
|
|
|
|
|
result = NO_ERROR;
|
2009-08-12 17:01:27 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
// return if no change in surface.
|
|
|
|
// asBinder() is safe on NULL (returns NULL)
|
|
|
|
if (surface->asBinder() == mSurface->asBinder()) {
|
|
|
|
return result;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
if (mSurface != 0) {
|
|
|
|
LOG1("clearing old preview surface %p", mSurface.get());
|
|
|
|
if (mUseOverlay) {
|
|
|
|
// Force the destruction of any previous overlay
|
|
|
|
sp<Overlay> dummy;
|
|
|
|
mHardware->setOverlay(dummy);
|
|
|
|
} else {
|
|
|
|
mSurface->unregisterBuffers();
|
2009-06-23 15:37:36 +00:00
|
|
|
}
|
2010-05-06 08:36:58 +00:00
|
|
|
}
|
|
|
|
mSurface = surface;
|
|
|
|
mOverlayRef = 0;
|
|
|
|
// 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-06-23 15:37:36 +00:00
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
return result;
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
status_t CameraService::Client::registerPreviewBuffers() {
|
|
|
|
int w, h;
|
|
|
|
CameraParameters params(mHardware->getParameters());
|
|
|
|
params.getPreviewSize(&w, &h);
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
// FIXME: don't use a hardcoded format here.
|
|
|
|
ISurface::BufferHeap buffers(w, h, w, h,
|
|
|
|
HAL_PIXEL_FORMAT_YCrCb_420_SP,
|
|
|
|
mOrientation,
|
|
|
|
0,
|
|
|
|
mHardware->getPreviewHeap());
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
status_t result = mSurface->registerBuffers(buffers);
|
|
|
|
if (result != NO_ERROR) {
|
|
|
|
LOGE("registerBuffers failed with status %d", result);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2010-05-06 08:36:58 +00:00
|
|
|
return result;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
status_t CameraService::Client::setOverlay() {
|
2009-06-23 15:37:36 +00:00
|
|
|
int w, h;
|
|
|
|
CameraParameters params(mHardware->getParameters());
|
|
|
|
params.getPreviewSize(&w, &h);
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
if (w != mOverlayW || h != mOverlayH) {
|
2009-07-15 23:44:27 +00:00
|
|
|
// Force the destruction of any previous overlay
|
|
|
|
sp<Overlay> dummy;
|
2010-05-06 08:36:58 +00:00
|
|
|
mHardware->setOverlay(dummy);
|
2009-07-15 23:44:27 +00:00
|
|
|
mOverlayRef = 0;
|
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
status_t result = NO_ERROR;
|
|
|
|
if (mSurface == 0) {
|
|
|
|
result = mHardware->setOverlay(NULL);
|
|
|
|
} else {
|
|
|
|
if (mOverlayRef == 0) {
|
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
|
2010-05-06 08:36:58 +00:00
|
|
|
// wait in the createOverlay call if the previous overlay is in the
|
2009-10-08 02:18:20 +00:00
|
|
|
// 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);
|
2010-05-06 08:36:58 +00:00
|
|
|
if (mOverlayRef != 0) break;
|
2010-01-04 16:55:04 +00:00
|
|
|
LOGW("Overlay create failed - retrying");
|
2009-10-08 02:18:20 +00:00
|
|
|
usleep(20000);
|
|
|
|
}
|
2010-05-06 08:36:58 +00:00
|
|
|
if (mOverlayRef == 0) {
|
2009-07-15 23:44:27 +00:00
|
|
|
LOGE("Overlay Creation Failed!");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2010-05-06 08:36:58 +00:00
|
|
|
result = mHardware->setOverlay(new Overlay(mOverlayRef));
|
2009-07-15 23:44:27 +00:00
|
|
|
}
|
2009-06-23 15:37:36 +00:00
|
|
|
}
|
2010-05-06 08:36:58 +00:00
|
|
|
if (result != NO_ERROR) {
|
|
|
|
LOGE("mHardware->setOverlay() failed with status %d\n", result);
|
|
|
|
return result;
|
2009-06-23 15:37:36 +00:00
|
|
|
}
|
2009-07-15 23:44:27 +00:00
|
|
|
|
|
|
|
mOverlayW = w;
|
|
|
|
mOverlayH = h;
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the preview callback flag to affect how the received frames from
|
|
|
|
// preview are handled.
|
|
|
|
void CameraService::Client::setPreviewCallbackFlag(int callback_flag) {
|
|
|
|
LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid());
|
|
|
|
Mutex::Autolock lock(mLock);
|
|
|
|
if (checkPidAndHardware() != NO_ERROR) return;
|
|
|
|
|
|
|
|
mPreviewCallbackFlag = callback_flag;
|
|
|
|
|
|
|
|
// If we don't use overlay, we always need the preview frame for display.
|
|
|
|
// If we do use overlay, we only need the preview frame if the user
|
|
|
|
// wants the data.
|
|
|
|
if (mUseOverlay) {
|
|
|
|
if(mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ENABLE_MASK) {
|
|
|
|
enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
|
|
|
|
} else {
|
|
|
|
disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
|
|
|
|
}
|
|
|
|
}
|
2009-06-23 15:37:36 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
// start preview mode
|
|
|
|
status_t CameraService::Client::startPreview() {
|
|
|
|
LOG1("startPreview (pid %d)", getCallingPid());
|
|
|
|
return startCameraMode(CAMERA_PREVIEW_MODE);
|
|
|
|
}
|
2009-06-23 15:37:36 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
// start recording mode
|
|
|
|
status_t CameraService::Client::startRecording() {
|
|
|
|
LOG1("startRecording (pid %d)", getCallingPid());
|
|
|
|
return startCameraMode(CAMERA_RECORDING_MODE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// start preview or recording
|
|
|
|
status_t CameraService::Client::startCameraMode(camera_mode mode) {
|
|
|
|
LOG1("startCameraMode(%d)", mode);
|
|
|
|
Mutex::Autolock lock(mLock);
|
|
|
|
status_t result = checkPidAndHardware();
|
|
|
|
if (result != NO_ERROR) return result;
|
2009-06-23 15:37:36 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
switch(mode) {
|
|
|
|
case CAMERA_PREVIEW_MODE:
|
|
|
|
if (mSurface == 0) {
|
|
|
|
LOG1("mSurface is not set yet.");
|
|
|
|
// still able to start preview in this case.
|
|
|
|
}
|
|
|
|
return startPreviewMode();
|
|
|
|
case CAMERA_RECORDING_MODE:
|
|
|
|
if (mSurface == 0) {
|
|
|
|
LOGE("mSurface must be set before startRecordingMode.");
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
|
|
|
return startRecordingMode();
|
|
|
|
default:
|
|
|
|
return UNKNOWN_ERROR;
|
2009-06-23 15:37:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
status_t CameraService::Client::startPreviewMode() {
|
|
|
|
LOG1("startPreviewMode");
|
|
|
|
status_t result = NO_ERROR;
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
// if preview has been enabled, nothing needs to be done
|
|
|
|
if (mHardware->previewEnabled()) {
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mUseOverlay) {
|
2009-06-23 15:37:36 +00:00
|
|
|
// If preview display has been set, set overlay now.
|
|
|
|
if (mSurface != 0) {
|
2010-05-06 08:36:58 +00:00
|
|
|
result = setOverlay();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2010-05-06 08:36:58 +00:00
|
|
|
if (result != NO_ERROR) return result;
|
|
|
|
result = mHardware->startPreview();
|
2009-03-04 03:31:44 +00:00
|
|
|
} else {
|
2010-05-06 08:36:58 +00:00
|
|
|
enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
|
|
|
|
result = mHardware->startPreview();
|
|
|
|
if (result != NO_ERROR) return result;
|
2009-06-23 15:37:36 +00:00
|
|
|
// If preview display has been set, register preview buffers now.
|
|
|
|
if (mSurface != 0) {
|
2010-05-06 08:36:58 +00:00
|
|
|
// Unregister here because the surface may be previously registered
|
|
|
|
// with the raw (snapshot) heap.
|
2009-06-23 15:37:36 +00:00
|
|
|
mSurface->unregisterBuffers();
|
2010-05-06 08:36:58 +00:00
|
|
|
result = registerPreviewBuffers();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
2010-05-06 08:36:58 +00:00
|
|
|
return result;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
status_t CameraService::Client::startRecordingMode() {
|
|
|
|
LOG1("startRecordingMode");
|
|
|
|
status_t result = NO_ERROR;
|
2009-09-28 23:14:58 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
// if recording has been enabled, nothing needs to be done
|
|
|
|
if (mHardware->recordingEnabled()) {
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
2009-06-22 08:03:41 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
// if preview has not been started, start preview first
|
|
|
|
if (!mHardware->previewEnabled()) {
|
|
|
|
result = startPreviewMode();
|
|
|
|
if (result != NO_ERROR) {
|
|
|
|
return result;
|
2009-11-27 13:07:55 +00:00
|
|
|
}
|
2009-03-25 03:21:36 +00:00
|
|
|
}
|
2009-08-12 17:01:27 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
// start recording mode
|
|
|
|
enableMsgType(CAMERA_MSG_VIDEO_FRAME);
|
|
|
|
mCameraService->playSound(SOUND_RECORDING);
|
|
|
|
result = mHardware->startRecording();
|
|
|
|
if (result != NO_ERROR) {
|
|
|
|
LOGE("mHardware->startRecording() failed with status %d", result);
|
|
|
|
}
|
|
|
|
return result;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// stop preview mode
|
2010-05-06 08:36:58 +00:00
|
|
|
void CameraService::Client::stopPreview() {
|
|
|
|
LOG1("stopPreview (pid %d)", getCallingPid());
|
|
|
|
Mutex::Autolock lock(mLock);
|
|
|
|
if (checkPidAndHardware() != NO_ERROR) return;
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
|
|
|
|
mHardware->stopPreview();
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
if (mSurface != 0 && !mUseOverlay) {
|
|
|
|
mSurface->unregisterBuffers();
|
2009-11-11 01:08:08 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
mPreviewBuffer.clear();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// stop recording mode
|
2010-05-06 08:36:58 +00:00
|
|
|
void CameraService::Client::stopRecording() {
|
|
|
|
LOG1("stopRecording (pid %d)", getCallingPid());
|
|
|
|
Mutex::Autolock lock(mLock);
|
|
|
|
if (checkPidAndHardware() != NO_ERROR) return;
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
mCameraService->playSound(SOUND_RECORDING);
|
|
|
|
disableMsgType(CAMERA_MSG_VIDEO_FRAME);
|
|
|
|
mHardware->stopRecording();
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
mPreviewBuffer.clear();
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
// release a recording frame
|
|
|
|
void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem) {
|
|
|
|
Mutex::Autolock lock(mLock);
|
|
|
|
if (checkPidAndHardware() != NO_ERROR) return;
|
|
|
|
mHardware->releaseRecordingFrame(mem);
|
|
|
|
}
|
2009-08-12 17:01:27 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
bool CameraService::Client::previewEnabled() {
|
|
|
|
LOG1("previewEnabled (pid %d)", getCallingPid());
|
2009-08-12 17:01:27 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
Mutex::Autolock lock(mLock);
|
|
|
|
if (checkPidAndHardware() != NO_ERROR) return false;
|
|
|
|
return mHardware->previewEnabled();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
bool CameraService::Client::recordingEnabled() {
|
|
|
|
LOG1("recordingEnabled (pid %d)", getCallingPid());
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
Mutex::Autolock lock(mLock);
|
2010-05-06 08:36:58 +00:00
|
|
|
if (checkPidAndHardware() != NO_ERROR) return false;
|
|
|
|
return mHardware->recordingEnabled();
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
status_t CameraService::Client::autoFocus() {
|
|
|
|
LOG1("autoFocus (pid %d)", getCallingPid());
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
Mutex::Autolock lock(mLock);
|
|
|
|
status_t result = checkPidAndHardware();
|
|
|
|
if (result != NO_ERROR) return result;
|
|
|
|
|
|
|
|
return mHardware->autoFocus();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
status_t CameraService::Client::cancelAutoFocus() {
|
|
|
|
LOG1("cancelAutoFocus (pid %d)", getCallingPid());
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
Mutex::Autolock lock(mLock);
|
2010-05-06 08:36:58 +00:00
|
|
|
status_t result = checkPidAndHardware();
|
|
|
|
if (result != NO_ERROR) return result;
|
|
|
|
|
|
|
|
return mHardware->cancelAutoFocus();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
// take a picture - image is returned in callback
|
|
|
|
status_t CameraService::Client::takePicture() {
|
|
|
|
LOG1("takePicture (pid %d)", getCallingPid());
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
Mutex::Autolock lock(mLock);
|
2010-05-06 08:36:58 +00:00
|
|
|
status_t result = checkPidAndHardware();
|
|
|
|
if (result != NO_ERROR) return result;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
// set preview/capture parameters - key/value pairs
|
|
|
|
status_t CameraService::Client::setParameters(const String8& params) {
|
|
|
|
LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
|
|
|
|
|
|
|
|
Mutex::Autolock lock(mLock);
|
|
|
|
status_t result = checkPidAndHardware();
|
|
|
|
if (result != NO_ERROR) return result;
|
|
|
|
|
|
|
|
CameraParameters p(params);
|
|
|
|
return mHardware->setParameters(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get preview/capture parameters - key/value pairs
|
|
|
|
String8 CameraService::Client::getParameters() const {
|
|
|
|
Mutex::Autolock lock(mLock);
|
|
|
|
if (checkPidAndHardware() != NO_ERROR) return String8();
|
|
|
|
|
|
|
|
String8 params(mHardware->getParameters().flatten());
|
|
|
|
LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t CameraService::Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
|
|
|
|
LOG1("sendCommand (pid %d)", getCallingPid());
|
|
|
|
Mutex::Autolock lock(mLock);
|
|
|
|
status_t result = checkPidAndHardware();
|
|
|
|
if (result != NO_ERROR) return result;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mHardware->sendCommand(cmd, arg1, arg2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void CameraService::Client::enableMsgType(int32_t msgType) {
|
|
|
|
android_atomic_or(msgType, &mMsgEnabled);
|
|
|
|
mHardware->enableMsgType(msgType);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CameraService::Client::disableMsgType(int32_t msgType) {
|
|
|
|
android_atomic_and(~msgType, &mMsgEnabled);
|
|
|
|
mHardware->disableMsgType(msgType);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CHECK_MESSAGE_INTERVAL 10 // 10ms
|
|
|
|
bool CameraService::Client::lockIfMessageWanted(int32_t msgType) {
|
|
|
|
int sleepCount = 0;
|
|
|
|
while (mMsgEnabled & msgType) {
|
|
|
|
if (mLock.tryLock() == NO_ERROR) {
|
|
|
|
if (sleepCount > 0) {
|
|
|
|
LOG1("lockIfMessageWanted(%d): waited for %d ms",
|
|
|
|
msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2010-05-06 08:36:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (sleepCount++ == 0) {
|
|
|
|
LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2010-05-06 08:36:58 +00:00
|
|
|
usleep(CHECK_MESSAGE_INTERVAL * 1000);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2010-05-06 08:36:58 +00:00
|
|
|
LOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
|
|
|
|
return false;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
// Converts from a raw pointer to the client to a strong pointer during a
|
|
|
|
// hardware callback. This requires the callbacks only happen when the client
|
|
|
|
// is still alive.
|
|
|
|
sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user) {
|
|
|
|
sp<Client> client = gCameraService->getClientById((int) user);
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
// This could happen if the Client is in the process of shutting down (the
|
|
|
|
// last strong reference is gone, but the destructor hasn't finished
|
|
|
|
// stopping the hardware).
|
|
|
|
if (client == 0) return NULL;
|
|
|
|
|
|
|
|
// The checks below are not necessary and are for debugging only.
|
|
|
|
if (client->mCameraService.get() != gCameraService) {
|
|
|
|
LOGE("mismatch service!");
|
|
|
|
return NULL;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
if (client->mHardware == 0) {
|
|
|
|
LOGE("mHardware == 0: callback after disconnect()?");
|
|
|
|
return NULL;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2010-05-06 08:36:58 +00:00
|
|
|
|
|
|
|
return client;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
// Callback messages can be dispatched to internal handlers or pass to our
|
|
|
|
// client's callback functions, depending on the message type.
|
|
|
|
//
|
|
|
|
// notifyCallback:
|
|
|
|
// CAMERA_MSG_SHUTTER handleShutter
|
|
|
|
// (others) c->notifyCallback
|
|
|
|
// dataCallback:
|
|
|
|
// CAMERA_MSG_PREVIEW_FRAME handlePreviewData
|
|
|
|
// CAMERA_MSG_POSTVIEW_FRAME handlePostview
|
|
|
|
// CAMERA_MSG_RAW_IMAGE handleRawPicture
|
|
|
|
// CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture
|
|
|
|
// (others) c->dataCallback
|
|
|
|
// dataCallbackTimestamp
|
|
|
|
// (others) c->dataCallbackTimestamp
|
|
|
|
//
|
|
|
|
// NOTE: the *Callback functions grab mLock of the client before passing
|
|
|
|
// control to handle* functions. So the handle* functions must release the
|
|
|
|
// lock before calling the ICameraClient's callbacks, so those callbacks can
|
|
|
|
// invoke methods in the Client class again (For example, the preview frame
|
|
|
|
// callback may want to releaseRecordingFrame). The handle* functions must
|
|
|
|
// release the lock after all accesses to member variables, so it must be
|
|
|
|
// handled very carefully.
|
|
|
|
|
|
|
|
void CameraService::Client::notifyCallback(int32_t msgType, int32_t ext1,
|
|
|
|
int32_t ext2, void* user) {
|
|
|
|
LOG2("notifyCallback(%d)", msgType);
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
sp<Client> client = getClientFromCookie(user);
|
|
|
|
if (client == 0) return;
|
|
|
|
if (!client->lockIfMessageWanted(msgType)) return;
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
switch (msgType) {
|
|
|
|
case CAMERA_MSG_SHUTTER:
|
|
|
|
// ext1 is the dimension of the yuv picture.
|
|
|
|
client->handleShutter((image_rect_type *)ext1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
client->handleGenericNotify(msgType, ext1, ext2);
|
|
|
|
break;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
void CameraService::Client::dataCallback(int32_t msgType,
|
|
|
|
const sp<IMemory>& dataPtr, void* user) {
|
|
|
|
LOG2("dataCallback(%d)", msgType);
|
2009-09-15 06:51:56 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
sp<Client> client = getClientFromCookie(user);
|
|
|
|
if (client == 0) return;
|
|
|
|
if (!client->lockIfMessageWanted(msgType)) return;
|
2009-09-15 06:51:56 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
if (dataPtr == 0) {
|
|
|
|
LOGE("Null data returned in data callback");
|
|
|
|
client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
|
|
|
|
return;
|
2009-09-15 06:51:56 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +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:
|
|
|
|
client->handleGenericData(msgType, dataPtr);
|
|
|
|
break;
|
|
|
|
}
|
2009-09-15 06:51:56 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
void CameraService::Client::dataCallbackTimestamp(nsecs_t timestamp,
|
|
|
|
int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
|
|
|
|
LOG2("dataCallbackTimestamp(%d)", msgType);
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
sp<Client> client = getClientFromCookie(user);
|
|
|
|
if (client == 0) return;
|
|
|
|
if (!client->lockIfMessageWanted(msgType)) return;
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
if (dataPtr == 0) {
|
|
|
|
LOGE("Null data returned in data with timestamp callback");
|
|
|
|
client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
|
|
|
|
return;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
// snapshot taken callback
|
|
|
|
// "size" is the width and height of yuv picture for registerBuffer.
|
|
|
|
// If it is NULL, use the picture size from parameters.
|
|
|
|
void CameraService::Client::handleShutter(image_rect_type *size) {
|
|
|
|
mCameraService->playSound(SOUND_SHUTTER);
|
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;
|
2010-05-06 08:36:58 +00:00
|
|
|
if (c != 0) {
|
|
|
|
mLock.unlock();
|
2009-10-15 17:02:22 +00:00
|
|
|
c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
|
2010-05-06 08:36:58 +00:00
|
|
|
if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
|
2009-10-15 17:02:22 +00:00
|
|
|
}
|
2010-05-06 08:36:58 +00:00
|
|
|
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-05-06 08:36:58 +00:00
|
|
|
LOG1("Snapshot image width=%d, height=%d", w, h);
|
2009-10-23 09:39:46 +00:00
|
|
|
}
|
2010-02-17 03:42:32 +00:00
|
|
|
// FIXME: don't use hardcoded format constants here
|
2009-03-04 03:31:44 +00:00
|
|
|
ISurface::BufferHeap buffers(w, h, w, h,
|
2010-02-17 03:42:32 +00:00
|
|
|
HAL_PIXEL_FORMAT_YCrCb_420_SP, mOrientation, 0,
|
2010-02-17 01:33:37 +00:00
|
|
|
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
|
|
|
}
|
2010-05-06 08:36:58 +00:00
|
|
|
|
|
|
|
mLock.unlock();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-08-12 17:01:27 +00:00
|
|
|
// preview callback - frame buffer update
|
2010-05-06 08:36:58 +00:00
|
|
|
void CameraService::Client::handlePreviewData(const sp<IMemory>& mem) {
|
2009-08-12 17:01:27 +00:00
|
|
|
ssize_t offset;
|
|
|
|
size_t size;
|
|
|
|
sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
if (!mUseOverlay) {
|
|
|
|
if (mSurface != 0) {
|
2009-08-12 17:01:27 +00:00
|
|
|
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
|
2010-05-06 08:36:58 +00:00
|
|
|
LOG2("frame callback is disabled");
|
|
|
|
mLock.unlock();
|
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
|
2010-05-06 08:36:58 +00:00
|
|
|
if (c == 0 || (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
|
|
|
|
LOG2("Disable preview callback");
|
2009-08-12 17:01:27 +00:00
|
|
|
mPreviewCallbackFlag &= ~(FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
|
2010-05-06 08:36:58 +00:00
|
|
|
FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
|
|
|
|
FRAME_CALLBACK_FLAG_ENABLE_MASK);
|
|
|
|
if (mUseOverlay) {
|
|
|
|
disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
|
|
|
|
}
|
2009-08-12 17:01:27 +00:00
|
|
|
}
|
2009-10-15 17:02:22 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
if (c != 0) {
|
|
|
|
// Is the received frame copied out or not?
|
|
|
|
if (flags & FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
|
|
|
|
LOG2("frame is copied");
|
|
|
|
copyFrameAndPostCopiedFrame(c, heap, offset, size);
|
|
|
|
} else {
|
|
|
|
LOG2("frame is forwarded");
|
|
|
|
mLock.unlock();
|
|
|
|
c->dataCallback(CAMERA_MSG_PREVIEW_FRAME, mem);
|
|
|
|
}
|
2009-10-15 17:02:22 +00:00
|
|
|
} else {
|
2010-05-06 08:36:58 +00:00
|
|
|
mLock.unlock();
|
2009-10-15 17:02:22 +00:00
|
|
|
}
|
2009-08-12 17:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// picture callback - postview image ready
|
2010-05-06 08:36:58 +00:00
|
|
|
void CameraService::Client::handlePostview(const sp<IMemory>& mem) {
|
|
|
|
disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
|
2009-08-12 17:01:27 +00:00
|
|
|
|
2009-10-15 17:02:22 +00:00
|
|
|
sp<ICameraClient> c = mCameraClient;
|
2010-05-06 08:36:58 +00:00
|
|
|
mLock.unlock();
|
|
|
|
if (c != 0) {
|
2009-10-15 17:02:22 +00:00
|
|
|
c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem);
|
|
|
|
}
|
2009-08-12 17:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// picture callback - raw image ready
|
2010-05-06 08:36:58 +00:00
|
|
|
void CameraService::Client::handleRawPicture(const sp<IMemory>& mem) {
|
|
|
|
disableMsgType(CAMERA_MSG_RAW_IMAGE);
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
ssize_t offset;
|
|
|
|
size_t size;
|
|
|
|
sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
|
|
|
|
|
|
|
|
// 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;
|
2010-05-06 08:36:58 +00:00
|
|
|
mLock.unlock();
|
|
|
|
if (c != 0) {
|
2009-10-15 17:02:22 +00:00
|
|
|
c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem);
|
|
|
|
}
|
2009-08-12 17:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// picture callback - compressed picture ready
|
2010-05-06 08:36:58 +00:00
|
|
|
void CameraService::Client::handleCompressedPicture(const sp<IMemory>& mem) {
|
|
|
|
disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
|
2009-08-12 17:01:27 +00:00
|
|
|
|
2009-10-15 17:02:22 +00:00
|
|
|
sp<ICameraClient> c = mCameraClient;
|
2010-05-06 08:36:58 +00:00
|
|
|
mLock.unlock();
|
|
|
|
if (c != 0) {
|
2009-10-15 17:02:22 +00:00
|
|
|
c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem);
|
|
|
|
}
|
2009-08-12 17:01:27 +00:00
|
|
|
}
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
void CameraService::Client::handleGenericNotify(int32_t msgType,
|
|
|
|
int32_t ext1, int32_t ext2) {
|
|
|
|
sp<ICameraClient> c = mCameraClient;
|
|
|
|
mLock.unlock();
|
|
|
|
if (c != 0) {
|
|
|
|
c->notifyCallback(msgType, ext1, ext2);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
void CameraService::Client::handleGenericData(int32_t msgType,
|
|
|
|
const sp<IMemory>& dataPtr) {
|
|
|
|
sp<ICameraClient> c = mCameraClient;
|
|
|
|
mLock.unlock();
|
|
|
|
if (c != 0) {
|
|
|
|
c->dataCallback(msgType, dataPtr);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
void CameraService::Client::handleGenericDataTimestamp(nsecs_t timestamp,
|
|
|
|
int32_t msgType, const sp<IMemory>& dataPtr) {
|
|
|
|
sp<ICameraClient> c = mCameraClient;
|
|
|
|
mLock.unlock();
|
|
|
|
if (c != 0) {
|
2009-10-15 17:02:22 +00:00
|
|
|
c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
|
|
|
|
}
|
2009-09-28 23:14:58 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
void CameraService::Client::copyFrameAndPostCopiedFrame(
|
|
|
|
const sp<ICameraClient>& client, const sp<IMemoryHeap>& heap,
|
|
|
|
size_t offset, size_t size) {
|
|
|
|
LOG2("copyFrameAndPostCopiedFrame");
|
2009-03-04 03:31:44 +00:00
|
|
|
// 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-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;
|
2010-05-06 08:36:58 +00:00
|
|
|
|
|
|
|
if (mPreviewBuffer == 0) {
|
|
|
|
mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
|
|
|
|
} else if (size > mPreviewBuffer->virtualSize()) {
|
|
|
|
mPreviewBuffer.clear();
|
|
|
|
mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
|
|
|
|
}
|
|
|
|
if (mPreviewBuffer == 0) {
|
|
|
|
LOGE("failed to allocate space for preview buffer");
|
|
|
|
mLock.unlock();
|
|
|
|
return;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2010-05-06 08:36:58 +00:00
|
|
|
previewBuffer = mPreviewBuffer;
|
|
|
|
|
|
|
|
memcpy(previewBuffer->base(), (uint8_t *)heap->base() + offset, size);
|
2009-03-04 03:31:44 +00:00
|
|
|
|
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");
|
2010-05-06 08:36:58 +00:00
|
|
|
mLock.unlock();
|
2009-03-04 03:31:44 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-05-06 08:36:58 +00:00
|
|
|
|
|
|
|
mLock.unlock();
|
2009-10-15 17:02:22 +00:00
|
|
|
client->dataCallback(CAMERA_MSG_PREVIEW_FRAME, frame);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2010-03-05 19:33:03 +00:00
|
|
|
static const int kDumpLockRetries = 50;
|
|
|
|
static const int kDumpLockSleep = 60000;
|
|
|
|
|
|
|
|
static bool tryLock(Mutex& mutex)
|
|
|
|
{
|
|
|
|
bool locked = false;
|
|
|
|
for (int i = 0; i < kDumpLockRetries; ++i) {
|
|
|
|
if (mutex.tryLock() == NO_ERROR) {
|
|
|
|
locked = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
usleep(kDumpLockSleep);
|
|
|
|
}
|
|
|
|
return locked;
|
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
status_t CameraService::dump(int fd, const Vector<String16>& args) {
|
2010-03-05 19:33:03 +00:00
|
|
|
static const char* kDeadlockedString = "CameraService may be deadlocked\n";
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
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(),
|
2010-05-06 08:36:58 +00:00
|
|
|
getCallingUid());
|
2009-03-04 03:31:44 +00:00
|
|
|
result.append(buffer);
|
|
|
|
write(fd, result.string(), result.size());
|
|
|
|
} else {
|
2010-03-05 19:33:03 +00:00
|
|
|
bool locked = tryLock(mServiceLock);
|
|
|
|
// failed to lock - CameraService is probably deadlocked
|
|
|
|
if (!locked) {
|
|
|
|
String8 result(kDeadlockedString);
|
|
|
|
write(fd, result.string(), result.size());
|
|
|
|
}
|
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
bool hasClient = false;
|
|
|
|
for (int i = 0; i < NUM_CAMERAS; i++) {
|
|
|
|
sp<Client> client = mClient[i].promote();
|
|
|
|
if (client == 0) continue;
|
|
|
|
hasClient = true;
|
|
|
|
sprintf(buffer, "Client[%d] (%p) PID: %d\n",
|
|
|
|
i,
|
|
|
|
client->getCameraClient()->asBinder().get(),
|
|
|
|
client->mClientPid);
|
2009-03-04 03:31:44 +00:00
|
|
|
result.append(buffer);
|
|
|
|
write(fd, result.string(), result.size());
|
2010-05-06 08:36:58 +00:00
|
|
|
client->mHardware->dump(fd, args);
|
|
|
|
}
|
|
|
|
if (!hasClient) {
|
2009-03-04 03:31:44 +00:00
|
|
|
result.append("No camera client yet.\n");
|
|
|
|
write(fd, result.string(), result.size());
|
|
|
|
}
|
2010-03-05 19:33:03 +00:00
|
|
|
|
|
|
|
if (locked) mServiceLock.unlock();
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-05-06 08:36:58 +00:00
|
|
|
// change logging level
|
|
|
|
int n = args.size();
|
|
|
|
for (int i = 0; i + 1 < n; i++) {
|
|
|
|
if (args[i] == String16("-v")) {
|
|
|
|
String8 levelStr(args[i+1]);
|
|
|
|
int level = atoi(levelStr.string());
|
|
|
|
sprintf(buffer, "Set Log Level to %d", level);
|
|
|
|
result.append(buffer);
|
|
|
|
setLogLevel(level);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-05-06 08:36:58 +00:00
|
|
|
return NO_ERROR;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}; // namespace android
|