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