dom/camera/CameraRecorderProfiles.h

branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
equal deleted inserted replaced
-1:000000000000 0:a8a20828f294
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/. */
4
5 #ifndef DOM_CAMERA_CAMERA_RECORDER_PROFILES_H
6 #define DOM_CAMERA_CAMERA_RECORDER_PROFILES_H
7
8 #include "nsISupportsImpl.h"
9 #include "nsMimeTypes.h"
10 #include "nsAutoPtr.h"
11 #include "nsTArray.h"
12 #include "jsapi.h"
13 #include "CameraCommon.h"
14
15 namespace mozilla {
16
17 class CameraControlImpl;
18
19 class RecorderVideoProfile
20 {
21 public:
22 RecorderVideoProfile(uint32_t aCameraId, uint32_t aQualityIndex);
23 virtual ~RecorderVideoProfile();
24
25 int GetBitrate() const { return mBitrate; }
26 int GetFramerate() const { return mFramerate; }
27 int GetWidth() const { return mWidth; }
28 int GetHeight() const { return mHeight; }
29
30 enum Codec {
31 H263,
32 H264,
33 MPEG4SP,
34 UNKNOWN
35 };
36 Codec GetCodec() const { return mCodec; }
37 const char* GetCodecName() const
38 {
39 switch (mCodec) {
40 case H263: return "h263";
41 case H264: return "h264";
42 case MPEG4SP: return "mpeg4sp";
43 default: return nullptr;
44 }
45 }
46
47 nsresult GetJsObject(JSContext* aCx, JSObject** aObject);
48
49 protected:
50 uint32_t mCameraId;
51 uint32_t mQualityIndex;
52 Codec mCodec;
53 int mBitrate;
54 int mFramerate;
55 int mWidth;
56 int mHeight;
57 };
58
59 class RecorderAudioProfile
60 {
61 public:
62 RecorderAudioProfile(uint32_t aCameraId, uint32_t aQualityIndex);
63 virtual ~RecorderAudioProfile();
64
65 int GetBitrate() const { return mBitrate; }
66 int GetSamplerate() const { return mSamplerate; }
67 int GetChannels() const { return mChannels; }
68
69 enum Codec {
70 AMRNB,
71 AMRWB,
72 AAC,
73 UNKNOWN
74 };
75
76 public:
77 Codec GetCodec() const { return mCodec; }
78 const char* GetCodecName() const
79 {
80 switch (mCodec) {
81 case AMRNB: return "amrnb";
82 case AMRWB: return "amrwb";
83 case AAC: return "aac";
84 default: return nullptr;
85 }
86 }
87
88 nsresult GetJsObject(JSContext* aCx, JSObject** aObject);
89
90 protected:
91 uint32_t mCameraId;
92 uint32_t mQualityIndex;
93 Codec mCodec;
94 int mBitrate;
95 int mSamplerate;
96 int mChannels;
97 };
98
99 class RecorderProfile
100 {
101 public:
102 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RecorderProfile)
103
104 RecorderProfile(uint32_t aCameraId, uint32_t aQualityIndex);
105
106 virtual const RecorderVideoProfile* GetVideoProfile() const = 0;
107 virtual const RecorderAudioProfile* GetAudioProfile() const = 0;
108 const char* GetName() const { return mName; }
109
110 enum FileFormat {
111 THREE_GPP,
112 MPEG4,
113 UNKNOWN
114 };
115 FileFormat GetFileFormat() const { return mFileFormat; }
116 const char* GetFileFormatName() const
117 {
118 switch (mFileFormat) {
119 case THREE_GPP: return "3gp";
120 case MPEG4: return "mp4";
121 default: return nullptr;
122 }
123 }
124 const char* GetFileMimeType() const
125 {
126 switch (mFileFormat) {
127 case THREE_GPP: return VIDEO_3GPP;
128 case MPEG4: return VIDEO_MP4;
129 default: return nullptr;
130 }
131 }
132
133 virtual nsresult GetJsObject(JSContext* aCx, JSObject** aObject) = 0;
134
135 protected:
136 virtual ~RecorderProfile();
137
138 uint32_t mCameraId;
139 uint32_t mQualityIndex;
140 const char* mName;
141 FileFormat mFileFormat;
142 };
143
144 template <class Audio, class Video>
145 class RecorderProfileBase : public RecorderProfile
146 {
147 public:
148 RecorderProfileBase(uint32_t aCameraId, uint32_t aQualityIndex)
149 : RecorderProfile(aCameraId, aQualityIndex)
150 , mVideo(aCameraId, aQualityIndex)
151 , mAudio(aCameraId, aQualityIndex)
152 {
153 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
154 }
155
156 virtual ~RecorderProfileBase()
157 {
158 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
159 }
160
161 const RecorderVideoProfile* GetVideoProfile() const { return &mVideo; }
162 const RecorderAudioProfile* GetAudioProfile() const { return &mAudio; }
163
164 nsresult GetJsObject(JSContext* aCx, JSObject** aObject)
165 {
166 NS_ENSURE_TRUE(aObject, NS_ERROR_INVALID_ARG);
167
168 const char* format = GetFileFormatName();
169 if (!format) {
170 // the profile must have a file format
171 return NS_ERROR_FAILURE;
172 }
173
174 JS::Rooted<JSObject*> o(aCx, JS_NewObject(aCx, nullptr, JS::NullPtr(), JS::NullPtr()));
175 if (!o) {
176 return NS_ERROR_OUT_OF_MEMORY;
177 }
178
179 JS::Rooted<JSString*> s(aCx, JS_NewStringCopyZ(aCx, format));
180 JS::Rooted<JS::Value> v(aCx, STRING_TO_JSVAL(s));
181 if (!JS_SetProperty(aCx, o, "format", v)) {
182 return NS_ERROR_FAILURE;
183 }
184
185 JS::Rooted<JSObject*> video(aCx);
186 nsresult rv = mVideo.GetJsObject(aCx, video.address());
187 NS_ENSURE_SUCCESS(rv, rv);
188 v = OBJECT_TO_JSVAL(video);
189 if (!JS_SetProperty(aCx, o, "video", v)) {
190 return NS_ERROR_FAILURE;
191 }
192
193 JS::Rooted<JSObject*> audio(aCx);
194 rv = mAudio.GetJsObject(aCx, audio.address());
195 NS_ENSURE_SUCCESS(rv, rv);
196 v = OBJECT_TO_JSVAL(audio);
197 if (!JS_SetProperty(aCx, o, "audio", v)) {
198 return NS_ERROR_FAILURE;
199 }
200
201 *aObject = o;
202 return NS_OK;
203 }
204
205 protected:
206 Video mVideo;
207 Audio mAudio;
208 };
209
210 class RecorderProfileManager
211 {
212 public:
213 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RecorderProfileManager)
214
215 virtual bool IsSupported(uint32_t aQualityIndex) const { return true; }
216 virtual already_AddRefed<RecorderProfile> Get(uint32_t aQualityIndex) const = 0;
217
218 uint32_t GetMaxQualityIndex() const { return mMaxQualityIndex; }
219 nsresult GetJsObject(JSContext* aCx, JSObject** aObject) const;
220
221 protected:
222 RecorderProfileManager(uint32_t aCameraId);
223 virtual ~RecorderProfileManager();
224
225 uint32_t mCameraId;
226 uint32_t mMaxQualityIndex;
227 };
228
229 } // namespace mozilla
230
231 #endif // DOM_CAMERA_CAMERA_RECORDER_PROFILES_H

mercurial