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_ROUTING_MANAGER_CALLBACK_H 16 #define NAPI_AUDIO_ROUTING_MANAGER_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_system_manager.h" 23 24 namespace OHOS { 25 namespace AudioStandard { 26 27 const std::string PREFERRED_OUTPUT_DEVICE_CALLBACK_NAME = "preferredOutputDeviceChangeForRendererInfo"; 28 const std::string PREFER_OUTPUT_DEVICE_CALLBACK_NAME = "preferOutputDeviceChangeForRendererInfo"; 29 const std::string PREFERRED_INPUT_DEVICE_CALLBACK_NAME = "preferredInputDeviceChangeForCapturerInfo"; 30 31 class NapiAudioPreferredOutputDeviceChangeCallback : public AudioPreferredOutputDeviceChangeCallback { 32 public: 33 explicit NapiAudioPreferredOutputDeviceChangeCallback(napi_env env); 34 virtual ~NapiAudioPreferredOutputDeviceChangeCallback(); 35 void SaveCallbackReference(napi_value callback); 36 void OnPreferredOutputDeviceUpdated(const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &desc) override; 37 void CreatePreferredOutTsfn(napi_env env); 38 bool GetPreferredOutTsfnFlag(); 39 bool ContainSameJsCallback(napi_value args); 40 41 private: 42 struct AudioActiveOutputDeviceChangeJsCallback { 43 std::shared_ptr<AutoRef> callback = nullptr; 44 std::string callbackName = "unknown"; 45 std::vector<std::shared_ptr<AudioDeviceDescriptor>> desc; 46 }; 47 48 void OnJsCallbackActiveOutputDeviceChange(std::unique_ptr<AudioActiveOutputDeviceChangeJsCallback> &jsCb); 49 static void SafeJsCallbackActiveOutputDeviceChangeWork(napi_env env, napi_value js_cb, void *context, void *data); 50 static void ActiveOutputDeviceChangeTsfnFinalize(napi_env env, void *data, void *hint); 51 52 napi_env env_ = nullptr; 53 std::shared_ptr<AutoRef> callback_ = nullptr; 54 bool regAmOutputDevChgTsfn_ = false; 55 napi_threadsafe_function amOutputDevChgTsfn_ = nullptr; 56 }; 57 58 class NapiAudioPreferredInputDeviceChangeCallback : public AudioPreferredInputDeviceChangeCallback { 59 public: 60 explicit NapiAudioPreferredInputDeviceChangeCallback(napi_env env); 61 virtual ~NapiAudioPreferredInputDeviceChangeCallback(); 62 void SaveCallbackReference(napi_value callback); 63 void OnPreferredInputDeviceUpdated(const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &desc) override; 64 void CreatePreferredInTsfn(napi_env env); 65 bool GetPreferredInTsfnFlag(); 66 bool ContainSameJsCallback(napi_value args); 67 68 private: 69 struct AudioActiveInputDeviceChangeJsCallback { 70 std::shared_ptr<AutoRef> callback = nullptr; 71 std::string callbackName = "unknown"; 72 std::vector<std::shared_ptr<AudioDeviceDescriptor>> desc; 73 }; 74 75 void OnJsCallbackActiveInputDeviceChange(std::unique_ptr<AudioActiveInputDeviceChangeJsCallback> &jsCb); 76 static void SafeJsCallbackActiveInputDeviceChangeWork(napi_env env, napi_value js_cb, void *context, void *data); 77 static void ActiveInputDeviceChangeTsfnFinalize(napi_env env, void *data, void *hint); 78 79 napi_env env_ = nullptr; 80 std::shared_ptr<AutoRef> callback_ = nullptr; 81 bool regAmInputDevChgTsfn_ = false; 82 napi_threadsafe_function amInputDevChgTsfn_ = nullptr; 83 }; 84 } // namespace AudioStandard 85 } // namespace OHOS 86 #endif /* NAPI_AUDIO_ROUTING_MANAGER_CALLBACK_H */