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