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_RENDERER_CALLBACK_H 16 #define NAPI_AUDIO_RENDERER_CALLBACK_H 17 18 #include <uv.h> 19 20 #include "napi/native_api.h" 21 #include "napi/native_node_api.h" 22 #include "napi_async_work.h" 23 #include "audio_renderer.h" 24 #include "napi_audio_renderer_callback_inner.h" 25 26 namespace OHOS { 27 namespace AudioStandard { 28 const std::string INTERRUPT_CALLBACK_NAME = "interrupt"; 29 const std::string AUDIO_INTERRUPT_CALLBACK_NAME = "audioInterrupt"; 30 const std::string STATE_CHANGE_CALLBACK_NAME = "stateChange"; 31 const std::string MARK_REACH_CALLBACK_NAME = "markReach"; 32 const std::string PERIOD_REACH_CALLBACK_NAME = "periodReach"; 33 const std::string DATA_REQUEST_CALLBACK_NAME = "dataRequest"; 34 const std::string DEVICECHANGE_CALLBACK_NAME = "outputDeviceChange"; 35 const std::string OUTPUT_DEVICECHANGE_WITH_INFO = "outputDeviceChangeWithInfo"; 36 const std::string WRITE_DATA_CALLBACK_NAME = "writeData"; 37 38 class NapiAudioRendererCallback : public AudioRendererCallback, 39 public NapiAudioRendererCallbackInner { 40 public: 41 explicit NapiAudioRendererCallback(napi_env env); 42 ~NapiAudioRendererCallback() override; 43 void OnInterrupt(const InterruptEvent &interruptEvent) override; 44 void OnStateChange(const RendererState state, const StateChangeCmdType __attribute__((unused)) cmdType) override; 45 void CreateArInterrupt(napi_env env); 46 void CreateArStateChange(napi_env env); 47 bool GetArInterruptTsfnFlag(); 48 bool GetArStateChangeTsfnFlag(); 49 void SaveCallbackReference(const std::string &callbackName, napi_value args) override; 50 void RemoveCallbackReference(const std::string &callbackName, napi_env env, 51 napi_value callback, napi_value args = nullptr) override; 52 bool CheckIfTargetCallbackName(const std::string &callbackName) override; 53 protected: 54 std::shared_ptr<AutoRef> GetCallback(const std::string &callbackName) override; 55 napi_env &GetEnv() override; 56 57 private: 58 struct AudioRendererJsCallback { 59 std::shared_ptr<AutoRef> callback = nullptr; 60 std::string callbackName = "unknown"; 61 InterruptEvent interruptEvent; 62 RendererState state; 63 }; 64 65 void OnJsCallbackInterrupt(std::unique_ptr<AudioRendererJsCallback> &jsCb); 66 void OnJsCallbackStateChange(std::unique_ptr<AudioRendererJsCallback> &jsCb); 67 static void SafeJsCallbackStateChangeWork(napi_env env, napi_value js_cb, void *context, void *data); 68 static void StateChangeTsfnFinalize(napi_env env, void *data, void *hint); 69 static void SafeJsCallbackInterruptWork(napi_env env, napi_value js_cb, void *context, void *data); 70 static void InterruptTsfnFinalize(napi_env env, void *data, void *hint); 71 72 std::mutex mutex_; 73 napi_env env_ = nullptr; 74 std::shared_ptr<AutoRef> interruptCallback_ = nullptr; 75 std::shared_ptr<AutoRef> stateChangeCallback_ = nullptr; 76 bool regArStateChgTsfn_ = false; 77 bool regArInterruptTsfn_ = false; 78 napi_threadsafe_function arStateChgTsfn_ = nullptr; 79 napi_threadsafe_function arInterruptTsfn_ = nullptr; 80 }; 81 } // namespace AudioStandard 82 } // namespace OHOS 83 #endif /* NAPI_AUDIO_RENDERER_CALLBACK_H */ 84