1 /*
2  * Copyright (c) 2022 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 
16 #ifndef NAPI_AUDIO_RENDERER_STATE_CALLBACK_H
17 #define NAPI_AUDIO_RENDERER_STATE_CALLBACK_H
18 
19 #include <uv.h>
20 #include "napi/native_api.h"
21 #include "napi/native_node_api.h"
22 #include "napi_async_work.h"
23 #include "audio_system_manager.h"
24 #include "audio_stream_manager.h"
25 
26 namespace OHOS {
27 namespace AudioStandard {
28 class NapiAudioRendererStateCallback : public AudioRendererStateChangeCallback {
29 public:
30     explicit NapiAudioRendererStateCallback(napi_env env);
31     virtual ~NapiAudioRendererStateCallback();
32     void SaveCallbackReference(napi_value args);
33     void RemoveCallbackReference();
34     void OnRendererStateChange(
35         const std::vector<std::unique_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos) override;
36 
37 private:
38     struct AudioRendererStateJsCallback {
39         std::shared_ptr<AutoRef> callback = nullptr;
40         std::vector<std::unique_ptr<AudioRendererChangeInfo>> changeInfos;
41     };
42 
43     static void WorkCallbackInterruptDone(uv_work_t *work, int status);
44     void OnJsCallbackRendererState(std::unique_ptr<AudioRendererStateJsCallback> &jsCb);
45 
46     std::mutex mutex_;
47     napi_env env_ = nullptr;
48     std::shared_ptr<AutoRef> rendererStateCallback_ = nullptr;
49 };
50 }  // namespace AudioStandard
51 }  // namespace OHOS
52 #endif /* NAPI_AUDIO_RENDERER_STATE_CALLBACK_H */