BaseCameraAdapter.cpp revision 5e02dd977e79ee977ee063496033bbc363a70780
1/*
2 * Copyright (C) Texas Instruments - http://www.ti.com/
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18
19#define LOG_TAG "CameraHAL"
20
21#include "BaseCameraAdapter.h"
22
23namespace android {
24
25/*--------------------Camera Adapter Class STARTS here-----------------------------*/
26
27BaseCameraAdapter::BaseCameraAdapter()
28{
29    mReleaseImageBuffersCallback = NULL;
30    mEndImageCaptureCallback = NULL;
31    mErrorNotifier = NULL;
32    mEndCaptureData = NULL;
33    mReleaseData = NULL;
34    mRecording = false;
35
36    mPreviewBuffers = NULL;
37    mPreviewBufferCount = 0;
38    mPreviewBuffersLength = 0;
39
40    mVideoBuffers = NULL;
41    mVideoBuffersCount = 0;
42    mVideoBuffersLength = 0;
43
44    mCaptureBuffers = NULL;
45    mCaptureBuffersCount = 0;
46    mCaptureBuffersLength = 0;
47
48    mPreviewDataBuffers = NULL;
49    mPreviewDataBuffersCount = 0;
50    mPreviewDataBuffersLength = 0;
51
52    mAdapterState = INTIALIZED_STATE;
53
54#if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS
55    mStartFocus.tv_sec = 0;
56    mStartFocus.tv_usec = 0;
57    mStartCapture.tv_sec = 0;
58    mStartCapture.tv_usec = 0;
59#endif
60
61}
62
63BaseCameraAdapter::~BaseCameraAdapter()
64{
65     LOG_FUNCTION_NAME;
66
67     Mutex::Autolock lock(mSubscriberLock);
68
69     mFrameSubscribers.clear();
70     mImageSubscribers.clear();
71     mRawSubscribers.clear();
72     mVideoSubscribers.clear();
73     mFocusSubscribers.clear();
74     mShutterSubscribers.clear();
75     mZoomSubscribers.clear();
76     mFaceSubscribers.clear();
77
78     LOG_FUNCTION_NAME_EXIT;
79}
80
81status_t BaseCameraAdapter::registerImageReleaseCallback(release_image_buffers_callback callback, void *user_data)
82{
83    status_t ret = NO_ERROR;
84
85    LOG_FUNCTION_NAME;
86
87    mReleaseImageBuffersCallback = callback;
88    mReleaseData = user_data;
89
90    LOG_FUNCTION_NAME_EXIT;
91
92    return ret;
93}
94
95status_t BaseCameraAdapter::registerEndCaptureCallback(end_image_capture_callback callback, void *user_data)
96{
97    status_t ret = NO_ERROR;
98
99    LOG_FUNCTION_NAME;
100
101    mEndImageCaptureCallback= callback;
102    mEndCaptureData = user_data;
103
104    LOG_FUNCTION_NAME_EXIT;
105
106    return ret;
107}
108
109status_t BaseCameraAdapter::setErrorHandler(ErrorNotifier *errorNotifier)
110{
111    status_t ret = NO_ERROR;
112
113    LOG_FUNCTION_NAME;
114
115    if ( NULL == errorNotifier )
116        {
117        CAMHAL_LOGEA("Invalid Error Notifier reference");
118        ret = -EINVAL;
119        }
120
121    if ( NO_ERROR == ret )
122        {
123        mErrorNotifier = errorNotifier;
124        }
125
126    LOG_FUNCTION_NAME_EXIT;
127
128    return ret;
129}
130
131void BaseCameraAdapter::enableMsgType(int32_t msgs, frame_callback callback, event_callback eventCb, void* cookie)
132{
133    Mutex::Autolock lock(mSubscriberLock);
134
135    LOG_FUNCTION_NAME;
136
137    if ( CameraFrame::PREVIEW_FRAME_SYNC == msgs )
138        {
139        mFrameSubscribers.add((int) cookie, callback);
140        }
141    else if ( CameraFrame::FRAME_DATA_SYNC == msgs )
142        {
143        mFrameDataSubscribers.add((int) cookie, callback);
144        }
145    else if ( CameraFrame::IMAGE_FRAME == msgs)
146        {
147        mImageSubscribers.add((int) cookie, callback);
148        }
149    else if ( CameraFrame::RAW_FRAME == msgs)
150        {
151        mRawSubscribers.add((int) cookie, callback);
152        }
153    else if ( CameraFrame::VIDEO_FRAME_SYNC == msgs)
154        {
155        mVideoSubscribers.add((int) cookie, callback);
156        }
157    else if ( CameraHalEvent::ALL_EVENTS == msgs)
158        {
159        mFocusSubscribers.add((int) cookie, eventCb);
160        mShutterSubscribers.add((int) cookie, eventCb);
161        mZoomSubscribers.add((int) cookie, eventCb);
162        mFaceSubscribers.add((int) cookie, eventCb);
163        }
164    else
165        {
166        CAMHAL_LOGEA("Message type subscription no supported yet!");
167        }
168
169    LOG_FUNCTION_NAME_EXIT;
170}
171
172void BaseCameraAdapter::disableMsgType(int32_t msgs, void* cookie)
173{
174    Mutex::Autolock lock(mSubscriberLock);
175
176    LOG_FUNCTION_NAME;
177
178    if ( CameraFrame::PREVIEW_FRAME_SYNC == msgs )
179        {
180        mFrameSubscribers.removeItem((int) cookie);
181        }
182    else if ( CameraFrame::FRAME_DATA_SYNC == msgs )
183        {
184        mFrameDataSubscribers.removeItem((int) cookie);
185        }
186    else if ( CameraFrame::IMAGE_FRAME == msgs)
187        {
188        mImageSubscribers.removeItem((int) cookie);
189        }
190    else if ( CameraFrame::RAW_FRAME == msgs)
191        {
192        mRawSubscribers.removeItem((int) cookie);
193        }
194    else if ( CameraFrame::VIDEO_FRAME_SYNC == msgs)
195        {
196        mVideoSubscribers.removeItem((int) cookie);
197        }
198    else if ( CameraFrame::ALL_FRAMES  == msgs )
199        {
200        mFrameSubscribers.removeItem((int) cookie);
201        mFrameDataSubscribers.removeItem((int) cookie);
202        mImageSubscribers.removeItem((int) cookie);
203        mRawSubscribers.removeItem((int) cookie);
204        mVideoSubscribers.removeItem((int) cookie);
205        }
206    else if ( CameraHalEvent::ALL_EVENTS == msgs)
207        {
208         //Subscribe only for focus
209         //TODO: Process case by case
210        mFocusSubscribers.removeItem((int) cookie);
211        mShutterSubscribers.removeItem((int) cookie);
212        mZoomSubscribers.removeItem((int) cookie);
213        mFaceSubscribers.removeItem((int) cookie);
214        }
215    else
216        {
217        CAMHAL_LOGEB("Message type 0x%x subscription no supported yet!", msgs);
218        }
219
220    LOG_FUNCTION_NAME_EXIT;
221}
222
223void BaseCameraAdapter::returnFrame(void* frameBuf, CameraFrame::FrameType frameType)
224{
225    status_t res = NO_ERROR;
226    size_t subscriberCount = 0;
227    int refCount = -1;
228
229    Mutex::Autolock lock(mReturnFrameLock);
230
231    if ( NULL == frameBuf )
232        {
233        CAMHAL_LOGEA("Invalid frameBuf");
234        return;
235        }
236
237    if ( NO_ERROR == res)
238        {
239
240        refCount = getFrameRefCount(frameBuf,  frameType);
241
242        if ( 0 < refCount )
243            {
244
245            refCount--;
246            setFrameRefCount(frameBuf, frameType, refCount);
247
248            if ( ( mRecording ) && (  CameraFrame::VIDEO_FRAME_SYNC == frameType ) )
249                {
250                refCount += getFrameRefCount(frameBuf, CameraFrame::PREVIEW_FRAME_SYNC);
251                }
252            else if ( ( mRecording ) && (  CameraFrame::PREVIEW_FRAME_SYNC == frameType ) )
253                {
254                refCount += getFrameRefCount(frameBuf, CameraFrame::VIDEO_FRAME_SYNC);
255                }
256
257            }
258        else
259            {
260            return;
261            }
262        }
263
264    if ( NO_ERROR == res )
265        {
266        //check if someone is holding this buffer
267        if ( 0 == refCount )
268            {
269            res = fillThisBuffer(frameBuf, frameType);
270            }
271        }
272
273}
274
275status_t BaseCameraAdapter::sendCommand(CameraCommands operation, int value1, int value2, int value3)
276{
277    status_t ret = NO_ERROR;
278    struct timeval *refTimestamp;
279    BuffersDescriptor *desc = NULL;
280    CameraFrame *frame = NULL;
281
282    LOG_FUNCTION_NAME;
283
284    switch ( operation ) {
285        case CameraAdapter::CAMERA_USE_BUFFERS_PREVIEW:
286                CAMHAL_LOGDA("Use buffers for preview");
287                desc = ( BuffersDescriptor * ) value1;
288
289                if ( NULL == desc )
290                    {
291                    CAMHAL_LOGEA("Invalid preview buffers!");
292                    return -EINVAL;
293                    }
294
295                if ( ret == NO_ERROR )
296                    {
297                    ret = setState(operation);
298                    }
299
300                if ( ret == NO_ERROR )
301                    {
302                    Mutex::Autolock lock(mPreviewBufferLock);
303                    mPreviewBuffers = (int *) desc->mBuffers;
304                    mPreviewBuffersLength = desc->mLength;
305                    mPreviewBuffersAvailable.clear();
306                    for ( uint32_t i = 0 ; i < desc->mMaxQueueable ; i++ )
307                        {
308                        mPreviewBuffersAvailable.add(mPreviewBuffers[i], 0);
309                        }
310                    // initial ref count for undeqeueued buffers is 1 since buffer provider
311                    // is still holding on to it
312                    for ( uint32_t i = desc->mMaxQueueable ; i < desc->mCount ; i++ )
313                        {
314                        mPreviewBuffersAvailable.add(mPreviewBuffers[i], 1);
315                        }
316                    }
317
318                if ( NULL != desc )
319                    {
320                    ret = useBuffers(CameraAdapter::CAMERA_PREVIEW,
321                                     desc->mBuffers,
322                                     desc->mCount,
323                                     desc->mLength,
324                                     desc->mMaxQueueable);
325                    }
326
327                if ( ret == NO_ERROR )
328                    {
329                    ret = commitState();
330                    }
331                else
332                    {
333                    ret |= rollbackState();
334                    }
335
336                break;
337
338        case CameraAdapter::CAMERA_USE_BUFFERS_PREVIEW_DATA:
339                    CAMHAL_LOGDA("Use buffers for preview data");
340                    desc = ( BuffersDescriptor * ) value1;
341
342                    if ( NULL == desc )
343                        {
344                        CAMHAL_LOGEA("Invalid preview data buffers!");
345                        return -EINVAL;
346                        }
347
348                    if ( ret == NO_ERROR )
349                        {
350                        ret = setState(operation);
351                        }
352
353                    if ( ret == NO_ERROR )
354                        {
355                        Mutex::Autolock lock(mPreviewDataBufferLock);
356                        mPreviewDataBuffers = (int *) desc->mBuffers;
357                        mPreviewDataBuffersLength = desc->mLength;
358                        mPreviewDataBuffersAvailable.clear();
359                        for ( uint32_t i = 0 ; i < desc->mMaxQueueable ; i++ )
360                            {
361                            mPreviewDataBuffersAvailable.add(mPreviewDataBuffers[i], true);
362                            }
363                        // initial ref count for undeqeueued buffers is 1 since buffer provider
364                        // is still holding on to it
365                        for ( uint32_t i = desc->mMaxQueueable ; i < desc->mCount ; i++ )
366                            {
367                            mPreviewDataBuffersAvailable.add(mPreviewBuffers[i], 1);
368                            }
369                        }
370
371                    if ( NULL != desc )
372                        {
373                        ret = useBuffers(CameraAdapter::CAMERA_MEASUREMENT,
374                                         desc->mBuffers,
375                                         desc->mCount,
376                                         desc->mLength,
377                                         desc->mMaxQueueable);
378                        }
379
380                    if ( ret == NO_ERROR )
381                        {
382                        ret = commitState();
383                        }
384                    else
385                        {
386                        ret |= rollbackState();
387                        }
388
389                    break;
390
391        case CameraAdapter::CAMERA_USE_BUFFERS_IMAGE_CAPTURE:
392                CAMHAL_LOGDA("Use buffers for image capture");
393                desc = ( BuffersDescriptor * ) value1;
394
395                if ( NULL == desc )
396                    {
397                    CAMHAL_LOGEA("Invalid capture buffers!");
398                    return -EINVAL;
399                    }
400
401                if ( ret == NO_ERROR )
402                    {
403                    ret = setState(operation);
404                    }
405
406                if ( ret == NO_ERROR )
407                    {
408                    Mutex::Autolock lock(mCaptureBufferLock);
409                    mCaptureBuffers = (int *) desc->mBuffers;
410                    mCaptureBuffersLength = desc->mLength;
411                    mCaptureBuffersAvailable.clear();
412                    for ( uint32_t i = 0 ; i < desc->mMaxQueueable ; i++ )
413                        {
414                        mCaptureBuffersAvailable.add(mCaptureBuffers[i], true);
415                        }
416                    // initial ref count for undeqeueued buffers is 1 since buffer provider
417                    // is still holding on to it
418                    for ( uint32_t i = desc->mMaxQueueable ; i < desc->mCount ; i++ )
419                        {
420                        mCaptureBuffersAvailable.add(mPreviewBuffers[i], 1);
421                        }
422                    }
423
424                if ( NULL != desc )
425                    {
426                    ret = useBuffers(CameraAdapter::CAMERA_IMAGE_CAPTURE,
427                                     desc->mBuffers,
428                                     desc->mCount,
429                                     desc->mLength,
430                                     desc->mMaxQueueable);
431                    }
432
433                if ( ret == NO_ERROR )
434                    {
435                    ret = commitState();
436                    }
437                else
438                    {
439                    ret |= rollbackState();
440                    }
441
442                break;
443
444        case CameraAdapter::CAMERA_START_SMOOTH_ZOOM:
445            {
446
447            if ( ret == NO_ERROR )
448                {
449                ret = setState(operation);
450                }
451
452            if ( ret == NO_ERROR )
453                {
454                ret = startSmoothZoom(value1);
455                }
456
457            if ( ret == NO_ERROR )
458                {
459                ret = commitState();
460                }
461            else
462                {
463                ret |= rollbackState();
464                }
465
466            break;
467
468            }
469
470        case CameraAdapter::CAMERA_STOP_SMOOTH_ZOOM:
471            {
472
473            if ( ret == NO_ERROR )
474                {
475                ret = setState(operation);
476                }
477
478            if ( ret == NO_ERROR )
479                {
480                ret = stopSmoothZoom();
481                }
482
483            if ( ret == NO_ERROR )
484                {
485                ret = commitState();
486                }
487            else
488                {
489                ret |= rollbackState();
490                }
491
492            break;
493
494            }
495
496        case CameraAdapter::CAMERA_START_PREVIEW:
497            {
498
499                CAMHAL_LOGDA("Start Preview");
500
501            if ( ret == NO_ERROR )
502                {
503                ret = setState(operation);
504                }
505
506            if ( ret == NO_ERROR )
507                {
508                ret = startPreview();
509                }
510
511            if ( ret == NO_ERROR )
512                {
513                ret = commitState();
514                }
515            else
516                {
517                ret |= rollbackState();
518                }
519
520            break;
521
522            }
523
524        case CameraAdapter::CAMERA_STOP_PREVIEW:
525            {
526
527            CAMHAL_LOGDA("Stop Preview");
528
529            if ( ret == NO_ERROR )
530                {
531                ret = setState(operation);
532                }
533
534            if ( ret == NO_ERROR )
535                {
536                ret = stopPreview();
537                }
538
539            if ( ret == NO_ERROR )
540                {
541                ret = commitState();
542                }
543            else
544                {
545                ret |= rollbackState();
546                }
547
548            break;
549
550            }
551
552        case CameraAdapter::CAMERA_START_VIDEO:
553            {
554
555            CAMHAL_LOGDA("Start video recording");
556
557            if ( ret == NO_ERROR )
558                {
559                ret = setState(operation);
560                }
561
562            if ( ret == NO_ERROR )
563                {
564                ret = startVideoCapture();
565                }
566
567            if ( ret == NO_ERROR )
568                {
569                ret = commitState();
570                }
571            else
572                {
573                ret |= rollbackState();
574                }
575
576            break;
577
578            }
579
580        case CameraAdapter::CAMERA_STOP_VIDEO:
581            {
582
583            CAMHAL_LOGDA("Stop video recording");
584
585            if ( ret == NO_ERROR )
586                {
587                ret = setState(operation);
588                }
589
590            if ( ret == NO_ERROR )
591                {
592                ret = stopVideoCapture();
593                }
594
595            if ( ret == NO_ERROR )
596                {
597                ret = commitState();
598                }
599            else
600                {
601                ret |= rollbackState();
602                }
603
604            break;
605
606            }
607
608        case CameraAdapter::CAMERA_PREVIEW_FLUSH_BUFFERS:
609            {
610
611            if ( ret == NO_ERROR )
612                {
613                ret = setState(operation);
614                }
615
616            if ( ret == NO_ERROR )
617                {
618                ret = flushBuffers();
619                }
620
621            if ( ret == NO_ERROR )
622                {
623                ret = commitState();
624                }
625            else
626                {
627                ret |= rollbackState();
628                }
629
630            break;
631
632            }
633
634        case CameraAdapter::CAMERA_START_IMAGE_CAPTURE:
635            {
636
637#if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS
638
639            refTimestamp = ( struct timeval * ) value1;
640            if ( NULL != refTimestamp )
641                {
642                memcpy( &mStartCapture, refTimestamp, sizeof( struct timeval ));
643                }
644
645#endif
646
647            if ( ret == NO_ERROR )
648                {
649                ret = setState(operation);
650                }
651
652            if ( ret == NO_ERROR )
653                {
654                ret = takePicture();
655                }
656
657            if ( ret == NO_ERROR )
658                {
659                ret = commitState();
660                }
661            else
662                {
663                ret |= rollbackState();
664                }
665
666            break;
667
668            }
669
670        case CameraAdapter::CAMERA_STOP_IMAGE_CAPTURE:
671            {
672
673            if ( ret == NO_ERROR )
674                {
675                ret = setState(operation);
676                }
677
678            if ( ret == NO_ERROR )
679                {
680                ret = stopImageCapture();
681                }
682
683            if ( ret == NO_ERROR )
684                {
685                ret = commitState();
686                }
687            else
688                {
689                ret |= rollbackState();
690                }
691
692            break;
693
694            }
695
696        case CameraAdapter::CAMERA_START_BRACKET_CAPTURE:
697            {
698
699#if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS
700
701            refTimestamp = ( struct timeval * ) value2;
702            if ( NULL != refTimestamp )
703                {
704                memcpy( &mStartCapture, refTimestamp, sizeof( struct timeval ));
705                }
706
707#endif
708
709            if ( ret == NO_ERROR )
710                {
711                ret = setState(operation);
712                }
713
714            if ( ret == NO_ERROR )
715                {
716                ret = startBracketing(value1);
717                }
718
719            if ( ret == NO_ERROR )
720                {
721                ret = commitState();
722                }
723            else
724                {
725                ret |= rollbackState();
726                }
727
728            break;
729
730            }
731
732        case CameraAdapter::CAMERA_STOP_BRACKET_CAPTURE:
733            {
734
735            if ( ret == NO_ERROR )
736                {
737                ret = setState(operation);
738                }
739
740            if ( ret == NO_ERROR )
741                {
742                ret = stopBracketing();
743                }
744
745            if ( ret == NO_ERROR )
746                {
747                ret = commitState();
748                }
749            else
750                {
751                ret |= rollbackState();
752                }
753
754            break;
755
756            }
757
758        case CameraAdapter::CAMERA_PERFORM_AUTOFOCUS:
759
760#if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS
761
762            refTimestamp = ( struct timeval * ) value1;
763            if ( NULL != refTimestamp )
764                {
765                memcpy( &mStartFocus, refTimestamp, sizeof( struct timeval ));
766                }
767
768#endif
769
770            if ( ret == NO_ERROR )
771                {
772                ret = setState(operation);
773                }
774
775            if ( ret == NO_ERROR )
776                {
777                ret = autoFocus();
778                }
779
780            if ( ret == NO_ERROR )
781                {
782                ret = commitState();
783                }
784            else
785                {
786                ret |= rollbackState();
787                }
788
789            break;
790
791        case CameraAdapter::CAMERA_CANCEL_AUTOFOCUS:
792
793            if ( ret == NO_ERROR )
794                {
795                ret = setState(operation);
796                }
797
798            if ( ret == NO_ERROR )
799                {
800                ret = cancelAutoFocus();
801                }
802
803            if ( ret == NO_ERROR )
804                {
805                ret = commitState();
806                }
807            else
808                {
809                ret |= rollbackState();
810                }
811
812            break;
813
814        case CameraAdapter::CAMERA_QUERY_RESOLUTION_PREVIEW:
815
816             if ( ret == NO_ERROR )
817                 {
818                 ret = setState(operation);
819                 }
820
821             if ( ret == NO_ERROR )
822                 {
823                 frame = ( CameraFrame * ) value1;
824
825                 if ( NULL != frame )
826                     {
827                     ret = getFrameSize(frame->mWidth, frame->mHeight);
828                     }
829                 else
830                     {
831                     ret = -EINVAL;
832                     }
833                 }
834
835             if ( ret == NO_ERROR )
836                 {
837                 ret = commitState();
838                 }
839             else
840                 {
841                 ret |= rollbackState();
842                 }
843
844             break;
845
846         case CameraAdapter::CAMERA_QUERY_BUFFER_SIZE_IMAGE_CAPTURE:
847
848             if ( ret == NO_ERROR )
849                 {
850                 ret = setState(operation);
851                 }
852
853             if ( ret == NO_ERROR )
854                 {
855                 frame = ( CameraFrame * ) value1;
856
857                 if ( NULL != frame )
858                     {
859                     ret = getPictureBufferSize(frame->mLength, value2);
860                     }
861                 else
862                     {
863                     ret = -EINVAL;
864                     }
865                 }
866
867             if ( ret == NO_ERROR )
868                 {
869                 ret = commitState();
870                 }
871             else
872                 {
873                 ret |= rollbackState();
874                 }
875
876             break;
877
878         case CameraAdapter::CAMERA_QUERY_BUFFER_SIZE_PREVIEW_DATA:
879
880             if ( ret == NO_ERROR )
881                 {
882                 ret = setState(operation);
883                 }
884
885             if ( ret == NO_ERROR )
886                 {
887                 frame = ( CameraFrame * ) value1;
888
889                 if ( NULL != frame )
890                     {
891                     ret = getFrameDataSize(frame->mLength, value2);
892                     }
893                 else
894                     {
895                     ret = -EINVAL;
896                     }
897                 }
898
899             if ( ret == NO_ERROR )
900                 {
901                 ret = commitState();
902                 }
903             else
904                 {
905                 ret |= rollbackState();
906                 }
907
908             break;
909
910         case CameraAdapter::CAMERA_START_FD:
911
912             ret = startFaceDetection();
913
914             break;
915
916         case CameraAdapter::CAMERA_STOP_FD:
917
918             ret = stopFaceDetection();
919
920             break;
921
922        default:
923            CAMHAL_LOGEB("Command 0x%x unsupported!", operation);
924            break;
925    };
926
927    LOG_FUNCTION_NAME_EXIT;
928    return ret;
929}
930
931status_t BaseCameraAdapter::notifyFocusSubscribers(bool status)
932{
933    event_callback eventCb;
934    CameraHalEvent focusEvent;
935    status_t ret = NO_ERROR;
936
937    LOG_FUNCTION_NAME;
938
939    if ( mFocusSubscribers.size() == 0 ) {
940        CAMHAL_LOGDA("No Focus Subscribers!");
941        return NO_INIT;
942    }
943
944#if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS
945
946     //dump the AF latency
947     CameraHal::PPM("Focus finished in: ", &mStartFocus);
948
949#endif
950
951    focusEvent.mEventData = new CameraHalEvent::CameraHalEventData();
952    if ( NULL == focusEvent.mEventData.get() ) {
953        return -ENOMEM;
954    }
955
956    focusEvent.mEventType = CameraHalEvent::EVENT_FOCUS_LOCKED;
957    focusEvent.mEventData->focusEvent.focusLocked = status;
958    focusEvent.mEventData->focusEvent.focusError = !status;
959
960    for (unsigned int i = 0 ; i < mFocusSubscribers.size(); i++ )
961        {
962        focusEvent.mCookie = (void *) mFocusSubscribers.keyAt(i);
963        eventCb = (event_callback) mFocusSubscribers.valueAt(i);
964        eventCb ( &focusEvent );
965        }
966
967    focusEvent.mEventData.clear();
968
969    LOG_FUNCTION_NAME_EXIT;
970
971    return ret;
972}
973
974status_t BaseCameraAdapter::notifyShutterSubscribers()
975{
976    CameraHalEvent shutterEvent;
977    event_callback eventCb;
978    status_t ret = NO_ERROR;
979
980    LOG_FUNCTION_NAME;
981
982    if ( mShutterSubscribers.size() == 0 )
983        {
984        CAMHAL_LOGEA("No shutter Subscribers!");
985        return NO_INIT;
986        }
987
988    shutterEvent.mEventData = new CameraHalEvent::CameraHalEventData();
989    if ( NULL == shutterEvent.mEventData.get() ) {
990        return -ENOMEM;
991    }
992
993    shutterEvent.mEventType = CameraHalEvent::EVENT_SHUTTER;
994    shutterEvent.mEventData->shutterEvent.shutterClosed = true;
995
996    for (unsigned int i = 0 ; i < mShutterSubscribers.size() ; i++ ) {
997        shutterEvent.mCookie = ( void * ) mShutterSubscribers.keyAt(i);
998        eventCb = ( event_callback ) mShutterSubscribers.valueAt(i);
999
1000        CAMHAL_LOGEA("Sending shutter callback");
1001
1002        eventCb ( &shutterEvent );
1003    }
1004
1005    shutterEvent.mEventData.clear();
1006
1007    LOG_FUNCTION_NAME;
1008
1009    return ret;
1010}
1011
1012status_t BaseCameraAdapter::notifyZoomSubscribers(int zoomIdx, bool targetReached)
1013{
1014    event_callback eventCb;
1015    CameraHalEvent zoomEvent;
1016    status_t ret = NO_ERROR;
1017
1018    LOG_FUNCTION_NAME;
1019
1020    if ( mZoomSubscribers.size() == 0 ) {
1021        CAMHAL_LOGDA("No zoom Subscribers!");
1022        return NO_INIT;
1023    }
1024
1025    zoomEvent.mEventData = new CameraHalEvent::CameraHalEventData();
1026    if ( NULL == zoomEvent.mEventData.get() ) {
1027        return -ENOMEM;
1028    }
1029
1030    zoomEvent.mEventType = CameraHalEvent::EVENT_ZOOM_INDEX_REACHED;
1031    zoomEvent.mEventData->zoomEvent.currentZoomIndex = zoomIdx;
1032    zoomEvent.mEventData->zoomEvent.targetZoomIndexReached = targetReached;
1033
1034    for (unsigned int i = 0 ; i < mZoomSubscribers.size(); i++ ) {
1035        zoomEvent.mCookie = (void *) mZoomSubscribers.keyAt(i);
1036        eventCb = (event_callback) mZoomSubscribers.valueAt(i);
1037
1038        eventCb ( &zoomEvent );
1039    }
1040
1041    zoomEvent.mEventData.clear();
1042
1043    LOG_FUNCTION_NAME_EXIT;
1044
1045    return ret;
1046}
1047
1048status_t BaseCameraAdapter::notifyFaceSubscribers(sp<CameraFDResult> &faces)
1049{
1050    event_callback eventCb;
1051    CameraHalEvent faceEvent;
1052    status_t ret = NO_ERROR;
1053
1054    LOG_FUNCTION_NAME;
1055
1056    if ( mFaceSubscribers.size() == 0 ) {
1057        CAMHAL_LOGDA("No face detection subscribers!");
1058        return NO_INIT;
1059    }
1060
1061    faceEvent.mEventData = new CameraHalEvent::CameraHalEventData();
1062    if ( NULL == faceEvent.mEventData.get() ) {
1063        return -ENOMEM;
1064    }
1065
1066    faceEvent.mEventType = CameraHalEvent::EVENT_FACE;
1067    faceEvent.mEventData->faceEvent = faces;
1068
1069    for (unsigned int i = 0 ; i < mFaceSubscribers.size(); i++ ) {
1070        faceEvent.mCookie = (void *) mFaceSubscribers.keyAt(i);
1071        eventCb = (event_callback) mFaceSubscribers.valueAt(i);
1072
1073        eventCb ( &faceEvent );
1074    }
1075
1076    faceEvent.mEventData.clear();
1077
1078    LOG_FUNCTION_NAME_EXIT;
1079
1080    return ret;
1081}
1082
1083status_t BaseCameraAdapter::sendFrameToSubscribers(CameraFrame *frame)
1084{
1085    status_t ret = NO_ERROR;
1086    frame_callback callback;
1087    uint32_t i = 0;
1088    KeyedVector<int, frame_callback> *subscribers = NULL;
1089    size_t refCount = 0;
1090    unsigned int mask;
1091
1092    if ( NULL == frame )
1093        {
1094        CAMHAL_LOGEA("Invalid CameraFrame");
1095        return -EINVAL;
1096        }
1097
1098    for( mask = 1; mask < CameraFrame::ALL_FRAMES; mask <<= 1){
1099      if( mask & frame->mFrameMask ){
1100        switch( mask ){
1101
1102        case CameraFrame::IMAGE_FRAME:
1103          {
1104#if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS
1105            CameraHal::PPM("Shot to Jpeg: ", &mStartCapture);
1106#endif
1107            subscribers = &mImageSubscribers;
1108            frame->mFrameType = CameraFrame::IMAGE_FRAME;
1109            if (NULL != subscribers)
1110              {
1111                refCount = getFrameRefCount(frame->mBuffer, CameraFrame::IMAGE_FRAME);
1112                CAMHAL_LOGVB("Type of Frame: 0x%x address: 0x%x refCount start %d",
1113                             frame->mFrameType,
1114                             ( uint32_t ) frame->mBuffer,
1115                             refCount);
1116                for ( i = 0 ; i < refCount; i++ )
1117                  {
1118                    frame->mCookie = ( void * ) subscribers->keyAt(i);
1119                    callback = (frame_callback) subscribers->valueAt(i);
1120                    callback(frame);
1121                  }
1122              }
1123          }
1124          break;
1125        case CameraFrame::RAW_FRAME:
1126          {
1127            subscribers = &mRawSubscribers;
1128            frame->mFrameType = CameraFrame::RAW_FRAME;
1129            if (NULL != subscribers)
1130              {
1131                refCount = getFrameRefCount(frame->mBuffer, CameraFrame::RAW_FRAME);
1132                CAMHAL_LOGVB("Type of Frame: 0x%x address: 0x%x refCount start %d",
1133                             frame->mFrameType,
1134                             ( uint32_t ) frame->mBuffer,
1135                             refCount);
1136                for ( i = 0 ; i < refCount; i++ )
1137                  {
1138                    frame->mCookie = ( void * ) subscribers->keyAt(i);
1139                    callback = (frame_callback) subscribers->valueAt(i);
1140                    callback(frame);
1141                  }
1142              }
1143          }
1144          break;
1145        case CameraFrame::PREVIEW_FRAME_SYNC:
1146          {
1147            subscribers = &mFrameSubscribers;
1148            frame->mFrameType = CameraFrame::PREVIEW_FRAME_SYNC;
1149            if (NULL != subscribers)
1150              {
1151                refCount = getFrameRefCount(frame->mBuffer, CameraFrame::PREVIEW_FRAME_SYNC);
1152                CAMHAL_LOGVB("Type of Frame: 0x%x address: 0x%x refCount start %d",
1153                             frame->mFrameType,
1154                             ( uint32_t ) frame->mBuffer,
1155                             refCount);
1156                for ( i = 0 ; i < refCount; i++ )
1157                  {
1158                    frame->mCookie = ( void * ) subscribers->keyAt(i);
1159                    callback = (frame_callback) subscribers->valueAt(i);
1160                    callback(frame);
1161                  }
1162              }
1163          }
1164          break;
1165        case CameraFrame::SNAPSHOT_FRAME:
1166          {
1167            subscribers = &mFrameSubscribers;
1168            frame->mFrameType = CameraFrame::SNAPSHOT_FRAME;
1169            if (NULL != subscribers)
1170              {
1171                refCount = getFrameRefCount(frame->mBuffer, CameraFrame::SNAPSHOT_FRAME);
1172                CAMHAL_LOGVB("Type of Frame: 0x%x address: 0x%x refCount start %d",
1173                             frame->mFrameType,
1174                             ( uint32_t ) frame->mBuffer,
1175                             refCount);
1176                for ( i = 0 ; i < refCount; i++ )
1177                  {
1178                    frame->mCookie = ( void * ) subscribers->keyAt(i);
1179                    callback = (frame_callback) subscribers->valueAt(i);
1180                    callback(frame);
1181                  }
1182              }
1183          }
1184          break;
1185        case CameraFrame::VIDEO_FRAME_SYNC:
1186          {
1187            subscribers = &mVideoSubscribers;
1188            frame->mFrameType = CameraFrame::VIDEO_FRAME_SYNC;
1189            if (NULL != subscribers)
1190              {
1191                refCount = getFrameRefCount(frame->mBuffer, CameraFrame::VIDEO_FRAME_SYNC);
1192                CAMHAL_LOGVB("Type of Frame: 0x%x address: 0x%x refCount start %d",
1193                             frame->mFrameType,
1194                             ( uint32_t ) frame->mBuffer,
1195                             refCount);
1196                for ( i = 0 ; i < refCount; i++ )
1197                  {
1198                    frame->mCookie = ( void * ) subscribers->keyAt(i);
1199                    callback = (frame_callback) subscribers->valueAt(i);
1200                    callback(frame);
1201                  }
1202              }
1203          }
1204          break;
1205        case CameraFrame::FRAME_DATA_SYNC:
1206          {
1207            subscribers = &mFrameDataSubscribers;
1208            frame->mFrameType = CameraFrame::FRAME_DATA_SYNC;
1209            if (NULL != subscribers)
1210              {
1211                refCount = getFrameRefCount(frame->mBuffer, CameraFrame::FRAME_DATA_SYNC);
1212                CAMHAL_LOGVB("Type of Frame: 0x%x address: 0x%x refCount start %d",
1213                             frame->mFrameType,
1214                             ( uint32_t ) frame->mBuffer,
1215                             refCount);
1216                for ( i = 0 ; i < refCount; i++ )
1217                  {
1218                    frame->mCookie = ( void * ) subscribers->keyAt(i);
1219                    callback = (frame_callback) subscribers->valueAt(i);
1220                    callback(frame);
1221                  }
1222              }
1223          }
1224          break;
1225        default:
1226          CAMHAL_LOGEB("FRAMETYPE NOT SUPPORTED 0x%x", mask);
1227        break;
1228        }//SWITCH
1229        frame->mFrameMask &= ~mask;
1230      }//IF
1231    }//FOR
1232    return ret;
1233}
1234int BaseCameraAdapter::setInitFrameRefCount(void* buf, unsigned int mask)
1235{
1236  int ret = NO_ERROR;
1237  unsigned int lmask;
1238
1239  LOG_FUNCTION_NAME;
1240
1241  if (buf == NULL)
1242    {
1243      return -EINVAL;
1244    }
1245
1246  for( lmask = 1; lmask < CameraFrame::ALL_FRAMES; lmask <<= 1){
1247    if( lmask & mask ){
1248      switch( lmask ){
1249
1250      case CameraFrame::IMAGE_FRAME:
1251        {
1252          setFrameRefCount(buf, CameraFrame::IMAGE_FRAME, (int) mRawSubscribers.size());
1253        }
1254        break;
1255      case CameraFrame::RAW_FRAME:
1256        {
1257          setFrameRefCount(buf, CameraFrame::RAW_FRAME, mRawSubscribers.size());
1258        }
1259        break;
1260      case CameraFrame::PREVIEW_FRAME_SYNC:
1261        {
1262          setFrameRefCount(buf, CameraFrame::PREVIEW_FRAME_SYNC, mFrameSubscribers.size());
1263        }
1264        break;
1265      case CameraFrame::SNAPSHOT_FRAME:
1266        {
1267          setFrameRefCount(buf, CameraFrame::SNAPSHOT_FRAME, mFrameSubscribers.size());
1268        }
1269        break;
1270      case CameraFrame::VIDEO_FRAME_SYNC:
1271        {
1272          setFrameRefCount(buf,CameraFrame::VIDEO_FRAME_SYNC, mVideoSubscribers.size());
1273        }
1274        break;
1275      case CameraFrame::FRAME_DATA_SYNC:
1276        {
1277          setFrameRefCount(buf, CameraFrame::FRAME_DATA_SYNC, mFrameDataSubscribers.size());
1278        }
1279        break;
1280      default:
1281        CAMHAL_LOGEB("FRAMETYPE NOT SUPPORTED 0x%x", lmask);
1282        break;
1283      }//SWITCH
1284      mask &= ~lmask;
1285    }//IF
1286  }//FOR
1287  LOG_FUNCTION_NAME_EXIT;
1288  return ret;
1289}
1290
1291int BaseCameraAdapter::getFrameRefCount(void* frameBuf, CameraFrame::FrameType frameType)
1292{
1293    int res = -1;
1294
1295    LOG_FUNCTION_NAME;
1296
1297    switch ( frameType )
1298        {
1299        case CameraFrame::IMAGE_FRAME:
1300        case CameraFrame::RAW_FRAME:
1301                {
1302                Mutex::Autolock lock(mCaptureBufferLock);
1303                res = mCaptureBuffersAvailable.valueFor( ( unsigned int ) frameBuf );
1304                }
1305            break;
1306        case CameraFrame::PREVIEW_FRAME_SYNC:
1307        case CameraFrame::SNAPSHOT_FRAME:
1308                {
1309                Mutex::Autolock lock(mPreviewBufferLock);
1310                res = mPreviewBuffersAvailable.valueFor( ( unsigned int ) frameBuf );
1311                }
1312            break;
1313        case CameraFrame::FRAME_DATA_SYNC:
1314                {
1315                Mutex::Autolock lock(mPreviewDataBufferLock);
1316                res = mPreviewDataBuffersAvailable.valueFor( ( unsigned int ) frameBuf );
1317                }
1318            break;
1319        case CameraFrame::VIDEO_FRAME_SYNC:
1320                {
1321                Mutex::Autolock lock(mVideoBufferLock);
1322                res = mVideoBuffersAvailable.valueFor( ( unsigned int ) frameBuf );
1323                }
1324            break;
1325        default:
1326            break;
1327        };
1328
1329    LOG_FUNCTION_NAME_EXIT;
1330
1331    return res;
1332}
1333
1334void BaseCameraAdapter::setFrameRefCount(void* frameBuf, CameraFrame::FrameType frameType, int refCount)
1335{
1336
1337    LOG_FUNCTION_NAME;
1338
1339    switch ( frameType )
1340        {
1341        case CameraFrame::IMAGE_FRAME:
1342        case CameraFrame::RAW_FRAME:
1343                {
1344                Mutex::Autolock lock(mCaptureBufferLock);
1345                mCaptureBuffersAvailable.replaceValueFor(  ( unsigned int ) frameBuf, refCount);
1346                }
1347            break;
1348        case CameraFrame::PREVIEW_FRAME_SYNC:
1349        case CameraFrame::SNAPSHOT_FRAME:
1350                {
1351                Mutex::Autolock lock(mPreviewBufferLock);
1352                mPreviewBuffersAvailable.replaceValueFor(  ( unsigned int ) frameBuf, refCount);
1353                }
1354            break;
1355        case CameraFrame::FRAME_DATA_SYNC:
1356                {
1357                Mutex::Autolock lock(mPreviewDataBufferLock);
1358                mPreviewDataBuffersAvailable.replaceValueFor(  ( unsigned int ) frameBuf, refCount);
1359                }
1360            break;
1361        case CameraFrame::VIDEO_FRAME_SYNC:
1362                {
1363                Mutex::Autolock lock(mVideoBufferLock);
1364                mVideoBuffersAvailable.replaceValueFor(  ( unsigned int ) frameBuf, refCount);
1365                }
1366            break;
1367        default:
1368            break;
1369        };
1370
1371    LOG_FUNCTION_NAME_EXIT;
1372
1373}
1374
1375status_t BaseCameraAdapter::startVideoCapture()
1376{
1377    status_t ret = NO_ERROR;
1378
1379    LOG_FUNCTION_NAME;
1380
1381    Mutex::Autolock lock(mVideoBufferLock);
1382
1383    //If the capture is already ongoing, return from here.
1384    if ( mRecording )
1385        {
1386        ret = NO_INIT;
1387        }
1388
1389
1390    if ( NO_ERROR == ret )
1391        {
1392
1393        for ( unsigned int i = 0 ; i < mPreviewBuffersAvailable.size() ; i++ )
1394            {
1395            mVideoBuffersAvailable.add(mPreviewBuffersAvailable.keyAt(i), 0);
1396            }
1397
1398        mRecording = true;
1399        }
1400
1401    LOG_FUNCTION_NAME_EXIT;
1402
1403    return ret;
1404}
1405
1406status_t BaseCameraAdapter::stopVideoCapture()
1407{
1408    status_t ret = NO_ERROR;
1409
1410    LOG_FUNCTION_NAME;
1411
1412    if ( !mRecording )
1413        {
1414        ret = NO_INIT;
1415        }
1416
1417    if ( NO_ERROR == ret )
1418        {
1419        for ( unsigned int i = 0 ; i < mVideoBuffersAvailable.size() ; i++ )
1420            {
1421            void *frameBuf = ( void * ) mVideoBuffersAvailable.keyAt(i);
1422            if( getFrameRefCount(frameBuf,  CameraFrame::VIDEO_FRAME_SYNC) > 0)
1423                {
1424                returnFrame(frameBuf, CameraFrame::VIDEO_FRAME_SYNC);
1425                }
1426            }
1427
1428        mVideoBuffersAvailable.clear();
1429
1430        mRecording = false;
1431        }
1432
1433    LOG_FUNCTION_NAME_EXIT;
1434
1435    return ret;
1436}
1437
1438//-----------------Stub implementation of the interface ------------------------------
1439
1440status_t BaseCameraAdapter::takePicture()
1441{
1442    status_t ret = NO_ERROR;
1443
1444    LOG_FUNCTION_NAME;
1445
1446    LOG_FUNCTION_NAME_EXIT;
1447
1448    return ret;
1449}
1450
1451status_t BaseCameraAdapter::stopImageCapture()
1452{
1453    status_t ret = NO_ERROR;
1454
1455    LOG_FUNCTION_NAME;
1456
1457    LOG_FUNCTION_NAME_EXIT;
1458
1459    return ret;
1460}
1461
1462status_t BaseCameraAdapter::startBracketing(int range)
1463{
1464    status_t ret = NO_ERROR;
1465
1466    LOG_FUNCTION_NAME;
1467
1468    LOG_FUNCTION_NAME_EXIT;
1469
1470    return ret;
1471}
1472
1473status_t BaseCameraAdapter::stopBracketing()
1474{
1475    status_t ret = NO_ERROR;
1476
1477    LOG_FUNCTION_NAME;
1478
1479    LOG_FUNCTION_NAME_EXIT;
1480
1481    return ret;
1482}
1483
1484status_t BaseCameraAdapter::autoFocus()
1485{
1486    status_t ret = NO_ERROR;
1487
1488    LOG_FUNCTION_NAME;
1489
1490    notifyFocusSubscribers(false);
1491
1492    LOG_FUNCTION_NAME_EXIT;
1493
1494    return ret;
1495}
1496
1497status_t BaseCameraAdapter::cancelAutoFocus()
1498{
1499    status_t ret = NO_ERROR;
1500
1501    LOG_FUNCTION_NAME;
1502
1503    LOG_FUNCTION_NAME_EXIT;
1504
1505    return ret;
1506}
1507
1508status_t BaseCameraAdapter::startSmoothZoom(int targetIdx)
1509{
1510    status_t ret = NO_ERROR;
1511
1512    LOG_FUNCTION_NAME;
1513
1514    LOG_FUNCTION_NAME_EXIT;
1515
1516    return ret;
1517}
1518
1519status_t BaseCameraAdapter::stopSmoothZoom()
1520{
1521    status_t ret = NO_ERROR;
1522
1523    LOG_FUNCTION_NAME;
1524
1525    LOG_FUNCTION_NAME_EXIT;
1526
1527    return ret;
1528}
1529
1530status_t BaseCameraAdapter::startPreview()
1531{
1532    status_t ret = NO_ERROR;
1533
1534    LOG_FUNCTION_NAME;
1535
1536    LOG_FUNCTION_NAME_EXIT;
1537
1538    return ret;
1539}
1540
1541status_t BaseCameraAdapter::stopPreview()
1542{
1543    status_t ret = NO_ERROR;
1544
1545    LOG_FUNCTION_NAME;
1546
1547    LOG_FUNCTION_NAME_EXIT;
1548
1549    return ret;
1550}
1551
1552status_t BaseCameraAdapter::useBuffers(CameraMode mode, void* bufArr, int num, size_t length, unsigned int queueable)
1553{
1554    status_t ret = NO_ERROR;
1555
1556    LOG_FUNCTION_NAME;
1557
1558    LOG_FUNCTION_NAME_EXIT;
1559
1560    return ret;
1561}
1562
1563status_t BaseCameraAdapter::fillThisBuffer(void* frameBuf, CameraFrame::FrameType frameType)
1564{
1565    status_t ret = NO_ERROR;
1566
1567    LOG_FUNCTION_NAME;
1568
1569    LOG_FUNCTION_NAME_EXIT;
1570
1571    return ret;
1572}
1573
1574status_t BaseCameraAdapter::getFrameSize(size_t &width, size_t &height)
1575{
1576    status_t ret = NO_ERROR;
1577
1578    LOG_FUNCTION_NAME;
1579
1580    LOG_FUNCTION_NAME_EXIT;
1581
1582    return ret;
1583}
1584
1585status_t BaseCameraAdapter::getFrameDataSize(size_t &dataFrameSize, size_t bufferCount)
1586{
1587    status_t ret = NO_ERROR;
1588
1589    LOG_FUNCTION_NAME;
1590
1591    LOG_FUNCTION_NAME_EXIT;
1592
1593    return ret;
1594}
1595
1596status_t BaseCameraAdapter::getPictureBufferSize(size_t &length, size_t bufferCount)
1597{
1598    status_t ret = NO_ERROR;
1599
1600    LOG_FUNCTION_NAME;
1601
1602    LOG_FUNCTION_NAME_EXIT;
1603
1604    return ret;
1605}
1606
1607status_t BaseCameraAdapter::startFaceDetection()
1608{
1609    status_t ret = NO_ERROR;
1610
1611    LOG_FUNCTION_NAME;
1612
1613    LOG_FUNCTION_NAME_EXIT;
1614
1615    return ret;
1616}
1617
1618status_t BaseCameraAdapter::stopFaceDetection()
1619{
1620    status_t ret = NO_ERROR;
1621
1622    LOG_FUNCTION_NAME;
1623
1624    LOG_FUNCTION_NAME_EXIT;
1625
1626    return ret;
1627}
1628
1629status_t BaseCameraAdapter::setState(CameraCommands operation)
1630{
1631    status_t ret = NO_ERROR;
1632
1633    LOG_FUNCTION_NAME;
1634
1635    mLock.lock();
1636
1637    switch ( mAdapterState )
1638        {
1639
1640        case INTIALIZED_STATE:
1641
1642            switch ( operation )
1643                {
1644
1645                case CAMERA_USE_BUFFERS_PREVIEW:
1646                    CAMHAL_LOGDB("Adapter state switch INTIALIZED_STATE->LOADED_PREVIEW_STATE event = 0x%x",
1647                                 operation);
1648                    mNextState = LOADED_PREVIEW_STATE;
1649                    break;
1650
1651                //These events don't change the current state
1652                case CAMERA_QUERY_RESOLUTION_PREVIEW:
1653                case CAMERA_QUERY_BUFFER_SIZE_IMAGE_CAPTURE:
1654                case CAMERA_QUERY_BUFFER_SIZE_PREVIEW_DATA:
1655                    CAMHAL_LOGDB("Adapter state switch INTIALIZED_STATE->INTIALIZED_STATE event = 0x%x",
1656                                 operation);
1657                    mNextState = INTIALIZED_STATE;
1658                    break;
1659
1660                default:
1661                    CAMHAL_LOGEB("Adapter state switch INTIALIZED_STATE Invalid Op! event = 0x%x",
1662                                 operation);
1663                    ret = INVALID_OPERATION;
1664                    break;
1665
1666                }
1667
1668            break;
1669
1670        case LOADED_PREVIEW_STATE:
1671
1672            switch ( operation )
1673                {
1674
1675                case CAMERA_START_PREVIEW:
1676                    CAMHAL_LOGDB("Adapter state switch LOADED_PREVIEW_STATE->PREVIEW_STATE event = 0x%x",
1677                                 operation);
1678                    mNextState = PREVIEW_STATE;
1679                    break;
1680
1681                //These events don't change the current state
1682                case CAMERA_QUERY_BUFFER_SIZE_IMAGE_CAPTURE:
1683                case CAMERA_QUERY_BUFFER_SIZE_PREVIEW_DATA:
1684                case CAMERA_USE_BUFFERS_PREVIEW_DATA:
1685                    CAMHAL_LOGDB("Adapter state switch LOADED_PREVIEW_STATE->LOADED_PREVIEW_STATE event = 0x%x",
1686                                 operation);
1687                    mNextState = LOADED_PREVIEW_STATE;
1688                    break;
1689
1690                default:
1691                    CAMHAL_LOGDB("Adapter state switch LOADED_PREVIEW Invalid Op! event = 0x%x",
1692                                 operation);
1693                    ret = INVALID_OPERATION;
1694                    break;
1695
1696                }
1697
1698            break;
1699
1700        case PREVIEW_STATE:
1701
1702            switch ( operation )
1703                {
1704
1705                case CAMERA_STOP_PREVIEW:
1706                    CAMHAL_LOGDB("Adapter state switch PREVIEW_STATE->INTIALIZED_STATE event = 0x%x",
1707                                 operation);
1708                    mNextState = INTIALIZED_STATE;
1709                    break;
1710
1711                case CAMERA_PERFORM_AUTOFOCUS:
1712                    CAMHAL_LOGDB("Adapter state switch PREVIEW_STATE->AF_STATE event = 0x%x",
1713                                 operation);
1714                    mNextState = AF_STATE;
1715                    break;
1716
1717                case CAMERA_START_SMOOTH_ZOOM:
1718                    CAMHAL_LOGDB("Adapter state switch PREVIEW_STATE->ZOOM_STATE event = 0x%x",
1719                                 operation);
1720                    mNextState = ZOOM_STATE;
1721                    break;
1722
1723                case CAMERA_USE_BUFFERS_IMAGE_CAPTURE:
1724                    CAMHAL_LOGDB("Adapter state switch PREVIEW_STATE->LOADED_CAPTURE_STATE event = 0x%x",
1725                                 operation);
1726                    mNextState = LOADED_CAPTURE_STATE;
1727                    break;
1728
1729                case CAMERA_START_VIDEO:
1730                    CAMHAL_LOGDB("Adapter state switch PREVIEW_STATE->VIDEO_STATE event = 0x%x",
1731                                 operation);
1732                    mNextState = VIDEO_STATE;
1733                    break;
1734
1735                case CAMERA_CANCEL_AUTOFOCUS:
1736                case CAMERA_QUERY_BUFFER_SIZE_IMAGE_CAPTURE:
1737                case CAMERA_STOP_SMOOTH_ZOOM:
1738                    CAMHAL_LOGDB("Adapter state switch PREVIEW_ACTIVE->PREVIEW_ACTIVE event = 0x%x",
1739                                 operation);
1740                    mNextState = PREVIEW_STATE;
1741                    break;
1742
1743                default:
1744                    CAMHAL_LOGEB("Adapter state switch PREVIEW_ACTIVE Invalid Op! event = 0x%x",
1745                                 operation);
1746                    ret = INVALID_OPERATION;
1747                    break;
1748
1749                }
1750
1751            break;
1752
1753        case LOADED_CAPTURE_STATE:
1754
1755            switch ( operation )
1756                {
1757
1758                case CAMERA_START_IMAGE_CAPTURE:
1759                    CAMHAL_LOGDB("Adapter state switch LOADED_CAPTURE_STATE->CAPTURE_STATE event = 0x%x",
1760                                 operation);
1761                    mNextState = CAPTURE_STATE;
1762                    break;
1763
1764                case CAMERA_START_BRACKET_CAPTURE:
1765                    CAMHAL_LOGDB("Adapter state switch LOADED_CAPTURE_STATE->BRACKETING_STATE event = 0x%x",
1766                                 operation);
1767                    mNextState = BRACKETING_STATE;
1768                    break;
1769
1770                default:
1771                    CAMHAL_LOGEB("Adapter state switch LOADED_CAPTURE_STATE Invalid Op! event = 0x%x",
1772                                 operation);
1773                    ret = INVALID_OPERATION;
1774                    break;
1775
1776                }
1777
1778            break;
1779
1780        case CAPTURE_STATE:
1781
1782            switch ( operation )
1783                {
1784                case CAMERA_STOP_IMAGE_CAPTURE:
1785                    CAMHAL_LOGDB("Adapter state switch CAPTURE_STATE->PREVIEW_STATE event = 0x%x",
1786                                 operation);
1787                    mNextState = PREVIEW_STATE;
1788                    break;
1789
1790                default:
1791                    CAMHAL_LOGEB("Adapter state switch CAPTURE_STATE Invalid Op! event = 0x%x",
1792                                 operation);
1793                    ret = INVALID_OPERATION;
1794                    break;
1795
1796                }
1797
1798            break;
1799
1800        case BRACKETING_STATE:
1801
1802            switch ( operation )
1803                {
1804
1805                case CAMERA_STOP_BRACKET_CAPTURE:
1806                    CAMHAL_LOGDB("Adapter state switch BRACKETING_STATE->PREVIEW_STATE event = 0x%x",
1807                                 operation);
1808                    mNextState = PREVIEW_STATE;
1809                    break;
1810
1811                case CAMERA_START_IMAGE_CAPTURE:
1812                    CAMHAL_LOGDB("Adapter state switch BRACKETING_STATE->CAPTURE_STATE event = 0x%x",
1813                                 operation);
1814                    mNextState = CAPTURE_STATE;
1815                    break;
1816
1817                default:
1818                    CAMHAL_LOGEB("Adapter state switch BRACKETING_STATE Invalid Op! event = 0x%x",
1819                                 operation);
1820                    ret = INVALID_OPERATION;
1821                    break;
1822
1823                }
1824
1825            break;
1826
1827        case AF_STATE:
1828
1829            switch ( operation )
1830                {
1831
1832                case CAMERA_CANCEL_AUTOFOCUS:
1833                    CAMHAL_LOGDB("Adapter state switch AF_STATE->PREVIEW_STATE event = 0x%x",
1834                                 operation);
1835                    mNextState = PREVIEW_STATE;
1836                    break;
1837
1838                case CAMERA_START_SMOOTH_ZOOM:
1839                    CAMHAL_LOGDB("Adapter state switch AF_STATE->AF_ZOOM_STATE event = 0x%x",
1840                                 operation);
1841                    mNextState = AF_ZOOM_STATE;
1842                    break;
1843
1844                default:
1845                    CAMHAL_LOGEB("Adapter state switch AF_STATE Invalid Op! event = 0x%x",
1846                                 operation);
1847                    ret = INVALID_OPERATION;
1848                    break;
1849
1850                }
1851
1852            break;
1853
1854        case ZOOM_STATE:
1855
1856            switch ( operation )
1857                {
1858
1859                case CAMERA_STOP_SMOOTH_ZOOM:
1860                    CAMHAL_LOGDB("Adapter state switch ZOOM_STATE->PREVIEW_STATE event = 0x%x",
1861                                 operation);
1862                    mNextState = PREVIEW_STATE;
1863                    break;
1864
1865                case CAMERA_PERFORM_AUTOFOCUS:
1866                    CAMHAL_LOGDB("Adapter state switch ZOOM_STATE->AF_ZOOM_STATE event = 0x%x",
1867                                 operation);
1868                    mNextState = AF_ZOOM_STATE;
1869                    break;
1870
1871                case CAMERA_START_VIDEO:
1872                    CAMHAL_LOGDB("Adapter state switch ZOOM_STATE->VIDEO_ZOOM_STATE event = 0x%x",
1873                                 operation);
1874                    mNextState = VIDEO_ZOOM_STATE;
1875                    break;
1876
1877                default:
1878                    CAMHAL_LOGEB("Adapter state switch ZOOM_STATE Invalid Op! event = 0x%x",
1879                                 operation);
1880                    ret = INVALID_OPERATION;
1881                    break;
1882
1883                }
1884
1885            break;
1886
1887        case VIDEO_STATE:
1888
1889            switch ( operation )
1890                {
1891
1892                case CAMERA_STOP_VIDEO:
1893                    CAMHAL_LOGDB("Adapter state switch VIDEO_STATE->PREVIEW_STATE event = 0x%x",
1894                                 operation);
1895                    mNextState = PREVIEW_STATE;
1896                    break;
1897
1898                case CAMERA_START_SMOOTH_ZOOM:
1899                    CAMHAL_LOGDB("Adapter state switch VIDEO_STATE->VIDEO_ZOOM_STATE event = 0x%x",
1900                                 operation);
1901                    mNextState = VIDEO_ZOOM_STATE;
1902                    break;
1903
1904                case CAMERA_USE_BUFFERS_IMAGE_CAPTURE:
1905                    CAMHAL_LOGDB("Adapter state switch VIDEO_STATE->VIDEO_LOADED_CAPTURE_STATE event = 0x%x",
1906                                 operation);
1907                    mNextState = VIDEO_LOADED_CAPTURE_STATE;
1908                    break;
1909
1910                case CAMERA_QUERY_BUFFER_SIZE_IMAGE_CAPTURE:
1911                    CAMHAL_LOGDB("Adapter state switch VIDEO_STATE->VIDEO_STATE event = 0x%x",
1912                                 operation);
1913                    mNextState = VIDEO_STATE;
1914                    break;
1915
1916                default:
1917                    CAMHAL_LOGEB("Adapter state switch VIDEO_STATE Invalid Op! event = 0x%x",
1918                                 operation);
1919                    ret = INVALID_OPERATION;
1920                    break;
1921
1922                }
1923
1924            break;
1925
1926        case VIDEO_LOADED_CAPTURE_STATE:
1927
1928            switch ( operation )
1929                {
1930
1931                case CAMERA_START_IMAGE_CAPTURE:
1932                    CAMHAL_LOGDB("Adapter state switch LOADED_CAPTURE_STATE->CAPTURE_STATE event = 0x%x",
1933                                 operation);
1934                    mNextState = VIDEO_CAPTURE_STATE;
1935                    break;
1936
1937                default:
1938                    CAMHAL_LOGEB("Adapter state switch LOADED_CAPTURE_STATE Invalid Op! event = 0x%x",
1939                                 operation);
1940                    ret = INVALID_OPERATION;
1941                    break;
1942
1943                }
1944
1945            break;
1946
1947        case VIDEO_CAPTURE_STATE:
1948
1949            switch ( operation )
1950                {
1951                case CAMERA_STOP_IMAGE_CAPTURE:
1952                    CAMHAL_LOGDB("Adapter state switch CAPTURE_STATE->PREVIEW_STATE event = 0x%x",
1953                                 operation);
1954                    mNextState = VIDEO_STATE;
1955                    break;
1956
1957                default:
1958                    CAMHAL_LOGEB("Adapter state switch CAPTURE_STATE Invalid Op! event = 0x%x",
1959                                 operation);
1960                    ret = INVALID_OPERATION;
1961                    break;
1962
1963                }
1964
1965            break;
1966
1967        case AF_ZOOM_STATE:
1968
1969            switch ( operation )
1970                {
1971
1972                case CAMERA_STOP_SMOOTH_ZOOM:
1973                    CAMHAL_LOGDB("Adapter state switch AF_ZOOM_STATE->AF_STATE event = 0x%x",
1974                                 operation);
1975                    mNextState = AF_STATE;
1976                    break;
1977
1978                case CAMERA_CANCEL_AUTOFOCUS:
1979                    CAMHAL_LOGDB("Adapter state switch AF_ZOOM_STATE->ZOOM_STATE event = 0x%x",
1980                                 operation);
1981                    mNextState = ZOOM_STATE;
1982                    break;
1983
1984                default:
1985                    CAMHAL_LOGEB("Adapter state switch AF_ZOOM_STATE Invalid Op! event = 0x%x",
1986                                 operation);
1987                    ret = INVALID_OPERATION;
1988                    break;
1989
1990                }
1991
1992            break;
1993
1994        case VIDEO_ZOOM_STATE:
1995
1996            switch ( operation )
1997                {
1998
1999                case CAMERA_STOP_SMOOTH_ZOOM:
2000                    CAMHAL_LOGDB("Adapter state switch VIDEO_ZOOM_STATE->VIDEO_STATE event = 0x%x",
2001                                 operation);
2002                    mNextState = VIDEO_STATE;
2003                    break;
2004
2005                case CAMERA_STOP_VIDEO:
2006                    CAMHAL_LOGDB("Adapter state switch VIDEO_ZOOM_STATE->ZOOM_STATE event = 0x%x",
2007                                 operation);
2008                    mNextState = ZOOM_STATE;
2009                    break;
2010
2011                default:
2012                    CAMHAL_LOGEB("Adapter state switch VIDEO_ZOOM_STATE Invalid Op! event = 0x%x",
2013                                 operation);
2014                    ret = INVALID_OPERATION;
2015                    break;
2016
2017                }
2018
2019            break;
2020
2021        case BRACKETING_ZOOM_STATE:
2022
2023            switch ( operation )
2024                {
2025
2026                case CAMERA_STOP_SMOOTH_ZOOM:
2027                    CAMHAL_LOGDB("Adapter state switch BRACKETING_ZOOM_STATE->BRACKETING_STATE event = 0x%x",
2028                                 operation);
2029                    mNextState = BRACKETING_STATE;
2030                    break;
2031
2032                default:
2033                    CAMHAL_LOGEB("Adapter state switch BRACKETING_ZOOM_STATE Invalid Op! event = 0x%x",
2034                                 operation);
2035                    ret = INVALID_OPERATION;
2036                    break;
2037
2038                }
2039
2040            break;
2041
2042        default:
2043            CAMHAL_LOGEA("Invalid Adapter state!");
2044            ret = INVALID_OPERATION;
2045        }
2046
2047    LOG_FUNCTION_NAME_EXIT;
2048
2049    return ret;
2050}
2051
2052//State transition finished successfully.
2053//Commit the state and unlock the adapter state.
2054status_t BaseCameraAdapter::commitState()
2055{
2056    status_t ret = NO_ERROR;
2057
2058    LOG_FUNCTION_NAME;
2059
2060    mAdapterState = mNextState;
2061
2062    mLock.unlock();
2063
2064    LOG_FUNCTION_NAME_EXIT;
2065
2066    return ret;
2067}
2068
2069status_t BaseCameraAdapter::rollbackState()
2070{
2071    status_t ret = NO_ERROR;
2072
2073    LOG_FUNCTION_NAME;
2074
2075    mNextState = mAdapterState;
2076
2077    mLock.unlock();
2078
2079    LOG_FUNCTION_NAME_EXIT;
2080
2081    return ret;
2082}
2083
2084// getNextState() and getState()
2085// publicly exposed functions to retrieve the adapter states
2086// please notice that these functions are locked
2087CameraAdapter::AdapterState BaseCameraAdapter::getState()
2088{
2089    status_t ret = NO_ERROR;
2090
2091    LOG_FUNCTION_NAME;
2092
2093    Mutex::Autolock lock(mLock);
2094
2095    LOG_FUNCTION_NAME_EXIT;
2096
2097    return mAdapterState;
2098}
2099
2100CameraAdapter::AdapterState BaseCameraAdapter::getNextState()
2101{
2102    status_t ret = NO_ERROR;
2103
2104    LOG_FUNCTION_NAME;
2105
2106    Mutex::Autolock lock(mLock);
2107
2108    LOG_FUNCTION_NAME_EXIT;
2109
2110    return mNextState;
2111}
2112
2113// getNextState() and getState()
2114// internal protected functions to retrieve the adapter states
2115// please notice that these functions are NOT locked to help
2116// internal functions query state in the middle of state
2117// transition
2118status_t BaseCameraAdapter::getState(AdapterState &state)
2119{
2120    status_t ret = NO_ERROR;
2121
2122    LOG_FUNCTION_NAME;
2123
2124    state = mAdapterState;
2125
2126    LOG_FUNCTION_NAME_EXIT;
2127
2128    return ret;
2129}
2130
2131status_t BaseCameraAdapter::getNextState(AdapterState &state)
2132{
2133    status_t ret = NO_ERROR;
2134
2135    LOG_FUNCTION_NAME;
2136
2137    state = mNextState;
2138
2139    LOG_FUNCTION_NAME_EXIT;
2140
2141    return ret;
2142}
2143
2144void BaseCameraAdapter::onOrientationEvent(uint32_t orientation, uint32_t tilt)
2145{
2146    LOG_FUNCTION_NAME;
2147    LOG_FUNCTION_NAME_EXIT;
2148}
2149//-----------------------------------------------------------------------------
2150
2151
2152
2153};
2154
2155/*--------------------Camera Adapter Class ENDS here-----------------------------*/
2156
2157