1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "napi_playback_state.h"
17 #include "napi_utils.h"
18
19 namespace OHOS::AVSession {
20 std::map<std::string, NapiPlaybackState::GetterType> NapiPlaybackState::getterMap_ = {
21 { "state", GetState },
22 { "speed", GetSpeed },
23 { "position", GetPosition },
24 { "bufferedTime", GetBufferedTime },
25 { "loopMode", GetLoopMode },
26 { "isFavorite", GetIsFavorite },
27 { "activeItemId", GetActiveItemId },
28 { "volume", GetVolume },
29 { "extras", GetExtras },
30 };
31
32 std::map<int32_t, NapiPlaybackState::SetterType> NapiPlaybackState::setterMap_ = {
33 { AVPlaybackState::PLAYBACK_KEY_STATE, SetState },
34 { AVPlaybackState::PLAYBACK_KEY_SPEED, SetSpeed },
35 { AVPlaybackState::PLAYBACK_KEY_POSITION, SetPosition },
36 { AVPlaybackState::PLAYBACK_KEY_BUFFERED_TIME, SetBufferedTime },
37 { AVPlaybackState::PLAYBACK_KEY_LOOP_MODE, SetLoopMode },
38 { AVPlaybackState::PLAYBACK_KEY_IS_FAVORITE, SetIsFavorite },
39 { AVPlaybackState::PLAYBACK_KEY_ACTIVE_ITEM_ID, SetActiveItemId },
40 { AVPlaybackState::PLAYBACK_KEY_VOLUME, SetVolume },
41 { AVPlaybackState::PLAYBACK_KEY_EXTRAS, SetExtras },
42 };
43
44 std::map<std::string, int32_t> NapiPlaybackState::filterMap_ = {
45 { "state", AVPlaybackState::PLAYBACK_KEY_STATE },
46 { "speed", AVPlaybackState::PLAYBACK_KEY_SPEED },
47 { "position", AVPlaybackState::PLAYBACK_KEY_POSITION },
48 { "bufferedTime", AVPlaybackState::PLAYBACK_KEY_BUFFERED_TIME },
49 { "loopMode", AVPlaybackState::PLAYBACK_KEY_LOOP_MODE },
50 { "isFavorite", AVPlaybackState::PLAYBACK_KEY_IS_FAVORITE },
51 { "activeItemId", AVPlaybackState::PLAYBACK_KEY_ACTIVE_ITEM_ID },
52 { "volume", AVPlaybackState::PLAYBACK_KEY_VOLUME },
53 { "extras", AVPlaybackState::PLAYBACK_KEY_EXTRAS },
54 };
55
ConvertFilter(napi_env env,napi_value filter,AVPlaybackState::PlaybackStateMaskType & mask)56 napi_status NapiPlaybackState::ConvertFilter(napi_env env, napi_value filter,
57 AVPlaybackState::PlaybackStateMaskType& mask)
58 {
59 napi_valuetype type = napi_undefined;
60 auto status = napi_typeof(env, filter, &type);
61 CHECK_RETURN(status == napi_ok, "napi_typeof failed", status);
62
63 if (type == napi_string) {
64 std::string stringFilter;
65 status = NapiUtils::GetValue(env, filter, stringFilter);
66 CHECK_RETURN(status == napi_ok, "get string filter failed", status);
67 CHECK_RETURN(stringFilter == "all", "string filter only support all", napi_invalid_arg);
68
69 mask.set();
70 return napi_ok;
71 }
72
73 uint32_t count = 0;
74 status = napi_get_array_length(env, filter, &count);
75 CHECK_RETURN(status == napi_ok, "get array length failed", status);
76 for (uint32_t i = 0; i < count; i++) {
77 napi_value value {};
78 status = napi_get_element(env, filter, i, &value);
79 CHECK_RETURN(status == napi_ok, "get element failed", status);
80 std::string metaKey;
81 status = NapiUtils::GetValue(env, value, metaKey);
82 CHECK_RETURN(status == napi_ok, "get string value failed", status);
83 for (const auto& pair : filterMap_) {
84 if (pair.first == metaKey) {
85 mask.set(pair.second);
86 }
87 }
88 }
89
90 return napi_ok;
91 }
92
GetValue(napi_env env,napi_value in,AVPlaybackState & out)93 napi_status NapiPlaybackState::GetValue(napi_env env, napi_value in, AVPlaybackState& out)
94 {
95 std::vector<std::string> propertyNames;
96 auto status = NapiUtils::GetPropertyNames(env, in, propertyNames);
97 CHECK_RETURN(status == napi_ok, "get property name failed", status);
98
99 for (const auto& name : propertyNames) {
100 auto it = getterMap_.find(name);
101 if (it == getterMap_.end()) {
102 SLOGE("property %{public}s is not of metadata", name.c_str());
103 return napi_invalid_arg;
104 }
105 auto getter = it->second;
106 if (getter(env, in, out) != napi_ok) {
107 SLOGE("get property %{public}s failed", name.c_str());
108 return napi_generic_failure;
109 }
110 }
111
112 return napi_ok;
113 }
114
SetValue(napi_env env,const AVPlaybackState & in,napi_value & out)115 napi_status NapiPlaybackState::SetValue(napi_env env, const AVPlaybackState& in, napi_value& out)
116 {
117 napi_status status = napi_create_object(env, &out);
118 CHECK_RETURN((status == napi_ok) && (out != nullptr), "create object failed", status);
119
120 auto mask = in.GetMask();
121 for (int i = 0; i < AVPlaybackState::PLAYBACK_KEY_MAX; ++i) {
122 if (!mask.test(i)) {
123 continue;
124 }
125 auto setter = setterMap_[i];
126 if (setter(env, in, out) != napi_ok) {
127 SLOGE("set property %{public}d failed", i);
128 return napi_generic_failure;
129 }
130 }
131
132 return napi_ok;
133 }
134
GetState(napi_env env,napi_value in,AVPlaybackState & out)135 napi_status NapiPlaybackState::GetState(napi_env env, napi_value in, AVPlaybackState& out)
136 {
137 int32_t property;
138 auto status = NapiUtils::GetNamedProperty(env, in, "state", property);
139 CHECK_RETURN(status == napi_ok, "get property failed", status);
140 out.SetState(property);
141 return status;
142 }
143
SetState(napi_env env,const AVPlaybackState & in,napi_value & out)144 napi_status NapiPlaybackState::SetState(napi_env env, const AVPlaybackState& in, napi_value& out)
145 {
146 napi_value property {};
147 auto status = NapiUtils::SetValue(env, in.GetState(), property);
148 CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
149 status = napi_set_named_property(env, out, "state", property);
150 CHECK_RETURN(status == napi_ok, "set property failed", status);
151 return status;
152 }
153
GetSpeed(napi_env env,napi_value in,AVPlaybackState & out)154 napi_status NapiPlaybackState::GetSpeed(napi_env env, napi_value in, AVPlaybackState& out)
155 {
156 double property;
157 auto status = NapiUtils::GetNamedProperty(env, in, "speed", property);
158 CHECK_RETURN(status == napi_ok, "get property failed", status);
159 out.SetSpeed(property);
160 return status;
161 }
162
SetSpeed(napi_env env,const AVPlaybackState & in,napi_value & out)163 napi_status NapiPlaybackState::SetSpeed(napi_env env, const AVPlaybackState& in, napi_value& out)
164 {
165 napi_value property {};
166 auto status = NapiUtils::SetValue(env, in.GetSpeed(), property);
167 CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
168 status = napi_set_named_property(env, out, "speed", property);
169 CHECK_RETURN(status == napi_ok, "set property failed", status);
170 return status;
171 }
172
GetPosition(napi_env env,napi_value in,AVPlaybackState & out)173 napi_status NapiPlaybackState::GetPosition(napi_env env, napi_value in, AVPlaybackState& out)
174 {
175 napi_value result {};
176 auto status = napi_get_named_property(env, in, "position", &result);
177 CHECK_RETURN((status == napi_ok) && (result != nullptr), "get property failed", status);
178 int64_t updateTime {};
179 status = NapiUtils::GetNamedProperty(env, result, "updateTime", updateTime);
180 CHECK_RETURN(status == napi_ok, "get property failed", status);
181 int64_t elapsedTime {};
182 status = NapiUtils::GetNamedProperty(env, result, "elapsedTime", elapsedTime);
183 CHECK_RETURN(status == napi_ok, "get property failed", status);
184 out.SetPosition({elapsedTime, updateTime});
185 return status;
186 }
187
SetPosition(napi_env env,const AVPlaybackState & in,napi_value & out)188 napi_status NapiPlaybackState::SetPosition(napi_env env, const AVPlaybackState& in, napi_value& out)
189 {
190 napi_value positionProperty {};
191 napi_status status = napi_create_object(env, &positionProperty);
192 CHECK_RETURN((status == napi_ok) && (positionProperty != nullptr), "create object failed", status);
193
194 auto position = in.GetPosition();
195 napi_value elapsedProperty {};
196 status = NapiUtils::SetValue(env, position.elapsedTime_, elapsedProperty);
197 CHECK_RETURN((status == napi_ok) && (elapsedProperty != nullptr), "create property failed", status);
198 status = napi_set_named_property(env, positionProperty, "elapsedTime", elapsedProperty);
199 CHECK_RETURN(status == napi_ok, "set property failed", status);
200
201 napi_value updateProperty {};
202 status = NapiUtils::SetValue(env, position.updateTime_, updateProperty);
203 CHECK_RETURN((status == napi_ok) && (elapsedProperty != nullptr), "create property failed", status);
204 status = napi_set_named_property(env, positionProperty, "updateTime", updateProperty);
205 CHECK_RETURN(status == napi_ok, "set property failed", status);
206
207 status = napi_set_named_property(env, out, "position", positionProperty);
208 CHECK_RETURN(status == napi_ok, "set property failed", status);
209
210 return status;
211 }
212
GetBufferedTime(napi_env env,napi_value in,AVPlaybackState & out)213 napi_status NapiPlaybackState::GetBufferedTime(napi_env env, napi_value in, AVPlaybackState& out)
214 {
215 int64_t property;
216 auto status = NapiUtils::GetNamedProperty(env, in, "bufferedTime", property);
217 CHECK_RETURN(status == napi_ok, "get property failed", status);
218 out.SetBufferedTime(property);
219 return status;
220 }
221
SetBufferedTime(napi_env env,const AVPlaybackState & in,napi_value & out)222 napi_status NapiPlaybackState::SetBufferedTime(napi_env env, const AVPlaybackState& in, napi_value& out)
223 {
224 napi_value property {};
225 auto status = NapiUtils::SetValue(env, in.GetBufferedTime(), property);
226 CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
227 status = napi_set_named_property(env, out, "bufferedTime", property);
228 CHECK_RETURN(status == napi_ok, "set property failed", status);
229 return status;
230 }
231
GetLoopMode(napi_env env,napi_value in,AVPlaybackState & out)232 napi_status NapiPlaybackState::GetLoopMode(napi_env env, napi_value in, AVPlaybackState& out)
233 {
234 int32_t property;
235 auto status = NapiUtils::GetNamedProperty(env, in, "loopMode", property);
236 CHECK_RETURN(status == napi_ok, "get property failed", status);
237 out.SetLoopMode(property);
238 return status;
239 }
240
SetLoopMode(napi_env env,const AVPlaybackState & in,napi_value & out)241 napi_status NapiPlaybackState::SetLoopMode(napi_env env, const AVPlaybackState& in, napi_value& out)
242 {
243 napi_value property {};
244 auto status = NapiUtils::SetValue(env, in.GetLoopMode(), property);
245 CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
246 status = napi_set_named_property(env, out, "loopMode", property);
247 CHECK_RETURN(status == napi_ok, "set property failed", status);
248 return status;
249 }
250
GetIsFavorite(napi_env env,napi_value in,AVPlaybackState & out)251 napi_status NapiPlaybackState::GetIsFavorite(napi_env env, napi_value in, AVPlaybackState& out)
252 {
253 bool property;
254 auto status = NapiUtils::GetNamedProperty(env, in, "isFavorite", property);
255 CHECK_RETURN(status == napi_ok, "get property failed", status);
256 out.SetFavorite(property);
257 return status;
258 }
259
SetIsFavorite(napi_env env,const AVPlaybackState & in,napi_value & out)260 napi_status NapiPlaybackState::SetIsFavorite(napi_env env, const AVPlaybackState& in, napi_value& out)
261 {
262 napi_value property {};
263 auto status = NapiUtils::SetValue(env, in.GetFavorite(), property);
264 CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
265 status = napi_set_named_property(env, out, "isFavorite", property);
266 CHECK_RETURN(status == napi_ok, "set property failed", status);
267 return status;
268 }
269
GetActiveItemId(napi_env env,napi_value in,AVPlaybackState & out)270 napi_status NapiPlaybackState::GetActiveItemId(napi_env env, napi_value in, AVPlaybackState& out)
271 {
272 int32_t property;
273 auto status = NapiUtils::GetNamedProperty(env, in, "activeItemId", property);
274 CHECK_RETURN(status == napi_ok, "get property failed", status);
275 out.SetActiveItemId(property);
276 return status;
277 }
278
SetActiveItemId(napi_env env,const AVPlaybackState & in,napi_value & out)279 napi_status NapiPlaybackState::SetActiveItemId(napi_env env, const AVPlaybackState& in, napi_value& out)
280 {
281 napi_value property {};
282 auto status = NapiUtils::SetValue(env, in.GetActiveItemId(), property);
283 CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
284 status = napi_set_named_property(env, out, "activeItemId", property);
285 CHECK_RETURN(status == napi_ok, "set property failed", status);
286 return status;
287 }
288
GetVolume(napi_env env,napi_value in,AVPlaybackState & out)289 napi_status NapiPlaybackState::GetVolume(napi_env env, napi_value in, AVPlaybackState& out)
290 {
291 int32_t property;
292 auto status = NapiUtils::GetNamedProperty(env, in, "volume", property);
293 CHECK_RETURN(status == napi_ok, "get property failed", status);
294 out.SetVolume(property);
295 return status;
296 }
297
SetVolume(napi_env env,const AVPlaybackState & in,napi_value & out)298 napi_status NapiPlaybackState::SetVolume(napi_env env, const AVPlaybackState& in, napi_value& out)
299 {
300 napi_value property {};
301 auto status = NapiUtils::SetValue(env, in.GetVolume(), property);
302 CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
303 status = napi_set_named_property(env, out, "volume", property);
304 CHECK_RETURN(status == napi_ok, "set property failed", status);
305 return status;
306 }
307
GetExtras(napi_env env,napi_value in,AVPlaybackState & out)308 napi_status NapiPlaybackState::GetExtras(napi_env env, napi_value in, AVPlaybackState& out)
309 {
310 AAFwk::WantParams property {};
311 auto status = NapiUtils::GetNamedProperty(env, in, "extras", property);
312 CHECK_RETURN(status == napi_ok, "get property failed", status);
313 out.SetExtras(std::make_shared<AAFwk::WantParams>(property));
314 return status;
315 }
316
SetExtras(napi_env env,const AVPlaybackState & in,napi_value & out)317 napi_status NapiPlaybackState::SetExtras(napi_env env, const AVPlaybackState& in, napi_value& out)
318 {
319 napi_value property {};
320 auto extras = in.GetExtras();
321 if (extras == nullptr) {
322 SLOGE("Extras is null");
323 return napi_generic_failure;
324 }
325 AAFwk::WantParams params = *extras;
326 auto status = NapiUtils::SetValue(env, params, property);
327 CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
328 status = napi_set_named_property(env, out, "extras", property);
329 CHECK_RETURN(status == napi_ok, "set property failed", status);
330 return status;
331 }
332 }
333