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 "CameraParams2"
19 // #define LOG_NDEBUG 0
20 #include <utils/Log.h>
21 
22 #include <string.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <camera/CameraParameters2.h>
26 
27 namespace android {
28 
CameraParameters2()29 CameraParameters2::CameraParameters2()
30                 : mMap()
31 {
32 }
33 
~CameraParameters2()34 CameraParameters2::~CameraParameters2()
35 {
36 }
37 
flatten() const38 String8 CameraParameters2::flatten() const
39 {
40     String8 flattened("");
41     size_t size = mMap.size();
42 
43     for (size_t i = 0; i < size; i++) {
44         String8 k, v;
45         k = mMap.keyAt(i);
46         v = mMap.valueAt(i);
47 
48         flattened += k;
49         flattened += "=";
50         flattened += v;
51         if (i != size-1)
52             flattened += ";";
53     }
54 
55     ALOGV("%s: Flattened params = %s", __FUNCTION__, flattened.string());
56 
57     return flattened;
58 }
59 
unflatten(const String8 & params)60 void CameraParameters2::unflatten(const String8 &params)
61 {
62     const char *a = params.string();
63     const char *b;
64 
65     mMap.clear();
66 
67     for (;;) {
68         // Find the bounds of the key name.
69         b = strchr(a, '=');
70         if (b == 0)
71             break;
72 
73         // Create the key string.
74         String8 k(a, (size_t)(b-a));
75 
76         // Find the value.
77         a = b+1;
78         b = strchr(a, ';');
79         if (b == 0) {
80             // If there's no semicolon, this is the last item.
81             String8 v(a);
82             mMap.add(k, v);
83             break;
84         }
85 
86         String8 v(a, (size_t)(b-a));
87         mMap.add(k, v);
88         a = b+1;
89     }
90 }
91 
92 
set(const char * key,const char * value)93 void CameraParameters2::set(const char *key, const char *value)
94 {
95     // XXX i think i can do this with strspn()
96     if (strchr(key, '=') || strchr(key, ';')) {
97         //XXX ALOGE("Key \"%s\"contains invalid character (= or ;)", key);
98         return;
99     }
100 
101     if (strchr(value, '=') || strchr(value, ';')) {
102         //XXX ALOGE("Value \"%s\"contains invalid character (= or ;)", value);
103         return;
104     }
105 
106     // Replacing a value updates the key's order to be the new largest order
107     ssize_t res = mMap.replaceValueFor(String8(key), String8(value));
108     LOG_ALWAYS_FATAL_IF(res < 0, "replaceValueFor(%s,%s) failed", key, value);
109 }
110 
set(const char * key,int value)111 void CameraParameters2::set(const char *key, int value)
112 {
113     char str[16];
114     sprintf(str, "%d", value);
115     set(key, str);
116 }
117 
setFloat(const char * key,float value)118 void CameraParameters2::setFloat(const char *key, float value)
119 {
120     char str[16];  // 14 should be enough. We overestimate to be safe.
121     snprintf(str, sizeof(str), "%g", value);
122     set(key, str);
123 }
124 
get(const char * key) const125 const char *CameraParameters2::get(const char *key) const
126 {
127     ssize_t idx = mMap.indexOfKey(String8(key));
128     if (idx < 0) {
129         return NULL;
130     } else {
131         return mMap.valueAt(idx).string();
132     }
133 }
134 
getInt(const char * key) const135 int CameraParameters2::getInt(const char *key) const
136 {
137     const char *v = get(key);
138     if (v == 0)
139         return -1;
140     return strtol(v, 0, 0);
141 }
142 
getFloat(const char * key) const143 float CameraParameters2::getFloat(const char *key) const
144 {
145     const char *v = get(key);
146     if (v == 0) return -1;
147     return strtof(v, 0);
148 }
149 
compareSetOrder(const char * key1,const char * key2,int * order) const150 status_t CameraParameters2::compareSetOrder(const char *key1, const char *key2,
151         int *order) const {
152     if (key1 == NULL) {
153         ALOGE("%s: key1 must not be NULL", __FUNCTION__);
154         return BAD_VALUE;
155     } else if (key2 == NULL) {
156         ALOGE("%s: key2 must not be NULL", __FUNCTION__);
157         return BAD_VALUE;
158     } else if (order == NULL) {
159         ALOGE("%s: order must not be NULL", __FUNCTION__);
160         return BAD_VALUE;
161     }
162 
163     ssize_t index1 = mMap.indexOfKey(String8(key1));
164     ssize_t index2 = mMap.indexOfKey(String8(key2));
165     if (index1 < 0) {
166         ALOGW("%s: Key1 (%s) was not set", __FUNCTION__, key1);
167         return NAME_NOT_FOUND;
168     } else if (index2 < 0) {
169         ALOGW("%s: Key2 (%s) was not set", __FUNCTION__, key2);
170         return NAME_NOT_FOUND;
171     }
172 
173     *order = (index1 == index2) ? 0  :
174              (index1 < index2)  ? -1 :
175              1;
176 
177     return OK;
178 }
179 
remove(const char * key)180 void CameraParameters2::remove(const char *key)
181 {
182     mMap.removeItem(String8(key));
183 }
184 
185 // Parse string like "640x480" or "10000,20000"
parse_pair(const char * str,int * first,int * second,char delim,char ** endptr=NULL)186 static int parse_pair(const char *str, int *first, int *second, char delim,
187                       char **endptr = NULL)
188 {
189     // Find the first integer.
190     char *end;
191     int w = (int)strtol(str, &end, 10);
192     // If a delimeter does not immediately follow, give up.
193     if (*end != delim) {
194         ALOGE("Cannot find delimeter (%c) in str=%s", delim, str);
195         return -1;
196     }
197 
198     // Find the second integer, immediately after the delimeter.
199     int h = (int)strtol(end+1, &end, 10);
200 
201     *first = w;
202     *second = h;
203 
204     if (endptr) {
205         *endptr = end;
206     }
207 
208     return 0;
209 }
210 
parseSizesList(const char * sizesStr,Vector<Size> & sizes)211 static void parseSizesList(const char *sizesStr, Vector<Size> &sizes)
212 {
213     if (sizesStr == 0) {
214         return;
215     }
216 
217     char *sizeStartPtr = (char *)sizesStr;
218 
219     while (true) {
220         int width, height;
221         int success = parse_pair(sizeStartPtr, &width, &height, 'x',
222                                  &sizeStartPtr);
223         if (success == -1 || (*sizeStartPtr != ',' && *sizeStartPtr != '\0')) {
224             ALOGE("Picture sizes string \"%s\" contains invalid character.", sizesStr);
225             return;
226         }
227         sizes.push(Size(width, height));
228 
229         if (*sizeStartPtr == '\0') {
230             return;
231         }
232         sizeStartPtr++;
233     }
234 }
235 
setPreviewSize(int width,int height)236 void CameraParameters2::setPreviewSize(int width, int height)
237 {
238     char str[32];
239     sprintf(str, "%dx%d", width, height);
240     set(CameraParameters::KEY_PREVIEW_SIZE, str);
241 }
242 
getPreviewSize(int * width,int * height) const243 void CameraParameters2::getPreviewSize(int *width, int *height) const
244 {
245     *width = *height = -1;
246     // Get the current string, if it doesn't exist, leave the -1x-1
247     const char *p = get(CameraParameters::KEY_PREVIEW_SIZE);
248     if (p == 0)  return;
249     parse_pair(p, width, height, 'x');
250 }
251 
getPreferredPreviewSizeForVideo(int * width,int * height) const252 void CameraParameters2::getPreferredPreviewSizeForVideo(int *width, int *height) const
253 {
254     *width = *height = -1;
255     const char *p = get(CameraParameters::KEY_PREFERRED_PREVIEW_SIZE_FOR_VIDEO);
256     if (p == 0)  return;
257     parse_pair(p, width, height, 'x');
258 }
259 
getSupportedPreviewSizes(Vector<Size> & sizes) const260 void CameraParameters2::getSupportedPreviewSizes(Vector<Size> &sizes) const
261 {
262     const char *previewSizesStr = get(CameraParameters::KEY_SUPPORTED_PREVIEW_SIZES);
263     parseSizesList(previewSizesStr, sizes);
264 }
265 
setVideoSize(int width,int height)266 void CameraParameters2::setVideoSize(int width, int height)
267 {
268     char str[32];
269     sprintf(str, "%dx%d", width, height);
270     set(CameraParameters::KEY_VIDEO_SIZE, str);
271 }
272 
getVideoSize(int * width,int * height) const273 void CameraParameters2::getVideoSize(int *width, int *height) const
274 {
275     *width = *height = -1;
276     const char *p = get(CameraParameters::KEY_VIDEO_SIZE);
277     if (p == 0) return;
278     parse_pair(p, width, height, 'x');
279 }
280 
getSupportedVideoSizes(Vector<Size> & sizes) const281 void CameraParameters2::getSupportedVideoSizes(Vector<Size> &sizes) const
282 {
283     const char *videoSizesStr = get(CameraParameters::KEY_SUPPORTED_VIDEO_SIZES);
284     parseSizesList(videoSizesStr, sizes);
285 }
286 
setPreviewFrameRate(int fps)287 void CameraParameters2::setPreviewFrameRate(int fps)
288 {
289     set(CameraParameters::KEY_PREVIEW_FRAME_RATE, fps);
290 }
291 
getPreviewFrameRate() const292 int CameraParameters2::getPreviewFrameRate() const
293 {
294     return getInt(CameraParameters::KEY_PREVIEW_FRAME_RATE);
295 }
296 
getPreviewFpsRange(int * min_fps,int * max_fps) const297 void CameraParameters2::getPreviewFpsRange(int *min_fps, int *max_fps) const
298 {
299     *min_fps = *max_fps = -1;
300     const char *p = get(CameraParameters::KEY_PREVIEW_FPS_RANGE);
301     if (p == 0) return;
302     parse_pair(p, min_fps, max_fps, ',');
303 }
304 
setPreviewFpsRange(int min_fps,int max_fps)305 void CameraParameters2::setPreviewFpsRange(int min_fps, int max_fps)
306 {
307     String8 str = String8::format("%d,%d", min_fps, max_fps);
308     set(CameraParameters::KEY_PREVIEW_FPS_RANGE, str.string());
309 }
310 
setPreviewFormat(const char * format)311 void CameraParameters2::setPreviewFormat(const char *format)
312 {
313     set(CameraParameters::KEY_PREVIEW_FORMAT, format);
314 }
315 
getPreviewFormat() const316 const char *CameraParameters2::getPreviewFormat() const
317 {
318     return get(CameraParameters::KEY_PREVIEW_FORMAT);
319 }
320 
setPictureSize(int width,int height)321 void CameraParameters2::setPictureSize(int width, int height)
322 {
323     char str[32];
324     sprintf(str, "%dx%d", width, height);
325     set(CameraParameters::KEY_PICTURE_SIZE, str);
326 }
327 
getPictureSize(int * width,int * height) const328 void CameraParameters2::getPictureSize(int *width, int *height) const
329 {
330     *width = *height = -1;
331     // Get the current string, if it doesn't exist, leave the -1x-1
332     const char *p = get(CameraParameters::KEY_PICTURE_SIZE);
333     if (p == 0) return;
334     parse_pair(p, width, height, 'x');
335 }
336 
getSupportedPictureSizes(Vector<Size> & sizes) const337 void CameraParameters2::getSupportedPictureSizes(Vector<Size> &sizes) const
338 {
339     const char *pictureSizesStr = get(CameraParameters::KEY_SUPPORTED_PICTURE_SIZES);
340     parseSizesList(pictureSizesStr, sizes);
341 }
342 
setPictureFormat(const char * format)343 void CameraParameters2::setPictureFormat(const char *format)
344 {
345     set(CameraParameters::KEY_PICTURE_FORMAT, format);
346 }
347 
getPictureFormat() const348 const char *CameraParameters2::getPictureFormat() const
349 {
350     return get(CameraParameters::KEY_PICTURE_FORMAT);
351 }
352 
dump() const353 void CameraParameters2::dump() const
354 {
355     ALOGD("dump: mMap.size = %zu", mMap.size());
356     for (size_t i = 0; i < mMap.size(); i++) {
357         String8 k, v;
358         k = mMap.keyAt(i);
359         v = mMap.valueAt(i);
360         ALOGD("%s: %s\n", k.string(), v.string());
361     }
362 }
363 
dump(int fd,const Vector<String16> & args) const364 status_t CameraParameters2::dump(int fd, const Vector<String16>& args) const
365 {
366     (void)args;
367     const size_t SIZE = 256;
368     char buffer[SIZE];
369     String8 result;
370     snprintf(buffer, 255, "CameraParameters2::dump: mMap.size = %zu\n", mMap.size());
371     result.append(buffer);
372     for (size_t i = 0; i < mMap.size(); i++) {
373         String8 k, v;
374         k = mMap.keyAt(i);
375         v = mMap.valueAt(i);
376         snprintf(buffer, 255, "\t%s: %s\n", k.string(), v.string());
377         result.append(buffer);
378     }
379     write(fd, result.string(), result.size());
380     return NO_ERROR;
381 }
382 
383 }; // namespace android
384