Home | History | Annotate | Download | only in camera
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef HW_EMULATOR_CAMERA_CALLBACK_NOTIFIER_H
     18 #define HW_EMULATOR_CAMERA_CALLBACK_NOTIFIER_H
     19 
     20 /*
     21  * Contains declaration of a class CallbackNotifier that manages callbacks set
     22  * via set_callbacks, enable_msg_type, and disable_msg_type camera HAL API.
     23  */
     24 
     25 #include <utils/List.h>
     26 #include <camera/CameraParameters.h>
     27 
     28 namespace android {
     29 
     30 class EmulatedCameraDevice;
     31 class FrameProducer;
     32 
     33 /* Manages callbacks set via set_callbacks, enable_msg_type, and disable_msg_type
     34  * camera HAL API.
     35  *
     36  * Objects of this class are contained in EmulatedCamera objects, and handle
     37  * relevant camera API callbacks.
     38  * Locking considerations. Apparently, it's not allowed to call callbacks
     39  * registered in this class, while holding a lock: recursion is quite possible,
     40  * which will cause a deadlock.
     41  */
     42 class CallbackNotifier {
     43 public:
     44     /* Constructs CallbackNotifier instance. */
     45     CallbackNotifier();
     46 
     47     /* Destructs CallbackNotifier instance. */
     48     ~CallbackNotifier();
     49 
     50     /****************************************************************************
     51      * Camera API
     52      ***************************************************************************/
     53 
     54 public:
     55     /* Actual handler for camera_device_ops_t::set_callbacks callback.
     56      * This method is called by the containing emulated camera object when it is
     57      * handing the camera_device_ops_t::set_callbacks callback.
     58      */
     59     void setCallbacks(camera_notify_callback notify_cb,
     60                       camera_data_callback data_cb,
     61                       camera_data_timestamp_callback data_cb_timestamp,
     62                       camera_request_memory get_memory,
     63                       void* user);
     64 
     65     /* Actual handler for camera_device_ops_t::enable_msg_type callback.
     66      * This method is called by the containing emulated camera object when it is
     67      * handing the camera_device_ops_t::enable_msg_type callback.
     68      */
     69     void enableMessage(uint msg_type);
     70 
     71     /* Actual handler for camera_device_ops_t::disable_msg_type callback.
     72      * This method is called by the containing emulated camera object when it is
     73      * handing the camera_device_ops_t::disable_msg_type callback.
     74      */
     75     void disableMessage(uint msg_type);
     76 
     77     /* Actual handler for camera_device_ops_t::store_meta_data_in_buffers
     78      * callback. This method is called by the containing emulated camera object
     79      * when it is handing the camera_device_ops_t::store_meta_data_in_buffers
     80      * callback.
     81      * Return:
     82      *  NO_ERROR on success, or an appropriate error status.
     83      */
     84     status_t storeMetaDataInBuffers(bool enable);
     85 
     86     /* Enables video recording.
     87      * This method is called by the containing emulated camera object when it is
     88      * handing the camera_device_ops_t::start_recording callback.
     89      * Param:
     90      *  fps - Video frame frequency. This parameter determins when a frame
     91      *      received via onNextFrameAvailable call will be pushed through the
     92      *      callback.
     93      * Return:
     94      *  NO_ERROR on success, or an appropriate error status.
     95      */
     96     status_t enableVideoRecording(int fps);
     97 
     98     /* Disables video recording.
     99      * This method is called by the containing emulated camera object when it is
    100      * handing the camera_device_ops_t::stop_recording callback.
    101      */
    102     void disableVideoRecording();
    103 
    104     /* Releases video frame, sent to the framework.
    105      * This method is called by the containing emulated camera object when it is
    106      * handing the camera_device_ops_t::release_recording_frame callback.
    107      */
    108     void releaseRecordingFrame(const void* opaque);
    109 
    110     /* Send a message to the notify callback that auto-focus has completed.
    111      * This method is called from the containing emulated camera object when it
    112      * has received confirmation from the camera device that auto-focusing is
    113      * completed.
    114      */
    115     void autoFocusComplete();
    116 
    117     /* Actual handler for camera_device_ops_t::msg_type_enabled callback.
    118      * This method is called by the containing emulated camera object when it is
    119      * handing the camera_device_ops_t::msg_type_enabled callback.
    120      * Note: this method doesn't grab a lock while checking message status, since
    121      * upon exit the status would be undefined anyway. So, grab a lock before
    122      * calling this method if you care about persisting a defined message status.
    123      * Return:
    124      *  0 if message is disabled, or non-zero value, if message is enabled.
    125      */
    126     inline int isMessageEnabled(uint msg_type)
    127     {
    128         return mMessageEnabler & msg_type;
    129     }
    130 
    131     /* Checks id video recording is enabled.
    132      * This method is called by the containing emulated camera object when it is
    133      * handing the camera_device_ops_t::recording_enabled callback.
    134      * Note: this method doesn't grab a lock while checking video recordin status,
    135      * since upon exit the status would be undefined anyway. So, grab a lock
    136      * before calling this method if you care about persisting of a defined video
    137      * recording status.
    138      * Return:
    139      *  true if video recording is enabled, or false if it is disabled.
    140      */
    141     inline bool isVideoRecordingEnabled() const
    142     {
    143         return mVideoRecEnabled;
    144     }
    145 
    146     /****************************************************************************
    147      * Public API
    148      ***************************************************************************/
    149 
    150 public:
    151     /* Resets the callback notifier. */
    152     void cleanupCBNotifier();
    153 
    154     /* Next frame is available in the camera device.
    155      * This is a notification callback that is invoked by the camera device when
    156      * a new frame is available. The captured frame is available through the
    157      * |camera_dev| obejct.
    158      * Note that most likely this method is called in context of a worker thread
    159      * that camera device has created for frame capturing.
    160      * Param:
    161      * timestamp - Frame's timestamp.
    162      * camera_dev - Camera device instance that delivered the frame.
    163      */
    164     void onNextFrameAvailable(nsecs_t timestamp,
    165                               EmulatedCameraDevice* camera_dev);
    166 
    167     /* Entry point for notifications that occur in camera device.
    168      * Param:
    169      *  err - CAMERA_ERROR_XXX error code.
    170      */
    171     void onCameraDeviceError(int err);
    172 
    173     /* Sets, or resets taking picture state.
    174      * This state control whether or not to notify the framework about compressed
    175      * image, shutter, and other picture related events.
    176      */
    177     void setTakingPicture(bool taking)
    178     {
    179         mTakingPicture = taking;
    180     }
    181 
    182     /* Sets JPEG quality used to compress frame during picture taking. */
    183     void setJpegQuality(int jpeg_quality)
    184     {
    185         mJpegQuality = jpeg_quality;
    186     }
    187 
    188     /* Sets the camera parameters that will be used to populate exif data in the
    189      * picture.
    190      */
    191     void setCameraParameters(CameraParameters cameraParameters)
    192     {
    193         mCameraParameters = cameraParameters;
    194     }
    195 
    196     /****************************************************************************
    197      * Private API
    198      ***************************************************************************/
    199 
    200 protected:
    201     /* Checks if it's time to push new video frame.
    202      * Note that this method must be called while object is locked.
    203      * Param:
    204      *  timestamp - Timestamp for the new frame. */
    205     bool isNewVideoFrameTime(nsecs_t timestamp);
    206 
    207     /****************************************************************************
    208      * Data members
    209      ***************************************************************************/
    210 
    211 protected:
    212     /* Locks this instance for data change. */
    213     Mutex                           mObjectLock;
    214 
    215     /*
    216      * Callbacks, registered in set_callbacks.
    217      */
    218 
    219     camera_notify_callback          mNotifyCB;
    220     camera_data_callback            mDataCB;
    221     camera_data_timestamp_callback  mDataCBTimestamp;
    222     camera_request_memory           mGetMemoryCB;
    223     void*                           mCBOpaque;
    224 
    225     /* video frame queue for the CameraHeapMemory destruction */
    226     List<camera_memory_t*>          mCameraMemoryTs;
    227 
    228     /* Timestamp when last frame has been delivered to the framework. */
    229     nsecs_t                         mLastFrameTimestamp;
    230 
    231     /* Video frequency in nanosec. */
    232     nsecs_t                         mFrameRefreshFreq;
    233 
    234     /* Message enabler. */
    235     uint32_t                        mMessageEnabler;
    236 
    237     /* JPEG quality used to compress frame during picture taking. */
    238     int                             mJpegQuality;
    239 
    240     /* Camera parameters used for EXIF data in picture */
    241     CameraParameters                mCameraParameters;
    242 
    243     /* Video recording status. */
    244     bool                            mVideoRecEnabled;
    245 
    246     /* Picture taking status. */
    247     bool                            mTakingPicture;
    248 };
    249 
    250 }; /* namespace android */
    251 
    252 #endif  /* HW_EMULATOR_CAMERA_CALLBACK_NOTIFIER_H */
    253