• 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 #include "audio_errors.h"
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 STATE_CHANGE_CALLBACK_NAME = "stateChange";
45 }
46 
47 class AudioCommonNapi {
48 public:
49     enum AudioVolumeType {
50         VOLUMETYPE_DEFAULT = -1,
51         VOICE_CALL = 0,
52         RINGTONE = 2,
53         MEDIA = 3,
54         ALARM = 4,
55         ACCESSIBILITY = 5,
56         VOICE_ASSISTANT = 9,
57         ULTRASONIC = 10,
58         VOLUMETYPE_MAX,
59         ALL = 100
60     };
61     AudioCommonNapi() = delete;
62     ~AudioCommonNapi() = delete;
63     static std::string GetStringArgument(napi_env env, napi_value value);
64     static std::string getMessageByCode(int32_t &code);
65     static void throwError (napi_env env, int32_t code);
66     static bool IsLegalInputArgumentVolType(int32_t inputType);
67     static bool IsLegalInputArgumentDeviceFlag(int32_t inputType);
68     static bool IsLegalInputArgumentActiveDeviceType(int32_t deviceType);
69     static bool IsLegalInputArgumentCommunicationDeviceType(int32_t deviceType);
70     static bool IsLegalInputArgumentRingMode(int32_t ringerMode);
71     static bool IsLegalInputArgumentStreamUsage(int32_t streamUsage);
72     static bool IsLegalInputArgumentAudioEffectMode(int32_t audioEffectMode);
73     static AudioVolumeType GetNativeAudioVolumeType(int32_t volumeType);
74     static bool IsSameCallback(napi_env env, napi_value callback, napi_ref refCallback);
75     static bool IsLegalInputArgumentVolumeAdjustType(int32_t adjustType);
76     static bool IsLegalInputArgumentDeviceType(int32_t deviceType);
77     static bool IsLegalInputArgumentInterruptMode(int32_t interruptMode);
78 private:
79     static constexpr int32_t MAX_VOLUME_LEVEL = 15;
80     static constexpr int32_t MIN_VOLUME_LEVEL = 0;
81 };
82 
83 struct AutoRef {
AutoRefAutoRef84     AutoRef(napi_env env, napi_ref cb)
85         : env_(env), cb_(cb)
86     {
87     }
~AutoRefAutoRef88     ~AutoRef()
89     {
90         if (env_ != nullptr && cb_ != nullptr) {
91             (void)napi_delete_reference(env_, cb_);
92         }
93     }
94     napi_env env_;
95     napi_ref cb_;
96 };
97 
98 const int32_t  NAPI_ERROR_INVALID_PARAM = 6800101;
99 const int32_t  NAPI_ERR_NO_PERMISSION = 201;
100 const int32_t  NAPI_ERR_PERMISSION_DENIED = 202;
101 const int32_t  NAPI_ERR_INPUT_INVALID = 401;
102 const int32_t  NAPI_ERR_INVALID_PARAM = 6800101;
103 const int32_t  NAPI_ERR_NO_MEMORY = 6800102;
104 const int32_t  NAPI_ERR_ILLEGAL_STATE = 6800103;
105 const int32_t  NAPI_ERR_UNSUPPORTED = 6800104;
106 const int32_t  NAPI_ERR_TIMEOUT = 6800105;
107 const int32_t  NAPI_ERR_STREAM_LIMIT = 6800201;
108 const int32_t  NAPI_ERR_SYSTEM = 6800301;
109 
110 const std::string NAPI_ERROR_INVALID_PARAM_INFO = "input parameter value error";
111 const std::string NAPI_ERROR_PERMISSION_DENIED_INFO = "not system app";
112 const std::string NAPI_ERR_INPUT_INVALID_INFO = "input parameter type or number mismatch";
113 const std::string NAPI_ERR_INVALID_PARAM_INFO = "invalid parameter";
114 const std::string NAPI_ERR_NO_MEMORY_INFO = "allocate memory failed";
115 const std::string NAPI_ERR_ILLEGAL_STATE_INFO = "Operation not permit at current state";
116 const std::string NAPI_ERR_UNSUPPORTED_INFO = "unsupported option";
117 const std::string NAPI_ERR_TIMEOUT_INFO = "time out";
118 const std::string NAPI_ERR_STREAM_LIMIT_INFO = "stream number limited";
119 const std::string NAPI_ERR_SYSTEM_INFO = "system error";
120 }  // namespace AudioStandard
121 }  // namespace OHOS
122 #endif // AUDIO_COMMON_NAPI_H_
123