• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022-2025 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_RECORDER_NAPI_H
17 #define AV_RECORDER_NAPI_H
18 
19 #include "recorder.h"
20 #include "av_common.h"
21 #include "media_errors.h"
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 #include "common_napi.h"
25 #include "task_queue.h"
26 #include "recorder_profiles.h"
27 #include "pixel_map_napi.h"
28 #include "buffer/avbuffer.h"
29 
30 namespace OHOS {
31 namespace Media {
32 /* type AVRecorderState = 'idle' | 'prepared' | 'started' | 'paused' | 'stopped' | 'released' | 'error'; */
33 namespace AVRecorderState {
34 const std::string STATE_IDLE = "idle";
35 const std::string STATE_PREPARED = "prepared";
36 const std::string STATE_STARTED = "started";
37 const std::string STATE_PAUSED = "paused";
38 const std::string STATE_STOPPED = "stopped";
39 const std::string STATE_RELEASED = "released";
40 const std::string STATE_ERROR = "error";
41 }
42 
43 namespace AVRecordergOpt {
44 const std::string PREPARE = "Prepare";
45 const std::string SET_ORIENTATION_HINT = "SetOrientationHint";
46 const std::string GETINPUTSURFACE = "GetInputSurface";
47 const std::string GETINPUTMETASURFACE = "GetInputMetaSurface";
48 const std::string START = "Start";
49 const std::string PAUSE = "Pause";
50 const std::string RESUME = "Resume";
51 const std::string STOP = "Stop";
52 const std::string RESET = "Reset";
53 const std::string RELEASE = "Release";
54 const std::string GET_AV_RECORDER_PROFILE = "GetAVRecorderProfile";
55 const std::string SET_AV_RECORDER_CONFIG = "SetAVRecorderConfig";
56 const std::string GET_AV_RECORDER_CONFIG = "GetAVRecorderConfig";
57 const std::string GET_CURRENT_AUDIO_CAPTURER_INFO = "GetCurrentAudioCapturerInfo";
58 const std::string GET_MAX_AMPLITUDE = "GetMaxAmplitude";
59 const std::string GET_ENCODER_INFO = "GetEncoderInfo";
60 const std::string IS_WATERMARK_SUPPORTED = "IsWatermarkSupported";
61 const std::string SET_WATERMARK = "SetWatermark";
62 const std::string SET_METADATA = "SetMetadata";
63 const std::string SET_WILL_MUTE_WHEN_INTERRUPTED = "SetWillMuteWhenInterrupted";
64 }
65 
66 constexpr int32_t AVRECORDER_DEFAULT_AUDIO_BIT_RATE = 48000;
67 constexpr int32_t AVRECORDER_DEFAULT_AUDIO_CHANNELS = 2;
68 constexpr int32_t AVRECORDER_DEFAULT_AUDIO_SAMPLE_RATE = 48000;
69 constexpr int32_t AVRECORDER_DEFAULT_VIDEO_BIT_RATE = 48000;
70 constexpr int32_t AVRECORDER_DEFAULT_FRAME_HEIGHT = -1;
71 constexpr int32_t AVRECORDER_DEFAULT_FRAME_WIDTH = -1;
72 constexpr int32_t AVRECORDER_DEFAULT_FRAME_RATE = 30;
73 
74 const std::map<std::string, std::vector<std::string>> stateCtrlList = {
75     {AVRecorderState::STATE_IDLE, {
76         AVRecordergOpt::PREPARE,
77         AVRecordergOpt::RESET,
78         AVRecordergOpt::RELEASE,
79         AVRecordergOpt::GET_AV_RECORDER_PROFILE,
80         AVRecordergOpt::SET_AV_RECORDER_CONFIG,
81         AVRecordergOpt::GET_ENCODER_INFO,
82         AVRecordergOpt::SET_WILL_MUTE_WHEN_INTERRUPTED,
83     }},
84     {AVRecorderState::STATE_PREPARED, {
85         AVRecordergOpt::SET_ORIENTATION_HINT,
86         AVRecordergOpt::GETINPUTSURFACE,
87         AVRecordergOpt::GETINPUTMETASURFACE,
88         AVRecordergOpt::START,
89         AVRecordergOpt::RESET,
90         AVRecordergOpt::RELEASE,
91         AVRecordergOpt::GET_CURRENT_AUDIO_CAPTURER_INFO,
92         AVRecordergOpt::GET_MAX_AMPLITUDE,
93         AVRecordergOpt::GET_ENCODER_INFO,
94         AVRecordergOpt::GET_AV_RECORDER_CONFIG,
95         AVRecordergOpt::IS_WATERMARK_SUPPORTED,
96         AVRecordergOpt::SET_WATERMARK,
97         AVRecordergOpt::SET_METADATA
98     }},
99     {AVRecorderState::STATE_STARTED, {
100         AVRecordergOpt::START,
101         AVRecordergOpt::RESUME,
102         AVRecordergOpt::PAUSE,
103         AVRecordergOpt::STOP,
104         AVRecordergOpt::RESET,
105         AVRecordergOpt::RELEASE,
106         AVRecordergOpt::GET_CURRENT_AUDIO_CAPTURER_INFO,
107         AVRecordergOpt::GET_MAX_AMPLITUDE,
108         AVRecordergOpt::GET_ENCODER_INFO,
109         AVRecordergOpt::GET_AV_RECORDER_CONFIG,
110         AVRecordergOpt::IS_WATERMARK_SUPPORTED,
111         AVRecordergOpt::SET_METADATA
112     }},
113     {AVRecorderState::STATE_PAUSED, {
114         AVRecordergOpt::PAUSE,
115         AVRecordergOpt::RESUME,
116         AVRecordergOpt::STOP,
117         AVRecordergOpt::RESET,
118         AVRecordergOpt::RELEASE,
119         AVRecordergOpt::GET_CURRENT_AUDIO_CAPTURER_INFO,
120         AVRecordergOpt::GET_MAX_AMPLITUDE,
121         AVRecordergOpt::GET_ENCODER_INFO,
122         AVRecordergOpt::GET_AV_RECORDER_CONFIG,
123         AVRecordergOpt::IS_WATERMARK_SUPPORTED,
124         AVRecordergOpt::SET_METADATA
125     }},
126     {AVRecorderState::STATE_STOPPED, {
127         AVRecordergOpt::STOP,
128         AVRecordergOpt::PREPARE,
129         AVRecordergOpt::RESET,
130         AVRecordergOpt::RELEASE,
131         AVRecordergOpt::GET_ENCODER_INFO,
132         AVRecordergOpt::GET_AV_RECORDER_CONFIG
133     }},
134     {AVRecorderState::STATE_RELEASED, {
135         AVRecordergOpt::RELEASE
136     }},
137     {AVRecorderState::STATE_ERROR, {
138         AVRecordergOpt::RESET,
139         AVRecordergOpt::RELEASE
140     }},
141 };
142 
143 /**
144  * on(type: 'stateChange', callback: (state: AVPlayerState, reason: StateChangeReason) => void): void
145  * on(type: 'error', callback: ErrorCallback): void
146  * on(type: 'audioCaptureChange', callback: Callback<AudioCaptureChangeInfo>): void
147  * on(type: 'photoAssetAvailable', callback: Callback<photoAccessHelper.PhotoAsset>): void
148  */
149 namespace AVRecorderEvent {
150 const std::string EVENT_STATE_CHANGE = "stateChange";
151 const std::string EVENT_ERROR = "error";
152 const std::string EVENT_AUDIO_CAPTURE_CHANGE = "audioCapturerChange";
153 const std::string EVENT_PHOTO_ASSET_AVAILABLE = "photoAssetAvailable";
154 }
155 
156 struct AVRecorderAsyncContext;
157 struct AVRecorderProfile {
158     int32_t audioBitrate = AVRECORDER_DEFAULT_AUDIO_BIT_RATE;
159     int32_t audioChannels = AVRECORDER_DEFAULT_AUDIO_CHANNELS;
160     int32_t audioSampleRate = AVRECORDER_DEFAULT_AUDIO_SAMPLE_RATE;
161     AudioCodecFormat audioCodecFormat = AudioCodecFormat::AUDIO_DEFAULT;
162 
163     int32_t videoBitrate = AVRECORDER_DEFAULT_VIDEO_BIT_RATE;
164     int32_t videoFrameWidth = AVRECORDER_DEFAULT_FRAME_HEIGHT;
165     int32_t videoFrameHeight = AVRECORDER_DEFAULT_FRAME_WIDTH;
166     int32_t videoFrameRate = AVRECORDER_DEFAULT_FRAME_RATE;
167     bool isHdr = false;
168     bool enableTemporalScale = false;
169     bool enableStableQualityMode = false;
170     bool enableBFrame = false;
171     VideoCodecFormat videoCodecFormat = VideoCodecFormat::VIDEO_DEFAULT;
172 
173     OutputFormatType fileFormat = OutputFormatType::FORMAT_DEFAULT;
174 };
175 
176 struct AVRecorderConfig {
177     AudioSourceType audioSourceType; // source type;
178     VideoSourceType videoSourceType;
179     std::vector<MetaSourceType> metaSourceTypeVec;
180     AVRecorderProfile profile;
181     std::string url;
182     int32_t rotation = 0; // Optional
183     int32_t maxDuration = INT32_MAX; // Optional
184     Location location; // Optional
185     AVMetadata metadata; // Optional
186     FileGenerationMode fileGenerationMode = FileGenerationMode::APP_CREATE;
187     bool withVideo = false;
188     bool withAudio = false;
189     bool withLocation = false;
190 };
191 
192 struct WatermarkConfig {
193     int32_t top = -1; // offset of the watermark to the top line of pixel
194     int32_t left = -1; // offset of the watermark to the left line if pixel
195 };
196 
197 using RetInfo = std::pair<int32_t, std::string>;
198 
199 class AVRecorderNapi {
200 public:
201     __attribute__((visibility("default"))) static napi_value Init(napi_env env, napi_value exports);
202 
203     using AvRecorderTaskqFunc = RetInfo (AVRecorderNapi::*)();
204 
205 private:
206     static napi_value Constructor(napi_env env, napi_callback_info info);
207     static void Destructor(napi_env env, void *nativeObject, void *finalize);
208     /**
209      * createAVRecorder(callback: AsyncCallback<VideoPlayer>): void
210      * createAVRecorder(): Promise<VideoPlayer>
211      */
212     static napi_value JsCreateAVRecorder(napi_env env, napi_callback_info info);
213     /**
214      * prepare(config: AVRecorderConfig, callback: AsyncCallback<void>): void;
215      * prepare(config: AVRecorderConfig): Promise<void>;
216      */
217     static napi_value JsPrepare(napi_env env, napi_callback_info info);
218     /**
219      * setOrientationHint(config: AVRecorderConfig, callback: AsyncCallback<void>): void;
220      * setOrientationHint(config: AVRecorderConfig): Promise<void>;
221      */
222     static napi_value JsSetOrientationHint(napi_env env, napi_callback_info info);
223     /**
224      * setWatermark(watermark: image.PixelMap, config: WatermarkConfig): promise<void>;
225     */
226     static napi_value JsSetWatermark(napi_env env, napi_callback_info info);
227     /**
228      * setMetadata(metadata: Record<string, string>): void;
229     */
230     static napi_value JsSetMetadata(napi_env env, napi_callback_info info);
231     /**
232      * getInputSurface(callback: AsyncCallback<string>): void
233      * getInputSurface(): Promise<string>
234      */
235     static napi_value JsGetInputSurface(napi_env env, napi_callback_info info);
236     /**
237      * getInputMetaSurface(callback: AsyncCallback<string>): void
238      * getInputMetaSurface(): Promise<string>
239      */
240     static napi_value JsGetInputMetaSurface(napi_env env, napi_callback_info info);
241     /**
242      * start(callback: AsyncCallback<void>): void;
243      * start(): Promise<void>;
244      */
245     static napi_value JsStart(napi_env env, napi_callback_info info);
246     /**
247      * pause(callback: AsyncCallback<void>): void;
248      * pause(): Promise<void>;
249      */
250     static napi_value JsPause(napi_env env, napi_callback_info info);
251     /**
252      * resume(callback: AsyncCallback<void>): void;
253      * resume(): Promise<void>;
254      */
255     static napi_value JsResume(napi_env env, napi_callback_info info);
256     /**
257      * stop(callback: AsyncCallback<void>): void;
258      * stop(): Promise<void>;
259      */
260     static napi_value JsStop(napi_env env, napi_callback_info info);
261     /**
262      * reset(callback: AsyncCallback<void>): void
263      * reset(): Promise<void>
264      */
265     static napi_value JsReset(napi_env env, napi_callback_info info);
266     /**
267      * release(callback: AsyncCallback<void>): void
268      * release(): Promise<void>
269      */
270     static napi_value JsRelease(napi_env env, napi_callback_info info);
271     /**
272      * on(type: 'stateChange', callback: (state: AVPlayerState, reason: StateChangeReason) => void): void
273      * on(type: 'error', callback: ErrorCallback): void
274      * on(type: 'audioCaptureChange', callback: Callback<AudioCaptureChangeInfo>): void
275      * on(type: 'photoAssetAvailable', callback: Callback<photoAccessHelper.PhotoAsset>): void
276      */
277     static napi_value JsSetEventCallback(napi_env env, napi_callback_info info);
278     /**
279      * off(type: 'stateChange'): void;
280      * off(type: 'error'): void;
281      * off(type: 'audioCaptureChange'): void
282      * off(type: 'photoAssetAvailable'): void
283      */
284     static napi_value JsCancelEventCallback(napi_env env, napi_callback_info info);
285     /**
286      * readonly state: AVRecorderState;
287      */
288     static napi_value JsGetState(napi_env env, napi_callback_info info);
289     /**
290      * getVideoRecorderProfile(sourceId: number, qualityLevel: VideoRecorderQualityLevel,
291      *     callback: AsyncCallback<AVRecorderProfile>);
292      * getVideoRecorderProfile(sourceId: number, qualityLevel: VideoRecorderQualityLevel): Promise<AVRecorderProfile>;
293     */
294     static napi_value JsGetAVRecorderProfile(napi_env env, napi_callback_info info);
295     /**
296      * setAVRecorderConfig(config: AVRecorderConfig, callback: AsyncCallback<void>): void;
297      * setAVRecorderConfig(config: AVRecorderConfig): Promise<void>;
298     */
299     static napi_value JsSetAVRecorderConfig(napi_env env, napi_callback_info info);
300     /**
301      * getAVRecorderConfig(callback: AsyncCallback<AVRecorderConfig>);
302      * getAVRecorderConfig(): Promise<AVRecorderConfig>;
303     */
304     static napi_value JsGetAVRecorderConfig(napi_env env, napi_callback_info info);
305     /**
306      * getCurrentAudioCapturerInfo(callback: AsyncCallback<audio.AudioCapturerChangeInfo>): void;
307      * getCurrentAudioCapturerInfo(): Promise<audio.AudioCapturerChangeInfo>;
308     */
309     static napi_value JsGetCurrentAudioCapturerInfo(napi_env env, napi_callback_info info);
310     /**
311      * getAudioCapturerMaxAmplitude(callback: AsyncCallback<number>): void;
312      * getAudioCapturerMaxAmplitude(): Promise<number>;
313     */
314     static napi_value JsGetAudioCapturerMaxAmplitude(napi_env env,  napi_callback_info info);
315     /**
316      * getAvailableEncoder(callback: AsyncCallback<Array<EncoderInfo>>): void;
317      * getAvailableEncoder(): Promise<Array<EncoderInfo>>;
318     */
319     static napi_value JsGetAvailableEncoder(napi_env env,  napi_callback_info info);
320     /**
321      * isWatermarkSupported(): promise<boolean>;
322     */
323     static napi_value JsIsWatermarkSupported(napi_env env, napi_callback_info info);
324 
325     static napi_value JsSetWillMuteWhenInterrupted(napi_env env, napi_callback_info info);
326 
327     static AVRecorderNapi* GetJsInstanceAndArgs(napi_env env, napi_callback_info info,
328         size_t &argCount, napi_value *args);
329     static std::shared_ptr<TaskHandler<RetInfo>> GetPrepareTask(std::unique_ptr<AVRecorderAsyncContext> &asyncCtx);
330     static std::shared_ptr<TaskHandler<RetInfo>> GetSetOrientationHintTask(
331         const std::unique_ptr<AVRecorderAsyncContext> &asyncCtx);
332     static std::shared_ptr<TaskHandler<RetInfo>> GetInputMetaSurface(
333         const std::unique_ptr<AVRecorderAsyncContext> &asyncCtx);
334     static std::shared_ptr<TaskHandler<RetInfo>> GetPromiseTask(AVRecorderNapi *avnapi, const std::string &opt);
335     static std::shared_ptr<TaskHandler<RetInfo>> GetAVRecorderProfileTask(
336         const std::unique_ptr<AVRecorderAsyncContext> &asyncCtx);
337     static std::shared_ptr<TaskHandler<RetInfo>> SetAVRecorderConfigTask(
338         std::unique_ptr<AVRecorderAsyncContext> &asyncCtx);
339     static napi_value ExecuteByPromise(napi_env env, napi_callback_info info, const std::string &opt);
340     static std::shared_ptr<TaskHandler<RetInfo>> GetAVRecorderConfigTask(
341         const std::unique_ptr<AVRecorderAsyncContext> &asyncCtx);
342     static std::shared_ptr<TaskHandler<RetInfo>> GetCurrentCapturerChangeInfoTask(
343         const std::unique_ptr<AVRecorderAsyncContext> &asyncCtx);
344     static std::shared_ptr<TaskHandler<RetInfo>> GetMaxAmplitudeTask(
345         const std::unique_ptr<AVRecorderAsyncContext> &asyncCtx);
346     static std::shared_ptr<TaskHandler<RetInfo>> GetEncoderInfoTask(
347         const std::unique_ptr<AVRecorderAsyncContext> &asyncCtx);
348     static std::shared_ptr<TaskHandler<RetInfo>> IsWatermarkSupportedTask(
349         const std::unique_ptr<AVRecorderAsyncContext> &asyncCtx);
350     static std::shared_ptr<TaskHandler<RetInfo>> SetWatermarkTask(
351         const std::unique_ptr<AVRecorderAsyncContext> &asyncCtx);
352     static std::shared_ptr<TaskHandler<RetInfo>> SetWillMuteWhenInterruptedTask(
353         const std::unique_ptr<AVRecorderAsyncContext> &asyncCtx, bool enable);
354     static int32_t GetAudioCodecFormat(const std::string &mime, AudioCodecFormat &codecFormat);
355     static int32_t GetVideoCodecFormat(const std::string &mime, VideoCodecFormat &codecFormat);
356     static int32_t GetOutputFormat(const std::string &extension, OutputFormatType &type);
357 
358     static int32_t GetPropertyInt32(napi_env env, napi_value configObj, const std::string &type, int32_t &result,
359         bool &getValue);
360     static int32_t GetPropertyInt32Vec(napi_env env, napi_value configObj, const std::string &type,
361     std::vector<int32_t> &result, bool &getValue);
362 
363     static int32_t GetAVRecorderProfile(std::shared_ptr<AVRecorderProfile> &profile,
364         const int32_t sourceId, const int32_t qualityLevel);
365     static void MediaProfileLog(bool isVideo, AVRecorderProfile &profile);
366 
367     AVRecorderNapi();
368     ~AVRecorderNapi();
369 
370     RetInfo GetInputSurface();
371     RetInfo Start();
372     RetInfo Pause();
373     RetInfo Resume();
374     RetInfo Stop();
375     RetInfo Reset();
376     RetInfo Release();
377     RetInfo GetVideoRecorderProfile();
378 
379     int32_t GetAVRecorderConfig(std::shared_ptr<AVRecorderConfig> &config);
380     int32_t GetCurrentCapturerChangeInfo(AudioRecorderChangeInfo &changeInfo);
381     int32_t GetMaxAmplitude(int32_t &maxAmplitude);
382     int32_t GetEncoderInfo(std::vector<EncoderCapabilityData> &encoderInfo);
383     int32_t IsWatermarkSupported(bool &isWatermarkSupported);
384     int32_t SetWillMuteWhenInterrupted(bool muteWhenInterrupted);
385     int32_t SetWatermark(std::shared_ptr<PixelMap> &pixelMap, std::shared_ptr<WatermarkConfig> &watermarkConfig);
386     int32_t SetMetadata(const std::map<std::string, std::string> &recordMeta);
387 
388     void ErrorCallback(int32_t errCode, const std::string &operate, const std::string &add = "");
389     void StateCallback(const std::string &state);
390     void SetCallbackReference(const std::string &callbackName, std::shared_ptr<AutoRef> ref);
391     void CancelCallbackReference(const std::string &callbackName);
392     void CancelCallback();
393     void RemoveSurface();
394 
395     int32_t CheckStateMachine(const std::string &opt);
396     int32_t CheckRepeatOperation(const std::string &opt);
397     int32_t GetSourceType(std::unique_ptr<AVRecorderAsyncContext> &asyncCtx, napi_env env, napi_value args);
398     int32_t GetAudioProfile(std::unique_ptr<AVRecorderAsyncContext> &asyncCtx, napi_env env, napi_value item,
399         AVRecorderProfile &profile);
400     int32_t GetVideoProfile(std::unique_ptr<AVRecorderAsyncContext> &asyncCtx, napi_env env, napi_value item,
401         AVRecorderProfile &profile);
402     int32_t GetProfile(std::unique_ptr<AVRecorderAsyncContext> &asyncCtx, napi_env env, napi_value args);
403     int32_t GetModeAndUrl(std::unique_ptr<AVRecorderAsyncContext> &asyncCtx, napi_env env, napi_value args);
404     int32_t GetConfig(std::unique_ptr<AVRecorderAsyncContext> &asyncCtx, napi_env env, napi_value args);
405     int32_t GetRotation(std::unique_ptr<AVRecorderAsyncContext> &asyncCtx, napi_env env, napi_value args);
406     int32_t GetMetaType(std::unique_ptr<AVRecorderAsyncContext> &asyncCtx, napi_env env, napi_value args);
407     int32_t GetAVMetaData(std::unique_ptr<AVRecorderAsyncContext> &asyncCtx, napi_env env, napi_value args);
408     int32_t GetWatermarkParameter(std::unique_ptr<AVRecorderAsyncContext> &asyncCtx, napi_env env,
409         napi_value watermark, napi_value watermarkConfig);
410     int32_t GetWatermark(std::unique_ptr<AVRecorderAsyncContext> &asyncCtx, napi_env env, napi_value args);
411     int32_t GetWatermarkConfig(std::unique_ptr<AVRecorderAsyncContext> &asyncCtx, napi_env env, napi_value args);
412 
413     bool GetLocation(std::unique_ptr<AVRecorderAsyncContext> &asyncCtx, napi_env env, napi_value args);
414     int32_t GetSourceIdAndQuality(std::unique_ptr<AVRecorderAsyncContext> &asyncCtx, napi_env env,
415         napi_value sourceIdArgs, napi_value qualityArgs, const std::string &opt);
416     RetInfo SetProfile(std::shared_ptr<AVRecorderConfig> config);
417     RetInfo Configure(std::shared_ptr<AVRecorderConfig> config);
418     RetInfo ConfigureUrl(std::shared_ptr<AVRecorderConfig> config);
419     int32_t ConfigAVBufferMeta(std::shared_ptr<PixelMap> &pixelMap, std::shared_ptr<WatermarkConfig> &watermarkConfig,
420         std::shared_ptr<Meta> &meta);
421 
422     static thread_local napi_ref constructor_;
423     napi_env env_ = nullptr;
424     std::shared_ptr<Recorder> recorder_ = nullptr;
425     std::shared_ptr<RecorderCallback> recorderCb_ = nullptr;
426     std::map<std::string, std::shared_ptr<AutoRef>> eventCbMap_;
427     std::unique_ptr<TaskQueue> taskQue_;
428     static std::map<std::string, AvRecorderTaskqFunc> taskQFuncs_;
429     std::map<MetaSourceType, int32_t> metaSourceIDMap_;
430     sptr<Surface> surface_ = nullptr;
431     sptr<Surface> metaSurface_ = nullptr;
432     int32_t videoSourceID_ = -1;
433     int32_t audioSourceID_ = -1;
434     int32_t metaSourceID_ = -1;
435     bool withVideo_ = false;
436     bool getVideoInputSurface_ = false;
437     bool getMetaInputSurface_ = false;
438     int32_t sourceId_ = -1;
439     int32_t qualityLevel_ = -1;
440     bool hasConfiged_ = false;
441     int32_t videoFrameWidth_ = -1; // Required for watermarking. Synchronize the modification if any.
442     int32_t videoFrameHeight_ = -1; // Required for watermarking. Synchronize the modification if any.
443     int32_t rotation_ = 0; // Required for watermarking. Synchronize the modification if any.
444 };
445 
446 struct AVRecorderAsyncContext : public MediaAsyncContext {
AVRecorderAsyncContextAVRecorderAsyncContext447     explicit AVRecorderAsyncContext(napi_env env) : MediaAsyncContext(env) {}
448     ~AVRecorderAsyncContext() = default;
449 
450     void AVRecorderSignError(int32_t errCode, const std::string &operate,
451         const std::string &param, const std::string &add = "");
452 
453     AVRecorderNapi *napi = nullptr;
454     std::shared_ptr<AVRecorderConfig> config_ = nullptr;
455     std::string opt_ = "";
456     std::shared_ptr<TaskHandler<RetInfo>> task_ = nullptr;
457     std::shared_ptr<AVRecorderProfile> profile_ = nullptr;
458     AudioRecorderChangeInfo changeInfo_;
459     int32_t maxAmplitude_ = 0;
460     std::vector<EncoderCapabilityData> encoderInfo_;
461     MetaSourceType metaType_ = MetaSourceType::VIDEO_META_SOURCE_INVALID;
462     std::shared_ptr<PixelMap> pixelMap_ = nullptr;
463     std::shared_ptr<WatermarkConfig> watermarkConfig_ = nullptr;
464     bool isWatermarkSupported_ = false;
465 };
466 
467 class MediaJsResultExtensionMethod {
468 public:
469     static int32_t SetAudioCodecFormat(AudioCodecFormat &codecFormat, std::string &mime);
470     static int32_t SetVideoCodecFormat(VideoCodecFormat &codecFormat, std::string &mime);
471     static int32_t SetFileFormat(OutputFormatType &type, std::string &extension);
472 };
473 class MediaJsAVRecorderProfile : public MediaJsResult {
474 public:
MediaJsAVRecorderProfile(std::shared_ptr<AVRecorderProfile> value)475     explicit MediaJsAVRecorderProfile(std::shared_ptr<AVRecorderProfile> value)
476         : value_(value)
477     {
478     }
479     ~MediaJsAVRecorderProfile() = default;
480     napi_status GetJsResult(napi_env env, napi_value &result) override;
481 
482 private:
483     std::shared_ptr<AVRecorderProfile> value_ = nullptr;
484 };
485 class MediaJsAVRecorderConfig : public MediaJsResult {
486 public:
MediaJsAVRecorderConfig(std::shared_ptr<AVRecorderConfig> value)487     explicit MediaJsAVRecorderConfig(std::shared_ptr<AVRecorderConfig> value)
488         : value_(value)
489     {
490     }
491     ~MediaJsAVRecorderConfig() = default;
492     napi_status GetJsResult(napi_env env, napi_value &result) override;
493     napi_status audioToSet(napi_env env, napi_value &profile, napi_value &result);
494     napi_status videoToSet(napi_env env, napi_value &profile, napi_value &result);
495     napi_status locationToSet(napi_env env, napi_value &location, napi_value &result);
496 
497 private:
498     std::shared_ptr<AVRecorderConfig> value_ = nullptr;
499 };
500 class MediaJsEncoderInfo : public MediaJsResult {
501 public:
MediaJsEncoderInfo(const std::vector<EncoderCapabilityData> encoderInfo)502     explicit MediaJsEncoderInfo(const std::vector<EncoderCapabilityData> encoderInfo)
503         : encoderInfo_(encoderInfo)
504     {
505     }
506     ~MediaJsEncoderInfo() = default;
507     napi_status GetJsResult(napi_env env, napi_value &result) override;
508     napi_status GetAudioEncoderInfo(
509         napi_env env, EncoderCapabilityData encoderCapData, napi_value &result, uint32_t position);
510     napi_status GetVideoEncoderInfo(
511         napi_env env, EncoderCapabilityData encoderCapData, napi_value &result, uint32_t position);
512 private:
513     std::vector<EncoderCapabilityData> encoderInfo_;
514 };
515 } // namespace Media
516 } // namespace OHOS
517 #endif // AV_RECORDER_NAPI_H