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(AudioStreamType streamType, napi_value callback); 36 void OnPreferredOutputDeviceUpdated(const std::vector<sptr<AudioDeviceDescriptor>> &desc) override; 37 void RemoveCallbackReference(napi_env env, napi_value callback); 38 void RemoveAllCallbacks(); 39 40 private: 41 struct AudioActiveOutputDeviceChangeJsCallback { 42 std::shared_ptr<AutoRef> callback = nullptr; 43 std::string callbackName = "unknown"; 44 std::vector<sptr<AudioDeviceDescriptor>> desc; 45 }; 46 47 static void WorkCallbackInterruptDone(uv_work_t *work, int status); 48 void OnJsCallbackActiveOutputDeviceChange(std::unique_ptr<AudioActiveOutputDeviceChangeJsCallback> &jsCb); 49 50 std::mutex mutex_; 51 napi_env env_ = nullptr; 52 std::shared_ptr<AutoRef> preferredOutputDeviceCallback_ = nullptr; 53 std::list<std::pair<std::shared_ptr<AutoRef>, AudioStreamType>> preferredOutputDeviceCbList_; 54 }; 55 56 class NapiAudioPreferredInputDeviceChangeCallback : public AudioPreferredInputDeviceChangeCallback { 57 public: 58 explicit NapiAudioPreferredInputDeviceChangeCallback(napi_env env); 59 virtual ~NapiAudioPreferredInputDeviceChangeCallback(); 60 void SaveCallbackReference(SourceType sourceType, napi_value callback); 61 void OnPreferredInputDeviceUpdated(const std::vector<sptr<AudioDeviceDescriptor>> &desc) override; 62 void RemoveCallbackReference(napi_env env, napi_value callback); 63 void RemoveAllCallbacks(); 64 65 private: 66 struct AudioActiveInputDeviceChangeJsCallback { 67 std::shared_ptr<AutoRef> callback = nullptr; 68 std::string callbackName = "unknown"; 69 std::vector<sptr<AudioDeviceDescriptor>> desc; 70 }; 71 72 static void WorkCallbackInterruptDone(uv_work_t *work, int status); 73 void OnJsCallbackActiveInputDeviceChange(std::unique_ptr<AudioActiveInputDeviceChangeJsCallback> &jsCb); 74 75 std::mutex preferredInputListMutex_; 76 napi_env env_ = nullptr; 77 std::shared_ptr<AutoRef> preferredInputDeviceCallback_ = nullptr; 78 std::list<std::pair<std::shared_ptr<AutoRef>, SourceType>> preferredInputDeviceCbList_; 79 }; 80 } // namespace AudioStandard 81 } // namespace OHOS 82 #endif /* NAPI_AUDIO_ROUTING_MANAGER_CALLBACK_H */