• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CAPTURER_POSITION_CALLBACK_H
16 #define NAPI_CAPTURER_POSITION_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_capturer.h"
23 #include "napi_audio_capturer_callback_inner.h"
24 
25 namespace OHOS {
26 namespace AudioStandard {
27 class NapiCapturerPositionCallback : public CapturerPositionCallback,
28     public NapiAudioCapturerCallbackInner {
29 public:
30     explicit NapiCapturerPositionCallback(napi_env env);
31     ~NapiCapturerPositionCallback() override;
32     void SaveCallbackReference(const std::string &callbackName, napi_value args) override;
33     void RemoveCallbackReference(const std::string &callbackName, napi_env env, napi_value callback) override;
34     void OnMarkReached(const int64_t &framePosition) override;
35     void CreateCapturePositionTsfn(napi_env env);
36     bool GetCapturePositionFlag();
37     bool CheckIfTargetCallbackName(const std::string &callbackName) override;
38 protected:
39     std::shared_ptr<AutoRef> GetCallback(const std::string &callbackName) override;
40     napi_env &GetEnv() override;
41 
42 private:
43     struct CapturerPositionJsCallback {
44         std::shared_ptr<AutoRef> callback = nullptr;
45         std::string callbackName = "unknown";
46         int64_t position = 0;
47     };
48 
49     void OnJsCapturerPositionCallback(std::unique_ptr<CapturerPositionJsCallback> &jsCb);
50     static void CapturePostionTsfnFinalize(napi_env env, void *data, void *hint);
51     static void SafeJsCallbackCapturerPositionWork(napi_env env, napi_value js_cb, void *context, void *data);
52 
53     std::mutex mutex_;
54     napi_env env_ = nullptr;
55     std::shared_ptr<AutoRef> capturerPositionCallback_ = nullptr;
56     bool regAcPosTsfn_ = false;
57     napi_threadsafe_function acPosTsfn_ = nullptr;
58 };
59 }  // namespace AudioStandard
60 }  // namespace OHOS
61 #endif /* NAPI_CAPTURER_POSITION_CALLBACK_H */