1 /*
2  * Copyright (C) 2024 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_SCREEN_CAPTURE_NAPI_H
17 #define AV_SCREEN_CAPTURE_NAPI_H
18 
19 #include "screen_capture.h"
20 #include "screen_capture_controller.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 
27 namespace OHOS {
28 namespace Media {
29 namespace AVScreenCapturegOpt {
30 const std::string INIT = "Init";
31 const std::string REPORT_USER_CHOICE = "ReportAVScreenCaptureUserChoice";
32 const std::string START_RECORDING = "StartRecording";
33 const std::string STOP_RECORDING = "StopRecording";
34 const std::string SKIP_PRIVACY_MODE = "SkipPrivacyMode";
35 const std::string SET_MIC_ENABLE = "SetMicrophoneEnable";
36 const std::string RELEASE = "Release";
37 }
38 constexpr int32_t AVSCREENCAPTURE_DEFAULT_AUDIO_BIT_RATE = 96000;
39 constexpr int32_t AVSCREENCAPTURE_DEFAULT_AUDIO_CHANNELS = 2;
40 constexpr int32_t AVSCREENCAPTURE_DEFAULT_AUDIO_SAMPLE_RATE = 48000;
41 constexpr int32_t AVSCREENCAPTURE_DEFAULT_VIDEO_FRAME_RATE = 60;
42 constexpr int32_t AVSCREENCAPTURE_DEFAULT_VIDEO_BIT_RATE = 10000000;
43 constexpr int32_t AVSCREENCAPTURE_DEFAULT_FRAME_HEIGHT = -1;
44 constexpr int32_t AVSCREENCAPTURE_DEFAULT_FRAME_WIDTH = -1;
45 const std::string AVSCREENCAPTURE_DEFAULT_FILE_FORMAT = "mp4";
46 
47 namespace AVScreenCaptureEvent {
48 const std::string EVENT_STATE_CHANGE = "stateChange";
49 const std::string EVENT_ERROR = "error";
50 }
51 
52 enum AVScreenCaptureRecorderPreset: int32_t {
53     SCREEN_RECORD_PRESET_H264_AAC_MP4 = 0,
54     SCREEN_RECORD_PRESET_H265_AAC_MP4 = 1
55 };
56 
57 struct AVScreenCaptureAsyncContext;
58 
59 using RetInfo = std::pair<int32_t, std::string>;
60 
61 class AVScreenCaptureNapi {
62 public:
63     __attribute__((visibility("default"))) static napi_value Init(napi_env env, napi_value exports);
64 
65     using AvScreenCaptureTaskqFunc = RetInfo (AVScreenCaptureNapi::*)();
66 private:
67     static napi_value Constructor(napi_env env, napi_callback_info info);
68     static void Destructor(napi_env env, void *nativeObject, void *finalize);
69     /**
70      * createAVScreenCaptureRecorder(): Promise<AVScreenCaptureRecorder>
71      */
72     static napi_value JsCreateAVScreenRecorder(napi_env env, napi_callback_info info);
73     /**
74      * reportAVScreenCaptureUserChoice(sessionId: number, choice: string): Promise<void>
75      */
76     static napi_value JsReportAVScreenCaptureUserChoice(napi_env env, napi_callback_info info);
77     /**
78      * init(config: AVScreenCaptureRecordConfig): Promise<void>
79      */
80     static napi_value JsInit(napi_env env, napi_callback_info info);
81     /**
82      * startRecording(): Promise<void>
83      */
84     static napi_value JsStartRecording(napi_env env, napi_callback_info info);
85     /**
86      * stopRecording(): Promise<void>
87      */
88     static napi_value JsStopRecording(napi_env env, napi_callback_info info);
89     /**
90      * skipPrivacyMode(windowIDs: Array<number>): Promise<void>
91      */
92     static napi_value JsSkipPrivacyMode(napi_env env, napi_callback_info info);
93     /**
94      * setMicrophoneEnabled(enable: boolean): Promise<void>
95      */
96     static napi_value JsSetMicrophoneEnabled(napi_env env, napi_callback_info info);
97     /**
98      * release(): Promise<void>
99      */
100     static napi_value JsRelease(napi_env env, napi_callback_info info);
101     /**
102      * on(type: 'stateChange', callback: Callback<AVScreenCaptureOnInfoType>): void
103      * on(type: 'error', callback: ErrorCallback): void
104      */
105     static napi_value JsSetEventCallback(napi_env env, napi_callback_info info);
106     /**
107      * off(type: 'stateChange', callback?: Callback<AVScreenCaptureOnInfoType>): void
108      * off(type: 'error', callback?: ErrorCallback): void
109      */
110     static napi_value JsCancelEventCallback(napi_env env, napi_callback_info info);
111 
112     static std::shared_ptr<TaskHandler<RetInfo>> GetPromiseTask(AVScreenCaptureNapi *avnapi, const std::string &opt);
113     static napi_value ExecuteByPromise(napi_env env, napi_callback_info info, const std::string &opt);
114     static AVScreenCaptureNapi* GetJsInstanceAndArgs(napi_env env, napi_callback_info info,
115         size_t &argCount, napi_value *args);
116     static std::shared_ptr<TaskHandler<RetInfo>> GetInitTask(
117         const std::unique_ptr<AVScreenCaptureAsyncContext> &asyncCtx);
118     static std::shared_ptr<TaskHandler<RetInfo>> GetSetMicrophoneEnableTask(
119         const std::unique_ptr<AVScreenCaptureAsyncContext> &asyncCtx, const bool enable);
120     static std::shared_ptr<TaskHandler<RetInfo>> GetSkipPrivacyModeTask(
121         const std::unique_ptr<AVScreenCaptureAsyncContext> &asyncCtx, const std::vector<uint64_t> windowIDsVec);
122     static int32_t GetPropertyInt32(napi_env env, napi_value configObj, const std::string &type, int32_t &result);
123     static int32_t CheckVideoCodecFormat(const int32_t &preset);
124     static int32_t CheckVideoFrameFormat(const int32_t &frameWidth, const int32_t &frameHeight,
125         int32_t &videoFrameWidth, int32_t &videoFrameHeight);
126     static VideoCodecFormat GetVideoCodecFormat(const int32_t &preset);
127     static int32_t GetAudioInfo(std::unique_ptr<AVScreenCaptureAsyncContext> &asyncCtx, napi_env env, napi_value args);
128     static int32_t GetVideoInfo(std::unique_ptr<AVScreenCaptureAsyncContext> &asyncCtx, napi_env env, napi_value args);
129     static void AsyncJsReportAVScreenCaptureUserChoice(napi_env env, void *data);
130     static int32_t CheckAudioSampleRate(const int32_t &audioSampleRate);
131     static int32_t CheckAudioChannelCount(const int32_t &audioChannelCount);
132     static napi_status GetWindowIDsVectorParams(std::vector<uint64_t> &windowIDsVec, napi_env env, napi_value* args);
133 
134     AVScreenCaptureNapi();
135     ~AVScreenCaptureNapi();
136 
137     RetInfo StartRecording();
138     RetInfo StopRecording();
139     RetInfo Release();
140 
141     void ErrorCallback(int32_t errCode, const std::string &operate, const std::string &add = "");
142     void StateCallback(const AVScreenCaptureStateCode &stateCode);
143     void SetCallbackReference(const std::string &callbackName, std::shared_ptr<AutoRef> ref);
144     void CancelCallbackReference(const std::string &callbackName);
145     void CancelCallback();
146 
147     int32_t GetConfig(std::unique_ptr<AVScreenCaptureAsyncContext> &asyncCtx, napi_env env, napi_value args);
148     int32_t GetRecorderInfo(std::unique_ptr<AVScreenCaptureAsyncContext> &asyncCtx, napi_env env, napi_value args);
149 
150     static thread_local napi_ref constructor_;
151     napi_env env_ = nullptr;
152     std::shared_ptr<ScreenCapture> screenCapture_ = nullptr;
153     std::shared_ptr<ScreenCaptureCallBack> screenCaptureCb_ = nullptr;
154     std::map<std::string, std::shared_ptr<AutoRef>> eventCbMap_;
155     std::unique_ptr<TaskQueue> taskQue_;
156     static std::map<std::string, AvScreenCaptureTaskqFunc> taskQFuncs_;
157 };
158 
159 struct AVScreenCaptureAsyncContext : public MediaAsyncContext {
AVScreenCaptureAsyncContextAVScreenCaptureAsyncContext160     explicit AVScreenCaptureAsyncContext(napi_env env) : MediaAsyncContext(env) {}
161     ~AVScreenCaptureAsyncContext() = default;
162 
163     void AVScreenCaptureSignError(int32_t errCode, const std::string &operate,
164         const std::string &param, const std::string &add = "");
165 
166     AVScreenCaptureNapi *napi = nullptr;
167     AVScreenCaptureConfig config_;
168     std::shared_ptr<ScreenCaptureController> controller_ = nullptr;
169     std::string opt_ = "";
170     std::shared_ptr<TaskHandler<RetInfo>> task_ = nullptr;
171 };
172 
173 } // namespace Media
174 } // namespace OHOS
175 #endif