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_EMULATED_CAMERA_H
     18 #define HW_EMULATOR_CAMERA_EMULATED_CAMERA_H
     19 
     20 /*
     21  * Contains declaration of a class EmulatedCamera that encapsulates
     22  * functionality common to all version 1.0 emulated camera devices ("fake",
     23  * "webcam", "video file", etc.).  Instances of this class (for each emulated
     24  * camera) are created during the construction of the EmulatedCameraFactory
     25  * instance.  This class serves as an entry point for all camera API calls that
     26  * defined by camera_device_ops_t API.
     27  */
     28 
     29 #include <camera/CameraParameters.h>
     30 #include "EmulatedBaseCamera.h"
     31 #include "EmulatedCameraDevice.h"
     32 #include "PreviewWindow.h"
     33 #include "CallbackNotifier.h"
     34 
     35 namespace android {
     36 
     37 /* Encapsulates functionality common to all version 1.0 emulated camera devices
     38  * ("fake", "webcam", "file stream", etc.).
     39  *
     40  * Note that EmulatedCameraFactory instantiates object of this class just once,
     41  * when EmulatedCameraFactory instance gets constructed. Connection to /
     42  * disconnection from the actual camera device is handled by calls to
     43  * connectDevice(), and closeCamera() methods of this class that are ivoked in
     44  * response to hw_module_methods_t::open, and camera_device::close callbacks.
     45  */
     46 class EmulatedCamera : public camera_device, public EmulatedBaseCamera {
     47 public:
     48     /* Constructs EmulatedCamera instance.
     49      * Param:
     50      *  cameraId - Zero based camera identifier, which is an index of the camera
     51      *      instance in camera factory's array.
     52      *  module - Emulated camera HAL module descriptor.
     53      */
     54     EmulatedCamera(int cameraId,
     55                    struct hw_module_t* module);
     56 
     57     /* Destructs EmulatedCamera instance. */
     58     virtual ~EmulatedCamera();
     59 
     60     /****************************************************************************
     61      * Abstract API
     62      ***************************************************************************/
     63 
     64 public:
     65     /* Gets emulated camera device used by this instance of the emulated camera.
     66      */
     67     virtual EmulatedCameraDevice* getCameraDevice() = 0;
     68 
     69     /****************************************************************************
     70      * Public API
     71      ***************************************************************************/
     72 
     73 public:
     74     /** Override of base class method */
     75     virtual status_t Initialize();
     76 
     77     /* Next frame is available in the camera device.
     78      * This is a notification callback that is invoked by the camera device when
     79      * a new frame is available.
     80      * Note that most likely this method is called in context of a worker thread
     81      * that camera device has created for frame capturing.
     82      * Param:
     83      *  frame - Captured frame, or NULL if camera device didn't pull the frame
     84      *      yet. If NULL is passed in this parameter use GetCurrentFrame method
     85      *      of the camera device class to obtain the next frame. Also note that
     86      *      the size of the frame that is passed here (as well as the frame
     87      *      returned from the GetCurrentFrame method) is defined by the current
     88      *      frame settings (width + height + pixel format) for the camera device.
     89      * timestamp - Frame's timestamp.
     90      * camera_dev - Camera device instance that delivered the frame.
     91      */
     92     virtual void onNextFrameAvailable(const void* frame,
     93                                       nsecs_t timestamp,
     94                                       EmulatedCameraDevice* camera_dev);
     95 
     96     /* Entry point for notifications that occur in camera device.
     97      * Param:
     98      *  err - CAMERA_ERROR_XXX error code.
     99      */
    100     virtual void onCameraDeviceError(int err);
    101 
    102     /****************************************************************************
    103      * Camera API implementation
    104      ***************************************************************************/
    105 
    106 public:
    107     /** Override of base class method */
    108     virtual status_t connectCamera(hw_device_t** device);
    109 
    110     /** Override of base class method */
    111     virtual status_t closeCamera();
    112 
    113     /** Override of base class method */
    114     virtual status_t getCameraInfo(struct camera_info* info);
    115 
    116     /****************************************************************************
    117      * Camera API implementation.
    118      * These methods are called from the camera API callback routines.
    119      ***************************************************************************/
    120 
    121 protected:
    122     /* Actual handler for camera_device_ops_t::set_preview_window callback.
    123      * NOTE: When this method is called the object is locked.
    124      * Note that failures in this method are reported as negave EXXX statuses.
    125      */
    126     virtual status_t setPreviewWindow(struct preview_stream_ops *window);
    127 
    128     /* Actual handler for camera_device_ops_t::set_callbacks callback.
    129      * NOTE: When this method is called the object is locked.
    130      */
    131     virtual void setCallbacks(camera_notify_callback notify_cb,
    132                               camera_data_callback data_cb,
    133                               camera_data_timestamp_callback data_cb_timestamp,
    134                               camera_request_memory get_memory,
    135                               void* user);
    136 
    137     /* Actual handler for camera_device_ops_t::enable_msg_type callback.
    138      * NOTE: When this method is called the object is locked.
    139      */
    140     virtual void enableMsgType(int32_t msg_type);
    141 
    142     /* Actual handler for camera_device_ops_t::disable_msg_type callback.
    143      * NOTE: When this method is called the object is locked.
    144      */
    145     virtual void disableMsgType(int32_t msg_type);
    146 
    147     /* Actual handler for camera_device_ops_t::msg_type_enabled callback.
    148      * NOTE: When this method is called the object is locked.
    149      * Return:
    150      *  0 if message(s) is (are) disabled, != 0 if enabled.
    151      */
    152     virtual int isMsgTypeEnabled(int32_t msg_type);
    153 
    154     /* Actual handler for camera_device_ops_t::start_preview callback.
    155      * NOTE: When this method is called the object is locked.
    156      * Note that failures in this method are reported as negave EXXX statuses.
    157      */
    158     virtual status_t startPreview();
    159 
    160     /* Actual handler for camera_device_ops_t::stop_preview callback.
    161      * NOTE: When this method is called the object is locked.
    162      */
    163     virtual void stopPreview();
    164 
    165     /* Actual handler for camera_device_ops_t::preview_enabled callback.
    166      * NOTE: When this method is called the object is locked.
    167      * Return:
    168      *  0 if preview is disabled, != 0 if enabled.
    169      */
    170     virtual int isPreviewEnabled();
    171 
    172     /* Actual handler for camera_device_ops_t::store_meta_data_in_buffers callback.
    173      * NOTE: When this method is called the object is locked.
    174      * Note that failures in this method are reported as negave EXXX statuses.
    175      */
    176     virtual status_t storeMetaDataInBuffers(int enable);
    177 
    178     /* Actual handler for camera_device_ops_t::start_recording callback.
    179      * NOTE: When this method is called the object is locked.
    180      * Note that failures in this method are reported as negave EXXX statuses.
    181      */
    182     virtual status_t startRecording();
    183 
    184     /* Actual handler for camera_device_ops_t::stop_recording callback.
    185      * NOTE: When this method is called the object is locked.
    186      */
    187     virtual void stopRecording();
    188 
    189     /* Actual handler for camera_device_ops_t::recording_enabled callback.
    190      * NOTE: When this method is called the object is locked.
    191      * Return:
    192      *  0 if recording is disabled, != 0 if enabled.
    193      */
    194     virtual int isRecordingEnabled();
    195 
    196     /* Actual handler for camera_device_ops_t::release_recording_frame callback.
    197      * NOTE: When this method is called the object is locked.
    198      */
    199     virtual void releaseRecordingFrame(const void* opaque);
    200 
    201     /* Actual handler for camera_device_ops_t::auto_focus callback.
    202      * NOTE: When this method is called the object is locked.
    203      * Note that failures in this method are reported as negave EXXX statuses.
    204      */
    205     virtual status_t setAutoFocus();
    206 
    207     /* Actual handler for camera_device_ops_t::cancel_auto_focus callback.
    208      * NOTE: When this method is called the object is locked.
    209      * Note that failures in this method are reported as negave EXXX statuses.
    210      */
    211     virtual status_t cancelAutoFocus();
    212 
    213     /* Actual handler for camera_device_ops_t::take_picture callback.
    214      * NOTE: When this method is called the object is locked.
    215      * Note that failures in this method are reported as negave EXXX statuses.
    216      */
    217     virtual status_t takePicture();
    218 
    219     /* Actual handler for camera_device_ops_t::cancel_picture callback.
    220      * NOTE: When this method is called the object is locked.
    221      * Note that failures in this method are reported as negave EXXX statuses.
    222      */
    223     virtual status_t cancelPicture();
    224 
    225     /* Actual handler for camera_device_ops_t::set_parameters callback.
    226      * NOTE: When this method is called the object is locked.
    227      * Note that failures in this method are reported as negave EXXX statuses.
    228      */
    229     virtual status_t setParameters(const char* parms);
    230 
    231     /* Actual handler for camera_device_ops_t::get_parameters callback.
    232      * NOTE: When this method is called the object is locked.
    233      * Return:
    234      *  Flattened parameters string. The caller will free the buffer allocated
    235      *  for the string by calling camera_device_ops_t::put_parameters callback.
    236      */
    237     virtual char* getParameters();
    238 
    239     /* Actual handler for camera_device_ops_t::put_parameters callback.
    240      * Called to free the string returned from camera_device_ops_t::get_parameters
    241      * callback. There is nothing more to it: the name of the callback is just
    242      * misleading.
    243      * NOTE: When this method is called the object is locked.
    244      */
    245     virtual void putParameters(char* params);
    246 
    247     /* Actual handler for camera_device_ops_t::send_command callback.
    248      * NOTE: When this method is called the object is locked.
    249      * Note that failures in this method are reported as negave EXXX statuses.
    250      */
    251     virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
    252 
    253     /* Actual handler for camera_device_ops_t::release callback.
    254      * NOTE: When this method is called the object is locked.
    255      */
    256     virtual void releaseCamera();
    257 
    258     /* Actual handler for camera_device_ops_t::dump callback.
    259      * NOTE: When this method is called the object is locked.
    260      * Note that failures in this method are reported as negave EXXX statuses.
    261      */
    262     virtual status_t dumpCamera(int fd);
    263 
    264     /****************************************************************************
    265      * Preview management.
    266      ***************************************************************************/
    267 
    268 protected:
    269     /* Starts preview.
    270      * Note that when this method is called mPreviewWindow may be NULL,
    271      * indicating that framework has an intention to start displaying video
    272      * frames, but didn't create the preview window yet.
    273      * Return:
    274      *  NO_ERROR on success, or an appropriate error status on failure.
    275      */
    276     virtual status_t doStartPreview();
    277 
    278     /* Stops preview.
    279      * This method reverts DoStartPreview.
    280      * Return:
    281      *  NO_ERROR on success, or an appropriate error status on failure.
    282      */
    283     virtual status_t doStopPreview();
    284 
    285     /****************************************************************************
    286      * Private API.
    287      ***************************************************************************/
    288 
    289 protected:
    290     /* Cleans up camera when released. */
    291     virtual status_t cleanupCamera();
    292 
    293     /****************************************************************************
    294      * Camera API callbacks as defined by camera_device_ops structure.
    295      * See hardware/libhardware/include/hardware/camera.h for information on
    296      * each of these callbacks. Implemented in this class, these callbacks simply
    297      * dispatch the call into an instance of EmulatedCamera class defined by the
    298      * 'camera_device' parameter.
    299      ***************************************************************************/
    300 
    301 private:
    302     static int set_preview_window(struct camera_device* dev,
    303                                    struct preview_stream_ops* window);
    304 
    305     static void set_callbacks(struct camera_device* dev,
    306                               camera_notify_callback notify_cb,
    307                               camera_data_callback data_cb,
    308                               camera_data_timestamp_callback data_cb_timestamp,
    309                               camera_request_memory get_memory,
    310                               void* user);
    311 
    312     static void enable_msg_type(struct camera_device* dev, int32_t msg_type);
    313 
    314     static void disable_msg_type(struct camera_device* dev, int32_t msg_type);
    315 
    316     static int msg_type_enabled(struct camera_device* dev, int32_t msg_type);
    317 
    318     static int start_preview(struct camera_device* dev);
    319 
    320     static void stop_preview(struct camera_device* dev);
    321 
    322     static int preview_enabled(struct camera_device* dev);
    323 
    324     static int store_meta_data_in_buffers(struct camera_device* dev, int enable);
    325 
    326     static int start_recording(struct camera_device* dev);
    327 
    328     static void stop_recording(struct camera_device* dev);
    329 
    330     static int recording_enabled(struct camera_device* dev);
    331 
    332     static void release_recording_frame(struct camera_device* dev,
    333                                         const void* opaque);
    334 
    335     static int auto_focus(struct camera_device* dev);
    336 
    337     static int cancel_auto_focus(struct camera_device* dev);
    338 
    339     static int take_picture(struct camera_device* dev);
    340 
    341     static int cancel_picture(struct camera_device* dev);
    342 
    343     static int set_parameters(struct camera_device* dev, const char* parms);
    344 
    345     static char* get_parameters(struct camera_device* dev);
    346 
    347     static void put_parameters(struct camera_device* dev, char* params);
    348 
    349     static int send_command(struct camera_device* dev,
    350                             int32_t cmd,
    351                             int32_t arg1,
    352                             int32_t arg2);
    353 
    354     static void release(struct camera_device* dev);
    355 
    356     static int dump(struct camera_device* dev, int fd);
    357 
    358     static int close(struct hw_device_t* device);
    359 
    360     /****************************************************************************
    361      * Data members
    362      ***************************************************************************/
    363 
    364 protected:
    365     /* Locks this instance for parameters, state, etc. change. */
    366     Mutex                           mObjectLock;
    367 
    368     /* Camera parameters. */
    369     CameraParameters                mParameters;
    370 
    371     /* Preview window. */
    372     PreviewWindow                   mPreviewWindow;
    373 
    374     /* Callback notifier. */
    375     CallbackNotifier                mCallbackNotifier;
    376 
    377 private:
    378     /* Registered callbacks implementing camera API. */
    379     static camera_device_ops_t      mDeviceOps;
    380 
    381     /****************************************************************************
    382      * Common keys
    383      ***************************************************************************/
    384 
    385 public:
    386     static const char FACING_KEY[];
    387     static const char ORIENTATION_KEY[];
    388     static const char RECORDING_HINT_KEY[];
    389 
    390      /****************************************************************************
    391      * Common string values
    392      ***************************************************************************/
    393 
    394     /* Possible values for FACING_KEY */
    395     static const char FACING_BACK[];
    396     static const char FACING_FRONT[];
    397 };
    398 
    399 }; /* namespace android */
    400 
    401 #endif  /* HW_EMULATOR_CAMERA_EMULATED_CAMERA_H */
    402