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 //#define LOG_NDEBUG 0
     18 #define LOG_TAG "UsbCameraDevice"
     19 #include <cutils/log.h>
     20 
     21 #include <system/camera_metadata.h>
     22 
     23 #define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
     24 #include <utils/Trace.h>
     25 
     26 #include "Camera.h"
     27 #include "UsbCamera.h"
     28 
     29 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
     30 
     31 namespace usb_camera_hal {
     32 
     33 UsbCamera::UsbCamera(int id) : Camera(id) {
     34 }
     35 
     36 UsbCamera::~UsbCamera() {
     37 }
     38 
     39 int UsbCamera::initStaticInfo() {
     40     /*
     41      * Setup static camera info.  This will have to customized per camera
     42      * device.
     43      * TODO: this is just some sample code, need tailor for USB cameras.
     44      */
     45     if (mStaticInfo != NULL) {
     46         free_camera_metadata(mStaticInfo);
     47     }
     48 
     49     Metadata m;
     50 
     51     /* android.control */
     52     int32_t android_control_ae_available_target_fps_ranges[] = {30, 30};
     53     m.addInt32(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES,
     54             ARRAY_SIZE(android_control_ae_available_target_fps_ranges),
     55             android_control_ae_available_target_fps_ranges);
     56 
     57     int32_t android_control_ae_compensation_range[] = {-4, 4};
     58     m.addInt32(ANDROID_CONTROL_AE_COMPENSATION_RANGE,
     59             ARRAY_SIZE(android_control_ae_compensation_range),
     60             android_control_ae_compensation_range);
     61 
     62     camera_metadata_rational_t android_control_ae_compensation_step[] = {{2,1}};
     63     m.addRational(ANDROID_CONTROL_AE_COMPENSATION_STEP,
     64             ARRAY_SIZE(android_control_ae_compensation_step),
     65             android_control_ae_compensation_step);
     66 
     67     int32_t android_control_max_regions[] = {/*AE*/ 1,/*AWB*/ 1,/*AF*/ 1};
     68     m.addInt32(ANDROID_CONTROL_MAX_REGIONS,
     69             ARRAY_SIZE(android_control_max_regions),
     70             android_control_max_regions);
     71 
     72     /* android.jpeg */
     73     int32_t android_jpeg_available_thumbnail_sizes[] = {0, 0, 128, 96};
     74     m.addInt32(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES,
     75             ARRAY_SIZE(android_jpeg_available_thumbnail_sizes),
     76             android_jpeg_available_thumbnail_sizes);
     77 
     78     int32_t android_jpeg_max_size[] = {13 * 1024 * 1024}; // 13MB
     79     m.addInt32(ANDROID_JPEG_MAX_SIZE,
     80             ARRAY_SIZE(android_jpeg_max_size),
     81             android_jpeg_max_size);
     82 
     83     /* android.lens */
     84     float android_lens_info_available_focal_lengths[] = {1.0};
     85     m.addFloat(ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS,
     86             ARRAY_SIZE(android_lens_info_available_focal_lengths),
     87             android_lens_info_available_focal_lengths);
     88 
     89     /* android.request */
     90     int32_t android_request_max_num_output_streams[] = {0, 3, 1};
     91     m.addInt32(ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS,
     92             ARRAY_SIZE(android_request_max_num_output_streams),
     93             android_request_max_num_output_streams);
     94 
     95     /* android.scaler */
     96     int32_t android_scaler_available_formats[] = {
     97             HAL_PIXEL_FORMAT_RAW16,
     98             HAL_PIXEL_FORMAT_BLOB,
     99             HAL_PIXEL_FORMAT_RGBA_8888,
    100             HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED,
    101             // These are handled by YCbCr_420_888
    102             //        HAL_PIXEL_FORMAT_YV12,
    103             //        HAL_PIXEL_FORMAT_YCrCb_420_SP,
    104             HAL_PIXEL_FORMAT_YCbCr_420_888};
    105     m.addInt32(ANDROID_SCALER_AVAILABLE_FORMATS,
    106             ARRAY_SIZE(android_scaler_available_formats),
    107             android_scaler_available_formats);
    108 
    109     int64_t android_scaler_available_jpeg_min_durations[] = {1};
    110     m.addInt64(ANDROID_SCALER_AVAILABLE_JPEG_MIN_DURATIONS,
    111             ARRAY_SIZE(android_scaler_available_jpeg_min_durations),
    112             android_scaler_available_jpeg_min_durations);
    113 
    114     int32_t android_scaler_available_jpeg_sizes[] = {640, 480};
    115     m.addInt32(ANDROID_SCALER_AVAILABLE_JPEG_SIZES,
    116             ARRAY_SIZE(android_scaler_available_jpeg_sizes),
    117             android_scaler_available_jpeg_sizes);
    118 
    119     float android_scaler_available_max_digital_zoom[] = {1};
    120     m.addFloat(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM,
    121             ARRAY_SIZE(android_scaler_available_max_digital_zoom),
    122             android_scaler_available_max_digital_zoom);
    123 
    124     int64_t android_scaler_available_processed_min_durations[] = {1};
    125     m.addInt64(ANDROID_SCALER_AVAILABLE_PROCESSED_MIN_DURATIONS,
    126             ARRAY_SIZE(android_scaler_available_processed_min_durations),
    127             android_scaler_available_processed_min_durations);
    128 
    129     int32_t android_scaler_available_processed_sizes[] = {640, 480};
    130     m.addInt32(ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES,
    131             ARRAY_SIZE(android_scaler_available_processed_sizes),
    132             android_scaler_available_processed_sizes);
    133 
    134     int64_t android_scaler_available_raw_min_durations[] = {1};
    135     m.addInt64(ANDROID_SCALER_AVAILABLE_RAW_MIN_DURATIONS,
    136             ARRAY_SIZE(android_scaler_available_raw_min_durations),
    137             android_scaler_available_raw_min_durations);
    138 
    139     int32_t android_scaler_available_raw_sizes[] = {640, 480};
    140     m.addInt32(ANDROID_SCALER_AVAILABLE_RAW_SIZES,
    141             ARRAY_SIZE(android_scaler_available_raw_sizes),
    142             android_scaler_available_raw_sizes);
    143 
    144     /* android.sensor */
    145 
    146     int32_t android_sensor_info_active_array_size[] = {0, 0, 640, 480};
    147     m.addInt32(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE,
    148             ARRAY_SIZE(android_sensor_info_active_array_size),
    149             android_sensor_info_active_array_size);
    150 
    151     int32_t android_sensor_info_sensitivity_range[] =
    152             {100, 1600};
    153     m.addInt32(ANDROID_SENSOR_INFO_SENSITIVITY_RANGE,
    154             ARRAY_SIZE(android_sensor_info_sensitivity_range),
    155             android_sensor_info_sensitivity_range);
    156 
    157     int64_t android_sensor_info_max_frame_duration[] = {30000000000};
    158     m.addInt64(ANDROID_SENSOR_INFO_MAX_FRAME_DURATION,
    159             ARRAY_SIZE(android_sensor_info_max_frame_duration),
    160             android_sensor_info_max_frame_duration);
    161 
    162     float android_sensor_info_physical_size[] = {3.2, 2.4};
    163     m.addFloat(ANDROID_SENSOR_INFO_PHYSICAL_SIZE,
    164             ARRAY_SIZE(android_sensor_info_physical_size),
    165             android_sensor_info_physical_size);
    166 
    167     int32_t android_sensor_info_pixel_array_size[] = {640, 480};
    168     m.addInt32(ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE,
    169             ARRAY_SIZE(android_sensor_info_pixel_array_size),
    170             android_sensor_info_pixel_array_size);
    171 
    172     int32_t android_sensor_orientation[] = {0};
    173     m.addInt32(ANDROID_SENSOR_ORIENTATION,
    174             ARRAY_SIZE(android_sensor_orientation),
    175             android_sensor_orientation);
    176 
    177     /* End of static camera characteristics */
    178 
    179     mStaticInfo = clone_camera_metadata(m.get());
    180 
    181     return 0;
    182 }
    183 
    184 int UsbCamera::openDevice() {
    185     // TODO: implement usb camera device open sequence: open device nodes etc.
    186 
    187     return 0;
    188 }
    189 
    190 int UsbCamera::closeDevice() {
    191     // TODO: implement usb camera device close sequence: close device nodes etc.
    192 
    193     return 0;
    194 }
    195 
    196 int UsbCamera::processCaptureBuffer(const camera3_stream_buffer_t *in,
    197         camera3_stream_buffer_t *out) {
    198     if (in->acquire_fence != -1) {
    199         int res = sync_wait(in->acquire_fence, CAMERA_SYNC_TIMEOUT_MS);
    200         if (res == -ETIME) {
    201             ALOGE("%s:%d: Timeout waiting on buffer acquire fence",
    202                     __func__, mId);
    203             return res;
    204         } else if (res) {
    205             ALOGE("%s:%d: Error waiting on buffer acquire fence: %s(%d)",
    206                     __func__, mId, strerror(-res), res);
    207             return res;
    208         }
    209     }
    210 
    211     out->stream = in->stream;
    212     out->buffer = in->buffer;
    213     out->status = CAMERA3_BUFFER_STATUS_OK;
    214     // TODO: use driver-backed release fences
    215     out->acquire_fence = -1;
    216     out->release_fence = -1;
    217 
    218     // TODO: lock and software-paint buffer
    219     return 0;
    220 }
    221 
    222 int UsbCamera::initDevice() {
    223     int res;
    224     Metadata base;
    225 
    226     // Create standard settings templates from copies of base metadata
    227     res = base.add1UInt8(ANDROID_CONTROL_MODE, ANDROID_CONTROL_MODE_OFF);
    228     if (res)
    229         return res;
    230 
    231     // Use base settings to create all other templates and set them. This is just some samples,
    232     // More initialization may be needed.
    233     res = initPreviewTemplate(base);
    234     if (res)
    235         return res;
    236     res = initStillTemplate(base);
    237     if (res)
    238         return res;
    239     res = initRecordTemplate(base);
    240     if (res)
    241         return res;
    242     res = initSnapshotTemplate(base);
    243     if (res)
    244         return res;
    245     res = initZslTemplate(base);
    246     if (res)
    247         return res;
    248     res = initManualTemplate(base);
    249     if (res)
    250         return res;
    251 
    252     return 0;
    253 }
    254 
    255 int UsbCamera::initPreviewTemplate(Metadata m) {
    256     // Setup default preview controls
    257     int res = m.add1UInt8(ANDROID_CONTROL_CAPTURE_INTENT,
    258                             ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW);
    259 
    260     if (res)
    261         return res;
    262     // TODO: set fast auto-focus, auto-whitebalance, auto-exposure, auto flash
    263     return setTemplate(CAMERA3_TEMPLATE_PREVIEW, m.get());
    264 }
    265 
    266 int UsbCamera::initStillTemplate(Metadata m) {
    267     int res = m.add1UInt8(ANDROID_CONTROL_CAPTURE_INTENT,
    268                             ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE);
    269     // Setup default still capture controls
    270     if (res)
    271         return res;
    272     // TODO: set fast auto-focus, auto-whitebalance, auto-exposure, auto flash
    273     return setTemplate(CAMERA3_TEMPLATE_STILL_CAPTURE, m.get());
    274 }
    275 
    276 int UsbCamera::initRecordTemplate(Metadata m) {
    277     int res = m.add1UInt8(ANDROID_CONTROL_CAPTURE_INTENT,
    278                             ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD);
    279     // Setup default video record controls
    280     if (res)
    281         return res;
    282     // TODO: set slow auto-focus, auto-whitebalance, auto-exposure, flash off
    283     return setTemplate(CAMERA3_TEMPLATE_VIDEO_RECORD, m.get());
    284 }
    285 
    286 int UsbCamera::initSnapshotTemplate(Metadata m) {
    287     int res = m.add1UInt8(ANDROID_CONTROL_CAPTURE_INTENT,
    288                             ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT);
    289     // Setup default video snapshot controls
    290     if (res)
    291         return res;
    292     // TODO: set slow auto-focus, auto-whitebalance, auto-exposure, flash off
    293     return setTemplate(CAMERA3_TEMPLATE_VIDEO_SNAPSHOT, m.get());
    294 }
    295 
    296 int UsbCamera::initZslTemplate(Metadata m) {
    297     int res = m.add1UInt8(ANDROID_CONTROL_CAPTURE_INTENT,
    298                             ANDROID_CONTROL_CAPTURE_INTENT_ZERO_SHUTTER_LAG);
    299     // Setup default zero shutter lag controls
    300     if (res)
    301         return res;
    302     // TODO: set reprocessing parameters for zsl input queue
    303     return setTemplate(CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG, m.get());
    304 }
    305 
    306 int UsbCamera::initManualTemplate(Metadata m) {
    307     int res = m.add1UInt8(ANDROID_CONTROL_CAPTURE_INTENT,
    308                             ANDROID_CONTROL_CAPTURE_INTENT_MANUAL);
    309     // Setup manual controls
    310     if (res)
    311         return res;
    312     // TODO: set reprocessing parameters for zsl input queue
    313     return setTemplate(CAMERA3_TEMPLATE_MANUAL, m.get());
    314 }
    315 
    316 bool UsbCamera::isValidCaptureSettings(const camera_metadata_t* settings) {
    317     // TODO: reject settings that cannot be captured
    318     return true;
    319 }
    320 
    321 } // namespace usb_camera_hal
    322