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_control_command.h"
17 #include "avsession_log.h"
18 #include "avsession_errors.h"
19 #include "napi_utils.h"
20
21 namespace OHOS::AVSession {
22 std::map<std::string, std::tuple<NapiControlCommand::GetterType, NapiControlCommand::SetterType, int32_t>>
23 NapiControlCommand::commandMap_ = {
24 { "play", { GetNoneParam, SetNoneParam, AVControlCommand::SESSION_CMD_PLAY } },
25 { "pause", { GetNoneParam, SetNoneParam, AVControlCommand::SESSION_CMD_PAUSE } },
26 { "stop", { GetNoneParam, SetNoneParam, AVControlCommand::SESSION_CMD_STOP } },
27 { "playNext", { GetNoneParam, SetNoneParam, AVControlCommand::SESSION_CMD_PLAY_NEXT } },
28 { "playPrevious", { GetNoneParam, SetNoneParam, AVControlCommand::SESSION_CMD_PLAY_PREVIOUS } },
29 { "fastForward", { GetNoneParam, SetNoneParam, AVControlCommand::SESSION_CMD_FAST_FORWARD } },
30 { "rewind", { GetNoneParam, SetNoneParam, AVControlCommand::SESSION_CMD_REWIND } },
31 { "seek", { GetSeekTime, SetSeekTime, AVControlCommand::SESSION_CMD_SEEK } },
32 { "setSpeed", { GetSpeed, SetSpeed, AVControlCommand::SESSION_CMD_SET_SPEED } },
33 { "setLoopMode", { GetLoopMode, SetLoopMode, AVControlCommand::SESSION_CMD_SET_LOOP_MODE } },
34 { "toggleFavorite", { GetAssetId, SetAssetId, AVControlCommand::SESSION_CMD_TOGGLE_FAVORITE } },
35 };
36
ConvertCommand(const std::string & cmd)37 int32_t NapiControlCommand::ConvertCommand(const std::string& cmd)
38 {
39 for (const auto& [key, value] : commandMap_) {
40 if (key == cmd) {
41 return std::get<ENUM_INDEX>(value);
42 }
43 }
44 return AVControlCommand::SESSION_CMD_INVALID;
45 }
46
ConvertCommand(int32_t cmd)47 std::string NapiControlCommand::ConvertCommand(int32_t cmd)
48 {
49 for (const auto& [key, value] : commandMap_) {
50 if (std::get<ENUM_INDEX>(value) == cmd) {
51 return key;
52 }
53 }
54 return {};
55 }
56
ConvertCommands(const std::vector<int32_t> & cmds)57 std::vector<std::string> NapiControlCommand::ConvertCommands(const std::vector<int32_t>& cmds)
58 {
59 std::vector<std::string> result;
60 for (const auto& cmd : cmds) {
61 auto stringCmd = ConvertCommand(cmd);
62 if (!stringCmd.empty()) {
63 result.push_back(stringCmd);
64 }
65 }
66 return result;
67 }
68
GetValue(napi_env env,napi_value in,AVControlCommand & out)69 napi_status NapiControlCommand::GetValue(napi_env env, napi_value in, AVControlCommand& out)
70 {
71 std::string cmd;
72 auto status = NapiUtils::GetNamedProperty(env, in, "command", cmd);
73 if (status != napi_ok) {
74 SLOGE("get command property failed");
75 return status;
76 }
77
78 SLOGI("cmd=%{public}s", cmd.c_str());
79 auto it = commandMap_.find(cmd);
80 if (it == commandMap_.end()) {
81 SLOGE("cmd is invalid");
82 return napi_invalid_arg;
83 }
84 if (out.SetCommand(ConvertCommand(cmd)) != AVSESSION_SUCCESS) {
85 SLOGE("native set command failed");
86 return napi_invalid_arg;
87 }
88 auto getter = std::get<GETTER_INDEX>(it->second);
89 return getter(env, in, out);
90 }
91
SetValue(napi_env env,AVControlCommand & in,napi_value & out)92 napi_status NapiControlCommand::SetValue(napi_env env, AVControlCommand& in, napi_value& out)
93 {
94 napi_status status = napi_create_object(env, &out);
95 CHECK_RETURN((status == napi_ok) && (out != nullptr), "create object failed", status);
96
97 auto cmd = ConvertCommand(in.GetCommand());
98 napi_value property {};
99 status = NapiUtils::SetValue(env, cmd, property);
100 CHECK_RETURN(status == napi_ok, "create command property failed", status);
101 status = napi_set_named_property(env, out, "command", property);
102 CHECK_RETURN(status == napi_ok, "set command property failed", status);
103
104 auto it = commandMap_.find(cmd);
105 if (it == commandMap_.end()) {
106 SLOGE("cmd is invalid");
107 return napi_invalid_arg;
108 }
109
110 auto setter = std::get<SETTER_INDEX>(it->second);
111 return setter(env, in, out);
112 }
113
GetNoneParam(napi_env env,napi_value in,AVControlCommand & out)114 napi_status NapiControlCommand::GetNoneParam(napi_env env, napi_value in, AVControlCommand& out)
115 {
116 SLOGD("no param need to get");
117 (void)(env);
118 (void)(in);
119 (void)(out);
120 return napi_ok;
121 }
122
SetNoneParam(napi_env env,AVControlCommand & in,napi_value & out)123 napi_status NapiControlCommand::SetNoneParam(napi_env env, AVControlCommand& in, napi_value& out)
124 {
125 SLOGD("no param need to set");
126 (void)(env);
127 (void)(in);
128 (void)(out);
129 return napi_ok;
130 }
131
GetSpeed(napi_env env,napi_value in,AVControlCommand & out)132 napi_status NapiControlCommand::GetSpeed(napi_env env, napi_value in, AVControlCommand& out)
133 {
134 double speed {};
135 auto status = NapiUtils::GetNamedProperty(env, in, "parameter", speed);
136 if (status != napi_ok) {
137 SLOGE("get parameter failed");
138 return status;
139 }
140
141 CHECK_AND_RETURN_RET_LOG(out.SetSpeed(speed) == AVSESSION_SUCCESS, napi_invalid_arg, "set parameter failed");
142 return status;
143 }
144
SetSpeed(napi_env env,AVControlCommand & in,napi_value & out)145 napi_status NapiControlCommand::SetSpeed(napi_env env, AVControlCommand& in, napi_value& out)
146 {
147 double speed {};
148 CHECK_AND_RETURN_RET_LOG(in.GetSpeed(speed) == AVSESSION_SUCCESS, napi_invalid_arg, "get parameter failed");
149
150 napi_value property {};
151 auto status = NapiUtils::SetValue(env, speed, property);
152 if (status != napi_ok) {
153 SLOGE("create speed property failed");
154 return status;
155 }
156
157 status = napi_set_named_property(env, out, "parameter", property);
158 CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "set parameter property failed");
159 return status;
160 }
161
GetSeekTime(napi_env env,napi_value in,AVControlCommand & out)162 napi_status NapiControlCommand::GetSeekTime(napi_env env, napi_value in, AVControlCommand& out)
163 {
164 int64_t time {};
165 auto status = NapiUtils::GetNamedProperty(env, in, "parameter", time);
166 if (status != napi_ok) {
167 SLOGE("get parameter failed");
168 return status;
169 }
170
171 CHECK_AND_RETURN_RET_LOG(out.SetSeekTime(time) == AVSESSION_SUCCESS, napi_invalid_arg, "set parameter failed");
172 return status;
173 }
174
SetSeekTime(napi_env env,AVControlCommand & in,napi_value & out)175 napi_status NapiControlCommand::SetSeekTime(napi_env env, AVControlCommand& in, napi_value& out)
176 {
177 int64_t time {};
178 CHECK_AND_RETURN_RET_LOG(in.GetSeekTime(time) == AVSESSION_SUCCESS, napi_invalid_arg, "get parameter failed");
179
180 napi_value property {};
181 auto status = NapiUtils::SetValue(env, time, property);
182 if (status != napi_ok) {
183 SLOGE("create speed property failed");
184 return status;
185 }
186
187 status = napi_set_named_property(env, out, "parameter", property);
188 CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "set parameter property failed");
189 return status;
190 }
191
GetLoopMode(napi_env env,napi_value in,AVControlCommand & out)192 napi_status NapiControlCommand::GetLoopMode(napi_env env, napi_value in, AVControlCommand& out)
193 {
194 int32_t loopMode {};
195 auto status = NapiUtils::GetNamedProperty(env, in, "parameter", loopMode);
196 if (status != napi_ok) {
197 SLOGE("get parameter failed");
198 return status;
199 }
200
201 CHECK_AND_RETURN_RET_LOG(out.SetLoopMode(loopMode) == AVSESSION_SUCCESS, napi_invalid_arg, "set parameter failed");
202 return status;
203 }
204
SetLoopMode(napi_env env,AVControlCommand & in,napi_value & out)205 napi_status NapiControlCommand::SetLoopMode(napi_env env, AVControlCommand& in, napi_value& out)
206 {
207 int32_t loopMode {};
208 CHECK_AND_RETURN_RET_LOG(in.GetLoopMode(loopMode) == AVSESSION_SUCCESS, napi_invalid_arg, "get parameter failed");
209
210 napi_value property {};
211 auto status = NapiUtils::SetValue(env, loopMode, property);
212 if (status != napi_ok) {
213 SLOGE("create speed property failed");
214 return status;
215 }
216
217 status = napi_set_named_property(env, out, "parameter", property);
218 CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "set parameter property failed");
219 return status;
220 }
GetAssetId(napi_env env,napi_value in,AVControlCommand & out)221 napi_status NapiControlCommand::GetAssetId(napi_env env, napi_value in, AVControlCommand& out)
222 {
223 std::string assetId;
224 auto status = NapiUtils::GetNamedProperty(env, in, "parameter", assetId);
225 if (status != napi_ok) {
226 SLOGE("get parameter failed");
227 return status;
228 }
229
230 CHECK_AND_RETURN_RET_LOG(out.SetAssetId(assetId) == AVSESSION_SUCCESS, napi_invalid_arg, "set parameter failed");
231 return status;
232 }
233
SetAssetId(napi_env env,AVControlCommand & in,napi_value & out)234 napi_status NapiControlCommand::SetAssetId(napi_env env, AVControlCommand& in, napi_value& out)
235 {
236 std::string assetId;
237 CHECK_AND_RETURN_RET_LOG(in.GetAssetId(assetId) == AVSESSION_SUCCESS, napi_invalid_arg, "get parameter failed");
238
239 napi_value property {};
240 auto status = NapiUtils::SetValue(env, assetId, property);
241 if (status != napi_ok) {
242 SLOGE("create speed property failed");
243 return status;
244 }
245
246 status = napi_set_named_property(env, out, "parameter", property);
247 CHECK_AND_RETURN_RET_LOG(status == napi_ok, status, "set parameter property failed");
248 return status;
249 }
250 }