|
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 #include "CameraRecorderProfiles.h" |
|
6 #include "jsapi.h" |
|
7 #include "CameraCommon.h" |
|
8 |
|
9 using namespace mozilla; |
|
10 |
|
11 /** |
|
12 * Video profile implementation. |
|
13 */ |
|
14 RecorderVideoProfile::RecorderVideoProfile(uint32_t aCameraId, uint32_t aQualityIndex) |
|
15 : mCameraId(aCameraId) |
|
16 , mQualityIndex(aQualityIndex) |
|
17 { |
|
18 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this); |
|
19 } |
|
20 |
|
21 RecorderVideoProfile::~RecorderVideoProfile() |
|
22 { |
|
23 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this); |
|
24 } |
|
25 |
|
26 nsresult |
|
27 RecorderVideoProfile::GetJsObject(JSContext* aCx, JSObject** aObject) |
|
28 { |
|
29 NS_ENSURE_TRUE(aObject, NS_ERROR_INVALID_ARG); |
|
30 |
|
31 JS::Rooted<JSObject*> o(aCx, JS_NewObject(aCx, nullptr, JS::NullPtr(), JS::NullPtr())); |
|
32 NS_ENSURE_TRUE(o, NS_ERROR_OUT_OF_MEMORY); |
|
33 |
|
34 const char* codec = GetCodecName(); |
|
35 NS_ENSURE_TRUE(codec, NS_ERROR_FAILURE); |
|
36 |
|
37 JS::Rooted<JSString*> s(aCx, JS_NewStringCopyZ(aCx, codec)); |
|
38 JS::Rooted<JS::Value> v(aCx, STRING_TO_JSVAL(s)); |
|
39 if (!JS_SetProperty(aCx, o, "codec", v)) { |
|
40 return NS_ERROR_FAILURE; |
|
41 } |
|
42 |
|
43 if (mBitrate != -1) { |
|
44 v = INT_TO_JSVAL(mBitrate); |
|
45 if (!JS_SetProperty(aCx, o, "bitrate", v)) { |
|
46 return NS_ERROR_FAILURE; |
|
47 } |
|
48 } |
|
49 if (mFramerate != -1) { |
|
50 v = INT_TO_JSVAL(mFramerate); |
|
51 if (!JS_SetProperty(aCx, o, "framerate", v)) { |
|
52 return NS_ERROR_FAILURE; |
|
53 } |
|
54 } |
|
55 if (mWidth != -1) { |
|
56 v = INT_TO_JSVAL(mWidth); |
|
57 if (!JS_SetProperty(aCx, o, "width", v)) { |
|
58 return NS_ERROR_FAILURE; |
|
59 } |
|
60 } |
|
61 if (mHeight != -1) { |
|
62 v = INT_TO_JSVAL(mHeight); |
|
63 if (!JS_SetProperty(aCx, o, "height", v)) { |
|
64 return NS_ERROR_FAILURE; |
|
65 } |
|
66 } |
|
67 |
|
68 *aObject = o; |
|
69 return NS_OK; |
|
70 } |
|
71 |
|
72 /** |
|
73 * Audio profile implementation. |
|
74 */ |
|
75 RecorderAudioProfile::RecorderAudioProfile(uint32_t aCameraId, uint32_t aQualityIndex) |
|
76 : mCameraId(aCameraId) |
|
77 , mQualityIndex(aQualityIndex) |
|
78 { |
|
79 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this); |
|
80 } |
|
81 |
|
82 RecorderAudioProfile::~RecorderAudioProfile() |
|
83 { |
|
84 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this); |
|
85 } |
|
86 |
|
87 nsresult |
|
88 RecorderAudioProfile::GetJsObject(JSContext* aCx, JSObject** aObject) |
|
89 { |
|
90 NS_ENSURE_TRUE(aObject, NS_ERROR_INVALID_ARG); |
|
91 |
|
92 JS::Rooted<JSObject*> o(aCx, JS_NewObject(aCx, nullptr, JS::NullPtr(), JS::NullPtr())); |
|
93 NS_ENSURE_TRUE(o, NS_ERROR_OUT_OF_MEMORY); |
|
94 |
|
95 const char* codec = GetCodecName(); |
|
96 NS_ENSURE_TRUE(codec, NS_ERROR_FAILURE); |
|
97 |
|
98 JS::Rooted<JSString*> s(aCx, JS_NewStringCopyZ(aCx, codec)); |
|
99 JS::Rooted<JS::Value> v(aCx, STRING_TO_JSVAL(s)); |
|
100 if (!JS_SetProperty(aCx, o, "codec", v)) { |
|
101 return NS_ERROR_FAILURE; |
|
102 } |
|
103 |
|
104 if (mBitrate != -1) { |
|
105 v = INT_TO_JSVAL(mBitrate); |
|
106 if (!JS_SetProperty(aCx, o, "bitrate", v)) { |
|
107 return NS_ERROR_FAILURE; |
|
108 } |
|
109 } |
|
110 if (mSamplerate != -1) { |
|
111 v = INT_TO_JSVAL(mSamplerate); |
|
112 if (!JS_SetProperty(aCx, o, "samplerate", v)) { |
|
113 return NS_ERROR_FAILURE; |
|
114 } |
|
115 } |
|
116 if (mChannels != -1) { |
|
117 v = INT_TO_JSVAL(mChannels); |
|
118 if (!JS_SetProperty(aCx, o, "channels", v)) { |
|
119 return NS_ERROR_FAILURE; |
|
120 } |
|
121 } |
|
122 |
|
123 *aObject = o; |
|
124 return NS_OK; |
|
125 } |
|
126 |
|
127 /** |
|
128 * Recorder Profile |
|
129 */ |
|
130 RecorderProfile::RecorderProfile(uint32_t aCameraId, uint32_t aQualityIndex) |
|
131 : mCameraId(aCameraId) |
|
132 , mQualityIndex(aQualityIndex) |
|
133 , mName(nullptr) |
|
134 { |
|
135 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this); |
|
136 } |
|
137 |
|
138 RecorderProfile::~RecorderProfile() |
|
139 { |
|
140 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this); |
|
141 } |
|
142 |
|
143 /** |
|
144 * Recorder profile manager implementation. |
|
145 */ |
|
146 RecorderProfileManager::RecorderProfileManager(uint32_t aCameraId) |
|
147 : mCameraId(aCameraId) |
|
148 { |
|
149 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this); |
|
150 } |
|
151 |
|
152 RecorderProfileManager::~RecorderProfileManager() |
|
153 { |
|
154 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this); |
|
155 } |
|
156 |
|
157 nsresult |
|
158 RecorderProfileManager::GetJsObject(JSContext* aCx, JSObject** aObject) const |
|
159 { |
|
160 NS_ENSURE_TRUE(aObject, NS_ERROR_INVALID_ARG); |
|
161 |
|
162 JS::Rooted<JSObject*> o(aCx, JS_NewObject(aCx, nullptr, JS::NullPtr(), JS::NullPtr())); |
|
163 if (!o) { |
|
164 return NS_ERROR_OUT_OF_MEMORY; |
|
165 } |
|
166 |
|
167 for (uint32_t q = 0; q < GetMaxQualityIndex(); ++q) { |
|
168 if (!IsSupported(q)) { |
|
169 continue; |
|
170 } |
|
171 |
|
172 nsRefPtr<RecorderProfile> profile = Get(q); |
|
173 if (!profile) { |
|
174 return NS_ERROR_OUT_OF_MEMORY; |
|
175 } |
|
176 |
|
177 const char* profileName = profile->GetName(); |
|
178 if (!profileName) { |
|
179 // don't allow anonymous recorder profiles |
|
180 continue; |
|
181 } |
|
182 |
|
183 JS::Rooted<JSObject*> p(aCx); |
|
184 nsresult rv = profile->GetJsObject(aCx, p.address()); |
|
185 NS_ENSURE_SUCCESS(rv, rv); |
|
186 JS::Rooted<JS::Value> v(aCx, OBJECT_TO_JSVAL(p)); |
|
187 |
|
188 if (!JS_SetProperty(aCx, o, profileName, v)) { |
|
189 return NS_ERROR_FAILURE; |
|
190 } |
|
191 } |
|
192 |
|
193 *aObject = o; |
|
194 return NS_OK; |
|
195 } |