• 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 JsRelease(napi_env env, napi_callback_info info);
41     /**
42      * url: string
43      */
44     static napi_value JsSetUrl(napi_env env, napi_callback_info info);
45     static napi_value JsGetUrl(napi_env env, napi_callback_info info);
46     /**
47      * fdSrc: AVFileDescriptor
48      */
49     static napi_value JsGetAVFileDescriptor(napi_env env, napi_callback_info info);
50     static napi_value JsSetAVFileDescriptor(napi_env env, napi_callback_info info);
51 
52     static AVImageGeneratorNapi* GetJsInstance(napi_env env, napi_callback_info info);
53     static AVImageGeneratorNapi* GetJsInstanceWithParameter(napi_env env, napi_callback_info info,
54         size_t &argc, napi_value *argv);
55     static void CreatePixelMapComplete(napi_env env, napi_status status, void *data);
56     static void CommonCallbackRoutine(napi_env env, AVImageGeneratorAsyncContext* &asyncContext,
57                                       const napi_value &valueParam);
58 
59     static bool CheckSystemApp(napi_env env);
60 
61     AVImageGeneratorNapi();
62     ~AVImageGeneratorNapi();
63     void SaveCallbackReference(const std::string &callbackName, std::shared_ptr<AutoRef> ref);
64     void ClearCallbackReference();
65     void ClearCallbackReference(const std::string &callbackName);
66     void StartListenCurrentResource();
67     void PauseListenCurrentResource();
68     void OnErrorCb(MediaServiceExtErrCodeAPI9 errorCode, const std::string &errorMsg);
69     void SetSource(std::string url);
70     void ResetUserParameters();
71 
72     int32_t GetFetchFrameArgs(std::unique_ptr<AVImageGeneratorAsyncContext> &asyncCtx, napi_env env, napi_value timeUs,
73         napi_value option, napi_value params);
74     void SetAVFileDescriptorTask(std::shared_ptr<AVMetadataHelper>& avHelper, AVFileDescriptor& fileDescriptor);
75 
76     std::string GetCurrentState();
77 
78     void StopTaskQue();
79     void WaitTaskQueStop();
80 
81     std::condition_variable stopTaskQueCond_;
82     bool taskQueStoped_ = false;
83 
84     static thread_local napi_ref constructor_;
85     napi_env env_ = nullptr;
86     std::shared_ptr<AVMetadataHelper> helper_ = nullptr;
87     std::atomic<bool> isReleased_ = false;
88     std::string url_ = "";
89     struct AVFileDescriptor fileDescriptor_;
90     std::unique_ptr<TaskQueue> taskQue_;
91     std::mutex mutex_;
92     std::mutex taskMutex_;
93     std::map<std::string, std::shared_ptr<AutoRef>> refMap_;
94     std::atomic<HelperState> state_ = HelperState::HELPER_STATE_IDLE;
95     std::condition_variable stateChangeCond_;
96     std::atomic<bool> stopWait_;
97 };
98 
99 struct AVImageGeneratorAsyncContext : public MediaAsyncContext {
AVImageGeneratorAsyncContextAVImageGeneratorAsyncContext100     explicit AVImageGeneratorAsyncContext(napi_env env) : MediaAsyncContext(env) {}
101     ~AVImageGeneratorAsyncContext() = default;
102 
103     AVImageGeneratorNapi *napi = nullptr;
104     std::string opt_ = "";
105     std::shared_ptr<PixelMap> pixel_ = nullptr;
106     int32_t status = 0;
107     int64_t timeUs_ = 0;
108     int32_t option_ = 0;
109     PixelMapParams param_;
110     std::shared_ptr<AVMetadataHelper> innerHelper_ = nullptr;
111 };
112 } // namespace Media
113 } // namespace OHOS
114 #endif // AV_IMAGE_GENERATOR_NAPI_H