• 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_CAPTURER_DEVICE_CHANGE_CALLBACK_H
16 #define NAPI_AUDIO_CAPTURER_DEVICE_CHANGE_CALLBACK_H
17 
18 #include <uv.h>
19 #include <list>
20 #include "napi/native_api.h"
21 #include "napi/native_node_api.h"
22 #include "napi_async_work.h"
23 #include "audio_capturer.h"
24 #include "napi_audio_capturer_callback_inner.h"
25 #include "napi_audio_capturer_callbacks.h"
26 
27 namespace OHOS {
28 namespace AudioStandard {
29 class NapiAudioCapturerDeviceChangeCallback : public AudioCapturerDeviceChangeCallback,
30     public NapiAudioCapturerCallbackInner {
31 public:
32     explicit NapiAudioCapturerDeviceChangeCallback(napi_env env);
33     ~NapiAudioCapturerDeviceChangeCallback() override;
34     void SaveCallbackReference(const std::string &callbackName, napi_value args) override;
35     void RemoveCallbackReference(const std::string &callbackName, napi_env env, napi_value callback) override;
36     void OnStateChange(const AudioDeviceDescriptor &deviceInfo) override;
37     bool ContainSameJsCallback(napi_value args);
38     void CreateCaptureDeviceChangeTsfn(napi_env env);
39     bool CheckIfTargetCallbackName(const std::string &callbackName) override;
40 protected:
41     std::shared_ptr<AutoRef> GetCallback(const std::string &callbackName) override;
42     napi_env &GetEnv() override;
43 private:
44     struct AudioCapturerDeviceChangeJsCallback {
45         napi_ref callback_;
46         napi_env env_;
47         AudioDeviceDescriptor deviceInfo_ = AudioDeviceDescriptor(AudioDeviceDescriptor::DEVICE_INFO);
48     };
49 
50     void OnJsCallbackCapturerDeviceInfo(napi_ref method, const AudioDeviceDescriptor &deviceInfo);
51     static void CaptureDeviceInfoTsfnFinalize(napi_env env, void *data, void *hint);
52     static void SafeJsCallbackCapturerDeviceInfoWork(napi_env env, napi_value js_cb, void *context, void *data);
53 
54     std::mutex mutex_;
55     napi_env env_ = nullptr;
56     napi_ref callback_ = nullptr;
57     bool regAcDevChgTsfn_ = false;
58     napi_threadsafe_function acDevChgTsfn_ = nullptr;
59     std::shared_ptr<AutoRef> callbackPtr_ = nullptr;
60 };
61 }  // namespace AudioStandard
62 }  // namespace OHOS
63 #endif /* NAPI_AUDIO_CAPTURER_DEVICE_CHANGE_CALLBACK_H */