|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #include "DOMCameraCapabilities.h" |
|
8 #include "nsPIDOMWindow.h" |
|
9 #include "nsContentUtils.h" |
|
10 #include "mozilla/dom/CameraManagerBinding.h" |
|
11 #include "mozilla/dom/CameraCapabilitiesBinding.h" |
|
12 #include "Navigator.h" |
|
13 #include "CameraCommon.h" |
|
14 #include "ICameraControl.h" |
|
15 #include "CameraRecorderProfiles.h" |
|
16 |
|
17 namespace mozilla { |
|
18 namespace dom { |
|
19 |
|
20 NS_IMPL_CYCLE_COLLECTION_CLASS(CameraCapabilities) |
|
21 |
|
22 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(CameraCapabilities) |
|
23 NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow) |
|
24 tmp->mRecorderProfiles = JS::UndefinedValue(); |
|
25 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER |
|
26 NS_IMPL_CYCLE_COLLECTION_UNLINK_END |
|
27 |
|
28 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(CameraCapabilities) |
|
29 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow) |
|
30 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS |
|
31 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END |
|
32 |
|
33 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(CameraCapabilities) |
|
34 NS_IMPL_CYCLE_COLLECTION_TRACE_JSVAL_MEMBER_CALLBACK(mRecorderProfiles) |
|
35 NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER |
|
36 NS_IMPL_CYCLE_COLLECTION_TRACE_END |
|
37 |
|
38 NS_IMPL_CYCLE_COLLECTING_ADDREF(CameraCapabilities) |
|
39 NS_IMPL_CYCLE_COLLECTING_RELEASE(CameraCapabilities) |
|
40 |
|
41 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CameraCapabilities) |
|
42 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY |
|
43 NS_INTERFACE_MAP_ENTRY(nsISupports) |
|
44 NS_INTERFACE_MAP_END |
|
45 |
|
46 /* static */ |
|
47 bool |
|
48 CameraCapabilities::HasSupport(JSContext* aCx, JSObject* aGlobal) |
|
49 { |
|
50 return Navigator::HasCameraSupport(aCx, aGlobal); |
|
51 } |
|
52 |
|
53 CameraCapabilities::CameraCapabilities(nsPIDOMWindow* aWindow) |
|
54 : mRecorderProfiles(JS::UndefinedValue()) |
|
55 , mWindow(aWindow) |
|
56 { |
|
57 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this); |
|
58 MOZ_COUNT_CTOR(CameraCapabilities); |
|
59 mozilla::HoldJSObjects(this); |
|
60 SetIsDOMBinding(); |
|
61 } |
|
62 |
|
63 CameraCapabilities::~CameraCapabilities() |
|
64 { |
|
65 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this); |
|
66 mRecorderProfiles = JS::UndefinedValue(); |
|
67 mozilla::DropJSObjects(this); |
|
68 MOZ_COUNT_DTOR(CameraCapabilities); |
|
69 } |
|
70 |
|
71 JSObject* |
|
72 CameraCapabilities::WrapObject(JSContext* aCx) |
|
73 { |
|
74 return CameraCapabilitiesBinding::Wrap(aCx, this); |
|
75 } |
|
76 |
|
77 #define LOG_IF_ERROR(rv, param) \ |
|
78 do { \ |
|
79 if (NS_FAILED(rv)) { \ |
|
80 DOM_CAMERA_LOGW("Error %x trying to get " #param "\n", \ |
|
81 (rv)); \ |
|
82 } \ |
|
83 } while(0) |
|
84 |
|
85 nsresult |
|
86 CameraCapabilities::TranslateToDictionary(ICameraControl* aCameraControl, |
|
87 uint32_t aKey, nsTArray<CameraSize>& aSizes) |
|
88 { |
|
89 nsresult rv; |
|
90 nsTArray<ICameraControl::Size> sizes; |
|
91 |
|
92 rv = aCameraControl->Get(aKey, sizes); |
|
93 if (NS_FAILED(rv)) { |
|
94 return rv; |
|
95 } |
|
96 |
|
97 aSizes.Clear(); |
|
98 aSizes.SetCapacity(sizes.Length()); |
|
99 for (uint32_t i = 0; i < sizes.Length(); ++i) { |
|
100 CameraSize* s = aSizes.AppendElement(); |
|
101 s->mWidth = sizes[i].width; |
|
102 s->mHeight = sizes[i].height; |
|
103 } |
|
104 |
|
105 return NS_OK; |
|
106 } |
|
107 |
|
108 nsresult |
|
109 CameraCapabilities::Populate(ICameraControl* aCameraControl) |
|
110 { |
|
111 NS_ENSURE_TRUE(aCameraControl, NS_ERROR_INVALID_ARG); |
|
112 |
|
113 nsresult rv; |
|
114 |
|
115 rv = TranslateToDictionary(aCameraControl, CAMERA_PARAM_SUPPORTED_PREVIEWSIZES, mPreviewSizes); |
|
116 LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_PREVIEWSIZES); |
|
117 |
|
118 rv = TranslateToDictionary(aCameraControl, CAMERA_PARAM_SUPPORTED_PICTURESIZES, mPictureSizes); |
|
119 LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_PICTURESIZES); |
|
120 |
|
121 rv = TranslateToDictionary(aCameraControl, CAMERA_PARAM_SUPPORTED_JPEG_THUMBNAIL_SIZES, mThumbnailSizes); |
|
122 LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_JPEG_THUMBNAIL_SIZES); |
|
123 |
|
124 rv = TranslateToDictionary(aCameraControl, CAMERA_PARAM_SUPPORTED_VIDEOSIZES, mVideoSizes); |
|
125 LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_VIDEOSIZES); |
|
126 |
|
127 rv = aCameraControl->Get(CAMERA_PARAM_SUPPORTED_PICTUREFORMATS, mFileFormats); |
|
128 LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_PICTUREFORMATS); |
|
129 |
|
130 rv = aCameraControl->Get(CAMERA_PARAM_SUPPORTED_WHITEBALANCES, mWhiteBalanceModes); |
|
131 LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_WHITEBALANCES); |
|
132 |
|
133 rv = aCameraControl->Get(CAMERA_PARAM_SUPPORTED_SCENEMODES, mSceneModes); |
|
134 LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_SCENEMODES); |
|
135 |
|
136 rv = aCameraControl->Get(CAMERA_PARAM_SUPPORTED_EFFECTS, mEffects); |
|
137 LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_EFFECTS); |
|
138 |
|
139 rv = aCameraControl->Get(CAMERA_PARAM_SUPPORTED_FLASHMODES, mFlashModes); |
|
140 LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_FLASHMODES); |
|
141 |
|
142 rv = aCameraControl->Get(CAMERA_PARAM_SUPPORTED_FOCUSMODES, mFocusModes); |
|
143 LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_FOCUSMODES); |
|
144 |
|
145 rv = aCameraControl->Get(CAMERA_PARAM_SUPPORTED_ISOMODES, mIsoModes); |
|
146 LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_ISOMODES); |
|
147 |
|
148 rv = aCameraControl->Get(CAMERA_PARAM_SUPPORTED_ZOOMRATIOS, mZoomRatios); |
|
149 LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_ZOOMRATIOS); |
|
150 |
|
151 int32_t areas; |
|
152 rv = aCameraControl->Get(CAMERA_PARAM_SUPPORTED_MAXFOCUSAREAS, areas); |
|
153 LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_MAXFOCUSAREAS); |
|
154 mMaxFocusAreas = areas < 0 ? 0 : areas; |
|
155 |
|
156 rv = aCameraControl->Get(CAMERA_PARAM_SUPPORTED_MAXMETERINGAREAS, areas); |
|
157 LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_MAXMETERINGAREAS); |
|
158 mMaxMeteringAreas = areas < 0 ? 0 : areas; |
|
159 |
|
160 int32_t faces; |
|
161 rv = aCameraControl->Get(CAMERA_PARAM_SUPPORTED_MAXDETECTEDFACES, faces); |
|
162 LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_MAXDETECTEDFACES); |
|
163 mMaxDetectedFaces = faces < 0 ? 0 : faces; |
|
164 |
|
165 rv = aCameraControl->Get(CAMERA_PARAM_SUPPORTED_MINEXPOSURECOMPENSATION, mMinExposureCompensation); |
|
166 LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_MINEXPOSURECOMPENSATION); |
|
167 |
|
168 rv = aCameraControl->Get(CAMERA_PARAM_SUPPORTED_MAXEXPOSURECOMPENSATION, mMaxExposureCompensation); |
|
169 LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_MAXEXPOSURECOMPENSATION); |
|
170 |
|
171 rv = aCameraControl->Get(CAMERA_PARAM_SUPPORTED_EXPOSURECOMPENSATIONSTEP, mExposureCompensationStep); |
|
172 LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_EXPOSURECOMPENSATIONSTEP); |
|
173 |
|
174 mRecorderProfileManager = aCameraControl->GetRecorderProfileManager(); |
|
175 if (!mRecorderProfileManager) { |
|
176 DOM_CAMERA_LOGW("Unable to get recorder profile manager\n"); |
|
177 } else { |
|
178 AutoJSContext js; |
|
179 |
|
180 JS::Rooted<JSObject*> o(js); |
|
181 nsresult rv = mRecorderProfileManager->GetJsObject(js, o.address()); |
|
182 if (NS_FAILED(rv)) { |
|
183 DOM_CAMERA_LOGE("Failed to JS-objectify profile manager (%d)\n", rv); |
|
184 return rv; |
|
185 } |
|
186 |
|
187 mRecorderProfiles = JS::ObjectValue(*o); |
|
188 } |
|
189 |
|
190 // For now, always return success, since the presence or absence of capabilities |
|
191 // indicates whether or not they are supported. |
|
192 return NS_OK; |
|
193 } |
|
194 |
|
195 void |
|
196 CameraCapabilities::GetPreviewSizes(nsTArray<dom::CameraSize>& retval) const |
|
197 { |
|
198 retval = mPreviewSizes; |
|
199 } |
|
200 |
|
201 void |
|
202 CameraCapabilities::GetPictureSizes(nsTArray<dom::CameraSize>& retval) const |
|
203 { |
|
204 retval = mPictureSizes; |
|
205 } |
|
206 |
|
207 void |
|
208 CameraCapabilities::GetThumbnailSizes(nsTArray<dom::CameraSize>& retval) const |
|
209 { |
|
210 retval = mThumbnailSizes; |
|
211 } |
|
212 |
|
213 void |
|
214 CameraCapabilities::GetVideoSizes(nsTArray<dom::CameraSize>& retval) const |
|
215 { |
|
216 retval = mVideoSizes; |
|
217 } |
|
218 |
|
219 void |
|
220 CameraCapabilities::GetFileFormats(nsTArray<nsString>& retval) const |
|
221 { |
|
222 retval = mFileFormats; |
|
223 } |
|
224 |
|
225 void |
|
226 CameraCapabilities::GetWhiteBalanceModes(nsTArray<nsString>& retval) const |
|
227 { |
|
228 retval = mWhiteBalanceModes; |
|
229 } |
|
230 |
|
231 void |
|
232 CameraCapabilities::GetSceneModes(nsTArray<nsString>& retval) const |
|
233 { |
|
234 retval = mSceneModes; |
|
235 } |
|
236 |
|
237 void |
|
238 CameraCapabilities::GetEffects(nsTArray<nsString>& retval) const |
|
239 { |
|
240 retval = mEffects; |
|
241 } |
|
242 |
|
243 void |
|
244 CameraCapabilities::GetFlashModes(nsTArray<nsString>& retval) const |
|
245 { |
|
246 retval = mFlashModes; |
|
247 } |
|
248 |
|
249 void |
|
250 CameraCapabilities::GetFocusModes(nsTArray<nsString>& retval) const |
|
251 { |
|
252 retval = mFocusModes; |
|
253 } |
|
254 |
|
255 void |
|
256 CameraCapabilities::GetZoomRatios(nsTArray<double>& retval) const |
|
257 { |
|
258 retval = mZoomRatios; |
|
259 } |
|
260 |
|
261 uint32_t |
|
262 CameraCapabilities::MaxFocusAreas() const |
|
263 { |
|
264 return mMaxFocusAreas; |
|
265 } |
|
266 |
|
267 uint32_t |
|
268 CameraCapabilities::MaxMeteringAreas() const |
|
269 { |
|
270 return mMaxMeteringAreas; |
|
271 } |
|
272 |
|
273 uint32_t |
|
274 CameraCapabilities::MaxDetectedFaces() const |
|
275 { |
|
276 return mMaxDetectedFaces; |
|
277 } |
|
278 |
|
279 double |
|
280 CameraCapabilities::MinExposureCompensation() const |
|
281 { |
|
282 return mMinExposureCompensation; |
|
283 } |
|
284 |
|
285 double |
|
286 CameraCapabilities::MaxExposureCompensation() const |
|
287 { |
|
288 return mMaxExposureCompensation; |
|
289 } |
|
290 |
|
291 double |
|
292 CameraCapabilities::ExposureCompensationStep() const |
|
293 { |
|
294 return mExposureCompensationStep; |
|
295 } |
|
296 |
|
297 void |
|
298 CameraCapabilities::GetRecorderProfiles(JSContext* aCx, |
|
299 JS::MutableHandle<JS::Value> aRetval) const |
|
300 { |
|
301 JS::ExposeValueToActiveJS(mRecorderProfiles); |
|
302 aRetval.set(mRecorderProfiles); |
|
303 } |
|
304 |
|
305 void |
|
306 CameraCapabilities::GetIsoModes(nsTArray<nsString>& retval) const |
|
307 { |
|
308 retval = mIsoModes; |
|
309 } |
|
310 |
|
311 } // namespace dom |
|
312 } // namespace mozilla |