dom/camera/CameraRecorderProfiles.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/camera/CameraRecorderProfiles.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,231 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#ifndef DOM_CAMERA_CAMERA_RECORDER_PROFILES_H
     1.9 +#define DOM_CAMERA_CAMERA_RECORDER_PROFILES_H
    1.10 +
    1.11 +#include "nsISupportsImpl.h"
    1.12 +#include "nsMimeTypes.h"
    1.13 +#include "nsAutoPtr.h"
    1.14 +#include "nsTArray.h"
    1.15 +#include "jsapi.h"
    1.16 +#include "CameraCommon.h"
    1.17 +
    1.18 +namespace mozilla {
    1.19 +
    1.20 +class CameraControlImpl;
    1.21 +
    1.22 +class RecorderVideoProfile
    1.23 +{
    1.24 +public:
    1.25 +  RecorderVideoProfile(uint32_t aCameraId, uint32_t aQualityIndex);
    1.26 +  virtual ~RecorderVideoProfile();
    1.27 +
    1.28 +  int GetBitrate() const    { return mBitrate; }
    1.29 +  int GetFramerate() const  { return mFramerate; }
    1.30 +  int GetWidth() const      { return mWidth; }
    1.31 +  int GetHeight() const     { return mHeight; }
    1.32 +
    1.33 +  enum Codec {
    1.34 +    H263,
    1.35 +    H264,
    1.36 +    MPEG4SP,
    1.37 +    UNKNOWN
    1.38 +  };
    1.39 +  Codec GetCodec() const    { return mCodec; }
    1.40 +  const char* GetCodecName() const
    1.41 +  {
    1.42 +    switch (mCodec) {
    1.43 +      case H263:    return "h263";
    1.44 +      case H264:    return "h264";
    1.45 +      case MPEG4SP: return "mpeg4sp";
    1.46 +      default:      return nullptr;
    1.47 +    }
    1.48 +  }
    1.49 +
    1.50 +  nsresult GetJsObject(JSContext* aCx, JSObject** aObject);
    1.51 +
    1.52 +protected:
    1.53 +  uint32_t mCameraId;
    1.54 +  uint32_t mQualityIndex;
    1.55 +  Codec mCodec;
    1.56 +  int mBitrate;
    1.57 +  int mFramerate;
    1.58 +  int mWidth;
    1.59 +  int mHeight;
    1.60 +};
    1.61 +
    1.62 +class RecorderAudioProfile
    1.63 +{
    1.64 +public:
    1.65 +  RecorderAudioProfile(uint32_t aCameraId, uint32_t aQualityIndex);
    1.66 +  virtual ~RecorderAudioProfile();
    1.67 +
    1.68 +  int GetBitrate() const    { return mBitrate; }
    1.69 +  int GetSamplerate() const { return mSamplerate; }
    1.70 +  int GetChannels() const   { return mChannels; }
    1.71 +
    1.72 +  enum Codec {
    1.73 +    AMRNB,
    1.74 +    AMRWB,
    1.75 +    AAC,
    1.76 +    UNKNOWN
    1.77 +  };
    1.78 +
    1.79 +public:
    1.80 +  Codec GetCodec() const    { return mCodec; }
    1.81 +  const char* GetCodecName() const
    1.82 +  {
    1.83 +    switch (mCodec) {
    1.84 +      case AMRNB: return "amrnb";
    1.85 +      case AMRWB: return "amrwb";
    1.86 +      case AAC:   return "aac";
    1.87 +      default:    return nullptr;
    1.88 +    }
    1.89 +  }
    1.90 +
    1.91 +  nsresult GetJsObject(JSContext* aCx, JSObject** aObject);
    1.92 +
    1.93 +protected:
    1.94 +  uint32_t mCameraId;
    1.95 +  uint32_t mQualityIndex;
    1.96 +  Codec mCodec;
    1.97 +  int mBitrate;
    1.98 +  int mSamplerate;
    1.99 +  int mChannels;
   1.100 +};
   1.101 +
   1.102 +class RecorderProfile
   1.103 +{
   1.104 +public:
   1.105 +  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RecorderProfile)
   1.106 +
   1.107 +  RecorderProfile(uint32_t aCameraId, uint32_t aQualityIndex);
   1.108 +
   1.109 +  virtual const RecorderVideoProfile* GetVideoProfile() const = 0;
   1.110 +  virtual const RecorderAudioProfile* GetAudioProfile() const = 0;
   1.111 +  const char* GetName() const { return mName; }
   1.112 +
   1.113 +  enum FileFormat {
   1.114 +    THREE_GPP,
   1.115 +    MPEG4,
   1.116 +    UNKNOWN
   1.117 +  };
   1.118 +  FileFormat GetFileFormat() const { return mFileFormat; }
   1.119 +  const char* GetFileFormatName() const
   1.120 +  {
   1.121 +    switch (mFileFormat) {
   1.122 +      case THREE_GPP: return "3gp";
   1.123 +      case MPEG4:     return "mp4";
   1.124 +      default:        return nullptr;
   1.125 +    }
   1.126 +  }
   1.127 +  const char* GetFileMimeType() const
   1.128 +  {
   1.129 +    switch (mFileFormat) {
   1.130 +      case THREE_GPP: return VIDEO_3GPP;
   1.131 +      case MPEG4:     return VIDEO_MP4;
   1.132 +      default:        return nullptr;
   1.133 +    }
   1.134 +  }
   1.135 +
   1.136 +  virtual nsresult GetJsObject(JSContext* aCx, JSObject** aObject) = 0;
   1.137 +
   1.138 +protected:
   1.139 +  virtual ~RecorderProfile();
   1.140 +
   1.141 +  uint32_t mCameraId;
   1.142 +  uint32_t mQualityIndex;
   1.143 +  const char* mName;
   1.144 +  FileFormat mFileFormat;
   1.145 +};
   1.146 +
   1.147 +template <class Audio, class Video>
   1.148 +class RecorderProfileBase : public RecorderProfile
   1.149 +{
   1.150 +public:
   1.151 +  RecorderProfileBase(uint32_t aCameraId, uint32_t aQualityIndex)
   1.152 +    : RecorderProfile(aCameraId, aQualityIndex)
   1.153 +    , mVideo(aCameraId, aQualityIndex)
   1.154 +    , mAudio(aCameraId, aQualityIndex)
   1.155 +  {
   1.156 +    DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
   1.157 +  }
   1.158 +
   1.159 +  virtual ~RecorderProfileBase()
   1.160 +  {
   1.161 +    DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
   1.162 +  }
   1.163 +
   1.164 +  const RecorderVideoProfile* GetVideoProfile() const { return &mVideo; }
   1.165 +  const RecorderAudioProfile* GetAudioProfile() const { return &mAudio; }
   1.166 +
   1.167 +  nsresult GetJsObject(JSContext* aCx, JSObject** aObject)
   1.168 +  {
   1.169 +    NS_ENSURE_TRUE(aObject, NS_ERROR_INVALID_ARG);
   1.170 +
   1.171 +    const char* format = GetFileFormatName();
   1.172 +    if (!format) {
   1.173 +      // the profile must have a file format
   1.174 +      return NS_ERROR_FAILURE;
   1.175 +    }
   1.176 +
   1.177 +    JS::Rooted<JSObject*> o(aCx, JS_NewObject(aCx, nullptr, JS::NullPtr(), JS::NullPtr()));
   1.178 +    if (!o) {
   1.179 +      return NS_ERROR_OUT_OF_MEMORY;
   1.180 +    }
   1.181 +
   1.182 +    JS::Rooted<JSString*> s(aCx, JS_NewStringCopyZ(aCx, format));
   1.183 +    JS::Rooted<JS::Value> v(aCx, STRING_TO_JSVAL(s));
   1.184 +    if (!JS_SetProperty(aCx, o, "format", v)) {
   1.185 +      return NS_ERROR_FAILURE;
   1.186 +    }
   1.187 +
   1.188 +    JS::Rooted<JSObject*> video(aCx);
   1.189 +    nsresult rv = mVideo.GetJsObject(aCx, video.address());
   1.190 +    NS_ENSURE_SUCCESS(rv, rv);
   1.191 +    v = OBJECT_TO_JSVAL(video);
   1.192 +    if (!JS_SetProperty(aCx, o, "video", v)) {
   1.193 +      return NS_ERROR_FAILURE;
   1.194 +    }
   1.195 +
   1.196 +    JS::Rooted<JSObject*> audio(aCx);
   1.197 +    rv = mAudio.GetJsObject(aCx, audio.address());
   1.198 +    NS_ENSURE_SUCCESS(rv, rv);
   1.199 +    v = OBJECT_TO_JSVAL(audio);
   1.200 +    if (!JS_SetProperty(aCx, o, "audio", v)) {
   1.201 +      return NS_ERROR_FAILURE;
   1.202 +    }
   1.203 +
   1.204 +    *aObject = o;
   1.205 +    return NS_OK;
   1.206 +  }
   1.207 +
   1.208 +protected:
   1.209 +  Video mVideo;
   1.210 +  Audio mAudio;
   1.211 +};
   1.212 +
   1.213 +class RecorderProfileManager
   1.214 +{
   1.215 +public:
   1.216 +  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RecorderProfileManager)
   1.217 +
   1.218 +  virtual bool IsSupported(uint32_t aQualityIndex) const { return true; }
   1.219 +  virtual already_AddRefed<RecorderProfile> Get(uint32_t aQualityIndex) const = 0;
   1.220 +
   1.221 +  uint32_t GetMaxQualityIndex() const { return mMaxQualityIndex; }
   1.222 +  nsresult GetJsObject(JSContext* aCx, JSObject** aObject) const;
   1.223 +
   1.224 +protected:
   1.225 +  RecorderProfileManager(uint32_t aCameraId);
   1.226 +  virtual ~RecorderProfileManager();
   1.227 +
   1.228 +  uint32_t mCameraId;
   1.229 +  uint32_t mMaxQualityIndex;
   1.230 +};
   1.231 +
   1.232 +} // namespace mozilla
   1.233 +
   1.234 +#endif // DOM_CAMERA_CAMERA_RECORDER_PROFILES_H

mercurial