Bumping gaia.json for 2 gaia revision(s) a=gaia-bump
[gecko.git] / dom / camera / CameraRecorderProfiles.h
blob1ce19255aacd6f376a093c11ef7ebacb43cf804a
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 "CameraCommon.h"
15 namespace mozilla {
17 class CameraControlImpl;
19 class RecorderVideoProfile
21 public:
22 RecorderVideoProfile(uint32_t aCameraId, uint32_t aQualityIndex);
23 virtual ~RecorderVideoProfile();
25 int GetBitrate() const { return mBitrate; }
26 int GetFramerate() const { return mFramerate; }
27 int GetWidth() const { return mWidth; }
28 int GetHeight() const { return mHeight; }
30 enum Codec {
31 H263,
32 H264,
33 MPEG4SP,
34 UNKNOWN
36 Codec GetCodec() const { return mCodec; }
37 const char* GetCodecName() const
39 switch (mCodec) {
40 case H263: return "h263";
41 case H264: return "h264";
42 case MPEG4SP: return "mpeg4sp";
43 default: return nullptr;
47 // Get a representation of this video profile that can be returned
48 // to JS, possibly as a child member of another object.
50 // Return values:
51 // - NS_OK on success;
52 // - NS_ERROR_INVALID_ARG if 'aObject' is null;
53 // - NS_ERROR_OUT_OF_MEMORY if a new object could not be allocated;
54 // - NS_ERROR_FAILURE if construction of the JS object fails.
55 nsresult GetJsObject(JSContext* aCx, JSObject** aObject);
57 protected:
58 uint32_t mCameraId;
59 uint32_t mQualityIndex;
60 Codec mCodec;
61 int mBitrate;
62 int mFramerate;
63 int mWidth;
64 int mHeight;
67 class RecorderAudioProfile
69 public:
70 RecorderAudioProfile(uint32_t aCameraId, uint32_t aQualityIndex);
71 virtual ~RecorderAudioProfile();
73 int GetBitrate() const { return mBitrate; }
74 int GetSamplerate() const { return mSamplerate; }
75 int GetChannels() const { return mChannels; }
77 enum Codec {
78 AMRNB,
79 AMRWB,
80 AAC,
81 UNKNOWN
84 Codec GetCodec() const { return mCodec; }
85 const char* GetCodecName() const
87 switch (mCodec) {
88 case AMRNB: return "amrnb";
89 case AMRWB: return "amrwb";
90 case AAC: return "aac";
91 default: return nullptr;
95 // Get a representation of this audio profile that can be returned
96 // to JS, possibly as a child member of another object.
98 // Return values:
99 // - NS_OK on success;
100 // - NS_ERROR_INVALID_ARG if 'aObject' is null;
101 // - NS_ERROR_OUT_OF_MEMORY if a new object could not be allocated;
102 // - NS_ERROR_FAILURE if construction of the JS object fails.
103 nsresult GetJsObject(JSContext* aCx, JSObject** aObject);
105 protected:
106 uint32_t mCameraId;
107 uint32_t mQualityIndex;
108 Codec mCodec;
109 int mBitrate;
110 int mSamplerate;
111 int mChannels;
114 class RecorderProfile
116 public:
117 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RecorderProfile)
119 RecorderProfile(uint32_t aCameraId, uint32_t aQualityIndex);
121 virtual const RecorderVideoProfile* GetVideoProfile() const = 0;
122 virtual const RecorderAudioProfile* GetAudioProfile() const = 0;
123 const char* GetName() const { return mName; }
125 enum FileFormat {
126 THREE_GPP,
127 MPEG4,
128 UNKNOWN
130 FileFormat GetFileFormat() const { return mFileFormat; }
131 const char* GetFileFormatName() const
133 switch (mFileFormat) {
134 case THREE_GPP: return "3gp";
135 case MPEG4: return "mp4";
136 default: return nullptr;
139 const char* GetFileMimeType() const
141 switch (mFileFormat) {
142 case THREE_GPP: return VIDEO_3GPP;
143 case MPEG4: return VIDEO_MP4;
144 default: return nullptr;
148 // Get a representation of this recorder profile that can be returned
149 // to JS, possibly as a child member of another object.
151 // Return values:
152 // - NS_OK on success;
153 // - NS_ERROR_INVALID_ARG if 'aObject' is null;
154 // - NS_ERROR_OUT_OF_MEMORY if a new object could not be allocated;
155 // - NS_ERROR_FAILURE if construction of the JS object fails.
156 virtual nsresult GetJsObject(JSContext* aCx, JSObject** aObject) = 0;
158 protected:
159 virtual ~RecorderProfile();
161 uint32_t mCameraId;
162 uint32_t mQualityIndex;
163 const char* mName;
164 FileFormat mFileFormat;
167 template <class Audio, class Video>
168 class RecorderProfileBase : public RecorderProfile
170 public:
171 RecorderProfileBase(uint32_t aCameraId, uint32_t aQualityIndex)
172 : RecorderProfile(aCameraId, aQualityIndex)
173 , mVideo(aCameraId, aQualityIndex)
174 , mAudio(aCameraId, aQualityIndex)
176 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
179 virtual ~RecorderProfileBase()
181 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
184 const RecorderVideoProfile* GetVideoProfile() const { return &mVideo; }
185 const RecorderAudioProfile* GetAudioProfile() const { return &mAudio; }
187 // Get a representation of this recorder profile that can be returned
188 // to JS, possibly as a child member of another object.
190 // Return values:
191 // - NS_OK on success;
192 // - NS_ERROR_INVALID_ARG if 'aObject' is null;
193 // - NS_ERROR_OUT_OF_MEMORY if a new object could not be allocated;
194 // - NS_ERROR_NOT_AVAILABLE if the profile has no file format name;
195 // - NS_ERROR_FAILURE if construction of the JS object fails.
196 nsresult
197 GetJsObject(JSContext* aCx, JSObject** aObject)
199 NS_ENSURE_TRUE(aObject, NS_ERROR_INVALID_ARG);
201 const char* format = GetFileFormatName();
202 if (!format) {
203 // the profile must have a file format
204 return NS_ERROR_NOT_AVAILABLE;
207 JS::Rooted<JSObject*> o(aCx, JS_NewObject(aCx, nullptr, JS::NullPtr(), JS::NullPtr()));
208 if (!o) {
209 return NS_ERROR_OUT_OF_MEMORY;
212 JS::Rooted<JSString*> s(aCx, JS_NewStringCopyZ(aCx, format));
213 JS::Rooted<JS::Value> v(aCx, STRING_TO_JSVAL(s));
214 if (!JS_SetProperty(aCx, o, "format", v)) {
215 return NS_ERROR_FAILURE;
218 JS::Rooted<JSObject*> video(aCx);
219 nsresult rv = mVideo.GetJsObject(aCx, video.address());
220 NS_ENSURE_SUCCESS(rv, rv);
221 v = OBJECT_TO_JSVAL(video);
222 if (!JS_SetProperty(aCx, o, "video", v)) {
223 return NS_ERROR_FAILURE;
226 JS::Rooted<JSObject*> audio(aCx);
227 rv = mAudio.GetJsObject(aCx, audio.address());
228 NS_ENSURE_SUCCESS(rv, rv);
229 v = OBJECT_TO_JSVAL(audio);
230 if (!JS_SetProperty(aCx, o, "audio", v)) {
231 return NS_ERROR_FAILURE;
234 *aObject = o;
235 return NS_OK;
238 protected:
239 Video mVideo;
240 Audio mAudio;
243 class RecorderProfileManager
245 public:
246 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RecorderProfileManager)
248 virtual bool IsSupported(uint32_t aQualityIndex) const { return true; }
249 virtual already_AddRefed<RecorderProfile> Get(uint32_t aQualityIndex) const = 0;
251 uint32_t GetMaxQualityIndex() const { return mMaxQualityIndex; }
253 // Get a representation of all supported recorder profiles that can be
254 // returned to JS.
256 // Return values:
257 // - NS_OK on success;
258 // - NS_ERROR_INVALID_ARG if 'aObject' is null;
259 // - NS_ERROR_OUT_OF_MEMORY if a new object could not be allocated;
260 // - NS_ERROR_NOT_AVAILABLE if the profile has no file format name;
261 // - NS_ERROR_FAILURE if construction of the JS object fails.
262 nsresult GetJsObject(JSContext* aCx, JSObject** aObject) const;
264 protected:
265 RecorderProfileManager(uint32_t aCameraId);
266 virtual ~RecorderProfileManager();
268 uint32_t mCameraId;
269 uint32_t mMaxQualityIndex;
272 } // namespace mozilla
274 #endif // DOM_CAMERA_CAMERA_RECORDER_PROFILES_H