• 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 
16 #ifndef AV_IMAGE_GENERATOR_NAPI_H
17 #define AV_IMAGE_GENERATOR_NAPI_H
18 
19 #include "media_errors.h"
20 #include "napi/native_api.h"
21 #include "napi/native_node_api.h"
22 #include "common_napi.h"
23 #include "audio_info.h"
24 #include "audio_effect.h"
25 #include "task_queue.h"
26 #include "avmetadatahelper_callback.h"
27 
28 namespace OHOS {
29 namespace Media {
30 struct AVImageGeneratorAsyncContext;
31 using TaskRet = std::pair<int32_t, std::string>;
32 class AVImageGeneratorNapi : public AVMetadataHelperNotify {
33 public:
34     __attribute__((visibility("default"))) static napi_value Init(napi_env env, napi_value exports);
35 
36 private:
37     static napi_value Constructor(napi_env env, napi_callback_info info);
38     static void Destructor(napi_env env, void *nativeObject, void *finalize);
39     static napi_value JsCreateAVImageGenerator(napi_env env, napi_callback_info info);
40     static napi_value JsFetchFrameAtTime(napi_env env, napi_callback_info info);
41     static napi_value JsRelease(napi_env env, napi_callback_info info);
42     /**
43      * url: string
44      */
45     static napi_value JsSetUrl(napi_env env, napi_callback_info info);
46     static napi_value JsGetUrl(napi_env env, napi_callback_info info);
47     /**
48      * fdSrc: AVFileDescriptor
49      */
50     static napi_value JsGetAVFileDescriptor(napi_env env, napi_callback_info info);
51     static napi_value JsSetAVFileDescriptor(napi_env env, napi_callback_info info);
52 
53     static AVImageGeneratorNapi* GetJsInstance(napi_env env, napi_callback_info info);
54     static AVImageGeneratorNapi* GetJsInstanceWithParameter(napi_env env, napi_callback_info info,
55         size_t &argc, napi_value *argv);
56     static void CreatePixelMapComplete(napi_env env, napi_status status, void *data);
57     static void CommonCallbackRoutine(napi_env env, AVImageGeneratorAsyncContext* &asyncContext,
58                                       const napi_value &valueParam);
59 
60     static bool CheckSystemApp(napi_env env);
61 
62     AVImageGeneratorNapi();
63     ~AVImageGeneratorNapi() override;
64     void SaveCallbackReference(const std::string &callbackName, std::shared_ptr<AutoRef> ref);
65     void ClearCallbackReference();
66     void ClearCallbackReference(const std::string &callbackName);
67     void StartListenCurrentResource();
68     void PauseListenCurrentResource();
69     void OnErrorCb(MediaServiceExtErrCodeAPI9 errorCode, const std::string &errorMsg);
70     void SetSource(std::string url);
71     void ResetUserParameters();
72 
73     std::shared_ptr<TaskHandler<TaskRet>> FetchFrameAtTimeTask(
74         std::unique_ptr<AVImageGeneratorAsyncContext> &promiseCtx);
75     int32_t GetFetchFrameArgs(std::unique_ptr<AVImageGeneratorAsyncContext> &asyncCtx,
76         napi_env env, napi_value timeUs, napi_value option, napi_value format);
77     std::shared_ptr<TaskHandler<TaskRet>> ReleaseTask();
78     void SetAVFileDescriptorTask(std::shared_ptr<AVMetadataHelper>& avHelper, AVFileDescriptor& fileDescriptor);
79 
80     std::string GetCurrentState();
81     void NotifyState(HelperStates state) override;
82 
83     void StopTaskQue();
84     void WaitTaskQueStop();
85 
86     std::condition_variable stopTaskQueCond_;
87     bool taskQueStoped_ = false;
88 
89     static thread_local napi_ref constructor_;
90     napi_env env_ = nullptr;
91     std::shared_ptr<AVMetadataHelper> helper_ = nullptr;
92     std::shared_ptr<AVMetadataHelperCallback> generatorCb_ = nullptr;
93     std::atomic<bool> isReleased_ = false;
94     std::string url_ = "";
95     struct AVFileDescriptor fileDescriptor_;
96     std::unique_ptr<TaskQueue> taskQue_;
97     std::mutex mutex_;
98     std::mutex taskMutex_;
99     std::map<std::string, std::shared_ptr<AutoRef>> refMap_;
100     HelperStates state_ = HELPER_IDLE;
101     std::condition_variable stateChangeCond_;
102     std::atomic<bool> stopWait_;
103     int64_t timeUs_ = 0;
104     int32_t option_ = 0;
105     PixelMapParams param_;
106 };
107 
108 struct AVImageGeneratorAsyncContext : public MediaAsyncContext {
AVImageGeneratorAsyncContextAVImageGeneratorAsyncContext109     explicit AVImageGeneratorAsyncContext(napi_env env) : MediaAsyncContext(env) {}
110     ~AVImageGeneratorAsyncContext() = default;
111 
112     AVImageGeneratorNapi *napi = nullptr;
113     std::string opt_ = "";
114     std::shared_ptr<TaskHandler<TaskRet>> task_ = nullptr;
115     std::shared_ptr<PixelMap> pixel_ = nullptr;
116     uint32_t status;
117 };
118 } // namespace Media
119 } // namespace OHOS
120 #endif // AV_IMAGE_GENERATOR_NAPI_H