• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 };
28 
29 std::map<int32_t, NapiPlaybackState::SetterType> NapiPlaybackState::setterMap_ = {
30     { AVPlaybackState::PLAYBACK_KEY_STATE, SetState },
31     { AVPlaybackState::PLAYBACK_KEY_SPEED, SetSpeed },
32     { AVPlaybackState::PLAYBACK_KEY_POSITION, SetPosition },
33     { AVPlaybackState::PLAYBACK_KEY_BUFFERED_TIME, SetBufferedTime },
34     { AVPlaybackState::PLAYBACK_KEY_LOOP_MODE, SetLoopMode },
35     { AVPlaybackState::PLAYBACK_KEY_IS_FAVORITE, SetIsFavorite },
36 };
37 
38 std::map<std::string, int32_t> NapiPlaybackState::filterMap_ = {
39     { "state", AVPlaybackState::PLAYBACK_KEY_STATE },
40     { "speed", AVPlaybackState::PLAYBACK_KEY_SPEED },
41     { "position", AVPlaybackState::PLAYBACK_KEY_POSITION },
42     { "bufferedTime", AVPlaybackState::PLAYBACK_KEY_BUFFERED_TIME },
43     { "loopMode", AVPlaybackState::PLAYBACK_KEY_LOOP_MODE },
44     { "isFavorite", AVPlaybackState::PLAYBACK_KEY_IS_FAVORITE },
45 };
46 
ConvertFilter(napi_env env,napi_value filter,AVPlaybackState::PlaybackStateMaskType & mask)47 napi_status NapiPlaybackState::ConvertFilter(napi_env env, napi_value filter,
48                                              AVPlaybackState::PlaybackStateMaskType& mask)
49 {
50     napi_valuetype type = napi_undefined;
51     auto status = napi_typeof(env, filter, &type);
52     CHECK_RETURN(status == napi_ok, "napi_typeof failed", status);
53 
54     if (type == napi_string) {
55         std::string stringFilter;
56         status = NapiUtils::GetValue(env, filter, stringFilter);
57         CHECK_RETURN(status == napi_ok, "get string filter failed", status);
58         CHECK_RETURN(stringFilter == "all", "string filter only support all", napi_invalid_arg);
59 
60         mask.set();
61         return napi_ok;
62     }
63 
64     uint32_t count = 0;
65     status = napi_get_array_length(env, filter, &count);
66     CHECK_RETURN(status == napi_ok, "get array length failed", status);
67     for (uint32_t i = 0; i < count; i++) {
68         napi_value value {};
69         status = napi_get_element(env, filter, i, &value);
70         CHECK_RETURN(status == napi_ok, "get element failed", status);
71         std::string metaKey;
72         status = NapiUtils::GetValue(env, value, metaKey);
73         CHECK_RETURN(status == napi_ok, "get string value failed", status);
74         for (const auto& pair : filterMap_) {
75             if (pair.first == metaKey) {
76                 mask.set(pair.second);
77             }
78         }
79     }
80 
81     return napi_ok;
82 }
83 
GetValue(napi_env env,napi_value in,AVPlaybackState & out)84 napi_status NapiPlaybackState::GetValue(napi_env env, napi_value in, AVPlaybackState& out)
85 {
86     std::vector<std::string> propertyNames;
87     auto status = NapiUtils::GetPropertyNames(env, in, propertyNames);
88     CHECK_RETURN(status == napi_ok, "get property name failed", status);
89 
90     for (const auto& name : propertyNames) {
91         auto it = getterMap_.find(name);
92         if (it == getterMap_.end()) {
93             SLOGE("property %{public}s is not of metadata", name.c_str());
94             return napi_invalid_arg;
95         }
96         auto getter = it->second;
97         if (getter(env, in, out) != napi_ok) {
98             SLOGE("get property %{public}s failed", name.c_str());
99             return napi_generic_failure;
100         }
101     }
102 
103     return napi_ok;
104 }
105 
SetValue(napi_env env,const AVPlaybackState & in,napi_value & out)106 napi_status NapiPlaybackState::SetValue(napi_env env, const AVPlaybackState& in, napi_value& out)
107 {
108     napi_status status = napi_create_object(env, &out);
109     CHECK_RETURN((status == napi_ok) && (out != nullptr), "create object failed", status);
110 
111     auto mask = in.GetMask();
112     for (int i = 0; i < AVPlaybackState::PLAYBACK_KEY_MAX; ++i) {
113         if (!mask.test(i)) {
114             continue;
115         }
116         auto setter = setterMap_[i];
117         if (setter(env, in, out) != napi_ok) {
118             SLOGE("set property %{public}d failed", i);
119             return napi_generic_failure;
120         }
121     }
122 
123     return napi_ok;
124 }
125 
GetState(napi_env env,napi_value in,AVPlaybackState & out)126 napi_status NapiPlaybackState::GetState(napi_env env, napi_value in, AVPlaybackState& out)
127 {
128     int32_t property;
129     auto status = NapiUtils::GetNamedProperty(env, in, "state", property);
130     CHECK_RETURN(status == napi_ok, "get property failed", status);
131     out.SetState(property);
132     return status;
133 }
134 
SetState(napi_env env,const AVPlaybackState & in,napi_value & out)135 napi_status NapiPlaybackState::SetState(napi_env env, const AVPlaybackState& in, napi_value& out)
136 {
137     napi_value property {};
138     auto status = NapiUtils::SetValue(env, in.GetState(), property);
139     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
140     status = napi_set_named_property(env, out, "state", property);
141     CHECK_RETURN(status == napi_ok, "set property failed", status);
142     return status;
143 }
144 
GetSpeed(napi_env env,napi_value in,AVPlaybackState & out)145 napi_status NapiPlaybackState::GetSpeed(napi_env env, napi_value in, AVPlaybackState& out)
146 {
147     double property;
148     auto status = NapiUtils::GetNamedProperty(env, in, "speed", property);
149     CHECK_RETURN(status == napi_ok, "get property failed", status);
150     out.SetSpeed(property);
151     return status;
152 }
153 
SetSpeed(napi_env env,const AVPlaybackState & in,napi_value & out)154 napi_status NapiPlaybackState::SetSpeed(napi_env env, const AVPlaybackState& in, napi_value& out)
155 {
156     napi_value property {};
157     auto status = NapiUtils::SetValue(env, in.GetSpeed(), property);
158     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
159     status = napi_set_named_property(env, out, "speed", property);
160     CHECK_RETURN(status == napi_ok, "set property failed", status);
161     return status;
162 }
163 
GetPosition(napi_env env,napi_value in,AVPlaybackState & out)164 napi_status NapiPlaybackState::GetPosition(napi_env env, napi_value in, AVPlaybackState& out)
165 {
166     napi_value result {};
167     auto status = napi_get_named_property(env, in, "position", &result);
168     CHECK_RETURN((status == napi_ok) && (result != nullptr), "get property failed", status);
169     int64_t updateTime {};
170     status = NapiUtils::GetNamedProperty(env, result, "updateTime", updateTime);
171     CHECK_RETURN(status == napi_ok, "get property failed", status);
172     int64_t elapsedTime {};
173     status = NapiUtils::GetNamedProperty(env, result, "elapsedTime", elapsedTime);
174     CHECK_RETURN(status == napi_ok, "get property failed", status);
175     out.SetPosition({elapsedTime, updateTime});
176     return status;
177 }
178 
SetPosition(napi_env env,const AVPlaybackState & in,napi_value & out)179 napi_status NapiPlaybackState::SetPosition(napi_env env, const AVPlaybackState& in, napi_value& out)
180 {
181     napi_value positionProperty {};
182     napi_status status = napi_create_object(env, &positionProperty);
183     CHECK_RETURN((status == napi_ok) && (positionProperty != nullptr), "create object failed", status);
184 
185     auto position = in.GetPosition();
186     napi_value elapsedProperty {};
187     status = NapiUtils::SetValue(env, position.elapsedTime_, elapsedProperty);
188     CHECK_RETURN((status == napi_ok) && (elapsedProperty != nullptr), "create property failed", status);
189     status = napi_set_named_property(env, positionProperty, "elapsedTime", elapsedProperty);
190     CHECK_RETURN(status == napi_ok, "set property failed", status);
191 
192     napi_value updateProperty {};
193     status = NapiUtils::SetValue(env, position.updateTime_, updateProperty);
194     CHECK_RETURN((status == napi_ok) && (elapsedProperty != nullptr), "create property failed", status);
195     status = napi_set_named_property(env, positionProperty, "updateTime", updateProperty);
196     CHECK_RETURN(status == napi_ok, "set property failed", status);
197 
198     status = napi_set_named_property(env, out, "position", positionProperty);
199     CHECK_RETURN(status == napi_ok, "set property failed", status);
200 
201     return status;
202 }
203 
GetBufferedTime(napi_env env,napi_value in,AVPlaybackState & out)204 napi_status NapiPlaybackState::GetBufferedTime(napi_env env, napi_value in, AVPlaybackState& out)
205 {
206     int64_t property;
207     auto status = NapiUtils::GetNamedProperty(env, in, "bufferedTime", property);
208     CHECK_RETURN(status == napi_ok, "get property failed", status);
209     out.SetBufferedTime(property);
210     return status;
211 }
212 
SetBufferedTime(napi_env env,const AVPlaybackState & in,napi_value & out)213 napi_status NapiPlaybackState::SetBufferedTime(napi_env env, const AVPlaybackState& in, napi_value& out)
214 {
215     napi_value property {};
216     auto status = NapiUtils::SetValue(env, in.GetBufferedTime(), property);
217     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
218     status = napi_set_named_property(env, out, "bufferedTime", property);
219     CHECK_RETURN(status == napi_ok, "set property failed", status);
220     return status;
221 }
222 
GetLoopMode(napi_env env,napi_value in,AVPlaybackState & out)223 napi_status NapiPlaybackState::GetLoopMode(napi_env env, napi_value in, AVPlaybackState& out)
224 {
225     int32_t property;
226     auto status = NapiUtils::GetNamedProperty(env, in, "loopMode", property);
227     CHECK_RETURN(status == napi_ok, "get property failed", status);
228     out.SetLoopMode(property);
229     return status;
230 }
231 
SetLoopMode(napi_env env,const AVPlaybackState & in,napi_value & out)232 napi_status NapiPlaybackState::SetLoopMode(napi_env env, const AVPlaybackState& in, napi_value& out)
233 {
234     napi_value property {};
235     auto status = NapiUtils::SetValue(env, in.GetLoopMode(), property);
236     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
237     status = napi_set_named_property(env, out, "loopMode", property);
238     CHECK_RETURN(status == napi_ok, "set property failed", status);
239     return status;
240 }
241 
GetIsFavorite(napi_env env,napi_value in,AVPlaybackState & out)242 napi_status NapiPlaybackState::GetIsFavorite(napi_env env, napi_value in, AVPlaybackState& out)
243 {
244     bool property;
245     auto status = NapiUtils::GetNamedProperty(env, in, "isFavorite", property);
246     CHECK_RETURN(status == napi_ok, "get property failed", status);
247     out.SetFavorite(property);
248     return status;
249 }
250 
SetIsFavorite(napi_env env,const AVPlaybackState & in,napi_value & out)251 napi_status NapiPlaybackState::SetIsFavorite(napi_env env, const AVPlaybackState& in, napi_value& out)
252 {
253     napi_value property {};
254     auto status = NapiUtils::SetValue(env, in.GetFavorite(), property);
255     CHECK_RETURN((status == napi_ok) && (property != nullptr), "create property failed", status);
256     status = napi_set_named_property(env, out, "isFavorite", property);
257     CHECK_RETURN(status == napi_ok, "set property failed", status);
258     return status;
259 }
260 }