Camera.cpp revision 3ee3550a2f529cbf56d87d8503f59a8f45dccf32
1/* 2** 3** Copyright (C) 2008, The Android Open Source Project 4** 5** Licensed under the Apache License, Version 2.0 (the "License"); 6** you may not use this file except in compliance with the License. 7** You may obtain a copy of the License at 8** 9** http://www.apache.org/licenses/LICENSE-2.0 10** 11** Unless required by applicable law or agreed to in writing, software 12** distributed under the License is distributed on an "AS IS" BASIS, 13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14** See the License for the specific language governing permissions and 15** limitations under the License. 16*/ 17 18//#define LOG_NDEBUG 0 19#define LOG_TAG "Camera" 20#include <utils/Log.h> 21#include <utils/threads.h> 22#include <utils/String16.h> 23#include <binder/IPCThreadState.h> 24#include <binder/IServiceManager.h> 25#include <binder/IMemory.h> 26 27#include <camera/Camera.h> 28#include <camera/ICameraRecordingProxyListener.h> 29#include <camera/ICameraService.h> 30#include <camera/ICamera.h> 31 32#include <gui/IGraphicBufferProducer.h> 33#include <gui/Surface.h> 34 35namespace android { 36 37Camera::Camera(int cameraId) 38 : CameraBase(cameraId) 39{ 40} 41 42// construct a camera client from an existing camera remote 43sp<Camera> Camera::create(const sp<ICamera>& camera) 44{ 45 ALOGV("create"); 46 if (camera == 0) { 47 ALOGE("camera remote is a NULL pointer"); 48 return 0; 49 } 50 51 sp<Camera> c = new Camera(-1); 52 if (camera->connect(c) == NO_ERROR) { 53 c->mStatus = NO_ERROR; 54 c->mCamera = camera; 55 camera->asBinder()->linkToDeath(c); 56 return c; 57 } 58 return 0; 59} 60 61Camera::~Camera() 62{ 63 // We don't need to call disconnect() here because if the CameraService 64 // thinks we are the owner of the hardware, it will hold a (strong) 65 // reference to us, and we can't possibly be here. We also don't want to 66 // call disconnect() here if we are in the same process as mediaserver, 67 // because we may be invoked by CameraService::Client::connect() and will 68 // deadlock if we call any method of ICamera here. 69} 70 71sp<Camera> Camera::connect(int cameraId, const String16& clientPackageName, 72 int clientUid) 73{ 74 return CameraBaseT::connect(cameraId, clientPackageName, clientUid); 75} 76 77status_t Camera::reconnect() 78{ 79 ALOGV("reconnect"); 80 sp <ICamera> c = mCamera; 81 if (c == 0) return NO_INIT; 82 return c->connect(this); 83} 84 85status_t Camera::lock() 86{ 87 sp <ICamera> c = mCamera; 88 if (c == 0) return NO_INIT; 89 return c->lock(); 90} 91 92status_t Camera::unlock() 93{ 94 sp <ICamera> c = mCamera; 95 if (c == 0) return NO_INIT; 96 return c->unlock(); 97} 98 99// pass the buffered IGraphicBufferProducer to the camera service 100status_t Camera::setPreviewTexture(const sp<IGraphicBufferProducer>& bufferProducer) 101{ 102 ALOGV("setPreviewTexture(%p)", bufferProducer.get()); 103 sp <ICamera> c = mCamera; 104 if (c == 0) return NO_INIT; 105 ALOGD_IF(bufferProducer == 0, "app passed NULL surface"); 106 return c->setPreviewTexture(bufferProducer); 107} 108 109// start preview mode 110status_t Camera::startPreview() 111{ 112 ALOGV("startPreview"); 113 sp <ICamera> c = mCamera; 114 if (c == 0) return NO_INIT; 115 return c->startPreview(); 116} 117 118status_t Camera::storeMetaDataInBuffers(bool enabled) 119{ 120 ALOGV("storeMetaDataInBuffers: %s", 121 enabled? "true": "false"); 122 sp <ICamera> c = mCamera; 123 if (c == 0) return NO_INIT; 124 return c->storeMetaDataInBuffers(enabled); 125} 126 127// start recording mode, must call setPreviewDisplay first 128status_t Camera::startRecording() 129{ 130 ALOGV("startRecording"); 131 sp <ICamera> c = mCamera; 132 if (c == 0) return NO_INIT; 133 return c->startRecording(); 134} 135 136// stop preview mode 137void Camera::stopPreview() 138{ 139 ALOGV("stopPreview"); 140 sp <ICamera> c = mCamera; 141 if (c == 0) return; 142 c->stopPreview(); 143} 144 145// stop recording mode 146void Camera::stopRecording() 147{ 148 ALOGV("stopRecording"); 149 { 150 Mutex::Autolock _l(mLock); 151 mRecordingProxyListener.clear(); 152 } 153 sp <ICamera> c = mCamera; 154 if (c == 0) return; 155 c->stopRecording(); 156} 157 158// release a recording frame 159void Camera::releaseRecordingFrame(const sp<IMemory>& mem) 160{ 161 ALOGV("releaseRecordingFrame"); 162 sp <ICamera> c = mCamera; 163 if (c == 0) return; 164 c->releaseRecordingFrame(mem); 165} 166 167// get preview state 168bool Camera::previewEnabled() 169{ 170 ALOGV("previewEnabled"); 171 sp <ICamera> c = mCamera; 172 if (c == 0) return false; 173 return c->previewEnabled(); 174} 175 176// get recording state 177bool Camera::recordingEnabled() 178{ 179 ALOGV("recordingEnabled"); 180 sp <ICamera> c = mCamera; 181 if (c == 0) return false; 182 return c->recordingEnabled(); 183} 184 185status_t Camera::autoFocus() 186{ 187 ALOGV("autoFocus"); 188 sp <ICamera> c = mCamera; 189 if (c == 0) return NO_INIT; 190 return c->autoFocus(); 191} 192 193status_t Camera::cancelAutoFocus() 194{ 195 ALOGV("cancelAutoFocus"); 196 sp <ICamera> c = mCamera; 197 if (c == 0) return NO_INIT; 198 return c->cancelAutoFocus(); 199} 200 201// take a picture 202status_t Camera::takePicture(int msgType) 203{ 204 ALOGV("takePicture: 0x%x", msgType); 205 sp <ICamera> c = mCamera; 206 if (c == 0) return NO_INIT; 207 return c->takePicture(msgType); 208} 209 210// set preview/capture parameters - key/value pairs 211status_t Camera::setParameters(const String8& params) 212{ 213 ALOGV("setParameters"); 214 sp <ICamera> c = mCamera; 215 if (c == 0) return NO_INIT; 216 return c->setParameters(params); 217} 218 219// get preview/capture parameters - key/value pairs 220String8 Camera::getParameters() const 221{ 222 ALOGV("getParameters"); 223 String8 params; 224 sp <ICamera> c = mCamera; 225 if (c != 0) params = mCamera->getParameters(); 226 return params; 227} 228 229// send command to camera driver 230status_t Camera::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) 231{ 232 ALOGV("sendCommand"); 233 sp <ICamera> c = mCamera; 234 if (c == 0) return NO_INIT; 235 return c->sendCommand(cmd, arg1, arg2); 236} 237 238void Camera::setListener(const sp<CameraListener>& listener) 239{ 240 Mutex::Autolock _l(mLock); 241 mListener = listener; 242} 243 244void Camera::setRecordingProxyListener(const sp<ICameraRecordingProxyListener>& listener) 245{ 246 Mutex::Autolock _l(mLock); 247 mRecordingProxyListener = listener; 248} 249 250void Camera::setPreviewCallbackFlags(int flag) 251{ 252 ALOGV("setPreviewCallbackFlags"); 253 sp <ICamera> c = mCamera; 254 if (c == 0) return; 255 mCamera->setPreviewCallbackFlag(flag); 256} 257 258status_t Camera::setPreviewCallbackTarget( 259 const sp<IGraphicBufferProducer>& callbackProducer) 260{ 261 sp <ICamera> c = mCamera; 262 if (c == 0) return NO_INIT; 263 return c->setPreviewCallbackTarget(callbackProducer); 264} 265 266// callback from camera service 267void Camera::notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2) 268{ 269 return CameraBaseT::notifyCallback(msgType, ext1, ext2); 270} 271 272// callback from camera service when frame or image is ready 273void Camera::dataCallback(int32_t msgType, const sp<IMemory>& dataPtr, 274 camera_frame_metadata_t *metadata) 275{ 276 sp<CameraListener> listener; 277 { 278 Mutex::Autolock _l(mLock); 279 listener = mListener; 280 } 281 if (listener != NULL) { 282 listener->postData(msgType, dataPtr, metadata); 283 } 284} 285 286// callback from camera service when timestamped frame is ready 287void Camera::dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) 288{ 289 // If recording proxy listener is registered, forward the frame and return. 290 // The other listener (mListener) is ignored because the receiver needs to 291 // call releaseRecordingFrame. 292 sp<ICameraRecordingProxyListener> proxylistener; 293 { 294 Mutex::Autolock _l(mLock); 295 proxylistener = mRecordingProxyListener; 296 } 297 if (proxylistener != NULL) { 298 proxylistener->dataCallbackTimestamp(timestamp, msgType, dataPtr); 299 return; 300 } 301 302 sp<CameraListener> listener; 303 { 304 Mutex::Autolock _l(mLock); 305 listener = mListener; 306 } 307 308 if (listener != NULL) { 309 listener->postDataTimestamp(timestamp, msgType, dataPtr); 310 } else { 311 ALOGW("No listener was set. Drop a recording frame."); 312 releaseRecordingFrame(dataPtr); 313 } 314} 315 316sp<ICameraRecordingProxy> Camera::getRecordingProxy() { 317 ALOGV("getProxy"); 318 return new RecordingProxy(this); 319} 320 321status_t Camera::RecordingProxy::startRecording(const sp<ICameraRecordingProxyListener>& listener) 322{ 323 ALOGV("RecordingProxy::startRecording"); 324 mCamera->setRecordingProxyListener(listener); 325 mCamera->reconnect(); 326 return mCamera->startRecording(); 327} 328 329void Camera::RecordingProxy::stopRecording() 330{ 331 ALOGV("RecordingProxy::stopRecording"); 332 mCamera->stopRecording(); 333} 334 335void Camera::RecordingProxy::releaseRecordingFrame(const sp<IMemory>& mem) 336{ 337 ALOGV("RecordingProxy::releaseRecordingFrame"); 338 mCamera->releaseRecordingFrame(mem); 339} 340 341Camera::RecordingProxy::RecordingProxy(const sp<Camera>& camera) 342{ 343 mCamera = camera; 344} 345 346}; // namespace android 347