1 /* 2 * Copyright (c) 2025 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_STREAM_VOLUME_CHANGE_CALLBACK_H 16 #define NAPI_AUDIO_STREAM_VOLUME_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_policy_interface.h" 23 #include "audio_system_manager.h" 24 25 namespace OHOS { 26 namespace AudioStandard { 27 28 const std::string AUDIO_STREAM_VOLUME_CHANGE_CALLBACK_NAME = "streamVolumeChange"; 29 30 class NapiAudioStreamVolumeChangeCallback : public StreamVolumeChangeCallback { 31 public: 32 explicit NapiAudioStreamVolumeChangeCallback(napi_env env); 33 virtual ~NapiAudioStreamVolumeChangeCallback(); 34 void OnStreamVolumeChange(StreamVolumeEvent volumeEvent) override; 35 void SaveCallbackReference(const std::string &callbackName, napi_value args); 36 bool ContainSameJsCallback(napi_value args); 37 void CreateStreamVolumeChangeTsfn(napi_env env); 38 bool GetVolumeTsfnFlag(); 39 napi_threadsafe_function GetTsfn(); 40 41 private: 42 struct AudioStreamVolumeChangeJsCallback { 43 std::shared_ptr<AutoRef> callback = nullptr; 44 std::string callbackName = "unknown"; 45 StreamVolumeEvent volumeEvent; 46 }; 47 48 void OnJsCallbackStreamVolumeChange(std::unique_ptr<AudioStreamVolumeChangeJsCallback> &jsCb); 49 static void CleanUp(void *data); 50 static void SafeJsCallbackStreamVolumeChangeWork(napi_env env, napi_value js_cb, void *context, void *data); 51 static void StreamVolumeChangeTsfnFinalize(napi_env env, void *data, void *hint); 52 53 std::shared_ptr<AutoRef> audioStreamVolumeChangeCallback_ = nullptr; 54 std::mutex mutex_; 55 napi_env env_; 56 napi_ref callback_ = nullptr; 57 static napi_ref sConstructor_; 58 bool regVolumeTsfn_ = false; 59 napi_threadsafe_function amVolEntTsfn_ = nullptr; 60 }; 61 } // namespace AudioStandard 62 } // namespace OHOS 63 #endif /* NAPI_AUDIO_INTERRUPT_MANAGER_H */