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 #ifndef NAPI_CONTROL_COMMAND_H
17 #define NAPI_CONTROL_COMMAND_H
18 
19 #include <map>
20 #include <functional>
21 #include "napi/native_api.h"
22 #include "avcontrol_command.h"
23 
24 namespace OHOS::AVSession {
25 class NapiControlCommand {
26 public:
27     using GetterType = std::function<napi_status(napi_env, napi_value, AVControlCommand&)>;
28     using SetterType = std::function<napi_status(napi_env, AVControlCommand&, napi_value&)>;
29 
30     static std::string ConvertCommand(int32_t cmd);
31     static int32_t ConvertCommand(const std::string& cmd);
32     static std::vector<std::string> ConvertCommands(const std::vector<int32_t>& cmds);
33 
34     static napi_status GetValue(napi_env env, napi_value in, AVControlCommand& out);
35     static napi_status SetValue(napi_env env, AVControlCommand& in, napi_value& out);
36 
37     static napi_status GetNoneParam(napi_env env, napi_value in, AVControlCommand& out);
38     static napi_status GetSpeed(napi_env env, napi_value in, AVControlCommand& out);
39     static napi_status GetForwardTime(napi_env env, napi_value in, AVControlCommand& out);
40     static napi_status GetRewindTime(napi_env env, napi_value in, AVControlCommand& out);
41     static napi_status GetSeekTime(napi_env env, napi_value in, AVControlCommand& out);
42     static napi_status GetLoopMode(napi_env env, napi_value in, AVControlCommand& out);
43     static napi_status GetAssetId(napi_env env, napi_value in, AVControlCommand& out);
44     static napi_status GetPlayFromAssetId(napi_env env, napi_value in, AVControlCommand& out);
45 
46     static napi_status SetNoneParam(napi_env env, AVControlCommand& in, napi_value& out);
47     static napi_status SetSpeed(napi_env env, AVControlCommand& in, napi_value& out);
48     static napi_status SetForwardTime(napi_env env, AVControlCommand& in, napi_value& out);
49     static napi_status SetRewindTime(napi_env env, AVControlCommand& in, napi_value& out);
50     static napi_status SetSeekTime(napi_env env, AVControlCommand& in, napi_value& out);
51     static napi_status SetLoopMode(napi_env env, AVControlCommand& in, napi_value& out);
52     static napi_status SetAssetId(napi_env env, AVControlCommand& in, napi_value& out);
53     static napi_status SetPlayFromAssetId(napi_env env, AVControlCommand& in, napi_value& out);
54 
55 private:
56     static std::map<std::string, std::tuple<GetterType, SetterType, int32_t>> commandMap_;
57     static constexpr int GETTER_INDEX = 0;
58     static constexpr int SETTER_INDEX = 1;
59     static constexpr int ENUM_INDEX = 2;
60 };
61 }
62 #endif // NAPI_CONTROL_COMMAND_H