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