1 /*
2  * Copyright (c) 2023 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 SYSTEM_TONE_NAPI_H
17 #define SYSTEM_TONE_NAPI_H
18 
19 #include "napi/native_api.h"
20 #include "napi/native_node_api.h"
21 
22 #include "media_errors.h"
23 #include "system_sound_log.h"
24 #include "system_tone_options_napi.h"
25 #include "system_tone_player.h"
26 
27 namespace OHOS {
28 namespace Media {
29 static const std::string SYSTEM_TONE_PLAYER_NAPI_CLASS_NAME = "SystemTonePlayer";
30 
31 class SystemTonePlayerNapi {
32 public:
33     static napi_value Init(napi_env env, napi_value exports);
34     static napi_value GetSystemTonePlayerInstance(napi_env env, std::shared_ptr<SystemTonePlayer> &systemTonePlayer);
35 
36     SystemTonePlayerNapi();
37     ~SystemTonePlayerNapi();
38 
39 private:
40     static void SystemTonePlayerNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint);
41     static napi_value SystemTonePlayerNapiConstructor(napi_env env, napi_callback_info info);
42 
43     static napi_value GetTitle(napi_env env, napi_callback_info info);
44     static napi_value Prepare(napi_env env, napi_callback_info info);
45     static napi_value Start(napi_env env, napi_callback_info info);
46     static napi_value Stop(napi_env env, napi_callback_info info);
47     static napi_value Release(napi_env env, napi_callback_info info);
48     static napi_value SetAudioVolumeScale(napi_env env, napi_callback_info info);
49     static napi_value GetAudioVolumeScale(napi_env env, napi_callback_info info);
50     static napi_value GetSupportedHapticsFeatures(napi_env env, napi_callback_info info);
51     static napi_value SetHapticsFeature(napi_env env, napi_callback_info info);
52     static napi_value GetHapticsFeature(napi_env env, napi_callback_info info);
53     static void AsyncStart(napi_env env, void *data);
54     static void AsyncStop(napi_env env, void *data);
55     static void AsyncRelease(napi_env env, void *data);
56 
57     static void CommonAsyncCallbackComplete(napi_env env, napi_status status, void* data);
58     static void GetTitleAsyncCallbackComplete(napi_env env, napi_status status, void *data);
59     static void StartAsyncCallbackComplete(napi_env env, napi_status status, void *data);
60 
61     napi_env env_;
62     std::shared_ptr<SystemTonePlayer> systemTonePlayer_;
63 
64     static thread_local napi_ref sConstructor_;
65     static std::shared_ptr<SystemTonePlayer> sSystemTonePlayer_;
66 };
67 
68 struct SystemTonePlayerAsyncContext {
69     napi_env env;
70     napi_async_work work;
71     napi_deferred deferred;
72     napi_ref callbackRef;
73     bool status;
74     SystemTonePlayerNapi *objectInfo;
75     std::string title;
76     int32_t streamID;
77     SystemToneOptions systemToneOptions {false, false};
78     float volume;
79     std::vector<ToneHapticsFeature> toneHapticsFeatures;
80     ToneHapticsFeature toneHapticsFeature;
81 };
82 } // namespace Media
83 } // namespace OHOS
84 #endif // SYSTEM_TONE_NAPI_H