• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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_LOOPBACK_CALLBACK_H
16 #define NAPI_AUDIO_LOOPBACK_CALLBACK_H
17 
18 #include <uv.h>
19 
20 #include "napi/native_api.h"
21 #include "napi/native_node_api.h"
22 #include "napi_async_work.h"
23 #include "audio_loopback.h"
24 #include "napi_audio_loopback_callback_inner.h"
25 
26 namespace OHOS {
27 namespace AudioStandard {
28 const std::string STATUS_CHANGE_CALLBACK_NAME = "statusChange";
29 
30 class NapiAudioLoopbackCallback : public AudioLoopbackCallback,
31     public NapiAudioLoopbackCallbackInner {
32 public:
33     explicit NapiAudioLoopbackCallback(napi_env env);
34     ~NapiAudioLoopbackCallback() override;
35     void OnStatusChange(const AudioLoopbackStatus status,
36         const StateChangeCmdType __attribute__((unused)) cmdType) override;
37     void CreateArStatusChange(napi_env env);
38     bool GetArStatusChangeTsfnFlag();
39     void SaveCallbackReference(const std::string &callbackName, napi_value args) override;
40     void RemoveCallbackReference(const std::string &callbackName, napi_env env,
41         napi_value callback, napi_value args = nullptr) override;
42     bool CheckIfTargetCallbackName(const std::string &callbackName) override;
43 protected:
44     std::shared_ptr<AutoRef> GetCallback(const std::string &callbackName) override;
45     napi_env &GetEnv() override;
46 
47 private:
48     struct AudioLoopbackJsCallback {
49         std::shared_ptr<AutoRef> callback = nullptr;
50         std::string callbackName = "unknown";
51         AudioLoopbackStatus status;
52     };
53 
54     void OnJsCallbackStatusChange(std::unique_ptr<AudioLoopbackJsCallback> &jsCb);
55     static void SafeJsCallbackStatusChangeWork(napi_env env, napi_value js_cb, void *context, void *data);
56     static void StatusChangeTsfnFinalize(napi_env env, void *data, void *hint);
57 
58     std::mutex mutex_;
59     napi_env env_ = nullptr;
60     std::shared_ptr<AutoRef> statusChangeCallback_ = nullptr;
61     bool regArStatusChgTsfn_ = false;
62     napi_threadsafe_function arStatusChgTsfn_ = nullptr;
63 };
64 }  // namespace AudioStandard
65 }  // namespace OHOS
66 #endif // NAPI_AUDIO_LOOPBACK_CALLBACK_H
67