• 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_FACTORY_H
18 #define HW_EMULATOR_CAMERA_EMULATED_CAMERA_FACTORY_H
19 
20 #include "EmulatedBaseCamera.h"
21 #include "QemuClient.h"
22 
23 #include <cutils/properties.h>
24 
25 #include <utils/RefBase.h>
26 #include <vector>
27 
28 namespace android {
29 
30 class EmulatedCameraHotplugThread;
31 
32 /*
33  * Contains declaration of a class EmulatedCameraFactory that manages cameras
34  * available for the emulation. A global instance of this class is statically
35  * instantiated and initialized when camera emulation HAL is loaded.
36  */
37 
38 /*
39  * Class EmulatedCameraFactoryManages cameras available for the emulation.
40  *
41  * When the global static instance of this class is created on the module load,
42  * it enumerates cameras available for the emulation by connecting to the
43  * emulator's 'camera' service. For every camera found out there it creates an
44  * instance of an appropriate class, and stores it an in array of emulated
45  * cameras. In addition to the cameras reported by the emulator, a fake camera
46  * emulator is always created, so there is always at least one camera that is
47  * available.
48  *
49  * Instance of this class is also used as the entry point for the camera HAL API,
50  * including:
51  *  - hw_module_methods_t::open entry point
52  *  - camera_module_t::get_number_of_cameras entry point
53  *  - camera_module_t::get_camera_info entry point
54  *
55  */
56 class EmulatedCameraFactory {
57 public:
58     /*
59      * Constructs EmulatedCameraFactory instance.
60      * In this constructor the factory will create and initialize a list of
61      * emulated cameras. All errors that occur on this constructor are reported
62      * via mConstructedOK data member of this class.
63      */
64     EmulatedCameraFactory();
65 
66     /*
67      * Destructs EmulatedCameraFactory instance.
68      */
69     ~EmulatedCameraFactory();
70 
71 public:
72     /****************************************************************************
73      * Camera HAL API handlers.
74      ***************************************************************************/
75 
76     /*
77      * Opens (connects to) a camera device.
78      *
79      * This method is called in response to hw_module_methods_t::open callback.
80      */
81     int cameraDeviceOpen(int camera_id, hw_device_t **device);
82 
83     /*
84      * Gets emulated camera information.
85      *
86      * This method is called in response to camera_module_t::get_camera_info
87      * callback.
88      */
89     int getCameraInfo(int camera_id, struct camera_info *info);
90 
91     /*
92      * Sets emulated camera callbacks.
93      *
94      * This method is called in response to camera_module_t::set_callbacks
95      * callback.
96      */
97     int setCallbacks(const camera_module_callbacks_t *callbacks);
98 
99     /*
100      * Fill in vendor tags for the module.
101      *
102      * This method is called in response to camera_module_t::get_vendor_tag_ops
103      * callback.
104      */
105     void getVendorTagOps(vendor_tag_ops_t *ops);
106 
107 public:
108     /****************************************************************************
109      * Camera HAL API callbacks.
110      ***************************************************************************/
111 
112     /*
113      * camera_module_t::get_number_of_cameras callback entry point.
114      */
115     static int get_number_of_cameras(void);
116 
117     /*
118      * camera_module_t::get_camera_info callback entry point.
119      */
120     static int get_camera_info(int camera_id, struct camera_info *info);
121 
122     /*
123      * camera_module_t::set_callbacks callback entry point.
124      */
125     static int set_callbacks(const camera_module_callbacks_t *callbacks);
126 
127     /*
128      * camera_module_t::get_vendor_tag_ops callback entry point.
129      */
130     static void get_vendor_tag_ops(vendor_tag_ops_t *ops);
131 
132     /*
133      * camera_module_t::open_legacy callback entry point.
134      */
135     static int open_legacy(const struct hw_module_t *module, const char *id,
136             uint32_t halVersion, struct hw_device_t **device);
137 
138 private:
139     /*
140      * hw_module_methods_t::open callback entry point.
141      */
142     static int device_open(const hw_module_t *module, const char *name,
143             hw_device_t **device);
144 
145 public:
146     /****************************************************************************
147      * Public API.
148      ***************************************************************************/
149 
150     /*
151      * Gets fake camera orientation.
152      */
getFakeCameraOrientation()153     int getFakeCameraOrientation() {
154         const char *key = "qemu.camera.fake.orientation";
155         int degree = property_get_int32(key, 90);
156         return degree;
157     }
158 
159     /*
160      * Gets qemu camera orientation.
161      */
getQemuCameraOrientation()162     int getQemuCameraOrientation() {
163         const char *key = "qemu.camera.webcam.orientation";
164         int degree = property_get_int32(key, 90);
165         return degree;
166     }
167 
168     /*
169      * Gets number of emulated cameras.
170      */
getEmulatedCameraNum()171     int getEmulatedCameraNum() const {
172         return mEmulatedCameraNum;
173     }
174 
175     /*
176      * Checks whether or not the constructor has succeeded.
177      */
isConstructedOK()178     bool isConstructedOK() const {
179         return mConstructedOK;
180     }
181 
182     void onStatusChanged(int cameraId, int newStatus);
183 
184 private:
185     /****************************************************************************
186      * Private API
187      ***************************************************************************/
188 
189     // For carrying QEMU camera information between methods.
190     struct QemuCameraInfo {
191         char *name;
192         char *frameDims;
193         char *dir;
194     };
195 
196     /*
197      * Args:
198      *     token: token whose value is being searched for.
199      *     s: string containing one or more tokens in the format
200      *        "token_name=token_value".
201      *     value: Output parameter for the value of the token.
202      *
203      * Returns:
204      *     true if the token was successfully parsed.
205      */
206     bool getTokenValue(const char *token, const std::string &s, char **value);
207 
208     /*
209      * Args:
210      *     qemuCameras: Output parameter for the list of detected camera
211      *                  strings. Each camera is represented by a string of three
212      *                  attributes "name=... framedims=... dir=...", not
213      *                  necessarily in that order.
214      */
215     void findQemuCameras(std::vector<QemuCameraInfo> *qemuCameras);
216 
217     /*
218      * Populates emulated cameras array with cameras that are available via
219      * 'camera' service in the emulator. For each such camera, one of the
220      * EmulatedQemuCamera* classes will be created and added to
221      * mEmulatedCameras (based on the HAL version specified in system
222      * properties).
223      */
224     void createQemuCameras(const std::vector<QemuCameraInfo> &qemuCameras);
225 
226     /*
227      * Creates a fake camera and adds it to mEmulatedCameras. If backCamera is
228      * true, it will be created as if it were a camera on the back of the phone.
229      * Otherwise, it will be front-facing.
230      */
231     void createFakeCamera(bool backCamera);
232 
233     /*
234      * Waits till qemu-props has done setup, timeout after 500ms.
235      */
236     void waitForQemuSfFakeCameraPropertyAvailable();
237 
238     /*
239      * Checks if fake camera emulation is on for the camera facing back.
240      */
241     bool isFakeCameraEmulationOn(bool backCamera);
242 
243     /*
244      * Gets camera device version number to use for back camera emulation.
245      */
246     int getCameraHalVersion(bool backCamera);
247 
248 
249 private:
250     /****************************************************************************
251      * Data members.
252      ***************************************************************************/
253 
254     // Connection to the camera service in the emulator.
255     FactoryQemuClient mQemuClient;
256 
257     // Array of cameras available for the emulation.
258     EmulatedBaseCamera **mEmulatedCameras;
259 
260     // Number of emulated cameras (including the fake ones).
261     int mEmulatedCameraNum;
262 
263     // Number of emulated fake cameras.
264     int mFakeCameraNum;
265 
266     // Flags whether or not constructor has succeeded.
267     bool mConstructedOK;
268 
269     // Camera callbacks (for status changing).
270     const camera_module_callbacks_t *mCallbacks;
271 
272     // Hotplug thread (to call onStatusChanged).
273     sp<EmulatedCameraHotplugThread> mHotplugThread;
274 
275 public:
276     // Contains device open entry point, as required by HAL API.
277     static struct hw_module_methods_t mCameraModuleMethods;
278 };
279 
280 }; // end of namespace android
281 
282 // References the global EmulatedCameraFactory instance.
283 extern android::EmulatedCameraFactory gEmulatedCameraFactory;
284 
285 #endif  // HW_EMULATOR_CAMERA_EMULATED_CAMERA_FACTORY_H
286