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";
149
150const char CameraParameters::PIXEL_FORMAT_YUV422SP[] = "yuv422sp";
151const char CameraParameters::PIXEL_FORMAT_YUV420SP[] = "yuv420sp";
152const char CameraParameters::PIXEL_FORMAT_YUV422I[] = "yuv422i-yuyv";
153const char CameraParameters::PIXEL_FORMAT_YUV420P[]  = "yuv420p";
154const char CameraParameters::PIXEL_FORMAT_RGB565[] = "rgb565";
155const char CameraParameters::PIXEL_FORMAT_RGBA8888[] = "rgba8888";
156const char CameraParameters::PIXEL_FORMAT_JPEG[] = "jpeg";
157const char CameraParameters::PIXEL_FORMAT_BAYER_RGGB[] = "bayer-rggb";
158
159// Values for focus mode settings.
160const char CameraParameters::FOCUS_MODE_AUTO[] = "auto";
161const char CameraParameters::FOCUS_MODE_INFINITY[] = "infinity";
162const char CameraParameters::FOCUS_MODE_MACRO[] = "macro";
163const char CameraParameters::FOCUS_MODE_FIXED[] = "fixed";
164const char CameraParameters::FOCUS_MODE_EDOF[] = "edof";
165const char CameraParameters::FOCUS_MODE_CONTINUOUS_VIDEO[] = "continuous-video";
166const char CameraParameters::FOCUS_MODE_CONTINUOUS_PICTURE[] = "continuous-picture";
167
168CameraParameters::CameraParameters()
169                : mMap()
170{
171}
172
173CameraParameters::~CameraParameters()
174{
175}
176
177String8 CameraParameters::flatten() const
178{
179    String8 flattened("");
180    size_t size = mMap.size();
181
182    for (size_t i = 0; i < size; i++) {
183        String8 k, v;
184        k = mMap.keyAt(i);
185        v = mMap.valueAt(i);
186
187        flattened += k;
188        flattened += "=";
189        flattened += v;
190        if (i != size-1)
191            flattened += ";";
192    }
193
194    return flattened;
195}
196
197void CameraParameters::unflatten(const String8 &params)
198{
199    const char *a = params.string();
200    const char *b;
201
202    mMap.clear();
203
204    for (;;) {
205        // Find the bounds of the key name.
206        b = strchr(a, '=');
207        if (b == 0)
208            break;
209
210        // Create the key string.
211        String8 k(a, (size_t)(b-a));
212
213        // Find the value.
214        a = b+1;
215        b = strchr(a, ';');
216        if (b == 0) {
217            // If there's no semicolon, this is the last item.
218            String8 v(a);
219            mMap.add(k, v);
220            break;
221        }
222
223        String8 v(a, (size_t)(b-a));
224        mMap.add(k, v);
225        a = b+1;
226    }
227}
228
229
230void CameraParameters::set(const char *key, const char *value)
231{
232    // XXX i think i can do this with strspn()
233    if (strchr(key, '=') || strchr(key, ';')) {
234        //XXX ALOGE("Key \"%s\"contains invalid character (= or ;)", key);
235        return;
236    }
237
238    if (strchr(value, '=') || strchr(key, ';')) {
239        //XXX ALOGE("Value \"%s\"contains invalid character (= or ;)", value);
240        return;
241    }
242
243    mMap.replaceValueFor(String8(key), String8(value));
244}
245
246void CameraParameters::set(const char *key, int value)
247{
248    char str[16];
249    sprintf(str, "%d", value);
250    set(key, str);
251}
252
253void CameraParameters::setFloat(const char *key, float value)
254{
255    char str[16];  // 14 should be enough. We overestimate to be safe.
256    snprintf(str, sizeof(str), "%g", value);
257    set(key, str);
258}
259
260const char *CameraParameters::get(const char *key) const
261{
262    String8 v = mMap.valueFor(String8(key));
263    if (v.length() == 0)
264        return 0;
265    return v.string();
266}
267
268int CameraParameters::getInt(const char *key) const
269{
270    const char *v = get(key);
271    if (v == 0)
272        return -1;
273    return strtol(v, 0, 0);
274}
275
276float CameraParameters::getFloat(const char *key) const
277{
278    const char *v = get(key);
279    if (v == 0) return -1;
280    return strtof(v, 0);
281}
282
283void CameraParameters::remove(const char *key)
284{
285    mMap.removeItem(String8(key));
286}
287
288// Parse string like "640x480" or "10000,20000"
289static int parse_pair(const char *str, int *first, int *second, char delim,
290                      char **endptr = NULL)
291{
292    // Find the first integer.
293    char *end;
294    int w = (int)strtol(str, &end, 10);
295    // If a delimeter does not immediately follow, give up.
296    if (*end != delim) {
297        ALOGE("Cannot find delimeter (%c) in str=%s", delim, str);
298        return -1;
299    }
300
301    // Find the second integer, immediately after the delimeter.
302    int h = (int)strtol(end+1, &end, 10);
303
304    *first = w;
305    *second = h;
306
307    if (endptr) {
308        *endptr = end;
309    }
310
311    return 0;
312}
313
314static void parseSizesList(const char *sizesStr, Vector<Size> &sizes)
315{
316    if (sizesStr == 0) {
317        return;
318    }
319
320    char *sizeStartPtr = (char *)sizesStr;
321
322    while (true) {
323        int width, height;
324        int success = parse_pair(sizeStartPtr, &width, &height, 'x',
325                                 &sizeStartPtr);
326        if (success == -1 || (*sizeStartPtr != ',' && *sizeStartPtr != '\0')) {
327            ALOGE("Picture sizes string \"%s\" contains invalid character.", sizesStr);
328            return;
329        }
330        sizes.push(Size(width, height));
331
332        if (*sizeStartPtr == '\0') {
333            return;
334        }
335        sizeStartPtr++;
336    }
337}
338
339void CameraParameters::setPreviewSize(int width, int height)
340{
341    char str[32];
342    sprintf(str, "%dx%d", width, height);
343    set(KEY_PREVIEW_SIZE, str);
344}
345
346void CameraParameters::getPreviewSize(int *width, int *height) const
347{
348    *width = *height = -1;
349    // Get the current string, if it doesn't exist, leave the -1x-1
350    const char *p = get(KEY_PREVIEW_SIZE);
351    if (p == 0)  return;
352    parse_pair(p, width, height, 'x');
353}
354
355void CameraParameters::getPreferredPreviewSizeForVideo(int *width, int *height) const
356{
357    *width = *height = -1;
358    const char *p = get(KEY_PREFERRED_PREVIEW_SIZE_FOR_VIDEO);
359    if (p == 0)  return;
360    parse_pair(p, width, height, 'x');
361}
362
363void CameraParameters::getSupportedPreviewSizes(Vector<Size> &sizes) const
364{
365    const char *previewSizesStr = get(KEY_SUPPORTED_PREVIEW_SIZES);
366    parseSizesList(previewSizesStr, sizes);
367}
368
369void CameraParameters::setVideoSize(int width, int height)
370{
371    char str[32];
372    sprintf(str, "%dx%d", width, height);
373    set(KEY_VIDEO_SIZE, str);
374}
375
376void CameraParameters::getVideoSize(int *width, int *height) const
377{
378    *width = *height = -1;
379    const char *p = get(KEY_VIDEO_SIZE);
380    if (p == 0) return;
381    parse_pair(p, width, height, 'x');
382}
383
384void CameraParameters::getSupportedVideoSizes(Vector<Size> &sizes) const
385{
386    const char *videoSizesStr = get(KEY_SUPPORTED_VIDEO_SIZES);
387    parseSizesList(videoSizesStr, sizes);
388}
389
390void CameraParameters::setPreviewFrameRate(int fps)
391{
392    set(KEY_PREVIEW_FRAME_RATE, fps);
393}
394
395int CameraParameters::getPreviewFrameRate() const
396{
397    return getInt(KEY_PREVIEW_FRAME_RATE);
398}
399
400void CameraParameters::getPreviewFpsRange(int *min_fps, int *max_fps) const
401{
402    *min_fps = *max_fps = -1;
403    const char *p = get(KEY_PREVIEW_FPS_RANGE);
404    if (p == 0) return;
405    parse_pair(p, min_fps, max_fps, ',');
406}
407
408void CameraParameters::setPreviewFormat(const char *format)
409{
410    set(KEY_PREVIEW_FORMAT, format);
411}
412
413const char *CameraParameters::getPreviewFormat() const
414{
415    return get(KEY_PREVIEW_FORMAT);
416}
417
418void CameraParameters::setPictureSize(int width, int height)
419{
420    char str[32];
421    sprintf(str, "%dx%d", width, height);
422    set(KEY_PICTURE_SIZE, str);
423}
424
425void CameraParameters::getPictureSize(int *width, int *height) const
426{
427    *width = *height = -1;
428    // Get the current string, if it doesn't exist, leave the -1x-1
429    const char *p = get(KEY_PICTURE_SIZE);
430    if (p == 0) return;
431    parse_pair(p, width, height, 'x');
432}
433
434void CameraParameters::getSupportedPictureSizes(Vector<Size> &sizes) const
435{
436    const char *pictureSizesStr = get(KEY_SUPPORTED_PICTURE_SIZES);
437    parseSizesList(pictureSizesStr, sizes);
438}
439
440void CameraParameters::setPictureFormat(const char *format)
441{
442    set(KEY_PICTURE_FORMAT, format);
443}
444
445const char *CameraParameters::getPictureFormat() const
446{
447    return get(KEY_PICTURE_FORMAT);
448}
449
450void CameraParameters::dump() const
451{
452    ALOGD("dump: mMap.size = %d", mMap.size());
453    for (size_t i = 0; i < mMap.size(); i++) {
454        String8 k, v;
455        k = mMap.keyAt(i);
456        v = mMap.valueAt(i);
457        ALOGD("%s: %s\n", k.string(), v.string());
458    }
459}
460
461status_t CameraParameters::dump(int fd, const Vector<String16>& args) const
462{
463    const size_t SIZE = 256;
464    char buffer[SIZE];
465    String8 result;
466    snprintf(buffer, 255, "CameraParameters::dump: mMap.size = %d\n", mMap.size());
467    result.append(buffer);
468    for (size_t i = 0; i < mMap.size(); i++) {
469        String8 k, v;
470        k = mMap.keyAt(i);
471        v = mMap.valueAt(i);
472        snprintf(buffer, 255, "\t%s: %s\n", k.string(), v.string());
473        result.append(buffer);
474    }
475    write(fd, result.string(), result.size());
476    return NO_ERROR;
477}
478
479}; // namespace android
480