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 "media_data_source_callback.h"
23 #include "common_napi.h"
24 #include "audio_info.h"
25 #include "audio_effect.h"
26 #include "task_queue.h"
27 #include "avmetadatahelper.h"
28 
29 namespace OHOS {
30 namespace Media {
31 struct AVImageGeneratorAsyncContext;
32 class AVImageGeneratorNapi {
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();
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     int32_t GetFetchFrameArgs(std::unique_ptr<AVImageGeneratorAsyncContext> &asyncCtx, napi_env env, napi_value timeUs,
74         napi_value option, napi_value params);
75     void SetAVFileDescriptorTask(std::shared_ptr<AVMetadataHelper>& avHelper, AVFileDescriptor& fileDescriptor);
76 
77     std::string GetCurrentState();
78 
79     void StopTaskQue();
80     void WaitTaskQueStop();
81 
82     std::condition_variable stopTaskQueCond_;
83     bool taskQueStoped_ = false;
84 
85     static thread_local napi_ref constructor_;
86     napi_env env_ = nullptr;
87     std::shared_ptr<AVMetadataHelper> helper_ = nullptr;
88     std::atomic<bool> isReleased_ = false;
89     std::string url_ = "";
90     struct AVFileDescriptor fileDescriptor_;
91     std::unique_ptr<TaskQueue> taskQue_;
92     std::mutex mutex_;
93     std::mutex taskMutex_;
94     std::map<std::string, std::shared_ptr<AutoRef>> refMap_;
95     std::atomic<HelperState> state_ = HelperState::HELPER_STATE_IDLE;
96     std::condition_variable stateChangeCond_;
97     std::atomic<bool> stopWait_;
98 };
99 
100 struct AVImageGeneratorAsyncContext : public MediaAsyncContext {
AVImageGeneratorAsyncContextAVImageGeneratorAsyncContext101     explicit AVImageGeneratorAsyncContext(napi_env env) : MediaAsyncContext(env) {}
102     ~AVImageGeneratorAsyncContext() = default;
103 
104     AVImageGeneratorNapi *napi = nullptr;
105     std::string opt_ = "";
106     std::shared_ptr<PixelMap> pixel_ = nullptr;
107     int32_t status = 0;
108     int64_t timeUs_ = 0;
109     int32_t option_ = 0;
110     PixelMapParams param_;
111     std::shared_ptr<AVMetadataHelper> innerHelper_ = nullptr;
112 };
113 } // namespace Media
114 } // namespace OHOS
115 #endif // AV_IMAGE_GENERATOR_NAPI_H