• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef HW_EMULATOR_CAMERA_EMULATED_CAMERA_H
18 #define HW_EMULATOR_CAMERA_EMULATED_CAMERA_H
19 
20 /*
21  * Contains declaration of a class EmulatedCamera that encapsulates
22  * functionality common to all version 1.0 emulated camera devices ("fake",
23  * "webcam", "video file", etc.).  Instances of this class (for each emulated
24  * camera) are created during the construction of the EmulatedCameraFactory
25  * instance.  This class serves as an entry point for all camera API calls that
26  * defined by camera_device_ops_t API.
27  */
28 
29 #include <camera/CameraParameters.h>
30 #include "EmulatedBaseCamera.h"
31 #include "EmulatedCameraDevice.h"
32 #include "PreviewWindow.h"
33 #include "CallbackNotifier.h"
34 
35 namespace android {
36 
37 /* Encapsulates functionality common to all version 1.0 emulated camera devices
38  * ("fake", "webcam", "file stream", etc.).
39  *
40  * Note that EmulatedCameraFactory instantiates object of this class just once,
41  * when EmulatedCameraFactory instance gets constructed. Connection to /
42  * disconnection from the actual camera device is handled by calls to
43  * connectDevice(), and closeCamera() methods of this class that are ivoked in
44  * response to hw_module_methods_t::open, and camera_device::close callbacks.
45  */
46 class EmulatedCamera : public camera_device, public EmulatedBaseCamera {
47 public:
48     /* Constructs EmulatedCamera instance.
49      * Param:
50      *  cameraId - Zero based camera identifier, which is an index of the camera
51      *      instance in camera factory's array.
52      *  module - Emulated camera HAL module descriptor.
53      */
54     EmulatedCamera(int cameraId,
55                    struct hw_module_t* module);
56 
57     /* Destructs EmulatedCamera instance. */
58     virtual ~EmulatedCamera();
59 
60     /****************************************************************************
61      * Abstract API
62      ***************************************************************************/
63 
64 public:
65     /* Gets emulated camera device used by this instance of the emulated camera.
66      */
67     virtual EmulatedCameraDevice* getCameraDevice() = 0;
68 
69     /****************************************************************************
70      * Public API
71      ***************************************************************************/
72 
73 public:
74     /** Override of base class method */
75     virtual status_t Initialize();
76 
77     /* Next frame is available in the camera device.
78      * This is a notification callback that is invoked by the camera device when
79      * a new frame is available. The captured frame is available through
80      * the |camera_dev| object. Remember to create a
81      * EmulatedCameraDevice::FrameLock instance to lock the frame before
82      * accessing it.
83      * Note that most likely this method is called in context of a worker thread
84      * that camera device has created for frame capturing.
85      * Param:
86      * timestamp - Frame's timestamp.
87      * camera_dev - Camera device instance that delivered the frame.
88      */
89     virtual void onNextFrameAvailable(nsecs_t timestamp,
90                                       EmulatedCameraDevice* camera_dev);
91 
92     /* Entry point for notifications that occur in camera device.
93      * Param:
94      *  err - CAMERA_ERROR_XXX error code.
95      */
96     virtual void onCameraDeviceError(int err);
97 
98     /* Signal to the callback notifier that a pictuer is being taken. */
99     void setTakingPicture(bool takingPicture);
100 
101     /****************************************************************************
102      * Camera API implementation
103      ***************************************************************************/
104 
105 public:
106     /** Override of base class method */
107     virtual status_t connectCamera(hw_device_t** device);
108 
109     /** Override of base class method */
110     virtual status_t closeCamera();
111 
112     /** Override of base class method */
113     virtual status_t getCameraInfo(struct camera_info* info);
114 
115     /****************************************************************************
116      * Camera API implementation.
117      * These methods are called from the camera API callback routines.
118      ***************************************************************************/
119 
120 public:
121     /* Signal that a requested auto-focus has completed. This will be called
122      * from the camera device's worker thread. */
123     void autoFocusComplete();
124 
125 protected:
126     /* Actual handler for camera_device_ops_t::set_preview_window callback.
127      * NOTE: When this method is called the object is locked.
128      * Note that failures in this method are reported as negave EXXX statuses.
129      */
130     virtual status_t setPreviewWindow(struct preview_stream_ops *window);
131 
132     /* Actual handler for camera_device_ops_t::set_callbacks callback.
133      * NOTE: When this method is called the object is locked.
134      */
135     virtual void setCallbacks(camera_notify_callback notify_cb,
136                               camera_data_callback data_cb,
137                               camera_data_timestamp_callback data_cb_timestamp,
138                               camera_request_memory get_memory,
139                               void* user);
140 
141     /* Actual handler for camera_device_ops_t::enable_msg_type callback.
142      * NOTE: When this method is called the object is locked.
143      */
144     virtual void enableMsgType(int32_t msg_type);
145 
146     /* Actual handler for camera_device_ops_t::disable_msg_type callback.
147      * NOTE: When this method is called the object is locked.
148      */
149     virtual void disableMsgType(int32_t msg_type);
150 
151     /* Actual handler for camera_device_ops_t::msg_type_enabled callback.
152      * NOTE: When this method is called the object is locked.
153      * Return:
154      *  0 if message(s) is (are) disabled, != 0 if enabled.
155      */
156     virtual int isMsgTypeEnabled(int32_t msg_type);
157 
158     /* Actual handler for camera_device_ops_t::start_preview 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     virtual status_t startPreview();
163 
164     /* Actual handler for camera_device_ops_t::stop_preview callback.
165      * NOTE: When this method is called the object is locked.
166      */
167     virtual void stopPreview();
168 
169     /* Actual handler for camera_device_ops_t::preview_enabled callback.
170      * NOTE: When this method is called the object is locked.
171      * Return:
172      *  0 if preview is disabled, != 0 if enabled.
173      */
174     virtual int isPreviewEnabled();
175 
176     /* Actual handler for camera_device_ops_t::store_meta_data_in_buffers callback.
177      * NOTE: When this method is called the object is locked.
178      * Note that failures in this method are reported as negave EXXX statuses.
179      */
180     virtual status_t storeMetaDataInBuffers(int enable);
181 
182     /* Actual handler for camera_device_ops_t::start_recording callback.
183      * NOTE: When this method is called the object is locked.
184      * Note that failures in this method are reported as negave EXXX statuses.
185      */
186     virtual status_t startRecording();
187 
188     /* Actual handler for camera_device_ops_t::stop_recording callback.
189      * NOTE: When this method is called the object is locked.
190      */
191     virtual void stopRecording();
192 
193     /* Actual handler for camera_device_ops_t::recording_enabled callback.
194      * NOTE: When this method is called the object is locked.
195      * Return:
196      *  0 if recording is disabled, != 0 if enabled.
197      */
198     virtual int isRecordingEnabled();
199 
200     /* Actual handler for camera_device_ops_t::release_recording_frame callback.
201      * NOTE: When this method is called the object is locked.
202      */
203     virtual void releaseRecordingFrame(const void* opaque);
204 
205     /* Actual handler for camera_device_ops_t::auto_focus callback.
206      * NOTE: When this method is called the object is locked.
207      * Note that failures in this method are reported as negave EXXX statuses.
208      */
209     virtual status_t setAutoFocus();
210 
211     /* Actual handler for camera_device_ops_t::cancel_auto_focus 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      */
215     virtual status_t cancelAutoFocus();
216 
217     /* Actual handler for camera_device_ops_t::take_picture callback.
218      * NOTE: When this method is called the object is locked.
219      * Note that failures in this method are reported as negave EXXX statuses.
220      */
221     virtual status_t takePicture();
222 
223     /* Actual handler for camera_device_ops_t::cancel_picture callback.
224      * NOTE: When this method is called the object is locked.
225      * Note that failures in this method are reported as negave EXXX statuses.
226      */
227     virtual status_t cancelPicture();
228 
229     /* Actual handler for camera_device_ops_t::set_parameters callback.
230      * NOTE: When this method is called the object is locked.
231      * Note that failures in this method are reported as negave EXXX statuses.
232      */
233     virtual status_t setParameters(const char* parms);
234 
235     /* Actual handler for camera_device_ops_t::get_parameters callback.
236      * NOTE: When this method is called the object is locked.
237      * Return:
238      *  Flattened parameters string. The caller will free the buffer allocated
239      *  for the string by calling camera_device_ops_t::put_parameters callback.
240      */
241     virtual char* getParameters();
242 
243     /* Actual handler for camera_device_ops_t::put_parameters callback.
244      * Called to free the string returned from camera_device_ops_t::get_parameters
245      * callback. There is nothing more to it: the name of the callback is just
246      * misleading.
247      * NOTE: When this method is called the object is locked.
248      */
249     virtual void putParameters(char* params);
250 
251     /* Actual handler for camera_device_ops_t::send_command callback.
252      * NOTE: When this method is called the object is locked.
253      * Note that failures in this method are reported as negave EXXX statuses.
254      */
255     virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
256 
257     /* Actual handler for camera_device_ops_t::release callback.
258      * NOTE: When this method is called the object is locked.
259      */
260     virtual void releaseCamera();
261 
262     /* Actual handler for camera_device_ops_t::dump callback.
263      * NOTE: When this method is called the object is locked.
264      * Note that failures in this method are reported as negave EXXX statuses.
265      */
266     virtual status_t dumpCamera(int fd);
267 
268     /****************************************************************************
269      * Preview management.
270      ***************************************************************************/
271 
272 protected:
273     /* Starts preview.
274      * Note that when this method is called mPreviewWindow may be NULL,
275      * indicating that framework has an intention to start displaying video
276      * frames, but didn't create the preview window yet.
277      * Return:
278      *  NO_ERROR on success, or an appropriate error status on failure.
279      */
280     virtual status_t doStartPreview();
281 
282     /* Stops preview.
283      * This method reverts DoStartPreview.
284      * Return:
285      *  NO_ERROR on success, or an appropriate error status on failure.
286      */
287     virtual status_t doStopPreview();
288 
289     /****************************************************************************
290      * Private API.
291      ***************************************************************************/
292 
293 protected:
294     /* Cleans up camera when released. */
295     virtual status_t cleanupCamera();
296 
297 private:
298     status_t getConfiguredPixelFormat(uint32_t* pixelFormat) const;
299     status_t getConfiguredFrameSize(int* width, int* height) const;
300 
301     /****************************************************************************
302      * Camera API callbacks as defined by camera_device_ops structure.
303      * See hardware/libhardware/include/hardware/camera.h for information on
304      * each of these callbacks. Implemented in this class, these callbacks simply
305      * dispatch the call into an instance of EmulatedCamera class defined by the
306      * 'camera_device' parameter.
307      ***************************************************************************/
308 
309 private:
310     static int set_preview_window(struct camera_device* dev,
311                                    struct preview_stream_ops* window);
312 
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 
320     static void enable_msg_type(struct camera_device* dev, int32_t msg_type);
321 
322     static void disable_msg_type(struct camera_device* dev, int32_t msg_type);
323 
324     static int msg_type_enabled(struct camera_device* dev, int32_t msg_type);
325 
326     static int start_preview(struct camera_device* dev);
327 
328     static void stop_preview(struct camera_device* dev);
329 
330     static int preview_enabled(struct camera_device* dev);
331 
332     static int store_meta_data_in_buffers(struct camera_device* dev, int enable);
333 
334     static int start_recording(struct camera_device* dev);
335 
336     static void stop_recording(struct camera_device* dev);
337 
338     static int recording_enabled(struct camera_device* dev);
339 
340     static void release_recording_frame(struct camera_device* dev,
341                                         const void* opaque);
342 
343     static int auto_focus(struct camera_device* dev);
344 
345     static int cancel_auto_focus(struct camera_device* dev);
346 
347     static int take_picture(struct camera_device* dev);
348 
349     static int cancel_picture(struct camera_device* dev);
350 
351     static int set_parameters(struct camera_device* dev, const char* parms);
352 
353     static char* get_parameters(struct camera_device* dev);
354 
355     static void put_parameters(struct camera_device* dev, char* params);
356 
357     static int send_command(struct camera_device* dev,
358                             int32_t cmd,
359                             int32_t arg1,
360                             int32_t arg2);
361 
362     static void release(struct camera_device* dev);
363 
364     static int dump(struct camera_device* dev, int fd);
365 
366     static int close(struct hw_device_t* device);
367 
368     /****************************************************************************
369      * Data members
370      ***************************************************************************/
371 
372 protected:
373     /* Locks this instance for parameters, state, etc. change. */
374     Mutex                           mObjectLock;
375 
376     /* Camera parameters. */
377     CameraParameters                mParameters;
378 
379     /* Preview window. */
380     PreviewWindow                   mPreviewWindow;
381 
382     /* Callback notifier. */
383     CallbackNotifier                mCallbackNotifier;
384 
385 private:
386     /* Registered callbacks implementing camera API. */
387     static camera_device_ops_t      mDeviceOps;
388 
389     /****************************************************************************
390      * Common keys
391      ***************************************************************************/
392 
393 public:
394     static const char FACING_KEY[];
395     static const char ORIENTATION_KEY[];
396     static const char RECORDING_HINT_KEY[];
397 
398      /****************************************************************************
399      * Common string values
400      ***************************************************************************/
401 
402     /* Possible values for FACING_KEY */
403     static const char FACING_BACK[];
404     static const char FACING_FRONT[];
405 };
406 
407 }; /* namespace android */
408 
409 #endif  /* HW_EMULATOR_CAMERA_EMULATED_CAMERA_H */
410