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_MANAGER_CALLBACK_H 16 #define NAPI_AUDIO_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 const std::string DEVICE_CHANGE_CALLBACK_NAME = "deviceChange"; 27 const std::string MIC_STATE_CHANGE_CALLBACK_NAME = "micStateChange"; 28 const std::string MICROPHONE_BLOCKED_CALLBACK_NAME = "micBlockStatusChanged"; 29 30 class NapiAudioManagerCallback : public AudioManagerDeviceChangeCallback, 31 public AudioManagerMicrophoneBlockedCallback { 32 public: 33 static bool IsSameCallback(napi_env env, napi_value callback, napi_ref refCallback); 34 35 explicit NapiAudioManagerCallback(napi_env env); 36 virtual ~NapiAudioManagerCallback(); 37 void SaveCallbackReference(const std::string &callbackName, napi_value args); 38 void OnDeviceChange(const DeviceChangeAction &deviceChangeAction) override; 39 void OnMicrophoneBlocked(const MicrophoneBlockedInfo µphoneBlockedInfo) override; 40 void SaveMicrophoneBlockedCallbackReference(napi_value callback); 41 void RemoveMicrophoneBlockedCallbackReference(napi_env env, napi_value callback); 42 void RemoveAllMicrophoneBlockedCallback(); 43 int32_t GetMicrophoneBlockedCbListSize(); 44 int32_t GetAudioManagerDeviceChangeCbListSize(); 45 void SaveAudioManagerDeviceChangeCbRef(DeviceFlag deviceFlag, napi_value callback); 46 void RemoveAudioManagerDeviceChangeCbRef(napi_env env, napi_value callback); 47 void RemoveAllAudioManagerDeviceChangeCb(); 48 49 void SaveRoutingManagerDeviceChangeCbRef(DeviceFlag deviceFlag, napi_value callback); 50 void RemoveRoutingManagerDeviceChangeCbRef(napi_env env, napi_value callback); 51 void RemoveAllRoutingManagerDeviceChangeCb(); 52 int32_t GetRoutingManagerDeviceChangeCbListSize(); 53 54 private: 55 struct AudioManagerJsCallback { 56 std::shared_ptr<AutoRef> callback = nullptr; 57 std::string callbackName = "unknown"; 58 DeviceChangeAction deviceChangeAction; 59 MicrophoneBlockedInfo microphoneBlockedInfo; 60 }; 61 62 static void WorkCallbackInterruptDone(uv_work_t *work, int status); 63 void OnJsCallbackDeviceChange(std::unique_ptr<AudioManagerJsCallback> &jsCb); 64 void OnJsCallbackMicrophoneBlocked(std::unique_ptr<AudioManagerJsCallback> &jsCb); 65 66 std::mutex mutex_; 67 napi_env env_ = nullptr; 68 std::shared_ptr<AutoRef> deviceChangeCallback_ = nullptr; 69 std::shared_ptr<AutoRef> onMicroPhoneBlockedCallback_ = nullptr; 70 std::list<std::pair<std::shared_ptr<AutoRef>, DeviceFlag>> audioManagerDeviceChangeCbList_; 71 std::list<std::pair<std::shared_ptr<AutoRef>, DeviceFlag>> routingManagerDeviceChangeCbList_; 72 std::list<std::shared_ptr<AutoRef>> microphoneBlockedCbList_; 73 }; 74 } // namespace AudioStandard 75 } // namespace OHOS 76 #endif /* NAPI_AUDIO_MANAGER_CALLBACK_H */