1/*
2**
3** Copyright 2008, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9**     http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#define LOG_TAG "CameraParams"
19#include <utils/Log.h>
20
21#include <string.h>
22#include <stdlib.h>
23#include <camera/CameraParameters.h>
24
25namespace android {
26// Parameter keys to communicate between camera application and driver.
27const char CameraParameters::KEY_PREVIEW_SIZE[] = "preview-size";
28const char CameraParameters::KEY_SUPPORTED_PREVIEW_SIZES[] = "preview-size-values";
29const char CameraParameters::KEY_PREVIEW_FORMAT[] = "preview-format";
30const char CameraParameters::KEY_SUPPORTED_PREVIEW_FORMATS[] = "preview-format-values";
31const char CameraParameters::KEY_PREVIEW_FRAME_RATE[] = "preview-frame-rate";
32const char CameraParameters::KEY_SUPPORTED_PREVIEW_FRAME_RATES[] = "preview-frame-rate-values";
33const char CameraParameters::KEY_PREVIEW_FPS_RANGE[] = "preview-fps-range";
34const char CameraParameters::KEY_SUPPORTED_PREVIEW_FPS_RANGE[] = "preview-fps-range-values";
35const char CameraParameters::KEY_PICTURE_SIZE[] = "picture-size";
36const char CameraParameters::KEY_SUPPORTED_PICTURE_SIZES[] = "picture-size-values";
37const char CameraParameters::KEY_PICTURE_FORMAT[] = "picture-format";
38const char CameraParameters::KEY_SUPPORTED_PICTURE_FORMATS[] = "picture-format-values";
39const char CameraParameters::KEY_JPEG_THUMBNAIL_WIDTH[] = "jpeg-thumbnail-width";
40const char CameraParameters::KEY_JPEG_THUMBNAIL_HEIGHT[] = "jpeg-thumbnail-height";
41const char CameraParameters::KEY_SUPPORTED_JPEG_THUMBNAIL_SIZES[] = "jpeg-thumbnail-size-values";
42const char CameraParameters::KEY_JPEG_THUMBNAIL_QUALITY[] = "jpeg-thumbnail-quality";
43const char CameraParameters::KEY_JPEG_QUALITY[] = "jpeg-quality";
44const char CameraParameters::KEY_ROTATION[] = "rotation";
45const char CameraParameters::KEY_GPS_LATITUDE[] = "gps-latitude";
46const char CameraParameters::KEY_GPS_LONGITUDE[] = "gps-longitude";
47const char CameraParameters::KEY_GPS_ALTITUDE[] = "gps-altitude";
48const char CameraParameters::KEY_GPS_TIMESTAMP[] = "gps-timestamp";
49const char CameraParameters::KEY_GPS_PROCESSING_METHOD[] = "gps-processing-method";
50const char CameraParameters::KEY_WHITE_BALANCE[] = "whitebalance";
51const char CameraParameters::KEY_SUPPORTED_WHITE_BALANCE[] = "whitebalance-values";
52const char CameraParameters::KEY_EFFECT[] = "effect";
53const char CameraParameters::KEY_SUPPORTED_EFFECTS[] = "effect-values";
54const char CameraParameters::KEY_ANTIBANDING[] = "antibanding";
55const char CameraParameters::KEY_SUPPORTED_ANTIBANDING[] = "antibanding-values";
56const char CameraParameters::KEY_SCENE_MODE[] = "scene-mode";
57const char CameraParameters::KEY_SUPPORTED_SCENE_MODES[] = "scene-mode-values";
58const char CameraParameters::KEY_FLASH_MODE[] = "flash-mode";
59const char CameraParameters::KEY_SUPPORTED_FLASH_MODES[] = "flash-mode-values";
60const char CameraParameters::KEY_FOCUS_MODE[] = "focus-mode";
61const char CameraParameters::KEY_SUPPORTED_FOCUS_MODES[] = "focus-mode-values";
62const char CameraParameters::KEY_MAX_NUM_FOCUS_AREAS[] = "max-num-focus-areas";
63const char CameraParameters::KEY_FOCUS_AREAS[] = "focus-areas";
64const char CameraParameters::KEY_FOCAL_LENGTH[] = "focal-length";
65const char CameraParameters::KEY_HORIZONTAL_VIEW_ANGLE[] = "horizontal-view-angle";
66const char CameraParameters::KEY_VERTICAL_VIEW_ANGLE[] = "vertical-view-angle";
67const char CameraParameters::KEY_EXPOSURE_COMPENSATION[] = "exposure-compensation";
68const char CameraParameters::KEY_MAX_EXPOSURE_COMPENSATION[] = "max-exposure-compensation";
69const char CameraParameters::KEY_MIN_EXPOSURE_COMPENSATION[] = "min-exposure-compensation";
70const char CameraParameters::KEY_EXPOSURE_COMPENSATION_STEP[] = "exposure-compensation-step";
71const char CameraParameters::KEY_AUTO_EXPOSURE_LOCK[] = "auto-exposure-lock";
72const char CameraParameters::KEY_AUTO_EXPOSURE_LOCK_SUPPORTED[] = "auto-exposure-lock-supported";
73const char CameraParameters::KEY_AUTO_WHITEBALANCE_LOCK[] = "auto-whitebalance-lock";
74const char CameraParameters::KEY_AUTO_WHITEBALANCE_LOCK_SUPPORTED[] = "auto-whitebalance-lock-supported";
75const char CameraParameters::KEY_MAX_NUM_METERING_AREAS[] = "max-num-metering-areas";
76const char CameraParameters::KEY_METERING_AREAS[] = "metering-areas";
77const char CameraParameters::KEY_ZOOM[] = "zoom";
78const char CameraParameters::KEY_MAX_ZOOM[] = "max-zoom";
79const char CameraParameters::KEY_ZOOM_RATIOS[] = "zoom-ratios";
80const char CameraParameters::KEY_ZOOM_SUPPORTED[] = "zoom-supported";
81const char CameraParameters::KEY_SMOOTH_ZOOM_SUPPORTED[] = "smooth-zoom-supported";
82const char CameraParameters::KEY_FOCUS_DISTANCES[] = "focus-distances";
83const char CameraParameters::KEY_VIDEO_FRAME_FORMAT[] = "video-frame-format";
84const char CameraParameters::KEY_VIDEO_SIZE[] = "video-size";
85const char CameraParameters::KEY_SUPPORTED_VIDEO_SIZES[] = "video-size-values";
86const char CameraParameters::KEY_PREFERRED_PREVIEW_SIZE_FOR_VIDEO[] = "preferred-preview-size-for-video";
87const char CameraParameters::KEY_MAX_NUM_DETECTED_FACES_HW[] = "max-num-detected-faces-hw";
88const char CameraParameters::KEY_MAX_NUM_DETECTED_FACES_SW[] = "max-num-detected-faces-sw";
89const char CameraParameters::KEY_RECORDING_HINT[] = "recording-hint";
90const char CameraParameters::KEY_VIDEO_SNAPSHOT_SUPPORTED[] = "video-snapshot-supported";
91const char CameraParameters::KEY_VIDEO_STABILIZATION[] = "video-stabilization";
92const char CameraParameters::KEY_VIDEO_STABILIZATION_SUPPORTED[] = "video-stabilization-supported";
93
94const char CameraParameters::TRUE[] = "true";
95const char CameraParameters::FALSE[] = "false";
96const char CameraParameters::FOCUS_DISTANCE_INFINITY[] = "Infinity";
97
98// Values for white balance settings.
99const char CameraParameters::WHITE_BALANCE_AUTO[] = "auto";
100const char CameraParameters::WHITE_BALANCE_INCANDESCENT[] = "incandescent";
101const char CameraParameters::WHITE_BALANCE_FLUORESCENT[] = "fluorescent";
102const char CameraParameters::WHITE_BALANCE_WARM_FLUORESCENT[] = "warm-fluorescent";
103const char CameraParameters::WHITE_BALANCE_DAYLIGHT[] = "daylight";
104const char CameraParameters::WHITE_BALANCE_CLOUDY_DAYLIGHT[] = "cloudy-daylight";
105const char CameraParameters::WHITE_BALANCE_TWILIGHT[] = "twilight";
106const char CameraParameters::WHITE_BALANCE_SHADE[] = "shade";
107
108// Values for effect settings.
109const char CameraParameters::EFFECT_NONE[] = "none";
110const char CameraParameters::EFFECT_MONO[] = "mono";
111const char CameraParameters::EFFECT_NEGATIVE[] = "negative";
112const char CameraParameters::EFFECT_SOLARIZE[] = "solarize";
113const char CameraParameters::EFFECT_SEPIA[] = "sepia";
114const char CameraParameters::EFFECT_POSTERIZE[] = "posterize";
115const char CameraParameters::EFFECT_WHITEBOARD[] = "whiteboard";
116const char CameraParameters::EFFECT_BLACKBOARD[] = "blackboard";
117const char CameraParameters::EFFECT_AQUA[] = "aqua";
118
119// Values for antibanding settings.
120const char CameraParameters::ANTIBANDING_AUTO[] = "auto";
121const char CameraParameters::ANTIBANDING_50HZ[] = "50hz";
122const char CameraParameters::ANTIBANDING_60HZ[] = "60hz";
123const char CameraParameters::ANTIBANDING_OFF[] = "off";
124
125// Values for flash mode settings.
126const char CameraParameters::FLASH_MODE_OFF[] = "off";
127const char CameraParameters::FLASH_MODE_AUTO[] = "auto";
128const char CameraParameters::FLASH_MODE_ON[] = "on";
129const char CameraParameters::FLASH_MODE_RED_EYE[] = "red-eye";
130const char CameraParameters::FLASH_MODE_TORCH[] = "torch";
131
132// Values for scene mode settings.
133const char CameraParameters::SCENE_MODE_AUTO[] = "auto";
134const char CameraParameters::SCENE_MODE_ACTION[] = "action";
135const char CameraParameters::SCENE_MODE_PORTRAIT[] = "portrait";
136const char CameraParameters::SCENE_MODE_LANDSCAPE[] = "landscape";
137const char CameraParameters::SCENE_MODE_NIGHT[] = "night";
138const char CameraParameters::SCENE_MODE_NIGHT_PORTRAIT[] = "night-portrait";
139const char CameraParameters::SCENE_MODE_THEATRE[] = "theatre";
140const char CameraParameters::SCENE_MODE_BEACH[] = "beach";
141const char CameraParameters::SCENE_MODE_SNOW[] = "snow";
142const char CameraParameters::SCENE_MODE_SUNSET[] = "sunset";
143const char CameraParameters::SCENE_MODE_STEADYPHOTO[] = "steadyphoto";
144const char CameraParameters::SCENE_MODE_FIREWORKS[] = "fireworks";
145const char CameraParameters::SCENE_MODE_SPORTS[] = "sports";
146const char CameraParameters::SCENE_MODE_PARTY[] = "party";
147const char CameraParameters::SCENE_MODE_CANDLELIGHT[] = "candlelight";
148const char CameraParameters::SCENE_MODE_BARCODE[] = "barcode";
149const char CameraParameters::SCENE_MODE_HDR[] = "hdr";
150
151const char CameraParameters::PIXEL_FORMAT_YUV422SP[] = "yuv422sp";
152const char CameraParameters::PIXEL_FORMAT_YUV420SP[] = "yuv420sp";
153const char CameraParameters::PIXEL_FORMAT_YUV422I[] = "yuv422i-yuyv";
154const char CameraParameters::PIXEL_FORMAT_YUV420P[]  = "yuv420p";
155const char CameraParameters::PIXEL_FORMAT_RGB565[] = "rgb565";
156const char CameraParameters::PIXEL_FORMAT_RGBA8888[] = "rgba8888";
157const char CameraParameters::PIXEL_FORMAT_JPEG[] = "jpeg";
158const char CameraParameters::PIXEL_FORMAT_BAYER_RGGB[] = "bayer-rggb";
159const char CameraParameters::PIXEL_FORMAT_ANDROID_OPAQUE[] = "android-opaque";
160
161// Values for focus mode settings.
162const char CameraParameters::FOCUS_MODE_AUTO[] = "auto";
163const char CameraParameters::FOCUS_MODE_INFINITY[] = "infinity";
164const char CameraParameters::FOCUS_MODE_MACRO[] = "macro";
165const char CameraParameters::FOCUS_MODE_FIXED[] = "fixed";
166const char CameraParameters::FOCUS_MODE_EDOF[] = "edof";
167const char CameraParameters::FOCUS_MODE_CONTINUOUS_VIDEO[] = "continuous-video";
168const char CameraParameters::FOCUS_MODE_CONTINUOUS_PICTURE[] = "continuous-picture";
169
170CameraParameters::CameraParameters()
171                : mMap()
172{
173}
174
175CameraParameters::~CameraParameters()
176{
177}
178
179String8 CameraParameters::flatten() const
180{
181    String8 flattened("");
182    size_t size = mMap.size();
183
184    for (size_t i = 0; i < size; i++) {
185        String8 k, v;
186        k = mMap.keyAt(i);
187        v = mMap.valueAt(i);
188
189        flattened += k;
190        flattened += "=";
191        flattened += v;
192        if (i != size-1)
193            flattened += ";";
194    }
195
196    return flattened;
197}
198
199void CameraParameters::unflatten(const String8 &params)
200{
201    const char *a = params.string();
202    const char *b;
203
204    mMap.clear();
205
206    for (;;) {
207        // Find the bounds of the key name.
208        b = strchr(a, '=');
209        if (b == 0)
210            break;
211
212        // Create the key string.
213        String8 k(a, (size_t)(b-a));
214
215        // Find the value.
216        a = b+1;
217        b = strchr(a, ';');
218        if (b == 0) {
219            // If there's no semicolon, this is the last item.
220            String8 v(a);
221            mMap.add(k, v);
222            break;
223        }
224
225        String8 v(a, (size_t)(b-a));
226        mMap.add(k, v);
227        a = b+1;
228    }
229}
230
231
232void CameraParameters::set(const char *key, const char *value)
233{
234    // XXX i think i can do this with strspn()
235    if (strchr(key, '=') || strchr(key, ';')) {
236        //XXX ALOGE("Key \"%s\"contains invalid character (= or ;)", key);
237        return;
238    }
239
240    if (strchr(value, '=') || strchr(key, ';')) {
241        //XXX ALOGE("Value \"%s\"contains invalid character (= or ;)", value);
242        return;
243    }
244
245    mMap.replaceValueFor(String8(key), String8(value));
246}
247
248void CameraParameters::set(const char *key, int value)
249{
250    char str[16];
251    sprintf(str, "%d", value);
252    set(key, str);
253}
254
255void CameraParameters::setFloat(const char *key, float value)
256{
257    char str[16];  // 14 should be enough. We overestimate to be safe.
258    snprintf(str, sizeof(str), "%g", value);
259    set(key, str);
260}
261
262const char *CameraParameters::get(const char *key) const
263{
264    String8 v = mMap.valueFor(String8(key));
265    if (v.length() == 0)
266        return 0;
267    return v.string();
268}
269
270int CameraParameters::getInt(const char *key) const
271{
272    const char *v = get(key);
273    if (v == 0)
274        return -1;
275    return strtol(v, 0, 0);
276}
277
278float CameraParameters::getFloat(const char *key) const
279{
280    const char *v = get(key);
281    if (v == 0) return -1;
282    return strtof(v, 0);
283}
284
285void CameraParameters::remove(const char *key)
286{
287    mMap.removeItem(String8(key));
288}
289
290// Parse string like "640x480" or "10000,20000"
291static int parse_pair(const char *str, int *first, int *second, char delim,
292                      char **endptr = NULL)
293{
294    // Find the first integer.
295    char *end;
296    int w = (int)strtol(str, &end, 10);
297    // If a delimeter does not immediately follow, give up.
298    if (*end != delim) {
299        ALOGE("Cannot find delimeter (%c) in str=%s", delim, str);
300        return -1;
301    }
302
303    // Find the second integer, immediately after the delimeter.
304    int h = (int)strtol(end+1, &end, 10);
305
306    *first = w;
307    *second = h;
308
309    if (endptr) {
310        *endptr = end;
311    }
312
313    return 0;
314}
315
316static void parseSizesList(const char *sizesStr, Vector<Size> &sizes)
317{
318    if (sizesStr == 0) {
319        return;
320    }
321
322    char *sizeStartPtr = (char *)sizesStr;
323
324    while (true) {
325        int width, height;
326        int success = parse_pair(sizeStartPtr, &width, &height, 'x',
327                                 &sizeStartPtr);
328        if (success == -1 || (*sizeStartPtr != ',' && *sizeStartPtr != '\0')) {
329            ALOGE("Picture sizes string \"%s\" contains invalid character.", sizesStr);
330            return;
331        }
332        sizes.push(Size(width, height));
333
334        if (*sizeStartPtr == '\0') {
335            return;
336        }
337        sizeStartPtr++;
338    }
339}
340
341void CameraParameters::setPreviewSize(int width, int height)
342{
343    char str[32];
344    sprintf(str, "%dx%d", width, height);
345    set(KEY_PREVIEW_SIZE, str);
346}
347
348void CameraParameters::getPreviewSize(int *width, int *height) const
349{
350    *width = *height = -1;
351    // Get the current string, if it doesn't exist, leave the -1x-1
352    const char *p = get(KEY_PREVIEW_SIZE);
353    if (p == 0)  return;
354    parse_pair(p, width, height, 'x');
355}
356
357void CameraParameters::getPreferredPreviewSizeForVideo(int *width, int *height) const
358{
359    *width = *height = -1;
360    const char *p = get(KEY_PREFERRED_PREVIEW_SIZE_FOR_VIDEO);
361    if (p == 0)  return;
362    parse_pair(p, width, height, 'x');
363}
364
365void CameraParameters::getSupportedPreviewSizes(Vector<Size> &sizes) const
366{
367    const char *previewSizesStr = get(KEY_SUPPORTED_PREVIEW_SIZES);
368    parseSizesList(previewSizesStr, sizes);
369}
370
371void CameraParameters::setVideoSize(int width, int height)
372{
373    char str[32];
374    sprintf(str, "%dx%d", width, height);
375    set(KEY_VIDEO_SIZE, str);
376}
377
378void CameraParameters::getVideoSize(int *width, int *height) const
379{
380    *width = *height = -1;
381    const char *p = get(KEY_VIDEO_SIZE);
382    if (p == 0) return;
383    parse_pair(p, width, height, 'x');
384}
385
386void CameraParameters::getSupportedVideoSizes(Vector<Size> &sizes) const
387{
388    const char *videoSizesStr = get(KEY_SUPPORTED_VIDEO_SIZES);
389    parseSizesList(videoSizesStr, sizes);
390}
391
392void CameraParameters::setPreviewFrameRate(int fps)
393{
394    set(KEY_PREVIEW_FRAME_RATE, fps);
395}
396
397int CameraParameters::getPreviewFrameRate() const
398{
399    return getInt(KEY_PREVIEW_FRAME_RATE);
400}
401
402void CameraParameters::getPreviewFpsRange(int *min_fps, int *max_fps) const
403{
404    *min_fps = *max_fps = -1;
405    const char *p = get(KEY_PREVIEW_FPS_RANGE);
406    if (p == 0) return;
407    parse_pair(p, min_fps, max_fps, ',');
408}
409
410void CameraParameters::setPreviewFormat(const char *format)
411{
412    set(KEY_PREVIEW_FORMAT, format);
413}
414
415const char *CameraParameters::getPreviewFormat() const
416{
417    return get(KEY_PREVIEW_FORMAT);
418}
419
420void CameraParameters::setPictureSize(int width, int height)
421{
422    char str[32];
423    sprintf(str, "%dx%d", width, height);
424    set(KEY_PICTURE_SIZE, str);
425}
426
427void CameraParameters::getPictureSize(int *width, int *height) const
428{
429    *width = *height = -1;
430    // Get the current string, if it doesn't exist, leave the -1x-1
431    const char *p = get(KEY_PICTURE_SIZE);
432    if (p == 0) return;
433    parse_pair(p, width, height, 'x');
434}
435
436void CameraParameters::getSupportedPictureSizes(Vector<Size> &sizes) const
437{
438    const char *pictureSizesStr = get(KEY_SUPPORTED_PICTURE_SIZES);
439    parseSizesList(pictureSizesStr, sizes);
440}
441
442void CameraParameters::setPictureFormat(const char *format)
443{
444    set(KEY_PICTURE_FORMAT, format);
445}
446
447const char *CameraParameters::getPictureFormat() const
448{
449    return get(KEY_PICTURE_FORMAT);
450}
451
452void CameraParameters::dump() const
453{
454    ALOGD("dump: mMap.size = %d", mMap.size());
455    for (size_t i = 0; i < mMap.size(); i++) {
456        String8 k, v;
457        k = mMap.keyAt(i);
458        v = mMap.valueAt(i);
459        ALOGD("%s: %s\n", k.string(), v.string());
460    }
461}
462
463status_t CameraParameters::dump(int fd, const Vector<String16>& args) const
464{
465    const size_t SIZE = 256;
466    char buffer[SIZE];
467    String8 result;
468    snprintf(buffer, 255, "CameraParameters::dump: mMap.size = %d\n", mMap.size());
469    result.append(buffer);
470    for (size_t i = 0; i < mMap.size(); i++) {
471        String8 k, v;
472        k = mMap.keyAt(i);
473        v = mMap.valueAt(i);
474        snprintf(buffer, 255, "\t%s: %s\n", k.string(), v.string());
475        result.append(buffer);
476    }
477    write(fd, result.string(), result.size());
478    return NO_ERROR;
479}
480
481}; // namespace android
482