Home | History | Annotate | Download | only in usbcamera
      1 /*
      2  * Copyright (C) 2015 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 CAMERA_H_
     18 #define CAMERA_H_
     19 
     20 #include <hardware/hardware.h>
     21 #include <hardware/camera3.h>
     22 #include <utils/Mutex.h>
     23 #include <utils/Vector.h>
     24 #include "Metadata.h"
     25 #include <sync/sync.h>
     26 #include "Stream.h"
     27 
     28 #define CAMERA_SYNC_TIMEOUT_MS 5000
     29 
     30 namespace usb_camera_hal {
     31 // Camera represents a physical camera on a device.
     32 // This is constructed when the HAL module is loaded, one per physical camera.
     33 // It is opened by the framework, and must be closed before it can be opened
     34 // again.
     35 // This is an abstract class, containing all logic and data shared between all
     36 // camera devices.
     37 class Camera {
     38     public:
     39         // id is used to distinguish cameras. 0 <= id < NUM_CAMERAS.
     40         // module is a handle to the HAL module, used when the device is opened.
     41         explicit Camera(int id);
     42         virtual ~Camera();
     43 
     44         // Common Camera Device Operations (see <hardware/camera_common.h>)
     45         int open(const hw_module_t *module, hw_device_t **device);
     46         int getInfo(struct camera_info *info);
     47         int close();
     48 
     49         // Camera v3 Device Operations (see <hardware/camera3.h>)
     50         int initialize(const camera3_callback_ops_t *callback_ops);
     51         int configureStreams(camera3_stream_configuration_t *stream_list);
     52         const camera_metadata_t *constructDefaultRequestSettings(int type);
     53         int processCaptureRequest(camera3_capture_request_t *request);
     54         int flush();
     55         void dump(int fd);
     56 
     57         // Update static camera characteristics. This method could be called by
     58         // HAL hotplug thread when camera is plugged.
     59         void updateInfo();
     60 
     61     protected:
     62         // Initialize static camera characteristics.
     63         virtual int initStaticInfo() = 0;
     64         // Verify settings are valid for a capture
     65         virtual bool isValidCaptureSettings(const camera_metadata_t *) = 0;
     66         // Separate open method for individual devices
     67         virtual int openDevice() = 0;
     68         // Separate initialization method for individual devices when opened
     69         virtual int initDevice() = 0;
     70         // Flush camera pipeline for each individual device
     71         virtual int flushDevice() = 0;
     72         // Separate close method for individual devices
     73         virtual int closeDevice() = 0;
     74         // Capture and file an output buffer for an input buffer.
     75         virtual int processCaptureBuffer(const camera3_stream_buffer_t *in,
     76                 camera3_stream_buffer_t *out) = 0;
     77         // Accessor method used by initDevice() to set the templates' metadata
     78         int setTemplate(int type, camera_metadata_t *settings);
     79         // Prettyprint template names
     80         const char* templateToString(int type);
     81         // Process an output buffer
     82 
     83         // Identifier used by framework to distinguish cameras
     84         const int mId;
     85         // Metadata containing persistent camera characteristics
     86         Metadata mMetadata;
     87         // camera_metadata structure containing static characteristics
     88         camera_metadata_t *mStaticInfo;
     89 
     90     private:
     91         // Camera device handle returned to framework for use
     92         camera3_device_t mDevice;
     93         // Reuse a stream already created by this device. Must be called with mDeviceLock held.
     94         Stream *reuseStreamLocked(camera3_stream_t *astream);
     95         // Destroy all streams in a stream array, and the array itself. Must be called with
     96         // mDeviceLock held.
     97         void destroyStreamsLocked(android::Vector<Stream *> &streams);
     98         // Verify a set of streams is valid in aggregate. Must be called with mDeviceLock held.
     99         bool isValidStreamSetLocked(const android::Vector<Stream *> &streams);
    100         // Calculate usage and max_bufs of each stream. Must be called with mDeviceLock held.
    101         void setupStreamsLocked(android::Vector<Stream *> &streams);
    102         // Update new settings for re-use and clean up old settings. Must be called with
    103         // mDeviceLock held.
    104         void updateSettingsLocked(const camera_metadata_t *new_settings);
    105         // Send a shutter notify message with start of exposure time
    106         void notifyShutter(uint32_t frame_number, uint64_t timestamp);
    107         // Is type a valid template type (and valid index into mTemplates)
    108         bool isValidTemplateType(int type);
    109 
    110         // Busy flag indicates camera is in use
    111         bool mBusy;
    112         // Camera device operations handle shared by all devices
    113         const static camera3_device_ops_t sOps;
    114         // Methods used to call back into the framework
    115         const camera3_callback_ops_t *mCallbackOps;
    116         // Lock protecting the Camera object for modifications
    117         android::Mutex mDeviceLock;
    118         // Lock protecting only static camera characteristics, which may
    119         // be accessed without the camera device open
    120         android::Mutex mStaticInfoLock;
    121         // Array of handles to streams currently in use by the device
    122         android::Vector<Stream *> mStreams;
    123         // Static array of standard camera settings templates
    124         camera_metadata_t *mTemplates[CAMERA3_TEMPLATE_COUNT];
    125         // Most recent request settings seen, memoized to be reused
    126         camera_metadata_t *mSettings;
    127         bool mIsInitialized;
    128 };
    129 } // namespace usb_camera_hal
    130 
    131 #endif // CAMERA_H_
    132