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_ROUTING_MANAGER_CALLBACK_NAPI_H 17 #define AUDIO_ROUTING_MANAGER_CALLBACK_NAPI_H 18 19 #include "audio_common_napi.h" 20 #include "audio_routing_manager_napi.h" 21 #include "audio_routing_manager.h" 22 #include "napi/native_api.h" 23 #include "napi/native_node_api.h" 24 #include <algorithm> 25 26 namespace { 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 32 namespace OHOS { 33 namespace AudioStandard { 34 class AudioPreferredOutputDeviceChangeCallbackNapi : public AudioPreferredOutputDeviceChangeCallback { 35 public: 36 explicit AudioPreferredOutputDeviceChangeCallbackNapi(napi_env env); 37 virtual ~AudioPreferredOutputDeviceChangeCallbackNapi(); 38 void SaveCallbackReference(AudioStreamType streamType, napi_value callback); 39 void OnPreferredOutputDeviceUpdated(const std::vector<sptr<AudioDeviceDescriptor>> &desc) override; 40 void RemoveCallbackReference(napi_env env, napi_value args); 41 void RemoveAllCallbacks(); 42 43 private: 44 struct AudioActiveOutputDeviceChangeJsCallback { 45 std::shared_ptr<AutoRef> callback = nullptr; 46 std::string callbackName = "unknown"; 47 std::vector<sptr<AudioDeviceDescriptor>> desc; 48 }; 49 50 void OnJsCallbackActiveOutputDeviceChange(std::unique_ptr<AudioActiveOutputDeviceChangeJsCallback> &jsCb); 51 52 std::mutex mutex_; 53 napi_env env_ = nullptr; 54 std::shared_ptr<AutoRef> preferredOutputDeviceCallback_ = nullptr; 55 std::list<std::pair<std::shared_ptr<AutoRef>, AudioStreamType>> preferredOutputDeviceCbList_; 56 }; 57 58 class AudioPreferredInputDeviceChangeCallbackNapi : public AudioPreferredInputDeviceChangeCallback { 59 public: 60 explicit AudioPreferredInputDeviceChangeCallbackNapi(napi_env env); 61 virtual ~AudioPreferredInputDeviceChangeCallbackNapi(); 62 void SaveCallbackReference(SourceType sourceType, napi_value callback); 63 void OnPreferredInputDeviceUpdated(const std::vector<sptr<AudioDeviceDescriptor>> &desc) override; 64 void RemoveCallbackReference(napi_env env, napi_value args); 65 void RemoveAllCallbacks(); 66 67 private: 68 struct AudioActiveInputDeviceChangeJsCallback { 69 std::shared_ptr<AutoRef> callback = nullptr; 70 std::string callbackName = "unknown"; 71 std::vector<sptr<AudioDeviceDescriptor>> desc; 72 }; 73 74 void OnJsCallbackActiveInputDeviceChange(std::unique_ptr<AudioActiveInputDeviceChangeJsCallback> &jsCb); 75 76 std::mutex preferredInputListMutex_; 77 napi_env env_ = nullptr; 78 std::shared_ptr<AutoRef> preferredInputDeviceCallback_ = nullptr; 79 std::list<std::pair<std::shared_ptr<AutoRef>, SourceType>> preferredInputDeviceCbList_; 80 }; 81 } // namespace AudioStandard 82 } // namespace OHOS 83 #endif // AUDIO_ROUTING_MANAGER_CALLBACK_NAPI_H 84