camera_test_menu.cpp revision 8dc6e2e3019301a256d3025ecaf266d7a0979871
1#include <stdlib.h>
2#include <unistd.h>
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <fcntl.h>
6#include <time.h>
7#include <semaphore.h>
8#include <pthread.h>
9
10#include <surfaceflinger/Surface.h>
11#include <surfaceflinger/ISurface.h>
12#include <surfaceflinger/ISurfaceComposer.h>
13#include <surfaceflinger/ISurfaceComposerClient.h>
14#include <surfaceflinger/SurfaceComposerClient.h>
15
16#include <camera/Camera.h>
17#include <camera/ICamera.h>
18#include <media/mediarecorder.h>
19
20#include <binder/IPCThreadState.h>
21#include <binder/ProcessState.h>
22#include <binder/IServiceManager.h>
23#include <cutils/properties.h>
24#include <camera/CameraParameters.h>
25#include <system/audio.h>
26#include <system/camera.h>
27
28#include <cutils/memory.h>
29#include <utils/Log.h>
30
31#include <sys/wait.h>
32
33#include "camera_test.h"
34
35using namespace android;
36
37int camera_index = 0;
38int print_menu;
39sp<Camera> camera;
40sp<MediaRecorder> recorder;
41sp<SurfaceComposerClient> client;
42sp<SurfaceControl> surfaceControl;
43sp<Surface> previewSurface;
44CameraParameters params;
45float compensation = 0.0;
46double latitude = 0.0;
47double longitude = 0.0;
48double degree_by_step = 17.5609756;//..0975609756097;
49double altitude = 0.0;
50int awb_mode = 0;
51int effects_mode = 0;
52int scene_mode = 0;
53int caf_mode = 0;
54int vnf_mode = 0;
55int vstab_mode = 0;
56
57int tempBracketRange = 1;
58int tempBracketIdx = 0;
59int measurementIdx = 0;
60int expBracketIdx = 0;
61int AutoConvergenceModeIDX = 0;
62int ManualConvergenceValuesIDX = 0;
63int ManualConvergenceDefaultValueIDX = 2;
64int gbceIDX = 0;
65int rotation = 0;
66bool reSizePreview = true;
67bool hardwareActive = false;
68bool recordingMode = false;
69bool previewRunning = false;
70int saturation = 0;
71int zoomIDX = 0;
72int videoCodecIDX = 0;
73int audioCodecIDX = 0;
74int outputFormatIDX = 0;
75int contrast = 0;
76int brightness = 0;
77unsigned int burst = 0;
78int sharpness = 0;
79int iso_mode = 0;
80int capture_mode = 0;
81int exposure_mode = 0;
82int ippIDX = 0;
83int ippIDX_old = 0;
84int previewFormat = 0;
85int jpegQuality = 85;
86int thumbQuality = 85;
87int flashIdx = 0;
88int fpsRangeIdx = 0;
89timeval autofocus_start, picture_start;
90char script_name[80];
91int prevcnt = 0;
92int videoFd = -1;
93int elockidx = 0;
94int wblockidx = 0;
95
96
97char dir_path[80] = SDCARD_PATH;
98
99const char *cameras[] = {"Primary Camera", "Secondary Camera 1", "Stereo Camera", "USB Camera", "Fake Camera"};
100const char *measurement[] = {"disable", "enable"};
101const char *expBracketing[] = {"disable", "enable"};
102const char *expBracketingRange[] = {"", "-30,0,30,0,-30"};
103const char *tempBracketing[] = {"disable", "enable"};
104const char *faceDetection[] = {"disable", "enable"};
105const char *lock[] = {"false", "true"};
106
107#if defined(OMAP_ENHANCEMENT) && defined(TARGET_OMAP3)
108const char *ipp_mode[] = { "off", "Chroma Suppression", "Edge Enhancement" };
109#else
110const char *ipp_mode[] = { "off", "ldc", "nsf", "ldc-nsf" };
111#endif
112
113const char *iso [] = { "auto", "100", "200", "400", "800", "1200", "1600"};
114
115const char *effects [] = {
116#if defined(OMAP_ENHANCEMENT) && defined(TARGET_OMAP3)
117    "none",
118    "mono",
119    "negative",
120    "solarize",
121    "sepia",
122    "whiteboard",
123    "blackboard",
124    "cool",
125    "emboss"
126#else
127    "none",
128    "mono",
129    "negative",
130    "solarize",
131    "sepia",
132    "vivid",
133    "whiteboard",
134    "blackboard",
135    "cool",
136    "emboss",
137    "blackwhite",
138    "aqua",
139    "posterize"
140#endif
141};
142
143const char CameraParameters::FLASH_MODE_OFF[] = "off";
144const char CameraParameters::FLASH_MODE_AUTO[] = "auto";
145const char CameraParameters::FLASH_MODE_ON[] = "on";
146const char CameraParameters::FLASH_MODE_RED_EYE[] = "red-eye";
147const char CameraParameters::FLASH_MODE_TORCH[] = "torch";
148
149const char *flashModes[] = {
150    "off",
151    "auto",
152    "on",
153    "red-eye",
154    "torch",
155    "fill-in",
156};
157
158const char *caf [] = { "Off", "On" };
159const char *vnf [] = { "Off", "On" };
160const char *vstab [] = { "Off", "On" };
161
162
163const char *scene [] = {
164#if defined(OMAP_ENHANCEMENT) && defined(TARGET_OMAP3)
165    "auto",
166    "portrait",
167    "landscape",
168    "night",
169    "night-portrait",
170    "fireworks",
171    "snow",
172    "action",
173#else
174    "auto",
175    "portrait",
176    "landscape",
177    "night",
178    "night-portrait",
179    "night-indoor",
180    "fireworks",
181    "sport",
182    "cine",
183    "beach",
184    "snow",
185    "mood",
186    "closeup",
187    "underwater",
188    "document",
189    "barcode",
190    "oldfilm",
191    "candlelight",
192    "party",
193    "steadyphoto",
194    "sunset",
195    "action",
196    "theatre"
197#endif
198};
199const char *strawb_mode[] = {
200    "auto",
201    "incandescent",
202    "fluorescent",
203    "daylight",
204    "horizon",
205    "shadow",
206    "tungsten",
207    "shade",
208    "twilight",
209    "warm-fluorescent",
210    "facepriority",
211    "sunset"
212};
213
214size_t length_cam =  ARRAY_SIZE(cameras);
215
216
217preview_size previewSize [] = {
218  { 0,   0,  "NULL"},
219  { 128, 96, "SQCIF" },
220  { 176, 144, "QCIF" },
221  { 352, 288, "CIF" },
222  { 320, 240, "QVGA" },
223  { 352, 288, "CIF" },
224  { 640, 480, "VGA" },
225  { 720, 480, "NTSC" },
226  { 720, 576, "PAL" },
227  { 800, 480, "WVGA" },
228  { 848, 480, "WVGA2"},
229  { 864, 480, "WVGA3"},
230  { 992, 560, "WVGA4"},
231  { 1280, 720, "HD" },
232  { 1920, 1080, "FULLHD"},
233};
234
235size_t length_previewSize =  ARRAY_SIZE(previewSize);
236
237Vcapture_size VcaptureSize [] = {
238  { 128, 96, "SQCIF" },
239  { 176, 144, "QCIF" },
240  { 352, 288, "CIF" },
241  { 320, 240, "QVGA" },
242  { 640, 480, "VGA" },
243  { 704, 480, "TVNTSC" },
244  { 704, 576, "TVPAL" },
245  { 720, 480, "D1NTSC" },
246  { 720, 576, "D1PAL" },
247  { 800, 480, "WVGA" },
248  #if defined(OMAP_ENHANCEMENT) && defined(TARGET_OMAP3)
249  { 848, 480, "WVGA2"},
250  { 864, 480, "WVGA3"},
251  { 992, 560, "WVGA4"},
252  #endif
253  { 1280, 720, "HD" },
254  { 1920, 1080, "FULLHD"},
255};
256
257size_t lenght_Vcapture_size = ARRAY_SIZE(VcaptureSize);
258
259capture_Size captureSize[] = {
260  {  320, 240,  "QVGA" },
261  {  640, 480,  "VGA" },
262  {  800, 600,  "SVGA" },
263  { 1152, 864,  "1MP" },
264  { 1280, 1024, "1.3MP" },
265  { 1600, 1200,  "2MP" },
266  { 2048, 1536,  "3MP" },
267  { 2592, 1944,  "5MP" },
268  { 2608, 1960,  "5MP" },
269  { 3264, 2448,  "8MP" },
270  { 3648, 2736, "10MP"},
271  { 4032, 3024, "12MP"},
272};
273
274size_t length_capture_Size = ARRAY_SIZE(captureSize);
275
276
277outformat outputFormat[] = {
278        { OUTPUT_FORMAT_THREE_GPP, "3gp" },
279        { OUTPUT_FORMAT_MPEG_4, "mp4" },
280    };
281
282size_t length_outformat = ARRAY_SIZE(outputFormat);
283
284video_Codecs videoCodecs[] = {
285  { VIDEO_ENCODER_H263, "H263" },
286  { VIDEO_ENCODER_H264, "H264" },
287  { VIDEO_ENCODER_MPEG_4_SP, "MPEG4"}
288};
289
290size_t length_video_Codecs = ARRAY_SIZE(videoCodecs);
291
292audio_Codecs audioCodecs[] = {
293  { AUDIO_ENCODER_AMR_NB, "AMR_NB" },
294  { AUDIO_ENCODER_AMR_WB, "AMR_WB" },
295  { AUDIO_ENCODER_AAC, "AAC" },
296  { AUDIO_ENCODER_AAC_PLUS, "AAC+" },
297  { AUDIO_ENCODER_EAAC_PLUS, "EAAC+" },
298  { AUDIO_ENCODER_LIST_END, "disabled"},
299};
300
301size_t length_audio_Codecs = ARRAY_SIZE(audioCodecs);
302
303V_bitRate VbitRate[] = {
304  {    64000, "64K"  },
305  {   128000, "128K" },
306  {   192000, "192K" },
307  {   240000, "240K" },
308  {   320000, "320K" },
309  {   360000, "360K" },
310  {   384000, "384K" },
311  {   420000, "420K" },
312  {   768000, "768K" },
313  {  1000000, "1M"   },
314  {  1500000, "1.5M" },
315  {  2000000, "2M"   },
316  {  4000000, "4M"   },
317  {  6000000, "6M"   },
318  {  8000000, "8M"   },
319  { 10000000, "10M"  },
320};
321
322size_t length_V_bitRate = ARRAY_SIZE(VbitRate);
323
324Zoom zoom[] = {
325  { 0,  "1x"  },
326  { 12, "1.5x"},
327  { 20, "2x"  },
328  { 27, "2.5x"},
329  { 32, "3x"  },
330  { 36, "3.5x"},
331  { 40, "4x"  },
332  { 60, "8x"  },
333};
334
335size_t length_Zoom = ARRAY_SIZE(zoom);
336
337fps_ranges fpsRanges[] = {
338  { "5000,30000", "[5:30]" },
339  { "5000,10000", "[5:10]" },
340  { "5000,15000", "[5:15]" },
341  { "5000,20000", "[5:20]" },
342};
343
344size_t length_fps_ranges = ARRAY_SIZE(fpsRanges);
345
346fpsConst_Ranges fpsConstRanges[] = {
347  { "5000,5000", "[5:5]", 5 },
348  { "10000,10000", "[10:10]", 10 },
349  { "15000,15000", "[15:15]", 15 },
350  { "20000,20000", "[20:20]", 20 },
351  { "25000,25000", "[25:25]", 25 },
352  { "30000,30000", "[30:30]", 30 },
353};
354
355size_t length_fpsConst_Ranges = ARRAY_SIZE(fpsConstRanges);
356
357fpsConst_RangesSec fpsConstRangesSec[] = {
358  { "5000,5000", "[5:5]", 5 },
359  { "10000,10000", "[10:10]", 10 },
360  { "15000,15000", "[15:15]", 15 },
361  { "20000,20000", "[20:20]", 20 },
362  { "25000,25000", "[25:25]", 25 },
363  { "27000,27000", "[27:27]", 27 },
364};
365
366size_t length_fpsConst_RangesSec = ARRAY_SIZE(fpsConstRangesSec);
367
368const char *antibanding[] = {
369    "off",
370    "auto",
371    "50hz",
372    "60hz",
373};
374int antibanding_mode = 0;
375const char *focus[] = {
376    "auto",
377    "infinity",
378    "macro",
379    "continuous-video",
380    "extended",
381    "portrait",
382};
383int focus_mode = 0;
384pixel_format pixelformat[] = {
385  { HAL_PIXEL_FORMAT_YCbCr_422_I, CameraParameters::PIXEL_FORMAT_YUV422I },
386  { HAL_PIXEL_FORMAT_YCrCb_420_SP, CameraParameters::PIXEL_FORMAT_YUV420SP },
387  { HAL_PIXEL_FORMAT_RGB_565, CameraParameters::PIXEL_FORMAT_RGB565 },
388  { -1, CameraParameters::PIXEL_FORMAT_JPEG },
389  { -1, "raw" },
390  };
391
392const char *codingformat[] = {"yuv422i-yuyv", "yuv420sp", "rgb565", "jpeg", "raw", "jps", "mpo", "raw+jpeg", "raw+mpo"};
393const char *gbce[] = {"disable", "enable"};
394int pictureFormat = 3; // jpeg
395const char *exposure[] = {"auto", "macro", "portrait", "landscape", "sports", "night", "night-portrait", "backlighting", "manual"};
396const char *capture[] = { "high-performance", "high-quality", "video-mode" };
397const char *autoconvergencemode[] = { "mode-disable", "mode-frame", "mode-center", "mode-fft", "mode-manual" };
398const char *manualconvergencevalues[] = { "-100", "-50", "-30", "-25", "0", "25", "50", "100" };
399
400const struct {
401    int fps;
402} frameRate[] = {
403    {0},
404    {5},
405    {10},
406    {15},
407    {20},
408    {25},
409    {30}
410};
411
412int thumbSizeIDX =  3;
413int previewSizeIDX = ARRAY_SIZE(previewSize) - 1;
414int captureSizeIDX = ARRAY_SIZE(captureSize) - 1;
415int frameRateIDX = ARRAY_SIZE(fpsConstRanges) - 1;
416int frameRateIDXSec = ARRAY_SIZE(fpsConstRangesSec) - 1;
417int VcaptureSizeIDX = ARRAY_SIZE(VcaptureSize) - 1;
418int VbitRateIDX = ARRAY_SIZE(VbitRate) - 1;
419
420static unsigned int recording_counter = 1;
421
422int dump_preview = 0;
423int bufferStarvationTest = 0;
424bool showfps = false;
425
426const char *metering[] = {
427    "center",
428    "average",
429};
430int meter_mode = 0;
431bool bLogSysLinkTrace = true;
432bool stressTest = false;
433bool stopScript = false;
434int restartCount = 0;
435
436/** Calculate delay from a reference time */
437unsigned long long timeval_delay(const timeval *ref) {
438    unsigned long long st, end, delay;
439    timeval current_time;
440
441    gettimeofday(&current_time, 0);
442
443    st = ref->tv_sec * 1000000 + ref->tv_usec;
444    end = current_time.tv_sec * 1000000 + current_time.tv_usec;
445    delay = end - st;
446
447    return delay;
448}
449
450/** Callback for takePicture() */
451void my_raw_callback(const sp<IMemory>& mem) {
452
453    static int      counter = 1;
454    unsigned char   *buff = NULL;
455    int             size;
456    int             fd = -1;
457    char            fn[256];
458
459    LOG_FUNCTION_NAME;
460
461    if (mem == NULL)
462        goto out;
463
464    //Start preview after capture.
465    camera->startPreview();
466
467    fn[0] = 0;
468    sprintf(fn, "/sdcard/img%03d.raw", counter);
469    fd = open(fn, O_CREAT | O_WRONLY | O_SYNC | O_TRUNC, 0777);
470
471    if (fd < 0)
472        goto out;
473
474    size = mem->size();
475
476    if (size <= 0)
477        goto out;
478
479    buff = (unsigned char *)mem->pointer();
480
481    if (!buff)
482        goto out;
483
484    if (size != write(fd, buff, size))
485        printf("Bad Write int a %s error (%d)%s\n", fn, errno, strerror(errno));
486
487    counter++;
488    printf("%s: buffer=%08X, size=%d stored at %s\n",
489           __FUNCTION__, (int)buff, size, fn);
490
491out:
492
493    if (fd >= 0)
494        close(fd);
495
496    LOG_FUNCTION_NAME_EXIT;
497}
498
499void saveFile(const sp<IMemory>& mem) {
500    static int      counter = 1;
501    unsigned char   *buff = NULL;
502    int             size;
503    int             fd = -1;
504    char            fn[256];
505
506    LOG_FUNCTION_NAME;
507
508    if (mem == NULL)
509        goto out;
510
511    fn[0] = 0;
512    sprintf(fn, "/sdcard/preview%03d.yuv", counter);
513    fd = open(fn, O_CREAT | O_WRONLY | O_SYNC | O_TRUNC, 0777);
514    if(fd < 0) {
515        LOGE("Unable to open file %s: %s", fn, strerror(fd));
516        goto out;
517    }
518
519    size = mem->size();
520    if (size <= 0) {
521        LOGE("IMemory object is of zero size");
522        goto out;
523    }
524
525    buff = (unsigned char *)mem->pointer();
526    if (!buff) {
527        LOGE("Buffer pointer is invalid");
528        goto out;
529    }
530
531    if (size != write(fd, buff, size))
532        printf("Bad Write int a %s error (%d)%s\n", fn, errno, strerror(errno));
533
534    counter++;
535    printf("%s: buffer=%08X, size=%d\n",
536           __FUNCTION__, (int)buff, size);
537
538out:
539
540    if (fd >= 0)
541        close(fd);
542
543    LOG_FUNCTION_NAME_EXIT;
544}
545
546
547void debugShowFPS()
548{
549    static int mFrameCount = 0;
550    static int mLastFrameCount = 0;
551    static nsecs_t mLastFpsTime = 0;
552    static float mFps = 0;
553    mFrameCount++;
554    if ( ( mFrameCount % 30 ) == 0 ) {
555        nsecs_t now = systemTime();
556        nsecs_t diff = now - mLastFpsTime;
557        mFps =  ((mFrameCount - mLastFrameCount) * float(s2ns(1))) / diff;
558        mLastFpsTime = now;
559        mLastFrameCount = mFrameCount;
560        printf("####### [%d] Frames, %f FPS", mFrameCount, mFps);
561    }
562}
563
564/** Callback for startPreview() */
565void my_preview_callback(const sp<IMemory>& mem) {
566
567    printf("PREVIEW Callback 0x%x", ( unsigned int ) mem->pointer());
568    if (dump_preview) {
569
570        if(prevcnt==50)
571        saveFile(mem);
572
573        prevcnt++;
574
575        uint8_t *ptr = (uint8_t*) mem->pointer();
576
577        printf("PRV_CB: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x", ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], ptr[6], ptr[7], ptr[8], ptr[9]);
578
579    }
580
581    debugShowFPS();
582}
583
584/** Callback for takePicture() */
585void my_jpeg_callback(const sp<IMemory>& mem) {
586    static int  counter = 1;
587    unsigned char   *buff = NULL;
588    int     size;
589    int     fd = -1;
590    char        fn[256];
591
592    LOG_FUNCTION_NAME;
593
594    //Start preview after capture.
595    camera->startPreview();
596
597    if (mem == NULL)
598        goto out;
599
600    fn[0] = 0;
601    sprintf(fn, "%s/img%03d.jpg", dir_path,counter);
602    fd = open(fn, O_CREAT | O_WRONLY | O_SYNC | O_TRUNC, 0777);
603
604    if(fd < 0) {
605        LOGE("Unable to open file %s: %s", fn, strerror(fd));
606        goto out;
607    }
608
609    size = mem->size();
610    if (size <= 0) {
611        LOGE("IMemory object is of zero size");
612        goto out;
613    }
614
615    buff = (unsigned char *)mem->pointer();
616    if (!buff) {
617        LOGE("Buffer pointer is invalid");
618        goto out;
619    }
620
621    if (size != write(fd, buff, size))
622        printf("Bad Write int a %s error (%d)%s\n", fn, errno, strerror(errno));
623
624    counter++;
625    printf("%s: buffer=%08X, size=%d stored at %s\n",
626           __FUNCTION__, (int)buff, size, fn);
627
628out:
629
630    if (fd >= 0)
631        close(fd);
632
633    LOG_FUNCTION_NAME_EXIT;
634}
635
636void my_face_callback(camera_frame_metadata_t *metadata) {
637    int idx;
638
639    if ( NULL == metadata ) {
640        return;
641    }
642
643    for ( idx = 0 ; idx < metadata->number_of_faces ; idx++ ) {
644        printf("Face %d at %d,%d %d,%d \n",
645               idx,
646               metadata->faces[idx].rect[0],
647               metadata->faces[idx].rect[1],
648               metadata->faces[idx].rect[2],
649               metadata->faces[idx].rect[3]);
650    }
651
652}
653
654void CameraHandler::notify(int32_t msgType, int32_t ext1, int32_t ext2) {
655
656    printf("Notify cb: %d %d %d\n", msgType, ext1, ext2);
657
658    if ( msgType & CAMERA_MSG_FOCUS )
659        printf("AutoFocus %s in %llu us\n", (ext1) ? "OK" : "FAIL", timeval_delay(&autofocus_start));
660
661    if ( msgType & CAMERA_MSG_SHUTTER )
662        printf("Shutter done in %llu us\n", timeval_delay(&picture_start));
663
664    if ( msgType & CAMERA_MSG_ERROR && (ext1 == 1))
665      {
666        printf("Camera Test CAMERA_MSG_ERROR.....\n");
667        if (stressTest)
668          {
669            printf("Camera Test Notified of Error Restarting.....\n");
670            stopScript = true;
671          }
672        else
673          {
674            printf("Camera Test Notified of Error Stopping.....\n");
675            stopScript =false;
676            stopPreview();
677
678            if (recordingMode)
679              {
680                stopRecording();
681                closeRecorder();
682                recordingMode = false;
683              }
684          }
685      }
686}
687
688void CameraHandler::postData(int32_t msgType,
689                             const sp<IMemory>& dataPtr,
690                             camera_frame_metadata_t *metadata) {
691    printf("Data cb: %d\n", msgType);
692
693    if ( msgType & CAMERA_MSG_PREVIEW_FRAME )
694        my_preview_callback(dataPtr);
695
696    if ( msgType & CAMERA_MSG_RAW_IMAGE ) {
697        printf("RAW done in %llu us\n", timeval_delay(&picture_start));
698        my_raw_callback(dataPtr);
699    }
700
701    if (msgType & CAMERA_MSG_POSTVIEW_FRAME) {
702        printf("Postview frame %llu us\n", timeval_delay(&picture_start));
703    }
704
705    if (msgType & CAMERA_MSG_COMPRESSED_IMAGE ) {
706        printf("JPEG done in %llu us\n", timeval_delay(&picture_start));
707        my_jpeg_callback(dataPtr);
708    }
709
710    if ( ( msgType & CAMERA_MSG_PREVIEW_METADATA ) &&
711         ( NULL != metadata ) ) {
712        printf("Face detected %d \n", metadata->number_of_faces);
713        my_face_callback(metadata);
714    }
715}
716
717void CameraHandler::postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr)
718
719{
720    printf("Recording cb: %d %lld %p\n", msgType, timestamp, dataPtr.get());
721
722    static uint32_t count = 0;
723
724    //if(count==100)
725    //saveFile(dataPtr);
726
727    count++;
728
729    uint8_t *ptr = (uint8_t*) dataPtr->pointer();
730
731    printf("VID_CB: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x", ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], ptr[6], ptr[7], ptr[8], ptr[9]);
732
733    camera->releaseRecordingFrame(dataPtr);
734}
735
736int createPreviewSurface(unsigned int width, unsigned int height, int32_t pixFormat) {
737    unsigned int previewWidth, previewHeight;
738
739    if ( MAX_PREVIEW_SURFACE_WIDTH < width ) {
740        previewWidth = MAX_PREVIEW_SURFACE_WIDTH;
741    } else {
742        previewWidth = width;
743    }
744
745    if ( MAX_PREVIEW_SURFACE_HEIGHT < height ) {
746        previewHeight = MAX_PREVIEW_SURFACE_HEIGHT;
747    } else {
748        previewHeight = height;
749    }
750
751    client = new SurfaceComposerClient();
752
753    if ( NULL == client.get() ) {
754        printf("Unable to establish connection to Surface Composer \n");
755
756        return -1;
757    }
758
759    surfaceControl = client->createSurface(0,
760                                           previewWidth,
761                                           previewHeight,
762                                           pixFormat);
763
764    previewSurface = surfaceControl->getSurface();
765
766    client->openGlobalTransaction();
767    surfaceControl->setLayer(0x7fffffff);
768    surfaceControl->setPosition(0, 0);
769    surfaceControl->setSize(previewWidth, previewHeight);
770    surfaceControl->show();
771    client->closeGlobalTransaction();
772
773    return 0;
774}
775
776void printSupportedParams()
777{
778    printf("\n\r\tSupported Cameras: %s", params.get("camera-indexes"));
779    printf("\n\r\tSupported Picture Sizes: %s", params.get(CameraParameters::KEY_SUPPORTED_PICTURE_SIZES));
780    printf("\n\r\tSupported Picture Formats: %s", params.get(CameraParameters::KEY_SUPPORTED_PICTURE_FORMATS));
781    printf("\n\r\tSupported Preview Sizes: %s", params.get(CameraParameters::KEY_SUPPORTED_PREVIEW_SIZES));
782    printf("\n\r\tSupported Preview Formats: %s", params.get(CameraParameters::KEY_SUPPORTED_PREVIEW_FORMATS));
783    printf("\n\r\tSupported Preview Frame Rates: %s", params.get(CameraParameters::KEY_SUPPORTED_PREVIEW_FRAME_RATES));
784    printf("\n\r\tSupported Thumbnail Sizes: %s", params.get(CameraParameters::KEY_SUPPORTED_JPEG_THUMBNAIL_SIZES));
785    printf("\n\r\tSupported Whitebalance Modes: %s", params.get(CameraParameters::KEY_SUPPORTED_WHITE_BALANCE));
786    printf("\n\r\tSupported Effects: %s", params.get(CameraParameters::KEY_SUPPORTED_EFFECTS));
787    printf("\n\r\tSupported Scene Modes: %s", params.get(CameraParameters::KEY_SUPPORTED_SCENE_MODES));
788    printf("\n\r\tSupported Focus Modes: %s", params.get(CameraParameters::KEY_SUPPORTED_FOCUS_MODES));
789    printf("\n\r\tSupported Antibanding Options: %s", params.get(CameraParameters::KEY_SUPPORTED_ANTIBANDING));
790    printf("\n\r\tSupported Flash Modes: %s", params.get(CameraParameters::KEY_SUPPORTED_FLASH_MODES));
791    printf("\n\r\tSupported Focus Areas: %d", params.getInt(CameraParameters::KEY_MAX_NUM_FOCUS_AREAS));
792
793    if ( NULL != params.get(CameraParameters::KEY_FOCUS_DISTANCES) ) {
794        printf("\n\r\tFocus Distances: %s \n", params.get(CameraParameters::KEY_FOCUS_DISTANCES));
795    }
796
797    return;
798}
799
800
801int destroyPreviewSurface() {
802
803    if ( NULL != previewSurface.get() ) {
804        previewSurface.clear();
805    }
806
807    if ( NULL != surfaceControl.get() ) {
808        surfaceControl->clear();
809        surfaceControl.clear();
810    }
811
812    if ( NULL != client.get() ) {
813        client->dispose();
814        client.clear();
815    }
816
817    return 0;
818}
819
820int openRecorder() {
821    recorder = new MediaRecorder();
822
823    if ( NULL == recorder.get() ) {
824        printf("Error while creating MediaRecorder\n");
825
826        return -1;
827    }
828
829    return 0;
830}
831
832int closeRecorder() {
833    if ( NULL == recorder.get() ) {
834        printf("invalid recorder reference\n");
835
836        return -1;
837    }
838
839    if ( recorder->init() < 0 ) {
840        printf("recorder failed to initialize\n");
841
842        return -1;
843    }
844
845    if ( recorder->close() < 0 ) {
846        printf("recorder failed to close\n");
847
848        return -1;
849    }
850
851    if ( recorder->release() < 0 ) {
852        printf("error while releasing recorder\n");
853
854        return -1;
855    }
856
857    recorder.clear();
858
859    return 0;
860}
861
862int configureRecorder() {
863
864    char videoFile[256],vbit_string[50];
865    videoFd = -1;
866
867    if ( ( NULL == recorder.get() ) || ( NULL == camera.get() ) ) {
868        printf("invalid recorder and/or camera references\n");
869
870        return -1;
871    }
872
873    camera->unlock();
874
875    sprintf(vbit_string,"video-param-encoding-bitrate=%u", VbitRate[VbitRateIDX].bit_rate);
876    String8 bit_rate(vbit_string);
877    if ( recorder->setParameters(bit_rate) < 0 ) {
878        printf("error while configuring bit rate\n");
879
880        return -1;
881    }
882
883    if ( recorder->setCamera(camera->remote(), camera->getRecordingProxy()) < 0 ) {
884        printf("error while setting the camera\n");
885
886        return -1;
887    }
888
889    if ( recorder->setVideoSource(VIDEO_SOURCE_CAMERA) < 0 ) {
890        printf("error while configuring camera video source\n");
891
892        return -1;
893    }
894
895
896    if ( AUDIO_ENCODER_LIST_END != audioCodecs[audioCodecIDX].type ) {
897        if ( recorder->setAudioSource(AUDIO_SOURCE_DEFAULT) < 0 ) {
898            printf("error while configuring camera audio source\n");
899
900            return -1;
901        }
902    }
903
904    if ( recorder->setOutputFormat(outputFormat[outputFormatIDX].type) < 0 ) {
905        printf("error while configuring output format\n");
906
907        return -1;
908    }
909
910    if(mkdir("/mnt/sdcard/videos",0777) == -1)
911         printf("\n Directory --videos-- was not created \n");
912    sprintf(videoFile, "/mnt/sdcard/videos/video%d.%s", recording_counter,outputFormat[outputFormatIDX].desc);
913
914    videoFd = open(videoFile, O_CREAT | O_RDWR);
915
916    if(videoFd < 0){
917        printf("Error while creating video filename\n");
918
919        return -1;
920    }
921
922    if ( recorder->setOutputFile(videoFd, 0, 0) < 0 ) {
923        printf("error while configuring video filename\n");
924
925        return -1;
926    }
927
928    recording_counter++;
929
930    if (camera_index == 0) {
931        if ( recorder->setVideoFrameRate(fpsConstRanges[frameRateIDX].constFramerate) < 0 ) {
932            printf("error while configuring video framerate\n");
933            return -1;
934        }
935    }
936    else  {
937        if ( recorder->setVideoFrameRate(fpsConstRangesSec[frameRateIDXSec].constFramerate) < 0 ) {
938            printf("error while configuring video framerate\n");
939            return -1;
940        }
941    }
942
943    if ( recorder->setVideoSize(VcaptureSize[VcaptureSizeIDX].width, VcaptureSize[VcaptureSizeIDX].height) < 0 ) {
944        printf("error while configuring video size\n");
945
946        return -1;
947    }
948
949    if ( recorder->setVideoEncoder(videoCodecs[videoCodecIDX].type) < 0 ) {
950        printf("error while configuring video codec\n");
951
952        return -1;
953    }
954
955    if ( AUDIO_ENCODER_LIST_END != audioCodecs[audioCodecIDX].type ) {
956        if ( recorder->setAudioEncoder(audioCodecs[audioCodecIDX].type) < 0 ) {
957            printf("error while configuring audio codec\n");
958
959            return -1;
960        }
961    }
962
963    if ( recorder->setPreviewSurface( surfaceControl->getSurface() ) < 0 ) {
964        printf("error while configuring preview surface\n");
965
966        return -1;
967    }
968
969    return 0;
970}
971
972int startRecording() {
973    if ( ( NULL == recorder.get() ) || ( NULL == camera.get() ) ) {
974        printf("invalid recorder and/or camera references\n");
975
976        return -1;
977    }
978
979    camera->unlock();
980
981    if ( recorder->prepare() < 0 ) {
982        printf("recorder prepare failed\n");
983
984        return -1;
985    }
986
987    if ( recorder->start() < 0 ) {
988        printf("recorder start failed\n");
989
990        return -1;
991    }
992
993    return 0;
994}
995
996int stopRecording() {
997    if ( NULL == recorder.get() ) {
998        printf("invalid recorder reference\n");
999
1000        return -1;
1001    }
1002
1003    if ( recorder->stop() < 0 ) {
1004        printf("recorder failed to stop\n");
1005
1006        return -1;
1007    }
1008
1009    if ( 0 < videoFd ) {
1010        close(videoFd);
1011    }
1012
1013    return 0;
1014}
1015
1016int openCamera() {
1017    printf("openCamera(camera_index=%d)\n", camera_index);
1018    camera = Camera::connect(camera_index);
1019
1020    if ( NULL == camera.get() ) {
1021        printf("Unable to connect to CameraService\n");
1022        printf("Retrying... \n");
1023        sleep(1);
1024        camera = Camera::connect(camera_index);
1025
1026        if ( NULL == camera.get() ) {
1027            printf("Giving up!! \n");
1028            return -1;
1029        }
1030    }
1031
1032    params = camera->getParameters();
1033    camera->setParameters(params.flatten());
1034
1035    camera->setListener(new CameraHandler());
1036
1037    hardwareActive = true;
1038
1039    return 0;
1040}
1041
1042int closeCamera() {
1043    if ( NULL == camera.get() ) {
1044        printf("invalid camera reference\n");
1045
1046        return -1;
1047    }
1048
1049    camera->disconnect();
1050    camera.clear();
1051
1052    hardwareActive = false;
1053
1054    return 0;
1055}
1056
1057int startPreview() {
1058    int previewWidth, previewHeight;
1059    if (reSizePreview) {
1060
1061        if(recordingMode)
1062        {
1063            previewWidth = VcaptureSize[VcaptureSizeIDX].width;
1064            previewHeight = VcaptureSize[VcaptureSizeIDX].height;
1065        }else
1066        {
1067            previewWidth = previewSize[previewSizeIDX].width;
1068            previewHeight = previewSize[previewSizeIDX].height;
1069        }
1070
1071        if ( createPreviewSurface(previewWidth,
1072                                  previewHeight,
1073                                  pixelformat[previewFormat].pixelFormatDesc) < 0 ) {
1074            printf("Error while creating preview surface\n");
1075            return -1;
1076        }
1077
1078        if ( !hardwareActive ) {
1079            openCamera();
1080        }
1081
1082        params.setPreviewSize(previewWidth, previewHeight);
1083        params.setPictureSize(captureSize[captureSizeIDX].width, captureSize[captureSizeIDX].height);
1084
1085        camera->setParameters(params.flatten());
1086        camera->setPreviewDisplay(previewSurface);
1087
1088        if(!hardwareActive) prevcnt = 0;
1089
1090        camera->startPreview();
1091
1092        previewRunning = true;
1093        reSizePreview = false;
1094
1095    }
1096
1097    return 0;
1098}
1099
1100void stopPreview() {
1101    if ( hardwareActive ) {
1102        camera->stopPreview();
1103
1104        destroyPreviewSurface();
1105
1106        previewRunning  = false;
1107        reSizePreview = true;
1108        closeCamera();
1109    }
1110}
1111
1112void initDefaults() {
1113    camera_index = 0;
1114    antibanding_mode = 0;
1115    focus_mode = 0;
1116    fpsRangeIdx = 0;
1117    previewSizeIDX = 1;  /* Default resolution set to WVGA */
1118    captureSizeIDX = 3;  /* Default capture resolution is 8MP */
1119    frameRateIDX = ARRAY_SIZE(fpsConstRanges) - 1;      /* Default frame rate is 30 FPS */
1120#if defined(OMAP_ENHANCEMENT) && defined(TARGET_OMAP3)
1121    VcaptureSizeIDX = ARRAY_SIZE(VcaptureSize) - 6;/* Default video record is WVGA */
1122#else
1123    VcaptureSizeIDX = ARRAY_SIZE(VcaptureSize) - 2;/* Default video record is WVGA */
1124#endif
1125    VbitRateIDX = ARRAY_SIZE(VbitRate) - 4;        /*Default video bit rate is 4M */
1126    thumbSizeIDX = 0;
1127    compensation = 0.0;
1128    awb_mode = 0;
1129    effects_mode = 0;
1130    scene_mode = 0;
1131    caf_mode = 0;
1132    vnf_mode = 0;
1133    vstab_mode = 0;
1134    expBracketIdx = 0;
1135    flashIdx = 0;
1136    rotation = 0;
1137    zoomIDX = 0;
1138    videoCodecIDX = 0;
1139    gbceIDX = 0;
1140#ifdef TARGET_OMAP4
1141    ///Temporary fix until OMAP3 and OMAP4 3A values are synced
1142    contrast = 90;
1143    brightness = 50;
1144    sharpness = 0;
1145    saturation = 50;
1146#else
1147    contrast = 100;
1148    brightness = 100;
1149    sharpness = 0;
1150    saturation = 100;
1151#endif
1152    iso_mode = 0;
1153    capture_mode = 0;
1154    exposure_mode = 0;
1155    ippIDX = 0;//set the ipp to ldc-nsf as the capture mode is set to HQ by default
1156    ippIDX_old = ippIDX;
1157    jpegQuality = 85;
1158    bufferStarvationTest = 0;
1159    meter_mode = 0;
1160    previewFormat = 1;
1161    pictureFormat = 3; // jpeg
1162    params.setPreviewSize(previewSize[previewSizeIDX].width, previewSize[previewSizeIDX].height);
1163    params.setPictureSize(captureSize[captureSizeIDX].width, captureSize[captureSizeIDX].height);
1164    params.set(CameraParameters::KEY_ROTATION, rotation);
1165    params.set(KEY_COMPENSATION, (int) (compensation * 10));
1166    params.set(params.KEY_WHITE_BALANCE, strawb_mode[awb_mode]);
1167    params.set(KEY_MODE, (capture[capture_mode]));
1168    params.set(params.KEY_SCENE_MODE, scene[scene_mode]);
1169    params.set(KEY_CAF, caf_mode);
1170    params.set(KEY_ISO, iso_mode);
1171    params.set(KEY_GBCE, gbce[gbceIDX]);
1172    params.set(KEY_SHARPNESS, sharpness);
1173    params.set(KEY_CONTRAST, contrast);
1174    params.set(CameraParameters::KEY_ZOOM, zoom[zoomIDX].idx);
1175    params.set(KEY_EXPOSURE, exposure[exposure_mode]);
1176    params.set(KEY_BRIGHTNESS, brightness);
1177    params.set(KEY_SATURATION, saturation);
1178    params.set(params.KEY_EFFECT, effects[effects_mode]);
1179    params.setPreviewFrameRate(frameRate[ARRAY_SIZE(frameRate) - 1].fps);
1180    params.set(params.KEY_ANTIBANDING, antibanding[antibanding_mode]);
1181    params.set(params.KEY_FOCUS_MODE, focus[focus_mode]);
1182    params.set(KEY_IPP, ippIDX);
1183    params.set(CameraParameters::KEY_JPEG_QUALITY, jpegQuality);
1184    params.setPreviewFormat(pixelformat[previewFormat].pixformat);
1185    params.setPictureFormat(codingformat[pictureFormat]);
1186    params.set(KEY_BUFF_STARV, bufferStarvationTest); //enable buffer starvation
1187    params.set(KEY_METERING_MODE, metering[meter_mode]);
1188    params.set(CameraParameters::KEY_JPEG_THUMBNAIL_WIDTH, previewSize[thumbSizeIDX].width);
1189    params.set(CameraParameters::KEY_JPEG_THUMBNAIL_HEIGHT, previewSize[thumbSizeIDX].height);
1190    ManualConvergenceValuesIDX = ManualConvergenceDefaultValueIDX;
1191    params.set(KEY_MANUALCONVERGENCE_VALUES, manualconvergencevalues[ManualConvergenceValuesIDX]);
1192    params.set(KEY_S3D2D_PREVIEW_MODE, "off");
1193    params.set(KEY_STEREO_CAMERA, "false");
1194    params.set(KEY_EXIF_MODEL, MODEL);
1195    params.set(KEY_EXIF_MAKE, MAKE);
1196}
1197
1198int menu_gps() {
1199    char ch;
1200    char coord_str[100];
1201
1202    if (print_menu) {
1203        printf("\n\n== GPS MENU ============================\n\n");
1204        printf("   e. Latitude:       %.7lf\n", latitude);
1205        printf("   d. Longitude:      %.7lf\n", longitude);
1206        printf("   c. Altitude:       %.7lf\n", altitude);
1207        printf("\n");
1208        printf("   q. Return to main menu\n");
1209        printf("\n");
1210        printf("   Choice: ");
1211    }
1212
1213    ch = getchar();
1214    printf("%c", ch);
1215
1216    print_menu = 1;
1217
1218    switch (ch) {
1219
1220        case 'e':
1221            latitude += degree_by_step;
1222
1223            if (latitude > 90.0) {
1224                latitude -= 180.0;
1225            }
1226
1227            snprintf(coord_str, 7, "%.7lf", latitude);
1228            params.set(params.KEY_GPS_LATITUDE, coord_str);
1229
1230            if ( hardwareActive )
1231                camera->setParameters(params.flatten());
1232
1233            break;
1234
1235        case 'd':
1236            longitude += degree_by_step;
1237
1238            if (longitude > 180.0) {
1239                longitude -= 360.0;
1240            }
1241
1242            snprintf(coord_str, 7, "%.7lf", longitude);
1243            params.set(params.KEY_GPS_LONGITUDE, coord_str);
1244
1245            if ( hardwareActive )
1246                camera->setParameters(params.flatten());
1247
1248            break;
1249
1250        case 'c':
1251            altitude += 12345.67890123456789;
1252
1253            if (altitude > 100000.0) {
1254                altitude -= 200000.0;
1255            }
1256
1257            snprintf(coord_str, 100, "%.20lf", altitude);
1258            params.set(params.KEY_GPS_ALTITUDE, coord_str);
1259
1260            if ( hardwareActive )
1261                camera->setParameters(params.flatten());
1262
1263            break;
1264
1265        case 'Q':
1266        case 'q':
1267            return -1;
1268
1269        default:
1270            print_menu = 0;
1271            break;
1272    }
1273
1274    return 0;
1275}
1276
1277int functional_menu() {
1278    char ch;
1279
1280    if (print_menu) {
1281
1282        printf("\n\n=========== FUNCTIONAL TEST MENU ===================\n\n");
1283
1284        printf(" \n\nSTART / STOP / GENERAL SERVICES \n");
1285        printf(" -----------------------------\n");
1286        printf("   A  Select Camera %s\n", cameras[camera_index]);
1287        printf("   [. Resume Preview after capture\n");
1288        printf("   0. Reset to defaults\n");
1289        printf("   q. Quit\n");
1290        printf("   @. Disconnect and Reconnect to CameraService \n");
1291        printf("   /. Enable/Disable showfps: %s\n", ((showfps)? "Enabled":"Disabled"));
1292        printf("   a. GEO tagging settings menu\n");
1293        printf("   E.  Camera Capability Dump");
1294
1295
1296        printf(" \n\n PREVIEW SUB MENU \n");
1297        printf(" -----------------------------\n");
1298        printf("   1. Start Preview\n");
1299        printf("   2. Stop Preview\n");
1300        printf("   ~. Preview format %s\n", pixelformat[previewFormat].pixformat);
1301#if defined(OMAP_ENHANCEMENT) && defined(TARGET_OMAP3)
1302        printf("   4. Preview size:   %4d x %4d - %s\n",previewSize[previewSizeIDX].width,  previewSize[previewSizeIDX].height, previewSize[previewSizeIDX].desc);
1303#else
1304        printf("   4. Preview size:   %4d x %4d - %s\n",previewSize[previewSizeIDX].width, camera_index == 2 ? previewSize[previewSizeIDX].height*2 : previewSize[previewSizeIDX].height, previewSize[previewSizeIDX].desc);
1305#endif
1306        printf("   R. Preview framerate range: %s\n", fpsRanges[fpsRangeIdx].rangeDescription);
1307        printf("   &. Dump a preview frame\n");
1308        printf("   _. Auto Convergence mode: %s\n", autoconvergencemode[AutoConvergenceModeIDX]);
1309        printf("   ^. Manual Convergence Value: %s\n", manualconvergencevalues[ManualConvergenceValuesIDX]);
1310        printf("   {. 2D Preview in 3D Stereo Mode: %s\n", params.get(KEY_S3D2D_PREVIEW_MODE));
1311
1312        printf(" \n\n IMAGE CAPTURE SUB MENU \n");
1313        printf(" -----------------------------\n");
1314        printf("   p. Take picture/Full Press\n");
1315        printf("   H. Exposure Bracketing: %s\n", expBracketing[expBracketIdx]);
1316        printf("   U. Temporal Bracketing:   %s\n", tempBracketing[tempBracketIdx]);
1317        printf("   W. Temporal Bracketing Range: [-%d;+%d]\n", tempBracketRange, tempBracketRange);
1318        printf("   $. Picture Format: %s\n", codingformat[pictureFormat]);
1319        printf("   3. Picture Rotation:       %3d degree\n", rotation );
1320        printf("   5. Picture size:   %4d x %4d - %s\n",captureSize[captureSizeIDX].width, captureSize[captureSizeIDX].height,              captureSize[captureSizeIDX].name);
1321        printf("   i. ISO mode:       %s\n", iso[iso_mode]);
1322        printf("   u. Capture Mode:   %s\n", capture[capture_mode]);
1323        printf("   k. IPP Mode:       %s\n", ipp_mode[ippIDX]);
1324        printf("   K. GBCE: %s\n", gbce[gbceIDX]);
1325        printf("   o. Jpeg Quality:   %d\n", jpegQuality);
1326        printf("   #. Burst Images:  %3d\n", burst);
1327        printf("   :. Thumbnail Size:  %4d x %4d - %s\n",previewSize[thumbSizeIDX].width, previewSize[thumbSizeIDX].height, previewSize[thumbSizeIDX].desc);
1328        printf("   ': Thumbnail Quality %d\n", thumbQuality);
1329
1330        printf(" \n\n VIDEO CAPTURE SUB MENU \n");
1331        printf(" -----------------------------\n");
1332
1333        printf("   6. Start Video Recording\n");
1334        printf("   2. Stop Recording\n");
1335        printf("   l. Video Capture resolution:   %4d x %4d - %s\n",VcaptureSize[VcaptureSizeIDX].width,VcaptureSize[VcaptureSizeIDX].height, VcaptureSize[VcaptureSizeIDX].desc);
1336        printf("   ]. Video Bit rate :  %s\n", VbitRate[VbitRateIDX].desc);
1337        printf("   9. Video Codec:    %s\n", videoCodecs[videoCodecIDX].desc);
1338        printf("   D. Audio Codec:    %s\n", audioCodecs[audioCodecIDX].desc);
1339        printf("   v. Output Format:  %s\n", outputFormat[outputFormatIDX].desc);
1340
1341        if  (camera_index == 1) {
1342            printf("   r. Framerate:     %d\n", fpsConstRangesSec[frameRateIDXSec].constFramerate);
1343        }
1344        else {
1345            printf("   r. Framerate:     %d\n", fpsConstRanges[frameRateIDX].constFramerate);
1346        }
1347        printf("   *. Start Video Recording dump ( 1 raw frame ) \n");
1348        printf("   B  VNF              %s \n", vnf[vnf_mode]);
1349        printf("   C  VSTAB              %s", vstab[vstab_mode]);
1350
1351        printf(" \n\n 3A SETTING SUB MENU \n");
1352        printf(" -----------------------------\n");
1353
1354        printf("   M. Measurement Data: %s\n", measurement[measurementIdx]);
1355        printf("   F. Start face detection \n");
1356        printf("   T. Stop face detection \n");
1357        printf("   G. Touch/Focus area AF\n");
1358        printf("   f. Auto Focus/Half Press\n");
1359        printf("   J.Flash:              %s\n", flashModes[flashIdx]);
1360        printf("   7. EV offset:      %4.1f\n", compensation);
1361        printf("   8. AWB mode:       %s\n", strawb_mode[awb_mode]);
1362        printf("   z. Zoom            %s\n", zoom[zoomIDX].zoom_description);
1363        printf("   j. Exposure        %s\n", exposure[exposure_mode]);
1364        printf("   e. Effect:         %s\n", effects[effects_mode]);
1365        printf("   w. Scene:          %s\n", scene[scene_mode]);
1366        printf("   s. Saturation:     %d\n", saturation);
1367        printf("   c. Contrast:       %d\n", contrast);
1368        printf("   h. Sharpness:      %d\n", sharpness);
1369        printf("   b. Brightness:     %d\n", brightness);
1370        printf("   x. Antibanding:    %s\n", antibanding[antibanding_mode]);
1371        printf("   g. Focus mode:     %s\n", focus[focus_mode]);
1372        printf("   m. Metering mode:     %s\n" , metering[meter_mode]);
1373        printf("   <. Exposure Lock:     %s\n", lock[elockidx]);
1374        printf("   >. WhiteBalance Lock:  %s\n",lock[wblockidx]);
1375        printf("\n");
1376        printf("   Choice: ");
1377    }
1378
1379    ch = getchar();
1380    printf("%c", ch);
1381
1382    print_menu = 1;
1383
1384    switch (ch) {
1385
1386    case '_':
1387        AutoConvergenceModeIDX++;
1388        AutoConvergenceModeIDX %= ARRAY_SIZE(autoconvergencemode);
1389        params.set(KEY_AUTOCONVERGENCE, autoconvergencemode[AutoConvergenceModeIDX]);
1390        if ( strcmp (autoconvergencemode[AutoConvergenceModeIDX], AUTOCONVERGENCE_MODE_MANUAL) == 0) {
1391            params.set(KEY_MANUALCONVERGENCE_VALUES, manualconvergencevalues[ManualConvergenceValuesIDX]);
1392        }
1393        else {
1394            params.set(KEY_MANUALCONVERGENCE_VALUES, manualconvergencevalues[ManualConvergenceDefaultValueIDX]);
1395            ManualConvergenceValuesIDX = ManualConvergenceDefaultValueIDX;
1396        }
1397        camera->setParameters(params.flatten());
1398
1399        break;
1400    case '^':
1401        if ( strcmp (autoconvergencemode[AutoConvergenceModeIDX], AUTOCONVERGENCE_MODE_MANUAL) == 0) {
1402            ManualConvergenceValuesIDX++;
1403            ManualConvergenceValuesIDX %= ARRAY_SIZE(manualconvergencevalues);
1404            params.set(KEY_MANUALCONVERGENCE_VALUES, manualconvergencevalues[ManualConvergenceValuesIDX]);
1405            camera->setParameters(params.flatten());
1406        }
1407        break;
1408    case 'A':
1409        camera_index++;
1410        camera_index %= ARRAY_SIZE(cameras);
1411        if ( camera_index == 2) {
1412            params.set(KEY_STEREO_CAMERA, "true");
1413        } else {
1414            params.set(KEY_STEREO_CAMERA, "false");
1415        }
1416        closeCamera();
1417
1418        openCamera();
1419
1420        if (camera_index == 0) {
1421            params.setPreviewFrameRate(30);
1422        } else {
1423            params.setPreviewFrameRate(27);
1424        }
1425
1426
1427        break;
1428    case '[':
1429        if ( hardwareActive ) {
1430            camera->setParameters(params.flatten());
1431            camera->startPreview();
1432        }
1433        break;
1434
1435    case '0':
1436        initDefaults();
1437        break;
1438
1439        case '1':
1440
1441            if ( startPreview() < 0 ) {
1442                printf("Error while starting preview\n");
1443
1444                return -1;
1445            }
1446
1447            break;
1448
1449        case '2':
1450            stopPreview();
1451
1452            if ( recordingMode ) {
1453                camera->disconnect();
1454                camera.clear();
1455                stopRecording();
1456                closeRecorder();
1457
1458                camera = Camera::connect(camera_index);
1459                  if ( NULL == camera.get() ) {
1460                      sleep(1);
1461                      camera = Camera::connect(camera_index);
1462                      if ( NULL == camera.get() ) {
1463                          return -1;
1464                      }
1465                  }
1466                  camera->setListener(new CameraHandler());
1467                  camera->setParameters(params.flatten());
1468                  recordingMode = false;
1469            }
1470
1471            break;
1472
1473        case '3':
1474            rotation += 90;
1475            rotation %= 360;
1476            params.set(CameraParameters::KEY_ROTATION, rotation);
1477            if ( hardwareActive )
1478                camera->setParameters(params.flatten());
1479
1480            break;
1481
1482        case '4':
1483            previewSizeIDX += 1;
1484            previewSizeIDX %= ARRAY_SIZE(previewSize);
1485            if ( strcmp(params.get(KEY_STEREO_CAMERA), "false") == 0 ) {
1486                params.setPreviewSize(previewSize[previewSizeIDX].width, previewSize[previewSizeIDX].height);
1487            } else {
1488                params.setPreviewSize(previewSize[previewSizeIDX].width, previewSize[previewSizeIDX].height*2);
1489            }
1490            reSizePreview = true;
1491
1492            if ( hardwareActive && previewRunning ) {
1493                camera->stopPreview();
1494                camera->setParameters(params.flatten());
1495                camera->startPreview();
1496            } else if ( hardwareActive ) {
1497                camera->setParameters(params.flatten());
1498            }
1499
1500            break;
1501
1502        case '5':
1503            captureSizeIDX += 1;
1504            captureSizeIDX %= ARRAY_SIZE(captureSize);
1505            params.setPictureSize(captureSize[captureSizeIDX].width, captureSize[captureSizeIDX].height);
1506
1507            if ( hardwareActive )
1508                camera->setParameters(params.flatten());
1509            break;
1510
1511        case 'l':
1512        case 'L':
1513            VcaptureSizeIDX++;
1514            VcaptureSizeIDX %= ARRAY_SIZE(VcaptureSize);
1515            break;
1516
1517        case ']':
1518            VbitRateIDX++;
1519            VbitRateIDX %= ARRAY_SIZE(VbitRate);
1520            break;
1521
1522
1523        case '6':
1524
1525            if ( !recordingMode ) {
1526
1527                recordingMode = true;
1528
1529                if ( startPreview() < 0 ) {
1530                    printf("Error while starting preview\n");
1531
1532                    return -1;
1533                }
1534
1535                if ( openRecorder() < 0 ) {
1536                    printf("Error while openning video recorder\n");
1537
1538                    return -1;
1539                }
1540
1541                if ( configureRecorder() < 0 ) {
1542                    printf("Error while configuring video recorder\n");
1543
1544                    return -1;
1545                }
1546
1547                if ( startRecording() < 0 ) {
1548                    printf("Error while starting video recording\n");
1549
1550                    return -1;
1551                }
1552            }
1553
1554            break;
1555
1556        case '7':
1557
1558            if ( compensation > 2.0) {
1559                compensation = -2.0;
1560            } else {
1561                compensation += 0.1;
1562            }
1563
1564            params.set(KEY_COMPENSATION, (int) (compensation * 10));
1565
1566            if ( hardwareActive )
1567                camera->setParameters(params.flatten());
1568
1569            break;
1570
1571        case '8':
1572            awb_mode++;
1573            awb_mode %= ARRAY_SIZE(strawb_mode);
1574            params.set(params.KEY_WHITE_BALANCE, strawb_mode[awb_mode]);
1575
1576            if ( hardwareActive )
1577                camera->setParameters(params.flatten());
1578
1579            break;
1580
1581        case '9':
1582            videoCodecIDX++;
1583            videoCodecIDX %= ARRAY_SIZE(videoCodecs);
1584            break;
1585        case '~':
1586            previewFormat += 1;
1587            previewFormat %= ARRAY_SIZE(pixelformat) - 1;
1588            params.setPreviewFormat(pixelformat[previewFormat].pixformat);
1589
1590            if ( hardwareActive )
1591                camera->setParameters(params.flatten());
1592
1593            break;
1594        case '$':
1595            pictureFormat += 1;
1596            if ( strcmp(params.get(KEY_STEREO_CAMERA), "false") == 0 && pictureFormat > 4 )
1597                pictureFormat = 0;
1598            pictureFormat %= ARRAY_SIZE(codingformat);
1599            params.setPictureFormat(codingformat[pictureFormat]);
1600            if ( hardwareActive )
1601                camera->setParameters(params.flatten());
1602
1603            break;
1604
1605        case '?' :
1606            ///Set mode=3 to select video mode
1607            params.set(KEY_MODE, 3);
1608            params.set(KEY_VNF, 1);
1609            params.set(KEY_VSTAB, 1);
1610            break;
1611
1612        case ':':
1613            thumbSizeIDX += 1;
1614            thumbSizeIDX %= ARRAY_SIZE(previewSize);
1615            params.set(CameraParameters::KEY_JPEG_THUMBNAIL_WIDTH, previewSize[thumbSizeIDX].width);
1616            params.set(CameraParameters::KEY_JPEG_THUMBNAIL_HEIGHT, previewSize[thumbSizeIDX].height);
1617
1618            if ( hardwareActive )
1619                camera->setParameters(params.flatten());
1620
1621            break;
1622
1623        case '\'':
1624            if ( thumbQuality >= 100) {
1625                thumbQuality = 0;
1626            } else {
1627                thumbQuality += 5;
1628            }
1629
1630            params.set(CameraParameters::KEY_JPEG_THUMBNAIL_QUALITY, thumbQuality);
1631            if ( hardwareActive )
1632                camera->setParameters(params.flatten());
1633            break;
1634
1635        case 'B' :
1636            vnf_mode++;
1637            vnf_mode %= ARRAY_SIZE(vnf);
1638            params.set(KEY_VNF, vnf_mode);
1639
1640            if ( hardwareActive )
1641                camera->setParameters(params.flatten());
1642            break;
1643
1644        case 'C' :
1645            vstab_mode++;
1646            vstab_mode %= ARRAY_SIZE(vstab);
1647            params.set(KEY_VSTAB, vstab_mode);
1648
1649            if ( hardwareActive )
1650                camera->setParameters(params.flatten());
1651            break;
1652
1653        case 'E':
1654            if(hardwareActive)
1655                params.unflatten(camera->getParameters());
1656            printSupportedParams();
1657            break;
1658
1659        case '*':
1660            if ( hardwareActive )
1661                camera->startRecording();
1662            break;
1663
1664        case 'o':
1665            if ( jpegQuality >= 100) {
1666                jpegQuality = 0;
1667            } else {
1668                jpegQuality += 5;
1669            }
1670
1671            params.set(CameraParameters::KEY_JPEG_QUALITY, jpegQuality);
1672            if ( hardwareActive )
1673                camera->setParameters(params.flatten());
1674            break;
1675
1676        case 'M':
1677            measurementIdx = (measurementIdx + 1)%ARRAY_SIZE(measurement);
1678            params.set(KEY_MEASUREMENT, measurement[measurementIdx]);
1679            if ( hardwareActive )
1680                camera->setParameters(params.flatten());
1681            break;
1682        case 'm':
1683        {
1684            meter_mode = (meter_mode + 1)%ARRAY_SIZE(metering);
1685            params.set(KEY_METERING_MODE, metering[meter_mode]);
1686            if ( hardwareActive )
1687                camera->setParameters(params.flatten());
1688            break;
1689        }
1690
1691        case 'k':
1692            ippIDX += 1;
1693            ippIDX %= ARRAY_SIZE(ipp_mode);
1694            ippIDX_old = ippIDX;
1695
1696#if defined(OMAP_ENHANCEMENT) && defined(TARGET_OMAP3)
1697            params.set(KEY_IPP, ipp_mode[ippIDX]);
1698#else
1699            params.set(KEY_IPP, ippIDX);
1700#endif
1701
1702            if ( hardwareActive )
1703                camera->setParameters(params.flatten());
1704
1705            break;
1706
1707        case 'K':
1708            gbceIDX+= 1;
1709            gbceIDX %= ARRAY_SIZE(gbce);
1710            params.set(KEY_GBCE, gbce[gbceIDX]);
1711
1712            if ( hardwareActive )
1713                camera->setParameters(params.flatten());
1714            break;
1715
1716        case 'F':
1717            if ( hardwareActive )
1718                camera->sendCommand(CAMERA_CMD_START_FACE_DETECTION, 0, 0);
1719
1720            break;
1721
1722        case 'T':
1723
1724            if ( hardwareActive )
1725                camera->sendCommand(CAMERA_CMD_STOP_FACE_DETECTION, 0, 0);
1726
1727            break;
1728
1729        case '@':
1730            if ( hardwareActive ) {
1731
1732                closeCamera();
1733
1734                if ( 0 >= openCamera() ) {
1735                    printf( "Reconnected to CameraService \n");
1736                }
1737            }
1738
1739            break;
1740
1741        case '#':
1742
1743            if ( burst >= MAX_BURST ) {
1744                burst = 0;
1745            } else {
1746                burst += BURST_INC;
1747            }
1748            params.set(KEY_BURST, burst);
1749
1750            if ( hardwareActive )
1751                camera->setParameters(params.flatten());
1752
1753            break;
1754
1755        case 'J':
1756            flashIdx++;
1757            flashIdx %= ARRAY_SIZE(flashModes);
1758            params.set(CameraParameters::KEY_FLASH_MODE, (flashModes[flashIdx]));
1759
1760            if ( hardwareActive )
1761                camera->setParameters(params.flatten());
1762
1763            break;
1764
1765        case 'u':
1766            capture_mode++;
1767            capture_mode %= ARRAY_SIZE(capture);
1768
1769            // HQ should always be in ldc-nsf
1770            // if not HQ, then return the ipp to its previous state
1771            if( !strcmp(capture[capture_mode], "high-quality") ) {
1772                ippIDX_old = ippIDX;
1773                ippIDX = 3;
1774                params.set(KEY_IPP, ipp_mode[ippIDX]);
1775            } else {
1776                ippIDX = ippIDX_old;
1777            }
1778
1779            params.set(KEY_MODE, (capture[capture_mode]));
1780
1781            if ( hardwareActive )
1782                camera->setParameters(params.flatten());
1783
1784            break;
1785
1786        case 'U':
1787            tempBracketIdx++;
1788            tempBracketIdx %= ARRAY_SIZE(tempBracketing);
1789            params.set(KEY_TEMP_BRACKETING, tempBracketing[tempBracketIdx]);
1790
1791            if ( hardwareActive )
1792                camera->setParameters(params.flatten());
1793
1794            break;
1795
1796        case 'H':
1797            expBracketIdx++;
1798            expBracketIdx %= ARRAY_SIZE(expBracketing);
1799
1800            params.set(KEY_EXP_BRACKETING_RANGE, expBracketingRange[expBracketIdx]);
1801
1802            if ( hardwareActive )
1803                camera->setParameters(params.flatten());
1804
1805            break;
1806
1807        case 'W':
1808            tempBracketRange++;
1809            tempBracketRange %= TEMP_BRACKETING_MAX_RANGE;
1810            if ( 0 == tempBracketRange ) {
1811                tempBracketRange = 1;
1812            }
1813
1814            params.set(KEY_TEMP_BRACKETING_NEG, tempBracketRange);
1815            params.set(KEY_TEMP_BRACKETING_POS, tempBracketRange);
1816
1817            if ( hardwareActive )
1818                camera->setParameters(params.flatten());
1819
1820            break;
1821
1822        case 'w':
1823            scene_mode++;
1824            scene_mode %= ARRAY_SIZE(scene);
1825            params.set(params.KEY_SCENE_MODE, scene[scene_mode]);
1826
1827            if ( hardwareActive )
1828                camera->setParameters(params.flatten());
1829
1830            break;
1831
1832        case 'i':
1833            iso_mode++;
1834            iso_mode %= ARRAY_SIZE(iso);
1835            params.set(KEY_ISO, iso[iso_mode]);
1836
1837            if ( hardwareActive )
1838                camera->setParameters(params.flatten());
1839            break;
1840
1841        case 'h':
1842            if ( sharpness >= 100) {
1843                sharpness = 0;
1844            } else {
1845                sharpness += 10;
1846            }
1847            params.set(KEY_SHARPNESS, sharpness);
1848            if ( hardwareActive )
1849                camera->setParameters(params.flatten());
1850            break;
1851
1852        case 'D':
1853        {
1854            audioCodecIDX++;
1855            audioCodecIDX %= ARRAY_SIZE(audioCodecs);
1856            break;
1857        }
1858
1859        case 'v':
1860        {
1861            outputFormatIDX++;
1862            outputFormatIDX %= ARRAY_SIZE(outputFormat);
1863            break;
1864        }
1865
1866        case 'z':
1867            zoomIDX++;
1868            zoomIDX %= ARRAY_SIZE(zoom);
1869            params.set(CameraParameters::KEY_ZOOM, zoom[zoomIDX].idx);
1870
1871            if ( hardwareActive )
1872                camera->setParameters(params.flatten());
1873
1874            break;
1875
1876        case 'j':
1877            exposure_mode++;
1878            exposure_mode %= ARRAY_SIZE(exposure);
1879            params.set(KEY_EXPOSURE, exposure[exposure_mode]);
1880
1881            if ( hardwareActive )
1882                camera->setParameters(params.flatten());
1883
1884            break;
1885
1886        case 'c':
1887            if( contrast >= 200){
1888                contrast = 0;
1889            } else {
1890                contrast += 10;
1891            }
1892            params.set(KEY_CONTRAST, contrast);
1893            if ( hardwareActive )
1894                camera->setParameters(params.flatten());
1895            break;
1896        case 'b':
1897            if ( brightness >= 200) {
1898                brightness = 0;
1899            } else {
1900                brightness += 10;
1901            }
1902
1903            params.set(KEY_BRIGHTNESS, brightness);
1904
1905            if ( hardwareActive )
1906                camera->setParameters(params.flatten());
1907
1908            break;
1909
1910        case 's':
1911        case 'S':
1912            if ( saturation >= 100) {
1913                saturation = 0;
1914            } else {
1915                saturation += 10;
1916            }
1917
1918            params.set(KEY_SATURATION, saturation);
1919
1920            if ( hardwareActive )
1921                camera->setParameters(params.flatten());
1922
1923            break;
1924
1925        case 'e':
1926            effects_mode++;
1927            effects_mode %= ARRAY_SIZE(effects);
1928            params.set(params.KEY_EFFECT, effects[effects_mode]);
1929
1930            if ( hardwareActive )
1931                camera->setParameters(params.flatten());
1932
1933            break;
1934
1935        case 'r':
1936
1937
1938            if (camera_index == 0) {
1939                frameRateIDX += 1;
1940                frameRateIDX %= ARRAY_SIZE(fpsConstRanges);
1941                params.set(CameraParameters::KEY_PREVIEW_FPS_RANGE, fpsConstRanges[frameRateIDX].range);
1942            } else
1943            {
1944                frameRateIDXSec += 1;
1945                frameRateIDXSec %= ARRAY_SIZE(fpsConstRangesSec);
1946                params.set(CameraParameters::KEY_PREVIEW_FPS_RANGE, fpsConstRangesSec[frameRateIDXSec].range);
1947
1948
1949            }
1950
1951            if ( hardwareActive ) {
1952                camera->setParameters(params.flatten());
1953            }
1954
1955            break;
1956
1957        case 'R':
1958            fpsRangeIdx += 1;
1959            fpsRangeIdx %= ARRAY_SIZE(fpsRanges);
1960            params.set(CameraParameters::KEY_PREVIEW_FPS_RANGE, fpsRanges[fpsRangeIdx].range);
1961
1962            if ( hardwareActive ) {
1963                camera->setParameters(params.flatten());
1964            }
1965
1966            break;
1967
1968        case 'x':
1969            antibanding_mode++;
1970            antibanding_mode %= ARRAY_SIZE(antibanding);
1971            params.set(params.KEY_ANTIBANDING, antibanding[antibanding_mode]);
1972
1973            if ( hardwareActive )
1974                camera->setParameters(params.flatten());
1975
1976            break;
1977
1978        case 'g':
1979            focus_mode++;
1980            focus_mode %= ARRAY_SIZE(focus);
1981            params.set(params.KEY_FOCUS_MODE, focus[focus_mode]);
1982
1983            if ( hardwareActive )
1984                camera->setParameters(params.flatten());
1985
1986            break;
1987
1988        case 'G':
1989
1990            params.set(CameraParameters::KEY_FOCUS_AREAS, TEST_FOCUS_AREA);
1991
1992            if ( hardwareActive )
1993                camera->setParameters(params.flatten());
1994
1995            params.remove(CameraParameters::KEY_FOCUS_AREAS);
1996
1997        case 'f':
1998
1999            gettimeofday(&autofocus_start, 0);
2000
2001            if ( hardwareActive )
2002                camera->autoFocus();
2003
2004            break;
2005
2006        case 'p':
2007
2008            gettimeofday(&picture_start, 0);
2009
2010            if ( hardwareActive )
2011                camera->takePicture(CAMERA_MSG_COMPRESSED_IMAGE|CAMERA_MSG_RAW_IMAGE);
2012
2013            break;
2014
2015        case '&':
2016            printf("Enabling Preview Callback");
2017            dump_preview = 1;
2018            if ( hardwareActive )
2019            camera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK);
2020            break;
2021
2022        case '{':
2023            if ( strcmp(params.get(KEY_S3D2D_PREVIEW_MODE), "off") == 0 )
2024                {
2025                params.set(KEY_S3D2D_PREVIEW_MODE, "on");
2026                }
2027            else
2028                {
2029                params.set(KEY_S3D2D_PREVIEW_MODE, "off");
2030                }
2031            if ( hardwareActive )
2032                camera->setParameters(params.flatten());
2033            break;
2034
2035        case 'a':
2036
2037            while (1) {
2038                if ( menu_gps() < 0)
2039                    break;
2040            };
2041
2042            break;
2043
2044        case 'q':
2045
2046            stopPreview();
2047
2048            return -1;
2049
2050        case '/':
2051        {
2052            if (showfps)
2053            {
2054                property_set("debug.image.showfps", "0");
2055                showfps = false;
2056            }
2057            else
2058            {
2059                property_set("debug.image.showfps", "1");
2060                showfps = true;
2061            }
2062            break;
2063        }
2064
2065    case '<':
2066      elockidx += 1;
2067      elockidx %= ARRAY_SIZE(lock);
2068      params.set(KEY_AUTO_EXPOSURE_LOCK, lock[elockidx]);
2069      if ( hardwareActive )
2070        camera->setParameters(params.flatten());
2071      break;
2072
2073    case '>':
2074      wblockidx += 1;
2075      wblockidx %= ARRAY_SIZE(lock);
2076      params.set(KEY_AUTO_WHITEBALANCE_LOCK, lock[wblockidx]);
2077      if ( hardwareActive )
2078        camera->setParameters(params.flatten());
2079      break;
2080
2081        default:
2082            print_menu = 0;
2083
2084            break;
2085    }
2086
2087    return 0;
2088}
2089
2090void print_usage() {
2091    printf(" USAGE: camera_test  <param>  <script>\n");
2092    printf(" <param>\n-----------\n\n");
2093    printf(" F or f -> Functional tests \n");
2094    printf(" A or a -> API tests \n");
2095    printf(" E or e -> Error scenario tests \n");
2096    printf(" S or s -> Stress tests; with syslink trace \n");
2097    printf(" SN or sn -> Stress tests; No syslink trace \n\n");
2098    printf(" <script>\n----------\n");
2099    printf("Script name (Only for stress tests)\n\n");
2100    return;
2101}
2102
2103int error_scenario() {
2104    char ch;
2105    status_t stat = NO_ERROR;
2106
2107    if (print_menu) {
2108        printf("   0. Buffer need\n");
2109        printf("   1. Not enough memory\n");
2110        printf("   2. Media server crash\n");
2111        printf("   3. Overlay object request\n");
2112        printf("   4. Pass unsupported preview&picture format\n");
2113        printf("   5. Pass unsupported preview&picture resolution\n");
2114        printf("   6. Pass unsupported preview framerate\n");
2115
2116        printf("   q. Quit\n");
2117        printf("   Choice: ");
2118    }
2119
2120    print_menu = 1;
2121    ch = getchar();
2122    printf("%c\n", ch);
2123
2124    switch (ch) {
2125        case '0': {
2126            printf("Case0:Buffer need\n");
2127            bufferStarvationTest = 1;
2128            params.set(KEY_BUFF_STARV, bufferStarvationTest); //enable buffer starvation
2129
2130            if ( !recordingMode ) {
2131                recordingMode = true;
2132                if ( startPreview() < 0 ) {
2133                    printf("Error while starting preview\n");
2134
2135                    return -1;
2136                }
2137
2138                if ( openRecorder() < 0 ) {
2139                    printf("Error while openning video recorder\n");
2140
2141                    return -1;
2142                }
2143
2144                if ( configureRecorder() < 0 ) {
2145                    printf("Error while configuring video recorder\n");
2146
2147                    return -1;
2148                }
2149
2150                if ( startRecording() < 0 ) {
2151                    printf("Error while starting video recording\n");
2152
2153                    return -1;
2154                }
2155
2156            }
2157
2158            usleep(1000000);//1s
2159
2160            stopPreview();
2161
2162            if ( recordingMode ) {
2163                stopRecording();
2164                closeRecorder();
2165
2166                recordingMode = false;
2167            }
2168
2169            break;
2170        }
2171
2172        case '1': {
2173            printf("Case1:Not enough memory\n");
2174            int* tMemoryEater = new int[999999999];
2175
2176            if (!tMemoryEater) {
2177                printf("Not enough memory\n");
2178                return -1;
2179            } else {
2180                delete tMemoryEater;
2181            }
2182
2183            break;
2184        }
2185
2186        case '2': {
2187            printf("Case2:Media server crash\n");
2188            //camera = Camera::connect();
2189
2190            if ( NULL == camera.get() ) {
2191                printf("Unable to connect to CameraService\n");
2192                return -1;
2193            }
2194
2195            break;
2196        }
2197
2198        case '3': {
2199            printf("Case3:Overlay object request\n");
2200            int err = 0;
2201
2202            err = open("/dev/video5", O_RDWR);
2203
2204            if (err < 0) {
2205                printf("Could not open the camera device5: %d\n",  err );
2206                return err;
2207            }
2208
2209            if ( startPreview() < 0 ) {
2210                printf("Error while starting preview\n");
2211                return -1;
2212            }
2213
2214            usleep(1000000);//1s
2215
2216            stopPreview();
2217
2218            close(err);
2219            break;
2220        }
2221
2222        case '4': {
2223
2224            if ( hardwareActive ) {
2225
2226                params.setPictureFormat("invalid-format");
2227                params.setPreviewFormat("invalid-format");
2228
2229                stat = camera->setParameters(params.flatten());
2230
2231                if ( NO_ERROR != stat ) {
2232                    printf("Test passed!\n");
2233                } else {
2234                    printf("Test failed!\n");
2235                }
2236
2237                initDefaults();
2238            }
2239
2240            break;
2241        }
2242
2243        case '5': {
2244
2245            if ( hardwareActive ) {
2246
2247                params.setPictureSize(-1, -1);
2248                params.setPreviewSize(-1, -1);
2249
2250                stat = camera->setParameters(params.flatten());
2251
2252                if ( NO_ERROR != stat ) {
2253                    printf("Test passed!\n");
2254                } else {
2255                    printf("Test failed!\n");
2256                }
2257
2258                initDefaults();
2259            }
2260
2261            break;
2262        }
2263
2264        case '6': {
2265
2266            if ( hardwareActive ) {
2267
2268                params.setPreviewFrameRate(-1);
2269
2270                stat = camera->setParameters(params.flatten());
2271
2272                if ( NO_ERROR != stat ) {
2273                    printf("Test passed!\n");
2274                } else {
2275                    printf("Test failed!\n");
2276                }
2277
2278                initDefaults();
2279            }
2280
2281
2282            break;
2283        }
2284
2285        case 'q': {
2286            return -1;
2287        }
2288
2289        default: {
2290            print_menu = 0;
2291            break;
2292        }
2293    }
2294
2295    return 0;
2296}
2297
2298int restartCamera() {
2299
2300  const char dir_path_name[80] = SDCARD_PATH;
2301
2302  printf("+++Restarting Camera After Error+++\n");
2303  stopPreview();
2304
2305  if (recordingMode) {
2306    stopRecording();
2307    closeRecorder();
2308
2309    recordingMode = false;
2310  }
2311
2312  sleep(3); //Wait a bit before restarting
2313
2314  restartCount++;
2315
2316  if (strcpy(dir_path, dir_path_name) == NULL)
2317  {
2318    printf("Error reseting dir name");
2319    return -1;
2320  }
2321
2322  if ( openCamera() < 0 )
2323  {
2324    printf("+++Camera Restarted Failed+++\n");
2325    system("echo camerahal_test > /sys/power/wake_unlock");
2326    return -1;
2327  }
2328
2329  initDefaults();
2330
2331  stopScript = false;
2332
2333  printf("+++Camera Restarted Successfully+++\n");
2334  return 0;
2335}
2336
2337int main(int argc, char *argv[]) {
2338    char *cmd;
2339    int pid;
2340    sp<ProcessState> proc(ProcessState::self());
2341
2342    unsigned long long st, end, delay;
2343    timeval current_time;
2344
2345    gettimeofday(&current_time, 0);
2346
2347    st = current_time.tv_sec * 1000000 + current_time.tv_usec;
2348
2349    cmd = NULL;
2350
2351    if ( argc < 2 ) {
2352        printf(" Please enter atleast 1 argument\n");
2353        print_usage();
2354
2355        return 0;
2356    }
2357    system("echo camerahal_test > /sys/power/wake_lock");
2358    if ( argc < 3 ) {
2359        switch (*argv[1]) {
2360            case 'S':
2361            case 's':
2362                printf("This is stress / regression tests \n");
2363                printf("Provide script file as 2nd argument\n");
2364
2365                break;
2366
2367            case 'F':
2368            case 'f':
2369                ProcessState::self()->startThreadPool();
2370
2371                if ( openCamera() < 0 ) {
2372                    printf("Camera initialization failed\n");
2373                    system("echo camerahal_test > /sys/power/wake_unlock");
2374                    return -1;
2375                }
2376
2377                initDefaults();
2378                print_menu = 1;
2379
2380                while ( 1 ) {
2381                    if ( functional_menu() < 0 )
2382                        break;
2383                };
2384
2385                break;
2386
2387            case 'A':
2388            case 'a':
2389                printf("API level test cases coming soon ... \n");
2390
2391                break;
2392
2393            case 'E':
2394            case 'e': {
2395                ProcessState::self()->startThreadPool();
2396
2397                if ( openCamera() < 0 ) {
2398                    printf("Camera initialization failed\n");
2399                    system("echo camerahal_test > /sys/power/wake_unlock");
2400                    return -1;
2401                }
2402
2403                initDefaults();
2404                print_menu = 1;
2405
2406                while (1) {
2407                    if (error_scenario() < 0) {
2408                        break;
2409                    }
2410                }
2411
2412                break;
2413            }
2414
2415            default:
2416                printf("INVALID OPTION USED\n");
2417                print_usage();
2418
2419                break;
2420        }
2421    } else if ( ( argc == 3) && ( ( *argv[1] == 'S' ) || ( *argv[1] == 's') ) ) {
2422
2423        if((argv[1][1] == 'N') || (argv[1][1] == 'n')) {
2424            bLogSysLinkTrace = false;
2425        }
2426
2427        ProcessState::self()->startThreadPool();
2428
2429        if ( openCamera() < 0 ) {
2430            printf("Camera initialization failed\n");
2431            system("echo camerahal_test > /sys/power/wake_unlock");
2432            return -1;
2433        }
2434
2435        initDefaults();
2436
2437        cmd = load_script(argv[2]);
2438
2439        if ( cmd != NULL) {
2440            start_logging(argv[2], pid);
2441            stressTest = true;
2442
2443            while (1)
2444              {
2445                if ( execute_functional_script(cmd) == 0 )
2446                  {
2447                    break;
2448                  }
2449                else
2450                  {
2451                    printf("CameraTest Restarting Camera...\n");
2452
2453                    free(cmd);
2454                    cmd = NULL;
2455
2456                    if ( (restartCamera() != 0)  || ((cmd = load_script(argv[2])) == NULL) )
2457                      {
2458                        printf("ERROR::CameraTest Restarting Camera...\n");
2459                        break;
2460                      }
2461                  }
2462              }
2463            free(cmd);
2464            stop_logging(pid);
2465        }
2466    } else if ( ( argc == 3) && ( ( *argv[1] == 'E' ) || ( *argv[1] == 'e') ) ) {
2467
2468        ProcessState::self()->startThreadPool();
2469
2470        if ( openCamera() < 0 ) {
2471            printf("Camera initialization failed\n");
2472            system("echo camerahal_test > /sys/power/wake_unlock");
2473            return -1;
2474        }
2475
2476        initDefaults();
2477
2478        cmd = load_script(argv[2]);
2479
2480        if ( cmd != NULL) {
2481            start_logging(argv[2], pid);
2482            execute_error_script(cmd);
2483            free(cmd);
2484            stop_logging(pid);
2485        }
2486
2487    } else {
2488        printf("INVALID OPTION USED\n");
2489        print_usage();
2490    }
2491
2492    gettimeofday(&current_time, 0);
2493    end = current_time.tv_sec * 1000000 + current_time.tv_usec;
2494    delay = end - st;
2495    printf("Application clossed after: %llu ms\n", delay);
2496    system("echo camerahal_test > /sys/power/wake_unlock");
2497    return 0;
2498}
2499
2500