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_SCENE_CALLBACK_H 16 #define NAPI_AUDIO_SCENE_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 "napi_audio_manager.h" 23 #include "audio_system_manager.h" 24 25 namespace OHOS { 26 namespace AudioStandard { 27 const std::string AUDIO_SCENE_CHANGE_CALLBACK_NAME = "audioSceneChange"; 28 29 class NapiAudioSceneChangedCallback : public AudioManagerAudioSceneChangedCallback { 30 public: 31 static bool IsSameCallback(napi_env env, napi_value callback, napi_ref refCallback); 32 33 explicit NapiAudioSceneChangedCallback(napi_env env); 34 virtual ~NapiAudioSceneChangedCallback(); 35 void SaveCallbackReference(const std::string &callbackName, napi_value callback); 36 void RemoveCallbackReference(napi_env env, napi_value callback); 37 void RemoveAllCallbackReference(); 38 int32_t GetAudioSceneCbListSize(); 39 void OnAudioSceneChange(const AudioScene audioScene) override; 40 void CreateSceneChgTsfn(napi_env env); 41 bool GetSceneChgTsfnFlag() const; 42 43 private: 44 struct AudioSceneJsCallback { 45 std::shared_ptr<AutoRef> callback = nullptr; 46 std::string callbackName = "unknown"; 47 AudioScene audioScene = AudioScene::AUDIO_SCENE_INVALID; 48 }; 49 50 void OnJsCallbackAudioSceneChange(std::unique_ptr<AudioSceneJsCallback> &jsCb); 51 static void SafeJsCallbackAudioSceneChangeWork(napi_env env, napi_value js_cb, void *context, void *data); 52 static void AudioSceneChangeTsfnFinalize(napi_env env, void *data, void *hint); 53 54 std::mutex mutex_; 55 napi_env env_ = nullptr; 56 std::list<std::shared_ptr<AutoRef>> audioSceneChangeCbList_; 57 bool regAmSceneChgTsfn_ = false; 58 napi_threadsafe_function amSceneChgTsfn_ = nullptr; 59 }; 60 } // namespace AudioStandard 61 } // namespace OHOS 62 #endif /* NAPI_AUDIO_SCENE_CALLBACK_H */