dom/camera/DOMCameraCapabilities.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/camera/DOMCameraCapabilities.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,312 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "DOMCameraCapabilities.h"
    1.11 +#include "nsPIDOMWindow.h"
    1.12 +#include "nsContentUtils.h"
    1.13 +#include "mozilla/dom/CameraManagerBinding.h"
    1.14 +#include "mozilla/dom/CameraCapabilitiesBinding.h"
    1.15 +#include "Navigator.h"
    1.16 +#include "CameraCommon.h"
    1.17 +#include "ICameraControl.h"
    1.18 +#include "CameraRecorderProfiles.h"
    1.19 +
    1.20 +namespace mozilla {
    1.21 +namespace dom {
    1.22 +
    1.23 +NS_IMPL_CYCLE_COLLECTION_CLASS(CameraCapabilities)
    1.24 +
    1.25 +NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(CameraCapabilities)
    1.26 +  NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow)
    1.27 +  tmp->mRecorderProfiles = JS::UndefinedValue();
    1.28 +  NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
    1.29 +NS_IMPL_CYCLE_COLLECTION_UNLINK_END
    1.30 +
    1.31 +NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(CameraCapabilities)
    1.32 +  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow)
    1.33 +  NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
    1.34 +NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
    1.35 +
    1.36 +NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(CameraCapabilities)
    1.37 +  NS_IMPL_CYCLE_COLLECTION_TRACE_JSVAL_MEMBER_CALLBACK(mRecorderProfiles)
    1.38 +  NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
    1.39 +NS_IMPL_CYCLE_COLLECTION_TRACE_END
    1.40 +
    1.41 +NS_IMPL_CYCLE_COLLECTING_ADDREF(CameraCapabilities)
    1.42 +NS_IMPL_CYCLE_COLLECTING_RELEASE(CameraCapabilities)
    1.43 +
    1.44 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CameraCapabilities)
    1.45 +  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
    1.46 +  NS_INTERFACE_MAP_ENTRY(nsISupports)
    1.47 +NS_INTERFACE_MAP_END
    1.48 +
    1.49 +/* static */
    1.50 +bool
    1.51 +CameraCapabilities::HasSupport(JSContext* aCx, JSObject* aGlobal)
    1.52 +{
    1.53 +  return Navigator::HasCameraSupport(aCx, aGlobal);
    1.54 +}
    1.55 +
    1.56 +CameraCapabilities::CameraCapabilities(nsPIDOMWindow* aWindow)
    1.57 +  : mRecorderProfiles(JS::UndefinedValue())
    1.58 +  , mWindow(aWindow)
    1.59 +{
    1.60 +  DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
    1.61 +  MOZ_COUNT_CTOR(CameraCapabilities);
    1.62 +  mozilla::HoldJSObjects(this);
    1.63 +  SetIsDOMBinding();
    1.64 +}
    1.65 +
    1.66 +CameraCapabilities::~CameraCapabilities()
    1.67 +{
    1.68 +  DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
    1.69 +  mRecorderProfiles = JS::UndefinedValue();
    1.70 +  mozilla::DropJSObjects(this);
    1.71 +  MOZ_COUNT_DTOR(CameraCapabilities);
    1.72 +}
    1.73 +
    1.74 +JSObject*
    1.75 +CameraCapabilities::WrapObject(JSContext* aCx)
    1.76 +{
    1.77 +  return CameraCapabilitiesBinding::Wrap(aCx, this);
    1.78 +}
    1.79 +
    1.80 +#define LOG_IF_ERROR(rv, param)                               \
    1.81 +  do {                                                        \
    1.82 +    if (NS_FAILED(rv)) {                                      \
    1.83 +      DOM_CAMERA_LOGW("Error %x trying to get " #param "\n",  \
    1.84 +        (rv));                                                \
    1.85 +    }                                                         \
    1.86 +  } while(0)
    1.87 +
    1.88 +nsresult
    1.89 +CameraCapabilities::TranslateToDictionary(ICameraControl* aCameraControl,
    1.90 +                                          uint32_t aKey, nsTArray<CameraSize>& aSizes)
    1.91 +{
    1.92 +  nsresult rv;
    1.93 +  nsTArray<ICameraControl::Size> sizes;
    1.94 +
    1.95 +  rv = aCameraControl->Get(aKey, sizes);
    1.96 +  if (NS_FAILED(rv)) {
    1.97 +    return rv;
    1.98 +  }
    1.99 +
   1.100 +  aSizes.Clear();
   1.101 +  aSizes.SetCapacity(sizes.Length());
   1.102 +  for (uint32_t i = 0; i < sizes.Length(); ++i) {
   1.103 +    CameraSize* s = aSizes.AppendElement();
   1.104 +    s->mWidth = sizes[i].width;
   1.105 +    s->mHeight = sizes[i].height;
   1.106 +  }
   1.107 +
   1.108 +  return NS_OK;
   1.109 +}
   1.110 +
   1.111 +nsresult
   1.112 +CameraCapabilities::Populate(ICameraControl* aCameraControl)
   1.113 +{
   1.114 +  NS_ENSURE_TRUE(aCameraControl, NS_ERROR_INVALID_ARG);
   1.115 +
   1.116 +  nsresult rv;
   1.117 +
   1.118 +  rv = TranslateToDictionary(aCameraControl, CAMERA_PARAM_SUPPORTED_PREVIEWSIZES, mPreviewSizes);
   1.119 +  LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_PREVIEWSIZES);
   1.120 +
   1.121 +  rv = TranslateToDictionary(aCameraControl, CAMERA_PARAM_SUPPORTED_PICTURESIZES, mPictureSizes);
   1.122 +  LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_PICTURESIZES);
   1.123 +
   1.124 +  rv = TranslateToDictionary(aCameraControl, CAMERA_PARAM_SUPPORTED_JPEG_THUMBNAIL_SIZES, mThumbnailSizes);
   1.125 +  LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_JPEG_THUMBNAIL_SIZES);
   1.126 +
   1.127 +  rv = TranslateToDictionary(aCameraControl, CAMERA_PARAM_SUPPORTED_VIDEOSIZES, mVideoSizes);
   1.128 +  LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_VIDEOSIZES);
   1.129 +
   1.130 +  rv = aCameraControl->Get(CAMERA_PARAM_SUPPORTED_PICTUREFORMATS, mFileFormats);
   1.131 +  LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_PICTUREFORMATS);
   1.132 +
   1.133 +  rv = aCameraControl->Get(CAMERA_PARAM_SUPPORTED_WHITEBALANCES, mWhiteBalanceModes);
   1.134 +  LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_WHITEBALANCES);
   1.135 +
   1.136 +  rv = aCameraControl->Get(CAMERA_PARAM_SUPPORTED_SCENEMODES, mSceneModes);
   1.137 +  LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_SCENEMODES);
   1.138 +
   1.139 +  rv = aCameraControl->Get(CAMERA_PARAM_SUPPORTED_EFFECTS, mEffects);
   1.140 +  LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_EFFECTS);
   1.141 +
   1.142 +  rv = aCameraControl->Get(CAMERA_PARAM_SUPPORTED_FLASHMODES, mFlashModes);
   1.143 +  LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_FLASHMODES);
   1.144 +
   1.145 +  rv = aCameraControl->Get(CAMERA_PARAM_SUPPORTED_FOCUSMODES, mFocusModes);
   1.146 +  LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_FOCUSMODES);
   1.147 +
   1.148 +  rv = aCameraControl->Get(CAMERA_PARAM_SUPPORTED_ISOMODES, mIsoModes);
   1.149 +  LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_ISOMODES);
   1.150 +
   1.151 +  rv = aCameraControl->Get(CAMERA_PARAM_SUPPORTED_ZOOMRATIOS, mZoomRatios);
   1.152 +  LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_ZOOMRATIOS);
   1.153 +
   1.154 +  int32_t areas;
   1.155 +  rv = aCameraControl->Get(CAMERA_PARAM_SUPPORTED_MAXFOCUSAREAS, areas);
   1.156 +  LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_MAXFOCUSAREAS);
   1.157 +  mMaxFocusAreas = areas < 0 ? 0 : areas;
   1.158 +
   1.159 +  rv = aCameraControl->Get(CAMERA_PARAM_SUPPORTED_MAXMETERINGAREAS, areas);
   1.160 +  LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_MAXMETERINGAREAS);
   1.161 +  mMaxMeteringAreas = areas < 0 ? 0 : areas;
   1.162 +
   1.163 +  int32_t faces;
   1.164 +  rv = aCameraControl->Get(CAMERA_PARAM_SUPPORTED_MAXDETECTEDFACES, faces);
   1.165 +  LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_MAXDETECTEDFACES);
   1.166 +  mMaxDetectedFaces = faces < 0 ? 0 : faces;
   1.167 +
   1.168 +  rv = aCameraControl->Get(CAMERA_PARAM_SUPPORTED_MINEXPOSURECOMPENSATION, mMinExposureCompensation);
   1.169 +  LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_MINEXPOSURECOMPENSATION);
   1.170 +
   1.171 +  rv = aCameraControl->Get(CAMERA_PARAM_SUPPORTED_MAXEXPOSURECOMPENSATION, mMaxExposureCompensation);
   1.172 +  LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_MAXEXPOSURECOMPENSATION);
   1.173 +
   1.174 +  rv = aCameraControl->Get(CAMERA_PARAM_SUPPORTED_EXPOSURECOMPENSATIONSTEP, mExposureCompensationStep);
   1.175 +  LOG_IF_ERROR(rv, CAMERA_PARAM_SUPPORTED_EXPOSURECOMPENSATIONSTEP);
   1.176 +
   1.177 +  mRecorderProfileManager = aCameraControl->GetRecorderProfileManager();
   1.178 +  if (!mRecorderProfileManager) {
   1.179 +    DOM_CAMERA_LOGW("Unable to get recorder profile manager\n");
   1.180 +  } else {
   1.181 +    AutoJSContext js;
   1.182 +
   1.183 +    JS::Rooted<JSObject*> o(js);
   1.184 +    nsresult rv = mRecorderProfileManager->GetJsObject(js, o.address());
   1.185 +    if (NS_FAILED(rv)) {
   1.186 +      DOM_CAMERA_LOGE("Failed to JS-objectify profile manager (%d)\n", rv);
   1.187 +      return rv;
   1.188 +    }
   1.189 +
   1.190 +    mRecorderProfiles = JS::ObjectValue(*o);
   1.191 +  }
   1.192 +
   1.193 +  // For now, always return success, since the presence or absence of capabilities
   1.194 +  // indicates whether or not they are supported.
   1.195 +  return NS_OK;
   1.196 +}
   1.197 +
   1.198 +void
   1.199 +CameraCapabilities::GetPreviewSizes(nsTArray<dom::CameraSize>& retval) const
   1.200 +{
   1.201 +  retval = mPreviewSizes;
   1.202 +}
   1.203 +
   1.204 +void
   1.205 +CameraCapabilities::GetPictureSizes(nsTArray<dom::CameraSize>& retval) const
   1.206 +{
   1.207 +  retval = mPictureSizes;
   1.208 +}
   1.209 +
   1.210 +void
   1.211 +CameraCapabilities::GetThumbnailSizes(nsTArray<dom::CameraSize>& retval) const
   1.212 +{
   1.213 +  retval = mThumbnailSizes;
   1.214 +}
   1.215 +
   1.216 +void
   1.217 +CameraCapabilities::GetVideoSizes(nsTArray<dom::CameraSize>& retval) const
   1.218 +{
   1.219 +  retval = mVideoSizes;
   1.220 +}
   1.221 +
   1.222 +void
   1.223 +CameraCapabilities::GetFileFormats(nsTArray<nsString>& retval) const
   1.224 +{
   1.225 +  retval = mFileFormats;
   1.226 +}
   1.227 +
   1.228 +void
   1.229 +CameraCapabilities::GetWhiteBalanceModes(nsTArray<nsString>& retval) const
   1.230 +{
   1.231 +  retval = mWhiteBalanceModes;
   1.232 +}
   1.233 +
   1.234 +void
   1.235 +CameraCapabilities::GetSceneModes(nsTArray<nsString>& retval) const
   1.236 +{
   1.237 +  retval = mSceneModes;
   1.238 +}
   1.239 +
   1.240 +void
   1.241 +CameraCapabilities::GetEffects(nsTArray<nsString>& retval) const
   1.242 +{
   1.243 +  retval = mEffects;
   1.244 +}
   1.245 +
   1.246 +void
   1.247 +CameraCapabilities::GetFlashModes(nsTArray<nsString>& retval) const
   1.248 +{
   1.249 +  retval = mFlashModes;
   1.250 +}
   1.251 +
   1.252 +void
   1.253 +CameraCapabilities::GetFocusModes(nsTArray<nsString>& retval) const
   1.254 +{
   1.255 +  retval = mFocusModes;
   1.256 +}
   1.257 +
   1.258 +void
   1.259 +CameraCapabilities::GetZoomRatios(nsTArray<double>& retval) const
   1.260 +{
   1.261 +  retval = mZoomRatios;
   1.262 +}
   1.263 +
   1.264 +uint32_t
   1.265 +CameraCapabilities::MaxFocusAreas() const
   1.266 +{
   1.267 +  return mMaxFocusAreas;
   1.268 +}
   1.269 +
   1.270 +uint32_t
   1.271 +CameraCapabilities::MaxMeteringAreas() const
   1.272 +{
   1.273 +  return mMaxMeteringAreas;
   1.274 +}
   1.275 +
   1.276 +uint32_t
   1.277 +CameraCapabilities::MaxDetectedFaces() const
   1.278 +{
   1.279 +  return mMaxDetectedFaces;
   1.280 +}
   1.281 +
   1.282 +double
   1.283 +CameraCapabilities::MinExposureCompensation() const
   1.284 +{
   1.285 +  return mMinExposureCompensation;
   1.286 +}
   1.287 +
   1.288 +double
   1.289 +CameraCapabilities::MaxExposureCompensation() const
   1.290 +{
   1.291 +  return mMaxExposureCompensation;
   1.292 +}
   1.293 +
   1.294 +double
   1.295 +CameraCapabilities::ExposureCompensationStep() const
   1.296 +{
   1.297 +  return mExposureCompensationStep;
   1.298 +}
   1.299 +
   1.300 +void
   1.301 +CameraCapabilities::GetRecorderProfiles(JSContext* aCx,
   1.302 +                                        JS::MutableHandle<JS::Value> aRetval) const
   1.303 +{
   1.304 +  JS::ExposeValueToActiveJS(mRecorderProfiles);
   1.305 +  aRetval.set(mRecorderProfiles);
   1.306 +}
   1.307 +
   1.308 +void
   1.309 +CameraCapabilities::GetIsoModes(nsTArray<nsString>& retval) const
   1.310 +{
   1.311 +  retval = mIsoModes;
   1.312 +}
   1.313 +
   1.314 +} // namespace dom
   1.315 +} // namespace mozilla

mercurial