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_NDEBUG 0
|
|
|
|
#define LOG_TAG "Camera"
|
|
|
|
#include <utils/Log.h>
|
2009-05-20 02:08:10 +00:00
|
|
|
#include <binder/IServiceManager.h>
|
2009-03-04 03:31:44 +00:00
|
|
|
#include <utils/threads.h>
|
2009-05-20 02:08:10 +00:00
|
|
|
#include <binder/IMemory.h>
|
2009-03-04 03:31:44 +00:00
|
|
|
#include <ui/Surface.h>
|
|
|
|
#include <ui/Camera.h>
|
|
|
|
#include <ui/ICameraService.h>
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
|
|
|
// client singleton for camera service binder interface
|
|
|
|
Mutex Camera::mLock;
|
|
|
|
sp<ICameraService> Camera::mCameraService;
|
|
|
|
sp<Camera::DeathNotifier> Camera::mDeathNotifier;
|
|
|
|
|
|
|
|
// establish binder interface to camera service
|
|
|
|
const sp<ICameraService>& Camera::getCameraService()
|
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
if (mCameraService.get() == 0) {
|
|
|
|
sp<IServiceManager> sm = defaultServiceManager();
|
|
|
|
sp<IBinder> binder;
|
|
|
|
do {
|
|
|
|
binder = sm->getService(String16("media.camera"));
|
|
|
|
if (binder != 0)
|
|
|
|
break;
|
|
|
|
LOGW("CameraService not published, waiting...");
|
|
|
|
usleep(500000); // 0.5 s
|
|
|
|
} while(true);
|
|
|
|
if (mDeathNotifier == NULL) {
|
|
|
|
mDeathNotifier = new DeathNotifier();
|
|
|
|
}
|
|
|
|
binder->linkToDeath(mDeathNotifier);
|
|
|
|
mCameraService = interface_cast<ICameraService>(binder);
|
|
|
|
}
|
|
|
|
LOGE_IF(mCameraService==0, "no CameraService!?");
|
|
|
|
return mCameraService;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
Camera::Camera()
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2009-04-27 19:01:59 +00:00
|
|
|
// construct a camera client from an existing camera remote
|
2009-04-23 21:07:23 +00:00
|
|
|
sp<Camera> Camera::create(const sp<ICamera>& camera)
|
|
|
|
{
|
2009-04-27 19:01:59 +00:00
|
|
|
LOGV("create");
|
|
|
|
if (camera == 0) {
|
|
|
|
LOGE("camera remote is a NULL pointer");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-04-23 21:07:23 +00:00
|
|
|
sp<Camera> c = new Camera();
|
|
|
|
if (camera->connect(c) == NO_ERROR) {
|
|
|
|
c->mStatus = NO_ERROR;
|
|
|
|
c->mCamera = camera;
|
|
|
|
camera->asBinder()->linkToDeath(c);
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
void Camera::init()
|
|
|
|
{
|
|
|
|
mStatus = UNKNOWN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
Camera::~Camera()
|
|
|
|
{
|
|
|
|
disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
sp<Camera> Camera::connect()
|
|
|
|
{
|
|
|
|
LOGV("connect");
|
|
|
|
sp<Camera> c = new Camera();
|
|
|
|
const sp<ICameraService>& cs = getCameraService();
|
|
|
|
if (cs != 0) {
|
|
|
|
c->mCamera = cs->connect(c);
|
|
|
|
}
|
|
|
|
if (c->mCamera != 0) {
|
|
|
|
c->mCamera->asBinder()->linkToDeath(c);
|
|
|
|
c->mStatus = NO_ERROR;
|
|
|
|
} else {
|
|
|
|
c.clear();
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::disconnect()
|
|
|
|
{
|
|
|
|
LOGV("disconnect");
|
|
|
|
if (mCamera != 0) {
|
|
|
|
mCamera->disconnect();
|
|
|
|
mCamera = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t Camera::reconnect()
|
|
|
|
{
|
|
|
|
LOGV("reconnect");
|
|
|
|
sp <ICamera> c = mCamera;
|
|
|
|
if (c == 0) return NO_INIT;
|
|
|
|
return c->connect(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
sp<ICamera> Camera::remote()
|
|
|
|
{
|
|
|
|
return mCamera;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t Camera::lock()
|
|
|
|
{
|
|
|
|
sp <ICamera> c = mCamera;
|
|
|
|
if (c == 0) return NO_INIT;
|
|
|
|
return c->lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t Camera::unlock()
|
|
|
|
{
|
|
|
|
sp <ICamera> c = mCamera;
|
|
|
|
if (c == 0) return NO_INIT;
|
|
|
|
return c->unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
// pass the buffered ISurface to the camera service
|
|
|
|
status_t Camera::setPreviewDisplay(const sp<Surface>& surface)
|
|
|
|
{
|
|
|
|
LOGV("setPreviewDisplay");
|
|
|
|
sp <ICamera> c = mCamera;
|
|
|
|
if (c == 0) return NO_INIT;
|
2009-06-23 15:37:36 +00:00
|
|
|
if (surface != 0) {
|
|
|
|
return c->setPreviewDisplay(surface->getISurface());
|
|
|
|
} else {
|
|
|
|
LOGD("app passed NULL surface");
|
|
|
|
return c->setPreviewDisplay(0);
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status_t Camera::setPreviewDisplay(const sp<ISurface>& surface)
|
|
|
|
{
|
|
|
|
LOGV("setPreviewDisplay");
|
|
|
|
if (surface == 0) {
|
2009-06-23 15:37:36 +00:00
|
|
|
LOGD("app passed NULL surface");
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
sp <ICamera> c = mCamera;
|
|
|
|
if (c == 0) return NO_INIT;
|
|
|
|
return c->setPreviewDisplay(surface);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-23 15:37:36 +00:00
|
|
|
// start preview mode
|
2009-03-04 03:31:44 +00:00
|
|
|
status_t Camera::startPreview()
|
|
|
|
{
|
|
|
|
LOGV("startPreview");
|
|
|
|
sp <ICamera> c = mCamera;
|
|
|
|
if (c == 0) return NO_INIT;
|
|
|
|
return c->startPreview();
|
|
|
|
}
|
|
|
|
|
|
|
|
// start recording mode, must call setPreviewDisplay first
|
|
|
|
status_t Camera::startRecording()
|
|
|
|
{
|
|
|
|
LOGV("startRecording");
|
|
|
|
sp <ICamera> c = mCamera;
|
|
|
|
if (c == 0) return NO_INIT;
|
|
|
|
return c->startRecording();
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop preview mode
|
|
|
|
void Camera::stopPreview()
|
|
|
|
{
|
|
|
|
LOGV("stopPreview");
|
|
|
|
sp <ICamera> c = mCamera;
|
|
|
|
if (c == 0) return;
|
|
|
|
c->stopPreview();
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop recording mode
|
|
|
|
void Camera::stopRecording()
|
|
|
|
{
|
|
|
|
LOGV("stopRecording");
|
|
|
|
sp <ICamera> c = mCamera;
|
|
|
|
if (c == 0) return;
|
|
|
|
c->stopRecording();
|
|
|
|
}
|
|
|
|
|
|
|
|
// release a recording frame
|
|
|
|
void Camera::releaseRecordingFrame(const sp<IMemory>& mem)
|
|
|
|
{
|
|
|
|
LOGV("releaseRecordingFrame");
|
|
|
|
sp <ICamera> c = mCamera;
|
|
|
|
if (c == 0) return;
|
|
|
|
c->releaseRecordingFrame(mem);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get preview state
|
|
|
|
bool Camera::previewEnabled()
|
|
|
|
{
|
|
|
|
LOGV("previewEnabled");
|
|
|
|
sp <ICamera> c = mCamera;
|
|
|
|
if (c == 0) return false;
|
|
|
|
return c->previewEnabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
// get recording state
|
|
|
|
bool Camera::recordingEnabled()
|
|
|
|
{
|
|
|
|
LOGV("recordingEnabled");
|
|
|
|
sp <ICamera> c = mCamera;
|
|
|
|
if (c == 0) return false;
|
|
|
|
return c->recordingEnabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t Camera::autoFocus()
|
|
|
|
{
|
|
|
|
LOGV("autoFocus");
|
|
|
|
sp <ICamera> c = mCamera;
|
|
|
|
if (c == 0) return NO_INIT;
|
|
|
|
return c->autoFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
// take a picture
|
|
|
|
status_t Camera::takePicture()
|
|
|
|
{
|
|
|
|
LOGV("takePicture");
|
|
|
|
sp <ICamera> c = mCamera;
|
|
|
|
if (c == 0) return NO_INIT;
|
|
|
|
return c->takePicture();
|
|
|
|
}
|
|
|
|
|
|
|
|
// set preview/capture parameters - key/value pairs
|
|
|
|
status_t Camera::setParameters(const String8& params)
|
|
|
|
{
|
|
|
|
LOGV("setParameters");
|
|
|
|
sp <ICamera> c = mCamera;
|
|
|
|
if (c == 0) return NO_INIT;
|
|
|
|
return c->setParameters(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get preview/capture parameters - key/value pairs
|
|
|
|
String8 Camera::getParameters() const
|
|
|
|
{
|
|
|
|
LOGV("getParameters");
|
|
|
|
String8 params;
|
|
|
|
sp <ICamera> c = mCamera;
|
|
|
|
if (c != 0) params = mCamera->getParameters();
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2009-06-24 00:30:11 +00:00
|
|
|
void Camera::setListener(const sp<CameraListener>& listener)
|
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
mListener = listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::setPreviewCallbackFlags(int flag)
|
|
|
|
{
|
|
|
|
LOGV("setPreviewCallbackFlags");
|
|
|
|
sp <ICamera> c = mCamera;
|
|
|
|
if (c == 0) return;
|
|
|
|
mCamera->setPreviewCallbackFlag(flag);
|
|
|
|
}
|
|
|
|
|
2009-05-07 19:25:25 +00:00
|
|
|
// callback from camera service
|
|
|
|
void Camera::notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2)
|
|
|
|
{
|
2009-06-24 00:30:11 +00:00
|
|
|
sp<CameraListener> listener;
|
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
listener = mListener;
|
|
|
|
}
|
|
|
|
if (listener != NULL) {
|
|
|
|
listener->notify(msgType, ext1, ext2);
|
|
|
|
}
|
2009-05-07 19:25:25 +00:00
|
|
|
}
|
|
|
|
|
2009-05-08 02:27:32 +00:00
|
|
|
// callback from camera service when frame or image is ready
|
|
|
|
void Camera::dataCallback(int32_t msgType, const sp<IMemory>& dataPtr)
|
2009-05-07 19:25:25 +00:00
|
|
|
{
|
2009-06-24 00:30:11 +00:00
|
|
|
sp<CameraListener> listener;
|
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
listener = mListener;
|
|
|
|
}
|
|
|
|
if (listener != NULL) {
|
|
|
|
listener->postData(msgType, dataPtr);
|
|
|
|
}
|
2009-05-07 19:25:25 +00:00
|
|
|
}
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
void Camera::binderDied(const wp<IBinder>& who) {
|
|
|
|
LOGW("ICamera died");
|
2009-07-02 17:04:20 +00:00
|
|
|
notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_SERVER_DIED, 0);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::DeathNotifier::binderDied(const wp<IBinder>& who) {
|
|
|
|
LOGV("binderDied");
|
|
|
|
Mutex::Autolock _l(Camera::mLock);
|
|
|
|
Camera::mCameraService.clear();
|
|
|
|
LOGW("Camera server died!");
|
|
|
|
}
|
|
|
|
|
|
|
|
}; // namespace android
|
|
|
|
|