Home | History | Annotate | Download | only in camera
      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 
     35 namespace android {
     36 
     37 Camera::Camera(int cameraId)
     38     : CameraBase(cameraId)
     39 {
     40 }
     41 
     42 CameraTraits<Camera>::TCamConnectService CameraTraits<Camera>::fnConnectService =
     43         &ICameraService::connect;
     44 
     45 // construct a camera client from an existing camera remote
     46 sp<Camera> Camera::create(const sp<ICamera>& camera)
     47 {
     48      ALOGV("create");
     49      if (camera == 0) {
     50          ALOGE("camera remote is a NULL pointer");
     51          return 0;
     52      }
     53 
     54     sp<Camera> c = new Camera(-1);
     55     if (camera->connect(c) == NO_ERROR) {
     56         c->mStatus = NO_ERROR;
     57         c->mCamera = camera;
     58         IInterface::asBinder(camera)->linkToDeath(c);
     59         return c;
     60     }
     61     return 0;
     62 }
     63 
     64 Camera::~Camera()
     65 {
     66     // We don't need to call disconnect() here because if the CameraService
     67     // thinks we are the owner of the hardware, it will hold a (strong)
     68     // reference to us, and we can't possibly be here. We also don't want to
     69     // call disconnect() here if we are in the same process as mediaserver,
     70     // because we may be invoked by CameraService::Client::connect() and will
     71     // deadlock if we call any method of ICamera here.
     72 }
     73 
     74 sp<Camera> Camera::connect(int cameraId, const String16& clientPackageName,
     75         int clientUid)
     76 {
     77     return CameraBaseT::connect(cameraId, clientPackageName, clientUid);
     78 }
     79 
     80 status_t Camera::connectLegacy(int cameraId, int halVersion,
     81         const String16& clientPackageName,
     82         int clientUid,
     83         sp<Camera>& camera)
     84 {
     85     ALOGV("%s: connect legacy camera device", __FUNCTION__);
     86     sp<Camera> c = new Camera(cameraId);
     87     sp<ICameraClient> cl = c;
     88     status_t status = NO_ERROR;
     89     const sp<ICameraService>& cs = CameraBaseT::getCameraService();
     90 
     91     if (cs != 0) {
     92         status = cs.get()->connectLegacy(cl, cameraId, halVersion, clientPackageName,
     93                                         clientUid, /*out*/c->mCamera);
     94     }
     95     if (status == OK && c->mCamera != 0) {
     96         IInterface::asBinder(c->mCamera)->linkToDeath(c);
     97         c->mStatus = NO_ERROR;
     98         camera = c;
     99     } else {
    100         ALOGW("An error occurred while connecting to camera %d: %d (%s)",
    101                 cameraId, status, strerror(-status));
    102         c.clear();
    103     }
    104     return status;
    105 }
    106 
    107 status_t Camera::reconnect()
    108 {
    109     ALOGV("reconnect");
    110     sp <ICamera> c = mCamera;
    111     if (c == 0) return NO_INIT;
    112     return c->connect(this);
    113 }
    114 
    115 status_t Camera::lock()
    116 {
    117     sp <ICamera> c = mCamera;
    118     if (c == 0) return NO_INIT;
    119     return c->lock();
    120 }
    121 
    122 status_t Camera::unlock()
    123 {
    124     sp <ICamera> c = mCamera;
    125     if (c == 0) return NO_INIT;
    126     return c->unlock();
    127 }
    128 
    129 // pass the buffered IGraphicBufferProducer to the camera service
    130 status_t Camera::setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer)
    131 {
    132     ALOGV("setPreviewTarget(%p)", bufferProducer.get());
    133     sp <ICamera> c = mCamera;
    134     if (c == 0) return NO_INIT;
    135     ALOGD_IF(bufferProducer == 0, "app passed NULL surface");
    136     return c->setPreviewTarget(bufferProducer);
    137 }
    138 
    139 // start preview mode
    140 status_t Camera::startPreview()
    141 {
    142     ALOGV("startPreview");
    143     sp <ICamera> c = mCamera;
    144     if (c == 0) return NO_INIT;
    145     return c->startPreview();
    146 }
    147 
    148 status_t Camera::storeMetaDataInBuffers(bool enabled)
    149 {
    150     ALOGV("storeMetaDataInBuffers: %s",
    151             enabled? "true": "false");
    152     sp <ICamera> c = mCamera;
    153     if (c == 0) return NO_INIT;
    154     return c->storeMetaDataInBuffers(enabled);
    155 }
    156 
    157 // start recording mode, must call setPreviewTarget first
    158 status_t Camera::startRecording()
    159 {
    160     ALOGV("startRecording");
    161     sp <ICamera> c = mCamera;
    162     if (c == 0) return NO_INIT;
    163     return c->startRecording();
    164 }
    165 
    166 // stop preview mode
    167 void Camera::stopPreview()
    168 {
    169     ALOGV("stopPreview");
    170     sp <ICamera> c = mCamera;
    171     if (c == 0) return;
    172     c->stopPreview();
    173 }
    174 
    175 // stop recording mode
    176 void Camera::stopRecording()
    177 {
    178     ALOGV("stopRecording");
    179     {
    180         Mutex::Autolock _l(mLock);
    181         mRecordingProxyListener.clear();
    182     }
    183     sp <ICamera> c = mCamera;
    184     if (c == 0) return;
    185     c->stopRecording();
    186 }
    187 
    188 // release a recording frame
    189 void Camera::releaseRecordingFrame(const sp<IMemory>& mem)
    190 {
    191     ALOGV("releaseRecordingFrame");
    192     sp <ICamera> c = mCamera;
    193     if (c == 0) return;
    194     c->releaseRecordingFrame(mem);
    195 }
    196 
    197 // get preview state
    198 bool Camera::previewEnabled()
    199 {
    200     ALOGV("previewEnabled");
    201     sp <ICamera> c = mCamera;
    202     if (c == 0) return false;
    203     return c->previewEnabled();
    204 }
    205 
    206 // get recording state
    207 bool Camera::recordingEnabled()
    208 {
    209     ALOGV("recordingEnabled");
    210     sp <ICamera> c = mCamera;
    211     if (c == 0) return false;
    212     return c->recordingEnabled();
    213 }
    214 
    215 status_t Camera::autoFocus()
    216 {
    217     ALOGV("autoFocus");
    218     sp <ICamera> c = mCamera;
    219     if (c == 0) return NO_INIT;
    220     return c->autoFocus();
    221 }
    222 
    223 status_t Camera::cancelAutoFocus()
    224 {
    225     ALOGV("cancelAutoFocus");
    226     sp <ICamera> c = mCamera;
    227     if (c == 0) return NO_INIT;
    228     return c->cancelAutoFocus();
    229 }
    230 
    231 // take a picture
    232 status_t Camera::takePicture(int msgType)
    233 {
    234     ALOGV("takePicture: 0x%x", msgType);
    235     sp <ICamera> c = mCamera;
    236     if (c == 0) return NO_INIT;
    237     return c->takePicture(msgType);
    238 }
    239 
    240 // set preview/capture parameters - key/value pairs
    241 status_t Camera::setParameters(const String8& params)
    242 {
    243     ALOGV("setParameters");
    244     sp <ICamera> c = mCamera;
    245     if (c == 0) return NO_INIT;
    246     return c->setParameters(params);
    247 }
    248 
    249 // get preview/capture parameters - key/value pairs
    250 String8 Camera::getParameters() const
    251 {
    252     ALOGV("getParameters");
    253     String8 params;
    254     sp <ICamera> c = mCamera;
    255     if (c != 0) params = mCamera->getParameters();
    256     return params;
    257 }
    258 
    259 // send command to camera driver
    260 status_t Camera::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
    261 {
    262     ALOGV("sendCommand");
    263     sp <ICamera> c = mCamera;
    264     if (c == 0) return NO_INIT;
    265     return c->sendCommand(cmd, arg1, arg2);
    266 }
    267 
    268 void Camera::setListener(const sp<CameraListener>& listener)
    269 {
    270     Mutex::Autolock _l(mLock);
    271     mListener = listener;
    272 }
    273 
    274 void Camera::setRecordingProxyListener(const sp<ICameraRecordingProxyListener>& listener)
    275 {
    276     Mutex::Autolock _l(mLock);
    277     mRecordingProxyListener = listener;
    278 }
    279 
    280 void Camera::setPreviewCallbackFlags(int flag)
    281 {
    282     ALOGV("setPreviewCallbackFlags");
    283     sp <ICamera> c = mCamera;
    284     if (c == 0) return;
    285     mCamera->setPreviewCallbackFlag(flag);
    286 }
    287 
    288 status_t Camera::setPreviewCallbackTarget(
    289         const sp<IGraphicBufferProducer>& callbackProducer)
    290 {
    291     sp <ICamera> c = mCamera;
    292     if (c == 0) return NO_INIT;
    293     return c->setPreviewCallbackTarget(callbackProducer);
    294 }
    295 
    296 // callback from camera service
    297 void Camera::notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2)
    298 {
    299     return CameraBaseT::notifyCallback(msgType, ext1, ext2);
    300 }
    301 
    302 // callback from camera service when frame or image is ready
    303 void Camera::dataCallback(int32_t msgType, const sp<IMemory>& dataPtr,
    304                           camera_frame_metadata_t *metadata)
    305 {
    306     sp<CameraListener> listener;
    307     {
    308         Mutex::Autolock _l(mLock);
    309         listener = mListener;
    310     }
    311     if (listener != NULL) {
    312         listener->postData(msgType, dataPtr, metadata);
    313     }
    314 }
    315 
    316 // callback from camera service when timestamped frame is ready
    317 void Camera::dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr)
    318 {
    319     // If recording proxy listener is registered, forward the frame and return.
    320     // The other listener (mListener) is ignored because the receiver needs to
    321     // call releaseRecordingFrame.
    322     sp<ICameraRecordingProxyListener> proxylistener;
    323     {
    324         Mutex::Autolock _l(mLock);
    325         proxylistener = mRecordingProxyListener;
    326     }
    327     if (proxylistener != NULL) {
    328         proxylistener->dataCallbackTimestamp(timestamp, msgType, dataPtr);
    329         return;
    330     }
    331 
    332     sp<CameraListener> listener;
    333     {
    334         Mutex::Autolock _l(mLock);
    335         listener = mListener;
    336     }
    337 
    338     if (listener != NULL) {
    339         listener->postDataTimestamp(timestamp, msgType, dataPtr);
    340     } else {
    341         ALOGW("No listener was set. Drop a recording frame.");
    342         releaseRecordingFrame(dataPtr);
    343     }
    344 }
    345 
    346 sp<ICameraRecordingProxy> Camera::getRecordingProxy() {
    347     ALOGV("getProxy");
    348     return new RecordingProxy(this);
    349 }
    350 
    351 status_t Camera::RecordingProxy::startRecording(const sp<ICameraRecordingProxyListener>& listener)
    352 {
    353     ALOGV("RecordingProxy::startRecording");
    354     mCamera->setRecordingProxyListener(listener);
    355     mCamera->reconnect();
    356     return mCamera->startRecording();
    357 }
    358 
    359 void Camera::RecordingProxy::stopRecording()
    360 {
    361     ALOGV("RecordingProxy::stopRecording");
    362     mCamera->stopRecording();
    363 }
    364 
    365 void Camera::RecordingProxy::releaseRecordingFrame(const sp<IMemory>& mem)
    366 {
    367     ALOGV("RecordingProxy::releaseRecordingFrame");
    368     mCamera->releaseRecordingFrame(mem);
    369 }
    370 
    371 Camera::RecordingProxy::RecordingProxy(const sp<Camera>& camera)
    372 {
    373     mCamera = camera;
    374 }
    375 
    376 }; // namespace android
    377