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