blob: f3cb20e4cb85f9563ca7d9300f9bfd3f5c1e598c [file] [log] [blame]
Chih-Wei Huang0d92eda2012-02-07 16:55:49 +08001/*
2**
3** Copyright 2008, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#ifndef ANDROID_HARDWARE_CAMERA_HARDWARE_H
19#define ANDROID_HARDWARE_CAMERA_HARDWARE_H
20
21#include <camera/CameraParameters.h>
22#include <hardware/camera.h>
23#include <utils/threads.h>
24#include <utils/threads.h>
25#include "V4L2Camera.h"
26
27namespace android {
28
29class CameraHardware : public camera_device {
30
31public:
32
33 /* Actual handler for camera_device_ops_t::set_preview_window callback.
34 * NOTE: When this method is called the object is locked.
35 * Note that failures in this method are reported as negave EXXX statuses.
36 */
37 status_t setPreviewWindow(struct preview_stream_ops *window);
38
39 /* Actual handler for camera_device_ops_t::set_callbacks callback.
40 * NOTE: When this method is called the object is locked.
41 */
42 void setCallbacks(camera_notify_callback notify_cb,
43 camera_data_callback data_cb,
44 camera_data_timestamp_callback data_cb_timestamp,
45 camera_request_memory get_memory,
46 void* user);
47
48 /* Actual handler for camera_device_ops_t::enable_msg_type callback.
49 * NOTE: When this method is called the object is locked.
50 */
51 void enableMsgType(int32_t msg_type);
52
53 /* Actual handler for camera_device_ops_t::disable_msg_type callback.
54 * NOTE: When this method is called the object is locked.
55 */
56 void disableMsgType(int32_t msg_type);
57
58 /* Actual handler for camera_device_ops_t::msg_type_enabled callback.
59 * NOTE: When this method is called the object is locked.
60 * Return:
61 * 0 if message(s) is (are) disabled, != 0 if enabled.
62 */
63 int isMsgTypeEnabled(int32_t msg_type);
64
65 /* Actual handler for camera_device_ops_t::start_preview callback.
66 * NOTE: When this method is called the object is locked.
67 * Note that failures in this method are reported as negave EXXX statuses.
68 */
69 status_t startPreview();
70
71 /* Actual handler for camera_device_ops_t::stop_preview callback.
72 * NOTE: When this method is called the object is locked.
73 */
74 void stopPreview();
75
76 /* Actual handler for camera_device_ops_t::preview_enabled callback.
77 * NOTE: When this method is called the object is locked.
78 * Return:
79 * 0 if preview is disabled, != 0 if enabled.
80 */
81 int isPreviewEnabled();
82
83 /* Actual handler for camera_device_ops_t::store_meta_data_in_buffers callback.
84 * NOTE: When this method is called the object is locked.
85 * Note that failures in this method are reported as negave EXXX statuses.
86 */
87 status_t storeMetaDataInBuffers(int enable);
88
89 /* Actual handler for camera_device_ops_t::start_recording callback.
90 * NOTE: When this method is called the object is locked.
91 * Note that failures in this method are reported as negave EXXX statuses.
92 */
93 status_t startRecording();
94
95 /* Actual handler for camera_device_ops_t::stop_recording callback.
96 * NOTE: When this method is called the object is locked.
97 */
98 void stopRecording();
99
100 /* Actual handler for camera_device_ops_t::recording_enabled callback.
101 * NOTE: When this method is called the object is locked.
102 * Return:
103 * 0 if recording is disabled, != 0 if enabled.
104 */
105 int isRecordingEnabled();
106
107 /* Actual handler for camera_device_ops_t::release_recording_frame callback.
108 * NOTE: When this method is called the object is locked.
109 */
110 void releaseRecordingFrame(const void* opaque);
111
112 /* Actual handler for camera_device_ops_t::auto_focus callback.
113 * NOTE: When this method is called the object is locked.
114 * Note that failures in this method are reported as negave EXXX statuses.
115 */
116 status_t setAutoFocus();
117
118 /* Actual handler for camera_device_ops_t::cancel_auto_focus callback.
119 * NOTE: When this method is called the object is locked.
120 * Note that failures in this method are reported as negave EXXX statuses.
121 */
122 status_t cancelAutoFocus();
123
124 /* Actual handler for camera_device_ops_t::take_picture callback.
125 * NOTE: When this method is called the object is locked.
126 * Note that failures in this method are reported as negave EXXX statuses.
127 */
128 status_t takePicture();
129
130 /* Actual handler for camera_device_ops_t::cancel_picture callback.
131 * NOTE: When this method is called the object is locked.
132 * Note that failures in this method are reported as negave EXXX statuses.
133 */
134 status_t cancelPicture();
135
136 /* Actual handler for camera_device_ops_t::set_parameters callback.
137 * NOTE: When this method is called the object is locked.
138 * Note that failures in this method are reported as negave EXXX statuses.
139 */
140 status_t setParameters(const char* parms);
141
142 /* Actual handler for camera_device_ops_t::get_parameters callback.
143 * NOTE: When this method is called the object is locked.
144 * Return:
145 * Flattened parameters string. The caller will free the buffer allocated
146 * for the string by calling camera_device_ops_t::put_parameters callback.
147 */
148 char* getParameters();
149
150 /*
151 * Called to free the string returned from camera_device_ops_t::get_parameters
152 * callback. There is nothing more to it: the name of the callback is just
153 * misleading.
154 * NOTE: When this method is called the object is locked.
155 */
156 void putParameters(char* params);
157
158 /* Actual handler for camera_device_ops_t::send_command callback.
159 * NOTE: When this method is called the object is locked.
160 * Note that failures in this method are reported as negave EXXX statuses.
161 */
162 status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
163
164 /* Actual handler for camera_device_ops_t::release callback.
165 * NOTE: When this method is called the object is locked.
166 */
167 void releaseCamera();
168
169
170 status_t dumpCamera(int fd);
171
172private:
173
174 bool PowerOn();
175 bool PowerOff();
176 bool NegotiatePreviewFormat(struct preview_stream_ops* win);
177
178public:
179 /* Constructs Camera instance.
180 * Param:
181 * cameraId - Zero based camera identifier, which is an index of the camera
182 * instance in camera factory's array.
183 * module - Emulated camera HAL module descriptor.
184 */
Andres Rodriguez937775c2012-04-19 23:38:00 +0800185 CameraHardware(const hw_module_t* module, char* devLocation);
Chih-Wei Huang0d92eda2012-02-07 16:55:49 +0800186
187 /* Destructs EmulatedCamera instance. */
188 virtual ~CameraHardware();
189
190 /****************************************************************************
191 * Camera API implementation
192 ***************************************************************************/
193
194public:
195 /* Creates connection to the emulated camera device.
196 * This method is called in response to hw_module_methods_t::open callback.
197 * NOTE: When this method is called the object is locked.
198 * Note that failures in this method are reported as negave EXXX statuses.
199 */
200 status_t connectCamera(hw_device_t** device);
201
202 /* Closes connection to the emulated camera.
203 * This method is called in response to camera_device::close callback.
204 * NOTE: When this method is called the object is locked.
205 * Note that failures in this method are reported as negave EXXX statuses.
206 */
207 status_t closeCamera();
208
209 /* Gets camera information.
210 * This method is called in response to camera_module_t::get_camera_info
211 * callback.
212 * NOTE: When this method is called the object is locked.
213 * Note that failures in this method are reported as negave EXXX statuses.
214 */
Alberto Panizzobbbfb372013-07-06 18:23:53 +0200215 static status_t getCameraInfo(struct camera_info* info, int facing,
216 int orientation);
Chih-Wei Huang0d92eda2012-02-07 16:55:49 +0800217
218private:
219
220 static const int kBufferCount = 4;
221
222 void initDefaultParameters();
223 void initHeapLocked();
224
225 class PreviewThread : public Thread {
226 CameraHardware* mHardware;
227
228 public:
229 PreviewThread(CameraHardware* hw);
230 virtual void onFirstRef();
231 virtual bool threadLoop();
232 };
233
234 status_t startPreviewLocked();
235 void stopPreviewLocked();
236
237 int previewThread();
238
239 static int beginAutoFocusThread(void *cookie);
240 int autoFocusThread();
241
242 static int beginPictureThread(void *cookie);
243 int pictureThread();
244
245 void fillPreviewWindow(uint8_t* yuyv, int srcWidth, int srcHeight);
246
Michael Goffioul8cf00b62018-06-08 13:02:21 -0700247 int getPreviewFrameRate(const CameraParameters& params);
248
Chih-Wei Huang0d92eda2012-02-07 16:55:49 +0800249 mutable Mutex mLock;
250
251 preview_stream_ops* mWin;
252 int mPreviewWinFmt;
253 int mPreviewWinWidth;
254 int mPreviewWinHeight;
255
256 CameraParameters mParameters;
Andres Rodriguez937775c2012-04-19 23:38:00 +0800257 char* mVideoDevice;
Chih-Wei Huang0d92eda2012-02-07 16:55:49 +0800258
259 camera_memory_t* mRawPreviewHeap;
260 int mRawPreviewFrameSize;
261 void* mRawPreviewBuffer;
262 int mRawPreviewWidth;
263 int mRawPreviewHeight;
264
265 camera_memory_t* mPreviewHeap;
266 int mPreviewFrameSize;
267 void* mPreviewBuffer[kBufferCount];
268 int mPreviewFmt;
269
270 camera_memory_t* mRawPictureHeap;
271 void* mRawBuffer;
272 int mRawPictureBufferSize;
273
274 camera_memory_t* mRecordingHeap;
275 void* mRecBuffers[kBufferCount];
276 int mRecordingFrameSize;
277 int mRecFmt;
278
279 camera_memory_t* mJpegPictureHeap;
280 int mJpegPictureBufferSize;
281
282 V4L2Camera camera;
283 bool mRecordingEnabled;
284
285 // protected by mLock
286 sp<PreviewThread> mPreviewThread;
287
288 camera_notify_callback mNotifyCb;
289 camera_data_callback mDataCb;
290 camera_data_timestamp_callback mDataCbTimestamp;
291 camera_request_memory mRequestMemory;
292 void *mCallbackCookie;
293
294 int32_t mMsgEnabled;
295
296 // only used from PreviewThread
297 int mCurrentPreviewFrame;
298 int mCurrentRecordingFrame;
299
Chih-Wei Huang47e3f0e2012-02-07 17:29:05 +0800300 char* mCameraPowerFile;
301
Chih-Wei Huang0d92eda2012-02-07 16:55:49 +0800302 /****************************************************************************
303 * Camera API callbacks as defined by camera_device_ops structure.
304 * See hardware/libhardware/include/hardware/camera.h for information on
305 * each of these callbacks. Implemented in this class, these callbacks simply
306 * dispatch the call into an instance of EmulatedCamera class defined by the
307 * 'camera_device' parameter.
308 ***************************************************************************/
309
310private:
311 static int set_preview_window(struct camera_device* dev,
312 struct preview_stream_ops* window);
313 static void set_callbacks(struct camera_device* dev,
314 camera_notify_callback notify_cb,
315 camera_data_callback data_cb,
316 camera_data_timestamp_callback data_cb_timestamp,
317 camera_request_memory get_memory,
318 void* user);
319 static void enable_msg_type(struct camera_device* dev, int32_t msg_type);
320 static void disable_msg_type(struct camera_device* dev, int32_t msg_type);
321 static int msg_type_enabled(struct camera_device* dev, int32_t msg_type);
322 static int start_preview(struct camera_device* dev);
323 static void stop_preview(struct camera_device* dev);
324 static int preview_enabled(struct camera_device* dev);
325 static int store_meta_data_in_buffers(struct camera_device* dev, int enable);
326 static int start_recording(struct camera_device* dev);
327 static void stop_recording(struct camera_device* dev);
328 static int recording_enabled(struct camera_device* dev);
329 static void release_recording_frame(struct camera_device* dev,
330 const void* opaque);
331 static int auto_focus(struct camera_device* dev);
332 static int cancel_auto_focus(struct camera_device* dev);
333 static int take_picture(struct camera_device* dev);
334 static int cancel_picture(struct camera_device* dev);
335 static int set_parameters(struct camera_device* dev, const char* parms);
336 static char* get_parameters(struct camera_device* dev);
337 static void put_parameters(struct camera_device* dev, char* params);
338 static int send_command(struct camera_device* dev,
339 int32_t cmd,
340 int32_t arg1,
341 int32_t arg2);
342 static void release(struct camera_device* dev);
343 static int dump(struct camera_device* dev, int fd);
344 static int close(struct hw_device_t* device);
345
346private:
347
348 /* Registered callbacks implementing camera API. */
349 static camera_device_ops_t mDeviceOps;
350
351};
352
353}; // namespace android
354
355#endif