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