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