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"
13 #include "CameraCommon.h"
17 class CameraControlImpl
;
19 class RecorderVideoProfile
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
; }
36 Codec
GetCodec() const { return mCodec
; }
37 const char* GetCodecName() const
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.
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
);
59 uint32_t mQualityIndex
;
67 class RecorderAudioProfile
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
; }
84 Codec
GetCodec() const { return mCodec
; }
85 const char* GetCodecName() const
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.
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
);
107 uint32_t mQualityIndex
;
114 class RecorderProfile
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
; }
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.
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;
159 virtual ~RecorderProfile();
162 uint32_t mQualityIndex
;
164 FileFormat mFileFormat
;
167 template <class Audio
, class Video
>
168 class RecorderProfileBase
: public RecorderProfile
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.
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.
197 GetJsObject(JSContext
* aCx
, JSObject
** aObject
)
199 NS_ENSURE_TRUE(aObject
, NS_ERROR_INVALID_ARG
);
201 const char* format
= GetFileFormatName();
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()));
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
;
243 class RecorderProfileManager
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
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;
265 RecorderProfileManager(uint32_t aCameraId
);
266 virtual ~RecorderProfileManager();
269 uint32_t mMaxQualityIndex
;
272 } // namespace mozilla
274 #endif // DOM_CAMERA_CAMERA_RECORDER_PROFILES_H