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