• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 void AsyncStart(napi_env env, void *data);
49     static void AsyncStop(napi_env env, void *data);
50     static void AsyncRelease(napi_env env, void *data);
51 
52     static void CommonAsyncCallbackComplete(napi_env env, napi_status status, void* data);
53     static void GetTitleAsyncCallbackComplete(napi_env env, napi_status status, void *data);
54     static void StartAsyncCallbackComplete(napi_env env, napi_status status, void *data);
55 
56     napi_env env_;
57     std::shared_ptr<SystemTonePlayer> systemTonePlayer_;
58 
59     static thread_local napi_ref sConstructor_;
60     static std::shared_ptr<SystemTonePlayer> sSystemTonePlayer_;
61 };
62 
63 struct SystemTonePlayerAsyncContext {
64     napi_env env;
65     napi_async_work work;
66     napi_deferred deferred;
67     napi_ref callbackRef;
68     bool status;
69     SystemTonePlayerNapi *objectInfo;
70     std::string title;
71     int32_t streamID;
72     SystemToneOptions systemToneOptions {false, false};
73 };
74 } // namespace Media
75 } // namespace OHOS
76 #endif // SYSTEM_TONE_NAPI_H