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 #include "ability_connection.h"
17 #include "ability_manager_client.h"
18 #include "screen_capture_server.h"
19 #include "ui_extension_ability_connection.h"
20 #include "extension_manager_client.h"
21 #include "image_source.h"
22 #include "image_type.h"
23 #include "pixel_map.h"
24 #include "media_log.h"
25 #include "media_errors.h"
26 #include "media_utils.h"
27 #include "uri_helper.h"
28 #include "media_dfx.h"
29 #include "scope_guard.h"
30 #include "screen_capture_listener_proxy.h"
31 #include "res_type.h"
32 #include "res_sched_client.h"
33 #include "param_wrapper.h"
34 #include <unistd.h>
35 #include <sys/stat.h>
36 #include "hitrace/tracechain.h"
37 #include "locale_config.h"
38 #include <sync_fence.h>
39 #include "parameter.h"
40 #include <unordered_map>
41 
42 using OHOS::Rosen::DMError;
43 
44 namespace {
45 const std::string DUMP_PATH = "/data/media/screen_capture.bin";
46 }
47 
48 namespace OHOS {
49 namespace Media {
50 namespace {
51 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_SCREENCAPTURE, "ScreenCaptureServer"};
52 static std::map<int32_t, std::weak_ptr<OHOS::Media::ScreenCaptureServer>> serverMap;
53 std::atomic<int32_t> activeSessionId_(-1);
54 
55 // As in the ScreenCaptureServer destructor function mutexGlobal_ is required to update serverMap
56 // MAKE SURE THAT when mutexGlobal_ is been holding MUST NOT trigger ScreenCaptureServer destructor to be called
57 std::mutex mutexGlobal_;
58 
GetScreenCaptureServerByIdWithLock(int32_t id)59 std::shared_ptr<OHOS::Media::ScreenCaptureServer> GetScreenCaptureServerByIdWithLock(int32_t id)
60 {
61     auto iter = serverMap.find(id);
62     if (iter == serverMap.end()) {
63         return nullptr;
64     }
65     return (iter->second).lock();
66 }
67 }
68 
69 static const std::string MP4 = "mp4";
70 static const std::string M4A = "m4a";
71 
72 static const std::string USER_CHOICE_ALLOW = "true";
73 static const std::string USER_CHOICE_DENY = "false";
74 static const std::string BUTTON_NAME_MIC = "mic";
75 static const std::string BUTTON_NAME_STOP = "stop";
76 static const std::string ICON_PATH_CAPSULE_STOP = "/etc/screencapture/capsule_stop.svg";
77 static const std::string ICON_PATH_NOTIFICATION = "/etc/screencapture/notification.png";
78 static const std::string ICON_PATH_MIC = "/etc/screencapture/mic.svg";
79 static const std::string ICON_PATH_MIC_OFF = "/etc/screencapture/mic_off.svg";
80 static const std::string ICON_PATH_STOP = "/etc/screencapture/stop.png";
81 static const std::string BACK_GROUND_COLOR = "#E84026";
82 static const std::string SELECT_ABILITY_NAME = "SelectWindowAbility";
83 static const int32_t SVG_HEIGHT = 80;
84 static const int32_t SVG_WIDTH = 80;
85 static const int32_t MDPI = 160;
86 static const int32_t MICROPHONE_OFF = 0;
87 static const int32_t MICROPHONE_STATE_COUNT = 2;
88 #ifdef SUPPORT_SCREEN_CAPTURE_WINDOW_NOTIFICATION
89     static const int32_t NOTIFICATION_MAX_TRY_NUM = 3;
90 #endif
91 
92 static const int32_t MAX_SESSION_ID = 256;
93 static const int32_t MAX_SESSION_PER_UID = 8;
94 static const auto NOTIFICATION_SUBSCRIBER = NotificationSubscriber();
95 static constexpr int32_t AUDIO_CHANGE_TIME = 200000; // 200 ms
96 
OnConnected()97 void NotificationSubscriber::OnConnected()
98 {
99     MEDIA_LOGI("NotificationSubscriber OnConnected");
100 }
101 
OnDisconnected()102 void NotificationSubscriber::OnDisconnected()
103 {
104     MEDIA_LOGI("NotificationSubscriber OnDisconnected");
105 }
106 
OnResponse(int32_t notificationId,OHOS::sptr<OHOS::Notification::NotificationButtonOption> buttonOption)107 void NotificationSubscriber::OnResponse(int32_t notificationId,
108                                         OHOS::sptr<OHOS::Notification::NotificationButtonOption> buttonOption)
109 {
110     MEDIA_LOGI("NotificationSubscriber OnResponse notificationId : %{public}d, ButtonName : %{public}s ",
111         notificationId, (buttonOption->GetButtonName()).c_str());
112 
113     // To avoid deadlock: first release mutexGlobal_, then be destructed
114     std::shared_ptr<OHOS::Media::ScreenCaptureServer> server;
115     {
116         std::unique_lock<std::mutex> lock(mutexGlobal_);
117         server = GetScreenCaptureServerByIdWithLock(notificationId);
118     }
119     if (server == nullptr) {
120         MEDIA_LOGW("OnResponse ScreenCaptureServer not exist, notificationId : %{public}d, ButtonName : %{public}s ",
121             notificationId, (buttonOption->GetButtonName()).c_str());
122         return;
123     }
124     if (BUTTON_NAME_STOP.compare(buttonOption->GetButtonName()) == 0) {
125         server->StopScreenCaptureByEvent(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_STOPPED_BY_USER);
126         return;
127     }
128     if (BUTTON_NAME_MIC.compare(buttonOption->GetButtonName()) == 0) {
129         server->UpdateMicrophoneEnabled();
130         return;
131     }
132 }
133 
OnDied()134 void NotificationSubscriber::OnDied()
135 {
136     MEDIA_LOGI("NotificationSubscriber OnDied");
137 }
138 
PrivateWindowListenerInScreenCapture(std::weak_ptr<ScreenCaptureServer> screenCaptureServer)139 PrivateWindowListenerInScreenCapture::PrivateWindowListenerInScreenCapture(
140     std::weak_ptr<ScreenCaptureServer> screenCaptureServer)
141 {
142     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
143     screenCaptureServer_ = screenCaptureServer;
144 }
145 
OnPrivateWindow(bool hasPrivate)146 void PrivateWindowListenerInScreenCapture::OnPrivateWindow(bool hasPrivate)
147 {
148     MEDIA_LOGI("PrivateWindowListenerInScreenCapture hasPrivateWindow: %{public}u", hasPrivate);
149     auto scrServer = screenCaptureServer_.lock();
150     if (scrServer) {
151         MEDIA_LOGI("Callback OnDMPrivateWindowChange hasPrivateWindow: %{public}u", hasPrivate);
152         scrServer->OnDMPrivateWindowChange(hasPrivate);
153     }
154 }
155 
OnDMPrivateWindowChange(bool hasPrivate)156 void ScreenCaptureServer::OnDMPrivateWindowChange(bool hasPrivate)
157 {
158     MEDIA_LOGI("OnDMPrivateWindowChange hasPrivateWindow: %{public}u", hasPrivate);
159     if (screenCaptureCb_ != nullptr) {
160         screenCaptureCb_->OnStateChange(hasPrivate ?
161         AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_ENTER_PRIVATE_SCENE :
162         AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_EXIT_PRIVATE_SCENE);
163     }
164 }
165 
Create()166 std::shared_ptr<IScreenCaptureService> ScreenCaptureServer::Create()
167 {
168     MEDIA_LOGI("ScreenCaptureServer Create start.");
169     std::shared_ptr<ScreenCaptureServer> serverTemp = std::make_shared<ScreenCaptureServer>();
170     CHECK_AND_RETURN_RET_LOG(serverTemp != nullptr, nullptr, "Failed to new ScreenCaptureServer");
171     int32_t countForUid = 0;
172     int32_t newSessionId = 0;
173 
174     // To avoid deadlock: first release mutexGlobal_, then be destructed
175     std::shared_ptr<ScreenCaptureServer> server;
176     for (int32_t i = 0; i < MAX_SESSION_ID; i++) {
177         // To avoid deadlock: first release mutexGlobal_, then be destructed
178         {
179             std::unique_lock<std::mutex> lock(mutexGlobal_);
180             server = GetScreenCaptureServerByIdWithLock(newSessionId);
181         }
182         if (server != nullptr) {
183             newSessionId++;
184             countForUid += (server->appInfo_.appUid == serverTemp->appInfo_.appUid) ? 1 : 0;
185             // To avoid deadlock: first release mutexGlobal_, then be destructed
186             // Do this without holding mutexGlobal_ to avoid dead lock when set nullptr trigger server destruct
187             server = nullptr;
188             continue;
189         }
190         if (countForUid >= MAX_SESSION_PER_UID) {
191             MEDIA_LOGE("Create failed, uid(%{public}d) has created too many ScreenCaptureServer instances",
192                 serverTemp->appInfo_.appUid);
193             return nullptr;
194         }
195         {
196             std::unique_lock<std::mutex> lock(mutexGlobal_);
197             serverMap.insert(std::make_pair(newSessionId, serverTemp));
198             MEDIA_LOGI("ScreenCaptureServer::Create newSessionId: %{public}d, serverMap size:%{public}zu",
199                 newSessionId, serverMap.size());
200         }
201         break;
202     }
203 
204     if (newSessionId == MAX_SESSION_ID) {
205         MEDIA_LOGE("Create failed for uid(%{public}d), there are too many ScreenCaptureServer instances",
206             serverTemp->appInfo_.appUid);
207         return nullptr;
208     }
209     serverTemp->SetSessionId(newSessionId);
210     MEDIA_LOGI("ScreenCaptureServer Create end.");
211     return std::static_pointer_cast<OHOS::Media::IScreenCaptureService>(serverTemp);
212 }
213 
GetRunningScreenCaptureInstancePid(int32_t & pid)214 int32_t ScreenCaptureServer::GetRunningScreenCaptureInstancePid(int32_t &pid)
215 {
216     MEDIA_LOGI("GetRunningScreenCaptureInstancePid in");
217     std::shared_ptr<ScreenCaptureServer> currentServer;
218     {
219         std::unique_lock<std::mutex> lock(mutexGlobal_);
220         if (activeSessionId_.load() < 0) {
221             return MSERR_UNKNOWN;
222         }
223         currentServer = GetScreenCaptureServerByIdWithLock(activeSessionId_.load());
224     }
225     if (currentServer != nullptr) {
226         MEDIA_LOGI("GetRunningScreenCaptureInstancePid uid(%{public}d) pid(%{public}d)",
227             currentServer->appInfo_.appUid, currentServer->appInfo_.appPid);
228         pid = currentServer->appInfo_.appPid;
229         return MSERR_OK;
230     }
231     return MSERR_UNKNOWN;
232 }
233 
GetSpecificServer(int32_t sessionId,std::shared_ptr<ScreenCaptureServer> & server)234 int32_t ScreenCaptureServer::GetSpecificServer(int32_t sessionId, std::shared_ptr<ScreenCaptureServer> &server)
235 {
236     // To avoid deadlock: first release mutexGlobal_, then be destructed
237     {
238         std::lock_guard<std::mutex> lock(mutexGlobal_);
239         server = GetScreenCaptureServerByIdWithLock(sessionId);
240     }
241     if (server == nullptr) {
242         return MSERR_UNKNOWN;
243     }
244     return MSERR_OK;
245 }
246 
GetChoiceFromJson(Json::Value & root,const std::string & content,std::string key,std::string & value)247 void ScreenCaptureServer::GetChoiceFromJson(Json::Value &root,
248     const std::string &content, std::string key, std::string &value)
249 {
250     Json::Reader reader;
251     bool parsingSuccessful = reader.parse(content, root);
252     if (!parsingSuccessful || root.type() != Json::objectValue) {
253         MEDIA_LOGE("Error parsing the string");
254         return;
255     }
256     const Json::Value keyJson = root[key];
257     if (!keyJson.isNull()) {
258         value = keyJson.asString();
259     }
260 }
261 
PrepareSelectWindow(Json::Value & root,std::shared_ptr<ScreenCaptureServer> & server)262 void ScreenCaptureServer::PrepareSelectWindow(Json::Value &root, std::shared_ptr<ScreenCaptureServer> &server)
263 {
264     if (root.type() != Json::objectValue) {
265         return;
266     }
267     const Json::Value missionIdJson = root["missionId"];
268     if (!missionIdJson.isNull() && missionIdJson.asInt64() >= 0) {
269         uint64_t missionId = missionIdJson.asInt64();
270         MEDIA_LOGI("Report Select MissionId: %{public}" PRIu64, missionId);
271         server->SetMissionId(missionId);
272     }
273     const Json::Value displayIdJson = root["displayId"];
274     if (!displayIdJson.isNull() && displayIdJson.asInt64() >= 0) {
275         uint64_t displayId = displayIdJson.asInt64();
276         MEDIA_LOGI("Report Select DisplayId: %{public}" PRIu64, displayId);
277         server->SetDisplayId(displayId);
278     }
279 }
280 
ReportAVScreenCaptureUserChoice(int32_t sessionId,const std::string & content)281 int32_t ScreenCaptureServer::ReportAVScreenCaptureUserChoice(int32_t sessionId, const std::string &content)
282 {
283     MEDIA_LOGI("ReportAVScreenCaptureUserChoice sessionId: %{public}d,"
284         "content: %{public}s", sessionId, content.c_str());
285     // To avoid deadlock: first release mutexGlobal_, then be destructed
286     std::shared_ptr<ScreenCaptureServer> server;
287     int32_t serverRet = GetSpecificServer(sessionId, server);
288     CHECK_AND_RETURN_RET_LOG(serverRet == MSERR_OK, serverRet,
289         "ReportAVScreenCaptureUserChoice failed to get instance, sessionId: %{public}d", sessionId);
290     std::shared_ptr<ScreenCaptureServer> currentServer;
291     Json::Value root;
292     std::string choice = "false";
293     GetChoiceFromJson(root, content, std::string("choice"), choice);
294     if (USER_CHOICE_ALLOW.compare(choice) == 0) {
295         int currentSessionId = -1;
296         {
297             std::lock_guard <std::mutex> lock(mutexGlobal_);
298             currentSessionId = activeSessionId_.load();
299         }
300         if (currentSessionId >= 0) {
301             {
302                 std::lock_guard<std::mutex> lock(mutexGlobal_);
303                 currentServer = GetScreenCaptureServerByIdWithLock(activeSessionId_.load());
304             }
305             if (currentServer != nullptr && sessionId != activeSessionId_.load()) {
306                 MEDIA_LOGW("ReportAVScreenCaptureUserChoice uid(%{public}d) is interrupted by uid(%{public}d)",
307                     currentServer->appInfo_.appUid, server->appInfo_.appUid);
308                 currentServer->StopScreenCaptureByEvent(
309                     AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INTERRUPTED_BY_OTHER);
310             }
311             {
312                 std::lock_guard <std::mutex> lock(mutexGlobal_);
313                 activeSessionId_.store(SESSION_ID_INVALID);
314             }
315         }
316         PrepareSelectWindow(root, server);
317         int32_t ret = server->OnReceiveUserPrivacyAuthority(true);
318         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret,
319             "ReportAVScreenCaptureUserChoice user choice is true but start failed");
320         MEDIA_LOGI("ReportAVScreenCaptureUserChoice user choice is true and start success");
321         return MSERR_OK;
322     } else if (USER_CHOICE_DENY.compare(choice) == 0) {
323         return server->OnReceiveUserPrivacyAuthority(false);
324     } else {
325         MEDIA_LOGW("ReportAVScreenCaptureUserChoice user choice is not support");
326     }
327     return MSERR_UNKNOWN;
328 }
329 
SetDisplayId(uint64_t displayId)330 void ScreenCaptureServer::SetDisplayId(uint64_t displayId)
331 {
332     captureConfig_.videoInfo.videoCapInfo.displayId = displayId;
333 }
334 
SetMissionId(uint64_t missionId)335 void ScreenCaptureServer::SetMissionId(uint64_t missionId)
336 {
337     missionIds_.emplace_back(missionId);
338 }
339 
SetMetaDataReport()340 void ScreenCaptureServer::SetMetaDataReport()
341 {
342     std::shared_ptr<Media::Meta> meta = std::make_shared<Media::Meta>();
343     meta->SetData(Tag::SCREEN_CAPTURE_ERR_CODE, statisticalEventInfo_.errCode);
344     meta->SetData(Tag::SCREEN_CAPTURE_ERR_MSG, statisticalEventInfo_.errMsg);
345     meta->SetData(Tag::SCREEN_CAPTURE_DURATION, statisticalEventInfo_.captureDuration);
346     meta->SetData(Tag::SCREEN_CAPTURE_AV_TYPE, avType_);
347     meta->SetData(Tag::SCREEN_CAPTURE_DATA_TYPE, dataMode_);
348     meta->SetData(Tag::SCREEN_CAPTURE_USER_AGREE, statisticalEventInfo_.userAgree);
349     meta->SetData(Tag::SCREEN_CAPTURE_REQURE_MIC, statisticalEventInfo_.requireMic);
350     meta->SetData(Tag::SCREEN_CAPTURE_ENABLE_MIC, statisticalEventInfo_.enableMic);
351     meta->SetData(Tag::SCREEN_CAPTURE_VIDEO_RESOLUTION, statisticalEventInfo_.videoResolution);
352     meta->SetData(Tag::SCREEN_CAPTURE_STOP_REASON, statisticalEventInfo_.stopReason);
353     meta->SetData(Tag::SCREEN_CAPTURE_START_LATENCY, statisticalEventInfo_.startLatency);
354     AppendMediaInfo(meta, instanceId_);
355     ReportMediaInfo(instanceId_);
356 }
357 
ScreenCaptureServer()358 ScreenCaptureServer::ScreenCaptureServer()
359 {
360     MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
361     InitAppInfo();
362     instanceId_ = OHOS::HiviewDFX::HiTraceChain::GetId().GetChainId();
363     CreateMediaInfo(SCREEN_CAPTRUER, IPCSkeleton::GetCallingUid(), instanceId_);
364 }
365 
~ScreenCaptureServer()366 ScreenCaptureServer::~ScreenCaptureServer()
367 {
368     MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
369     ReleaseInner();
370     CloseFd();
371 }
372 
SetSessionId(int32_t sessionId)373 void ScreenCaptureServer::SetSessionId(int32_t sessionId)
374 {
375     sessionId_ = sessionId;
376 }
377 
SetCaptureMode(CaptureMode captureMode)378 int32_t ScreenCaptureServer::SetCaptureMode(CaptureMode captureMode)
379 {
380     MediaTrace trace("ScreenCaptureServer::SetCaptureMode");
381     std::lock_guard<std::mutex> lock(mutex_);
382     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::CREATED, MSERR_INVALID_OPERATION,
383         "SetCaptureMode failed, capture is not CREATED, state:%{public}d, mode:%{public}d", captureState_, captureMode);
384     MEDIA_LOGI("ScreenCaptureServer::SetCaptureMode start, captureMode:%{public}d", captureMode);
385     int32_t ret = CheckCaptureMode(captureMode);
386     CHECK_AND_RETURN_RET(ret == MSERR_OK, ret);
387     captureConfig_.captureMode = captureMode;
388     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetCaptureMode OK.", FAKE_POINTER(this));
389     return MSERR_OK;
390 }
391 
SetDataType(DataType dataType)392 int32_t ScreenCaptureServer::SetDataType(DataType dataType)
393 {
394     MediaTrace trace("ScreenCaptureServer::SetDataType");
395     std::lock_guard<std::mutex> lock(mutex_);
396     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::CREATED, MSERR_INVALID_OPERATION,
397         "SetDataType failed, capture is not CREATED, state:%{public}d, dataType:%{public}d", captureState_, dataType);
398     MEDIA_LOGI("ScreenCaptureServer::SetDataType start, dataType:%{public}d", dataType);
399     int32_t ret = CheckDataType(dataType);
400     CHECK_AND_RETURN_RET(ret == MSERR_OK, ret);
401     captureConfig_.dataType = dataType;
402     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetDataType OK.", FAKE_POINTER(this));
403     return MSERR_OK;
404 }
405 
SetRecorderInfo(RecorderInfo recorderInfo)406 int32_t ScreenCaptureServer::SetRecorderInfo(RecorderInfo recorderInfo)
407 {
408     std::lock_guard<std::mutex> lock(mutex_);
409     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::CREATED, MSERR_INVALID_OPERATION,
410         "SetRecorderInfo failed, capture is not CREATED, state:%{public}d", captureState_);
411     MEDIA_LOGI("ScreenCaptureServer::SetRecorderInfo start");
412     url_ = recorderInfo.url;
413     avType_ = AVScreenCaptureAvType::AV_TYPE;
414 
415     if (MP4.compare(recorderInfo.fileFormat) == 0) {
416         fileFormat_ = OutputFormatType::FORMAT_MPEG_4;
417     } else if (M4A.compare(recorderInfo.fileFormat) == 0) {
418         fileFormat_ = OutputFormatType::FORMAT_M4A;
419     } else {
420         MEDIA_LOGE("invalid fileFormat type");
421         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_,
422             SCREEN_CAPTURE_ERR_INVALID_VAL, "invalid fileFormat type");
423         return MSERR_INVALID_VAL;
424     }
425     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetRecorderInfo OK.", FAKE_POINTER(this));
426     return MSERR_OK;
427 }
428 
SetOutputFile(int32_t outputFd)429 int32_t ScreenCaptureServer::SetOutputFile(int32_t outputFd)
430 {
431     std::lock_guard<std::mutex> lock(mutex_);
432     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::CREATED, MSERR_INVALID_OPERATION,
433         "SetOutputFile failed, capture is not CREATED, state:%{public}d", captureState_);
434     MEDIA_LOGI("ScreenCaptureServer::SetOutputFile start");
435     if (outputFd < 0) {
436         MEDIA_LOGI("invalid outputFd");
437         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_,
438             SCREEN_CAPTURE_ERR_INVALID_VAL, "invalid outputFd");
439         return MSERR_INVALID_VAL;
440     }
441 
442     int flags = fcntl(outputFd, F_GETFL);
443     if (flags == -1) {
444         MEDIA_LOGE("Fail to get File Status Flags");
445         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_INVALID_VAL,
446             "Fail to get File Status Flags");
447         return MSERR_INVALID_VAL;
448     }
449     if ((static_cast<unsigned int>(flags) & (O_RDWR | O_WRONLY)) == 0) {
450         MEDIA_LOGE("File descriptor is not in read-write mode or write-only mode");
451         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_INVALID_VAL,
452             "File descriptor is not in read-write mode or write-only mode");
453         return MSERR_INVALID_VAL;
454     }
455     CloseFd();
456     MEDIA_LOGI("ScreenCaptureServer fd in, fd is %{public}d", outputFd);
457     outputFd_ = dup(outputFd);
458     MEDIA_LOGI("ScreenCaptureServer fd dup, fd is %{public}d", outputFd_);
459     MEDIA_LOGI("ScreenCaptureServer SetOutputFile End");
460     return MSERR_OK;
461 }
462 
SetScreenCaptureCallback(const std::shared_ptr<ScreenCaptureCallBack> & callback)463 int32_t ScreenCaptureServer::SetScreenCaptureCallback(const std::shared_ptr<ScreenCaptureCallBack> &callback)
464 {
465     MediaTrace trace("ScreenCaptureServer::SetScreenCaptureCallback");
466     std::lock_guard<std::mutex> lock(mutex_);
467     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::CREATED, MSERR_INVALID_OPERATION,
468         "SetScreenCaptureCallback failed, capture is not CREATED, state:%{public}d", captureState_);
469     CHECK_AND_RETURN_RET_LOG(callback != nullptr, MSERR_INVALID_VAL,
470         "SetScreenCaptureCallback failed, callback is nullptr, state:%{public}d", captureState_);
471     MEDIA_LOGI("ScreenCaptureServer::SetScreenCaptureCallback start");
472     screenCaptureCb_ = callback;
473     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetScreenCaptureCallback OK.", FAKE_POINTER(this));
474     return MSERR_OK;
475 }
476 
InitAudioEncInfo(AudioEncInfo audioEncInfo)477 int32_t ScreenCaptureServer::InitAudioEncInfo(AudioEncInfo audioEncInfo)
478 {
479     std::lock_guard<std::mutex> lock(mutex_);
480     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::CREATED, MSERR_INVALID_OPERATION,
481         "InitAudioEncInfo failed, capture is not CREATED, state:%{public}d", captureState_);
482     MEDIA_LOGI("ScreenCaptureServer::InitAudioEncInfo start");
483     MEDIA_LOGD("audioEncInfo audioBitrate:%{public}d, audioCodecformat:%{public}d", audioEncInfo.audioBitrate,
484         audioEncInfo.audioCodecformat);
485     int32_t ret = CheckAudioEncInfo(audioEncInfo);
486     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "InitAudioEncInfo failed, ret:%{public}d", ret);
487     captureConfig_.audioInfo.audioEncInfo = audioEncInfo;
488     return MSERR_OK;
489 }
490 
InitVideoEncInfo(VideoEncInfo videoEncInfo)491 int32_t ScreenCaptureServer::InitVideoEncInfo(VideoEncInfo videoEncInfo)
492 {
493     std::lock_guard<std::mutex> lock(mutex_);
494     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::CREATED, MSERR_INVALID_OPERATION,
495         "InitVideoEncInfo failed, capture is not CREATED, state:%{public}d", captureState_);
496     MEDIA_LOGI("ScreenCaptureServer::InitVideoEncInfo start");
497     MEDIA_LOGD("videoEncInfo videoCodec:%{public}d,  videoBitrate:%{public}d, videoFrameRate:%{public}d",
498         videoEncInfo.videoCodec, videoEncInfo.videoBitrate, videoEncInfo.videoFrameRate);
499     int32_t ret = CheckVideoEncInfo(videoEncInfo);
500     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "InitVideoEncInfo failed, ret:%{public}d", ret);
501     captureConfig_.videoInfo.videoEncInfo = videoEncInfo;
502     return MSERR_OK;
503 }
504 
CheckScreenCapturePermission()505 bool ScreenCaptureServer::CheckScreenCapturePermission()
506 {
507     int result = Security::AccessToken::AccessTokenKit::VerifyAccessToken(appInfo_.appTokenId,
508         "ohos.permission.CAPTURE_SCREEN");
509     if (result == Security::AccessToken::PERMISSION_GRANTED) {
510         MEDIA_LOGI("user have the right to access capture screen!");
511         return true;
512     } else {
513         MEDIA_LOGE("user do not have the right to access capture screen!");
514         return false;
515     }
516 }
517 
IsUserPrivacyAuthorityNeeded()518 bool ScreenCaptureServer::IsUserPrivacyAuthorityNeeded()
519 {
520     MediaTrace trace("ScreenCaptureServer::IsUserPrivacyAuthorityNeeded");
521     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " IsUserPrivacyAuthorityNeeded start, appUid:%{public}d",
522         FAKE_POINTER(this), appInfo_.appUid);
523     if (appInfo_.appUid == ROOT_UID) {
524         MEDIA_LOGI("Root user. Privacy Authority Granted automaticly");
525         return false;
526     }
527     return true;
528 }
529 
CheckCaptureMode(CaptureMode captureMode)530 int32_t ScreenCaptureServer::CheckCaptureMode(CaptureMode captureMode)
531 {
532     MEDIA_LOGD("CheckCaptureMode start, captureMode:%{public}d", captureMode);
533     if ((captureMode > CAPTURE_SPECIFIED_WINDOW) || (captureMode < CAPTURE_HOME_SCREEN)) {
534         MEDIA_LOGE("invalid captureMode:%{public}d", captureMode);
535         return MSERR_INVALID_VAL;
536     }
537     MEDIA_LOGD("ScreenCaptureServer CheckCaptureMode OK.");
538     return MSERR_OK;
539 }
540 
CheckDataType(DataType dataType)541 int32_t ScreenCaptureServer::CheckDataType(DataType dataType)
542 {
543     MEDIA_LOGD("CheckDataType start, dataType:%{public}d", dataType);
544     if ((dataType > DataType::CAPTURE_FILE) || (dataType < DataType::ORIGINAL_STREAM)) {
545         MEDIA_LOGE("invalid dataType:%{public}d", dataType);
546         return MSERR_INVALID_VAL;
547     }
548     if (dataType == DataType::ENCODED_STREAM) {
549         MEDIA_LOGE("not supported dataType:%{public}d", dataType);
550         return MSERR_UNSUPPORT;
551     }
552     MEDIA_LOGD("ScreenCaptureServer CheckDataType OK.");
553     return MSERR_OK;
554 }
555 
CheckAudioCapParam(const AudioCaptureInfo & audioCapInfo)556 int32_t ScreenCaptureServer::CheckAudioCapParam(const AudioCaptureInfo &audioCapInfo)
557 {
558     MEDIA_LOGD("CheckAudioCapParam sampleRate:%{public}d, channels:%{public}d, source:%{public}d, state:%{public}d",
559         audioCapInfo.audioSampleRate, audioCapInfo.audioChannels, audioCapInfo.audioSource, audioCapInfo.state);
560     std::vector<AudioSamplingRate> supportedSamplingRates = AudioStandard::AudioCapturer::GetSupportedSamplingRates();
561     bool foundSupportSample = false;
562     for (auto iter = supportedSamplingRates.begin(); iter != supportedSamplingRates.end(); ++iter) {
563         if (static_cast<AudioSamplingRate>(audioCapInfo.audioSampleRate) == *iter) {
564             foundSupportSample = true;
565         }
566     }
567     if (!foundSupportSample) {
568         MEDIA_LOGE("invalid audioSampleRate:%{public}d", audioCapInfo.audioSampleRate);
569         return MSERR_UNSUPPORT;
570     }
571 
572     std::vector<AudioChannel> supportedChannelList = AudioStandard::AudioCapturer::GetSupportedChannels();
573     bool foundSupportChannel = false;
574     for (auto iter = supportedChannelList.begin(); iter != supportedChannelList.end(); ++iter) {
575         if (static_cast<AudioChannel>(audioCapInfo.audioChannels) == *iter) {
576             foundSupportChannel = true;
577         }
578     }
579     if (!foundSupportChannel) {
580         MEDIA_LOGE("invalid audioChannels:%{public}d", audioCapInfo.audioChannels);
581         return MSERR_UNSUPPORT;
582     }
583 
584     if ((audioCapInfo.audioSource <= SOURCE_INVALID) || (audioCapInfo.audioSource > APP_PLAYBACK)) {
585         MEDIA_LOGE("invalid audioSource:%{public}d", audioCapInfo.audioSource);
586         return MSERR_INVALID_VAL;
587     }
588     MEDIA_LOGD("ScreenCaptureServer CheckAudioCapParam OK.");
589     return MSERR_OK;
590 }
591 
CheckVideoCapParam(const VideoCaptureInfo & videoCapInfo)592 int32_t ScreenCaptureServer::CheckVideoCapParam(const VideoCaptureInfo &videoCapInfo)
593 {
594     MEDIA_LOGD("CheckVideoCapParam width:%{public}d, height:%{public}d, source:%{public}d, state:%{public}d",
595         videoCapInfo.videoFrameWidth, videoCapInfo.videoFrameHeight, videoCapInfo.videoSource, videoCapInfo.state);
596     if ((videoCapInfo.videoFrameWidth <= 0) || (videoCapInfo.videoFrameWidth > VIDEO_FRAME_WIDTH_MAX)) {
597         MEDIA_LOGE("videoCapInfo Width is invalid, videoFrameWidth:%{public}d, videoFrameHeight:%{public}d",
598             videoCapInfo.videoFrameWidth, videoCapInfo.videoFrameHeight);
599         return MSERR_INVALID_VAL;
600     }
601     if ((videoCapInfo.videoFrameHeight <= 0) || (videoCapInfo.videoFrameHeight > VIDEO_FRAME_HEIGHT_MAX)) {
602         MEDIA_LOGE("videoCapInfo Height is invalid, videoFrameWidth:%{public}d, videoFrameHeight:%{public}d",
603             videoCapInfo.videoFrameWidth, videoCapInfo.videoFrameHeight);
604         return MSERR_INVALID_VAL;
605     }
606 
607     if (videoCapInfo.videoSource != VIDEO_SOURCE_SURFACE_RGBA) {
608         MEDIA_LOGE("videoSource is invalid");
609         return MSERR_INVALID_VAL;
610     }
611     MEDIA_LOGD("ScreenCaptureServer CheckVideoCapParam OK.");
612     return MSERR_OK;
613 }
614 
CheckAudioEncParam(const AudioEncInfo & audioEncInfo)615 int32_t ScreenCaptureServer::CheckAudioEncParam(const AudioEncInfo &audioEncInfo)
616 {
617     MEDIA_LOGD("CheckAudioEncParam audioBitrate:%{public}d, audioCodecformat:%{public}d",
618         audioEncInfo.audioBitrate, audioEncInfo.audioCodecformat);
619     if ((audioEncInfo.audioCodecformat >= AudioCodecFormat::AUDIO_CODEC_FORMAT_BUTT) ||
620         (audioEncInfo.audioCodecformat < AudioCodecFormat::AUDIO_DEFAULT)) {
621         MEDIA_LOGE("invalid AudioCodecFormat:%{public}d", audioEncInfo.audioCodecformat);
622         return MSERR_INVALID_VAL;
623     }
624     if (audioEncInfo.audioBitrate < AUDIO_BITRATE_MIN || audioEncInfo.audioBitrate > AUDIO_BITRATE_MAX) {
625         MEDIA_LOGE("invalid audioBitrate:%{public}d", audioEncInfo.audioBitrate);
626         return MSERR_INVALID_VAL;
627     }
628     return MSERR_OK;
629 }
630 
CheckVideoEncParam(const VideoEncInfo & videoEncInfo)631 int32_t ScreenCaptureServer::CheckVideoEncParam(const VideoEncInfo &videoEncInfo)
632 {
633     MEDIA_LOGD("CheckVideoEncParam videoCodec:%{public}d, videoBitrate:%{public}d, videoFrameRate:%{public}d",
634         videoEncInfo.videoCodec, videoEncInfo.videoBitrate, videoEncInfo.videoFrameRate);
635     if ((videoEncInfo.videoCodec >= VideoCodecFormat::VIDEO_CODEC_FORMAT_BUTT) ||
636         (videoEncInfo.videoCodec < VideoCodecFormat::VIDEO_DEFAULT)) {
637         MEDIA_LOGE("invalid VideoCodecFormat:%{public}d", videoEncInfo.videoCodec);
638         return MSERR_INVALID_VAL;
639     }
640     if (videoEncInfo.videoBitrate < VIDEO_BITRATE_MIN || videoEncInfo.videoBitrate > VIDEO_BITRATE_MAX) {
641         MEDIA_LOGE("invalid videoBitrate:%{public}d", videoEncInfo.videoBitrate);
642         return MSERR_INVALID_VAL;
643     }
644     if (videoEncInfo.videoFrameRate < VIDEO_FRAME_RATE_MIN || videoEncInfo.videoFrameRate > VIDEO_FRAME_RATE_MAX) {
645         MEDIA_LOGE("invalid videoFrameRate:%{public}d", videoEncInfo.videoFrameRate);
646         return MSERR_INVALID_VAL;
647     }
648     return MSERR_OK;
649 }
650 
CheckAudioCapInfo(AudioCaptureInfo & audioCapInfo)651 int32_t ScreenCaptureServer::CheckAudioCapInfo(AudioCaptureInfo &audioCapInfo)
652 {
653     MEDIA_LOGD("ScreenCaptureServer CheckAudioCapInfo start, audioChannels:%{public}d, "
654         "audioSampleRate:%{public}d, audioSource:%{public}d, state:%{public}d.",
655         audioCapInfo.audioChannels, audioCapInfo.audioSampleRate, audioCapInfo.audioSource, audioCapInfo.state);
656     if (audioCapInfo.audioChannels == 0 && audioCapInfo.audioSampleRate == 0) {
657         MEDIA_LOGD("audioCap IGNORED sampleRate:%{public}d, channels:%{public}d, source:%{public}d, state:%{public}d",
658             audioCapInfo.audioSampleRate, audioCapInfo.audioChannels, audioCapInfo.audioSource, audioCapInfo.state);
659         audioCapInfo.state = AVScreenCaptureParamValidationState::VALIDATION_IGNORE;
660         return MSERR_OK;
661     }
662     MEDIA_LOGD("CheckAudioCapParam S sampleRate:%{public}d, channels:%{public}d, source:%{public}d, state:%{public}d",
663         audioCapInfo.audioSampleRate, audioCapInfo.audioChannels, audioCapInfo.audioSource, audioCapInfo.state);
664     int32_t ret = CheckAudioCapParam(audioCapInfo);
665     audioCapInfo.state = ret == MSERR_OK ? AVScreenCaptureParamValidationState::VALIDATION_VALID :
666         AVScreenCaptureParamValidationState::VALIDATION_INVALID;
667     MEDIA_LOGD("CheckAudioCapParam E sampleRate:%{public}d, channels:%{public}d, source:%{public}d, state:%{public}d",
668         audioCapInfo.audioSampleRate, audioCapInfo.audioChannels, audioCapInfo.audioSource, audioCapInfo.state);
669     MEDIA_LOGD("ScreenCaptureServer CheckAudioCapInfo end.");
670     return ret;
671 }
672 
CheckVideoCapInfo(VideoCaptureInfo & videoCapInfo)673 int32_t ScreenCaptureServer::CheckVideoCapInfo(VideoCaptureInfo &videoCapInfo)
674 {
675     MEDIA_LOGD("CheckVideoCapInfo start, videoFrameWidth:%{public}d, videoFrameHeight:%{public}d, "
676         "videoSource:%{public}d, state:%{public}d.", videoCapInfo.videoFrameWidth, videoCapInfo.videoFrameHeight,
677         videoCapInfo.videoSource, videoCapInfo.state);
678     if (videoCapInfo.videoFrameWidth == 0 && videoCapInfo.videoFrameHeight == 0) {
679         MEDIA_LOGD("videoCap IGNORED width:%{public}d, height:%{public}d, source:%{public}d, state:%{public}d",
680             videoCapInfo.videoFrameWidth, videoCapInfo.videoFrameHeight, videoCapInfo.videoSource, videoCapInfo.state);
681         videoCapInfo.state = AVScreenCaptureParamValidationState::VALIDATION_IGNORE;
682         return MSERR_OK;
683     }
684     MEDIA_LOGD("CheckVideoCapParam S width:%{public}d, height:%{public}d, source:%{public}d, state:%{public}d",
685         videoCapInfo.videoFrameWidth, videoCapInfo.videoFrameHeight, videoCapInfo.videoSource, videoCapInfo.state);
686     int32_t ret = CheckVideoCapParam(videoCapInfo);
687     videoCapInfo.state = ret == MSERR_OK ? AVScreenCaptureParamValidationState::VALIDATION_VALID :
688         AVScreenCaptureParamValidationState::VALIDATION_INVALID;
689     MEDIA_LOGD("CheckVideoCapParam E width:%{public}d, height:%{public}d, source:%{public}d, state:%{public}d",
690         videoCapInfo.videoFrameWidth, videoCapInfo.videoFrameHeight, videoCapInfo.videoSource, videoCapInfo.state);
691     MEDIA_LOGD("ScreenCaptureServer CheckVideoCapInfo end.");
692     return ret;
693 }
694 
CheckAudioEncInfo(AudioEncInfo & audioEncInfo)695 int32_t ScreenCaptureServer::CheckAudioEncInfo(AudioEncInfo &audioEncInfo)
696 {
697     MEDIA_LOGD("ScreenCaptureServer CheckAudioEncInfo start.");
698     int32_t ret = CheckAudioEncParam(audioEncInfo);
699     audioEncInfo.state = ret == MSERR_OK ? AVScreenCaptureParamValidationState::VALIDATION_VALID :
700         AVScreenCaptureParamValidationState::VALIDATION_INVALID;
701     MEDIA_LOGD("ScreenCaptureServer CheckAudioEncInfo end, state: %{public}d.", audioEncInfo.state);
702     return ret;
703 }
704 
CheckVideoEncInfo(VideoEncInfo & videoEncInfo)705 int32_t ScreenCaptureServer::CheckVideoEncInfo(VideoEncInfo &videoEncInfo)
706 {
707     MEDIA_LOGD("ScreenCaptureServer CheckVideoEncInfo start.");
708     int32_t ret = CheckVideoEncParam(videoEncInfo);
709     videoEncInfo.state = ret == MSERR_OK ? AVScreenCaptureParamValidationState::VALIDATION_VALID :
710         AVScreenCaptureParamValidationState::VALIDATION_INVALID;
711     MEDIA_LOGD("ScreenCaptureServer CheckVideoEncInfo end, state: %{public}d.", videoEncInfo.state);
712     return ret;
713 }
714 
CheckAllParams()715 int32_t ScreenCaptureServer::CheckAllParams()
716 {
717     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " CheckAllParams start, dataType:%{public}d.",
718         FAKE_POINTER(this), captureConfig_.dataType);
719     int32_t ret = CheckDataType(captureConfig_.dataType);
720     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "CheckAllParams CheckDataType failed, ret:%{public}d", ret);
721 
722     ret = CheckCaptureMode(captureConfig_.captureMode);
723     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "CheckAllParams CheckCaptureMode failed, ret:%{public}d", ret);
724 
725     if (captureConfig_.dataType == DataType::ORIGINAL_STREAM) {
726         if (isSurfaceMode_) {
727             dataMode_ = AVScreenCaptureDataMode::SUFFACE_MODE;
728         } else {
729             dataMode_ = AVScreenCaptureDataMode::BUFFER_MODE;
730         }
731         return CheckCaptureStreamParams();
732     }
733     if (captureConfig_.dataType == DataType::CAPTURE_FILE) {
734         dataMode_ = AVScreenCaptureDataMode::FILE_MODE;
735         return CheckCaptureFileParams();
736     }
737     return MSERR_INVALID_VAL;
738 }
739 
CheckCaptureStreamParams()740 int32_t ScreenCaptureServer::CheckCaptureStreamParams()
741 {
742     // For original stream:
743     // 1. Any of innerCapInfo/videoCapInfo should be not invalid and should not be both ignored
744     // 2. micCapInfo should not be invalid
745     // 3. For surface mode, videoCapInfo should be valid
746     CheckAudioCapInfo(captureConfig_.audioInfo.micCapInfo);
747     CheckAudioCapInfo(captureConfig_.audioInfo.innerCapInfo);
748     CheckVideoCapInfo(captureConfig_.videoInfo.videoCapInfo);
749     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " CheckCaptureStreamParams start, isSurfaceMode:%{public}s,"
750         " videoCapInfo.state:%{public}d, innerCapInfo.state:%{public}d.", FAKE_POINTER(this),
751         isSurfaceMode_ ? "true" : "false", captureConfig_.videoInfo.videoCapInfo.state,
752         captureConfig_.audioInfo.innerCapInfo.state);
753     if (isSurfaceMode_) {
754         // surface mode, surface must not nullptr and videoCapInfo must valid.
755         if (surface_ == nullptr ||
756             captureConfig_.videoInfo.videoCapInfo.state != AVScreenCaptureParamValidationState::VALIDATION_VALID) {
757             FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_INVALID_VAL,
758                 "video Cap state fault, videoCapInfo is invalid");
759             return MSERR_INVALID_VAL;
760         }
761     }
762     if (captureConfig_.audioInfo.innerCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_INVALID ||
763         captureConfig_.videoInfo.videoCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_INVALID) {
764         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_INVALID_VAL,
765             "audio inner cap or video cap state invalid");
766         return MSERR_INVALID_VAL;
767     }
768     if (captureConfig_.audioInfo.innerCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_IGNORE &&
769         captureConfig_.videoInfo.videoCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_IGNORE) {
770         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_INVALID_VAL,
771             "audio inner cap or video cap state ignore");
772         return MSERR_INVALID_VAL;
773     }
774     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " CheckCaptureStreamParams OK.", FAKE_POINTER(this));
775     return MSERR_OK;
776 }
777 
CheckCaptureFileParams()778 int32_t ScreenCaptureServer::CheckCaptureFileParams()
779 {
780     // For capture file:
781     // 1. All of innerCapInfo/videoCapInfo/audioEncInfo/videoEncInfo should be be valid
782     // 2. micCapInfo should not be invalid
783     CheckAudioCapInfo(captureConfig_.audioInfo.micCapInfo);
784     CheckAudioCapInfo(captureConfig_.audioInfo.innerCapInfo);
785     CheckAudioEncInfo(captureConfig_.audioInfo.audioEncInfo);
786     CheckVideoCapInfo(captureConfig_.videoInfo.videoCapInfo);
787     if (captureConfig_.videoInfo.videoCapInfo.state != AVScreenCaptureParamValidationState::VALIDATION_IGNORE) {
788         CheckVideoEncInfo(captureConfig_.videoInfo.videoEncInfo);
789     }
790     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " CheckCaptureFileParams start, "
791         "innerCapInfo.state:%{public}d, videoCapInfo.state:%{public}d, audioEncInfo.state:%{public}d, "
792         "videoEncInfo.state:%{public}d, micCapInfo.state:%{public}d.", FAKE_POINTER(this),
793         captureConfig_.audioInfo.innerCapInfo.state, captureConfig_.videoInfo.videoCapInfo.state,
794         captureConfig_.audioInfo.audioEncInfo.state, captureConfig_.videoInfo.videoEncInfo.state,
795         captureConfig_.audioInfo.micCapInfo.state);
796 
797     if (captureConfig_.audioInfo.innerCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_INVALID ||
798         captureConfig_.videoInfo.videoCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_INVALID ||
799         captureConfig_.audioInfo.audioEncInfo.state == AVScreenCaptureParamValidationState::VALIDATION_INVALID ||
800         captureConfig_.videoInfo.videoEncInfo.state == AVScreenCaptureParamValidationState::VALIDATION_INVALID) {
801         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_INVALID_VAL,
802             "innerCap audioEnc videoCap videoEnc state invalid");
803         return MSERR_INVALID_VAL;
804     }
805     if (captureConfig_.audioInfo.micCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_INVALID) {
806         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_INVALID_VAL,
807             "audio mic cap state invalid");
808         return MSERR_INVALID_VAL;
809     }
810     if (captureConfig_.audioInfo.micCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_IGNORE) {
811         return MSERR_OK;
812     }
813     const AudioCaptureInfo &micCapInfo = captureConfig_.audioInfo.micCapInfo;
814     const AudioCaptureInfo &innerCapInfo = captureConfig_.audioInfo.innerCapInfo;
815     if (micCapInfo.audioSampleRate == innerCapInfo.audioSampleRate &&
816         micCapInfo.audioChannels == innerCapInfo.audioChannels) {
817         return MSERR_OK;
818     }
819     MEDIA_LOGE("CheckCaptureFileParams failed, inner and mic param not consistent");
820     FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_INVALID_VAL,
821         "CheckCaptureFileParams failed, inner and mic param not consistent");
822     return MSERR_INVALID_VAL;
823 }
824 
825 // Should call in ipc thread
InitAppInfo()826 void ScreenCaptureServer::InitAppInfo()
827 {
828     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " InitAppInfo start.", FAKE_POINTER(this));
829     appInfo_.appTokenId = IPCSkeleton::GetCallingTokenID();
830     appInfo_.appFullTokenId = IPCSkeleton::GetCallingFullTokenID();
831     appInfo_.appUid = IPCSkeleton::GetCallingUid();
832     appInfo_.appPid = IPCSkeleton::GetCallingPid();
833     appName_ = GetClientBundleName(appInfo_.appUid);
834     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " InitAppInfo end.", FAKE_POINTER(this));
835 }
836 
GetCurrentMillisecond()837 int64_t ScreenCaptureServer::GetCurrentMillisecond()
838 {
839     std::chrono::system_clock::duration duration = std::chrono::system_clock::now().time_since_epoch();
840     int64_t time = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
841     return time;
842 }
843 
SetErrorInfo(int32_t errCode,const std::string & errMsg,StopReason stopReason,bool userAgree)844 void ScreenCaptureServer::SetErrorInfo(int32_t errCode, const std::string &errMsg, StopReason stopReason,
845     bool userAgree)
846 {
847     statisticalEventInfo_.errCode = errCode;
848     statisticalEventInfo_.errMsg = errMsg;
849     statisticalEventInfo_.stopReason = stopReason;
850     statisticalEventInfo_.userAgree = userAgree;
851 }
852 
RequestUserPrivacyAuthority()853 int32_t ScreenCaptureServer::RequestUserPrivacyAuthority()
854 {
855     MediaTrace trace("ScreenCaptureServer::RequestUserPrivacyAuthority");
856     // If Root is treated as whitelisted, how to guarantee RequestUserPrivacyAuthority function by TDD cases.
857     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " RequestUserPrivacyAuthority start.", FAKE_POINTER(this));
858     if (!IsUserPrivacyAuthorityNeeded()) {
859         MEDIA_LOGI("Privacy Authority Granted. uid:%{public}d", appInfo_.appUid);
860         return MSERR_OK;
861     }
862 
863     if (isPrivacyAuthorityEnabled_) {
864         if (GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"]
865                 .compare(appName_) != 0) {
866             return StartPrivacyWindow();
867         } else {
868             MEDIA_LOGI("ScreenCaptureServer::RequestUserPrivacyAuthority support screenrecorder");
869             return MSERR_OK;
870         }
871     }
872 
873     MEDIA_LOGI("privacy notification window not support, go on to check CAPTURE_SCREEN permission");
874     return CheckScreenCapturePermission() ? MSERR_OK : MSERR_INVALID_OPERATION;
875 }
876 
OnReceiveUserPrivacyAuthority(bool isAllowed)877 int32_t ScreenCaptureServer::OnReceiveUserPrivacyAuthority(bool isAllowed)
878 {
879     // Should callback be running in seperate thread?
880     std::lock_guard<std::mutex> lock(mutex_);
881     MEDIA_LOGI("OnReceiveUserPrivacyAuthority start, isAllowed:%{public}d, state:%{public}d", isAllowed, captureState_);
882     if (screenCaptureCb_ == nullptr) {
883         MEDIA_LOGE("OnReceiveUserPrivacyAuthority failed, screenCaptureCb is nullptr, state:%{public}d", captureState_);
884         captureState_ = AVScreenCaptureState::STOPPED;
885         SetErrorInfo(MSERR_UNKNOWN, "OnReceiveUserPrivacyAuthority failed, screenCaptureCb is nullptr",
886             StopReason::RECEIVE_USER_PRIVACY_AUTHORITY_FAILED, IsUserPrivacyAuthorityNeeded());
887         return MSERR_UNKNOWN;
888     }
889 
890     if (captureState_ != AVScreenCaptureState::STARTING) {
891         MEDIA_LOGE("OnReceiveUserPrivacyAuthority failed, capture is not STARTING");
892         screenCaptureCb_->OnError(ScreenCaptureErrorType::SCREEN_CAPTURE_ERROR_INTERNAL,
893             AVScreenCaptureErrorCode::SCREEN_CAPTURE_ERR_UNKNOWN);
894         return MSERR_UNKNOWN;
895     }
896     if (!isAllowed) {
897         captureState_ = AVScreenCaptureState::CREATED;
898         screenCaptureCb_->OnStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_CANCELED);
899         return MSERR_UNKNOWN;
900     }
901     int32_t ret = OnStartScreenCapture();
902     PostStartScreenCapture(ret == MSERR_OK);
903     return ret;
904 }
905 
StartAudioCapture()906 int32_t ScreenCaptureServer::StartAudioCapture()
907 {
908     int32_t ret = MSERR_UNKNOWN;
909     if (isMicrophoneOn_) {
910         ret = StartStreamMicAudioCapture();
911         if (ret != MSERR_OK) {
912             MEDIA_LOGE("StartStreamMicAudioCapture failed");
913         }
914     }
915     ret = StartStreamInnerAudioCapture();
916     if (ret != MSERR_OK) {
917         MEDIA_LOGE("StartStreamInnerAudioCapture failed");
918         micAudioCapture_ = nullptr;
919         return ret;
920     }
921     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartAudioCapture OK.", FAKE_POINTER(this));
922     return MSERR_OK;
923 }
924 
StartStreamInnerAudioCapture()925 int32_t ScreenCaptureServer::StartStreamInnerAudioCapture()
926 {
927     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartStreamInnerAudioCapture start, dataType:%{public}d,"
928         " innerCapInfo.state:%{public}d.",
929         FAKE_POINTER(this), captureConfig_.dataType, captureConfig_.audioInfo.innerCapInfo.state);
930     std::shared_ptr<AudioCapturerWrapper> innerCapture;
931     if (captureConfig_.audioInfo.innerCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID) {
932         MediaTrace trace("ScreenCaptureServer::StartAudioCaptureInner");
933         innerCapture = std::make_shared<AudioCapturerWrapper>(captureConfig_.audioInfo.innerCapInfo, screenCaptureCb_,
934             std::string("OS_InnerAudioCapture"), contentFilter_);
935         int32_t ret = innerCapture->Start(appInfo_);
936         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "StartAudioCapture innerCapture failed");
937     }
938     innerAudioCapture_ = innerCapture;
939     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartStreamInnerAudioCapture OK.", FAKE_POINTER(this));
940     return MSERR_OK;
941 }
942 
StartStreamMicAudioCapture()943 int32_t ScreenCaptureServer::StartStreamMicAudioCapture()
944 {
945     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartStreamMicAudioCapture start, dataType:%{public}d, "
946         "micCapInfo.state:%{public}d.",
947         FAKE_POINTER(this), captureConfig_.dataType, captureConfig_.audioInfo.micCapInfo.state);
948     std::shared_ptr<AudioCapturerWrapper> micCapture;
949     if (captureConfig_.audioInfo.micCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID) {
950         MediaTrace trace("ScreenCaptureServer::StartAudioCaptureMic");
951         ScreenCaptureContentFilter contentFilterMic;
952         micCapture = std::make_shared<AudioCapturerWrapper>(captureConfig_.audioInfo.micCapInfo, screenCaptureCb_,
953             std::string("OS_MicAudioCapture"), contentFilterMic);
954         int32_t ret = micCapture->Start(appInfo_);
955         if (ret != MSERR_OK) {
956             MEDIA_LOGE("StartStreamMicAudioCapture failed");
957             isMicrophoneOn_ = false;
958             screenCaptureCb_->OnStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_MIC_UNAVAILABLE);
959             return ret;
960         }
961     }
962     micAudioCapture_ = micCapture;
963     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartStreamMicAudioCapture OK.", FAKE_POINTER(this));
964     return MSERR_OK;
965 }
966 
StartFileInnerAudioCapture()967 int32_t ScreenCaptureServer::StartFileInnerAudioCapture()
968 {
969     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartFileInnerAudioCapture start, dataType:%{public}d, "
970         "innerCapInfo.state:%{public}d.",
971         FAKE_POINTER(this), captureConfig_.dataType, captureConfig_.audioInfo.innerCapInfo.state);
972     std::shared_ptr<AudioCapturerWrapper> innerCapture;
973     if (captureConfig_.audioInfo.innerCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID) {
974         MediaTrace trace("ScreenCaptureServer::StartFileInnerAudioCaptureInner");
975         innerCapture = std::make_shared<AudioCapturerWrapper>(captureConfig_.audioInfo.innerCapInfo, screenCaptureCb_,
976             std::string("OS_InnerAudioCapture"), contentFilter_);
977         int32_t ret = innerCapture->Start(appInfo_);
978         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "StartFileInnerAudioCapture failed");
979         if (isMicrophoneOn_ && audioSource_ && audioSource_->GetSpeakerAliveStatus() &&
980             !audioSource_->GetIsInVoIPCall()) {
981             ret = innerCapture->Pause();
982             CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "StartAudioCapture innerCapture Pause failed");
983         }
984     }
985     innerAudioCapture_ = innerCapture;
986     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartFileInnerAudioCapture OK.", FAKE_POINTER(this));
987     return MSERR_OK;
988 }
989 
StartFileMicAudioCapture()990 int32_t ScreenCaptureServer::StartFileMicAudioCapture()
991 {
992     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartFileMicAudioCapture start, dataType:%{public}d, "
993         "micCapInfo.state:%{public}d.",
994         FAKE_POINTER(this), captureConfig_.dataType, captureConfig_.audioInfo.micCapInfo.state);
995     std::shared_ptr<AudioCapturerWrapper> micCapture;
996     if (captureConfig_.audioInfo.micCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID) {
997         MediaTrace trace("ScreenCaptureServer::StartFileMicAudioCaptureInner");
998         ScreenCaptureContentFilter contentFilterMic;
999         micCapture = std::make_shared<AudioCapturerWrapper>(captureConfig_.audioInfo.micCapInfo, screenCaptureCb_,
1000             std::string("OS_MicAudioCapture"), contentFilterMic);
1001         if (audioSource_) {
1002             micCapture->SetIsInVoIPCall(audioSource_->GetIsInVoIPCall());
1003         }
1004         int32_t ret = micCapture->Start(appInfo_);
1005         if (ret != MSERR_OK) {
1006             MEDIA_LOGE("StartFileMicAudioCapture micCapture failed");
1007             isMicrophoneOn_ = false;
1008             screenCaptureCb_->OnStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_MIC_UNAVAILABLE);
1009             return ret;
1010         }
1011     }
1012     micAudioCapture_ = micCapture;
1013     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartFileMicAudioCapture OK.", FAKE_POINTER(this));
1014     return MSERR_OK;
1015 }
1016 
StartScreenCaptureStream()1017 int32_t ScreenCaptureServer::StartScreenCaptureStream()
1018 {
1019     MediaTrace trace("ScreenCaptureServer::StartScreenCaptureStream");
1020     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartScreenCaptureStream start, dataType:%{public}d.",
1021         FAKE_POINTER(this), captureConfig_.dataType);
1022     CHECK_AND_RETURN_RET(captureConfig_.dataType == DataType::ORIGINAL_STREAM, MSERR_INVALID_OPERATION);
1023     int32_t ret = StartAudioCapture();
1024     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "StartAudioCapture failed, ret:%{public}d, dataType:%{public}d",
1025         ret, captureConfig_.dataType);
1026 
1027     ret = StartVideoCapture();
1028     if (ret != MSERR_OK) {
1029         StopAudioCapture();
1030         MEDIA_LOGE("StartScreenCaptureStream failed");
1031         return ret;
1032     }
1033     MEDIA_LOGI("StartScreenCaptureStream success");
1034     return ret;
1035 }
1036 
StartScreenCaptureFile()1037 int32_t ScreenCaptureServer::StartScreenCaptureFile()
1038 {
1039     CHECK_AND_RETURN_RET(captureConfig_.dataType == DataType::CAPTURE_FILE, MSERR_INVALID_OPERATION);
1040 
1041     MEDIA_LOGI("StartScreenCaptureFile S");
1042     int32_t ret = InitRecorder();
1043     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "InitRecorder failed, ret:%{public}d, dataType:%{public}d",
1044         ret, captureConfig_.dataType);
1045 
1046     ON_SCOPE_EXIT(0) {
1047         if (recorder_ != nullptr) {
1048             recorder_->Release();
1049             recorder_ = nullptr;
1050             consumer_ = nullptr;
1051         }
1052     };
1053     std::string virtualScreenName = "screen_capture_file";
1054     ret = CreateVirtualScreen(virtualScreenName, consumer_);
1055     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "CreateVirtualScreen failed, ret:%{public}d, dataType:%{public}d",
1056         ret, captureConfig_.dataType);
1057 
1058     ON_SCOPE_EXIT(1) {
1059         DestroyVirtualScreen();
1060     };
1061 
1062     if (isMicrophoneOn_) {
1063         int32_t retMic = StartFileMicAudioCapture();
1064         if (retMic != MSERR_OK) {
1065             MEDIA_LOGE("StartScreenCaptureFile StartFileMicAudioCapture failed");
1066         }
1067     }
1068     int32_t retInner = StartFileInnerAudioCapture();
1069     CHECK_AND_RETURN_RET_LOG(retInner == MSERR_OK, retInner, "StartFileInnerAudioCapture failed, ret:%{public}d,"
1070         "dataType:%{public}d", retInner, captureConfig_.dataType);
1071     MEDIA_LOGI("StartScreenCaptureFile RecorderServer S");
1072     ret = recorder_->Start();
1073     if (ret != MSERR_OK) {
1074         StopAudioCapture();
1075         MEDIA_LOGE("StartScreenCaptureFile recorder start failed");
1076     }
1077     MEDIA_LOGI("StartScreenCaptureFile RecorderServer E");
1078     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "recorder failed, ret:%{public}d, dataType:%{public}d",
1079         ret, captureConfig_.dataType);
1080     CANCEL_SCOPE_EXIT_GUARD(1);
1081     CANCEL_SCOPE_EXIT_GUARD(0);
1082 
1083     MEDIA_LOGI("StartScreenCaptureFile E");
1084     return ret;
1085 }
1086 
OnStartScreenCapture()1087 int32_t ScreenCaptureServer::OnStartScreenCapture()
1088 {
1089     MediaTrace trace("ScreenCaptureServer::OnStartScreenCapture");
1090     MEDIA_LOGI("OnStartScreenCapture start, dataType:%{public}d", captureConfig_.dataType);
1091     int32_t ret = MSERR_UNSUPPORT;
1092     if (captureConfig_.dataType == DataType::ORIGINAL_STREAM) {
1093         ret = StartScreenCaptureStream();
1094     } else if (captureConfig_.dataType == DataType::CAPTURE_FILE) {
1095         ret = StartScreenCaptureFile();
1096     }
1097     if (ret == MSERR_OK) {
1098         int64_t endTime = GetCurrentMillisecond();
1099         statisticalEventInfo_.startLatency = static_cast<int32_t>(endTime - startTime_);
1100         MEDIA_LOGI("OnStartScreenCapture start success, dataType:%{public}d", captureConfig_.dataType);
1101     } else {
1102         MEDIA_LOGE("OnStartScreenCapture start failed, dataType:%{public}d", captureConfig_.dataType);
1103         statisticalEventInfo_.startLatency = -1; // latency -1 means invalid
1104     }
1105     return ret;
1106 }
1107 
ResSchedReportData(int64_t value,std::unordered_map<std::string,std::string> payload)1108 void ScreenCaptureServer::ResSchedReportData(int64_t value, std::unordered_map<std::string, std::string> payload)
1109 {
1110     payload["uid"] = std::to_string(appInfo_.appUid);
1111     payload["pid"] = std::to_string(appInfo_.appPid);
1112     uint32_t type = ResourceSchedule::ResType::RES_TYPE_REPORT_SCREEN_CAPTURE;
1113     ResourceSchedule::ResSchedClient::GetInstance().ReportData(type, value, payload);
1114 }
1115 
RegisterPrivateWindowListener()1116 void ScreenCaptureServer::RegisterPrivateWindowListener()
1117 {
1118     std::weak_ptr<ScreenCaptureServer> screenCaptureServer(shared_from_this());
1119     displayListener_ = new PrivateWindowListenerInScreenCapture(screenCaptureServer);
1120     DisplayManager::GetInstance().RegisterPrivateWindowListener(displayListener_);
1121 }
PostStartScreenCaptureSuccessAction()1122 void ScreenCaptureServer::PostStartScreenCaptureSuccessAction()
1123 {
1124     std::unordered_map<std::string, std::string> payload;
1125     int64_t value = ResourceSchedule::ResType::ScreenCaptureStatus::START_SCREEN_CAPTURE;
1126     ResSchedReportData(value, payload);
1127     captureState_ = AVScreenCaptureState::STARTED;
1128     ScreenCaptureMonitorServer::GetInstance()->CallOnScreenCaptureStarted(appInfo_.appPid);
1129     screenCaptureCb_->OnStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_STARTED);
1130 }
1131 
PostStartScreenCapture(bool isSuccess)1132 void ScreenCaptureServer::PostStartScreenCapture(bool isSuccess)
1133 {
1134     MediaTrace trace("ScreenCaptureServer::PostStartScreenCapture.");
1135     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " PostStartScreenCapture start, isSuccess:%{public}s, "
1136         "dataType:%{public}d.", FAKE_POINTER(this), isSuccess ? "true" : "false", captureConfig_.dataType);
1137     if (isSuccess) {
1138         MEDIA_LOGI("PostStartScreenCapture handle success");
1139 #ifdef SUPPORT_SCREEN_CAPTURE_WINDOW_NOTIFICATION
1140         if (isPrivacyAuthorityEnabled_ &&
1141             GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"]
1142                 .compare(appName_) != 0) {
1143             int32_t tryTimes = TryStartNotification();
1144             if (tryTimes > NOTIFICATION_MAX_TRY_NUM) {
1145                 captureState_ = AVScreenCaptureState::STARTED;
1146                 screenCaptureCb_->OnError(ScreenCaptureErrorType::SCREEN_CAPTURE_ERROR_INTERNAL,
1147                     AVScreenCaptureErrorCode::SCREEN_CAPTURE_ERR_UNKNOWN);
1148                 StopScreenCaptureInner(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INVLID);
1149                 return;
1150             }
1151         }
1152 #endif
1153         if (!UpdatePrivacyUsingPermissionState(START_VIDEO)) {
1154             MEDIA_LOGE("UpdatePrivacyUsingPermissionState START failed, dataType:%{public}d", captureConfig_.dataType);
1155             captureState_ = AVScreenCaptureState::STARTED;
1156             screenCaptureCb_->OnError(ScreenCaptureErrorType::SCREEN_CAPTURE_ERROR_INTERNAL,
1157                 AVScreenCaptureErrorCode::SCREEN_CAPTURE_ERR_UNKNOWN);
1158             StopScreenCaptureInner(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INVLID);
1159             return;
1160         }
1161         PostStartScreenCaptureSuccessAction();
1162     } else {
1163         MEDIA_LOGE("PostStartScreenCapture handle failure");
1164         if (isPrivacyAuthorityEnabled_) {
1165             screenCaptureCb_->OnError(ScreenCaptureErrorType::SCREEN_CAPTURE_ERROR_INTERNAL,
1166                 AVScreenCaptureErrorCode::SCREEN_CAPTURE_ERR_UNKNOWN);
1167         }
1168         isPrivacyAuthorityEnabled_ = false;
1169         isSurfaceMode_ = false;
1170         captureState_ = AVScreenCaptureState::STOPPED;
1171         SetErrorInfo(MSERR_UNKNOWN, "PostStartScreenCapture handle failure",
1172             StopReason::POST_START_SCREENCAPTURE_HANDLE_FAILURE, IsUserPrivacyAuthorityNeeded());
1173         return;
1174     }
1175     {
1176         std::lock_guard<std::mutex> lock(mutexGlobal_);
1177         activeSessionId_.store(sessionId_);
1178     }
1179     RegisterPrivateWindowListener();
1180     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " PostStartScreenCapture end.", FAKE_POINTER(this));
1181 }
1182 
1183 #ifdef SUPPORT_SCREEN_CAPTURE_WINDOW_NOTIFICATION
TryStartNotification()1184 int32_t ScreenCaptureServer::TryStartNotification()
1185 {
1186     int32_t tryTimes;
1187     for (tryTimes = 1; tryTimes <= NOTIFICATION_MAX_TRY_NUM; tryTimes++) {
1188         int32_t ret = StartNotification();
1189         if (ret == MSERR_OK) {
1190             break;
1191         }
1192     }
1193     return tryTimes;
1194 }
1195 #endif
1196 
InitAudioCap(AudioCaptureInfo audioInfo)1197 int32_t ScreenCaptureServer::InitAudioCap(AudioCaptureInfo audioInfo)
1198 {
1199     MediaTrace trace("ScreenCaptureServer::InitAudioCap");
1200     std::lock_guard<std::mutex> lock(mutex_);
1201     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " InitAudioCap start, audioChannels:%{public}d, "
1202         "audioSampleRate:%{public}d, audioSource:%{public}d, state:%{public}d.", FAKE_POINTER(this),
1203         audioInfo.audioChannels, audioInfo.audioSampleRate, audioInfo.audioSource, audioInfo.state);
1204     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::CREATED, MSERR_INVALID_OPERATION,
1205         "InitAudioCap failed, capture is not CREATED, state:%{public}d", captureState_);
1206 
1207     int ret = CheckAudioCapInfo(audioInfo);
1208     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "InitAudioCap CheckAudioCapInfo failed, audioSource:%{public}d",
1209         audioInfo.audioSource);
1210     if (audioInfo.audioSource == AudioCaptureSourceType::SOURCE_DEFAULT ||
1211         audioInfo.audioSource == AudioCaptureSourceType::MIC) {
1212         captureConfig_.audioInfo.micCapInfo = audioInfo;
1213         statisticalEventInfo_.requireMic = true;
1214     } else if (audioInfo.audioSource == AudioCaptureSourceType::ALL_PLAYBACK ||
1215         audioInfo.audioSource == AudioCaptureSourceType::APP_PLAYBACK) {
1216         captureConfig_.audioInfo.innerCapInfo = audioInfo;
1217         avType_ = (avType_ == AVScreenCaptureAvType::INVALID_TYPE) ? AVScreenCaptureAvType::AUDIO_TYPE :
1218             AVScreenCaptureAvType::AV_TYPE;
1219     }
1220     MEDIA_LOGI("InitAudioCap success sampleRate:%{public}d, channels:%{public}d, source:%{public}d, state:%{public}d",
1221         audioInfo.audioSampleRate, audioInfo.audioChannels, audioInfo.audioSource, audioInfo.state);
1222     return MSERR_OK;
1223 }
1224 
InitVideoCap(VideoCaptureInfo videoInfo)1225 int32_t ScreenCaptureServer::InitVideoCap(VideoCaptureInfo videoInfo)
1226 {
1227     MediaTrace trace("ScreenCaptureServer::InitVideoCap");
1228     std::lock_guard<std::mutex> lock(mutex_);
1229     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::CREATED, MSERR_INVALID_OPERATION,
1230         "InitVideoCap failed, capture is not CREATED, state:%{public}d", captureState_);
1231 
1232     int ret = CheckVideoCapInfo(videoInfo);
1233     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "InitVideoCap CheckVideoCapInfo failed");
1234     captureConfig_.videoInfo.videoCapInfo = videoInfo;
1235     avType_ = (avType_ == AVScreenCaptureAvType::AUDIO_TYPE) ? AVScreenCaptureAvType::AV_TYPE :
1236         AVScreenCaptureAvType::VIDEO_TYPE;
1237     statisticalEventInfo_.videoResolution = std::to_string(videoInfo.videoFrameWidth) + " * " +
1238         std::to_string(videoInfo.videoFrameHeight);
1239     MEDIA_LOGI("InitVideoCap success width:%{public}d, height:%{public}d, source:%{public}d, state:%{public}d",
1240         videoInfo.videoFrameWidth, videoInfo.videoFrameHeight, videoInfo.videoSource, videoInfo.state);
1241     return MSERR_OK;
1242 }
1243 
InitRecorderInfo(std::shared_ptr<IRecorderService> & recorder,AudioCaptureInfo audioInfo)1244 int32_t ScreenCaptureServer::InitRecorderInfo(std::shared_ptr<IRecorderService> &recorder, AudioCaptureInfo audioInfo)
1245 {
1246     int32_t ret = MSERR_OK;
1247     if (captureConfig_.videoInfo.videoCapInfo.state != AVScreenCaptureParamValidationState::VALIDATION_IGNORE) {
1248         ret = recorder_->SetVideoSource(captureConfig_.videoInfo.videoCapInfo.videoSource, videoSourceId_);
1249         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetVideoSource failed");
1250     }
1251     ret = recorder->SetOutputFormat(fileFormat_); // Change to REC_CONFIGURED
1252     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetOutputFormat failed");
1253     ret = recorder->SetAudioEncoder(audioSourceId_, captureConfig_.audioInfo.audioEncInfo.audioCodecformat);
1254     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetAudioEncoder failed");
1255     ret = recorder->SetAudioSampleRate(audioSourceId_, audioInfo.audioSampleRate);
1256     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetAudioSampleRate failed");
1257     ret = recorder->SetAudioChannels(audioSourceId_, audioInfo.audioChannels);
1258     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetAudioChannels failed");
1259     ret = recorder->SetAudioEncodingBitRate(audioSourceId_, captureConfig_.audioInfo.audioEncInfo.audioBitrate);
1260     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetAudioEncodingBitRate failed");
1261     if (captureConfig_.videoInfo.videoCapInfo.state != AVScreenCaptureParamValidationState::VALIDATION_IGNORE) {
1262         ret = recorder->SetVideoEncoder(videoSourceId_, captureConfig_.videoInfo.videoEncInfo.videoCodec);
1263         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetVideoEncoder failed");
1264         ret = recorder->SetVideoSize(videoSourceId_, captureConfig_.videoInfo.videoCapInfo.videoFrameWidth,
1265             captureConfig_.videoInfo.videoCapInfo.videoFrameHeight);
1266         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetVideoSize failed");
1267         ret = recorder->SetVideoFrameRate(videoSourceId_, captureConfig_.videoInfo.videoEncInfo.videoFrameRate);
1268         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetVideoFrameRate failed");
1269         ret = recorder->SetVideoEncodingBitRate(videoSourceId_, captureConfig_.videoInfo.videoEncInfo.videoBitrate);
1270         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetVideoEncodingBitRate failed");
1271     }
1272     return MSERR_OK;
1273 }
1274 
InitRecorder()1275 int32_t ScreenCaptureServer::InitRecorder()
1276 {
1277     CHECK_AND_RETURN_RET_LOG(outputFd_ > 0, MSERR_INVALID_OPERATION, "the outputFd is invalid");
1278     MEDIA_LOGI("InitRecorder start");
1279     MediaTrace trace("ScreenCaptureServer::InitRecorder");
1280     recorder_ = Media::RecorderServer::Create();
1281     CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_UNKNOWN, "init Recoder failed");
1282     ON_SCOPE_EXIT(0) {
1283         recorder_->Release();
1284     };
1285     int32_t ret;
1286     AudioCaptureInfo audioInfo;
1287     if (captureConfig_.audioInfo.innerCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID &&
1288         captureConfig_.audioInfo.micCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID) {
1289         MEDIA_LOGI("InitRecorder prepare to SetAudioDataSource");
1290         audioInfo = captureConfig_.audioInfo.innerCapInfo;
1291         audioSource_ = std::make_unique<AudioDataSource>(AVScreenCaptureMixMode::MIX_MODE, this);
1292         captureCallback_ = std::make_shared<ScreenRendererAudioStateChangeCallback>();
1293         audioSource_->SetAppPid(appInfo_.appPid);
1294         audioSource_->SetAppName(appName_);
1295         captureCallback_->SetAppName(appName_);
1296         captureCallback_->SetAudioSource(audioSource_);
1297         audioSource_->RegisterAudioRendererEventListener(appInfo_.appPid, captureCallback_);
1298         ret = recorder_->SetAudioDataSource(audioSource_, audioSourceId_);
1299         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetAudioDataSource failed");
1300     } else if (captureConfig_.audioInfo.innerCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID) {
1301         audioInfo = captureConfig_.audioInfo.innerCapInfo;
1302         MEDIA_LOGI("InitRecorder prepare to SetAudioSource inner");
1303         audioSource_ = std::make_unique<AudioDataSource>(AVScreenCaptureMixMode::INNER_MODE, this);
1304         ret = recorder_->SetAudioDataSource(audioSource_, audioSourceId_);
1305         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetAudioDataSource failed");
1306     } else if (captureConfig_.audioInfo.micCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID) {
1307         audioInfo = captureConfig_.audioInfo.micCapInfo;
1308         MEDIA_LOGI("InitRecorder prepare to SetAudioSource mic");
1309         audioSource_ = std::make_unique<AudioDataSource>(AVScreenCaptureMixMode::MIC_MODE, this);
1310         ret = recorder_->SetAudioDataSource(audioSource_, audioSourceId_);
1311         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetAudioDataSource failed");
1312     } else {
1313         MEDIA_LOGE("InitRecorder not VALIDATION_VALID");
1314         return MSERR_UNKNOWN;
1315     }
1316     MEDIA_LOGI("InitRecorder recorder SetAudioDataSource ret:%{public}d", ret);
1317     ret = InitRecorderInfo(recorder_, audioInfo);
1318     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "InitRecorderInfo failed");
1319     ret = recorder_->SetOutputFile(outputFd_);
1320     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetOutputFile failed");
1321     ret = recorder_->Prepare();
1322     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "recorder Prepare failed");
1323     if (captureConfig_.videoInfo.videoCapInfo.state != AVScreenCaptureParamValidationState::VALIDATION_IGNORE) {
1324         consumer_ = recorder_->GetSurface(videoSourceId_);
1325         CHECK_AND_RETURN_RET_LOG(consumer_ != nullptr, MSERR_UNKNOWN, "recorder GetSurface failed");
1326     }
1327     CANCEL_SCOPE_EXIT_GUARD(0);
1328     MEDIA_LOGI("InitRecorder success");
1329     return MSERR_OK;
1330 }
1331 
UpdatePrivacyUsingPermissionState(VideoPermissionState state)1332 bool ScreenCaptureServer::UpdatePrivacyUsingPermissionState(VideoPermissionState state)
1333 {
1334     MediaTrace trace("ScreenCaptureServer::UpdatePrivacyUsingPermissionState");
1335     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " UpdatePrivacyUsingPermissionState start, "
1336         "state: %{public}d, uid: %{public}d", FAKE_POINTER(this), state, appInfo_.appUid);
1337     if (!IsUserPrivacyAuthorityNeeded()) {
1338         MEDIA_LOGI("Using Permission Ignored. state: %{public}d, uid: %{public}d", state, appInfo_.appUid);
1339         return true;
1340     }
1341 
1342     int res = 0;
1343     if (state == START_VIDEO) {
1344         res = PrivacyKit::StartUsingPermission(appInfo_.appTokenId, "ohos.permission.CAPTURE_SCREEN");
1345         if (res != 0) {
1346             MEDIA_LOGE("start using perm error");
1347             return false;
1348         }
1349         res = PrivacyKit::AddPermissionUsedRecord(appInfo_.appTokenId, "ohos.permission.CAPTURE_SCREEN", 1, 0);
1350         if (res != 0) {
1351             MEDIA_LOGE("add screen capture record error: %{public}d", res);
1352             return false;
1353         }
1354     } else if (state == STOP_VIDEO) {
1355         res = PrivacyKit::StopUsingPermission(appInfo_.appTokenId, "ohos.permission.CAPTURE_SCREEN");
1356         if (res != 0) {
1357             MEDIA_LOGE("stop using perm error");
1358             return false;
1359         }
1360     }
1361     return true;
1362 }
1363 
SystemRecorderInterruptLatestRecorder()1364 void ScreenCaptureServer::SystemRecorderInterruptLatestRecorder()
1365 {
1366     std::shared_ptr<ScreenCaptureServer> latestServer;
1367     int currentSessionId = -1;
1368     {
1369         std::lock_guard <std::mutex> lock(mutexGlobal_);
1370         currentSessionId = activeSessionId_.load();
1371     }
1372     if (currentSessionId >= 0) {
1373         {
1374             std::lock_guard<std::mutex> lock(mutexGlobal_);
1375             latestServer = GetScreenCaptureServerByIdWithLock(activeSessionId_.load());
1376         }
1377         if (latestServer != nullptr && sessionId_ != activeSessionId_.load()) {
1378             MEDIA_LOGW("SystemRecorderInterruptLatestRecorder uid(%{public}d) is interrupted by uid(%{public}d)",
1379                 latestServer->appInfo_.appUid, this->appInfo_.appUid);
1380             latestServer->StopScreenCaptureByEvent(
1381                 AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INTERRUPTED_BY_OTHER);
1382             {
1383                 std::lock_guard <std::mutex> lock(mutexGlobal_);
1384                 activeSessionId_.store(SESSION_ID_INVALID);
1385             }
1386         } else {
1387             MEDIA_LOGE("Interrupt Failed old uid(%{public}d) sid(%{public}d), new uid(%{public}d) sid(%{public}d) ",
1388                 latestServer->appInfo_.appUid, currentSessionId, this->appInfo_.appUid, sessionId_);
1389         }
1390     }
1391 }
1392 
StartScreenCaptureInner(bool isPrivacyAuthorityEnabled)1393 int32_t ScreenCaptureServer::StartScreenCaptureInner(bool isPrivacyAuthorityEnabled)
1394 {
1395     MEDIA_LOGI("StartScreenCaptureInner S, appUid:%{public}d, appPid:%{public}d, isPrivacyAuthorityEnabled:%{public}d"
1396         ", isSurfaceMode:%{public}d, dataType:%{public}d", appInfo_.appUid, appInfo_.appPid, isPrivacyAuthorityEnabled,
1397         isSurfaceMode_, captureConfig_.dataType);
1398     MediaTrace trace("ScreenCaptureServer::StartScreenCaptureInner");
1399     int32_t ret = RegisterServerCallbacks();
1400     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "RegisterServerCallbacks failed");
1401 
1402     ret = CheckAllParams();
1403     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "StartScreenCaptureInner failed, invalid params");
1404 
1405     sptr<Rosen::Display> display = Rosen::DisplayManager::GetInstance().GetDefaultDisplaySync();
1406     CHECK_AND_RETURN_RET_LOG(display != nullptr, MSERR_UNKNOWN, "GetDefaultDisplaySync failed");
1407     density_ = display->GetDpi();
1408 
1409     appName_ = GetClientBundleName(appInfo_.appUid);
1410 
1411     isPrivacyAuthorityEnabled_ = isPrivacyAuthorityEnabled;
1412     captureState_ = AVScreenCaptureState::STARTING;
1413     ret = RequestUserPrivacyAuthority();
1414     if (ret != MSERR_OK) {
1415         captureState_ = AVScreenCaptureState::STOPPED;
1416         SetErrorInfo(ret, "StartScreenCaptureInner RequestUserPrivacyAuthority failed",
1417             StopReason::REQUEST_USER_PRIVACY_AUTHORITY_FAILED, IsUserPrivacyAuthorityNeeded());
1418         MEDIA_LOGE("StartScreenCaptureInner RequestUserPrivacyAuthority failed");
1419         return ret;
1420     }
1421 
1422     if (IsUserPrivacyAuthorityNeeded()) {
1423         if (isPrivacyAuthorityEnabled_ &&
1424             GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"]
1425                 .compare(appName_) != 0) {
1426             MEDIA_LOGI("Wait for user interactions to ALLOW/DENY capture");
1427             return MSERR_OK;
1428         } else {
1429             // system rec Interrupt at here, 3rd rec interrupt at ReportAVScreenCaptureUserChoice
1430             SystemRecorderInterruptLatestRecorder();
1431         }
1432         MEDIA_LOGI("privacy notification window not support, app has CAPTURE_SCREEN permission and go on");
1433     } else {
1434         MEDIA_LOGI("Privacy Authority granted automatically and go on"); // for root
1435     }
1436 
1437     ret = OnStartScreenCapture();
1438     PostStartScreenCapture(ret == MSERR_OK);
1439 
1440     MEDIA_LOGI("StartScreenCaptureInner E, appUid:%{public}d, appPid:%{public}d", appInfo_.appUid, appInfo_.appPid);
1441     return ret;
1442 }
1443 
IsTelInCallSkipList()1444 bool ScreenCaptureServer::IsTelInCallSkipList()
1445 {
1446     MEDIA_LOGI("ScreenCaptureServer::IsTelInCallSkipList isCalledBySystemApp : %{public}d", isCalledBySystemApp_);
1447     if (isCalledBySystemApp_ &&
1448         GetScreenCaptureSystemParam()["const.multimedia.screencapture.hiviewcarebundlename"]
1449             .compare(appName_) == 0) {
1450         MEDIA_LOGI("ScreenCaptureServer::IsTelInCallSkipList true");
1451         return true;
1452     }
1453     return false;
1454 }
1455 
RegisterServerCallbacks()1456 int32_t ScreenCaptureServer::RegisterServerCallbacks()
1457 {
1458     uint64_t tokenId = IPCSkeleton::GetCallingFullTokenID();
1459     isCalledBySystemApp_ = OHOS::Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(tokenId);
1460     MEDIA_LOGI("ScreenCaptureServer::RegisterServerCallbacks isCalledBySystemApp : %{public}d", isCalledBySystemApp_);
1461     std::weak_ptr<ScreenCaptureServer> wpScreenCaptureServer(shared_from_this());
1462     screenCaptureObserverCb_ = std::make_shared<ScreenCaptureObserverCallBack>(wpScreenCaptureServer);
1463     if (InCallObserver::GetInstance().IsInCall() && !IsTelInCallSkipList()) {
1464         MEDIA_LOGI("ScreenCaptureServer Start InCall Abort");
1465         screenCaptureCb_->OnStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_STOPPED_BY_CALL);
1466         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_UNSUPPORT,
1467             "ScreenCaptureServer Start InCall Abort");
1468         return MSERR_UNSUPPORT;
1469     } else {
1470         MEDIA_LOGI("ScreenCaptureServer Start RegisterScreenCaptureCallBack");
1471         InCallObserver::GetInstance().RegisterInCallObserverCallBack(screenCaptureObserverCb_);
1472     }
1473     AccountObserver::GetInstance().RegisterAccountObserverCallBack(screenCaptureObserverCb_);
1474     return MSERR_OK;
1475 }
1476 
StartPrivacyWindow()1477 int32_t ScreenCaptureServer::StartPrivacyWindow()
1478 {
1479     auto bundleName = GetClientBundleName(appInfo_.appUid);
1480     callingLabel_ = GetBundleResourceLabel(bundleName);
1481 
1482     std::string comStr = "{\"ability.want.params.uiExtensionType\":\"sys/commonUI\",\"sessionId\":\"";
1483     comStr += std::to_string(sessionId_).c_str();
1484     comStr += "\",\"callerUid\":\"";
1485     comStr += std::to_string(appInfo_.appUid).c_str();
1486     comStr += "\",\"appLabel\":\"";
1487     comStr += callingLabel_.c_str();
1488     comStr += "\"}";
1489 
1490     AAFwk::Want want;
1491     ErrCode ret = ERR_INVALID_VALUE;
1492 #ifdef PC_STANDARD
1493     if (captureConfig_.captureMode == CAPTURE_HOME_SCREEN) {
1494         want.SetElementName(GetScreenCaptureSystemParam()["const.multimedia.screencapture.dialogconnectionbundlename"],
1495             GetScreenCaptureSystemParam()["const.multimedia.screencapture.dialogconnectionabilityname"]);
1496         auto connection_ = sptr<UIExtensionAbilityConnection>(new (std::nothrow) UIExtensionAbilityConnection(comStr));
1497         ret = OHOS::AAFwk::ExtensionManagerClient::GetInstance().ConnectServiceExtensionAbility(want, connection_,
1498             nullptr, -1);
1499         MEDIA_LOGI("ConnectServiceExtensionAbility end %{public}d, DeviceType : PC", ret);
1500     } else if (captureConfig_.captureMode != CAPTURE_INVAILD) {
1501         AppExecFwk::ElementName element("",
1502             GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"],
1503             SELECT_ABILITY_NAME); // DeviceID
1504         want.SetElement(element);
1505         want.SetParam("params", comStr);
1506         want.SetParam("appLabel", callingLabel_);
1507         ret = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
1508         MEDIA_LOGI("StartAbility end %{public}d, DeviceType : PC", ret);
1509     }
1510 #else
1511     want.SetElementName(GetScreenCaptureSystemParam()["const.multimedia.screencapture.dialogconnectionbundlename"],
1512                         GetScreenCaptureSystemParam()["const.multimedia.screencapture.dialogconnectionabilityname"]);
1513     auto connection_ = sptr<UIExtensionAbilityConnection>(new (std::nothrow) UIExtensionAbilityConnection(comStr));
1514     ret = OHOS::AAFwk::ExtensionManagerClient::GetInstance().ConnectServiceExtensionAbility(want, connection_,
1515         nullptr, -1);
1516     MEDIA_LOGI("ConnectServiceExtensionAbility end %{public}d, Device : Phone", ret);
1517 #endif
1518     return ret;
1519 }
1520 
StartNotification()1521 int32_t ScreenCaptureServer::StartNotification()
1522 {
1523     int32_t result = NotificationHelper::SubscribeLocalLiveViewNotification(NOTIFICATION_SUBSCRIBER);
1524     MEDIA_LOGD("Screencapture service PublishNotification, result %{public}d", result);
1525     NotificationRequest request;
1526     localLiveViewContent_ = GetLocalLiveViewContent();
1527 
1528     std::shared_ptr<NotificationContent> content =
1529         std::make_shared<NotificationContent>(localLiveViewContent_);
1530 
1531     request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
1532     notificationId_ = sessionId_;
1533     request.SetNotificationId(notificationId_);
1534     request.SetContent(content);
1535     request.SetCreatorUid(AV_SCREEN_CAPTURE_SESSION_UID);
1536     request.SetOwnerUid(AV_SCREEN_CAPTURE_SESSION_UID);
1537     request.SetUnremovable(true);
1538     request.SetInProgress(true);
1539 
1540     std::shared_ptr<PixelMap> pixelMapTotalSpr = GetPixelMap(ICON_PATH_NOTIFICATION);
1541     request.SetLittleIcon(pixelMapTotalSpr);
1542     request.SetBadgeIconStyle(NotificationRequest::BadgeStyle::LITTLE);
1543 
1544     result = NotificationHelper::PublishNotification(request);
1545     MEDIA_LOGI("Screencapture service PublishNotification uid %{public}d, result %{public}d",
1546         AV_SCREEN_CAPTURE_SESSION_UID, result);
1547     return result;
1548 }
1549 
GetLocalLiveViewContent()1550 std::shared_ptr<NotificationLocalLiveViewContent> ScreenCaptureServer::GetLocalLiveViewContent()
1551 {
1552     std::shared_ptr<NotificationLocalLiveViewContent> localLiveViewContent =
1553         std::make_shared<NotificationLocalLiveViewContent>();
1554     localLiveViewContent->SetType(1);
1555     liveViewText_ = "\"";
1556     liveViewText_ += callingLabel_.c_str();
1557     liveViewText_ += "\"正在使用屏幕";
1558     localLiveViewContent->SetText(liveViewText_);
1559 
1560     auto capsule = NotificationCapsule();
1561     capsule.SetBackgroundColor(BACK_GROUND_COLOR);
1562     capsulePxSize_ = capsuleVpSize_ * density_ / MDPI;
1563     std::shared_ptr<PixelMap> pixelMapCapSpr = GetPixelMapSvg(ICON_PATH_CAPSULE_STOP, capsulePxSize_, capsulePxSize_);
1564     capsule.SetIcon(pixelMapCapSpr);
1565 
1566     localLiveViewContent->SetCapsule(capsule);
1567     localLiveViewContent->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::CAPSULE);
1568 
1569     auto countTime = NotificationTime();
1570     countTime.SetInitialTime(1);
1571     countTime.SetIsCountDown(false);
1572     countTime.SetIsPaused(false);
1573     countTime.SetIsInTitle(true);
1574 
1575     localLiveViewContent->SetTime(countTime);
1576     localLiveViewContent->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::TIME);
1577 
1578     auto basicButton = NotificationLocalLiveViewButton();
1579     basicButton.addSingleButtonName(BUTTON_NAME_STOP);
1580     std::shared_ptr<PixelMap> pixelMapStopSpr = GetPixelMap(ICON_PATH_STOP);
1581     basicButton.addSingleButtonIcon(pixelMapStopSpr);
1582 
1583     localLiveViewContent->SetButton(basicButton);
1584     localLiveViewContent->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::BUTTON);
1585     return localLiveViewContent;
1586 }
1587 
GetPixelMap(std::string path)1588 std::shared_ptr<PixelMap> ScreenCaptureServer::GetPixelMap(std::string path)
1589 {
1590     uint32_t errorCode = 0;
1591     SourceOptions opts;
1592     opts.formatHint = "image/png";
1593     std::unique_ptr<ImageSource> imageSource =
1594         ImageSource::CreateImageSource(path, opts, errorCode);
1595     DecodeOptions decodeOpts;
1596     std::unique_ptr<PixelMap> pixelMap = imageSource->CreatePixelMap(decodeOpts, errorCode);
1597     std::shared_ptr<PixelMap> pixelMapSpr = std::move(pixelMap);
1598     return pixelMapSpr;
1599 }
1600 
GetPixelMapSvg(std::string path,int32_t width,int32_t height)1601 std::shared_ptr<PixelMap> ScreenCaptureServer::GetPixelMapSvg(std::string path, int32_t width, int32_t height)
1602 {
1603     uint32_t errorCode = 0;
1604     SourceOptions opts;
1605     opts.formatHint = "image/svg+xml";
1606     std::unique_ptr<ImageSource> imageSource =
1607         ImageSource::CreateImageSource(path, opts, errorCode);
1608     DecodeOptions decodeOpts;
1609     decodeOpts.desiredSize.width = width;
1610     decodeOpts.desiredSize.height = height;
1611     std::unique_ptr<PixelMap> pixelMap = imageSource->CreatePixelMap(decodeOpts, errorCode);
1612     std::shared_ptr<PixelMap> pixelMapSpr = std::move(pixelMap);
1613     return pixelMapSpr;
1614 }
1615 
UpdateMicrophoneEnabled()1616 void ScreenCaptureServer::UpdateMicrophoneEnabled()
1617 {
1618     UpdateLiveViewContent();
1619     NotificationRequest request;
1620 
1621     std::shared_ptr<NotificationContent> content =
1622         std::make_shared<NotificationContent>(localLiveViewContent_);
1623 
1624     request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
1625     request.SetNotificationId(notificationId_);
1626     request.SetContent(content);
1627     request.SetCreatorUid(AV_SCREEN_CAPTURE_SESSION_UID);
1628     request.SetOwnerUid(AV_SCREEN_CAPTURE_SESSION_UID);
1629     request.SetUnremovable(true);
1630     request.SetInProgress(true);
1631 
1632     std::shared_ptr<PixelMap> pixelMapTotalSpr = GetPixelMap(ICON_PATH_CAPSULE_STOP);
1633     request.SetLittleIcon(pixelMapTotalSpr);
1634     request.SetBadgeIconStyle(NotificationRequest::BadgeStyle::LITTLE);
1635 
1636     int32_t result = NotificationHelper::PublishNotification(request);
1637     MEDIA_LOGI("Screencapture service UpdateMicrophoneEnabled uid %{public}d, result %{public}d",
1638         AV_SCREEN_CAPTURE_SESSION_UID, result);
1639     micCount_.store(micCount_.load() + 1);
1640 }
1641 
UpdateLiveViewContent()1642 void ScreenCaptureServer::UpdateLiveViewContent()
1643 {
1644     localLiveViewContent_->SetType(1);
1645     localLiveViewContent_->SetText(liveViewText_);
1646 
1647     auto capsule = NotificationCapsule();
1648     capsule.SetBackgroundColor(BACK_GROUND_COLOR);
1649     std::shared_ptr<PixelMap> pixelMapCapSpr = GetPixelMap(ICON_PATH_CAPSULE_STOP);
1650     capsule.SetIcon(pixelMapCapSpr);
1651 
1652     localLiveViewContent_->SetCapsule(capsule);
1653     localLiveViewContent_->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::CAPSULE);
1654 
1655     auto countTime = NotificationTime();
1656     countTime.SetIsCountDown(false);
1657     countTime.SetIsPaused(false);
1658     countTime.SetIsInTitle(true);
1659 
1660     localLiveViewContent_->SetTime(countTime);
1661     localLiveViewContent_->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::TIME);
1662 
1663     auto basicButton = NotificationLocalLiveViewButton();
1664     basicButton.addSingleButtonName(BUTTON_NAME_MIC);
1665     if (micCount_.load() % MICROPHONE_STATE_COUNT == MICROPHONE_OFF) {
1666         std::shared_ptr<PixelMap> pixelMapSpr = GetPixelMapSvg(ICON_PATH_MIC_OFF, SVG_HEIGHT, SVG_WIDTH);
1667         basicButton.addSingleButtonIcon(pixelMapSpr);
1668     } else {
1669         std::shared_ptr<PixelMap> pixelMapSpr = GetPixelMapSvg(ICON_PATH_MIC, SVG_HEIGHT, SVG_WIDTH);
1670         basicButton.addSingleButtonIcon(pixelMapSpr);
1671     }
1672 
1673     basicButton.addSingleButtonName(BUTTON_NAME_STOP);
1674     std::shared_ptr<PixelMap> pixelMapStopSpr = GetPixelMap(ICON_PATH_STOP);
1675     basicButton.addSingleButtonIcon(pixelMapStopSpr);
1676 
1677     localLiveViewContent_->SetButton(basicButton);
1678     localLiveViewContent_->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::BUTTON);
1679 }
1680 
GetDumpFlag()1681 void ScreenCaptureServer::GetDumpFlag()
1682 {
1683     const std::string dumpTag = "sys.media.screenCapture.dump.enable";
1684     std::string dumpEnable;
1685     int32_t dumpRes = OHOS::system::GetStringParameter(dumpTag, dumpEnable, "false");
1686     isDump_ = (dumpEnable == "true");
1687     MEDIA_LOGI("get dump flag, dumpRes: %{public}d, isDump_: %{public}d", dumpRes, isDump_);
1688 }
1689 
StartScreenCapture(bool isPrivacyAuthorityEnabled)1690 int32_t ScreenCaptureServer::StartScreenCapture(bool isPrivacyAuthorityEnabled)
1691 {
1692     MediaTrace trace("ScreenCaptureServer::StartScreenCapture");
1693     std::lock_guard<std::mutex> lock(mutex_);
1694     startTime_ = GetCurrentMillisecond();
1695     statisticalEventInfo_.enableMic = isMicrophoneOn_;
1696     GetDumpFlag();
1697     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartScreenCapture start, "
1698         "isPrivacyAuthorityEnabled:%{public}s, captureState:%{public}d.",
1699         FAKE_POINTER(this), isPrivacyAuthorityEnabled ? "true" : "false", captureState_);
1700     CHECK_AND_RETURN_RET_LOG(
1701         captureState_ == AVScreenCaptureState::CREATED || captureState_ == AVScreenCaptureState::STOPPED,
1702         MSERR_INVALID_OPERATION, "StartScreenCapture failed, not in CREATED or STOPPED, state:%{public}d",
1703         captureState_);
1704     MEDIA_LOGI("StartScreenCapture isPrivacyAuthorityEnabled:%{public}d", isPrivacyAuthorityEnabled);
1705     isSurfaceMode_ = false;
1706     return StartScreenCaptureInner(isPrivacyAuthorityEnabled);
1707 }
1708 
StartScreenCaptureWithSurface(sptr<Surface> surface,bool isPrivacyAuthorityEnabled)1709 int32_t ScreenCaptureServer::StartScreenCaptureWithSurface(sptr<Surface> surface, bool isPrivacyAuthorityEnabled)
1710 {
1711     std::lock_guard<std::mutex> lock(mutex_);
1712     CHECK_AND_RETURN_RET_LOG(
1713         captureState_ == AVScreenCaptureState::CREATED || captureState_ == AVScreenCaptureState::STOPPED,
1714         MSERR_INVALID_OPERATION, "StartScreenCaptureWithSurface failed, not in CREATED or STOPPED, state:%{public}d",
1715         captureState_);
1716     MEDIA_LOGI("StartScreenCaptureWithSurface isPrivacyAuthorityEnabled:%{public}d", isPrivacyAuthorityEnabled);
1717     if (surface == nullptr) {
1718         MEDIA_LOGE("surface is nullptr");
1719         return MSERR_INVALID_OPERATION;
1720     }
1721     surface_ = surface;
1722     isSurfaceMode_ = true;
1723     dataMode_ = AVScreenCaptureDataMode::SUFFACE_MODE;
1724     return StartScreenCaptureInner(isPrivacyAuthorityEnabled);
1725 }
1726 
StartVideoCapture()1727 int32_t ScreenCaptureServer::StartVideoCapture()
1728 {
1729     MediaTrace trace("ScreenCaptureServer::StartVideoCapture");
1730     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartVideoCapture start, state:%{public}d, "
1731         "dataType:%{public}d, isSurfaceMode:%{public}s.", FAKE_POINTER(this),
1732         captureConfig_.videoInfo.videoCapInfo.state, captureConfig_.dataType, isSurfaceMode_ ? "true" : "false");
1733     if (captureConfig_.videoInfo.videoCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_IGNORE) {
1734         MEDIA_LOGI("StartVideoCapture is ignored");
1735         return MSERR_OK;
1736     }
1737     CHECK_AND_RETURN_RET_LOG(
1738         captureConfig_.videoInfo.videoCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID,
1739         MSERR_INVALID_VAL, "StartVideoCapture failed, invalid param, dataType:%{public}d", captureConfig_.dataType);
1740 
1741     int32_t ret = StartHomeVideoCapture();
1742     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret,
1743         "StartHomeVideoCapture failed, isSurfaceMode:%{public}d, dataType:%{public}d",
1744         isSurfaceMode_, captureConfig_.dataType);
1745     MEDIA_LOGI("StartVideoCapture end.");
1746     return MSERR_OK;
1747 }
1748 
StartHomeVideoCapture()1749 int32_t ScreenCaptureServer::StartHomeVideoCapture()
1750 {
1751     MediaTrace trace("ScreenCaptureServer::StartHomeVideoCapture");
1752     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartHomeVideoCapture start, isSurfaceMode:%{public}s.",
1753         FAKE_POINTER(this), isSurfaceMode_ ? "true" : "false");
1754     std::string virtualScreenName = "screen_capture";
1755     if (isSurfaceMode_) {
1756         int32_t ret = CreateVirtualScreen(virtualScreenName, surface_);
1757         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "create virtual screen with input surface failed");
1758         return MSERR_OK;
1759     }
1760 
1761     ON_SCOPE_EXIT(0) {
1762         DestroyVirtualScreen();
1763         if (consumer_ != nullptr && surfaceCb_ != nullptr) {
1764             consumer_->UnregisterConsumerListener();
1765         }
1766         consumer_ = nullptr;
1767         surfaceCb_ = nullptr;
1768     };
1769     consumer_ = OHOS::Surface::CreateSurfaceAsConsumer();
1770     CHECK_AND_RETURN_RET_LOG(consumer_ != nullptr, MSERR_UNKNOWN, "CreateSurfaceAsConsumer failed");
1771     MEDIA_LOGI("ScreenCaptureServer consumer_ BUFFER_USAGE_CPU_READ BUFFER_USAGE_MEM_MMZ_CACHE S");
1772     consumer_->SetDefaultUsage(BUFFER_USAGE_CPU_READ | BUFFER_USAGE_MEM_MMZ_CACHE);
1773     MEDIA_LOGI("ScreenCaptureServer consumer_ BUFFER_USAGE_CPU_READ BUFFER_USAGE_MEM_MMZ_CACHE E");
1774     auto producer = consumer_->GetProducer();
1775     CHECK_AND_RETURN_RET_LOG(producer != nullptr, MSERR_UNKNOWN, "GetProducer failed");
1776     auto producerSurface = OHOS::Surface::CreateSurfaceAsProducer(producer);
1777     CHECK_AND_RETURN_RET_LOG(producerSurface != nullptr, MSERR_UNKNOWN, "CreateSurfaceAsProducer failed");
1778     surfaceCb_ = OHOS::sptr<ScreenCapBufferConsumerListener>::MakeSptr(consumer_, screenCaptureCb_);
1779     CHECK_AND_RETURN_RET_LOG(surfaceCb_ != nullptr, MSERR_UNKNOWN, "MakeSptr surfaceCb_ failed");
1780     consumer_->RegisterConsumerListener(surfaceCb_);
1781     int32_t ret = CreateVirtualScreen(virtualScreenName, producerSurface);
1782     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "create virtual screen without input surface failed");
1783     CANCEL_SCOPE_EXIT_GUARD(0);
1784     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartHomeVideoCapture OK.", FAKE_POINTER(this));
1785     return MSERR_OK;
1786 }
1787 
CreateVirtualScreen(const std::string & name,sptr<OHOS::Surface> consumer)1788 int32_t ScreenCaptureServer::CreateVirtualScreen(const std::string &name, sptr<OHOS::Surface> consumer)
1789 {
1790     MediaTrace trace("ScreenCaptureServer::CreateVirtualScreen");
1791     MEDIA_LOGI("CreateVirtualScreen Start");
1792     isConsumerStart_ = false;
1793     VirtualScreenOption virScrOption = InitVirtualScreenOption(name, consumer);
1794     sptr<Rosen::Display> display = Rosen::DisplayManager::GetInstance().GetDefaultDisplaySync();
1795     if (display != nullptr) {
1796         MEDIA_LOGI("get displayInfo width:%{public}d,height:%{public}d,density:%{public}d", display->GetWidth(),
1797                    display->GetHeight(), display->GetDpi());
1798         virScrOption.density_ = display->GetDpi();
1799     }
1800     if (missionIds_.size() > 0 && captureConfig_.captureMode == CAPTURE_SPECIFIED_WINDOW) {
1801         virScrOption.missionIds_ = missionIds_;
1802     } else if (captureConfig_.videoInfo.videoCapInfo.taskIDs.size() > 0 &&
1803         captureConfig_.captureMode == CAPTURE_SPECIFIED_WINDOW) {
1804         GetMissionIds(missionIds_);
1805         virScrOption.missionIds_ = missionIds_;
1806     }
1807     screenId_ = ScreenManager::GetInstance().CreateVirtualScreen(virScrOption);
1808     CHECK_AND_RETURN_RET_LOG(screenId_ >= 0, MSERR_UNKNOWN, "CreateVirtualScreen failed, invalid screenId");
1809     MEDIA_LOGI("CreateVirtualScreen success");
1810     return PrepareVirtualScreenMirror();
1811 }
1812 
PrepareVirtualScreenMirror()1813 int32_t ScreenCaptureServer::PrepareVirtualScreenMirror()
1814 {
1815     for (size_t i = 0; i < contentFilter_.windowIDsVec.size(); i++) {
1816         MEDIA_LOGI("After CreateVirtualScreen windowIDsVec value :%{public}" PRIu64, contentFilter_.windowIDsVec[i]);
1817     }
1818     if (GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"]
1819             .compare(appName_) == 0) {
1820         SetScreenScaleMode();
1821     }
1822     Rosen::DisplayManager::GetInstance().SetVirtualScreenBlackList(screenId_, contentFilter_.windowIDsVec);
1823     auto screen = ScreenManager::GetInstance().GetScreenById(screenId_);
1824     if (screen == nullptr) {
1825         MEDIA_LOGE("GetScreenById failed");
1826         DestroyVirtualScreen();
1827         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_UNKNOWN,
1828             "GetScreenById failed");
1829         return MSERR_UNKNOWN;
1830     }
1831     if (canvasRotation_) {
1832         SetCanvasRotationInner();
1833     }
1834     SkipPrivacyModeInner();
1835     int32_t ret = MakeVirtualScreenMirror();
1836     if (ret != MSERR_OK) {
1837         MEDIA_LOGE("MakeVirtualScreenMirror failed");
1838         DestroyVirtualScreen();
1839         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_UNKNOWN,
1840             "MakeVirtualScreenMirror failed");
1841         return MSERR_UNKNOWN;
1842     }
1843     isConsumerStart_ = true;
1844     return MSERR_OK;
1845 }
1846 
MakeVirtualScreenMirror()1847 int32_t ScreenCaptureServer::MakeVirtualScreenMirror()
1848 {
1849     MediaTrace trace("ScreenCaptureServer::MakeVirtualScreenMirror");
1850     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " MakeVirtualScreenMirror start.", FAKE_POINTER(this));
1851     CHECK_AND_RETURN_RET_LOG(screenId_ >= 0 && screenId_ != SCREEN_ID_INVALID, MSERR_UNKNOWN,
1852         "MakeVirtualScreenMirror failed, invalid screenId");
1853     std::vector<sptr<Screen>> screens;
1854     DMError ret = ScreenManager::GetInstance().GetAllScreens(screens);
1855     CHECK_AND_RETURN_RET_LOG(screens.size() > 0, MSERR_UNKNOWN,
1856         "MakeVirtualScreenMirror failed to GetAllScreens, ret:%{public}d", ret);
1857     std::vector<ScreenId> mirrorIds;
1858     mirrorIds.push_back(screenId_);
1859     sptr<Rosen::Display> defaultDisplay = Rosen::DisplayManager::GetInstance().GetDefaultDisplaySync();
1860     CHECK_AND_RETURN_RET_LOG(defaultDisplay != nullptr, MSERR_UNKNOWN, "make mirror GetDefaultDisplaySync failed");
1861     ScreenId mirrorGroup = defaultDisplay->GetScreenId();
1862 
1863     if (captureConfig_.captureMode != CAPTURE_SPECIFIED_SCREEN) {
1864         MEDIA_LOGI("MakeVirtualScreenMirror DefaultDisplay, screenId:%{public}" PRIu64, defaultDisplay->GetScreenId());
1865         ret = ScreenManager::GetInstance().MakeMirror(defaultDisplay->GetScreenId(), mirrorIds, mirrorGroup);
1866         CHECK_AND_RETURN_RET_LOG(ret == DMError::DM_OK, MSERR_UNKNOWN,
1867             "MakeVirtualScreenMirror failed to MakeMirror, captureMode:%{public}d, ret:%{public}d",
1868             captureConfig_.captureMode, ret);
1869         MEDIA_LOGI("MakeVirtualScreenMirror main screen success, screenId:%{public}" PRIu64, screens[0]->GetId());
1870         return MSERR_OK;
1871     }
1872     for (uint32_t i = 0; i < screens.size() ; i++) {
1873         if (screens[i]->GetId() == captureConfig_.videoInfo.videoCapInfo.displayId) {
1874             ret = ScreenManager::GetInstance().MakeMirror(screens[i]->GetId(), mirrorIds, mirrorGroup);
1875             CHECK_AND_RETURN_RET_LOG(ret == DMError::DM_OK, MSERR_UNKNOWN,
1876                 "MakeVirtualScreenMirror failed to MakeMirror for CAPTURE_SPECIFIED_SCREEN, ret:%{public}d", ret);
1877             MEDIA_LOGI("MakeVirtualScreenMirror extand screen success, screenId:%{public}" PRIu64,
1878                 captureConfig_.videoInfo.videoCapInfo.displayId);
1879             return MSERR_OK;
1880         }
1881     }
1882     MEDIA_LOGE("MakeVirtualScreenMirror failed to find screenId:%{public}" PRIu64,
1883         captureConfig_.videoInfo.videoCapInfo.displayId);
1884     FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_UNKNOWN,
1885         "MakeVirtualScreenMirror failed to find screenId");
1886     return MSERR_UNKNOWN;
1887 }
1888 
DestroyVirtualScreen()1889 void ScreenCaptureServer::DestroyVirtualScreen()
1890 {
1891     MediaTrace trace("ScreenCaptureServer::DestroyVirtualScreen");
1892     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " DestroyVirtualScreen start.", FAKE_POINTER(this));
1893     if (screenId_ >=0 && screenId_ != SCREEN_ID_INVALID) {
1894         if (isConsumerStart_) {
1895             std::vector<ScreenId> screenIds;
1896             screenIds.push_back(screenId_);
1897             ScreenManager::GetInstance().StopMirror(screenIds);
1898         }
1899         ScreenManager::GetInstance().DestroyVirtualScreen(screenId_);
1900         screenId_ = SCREEN_ID_INVALID;
1901         isConsumerStart_ = false;
1902     }
1903     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " DestroyVirtualScreen end.", FAKE_POINTER(this));
1904 }
1905 
CloseFd()1906 void ScreenCaptureServer::CloseFd()
1907 {
1908     MediaTrace trace("ScreenCaptureServer::CloseFd");
1909     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " CloseFd, fd is %{public}d", FAKE_POINTER(this),
1910         outputFd_);
1911     if (outputFd_ >= 0) {
1912         (void)::close(outputFd_);
1913         outputFd_ = -1;
1914     }
1915     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " CloseFd end.", FAKE_POINTER(this));
1916 }
1917 
InitVirtualScreenOption(const std::string & name,sptr<OHOS::Surface> consumer)1918 VirtualScreenOption ScreenCaptureServer::InitVirtualScreenOption(const std::string &name, sptr<OHOS::Surface> consumer)
1919 {
1920     MediaTrace trace("ScreenCaptureServer::InitVirtualScreenOption");
1921     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " InitVirtualScreenOption start, naem:%{public}s.",
1922         FAKE_POINTER(this), name.c_str());
1923     VirtualScreenOption virScrOption = {
1924         .name_ = name,
1925         .width_ = captureConfig_.videoInfo.videoCapInfo.videoFrameWidth,
1926         .height_ = captureConfig_.videoInfo.videoCapInfo.videoFrameHeight,
1927         .density_ = 0,
1928         .surface_ = consumer,
1929         .flags_ = 0,
1930         .isForShot_ = true,
1931         .missionIds_ = {},
1932     };
1933     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " InitVirtualScreenOption end.", FAKE_POINTER(this));
1934     return virScrOption;
1935 }
1936 
GetMissionIds(std::vector<uint64_t> & missionIds)1937 int32_t ScreenCaptureServer::GetMissionIds(std::vector<uint64_t> &missionIds)
1938 {
1939     int32_t size = static_cast<int32_t>(captureConfig_.videoInfo.videoCapInfo.taskIDs.size());
1940     std::list<int32_t> taskIDListTemp = captureConfig_.videoInfo.videoCapInfo.taskIDs;
1941     for (int32_t i = 0; i < size; i++) {
1942         int32_t taskId = taskIDListTemp.front();
1943         taskIDListTemp.pop_front();
1944         MEDIA_LOGI("ScreenCaptureServer::GetMissionIds taskId : %{public}s", std::to_string(taskId).c_str());
1945         uint64_t uintNum = static_cast<uint64_t>(taskId);
1946         missionIds.push_back(uintNum);
1947     }
1948     return MSERR_OK;
1949 }
1950 
AcquireAudioBuffer(std::shared_ptr<AudioBuffer> & audioBuffer,AudioCaptureSourceType type)1951 int32_t ScreenCaptureServer::AcquireAudioBuffer(std::shared_ptr<AudioBuffer> &audioBuffer, AudioCaptureSourceType type)
1952 {
1953     MediaTrace trace("ScreenCaptureServer::AcquireAudioBuffer");
1954     std::unique_lock<std::mutex> lock(mutex_);
1955     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " AcquireAudioBuffer start, state:%{public}d, "
1956         "type:%{public}d.", FAKE_POINTER(this), captureState_, type);
1957     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::STARTED, MSERR_INVALID_OPERATION,
1958         "AcquireAudioBuffer failed, capture is not STARTED, state:%{public}d, type:%{public}d", captureState_, type);
1959 
1960     if (((type == AudioCaptureSourceType::MIC) || (type == AudioCaptureSourceType::SOURCE_DEFAULT)) &&
1961         micAudioCapture_ != nullptr && micAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
1962         return micAudioCapture_->AcquireAudioBuffer(audioBuffer);
1963     }
1964     if (((type == AudioCaptureSourceType::ALL_PLAYBACK) || (type == AudioCaptureSourceType::APP_PLAYBACK)) &&
1965         innerAudioCapture_ != nullptr && innerAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
1966         return innerAudioCapture_->AcquireAudioBuffer(audioBuffer);
1967     }
1968     MEDIA_LOGE("AcquireAudioBuffer failed, source type not support, type:%{public}d", type);
1969     FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_UNKNOWN,
1970         "AcquireAudioBuffer failed, source type not support");
1971     return MSERR_UNKNOWN;
1972 }
1973 
AcquireAudioBufferMix(std::shared_ptr<AudioBuffer> & innerAudioBuffer,std::shared_ptr<AudioBuffer> & micAudioBuffer,AVScreenCaptureMixMode type)1974 int32_t ScreenCaptureServer::AcquireAudioBufferMix(std::shared_ptr<AudioBuffer> &innerAudioBuffer,
1975     std::shared_ptr<AudioBuffer> &micAudioBuffer, AVScreenCaptureMixMode type)
1976 {
1977     if (captureState_ != AVScreenCaptureState::STARTED) {
1978         return MSERR_INVALID_OPERATION;
1979     }
1980     if (type == AVScreenCaptureMixMode::MIX_MODE && micAudioCapture_ != nullptr &&
1981         micAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
1982         if (micAudioCapture_->AcquireAudioBuffer(micAudioBuffer) != MSERR_OK) {
1983             MEDIA_LOGE("micAudioCapture AcquireAudioBuffer failed");
1984         }
1985     }
1986     if (type == AVScreenCaptureMixMode::MIX_MODE && innerAudioCapture_ != nullptr &&
1987         innerAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
1988         if (innerAudioCapture_->AcquireAudioBuffer(innerAudioBuffer) != MSERR_OK) {
1989             MEDIA_LOGE("innerAudioCapture AcquireAudioBuffer failed");
1990         }
1991     }
1992     if (type == AVScreenCaptureMixMode::MIC_MODE && micAudioCapture_ != nullptr &&
1993         micAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
1994         if (micAudioCapture_->AcquireAudioBuffer(micAudioBuffer) != MSERR_OK) {
1995             MEDIA_LOGE("micAudioCapture AcquireAudioBuffer failed");
1996         }
1997     }
1998     if (type == AVScreenCaptureMixMode::INNER_MODE && innerAudioCapture_ != nullptr &&
1999         innerAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
2000         if (innerAudioCapture_->AcquireAudioBuffer(innerAudioBuffer) != MSERR_OK) {
2001             MEDIA_LOGE("innerAudioCapture AcquireAudioBuffer failed");
2002         }
2003     }
2004     return MSERR_OK;
2005 }
2006 
ReleaseAudioBuffer(AudioCaptureSourceType type)2007 int32_t ScreenCaptureServer::ReleaseAudioBuffer(AudioCaptureSourceType type)
2008 {
2009     MediaTrace trace("ScreenCaptureServer::ReleaseAudioBuffer");
2010     std::unique_lock<std::mutex> lock(mutex_);
2011     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " ReleaseAudioBuffer start, state:%{public}d, "
2012         "type:%{public}d.", FAKE_POINTER(this), captureState_, type);
2013     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::STARTED, MSERR_INVALID_OPERATION,
2014         "ReleaseAudioBuffer failed, capture is not STARTED, state:%{public}d, type:%{public}d", captureState_, type);
2015 
2016     if (((type == AudioCaptureSourceType::MIC) || (type == AudioCaptureSourceType::SOURCE_DEFAULT)) &&
2017         micAudioCapture_ != nullptr && micAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
2018         return micAudioCapture_->ReleaseAudioBuffer();
2019     }
2020     if (((type == AudioCaptureSourceType::ALL_PLAYBACK) || (type == AudioCaptureSourceType::APP_PLAYBACK)) &&
2021         innerAudioCapture_ != nullptr && innerAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
2022         return innerAudioCapture_->ReleaseAudioBuffer();
2023     }
2024     MEDIA_LOGE("ReleaseAudioBuffer failed, source type not support, type:%{public}d", type);
2025     FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_UNKNOWN,
2026         "ReleaseAudioBuffer failed, source type not support");
2027     return MSERR_UNKNOWN;
2028 }
2029 
ReleaseAudioBufferMix(AVScreenCaptureMixMode type)2030 int32_t ScreenCaptureServer::ReleaseAudioBufferMix(AVScreenCaptureMixMode type)
2031 {
2032     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::STARTED, MSERR_INVALID_OPERATION,
2033         "ReleaseAudioBuffer failed, capture is not STARTED, state:%{public}d, type:%{public}d", captureState_, type);
2034     if (type == AVScreenCaptureMixMode::MIX_MODE && micAudioCapture_ != nullptr &&
2035         micAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
2036         if (micAudioCapture_->ReleaseAudioBuffer() != MSERR_OK) {
2037             MEDIA_LOGE("micAudioCapture ReleaseAudioBuffer failed");
2038         }
2039     }
2040     if (type == AVScreenCaptureMixMode::MIX_MODE && innerAudioCapture_ != nullptr &&
2041         innerAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
2042         if (innerAudioCapture_->ReleaseAudioBuffer() != MSERR_OK) {
2043             MEDIA_LOGE("innerAudioCapture ReleaseAudioBuffer failed");
2044         }
2045     }
2046     if (type == AVScreenCaptureMixMode::MIC_MODE && micAudioCapture_ != nullptr &&
2047         micAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
2048         if (micAudioCapture_->ReleaseAudioBuffer() != MSERR_OK) {
2049             MEDIA_LOGE("micAudioCapture ReleaseAudioBuffer failed");
2050         }
2051     }
2052     if (type == AVScreenCaptureMixMode::INNER_MODE && innerAudioCapture_ != nullptr &&
2053         innerAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
2054         if (innerAudioCapture_->ReleaseAudioBuffer() != MSERR_OK) {
2055             MEDIA_LOGE("innerAudioCapture ReleaseAudioBuffer failed");
2056         }
2057     }
2058     return MSERR_OK;
2059 }
2060 
ReleaseInnerAudioBuffer()2061 int32_t ScreenCaptureServer::ReleaseInnerAudioBuffer()
2062 {
2063     CHECK_AND_RETURN_RET_LOG(innerAudioCapture_, MSERR_UNKNOWN, "innerAudioCapture_ is nullptr");
2064     int32_t ret = innerAudioCapture_->ReleaseAudioBuffer();
2065     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "innerAudioCapture ReleaseAudioBuffer failed");
2066     return ret;
2067 }
2068 
ReleaseMicAudioBuffer()2069 int32_t ScreenCaptureServer::ReleaseMicAudioBuffer()
2070 {
2071     CHECK_AND_RETURN_RET_LOG(micAudioCapture_, MSERR_UNKNOWN, "micAudioCapture_ is nullptr");
2072     int32_t ret = micAudioCapture_->ReleaseAudioBuffer();
2073     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "micAudioCapture ReleaseAudioBuffer failed");
2074     return ret;
2075 }
2076 
GetInnerAudioCaptureBufferSize(size_t & size)2077 int32_t ScreenCaptureServer::GetInnerAudioCaptureBufferSize(size_t &size)
2078 {
2079     if (innerAudioCapture_ == nullptr) {
2080         MEDIA_LOGE("innerAudioCapture_ is nullptr");
2081         return MSERR_UNKNOWN;
2082     }
2083     int32_t ret = innerAudioCapture_->GetBufferSize(size);
2084     return ret;
2085 }
2086 
GetMicAudioCaptureBufferSize(size_t & size)2087 int32_t ScreenCaptureServer::GetMicAudioCaptureBufferSize(size_t &size)
2088 {
2089     if (micAudioCapture_ == nullptr) {
2090         MEDIA_LOGE("micAudioCapture_ is nullptr");
2091         return MSERR_UNKNOWN;
2092     }
2093     int32_t ret = micAudioCapture_->GetBufferSize(size);
2094     return ret;
2095 }
2096 
AcquireVideoBuffer(sptr<OHOS::SurfaceBuffer> & surfaceBuffer,int32_t & fence,int64_t & timestamp,OHOS::Rect & damage)2097 int32_t ScreenCaptureServer::AcquireVideoBuffer(sptr<OHOS::SurfaceBuffer> &surfaceBuffer, int32_t &fence,
2098                                                 int64_t &timestamp, OHOS::Rect &damage)
2099 {
2100     MediaTrace trace("ScreenCaptureServer::AcquireVideoBuffer");
2101     std::unique_lock<std::mutex> lock(mutex_);
2102     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " AcquireVideoBuffer start, state:%{public}d, "
2103         "fence:%{public}d, timestamp:%{public}" PRId64, FAKE_POINTER(this), captureState_, fence, timestamp);
2104     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::STARTED, MSERR_INVALID_OPERATION,
2105         "AcquireVideoBuffer failed, capture is not STARTED, state:%{public}d", captureState_);
2106 
2107     CHECK_AND_RETURN_RET_LOG(surfaceCb_ != nullptr, MSERR_NO_MEMORY, "AcquireVideoBuffer failed, callback is nullptr");
2108     (static_cast<ScreenCapBufferConsumerListener *>(surfaceCb_.GetRefPtr()))->
2109         AcquireVideoBuffer(surfaceBuffer, fence, timestamp, damage);
2110     if (isDump_ && surfaceBuffer != nullptr) {
2111         void* addr = surfaceBuffer->GetVirAddr();
2112         uint32_t bufferSize = surfaceBuffer->GetSize();
2113         FILE *desFile = fopen(DUMP_PATH.c_str(), "wb+");
2114         if (desFile && addr != nullptr) {
2115             (void)fwrite(addr, 1, bufferSize, desFile);
2116             (void)fclose(desFile);
2117         } else if (desFile) {
2118             (void)fclose(desFile);
2119         }
2120     }
2121     if (surfaceBuffer != nullptr) {
2122         MEDIA_LOGD("getcurrent surfaceBuffer info, size:%{public}u", surfaceBuffer->GetSize());
2123         return MSERR_OK;
2124     }
2125     FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_UNKNOWN,
2126         "AcquireVideoBuffer fault");
2127     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " AcquireVideoBuffer end.", FAKE_POINTER(this));
2128     return MSERR_UNKNOWN;
2129 }
2130 
ReleaseVideoBuffer()2131 int32_t ScreenCaptureServer::ReleaseVideoBuffer()
2132 {
2133     MediaTrace trace("ScreenCaptureServer::ReleaseVideoBuffer");
2134     std::unique_lock<std::mutex> lock(mutex_);
2135     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " ReleaseVideoBuffer start, state:%{public}d.",
2136         FAKE_POINTER(this), captureState_);
2137     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::STARTED, MSERR_INVALID_OPERATION,
2138         "ReleaseVideoBuffer failed, capture is not STARTED, state:%{public}d", captureState_);
2139 
2140     CHECK_AND_RETURN_RET_LOG(surfaceCb_ != nullptr, MSERR_NO_MEMORY, "ReleaseVideoBuffer failed, callback is nullptr");
2141     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " ReleaseVideoBuffer end.", FAKE_POINTER(this));
2142     return (static_cast<ScreenCapBufferConsumerListener *>(surfaceCb_.GetRefPtr()))->ReleaseVideoBuffer();
2143 }
2144 
ExcludeContent(ScreenCaptureContentFilter & contentFilter)2145 int32_t ScreenCaptureServer::ExcludeContent(ScreenCaptureContentFilter &contentFilter)
2146 {
2147     std::unique_lock<std::mutex> lock(mutex_);
2148     CHECK_AND_RETURN_RET_LOG(captureState_ != AVScreenCaptureState::STOPPED, MSERR_INVALID_OPERATION,
2149         "ExcludeContent failed, capture is STOPPED");
2150 
2151     MEDIA_LOGI("ScreenCaptureServer::ExcludeContent start");
2152     contentFilter_ = contentFilter;
2153     if (captureState_ == AVScreenCaptureState::STARTED) {
2154         Rosen::DisplayManager::GetInstance().SetVirtualScreenBlackList(screenId_, contentFilter_.windowIDsVec);
2155     }
2156     int32_t ret = MSERR_OK;
2157     if (innerAudioCapture_ != nullptr) {
2158         ret = innerAudioCapture_->UpdateAudioCapturerConfig(contentFilter_);
2159     }
2160 
2161     // For the moment, not support:
2162     // For STREAM, should call AudioCapturer interface to make effect when start
2163     // For CAPTURE FILE, should call Recorder interface to make effect when start
2164     if (ret != MSERR_OK) {
2165         MEDIA_LOGE("ScreenCaptureServer::ExcludeContent UpdateAudioCapturerConfig failed");
2166         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_UNSUPPORT,
2167             "ExcludeContent failed, UpdateAudioCapturerConfig failed");
2168     }
2169     return ret;
2170 }
2171 
SetMicrophoneEnabled(bool isMicrophone)2172 int32_t ScreenCaptureServer::SetMicrophoneEnabled(bool isMicrophone)
2173 {
2174     MediaTrace trace("ScreenCaptureServer::SetMicrophoneEnabled");
2175     std::lock_guard<std::mutex> lock(mutex_);
2176     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetMicrophoneEnabled isMicrophoneOn_:%{public}d, "
2177         "new isMicrophone:%{public}d", FAKE_POINTER(this), isMicrophoneOn_, isMicrophone);
2178     int32_t ret = MSERR_UNKNOWN;
2179     isMicrophoneOn_ = isMicrophone;
2180     if (isMicrophone) {
2181         statisticalEventInfo_.enableMic = true;
2182     }
2183     if (captureState_ != AVScreenCaptureState::STARTED) {
2184         return MSERR_OK;
2185     }
2186     if (isMicrophone) {
2187         ret = SetMicrophoneOn();
2188         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "SetMicrophoneOn failed");
2189         if (screenCaptureCb_ != nullptr) {
2190             screenCaptureCb_->OnStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_MIC_UNMUTED_BY_USER);
2191         }
2192     } else {
2193         ret = SetMicrophoneOff();
2194         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "SetMicrophoneOff failed");
2195         if (screenCaptureCb_ != nullptr) {
2196             screenCaptureCb_->OnStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_MIC_MUTED_BY_USER);
2197         }
2198     }
2199     // For CAPTURE FILE, should call Recorder interface to make effect
2200     MEDIA_LOGI("SetMicrophoneEnabled OK.");
2201     return MSERR_OK;
2202 }
2203 
SetMicrophoneOn()2204 int32_t ScreenCaptureServer::SetMicrophoneOn()
2205 {
2206     int32_t ret = MSERR_UNKNOWN;
2207     if (!micAudioCapture_) {
2208         if (captureConfig_.dataType == DataType::ORIGINAL_STREAM) {
2209             ret = StartStreamMicAudioCapture();
2210             CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "StartStreamMicAudioCapture failed");
2211         } else if (captureConfig_.dataType == DataType::CAPTURE_FILE) {
2212             ret = StartFileMicAudioCapture();
2213             CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "StartFileMicAudioCapture failed");
2214         }
2215     } else if (micAudioCapture_->GetAudioCapturerState() == CAPTURER_PAUSED) {
2216         ret = micAudioCapture_->Resume();
2217         if (ret != MSERR_OK) {
2218             MEDIA_LOGE("micAudioCapture Resume failed");
2219             isMicrophoneOn_ = false;
2220             screenCaptureCb_->OnStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_MIC_UNAVAILABLE);
2221             return ret;
2222         }
2223     } else if (micAudioCapture_->GetAudioCapturerState() != CAPTURER_RECORDING) {
2224         MEDIA_LOGE("AudioCapturerState invalid");
2225     }
2226     usleep(AUDIO_CHANGE_TIME);
2227     if (innerAudioCapture_ && innerAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING &&
2228         audioSource_ && audioSource_->GetSpeakerAliveStatus() && !audioSource_->GetIsInVoIPCall()) {
2229         ret = innerAudioCapture_->Pause();
2230         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "innerAudioCapture Pause failed");
2231     }
2232     return MSERR_OK;
2233 }
2234 
SetMicrophoneOff()2235 int32_t ScreenCaptureServer::SetMicrophoneOff()
2236 {
2237     int32_t ret = MSERR_UNKNOWN;
2238     if (innerAudioCapture_ && innerAudioCapture_->GetAudioCapturerState() == CAPTURER_PAUSED) {
2239         ret = innerAudioCapture_->Resume();
2240         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "innerAudioCapture Resume failed");
2241     }
2242     usleep(AUDIO_CHANGE_TIME);
2243     if (micAudioCapture_ && micAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
2244         ret = micAudioCapture_->Pause();
2245         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "micAudioCapture Pause failed");
2246     }
2247     return MSERR_OK;
2248 }
2249 
OnSpeakerAliveStatusChanged(bool speakerAliveStatus)2250 int32_t ScreenCaptureServer::OnSpeakerAliveStatusChanged(bool speakerAliveStatus)
2251 {
2252     int32_t ret = MSERR_UNKNOWN;
2253     CHECK_AND_RETURN_RET_LOG(innerAudioCapture_, MSERR_UNKNOWN, "innerAudioCapture_ is nullptr");
2254     if (!speakerAliveStatus && innerAudioCapture_->GetAudioCapturerState() == CAPTURER_PAUSED) {
2255         ret = innerAudioCapture_->Resume();
2256         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "innerAudioCapture Resume failed");
2257     } else if (speakerAliveStatus && micAudioCapture_ &&
2258         micAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING && audioSource_ &&
2259         !audioSource_->GetIsInVoIPCall() && innerAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
2260         ret = innerAudioCapture_->Pause();
2261         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "innerAudioCapture Pause failed");
2262     }
2263     return MSERR_OK;
2264 }
2265 
ReStartMicForVoIPStatusSwitch()2266 int32_t ScreenCaptureServer::ReStartMicForVoIPStatusSwitch()
2267 {
2268     int32_t ret = MSERR_OK;
2269     StopMicAudioCapture();
2270     if (isMicrophoneOn_) {
2271         ret = StartFileMicAudioCapture();
2272         if (ret != MSERR_OK) {
2273             MEDIA_LOGE("OnVoIPStatusChanged StartFileMicAudioCapture failed, ret: %{public}d", ret);
2274         }
2275     }
2276     return ret;
2277 }
2278 
OnVoIPStatusChanged(bool isInVoIPCall)2279 int32_t ScreenCaptureServer::OnVoIPStatusChanged(bool isInVoIPCall)
2280 {
2281     MEDIA_LOGI("OnVoIPStatusChanged, isInVoIPCall:%{public}d", isInVoIPCall);
2282     int32_t ret = MSERR_UNKNOWN;
2283     if (isInVoIPCall) {
2284         CHECK_AND_RETURN_RET_LOG(innerAudioCapture_, MSERR_UNKNOWN, "innerAudioCapture is nullptr");
2285         if (innerAudioCapture_->GetAudioCapturerState() == CAPTURER_PAUSED) {
2286             ret = innerAudioCapture_->Resume();
2287             CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "innerAudioCapture Resume failed");
2288         }
2289         usleep(AUDIO_CHANGE_TIME);
2290         ReStartMicForVoIPStatusSwitch();
2291     } else {
2292         ReStartMicForVoIPStatusSwitch();
2293         usleep(AUDIO_CHANGE_TIME);
2294         CHECK_AND_RETURN_RET_LOG(innerAudioCapture_, MSERR_UNKNOWN, "innerAudioCapture is nullptr");
2295         if (innerAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING &&
2296             micAudioCapture_ && micAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING &&
2297             audioSource_ && audioSource_->GetSpeakerAliveStatus()) {
2298             ret = innerAudioCapture_->Pause();
2299             CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "innerAudioCapture Pause failed");
2300         }
2301     }
2302     return MSERR_OK;
2303 }
2304 
GetMicWorkingState()2305 bool ScreenCaptureServer::GetMicWorkingState()
2306 {
2307     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " GetMicWorkingState isMicrophoneOn_:%{public}d",
2308         FAKE_POINTER(this), isMicrophoneOn_);
2309     if (micAudioCapture_ != nullptr) {
2310         return micAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING;
2311     }
2312     return false;
2313 }
2314 
SetCanvasRotation(bool canvasRotation)2315 int32_t ScreenCaptureServer::SetCanvasRotation(bool canvasRotation)
2316 {
2317     MediaTrace trace("ScreenCaptureServer::SetCanvasRotation");
2318     std::lock_guard<std::mutex> lock(mutex_);
2319     canvasRotation_ = canvasRotation;
2320     MEDIA_LOGI("ScreenCaptureServer::SetCanvasRotation, canvasRotation:%{public}d", canvasRotation);
2321     if (captureState_ != AVScreenCaptureState::STARTED) { // Before Start
2322         return MSERR_OK;
2323     }
2324     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetCanvasRotation end.", FAKE_POINTER(this));
2325     return SetCanvasRotationInner();
2326 }
2327 
SetCanvasRotationInner()2328 int32_t ScreenCaptureServer::SetCanvasRotationInner()
2329 {
2330     MediaTrace trace("ScreenCaptureServer::SetCanvasRotationInner");
2331     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetCanvasRotationInner start.", FAKE_POINTER(this));
2332     CHECK_AND_RETURN_RET_LOG(screenId_ != SCREEN_ID_INVALID, MSERR_INVALID_VAL,
2333                              "SetCanvasRotation failed virtual screen not init");
2334     auto ret = ScreenManager::GetInstance().SetVirtualMirrorScreenCanvasRotation(screenId_, canvasRotation_);
2335     CHECK_AND_RETURN_RET_LOG(ret == DMError::DM_OK, MSERR_UNSUPPORT,
2336                              "SetVirtualMirrorScreenCanvasRotation failed, ret: %{public}d", ret);
2337     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetCanvasRotationInner OK.", FAKE_POINTER(this));
2338     return MSERR_OK;
2339 }
2340 
ResizeCanvas(int32_t width,int32_t height)2341 int32_t ScreenCaptureServer::ResizeCanvas(int32_t width, int32_t height)
2342 {
2343     MediaTrace trace("ScreenCaptureServer::ResizeCanvas");
2344     std::lock_guard<std::mutex> lock(mutex_);
2345     MEDIA_LOGI("ScreenCaptureServer::ResizeCanvas start, Width:%{public}d, Height:%{public}d", width, height);
2346     if (captureState_ != AVScreenCaptureState::STARTED) {
2347         MEDIA_LOGE("ResizeCanvas captureState_ invalid, captureState_:%{public}d", captureState_);
2348         return MSERR_INVALID_OPERATION;
2349     }
2350     if ((width <= 0) || (width > VIDEO_FRAME_WIDTH_MAX)) {
2351         MEDIA_LOGE("ResizeCanvas Width is invalid, Width:%{public}d, Height:%{public}d", width, height);
2352         return MSERR_INVALID_VAL;
2353     }
2354     if ((height <= 0) || (height > VIDEO_FRAME_HEIGHT_MAX)) {
2355         MEDIA_LOGE("ResizeCanvas Height is invalid, Width:%{public}d, Height:%{public}d", width, height);
2356         return MSERR_INVALID_VAL;
2357     }
2358     if (captureConfig_.dataType != DataType::ORIGINAL_STREAM) {
2359         MEDIA_LOGE("ResizeCanvas dataType invalid, dataType:%{public}d", captureConfig_.dataType);
2360         return MSERR_INVALID_OPERATION;
2361     }
2362 
2363     auto resizeRet = ScreenManager::GetInstance().ResizeVirtualScreen(screenId_, width, height);
2364     MEDIA_LOGI("ScreenCaptureServer::ResizeCanvas, ResizeVirtualScreen end, ret: %{public}d ", resizeRet);
2365     CHECK_AND_RETURN_RET_LOG(resizeRet == DMError::DM_OK, MSERR_UNSUPPORT, "ResizeVirtualScreen failed");
2366 
2367     return MSERR_OK;
2368 }
2369 
SkipPrivacyMode(std::vector<uint64_t> & windowIDsVec)2370 int32_t ScreenCaptureServer::SkipPrivacyMode(std::vector<uint64_t> &windowIDsVec)
2371 {
2372     MediaTrace trace("ScreenCaptureServer::SkipPrivacyMode");
2373     std::lock_guard<std::mutex> lock(mutex_);
2374     MEDIA_LOGI("ScreenCaptureServer::SkipPrivacyMode, windowIDsVec size:%{public}d",
2375         static_cast<int32_t>(windowIDsVec.size()));
2376     for (size_t i = 0; i < windowIDsVec.size(); i++) {
2377         MEDIA_LOGI("SkipPrivacyMode windowIDsVec value :%{public}" PRIu64, windowIDsVec[i]);
2378     }
2379     skipPrivacyWindowIDsVec_.assign(windowIDsVec.begin(), windowIDsVec.end());
2380     if (captureState_ != AVScreenCaptureState::STARTED) { // Before Start
2381         return MSERR_OK;
2382     }
2383     return SkipPrivacyModeInner();
2384 }
2385 
SkipPrivacyModeInner()2386 int32_t ScreenCaptureServer::SkipPrivacyModeInner()
2387 {
2388     MediaTrace trace("ScreenCaptureServer::SkipPrivacyModeInner");
2389     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SkipPrivacyModeInner start.", FAKE_POINTER(this));
2390     CHECK_AND_RETURN_RET_LOG(screenId_ != SCREEN_ID_INVALID, MSERR_INVALID_VAL,
2391                              "SkipPrivacyMode failed virtual screen not init");
2392     auto ret = Rosen::DisplayManager::GetInstance().SetVirtualScreenSecurityExemption(screenId_,
2393         appInfo_.appPid, skipPrivacyWindowIDsVec_);
2394     CHECK_AND_RETURN_RET_LOG(ret == DMError::DM_OK, MSERR_UNSUPPORT,
2395         "SkipPrivacyModeInner failed, ret: %{public}d", ret);
2396     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SkipPrivacyModeInner OK.", FAKE_POINTER(this));
2397     return MSERR_OK;
2398 }
2399 
SetMaxVideoFrameRate(int32_t frameRate)2400 int32_t ScreenCaptureServer::SetMaxVideoFrameRate(int32_t frameRate)
2401 {
2402     MediaTrace trace("ScreenCaptureServer::SetMaxVideoFrameRate");
2403     std::lock_guard<std::mutex> lock(mutex_);
2404     MEDIA_LOGI("ScreenCaptureServer::SetMaxVideoFrameRate start, frameRate:%{public}d", frameRate);
2405     if (captureState_ != AVScreenCaptureState::STARTED) {
2406         MEDIA_LOGE("SetMaxVideoFrameRate captureState_ invalid, captureState_:%{public}d", captureState_);
2407         return MSERR_INVALID_OPERATION;
2408     }
2409     if (frameRate <= 0) {
2410         MEDIA_LOGE("SetMaxVideoFrameRate frameRate is invalid, frameRate:%{public}d", frameRate);
2411         return MSERR_INVALID_VAL;
2412     }
2413 
2414     uint32_t actualRefreshRate = 0;
2415     auto res = ScreenManager::GetInstance().SetVirtualScreenMaxRefreshRate(screenId_,
2416         static_cast<uint32_t>(frameRate), actualRefreshRate);
2417 
2418     CHECK_AND_RETURN_RET_LOG(res == DMError::DM_OK, MSERR_UNSUPPORT, "SetMaxVideoFrameRate failed");
2419 
2420     MEDIA_LOGI("ScreenCaptureServer::SetMaxVideoFrameRate end, frameRate:%{public}d, actualRefreshRate:%{public}u",
2421         frameRate, actualRefreshRate);
2422     return MSERR_OK;
2423 }
2424 
SetScreenScaleMode()2425 int32_t ScreenCaptureServer::SetScreenScaleMode()
2426 {
2427     MediaTrace trace("ScreenCaptureServer::SetScreenScaleMode");
2428     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetScreenScaleMode start.", FAKE_POINTER(this));
2429     CHECK_AND_RETURN_RET_LOG(screenId_ != SCREEN_ID_INVALID, MSERR_INVALID_VAL,
2430                              "SetScreenScaleMode failed virtual screen not init");
2431     auto ret = ScreenManager::GetInstance().SetVirtualMirrorScreenScaleMode(
2432         screenId_, OHOS::Rosen::ScreenScaleMode::FILL_MODE);
2433     if (ret != DMError::DM_OK) {
2434         MEDIA_LOGW("SetScreenScaleMode failed, ret: %{public}d", ret);
2435         return static_cast<int32_t>(ret);
2436     }
2437     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetScreenScaleMode OK.", FAKE_POINTER(this));
2438     return MSERR_OK;
2439 }
2440 
StopAudioCapture()2441 int32_t ScreenCaptureServer::StopAudioCapture()
2442 {
2443     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StopAudioCapture start.", FAKE_POINTER(this));
2444     if (micAudioCapture_ != nullptr) {
2445         MediaTrace trace("ScreenCaptureServer::StopAudioCaptureMic");
2446         micAudioCapture_->Stop();
2447         micAudioCapture_ = nullptr;
2448     }
2449 
2450     if (innerAudioCapture_ != nullptr) {
2451         MediaTrace trace("ScreenCaptureServer::StopAudioCaptureInner");
2452         innerAudioCapture_->Stop();
2453         innerAudioCapture_ = nullptr;
2454     }
2455     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StopAudioCapture end.", FAKE_POINTER(this));
2456     return MSERR_OK;
2457 }
2458 
StopMicAudioCapture()2459 int32_t ScreenCaptureServer::StopMicAudioCapture()
2460 {
2461     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StopMicAudioCapture start.", FAKE_POINTER(this));
2462     if (micAudioCapture_ != nullptr) {
2463         MediaTrace trace("ScreenCaptureServer::StopAudioCaptureMic");
2464         micAudioCapture_->Stop();
2465         micAudioCapture_ = nullptr;
2466     }
2467     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StopMicAudioCapture end.", FAKE_POINTER(this));
2468     return MSERR_OK;
2469 }
2470 
StopVideoCapture()2471 int32_t ScreenCaptureServer::StopVideoCapture()
2472 {
2473     MediaTrace trace("ScreenCaptureServer::StopVideoCapture");
2474     MEDIA_LOGI("StopVideoCapture");
2475     if ((screenId_ < 0) || ((consumer_ == nullptr) && !isSurfaceMode_) || !isConsumerStart_) {
2476         MEDIA_LOGI("StopVideoCapture IGNORED, video capture not start");
2477         surfaceCb_ = nullptr;
2478         return MSERR_OK;
2479     }
2480 
2481     DestroyVirtualScreen();
2482     if (consumer_ != nullptr) {
2483         consumer_->UnregisterConsumerListener();
2484         consumer_ = nullptr;
2485     }
2486 
2487     if (surfaceCb_ != nullptr) {
2488         (static_cast<ScreenCapBufferConsumerListener *>(surfaceCb_.GetRefPtr()))->Release();
2489         surfaceCb_ = nullptr;
2490     }
2491     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StopVideoCapture end.", FAKE_POINTER(this));
2492     return MSERR_OK;
2493 }
2494 
StopScreenCaptureRecorder()2495 int32_t ScreenCaptureServer::StopScreenCaptureRecorder()
2496 {
2497     MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances StopScreenCaptureRecorder S", FAKE_POINTER(this));
2498     MediaTrace trace("ScreenCaptureServer::StopScreenCaptureRecorder");
2499     int32_t ret = MSERR_OK;
2500     if (recorder_ != nullptr) {
2501         ret = recorder_->Stop(false);
2502         if (ret != MSERR_OK) {
2503             MEDIA_LOGE("StopScreenCaptureRecorder recorder stop failed, ret:%{public}d", ret);
2504         }
2505         DestroyVirtualScreen();
2506         recorder_->Release();
2507         recorder_ = nullptr;
2508         StopAudioCapture();
2509     }
2510     captureCallback_ = nullptr;
2511     isConsumerStart_ = false;
2512     return ret;
2513 }
2514 
StopScreenCaptureByEvent(AVScreenCaptureStateCode stateCode)2515 int32_t ScreenCaptureServer::StopScreenCaptureByEvent(AVScreenCaptureStateCode stateCode)
2516 {
2517     MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances StopScreenCaptureByEvent S", FAKE_POINTER(this));
2518     MediaTrace trace("ScreenCaptureServer::StopScreenCaptureByEvent");
2519     std::lock_guard<std::mutex> lock(mutex_);
2520     return StopScreenCaptureInner(stateCode);
2521 }
2522 
StopScreenCaptureInner(AVScreenCaptureStateCode stateCode)2523 int32_t ScreenCaptureServer::StopScreenCaptureInner(AVScreenCaptureStateCode stateCode)
2524 {
2525     MediaTrace trace("ScreenCaptureServer::StopScreenCaptureInner");
2526     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StopScreenCaptureInner start, stateCode:%{public}d.",
2527         FAKE_POINTER(this), stateCode);
2528     if (screenCaptureCb_ != nullptr) {
2529         (static_cast<ScreenCaptureListenerCallback *>(screenCaptureCb_.get()))->Stop();
2530     }
2531     if (audioSource_ && audioSource_->GetAppPid() > 0) { // DataType::CAPTURE_FILE
2532         audioSource_->UnregisterAudioRendererEventListener(audioSource_->GetAppPid());
2533     }
2534     DisplayManager::GetInstance().UnregisterPrivateWindowListener(displayListener_);
2535     displayListener_ = nullptr;
2536     if (captureState_ == AVScreenCaptureState::CREATED || captureState_ == AVScreenCaptureState::STARTING) {
2537         captureState_ = AVScreenCaptureState::STOPPED;
2538         ScreenCaptureMonitorServer::GetInstance()->CallOnScreenCaptureFinished(appInfo_.appPid);
2539         if (screenCaptureCb_ != nullptr && stateCode != AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INVLID) {
2540             screenCaptureCb_->OnStateChange(stateCode);
2541         }
2542         isSurfaceMode_ = false;
2543         surface_ = nullptr;
2544         SetErrorInfo(MSERR_OK, "normal stopped", StopReason::NORMAL_STOPPED, IsUserPrivacyAuthorityNeeded());
2545         return MSERR_OK;
2546     }
2547     CHECK_AND_RETURN_RET(captureState_ != AVScreenCaptureState::STOPPED, MSERR_OK);
2548 
2549     int32_t ret = MSERR_OK;
2550     if (captureConfig_.dataType == DataType::CAPTURE_FILE) {
2551         ret = StopScreenCaptureRecorder();
2552     } else if (captureConfig_.dataType == DataType::ORIGINAL_STREAM) {
2553         int32_t retAudio = StopAudioCapture();
2554         int32_t retVideo = StopVideoCapture();
2555         ret = (retAudio == MSERR_OK && retVideo == MSERR_OK) ? MSERR_OK : MSERR_STOP_FAILED;
2556     } else {
2557         MEDIA_LOGW("StopScreenCaptureInner unsupport and ignore");
2558         return MSERR_OK;
2559     }
2560     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::STARTED, ret, "state:%{public}d", captureState_);
2561     captureState_ = AVScreenCaptureState::STOPPED;
2562     SetErrorInfo(MSERR_OK, "normal stopped", StopReason::NORMAL_STOPPED, IsUserPrivacyAuthorityNeeded());
2563     PostStopScreenCapture(stateCode);
2564     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StopScreenCaptureInner end.", FAKE_POINTER(this));
2565     return ret;
2566 }
2567 
PostStopScreenCapture(AVScreenCaptureStateCode stateCode)2568 void ScreenCaptureServer::PostStopScreenCapture(AVScreenCaptureStateCode stateCode)
2569 {
2570     MediaTrace trace("ScreenCaptureServer::PostStopScreenCapture");
2571     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " PostStopScreenCapture start, stateCode:%{public}d.",
2572         FAKE_POINTER(this), stateCode);
2573     ScreenCaptureMonitorServer::GetInstance()->CallOnScreenCaptureFinished(appInfo_.appPid);
2574     if (screenCaptureCb_ != nullptr && stateCode != AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INVLID) {
2575         screenCaptureCb_->OnStateChange(stateCode);
2576     }
2577 #ifdef SUPPORT_SCREEN_CAPTURE_WINDOW_NOTIFICATION
2578     if (isPrivacyAuthorityEnabled_ &&
2579         GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"]
2580             .compare(appName_) != 0) {
2581         // Remove real time notification
2582         int32_t ret = NotificationHelper::CancelNotification(notificationId_);
2583         MEDIA_LOGI("StopScreenCaptureInner CancelNotification id:%{public}d, ret:%{public}d ", notificationId_, ret);
2584         micCount_.store(0);
2585     }
2586 #endif
2587     isPrivacyAuthorityEnabled_ = false;
2588 
2589     if (!UpdatePrivacyUsingPermissionState(STOP_VIDEO)) {
2590         MEDIA_LOGE("UpdatePrivacyUsingPermissionState STOP failed, dataType:%{public}d", captureConfig_.dataType);
2591     }
2592     std::unordered_map<std::string, std::string> payload;
2593     int64_t value = ResourceSchedule::ResType::ScreenCaptureStatus::STOP_SCREEN_CAPTURE;
2594     ResSchedReportData(value, payload);
2595     {
2596         std::lock_guard<std::mutex> lock(mutexGlobal_);
2597         MEDIA_LOGI("StopScreenCaptureInner sessionId:%{public}d, activeSessionId_:%{public}d", sessionId_,
2598                    activeSessionId_.load());
2599         if (sessionId_ == activeSessionId_.load()) {
2600             activeSessionId_.store(SESSION_ID_INVALID);
2601         }
2602     }
2603 }
2604 
StopScreenCapture()2605 int32_t ScreenCaptureServer::StopScreenCapture()
2606 {
2607     MediaTrace trace("ScreenCaptureServer::StopScreenCapture");
2608     MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances StopScreenCapture S", FAKE_POINTER(this));
2609 
2610     std::lock_guard<std::mutex> lock(mutex_);
2611     int32_t ret = StopScreenCaptureInner(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INVLID);
2612     if (statisticalEventInfo_.startLatency == -1) {
2613         statisticalEventInfo_.captureDuration = -1; // latency -1 means invalid
2614     } else {
2615         int64_t endTime = GetCurrentMillisecond();
2616         statisticalEventInfo_.captureDuration = static_cast<int32_t>(endTime - startTime_ -
2617             statisticalEventInfo_.startLatency);
2618     }
2619 
2620     MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances StopScreenCapture E", FAKE_POINTER(this));
2621     return ret;
2622 }
2623 
Release()2624 void ScreenCaptureServer::Release()
2625 {
2626     ReleaseInner();
2627 }
2628 
ReleaseInner()2629 void ScreenCaptureServer::ReleaseInner()
2630 {
2631     MediaTrace trace("ScreenCaptureServer::ReleaseInner");
2632     MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances ReleaseInner S", FAKE_POINTER(this));
2633     int32_t sessionId;
2634     {
2635         std::lock_guard<std::mutex> lock(mutex_);
2636         StopScreenCaptureInner(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INVLID);
2637         sessionId = sessionId_;
2638         sessionId_ = SESSION_ID_INVALID;
2639         MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances ReleaseInner Stop done, sessionId:%{public}d",
2640             FAKE_POINTER(this), sessionId);
2641     }
2642     {
2643         std::lock_guard<std::mutex> lock(mutexGlobal_);
2644         serverMap.erase(sessionId);
2645     }
2646     skipPrivacyWindowIDsVec_.clear();
2647     SetMetaDataReport();
2648     screenCaptureObserverCb_ = nullptr;
2649     MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances ReleaseInner E", FAKE_POINTER(this));
2650 }
2651 
ScreenCaptureObserverCallBack(std::weak_ptr<ScreenCaptureServer> screenCaptureServer)2652 ScreenCaptureObserverCallBack::ScreenCaptureObserverCallBack(
2653     std::weak_ptr<ScreenCaptureServer> screenCaptureServer)
2654 {
2655     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
2656     screenCaptureServer_ = screenCaptureServer;
2657 }
2658 
StopAndRelease(AVScreenCaptureStateCode state)2659 bool ScreenCaptureObserverCallBack::StopAndRelease(AVScreenCaptureStateCode state)
2660 {
2661     MEDIA_LOGI("ScreenCaptureObserverCallBack::StopAndRelease");
2662     auto scrServer = screenCaptureServer_.lock();
2663     if (scrServer && !scrServer->IsTelInCallSkipList()) {
2664         scrServer->StopScreenCaptureByEvent(state);
2665         scrServer->Release();
2666     }
2667     return true;
2668 }
2669 
OnBufferAvailable()2670 void ScreenCapBufferConsumerListener::OnBufferAvailable()
2671 {
2672     MediaTrace trace("ScreenCaptureServer::OnBufferAvailable");
2673     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " OnBufferAvailable start.", FAKE_POINTER(this));
2674     CHECK_AND_RETURN(consumer_ != nullptr);
2675     int64_t timestamp = 0;
2676     OHOS::Rect damage;
2677     OHOS::sptr<OHOS::SurfaceBuffer> buffer = nullptr;
2678     sptr<SyncFence> acquireFence = SyncFence::INVALID_FENCE;
2679     int32_t acquireBufferRet = consumer_->AcquireBuffer(buffer, acquireFence, timestamp, damage);
2680     if (acquireBufferRet != GSERROR_OK) {
2681         MEDIA_LOGE("ScreenCapBufferConsumerListener: 0x%{public}06" PRIXPTR " AcquireBuffer Fail Code %{public}d",
2682             FAKE_POINTER(this), acquireBufferRet);
2683     }
2684     int32_t flushFence = -1;
2685     if (acquireFence != nullptr && acquireFence != SyncFence::INVALID_FENCE) {
2686         acquireFence->Wait(1000); // 1000 ms
2687         flushFence = acquireFence->Get();
2688     }
2689     CHECK_AND_RETURN_LOG(buffer != nullptr, "Acquire SurfaceBuffer failed");
2690     if ((buffer->GetUsage() & BUFFER_USAGE_MEM_MMZ_CACHE) != 0) {
2691         MEDIA_LOGD("ScreenCaptureServer::OnBufferAvailable cache enable");
2692         buffer->InvalidateCache();
2693     }
2694     void *addr = buffer->GetVirAddr();
2695     if (addr == nullptr) {
2696         MEDIA_LOGE("Acquire SurfaceBuffer address invalid");
2697         int32_t releaseBufferRet = consumer_->ReleaseBuffer(buffer, -1); // -1 not wait
2698         if (releaseBufferRet != GSERROR_OK) {
2699             MEDIA_LOGE("ScreenCapBufferConsumerListener: 0x%{public}06" PRIXPTR " ReleaseBuffer Fail Code %{public}d",
2700                 FAKE_POINTER(this), releaseBufferRet);
2701         }
2702         return;
2703     }
2704     MEDIA_LOGD("SurfaceBuffer size:%{public}u", buffer->GetSize());
2705     {
2706         std::unique_lock<std::mutex> lock(bufferMutex_);
2707         if (availBuffers_.size() > MAX_BUFFER_SIZE) {
2708             MEDIA_LOGE("consume slow, drop video frame");
2709             int32_t releaseBufferRet = consumer_->ReleaseBuffer(buffer, -1); // -1 not wait
2710             if (releaseBufferRet != GSERROR_OK) {
2711                 MEDIA_LOGE("ScreenCapBufferConsumerListener: 0x%{public}06" PRIXPTR " consume slow ReleaseBuffer "
2712                     "Fail Code %{public}d", FAKE_POINTER(this), releaseBufferRet);
2713             }
2714             return;
2715         }
2716         availBuffers_.push(std::make_unique<SurfaceBufferEntry>(buffer, flushFence, timestamp, damage));
2717     }
2718     bufferCond_.notify_all();
2719     ProcessVideoBufferCallBack();
2720 }
2721 
ProcessVideoBufferCallBack()2722 void ScreenCapBufferConsumerListener::ProcessVideoBufferCallBack()
2723 {
2724     std::lock_guard<std::mutex> lock(mutex_);
2725     CHECK_AND_RETURN_LOG(screenCaptureCb_ != nullptr, "no consumer, will drop video frame");
2726     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " OnBufferAvailable end.", FAKE_POINTER(this));
2727     screenCaptureCb_->OnVideoBufferAvailable(true);
2728 }
2729 
AcquireVideoBuffer(sptr<OHOS::SurfaceBuffer> & surfaceBuffer,int32_t & fence,int64_t & timestamp,OHOS::Rect & damage)2730 int32_t ScreenCapBufferConsumerListener::AcquireVideoBuffer(sptr<OHOS::SurfaceBuffer> &surfaceBuffer, int32_t &fence,
2731     int64_t &timestamp, OHOS::Rect &damage)
2732 {
2733     MediaTrace trace("ScreenCaptureServer::AcquireVideoBuffer");
2734     using namespace std::chrono_literals;
2735     std::unique_lock<std::mutex> lock(bufferMutex_);
2736     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " AcquireVideoBuffer start, fence:%{public}d, "
2737         "timestamp:%{public}" PRId64, FAKE_POINTER(this), fence, timestamp);
2738     if (!bufferCond_.wait_for(
2739         lock, std::chrono::milliseconds(OPERATION_TIMEOUT_IN_MS), [this] { return !availBuffers_.empty(); })) {
2740         return MSERR_UNKNOWN;
2741     }
2742     surfaceBuffer = availBuffers_.front()->buffer;
2743     fence = availBuffers_.front()->flushFence;
2744     timestamp = availBuffers_.front()->timeStamp;
2745     damage = availBuffers_.front()->damageRect;
2746     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " AcquireVideoBuffer end.", FAKE_POINTER(this));
2747     return MSERR_OK;
2748 }
2749 
~ScreenCapBufferConsumerListener()2750 ScreenCapBufferConsumerListener::~ScreenCapBufferConsumerListener()
2751 {
2752     std::unique_lock<std::mutex> lock(bufferMutex_);
2753     MEDIA_LOGD("ScreenCapBufferConsumerListener: 0x%{public}06" PRIXPTR " Destroy.", FAKE_POINTER(this));
2754     ReleaseBuffer();
2755 }
2756 
ReleaseBuffer()2757 int32_t ScreenCapBufferConsumerListener::ReleaseBuffer()
2758 {
2759     while (!availBuffers_.empty()) {
2760         if (consumer_ != nullptr) {
2761             int32_t releaseBufferRet = consumer_->ReleaseBuffer(availBuffers_.front()->buffer, -1);  // -1 not wait
2762             if (releaseBufferRet != GSERROR_OK) {
2763                 MEDIA_LOGE("ScreenCapBufferConsumerListener: 0x%{public}06" PRIXPTR " ReleaseBuffer "
2764                     "Fail Code %{public}d", FAKE_POINTER(this), releaseBufferRet);
2765             }
2766         }
2767         availBuffers_.pop();
2768     }
2769     return MSERR_OK;
2770 }
2771 
ReleaseVideoBuffer()2772 int32_t ScreenCapBufferConsumerListener::ReleaseVideoBuffer()
2773 {
2774     MediaTrace trace("ScreenCaptureServer::ReleaseVideoBuffer");
2775     std::unique_lock<std::mutex> lock(bufferMutex_);
2776     MEDIA_LOGD("ScreenCapBufferConsumerListener: 0x%{public}06" PRIXPTR " ReleaseVideoBuffer start.",
2777         FAKE_POINTER(this));
2778     CHECK_AND_RETURN_RET_LOG(!availBuffers_.empty(), MSERR_OK, "buffer queue is empty, no video frame to release");
2779 
2780     if (consumer_ != nullptr) {
2781         int32_t releaseBufferRet = consumer_->ReleaseBuffer(availBuffers_.front()->buffer, -1); // -1 not wait
2782         if (releaseBufferRet != GSERROR_OK) {
2783             MEDIA_LOGE("ScreenCapBufferConsumerListener: 0x%{public}06" PRIXPTR " ReleaseVideoBuffer "
2784                 "Fail Code %{public}d", FAKE_POINTER(this), releaseBufferRet);
2785         }
2786     }
2787     availBuffers_.pop();
2788     MEDIA_LOGD("ScreenCapBufferConsumerListener: 0x%{public}06" PRIXPTR " ReleaseVideoBuffer end.", FAKE_POINTER(this));
2789     return MSERR_OK;
2790 }
2791 
Release()2792 int32_t ScreenCapBufferConsumerListener::Release()
2793 {
2794     std::unique_lock<std::mutex> lock(bufferMutex_);
2795     MEDIA_LOGI("ScreenCapBufferConsumerListener Release");
2796     return ReleaseBuffer();
2797 }
2798 
SetAudioSource(std::shared_ptr<AudioDataSource> audioSource)2799 void ScreenRendererAudioStateChangeCallback::SetAudioSource(std::shared_ptr<AudioDataSource> audioSource)
2800 {
2801     audioSource_ = audioSource;
2802 }
2803 
SetAppName(std::string appName)2804 void ScreenRendererAudioStateChangeCallback::SetAppName(std::string appName)
2805 {
2806     appName_ = appName;
2807 }
2808 
OnRendererStateChange(const std::vector<std::unique_ptr<AudioRendererChangeInfo>> & audioRendererChangeInfos)2809 void ScreenRendererAudioStateChangeCallback::OnRendererStateChange(
2810     const std::vector<std::unique_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos)
2811 {
2812     MEDIA_LOGD("ScreenRendererAudioStateChangeCallback IN");
2813     CHECK_AND_RETURN(audioSource_ != nullptr);
2814     audioSource_->SpeakerStateUpdate(audioRendererChangeInfos);
2815     std::string region = Global::I18n::LocaleConfig::GetSystemRegion();
2816     if (GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"]
2817             .compare(appName_) == 0 && region == "CN") {
2818         audioSource_->VoIPStateUpdate(audioRendererChangeInfos);
2819     }
2820 }
2821 
SpeakerStateUpdate(const std::vector<std::unique_ptr<AudioRendererChangeInfo>> & audioRendererChangeInfos)2822 void AudioDataSource::SpeakerStateUpdate(
2823     const std::vector<std::unique_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos)
2824 {
2825     (void)audioRendererChangeInfos;
2826     std::vector<std::unique_ptr<AudioRendererChangeInfo>> allAudioRendererChangeInfos;
2827     AudioStreamManager::GetInstance()->GetCurrentRendererChangeInfos(allAudioRendererChangeInfos);
2828     uint32_t changeInfoSize = allAudioRendererChangeInfos.size();
2829     if (changeInfoSize == 0) {
2830         return;
2831     }
2832     bool speakerAlive = HasSpeakerStream(allAudioRendererChangeInfos);
2833     if (speakerAlive != speakerAliveStatus_) {
2834         speakerAliveStatus_ = speakerAlive;
2835         CHECK_AND_RETURN(screenCaptureServer_ != nullptr);
2836         screenCaptureServer_->OnSpeakerAliveStatusChanged(speakerAlive);
2837         if (speakerAlive) {
2838             MEDIA_LOGI("HEADSET Change to Speaker.");
2839         } else {
2840             MEDIA_LOGI("Speaker Change to HEADSET.");
2841         }
2842     }
2843 }
2844 
HasSpeakerStream(const std::vector<std::unique_ptr<AudioRendererChangeInfo>> & audioRendererChangeInfos)2845 bool AudioDataSource::HasSpeakerStream(
2846     const std::vector<std::unique_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos)
2847 {
2848     uint32_t changeInfoIndex = 0;
2849     uint32_t headSetCount = 0;
2850     bool hasSpeakerStream = true;
2851     for (const std::unique_ptr<AudioRendererChangeInfo> &changeInfo: audioRendererChangeInfos) {
2852         if (!changeInfo) {
2853             continue;
2854         }
2855         MEDIA_LOGI("ChangeInfo Id: %{public}d, Client pid : %{public}d, State : %{public}d, DeviceType : %{public}d",
2856             changeInfoIndex, changeInfo->clientPid, static_cast<int32_t>(changeInfo->rendererState),
2857             static_cast<int32_t>(changeInfo->outputDeviceInfo.deviceType));
2858         if (changeInfo->outputDeviceInfo.deviceType == DEVICE_TYPE_WIRED_HEADSET ||
2859             changeInfo->outputDeviceInfo.deviceType == DEVICE_TYPE_WIRED_HEADPHONES ||
2860             changeInfo->outputDeviceInfo.deviceType == DEVICE_TYPE_BLUETOOTH_SCO ||
2861             changeInfo->outputDeviceInfo.deviceType == DEVICE_TYPE_BLUETOOTH_A2DP ||
2862             changeInfo->outputDeviceInfo.deviceType == DEVICE_TYPE_USB_HEADSET ||
2863             changeInfo->outputDeviceInfo.deviceType == DEVICE_TYPE_USB_ARM_HEADSET) {
2864             headSetCount++;
2865         }
2866         changeInfoIndex++;
2867     }
2868     if (headSetCount == changeInfoIndex) { // only if all streams in headset
2869         hasSpeakerStream = false;
2870     }
2871     return hasSpeakerStream;
2872 }
2873 
VoIPStateUpdate(const std::vector<std::unique_ptr<AudioRendererChangeInfo>> & audioRendererChangeInfos)2874 void AudioDataSource::VoIPStateUpdate(
2875     const std::vector<std::unique_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos)
2876 {
2877     std::lock_guard<std::mutex> lock(voipStatusChangeMutex_);
2878     (void)audioRendererChangeInfos;
2879     std::vector<std::unique_ptr<AudioRendererChangeInfo>> allAudioRendererChangeInfos;
2880     AudioStreamManager::GetInstance()->GetCurrentRendererChangeInfos(allAudioRendererChangeInfos);
2881     bool isInVoIPCall = false;
2882     for (const std::unique_ptr<AudioRendererChangeInfo> &changeInfo: allAudioRendererChangeInfos) {
2883         if (!changeInfo) {
2884             continue;
2885         }
2886         MEDIA_LOGI("Client pid : %{public}d, State : %{public}d, DeviceType : %{public}d",
2887             changeInfo->clientPid, static_cast<int32_t>(changeInfo->rendererState),
2888             static_cast<int32_t>(changeInfo->outputDeviceInfo.deviceType));
2889         if (changeInfo->rendererState == RendererState::RENDERER_RUNNING &&
2890             changeInfo->rendererInfo.streamUsage == AudioStandard::StreamUsage::STREAM_USAGE_VOICE_COMMUNICATION) {
2891             isInVoIPCall = true;
2892             break;
2893         }
2894     }
2895     if (isInVoIPCall_.load() == isInVoIPCall) {
2896         return;
2897     }
2898     isInVoIPCall_.store(isInVoIPCall);
2899     CHECK_AND_RETURN(screenCaptureServer_ != nullptr);
2900     screenCaptureServer_->OnVoIPStatusChanged(isInVoIPCall);
2901 }
2902 
SetAppPid(int32_t appid)2903 void AudioDataSource::SetAppPid(int32_t appid)
2904 {
2905     appPid_ = appid;
2906 }
2907 
GetAppPid()2908 int32_t AudioDataSource::GetAppPid()
2909 {
2910     return appPid_ ;
2911 }
2912 
GetIsInVoIPCall()2913 bool AudioDataSource::GetIsInVoIPCall()
2914 {
2915     return isInVoIPCall_.load();
2916 }
2917 
GetSpeakerAliveStatus()2918 bool AudioDataSource::GetSpeakerAliveStatus()
2919 {
2920     return speakerAliveStatus_;
2921 }
2922 
SetAppName(std::string appName)2923 void AudioDataSource::SetAppName(std::string appName)
2924 {
2925     appName_ = appName;
2926 }
2927 
RegisterAudioRendererEventListener(const int32_t clientPid,const std::shared_ptr<AudioRendererStateChangeCallback> & callback)2928 int32_t AudioDataSource::RegisterAudioRendererEventListener(const int32_t clientPid,
2929     const std::shared_ptr<AudioRendererStateChangeCallback> &callback)
2930 {
2931     CHECK_AND_RETURN_RET_LOG(callback != nullptr, MSERR_INVALID_VAL, "audio callback is null");
2932     int32_t ret = AudioStreamManager::GetInstance()->RegisterAudioRendererEventListener(clientPid, callback);
2933     std::vector<std::unique_ptr<AudioRendererChangeInfo>> audioRendererChangeInfos;
2934     SpeakerStateUpdate(audioRendererChangeInfos);
2935     std::string region = Global::I18n::LocaleConfig::GetSystemRegion();
2936     if (GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"]
2937             .compare(appName_) == 0 && region == "CN") {
2938         VoIPStateUpdate(audioRendererChangeInfos);
2939     }
2940     return ret;
2941 }
2942 
UnregisterAudioRendererEventListener(const int32_t clientPid)2943 int32_t AudioDataSource::UnregisterAudioRendererEventListener(const int32_t clientPid)
2944 {
2945     MEDIA_LOGI("client id: %{public}d", clientPid);
2946     return AudioStreamManager::GetInstance()->UnregisterAudioRendererEventListener(clientPid);
2947 }
2948 
ReadAt(std::shared_ptr<AVBuffer> buffer,uint32_t length)2949 int32_t AudioDataSource::ReadAt(std::shared_ptr<AVBuffer> buffer, uint32_t length)
2950 {
2951     MEDIA_LOGD("AudioDataSource ReadAt start");
2952     std::shared_ptr<AudioBuffer> innerAudioBuffer = nullptr;
2953     std::shared_ptr<AudioBuffer> micAudioBuffer = nullptr;
2954     int32_t ret = MSERR_OK;
2955     if (screenCaptureServer_ == nullptr) {
2956         return MSERR_UNKNOWN;
2957     }
2958     ret = screenCaptureServer_->AcquireAudioBufferMix(innerAudioBuffer, micAudioBuffer, type_);
2959     if (ret != MSERR_OK) {
2960         return MSERR_INVALID_VAL;
2961     }
2962     MEDIA_LOGD("AcquireAudioBufferMix sucess");
2963     std::shared_ptr<AVMemory> &bufferMem = buffer->memory_;
2964     if (buffer->memory_ == nullptr) {
2965         MEDIA_LOGE("buffer->memory_ is nullptr");
2966         return MSERR_INVALID_VAL;
2967     }
2968     if (type_ == AVScreenCaptureMixMode::MIX_MODE) {
2969         return MixModeBufferWrite(innerAudioBuffer, micAudioBuffer, bufferMem);
2970     } else if (type_ == AVScreenCaptureMixMode::INNER_MODE && innerAudioBuffer != nullptr) {
2971         bufferMem->Write(reinterpret_cast<uint8_t*>(innerAudioBuffer->buffer), innerAudioBuffer->length, 0);
2972         return screenCaptureServer_->ReleaseAudioBufferMix(type_);
2973     } else if (type_ == AVScreenCaptureMixMode::MIC_MODE && micAudioBuffer != nullptr) {
2974         bufferMem->Write(reinterpret_cast<uint8_t*>(micAudioBuffer->buffer), micAudioBuffer->length, 0);
2975         return screenCaptureServer_->ReleaseAudioBufferMix(type_);
2976     }
2977     return MSERR_UNKNOWN;
2978 }
2979 
MixModeBufferWrite(std::shared_ptr<AudioBuffer> & innerAudioBuffer,std::shared_ptr<AudioBuffer> & micAudioBuffer,std::shared_ptr<AVMemory> & bufferMem)2980 int32_t AudioDataSource::MixModeBufferWrite(std::shared_ptr<AudioBuffer> &innerAudioBuffer,
2981     std::shared_ptr<AudioBuffer> &micAudioBuffer, std::shared_ptr<AVMemory> &bufferMem)
2982 {
2983     if (innerAudioBuffer && innerAudioBuffer->buffer && micAudioBuffer && micAudioBuffer->buffer) {
2984         char* mixData = new char[innerAudioBuffer->length];
2985         char* srcData[2] = {nullptr};
2986         srcData[0] = reinterpret_cast<char*>(innerAudioBuffer->buffer);
2987         srcData[1] = reinterpret_cast<char*>(micAudioBuffer->buffer);
2988         int channels = 2;
2989         MixAudio(srcData, mixData, channels, innerAudioBuffer->length);
2990         bufferMem->Write(reinterpret_cast<uint8_t*>(mixData), innerAudioBuffer->length, 0);
2991         delete[] mixData;
2992     } else if (innerAudioBuffer && innerAudioBuffer->buffer) {
2993         bufferMem->Write(reinterpret_cast<uint8_t*>(innerAudioBuffer->buffer), innerAudioBuffer->length, 0);
2994     } else if (micAudioBuffer && micAudioBuffer->buffer) {
2995         bufferMem->Write(reinterpret_cast<uint8_t*>(micAudioBuffer->buffer), micAudioBuffer->length, 0);
2996     } else {
2997         MEDIA_LOGE("without buffer write");
2998         return MSERR_UNKNOWN;
2999     }
3000     if (innerAudioBuffer) {
3001         if (screenCaptureServer_->ReleaseInnerAudioBuffer() != MSERR_OK) {
3002             MEDIA_LOGE("ReleaseInnerAudioBuffer failed");
3003         }
3004     }
3005     if (micAudioBuffer) {
3006         if (screenCaptureServer_->ReleaseMicAudioBuffer() != MSERR_OK) {
3007             MEDIA_LOGE("ReleaseMicAudioBuffer failed");
3008         }
3009     }
3010     return MSERR_OK;
3011 }
3012 
GetSize(int64_t & size)3013 int32_t AudioDataSource::GetSize(int64_t &size)
3014 {
3015     size_t bufferLen = 0;
3016     int32_t ret = screenCaptureServer_->GetInnerAudioCaptureBufferSize(bufferLen);
3017     MEDIA_LOGD("AudioDataSource::GetSize : %{public}zu", bufferLen);
3018     if (ret == MSERR_OK) {
3019         size = static_cast<int64_t>(bufferLen);
3020         return ret;
3021     }
3022     ret = screenCaptureServer_->GetMicAudioCaptureBufferSize(bufferLen);
3023     MEDIA_LOGD("AudioDataSource::GetSize : %{public}zu", bufferLen);
3024     if (ret == MSERR_OK) {
3025         size = static_cast<int64_t>(bufferLen);
3026         return ret;
3027     }
3028     return ret;
3029 }
3030 
MixAudio(char ** srcData,char * mixData,int channels,int bufferSize)3031 void AudioDataSource::MixAudio(char** srcData, char* mixData, int channels, int bufferSize)
3032 {
3033     MEDIA_LOGD("AudioDataSource MixAudio");
3034     int const max = 32767;
3035     int const min = -32768;
3036     double const splitNum = 32;
3037     int const doubleChannels = 2;
3038     double coefficient = 1;
3039     int totalNum = 0;
3040     if (channels == 0) {
3041         return;
3042     }
3043     for (totalNum = 0; totalNum < bufferSize / channels; totalNum++) {
3044         int temp = 0;
3045         for (int channelNum = 0; channelNum < channels; channelNum++) {
3046             temp += *reinterpret_cast<short*>(srcData[channelNum] + totalNum * channels);
3047         }
3048         int32_t output = static_cast<int32_t>(temp * coefficient);
3049         if (output > max) {
3050             coefficient = static_cast<double>(max) / static_cast<double>(output);
3051             output = max;
3052         }
3053         if (output < min) {
3054             coefficient = static_cast<double>(min) / static_cast<double>(output);
3055             output = min;
3056         }
3057         if (coefficient < 1) {
3058             coefficient += (static_cast<double>(1) - coefficient) / splitNum;
3059         }
3060         *reinterpret_cast<short*>(mixData + totalNum * doubleChannels) = static_cast<short>(output);
3061     }
3062 }
3063 } // namespace Media
3064 } // namespace OHOS
3065