• 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_META_DATA_EXTRACTOR_NAPI_H
17 #define AV_META_DATA_EXTRACTOR_NAPI_H
18 
19 #include "media_errors.h"
20 #include "napi/native_api.h"
21 #include "napi/native_node_api.h"
22 #include "helper_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_callback.h"
28 
29 namespace OHOS {
30 namespace Media {
31 struct AVMetadataExtractorAsyncContext;
32 using TaskRet = std::pair<int32_t, std::string>;
33 class AVMetadataExtractorNapi : public AVMetadataHelperNotify {
34 public:
35     __attribute__((visibility("default"))) static napi_value Init(napi_env env, napi_value exports);
36 
37 private:
38     static napi_value Constructor(napi_env env, napi_callback_info info);
39     static void Destructor(napi_env env, void *nativeObject, void *finalize);
40     static napi_value JsCreateAVMetadataExtractor(napi_env env, napi_callback_info info);
41     static napi_value JsResolveMetadata(napi_env env, napi_callback_info info);
42     static napi_value JsFetchArtPicture(napi_env env, napi_callback_info info);
43     static napi_value JsRelease(napi_env env, napi_callback_info info);
44     /**
45      * url: string
46      */
47     static napi_value JsSetUrl(napi_env env, napi_callback_info info);
48     static napi_value JsGetUrl(napi_env env, napi_callback_info info);
49     /**
50      * fdSrc: AVFileDescriptor
51      */
52     static napi_value JsGetAVFileDescriptor(napi_env env, napi_callback_info info);
53     static napi_value JsSetAVFileDescriptor(napi_env env, napi_callback_info info);
54     /**
55      * dataSrc: DataSrcDescriptor
56      */
57     static napi_value JsSetDataSrc(napi_env env, napi_callback_info info);
58     static napi_value JsGetDataSrc(napi_env env, napi_callback_info info);
59 
60     static AVMetadataExtractorNapi* GetJsInstance(napi_env env, napi_callback_info info);
61     static AVMetadataExtractorNapi* GetJsInstanceWithParameter(napi_env env, napi_callback_info info,
62         size_t &argc, napi_value *argv);
63     static void FetchArtPictureComplete(napi_env env, napi_status status, void *data);
64     static void CommonCallbackRoutine(napi_env env, AVMetadataExtractorAsyncContext* &asyncContext,
65                                       const napi_value &valueParam);
66     static void ResolveMetadataComplete(napi_env env, napi_status status, void *data);
67 
68     AVMetadataExtractorNapi();
69     ~AVMetadataExtractorNapi() override;
70     void SaveCallbackReference(const std::string &callbackName, std::shared_ptr<AutoRef> ref);
71     void ClearCallbackReference();
72     void ClearCallbackReference(const std::string &callbackName);
73     void StartListenCurrentResource();
74     void PauseListenCurrentResource();
75     void OnErrorCb(MediaServiceExtErrCodeAPI9 errorCode, const std::string &errorMsg);
76     void SetSource(std::string url);
77     void ResetUserParameters();
78 
79     std::shared_ptr<TaskHandler<TaskRet>> ResolveMetadataTask(
80         std::unique_ptr<AVMetadataExtractorAsyncContext> &promiseCtx);
81     std::shared_ptr<TaskHandler<TaskRet>> FetchArtPictureTask(
82         std::unique_ptr<AVMetadataExtractorAsyncContext> &promiseCtx);
83     std::shared_ptr<TaskHandler<TaskRet>> ReleaseTask();
84     void SetAVFileDescriptorTask(std::shared_ptr<AVMetadataHelper>& avHelper, AVFileDescriptor& fileDescriptor);
85     void SetDataSrcTask(std::shared_ptr<AVMetadataHelper>& avHelper,
86         std::shared_ptr<HelperDataSourceCallback>& dataSrcCb);
87 
88     std::string GetCurrentState();
89     void NotifyState(HelperStates state) override;
90 
91     void StopTaskQue();
92     void WaitTaskQueStop();
93 
94     std::condition_variable stopTaskQueCond_;
95     bool taskQueStoped_ = false;
96 
97     static thread_local napi_ref constructor_;
98     napi_env env_ = nullptr;
99     std::shared_ptr<AVMetadataHelper> helper_ = nullptr;
100     std::shared_ptr<AVMetadataHelperCallback> extractorCb_ = nullptr;
101     std::shared_ptr<HelperDataSourceCallback> dataSrcCb_ = nullptr;
102     std::atomic<bool> isReleased_ = false;
103     std::string url_ = "";
104     struct AVFileDescriptor fileDescriptor_;
105     struct AVDataSrcDescriptor dataSrcDescriptor_;
106     std::unique_ptr<TaskQueue> taskQue_;
107     std::mutex mutex_;
108     std::mutex taskMutex_;
109     std::map<std::string, std::shared_ptr<AutoRef>> refMap_;
110     HelperStates state_ = HELPER_IDLE;
111     std::condition_variable stateChangeCond_;
112     std::atomic<bool> stopWait_;
113     PixelMapParams param_;
114 };
115 
116 struct AVMetadataExtractorAsyncContext : public MediaAsyncContext {
AVMetadataExtractorAsyncContextAVMetadataExtractorAsyncContext117     explicit AVMetadataExtractorAsyncContext(napi_env env) : MediaAsyncContext(env) {}
118     ~AVMetadataExtractorAsyncContext() = default;
119 
120     AVMetadataExtractorNapi *napi = nullptr;
121     std::string opt_ = "";
122     std::shared_ptr<TaskHandler<TaskRet>> task_ = nullptr;
123     std::shared_ptr<std::unordered_map<int32_t, std::string>> metadata_ = nullptr;
124     std::shared_ptr<PixelMap> artPicture_ = nullptr;
125     uint32_t status;
126 };
127 } // namespace Media
128 } // namespace OHOS
129 #endif // AV_META_DATA_EXTRACTOR_NAPI_H