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#ifndef BASE_CAMERA_ADAPTER_H
20#define BASE_CAMERA_ADAPTER_H
21
22#include "CameraHal.h"
23
24namespace android {
25
26class BaseCameraAdapter : public CameraAdapter
27{
28
29public:
30
31    BaseCameraAdapter();
32    virtual ~BaseCameraAdapter();
33
34    ///Initialzes the camera adapter creates any resources required
35    virtual status_t initialize(CameraProperties::Properties*) = 0;
36
37    virtual int setErrorHandler(ErrorNotifier *errorNotifier);
38
39    //Message/Frame notification APIs
40    virtual void enableMsgType(int32_t msgs, frame_callback callback=NULL, event_callback eventCb=NULL, void* cookie=NULL);
41    virtual void disableMsgType(int32_t msgs, void* cookie);
42    virtual void returnFrame(void * frameBuf, CameraFrame::FrameType frameType);
43    virtual void addFramePointers(void *frameBuf, void *y_uv);
44    virtual void removeFramePointers();
45
46    //APIs to configure Camera adapter and get the current parameter set
47    virtual status_t setParameters(const CameraParameters& params) = 0;
48    virtual void getParameters(CameraParameters& params)  = 0;
49
50    //API to send a command to the camera
51    virtual status_t sendCommand(CameraCommands operation, int value1 = 0, int value2 = 0, int value3 = 0 );
52
53    virtual status_t registerImageReleaseCallback(release_image_buffers_callback callback, void *user_data);
54
55    virtual status_t registerEndCaptureCallback(end_image_capture_callback callback, void *user_data);
56
57    //Retrieves the current Adapter state
58    virtual AdapterState getState();
59    //Retrieves the next Adapter state
60    virtual AdapterState getNextState();
61
62    // Rolls the state machine back to INTIALIZED_STATE from the current state
63    virtual status_t rollbackToInitializedState();
64
65protected:
66    //The first two methods will try to switch the adapter state.
67    //Every call to setState() should be followed by a corresponding
68    //call to commitState(). If the state switch fails, then it will
69    //get reset to the previous state via rollbackState().
70    virtual status_t setState(CameraCommands operation);
71    virtual status_t commitState();
72    virtual status_t rollbackState();
73
74    // Retrieves the current Adapter state - for internal use (not locked)
75    virtual status_t getState(AdapterState &state);
76    // Retrieves the next Adapter state - for internal use (not locked)
77    virtual status_t getNextState(AdapterState &state);
78
79    //-----------Interface that needs to be implemented by deriving classes --------------------
80
81    //Should be implmented by deriving classes in order to start image capture
82    virtual status_t takePicture();
83
84    //Should be implmented by deriving classes in order to start image capture
85    virtual status_t stopImageCapture();
86
87    //Should be implmented by deriving classes in order to start temporal bracketing
88    virtual status_t startBracketing(int range);
89
90    //Should be implemented by deriving classes in order to stop temporal bracketing
91    virtual status_t stopBracketing();
92
93    //Should be implemented by deriving classes in oder to initiate autoFocus
94    virtual status_t autoFocus();
95
96    //Should be implemented by deriving classes in oder to initiate autoFocus
97    virtual status_t cancelAutoFocus();
98
99    //Should be called by deriving classes in order to do some bookkeeping
100    virtual status_t startVideoCapture();
101
102    //Should be called by deriving classes in order to do some bookkeeping
103    virtual status_t stopVideoCapture();
104
105    //Should be implemented by deriving classes in order to start camera preview
106    virtual status_t startPreview();
107
108    //Should be implemented by deriving classes in order to stop camera preview
109    virtual status_t stopPreview();
110
111    //Should be implemented by deriving classes in order to start smooth zoom
112    virtual status_t startSmoothZoom(int targetIdx);
113
114    //Should be implemented by deriving classes in order to stop smooth zoom
115    virtual status_t stopSmoothZoom();
116
117    //Should be implemented by deriving classes in order to stop smooth zoom
118    virtual status_t useBuffers(CameraMode mode, void* bufArr, int num, size_t length, unsigned int queueable);
119
120    //Should be implemented by deriving classes in order queue a released buffer in CameraAdapter
121    virtual status_t fillThisBuffer(void* frameBuf, CameraFrame::FrameType frameType);
122
123    //API to get the frame size required to be allocated. This size is used to override the size passed
124    //by camera service when VSTAB/VNF is turned ON for example
125    virtual status_t getFrameSize(size_t &width, size_t &height);
126
127    //API to get required data frame size
128    virtual status_t getFrameDataSize(size_t &dataFrameSize, size_t bufferCount);
129
130    //API to get required picture buffers size with the current configuration in CameraParameters
131    virtual status_t getPictureBufferSize(size_t &length, size_t bufferCount);
132
133    // Should be implemented by deriving classes in order to start face detection
134    // ( if supported )
135    virtual status_t startFaceDetection();
136
137    // Should be implemented by deriving classes in order to stop face detection
138    // ( if supported )
139    virtual status_t stopFaceDetection();
140
141    virtual status_t switchToExecuting();
142
143    // Receive orientation events from CameraHal
144    virtual void onOrientationEvent(uint32_t orientation, uint32_t tilt);
145
146    // ---------------------Interface ends-----------------------------------
147
148    status_t notifyFocusSubscribers(CameraHalEvent::FocusStatus status);
149    status_t notifyShutterSubscribers();
150    status_t notifyZoomSubscribers(int zoomIdx, bool targetReached);
151    status_t notifyFaceSubscribers(sp<CameraFDResult> &faces);
152
153    //Send the frame to subscribers
154    status_t sendFrameToSubscribers(CameraFrame *frame);
155
156    //Resets the refCount for this particular frame
157    status_t resetFrameRefCount(CameraFrame &frame);
158
159    //A couple of helper functions
160    void setFrameRefCount(void* frameBuf, CameraFrame::FrameType frameType, int refCount);
161    int getFrameRefCount(void* frameBuf, CameraFrame::FrameType frameType);
162    int setInitFrameRefCount(void* buf, unsigned int mask);
163
164// private member functions
165private:
166    status_t __sendFrameToSubscribers(CameraFrame* frame,
167                                      KeyedVector<int, frame_callback> *subscribers,
168                                      CameraFrame::FrameType frameType);
169    status_t rollbackToPreviousState();
170
171// protected data types and variables
172protected:
173    enum FrameState {
174        STOPPED = 0,
175        RUNNING
176    };
177
178    enum FrameCommands {
179        START_PREVIEW = 0,
180        START_RECORDING,
181        RETURN_FRAME,
182        STOP_PREVIEW,
183        STOP_RECORDING,
184        DO_AUTOFOCUS,
185        TAKE_PICTURE,
186        FRAME_EXIT
187    };
188
189    enum AdapterCommands {
190        ACK = 0,
191        ERROR
192    };
193
194#if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS
195
196    struct timeval mStartFocus;
197    struct timeval mStartCapture;
198
199#endif
200
201    mutable Mutex mReturnFrameLock;
202
203    //Lock protecting the Adapter state
204    mutable Mutex mLock;
205    AdapterState mAdapterState;
206    AdapterState mNextState;
207
208    //Different frame subscribers get stored using these
209    KeyedVector<int, frame_callback> mFrameSubscribers;
210    KeyedVector<int, frame_callback> mFrameDataSubscribers;
211    KeyedVector<int, frame_callback> mVideoSubscribers;
212    KeyedVector<int, frame_callback> mImageSubscribers;
213    KeyedVector<int, frame_callback> mRawSubscribers;
214    KeyedVector<int, event_callback> mFocusSubscribers;
215    KeyedVector<int, event_callback> mZoomSubscribers;
216    KeyedVector<int, event_callback> mShutterSubscribers;
217    KeyedVector<int, event_callback> mFaceSubscribers;
218
219    //Preview buffer management data
220    int *mPreviewBuffers;
221    int mPreviewBufferCount;
222    size_t mPreviewBuffersLength;
223    KeyedVector<int, int> mPreviewBuffersAvailable;
224    mutable Mutex mPreviewBufferLock;
225
226    //Video buffer management data
227    int *mVideoBuffers;
228    KeyedVector<int, int> mVideoBuffersAvailable;
229    int mVideoBuffersCount;
230    size_t mVideoBuffersLength;
231    mutable Mutex mVideoBufferLock;
232
233    //Image buffer management data
234    int *mCaptureBuffers;
235    KeyedVector<int, bool> mCaptureBuffersAvailable;
236    int mCaptureBuffersCount;
237    size_t mCaptureBuffersLength;
238    mutable Mutex mCaptureBufferLock;
239
240    //Metadata buffermanagement
241    int *mPreviewDataBuffers;
242    KeyedVector<int, bool> mPreviewDataBuffersAvailable;
243    int mPreviewDataBuffersCount;
244    size_t mPreviewDataBuffersLength;
245    mutable Mutex mPreviewDataBufferLock;
246
247    TIUTILS::MessageQueue mFrameQ;
248    TIUTILS::MessageQueue mAdapterQ;
249    mutable Mutex mSubscriberLock;
250    ErrorNotifier *mErrorNotifier;
251    release_image_buffers_callback mReleaseImageBuffersCallback;
252    end_image_capture_callback mEndImageCaptureCallback;
253    void *mReleaseData;
254    void *mEndCaptureData;
255    bool mRecording;
256
257    uint32_t mFramesWithDucati;
258    uint32_t mFramesWithDisplay;
259    uint32_t mFramesWithEncoder;
260
261#ifdef DEBUG_LOG
262    KeyedVector<int, bool> mBuffersWithDucati;
263#endif
264
265    KeyedVector<void *, CameraFrame *> mFrameQueue;
266};
267
268};
269
270#endif //BASE_CAMERA_ADAPTER_H
271
272
273