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