• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 AUDIO_COMMON_NAPI_H_
17 #define AUDIO_COMMON_NAPI_H_
18 
19 #include "napi/native_api.h"
20 #include "napi/native_node_api.h"
21 #include "audio_info.h"
22 
23 
24 #define THROW_ERROR_ASSERT(env, assertion, code)        \
25     do {                                                \
26         if (!(assertion)) {                             \
27             AudioCommonNapi::throwError( env, code);    \
28             return nullptr;                             \
29         }                                               \
30     } while (0)
31 
32 #define GET_PARAMS(env, info, num) \
33     size_t argc = num;             \
34     napi_value argv[num] = {0};    \
35     napi_value thisVar = nullptr;  \
36     void *data;                    \
37     napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)
38 
39 namespace OHOS {
40 namespace AudioStandard {
41 namespace {
42     const std::string INTERRUPT_CALLBACK_NAME = "interrupt";
43     const std::string AUDIO_INTERRUPT_CALLBACK_NAME = "audioInterrupt";
44     const std::string INDEPENDENTINTERRUPT_CALLBACK_NAME = "independentInterrupt";
45     const std::string STATE_CHANGE_CALLBACK_NAME = "stateChange";
46 }
47 
48 class AudioCommonNapi {
49 public:
50     enum AudioVolumeType {
51         VOLUMETYPE_DEFAULT = -1,
52         VOICE_CALL = 0,
53         RINGTONE = 2,
54         MEDIA = 3,
55         VOICE_ASSISTANT = 9,
56         VOLUMETYPE_MAX,
57         ALL = 100
58     };
59     AudioCommonNapi() = delete;
60     ~AudioCommonNapi() = delete;
61     static std::string GetStringArgument(napi_env env, napi_value value);
62     static std::string getMessageByCode(int32_t &code);
63     static void throwError (napi_env env, int32_t code);
64     static bool IsLegalInputArgumentVolLevel(int32_t volLevel);
65     static bool IsLegalInputArgumentVolType(int32_t inputType);
66     static bool IsLegalInputArgumentDeviceFlag(int32_t inputType);
67     static bool IsLegalInputArgumentActiveDeviceType(int32_t deviceType);
68     static bool IsLegalInputArgumentCommunicationDeviceType(int32_t deviceType);
69     static bool IsLegalInputArgumentRingMode(int32_t ringerMode);
70     static AudioVolumeType GetNativeAudioVolumeType(int32_t volumeType);
71 private:
72     static constexpr int32_t MAX_VOLUME_LEVEL = 15;
73     static constexpr int32_t MIN_VOLUME_LEVEL = 0;
74 };
75 
76 struct AutoRef {
AutoRefAutoRef77     AutoRef(napi_env env, napi_ref cb)
78         : env_(env), cb_(cb)
79     {
80     }
~AutoRefAutoRef81     ~AutoRef()
82     {
83         if (env_ != nullptr && cb_ != nullptr) {
84             (void)napi_delete_reference(env_, cb_);
85         }
86     }
87     napi_env env_;
88     napi_ref cb_;
89 };
90 
91 const int32_t  NAPI_ERROR_INVALID_PARAM = 6800101;
92 const int32_t  NAPI_ERR_INPUT_INVALID = 401;
93 const int32_t  NAPI_ERR_INVALID_PARAM = 6800101;
94 const int32_t  NAPI_ERR_NO_MEMORY = 6800102;
95 const int32_t  NAPI_ERR_ILLEGAL_STATE = 6800103;
96 const int32_t  NAPI_ERR_UNSUPPORTED = 6800104;
97 const int32_t  NAPI_ERR_TIMEOUT = 6800105;
98 const int32_t  NAPI_ERR_STREAM_LIMIT = 6800201;
99 const int32_t  NAPI_ERR_SYSTEM = 6800301;
100 
101 const std::string NAPI_ERROR_INVALID_PARAM_INFO = "input parameter value error";
102 const std::string NAPI_ERR_INPUT_INVALID_INFO = "input parameter type or number mismatch";
103 const std::string NAPI_ERR_INVALID_PARAM_INFO = "invalid parameter";
104 const std::string NAPI_ERR_NO_MEMORY_INFO = "allocate memory failed";
105 const std::string NAPI_ERR_ILLEGAL_STATE_INFO = "Operation not permit at current state";
106 const std::string NAPI_ERR_UNSUPPORTED_INFO = "unsupported option";
107 const std::string NAPI_ERR_TIMEOUT_INFO = "time out";
108 const std::string NAPI_ERR_STREAM_LIMIT_INFO = "stream number limited";
109 const std::string NAPI_ERR_SYSTEM_INFO = "system error";
110 }  // namespace AudioStandard
111 }  // namespace OHOS
112 #endif // AUDIO_COMMON_NAPI_H_
113