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_CAPTURER_INFO_CHANGE_CALLBACK_H 16 #define NAPI_AUDIO_CAPTURER_INFO_CHANGE_CALLBACK_H 17 18 #include <uv.h> 19 #include <list> 20 #include "napi/native_api.h" 21 #include "napi/native_node_api.h" 22 #include "napi_async_work.h" 23 #include "audio_capturer.h" 24 #include "napi_audio_capturer_callback_inner.h" 25 #include "napi_audio_capturer_callbacks.h" 26 27 namespace OHOS { 28 namespace AudioStandard { 29 class NapiAudioCapturerInfoChangeCallback : public AudioCapturerInfoChangeCallback, 30 public NapiAudioCapturerCallbackInner { 31 public: 32 explicit NapiAudioCapturerInfoChangeCallback(napi_env env); 33 ~NapiAudioCapturerInfoChangeCallback() override; 34 void SaveCallbackReference(const std::string &callbackName, napi_value args) override; 35 void RemoveCallbackReference(const std::string &callbackName, napi_env env, napi_value callback) override; 36 void OnStateChange(const AudioCapturerChangeInfo &capturerChangeInfo) override; 37 bool ContainSameJsCallback(napi_value args); 38 void CreateCaptureInfoChangeTsfn(napi_env env); 39 bool CheckIfTargetCallbackName(const std::string &callbackName) override; 40 41 protected: 42 std::shared_ptr<AutoRef> GetCallback(const std::string &callbackName) override; 43 napi_env &GetEnv() override; 44 private: 45 struct AudioCapturerChangeInfoJsCallback { 46 napi_ref callback_; 47 napi_env env_; 48 AudioCapturerChangeInfo capturerChangeInfo_; 49 std::string callbackName = "unknown"; 50 }; 51 52 void OnJsCallbackCapturerChangeInfo(napi_ref method, const AudioCapturerChangeInfo &capturerChangeInfo); 53 static void SafeJsCallbackCapturerChangeInfoWork(napi_env env, napi_value js_cb, void *context, void *data); 54 static void CapturerChangeInfoTsfnFinalize(napi_env env, void *data, void *hint); 55 56 std::mutex mutex_; 57 napi_env env_ = nullptr; 58 napi_ref callback_ = nullptr; 59 bool regAcInfoChgTsfn_ = false; 60 napi_threadsafe_function acInfoChgTsfn_ = nullptr; 61 std::shared_ptr<AutoRef> callbackPtr_ = nullptr; 62 }; 63 } // namespace AudioStandard 64 } // namespace OHOS 65 #endif /* NAPI_AUDIO_CAPTURER_INFO_CHANGE_CALLBACK_H */