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_RENDERER_DEVICE_CHANGE_CALLBACK_H 16 #define NAPI_AUDIO_RENDERER_DEVICE_CHANGE_CALLBACK_H 17 18 #include <uv.h> 19 #include "napi/native_api.h" 20 #include "napi/native_node_api.h" 21 #include "napi_async_work.h" 22 #include "audio_renderer.h" 23 24 namespace OHOS { 25 namespace AudioStandard { 26 class NapiAudioRendererDeviceChangeCallback : public AudioRendererOutputDeviceChangeCallback { 27 public: 28 explicit NapiAudioRendererDeviceChangeCallback(napi_env env); 29 virtual ~NapiAudioRendererDeviceChangeCallback(); 30 void AddCallbackReference(napi_value args); 31 void RemoveCallbackReference(napi_env env, napi_value args); 32 void OnOutputDeviceChange(const DeviceInfo &deviceInfo, const AudioStreamDeviceChangeReason reason) override; 33 void RemoveAllCallbacks(); 34 int32_t GetCallbackListSize() const; 35 36 private: 37 struct AudioRendererDeviceChangeJsCallback { 38 napi_ref callback_; 39 napi_env env_; 40 DeviceInfo deviceInfo_; 41 }; 42 43 static void WorkCallbackCompleted(uv_work_t* work, int status); 44 void OnJsCallbackRendererDeviceInfo(napi_ref method, const DeviceInfo &deviceInfo); 45 46 std::mutex mutex_; 47 napi_env env_ = nullptr; 48 std::list<napi_ref> callbacks_ {}; 49 }; 50 51 class NapiAudioRendererOutputDeviceChangeWithInfoCallback : public AudioRendererOutputDeviceChangeCallback { 52 public: 53 explicit NapiAudioRendererOutputDeviceChangeWithInfoCallback(napi_env env); 54 virtual ~NapiAudioRendererOutputDeviceChangeWithInfoCallback(); 55 void AddCallbackReference(napi_value args); 56 void RemoveCallbackReference(napi_env env, napi_value args); 57 void OnOutputDeviceChange(const DeviceInfo &deviceInfo, const AudioStreamDeviceChangeReason reason) override; 58 void RemoveAllCallbacks(); 59 int32_t GetCallbackListSize() const; 60 61 private: 62 struct AudioRendererOutputDeviceChangeWithInfoJsCallback { 63 napi_ref callback_; 64 napi_env env_; 65 DeviceInfo deviceInfo_; 66 AudioStreamDeviceChangeReason reason_; 67 }; 68 69 static void WorkCallbackCompleted(uv_work_t* work, int status); 70 void OnJsCallbackOutputDeviceInfo(napi_ref method, const DeviceInfo &deviceInfo, 71 const AudioStreamDeviceChangeReason reason); 72 73 std::mutex mutex_; 74 napi_env env_ = nullptr; 75 std::list<napi_ref> callbacks_ {}; 76 }; 77 } // namespace AudioStandard 78 } // namespace OHOS 79 #endif // NAPI_AUDIO_RENDERER_DEVICE_CHANGE_CALLBACK_H 80