• 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 #ifndef NAPI_AUDIO_RENDERER_DEVICE_CHANGE_CALLBACK_H
16 #define NAPI_AUDIO_RENDERER_DEVICE_CHANGE_CALLBACK_H
17 
18 #include <uv.h>
19 #include "napi/native_api.h"
20 #include "napi/native_node_api.h"
21 #include "napi_async_work.h"
22 #include "audio_renderer.h"
23 #include "napi_audio_renderer_callback_inner.h"
24 #include "napi_audio_renderer_callback.h"
25 
26 namespace OHOS {
27 namespace AudioStandard {
28 class NapiAudioRendererDeviceChangeCallback : public AudioRendererOutputDeviceChangeCallback,
29     public NapiAudioRendererCallbackInner {
30 public:
31     explicit NapiAudioRendererDeviceChangeCallback(napi_env env);
32     ~NapiAudioRendererDeviceChangeCallback() override;
33     void AddCallbackReference(napi_value args);
34     void RemoveCallbackReference(napi_env env, napi_value args);
35     void OnOutputDeviceChange(const AudioDeviceDescriptor &deviceInfo,
36         const AudioStreamDeviceChangeReason reason) override;
37     void RemoveAllCallbacks();
38     void OnStateChange(const AudioDeviceDescriptor &deviceInfo);
39     int32_t GetCallbackListSize() const;
40     void CreateRendererDeviceChangeTsfn(napi_env env);
41     bool GetRendererDeviceChangeTsfnFlag();
42     void SaveCallbackReference(const std::string &callbackName, napi_value args) override;
43     void RemoveCallbackReference(const std::string &callbackName, napi_env env,
44         napi_value callback, napi_value args = nullptr) override;
45     bool CheckIfTargetCallbackName(const std::string &callbackName) override;
46 protected:
47     std::shared_ptr<AutoRef> GetCallback(const std::string &callbackName) override;
48     napi_env &GetEnv() override;
49 private:
50     struct AudioRendererDeviceChangeJsCallback {
51         napi_ref callback_;
52         napi_env env_;
53         AudioDeviceDescriptor deviceInfo_ = AudioDeviceDescriptor(AudioDeviceDescriptor::DEVICE_INFO);
54     };
55 
56     void OnJsCallbackRendererDeviceInfo(napi_ref method, const AudioDeviceDescriptor &deviceInfo);
57     static void RendererDeviceInfoTsfnFinalize(napi_env env, void *data, void *hint);
58     static void SafeJsCallbackRendererDeviceInfoWork(napi_env env, napi_value js_cb, void *context, void *data);
59 
60     std::mutex mutex_;
61     napi_env env_ = nullptr;
62     std::list<std::shared_ptr<AutoRef>> callbacks_ {};
63     bool regArDevInfoTsfn_ = false;
64     napi_threadsafe_function arDevInfoTsfn_ = nullptr;
65 };
66 
67 class NapiAudioRendererOutputDeviceChangeWithInfoCallback : public AudioRendererOutputDeviceChangeCallback,
68     public NapiAudioRendererCallbackInner {
69 public:
70     explicit NapiAudioRendererOutputDeviceChangeWithInfoCallback(napi_env env);
71     ~NapiAudioRendererOutputDeviceChangeWithInfoCallback() override;
72     void AddCallbackReference(napi_value args);
73     void RemoveCallbackReference(napi_env env, napi_value args);
74     void OnOutputDeviceChange(const AudioDeviceDescriptor &deviceInfo,
75         const AudioStreamDeviceChangeReason reason) override;
76     void RemoveAllCallbacks();
77     int32_t GetCallbackListSize() const;
78     void CreateOutputDeviceChangeTsfn(napi_env env);
79     bool GetOutputDeviceChangeTsfnFlag();
80     void SaveCallbackReference(const std::string &callbackName, napi_value args) override;
81     void RemoveCallbackReference(const std::string &callbackName, napi_env env, napi_value callback,
82         napi_value args = nullptr) override;
83     bool CheckIfTargetCallbackName(const std::string &callbackName) override;
84 
85 protected:
86     std::shared_ptr<AutoRef> GetCallback(const std::string &callbackName) override;
87     napi_env &GetEnv() override;
88 
89 private:
90     struct AudioRendererOutputDeviceChangeWithInfoJsCallback {
91         napi_ref callback_;
92         napi_env env_;
93         AudioDeviceDescriptor deviceInfo_ = AudioDeviceDescriptor(AudioDeviceDescriptor::DEVICE_INFO);
94         AudioStreamDeviceChangeReason reason_;
95     };
96 
97     void OnJsCallbackOutputDeviceInfo(napi_ref method, const AudioDeviceDescriptor &deviceInfo,
98         const AudioStreamDeviceChangeReason reason);
99     static void SafeJsCallbackOutputDeviceInfoWork(napi_env env, napi_value js_cb, void *context, void *data);
100     static void OutputDeviceInfoTsfnFinalize(napi_env env, void *data, void *hint);
101 
102     std::mutex mutex_;
103     napi_env env_ = nullptr;
104     std::list<std::shared_ptr<AutoRef>> callbacks_ {};
105     bool regArOutputDevChg_ = false;
106     napi_threadsafe_function arOutputDevChgTsfn_ = nullptr;
107 };
108 }  // namespace AudioStandard
109 }  // namespace OHOS
110 #endif // NAPI_AUDIO_RENDERER_DEVICE_CHANGE_CALLBACK_H
111