• 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.h"
27 
28 namespace OHOS {
29 namespace Media {
30 struct AVImageGeneratorAsyncContext;
31 class AVImageGeneratorNapi {
32 public:
33     __attribute__((visibility("default"))) static napi_value Init(napi_env env, napi_value exports);
34 
35 private:
36     static napi_value Constructor(napi_env env, napi_callback_info info);
37     static void Destructor(napi_env env, void *nativeObject, void *finalize);
38     static napi_value JsCreateAVImageGenerator(napi_env env, napi_callback_info info);
39     static napi_value JsFetchFrameAtTime(napi_env env, napi_callback_info info);
40     static napi_value JsFetchScaledFrameAtTime(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     static napi_value VerifyTheParameters(napi_env env, napi_callback_info info,
60                                           std::unique_ptr<AVImageGeneratorAsyncContext> &promiseCtx);
61 
62     static bool CheckSystemApp(napi_env env);
63 
64     AVImageGeneratorNapi();
65     ~AVImageGeneratorNapi();
66     void SaveCallbackReference(const std::string &callbackName, std::shared_ptr<AutoRef> ref);
67     void ClearCallbackReference();
68     void ClearCallbackReference(const std::string &callbackName);
69     void StartListenCurrentResource();
70     void PauseListenCurrentResource();
71     void OnErrorCb(MediaServiceExtErrCodeAPI9 errorCode, const std::string &errorMsg);
72     void SetSource(std::string url);
73     void ResetUserParameters();
74 
75     int32_t GetFetchFrameArgs(std::unique_ptr<AVImageGeneratorAsyncContext> &asyncCtx, napi_env env, napi_value timeUs,
76         napi_value option, napi_value params);
77     int32_t GetFetchScaledFrameArgs(std::unique_ptr<AVImageGeneratorAsyncContext> &asyncCtx, napi_env env,
78         napi_value timeUs, napi_value option, napi_value outputSize);
79     void SetAVFileDescriptorTask(std::shared_ptr<AVMetadataHelper>& avHelper, AVFileDescriptor& fileDescriptor);
80 
81     std::string GetCurrentState();
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::atomic<bool> isReleased_ = false;
93     std::string url_ = "";
94     struct AVFileDescriptor fileDescriptor_;
95     std::unique_ptr<TaskQueue> taskQue_;
96     std::mutex mutex_;
97     std::mutex taskMutex_;
98     std::map<std::string, std::shared_ptr<AutoRef>> refMap_;
99     std::atomic<HelperState> state_ = HelperState::HELPER_STATE_IDLE;
100     std::condition_variable stateChangeCond_;
101     std::atomic<bool> stopWait_;
102 };
103 
104 struct AVImageGeneratorAsyncContext : public MediaAsyncContext {
AVImageGeneratorAsyncContextAVImageGeneratorAsyncContext105     explicit AVImageGeneratorAsyncContext(napi_env env) : MediaAsyncContext(env) {}
106     ~AVImageGeneratorAsyncContext() = default;
107 
108     AVImageGeneratorNapi *napi = nullptr;
109     std::string opt_ = "";
110     std::shared_ptr<PixelMap> pixel_ = nullptr;
111     int32_t status = 0;
112     int64_t timeUs_ = 0;
113     int32_t option_ = 0;
114     PixelMapParams param_;
115     std::shared_ptr<AVMetadataHelper> innerHelper_ = nullptr;
116 };
117 } // namespace Media
118 } // namespace OHOS
119 #endif // AV_IMAGE_GENERATOR_NAPI_H