3 ** Copyright 2008, The Android Open Source Project
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
9 ** http://www.apache.org/licenses/LICENSE-2.0
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.
18 #define LOG_TAG "CameraParams2"
19 // #define LOG_NDEBUG 0
20 #include <utils/Log.h>
24 #include <camera/CameraParameters2.h>
28 CameraParameters2::CameraParameters2()
33 CameraParameters2::~CameraParameters2()
37 String8 CameraParameters2::flatten() const
39 String8 flattened("");
40 size_t size = mMap.size();
42 for (size_t i = 0; i < size; i++) {
54 ALOGV("%s: Flattened params = %s", __FUNCTION__, flattened.string());
59 void CameraParameters2::unflatten(const String8 ¶ms)
61 const char *a = params.string();
67 // Find the bounds of the key name.
72 // Create the key string.
73 String8 k(a, (size_t)(b-a));
79 // If there's no semicolon, this is the last item.
85 String8 v(a, (size_t)(b-a));
92 void CameraParameters2::set(const char *key, const char *value)
94 // XXX i think i can do this with strspn()
95 if (strchr(key, '=') || strchr(key, ';')) {
96 //XXX ALOGE("Key \"%s\"contains invalid character (= or ;)", key);
100 if (strchr(value, '=') || strchr(value, ';')) {
101 //XXX ALOGE("Value \"%s\"contains invalid character (= or ;)", value);
105 // Replacing a value updates the key's order to be the new largest order
106 ssize_t res = mMap.replaceValueFor(String8(key), String8(value));
107 LOG_ALWAYS_FATAL_IF(res < 0, "replaceValueFor(%s,%s) failed", key, value);
110 void CameraParameters2::set(const char *key, int value)
113 sprintf(str, "%d", value);
117 void CameraParameters2::setFloat(const char *key, float value)
119 char str[16]; // 14 should be enough. We overestimate to be safe.
120 snprintf(str, sizeof(str), "%g", value);
124 const char *CameraParameters2::get(const char *key) const
126 ssize_t idx = mMap.indexOfKey(String8(key));
130 return mMap.valueAt(idx).string();
134 int CameraParameters2::getInt(const char *key) const
136 const char *v = get(key);
139 return strtol(v, 0, 0);
142 float CameraParameters2::getFloat(const char *key) const
144 const char *v = get(key);
145 if (v == 0) return -1;
149 status_t CameraParameters2::compareSetOrder(const char *key1, const char *key2,
152 ALOGE("%s: key1 must not be NULL", __FUNCTION__);
154 } else if (key2 == NULL) {
155 ALOGE("%s: key2 must not be NULL", __FUNCTION__);
157 } else if (order == NULL) {
158 ALOGE("%s: order must not be NULL", __FUNCTION__);
162 ssize_t index1 = mMap.indexOfKey(String8(key1));
163 ssize_t index2 = mMap.indexOfKey(String8(key2));
165 ALOGW("%s: Key1 (%s) was not set", __FUNCTION__, key1);
166 return NAME_NOT_FOUND;
167 } else if (index2 < 0) {
168 ALOGW("%s: Key2 (%s) was not set", __FUNCTION__, key2);
169 return NAME_NOT_FOUND;
172 *order = (index1 == index2) ? 0 :
173 (index1 < index2) ? -1 :
179 void CameraParameters2::remove(const char *key)
181 mMap.removeItem(String8(key));
184 // Parse string like "640x480" or "10000,20000"
185 static int parse_pair(const char *str, int *first, int *second, char delim,
186 char **endptr = NULL)
188 // Find the first integer.
190 int w = (int)strtol(str, &end, 10);
191 // If a delimeter does not immediately follow, give up.
193 ALOGE("Cannot find delimeter (%c) in str=%s", delim, str);
197 // Find the second integer, immediately after the delimeter.
198 int h = (int)strtol(end+1, &end, 10);
210 static void parseSizesList(const char *sizesStr, Vector<Size> &sizes)
216 char *sizeStartPtr = (char *)sizesStr;
220 int success = parse_pair(sizeStartPtr, &width, &height, 'x',
222 if (success == -1 || (*sizeStartPtr != ',' && *sizeStartPtr != '\0')) {
223 ALOGE("Picture sizes string \"%s\" contains invalid character.", sizesStr);
226 sizes.push(Size(width, height));
228 if (*sizeStartPtr == '\0') {
235 void CameraParameters2::setPreviewSize(int width, int height)
238 sprintf(str, "%dx%d", width, height);
239 set(CameraParameters::KEY_PREVIEW_SIZE, str);
242 void CameraParameters2::getPreviewSize(int *width, int *height) const
244 *width = *height = -1;
245 // Get the current string, if it doesn't exist, leave the -1x-1
246 const char *p = get(CameraParameters::KEY_PREVIEW_SIZE);
248 parse_pair(p, width, height, 'x');
251 void CameraParameters2::getPreferredPreviewSizeForVideo(int *width, int *height) const
253 *width = *height = -1;
254 const char *p = get(CameraParameters::KEY_PREFERRED_PREVIEW_SIZE_FOR_VIDEO);
256 parse_pair(p, width, height, 'x');
259 void CameraParameters2::getSupportedPreviewSizes(Vector<Size> &sizes) const
261 const char *previewSizesStr = get(CameraParameters::KEY_SUPPORTED_PREVIEW_SIZES);
262 parseSizesList(previewSizesStr, sizes);
265 void CameraParameters2::setVideoSize(int width, int height)
268 sprintf(str, "%dx%d", width, height);
269 set(CameraParameters::KEY_VIDEO_SIZE, str);
272 void CameraParameters2::getVideoSize(int *width, int *height) const
274 *width = *height = -1;
275 const char *p = get(CameraParameters::KEY_VIDEO_SIZE);
277 parse_pair(p, width, height, 'x');
280 void CameraParameters2::getSupportedVideoSizes(Vector<Size> &sizes) const
282 const char *videoSizesStr = get(CameraParameters::KEY_SUPPORTED_VIDEO_SIZES);
283 parseSizesList(videoSizesStr, sizes);
286 void CameraParameters2::setPreviewFrameRate(int fps)
288 set(CameraParameters::KEY_PREVIEW_FRAME_RATE, fps);
291 int CameraParameters2::getPreviewFrameRate() const
293 return getInt(CameraParameters::KEY_PREVIEW_FRAME_RATE);
296 void CameraParameters2::getPreviewFpsRange(int *min_fps, int *max_fps) const
298 *min_fps = *max_fps = -1;
299 const char *p = get(CameraParameters::KEY_PREVIEW_FPS_RANGE);
301 parse_pair(p, min_fps, max_fps, ',');
304 void CameraParameters2::setPreviewFpsRange(int min_fps, int max_fps)
306 String8 str = String8::format("%d,%d", min_fps, max_fps);
307 set(CameraParameters::KEY_PREVIEW_FPS_RANGE, str.string());
310 void CameraParameters2::setPreviewFormat(const char *format)
312 set(CameraParameters::KEY_PREVIEW_FORMAT, format);
315 const char *CameraParameters2::getPreviewFormat() const
317 return get(CameraParameters::KEY_PREVIEW_FORMAT);
320 void CameraParameters2::setPictureSize(int width, int height)
323 sprintf(str, "%dx%d", width, height);
324 set(CameraParameters::KEY_PICTURE_SIZE, str);
327 void CameraParameters2::getPictureSize(int *width, int *height) const
329 *width = *height = -1;
330 // Get the current string, if it doesn't exist, leave the -1x-1
331 const char *p = get(CameraParameters::KEY_PICTURE_SIZE);
333 parse_pair(p, width, height, 'x');
336 void CameraParameters2::getSupportedPictureSizes(Vector<Size> &sizes) const
338 const char *pictureSizesStr = get(CameraParameters::KEY_SUPPORTED_PICTURE_SIZES);
339 parseSizesList(pictureSizesStr, sizes);
342 void CameraParameters2::setPictureFormat(const char *format)
344 set(CameraParameters::KEY_PICTURE_FORMAT, format);
347 const char *CameraParameters2::getPictureFormat() const
349 return get(CameraParameters::KEY_PICTURE_FORMAT);
352 void CameraParameters2::dump() const
354 ALOGD("dump: mMap.size = %zu", mMap.size());
355 for (size_t i = 0; i < mMap.size(); i++) {
359 ALOGD("%s: %s\n", k.string(), v.string());
363 status_t CameraParameters2::dump(int fd, const Vector<String16>& args) const
366 const size_t SIZE = 256;
369 snprintf(buffer, 255, "CameraParameters2::dump: mMap.size = %zu\n", mMap.size());
370 result.append(buffer);
371 for (size_t i = 0; i < mMap.size(); i++) {
375 snprintf(buffer, 255, "\t%s: %s\n", k.string(), v.string());
376 result.append(buffer);
378 write(fd, result.string(), result.size());
382 }; // namespace android