• 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_CALLBACK_H
16 #define NAPI_AUDIO_CAPTURER_CALLBACK_H
17 
18 #include <uv.h>
19 #include "napi/native_api.h"
20 #include "napi/native_node_api.h"
21 #include "napi_audio_capturer_callback_inner.h"
22 #include "napi_async_work.h"
23 #include "audio_capturer.h"
24 
25 namespace OHOS {
26 namespace AudioStandard {
27 const std::string INTERRUPT_CALLBACK_NAME = "interrupt";
28 const std::string AUDIO_INTERRUPT_CALLBACK_NAME = "audioInterrupt";
29 const std::string STATE_CHANGE_CALLBACK_NAME = "stateChange";
30 const std::string MARK_REACH_CALLBACK_NAME = "markReach";
31 const std::string PERIOD_REACH_CALLBACK_NAME = "periodReach";
32 const std::string INPUTDEVICE_CHANGE_CALLBACK_NAME = "inputDeviceChange";
33 const std::string AUDIO_CAPTURER_CHANGE_CALLBACK_NAME = "audioCapturerChange";
34 const std::string READ_DATA_CALLBACK_NAME = "readData";
35 
36 class NapiAudioCapturerCallback : public AudioCapturerCallback, public NapiAudioCapturerCallbackInner {
37 public:
38     explicit NapiAudioCapturerCallback(napi_env env);
39     ~NapiAudioCapturerCallback() override;
40     void OnInterrupt(const InterruptEvent &interruptEvent) override;
41     void OnStateChange(const CapturerState state) override;
42     void CreateStateChangeTsfn(napi_env env);
43     void CreateInterruptTsfn(napi_env env);
44     bool GetStateChangeTsfnFlag();
45     bool GetInterruptTsfnFlag();
46     void SaveCallbackReference(const std::string &callbackName, napi_value args) override;
47     void RemoveCallbackReference(const std::string &callbackName, napi_env env, napi_value callback) override;
48     bool CheckIfTargetCallbackName(const std::string &callbackName) override;
49 protected:
50     std::shared_ptr<AutoRef> GetCallback(const std::string &callbackName) override;
51     napi_env &GetEnv() override;
52 
53 private:
54     struct AudioCapturerJsCallback {
55         std::shared_ptr<AutoRef> callback = nullptr;
56         std::string callbackName = "unknown";
57         InterruptEvent interruptEvent;
58         CapturerState state;
59     };
60 
61     void OnJsCallbackInterrupt(std::unique_ptr<AudioCapturerJsCallback> &jsCb);
62     void OnJsCallbackStateChange(std::unique_ptr<AudioCapturerJsCallback> &jsCb);
63     static void SafeJsCallbackInterruptWork(napi_env env, napi_value js_cb, void *context, void *data);
64     static void InterruptTsfnFinalize(napi_env env, void *data, void *hint);
65     static void SafeJsCallbackStateChangeWork(napi_env env, napi_value js_cb, void *context, void *data);
66     static void StateChangeTsfnFinalize(napi_env env, void *data, void *hint);
67 
68     std::mutex mutex_;
69     napi_env env_ = nullptr;
70     std::shared_ptr<AutoRef> interruptCallback_ = nullptr;
71     std::shared_ptr<AutoRef> stateChangeCallback_ = nullptr;
72     bool regAcStateChgTsfn_ = false;
73     bool regAcInterruptTsfn_ = false;
74     napi_threadsafe_function acStateChgTsfn_ = nullptr;
75     napi_threadsafe_function acInterruptTsfn_ = nullptr;
76 };
77 }  // namespace AudioStandard
78 }  // namespace OHOS
79 #endif /* NAPI_AUDIO_CAPTURER_CALLBACK_H */
80