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