• 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:
30     explicit NapiAudioRendererDeviceChangeCallback(napi_env env);
31     ~NapiAudioRendererDeviceChangeCallback();
32     void OnOutputDeviceChange(const AudioDeviceDescriptor &deviceInfo,
33         const AudioStreamDeviceChangeReason reason) override;
34     void RemoveAllCallbacks();
35     int32_t GetCallbackListSize() const;
36     void CreateRendererDeviceChangeTsfn(napi_env env);
37     bool GetRendererDeviceChangeTsfnFlag();
38     void AddCallbackReference(napi_value args);
39     void RemoveCallbackReference(napi_env env, napi_value args = nullptr);
40 private:
41     struct AudioRendererDeviceChangeJsCallback {
42         napi_ref callback_;
43         napi_env env_;
44         AudioDeviceDescriptor deviceInfo_ = AudioDeviceDescriptor(AudioDeviceDescriptor::DEVICE_INFO);
45     };
46 
47     void OnJsCallbackRendererDeviceInfo(napi_ref method, const AudioDeviceDescriptor &deviceInfo);
48     static void RendererDeviceInfoTsfnFinalize(napi_env env, void *data, void *hint);
49     static void SafeJsCallbackRendererDeviceInfoWork(napi_env env, napi_value js_cb, void *context, void *data);
50 
51     std::mutex mutex_;
52     napi_env env_ = nullptr;
53     std::list<std::shared_ptr<AutoRef>> callbacks_ {};
54     bool regArDevInfoTsfn_ = false;
55     napi_threadsafe_function arDevInfoTsfn_ = nullptr;
56 };
57 
58 class NapiAudioRendererOutputDeviceChangeWithInfoCallback : public AudioRendererOutputDeviceChangeCallback {
59 public:
60     explicit NapiAudioRendererOutputDeviceChangeWithInfoCallback(napi_env env);
61     ~NapiAudioRendererOutputDeviceChangeWithInfoCallback();
62     void OnOutputDeviceChange(const AudioDeviceDescriptor &deviceInfo,
63         const AudioStreamDeviceChangeReason reason) override;
64     void RemoveAllCallbacks();
65     int32_t GetCallbackListSize() const;
66     void CreateOutputDeviceChangeTsfn(napi_env env);
67     bool GetOutputDeviceChangeTsfnFlag();
68     void AddCallbackReference(napi_value args);
69     void RemoveCallbackReference(napi_env env, napi_value args = nullptr);
70 
71 private:
72     struct AudioRendererOutputDeviceChangeWithInfoJsCallback {
73         napi_ref callback_;
74         napi_env env_;
75         AudioDeviceDescriptor deviceInfo_ = AudioDeviceDescriptor(AudioDeviceDescriptor::DEVICE_INFO);
76         AudioStreamDeviceChangeReason reason_;
77     };
78 
79     void OnJsCallbackOutputDeviceInfo(napi_ref method, const AudioDeviceDescriptor &deviceInfo,
80         const AudioStreamDeviceChangeReason reason);
81     static void SafeJsCallbackOutputDeviceInfoWork(napi_env env, napi_value js_cb, void *context, void *data);
82     static void OutputDeviceInfoTsfnFinalize(napi_env env, void *data, void *hint);
83 
84     std::mutex mutex_;
85     napi_env env_ = nullptr;
86     std::list<std::shared_ptr<AutoRef>> callbacks_ {};
87     bool regArOutputDevChg_ = false;
88     napi_threadsafe_function arOutputDevChgTsfn_ = nullptr;
89 };
90 }  // namespace AudioStandard
91 }  // namespace OHOS
92 #endif // NAPI_AUDIO_RENDERER_DEVICE_CHANGE_CALLBACK_H
93