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 16 #ifndef AUDIO_RENDERER_DEVICE_CHANGE_CALLBACK_NAPI_H_ 17 #define AUDIO_RENDERER_DEVICE_CHANGE_CALLBACK_NAPI_H_ 18 19 #include <uv.h> 20 #include <list> 21 #include "audio_common_napi.h" 22 #include "audio_renderer.h" 23 #include "napi/native_api.h" 24 #include "napi/native_node_api.h" 25 26 27 namespace OHOS { 28 namespace AudioStandard { 29 class AudioRendererDeviceChangeCallbackNapi : public AudioRendererDeviceChangeCallback { 30 public: 31 explicit AudioRendererDeviceChangeCallbackNapi(napi_env env); 32 virtual ~AudioRendererDeviceChangeCallbackNapi(); 33 void AddCallbackReference(napi_value callback); 34 void RemoveCallbackReference(napi_env env, napi_value args); 35 void OnStateChange(const DeviceInfo &deviceInfo) override; 36 void RemoveAllCallbacks() override; 37 int32_t GetCallbackListSize() const; 38 39 private: 40 struct AudioRendererDeviceChangeJsCallback { 41 napi_ref callback_; 42 napi_env env_; 43 DeviceInfo deviceInfo_; 44 }; 45 46 void OnJsCallbackRendererDeviceInfo(napi_ref method, const DeviceInfo &deviceInfo); 47 static void WorkCallbackCompleted(uv_work_t* work, int aStatus); 48 49 std::mutex mutex_; 50 napi_env env_ = nullptr; 51 std::list<napi_ref> callbacks_ {}; 52 }; 53 } // namespace AudioStandard 54 } // namespace OHOS 55 #endif // AUDIO_RENDERER_DEVICE_CHANGE_CALLBACK_NAPI_H_ 56