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 16 #ifndef NAPI_RENDERER_POSITION_CALLBACK_H 17 #define NAPI_RENDERER_POSITION_CALLBACK_H 18 19 #include <uv.h> 20 21 #include "napi/native_api.h" 22 #include "napi/native_node_api.h" 23 #include "napi_async_work.h" 24 #include "audio_renderer.h" 25 #include "napi_audio_renderer_callback.h" 26 27 namespace OHOS { 28 namespace AudioStandard { 29 class NapiRendererPositionCallback : public RendererPositionCallback, 30 public NapiAudioRendererCallbackInner { 31 public: 32 explicit NapiRendererPositionCallback(napi_env env); 33 ~NapiRendererPositionCallback() override; 34 void SaveCallbackReference(const std::string &callbackName, napi_value args) override; 35 void OnMarkReached(const int64_t &framePosition) override; 36 void CreateMarkReachedTsfn(napi_env env); 37 bool GetMarkReachedTsfnFlag(); 38 void RemoveCallbackReference(const std::string &callbackName, napi_env env, 39 napi_value callback, napi_value args = nullptr) override; 40 bool CheckIfTargetCallbackName(const std::string &callbackName) override; 41 protected: 42 std::shared_ptr<AutoRef> GetCallback(const std::string &callbackName) override; 43 napi_env &GetEnv() override; 44 private: 45 struct RendererPositionJsCallback { 46 std::shared_ptr<AutoRef> callback = nullptr; 47 std::string callbackName = "unknown"; 48 int64_t position = 0; 49 }; 50 51 void OnJsRendererPositionCallback(std::unique_ptr<RendererPositionJsCallback> &jsCb); 52 static void SafeJsCallbackPositionWork(napi_env env, napi_value js_cb, void *context, void *data); 53 static void PositionTsfnFinalize(napi_env env, void *data, void *hint); 54 55 std::mutex mutex_; 56 napi_env env_ = nullptr; 57 std::shared_ptr<AutoRef> renderPositionCallback_ = nullptr; 58 bool regArPosTsfn_ = false; 59 napi_threadsafe_function arPosTsfn_ = nullptr; 60 }; 61 } // namespace AudioStandard 62 } // namespace OHOS 63 #endif // NAPI_RENDERER_POSITION_CALLBACK_H 64