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 { "maxVolume", GetMaxVolume },
30 { "muted", GetMuted },
31 { "duration", GetDuration },
32 { "videoWidth", GetVideoWidth },
33 { "videoHeight", GetVideoHeight },
34 { "extras", GetExtras },
35 };
36
37 std::map<int32_t, NapiPlaybackState::SetterType> NapiPlaybackState::setterMap_ = {
38 { AVPlaybackState::PLAYBACK_KEY_STATE, SetState },
39 { AVPlaybackState::PLAYBACK_KEY_SPEED, SetSpeed },
40 { AVPlaybackState::PLAYBACK_KEY_POSITION, SetPosition },
41 { AVPlaybackState::PLAYBACK_KEY_BUFFERED_TIME, SetBufferedTime },
42 { AVPlaybackState::PLAYBACK_KEY_LOOP_MODE, SetLoopMode },
43 { AVPlaybackState::PLAYBACK_KEY_IS_FAVORITE, SetIsFavorite },
44 { AVPlaybackState::PLAYBACK_KEY_ACTIVE_ITEM_ID, SetActiveItemId },
45 { AVPlaybackState::PLAYBACK_KEY_VOLUME, SetVolume },
46 { AVPlaybackState::PLAYBACK_KEY_MAX_VOLUME, SetMaxVolume },
47 { AVPlaybackState::PLAYBACK_KEY_MUTED, SetMuted },
48 { AVPlaybackState::PLAYBACK_KEY_DURATION, SetDuration },
49 { AVPlaybackState::PLAYBACK_KEY_VIDEO_WIDTH, SetVideoWidth },
50 { AVPlaybackState::PLAYBACK_KEY_VIDEO_HEIGHT, SetVideoHeight },
51 { AVPlaybackState::PLAYBACK_KEY_EXTRAS, SetExtras },
52 };
53
54 std::map<std::string, int32_t> NapiPlaybackState::filterMap_ = {
55 { "state", AVPlaybackState::PLAYBACK_KEY_STATE },
56 { "speed", AVPlaybackState::PLAYBACK_KEY_SPEED },
57 { "position", AVPlaybackState::PLAYBACK_KEY_POSITION },
58 { "bufferedTime", AVPlaybackState::PLAYBACK_KEY_BUFFERED_TIME },
59 { "loopMode", AVPlaybackState::PLAYBACK_KEY_LOOP_MODE },
60 { "isFavorite", AVPlaybackState::PLAYBACK_KEY_IS_FAVORITE },
61 { "activeItemId", AVPlaybackState::PLAYBACK_KEY_ACTIVE_ITEM_ID },
62 { "volume", AVPlaybackState::PLAYBACK_KEY_VOLUME },
63 { "maxVolume", AVPlaybackState::PLAYBACK_KEY_MAX_VOLUME },
64 { "muted", AVPlaybackState::PLAYBACK_KEY_MUTED },
65 { "duration", AVPlaybackState::PLAYBACK_KEY_DURATION },
66 { "videoWidth", AVPlaybackState::PLAYBACK_KEY_VIDEO_WIDTH },
67 { "videoHeight", AVPlaybackState::PLAYBACK_KEY_VIDEO_HEIGHT },
68 { "extras", AVPlaybackState::PLAYBACK_KEY_EXTRAS },
69 };
70
ConvertFilter(napi_env env,napi_value filter,AVPlaybackState::PlaybackStateMaskType & mask)71 napi_status NapiPlaybackState::ConvertFilter(napi_env env, napi_value filter,
72 AVPlaybackState::PlaybackStateMaskType& mask)
73 {
74 napi_valuetype type = napi_undefined;
75 auto status = napi_typeof(env, filter, &type);
76 CHECK_RETURN(status == napi_ok, "napi_typeof failed", status);
77
78 if (type == napi_string) {
79 std::string stringFilter;
80 status = NapiUtils::GetValue(env, filter, stringFilter);
81 CHECK_RETURN(status == napi_ok, "get string filter failed", status);
82 CHECK_RETURN(stringFilter == "all", "string filter only support all", napi_invalid_arg);
83
84 mask.set();
85 return napi_ok;
86 }
87
88 uint32_t count = 0;
89 status = napi_get_array_length(env, filter, &count);
90 CHECK_RETURN(status == napi_ok, "get array length failed", status);
91 for (uint32_t i = 0; i < count; i++) {
92 napi_value value {};
93 status = napi_get_element(env, filter, i, &value);
94 CHECK_RETURN(status == napi_ok, "get element failed", status);
95 std::string metaKey;
96 status = NapiUtils::GetValue(env, value, metaKey);
97 CHECK_RETURN(status == napi_ok, "get string value failed", status);
98 for (const auto& pair : filterMap_) {
99 if (pair.first == metaKey) {
100 mask.set(pair.second);
101 }
102 }
103 }
104
105 return napi_ok;
106 }
107
GetValue(napi_env env,napi_value in,AVPlaybackState & out)108 napi_status NapiPlaybackState::GetValue(napi_env env, napi_value in, AVPlaybackState& out)
109 {
110 std::vector<std::string> propertyNames;
111 auto status = NapiUtils::GetPropertyNames(env, in, propertyNames);
112 CHECK_RETURN(status == napi_ok, "get property name failed", status);
113
114 for (const auto& name : propertyNames) {
115 auto it = getterMap_.find(name);
116 if (it == getterMap_.end()) {
117 SLOGE("property %{public}s is not of metadata", name.c_str());
118 return napi_invalid_arg;
119 }
120 auto getter = it->second;
121 if (getter(env, in, out) != napi_ok) {
122 SLOGE("get property %{public}s failed", name.c_str());
123 return napi_generic_failure;
124 }
125 }
126
127 return napi_ok;
128 }
129
SetValue(napi_env env,const AVPlaybackState & in,napi_value & out)130 napi_status NapiPlaybackState::SetValue(napi_env env, const AVPlaybackState& in, napi_value& out)
131 {
132 napi_status status = napi_create_object(env, &out);
133 CHECK_RETURN((status == napi_ok) && (out != nullptr), "create object failed", status);
134
135 auto mask = in.GetMask();
136 for (int i = 0; i < AVPlaybackState::PLAYBACK_KEY_MAX; ++i) {
137 if (!mask.test(i)) {
138 continue;
139 }
140 auto setter = setterMap_[i];
141 if (setter(env, in, out) != napi_ok) {
142 SLOGE("set property %{public}d failed", i);
143 return napi_generic_failure;
144 }
145 }
146
147 return napi_ok;
148 }
149
GetState(napi_env env,napi_value in,AVPlaybackState & out)150 napi_status NapiPlaybackState::GetState(napi_env env, napi_value in, AVPlaybackState& out)
151 {
152 int32_t property;
153 auto status = NapiUtils::GetNamedProperty(env, in, "state", property);
154 CHECK_RETURN(status == napi_ok, "get property failed", status);
155 out.SetState(property);
156 return status;
157 }
158
SetState(napi_env env,const AVPlaybackState & in,napi_value & out)159 napi_status NapiPlaybackState::SetState(napi_env env, const AVPlaybackState& in, napi_value& out)
160 {
161 napi_value property {};
162 auto status = NapiUtils::SetValue(env, in.GetState(), property);
163 CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
164 status = napi_set_named_property(env, out, "state", property);
165 CHECK_RETURN(status == napi_ok, "set property failed", status);
166 return status;
167 }
168
GetSpeed(napi_env env,napi_value in,AVPlaybackState & out)169 napi_status NapiPlaybackState::GetSpeed(napi_env env, napi_value in, AVPlaybackState& out)
170 {
171 double property;
172 auto status = NapiUtils::GetNamedProperty(env, in, "speed", property);
173 CHECK_RETURN(status == napi_ok, "get property failed", status);
174 out.SetSpeed(property);
175 return status;
176 }
177
SetSpeed(napi_env env,const AVPlaybackState & in,napi_value & out)178 napi_status NapiPlaybackState::SetSpeed(napi_env env, const AVPlaybackState& in, napi_value& out)
179 {
180 napi_value property {};
181 auto status = NapiUtils::SetValue(env, in.GetSpeed(), property);
182 CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
183 status = napi_set_named_property(env, out, "speed", property);
184 CHECK_RETURN(status == napi_ok, "set property failed", status);
185 return status;
186 }
187
GetPosition(napi_env env,napi_value in,AVPlaybackState & out)188 napi_status NapiPlaybackState::GetPosition(napi_env env, napi_value in, AVPlaybackState& out)
189 {
190 napi_value result {};
191 auto status = napi_get_named_property(env, in, "position", &result);
192 CHECK_RETURN((status == napi_ok) && (result != nullptr), "get property failed", status);
193 int64_t updateTime {};
194 status = NapiUtils::GetNamedProperty(env, result, "updateTime", updateTime);
195 CHECK_RETURN(status == napi_ok, "get property failed", status);
196 int64_t elapsedTime {};
197 status = NapiUtils::GetNamedProperty(env, result, "elapsedTime", elapsedTime);
198 CHECK_RETURN(status == napi_ok, "get property failed", status);
199 out.SetPosition({elapsedTime, updateTime});
200 return status;
201 }
202
SetPosition(napi_env env,const AVPlaybackState & in,napi_value & out)203 napi_status NapiPlaybackState::SetPosition(napi_env env, const AVPlaybackState& in, napi_value& out)
204 {
205 napi_value positionProperty {};
206 napi_status status = napi_create_object(env, &positionProperty);
207 CHECK_RETURN((status == napi_ok) && (positionProperty != nullptr), "create object failed", status);
208
209 auto position = in.GetPosition();
210 napi_value elapsedProperty {};
211 status = NapiUtils::SetValue(env, position.elapsedTime_, elapsedProperty);
212 CHECK_RETURN((status == napi_ok) && (elapsedProperty != nullptr), "create property failed", status);
213 status = napi_set_named_property(env, positionProperty, "elapsedTime", elapsedProperty);
214 CHECK_RETURN(status == napi_ok, "set property failed", status);
215
216 napi_value updateProperty {};
217 status = NapiUtils::SetValue(env, position.updateTime_, updateProperty);
218 CHECK_RETURN((status == napi_ok) && (elapsedProperty != nullptr), "create property failed", status);
219 status = napi_set_named_property(env, positionProperty, "updateTime", updateProperty);
220 CHECK_RETURN(status == napi_ok, "set property failed", status);
221
222 status = napi_set_named_property(env, out, "position", positionProperty);
223 CHECK_RETURN(status == napi_ok, "set property failed", status);
224
225 return status;
226 }
227
GetBufferedTime(napi_env env,napi_value in,AVPlaybackState & out)228 napi_status NapiPlaybackState::GetBufferedTime(napi_env env, napi_value in, AVPlaybackState& out)
229 {
230 int64_t property;
231 auto status = NapiUtils::GetNamedProperty(env, in, "bufferedTime", property);
232 CHECK_RETURN(status == napi_ok, "get property failed", status);
233 out.SetBufferedTime(property);
234 return status;
235 }
236
SetBufferedTime(napi_env env,const AVPlaybackState & in,napi_value & out)237 napi_status NapiPlaybackState::SetBufferedTime(napi_env env, const AVPlaybackState& in, napi_value& out)
238 {
239 napi_value property {};
240 auto status = NapiUtils::SetValue(env, in.GetBufferedTime(), property);
241 CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
242 status = napi_set_named_property(env, out, "bufferedTime", property);
243 CHECK_RETURN(status == napi_ok, "set property failed", status);
244 return status;
245 }
246
GetLoopMode(napi_env env,napi_value in,AVPlaybackState & out)247 napi_status NapiPlaybackState::GetLoopMode(napi_env env, napi_value in, AVPlaybackState& out)
248 {
249 int32_t property;
250 auto status = NapiUtils::GetNamedProperty(env, in, "loopMode", property);
251 CHECK_RETURN(status == napi_ok, "get property failed", status);
252 out.SetLoopMode(property);
253 return status;
254 }
255
SetLoopMode(napi_env env,const AVPlaybackState & in,napi_value & out)256 napi_status NapiPlaybackState::SetLoopMode(napi_env env, const AVPlaybackState& in, napi_value& out)
257 {
258 napi_value property {};
259 auto status = NapiUtils::SetValue(env, in.GetLoopMode(), property);
260 CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
261 status = napi_set_named_property(env, out, "loopMode", property);
262 CHECK_RETURN(status == napi_ok, "set property failed", status);
263 return status;
264 }
265
GetIsFavorite(napi_env env,napi_value in,AVPlaybackState & out)266 napi_status NapiPlaybackState::GetIsFavorite(napi_env env, napi_value in, AVPlaybackState& out)
267 {
268 bool property;
269 auto status = NapiUtils::GetNamedProperty(env, in, "isFavorite", property);
270 CHECK_RETURN(status == napi_ok, "get property failed", status);
271 out.SetFavorite(property);
272 return status;
273 }
274
SetIsFavorite(napi_env env,const AVPlaybackState & in,napi_value & out)275 napi_status NapiPlaybackState::SetIsFavorite(napi_env env, const AVPlaybackState& in, napi_value& out)
276 {
277 napi_value property {};
278 auto status = NapiUtils::SetValue(env, in.GetFavorite(), property);
279 CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
280 status = napi_set_named_property(env, out, "isFavorite", property);
281 CHECK_RETURN(status == napi_ok, "set property failed", status);
282 return status;
283 }
284
GetActiveItemId(napi_env env,napi_value in,AVPlaybackState & out)285 napi_status NapiPlaybackState::GetActiveItemId(napi_env env, napi_value in, AVPlaybackState& out)
286 {
287 int32_t property;
288 auto status = NapiUtils::GetNamedProperty(env, in, "activeItemId", property);
289 CHECK_RETURN(status == napi_ok, "get property failed", status);
290 out.SetActiveItemId(property);
291 return status;
292 }
293
SetActiveItemId(napi_env env,const AVPlaybackState & in,napi_value & out)294 napi_status NapiPlaybackState::SetActiveItemId(napi_env env, const AVPlaybackState& in, napi_value& out)
295 {
296 napi_value property {};
297 auto status = NapiUtils::SetValue(env, in.GetActiveItemId(), property);
298 CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
299 status = napi_set_named_property(env, out, "activeItemId", property);
300 CHECK_RETURN(status == napi_ok, "set property failed", status);
301 return status;
302 }
303
GetVolume(napi_env env,napi_value in,AVPlaybackState & out)304 napi_status NapiPlaybackState::GetVolume(napi_env env, napi_value in, AVPlaybackState& out)
305 {
306 int32_t property;
307 auto status = NapiUtils::GetNamedProperty(env, in, "volume", property);
308 CHECK_RETURN(status == napi_ok, "get property failed", status);
309 out.SetVolume(property);
310 return status;
311 }
312
SetVolume(napi_env env,const AVPlaybackState & in,napi_value & out)313 napi_status NapiPlaybackState::SetVolume(napi_env env, const AVPlaybackState& in, napi_value& out)
314 {
315 napi_value property {};
316 auto status = NapiUtils::SetValue(env, in.GetVolume(), property);
317 CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
318 status = napi_set_named_property(env, out, "volume", property);
319 CHECK_RETURN(status == napi_ok, "set property failed", status);
320 return status;
321 }
322
GetMaxVolume(napi_env env,napi_value in,AVPlaybackState & out)323 napi_status NapiPlaybackState::GetMaxVolume(napi_env env, napi_value in, AVPlaybackState& out)
324 {
325 int32_t property;
326 auto status = NapiUtils::GetNamedProperty(env, in, "maxVolume", property);
327 CHECK_RETURN(status == napi_ok, "get property failed", status);
328 out.SetMaxVolume(property);
329 return status;
330 }
331
SetMaxVolume(napi_env env,const AVPlaybackState & in,napi_value & out)332 napi_status NapiPlaybackState::SetMaxVolume(napi_env env, const AVPlaybackState& in, napi_value& out)
333 {
334 napi_value property {};
335 auto status = NapiUtils::SetValue(env, in.GetMaxVolume(), property);
336 CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
337 status = napi_set_named_property(env, out, "maxVolume", property);
338 CHECK_RETURN(status == napi_ok, "set property failed", status);
339 return status;
340 }
341
GetMuted(napi_env env,napi_value in,AVPlaybackState & out)342 napi_status NapiPlaybackState::GetMuted(napi_env env, napi_value in, AVPlaybackState& out)
343 {
344 bool property;
345 auto status = NapiUtils::GetNamedProperty(env, in, "muted", property);
346 CHECK_RETURN(status == napi_ok, "get property failed", status);
347 out.SetMuted(property);
348 return status;
349 }
350
SetMuted(napi_env env,const AVPlaybackState & in,napi_value & out)351 napi_status NapiPlaybackState::SetMuted(napi_env env, const AVPlaybackState& in, napi_value& out)
352 {
353 napi_value property {};
354 auto status = NapiUtils::SetValue(env, in.GetMuted(), property);
355 CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
356 status = napi_set_named_property(env, out, "muted", property);
357 CHECK_RETURN(status == napi_ok, "set property failed", status);
358 return status;
359 }
360
GetDuration(napi_env env,napi_value in,AVPlaybackState & out)361 napi_status NapiPlaybackState::GetDuration(napi_env env, napi_value in, AVPlaybackState& out)
362 {
363 int32_t property;
364 auto status = NapiUtils::GetNamedProperty(env, in, "duration", property);
365 CHECK_RETURN(status == napi_ok, "get property failed", status);
366 out.SetDuration(property);
367 return status;
368 }
369
SetDuration(napi_env env,const AVPlaybackState & in,napi_value & out)370 napi_status NapiPlaybackState::SetDuration(napi_env env, const AVPlaybackState& in, napi_value& out)
371 {
372 napi_value property {};
373 auto status = NapiUtils::SetValue(env, in.GetDuration(), property);
374 CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
375 status = napi_set_named_property(env, out, "duration", property);
376 CHECK_RETURN(status == napi_ok, "set property failed", status);
377 return status;
378 }
379
GetVideoWidth(napi_env env,napi_value in,AVPlaybackState & out)380 napi_status NapiPlaybackState::GetVideoWidth(napi_env env, napi_value in, AVPlaybackState& out)
381 {
382 int32_t property;
383 auto status = NapiUtils::GetNamedProperty(env, in, "videoWidth", property);
384 CHECK_RETURN(status == napi_ok, "get property failed", status);
385 out.SetVideoWidth(property);
386 return status;
387 }
388
SetVideoWidth(napi_env env,const AVPlaybackState & in,napi_value & out)389 napi_status NapiPlaybackState::SetVideoWidth(napi_env env, const AVPlaybackState& in, napi_value& out)
390 {
391 napi_value property {};
392 auto status = NapiUtils::SetValue(env, in.GetVideoWidth(), property);
393 CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
394 status = napi_set_named_property(env, out, "videoWidth", property);
395 CHECK_RETURN(status == napi_ok, "set property failed", status);
396 return status;
397 }
398
GetVideoHeight(napi_env env,napi_value in,AVPlaybackState & out)399 napi_status NapiPlaybackState::GetVideoHeight(napi_env env, napi_value in, AVPlaybackState& out)
400 {
401 int32_t property;
402 auto status = NapiUtils::GetNamedProperty(env, in, "videoHeight", property);
403 CHECK_RETURN(status == napi_ok, "get property failed", status);
404 out.SetVideoHeight(property);
405 return status;
406 }
407
SetVideoHeight(napi_env env,const AVPlaybackState & in,napi_value & out)408 napi_status NapiPlaybackState::SetVideoHeight(napi_env env, const AVPlaybackState& in, napi_value& out)
409 {
410 napi_value property {};
411 auto status = NapiUtils::SetValue(env, in.GetVideoHeight(), property);
412 CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
413 status = napi_set_named_property(env, out, "videoHeight", property);
414 CHECK_RETURN(status == napi_ok, "set property failed", status);
415 return status;
416 }
417
GetExtras(napi_env env,napi_value in,AVPlaybackState & out)418 napi_status NapiPlaybackState::GetExtras(napi_env env, napi_value in, AVPlaybackState& out)
419 {
420 AAFwk::WantParams property {};
421 auto status = NapiUtils::GetNamedProperty(env, in, "extras", property);
422 CHECK_RETURN(status == napi_ok, "get property failed", status);
423 out.SetExtras(std::make_shared<AAFwk::WantParams>(property));
424 return status;
425 }
426
SetExtras(napi_env env,const AVPlaybackState & in,napi_value & out)427 napi_status NapiPlaybackState::SetExtras(napi_env env, const AVPlaybackState& in, napi_value& out)
428 {
429 napi_value property {};
430 auto extras = in.GetExtras();
431 if (extras == nullptr) {
432 SLOGE("Extras is null");
433 return napi_generic_failure;
434 }
435 AAFwk::WantParams params = *extras;
436 auto status = NapiUtils::SetValue(env, params, property);
437 CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
438 status = napi_set_named_property(env, out, "extras", property);
439 CHECK_RETURN(status == napi_ok, "set property failed", status);
440 return status;
441 }
442 }
443