Backout a74bd5095902, Bug 959405 - Please update the Buri Moz-central, 1.3, 1.2 with...
[gecko.git] / dom / camera / CameraRecorderProfiles.h
blob7ec20120116ebf54cae13d2f4e373a01b7e85e8b
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef DOM_CAMERA_CAMERA_RECORDER_PROFILES_H
6 #define DOM_CAMERA_CAMERA_RECORDER_PROFILES_H
8 #include "nsISupportsImpl.h"
9 #include "nsMimeTypes.h"
10 #include "nsAutoPtr.h"
11 #include "nsTArray.h"
12 #include "jsapi.h"
13 #include "DictionaryHelpers.h"
14 #include "CameraCommon.h"
17 namespace mozilla {
19 class CameraControlImpl;
21 class RecorderVideoProfile
23 public:
24 RecorderVideoProfile(uint32_t aCameraId, uint32_t aQualityIndex);
25 virtual ~RecorderVideoProfile();
27 int GetBitrate() const { return mBitrate; }
28 int GetFramerate() const { return mFramerate; }
29 int GetWidth() const { return mWidth; }
30 int GetHeight() const { return mHeight; }
32 enum Codec {
33 H263,
34 H264,
35 MPEG4SP,
36 UNKNOWN
38 Codec GetCodec() const { return mCodec; }
39 const char* GetCodecName() const
41 switch (mCodec) {
42 case H263: return "h263";
43 case H264: return "h264";
44 case MPEG4SP: return "mpeg4sp";
45 default: return nullptr;
49 nsresult GetJsObject(JSContext* aCx, JSObject** aObject);
51 protected:
52 uint32_t mCameraId;
53 uint32_t mQualityIndex;
54 Codec mCodec;
55 int mBitrate;
56 int mFramerate;
57 int mWidth;
58 int mHeight;
61 class RecorderAudioProfile
63 public:
64 RecorderAudioProfile(uint32_t aCameraId, uint32_t aQualityIndex);
65 virtual ~RecorderAudioProfile();
67 int GetBitrate() const { return mBitrate; }
68 int GetSamplerate() const { return mSamplerate; }
69 int GetChannels() const { return mChannels; }
71 enum Codec {
72 AMRNB,
73 AMRWB,
74 AAC,
75 UNKNOWN
78 public:
79 Codec GetCodec() const { return mCodec; }
80 const char* GetCodecName() const
82 switch (mCodec) {
83 case AMRNB: return "amrnb";
84 case AMRWB: return "amrwb";
85 case AAC: return "aac";
86 default: return nullptr;
90 nsresult GetJsObject(JSContext* aCx, JSObject** aObject);
92 protected:
93 uint32_t mCameraId;
94 uint32_t mQualityIndex;
95 Codec mCodec;
96 int mBitrate;
97 int mSamplerate;
98 int mChannels;
101 class RecorderProfile
103 public:
104 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RecorderProfile)
106 RecorderProfile(uint32_t aCameraId, uint32_t aQualityIndex);
108 virtual const RecorderVideoProfile* GetVideoProfile() const = 0;
109 virtual const RecorderAudioProfile* GetAudioProfile() const = 0;
110 const char* GetName() const { return mName; }
112 enum FileFormat {
113 THREE_GPP,
114 MPEG4,
115 UNKNOWN
117 FileFormat GetFileFormat() const { return mFileFormat; }
118 const char* GetFileFormatName() const
120 switch (mFileFormat) {
121 case THREE_GPP: return "3gp";
122 case MPEG4: return "mp4";
123 default: return nullptr;
126 const char* GetFileMimeType() const
128 switch (mFileFormat) {
129 case THREE_GPP: return VIDEO_3GPP;
130 case MPEG4: return VIDEO_MP4;
131 default: return nullptr;
135 virtual nsresult GetJsObject(JSContext* aCx, JSObject** aObject) = 0;
137 protected:
138 virtual ~RecorderProfile();
140 uint32_t mCameraId;
141 uint32_t mQualityIndex;
142 const char* mName;
143 FileFormat mFileFormat;
146 template <class Audio, class Video>
147 class RecorderProfileBase : public RecorderProfile
149 public:
150 RecorderProfileBase(uint32_t aCameraId, uint32_t aQualityIndex)
151 : RecorderProfile(aCameraId, aQualityIndex)
152 , mVideo(aCameraId, aQualityIndex)
153 , mAudio(aCameraId, aQualityIndex)
155 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
158 virtual ~RecorderProfileBase()
160 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
163 const RecorderVideoProfile* GetVideoProfile() const { return &mVideo; }
164 const RecorderAudioProfile* GetAudioProfile() const { return &mAudio; }
166 nsresult GetJsObject(JSContext* aCx, JSObject** aObject)
168 NS_ENSURE_TRUE(aObject, NS_ERROR_INVALID_ARG);
170 const char* format = GetFileFormatName();
171 if (!format) {
172 // the profile must have a file format
173 return NS_ERROR_FAILURE;
176 JS::Rooted<JSObject*> o(aCx, JS_NewObject(aCx, nullptr, nullptr, nullptr));
177 if (!o) {
178 return NS_ERROR_OUT_OF_MEMORY;
181 JS::Rooted<JSString*> s(aCx, JS_NewStringCopyZ(aCx, format));
182 JS::Rooted<JS::Value> v(aCx, STRING_TO_JSVAL(s));
183 if (!JS_SetProperty(aCx, o, "format", v)) {
184 return NS_ERROR_FAILURE;
187 JS::Rooted<JSObject*> video(aCx);
188 nsresult rv = mVideo.GetJsObject(aCx, video.address());
189 NS_ENSURE_SUCCESS(rv, rv);
190 v = OBJECT_TO_JSVAL(video);
191 if (!JS_SetProperty(aCx, o, "video", v)) {
192 return NS_ERROR_FAILURE;
195 JS::Rooted<JSObject*> audio(aCx);
196 rv = mAudio.GetJsObject(aCx, audio.address());
197 NS_ENSURE_SUCCESS(rv, rv);
198 v = OBJECT_TO_JSVAL(audio);
199 if (!JS_SetProperty(aCx, o, "audio", v)) {
200 return NS_ERROR_FAILURE;
203 *aObject = o;
204 return NS_OK;
207 protected:
208 Video mVideo;
209 Audio mAudio;
212 class RecorderProfileManager
214 public:
215 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RecorderProfileManager)
217 virtual bool IsSupported(uint32_t aQualityIndex) const { return true; }
218 virtual already_AddRefed<RecorderProfile> Get(uint32_t aQualityIndex) const = 0;
220 uint32_t GetMaxQualityIndex() const { return mMaxQualityIndex; }
221 nsresult GetJsObject(JSContext* aCx, JSObject** aObject) const;
223 protected:
224 RecorderProfileManager(uint32_t aCameraId);
225 virtual ~RecorderProfileManager();
227 uint32_t mCameraId;
228 uint32_t mMaxQualityIndex;
231 } // namespace mozilla
233 #endif // DOM_CAMERA_CAMERA_RECORDER_PROFILES_H