• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "iservice_registry.h"
24 #include "pixel_map.h"
25 #include "media_log.h"
26 #include "media_errors.h"
27 #include "media_utils.h"
28 #include "uri_helper.h"
29 #include "media_dfx.h"
30 #include "scope_guard.h"
31 #include "screen_capture_listener_proxy.h"
32 #include "system_ability_definition.h"
33 #include "res_type.h"
34 #include "res_sched_client.h"
35 #include "param_wrapper.h"
36 #include <unistd.h>
37 #include <sys/stat.h>
38 #include "hitrace/tracechain.h"
39 #include "locale_config.h"
40 #include <sync_fence.h>
41 #include "parameter.h"
42 #include <unordered_map>
43 #include <algorithm>
44 #include <set>
45 #include <common/rs_common_def.h>
46 #include "session_manager_lite.h"
47 #include "window_manager_lite.h"
48 #include "want_agent_info.h"
49 #include "want_agent_helper.h"
50 #include "common_event_manager.h"
51 #ifdef PC_STANDARD
52 #include <parameters.h>
53 #endif
54 
55 using OHOS::Rosen::DMError;
56 
57 namespace {
58 const std::string DUMP_PATH = "/data/media/screen_capture.bin";
59 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_SCREENCAPTURE, "ScreenCaptureServer"};
60 }
61 
62 namespace OHOS {
63 namespace Media {
64 
65 static const std::string MP4 = "mp4";
66 static const std::string M4A = "m4a";
67 
68 static const std::string USER_CHOICE_ALLOW = "true";
69 static const std::string USER_CHOICE_DENY = "false";
70 static const std::string CHECK_BOX_SELECTED = "true";
71 static const std::string JSON_VALUE_TRUE = "true";
72 static const std::string BUTTON_NAME_MIC = "mic";
73 static const std::string BUTTON_NAME_STOP = "stop";
74 static const std::string ICON_PATH_CAPSULE_STOP = "/etc/screencapture/capsule_stop.svg";
75 static const std::string ICON_PATH_CAPSULE_STOP_2_0 = "/etc/screencapture/capsule_stop_live2.svg";
76 static const std::string ICON_PATH_NOTIFICATION = "/etc/screencapture/notification.png";
77 static const std::string ICON_PATH_MIC = "/etc/screencapture/mic.svg";
78 static const std::string ICON_PATH_MIC_OFF = "/etc/screencapture/mic_off.svg";
79 static const std::string ICON_PATH_STOP = "/etc/screencapture/light.svg";
80 static const std::string BACK_GROUND_COLOR = "#E84026";
81 #ifdef PC_STANDARD
82 static const std::string SELECT_ABILITY_NAME = "SelectWindowAbility";
83 static const int32_t SELECT_WINDOW_MISSION_ID_NUM_MAX = 2;
84 #endif
85 static const int32_t SVG_HEIGHT = 80;
86 static const int32_t SVG_WIDTH = 80;
87 static const int32_t MICROPHONE_OFF = 0;
88 static const int32_t MICROPHONE_STATE_COUNT = 2;
89 static const int32_t WINDOW_INFO_LIST_SIZE = 1;
90 #ifdef SUPPORT_SCREEN_CAPTURE_WINDOW_NOTIFICATION
91     static const int32_t NOTIFICATION_MAX_TRY_NUM = 3;
92 #endif
93 
94 static const auto NOTIFICATION_SUBSCRIBER = NotificationSubscriber();
95 static constexpr int32_t AUDIO_CHANGE_TIME = 80000; // 80 ms
96 static const int32_t UNSUPPORT_ERROR_CODE_API_VERSION_ISOLATION = 20;
97 
98 std::map<int32_t, std::weak_ptr<ScreenCaptureServer>> ScreenCaptureServer::serverMap_{};
99 std::map<int32_t, std::pair<int32_t, int32_t>> ScreenCaptureServer::saUidAppUidMap_{};
100 const int32_t ScreenCaptureServer::maxSessionId_ = 16;
101 const int32_t ScreenCaptureServer::maxAppLimit_ = 4;
102 UniqueIDGenerator ScreenCaptureServer::gIdGenerator_(ScreenCaptureServer::maxSessionId_);
103 std::list<int32_t> ScreenCaptureServer::startedSessionIDList_;
104 const int32_t ScreenCaptureServer::maxSessionPerUid_ = 4;
105 const int32_t ScreenCaptureServer::maxSCServerDataTypePerUid_ = 2;
106 std::atomic<int32_t> ScreenCaptureServer::systemScreenRecorderPid_ = -1;
107 
108 std::shared_mutex ScreenCaptureServer::mutexServerMapRWGlobal_;
109 std::shared_mutex ScreenCaptureServer::mutexListRWGlobal_;
110 std::shared_mutex ScreenCaptureServer::mutexSaAppInfoMapGlobal_;
111 
OnConnected()112 void NotificationSubscriber::OnConnected()
113 {
114     MEDIA_LOGI("NotificationSubscriber OnConnected");
115 }
116 
OnDisconnected()117 void NotificationSubscriber::OnDisconnected()
118 {
119     MEDIA_LOGI("NotificationSubscriber OnDisconnected");
120 }
121 
OnResponse(int32_t notificationId,OHOS::sptr<OHOS::Notification::NotificationButtonOption> buttonOption)122 void NotificationSubscriber::OnResponse(int32_t notificationId,
123                                         OHOS::sptr<OHOS::Notification::NotificationButtonOption> buttonOption)
124 {
125     CHECK_AND_RETURN(buttonOption != nullptr);
126     MEDIA_LOGI("NotificationSubscriber OnResponse notificationId : %{public}d, ButtonName : %{public}s ",
127         notificationId, (buttonOption->GetButtonName()).c_str());
128 
129     std::shared_ptr<ScreenCaptureServer> server =
130         ScreenCaptureServer::GetScreenCaptureServerByIdWithLock(notificationId);
131     if (server == nullptr) {
132         MEDIA_LOGW("OnResponse ScreenCaptureServer not exist, notificationId : %{public}d, ButtonName : %{public}s ",
133             notificationId, (buttonOption->GetButtonName()).c_str());
134         return;
135     }
136     if (BUTTON_NAME_STOP.compare(buttonOption->GetButtonName()) == 0) {
137         server->StopScreenCaptureByEvent(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_STOPPED_BY_USER);
138         return;
139     }
140     if (BUTTON_NAME_MIC.compare(buttonOption->GetButtonName()) == 0) {
141         server->UpdateMicrophoneEnabled();
142         return;
143     }
144 }
145 
OnDied()146 void NotificationSubscriber::OnDied()
147 {
148     MEDIA_LOGI("NotificationSubscriber OnDied");
149 }
150 
PrivateWindowListenerInScreenCapture(std::weak_ptr<ScreenCaptureServer> screenCaptureServer)151 PrivateWindowListenerInScreenCapture::PrivateWindowListenerInScreenCapture(
152     std::weak_ptr<ScreenCaptureServer> screenCaptureServer)
153 {
154     MEDIA_LOGD("0x%{public}06" PRIXPTR " PrivateWindowListenerInScreenCapture Instances create", FAKE_POINTER(this));
155     screenCaptureServer_ = screenCaptureServer;
156 }
157 
OnPrivateWindow(bool hasPrivate)158 void PrivateWindowListenerInScreenCapture::OnPrivateWindow(bool hasPrivate)
159 {
160     MEDIA_LOGI("PrivateWindowListenerInScreenCapture hasPrivateWindow: %{public}u", hasPrivate);
161     auto scrServer = screenCaptureServer_.lock();
162     if (scrServer) {
163         MEDIA_LOGI("Callback OnDMPrivateWindowChange hasPrivateWindow: %{public}u", hasPrivate);
164         scrServer->OnDMPrivateWindowChange(hasPrivate);
165     }
166 }
167 
OnConnect(Rosen::ScreenId screenId)168 void ScreenConnectListenerForSC::OnConnect(Rosen::ScreenId screenId)
169 {
170     MEDIA_LOGI("ScreenConnectListenerForSC OnConnect screenId: %{public}" PRIu64, screenId);
171 }
172 
OnDisconnect(Rosen::ScreenId screenId)173 void ScreenConnectListenerForSC::OnDisconnect(Rosen::ScreenId screenId)
174 {
175     MEDIA_LOGI("ScreenConnectListenerForSC OnDisconnect screenId: %{public}" PRIu64 "screenId_: %{public}" PRIu64,
176         screenId, screenId_);
177     auto callbackPtr = screenCaptureServer_.lock();
178     if (callbackPtr && screenId == screenId_) {
179         MEDIA_LOGI("ScreenConnectListenerForSC OnDisconnect NotifyCaptureContentChanged: %{public}" PRIu64, screenId_);
180         callbackPtr->NotifyCaptureContentChanged(
181             AVScreenCaptureContentChangedEvent::SCREEN_CAPTURE_CONTENT_UNAVAILABLE, nullptr);
182     }
183 }
184 
OnChange(Rosen::ScreenId screenId)185 void ScreenConnectListenerForSC::OnChange(Rosen::ScreenId screenId)
186 {
187     MEDIA_LOGI("ScreenConnectListenerForSC OnChange screenId: %{public}" PRIu64, screenId);
188 }
189 
AddScreenCaptureServerMap(int32_t sessionId,std::weak_ptr<ScreenCaptureServer> server)190 void ScreenCaptureServer::AddScreenCaptureServerMap(int32_t sessionId,
191     std::weak_ptr<ScreenCaptureServer> server)
192 {
193     std::unique_lock<std::shared_mutex> lock(ScreenCaptureServer::mutexServerMapRWGlobal_);
194     ScreenCaptureServer::serverMap_.insert(std::make_pair(sessionId, server));
195     MEDIA_LOGI("AddScreenCaptureServerMap end, serverMap size: %{public}d.",
196         static_cast<uint32_t>(ScreenCaptureServer::serverMap_.size()));
197 }
198 
RemoveScreenCaptureServerMap(int32_t sessionId)199 void ScreenCaptureServer::RemoveScreenCaptureServerMap(int32_t sessionId)
200 {
201     std::unique_lock<std::shared_mutex> lock(ScreenCaptureServer::mutexServerMapRWGlobal_);
202     ScreenCaptureServer::serverMap_.erase(sessionId);
203     int32_t returnId = ScreenCaptureServer::gIdGenerator_.ReturnID(sessionId);
204     if (returnId == -1) {
205         MEDIA_LOGI("RemoveScreenCaptureServerMap returnId: %{public}d is invalid", returnId);
206     }
207     MEDIA_LOGI("RemoveScreenCaptureServerMap end. sessionId: %{public}d, serverMap size: %{public}d.",
208         sessionId, static_cast<uint32_t>(ScreenCaptureServer::serverMap_.size()));
209 }
210 
CheckScreenCaptureSessionIdLimit(int32_t curAppUid)211 bool ScreenCaptureServer::CheckScreenCaptureSessionIdLimit(int32_t curAppUid)
212 {
213     int32_t countForUid = 0;
214     MEDIA_LOGI("CheckScreenCaptureSessionIdLimit start. curAppUid: %{public}d.", curAppUid);
215     {
216         std::unique_lock<std::shared_mutex> lock(ScreenCaptureServer::mutexServerMapRWGlobal_);
217         for (auto iter = ScreenCaptureServer::serverMap_.begin(); iter != ScreenCaptureServer::serverMap_.end();
218             iter++) {
219                 auto iterPtr = (iter->second).lock();
220                 if (iterPtr != nullptr) {
221                     if (curAppUid == iterPtr->GetAppUid()) {
222                         countForUid++;
223                     }
224                     CHECK_AND_RETURN_RET_LOG(countForUid <= ScreenCaptureServer::maxSessionPerUid_, false,
225                         "Create failed, uid(%{public}d) has created too many ScreenCaptureServer instances", curAppUid);
226                 }
227             }
228     }
229     MEDIA_LOGI("CheckScreenCaptureSessionIdLimit end.");
230     return true;
231 }
232 
CheckSCServerSpecifiedDataTypeNum(int32_t curAppUid,DataType dataType)233 bool ScreenCaptureServer::CheckSCServerSpecifiedDataTypeNum(int32_t curAppUid, DataType dataType)
234 {
235     int32_t countForUidDataType = 0;
236     MEDIA_LOGI("CheckSCServerSpecifiedDataTypeNum start. curAppUid: %{public}d, dataType: %{public}d.",
237         curAppUid, dataType);
238     {
239         std::unique_lock<std::shared_mutex> lock(ScreenCaptureServer::mutexServerMapRWGlobal_);
240         for (auto iter = ScreenCaptureServer::serverMap_.begin(); iter != ScreenCaptureServer::serverMap_.end();
241             iter++) {
242                 auto iterPtr = (iter->second).lock();
243                 if (iterPtr != nullptr) {
244                     if (curAppUid == iterPtr->GetAppUid() && dataType == iterPtr->GetSCServerDataType()) {
245                         countForUidDataType++;
246                     }
247                     CHECK_AND_RETURN_RET_LOG(countForUidDataType <= ScreenCaptureServer::maxSCServerDataTypePerUid_,
248                         false, "CheckSCServerSpecifiedDataTypeNum failed,"
249                         "uid(%{public}d) has created too many instances of dataType(%{public}d)", curAppUid, dataType);
250                 }
251             }
252     }
253     MEDIA_LOGI("CheckSCServerSpecifiedDataTypeNum end.");
254     return true;
255 }
256 
CountScreenCaptureAppNum(std::set<int32_t> & appSet)257 void ScreenCaptureServer::CountScreenCaptureAppNum(std::set<int32_t>& appSet)
258 {
259     std::unique_lock<std::shared_mutex> lock(ScreenCaptureServer::mutexServerMapRWGlobal_);
260     for (auto iter = ScreenCaptureServer::serverMap_.begin(); iter != ScreenCaptureServer::serverMap_.end(); iter++) {
261         auto iterPtr = iter->second.lock();
262         if (iterPtr != nullptr) {
263             appSet.insert(iterPtr->GetAppUid());
264         }
265     }
266 }
267 
CheckScreenCaptureAppLimit(int32_t curAppUid)268 bool ScreenCaptureServer::CheckScreenCaptureAppLimit(int32_t curAppUid)
269 {
270     std::set<int32_t> appSet;
271     CountScreenCaptureAppNum(appSet);
272     MEDIA_LOGI("appSet.size(): %{public}d", static_cast<int32_t>(appSet.size()));
273     if (static_cast<int32_t>(appSet.size()) > ScreenCaptureServer::maxAppLimit_) {
274         return false;
275     }
276     return true;
277 }
278 
GetScreenCaptureServerById(int32_t id)279 std::shared_ptr<ScreenCaptureServer> ScreenCaptureServer::GetScreenCaptureServerById(int32_t id)
280 {
281     auto iter = ScreenCaptureServer::serverMap_.find(id);
282     if (iter == ScreenCaptureServer::serverMap_.end()) {
283         return nullptr;
284     }
285     return (iter->second).lock();
286 }
287 
GetScreenCaptureServerByIdWithLock(int32_t id)288 std::shared_ptr<ScreenCaptureServer> ScreenCaptureServer::GetScreenCaptureServerByIdWithLock(int32_t id)
289 {
290     std::unique_lock<std::shared_mutex> lock(ScreenCaptureServer::mutexServerMapRWGlobal_);
291     return GetScreenCaptureServerById(id);
292 }
293 
GetStartedScreenCaptureServerPidList()294 std::list<int32_t> ScreenCaptureServer::GetStartedScreenCaptureServerPidList()
295 {
296     std::list<int32_t> startedScreenCapturePidList{};
297     std::unique_lock<std::shared_mutex> lock(ScreenCaptureServer::mutexServerMapRWGlobal_);
298     for (auto sessionId: ScreenCaptureServer::startedSessionIDList_) {
299         std::shared_ptr<ScreenCaptureServer> currentServer = GetScreenCaptureServerById(sessionId);
300         if (currentServer != nullptr) {
301             startedScreenCapturePidList.push_back(currentServer->GetAppPid() == 0 ? -1 : currentServer->GetAppPid());
302         }
303     }
304     return startedScreenCapturePidList;
305 }
306 
CountStartedScreenCaptureServerNumByPid(int32_t pid)307 int32_t ScreenCaptureServer::CountStartedScreenCaptureServerNumByPid(int32_t pid)
308 {
309     int32_t count = 0;
310     std::unique_lock<std::shared_mutex> lock(ScreenCaptureServer::mutexServerMapRWGlobal_);
311     for (auto sessionId: ScreenCaptureServer::startedSessionIDList_) {
312         std::shared_ptr<ScreenCaptureServer> currentServer = GetScreenCaptureServerById(sessionId);
313         if (currentServer != nullptr && currentServer->GetAppPid() == pid) {
314             count++;
315         }
316     }
317     return count;
318 }
319 
AddStartedSessionIdList(int32_t value)320 void ScreenCaptureServer::AddStartedSessionIdList(int32_t value)
321 {
322     std::unique_lock<std::shared_mutex> lock(ScreenCaptureServer::mutexListRWGlobal_);
323     ScreenCaptureServer::startedSessionIDList_.push_back(value);
324 }
325 
RemoveStartedSessionIdList(int32_t value)326 void ScreenCaptureServer::RemoveStartedSessionIdList(int32_t value)
327 {
328     std::unique_lock<std::shared_mutex> lock(ScreenCaptureServer::mutexListRWGlobal_);
329     ScreenCaptureServer::startedSessionIDList_.remove(value);
330 }
331 
GetAllStartedSessionIdList()332 std::list<int32_t> ScreenCaptureServer::GetAllStartedSessionIdList()
333 {
334     std::unique_lock<std::shared_mutex> lock(ScreenCaptureServer::mutexListRWGlobal_);
335     return GetStartedScreenCaptureServerPidList();
336 }
337 
CheckPidIsScreenRecorder(int32_t pid)338 bool ScreenCaptureServer::CheckPidIsScreenRecorder(int32_t pid)
339 {
340     MEDIA_LOGI("CheckPidIsScreenRecorder ScreenRecorder pid(%{public}d), input pid(%{public}d)",
341         (ScreenCaptureServer::systemScreenRecorderPid_).load(), pid);
342     return pid == (ScreenCaptureServer::systemScreenRecorderPid_).load();
343 }
344 
OnDMPrivateWindowChange(bool hasPrivate)345 void ScreenCaptureServer::OnDMPrivateWindowChange(bool hasPrivate)
346 {
347     MEDIA_LOGI("OnDMPrivateWindowChange hasPrivateWindow: %{public}u", hasPrivate);
348     NotifyStateChange(hasPrivate ?
349         AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_ENTER_PRIVATE_SCENE :
350         AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_EXIT_PRIVATE_SCENE);
351 }
352 
SetWindowIdList(uint64_t windowId)353 void ScreenCaptureServer::SetWindowIdList(uint64_t windowId)
354 {
355     windowIdList_.push_back(static_cast<int32_t>(windowId));
356 }
357 
GetWindowIdList()358 std::vector<int32_t> ScreenCaptureServer::GetWindowIdList()
359 {
360     return windowIdList_;
361 }
362 
GetDefaultDisplayId()363 uint64_t ScreenCaptureServer::GetDefaultDisplayId()
364 {
365     return displayScreenId_;
366 }
367 
SetCurDisplayId(uint64_t displayId)368 void ScreenCaptureServer::SetCurDisplayId(uint64_t displayId)
369 {
370     curWindowInDisplayId_ = displayId;
371 }
372 
GetCurDisplayId()373 uint64_t ScreenCaptureServer::GetCurDisplayId()
374 {
375     return curWindowInDisplayId_;
376 }
377 
SetDefaultDisplayIdOfWindows()378 void ScreenCaptureServer::SetDefaultDisplayIdOfWindows()
379 {
380     MEDIA_LOGI("SetDefaultDisplayIdOfWindows Start");
381     sptr<Rosen::Display> defaultDisplay = Rosen::DisplayManager::GetInstance().GetDefaultDisplaySync();
382     CHECK_AND_RETURN_LOG(defaultDisplay != nullptr, "SetDefaultDisplayIdOfWindows GetDefaultDisplaySync failed");
383 
384     uint64_t defaultDisplayId = defaultDisplay->GetScreenId();
385     std::unordered_map<uint64_t, uint64_t> windowDisplayIdMap;
386     auto ret = WindowManager::GetInstance().GetDisplayIdByWindowId(missionIds_, windowDisplayIdMap);
387     CHECK_AND_RETURN_LOG(ret == Rosen::WMError::WM_OK, "SetDefaultDisplayIdOfWindows GetDisplayIdByWindowId failed");
388     MEDIA_LOGI("SetDefaultDisplayIdOfWindows GetDisplayIdByWindowId ret: %{public}d", ret);
389     for (const auto& pair : windowDisplayIdMap) {
390         MEDIA_LOGD("SetDefaultDisplayIdOfWindows 0x%{public}06" PRIXPTR " WindowId:%{public}" PRIu64
391             " in DisplayId:%{public}" PRIu64, FAKE_POINTER(this), pair.first, pair.second);
392             defaultDisplayId = pair.second;
393     }
394     displayScreenId_ = defaultDisplayId;
395     SetCurDisplayId(displayScreenId_);
396     MEDIA_LOGI("SetDefaultDisplayIdOfWindows End. defaultDisplayId: %{public}" PRIu64, displayScreenId_);
397 }
398 
OnSceneSessionManagerDied(const wptr<IRemoteObject> & remote)399 void ScreenCaptureServer::OnSceneSessionManagerDied(const wptr<IRemoteObject>& remote)
400 {
401     MEDIA_LOGI("OnSceneSessionManagerDied Start");
402     windowLifecycleListener_ = nullptr;
403     windowInfoChangedListener_ = nullptr;
404 
405     auto remoteObj = remote.promote();
406     if (!remoteObj) {
407         MEDIA_LOGD("invalid remote object");
408         return;
409     }
410     remoteObj->RemoveDeathRecipient(lifecycleListenerDeathRecipient_);
411     lifecycleListenerDeathRecipient_ = nullptr;
412     MEDIA_LOGI("SCB Crash! Please Check!");
413 
414     int32_t ret = RegisterWindowRelatedListener();
415     CHECK_AND_RETURN_LOG(ret == MSERR_OK, "OnSceneSessionManagerDied: RegisterWindowRelatedListener failed.");
416     MEDIA_LOGI("OnSceneSessionManagerDied End");
417 }
418 
RegisterWindowLifecycleListener(std::vector<int32_t> windowIdList)419 int32_t ScreenCaptureServer::RegisterWindowLifecycleListener(std::vector<int32_t> windowIdList)
420 {
421     MEDIA_LOGI("RegisterWindowLifecycleListener start, windowIdListSize: %{public}d",
422         static_cast<int32_t>(windowIdList.size()));
423 
424     auto sceneSessionManager = SessionManagerLite::GetInstance().GetSceneSessionManagerLiteProxy();
425     CHECK_AND_RETURN_RET_LOG(sceneSessionManager != nullptr, MSERR_INVALID_OPERATION,
426         "sceneSessionManager is nullptr, RegisterWindowLifecycleListener failed.");
427 
428     if (!lifecycleListenerDeathRecipient_) {
429         MEDIA_LOGD("RegisterWindowLifecycleListener lifecycleListenerDeathRecipient_ is nullptr");
430         auto task = [weakThis = weak_from_this()] (const wptr<IRemoteObject>& remote) {
431             auto SCServer = weakThis.lock();
432             if (SCServer) {
433                 SCServer->OnSceneSessionManagerDied(remote);
434             }
435         };
436         lifecycleListenerDeathRecipient_ = sptr<SCDeathRecipientListener>::MakeSptr(task);
437 
438         auto listenerObject = sceneSessionManager->AsObject();
439         if (listenerObject) {
440             listenerObject->AddDeathRecipient(lifecycleListenerDeathRecipient_);
441             MEDIA_LOGI("RegisterWindowLifecycleListener AddDeathRecipient success.");
442         }
443     }
444 
445     if (windowLifecycleListener_ != nullptr) {
446         MEDIA_LOGI("RegisterWindowLifecycleListener windowLifecycleListener already registered");
447         return MSERR_OK;
448     }
449     std::weak_ptr<ScreenCaptureServer> screenCaptureServer(shared_from_this());
450     sptr<SCWindowLifecycleListener> listener(new (std::nothrow) SCWindowLifecycleListener(screenCaptureServer));
451     CHECK_AND_RETURN_RET_LOG(listener != nullptr, MSERR_INVALID_OPERATION,
452         "create new windowLifecycleListener failed.");
453     windowLifecycleListener_ = listener;
454 
455     Rosen::WMError ret = sceneSessionManager->RegisterSessionLifecycleListenerByIds(windowLifecycleListener_,
456         windowIdList);
457     CHECK_AND_RETURN_RET_LOG(ret == Rosen::WMError::WM_OK, MSERR_INVALID_OPERATION,
458         "RegisterSessionLifecycleListenerByIds failed.");
459 
460     MEDIA_LOGI("RegisterWindowLifecycleListener end.");
461     return MSERR_OK;
462 }
463 
UnRegisterWindowLifecycleListener()464 int32_t ScreenCaptureServer::UnRegisterWindowLifecycleListener()
465 {
466     MEDIA_LOGI("UnRegisterWindowLifecycleListener start.");
467     auto sceneSessionManager = SessionManagerLite::GetInstance().GetSceneSessionManagerLiteProxy();
468     CHECK_AND_RETURN_RET_LOG(sceneSessionManager != nullptr, MSERR_INVALID_OPERATION,
469         "sceneSessionManager is nullptr, UnRegisterWindowLifecycleListener failed.");
470 
471     if (lifecycleListenerDeathRecipient_) {
472         MEDIA_LOGD("UnRegisterWindowLifecycleListener lifecycleListenerDeathRecipient_ != nullptr");
473         auto listenerObject = sceneSessionManager->AsObject();
474         if (listenerObject) {
475             listenerObject->RemoveDeathRecipient(lifecycleListenerDeathRecipient_);
476             MEDIA_LOGI("UnRegisterWindowLifecycleListener RemoveDeathRecipient success.");
477         }
478         lifecycleListenerDeathRecipient_ = nullptr;
479     }
480 
481     if (!windowLifecycleListener_) {
482         MEDIA_LOGI("windowLifecycleListener already unregistered");
483         return MSERR_OK;
484     }
485     Rosen::WMError ret = sceneSessionManager->UnregisterSessionLifecycleListener(windowLifecycleListener_);
486     CHECK_AND_RETURN_RET_LOG(ret == Rosen::WMError::WM_OK, MSERR_INVALID_OPERATION,
487         "UnRegisterWindowLifecycleListener failed.");
488     windowLifecycleListener_ = nullptr;
489 
490     MEDIA_LOGI("UnRegisterWindowLifecycleListener end.");
491     return MSERR_OK;
492 }
493 
RegisterWindowInfoChangedListener()494 int32_t ScreenCaptureServer::RegisterWindowInfoChangedListener()
495 {
496     MEDIA_LOGI("RegisterWindowInfoChangedListener start.");
497 
498     if (windowInfoChangedListener_) {
499         MEDIA_LOGI("RegisterWindowInfoChangedListener: windowInfoChangedListener already registered");
500         return MSERR_OK;
501     }
502 
503     std::weak_ptr<ScreenCaptureServer> screenCaptureServer(shared_from_this());
504     sptr<SCWindowInfoChangedListener> listener(new (std::nothrow) SCWindowInfoChangedListener(screenCaptureServer));
505     CHECK_AND_RETURN_RET_LOG(listener != nullptr, MSERR_INVALID_OPERATION,
506         "create new windowInfoChangedListener failed.");
507     windowInfoChangedListener_ = listener;
508     windowInfoChangedListener_->AddInterestInfo(Rosen::WindowInfoKey::WINDOW_ID);
509     CHECK_AND_RETURN_RET_LOG(!windowIdList_.empty(), MSERR_INVALID_OPERATION,
510         "windowIdList is empty, AddInterestWindowId failed.");
511     windowInfoChangedListener_->AddInterestWindowId(windowIdList_.front());
512 
513     std::unordered_set<Rosen::WindowInfoKey> observedInfo;
514     observedInfo.insert(Rosen::WindowInfoKey::DISPLAY_ID);
515     Rosen::WMError ret = Rosen::WindowManager::GetInstance().RegisterWindowInfoChangeCallback(
516         observedInfo, windowInfoChangedListener_);
517     CHECK_AND_RETURN_RET_LOG(ret == Rosen::WMError::WM_OK, MSERR_INVALID_OPERATION,
518         "RegisterWindowInfoChangedListener failed.");
519 
520     MEDIA_LOGI("RegisterWindowInfoChangedListener end");
521     return MSERR_OK;
522 }
523 
UnRegisterWindowInfoChangedListener()524 int32_t ScreenCaptureServer::UnRegisterWindowInfoChangedListener()
525 {
526     MEDIA_LOGI("UnRegisterWindowInfoChangedListener start.");
527 
528     if (!windowInfoChangedListener_) {
529         MEDIA_LOGI("UnRegisterWindowInfoChangedListener: windowInfoChangedListener already unregistered");
530         return MSERR_OK;
531     }
532 
533     std::unordered_set<Rosen::WindowInfoKey> observedInfo;
534     observedInfo.insert(Rosen::WindowInfoKey::DISPLAY_ID);
535     Rosen::WMError ret = Rosen::WindowManager::GetInstance().UnregisterWindowInfoChangeCallback(
536         observedInfo, windowInfoChangedListener_);
537     CHECK_AND_RETURN_RET_LOG(ret == Rosen::WMError::WM_OK, MSERR_INVALID_OPERATION,
538         "UnRegisterWindowInfoChangedListener failed.");
539     windowInfoChangedListener_ = nullptr;
540 
541     MEDIA_LOGI("UnRegisterWindowInfoChangedListener end");
542     return MSERR_OK;
543 }
544 
RegisterWindowRelatedListener()545 int32_t ScreenCaptureServer::RegisterWindowRelatedListener()
546 {
547     int32_t ret = RegisterWindowLifecycleListener(windowIdList_);
548     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret,
549         "RegisterWindowRelatedListener RegisterWindowLifecycleListener failed");
550     ret = RegisterWindowInfoChangedListener();
551     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret,
552         "RegisterWindowRelatedListener RegisterWindowInfoChangedListener failed");
553     return MSERR_OK;
554 }
555 
NotifyCaptureContentChanged(AVScreenCaptureContentChangedEvent event,ScreenCaptureRect * area)556 void ScreenCaptureServer::NotifyCaptureContentChanged(AVScreenCaptureContentChangedEvent event,
557     ScreenCaptureRect* area)
558 {
559     if (screenCaptureCb_ != nullptr && captureState_ != AVScreenCaptureState::STOPPED) {
560         MEDIA_LOGI("NotifyCaptureContentChanged event: %{public}d", event);
561         screenCaptureCb_->OnCaptureContentChanged(event, area);
562     }
563 }
564 
SCWindowLifecycleListener(std::weak_ptr<ScreenCaptureServer> screenCaptureServer)565 SCWindowLifecycleListener::SCWindowLifecycleListener(std::weak_ptr<ScreenCaptureServer> screenCaptureServer)
566 {
567     MEDIA_LOGD("0x%{public}06" PRIXPTR " SCWindowLifecycleListener Instances create", FAKE_POINTER(this));
568     screenCaptureServer_ = screenCaptureServer;
569 }
570 
OnLifecycleEvent(SessionLifecycleEvent event,const LifecycleEventPayload & payload)571 void SCWindowLifecycleListener::OnLifecycleEvent(SessionLifecycleEvent event, const LifecycleEventPayload& payload)
572 {
573     MEDIA_LOGI("SCWindowLifecycleListener::OnLifecycleEvent Start.");
574     auto SCServer = screenCaptureServer_.lock();
575     CHECK_AND_RETURN_LOG(SCServer != nullptr, "screenCaptureServer is nullptr");
576     switch (event) {
577         case SessionLifecycleEvent::FOREGROUND: {
578             MEDIA_LOGI("OnLifecycleEvent: SessionLifecycleEvent::FOREGROUND");
579             if (SCServer->GetCurDisplayId() == SCServer->GetDefaultDisplayId() &&
580                 SCServer->GetWindowIdList().size() > 0) {
581                 WindowInfoOption windowInfoOption;
582                 std::vector<sptr<WindowInfo>> infos;
583                 windowInfoOption.windowId = SCServer->GetWindowIdList().front();
584                 Rosen::WMError ret = Rosen::WindowManager::GetInstance().ListWindowInfo(windowInfoOption, infos);
585                 CHECK_AND_RETURN_LOG(ret == Rosen::WMError::WM_OK && infos.size() > 0, "ListWindowInfo failed.");
586                 SCServer->NotifyCaptureContentChanged(
587                     AVScreenCaptureContentChangedEvent::SCREEN_CAPTURE_CONTENT_VISIBLE,
588                     reinterpret_cast<ScreenCaptureRect*>(&(infos.front()->windowLayoutInfo.rect)));
589             }
590             break;
591         }
592         case SessionLifecycleEvent::BACKGROUND: {
593             MEDIA_LOGI("OnLifecycleEvent: SessionLifecycleEvent::BACKGROUND");
594             if (SCServer->GetCurDisplayId() == SCServer->GetDefaultDisplayId()) {
595                 SCServer->NotifyCaptureContentChanged(AVScreenCaptureContentChangedEvent::SCREEN_CAPTURE_CONTENT_HIDE,
596                     nullptr);
597             }
598             break;
599         }
600         case SessionLifecycleEvent::DESTROYED: {
601             MEDIA_LOGI("OnLifecycleEvent: SessionLifecycleEvent::DESTROYED");
602             SCServer->NotifyCaptureContentChanged(
603                 AVScreenCaptureContentChangedEvent::SCREEN_CAPTURE_CONTENT_UNAVAILABLE, nullptr);
604             break;
605         }
606         default: {
607             MEDIA_LOGD("OnLifecycleEvent: other event");
608             break;
609         }
610     }
611     MEDIA_LOGI("SCWindowLifecycleListener::OnLifecycleEvent End.");
612 }
613 
SCWindowInfoChangedListener(std::weak_ptr<ScreenCaptureServer> screenCaptureServer)614 SCWindowInfoChangedListener::SCWindowInfoChangedListener(std::weak_ptr<ScreenCaptureServer> screenCaptureServer)
615 {
616     MEDIA_LOGD("0x%{public}06" PRIXPTR " SCWindowInfoChangedListener Instances create", FAKE_POINTER(this));
617     screenCaptureServer_ = screenCaptureServer;
618 }
619 
OnWindowInfoChanged(const std::vector<std::unordered_map<WindowInfoKey,WindowChangeInfoType>> & windowInfoList)620 void SCWindowInfoChangedListener::OnWindowInfoChanged(
621     const std::vector<std::unordered_map<WindowInfoKey, WindowChangeInfoType>>& windowInfoList)
622 {
623     MEDIA_LOGI("SCWindowInfoChangedListener::OnWindowInfoChanged start.");
624     auto SCServer = screenCaptureServer_.lock();
625     CHECK_AND_RETURN_LOG(SCServer != nullptr, "screenCaptureServer is nullptr");
626 
627     std::vector<std::unordered_map<WindowInfoKey, WindowChangeInfoType>> myWindowInfoList = windowInfoList;
628     if (myWindowInfoList.size() < WINDOW_INFO_LIST_SIZE ||
629         SCServer->GetWindowIdList().size() < WINDOW_INFO_LIST_SIZE) {
630         MEDIA_LOGI("myWindowInfoList or windowIdList is invalid.");
631         return;
632     }
633 
634     MEDIA_LOGI("OnWindowInfoChanged: the displayId of interestWindowId changed!");
635     auto iter = myWindowInfoList.front().find(WindowInfoKey::DISPLAY_ID);
636     if (iter != myWindowInfoList.front().end()) {
637         uint64_t displayId = std::get<uint64_t>(iter->second);
638         MEDIA_LOGI("OnWindowInfoChanged: the curDisplayId: %{public}" PRIu64, displayId);
639         SCServer->SetCurDisplayId(displayId);
640         if (displayId == SCServer->GetDefaultDisplayId()) {
641             MEDIA_LOGI("OnWindowInfoChanged: window back to initial display!");
642             if (SCServer->GetWindowIdList().size() > 0) {
643                 WindowInfoOption windowInfoOption;
644                 std::vector<sptr<WindowInfo>> infos;
645                 windowInfoOption.windowId = SCServer->GetWindowIdList().front();
646                 Rosen::WMError ret = Rosen::WindowManager::GetInstance().ListWindowInfo(windowInfoOption, infos);
647                 CHECK_AND_RETURN_LOG(ret == Rosen::WMError::WM_OK && infos.size() > 0, "ListWindowInfo failed.");
648                 SCServer->NotifyCaptureContentChanged(
649                     AVScreenCaptureContentChangedEvent::SCREEN_CAPTURE_CONTENT_VISIBLE,
650                     reinterpret_cast<ScreenCaptureRect*>(&(infos.front()->windowLayoutInfo.rect)));
651             }
652         } else {
653             MEDIA_LOGI("OnWindowInfoChanged: window switched to new display!");
654             SCServer->NotifyCaptureContentChanged(
655                 AVScreenCaptureContentChangedEvent::SCREEN_CAPTURE_CONTENT_HIDE, nullptr);
656         }
657     } else {
658         MEDIA_LOGI("OnWindowInfoChanged myWindowInfoList cannot find DISPLAY_ID_KEY");
659     }
660 }
661 
CanScreenCaptureInstanceBeCreate(int32_t appUid)662 bool ScreenCaptureServer::CanScreenCaptureInstanceBeCreate(int32_t appUid)
663 {
664     MEDIA_LOGI("CanScreenCaptureInstanceBeCreate start.");
665     CHECK_AND_RETURN_RET_LOG(ScreenCaptureServer::serverMap_.size() <= ScreenCaptureServer::maxSessionId_, false,
666         "ScreenCaptureInstanceCanBeCreate exceed ScreenCaptureServer instances limit.");
667     MEDIA_LOGI("curAppUid: %{public}d", appUid);
668     CHECK_AND_RETURN_RET_LOG(CheckScreenCaptureAppLimit(appUid), false,
669         "CurScreenCaptureAppNum reach limit, cannot create more app.");
670     return CheckScreenCaptureSessionIdLimit(appUid);
671 }
672 
CreateScreenCaptureNewInstance()673 std::shared_ptr<IScreenCaptureService> ScreenCaptureServer::CreateScreenCaptureNewInstance()
674 {
675     MEDIA_LOGI("CreateScreenCaptureNewInstance");
676     int32_t id = ScreenCaptureServer::gIdGenerator_.GetNewID();
677     CHECK_AND_RETURN_RET_LOG(id != -1, nullptr, "GetNewID failed.");
678     MEDIA_LOGI("CreateScreenCaptureNewInstance newId: %{public}d", id);
679     std::shared_ptr<ScreenCaptureServer> server = std::make_shared<ScreenCaptureServer>();
680     CHECK_AND_RETURN_RET_LOG(server != nullptr, nullptr, "Failed to create ScreenCaptureServer.");
681     server->SetSessionId(id);
682     server->GetAndSetAppVersion();
683     AddScreenCaptureServerMap(id, server);
684     return std::static_pointer_cast<IScreenCaptureService>(server);
685 }
686 
IsSAServiceCalling()687 bool ScreenCaptureServer::IsSAServiceCalling()
688 {
689     MEDIA_LOGI("ScreenCaptureServer::IsSAServiceCalling START.");
690     const auto tokenId = IPCSkeleton::GetCallingTokenID();
691     const auto tokenTypeFlag = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId);
692     if (tokenTypeFlag == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE ||
693         tokenTypeFlag == Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL) {
694         MEDIA_LOGI("ScreenCaptureServer::IsSAServiceCalling true.");
695         return true;
696     }
697     return false;
698 }
699 
Create()700 std::shared_ptr<IScreenCaptureService> ScreenCaptureServer::Create()
701 {
702     for (auto sessionId: ScreenCaptureServer::startedSessionIDList_) {
703         MEDIA_LOGD("ScreenCaptureServer::Create sessionId: %{public}d", sessionId);
704     }
705     MEDIA_LOGI("ScreenCaptureServer Create start.");
706     return CreateScreenCaptureNewInstance();
707 }
708 
AddSaAppInfoMap(int32_t saUid,int32_t curAppUid)709 void ScreenCaptureServer::AddSaAppInfoMap(int32_t saUid, int32_t curAppUid)
710 {
711     std::unique_lock<std::shared_mutex> lock(ScreenCaptureServer::mutexSaAppInfoMapGlobal_);
712     if (ScreenCaptureServer::saUidAppUidMap_.find(saUid) == ScreenCaptureServer::saUidAppUidMap_.end()) {
713         ScreenCaptureServer::saUidAppUidMap_.insert({saUid, std::make_pair(curAppUid, 1)});
714         MEDIA_LOGI("AddSaAppInfoMap insert SUCCESS! mapSize: %{public}d",
715             static_cast<uint32_t>(ScreenCaptureServer::saUidAppUidMap_.size()));
716     } else {
717         ScreenCaptureServer::saUidAppUidMap_[saUid].second++;
718     }
719 }
720 
RemoveSaAppInfoMap(int32_t saUid)721 void ScreenCaptureServer::RemoveSaAppInfoMap(int32_t saUid)
722 {
723     CHECK_AND_RETURN(saUid != -1);
724     MEDIA_LOGI("RemoveSaAppInfoMap saUid: %{public}d is valid.", saUid);
725     std::unique_lock<std::shared_mutex> lock(ScreenCaptureServer::mutexSaAppInfoMapGlobal_);
726     if (ScreenCaptureServer::saUidAppUidMap_.find(saUid) != ScreenCaptureServer::saUidAppUidMap_.end() &&
727         ScreenCaptureServer::saUidAppUidMap_[saUid].second > 0) {
728         ScreenCaptureServer::saUidAppUidMap_[saUid].second--;
729         if (ScreenCaptureServer::saUidAppUidMap_[saUid].second == 0) {
730             ScreenCaptureServer::saUidAppUidMap_.erase(saUid);
731         }
732     }
733     MEDIA_LOGI("RemoveSaAppInfoMap END! mapSize: %{public}d",
734         static_cast<uint32_t>(ScreenCaptureServer::saUidAppUidMap_.size()));
735 }
736 
CheckSaUid(int32_t saUid,int32_t appUid)737 bool ScreenCaptureServer::CheckSaUid(int32_t saUid, int32_t appUid)
738 {
739     std::unique_lock<std::shared_mutex> lock(ScreenCaptureServer::mutexSaAppInfoMapGlobal_);
740     if (ScreenCaptureServer::saUidAppUidMap_.find(saUid) != ScreenCaptureServer::saUidAppUidMap_.end()) {
741         if (ScreenCaptureServer::saUidAppUidMap_[saUid].first != appUid ||
742             (ScreenCaptureServer::saUidAppUidMap_[saUid].first == appUid &&
743             ScreenCaptureServer::saUidAppUidMap_[saUid].second >= ScreenCaptureServer::maxSessionPerUid_)) {
744                 MEDIA_LOGI("saUid Invalid! saUid: %{public}d linked with appUid: %{public}d, curAppUid: %{public}d",
745                     saUid, ScreenCaptureServer::saUidAppUidMap_[saUid].first, appUid);
746                 return false;
747             }
748     }
749     return true;
750 }
751 
IsSaUidValid(int32_t saUid,int32_t appUid)752 bool ScreenCaptureServer::IsSaUidValid(int32_t saUid, int32_t appUid)
753 {
754     CHECK_AND_RETURN_RET_LOG(saUid >= 0 && appUid >= 0, false, "saUid or appUid is invalid.");
755     CHECK_AND_RETURN_RET_LOG(IsSAServiceCalling(), false, "fake SAServiceCalling!");
756     return CheckSaUid(saUid, appUid);
757 }
758 
SetAndCheckAppInfo(OHOS::AudioStandard::AppInfo & appInfo)759 int32_t ScreenCaptureServer::SetAndCheckAppInfo(OHOS::AudioStandard::AppInfo &appInfo)
760 {
761     std::lock_guard<std::mutex> lock(mutex_);
762     MEDIA_LOGI("ScreenCaptureServer::SetAndCheckAppInfo(appInfo)");
763     const int32_t saUid = IPCSkeleton::GetCallingUid();
764     if (saUid >= 0) {
765         SetSCServerSaUid(saUid);
766     }
767     if (!IsSaUidValid(saUid, appInfo.appUid)) {
768         MEDIA_LOGI("SetAndCheckAppInfo failed, saUid-appUid exists.");
769         SetSCServerSaUid(-1);
770         return MSERR_INVALID_OPERATION;
771     }
772 
773     appInfo_.appUid = appInfo.appUid;
774     appInfo_.appPid = appInfo.appPid;
775     appInfo_.appTokenId = appInfo.appTokenId;
776     appInfo_.appFullTokenId = appInfo.appFullTokenId;
777     appName_ = GetClientBundleName(appInfo_.appUid);
778     AddSaAppInfoMap(saUid, appInfo_.appUid);
779     MEDIA_LOGI("ScreenCaptureServer::SetAndCheckAppInfo end.");
780     return MSERR_OK;
781 }
782 
SetSCServerSaUid(int32_t saUid)783 void ScreenCaptureServer::SetSCServerSaUid(int32_t saUid)
784 {
785     saUid_ = saUid;
786 }
787 
GetSCServerSaUid()788 int32_t ScreenCaptureServer::GetSCServerSaUid()
789 {
790     return saUid_;
791 }
792 
SetAndCheckSaLimit(OHOS::AudioStandard::AppInfo & appInfo)793 int32_t ScreenCaptureServer::SetAndCheckSaLimit(OHOS::AudioStandard::AppInfo &appInfo)
794 {
795     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetAndCheckSaLimit START.", FAKE_POINTER(this));
796     int32_t ret = SetAndCheckAppInfo(appInfo);
797     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "SetAndCheckSaLimit failed, saUid exists.");
798     bool createFlag = CanScreenCaptureInstanceBeCreate(appInfo.appUid);
799     if (!createFlag) {
800         MEDIA_LOGI("SetAndCheckSaLimit failed, cannot create ScreenCapture Instance.");
801         SetSCServerSaUid(-1);
802         return MSERR_INVALID_OPERATION;
803     }
804     MEDIA_LOGI("SetAndCheckSaLimit SUCCESS! appUid: %{public}d, saUid: %{public}d",
805         appInfo.appUid, GetSCServerSaUid());
806     return MSERR_OK;
807 }
808 
SetAndCheckLimit()809 int32_t ScreenCaptureServer::SetAndCheckLimit()
810 {
811     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetAndCheckLimit START.", FAKE_POINTER(this));
812     bool createFlag = CanScreenCaptureInstanceBeCreate(IPCSkeleton::GetCallingUid());
813     CHECK_AND_RETURN_RET_LOG(createFlag, MSERR_INVALID_OPERATION,
814         "SetAndCheckLimit failed, cannot create ScreenCapture Instance.");
815     return MSERR_OK;
816 }
817 
GetRunningScreenCaptureInstancePid(std::list<int32_t> & pidList)818 int32_t ScreenCaptureServer::GetRunningScreenCaptureInstancePid(std::list<int32_t> &pidList)
819 {
820     MEDIA_LOGI("GetRunningScreenCaptureInstancePid in");
821     pidList = GetAllStartedSessionIdList();
822     return MSERR_OK;
823 }
824 
GetChoiceFromJson(Json::Value & root,const std::string & content,std::string key,std::string & value)825 void ScreenCaptureServer::GetChoiceFromJson(Json::Value &root,
826     const std::string &content, std::string key, std::string &value)
827 {
828     Json::Reader reader;
829     bool parsingSuccessful = reader.parse(content, root);
830     if (!parsingSuccessful || root.type() != Json::objectValue) {
831         MEDIA_LOGE("Error parsing the string");
832         return;
833     }
834     const Json::Value keyJson = root[key];
835     if (!keyJson.isNull() && keyJson.isString()) {
836         value = keyJson.asString();
837     }
838 }
839 
GetValueFromJson(Json::Value & root,const std::string & content,const std::string key,bool & value)840 void ScreenCaptureServer::GetValueFromJson(Json::Value &root,
841     const std::string &content, const std::string key, bool &value)
842 {
843     value = false;
844 
845     Json::Reader reader;
846     bool parsingSuccessful = reader.parse(content, root);
847     if (!parsingSuccessful || root.type() != Json::objectValue) {
848         MEDIA_LOGE("Error parsing the string");
849         return;
850     }
851     const Json::Value keyJson = root[key];
852     if (!keyJson.isNull() && keyJson.isString()) {
853         if (JSON_VALUE_TRUE.compare(keyJson.asString()) == 0) {
854             value = true;
855         } else {
856             value = false;
857         }
858     }
859     MEDIA_LOGI("GetValueFromJson value: %{public}d", value);
860 }
861 
SetCaptureConfig(CaptureMode captureMode,int32_t missionId)862 void ScreenCaptureServer::SetCaptureConfig(CaptureMode captureMode, int32_t missionId)
863 {
864     captureConfig_.captureMode = captureMode;
865     if (missionId != -1) { // -1 无效值
866         captureConfig_.videoInfo.videoCapInfo.taskIDs = { missionId };
867     } else {
868         captureConfig_.videoInfo.videoCapInfo.taskIDs = {};
869     }
870 }
871 
PrepareSelectWindow(Json::Value & root)872 void ScreenCaptureServer::PrepareSelectWindow(Json::Value &root)
873 {
874     if (root.type() != Json::objectValue) {
875         return;
876     }
877     const Json::Value displayIdJson = root["displayId"];
878     if (!displayIdJson.isNull() && displayIdJson.isInt() && displayIdJson.asInt64() >= 0) {
879         uint64_t displayId = static_cast<uint64_t>(displayIdJson.asInt64());
880         MEDIA_LOGI("Report Select DisplayId: %{public}" PRIu64, displayId);
881         SetDisplayId(displayId);
882         // 手机模式 missionId displayId 均为-1
883         SetCaptureConfig(CaptureMode::CAPTURE_SPECIFIED_SCREEN, -1);
884     }
885     const Json::Value missionIdJson = root["missionId"];
886     if (!missionIdJson.isNull() && missionIdJson.isInt() && missionIdJson.asInt() >= 0) {
887         int32_t missionId = missionIdJson.asInt();
888         MEDIA_LOGI("Report Select MissionId: %{public}d", missionId);
889         SetMissionId(missionId);
890         SetCaptureConfig(CaptureMode::CAPTURE_SPECIFIED_WINDOW, missionId);
891     }
892     ScreenCaptureUserSelectionInfo selectionInfo = {0, static_cast<uint64_t>(0)};
893     PrepareUserSelectionInfo(selectionInfo);
894     NotifyUserSelected(selectionInfo);
895 }
896 
ReportAVScreenCaptureUserChoice(int32_t sessionId,const std::string & content)897 int32_t ScreenCaptureServer::ReportAVScreenCaptureUserChoice(int32_t sessionId, const std::string &content)
898 {
899     MEDIA_LOGI("ReportAVScreenCaptureUserChoice sessionId: %{public}d, content: %{public}s",
900         sessionId, content.c_str());
901     std::shared_ptr<ScreenCaptureServer> server = GetScreenCaptureServerByIdWithLock(sessionId);
902     CHECK_AND_RETURN_RET_LOG(server != nullptr, MSERR_UNKNOWN,
903         "ReportAVScreenCaptureUserChoice failed to get instance, sessionId: %{public}d", sessionId);
904     Json::Value root;
905     if (server->captureState_ == AVScreenCaptureState::POPUP_WINDOW) {
906         return server->HandlePopupWindowCase(root, content);
907     } else if (server->GetSCServerDataType() == DataType::ORIGINAL_STREAM &&
908         server->captureState_ == AVScreenCaptureState::STARTED) {
909         return server->HandleStreamDataCase(root, content);
910     }
911     return MSERR_UNKNOWN;
912 }
913 
HandlePopupWindowCase(Json::Value & root,const std::string & content)914 int32_t ScreenCaptureServer::HandlePopupWindowCase(Json::Value& root, const std::string &content)
915 {
916     MEDIA_LOGI("ReportAVScreenCaptureUserChoice captureState is %{public}d", AVScreenCaptureState::POPUP_WINDOW);
917     std::string choice = "false";
918     GetChoiceFromJson(root, content, std::string("choice"), choice);
919     GetValueFromJson(root, content, std::string("checkBoxSelected"), checkBoxSelected_);
920 
921     systemPrivacyProtectionSwitch_ = checkBoxSelected_;
922     appPrivacyProtectionSwitch_ = checkBoxSelected_;
923     MEDIA_LOGI("ReportAVScreenCaptureUserChoice checkBoxSelected: %{public}d", checkBoxSelected_);
924 
925     if (showShareSystemAudioBox_) {
926         GetValueFromJson(root, content, std::string("isInnerAudioBoxSelected"), isInnerAudioBoxSelected_);
927     }
928     MEDIA_LOGI("ReportAVScreenCaptureUserChoice showShareSystemAudioBox: %{public}d,"
929         "isInnerAudioBoxSelected: %{public}d", showShareSystemAudioBox_,
930         isInnerAudioBoxSelected_);
931 
932     if (USER_CHOICE_ALLOW.compare(choice) == 0) {
933         PrepareSelectWindow(root);
934         int32_t ret = OnReceiveUserPrivacyAuthority(true);
935         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret,
936             "ReportAVScreenCaptureUserChoice user choice is true but start failed");
937         MEDIA_LOGI("ReportAVScreenCaptureUserChoice user choice is true and start success");
938         return MSERR_OK;
939     } else if (USER_CHOICE_DENY.compare(choice) == 0) {
940         return OnReceiveUserPrivacyAuthority(false);
941     } else {
942         MEDIA_LOGW("ReportAVScreenCaptureUserChoice user choice is not support");
943     }
944     return MSERR_UNKNOWN;
945 }
946 
HandleStreamDataCase(Json::Value & root,const std::string & content)947 int32_t ScreenCaptureServer::HandleStreamDataCase(Json::Value& root, const std::string &content)
948 {
949     bool stopRecord = false;
950     GetValueFromJson(root, content, std::string("stopRecording"), stopRecord);
951     if (stopRecord) {
952         StopScreenCaptureByEvent(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_STOPPED_BY_USER);
953         MEDIA_LOGI("ReportAVScreenCaptureUserChoice user stop record");
954         return MSERR_OK;
955     }
956 
957     GetValueFromJson(root, content, std::string("appPrivacyProtectionSwitch"),
958         appPrivacyProtectionSwitch_);
959     GetValueFromJson(root, content, std::string("systemPrivacyProtectionSwitch"),
960         systemPrivacyProtectionSwitch_);
961 
962     SystemPrivacyProtected(virtualScreenId_, systemPrivacyProtectionSwitch_);
963     AppPrivacyProtected(virtualScreenId_, appPrivacyProtectionSwitch_);
964 
965     std::lock_guard<std::mutex> lock(mutex_);
966     NotificationRequest request;
967     SetPublishRequest(GetLocalLiveViewContent(), notificationId_, request);
968     request.SetWantAgent(GetWantAgent(callingLabel_, sessionId_));
969     return NotificationHelper::PublishNotification(request);
970 }
971 
GetAVScreenCaptureConfigurableParameters(int32_t sessionId,std::string & resultStr)972 int32_t ScreenCaptureServer::GetAVScreenCaptureConfigurableParameters(int32_t sessionId, std::string &resultStr)
973 {
974     MEDIA_LOGI("GetAVScreenCaptureConfigurableParameters sessionId :%{public}d",
975         sessionId);
976     std::shared_ptr<ScreenCaptureServer> server = GetScreenCaptureServerByIdWithLock(sessionId);
977     CHECK_AND_RETURN_RET_LOG(server != nullptr, MSERR_UNKNOWN,
978         "GetAVScreenCaptureConfigurableParameters failed to get instance, sessionId: %{public}d", sessionId);
979     Json::Value root;
980     root["appPrivacyProtectionSwitch"] = server->appPrivacyProtectionSwitch_;
981     root["systemPrivacyProtectionSwitch"] = server->systemPrivacyProtectionSwitch_;
982     Json::FastWriter fastWriter;
983     resultStr = fastWriter.write(root);
984     MEDIA_LOGI("GetAVScreenCaptureConfigurableParameters res: %{public}s", resultStr.c_str());
985     return MSERR_OK;
986 }
987 
GetAppPid()988 int32_t ScreenCaptureServer::GetAppPid()
989 {
990     return appInfo_.appPid;
991 }
992 
GetAppUid()993 int32_t ScreenCaptureServer::GetAppUid()
994 {
995     return appInfo_.appUid;
996 }
997 
GetSCServerDataType()998 DataType ScreenCaptureServer::GetSCServerDataType()
999 {
1000     return captureConfig_.dataType;
1001 }
1002 
IsMicrophoneSwitchTurnOn()1003 bool ScreenCaptureServer::IsMicrophoneSwitchTurnOn()
1004 {
1005     return isMicrophoneSwitchTurnOn_;
1006 }
1007 
IsMicrophoneCaptureRunning()1008 bool ScreenCaptureServer::IsMicrophoneCaptureRunning()
1009 {
1010     return micAudioCapture_ && micAudioCapture_->IsRecording();
1011 }
1012 
IsInnerCaptureRunning()1013 bool ScreenCaptureServer::IsInnerCaptureRunning()
1014 {
1015     return innerAudioCapture_ && innerAudioCapture_->IsRecording();
1016 }
1017 
IsSCRecorderFileWithVideo()1018 bool ScreenCaptureServer::IsSCRecorderFileWithVideo()
1019 {
1020     return recorderFileWithVideo_.load();
1021 }
1022 
GetInnerAudioCapture()1023 std::shared_ptr<AudioCapturerWrapper> ScreenCaptureServer::GetInnerAudioCapture()
1024 {
1025     return innerAudioCapture_;
1026 }
1027 
SetInnerAudioCapture(std::shared_ptr<AudioCapturerWrapper> innerAudioCapture)1028 void ScreenCaptureServer::SetInnerAudioCapture(std::shared_ptr<AudioCapturerWrapper> innerAudioCapture)
1029 {
1030     innerAudioCapture_ = innerAudioCapture;
1031 }
1032 
GetMicAudioCapture()1033 std::shared_ptr<AudioCapturerWrapper> ScreenCaptureServer::GetMicAudioCapture()
1034 {
1035     return micAudioCapture_;
1036 }
1037 
IsStopAcquireAudioBufferFlag()1038 bool ScreenCaptureServer::IsStopAcquireAudioBufferFlag()
1039 {
1040     return stopAcquireAudioBufferFromAudio_.load();
1041 }
1042 
SetDisplayId(uint64_t displayId)1043 void ScreenCaptureServer::SetDisplayId(uint64_t displayId)
1044 {
1045     captureConfig_.videoInfo.videoCapInfo.displayId = displayId;
1046 }
1047 
SetMissionId(uint64_t missionId)1048 void ScreenCaptureServer::SetMissionId(uint64_t missionId)
1049 {
1050     missionIds_.emplace_back(missionId);
1051 }
1052 
GetSCServerCaptureState()1053 AVScreenCaptureState ScreenCaptureServer::GetSCServerCaptureState()
1054 {
1055     return captureState_;
1056 }
1057 
SetMetaDataReport()1058 void ScreenCaptureServer::SetMetaDataReport()
1059 {
1060     std::shared_ptr<Media::Meta> meta = std::make_shared<Media::Meta>();
1061     meta->SetData(Tag::SCREEN_CAPTURE_ERR_CODE, statisticalEventInfo_.errCode);
1062     meta->SetData(Tag::SCREEN_CAPTURE_ERR_MSG, statisticalEventInfo_.errMsg);
1063     meta->SetData(Tag::SCREEN_CAPTURE_DURATION, statisticalEventInfo_.captureDuration);
1064     meta->SetData(Tag::SCREEN_CAPTURE_AV_TYPE, avType_);
1065     meta->SetData(Tag::SCREEN_CAPTURE_DATA_TYPE, dataMode_);
1066     meta->SetData(Tag::SCREEN_CAPTURE_USER_AGREE, statisticalEventInfo_.userAgree);
1067     meta->SetData(Tag::SCREEN_CAPTURE_REQURE_MIC, statisticalEventInfo_.requireMic);
1068     meta->SetData(Tag::SCREEN_CAPTURE_ENABLE_MIC, statisticalEventInfo_.enableMic);
1069     meta->SetData(Tag::SCREEN_CAPTURE_VIDEO_RESOLUTION, statisticalEventInfo_.videoResolution);
1070     meta->SetData(Tag::SCREEN_CAPTURE_STOP_REASON, statisticalEventInfo_.stopReason);
1071     meta->SetData(Tag::SCREEN_CAPTURE_START_LATENCY, statisticalEventInfo_.startLatency);
1072     AppendMediaInfo(meta, instanceId_);
1073     ReportMediaInfo(instanceId_);
1074 }
1075 
ScreenCaptureServer()1076 ScreenCaptureServer::ScreenCaptureServer()
1077 {
1078     MEDIA_LOGI("0x%{public}06" PRIXPTR " ScreenCaptureServer Instances create", FAKE_POINTER(this));
1079     InitAppInfo();
1080     instanceId_ = OHOS::HiviewDFX::HiTraceChain::GetId().GetChainId();
1081     CreateMediaInfo(SCREEN_CAPTRUER, IPCSkeleton::GetCallingUid(), instanceId_);
1082 }
1083 
~ScreenCaptureServer()1084 ScreenCaptureServer::~ScreenCaptureServer()
1085 {
1086     MEDIA_LOGI("0x%{public}06" PRIXPTR " ScreenCaptureServer Instances destroy", FAKE_POINTER(this));
1087     ReleaseInner();
1088     CloseFd();
1089 }
1090 
SetSessionId(int32_t sessionId)1091 void ScreenCaptureServer::SetSessionId(int32_t sessionId)
1092 {
1093     sessionId_ = sessionId;
1094     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " sessionId: %{public}d", FAKE_POINTER(this), sessionId_);
1095 }
1096 
GetAndSetAppVersion()1097 void ScreenCaptureServer::GetAndSetAppVersion()
1098 {
1099     appVersion_ = GetApiInfo(appInfo_.appUid);
1100     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " appVersion: %{public}d",
1101         FAKE_POINTER(this), appVersion_);
1102 }
1103 
CheckAppVersionForUnsupport(DMError result)1104 bool ScreenCaptureServer::CheckAppVersionForUnsupport(DMError result)
1105 {
1106     return appVersion_ >= UNSUPPORT_ERROR_CODE_API_VERSION_ISOLATION && result == DMError::DM_ERROR_DEVICE_NOT_SUPPORT;
1107 }
1108 
SetCaptureMode(CaptureMode captureMode)1109 int32_t ScreenCaptureServer::SetCaptureMode(CaptureMode captureMode)
1110 {
1111     MediaTrace trace("ScreenCaptureServer::SetCaptureMode");
1112     std::lock_guard<std::mutex> lock(mutex_);
1113     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::CREATED, MSERR_INVALID_OPERATION,
1114         "SetCaptureMode failed, capture is not CREATED, state:%{public}d, mode:%{public}d", captureState_, captureMode);
1115     MEDIA_LOGI("ScreenCaptureServer::SetCaptureMode start, captureMode:%{public}d", captureMode);
1116     int32_t ret = CheckCaptureMode(captureMode);
1117     CHECK_AND_RETURN_RET(ret == MSERR_OK, ret);
1118     captureConfig_.captureMode = captureMode;
1119     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetCaptureMode OK.", FAKE_POINTER(this));
1120     return MSERR_OK;
1121 }
1122 
SetDataType(DataType dataType)1123 int32_t ScreenCaptureServer::SetDataType(DataType dataType)
1124 {
1125     MediaTrace trace("ScreenCaptureServer::SetDataType");
1126     std::lock_guard<std::mutex> lock(mutex_);
1127     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::CREATED, MSERR_INVALID_OPERATION,
1128         "SetDataType failed, capture is not CREATED, state:%{public}d, dataType:%{public}d", captureState_, dataType);
1129     MEDIA_LOGI("ScreenCaptureServer::SetDataType start, dataType:%{public}d", dataType);
1130     int32_t ret = CheckDataType(dataType);
1131     CHECK_AND_RETURN_RET(ret == MSERR_OK, ret);
1132     captureConfig_.dataType = dataType;
1133     CHECK_AND_RETURN_RET_LOG(CheckSCServerSpecifiedDataTypeNum(GetAppUid(), GetSCServerDataType()),
1134         MSERR_INVALID_OPERATION,
1135         "ScreenCaptureServer: 0x%{public}06" PRIXPTR "SetDataType failed.", FAKE_POINTER(this));
1136     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetDataType OK.", FAKE_POINTER(this));
1137     return MSERR_OK;
1138 }
1139 
SetRecorderInfo(RecorderInfo recorderInfo)1140 int32_t ScreenCaptureServer::SetRecorderInfo(RecorderInfo recorderInfo)
1141 {
1142     std::lock_guard<std::mutex> lock(mutex_);
1143     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::CREATED, MSERR_INVALID_OPERATION,
1144         "SetRecorderInfo failed, capture is not CREATED, state:%{public}d", captureState_);
1145     MEDIA_LOGI("ScreenCaptureServer::SetRecorderInfo start");
1146     url_ = recorderInfo.url;
1147     avType_ = AVScreenCaptureAvType::AV_TYPE;
1148 
1149     if (MP4.compare(recorderInfo.fileFormat) == 0) {
1150         fileFormat_ = OutputFormatType::FORMAT_MPEG_4;
1151     } else if (M4A.compare(recorderInfo.fileFormat) == 0) {
1152         fileFormat_ = OutputFormatType::FORMAT_M4A;
1153     } else {
1154         MEDIA_LOGE("invalid fileFormat type");
1155         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_,
1156             SCREEN_CAPTURE_ERR_INVALID_VAL, "invalid fileFormat type");
1157         return MSERR_INVALID_VAL;
1158     }
1159     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetRecorderInfo OK.", FAKE_POINTER(this));
1160     return MSERR_OK;
1161 }
1162 
SetOutputFile(int32_t outputFd)1163 int32_t ScreenCaptureServer::SetOutputFile(int32_t outputFd)
1164 {
1165     std::lock_guard<std::mutex> lock(mutex_);
1166     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::CREATED, MSERR_INVALID_OPERATION,
1167         "SetOutputFile failed, capture is not CREATED, state:%{public}d", captureState_);
1168     MEDIA_LOGI("ScreenCaptureServer::SetOutputFile start");
1169     if (outputFd < 0) {
1170         MEDIA_LOGI("invalid outputFd");
1171         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_,
1172             SCREEN_CAPTURE_ERR_INVALID_VAL, "invalid outputFd");
1173         return MSERR_INVALID_VAL;
1174     }
1175 
1176     int flags = fcntl(outputFd, F_GETFL);
1177     if (flags == -1) {
1178         MEDIA_LOGE("Fail to get File Status Flags");
1179         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_INVALID_VAL,
1180             "Fail to get File Status Flags");
1181         return MSERR_INVALID_VAL;
1182     }
1183     if ((static_cast<unsigned int>(flags) & (O_RDWR | O_WRONLY)) == 0) {
1184         MEDIA_LOGE("File descriptor is not in read-write mode or write-only mode");
1185         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_INVALID_VAL,
1186             "File descriptor is not in read-write mode or write-only mode");
1187         return MSERR_INVALID_VAL;
1188     }
1189     CloseFd();
1190     MEDIA_LOGI("ScreenCaptureServer fd in, fd is %{public}d", outputFd);
1191     outputFd_ = dup(outputFd);
1192     CHECK_AND_RETURN_RET_LOG(outputFd_ >= 0, MSERR_NO_MEMORY, "dup outputFd failed");
1193     MEDIA_LOGI("ScreenCaptureServer fd dup, fd is %{public}d", outputFd_);
1194     MEDIA_LOGI("ScreenCaptureServer SetOutputFile End");
1195     return MSERR_OK;
1196 }
1197 
SetScreenCaptureCallback(const std::shared_ptr<ScreenCaptureCallBack> & callback)1198 int32_t ScreenCaptureServer::SetScreenCaptureCallback(const std::shared_ptr<ScreenCaptureCallBack> &callback)
1199 {
1200     MediaTrace trace("ScreenCaptureServer::SetScreenCaptureCallback");
1201     std::lock_guard<std::mutex> lock(mutex_);
1202     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::CREATED, MSERR_INVALID_OPERATION,
1203         "SetScreenCaptureCallback failed, capture is not CREATED, state:%{public}d", captureState_);
1204     CHECK_AND_RETURN_RET_LOG(callback != nullptr, MSERR_INVALID_VAL,
1205         "SetScreenCaptureCallback failed, callback is nullptr, state:%{public}d", captureState_);
1206     MEDIA_LOGI("ScreenCaptureServer::SetScreenCaptureCallback start");
1207     screenCaptureCb_ = callback;
1208     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetScreenCaptureCallback OK.", FAKE_POINTER(this));
1209     return MSERR_OK;
1210 }
1211 
InitAudioEncInfo(AudioEncInfo audioEncInfo)1212 int32_t ScreenCaptureServer::InitAudioEncInfo(AudioEncInfo audioEncInfo)
1213 {
1214     std::lock_guard<std::mutex> lock(mutex_);
1215     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::CREATED, MSERR_INVALID_OPERATION,
1216         "InitAudioEncInfo failed, capture is not CREATED, state:%{public}d", captureState_);
1217     MEDIA_LOGI("ScreenCaptureServer::InitAudioEncInfo start");
1218     MEDIA_LOGD("audioEncInfo audioBitrate:%{public}d, audioCodecformat:%{public}d", audioEncInfo.audioBitrate,
1219         audioEncInfo.audioCodecformat);
1220     int32_t ret = CheckAudioEncInfo(audioEncInfo);
1221     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "InitAudioEncInfo failed, ret:%{public}d", ret);
1222     captureConfig_.audioInfo.audioEncInfo = audioEncInfo;
1223     return MSERR_OK;
1224 }
1225 
InitVideoEncInfo(VideoEncInfo videoEncInfo)1226 int32_t ScreenCaptureServer::InitVideoEncInfo(VideoEncInfo videoEncInfo)
1227 {
1228     std::lock_guard<std::mutex> lock(mutex_);
1229     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::CREATED, MSERR_INVALID_OPERATION,
1230         "InitVideoEncInfo failed, capture is not CREATED, state:%{public}d", captureState_);
1231     MEDIA_LOGI("ScreenCaptureServer::InitVideoEncInfo start");
1232     MEDIA_LOGD("videoEncInfo videoCodec:%{public}d,  videoBitrate:%{public}d, videoFrameRate:%{public}d",
1233         videoEncInfo.videoCodec, videoEncInfo.videoBitrate, videoEncInfo.videoFrameRate);
1234     int32_t ret = CheckVideoEncInfo(videoEncInfo);
1235     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "InitVideoEncInfo failed, ret:%{public}d", ret);
1236     captureConfig_.videoInfo.videoEncInfo = videoEncInfo;
1237     return MSERR_OK;
1238 }
1239 
CheckScreenCapturePermission()1240 bool ScreenCaptureServer::CheckScreenCapturePermission()
1241 {
1242     int result = Security::AccessToken::AccessTokenKit::VerifyAccessToken(appInfo_.appTokenId,
1243         "ohos.permission.CAPTURE_SCREEN");
1244     if (result == Security::AccessToken::PERMISSION_GRANTED) {
1245         MEDIA_LOGI("user have the right to access capture screen!");
1246         return true;
1247     } else {
1248         MEDIA_LOGE("user do not have the right to access capture screen!");
1249         return false;
1250     }
1251 }
1252 
IsUserPrivacyAuthorityNeeded()1253 bool ScreenCaptureServer::IsUserPrivacyAuthorityNeeded()
1254 {
1255     MediaTrace trace("ScreenCaptureServer::IsUserPrivacyAuthorityNeeded");
1256     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " IsUserPrivacyAuthorityNeeded start, appUid:%{public}d",
1257         FAKE_POINTER(this), appInfo_.appUid);
1258     if (appInfo_.appUid == ROOT_UID) {
1259         MEDIA_LOGI("Root user. Privacy Authority Granted automaticly");
1260         return false;
1261     }
1262     return true;
1263 }
1264 
CheckCaptureMode(CaptureMode captureMode)1265 int32_t ScreenCaptureServer::CheckCaptureMode(CaptureMode captureMode)
1266 {
1267     MEDIA_LOGD("CheckCaptureMode start, captureMode:%{public}d", captureMode);
1268     if ((captureMode > CAPTURE_SPECIFIED_WINDOW) || (captureMode < CAPTURE_HOME_SCREEN)) {
1269         MEDIA_LOGE("invalid captureMode:%{public}d", captureMode);
1270         return MSERR_INVALID_VAL;
1271     }
1272     MEDIA_LOGD("ScreenCaptureServer CheckCaptureMode OK.");
1273     return MSERR_OK;
1274 }
1275 
CheckDataType(DataType dataType)1276 int32_t ScreenCaptureServer::CheckDataType(DataType dataType)
1277 {
1278     MEDIA_LOGD("CheckDataType start, dataType:%{public}d", dataType);
1279     if ((dataType > DataType::CAPTURE_FILE) || (dataType < DataType::ORIGINAL_STREAM)) {
1280         MEDIA_LOGE("invalid dataType:%{public}d", dataType);
1281         return MSERR_INVALID_VAL;
1282     }
1283     if (dataType == DataType::ENCODED_STREAM) {
1284         MEDIA_LOGE("not supported dataType:%{public}d", dataType);
1285         return MSERR_UNSUPPORT;
1286     }
1287     MEDIA_LOGD("ScreenCaptureServer CheckDataType OK.");
1288     return MSERR_OK;
1289 }
1290 
CheckAudioCapParam(const AudioCaptureInfo & audioCapInfo)1291 int32_t ScreenCaptureServer::CheckAudioCapParam(const AudioCaptureInfo &audioCapInfo)
1292 {
1293     MEDIA_LOGD("CheckAudioCapParam sampleRate:%{public}d, channels:%{public}d, source:%{public}d, state:%{public}d",
1294         audioCapInfo.audioSampleRate, audioCapInfo.audioChannels, audioCapInfo.audioSource, audioCapInfo.state);
1295     std::vector<AudioSamplingRate> supportedSamplingRates = AudioStandard::AudioCapturer::GetSupportedSamplingRates();
1296     bool foundSupportSample = false;
1297     for (auto iter = supportedSamplingRates.begin(); iter != supportedSamplingRates.end(); ++iter) {
1298         if (static_cast<AudioSamplingRate>(audioCapInfo.audioSampleRate) == *iter) {
1299             foundSupportSample = true;
1300         }
1301     }
1302     if (!foundSupportSample) {
1303         MEDIA_LOGE("invalid audioSampleRate:%{public}d", audioCapInfo.audioSampleRate);
1304         return MSERR_UNSUPPORT;
1305     }
1306 
1307     std::vector<AudioChannel> supportedChannelList = AudioStandard::AudioCapturer::GetSupportedChannels();
1308     bool foundSupportChannel = false;
1309     for (auto iter = supportedChannelList.begin(); iter != supportedChannelList.end(); ++iter) {
1310         if (static_cast<AudioChannel>(audioCapInfo.audioChannels) == *iter) {
1311             foundSupportChannel = true;
1312         }
1313     }
1314     if (!foundSupportChannel) {
1315         MEDIA_LOGE("invalid audioChannels:%{public}d", audioCapInfo.audioChannels);
1316         return MSERR_UNSUPPORT;
1317     }
1318 
1319     if ((audioCapInfo.audioSource <= SOURCE_INVALID) || (audioCapInfo.audioSource > APP_PLAYBACK)) {
1320         MEDIA_LOGE("invalid audioSource:%{public}d", audioCapInfo.audioSource);
1321         return MSERR_INVALID_VAL;
1322     }
1323     MEDIA_LOGD("ScreenCaptureServer CheckAudioCapParam OK.");
1324     return MSERR_OK;
1325 }
1326 
CheckVideoCapParam(const VideoCaptureInfo & videoCapInfo)1327 int32_t ScreenCaptureServer::CheckVideoCapParam(const VideoCaptureInfo &videoCapInfo)
1328 {
1329     MEDIA_LOGD("CheckVideoCapParam width:%{public}d, height:%{public}d, source:%{public}d, state:%{public}d",
1330         videoCapInfo.videoFrameWidth, videoCapInfo.videoFrameHeight, videoCapInfo.videoSource, videoCapInfo.state);
1331     if ((videoCapInfo.videoFrameWidth <= 0) || (videoCapInfo.videoFrameWidth > VIDEO_FRAME_WIDTH_MAX)) {
1332         MEDIA_LOGE("videoCapInfo Width is invalid, videoFrameWidth:%{public}d, videoFrameHeight:%{public}d",
1333             videoCapInfo.videoFrameWidth, videoCapInfo.videoFrameHeight);
1334         return MSERR_INVALID_VAL;
1335     }
1336     if ((videoCapInfo.videoFrameHeight <= 0) || (videoCapInfo.videoFrameHeight > VIDEO_FRAME_HEIGHT_MAX)) {
1337         MEDIA_LOGE("videoCapInfo Height is invalid, videoFrameWidth:%{public}d, videoFrameHeight:%{public}d",
1338             videoCapInfo.videoFrameWidth, videoCapInfo.videoFrameHeight);
1339         return MSERR_INVALID_VAL;
1340     }
1341 
1342     if (videoCapInfo.videoSource != VIDEO_SOURCE_SURFACE_RGBA) {
1343         MEDIA_LOGE("videoSource is invalid");
1344         return MSERR_INVALID_VAL;
1345     }
1346     MEDIA_LOGD("ScreenCaptureServer CheckVideoCapParam OK.");
1347     return MSERR_OK;
1348 }
1349 
CheckAudioEncParam(const AudioEncInfo & audioEncInfo)1350 int32_t ScreenCaptureServer::CheckAudioEncParam(const AudioEncInfo &audioEncInfo)
1351 {
1352     MEDIA_LOGD("CheckAudioEncParam audioBitrate:%{public}d, audioCodecformat:%{public}d",
1353         audioEncInfo.audioBitrate, audioEncInfo.audioCodecformat);
1354     if ((audioEncInfo.audioCodecformat >= AudioCodecFormat::AUDIO_CODEC_FORMAT_BUTT) ||
1355         (audioEncInfo.audioCodecformat < AudioCodecFormat::AUDIO_DEFAULT)) {
1356         MEDIA_LOGE("invalid AudioCodecFormat:%{public}d", audioEncInfo.audioCodecformat);
1357         return MSERR_INVALID_VAL;
1358     }
1359     if (audioEncInfo.audioBitrate < AUDIO_BITRATE_MIN || audioEncInfo.audioBitrate > AUDIO_BITRATE_MAX) {
1360         MEDIA_LOGE("invalid audioBitrate:%{public}d", audioEncInfo.audioBitrate);
1361         return MSERR_INVALID_VAL;
1362     }
1363     return MSERR_OK;
1364 }
1365 
CheckVideoEncParam(const VideoEncInfo & videoEncInfo)1366 int32_t ScreenCaptureServer::CheckVideoEncParam(const VideoEncInfo &videoEncInfo)
1367 {
1368     MEDIA_LOGD("CheckVideoEncParam videoCodec:%{public}d, videoBitrate:%{public}d, videoFrameRate:%{public}d",
1369         videoEncInfo.videoCodec, videoEncInfo.videoBitrate, videoEncInfo.videoFrameRate);
1370     if ((videoEncInfo.videoCodec >= VideoCodecFormat::VIDEO_CODEC_FORMAT_BUTT) ||
1371         (videoEncInfo.videoCodec < VideoCodecFormat::VIDEO_DEFAULT)) {
1372         MEDIA_LOGE("invalid VideoCodecFormat:%{public}d", videoEncInfo.videoCodec);
1373         return MSERR_INVALID_VAL;
1374     }
1375     if (videoEncInfo.videoBitrate < VIDEO_BITRATE_MIN || videoEncInfo.videoBitrate > VIDEO_BITRATE_MAX) {
1376         MEDIA_LOGE("invalid videoBitrate:%{public}d", videoEncInfo.videoBitrate);
1377         return MSERR_INVALID_VAL;
1378     }
1379     if (videoEncInfo.videoFrameRate < VIDEO_FRAME_RATE_MIN || videoEncInfo.videoFrameRate > VIDEO_FRAME_RATE_MAX) {
1380         MEDIA_LOGE("invalid videoFrameRate:%{public}d", videoEncInfo.videoFrameRate);
1381         return MSERR_INVALID_VAL;
1382     }
1383     return MSERR_OK;
1384 }
1385 
CheckAudioCapInfo(AudioCaptureInfo & audioCapInfo)1386 int32_t ScreenCaptureServer::CheckAudioCapInfo(AudioCaptureInfo &audioCapInfo)
1387 {
1388     MEDIA_LOGD("ScreenCaptureServer CheckAudioCapInfo start, audioChannels:%{public}d, "
1389         "audioSampleRate:%{public}d, audioSource:%{public}d, state:%{public}d.",
1390         audioCapInfo.audioChannels, audioCapInfo.audioSampleRate, audioCapInfo.audioSource, audioCapInfo.state);
1391     if (audioCapInfo.audioChannels == 0 && audioCapInfo.audioSampleRate == 0) {
1392         MEDIA_LOGD("audioCap IGNORED sampleRate:%{public}d, channels:%{public}d, source:%{public}d, state:%{public}d",
1393             audioCapInfo.audioSampleRate, audioCapInfo.audioChannels, audioCapInfo.audioSource, audioCapInfo.state);
1394         audioCapInfo.state = AVScreenCaptureParamValidationState::VALIDATION_IGNORE;
1395         return MSERR_OK;
1396     }
1397     MEDIA_LOGD("CheckAudioCapParam S sampleRate:%{public}d, channels:%{public}d, source:%{public}d, state:%{public}d",
1398         audioCapInfo.audioSampleRate, audioCapInfo.audioChannels, audioCapInfo.audioSource, audioCapInfo.state);
1399     int32_t ret = CheckAudioCapParam(audioCapInfo);
1400     audioCapInfo.state = ret == MSERR_OK ? AVScreenCaptureParamValidationState::VALIDATION_VALID :
1401         AVScreenCaptureParamValidationState::VALIDATION_INVALID;
1402     MEDIA_LOGD("CheckAudioCapParam E sampleRate:%{public}d, channels:%{public}d, source:%{public}d, state:%{public}d",
1403         audioCapInfo.audioSampleRate, audioCapInfo.audioChannels, audioCapInfo.audioSource, audioCapInfo.state);
1404     MEDIA_LOGD("ScreenCaptureServer CheckAudioCapInfo end.");
1405     return ret;
1406 }
1407 
CheckVideoCapInfo(VideoCaptureInfo & videoCapInfo)1408 int32_t ScreenCaptureServer::CheckVideoCapInfo(VideoCaptureInfo &videoCapInfo)
1409 {
1410     MEDIA_LOGD("CheckVideoCapInfo start, videoFrameWidth:%{public}d, videoFrameHeight:%{public}d, "
1411         "videoSource:%{public}d, state:%{public}d.", videoCapInfo.videoFrameWidth,
1412         videoCapInfo.videoFrameHeight, videoCapInfo.videoSource, videoCapInfo.state);
1413     if (videoCapInfo.videoFrameWidth == 0 && videoCapInfo.videoFrameHeight == 0) {
1414         MEDIA_LOGD("videoCap IGNORED width:%{public}d, height:%{public}d, source:%{public}d, state:%{public}d",
1415             videoCapInfo.videoFrameWidth, videoCapInfo.videoFrameHeight, videoCapInfo.videoSource, videoCapInfo.state);
1416         videoCapInfo.state = AVScreenCaptureParamValidationState::VALIDATION_IGNORE;
1417         return MSERR_OK;
1418     }
1419     MEDIA_LOGD("CheckVideoCapParam S width:%{public}d, height:%{public}d, source:%{public}d, state:%{public}d",
1420         videoCapInfo.videoFrameWidth, videoCapInfo.videoFrameHeight, videoCapInfo.videoSource, videoCapInfo.state);
1421     int32_t ret = CheckVideoCapParam(videoCapInfo);
1422     videoCapInfo.state = ret == MSERR_OK ? AVScreenCaptureParamValidationState::VALIDATION_VALID :
1423         AVScreenCaptureParamValidationState::VALIDATION_INVALID;
1424     MEDIA_LOGD("CheckVideoCapParam E width:%{public}d, height:%{public}d, source:%{public}d, state:%{public}d",
1425         videoCapInfo.videoFrameWidth, videoCapInfo.videoFrameHeight, videoCapInfo.videoSource, videoCapInfo.state);
1426     MEDIA_LOGD("ScreenCaptureServer CheckVideoCapInfo end.");
1427     return ret;
1428 }
1429 
CheckAudioEncInfo(AudioEncInfo & audioEncInfo)1430 int32_t ScreenCaptureServer::CheckAudioEncInfo(AudioEncInfo &audioEncInfo)
1431 {
1432     MEDIA_LOGD("ScreenCaptureServer CheckAudioEncInfo start.");
1433     int32_t ret = CheckAudioEncParam(audioEncInfo);
1434     audioEncInfo.state = ret == MSERR_OK ? AVScreenCaptureParamValidationState::VALIDATION_VALID :
1435         AVScreenCaptureParamValidationState::VALIDATION_INVALID;
1436     MEDIA_LOGD("ScreenCaptureServer CheckAudioEncInfo end, state: %{public}d.", audioEncInfo.state);
1437     return ret;
1438 }
1439 
CheckVideoEncInfo(VideoEncInfo & videoEncInfo)1440 int32_t ScreenCaptureServer::CheckVideoEncInfo(VideoEncInfo &videoEncInfo)
1441 {
1442     MEDIA_LOGD("ScreenCaptureServer CheckVideoEncInfo start.");
1443     int32_t ret = CheckVideoEncParam(videoEncInfo);
1444     videoEncInfo.state = ret == MSERR_OK ? AVScreenCaptureParamValidationState::VALIDATION_VALID :
1445         AVScreenCaptureParamValidationState::VALIDATION_INVALID;
1446     MEDIA_LOGD("ScreenCaptureServer CheckVideoEncInfo end, state: %{public}d.", videoEncInfo.state);
1447     return ret;
1448 }
1449 
CheckAllParams()1450 int32_t ScreenCaptureServer::CheckAllParams()
1451 {
1452     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " CheckAllParams start, dataType:%{public}d.",
1453         FAKE_POINTER(this), captureConfig_.dataType);
1454     int32_t ret = CheckDataType(captureConfig_.dataType);
1455     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "CheckAllParams CheckDataType failed, ret:%{public}d", ret);
1456 
1457     ret = CheckCaptureMode(captureConfig_.captureMode);
1458     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "CheckAllParams CheckCaptureMode failed, ret:%{public}d", ret);
1459 
1460     if (captureConfig_.dataType == DataType::ORIGINAL_STREAM) {
1461         if (isSurfaceMode_) {
1462             dataMode_ = AVScreenCaptureDataMode::SUFFACE_MODE;
1463         } else {
1464             dataMode_ = AVScreenCaptureDataMode::BUFFER_MODE;
1465         }
1466         return CheckCaptureStreamParams();
1467     }
1468     if (captureConfig_.dataType == DataType::CAPTURE_FILE) {
1469         dataMode_ = AVScreenCaptureDataMode::FILE_MODE;
1470         return CheckCaptureFileParams();
1471     }
1472     return MSERR_INVALID_VAL;
1473 }
1474 
CheckCaptureStreamParams()1475 int32_t ScreenCaptureServer::CheckCaptureStreamParams()
1476 {
1477     // For original stream:
1478     // 1. Any of innerCapInfo/videoCapInfo should be not invalid and should not be both ignored
1479     // 2. micCapInfo should not be invalid
1480     // 3. For surface mode, videoCapInfo should be valid
1481     CheckAudioCapInfo(captureConfig_.audioInfo.micCapInfo);
1482     CheckAudioCapInfo(captureConfig_.audioInfo.innerCapInfo);
1483     CheckVideoCapInfo(captureConfig_.videoInfo.videoCapInfo);
1484     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " CheckCaptureStreamParams start, isSurfaceMode:%{public}s,"
1485         " videoCapInfo.state:%{public}d, innerCapInfo.state:%{public}d.", FAKE_POINTER(this),
1486         isSurfaceMode_ ? "true" : "false", captureConfig_.videoInfo.videoCapInfo.state,
1487         captureConfig_.audioInfo.innerCapInfo.state);
1488     if (captureConfig_.audioInfo.micCapInfo.state != AVScreenCaptureParamValidationState::VALIDATION_VALID) {
1489         isMicrophoneSwitchTurnOn_ = false;
1490     }
1491     if (isSurfaceMode_) {
1492         // surface mode, surface must not nullptr and videoCapInfo must valid.
1493         if (surface_ == nullptr ||
1494             captureConfig_.videoInfo.videoCapInfo.state != AVScreenCaptureParamValidationState::VALIDATION_VALID) {
1495             FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_INVALID_VAL,
1496                 "video Cap state fault, videoCapInfo is invalid");
1497             return MSERR_INVALID_VAL;
1498         }
1499     }
1500     if (captureConfig_.audioInfo.innerCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_INVALID ||
1501         captureConfig_.videoInfo.videoCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_INVALID) {
1502         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_INVALID_VAL,
1503             "audio inner cap or video cap state invalid");
1504         return MSERR_INVALID_VAL;
1505     }
1506     if (captureConfig_.audioInfo.innerCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_IGNORE &&
1507         captureConfig_.videoInfo.videoCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_IGNORE) {
1508         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_INVALID_VAL,
1509             "audio inner cap or video cap state ignore");
1510         return MSERR_INVALID_VAL;
1511     }
1512     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " CheckCaptureStreamParams OK.", FAKE_POINTER(this));
1513     return MSERR_OK;
1514 }
1515 
CheckCaptureFileParams()1516 int32_t ScreenCaptureServer::CheckCaptureFileParams()
1517 {
1518     // For capture file:
1519     // 1. All of innerCapInfo/videoCapInfo/audioEncInfo/videoEncInfo should be be valid
1520     // 2. micCapInfo should not be invalid
1521     CheckAudioCapInfo(captureConfig_.audioInfo.micCapInfo);
1522     CheckAudioCapInfo(captureConfig_.audioInfo.innerCapInfo);
1523     CheckAudioEncInfo(captureConfig_.audioInfo.audioEncInfo);
1524     CheckVideoCapInfo(captureConfig_.videoInfo.videoCapInfo);
1525     if (captureConfig_.videoInfo.videoCapInfo.state != AVScreenCaptureParamValidationState::VALIDATION_IGNORE) {
1526         CheckVideoEncInfo(captureConfig_.videoInfo.videoEncInfo);
1527     }
1528     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " CheckCaptureFileParams start, "
1529         "innerCapInfo.state:%{public}d, videoCapInfo.state:%{public}d, audioEncInfo.state:%{public}d, "
1530         "videoEncInfo.state:%{public}d, micCapInfo.state:%{public}d.", FAKE_POINTER(this),
1531         captureConfig_.audioInfo.innerCapInfo.state, captureConfig_.videoInfo.videoCapInfo.state,
1532         captureConfig_.audioInfo.audioEncInfo.state, captureConfig_.videoInfo.videoEncInfo.state,
1533         captureConfig_.audioInfo.micCapInfo.state);
1534 
1535     if (captureConfig_.audioInfo.innerCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_INVALID ||
1536         captureConfig_.videoInfo.videoCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_INVALID ||
1537         captureConfig_.audioInfo.audioEncInfo.state == AVScreenCaptureParamValidationState::VALIDATION_INVALID ||
1538         captureConfig_.videoInfo.videoEncInfo.state == AVScreenCaptureParamValidationState::VALIDATION_INVALID) {
1539         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_INVALID_VAL,
1540             "innerCap audioEnc videoCap videoEnc state invalid");
1541         return MSERR_INVALID_VAL;
1542     }
1543     if (captureConfig_.audioInfo.micCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_INVALID) {
1544         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_INVALID_VAL,
1545             "audio mic cap state invalid");
1546         return MSERR_INVALID_VAL;
1547     }
1548     if (captureConfig_.audioInfo.micCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_IGNORE) {
1549         return MSERR_OK;
1550     }
1551     const AudioCaptureInfo &micCapInfo = captureConfig_.audioInfo.micCapInfo;
1552     const AudioCaptureInfo &innerCapInfo = captureConfig_.audioInfo.innerCapInfo;
1553     if (micCapInfo.audioSampleRate == innerCapInfo.audioSampleRate &&
1554         micCapInfo.audioChannels == innerCapInfo.audioChannels) {
1555         return MSERR_OK;
1556     }
1557     MEDIA_LOGE("CheckCaptureFileParams failed, inner and mic param not consistent");
1558     FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_INVALID_VAL,
1559         "CheckCaptureFileParams failed, inner and mic param not consistent");
1560     return MSERR_INVALID_VAL;
1561 }
1562 
1563 // Should call in ipc thread
InitAppInfo()1564 void ScreenCaptureServer::InitAppInfo()
1565 {
1566     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " InitAppInfo start.", FAKE_POINTER(this));
1567     appInfo_.appTokenId = IPCSkeleton::GetCallingTokenID();
1568     appInfo_.appFullTokenId = IPCSkeleton::GetCallingFullTokenID();
1569     appInfo_.appUid = IPCSkeleton::GetCallingUid();
1570     appInfo_.appPid = IPCSkeleton::GetCallingPid();
1571     appName_ = GetClientBundleName(appInfo_.appUid);
1572     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " InitAppInfo end.", FAKE_POINTER(this));
1573 }
1574 
GetCurrentMillisecond()1575 int64_t ScreenCaptureServer::GetCurrentMillisecond()
1576 {
1577     std::chrono::system_clock::duration duration = std::chrono::system_clock::now().time_since_epoch();
1578     int64_t time = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
1579     return time;
1580 }
1581 
SetErrorInfo(int32_t errCode,const std::string & errMsg,StopReason stopReason,bool userAgree)1582 void ScreenCaptureServer::SetErrorInfo(int32_t errCode, const std::string &errMsg, StopReason stopReason,
1583     bool userAgree)
1584 {
1585     statisticalEventInfo_.errCode = errCode;
1586     statisticalEventInfo_.errMsg = errMsg;
1587     statisticalEventInfo_.stopReason = stopReason;
1588     statisticalEventInfo_.userAgree = userAgree;
1589 }
1590 
CheckPrivacyWindowSkipPermission()1591 bool ScreenCaptureServer::CheckPrivacyWindowSkipPermission()
1592 {
1593     MEDIA_LOGI("ScreenCaptureServer::CheckPrivacyWindowSkipPermission() START.");
1594     int result = Security::AccessToken::AccessTokenKit::VerifyAccessToken(appInfo_.appTokenId,
1595         "ohos.permission.EXEMPT_CAPTURE_SCREEN_AUTHORIZE");
1596     if (result == Security::AccessToken::PERMISSION_GRANTED) {
1597         MEDIA_LOGI("CheckPrivacyWindowSkipPermission: user have the right to skip privacywindow");
1598         return true;
1599     }
1600     MEDIA_LOGD("CheckPrivacyWindowSkipPermission: user do not have the right to skip privacywindow");
1601     return false;
1602 }
1603 
RequestUserPrivacyAuthority()1604 int32_t ScreenCaptureServer::RequestUserPrivacyAuthority()
1605 {
1606     MediaTrace trace("ScreenCaptureServer::RequestUserPrivacyAuthority");
1607     // If Root is treated as whitelisted, how to guarantee RequestUserPrivacyAuthority function by TDD cases.
1608     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " RequestUserPrivacyAuthority start.", FAKE_POINTER(this));
1609 
1610     if (isPrivacyAuthorityEnabled_) {
1611         if (GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"]
1612                 .compare(appName_) != 0) {
1613             return StartPrivacyWindow();
1614         } else {
1615             MEDIA_LOGI("ScreenCaptureServer::RequestUserPrivacyAuthority support screenrecorder");
1616             return MSERR_OK;
1617         }
1618     }
1619 
1620     MEDIA_LOGI("privacy notification window not support, go on to check CAPTURE_SCREEN permission");
1621     return CheckScreenCapturePermission() ? MSERR_OK : MSERR_INVALID_OPERATION;
1622 }
1623 
OnReceiveUserPrivacyAuthority(bool isAllowed)1624 int32_t ScreenCaptureServer::OnReceiveUserPrivacyAuthority(bool isAllowed)
1625 {
1626     // Should callback be running in seperate thread?
1627     std::lock_guard<std::mutex> lock(mutex_);
1628     MEDIA_LOGI("OnReceiveUserPrivacyAuthority start, isAllowed:%{public}d, state:%{public}d", isAllowed, captureState_);
1629     if (screenCaptureCb_ == nullptr) {
1630         MEDIA_LOGE("OnReceiveUserPrivacyAuthority failed, screenCaptureCb is nullptr, state:%{public}d", captureState_);
1631         captureState_ = AVScreenCaptureState::STOPPED;
1632         SetErrorInfo(MSERR_UNKNOWN, "OnReceiveUserPrivacyAuthority failed, screenCaptureCb is nullptr",
1633             StopReason::RECEIVE_USER_PRIVACY_AUTHORITY_FAILED, IsUserPrivacyAuthorityNeeded());
1634         return MSERR_UNKNOWN;
1635     }
1636 
1637     if (captureState_ != AVScreenCaptureState::POPUP_WINDOW) {
1638         MEDIA_LOGE("OnReceiveUserPrivacyAuthority failed, capture is not POPUP_WINDOW");
1639         screenCaptureCb_->OnError(ScreenCaptureErrorType::SCREEN_CAPTURE_ERROR_INTERNAL,
1640             AVScreenCaptureErrorCode::SCREEN_CAPTURE_ERR_UNKNOWN);
1641         StopScreenCaptureInner(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INVLID);
1642         return MSERR_UNKNOWN;
1643     }
1644     if (!isAllowed) {
1645         captureState_ = AVScreenCaptureState::CREATED;
1646         screenCaptureCb_->OnStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_CANCELED);
1647         return MSERR_UNKNOWN;
1648     }
1649     int32_t ret = OnStartScreenCapture();
1650     PostStartScreenCapture(ret == MSERR_OK);
1651     return ret;
1652 }
1653 
StartAudioCapture()1654 int32_t ScreenCaptureServer::StartAudioCapture()
1655 {
1656     int32_t ret = MSERR_UNKNOWN;
1657     if (isMicrophoneSwitchTurnOn_) {
1658         ret = StartStreamMicAudioCapture();
1659         if (ret != MSERR_OK) {
1660             MEDIA_LOGE("StartAudioCapture StartStreamMicAudioCapture failed");
1661         }
1662     }
1663     ret = StartStreamInnerAudioCapture();
1664     if (ret != MSERR_OK) {
1665         MEDIA_LOGE("StartStreamInnerAudioCapture failed");
1666         StopMicAudioCapture();
1667         return ret;
1668     }
1669     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartAudioCapture OK.", FAKE_POINTER(this));
1670     return MSERR_OK;
1671 }
1672 
GenerateThreadNameByPrefix(std::string threadName)1673 std::string ScreenCaptureServer::GenerateThreadNameByPrefix(std::string threadName)
1674 {
1675     return threadName + std::to_string(sessionId_);
1676 }
1677 
StartStreamInnerAudioCapture()1678 int32_t ScreenCaptureServer::StartStreamInnerAudioCapture()
1679 {
1680     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartStreamInnerAudioCapture start, dataType:%{public}d,"
1681         " innerCapInfo.state:%{public}d.",
1682         FAKE_POINTER(this), captureConfig_.dataType, captureConfig_.audioInfo.innerCapInfo.state);
1683     std::shared_ptr<AudioCapturerWrapper> innerCapture;
1684     if (captureConfig_.audioInfo.innerCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID) {
1685         MediaTrace trace("ScreenCaptureServer::StartAudioCaptureInner");
1686         innerCapture = std::make_shared<AudioCapturerWrapper>(captureConfig_.audioInfo.innerCapInfo, screenCaptureCb_,
1687             std::string(GenerateThreadNameByPrefix("OS_SInnAd")), contentFilter_);
1688         int32_t ret = innerCapture->Start(appInfo_);
1689         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "StartAudioCapture innerCapture failed");
1690     }
1691     innerAudioCapture_ = innerCapture;
1692     if (showShareSystemAudioBox_ && !isInnerAudioBoxSelected_ && innerAudioCapture_) {
1693         MEDIA_LOGI("StartStreamInnerAudioCapture set isMute");
1694         innerAudioCapture_->SetIsMute(true);
1695     }
1696     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartStreamInnerAudioCapture OK.", FAKE_POINTER(this));
1697     return MSERR_OK;
1698 }
1699 
StartStreamMicAudioCapture()1700 int32_t ScreenCaptureServer::StartStreamMicAudioCapture()
1701 {
1702     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartStreamMicAudioCapture start, dataType:%{public}d, "
1703         "micCapInfo.state:%{public}d.",
1704         FAKE_POINTER(this), captureConfig_.dataType, captureConfig_.audioInfo.micCapInfo.state);
1705     std::shared_ptr<AudioCapturerWrapper> micCapture;
1706     if (captureConfig_.audioInfo.micCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID) {
1707         MediaTrace trace("ScreenCaptureServer::StartAudioCaptureMic");
1708         ScreenCaptureContentFilter contentFilterMic;
1709         micCapture = std::make_shared<AudioCapturerWrapper>(captureConfig_.audioInfo.micCapInfo, screenCaptureCb_,
1710             std::string(GenerateThreadNameByPrefix("OS_SMicAd")), contentFilterMic);
1711         int32_t ret = micCapture->Start(appInfo_);
1712         if (ret != MSERR_OK) {
1713             MEDIA_LOGE("StartStreamMicAudioCapture failed");
1714             NotifyStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_MIC_UNAVAILABLE);
1715             return ret;
1716         }
1717     }
1718     micAudioCapture_ = micCapture;
1719     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartStreamMicAudioCapture OK.", FAKE_POINTER(this));
1720     return MSERR_OK;
1721 }
1722 
StartFileInnerAudioCapture()1723 int32_t ScreenCaptureServer::StartFileInnerAudioCapture()
1724 {
1725     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartFileInnerAudioCapture start, dataType:%{public}d, "
1726         "innerCapInfo.state:%{public}d.",
1727         FAKE_POINTER(this), captureConfig_.dataType, captureConfig_.audioInfo.innerCapInfo.state);
1728     CHECK_AND_RETURN_RET(!IsInnerCaptureRunning(), MSERR_OK); // prevent repeat start
1729     std::shared_ptr<AudioCapturerWrapper> innerCapture;
1730     if (captureConfig_.audioInfo.innerCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID) {
1731         MediaTrace trace("ScreenCaptureServer::StartFileInnerAudioCaptureInner");
1732         innerCapture = std::make_shared<AudioCapturerWrapper>(captureConfig_.audioInfo.innerCapInfo, screenCaptureCb_,
1733             std::string(GenerateThreadNameByPrefix("OS_FInnAd")), contentFilter_);
1734         int32_t ret = innerCapture->Start(appInfo_);
1735         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "StartFileInnerAudioCapture failed");
1736         if (audioSource_ && audioSource_->GetSpeakerAliveStatus() && !audioSource_->GetIsInVoIPCall() &&
1737             IsMicrophoneCaptureRunning()) { // skip only when speaker on, voip off, mic on
1738             ret = innerCapture->Stop(); // pause
1739             MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " inner stop optimise.", FAKE_POINTER(this));
1740             CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "StartFileInnerAudioCapture pause failed");
1741         }
1742     }
1743     innerAudioCapture_ = innerCapture;
1744     if (showShareSystemAudioBox_ && !isInnerAudioBoxSelected_ && innerAudioCapture_) {
1745         MEDIA_LOGI("StartFileInnerAudioCapture set isMute");
1746         innerAudioCapture_->SetIsMute(true);
1747     }
1748     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartFileInnerAudioCapture OK.", FAKE_POINTER(this));
1749     return MSERR_OK;
1750 }
1751 
StartFileMicAudioCapture()1752 int32_t ScreenCaptureServer::StartFileMicAudioCapture()
1753 {
1754     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartFileMicAudioCapture start, dataType:%{public}d, "
1755         "micCapInfo.state:%{public}d.",
1756         FAKE_POINTER(this), captureConfig_.dataType, captureConfig_.audioInfo.micCapInfo.state);
1757     CHECK_AND_RETURN_RET(!IsMicrophoneCaptureRunning(), MSERR_OK); // prevent repeat start
1758 #ifdef SUPPORT_CALL
1759     if (InCallObserver::GetInstance().IsInCall(false) && !IsTelInCallSkipList()) {
1760         MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " skip creating micAudioCapture", FAKE_POINTER(this));
1761         NotifyStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_MIC_UNAVAILABLE);
1762         return MSERR_OK;
1763     }
1764 #endif
1765     std::shared_ptr<AudioCapturerWrapper> micCapture;
1766     if (captureConfig_.audioInfo.micCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID) {
1767         MediaTrace trace("ScreenCaptureServer::StartFileMicAudioCaptureInner");
1768         ScreenCaptureContentFilter contentFilterMic;
1769         micCapture = std::make_shared<AudioCapturerWrapper>(captureConfig_.audioInfo.micCapInfo, screenCaptureCb_,
1770             std::string(GenerateThreadNameByPrefix("OS_FMicAd")), contentFilterMic);
1771         if (audioSource_) {
1772             micCapture->SetIsInVoIPCall(audioSource_->GetIsInVoIPCall());
1773         }
1774         int32_t ret = micCapture->Start(appInfo_);
1775         if (ret != MSERR_OK) {
1776             MEDIA_LOGE("StartFileMicAudioCapture micCapture failed");
1777             NotifyStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_MIC_UNAVAILABLE);
1778             return ret;
1779         }
1780     }
1781     micAudioCapture_ = micCapture;
1782     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartFileMicAudioCapture OK.", FAKE_POINTER(this));
1783     return MSERR_OK;
1784 }
1785 
StartScreenCaptureStream()1786 int32_t ScreenCaptureServer::StartScreenCaptureStream()
1787 {
1788     MediaTrace trace("ScreenCaptureServer::StartScreenCaptureStream");
1789     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartScreenCaptureStream start, dataType:%{public}d.",
1790         FAKE_POINTER(this), captureConfig_.dataType);
1791     CHECK_AND_RETURN_RET(captureConfig_.dataType == DataType::ORIGINAL_STREAM, MSERR_INVALID_OPERATION);
1792     int32_t ret = StartAudioCapture();
1793     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "StartAudioCapture failed, ret:%{public}d, dataType:%{public}d",
1794         ret, captureConfig_.dataType);
1795 
1796     ret = StartStreamVideoCapture();
1797     if (ret != MSERR_OK) {
1798         StopAudioCapture();
1799         MEDIA_LOGE("StartScreenCaptureStream failed");
1800         return ret;
1801     }
1802     MEDIA_LOGI("StartScreenCaptureStream success");
1803     return ret;
1804 }
1805 
StartScreenCaptureFile()1806 int32_t ScreenCaptureServer::StartScreenCaptureFile()
1807 {
1808     CHECK_AND_RETURN_RET(captureConfig_.dataType == DataType::CAPTURE_FILE, MSERR_INVALID_OPERATION);
1809 
1810     MEDIA_LOGI("StartScreenCaptureFile S");
1811     int32_t ret = InitRecorder();
1812     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "InitRecorder failed, ret:%{public}d, dataType:%{public}d",
1813         ret, captureConfig_.dataType);
1814 
1815     ON_SCOPE_EXIT(0) {
1816         if (recorder_ != nullptr) {
1817             recorder_->Release();
1818             recorder_ = nullptr;
1819             consumer_ = nullptr;
1820         }
1821     };
1822     std::string virtualScreenName = "screen_capture_file";
1823     ret = CreateVirtualScreen(virtualScreenName, consumer_);
1824     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "CreateVirtualScreen failed, ret:%{public}d, dataType:%{public}d",
1825         ret, captureConfig_.dataType);
1826 
1827     ON_SCOPE_EXIT(1) {
1828         DestroyVirtualScreen();
1829     };
1830 
1831     if (isMicrophoneSwitchTurnOn_) {
1832         int32_t retMic = StartFileMicAudioCapture();
1833         if (retMic != MSERR_OK) {
1834             MEDIA_LOGE("StartScreenCaptureFile StartFileMicAudioCapture failed");
1835             // not return, if start mic capture failed, inner capture will be started
1836         }
1837     }
1838     int32_t retInner = StartFileInnerAudioCapture();
1839     CHECK_AND_RETURN_RET_LOG(retInner == MSERR_OK, retInner, "StartFileInnerAudioCapture failed, ret:%{public}d,"
1840         "dataType:%{public}d", retInner, captureConfig_.dataType);
1841     MEDIA_LOGI("StartScreenCaptureFile RecorderServer S");
1842     ret = recorder_->Start();
1843     if (ret != MSERR_OK) {
1844         StopAudioCapture();
1845         MEDIA_LOGE("StartScreenCaptureFile recorder start failed");
1846     }
1847     MEDIA_LOGI("StartScreenCaptureFile RecorderServer E");
1848     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "recorder failed, ret:%{public}d, dataType:%{public}d",
1849         ret, captureConfig_.dataType);
1850     CANCEL_SCOPE_EXIT_GUARD(1);
1851     CANCEL_SCOPE_EXIT_GUARD(0);
1852 
1853     MEDIA_LOGI("StartScreenCaptureFile E");
1854     return ret;
1855 }
1856 
OnStartScreenCapture()1857 int32_t ScreenCaptureServer::OnStartScreenCapture()
1858 {
1859     MediaTrace trace("ScreenCaptureServer::OnStartScreenCapture");
1860     MEDIA_LOGI("OnStartScreenCapture start, dataType:%{public}d", captureConfig_.dataType);
1861     captureState_ = AVScreenCaptureState::STARTING;
1862     int32_t ret = MSERR_UNSUPPORT;
1863     if (captureConfig_.dataType == DataType::ORIGINAL_STREAM) {
1864         ret = StartScreenCaptureStream();
1865     } else if (captureConfig_.dataType == DataType::CAPTURE_FILE) {
1866         ret = StartScreenCaptureFile();
1867     }
1868     if (ret == MSERR_OK) {
1869         int64_t endTime = GetCurrentMillisecond();
1870         statisticalEventInfo_.startLatency = static_cast<int32_t>(endTime - startTime_);
1871         MEDIA_LOGI("OnStartScreenCapture start success, dataType:%{public}d", captureConfig_.dataType);
1872     } else {
1873         MEDIA_LOGE("OnStartScreenCapture start failed, dataType:%{public}d", captureConfig_.dataType);
1874         statisticalEventInfo_.startLatency = -1; // latency -1 means invalid
1875     }
1876     return ret;
1877 }
1878 
ResSchedReportData(int64_t value,std::unordered_map<std::string,std::string> payload)1879 void ScreenCaptureServer::ResSchedReportData(int64_t value, std::unordered_map<std::string, std::string> payload)
1880 {
1881     payload["uid"] = std::to_string(appInfo_.appUid);
1882     payload["pid"] = std::to_string(appInfo_.appPid);
1883     uint32_t type = ResourceSchedule::ResType::RES_TYPE_REPORT_SCREEN_CAPTURE;
1884     ResourceSchedule::ResSchedClient::GetInstance().ReportData(type, value, payload);
1885 }
1886 
RegisterPrivateWindowListener()1887 void ScreenCaptureServer::RegisterPrivateWindowListener()
1888 {
1889     std::weak_ptr<ScreenCaptureServer> screenCaptureServer(shared_from_this());
1890     displayListener_ = new PrivateWindowListenerInScreenCapture(screenCaptureServer);
1891     DisplayManager::GetInstance().RegisterPrivateWindowListener(displayListener_);
1892 }
1893 
RegisterScreenConnectListener()1894 void ScreenCaptureServer::RegisterScreenConnectListener()
1895 {
1896     std::weak_ptr<ScreenCaptureServer> screenCaptureServer(shared_from_this());
1897     screenConnectListener_ = sptr<ScreenConnectListenerForSC>::MakeSptr(displayScreenId_, screenCaptureServer);
1898     MEDIA_LOGI("RegisterScreenConnectListener screenId: %{public}" PRIu64, displayScreenId_);
1899     ScreenManager::GetInstance().RegisterScreenListener(screenConnectListener_);
1900 }
1901 
PostStartScreenCaptureSuccessAction()1902 void ScreenCaptureServer::PostStartScreenCaptureSuccessAction()
1903 {
1904     std::unordered_map<std::string, std::string> payload;
1905     int64_t value = ResourceSchedule::ResType::ScreenCaptureStatus::START_SCREEN_CAPTURE;
1906     ResSchedReportData(value, payload);
1907     captureState_ = AVScreenCaptureState::STARTED;
1908     AddStartedSessionIdList(this->sessionId_);
1909     MEDIA_LOGI("sessionId: %{public}d is pushed, now the size of startedSessionIDList_ is: %{public}d",
1910         this->sessionId_, static_cast<uint32_t>(ScreenCaptureServer::startedSessionIDList_.size()));
1911     SetSystemScreenRecorderStatus(true);
1912     ScreenCaptureMonitorServer::GetInstance()->CallOnScreenCaptureStarted(appInfo_.appPid);
1913     NotifyStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_STARTED);
1914     if (displayScreenId_ != SCREEN_ID_INVALID) {
1915         NotifyDisplaySelected(displayScreenId_);
1916     }
1917 }
1918 
IsFirstStartPidInstance(int32_t pid)1919 bool ScreenCaptureServer::IsFirstStartPidInstance(int32_t pid)
1920 {
1921     std::list<int32_t> pidList{};
1922     ScreenCaptureServer::GetRunningScreenCaptureInstancePid(pidList);
1923     std::list<int32_t>::iterator iter = find(pidList.begin(), pidList.end(), pid);
1924     if (iter == pidList.end()) {
1925         MEDIA_LOGD("ScreenCaptureServer::IsFirstStartPidInstance firstPid: %{public}d", pid);
1926         return true;
1927     }
1928     MEDIA_LOGD("ScreenCaptureServer::IsFirstStartPidInstance pid: %{public}d exists", pid);
1929     return false;
1930 }
1931 
FirstPidUpdatePrivacyUsingPermissionState(int32_t pid)1932 bool ScreenCaptureServer::FirstPidUpdatePrivacyUsingPermissionState(int32_t pid)
1933 {
1934     if (IsFirstStartPidInstance(pid)) {
1935         return UpdatePrivacyUsingPermissionState(START_VIDEO);
1936     }
1937     return true;
1938 }
1939 
NotifyStateChange(AVScreenCaptureStateCode stateCode)1940 void ScreenCaptureServer::NotifyStateChange(AVScreenCaptureStateCode stateCode)
1941 {
1942     if (screenCaptureCb_ != nullptr) {
1943         MEDIA_LOGD("NotifyStateChange stateCode: %{public}d", stateCode);
1944         screenCaptureCb_->OnStateChange(stateCode);
1945     }
1946 }
1947 
NotifyDisplaySelected(uint64_t displayId)1948 void ScreenCaptureServer::NotifyDisplaySelected(uint64_t displayId)
1949 {
1950     if (screenCaptureCb_ != nullptr) {
1951         MEDIA_LOGD("NotifyDisplaySelected displayId: (%{public}" PRIu64 ")", displayId);
1952         screenCaptureCb_->OnDisplaySelected(displayId);
1953     }
1954 }
1955 
PrepareUserSelectionInfo(ScreenCaptureUserSelectionInfo & selectionInfo)1956 void ScreenCaptureServer::PrepareUserSelectionInfo(ScreenCaptureUserSelectionInfo &selectionInfo)
1957 {
1958     MEDIA_LOGI("PrepareUserSelectionInfo start");
1959     if (captureConfig_.captureMode == CaptureMode::CAPTURE_SPECIFIED_WINDOW) {
1960         selectionInfo.selectType = SELECT_TYPE_WINDOW;
1961         sptr<Rosen::Display> defaultDisplay = Rosen::DisplayManager::GetInstance().GetDefaultDisplaySync();
1962         CHECK_AND_RETURN_LOG(defaultDisplay != nullptr, "PrepareUserSelectionInfo GetDefaultDisplaySync failed");
1963         selectionInfo.displayId = GetDisplayIdOfWindows(defaultDisplay->GetScreenId());
1964     } else {
1965         selectionInfo.selectType = SELECT_TYPE_SCREEN;
1966         selectionInfo.displayId = captureConfig_.videoInfo.videoCapInfo.displayId;
1967     }
1968 }
1969 
NotifyUserSelected(ScreenCaptureUserSelectionInfo selectionInfo)1970 void ScreenCaptureServer::NotifyUserSelected(ScreenCaptureUserSelectionInfo selectionInfo)
1971 {
1972     if (screenCaptureCb_ != nullptr) {
1973         MEDIA_LOGI("NotifyUserSelected displayId: %{public}" PRIu64 ", selectType: %{public}d",
1974             selectionInfo.displayId, selectionInfo.selectType);
1975         screenCaptureCb_->OnUserSelected(selectionInfo);
1976     }
1977 }
1978 
PostStartScreenCapture(bool isSuccess)1979 void ScreenCaptureServer::PostStartScreenCapture(bool isSuccess)
1980 {
1981     CHECK_AND_RETURN(screenCaptureCb_ != nullptr);
1982     MediaTrace trace("ScreenCaptureServer::PostStartScreenCapture.");
1983     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " PostStartScreenCapture start, isSuccess:%{public}s, "
1984         "dataType:%{public}d.", FAKE_POINTER(this), isSuccess ? "true" : "false", captureConfig_.dataType);
1985     if (isSuccess) {
1986         MEDIA_LOGI("PostStartScreenCapture handle success");
1987 #ifdef SUPPORT_SCREEN_CAPTURE_WINDOW_NOTIFICATION
1988         if (isPrivacyAuthorityEnabled_ &&
1989             GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"]
1990                 .compare(appName_) != 0 && !isScreenCaptureAuthority_) {
1991             if (TryNotificationOnPostStartScreenCapture() == MSERR_UNKNOWN) {
1992                 return;
1993             }
1994         }
1995 #endif
1996         if (!FirstPidUpdatePrivacyUsingPermissionState(appInfo_.appPid)) {
1997             MEDIA_LOGE("UpdatePrivacyUsingPermissionState START failed, dataType:%{public}d", captureConfig_.dataType);
1998             captureState_ = AVScreenCaptureState::STARTED;
1999             screenCaptureCb_->OnError(ScreenCaptureErrorType::SCREEN_CAPTURE_ERROR_INTERNAL,
2000                 AVScreenCaptureErrorCode::SCREEN_CAPTURE_ERR_UNKNOWN);
2001             StopScreenCaptureInner(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INVLID);
2002             return;
2003         }
2004         MEDIA_LOGI("PostStartScreenCaptureSuccessAction START.");
2005         PostStartScreenCaptureSuccessAction();
2006     } else {
2007         MEDIA_LOGE("PostStartScreenCapture handle failure");
2008         if (isPrivacyAuthorityEnabled_) {
2009             screenCaptureCb_->OnError(ScreenCaptureErrorType::SCREEN_CAPTURE_ERROR_INTERNAL,
2010                 AVScreenCaptureErrorCode::SCREEN_CAPTURE_ERR_UNKNOWN);
2011         }
2012         StopScreenCaptureInner(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INVLID);
2013         isPrivacyAuthorityEnabled_ = false;
2014         isSurfaceMode_ = false;
2015         captureState_ = AVScreenCaptureState::STOPPED;
2016         SetErrorInfo(MSERR_UNKNOWN, "PostStartScreenCapture handle failure",
2017             StopReason::POST_START_SCREENCAPTURE_HANDLE_FAILURE, IsUserPrivacyAuthorityNeeded());
2018         return;
2019     }
2020     RegisterPrivateWindowListener();
2021     RegisterScreenConnectListener();
2022     RegisterLanguageSwitchListener();
2023     if (captureConfig_.captureMode == CAPTURE_SPECIFIED_WINDOW && missionIds_.size() == 1) {
2024         SetWindowIdList(missionIds_.front());
2025         SetDefaultDisplayIdOfWindows();
2026         RegisterWindowRelatedListener();
2027     }
2028     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " PostStartScreenCapture end.", FAKE_POINTER(this));
2029 }
2030 
2031 #ifdef SUPPORT_SCREEN_CAPTURE_WINDOW_NOTIFICATION
TryStartNotification()2032 int32_t ScreenCaptureServer::TryStartNotification()
2033 {
2034     int32_t tryTimes;
2035     for (tryTimes = 1; tryTimes <= NOTIFICATION_MAX_TRY_NUM; tryTimes++) {
2036         int32_t ret = StartNotification();
2037         if (ret == MSERR_OK) {
2038             break;
2039         }
2040     }
2041     return tryTimes;
2042 }
2043 
TryNotificationOnPostStartScreenCapture()2044 int32_t ScreenCaptureServer::TryNotificationOnPostStartScreenCapture()
2045 {
2046     int32_t tryTimes = TryStartNotification();
2047     if (tryTimes > NOTIFICATION_MAX_TRY_NUM) {
2048         captureState_ = AVScreenCaptureState::STARTED;
2049         if (screenCaptureCb_ != nullptr) {
2050             screenCaptureCb_->OnError(ScreenCaptureErrorType::SCREEN_CAPTURE_ERROR_INTERNAL,
2051                 AVScreenCaptureErrorCode::SCREEN_CAPTURE_ERR_UNKNOWN);
2052         }
2053         StopScreenCaptureInner(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INVLID);
2054         return MSERR_UNKNOWN;
2055     }
2056     return MSERR_OK;
2057 }
2058 #endif
2059 
RegisterLanguageSwitchListener()2060 void ScreenCaptureServer::RegisterLanguageSwitchListener()
2061 {
2062     MEDIA_LOGI("ScreenCaptureServer::RegisterLanguageSwitchListener");
2063     if (GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"]
2064             .compare(appName_) == 0) {
2065         return;
2066     }
2067     EventFwk::MatchingSkills matchingSkills;
2068     matchingSkills.AddEvent("usual.event.LOCALE_CHANGED");
2069     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
2070     auto onReceiveEvent = std::bind(&ScreenCaptureServer::OnReceiveEvent, this, std::placeholders::_1);
2071     subscriber_ = std::make_shared<ScreenCaptureSubscriber>(
2072         subscribeInfo, onReceiveEvent);
2073     if (subscriber_ == nullptr) {
2074         MEDIA_LOGE("RegisterLanguageSwitchListener subscriber_ is null");
2075         return;
2076     }
2077     EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_);
2078 }
2079 
OnReceiveEvent(const EventFwk::CommonEventData & data)2080 void ScreenCaptureServer::OnReceiveEvent(const EventFwk::CommonEventData &data)
2081 {
2082     MEDIA_LOGI("ScreenCaptureServer::OnReceiveEvent");
2083     NotificationRequest request;
2084     SetPublishRequest(GetLocalLiveViewContent(), notificationId_, request);
2085     if (GetSCServerDataType() == DataType::ORIGINAL_STREAM) {
2086         request.SetWantAgent(GetWantAgent(callingLabel_, sessionId_));
2087     }
2088     NotificationHelper::PublishNotification(request);
2089 }
2090 
UnRegisterLanguageSwitchListener()2091 void ScreenCaptureServer::UnRegisterLanguageSwitchListener()
2092 {
2093     MEDIA_LOGI("ScreenCaptureServer::UnRegisterLanguageSwitchListener");
2094     if (subscriber_ == nullptr) {
2095         MEDIA_LOGE("UnRegisterLanguageSwitchListener subscriber_ is null");
2096         return;
2097     }
2098     EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber_);
2099 }
2100 
InitAudioCap(AudioCaptureInfo audioInfo)2101 int32_t ScreenCaptureServer::InitAudioCap(AudioCaptureInfo audioInfo)
2102 {
2103     MediaTrace trace("ScreenCaptureServer::InitAudioCap");
2104     std::lock_guard<std::mutex> lock(mutex_);
2105     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " InitAudioCap start, audioChannels:%{public}d, "
2106         "audioSampleRate:%{public}d, audioSource:%{public}d, state:%{public}d.", FAKE_POINTER(this),
2107         audioInfo.audioChannels, audioInfo.audioSampleRate, audioInfo.audioSource, audioInfo.state);
2108     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::CREATED, MSERR_INVALID_OPERATION,
2109         "InitAudioCap failed, capture is not CREATED, state:%{public}d", captureState_);
2110 
2111     int ret = CheckAudioCapInfo(audioInfo);
2112     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "InitAudioCap CheckAudioCapInfo failed, audioSource:%{public}d",
2113         audioInfo.audioSource);
2114     if (audioInfo.audioSource == AudioCaptureSourceType::SOURCE_DEFAULT ||
2115         audioInfo.audioSource == AudioCaptureSourceType::MIC) {
2116         captureConfig_.audioInfo.micCapInfo = audioInfo;
2117         statisticalEventInfo_.requireMic = true;
2118     } else if (audioInfo.audioSource == AudioCaptureSourceType::ALL_PLAYBACK ||
2119         audioInfo.audioSource == AudioCaptureSourceType::APP_PLAYBACK) {
2120         captureConfig_.audioInfo.innerCapInfo = audioInfo;
2121         avType_ = (avType_ == AVScreenCaptureAvType::INVALID_TYPE) ? AVScreenCaptureAvType::AUDIO_TYPE :
2122             AVScreenCaptureAvType::AV_TYPE;
2123 #ifdef PC_STANDARD
2124         showShareSystemAudioBox_ = true;
2125         MEDIA_LOGI("InitAudioCap set showShareSystemAudioBox true.");
2126 #endif
2127     }
2128     MEDIA_LOGI("InitAudioCap success sampleRate:%{public}d, channels:%{public}d, source:%{public}d, state:%{public}d,"
2129         "showShareSystemAudioBox:%{public}d", audioInfo.audioSampleRate, audioInfo.audioChannels,
2130         audioInfo.audioSource, audioInfo.state, showShareSystemAudioBox_);
2131     return MSERR_OK;
2132 }
2133 
InitVideoCap(VideoCaptureInfo videoInfo)2134 int32_t ScreenCaptureServer::InitVideoCap(VideoCaptureInfo videoInfo)
2135 {
2136     MediaTrace trace("ScreenCaptureServer::InitVideoCap");
2137     std::lock_guard<std::mutex> lock(mutex_);
2138     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::CREATED, MSERR_INVALID_OPERATION,
2139         "InitVideoCap failed, capture is not CREATED, state:%{public}d", captureState_);
2140 
2141     int ret = CheckVideoCapInfo(videoInfo);
2142     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "InitVideoCap CheckVideoCapInfo failed");
2143     captureConfig_.videoInfo.videoCapInfo = videoInfo;
2144     avType_ = (avType_ == AVScreenCaptureAvType::AUDIO_TYPE) ? AVScreenCaptureAvType::AV_TYPE :
2145         AVScreenCaptureAvType::VIDEO_TYPE;
2146     statisticalEventInfo_.videoResolution = std::to_string(videoInfo.videoFrameWidth) + " * " +
2147         std::to_string(videoInfo.videoFrameHeight);
2148     MEDIA_LOGI("InitVideoCap success width:%{public}d, height:%{public}d, source:%{public}d, state:%{public}d",
2149         videoInfo.videoFrameWidth, videoInfo.videoFrameHeight, videoInfo.videoSource, videoInfo.state);
2150     return MSERR_OK;
2151 }
2152 
InitRecorderInfo(std::shared_ptr<IRecorderService> & recorder,AudioCaptureInfo audioInfo)2153 int32_t ScreenCaptureServer::InitRecorderInfo(std::shared_ptr<IRecorderService> &recorder, AudioCaptureInfo audioInfo)
2154 {
2155     CHECK_AND_RETURN_RET_LOG(recorder != nullptr, MSERR_UNKNOWN, "init InitRecorderInfo failed");
2156     int32_t ret = MSERR_OK;
2157     if (captureConfig_.videoInfo.videoCapInfo.state != AVScreenCaptureParamValidationState::VALIDATION_IGNORE) {
2158         ret = recorder_->SetVideoSource(captureConfig_.videoInfo.videoCapInfo.videoSource, videoSourceId_);
2159         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetVideoSource failed");
2160     }
2161     ret = recorder->SetOutputFormat(fileFormat_); // Change to REC_CONFIGURED
2162     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetOutputFormat failed");
2163     ret = recorder->SetAudioEncoder(audioSourceId_, captureConfig_.audioInfo.audioEncInfo.audioCodecformat);
2164     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetAudioEncoder failed");
2165     ret = recorder->SetAudioSampleRate(audioSourceId_, audioInfo.audioSampleRate);
2166     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetAudioSampleRate failed");
2167     ret = recorder->SetAudioChannels(audioSourceId_, audioInfo.audioChannels);
2168     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetAudioChannels failed");
2169     ret = recorder->SetAudioEncodingBitRate(audioSourceId_, captureConfig_.audioInfo.audioEncInfo.audioBitrate);
2170     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetAudioEncodingBitRate failed");
2171     if (captureConfig_.videoInfo.videoCapInfo.state != AVScreenCaptureParamValidationState::VALIDATION_IGNORE) {
2172         ret = recorder->SetVideoEncoder(videoSourceId_, captureConfig_.videoInfo.videoEncInfo.videoCodec);
2173         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetVideoEncoder failed");
2174         ret = recorder->SetVideoSize(videoSourceId_, captureConfig_.videoInfo.videoCapInfo.videoFrameWidth,
2175             captureConfig_.videoInfo.videoCapInfo.videoFrameHeight);
2176         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetVideoSize failed");
2177         ret = recorder->SetVideoFrameRate(videoSourceId_, captureConfig_.videoInfo.videoEncInfo.videoFrameRate);
2178         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetVideoFrameRate failed");
2179         ret = recorder->SetVideoEncodingBitRate(videoSourceId_, captureConfig_.videoInfo.videoEncInfo.videoBitrate);
2180         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetVideoEncodingBitRate failed");
2181         ret = recorder->SetVideoEnableBFrame(videoSourceId_, captureConfig_.strategy.enableBFrame);
2182         if (ret != MSERR_OK) {
2183             MEDIA_LOGE("recorder SetVideoEnableBFrame failed");
2184             // continue, do not return error
2185         }
2186     }
2187     return MSERR_OK;
2188 }
2189 
InitRecorderMix()2190 int32_t ScreenCaptureServer::InitRecorderMix()
2191 {
2192     int32_t ret = MSERR_OK;
2193     MEDIA_LOGI("InitRecorder prepare to SetAudioDataSource");
2194     audioSource_ = std::make_unique<AudioDataSource>(AVScreenCaptureMixMode::MIX_MODE, this);
2195     captureCallback_ = std::make_shared<ScreenRendererAudioStateChangeCallback>();
2196     audioSource_->SetAppPid(appInfo_.appPid);
2197     audioSource_->SetAppName(appName_);
2198     captureCallback_->SetAppName(appName_);
2199     captureCallback_->SetAudioSource(audioSource_);
2200     audioSource_->RegisterAudioRendererEventListener(appInfo_.appPid, captureCallback_);
2201     ret = recorder_->SetAudioDataSource(audioSource_, audioSourceId_);
2202     recorderFileAudioType_ = AVScreenCaptureMixMode::MIX_MODE;
2203     return ret;
2204 }
2205 
InitRecorderInner()2206 int32_t ScreenCaptureServer::InitRecorderInner()
2207 {
2208     int32_t ret = MSERR_OK;
2209     isMicrophoneSwitchTurnOn_ = false;
2210     MEDIA_LOGI("InitRecorder prepare to SetAudioSource inner");
2211     audioSource_ = std::make_unique<AudioDataSource>(AVScreenCaptureMixMode::INNER_MODE, this);
2212     ret = recorder_->SetAudioDataSource(audioSource_, audioSourceId_);
2213     recorderFileAudioType_ = AVScreenCaptureMixMode::INNER_MODE;
2214     return ret;
2215 }
2216 
InitRecorder()2217 int32_t ScreenCaptureServer::InitRecorder()
2218 {
2219     CHECK_AND_RETURN_RET_LOG(outputFd_ > 0, MSERR_INVALID_OPERATION, "the outputFd is invalid");
2220     MEDIA_LOGI("InitRecorder start");
2221     MediaTrace trace("ScreenCaptureServer::InitRecorder");
2222     recorder_ = Media::RecorderServer::Create();
2223     CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_UNKNOWN, "init Recoder failed");
2224     ON_SCOPE_EXIT(0) {
2225         recorder_->Release();
2226     };
2227     int32_t ret;
2228     AudioCaptureInfo audioInfo;
2229     if (captureConfig_.audioInfo.innerCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID &&
2230         captureConfig_.audioInfo.micCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID) {
2231         audioInfo = captureConfig_.audioInfo.innerCapInfo;
2232         ret = InitRecorderMix();
2233         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetAudioDataSource failed");
2234     } else if (captureConfig_.audioInfo.innerCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID) {
2235         audioInfo = captureConfig_.audioInfo.innerCapInfo;
2236         ret = InitRecorderInner();
2237         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetAudioDataSource failed");
2238     } else if (captureConfig_.audioInfo.micCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID) {
2239         audioInfo = captureConfig_.audioInfo.micCapInfo;
2240         MEDIA_LOGI("InitRecorder prepare to SetAudioSource mic");
2241         audioSource_ = std::make_unique<AudioDataSource>(AVScreenCaptureMixMode::MIC_MODE, this);
2242         ret = recorder_->SetAudioDataSource(audioSource_, audioSourceId_);
2243         recorderFileAudioType_ = AVScreenCaptureMixMode::MIC_MODE;
2244         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetAudioDataSource failed");
2245     } else {
2246         MEDIA_LOGE("InitRecorder not VALIDATION_VALID");
2247         return MSERR_UNKNOWN;
2248     }
2249     MEDIA_LOGI("InitRecorder recorder SetAudioDataSource ret:%{public}d", ret);
2250     ret = InitRecorderInfo(recorder_, audioInfo);
2251     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "InitRecorderInfo failed");
2252     ret = recorder_->SetOutputFile(outputFd_);
2253     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetOutputFile failed");
2254     ret = recorder_->Prepare();
2255     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "recorder Prepare failed");
2256     if (captureConfig_.videoInfo.videoCapInfo.state != AVScreenCaptureParamValidationState::VALIDATION_IGNORE) {
2257         recorderFileWithVideo_.store(true);
2258         consumer_ = recorder_->GetSurface(videoSourceId_);
2259         CHECK_AND_RETURN_RET_LOG(consumer_ != nullptr, MSERR_UNKNOWN, "recorder GetSurface failed");
2260     }
2261     CANCEL_SCOPE_EXIT_GUARD(0);
2262     MEDIA_LOGI("InitRecorder success");
2263     return MSERR_OK;
2264 }
2265 
UpdatePrivacyUsingPermissionState(VideoPermissionState state)2266 bool ScreenCaptureServer::UpdatePrivacyUsingPermissionState(VideoPermissionState state)
2267 {
2268     MediaTrace trace("ScreenCaptureServer::UpdatePrivacyUsingPermissionState");
2269     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " UpdatePrivacyUsingPermissionState start, "
2270         "state: %{public}d, uid: %{public}d", FAKE_POINTER(this), state, appInfo_.appUid);
2271     if (!IsUserPrivacyAuthorityNeeded()) {
2272         MEDIA_LOGI("Using Permission Ignored. state: %{public}d, uid: %{public}d", state, appInfo_.appUid);
2273         return true;
2274     }
2275 
2276     int res = 0;
2277     if (state == START_VIDEO) {
2278         res = PrivacyKit::StartUsingPermission(appInfo_.appTokenId, "ohos.permission.CAPTURE_SCREEN", appInfo_.appPid);
2279         if (res != 0) {
2280             MEDIA_LOGE("start using perm error");
2281             return false;
2282         }
2283         res = PrivacyKit::AddPermissionUsedRecord(appInfo_.appTokenId, "ohos.permission.CAPTURE_SCREEN", 1, 0);
2284         if (res != 0) {
2285             MEDIA_LOGE("add screen capture record error: %{public}d", res);
2286             return false;
2287         }
2288     } else if (state == STOP_VIDEO) {
2289         res = PrivacyKit::StopUsingPermission(appInfo_.appTokenId, "ohos.permission.CAPTURE_SCREEN", appInfo_.appPid);
2290         if (res != 0) {
2291             MEDIA_LOGE("stop using perm error");
2292             return false;
2293         }
2294     }
2295     return true;
2296 }
2297 
StartScreenCaptureInner(bool isPrivacyAuthorityEnabled)2298 int32_t ScreenCaptureServer::StartScreenCaptureInner(bool isPrivacyAuthorityEnabled)
2299 {
2300     MEDIA_LOGI("StartScreenCaptureInner S, appUid:%{public}d, appPid:%{public}d, isPrivacyAuthorityEnabled:%{public}d"
2301         ", isSurfaceMode:%{public}d, dataType:%{public}d", appInfo_.appUid, appInfo_.appPid, isPrivacyAuthorityEnabled,
2302         isSurfaceMode_, captureConfig_.dataType);
2303     MediaTrace trace("ScreenCaptureServer::StartScreenCaptureInner");
2304     PublishStartRecordEvent();
2305     int32_t ret = RegisterServerCallbacks();
2306     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "RegisterServerCallbacks failed");
2307 
2308     ret = CheckAllParams();
2309     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "StartScreenCaptureInner failed, invalid params");
2310 
2311     sptr<Rosen::Display> display = Rosen::DisplayManager::GetInstance().GetDefaultDisplaySync();
2312     CHECK_AND_RETURN_RET_LOG(display != nullptr, MSERR_UNKNOWN, "GetDefaultDisplaySync failed");
2313     density_ = display->GetVirtualPixelRatio();
2314 
2315     GetSystemUIFlag();
2316     appName_ = GetClientBundleName(appInfo_.appUid);
2317     callingLabel_ = GetBundleResourceLabel(appName_);
2318     MEDIA_LOGD("StartScreenCaptureInner callingLabel: %{public}s", callingLabel_.c_str());
2319 
2320     isPrivacyAuthorityEnabled_ = isPrivacyAuthorityEnabled;
2321     captureState_ = AVScreenCaptureState::POPUP_WINDOW;
2322     isScreenCaptureAuthority_ = CheckPrivacyWindowSkipPermission();
2323 
2324     if (GetSCServerDataType() == DataType::ORIGINAL_STREAM) {
2325         showSensitiveCheckBox_ = true;
2326         checkBoxSelected_ = true;
2327     }
2328     MEDIA_LOGI("StartScreenCaptureInner showSensitiveCheckBox: %{public}d, checkBoxSelected: %{public}d",
2329         showSensitiveCheckBox_, checkBoxSelected_);
2330 
2331     if (!isScreenCaptureAuthority_ && IsUserPrivacyAuthorityNeeded()) {
2332         ret = RequestUserPrivacyAuthority();
2333         if (ret != MSERR_OK) {
2334             captureState_ = AVScreenCaptureState::STOPPED;
2335             SetErrorInfo(ret, "StartScreenCaptureInner RequestUserPrivacyAuthority failed",
2336                 StopReason::REQUEST_USER_PRIVACY_AUTHORITY_FAILED, IsUserPrivacyAuthorityNeeded());
2337             MEDIA_LOGE("StartScreenCaptureInner RequestUserPrivacyAuthority failed");
2338             return ret;
2339         }
2340 
2341         if (isPrivacyAuthorityEnabled_ &&
2342             GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"]
2343                 .compare(appName_) != 0) {
2344             MEDIA_LOGI("Wait for user interactions to ALLOW/DENY capture");
2345             return MSERR_OK;
2346         }
2347         MEDIA_LOGI("privacy notification window not support, app has CAPTURE_SCREEN permission and go on");
2348     } else {
2349         MEDIA_LOGI("Privacy Authority granted automatically and go on"); // for root and skip permission
2350     }
2351 
2352     ret = OnStartScreenCapture();
2353     PostStartScreenCapture(ret == MSERR_OK);
2354 
2355     MEDIA_LOGI("StartScreenCaptureInner E, appUid:%{public}d, appPid:%{public}d", appInfo_.appUid, appInfo_.appPid);
2356     return ret;
2357 }
2358 
PublishStartRecordEvent()2359 void ScreenCaptureServer::PublishStartRecordEvent()
2360 {
2361     AAFwk::Want want;
2362     want.SetAction("custom.usual.event.START_RECORD");
2363     EventFwk::CommonEventData commonData {want};
2364     EventFwk::CommonEventManager::PublishCommonEvent(commonData);
2365     MEDIA_LOGI("custom.usual.event.START_RECORD publish");
2366 }
2367 
IsTelInCallSkipList()2368 bool ScreenCaptureServer::IsTelInCallSkipList()
2369 {
2370     MEDIA_LOGI("ScreenCaptureServer::IsTelInCallSkipList isCalledBySystemApp : %{public}d", isCalledBySystemApp_);
2371     if (isCalledBySystemApp_ &&
2372         GetScreenCaptureSystemParam()["const.multimedia.screencapture.hiviewcarebundlename"]
2373             .compare(appName_) == 0) {
2374         MEDIA_LOGI("ScreenCaptureServer::IsTelInCallSkipList true");
2375         return true;
2376     }
2377     return false;
2378 }
2379 
RegisterServerCallbacks()2380 int32_t ScreenCaptureServer::RegisterServerCallbacks()
2381 {
2382     std::weak_ptr<ScreenCaptureServer> wpScreenCaptureServer(shared_from_this());
2383     screenCaptureObserverCb_ = std::make_shared<ScreenCaptureObserverCallBack>(wpScreenCaptureServer);
2384 #ifdef SUPPORT_CALL
2385     uint64_t tokenId = IPCSkeleton::GetCallingFullTokenID();
2386     isCalledBySystemApp_ = OHOS::Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(tokenId);
2387     MEDIA_LOGI("ScreenCaptureServer::RegisterServerCallbacks isCalledBySystemApp: %{public}d", isCalledBySystemApp_);
2388     if (!captureConfig_.strategy.keepCaptureDuringCall && InCallObserver::GetInstance().IsInCall(true) &&
2389         !IsTelInCallSkipList()) {
2390         MEDIA_LOGI("ScreenCaptureServer Start InCall Abort");
2391         NotifyStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_STOPPED_BY_CALL);
2392         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_UNSUPPORT,
2393             "ScreenCaptureServer Start InCall Abort");
2394         return MSERR_UNSUPPORT;
2395     }
2396     MEDIA_LOGI("ScreenCaptureServer Start RegisterScreenCaptureCallBack");
2397     InCallObserver::GetInstance().RegisterInCallObserverCallBack(screenCaptureObserverCb_);
2398 #endif
2399     AccountObserver::GetInstance().RegisterAccountObserverCallBack(screenCaptureObserverCb_);
2400     return MSERR_OK;
2401 }
2402 
2403 #ifdef PC_STANDARD
CheckCaptureSpecifiedWindowForSelectWindow()2404 bool ScreenCaptureServer::CheckCaptureSpecifiedWindowForSelectWindow()
2405 {
2406     if (captureConfig_.captureMode == CAPTURE_SPECIFIED_WINDOW &&
2407         captureConfig_.videoInfo.videoCapInfo.taskIDs.size() < SELECT_WINDOW_MISSION_ID_NUM_MAX) {
2408             return true;
2409     }
2410     return false;
2411 }
2412 
SendConfigToUIParams(AAFwk::Want & want)2413 void ScreenCaptureServer::SendConfigToUIParams(AAFwk::Want& want)
2414 {
2415     want.SetParam("displayId", static_cast<int32_t>(captureConfig_.videoInfo.videoCapInfo.displayId));
2416     if (captureConfig_.captureMode == CAPTURE_SPECIFIED_SCREEN) {
2417         MEDIA_LOGI("CAPTURE_SPECIFIED_SCREEN, displayId: %{public}" PRId64 " missionId is dropped.",
2418             captureConfig_.videoInfo.videoCapInfo.displayId);
2419         captureConfig_.videoInfo.videoCapInfo.taskIDs = {};
2420         want.SetParam("missionId", -1); // -1 无效值
2421     } else if (CheckCaptureSpecifiedWindowForSelectWindow() &&
2422         captureConfig_.videoInfo.videoCapInfo.taskIDs.size() == 1) {
2423         MEDIA_LOGI("CAPTURE_SPECIFIED_WINDOW, missionId: %{public}d",
2424             *(captureConfig_.videoInfo.videoCapInfo.taskIDs.begin()));
2425         want.SetParam("missionId", *(captureConfig_.videoInfo.videoCapInfo.taskIDs.begin()));
2426     } else if (CheckCaptureSpecifiedWindowForSelectWindow() &&
2427         captureConfig_.videoInfo.videoCapInfo.taskIDs.size() == 0) {
2428         want.SetParam("missionId", -1); // -1 无效值
2429     }
2430 }
2431 #endif
2432 
StartPrivacyWindow()2433 int32_t ScreenCaptureServer::StartPrivacyWindow()
2434 {
2435     std::string comStr = "{\"ability.want.params.uiExtensionType\":\"sys/commonUI\",\"sessionId\":\"";
2436     comStr += std::to_string(sessionId_);
2437     comStr += "\",\"callerUid\":\"";
2438     comStr += std::to_string(appInfo_.appUid);
2439     comStr += "\",\"appLabel\":\"";
2440     comStr += callingLabel_;
2441     comStr += "\",\"showSensitiveCheckBox\":\"";
2442     comStr += std::to_string(showSensitiveCheckBox_);
2443     comStr += "\",\"checkBoxSelected\":\"";
2444     comStr += std::to_string(checkBoxSelected_);
2445     comStr += "\"}";
2446 
2447     AAFwk::Want want;
2448     ErrCode ret = ERR_INVALID_VALUE;
2449 #ifdef PC_STANDARD
2450     if (IsPickerPopUp()) {
2451         isRegionCapture_ = false;
2452         AppExecFwk::ElementName element("",
2453             GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"],
2454             SELECT_ABILITY_NAME); // DeviceID
2455         want.SetElement(element);
2456         want.SetParam("params", comStr);
2457         want.SetParam("appLabel", callingLabel_);
2458         want.SetParam("sessionId", sessionId_);
2459         want.SetParam("showSensitiveCheckBox", showSensitiveCheckBox_);
2460         want.SetParam("checkBoxSelected", checkBoxSelected_);
2461         want.SetParam("showShareSystemAudioBox", showShareSystemAudioBox_);
2462         SendConfigToUIParams(want);
2463         ret = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
2464         MEDIA_LOGI("StartAbility end %{public}d, DeviceType : PC", ret);
2465     } else {
2466         showShareSystemAudioBox_ = false;
2467         want.SetElementName(GetScreenCaptureSystemParam()["const.multimedia.screencapture.dialogconnectionbundlename"],
2468             GetScreenCaptureSystemParam()["const.multimedia.screencapture.dialogconnectionabilityname"]);
2469         connection_ = sptr<UIExtensionAbilityConnection>(new (std::nothrow) UIExtensionAbilityConnection(comStr));
2470         ret = OHOS::AAFwk::ExtensionManagerClient::GetInstance().ConnectServiceExtensionAbility(want, connection_,
2471             nullptr, -1);
2472         MEDIA_LOGI("ConnectServiceExtensionAbility end %{public}d, DeviceType : PC", ret);
2473     }
2474 #else
2475     want.SetElementName(GetScreenCaptureSystemParam()["const.multimedia.screencapture.dialogconnectionbundlename"],
2476                         GetScreenCaptureSystemParam()["const.multimedia.screencapture.dialogconnectionabilityname"]);
2477     connection_ = sptr<UIExtensionAbilityConnection>(new (std::nothrow) UIExtensionAbilityConnection(comStr));
2478     ret = OHOS::AAFwk::ExtensionManagerClient::GetInstance().ConnectServiceExtensionAbility(want, connection_,
2479         nullptr, -1);
2480     MEDIA_LOGI("ConnectServiceExtensionAbility end %{public}d, Device : Phone", ret);
2481 #endif
2482     return ret;
2483 }
2484 
StartNotification()2485 int32_t ScreenCaptureServer::StartNotification()
2486 {
2487     int32_t result = NotificationHelper::SubscribeLocalLiveViewNotification(NOTIFICATION_SUBSCRIBER);
2488     MEDIA_LOGD("Screencapture service PublishNotification, result %{public}d", result);
2489     NotificationRequest request;
2490     localLiveViewContent_ = GetLocalLiveViewContent();
2491 
2492     std::shared_ptr<NotificationContent> content =
2493         std::make_shared<NotificationContent>(localLiveViewContent_);
2494 
2495     request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
2496     notificationId_ = sessionId_;
2497     request.SetNotificationId(notificationId_);
2498     request.SetContent(content);
2499     request.SetCreatorUid(AV_SCREEN_CAPTURE_SESSION_UID);
2500     request.SetOwnerUid(AV_SCREEN_CAPTURE_SESSION_UID);
2501     request.SetUnremovable(true);
2502     request.SetRemoveAllowed(false);
2503     request.SetInProgress(true);
2504 
2505     if (GetSCServerDataType() == DataType::ORIGINAL_STREAM) {
2506         auto wantAgent = GetWantAgent(callingLabel_, sessionId_);
2507         if (wantAgent != nullptr) {
2508             request.SetWantAgent(wantAgent);
2509             MEDIA_LOGI("StartNotification, setWantAgent success");
2510         } else {
2511             MEDIA_LOGE("StartNotification, GetWantAgent returned null pointer");
2512         }
2513     }
2514     std::shared_ptr<PixelMap> pixelMapTotalSpr = GetPixelMap(ICON_PATH_NOTIFICATION);
2515     request.SetLittleIcon(pixelMapTotalSpr);
2516     request.SetBadgeIconStyle(NotificationRequest::BadgeStyle::LITTLE);
2517 
2518     result = NotificationHelper::PublishNotification(request);
2519     MEDIA_LOGI("Screencapture service UpdateMicrophoneEnabled uid %{public}d, result %{public}d",
2520         AV_SCREEN_CAPTURE_SESSION_UID, result);
2521     return result;
2522 }
2523 
GetStringByResourceName(const char * name)2524 std::string ScreenCaptureServer::GetStringByResourceName(const char* name)
2525 {
2526     std::string resourceContext;
2527     CHECK_AND_RETURN_RET_LOG(resourceManager_ != nullptr, resourceContext, "resourceManager is null");
2528     if (strcmp(name, NOTIFICATION_SCREEN_RECORDING_TITLE_ID) == 0 ||
2529         strcmp(name, NOTIFICATION_SCREEN_RECORDING_PRIVACY_ON_ID) == 0 ||
2530         strcmp(name, NOTIFICATION_SCREEN_RECORDING_PRIVACY_OFF_ID) == 0) {
2531         resourceManager_->GetStringByName(name, resourceContext);
2532         MEDIA_LOGD("get NOTIFICATION_SCREEN_RECORDING_TITLE_ID: %{public}s", resourceContext.c_str());
2533     } else {
2534         MEDIA_LOGE("resource name is error.");
2535     }
2536     return resourceContext;
2537 }
2538 
RefreshResConfig()2539 void ScreenCaptureServer::RefreshResConfig()
2540 {
2541     std::string language = Global::I18n::LocaleConfig::GetSystemLanguage();
2542     UErrorCode status = U_ZERO_ERROR;
2543     icu::Locale locale = icu::Locale::forLanguageTag(language, status);
2544     if (status != U_ZERO_ERROR) {
2545         MEDIA_LOGE("forLanguageTag failed, errCode:%{public}d", status);
2546     }
2547     if (resConfig_) {
2548         resConfig_->SetLocaleInfo(locale.getLanguage(), locale.getScript(), locale.getCountry());
2549         if (resourceManager_) {
2550             resourceManager_->UpdateResConfig(*resConfig_);
2551         }
2552     }
2553 }
2554 
InitResourceManager()2555 void ScreenCaptureServer::InitResourceManager()
2556 {
2557     if (resourceManager_ == nullptr) {
2558         resourceManager_ = Global::Resource::GetSystemResourceManagerNoSandBox();
2559     }
2560     if (resConfig_ == nullptr) {
2561         resConfig_ = Global::Resource::CreateResConfig();
2562     }
2563     RefreshResConfig();
2564 }
2565 
GetLocalLiveViewContent()2566 std::shared_ptr<NotificationLocalLiveViewContent> ScreenCaptureServer::GetLocalLiveViewContent()
2567 {
2568     std::shared_ptr<NotificationLocalLiveViewContent> localLiveViewContent =
2569         std::make_shared<NotificationLocalLiveViewContent>();
2570     localLiveViewContent->SetType(1);
2571     InitResourceManager();
2572     std::string recordingScreenTitleStr = GetStringByResourceName(NOTIFICATION_SCREEN_RECORDING_TITLE_ID);
2573     std::string from = "%s";
2574     std::string to = QUOTATION_MARKS_STRING + callingLabel_ + QUOTATION_MARKS_STRING;
2575     size_t startPos = recordingScreenTitleStr.find(from);
2576     if (startPos != std::string::npos) {
2577         recordingScreenTitleStr.replace(startPos, from.length(), to);
2578         liveViewText_ = recordingScreenTitleStr;
2579     }
2580     MEDIA_LOGD("GetLocalLiveViewContent liveViewText: %{public}s", liveViewText_.c_str());
2581 
2582     auto capsule = NotificationCapsule();
2583     SetupCapsule(capsule);
2584 
2585     if (GetSCServerDataType() == DataType::ORIGINAL_STREAM) {
2586         localLiveViewContent->SetTitle(liveViewText_);
2587         if (!systemPrivacyProtectionSwitch_ && !appPrivacyProtectionSwitch_) {
2588             liveViewSubText_ = GetStringByResourceName(NOTIFICATION_SCREEN_RECORDING_PRIVACY_OFF_ID);
2589         } else {
2590             liveViewSubText_ = GetStringByResourceName(NOTIFICATION_SCREEN_RECORDING_PRIVACY_ON_ID);
2591         }
2592         localLiveViewContent->SetText(liveViewSubText_);
2593         MEDIA_LOGI("GetLocalLiveViewContentc additionalText: %{public}s", liveViewSubText_.c_str());
2594         capsule.SetTitle(callingLabel_);
2595     } else {
2596         localLiveViewContent->SetText(liveViewText_);
2597         auto countTime = CreateCountTime();
2598         countTime.SetInitialTime(1);
2599         localLiveViewContent->SetTime(countTime);
2600         localLiveViewContent->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::TIME);
2601     }
2602     localLiveViewContent->SetCapsule(capsule);
2603     localLiveViewContent->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::CAPSULE);
2604 
2605     auto basicButton = NotificationLocalLiveViewButton();
2606     basicButton.addSingleButtonName(BUTTON_NAME_STOP);
2607     std::shared_ptr<PixelMap> pixelMapStopSpr = GetPixelMapSvg(ICON_PATH_STOP, SVG_HEIGHT, SVG_WIDTH);
2608     basicButton.addSingleButtonIcon(pixelMapStopSpr);
2609 
2610     localLiveViewContent->SetButton(basicButton);
2611     localLiveViewContent->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::BUTTON);
2612     return localLiveViewContent;
2613 }
2614 
SetupCapsule(NotificationCapsule & capsule)2615 void ScreenCaptureServer::SetupCapsule(NotificationCapsule& capsule)
2616 {
2617     capsule.SetBackgroundColor(BACK_GROUND_COLOR);
2618     capsulePxSize_ = static_cast<int32_t>(capsuleVpSize_ * density_);
2619 
2620     std::shared_ptr<PixelMap> pixelMapCapSpr;
2621     if (isSystemUI2_) {
2622         pixelMapCapSpr = GetPixelMapSvg(ICON_PATH_CAPSULE_STOP_2_0, capsulePxSize_, capsulePxSize_);
2623     } else {
2624         pixelMapCapSpr = GetPixelMapSvg(ICON_PATH_CAPSULE_STOP, capsulePxSize_, capsulePxSize_);
2625     }
2626     capsule.SetIcon(pixelMapCapSpr);
2627 }
2628 
GetPixelMap(std::string path)2629 std::shared_ptr<PixelMap> ScreenCaptureServer::GetPixelMap(std::string path)
2630 {
2631     uint32_t errorCode = 0;
2632     SourceOptions opts;
2633     opts.formatHint = "image/png";
2634     std::unique_ptr<ImageSource> imageSource =
2635         ImageSource::CreateImageSource(path, opts, errorCode);
2636     CHECK_AND_RETURN_RET_LOG(imageSource != nullptr, nullptr, "GetPixelMap CreateImageSource failed");
2637     DecodeOptions decodeOpts;
2638     std::unique_ptr<PixelMap> pixelMap = imageSource->CreatePixelMap(decodeOpts, errorCode);
2639     std::shared_ptr<PixelMap> pixelMapSpr = std::move(pixelMap);
2640     return pixelMapSpr;
2641 }
2642 
GetPixelMapSvg(std::string path,int32_t width,int32_t height)2643 std::shared_ptr<PixelMap> ScreenCaptureServer::GetPixelMapSvg(std::string path, int32_t width, int32_t height)
2644 {
2645     uint32_t errorCode = 0;
2646     SourceOptions opts;
2647     opts.formatHint = "image/svg+xml";
2648     std::unique_ptr<ImageSource> imageSource =
2649         ImageSource::CreateImageSource(path, opts, errorCode);
2650     CHECK_AND_RETURN_RET_LOG(imageSource != nullptr, nullptr, "GetPixelMapSvg CreateImageSource failed");
2651     DecodeOptions decodeOpts;
2652     decodeOpts.desiredSize.width = width;
2653     decodeOpts.desiredSize.height = height;
2654     std::unique_ptr<PixelMap> pixelMap = imageSource->CreatePixelMap(decodeOpts, errorCode);
2655     std::shared_ptr<PixelMap> pixelMapSpr = std::move(pixelMap);
2656     return pixelMapSpr;
2657 }
2658 
UpdateMicrophoneEnabled()2659 void ScreenCaptureServer::UpdateMicrophoneEnabled()
2660 {
2661     UpdateLiveViewContent();
2662     NotificationRequest request;
2663 
2664     std::shared_ptr<NotificationContent> content =
2665         std::make_shared<NotificationContent>(localLiveViewContent_);
2666 
2667     request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
2668     request.SetNotificationId(notificationId_);
2669     request.SetContent(content);
2670     request.SetCreatorUid(AV_SCREEN_CAPTURE_SESSION_UID);
2671     request.SetOwnerUid(AV_SCREEN_CAPTURE_SESSION_UID);
2672     request.SetUnremovable(true);
2673     request.SetInProgress(true);
2674 
2675     std::shared_ptr<PixelMap> pixelMapTotalSpr;
2676     if (isSystemUI2_) {
2677         pixelMapTotalSpr = GetPixelMapSvg(ICON_PATH_CAPSULE_STOP_2_0, capsulePxSize_, capsulePxSize_);
2678     } else {
2679         pixelMapTotalSpr = GetPixelMapSvg(ICON_PATH_CAPSULE_STOP, capsulePxSize_, capsulePxSize_);
2680     }
2681     request.SetLittleIcon(pixelMapTotalSpr);
2682     request.SetBadgeIconStyle(NotificationRequest::BadgeStyle::LITTLE);
2683 
2684     int32_t result = NotificationHelper::PublishNotification(request);
2685     MEDIA_LOGI("Screencapture service UpdateMicrophoneEnabled uid %{public}d, result %{public}d",
2686         AV_SCREEN_CAPTURE_SESSION_UID, result);
2687     micCount_.store(micCount_.load() + 1);
2688 }
2689 
UpdateLiveViewContent()2690 void ScreenCaptureServer::UpdateLiveViewContent()
2691 {
2692     localLiveViewContent_->SetType(1);
2693 
2694     auto capsule = NotificationCapsule();
2695     capsule.SetBackgroundColor(BACK_GROUND_COLOR);
2696     std::shared_ptr<PixelMap> pixelMapCapSpr;
2697     if (isSystemUI2_) {
2698         pixelMapCapSpr = GetPixelMapSvg(ICON_PATH_CAPSULE_STOP_2_0, capsulePxSize_, capsulePxSize_);
2699     } else {
2700         pixelMapCapSpr = GetPixelMapSvg(ICON_PATH_CAPSULE_STOP, capsulePxSize_, capsulePxSize_);
2701     }
2702     capsule.SetIcon(pixelMapCapSpr);
2703 
2704     if (GetSCServerDataType() == DataType::ORIGINAL_STREAM) {
2705         localLiveViewContent_->SetTitle(liveViewText_);
2706         localLiveViewContent_->SetText(liveViewSubText_);
2707         capsule.SetTitle(callingLabel_);
2708     } else {
2709         localLiveViewContent_->SetText(liveViewText_);
2710         auto countTime = CreateCountTime();
2711 
2712         localLiveViewContent_->SetTime(countTime);
2713         localLiveViewContent_->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::TIME);
2714     }
2715     localLiveViewContent_->SetCapsule(capsule);
2716     localLiveViewContent_->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::CAPSULE);
2717 
2718     auto basicButton = NotificationLocalLiveViewButton();
2719     basicButton.addSingleButtonName(BUTTON_NAME_MIC);
2720     if (micCount_.load() % MICROPHONE_STATE_COUNT == MICROPHONE_OFF) {
2721         std::shared_ptr<PixelMap> pixelMapSpr = GetPixelMapSvg(ICON_PATH_MIC_OFF, SVG_HEIGHT, SVG_WIDTH);
2722         basicButton.addSingleButtonIcon(pixelMapSpr);
2723     } else {
2724         std::shared_ptr<PixelMap> pixelMapSpr = GetPixelMapSvg(ICON_PATH_MIC, SVG_HEIGHT, SVG_WIDTH);
2725         basicButton.addSingleButtonIcon(pixelMapSpr);
2726     }
2727 
2728     basicButton.addSingleButtonName(BUTTON_NAME_STOP);
2729     std::shared_ptr<PixelMap> pixelMapStopSpr = GetPixelMapSvg(ICON_PATH_STOP, SVG_HEIGHT, SVG_WIDTH);
2730     basicButton.addSingleButtonIcon(pixelMapStopSpr);
2731 
2732     localLiveViewContent_->SetButton(basicButton);
2733     localLiveViewContent_->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::BUTTON);
2734 }
2735 
CreateCountTime()2736 NotificationTime ScreenCaptureServer::CreateCountTime()
2737 {
2738     auto countTime = NotificationTime();
2739     countTime.SetIsCountDown(false);
2740     countTime.SetIsPaused(false);
2741     countTime.SetIsInTitle(true);
2742     return countTime;
2743 }
2744 
GetSystemUIFlag()2745 void ScreenCaptureServer::GetSystemUIFlag()
2746 {
2747     const std::string systemUITag = "persist.systemui.live2";
2748     std::string systemUI2;
2749     int32_t systemUIRes = OHOS::system::GetStringParameter(systemUITag, systemUI2, "false");
2750     if (systemUIRes != 0) {
2751         MEDIA_LOGI("Failed to get systemUI flag, Res: %{public}d", systemUIRes);
2752         isSystemUI2_ = false;
2753     } else {
2754         isSystemUI2_ = (systemUI2 == "true");
2755         MEDIA_LOGI("get systemUI flag, Res: %{public}d, isSystemUI2_: %{public}d", systemUIRes, isSystemUI2_);
2756     }
2757 }
2758 
GetDumpFlag()2759 void ScreenCaptureServer::GetDumpFlag()
2760 {
2761     const std::string dumpTag = "sys.media.screenCapture.dump.enable";
2762     std::string dumpEnable;
2763     int32_t dumpRes = OHOS::system::GetStringParameter(dumpTag, dumpEnable, "false");
2764     isDump_ = (dumpEnable == "true");
2765     MEDIA_LOGI("get dump flag, dumpRes: %{public}d, isDump_: %{public}d", dumpRes, isDump_);
2766 }
2767 
StartScreenCapture(bool isPrivacyAuthorityEnabled)2768 int32_t ScreenCaptureServer::StartScreenCapture(bool isPrivacyAuthorityEnabled)
2769 {
2770     MediaTrace trace("ScreenCaptureServer::StartScreenCapture");
2771     std::lock_guard<std::mutex> lock(mutex_);
2772     startTime_ = GetCurrentMillisecond();
2773     statisticalEventInfo_.enableMic = isMicrophoneSwitchTurnOn_;
2774     GetDumpFlag();
2775     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartScreenCapture start, "
2776         "isPrivacyAuthorityEnabled:%{public}s, captureState:%{public}d.",
2777         FAKE_POINTER(this), isPrivacyAuthorityEnabled ? "true" : "false", captureState_);
2778     CHECK_AND_RETURN_RET_LOG(
2779         captureState_ == AVScreenCaptureState::CREATED || captureState_ == AVScreenCaptureState::STOPPED,
2780         MSERR_INVALID_OPERATION, "StartScreenCapture failed, not in CREATED or STOPPED, state:%{public}d",
2781         captureState_);
2782     MEDIA_LOGI("StartScreenCapture isPrivacyAuthorityEnabled:%{public}d", isPrivacyAuthorityEnabled);
2783     isSurfaceMode_ = false;
2784     return StartScreenCaptureInner(isPrivacyAuthorityEnabled);
2785 }
2786 
StartScreenCaptureWithSurface(sptr<Surface> surface,bool isPrivacyAuthorityEnabled)2787 int32_t ScreenCaptureServer::StartScreenCaptureWithSurface(sptr<Surface> surface, bool isPrivacyAuthorityEnabled)
2788 {
2789     std::lock_guard<std::mutex> lock(mutex_);
2790     CHECK_AND_RETURN_RET_LOG(
2791         captureState_ == AVScreenCaptureState::CREATED || captureState_ == AVScreenCaptureState::STOPPED,
2792         MSERR_INVALID_OPERATION, "StartScreenCaptureWithSurface failed, not in CREATED or STOPPED, state:%{public}d",
2793         captureState_);
2794     MEDIA_LOGI("StartScreenCaptureWithSurface isPrivacyAuthorityEnabled:%{public}d", isPrivacyAuthorityEnabled);
2795     if (surface == nullptr) {
2796         MEDIA_LOGE("surface is nullptr");
2797         return MSERR_INVALID_OPERATION;
2798     }
2799     surface_ = surface;
2800     isSurfaceMode_ = true;
2801     dataMode_ = AVScreenCaptureDataMode::SUFFACE_MODE;
2802     return StartScreenCaptureInner(isPrivacyAuthorityEnabled);
2803 }
2804 
StartStreamVideoCapture()2805 int32_t ScreenCaptureServer::StartStreamVideoCapture()
2806 {
2807     MediaTrace trace("ScreenCaptureServer::StartStreamVideoCapture");
2808     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartStreamVideoCapture start, state:%{public}d, "
2809         "dataType:%{public}d, isSurfaceMode:%{public}s.", FAKE_POINTER(this),
2810         captureConfig_.videoInfo.videoCapInfo.state, captureConfig_.dataType, isSurfaceMode_ ? "true" : "false");
2811     if (captureConfig_.videoInfo.videoCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_IGNORE) {
2812         MEDIA_LOGI("StartStreamVideoCapture is ignored");
2813         return MSERR_OK;
2814     }
2815     CHECK_AND_RETURN_RET_LOG(
2816         captureConfig_.videoInfo.videoCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID,
2817         MSERR_INVALID_VAL, "StartStreamVideoCapture failed, invalid param, dataType:%{public}d",
2818         captureConfig_.dataType);
2819 
2820     int32_t ret = StartStreamHomeVideoCapture();
2821     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret,
2822         "StartStreamHomeVideoCapture failed, isSurfaceMode:%{public}d, dataType:%{public}d",
2823         isSurfaceMode_, captureConfig_.dataType);
2824     MEDIA_LOGI("StartStreamVideoCapture end.");
2825     return MSERR_OK;
2826 }
2827 
StartStreamHomeVideoCapture()2828 int32_t ScreenCaptureServer::StartStreamHomeVideoCapture()
2829 {
2830     MediaTrace trace("ScreenCaptureServer::StartStreamHomeVideoCapture");
2831     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartStreamHomeVideoCapture start, "
2832         "isSurfaceMode: %{public}s.", FAKE_POINTER(this), isSurfaceMode_ ? "true" : "false");
2833     std::string virtualScreenName = "screen_capture";
2834     if (isSurfaceMode_) {
2835         int32_t ret = CreateVirtualScreen(virtualScreenName, surface_);
2836         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "create virtual screen with input surface failed");
2837         return MSERR_OK;
2838     }
2839 
2840     ON_SCOPE_EXIT(0) {
2841         DestroyVirtualScreen();
2842         if (consumer_ != nullptr && surfaceCb_ != nullptr) {
2843             consumer_->UnregisterConsumerListener();
2844         }
2845         consumer_ = nullptr;
2846         if (surfaceCb_ != nullptr) {
2847             (static_cast<ScreenCapBufferConsumerListener *>(surfaceCb_.GetRefPtr()))->StopBufferThread();
2848             (static_cast<ScreenCapBufferConsumerListener *>(surfaceCb_.GetRefPtr()))->Release();
2849             surfaceCb_ = nullptr;
2850         }
2851     };
2852     consumer_ = OHOS::Surface::CreateSurfaceAsConsumer();
2853     CHECK_AND_RETURN_RET_LOG(consumer_ != nullptr, MSERR_UNKNOWN, "CreateSurfaceAsConsumer failed");
2854     MEDIA_LOGI("ScreenCaptureServer consumer_ BUFFER_USAGE_CPU_READ BUFFER_USAGE_MEM_MMZ_CACHE S");
2855     consumer_->SetDefaultUsage(BUFFER_USAGE_CPU_READ | BUFFER_USAGE_MEM_MMZ_CACHE);
2856     MEDIA_LOGI("ScreenCaptureServer consumer_ BUFFER_USAGE_CPU_READ BUFFER_USAGE_MEM_MMZ_CACHE E");
2857     auto producer = consumer_->GetProducer();
2858     CHECK_AND_RETURN_RET_LOG(producer != nullptr, MSERR_UNKNOWN, "GetProducer failed");
2859     auto producerSurface = OHOS::Surface::CreateSurfaceAsProducer(producer);
2860     CHECK_AND_RETURN_RET_LOG(producerSurface != nullptr, MSERR_UNKNOWN, "CreateSurfaceAsProducer failed");
2861     surfaceCb_ = OHOS::sptr<ScreenCapBufferConsumerListener>::MakeSptr(consumer_, screenCaptureCb_);
2862     CHECK_AND_RETURN_RET_LOG(surfaceCb_ != nullptr, MSERR_UNKNOWN, "MakeSptr surfaceCb_ failed");
2863     consumer_->RegisterConsumerListener(surfaceCb_);
2864     MEDIA_LOGD("StartStreamHomeVideoCapture producerSurface: %{public}" PRIu64, producerSurface->GetUniqueId());
2865     int32_t ret = CreateVirtualScreen(virtualScreenName, producerSurface);
2866     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "create virtual screen without input surface failed");
2867     CANCEL_SCOPE_EXIT_GUARD(0);
2868     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartStreamHomeVideoCapture OK.", FAKE_POINTER(this));
2869     MEDIA_LOGI("ScreenCaptureServer:StartStreamHomeVideoCapture surfaceCb_: 0x%{public}06" PRIXPTR,
2870         FAKE_POINTER(surfaceCb_.GetRefPtr()));
2871     if (!isSurfaceMode_) {
2872         ret = (static_cast<ScreenCapBufferConsumerListener*>(surfaceCb_.GetRefPtr()))->StartBufferThread();
2873         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "start buffer thread failed");
2874     }
2875     return MSERR_OK;
2876 }
2877 
SetVirtualScreenAutoRotation()2878 int32_t ScreenCaptureServer::SetVirtualScreenAutoRotation()
2879 {
2880     CHECK_AND_RETURN_RET(captureConfig_.dataType == DataType::ORIGINAL_STREAM, MSERR_INVALID_OPERATION);
2881     MEDIA_LOGI("config strategy canvasFollowRotation %{public}d", captureConfig_.strategy.canvasFollowRotation);
2882     auto setAutoRotationRet = ScreenManager::GetInstance().SetVirtualScreenAutoRotation(virtualScreenId_,
2883         captureConfig_.strategy.canvasFollowRotation);
2884     MEDIA_LOGI("SetVirtualScreenAutoRotation setAutoRotationRet %{public}d", setAutoRotationRet);
2885     return MSERR_OK;
2886 }
2887 
CreateVirtualScreen(const std::string & name,sptr<OHOS::Surface> consumer)2888 int32_t ScreenCaptureServer::CreateVirtualScreen(const std::string &name, sptr<OHOS::Surface> consumer)
2889 {
2890     MediaTrace trace("ScreenCaptureServer::CreateVirtualScreen");
2891     MEDIA_LOGI("0x%{public}06" PRIXPTR " CreateVirtualScreen Start", FAKE_POINTER(this));
2892     isConsumerStart_ = false;
2893     VirtualScreenOption virScrOption = InitVirtualScreenOption(name, consumer);
2894     sptr<Rosen::Display> display = Rosen::DisplayManager::GetInstance().GetDefaultDisplaySync();
2895     if (display != nullptr) {
2896         MEDIA_LOGI("get displayInfo width:%{public}d,height:%{public}d, density:%{public}f", display->GetWidth(),
2897                    display->GetHeight(), display->GetVirtualPixelRatio());
2898         virScrOption.density_ = display->GetVirtualPixelRatio();
2899     }
2900     if (missionIds_.size() > 0 && captureConfig_.captureMode == CAPTURE_SPECIFIED_WINDOW) {
2901         virScrOption.missionIds_ = missionIds_;
2902     } else if (captureConfig_.videoInfo.videoCapInfo.taskIDs.size() > 0 &&
2903         captureConfig_.captureMode == CAPTURE_SPECIFIED_WINDOW) {
2904         GetMissionIds(missionIds_);
2905         virScrOption.missionIds_ = missionIds_;
2906     }
2907     virtualScreenId_ = ScreenManager::GetInstance().CreateVirtualScreen(virScrOption);
2908     CHECK_AND_RETURN_RET_LOG(virtualScreenId_ >= 0, MSERR_UNKNOWN, "CreateVirtualScreen failed, invalid screenId");
2909     SetVirtualScreenAutoRotation();
2910     CHECK_AND_RETURN_RET_LOG(HandleOriginalStreamPrivacy() == MSERR_OK,
2911         MSERR_UNKNOWN, "SetScreenSkipProtectedWindow failed");
2912     if (!showCursor_) {
2913         MEDIA_LOGI("CreateVirtualScreen without cursor");
2914         int32_t ret = ShowCursorInner();
2915         if (ret != MSERR_OK) {
2916             MEDIA_LOGE("CreateVirtualScreen SetVirtualScreenBlackList failed");
2917         }
2918     }
2919     MEDIA_LOGI("CreateVirtualScreen success, screenId: %{public}" PRIu64, virtualScreenId_);
2920     return PrepareVirtualScreenMirror();
2921 }
2922 
HandleOriginalStreamPrivacy()2923 int32_t ScreenCaptureServer::HandleOriginalStreamPrivacy()
2924 {
2925     if (captureConfig_.dataType == DataType::ORIGINAL_STREAM) {
2926         if (checkBoxSelected_) {
2927             MEDIA_LOGI("CreateVirtualScreen checkBoxSelected: %{public}d", checkBoxSelected_);
2928             std::vector<ScreenId> screenIds;
2929             screenIds.push_back(virtualScreenId_);
2930             auto ret = ScreenManager::GetInstance().SetScreenSkipProtectedWindow(screenIds, true);
2931             CHECK_AND_RETURN_RET_LOG(ret == DMError::DM_OK || ret == DMError::DM_ERROR_DEVICE_NOT_SUPPORT,
2932                 MSERR_UNKNOWN, "0x%{public}06" PRIXPTR " SetScreenSkipProtectedWindow failed, ret: %{public}d",
2933                 FAKE_POINTER(this), ret);
2934             MEDIA_LOGI("0x%{public}06" PRIXPTR " SetScreenSkipProtectedWindow success", FAKE_POINTER(this));
2935             AppPrivacyProtected(virtualScreenId_, true);
2936         } else {
2937             AppPrivacyProtected(virtualScreenId_, false);
2938         }
2939     }
2940     return MSERR_OK;
2941 }
2942 
PrepareVirtualScreenMirror()2943 int32_t ScreenCaptureServer::PrepareVirtualScreenMirror()
2944 {
2945     for (size_t i = 0; i < contentFilter_.windowIDsVec.size(); i++) {
2946         MEDIA_LOGD("After CreateVirtualScreen windowIDsVec value :%{public}" PRIu64, contentFilter_.windowIDsVec[i]);
2947     }
2948     SetScreenScaleMode();
2949     Rosen::DisplayManager::GetInstance().SetVirtualScreenBlackList(virtualScreenId_, contentFilter_.windowIDsVec,
2950         surfaceIdList_, surfaceTypeList_);
2951     MEDIA_LOGI("PrepareVirtualScreenMirror screenId: %{public}" PRIu64, virtualScreenId_);
2952     auto screen = ScreenManager::GetInstance().GetScreenById(virtualScreenId_);
2953     if (screen == nullptr) {
2954         MEDIA_LOGE("GetScreenById failed");
2955         DestroyVirtualScreen();
2956         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_UNKNOWN,
2957             "GetScreenById failed");
2958         return MSERR_UNKNOWN;
2959     }
2960     if (canvasRotation_) {
2961         SetCanvasRotationInner();
2962     }
2963     SkipPrivacyModeInner();
2964     int32_t ret = MakeVirtualScreenMirror();
2965     if (ret != MSERR_OK) {
2966         MEDIA_LOGE("MakeVirtualScreenMirror failed");
2967         DestroyVirtualScreen();
2968         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_UNKNOWN,
2969             "MakeVirtualScreenMirror failed");
2970         return MSERR_UNKNOWN;
2971     }
2972     isConsumerStart_ = true;
2973     return MSERR_OK;
2974 }
2975 
GetDisplayIdOfWindows(uint64_t displayId)2976 uint64_t ScreenCaptureServer::GetDisplayIdOfWindows(uint64_t displayId)
2977 {
2978     uint64_t defaultDisplayIdValue = displayId;
2979     if (missionIds_.size() > 0) {
2980         std::unordered_map<uint64_t, uint64_t> windowDisplayIdMap;
2981         auto ret = WindowManager::GetInstance().GetDisplayIdByWindowId(missionIds_, windowDisplayIdMap);
2982         MEDIA_LOGI("MakeVirtualScreenMirror 0x%{public}06" PRIXPTR
2983             "GetWindowDisplayIds ret:%{public}d", FAKE_POINTER(this), ret);
2984         for (const auto& pair : windowDisplayIdMap) {
2985             MEDIA_LOGD("MakeVirtualScreenMirror 0x%{public}06" PRIXPTR " WindowId:%{public}" PRIu64
2986                 " in DisplayId:%{public}" PRIu64, FAKE_POINTER(this), pair.first, pair.second);
2987             defaultDisplayIdValue = pair.second;
2988         }
2989         MEDIA_LOGI("MakeVirtualScreenMirror 0x%{public}06" PRIXPTR
2990             " For Specific Window %{public}" PRIu64, FAKE_POINTER(this), defaultDisplayIdValue);
2991     }
2992     return defaultDisplayIdValue;
2993 }
2994 
2995 #ifdef PC_STANDARD
IsHopper()2996 bool ScreenCaptureServer::IsHopper()
2997 {
2998     std::string foldScreenFlag = system::GetParameter("const.window.foldscreen.type", "0,0,0,0");
2999     if (foldScreenFlag.empty()) {
3000         MEDIA_LOGI("foldscreen type is empty");
3001         return false;
3002     }
3003     MEDIA_LOGI("foldscreen type is %{public}s", foldScreenFlag.c_str());
3004     return foldScreenFlag[0] == '5';
3005 }
3006 
MakeVirtualScreenMirrorForWindowForHopper(sptr<Rosen::Display> defaultDisplay,std::vector<ScreenId> mirrorIds)3007 int32_t ScreenCaptureServer::MakeVirtualScreenMirrorForWindowForHopper(sptr<Rosen::Display> defaultDisplay,
3008     std::vector<ScreenId> mirrorIds)
3009 {
3010     ScreenId mirrorGroup = defaultDisplay->GetScreenId();
3011     uint64_t defaultDisplayId = GetDisplayIdOfWindows(defaultDisplay->GetScreenId());
3012     DMError ret = ScreenManager::GetInstance().MakeMirrorForRecord(defaultDisplayId, mirrorIds, mirrorGroup);
3013     CHECK_AND_RETURN_RET_LOG(ret == DMError::DM_OK, MSERR_UNKNOWN,
3014         "MakeVirtualScreenMirror failed, captureMode:%{public}d, ret:%{public}d", captureConfig_.captureMode, ret);
3015     MEDIA_LOGI("MakeVirtualScreenMirror window screen success, screenId:%{public}" PRIu64, defaultDisplayId);
3016     displayScreenId_ = defaultDisplayId;
3017     return MSERR_OK;
3018 }
3019 
IsPickerPopUp()3020 bool ScreenCaptureServer::IsPickerPopUp()
3021 {
3022     if (captureConfig_.strategy.pickerPopUp == AVScreenCapturePickerPopUp::SCREEN_CAPTURE_PICKER_POPUP_ENABLE) {
3023         return true;
3024     }
3025     if (captureConfig_.strategy.pickerPopUp == AVScreenCapturePickerPopUp::SCREEN_CAPTURE_PICKER_POPUP_DISABLE) {
3026         return false;
3027     }
3028     return !isRegionCapture_ &&
3029            (captureConfig_.captureMode == CAPTURE_SPECIFIED_SCREEN || CheckCaptureSpecifiedWindowForSelectWindow());
3030 }
3031 #endif
3032 
MakeVirtualScreenMirrorForWindow(sptr<Rosen::Display> defaultDisplay,std::vector<ScreenId> mirrorIds)3033 int32_t ScreenCaptureServer::MakeVirtualScreenMirrorForWindow(sptr<Rosen::Display> defaultDisplay,
3034     std::vector<ScreenId> mirrorIds)
3035 {
3036     ScreenId mirrorGroup = defaultDisplay->GetScreenId();
3037     uint64_t defaultDisplayId = GetDisplayIdOfWindows(defaultDisplay->GetScreenId());
3038     DMError ret = ScreenManager::GetInstance().MakeMirror(defaultDisplayId, mirrorIds, mirrorGroup);
3039     CHECK_AND_RETURN_RET_LOG(ret == DMError::DM_OK, MSERR_UNKNOWN,
3040         "MakeVirtualScreenMirror failed, captureMode:%{public}d, ret:%{public}d", captureConfig_.captureMode, ret);
3041     MEDIA_LOGI("MakeVirtualScreenMirror window screen success, screenId:%{public}" PRIu64, defaultDisplayId);
3042     displayScreenId_ = defaultDisplayId;
3043     return MSERR_OK;
3044 }
3045 
MakeVirtualScreenMirrorForHomeScreen(sptr<Rosen::Display> defaultDisplay,std::vector<ScreenId> mirrorIds)3046 int32_t ScreenCaptureServer::MakeVirtualScreenMirrorForHomeScreen(sptr<Rosen::Display> defaultDisplay,
3047     std::vector<ScreenId> mirrorIds)
3048 {
3049     ScreenId mirrorGroup = defaultDisplay->GetScreenId();
3050     MEDIA_LOGI("MakeVirtualScreenMirror DefaultDisplay, screenId:%{public}" PRIu64, mirrorGroup);
3051     DMError ret = ScreenManager::GetInstance().MakeMirror(defaultDisplay->GetScreenId(), mirrorIds, mirrorGroup);
3052     CHECK_AND_RETURN_RET_LOG(ret == DMError::DM_OK, MSERR_UNKNOWN,
3053         "MakeVirtualScreenMirror failed, captureMode:%{public}d, ret:%{public}d", captureConfig_.captureMode, ret);
3054     displayScreenId_ = defaultDisplay->GetScreenId();
3055     MEDIA_LOGI("MakeVirtualScreenMirror default screen success, screenId:%{public}" PRIu64,
3056         defaultDisplay->GetScreenId());
3057     return MSERR_OK;
3058 }
3059 
3060 #ifdef PC_STANDARD
MakeVirtualScreenMirrorForHomeScreenForHopper(sptr<Rosen::Display> defaultDisplay,std::vector<ScreenId> mirrorIds)3061 int32_t ScreenCaptureServer::MakeVirtualScreenMirrorForHomeScreenForHopper(sptr<Rosen::Display> defaultDisplay,
3062     std::vector<ScreenId> mirrorIds)
3063 {
3064     ScreenId mirrorGroup = defaultDisplay->GetScreenId();
3065     MEDIA_LOGI("MakeVirtualScreenMirror DefaultDisplay, screenId:%{public}" PRIu64, mirrorGroup);
3066     DMError ret = ScreenManager::GetInstance().MakeMirrorForRecord(defaultDisplay->GetScreenId(), mirrorIds,
3067         mirrorGroup);
3068     CHECK_AND_RETURN_RET_LOG(ret == DMError::DM_OK, MSERR_UNKNOWN,
3069         "MakeMirrorForRecord failed, captureMode:%{public}d, ret:%{public}d", captureConfig_.captureMode, ret);
3070     displayScreenId_ = defaultDisplay->GetScreenId();
3071     MEDIA_LOGI("MakeVirtualScreenMirror default screen success, screenId:%{public}" PRIu64,
3072         defaultDisplay->GetScreenId());
3073     return MSERR_OK;
3074 }
3075 #endif
3076 
MakeVirtualScreenMirrorForSpecifiedScreen(sptr<Rosen::Display> defaultDisplay,std::vector<ScreenId> mirrorIds)3077 int32_t ScreenCaptureServer::MakeVirtualScreenMirrorForSpecifiedScreen(sptr<Rosen::Display> defaultDisplay,
3078     std::vector<ScreenId> mirrorIds)
3079 {
3080     std::vector<sptr<Screen>> screens;
3081     DMError ret = ScreenManager::GetInstance().GetAllScreens(screens);
3082     CHECK_AND_RETURN_RET_LOG(screens.size() > 0, MSERR_UNKNOWN,
3083         "MakeVirtualScreenMirror failed to GetAllScreens, ret:%{public}d", ret);
3084     for (uint32_t i = 0; i < screens.size() ; i++) {
3085         if (screens[i]->GetId() == captureConfig_.videoInfo.videoCapInfo.displayId) {
3086             ScreenId mirrorGroup = defaultDisplay->GetScreenId();
3087             ret = ScreenManager::GetInstance().MakeMirror(screens[i]->GetId(), mirrorIds, mirrorGroup);
3088             CHECK_AND_RETURN_RET_LOG(ret == DMError::DM_OK, MSERR_UNKNOWN,
3089                 "MakeVirtualScreenMirror failed to MakeMirror for CAPTURE_SPECIFIED_SCREEN, ret:%{public}d", ret);
3090             displayScreenId_ = screens[i]->GetId();
3091             MEDIA_LOGI("MakeVirtualScreenMirror extend screen success, screenId:%{public}" PRIu64,
3092                 captureConfig_.videoInfo.videoCapInfo.displayId);
3093             return MSERR_OK;
3094         }
3095     }
3096     MEDIA_LOGE("MakeVirtualScreenMirror failed to find screenId:%{public}" PRIu64,
3097         captureConfig_.videoInfo.videoCapInfo.displayId);
3098     FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_UNKNOWN,
3099         "MakeVirtualScreenMirror failed to find screenId");
3100     return MSERR_UNKNOWN;
3101 }
3102 
3103 #ifdef PC_STANDARD
MakeVirtualScreenMirrorForSpecifiedScreenForHopper(sptr<Rosen::Display> defaultDisplay,std::vector<ScreenId> mirrorIds)3104 int32_t ScreenCaptureServer::MakeVirtualScreenMirrorForSpecifiedScreenForHopper(sptr<Rosen::Display> defaultDisplay,
3105     std::vector<ScreenId> mirrorIds)
3106 {
3107     std::vector<Rosen::DisplayId> allDisplayIds = Rosen::DisplayManager::GetInstance().GetAllDisplayIds();
3108     CHECK_AND_RETURN_RET_LOG(allDisplayIds.size() > 0, MSERR_UNKNOWN,
3109         "MakeVirtualScreenMirror failed to GetAllDisplayIds, allDisplayIds is empty");
3110     for (uint32_t i = 0; i < allDisplayIds.size() ; i++) {
3111         if (allDisplayIds[i] == captureConfig_.videoInfo.videoCapInfo.displayId) {
3112             ScreenId mirrorGroup = defaultDisplay->GetScreenId();
3113             DMError ret = ScreenManager::GetInstance().MakeMirrorForRecord(allDisplayIds[i], mirrorIds, mirrorGroup);
3114             CHECK_AND_RETURN_RET_LOG(ret == DMError::DM_OK, MSERR_UNKNOWN,
3115                 "MakeVirtualScreenMirror failed to MakeMirrorForRecord for CAPTURE_SPECIFIED_SCREEN, ret:%{public}d",
3116                 ret);
3117             displayScreenId_ = allDisplayIds[i];
3118             MEDIA_LOGI("MakeVirtualScreenMirrorForHopper extend screen success, displayId:%{public}" PRIu64,
3119                 captureConfig_.videoInfo.videoCapInfo.displayId);
3120             return MSERR_OK;
3121         }
3122     }
3123     MEDIA_LOGE("MakeVirtualScreenMirror failed to find displayId:%{public}" PRIu64,
3124         captureConfig_.videoInfo.videoCapInfo.displayId);
3125     FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_UNKNOWN,
3126         "MakeVirtualScreenMirror failed to find displayId");
3127     return MSERR_UNKNOWN;
3128 }
3129 #endif
3130 
MakeVirtualScreenMirror()3131 int32_t ScreenCaptureServer::MakeVirtualScreenMirror()
3132 {
3133     MediaTrace trace("ScreenCaptureServer::MakeVirtualScreenMirror");
3134     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " MakeVirtualScreenMirror start.", FAKE_POINTER(this));
3135     CHECK_AND_RETURN_RET_LOG(virtualScreenId_ >= 0 && virtualScreenId_ != SCREEN_ID_INVALID, MSERR_UNKNOWN,
3136         "MakeVirtualScreenMirror failed, invalid screenId");
3137     std::vector<ScreenId> mirrorIds;
3138     mirrorIds.push_back(virtualScreenId_);
3139     sptr<Rosen::Display> defaultDisplay = Rosen::DisplayManager::GetInstance().GetDefaultDisplaySync();
3140     CHECK_AND_RETURN_RET_LOG(defaultDisplay != nullptr, MSERR_UNKNOWN,
3141         "MakeVirtualScreenMirror GetDefaultDisplaySync failed");
3142     if (isRegionCapture_) {
3143         return SetCaptureAreaInner(regionDisplayId_, regionArea_);
3144     }
3145 #ifdef PC_STANDARD
3146     if (IsHopper() && captureConfig_.strategy.enableDeviceLevelCapture == false) {
3147         if (captureConfig_.captureMode == CAPTURE_SPECIFIED_WINDOW) {
3148             return MakeVirtualScreenMirrorForWindowForHopper(defaultDisplay, mirrorIds);
3149         }
3150         if (captureConfig_.captureMode != CAPTURE_SPECIFIED_SCREEN) {
3151             return MakeVirtualScreenMirrorForHomeScreenForHopper(defaultDisplay, mirrorIds);
3152         }
3153         return MakeVirtualScreenMirrorForSpecifiedScreenForHopper(defaultDisplay, mirrorIds);
3154     }
3155 #endif
3156     if (captureConfig_.captureMode == CAPTURE_SPECIFIED_WINDOW) {
3157         return MakeVirtualScreenMirrorForWindow(defaultDisplay, mirrorIds);
3158     }
3159     if (captureConfig_.captureMode != CAPTURE_SPECIFIED_SCREEN) {
3160         return MakeVirtualScreenMirrorForHomeScreen(defaultDisplay, mirrorIds);
3161     }
3162     return MakeVirtualScreenMirrorForSpecifiedScreen(defaultDisplay, mirrorIds);
3163 }
3164 
DestroyVirtualScreen()3165 void ScreenCaptureServer::DestroyVirtualScreen()
3166 {
3167     MediaTrace trace("ScreenCaptureServer::DestroyVirtualScreen");
3168     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " DestroyVirtualScreen start.", FAKE_POINTER(this));
3169     if (virtualScreenId_ >= 0 && virtualScreenId_ != SCREEN_ID_INVALID) {
3170         if (isConsumerStart_) {
3171             std::vector<ScreenId> screenIds;
3172             screenIds.push_back(virtualScreenId_);
3173             ScreenManager::GetInstance().StopMirror(screenIds);
3174         }
3175         ScreenManager::GetInstance().DestroyVirtualScreen(virtualScreenId_);
3176         virtualScreenId_ = SCREEN_ID_INVALID;
3177         isConsumerStart_ = false;
3178     }
3179     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " DestroyVirtualScreen end.", FAKE_POINTER(this));
3180 }
3181 
CloseFd()3182 void ScreenCaptureServer::CloseFd()
3183 {
3184     MediaTrace trace("ScreenCaptureServer::CloseFd");
3185     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " CloseFd, fd is %{public}d", FAKE_POINTER(this),
3186         outputFd_);
3187     if (outputFd_ >= 0) {
3188         (void)::close(outputFd_);
3189         outputFd_ = -1;
3190     }
3191     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " CloseFd end.", FAKE_POINTER(this));
3192 }
3193 
InitVirtualScreenOption(const std::string & name,sptr<OHOS::Surface> consumer)3194 VirtualScreenOption ScreenCaptureServer::InitVirtualScreenOption(const std::string &name, sptr<OHOS::Surface> consumer)
3195 {
3196     MediaTrace trace("ScreenCaptureServer::InitVirtualScreenOption");
3197     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " InitVirtualScreenOption start, name:%{public}s.",
3198         FAKE_POINTER(this), name.c_str());
3199     VirtualScreenOption virScrOption = {
3200         .name_ = name,
3201         .width_ = captureConfig_.videoInfo.videoCapInfo.videoFrameWidth,
3202         .height_ = captureConfig_.videoInfo.videoCapInfo.videoFrameHeight,
3203         .density_ = 0,
3204         .surface_ = consumer,
3205         .flags_ = captureConfig_.strategy.strategyForPrivacyMaskMode,
3206         .isForShot_ = true,
3207         .missionIds_ = {},
3208         .virtualScreenType_ = VirtualScreenType::SCREEN_RECORDING,
3209     };
3210     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " InitVirtualScreenOption end.", FAKE_POINTER(this));
3211     return virScrOption;
3212 }
3213 
GetMissionIds(std::vector<uint64_t> & missionIds)3214 int32_t ScreenCaptureServer::GetMissionIds(std::vector<uint64_t> &missionIds)
3215 {
3216     int32_t size = static_cast<int32_t>(captureConfig_.videoInfo.videoCapInfo.taskIDs.size());
3217     std::list<int32_t> taskIDListTemp = captureConfig_.videoInfo.videoCapInfo.taskIDs;
3218     for (int32_t i = 0; i < size; i++) {
3219         int32_t taskId = taskIDListTemp.front();
3220         taskIDListTemp.pop_front();
3221         MEDIA_LOGD("ScreenCaptureServer::GetMissionIds taskId : %{public}s", std::to_string(taskId).c_str());
3222         uint64_t uintNum = static_cast<uint64_t>(taskId);
3223         missionIds.push_back(uintNum);
3224     }
3225     return MSERR_OK;
3226 }
3227 
AcquireAudioBuffer(std::shared_ptr<AudioBuffer> & audioBuffer,AudioCaptureSourceType type)3228 int32_t ScreenCaptureServer::AcquireAudioBuffer(std::shared_ptr<AudioBuffer> &audioBuffer, AudioCaptureSourceType type)
3229 {
3230     MediaTrace trace("ScreenCaptureServer::AcquireAudioBuffer", HITRACE_LEVEL_DEBUG);
3231     std::unique_lock<std::mutex> lock(mutex_);
3232     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " AcquireAudioBuffer start, state:%{public}d, "
3233         "type:%{public}d.", FAKE_POINTER(this), captureState_, type);
3234     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::STARTED, MSERR_INVALID_OPERATION,
3235         "AcquireAudioBuffer failed, capture is not STARTED, state:%{public}d, type:%{public}d", captureState_, type);
3236 
3237     if (((type == AudioCaptureSourceType::MIC) || (type == AudioCaptureSourceType::SOURCE_DEFAULT)) &&
3238         IsMicrophoneCaptureRunning()) {
3239         return micAudioCapture_->AcquireAudioBuffer(audioBuffer);
3240     }
3241     if (((type == AudioCaptureSourceType::ALL_PLAYBACK) || (type == AudioCaptureSourceType::APP_PLAYBACK)) &&
3242         IsInnerCaptureRunning()) {
3243         return innerAudioCapture_->AcquireAudioBuffer(audioBuffer);
3244     }
3245     MEDIA_LOGE("AcquireAudioBuffer failed, source type not support, type:%{public}d", type);
3246     FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_UNKNOWN,
3247         "AcquireAudioBuffer failed, source type not support");
3248     return MSERR_UNKNOWN;
3249 }
3250 
ReleaseAudioBuffer(AudioCaptureSourceType type)3251 int32_t ScreenCaptureServer::ReleaseAudioBuffer(AudioCaptureSourceType type)
3252 {
3253     MediaTrace trace("ScreenCaptureServer::ReleaseAudioBuffer", HITRACE_LEVEL_DEBUG);
3254     std::unique_lock<std::mutex> lock(mutex_);
3255     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " ReleaseAudioBuffer start, state:%{public}d, "
3256         "type:%{public}d.", FAKE_POINTER(this), captureState_, type);
3257     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::STARTED, MSERR_INVALID_OPERATION,
3258         "ReleaseAudioBuffer failed, capture is not STARTED, state:%{public}d, type:%{public}d", captureState_, type);
3259 
3260     if (((type == AudioCaptureSourceType::MIC) || (type == AudioCaptureSourceType::SOURCE_DEFAULT)) &&
3261         IsMicrophoneCaptureRunning()) {
3262         return micAudioCapture_->ReleaseAudioBuffer();
3263     }
3264     if (((type == AudioCaptureSourceType::ALL_PLAYBACK) || (type == AudioCaptureSourceType::APP_PLAYBACK)) &&
3265         IsInnerCaptureRunning()) {
3266         return innerAudioCapture_->ReleaseAudioBuffer();
3267     }
3268     MEDIA_LOGE("ReleaseAudioBuffer failed, source type not support, type:%{public}d", type);
3269     FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_UNKNOWN,
3270         "ReleaseAudioBuffer failed, source type not support");
3271     return MSERR_UNKNOWN;
3272 }
3273 
ReleaseInnerAudioBuffer()3274 int32_t ScreenCaptureServer::ReleaseInnerAudioBuffer()
3275 {
3276     CHECK_AND_RETURN_RET_LOG(innerAudioCapture_, MSERR_UNKNOWN, "innerAudioCapture_ is nullptr");
3277     int32_t ret = innerAudioCapture_->ReleaseAudioBuffer();
3278     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "innerAudioCapture ReleaseAudioBuffer failed");
3279     return ret;
3280 }
3281 
ReleaseMicAudioBuffer()3282 int32_t ScreenCaptureServer::ReleaseMicAudioBuffer()
3283 {
3284     CHECK_AND_RETURN_RET_LOG(micAudioCapture_, MSERR_UNKNOWN, "micAudioCapture_ is nullptr");
3285     int32_t ret = micAudioCapture_->ReleaseAudioBuffer();
3286     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "micAudioCapture ReleaseAudioBuffer failed");
3287     return ret;
3288 }
3289 
GetInnerAudioCaptureBufferSize(size_t & size)3290 int32_t ScreenCaptureServer::GetInnerAudioCaptureBufferSize(size_t &size)
3291 {
3292     if (innerAudioCapture_ == nullptr) {
3293         MEDIA_LOGE("innerAudioCapture_ is nullptr");
3294         return MSERR_UNKNOWN;
3295     }
3296     int32_t ret = innerAudioCapture_->GetBufferSize(size);
3297     return ret;
3298 }
3299 
GetMicAudioCaptureBufferSize(size_t & size)3300 int32_t ScreenCaptureServer::GetMicAudioCaptureBufferSize(size_t &size)
3301 {
3302     if (micAudioCapture_ == nullptr) {
3303         MEDIA_LOGE("micAudioCapture_ is nullptr");
3304         return MSERR_UNKNOWN;
3305     }
3306     int32_t ret = micAudioCapture_->GetBufferSize(size);
3307     return ret;
3308 }
3309 
AcquireVideoBuffer(sptr<OHOS::SurfaceBuffer> & surfaceBuffer,int32_t & fence,int64_t & timestamp,OHOS::Rect & damage)3310 int32_t ScreenCaptureServer::AcquireVideoBuffer(sptr<OHOS::SurfaceBuffer> &surfaceBuffer, int32_t &fence,
3311                                                 int64_t &timestamp, OHOS::Rect &damage)
3312 {
3313     MediaTrace trace("ScreenCaptureServer::AcquireVideoBuffer", HITRACE_LEVEL_DEBUG);
3314     std::unique_lock<std::mutex> lock(mutex_);
3315     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " AcquireVideoBuffer start, state:%{public}d, "
3316         "fence:%{public}d, timestamp:%{public}" PRId64, FAKE_POINTER(this), captureState_, fence, timestamp);
3317     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::STARTED, MSERR_INVALID_OPERATION,
3318         "AcquireVideoBuffer failed, capture is not STARTED, state:%{public}d", captureState_);
3319 
3320     CHECK_AND_RETURN_RET_LOG(surfaceCb_ != nullptr, MSERR_NO_MEMORY, "AcquireVideoBuffer failed, callback is nullptr");
3321     (static_cast<ScreenCapBufferConsumerListener *>(surfaceCb_.GetRefPtr()))->
3322         AcquireVideoBuffer(surfaceBuffer, fence, timestamp, damage);
3323     if (isDump_ && surfaceBuffer != nullptr) {
3324         void* addr = surfaceBuffer->GetVirAddr();
3325         uint32_t bufferSize = surfaceBuffer->GetSize();
3326         FILE *desFile = fopen(DUMP_PATH.c_str(), "wb+");
3327         if (desFile && addr != nullptr) {
3328             (void)fwrite(addr, 1, bufferSize, desFile);
3329             (void)fclose(desFile);
3330         } else if (desFile) {
3331             (void)fclose(desFile);
3332         }
3333     }
3334     if (surfaceBuffer != nullptr) {
3335         MEDIA_LOGD("getcurrent surfaceBuffer info, size:%{public}u", surfaceBuffer->GetSize());
3336         return MSERR_OK;
3337     }
3338     FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_UNKNOWN,
3339         "AcquireVideoBuffer fault");
3340     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " AcquireVideoBuffer end.", FAKE_POINTER(this));
3341     return MSERR_UNKNOWN;
3342 }
3343 
ReleaseVideoBuffer()3344 int32_t ScreenCaptureServer::ReleaseVideoBuffer()
3345 {
3346     MediaTrace trace("ScreenCaptureServer::ReleaseVideoBuffer", HITRACE_LEVEL_DEBUG);
3347     std::unique_lock<std::mutex> lock(mutex_);
3348     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " ReleaseVideoBuffer start, state:%{public}d.",
3349         FAKE_POINTER(this), captureState_);
3350     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::STARTED, MSERR_INVALID_OPERATION,
3351         "ReleaseVideoBuffer failed, capture is not STARTED, state:%{public}d", captureState_);
3352 
3353     CHECK_AND_RETURN_RET_LOG(surfaceCb_ != nullptr, MSERR_NO_MEMORY, "ReleaseVideoBuffer failed, callback is nullptr");
3354     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " ReleaseVideoBuffer end.", FAKE_POINTER(this));
3355     return (static_cast<ScreenCapBufferConsumerListener *>(surfaceCb_.GetRefPtr()))->ReleaseVideoBuffer();
3356 }
3357 
ExcludeContent(ScreenCaptureContentFilter & contentFilter)3358 int32_t ScreenCaptureServer::ExcludeContent(ScreenCaptureContentFilter &contentFilter)
3359 {
3360     std::unique_lock<std::mutex> lock(mutex_);
3361     CHECK_AND_RETURN_RET_LOG(captureState_ != AVScreenCaptureState::STOPPED, MSERR_INVALID_OPERATION,
3362         "ExcludeContent failed, capture is STOPPED");
3363 
3364     MEDIA_LOGI("ScreenCaptureServer::ExcludeContent start");
3365     contentFilter_ = contentFilter;
3366     if (captureState_ == AVScreenCaptureState::STARTED) {
3367         Rosen::DisplayManager::GetInstance().SetVirtualScreenBlackList(virtualScreenId_, contentFilter_.windowIDsVec,
3368             surfaceIdList_, surfaceTypeList_);
3369     }
3370     int32_t ret = MSERR_OK;
3371     if (innerAudioCapture_ != nullptr) {
3372         ret = innerAudioCapture_->UpdateAudioCapturerConfig(contentFilter_);
3373     }
3374 
3375     // For the moment, not support:
3376     // For STREAM, should call AudioCapturer interface to make effect when start
3377     // For CAPTURE FILE, should call Recorder interface to make effect when start
3378     if (ret != MSERR_OK) {
3379         MEDIA_LOGE("ScreenCaptureServer::ExcludeContent UpdateAudioCapturerConfig failed");
3380         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_UNSUPPORT,
3381             "ExcludeContent failed, UpdateAudioCapturerConfig failed");
3382     }
3383     return ret;
3384 }
3385 
SetCaptureArea(uint64_t displayId,OHOS::Rect area)3386 int32_t ScreenCaptureServer::SetCaptureArea(uint64_t displayId, OHOS::Rect area)
3387 {
3388     MediaTrace trace("ScreenCaptureServer::SetCaptureArea");
3389     std::lock_guard<std::mutex> lock(mutex_);
3390     CHECK_AND_RETURN_RET_LOG(CheckDisplayArea(displayId, area), MSERR_INVALID_VAL,
3391         "Check region area failed, invalid input area.");
3392     isRegionCapture_ = true;
3393     regionDisplayId_ = displayId;
3394     regionArea_ = area;
3395     if (captureState_ != AVScreenCaptureState::STARTED) {
3396         MEDIA_LOGI("ScreenCaptureServer::SetCaptureArea, virtual screen not created, return ok");
3397         return MSERR_OK;
3398     }
3399     return SetCaptureAreaInner(regionDisplayId_, regionArea_);
3400 }
3401 
SetCaptureAreaInner(uint64_t displayId,OHOS::Rect area)3402 int32_t ScreenCaptureServer::SetCaptureAreaInner(uint64_t displayId, OHOS::Rect area)
3403 {
3404     MediaTrace trace("ScreenCaptureServer::SetCaptureAreaInner");
3405     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetCaptureAreaInner start, state:%{public}d.",
3406         FAKE_POINTER(this), captureState_);
3407     CHECK_AND_RETURN_RET_LOG(virtualScreenId_ != SCREEN_ID_INVALID, MSERR_INVALID_VAL,
3408         "SetCaptureAreaInner failed virtual screen not init");
3409     ScreenId regionScreenId;
3410     DMRect regionAreaIn;
3411     DMRect regionAreaOut;
3412     regionAreaIn.posX_ = area.x;
3413     regionAreaIn.posY_ = area.y;
3414     regionAreaIn.width_ = static_cast<uint32_t>(area.w);
3415     regionAreaIn.height_ = static_cast<uint32_t>(area.h);
3416     auto ret = Rosen::DisplayManager::GetInstance().GetScreenAreaOfDisplayArea(
3417         displayId, regionAreaIn, regionScreenId, regionAreaOut);
3418     CHECK_AND_RETURN_RET_LOG(ret == DMError::DM_OK, MSERR_INVALID_OPERATION,
3419         "GetScreenAreaOfDisplayArea error: %{public}d", ret);
3420     MEDIA_LOGI("SetCaptureAreaInner after, displayId: %{public}" PRIu64, regionScreenId);
3421     MEDIA_LOGI("SetCaptureAreaInner after, x:%{public}d, y:%{public}d, w:%{public}d, h:%{public}d",
3422         regionAreaOut.posX_, regionAreaOut.posY_, regionAreaOut.width_, regionAreaOut.height_);
3423 
3424     std::vector<ScreenId> mirrorIds;
3425     mirrorIds.push_back(virtualScreenId_);
3426     ret = ScreenManager::GetInstance().MakeMirror(regionScreenId, mirrorIds, regionAreaOut, regionScreenId);
3427     CHECK_AND_RETURN_RET_LOG(ret == DMError::DM_OK, MSERR_UNKNOWN,
3428         "MakeMirror with region error: %{public}d", ret);
3429     displayScreenId_ = displayId;
3430     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetCaptureAreaInner end, state:%{public}d.",
3431         FAKE_POINTER(this), captureState_);
3432     return MSERR_OK;
3433 }
3434 
CheckDisplayArea(uint64_t displayId,OHOS::Rect area)3435 bool ScreenCaptureServer::CheckDisplayArea(uint64_t displayId, OHOS::Rect area)
3436 {
3437     MEDIA_LOGI("CheckDisplayArea input displayId: %{public}" PRIu64, displayId);
3438     sptr<Display> targetDisplay = Rosen::DisplayManager::GetInstance().GetDisplayById(displayId);
3439     CHECK_AND_RETURN_RET_LOG(targetDisplay != nullptr, false,
3440         "CheckDisplayArea failed to get target display, no displayId: %{public}" PRIu64, displayId);
3441     auto screenWidth = targetDisplay->GetWidth();
3442     auto screenHeight = targetDisplay->GetHeight();
3443     MEDIA_LOGI("CheckDisplayArea display with width: %{public}d, height:%{public}d", screenWidth, screenHeight);
3444     if (static_cast<int64_t>(area.x) + area.w > screenWidth || static_cast<int64_t>(area.y) + area.h > screenHeight) {
3445         MEDIA_LOGE("CheckDisplayArea input area out of range");
3446         return false;
3447     }
3448     MEDIA_LOGI("CheckDisplayArea success");
3449     return true;
3450 }
3451 
SetMicrophoneEnabled(bool isMicrophone)3452 int32_t ScreenCaptureServer::SetMicrophoneEnabled(bool isMicrophone)
3453 {
3454     MediaTrace trace("ScreenCaptureServer::SetMicrophoneEnabled");
3455     std::lock_guard<std::mutex> lock(mutex_);
3456     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetMicrophoneEnabled isMicrophoneSwitchTurnOn_:"
3457         "%{public}d, new isMicrophone:%{public}d", FAKE_POINTER(this), isMicrophoneSwitchTurnOn_, isMicrophone);
3458     int32_t ret = MSERR_UNKNOWN;
3459     if (isMicrophone) {
3460         statisticalEventInfo_.enableMic = true;
3461     }
3462     if (captureState_ != AVScreenCaptureState::STARTED) {
3463         isMicrophoneSwitchTurnOn_ = isMicrophone;
3464         return MSERR_OK;
3465     }
3466     CHECK_AND_RETURN_RET_LOG(captureConfig_.audioInfo.micCapInfo.state ==
3467         AVScreenCaptureParamValidationState::VALIDATION_VALID, MSERR_OK, "No Microphone Config");
3468     if (isMicrophone) {
3469         ret = SetMicrophoneOn();
3470         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "SetMicrophoneOn failed");
3471         NotifyStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_MIC_UNMUTED_BY_USER);
3472     } else {
3473         ret = SetMicrophoneOff();
3474         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "SetMicrophoneOff failed");
3475         NotifyStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_MIC_MUTED_BY_USER);
3476     }
3477     isMicrophoneSwitchTurnOn_ = isMicrophone;
3478     // For CAPTURE FILE, should call Recorder interface to make effect
3479     MEDIA_LOGI("SetMicrophoneEnabled OK.");
3480     return MSERR_OK;
3481 }
3482 
SetMicrophoneOn()3483 int32_t ScreenCaptureServer::SetMicrophoneOn()
3484 {
3485 #ifdef SUPPORT_CALL
3486     if (InCallObserver::GetInstance().IsInCall(true) && !IsTelInCallSkipList()) {
3487         MEDIA_LOGE("Try SetMicrophoneOn But In Call");
3488         NotifyStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_MIC_UNAVAILABLE);
3489         return MSERR_UNKNOWN;
3490     }
3491 #endif
3492     int32_t ret = MSERR_UNKNOWN;
3493     if (!micAudioCapture_) {
3494         if (captureConfig_.dataType == DataType::ORIGINAL_STREAM) {
3495             ret = StartStreamMicAudioCapture();
3496             CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "SetMicrophoneOn StartStreamMicAudioCapture failed");
3497         } else if (captureConfig_.dataType == DataType::CAPTURE_FILE) {
3498             ret = StartFileMicAudioCapture();
3499             CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "StartFileMicAudioCapture failed");
3500         }
3501     } else if (micAudioCapture_->IsStop()) {
3502         ret = micAudioCapture_->Start(appInfo_); // Resume
3503         if (ret != MSERR_OK) {
3504             MEDIA_LOGE("micAudioCapture Resume failed");
3505             NotifyStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_MIC_UNAVAILABLE);
3506             return ret;
3507         }
3508     } else if (!micAudioCapture_->IsRecording()) {
3509         MEDIA_LOGE("AudioCapturerState invalid");
3510     }
3511     return MSERR_OK;
3512 }
3513 
SetMicrophoneOff()3514 int32_t ScreenCaptureServer::SetMicrophoneOff()
3515 {
3516     int32_t ret = MSERR_UNKNOWN;
3517     if (recorderFileAudioType_ == AVScreenCaptureMixMode::MIX_MODE && innerAudioCapture_
3518         && !IsInnerCaptureRunning()) {
3519         ret = innerAudioCapture_->Start(appInfo_); // Resume
3520         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "innerAudioCapture Start failed");
3521     }
3522     if (IsMicrophoneCaptureRunning()) {
3523         usleep(AUDIO_CHANGE_TIME);
3524         MEDIA_LOGI("Microphone Pause");
3525         ret = StopMicAudioCapture(); // Pause
3526         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "micAudioCapture Pause failed");
3527     }
3528     return MSERR_OK;
3529 }
3530 
OnSpeakerAliveStatusChanged(bool speakerAliveStatus)3531 int32_t ScreenCaptureServer::OnSpeakerAliveStatusChanged(bool speakerAliveStatus)
3532 {
3533     int32_t ret = MSERR_UNKNOWN;
3534     CHECK_AND_RETURN_RET_LOG(innerAudioCapture_, MSERR_UNKNOWN, "innerAudioCapture_ is nullptr");
3535     if (!speakerAliveStatus && recorderFileAudioType_ == AVScreenCaptureMixMode::MIX_MODE &&
3536         innerAudioCapture_->IsStop()) { // back to headset
3537         ret = innerAudioCapture_->Start(appInfo_); // Resume
3538         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "innerAudioCapture Resume failed");
3539     }
3540     return MSERR_OK;
3541 }
3542 
ReStartMicForVoIPStatusSwitch()3543 int32_t ScreenCaptureServer::ReStartMicForVoIPStatusSwitch()
3544 {
3545     int32_t ret = MSERR_OK;
3546     usleep(AUDIO_CHANGE_TIME);
3547     StopMicAudioCapture();
3548     if (isMicrophoneSwitchTurnOn_) {
3549 #ifdef SUPPORT_CALL
3550         if (InCallObserver::GetInstance().IsInCall(true) && !IsTelInCallSkipList()) {
3551             MEDIA_LOGE("Try ReStartMicForVoIPStatusSwitch But In Call");
3552             NotifyStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_MIC_UNAVAILABLE);
3553             return MSERR_UNKNOWN;
3554         }
3555 #endif
3556         ret = StartMicAudioCapture();
3557         if (ret != MSERR_OK) {
3558             MEDIA_LOGE("OnVoIPStatusChanged StartMicAudioCapture failed, ret: %{public}d", ret);
3559         }
3560     }
3561     return ret;
3562 }
3563 
OnVoIPStatusChanged(bool isInVoIPCall)3564 int32_t ScreenCaptureServer::OnVoIPStatusChanged(bool isInVoIPCall)
3565 {
3566     MEDIA_LOGI("OnVoIPStatusChanged, isInVoIPCall:%{public}d", isInVoIPCall);
3567     if (isInVoIPCall) {
3568         CHECK_AND_RETURN_RET_LOG(innerAudioCapture_, MSERR_UNKNOWN, "innerAudioCapture is nullptr");
3569         if (recorderFileAudioType_ == AVScreenCaptureMixMode::MIX_MODE && innerAudioCapture_->IsStop()) {
3570             int32_t ret = innerAudioCapture_->Start(appInfo_); // Resume
3571             CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "OnVoIPStatusChanged innerAudioCapture Resume failed");
3572         }
3573     }
3574     return ReStartMicForVoIPStatusSwitch();
3575 }
3576 
3577 #ifdef SUPPORT_CALL
TelCallStateUpdated(bool isInTelCall)3578 int32_t ScreenCaptureServer::TelCallStateUpdated(bool isInTelCall)
3579 {
3580     if (!captureConfig_.strategy.keepCaptureDuringCall && isInTelCall) {
3581         StopScreenCaptureByEvent(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_STOPPED_BY_CALL);
3582         Release();
3583         return MSERR_OK;
3584     }
3585     CHECK_AND_RETURN_RET(captureState_ == AVScreenCaptureState::STARTED, MSERR_OK);
3586     if (isInTelCall_.load() == isInTelCall) {
3587         return MSERR_OK;
3588     }
3589     isInTelCall_.store(isInTelCall);
3590     if (isInTelCall) {
3591         return OnTelCallStart();
3592     }
3593     return OnTelCallStop();
3594 }
3595 
TelCallAudioStateUpdated(bool isInTelCallAudio)3596 int32_t ScreenCaptureServer::TelCallAudioStateUpdated(bool isInTelCallAudio)
3597 {
3598     CHECK_AND_RETURN_RET(captureState_ == AVScreenCaptureState::STARTED, MSERR_OK);
3599     if (IsTelInCallSkipList() || isInTelCallAudio_.load() == isInTelCallAudio) {
3600         return MSERR_OK;
3601     }
3602     isInTelCallAudio_.store(isInTelCallAudio);
3603     if (isInTelCallAudio) {
3604         return OnTelCallStart();
3605     }
3606     return OnTelCallStop();
3607 }
3608 
OnTelCallStart()3609 int32_t ScreenCaptureServer::OnTelCallStart()
3610 {
3611     std::lock_guard<std::mutex> lock(inCallMutex_);
3612     MEDIA_LOGI("OnTelCallStart InTelCall:%{public}d, Audio:%{public}d", isInTelCall_.load(), isInTelCallAudio_.load());
3613     int32_t ret = MSERR_OK;
3614     if (!isInTelCall_.load() && !isInTelCallAudio_.load()) {
3615         return ret;
3616     }
3617     if (recorderFileAudioType_ == AVScreenCaptureMixMode::MIX_MODE && innerAudioCapture_ && !IsInnerCaptureRunning()) {
3618         ret = innerAudioCapture_->Start(appInfo_); // Resume
3619         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "tel innerAudioCapture Start failed");
3620     }
3621     if (micAudioCapture_) {
3622         micAudioCapture_->SetIsInTelCall(true);
3623         if (IsMicrophoneCaptureRunning()) {
3624             usleep(AUDIO_CHANGE_TIME);
3625             ret = StopMicAudioCapture(); // Pause
3626             CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "micAudioCapture Pause failed");
3627             NotifyStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_MIC_UNAVAILABLE);
3628         }
3629     }
3630     return MSERR_OK;
3631 }
3632 
OnTelCallStop()3633 int32_t ScreenCaptureServer::OnTelCallStop()
3634 {
3635     MEDIA_LOGI("OnTelCallStop InTelCall:%{public}d, Audio:%{public}d", isInTelCall_.load(), isInTelCallAudio_.load());
3636     int32_t ret = MSERR_OK;
3637     if (isInTelCall_.load() || isInTelCallAudio_.load()) {
3638         return ret;
3639     }
3640     if (micAudioCapture_) {
3641         micAudioCapture_->SetIsInTelCall(false);
3642         if (isMicrophoneSwitchTurnOn_ && micAudioCapture_->IsStop()) {
3643             ret = micAudioCapture_->Start(appInfo_); // Resume
3644             if (ret != MSERR_OK) {
3645                 MEDIA_LOGE("micAudioCapture Resume failed");
3646                 NotifyStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_MIC_UNAVAILABLE);
3647                 return ret;
3648             }
3649         }
3650     }
3651     if (!micAudioCapture_ && isMicrophoneSwitchTurnOn_) {
3652         if (captureConfig_.dataType == DataType::ORIGINAL_STREAM) {
3653             ret = StartStreamMicAudioCapture();
3654             CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "SetMicrophoneOn StartStreamMicAudioCapture failed");
3655         } else if (captureConfig_.dataType == DataType::CAPTURE_FILE) {
3656             ret = StartFileMicAudioCapture();
3657             CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "StartFileMicAudioCapture failed");
3658         }
3659     }
3660     return MSERR_OK;
3661 }
3662 #endif
3663 
SetCanvasRotation(bool canvasRotation)3664 int32_t ScreenCaptureServer::SetCanvasRotation(bool canvasRotation)
3665 {
3666     MediaTrace trace("ScreenCaptureServer::SetCanvasRotation");
3667     std::lock_guard<std::mutex> lock(mutex_);
3668     canvasRotation_ = canvasRotation;
3669     MEDIA_LOGI("ScreenCaptureServer::SetCanvasRotation, canvasRotation:%{public}d", canvasRotation);
3670     CHECK_AND_RETURN_RET(captureState_ == AVScreenCaptureState::STARTED, MSERR_OK);  // Before Start
3671     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetCanvasRotation end.", FAKE_POINTER(this));
3672     return SetCanvasRotationInner();
3673 }
3674 
SetCanvasRotationInner()3675 int32_t ScreenCaptureServer::SetCanvasRotationInner()
3676 {
3677     MediaTrace trace("ScreenCaptureServer::SetCanvasRotationInner");
3678     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetCanvasRotationInner start.", FAKE_POINTER(this));
3679     CHECK_AND_RETURN_RET_LOG(virtualScreenId_ != SCREEN_ID_INVALID, MSERR_INVALID_VAL,
3680                              "SetCanvasRotation failed virtual screen not init");
3681     auto ret = ScreenManager::GetInstance().SetVirtualMirrorScreenCanvasRotation(virtualScreenId_, canvasRotation_);
3682     if (CheckAppVersionForUnsupport(ret)) {
3683         MEDIA_LOGE("SetVirtualMirrorScreenCanvasRotation failed, ret: %{public}d", ret);
3684         return MSERR_UNSUPPORT;
3685     }
3686     CHECK_AND_RETURN_RET_LOG(ret == DMError::DM_OK, MSERR_INVALID_OPERATION,
3687                              "SetVirtualMirrorScreenCanvasRotation failed, ret: %{public}d", ret);
3688     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetCanvasRotationInner OK.", FAKE_POINTER(this));
3689     return MSERR_OK;
3690 }
3691 
ShowCursor(bool showCursor)3692 int32_t ScreenCaptureServer::ShowCursor(bool showCursor)
3693 {
3694     MediaTrace trace("ScreenCaptureServer::ShowCursor");
3695     std::lock_guard<std::mutex> lock(mutex_);
3696     if (showCursor == showCursor_) {
3697         return MSERR_OK;
3698     }
3699     showCursor_ = showCursor;
3700     MEDIA_LOGI("ScreenCaptureServer::ShowCursor, showCursor:%{public}d", showCursor_);
3701     if (captureState_ != AVScreenCaptureState::STARTED) {
3702         MEDIA_LOGI("ScreenCaptureServer::ShowCursor, virtual screen not created, return ok.");
3703         return MSERR_OK;
3704     }
3705     return ShowCursorInner();
3706 }
3707 
ShowCursorInner()3708 int32_t ScreenCaptureServer::ShowCursorInner()
3709 {
3710     MediaTrace trace("ScreenCaptureServer::ShowCursorInner");
3711     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " ShowCursorInner start.", FAKE_POINTER(this));
3712     CHECK_AND_RETURN_RET_LOG(virtualScreenId_ != SCREEN_ID_INVALID, MSERR_INVALID_VAL,
3713         "ShowCursorInner failed, virtual screen not init");
3714     surfaceIdList_ = {};
3715     if (!showCursor_) {
3716         MEDIA_LOGI("ScreenCaptureServer 0x%{public}06" PRIXPTR " ShowCursorInner not show cursor", FAKE_POINTER(this));
3717         surfaceTypeList_ = {};
3718         Rosen::RSSurfaceNodeType surfaceNodeType = OHOS::Rosen::RSSurfaceNodeType::CURSOR_NODE;
3719         surfaceTypeList_.push_back(static_cast<uint8_t>(surfaceNodeType));
3720     } else {
3721         MEDIA_LOGI("ScreenCaptureServer 0x%{public}06" PRIXPTR " ShowCursorInner, show cursor", FAKE_POINTER(this));
3722         surfaceTypeList_ = {};
3723     }
3724     Rosen::DisplayManager::GetInstance().SetVirtualScreenBlackList(virtualScreenId_, contentFilter_.windowIDsVec,
3725         surfaceIdList_, surfaceTypeList_);
3726     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " ShowCursorInner OK.", FAKE_POINTER(this));
3727     return MSERR_OK;
3728 }
3729 
ResizeCanvas(int32_t width,int32_t height)3730 int32_t ScreenCaptureServer::ResizeCanvas(int32_t width, int32_t height)
3731 {
3732     MediaTrace trace("ScreenCaptureServer::ResizeCanvas");
3733     std::lock_guard<std::mutex> lock(mutex_);
3734     MEDIA_LOGI("ScreenCaptureServer::ResizeCanvas start, Width:%{public}d, Height:%{public}d", width, height);
3735     if (captureState_ != AVScreenCaptureState::STARTED) {
3736         MEDIA_LOGE("ResizeCanvas captureState_ invalid, captureState_:%{public}d", captureState_);
3737         return MSERR_INVALID_OPERATION;
3738     }
3739     if ((width <= 0) || (width > VIDEO_FRAME_WIDTH_MAX)) {
3740         MEDIA_LOGE("ResizeCanvas Width is invalid, Width:%{public}d, Height:%{public}d", width, height);
3741         return MSERR_INVALID_VAL;
3742     }
3743     if ((height <= 0) || (height > VIDEO_FRAME_HEIGHT_MAX)) {
3744         MEDIA_LOGE("ResizeCanvas Height is invalid, Width:%{public}d, Height:%{public}d", width, height);
3745         return MSERR_INVALID_VAL;
3746     }
3747     if (captureConfig_.dataType != DataType::ORIGINAL_STREAM) {
3748         MEDIA_LOGE("ResizeCanvas dataType invalid, dataType:%{public}d", captureConfig_.dataType);
3749         return MSERR_INVALID_OPERATION;
3750     }
3751 
3752     auto resizeRet = ScreenManager::GetInstance().ResizeVirtualScreen(virtualScreenId_, width, height);
3753     MEDIA_LOGI("ScreenCaptureServer::ResizeCanvas, ResizeVirtualScreen end, ret: %{public}d ", resizeRet);
3754     if (CheckAppVersionForUnsupport(resizeRet)) {
3755         MEDIA_LOGE("ResizeCanvas failed, resizeRet: %{public}d", resizeRet);
3756         return MSERR_UNSUPPORT;
3757     }
3758     CHECK_AND_RETURN_RET_LOG(resizeRet == DMError::DM_OK, MSERR_INVALID_OPERATION, "ResizeVirtualScreen failed");
3759 
3760     return MSERR_OK;
3761 }
3762 
UpdateSurface(sptr<Surface> surface)3763 int32_t ScreenCaptureServer::UpdateSurface(sptr<Surface> surface)
3764 {
3765     if (!isSurfaceMode_) {
3766         return MSERR_INVALID_OPERATION;
3767     }
3768     MediaTrace trace("ScreenCaptureServer::UpdateSurface");
3769     std::lock_guard<std::mutex> lock(mutex_);
3770     MEDIA_LOGI("ScreenCaptureServer::UpdateSurface start");
3771     if (captureState_ != AVScreenCaptureState::STARTED) {
3772         MEDIA_LOGE("UpdateSurface captureState_ invalid, captureState_:%{public}d", captureState_);
3773         return MSERR_INVALID_OPERATION;
3774     }
3775     CHECK_AND_RETURN_RET_LOG(surface != nullptr, MSERR_INVALID_OPERATION, "UpdateSurface failed, invalid param");
3776 
3777     auto res = ScreenManager::GetInstance().SetVirtualScreenSurface(virtualScreenId_, surface);
3778     MEDIA_LOGI("UpdateSurface, ret: %{public}d ", res);
3779     CHECK_AND_RETURN_RET_LOG(res == DMError::DM_OK, MSERR_UNSUPPORT, "UpdateSurface failed");
3780     surface_ = surface;
3781 
3782     return MSERR_OK;
3783 }
3784 
SkipPrivacyMode(std::vector<uint64_t> & windowIDsVec)3785 int32_t ScreenCaptureServer::SkipPrivacyMode(std::vector<uint64_t> &windowIDsVec)
3786 {
3787     MediaTrace trace("ScreenCaptureServer::SkipPrivacyMode");
3788     std::lock_guard<std::mutex> lock(mutex_);
3789     MEDIA_LOGI("ScreenCaptureServer::SkipPrivacyMode, windowIDsVec size:%{public}d",
3790         static_cast<int32_t>(windowIDsVec.size()));
3791     for (size_t i = 0; i < windowIDsVec.size(); i++) {
3792         MEDIA_LOGD("SkipPrivacyMode windowIDsVec value :%{public}" PRIu64, windowIDsVec[i]);
3793     }
3794     skipPrivacyWindowIDsVec_.assign(windowIDsVec.begin(), windowIDsVec.end());
3795     CHECK_AND_RETURN_RET(captureState_ == AVScreenCaptureState::STARTED, MSERR_OK); // Before Start
3796     return SkipPrivacyModeInner();
3797 }
3798 
SkipPrivacyModeInner()3799 int32_t ScreenCaptureServer::SkipPrivacyModeInner()
3800 {
3801     MediaTrace trace("ScreenCaptureServer::SkipPrivacyModeInner");
3802     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SkipPrivacyModeInner start.", FAKE_POINTER(this));
3803     CHECK_AND_RETURN_RET_LOG(virtualScreenId_ != SCREEN_ID_INVALID, MSERR_INVALID_VAL,
3804                              "SkipPrivacyMode failed virtual screen not init");
3805     auto ret = Rosen::DisplayManager::GetInstance().SetVirtualScreenSecurityExemption(virtualScreenId_,
3806         appInfo_.appPid, skipPrivacyWindowIDsVec_);
3807     if (CheckAppVersionForUnsupport(ret)) {
3808         MEDIA_LOGE("SetVirtualScreenSecurityExemption failed, ret: %{public}d", ret);
3809         return MSERR_UNSUPPORT;
3810     }
3811     CHECK_AND_RETURN_RET_LOG(ret == DMError::DM_OK, MSERR_INVALID_OPERATION,
3812         "SkipPrivacyModeInner failed, ret: %{public}d", ret);
3813     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SkipPrivacyModeInner OK.", FAKE_POINTER(this));
3814     return MSERR_OK;
3815 }
3816 
SetMaxVideoFrameRate(int32_t frameRate)3817 int32_t ScreenCaptureServer::SetMaxVideoFrameRate(int32_t frameRate)
3818 {
3819     MediaTrace trace("ScreenCaptureServer::SetMaxVideoFrameRate");
3820     std::lock_guard<std::mutex> lock(mutex_);
3821     MEDIA_LOGI("ScreenCaptureServer::SetMaxVideoFrameRate start, frameRate:%{public}d", frameRate);
3822     if (captureState_ != AVScreenCaptureState::STARTED) {
3823         MEDIA_LOGE("SetMaxVideoFrameRate captureState_ invalid, captureState_:%{public}d", captureState_);
3824         return MSERR_INVALID_OPERATION;
3825     }
3826     if (frameRate <= 0) {
3827         MEDIA_LOGE("SetMaxVideoFrameRate frameRate is invalid, frameRate:%{public}d", frameRate);
3828         return MSERR_INVALID_VAL;
3829     }
3830 
3831     uint32_t actualRefreshRate = 0;
3832     auto res = ScreenManager::GetInstance().SetVirtualScreenMaxRefreshRate(virtualScreenId_,
3833         static_cast<uint32_t>(frameRate), actualRefreshRate);
3834     if (CheckAppVersionForUnsupport(res)) {
3835         MEDIA_LOGE("SetVirtualScreenMaxRefreshRate failed, res: %{public}d", res);
3836         return MSERR_UNSUPPORT;
3837     }
3838     CHECK_AND_RETURN_RET_LOG(res == DMError::DM_OK, MSERR_INVALID_OPERATION, "SetMaxVideoFrameRate failed");
3839 
3840     MEDIA_LOGI("ScreenCaptureServer::SetMaxVideoFrameRate end, frameRate:%{public}d, actualRefreshRate:%{public}u",
3841         frameRate, actualRefreshRate);
3842     return MSERR_OK;
3843 }
3844 
GetScreenScaleMode(const AVScreenCaptureFillMode & fillMode)3845 ScreenScaleMode ScreenCaptureServer::GetScreenScaleMode(const AVScreenCaptureFillMode &fillMode)
3846 {
3847     MEDIA_LOGI("ScreenCaptureServer::GetScreenScaleMode in, fillMode: %{public}d", fillMode);
3848     static const std::map<AVScreenCaptureFillMode, ScreenScaleMode> modeMap = {
3849         {PRESERVE_ASPECT_RATIO, ScreenScaleMode::UNISCALE_MODE},
3850         {SCALE_TO_FILL, ScreenScaleMode::FILL_MODE}
3851     };
3852     ScreenScaleMode scaleMode = ScreenScaleMode::UNISCALE_MODE;
3853     auto iter = modeMap.find(fillMode);
3854     if (iter != modeMap.end()) {
3855         scaleMode = iter->second;
3856     }
3857     MEDIA_LOGI("ScreenCaptureServer::GetScreenScaleMode succeed, scaleMode: %{public}d", scaleMode);
3858     return scaleMode;
3859 }
3860 
SetScreenScaleMode()3861 int32_t ScreenCaptureServer::SetScreenScaleMode()
3862 {
3863     MediaTrace trace("ScreenCaptureServer::SetScreenScaleMode");
3864     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetScreenScaleMode start.", FAKE_POINTER(this));
3865     CHECK_AND_RETURN_RET_LOG(virtualScreenId_ != SCREEN_ID_INVALID, MSERR_INVALID_VAL,
3866                              "SetScreenScaleMode failed virtual screen not init");
3867     auto ret = ScreenManager::GetInstance().SetVirtualMirrorScreenScaleMode(
3868         virtualScreenId_, GetScreenScaleMode(captureConfig_.strategy.fillMode));
3869     if (ret != DMError::DM_OK) {
3870         MEDIA_LOGW("SetScreenScaleMode failed, ret: %{public}d", ret);
3871         return static_cast<int32_t>(ret);
3872     }
3873     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetScreenScaleMode OK.", FAKE_POINTER(this));
3874     return MSERR_OK;
3875 }
3876 
StopAudioCapture()3877 int32_t ScreenCaptureServer::StopAudioCapture()
3878 {
3879     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StopAudioCapture start.", FAKE_POINTER(this));
3880     StopMicAudioCapture();
3881     StopInnerAudioCapture();
3882     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StopAudioCapture end.", FAKE_POINTER(this));
3883     return MSERR_OK;
3884 }
3885 
StartMicAudioCapture()3886 int32_t ScreenCaptureServer::StartMicAudioCapture()
3887 {
3888     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartMicAudioCapture start, dataType:%{public}d, "
3889         "micCapInfo.state:%{public}d.",
3890         FAKE_POINTER(this), captureConfig_.dataType, captureConfig_.audioInfo.micCapInfo.state);
3891 #ifdef SUPPORT_CALL
3892     if (InCallObserver::GetInstance().IsInCall(true) && !IsTelInCallSkipList()) {
3893         MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " skip creating micAudioCapture", FAKE_POINTER(this));
3894         NotifyStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_MIC_UNAVAILABLE);
3895         return MSERR_OK;
3896     }
3897 #endif
3898     CHECK_AND_RETURN_RET_NOLOG(micAudioCapture_ != nullptr, MSERR_OK);
3899     if (captureConfig_.audioInfo.micCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID) {
3900         MediaTrace trace("ScreenCaptureServer::StartMicAudioCapture");
3901         if (audioSource_) {
3902             micAudioCapture_->SetIsInVoIPCall(audioSource_->GetIsInVoIPCall());
3903         }
3904         int32_t ret = micAudioCapture_->Start(appInfo_);
3905         if (ret != MSERR_OK) {
3906             MEDIA_LOGE("StartMicAudioCapture failed");
3907             NotifyStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_MIC_UNAVAILABLE);
3908             return ret;
3909         }
3910     }
3911     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartMicAudioCapture OK.", FAKE_POINTER(this));
3912     return MSERR_OK;
3913 }
3914 
StopInnerAudioCapture()3915 int32_t ScreenCaptureServer::StopInnerAudioCapture()
3916 {
3917     int32_t ret = MSERR_OK;
3918     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StopInnerAudioCapture start.", FAKE_POINTER(this));
3919     if (innerAudioCapture_ != nullptr) {
3920         MediaTrace trace("ScreenCaptureServer::StopInnerAudioCapture");
3921         ret = innerAudioCapture_->Stop();
3922     }
3923     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StopInnerAudioCapture end.", FAKE_POINTER(this));
3924     return ret;
3925 }
3926 
StopMicAudioCapture()3927 int32_t ScreenCaptureServer::StopMicAudioCapture()
3928 {
3929     int32_t ret = MSERR_OK;
3930     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StopMicAudioCapture start.", FAKE_POINTER(this));
3931     if (micAudioCapture_ != nullptr) {
3932         MediaTrace trace("ScreenCaptureServer::StopAudioCaptureMic");
3933         ret = micAudioCapture_->Stop();
3934     }
3935     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StopMicAudioCapture end.", FAKE_POINTER(this));
3936     return ret;
3937 }
3938 
StopVideoCapture()3939 int32_t ScreenCaptureServer::StopVideoCapture()
3940 {
3941     MediaTrace trace("ScreenCaptureServer::StopVideoCapture");
3942     MEDIA_LOGI("StopVideoCapture");
3943     if ((virtualScreenId_ < 0) || ((consumer_ == nullptr) && !isSurfaceMode_) || !isConsumerStart_) {
3944         MEDIA_LOGI("StopVideoCapture IGNORED, video capture not start");
3945         if (surfaceCb_ != nullptr) {
3946             (static_cast<ScreenCapBufferConsumerListener *>(surfaceCb_.GetRefPtr()))->StopBufferThread();
3947             (static_cast<ScreenCapBufferConsumerListener *>(surfaceCb_.GetRefPtr()))->Release();
3948             surfaceCb_ = nullptr;
3949         }
3950         return MSERR_OK;
3951     }
3952 
3953     DestroyVirtualScreen();
3954     if (consumer_ != nullptr) {
3955         consumer_->UnregisterConsumerListener();
3956         consumer_ = nullptr;
3957     }
3958 
3959     if (surfaceCb_ != nullptr) {
3960         (static_cast<ScreenCapBufferConsumerListener *>(surfaceCb_.GetRefPtr()))->StopBufferThread();
3961         (static_cast<ScreenCapBufferConsumerListener *>(surfaceCb_.GetRefPtr()))->Release();
3962         surfaceCb_ = nullptr;
3963     }
3964     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StopVideoCapture end.", FAKE_POINTER(this));
3965     return MSERR_OK;
3966 }
3967 
StopScreenCaptureRecorder()3968 int32_t ScreenCaptureServer::StopScreenCaptureRecorder()
3969 {
3970     MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances StopScreenCaptureRecorder S", FAKE_POINTER(this));
3971     MediaTrace trace("ScreenCaptureServer::StopScreenCaptureRecorder");
3972     int32_t ret = MSERR_OK;
3973     if (recorder_ != nullptr) {
3974         stopAcquireAudioBufferFromAudio_.store(true);
3975         if (recorderFileAudioType_ == AVScreenCaptureMixMode::MIX_MODE &&
3976             audioSource_ && audioSource_->IsInWaitMicSyncState() &&
3977             IsInnerCaptureRunning()) {
3978             int64_t currentAudioTime;
3979             innerAudioCapture_->GetCurrentAudioTime(currentAudioTime);
3980             MEDIA_LOGI("0x%{public}06" PRIXPTR " UseUpAllLeftBuffer currentAudioTime: %{public}" PRId64,
3981                 FAKE_POINTER(this), currentAudioTime);
3982             innerAudioCapture_->UseUpAllLeftBufferUntil(currentAudioTime);
3983         }
3984         ret = recorder_->Stop(false);
3985         if (ret != MSERR_OK) {
3986             MEDIA_LOGE("StopScreenCaptureRecorder recorder stop failed, ret:%{public}d", ret);
3987         }
3988         DestroyVirtualScreen();
3989         recorder_->Release();
3990         recorder_ = nullptr;
3991         StopAudioCapture();
3992     }
3993     showCursor_ = true;
3994     surfaceIdList_ = {};
3995     surfaceTypeList_ = {};
3996     captureCallback_ = nullptr;
3997     isConsumerStart_ = false;
3998     return ret;
3999 }
4000 
StopScreenCaptureByEvent(AVScreenCaptureStateCode stateCode)4001 int32_t ScreenCaptureServer::StopScreenCaptureByEvent(AVScreenCaptureStateCode stateCode)
4002 {
4003     MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances StopScreenCaptureByEvent S", FAKE_POINTER(this));
4004     MediaTrace trace("ScreenCaptureServer::StopScreenCaptureByEvent");
4005     if (captureState_ == AVScreenCaptureState::STOPPED) {
4006         MEDIA_LOGI("StopScreenCaptureByEvent repeat, capture is STOPPED.");
4007         return MSERR_OK;
4008     }
4009     std::lock_guard<std::mutex> lock(mutex_);
4010     return StopScreenCaptureInner(stateCode);
4011 }
4012 
SetSystemScreenRecorderStatus(bool status)4013 void ScreenCaptureServer::SetSystemScreenRecorderStatus(bool status)
4014 {
4015     if (GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"]
4016             .compare(appName_) != 0) {
4017         return;
4018     }
4019     if (status) {
4020         (ScreenCaptureServer::systemScreenRecorderPid_).store(appInfo_.appPid);
4021         ScreenCaptureMonitorServer::GetInstance()->SetSystemScreenRecorderStatus(true);
4022     } else {
4023         ScreenCaptureMonitorServer::GetInstance()->SetSystemScreenRecorderStatus(false);
4024     }
4025 }
4026 
StopScreenCaptureInner(AVScreenCaptureStateCode stateCode)4027 int32_t ScreenCaptureServer::StopScreenCaptureInner(AVScreenCaptureStateCode stateCode)
4028 {
4029     MediaTrace trace("ScreenCaptureServer::StopScreenCaptureInner");
4030     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StopScreenCaptureInner start, stateCode:%{public}d.",
4031         FAKE_POINTER(this), stateCode);
4032     if (screenCaptureCb_ != nullptr) {
4033         (static_cast<ScreenCaptureListenerCallback *>(screenCaptureCb_.get()))->Stop();
4034     }
4035     if (audioSource_ && audioSource_->GetAppPid() > 0) { // DataType::CAPTURE_FILE
4036         audioSource_->UnregisterAudioRendererEventListener(audioSource_->GetAppPid());
4037     }
4038     DisplayManager::GetInstance().UnregisterPrivateWindowListener(displayListener_);
4039     if (captureState_ == AVScreenCaptureState::CREATED || captureState_ == AVScreenCaptureState::POPUP_WINDOW ||
4040         captureState_ == AVScreenCaptureState::STARTING) {
4041         StopNotStartedScreenCapture(stateCode);
4042         return MSERR_OK;
4043     }
4044     CHECK_AND_RETURN_RET(captureState_ != AVScreenCaptureState::STOPPED, MSERR_OK);
4045     int32_t ret = MSERR_OK;
4046     DestroyPrivacySheet();
4047     if (captureConfig_.dataType == DataType::CAPTURE_FILE) {
4048         ret = StopScreenCaptureRecorder();
4049     } else if (captureConfig_.dataType == DataType::ORIGINAL_STREAM) {
4050         int32_t retAudio = StopAudioCapture();
4051         int32_t retVideo = StopVideoCapture();
4052         ret = (retAudio == MSERR_OK && retVideo == MSERR_OK) ? MSERR_OK : MSERR_STOP_FAILED;
4053     } else {
4054         MEDIA_LOGW("StopScreenCaptureInner unsupport and ignore");
4055         return MSERR_OK;
4056     }
4057     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::STARTED, ret, "state:%{public}d", captureState_);
4058     SetErrorInfo(MSERR_OK, "normal stopped", StopReason::NORMAL_STOPPED, IsUserPrivacyAuthorityNeeded());
4059     PostStopScreenCapture(stateCode);
4060 #ifdef SUPPORT_CALL
4061     InCallObserver::GetInstance().UnregisterInCallObserverCallBack(screenCaptureObserverCb_);
4062 #endif
4063     AccountObserver::GetInstance().UnregisterAccountObserverCallBack(screenCaptureObserverCb_);
4064     if (screenCaptureObserverCb_) {
4065         screenCaptureObserverCb_->Release();
4066     }
4067     ScreenManager::GetInstance().UnregisterScreenListener(screenConnectListener_);
4068     UnRegisterWindowLifecycleListener();
4069     UnRegisterWindowInfoChangedListener();
4070     UnRegisterLanguageSwitchListener();
4071     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StopScreenCaptureInner end.", FAKE_POINTER(this));
4072     return ret;
4073 }
4074 
StopNotStartedScreenCapture(AVScreenCaptureStateCode stateCode)4075 void ScreenCaptureServer::StopNotStartedScreenCapture(AVScreenCaptureStateCode stateCode)
4076 {
4077     DestroyPopWindow();
4078     captureState_ = AVScreenCaptureState::STOPPED;
4079     SetSystemScreenRecorderStatus(false);
4080     ScreenCaptureMonitorServer::GetInstance()->CallOnScreenCaptureFinished(appInfo_.appPid);
4081     if (screenCaptureCb_ != nullptr && stateCode != AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INVLID) {
4082         screenCaptureCb_->OnStateChange(stateCode);
4083     }
4084     isSurfaceMode_ = false;
4085     surface_ = nullptr;
4086     SetErrorInfo(MSERR_OK, "normal stopped", StopReason::NORMAL_STOPPED, IsUserPrivacyAuthorityNeeded());
4087 }
4088 
DestroyPrivacySheet()4089 bool ScreenCaptureServer::DestroyPrivacySheet()
4090 {
4091     // start ability, tell ability to destroy pop window
4092     MEDIA_LOGI("DestroyPrivacySheet start.");
4093     AAFwk::Want want;
4094 
4095     std::string bundleName = GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"];
4096     if (bundleName.empty()) {
4097         MEDIA_LOGE("Failed to get screenrecorder bundlename.");
4098         return false;
4099     }
4100     AppExecFwk::ElementName element("", bundleName, "PrivacyControlAbility");
4101 
4102     want.SetElement(element);
4103     want.SetParam("appLabel", callingLabel_);
4104     want.SetParam("sessionId", sessionId_);
4105     want.SetParam("terminateSelf", true);
4106     ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
4107     MEDIA_LOGI("DestroyPrivacySheet StartAbility end %{public}d", ret);
4108     if (ret != ERR_OK) {
4109         MEDIA_LOGE("Failed to start ability to destroy privacy sheet, error code : %{public}d", ret);
4110     }
4111 
4112     return ret == ERR_OK;
4113 }
4114 
DestroyPopWindow()4115 bool ScreenCaptureServer::DestroyPopWindow()
4116 {
4117     if (captureState_ != AVScreenCaptureState::POPUP_WINDOW) {
4118         MEDIA_LOGI("window not pop up, no need to destroy.");
4119         return true;
4120     }
4121 #ifdef PC_STANDARD
4122     if (IsPickerPopUp()) {
4123         MEDIA_LOGI("DestroyPopWindow end, type: picker, deviceType: PC.");
4124         ErrCode ret = ERR_INVALID_VALUE;
4125         AAFwk::Want want;
4126         AppExecFwk::ElementName element("",
4127             GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"],
4128             SELECT_ABILITY_NAME); // DeviceID
4129         want.SetElement(element);
4130         want.SetParam("appLabel", callingLabel_);
4131         want.SetParam("sessionId", sessionId_);
4132         want.SetParam("terminateSelf", true); // inform picker to terminateSelf
4133         SendConfigToUIParams(want);
4134         ret = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
4135         MEDIA_LOGI("Destroy picker end %{public}d, DeviceType: PC", ret);
4136         return ret == ERR_OK;
4137     } else {
4138         if (connection_ != nullptr) {
4139             MEDIA_LOGI("DestroyPopWindow close dialog, deviceType: PC.");
4140             return connection_->CloseDialog();
4141         }
4142     }
4143 #else
4144     if (connection_ != nullptr) {
4145         MEDIA_LOGI("DestroyPopWindow close dialog, deviceType: Phone.");
4146         return connection_->CloseDialog();
4147     }
4148 #endif
4149     return true;
4150 }
4151 
IsLastStartedPidInstance(int32_t pid)4152 bool ScreenCaptureServer::IsLastStartedPidInstance(int32_t pid)
4153 {
4154     MEDIA_LOGI("ScreenCaptureServer::IsLastStartedPidInstance START.");
4155     if (CountStartedScreenCaptureServerNumByPid(pid) != 1) {
4156         MEDIA_LOGD("pid: %{public}d exists more than one instance.", pid);
4157         return false;
4158     }
4159     return true;
4160 }
4161 
LastPidUpdatePrivacyUsingPermissionState(int32_t pid)4162 bool ScreenCaptureServer::LastPidUpdatePrivacyUsingPermissionState(int32_t pid)
4163 {
4164     if (IsLastStartedPidInstance(pid)) {
4165         return UpdatePrivacyUsingPermissionState(STOP_VIDEO);
4166     }
4167     return true;
4168 }
4169 
PostStopScreenCapture(AVScreenCaptureStateCode stateCode)4170 void ScreenCaptureServer::PostStopScreenCapture(AVScreenCaptureStateCode stateCode)
4171 {
4172     MediaTrace trace("ScreenCaptureServer::PostStopScreenCapture");
4173     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " PostStopScreenCapture start, stateCode:%{public}d.",
4174         FAKE_POINTER(this), stateCode);
4175     SetSystemScreenRecorderStatus(false);
4176     ScreenCaptureMonitorServer::GetInstance()->CallOnScreenCaptureFinished(appInfo_.appPid);
4177     if (screenCaptureCb_ != nullptr && stateCode != AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INVLID) {
4178         screenCaptureCb_->OnStateChange(stateCode);
4179     }
4180 #ifdef SUPPORT_SCREEN_CAPTURE_WINDOW_NOTIFICATION
4181     if (isPrivacyAuthorityEnabled_ &&
4182         GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"]
4183             .compare(appName_) != 0 && !isScreenCaptureAuthority_) {
4184         // Remove real time notification
4185         int32_t ret = NotificationHelper::CancelNotification(notificationId_);
4186         MEDIA_LOGI("StopScreenCaptureInner CancelNotification id:%{public}d, ret:%{public}d ", notificationId_, ret);
4187         micCount_.store(0);
4188     }
4189 #endif
4190     isPrivacyAuthorityEnabled_ = false;
4191     isRegionCapture_ = false;
4192 
4193     if (!LastPidUpdatePrivacyUsingPermissionState(appInfo_.appPid)) {
4194         MEDIA_LOGE("UpdatePrivacyUsingPermissionState STOP failed, dataType:%{public}d", captureConfig_.dataType);
4195     }
4196     std::unordered_map<std::string, std::string> payload;
4197     int64_t value = ResourceSchedule::ResType::ScreenCaptureStatus::STOP_SCREEN_CAPTURE;
4198     ResSchedReportData(value, payload);
4199     RemoveStartedSessionIdList(this->sessionId_);
4200     MEDIA_LOGI("PostStopScreenCapture sessionId: %{public}d is removed from list, list_size is %{public}d.",
4201         this->sessionId_, static_cast<uint32_t>(ScreenCaptureServer::startedSessionIDList_.size()));
4202     captureState_ = AVScreenCaptureState::STOPPED;
4203 }
4204 
StopScreenCapture()4205 int32_t ScreenCaptureServer::StopScreenCapture()
4206 {
4207     MediaTrace trace("ScreenCaptureServer::StopScreenCapture");
4208     MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances StopScreenCapture S", FAKE_POINTER(this));
4209 
4210     if (captureState_ == AVScreenCaptureState::STOPPED) {
4211         MEDIA_LOGI("StopScreenCapture repeat, capture is STOPPED.");
4212         return MSERR_OK;
4213     }
4214     std::lock_guard<std::mutex> lock(mutex_);
4215     int32_t ret = StopScreenCaptureInner(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INVLID);
4216     if (statisticalEventInfo_.startLatency == -1) {
4217         statisticalEventInfo_.captureDuration = -1; // latency -1 means invalid
4218     } else {
4219         int64_t endTime = GetCurrentMillisecond();
4220         statisticalEventInfo_.captureDuration = static_cast<int32_t>(endTime - startTime_ -
4221             statisticalEventInfo_.startLatency);
4222     }
4223 
4224     MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances StopScreenCapture E", FAKE_POINTER(this));
4225     isScreenCaptureAuthority_ = false;
4226     return ret;
4227 }
4228 
Release()4229 void ScreenCaptureServer::Release()
4230 {
4231     ReleaseInner();
4232 }
4233 
ReleaseInner()4234 void ScreenCaptureServer::ReleaseInner()
4235 {
4236     MediaTrace trace("ScreenCaptureServer::ReleaseInner");
4237     MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances ReleaseInner S", FAKE_POINTER(this));
4238     if (captureState_ != AVScreenCaptureState::STOPPED) {
4239         std::lock_guard<std::mutex> lock(mutex_);
4240         if (captureState_ != AVScreenCaptureState::STOPPED) {
4241             StopScreenCaptureInner(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INVLID);
4242             MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances ReleaseInner Stop done, sessionId:%{public}d",
4243                 FAKE_POINTER(this), sessionId_);
4244         }
4245     }
4246     MEDIA_LOGI("ScreenCaptureServer::ReleaseInner before RemoveScreenCaptureServerMap");
4247 
4248     RemoveSaAppInfoMap(saUid_);
4249     RemoveScreenCaptureServerMap(sessionId_);
4250     sessionId_ = SESSION_ID_INVALID;
4251     MEDIA_LOGD("ReleaseInner removeMap success, mapSize: %{public}d",
4252         static_cast<int32_t>(ScreenCaptureServer::serverMap_.size()));
4253     skipPrivacyWindowIDsVec_.clear();
4254     SetMetaDataReport();
4255     MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances ReleaseInner E", FAKE_POINTER(this));
4256 }
4257 
SetScreenCaptureStrategy(ScreenCaptureStrategy strategy)4258 int32_t ScreenCaptureServer::SetScreenCaptureStrategy(ScreenCaptureStrategy strategy)
4259 {
4260     std::lock_guard<std::mutex> lock(mutex_);
4261     CHECK_AND_RETURN_RET_LOG(captureState_ < AVScreenCaptureState::POPUP_WINDOW, MSERR_INVALID_STATE,
4262         "strategy can not be modified after screen capture started");
4263     MEDIA_LOGI("SetScreenCaptureStrategy enableDeviceLevelCapture: %{public}d, keepCaptureDuringCall: %{public}d,"
4264                "strategyForPrivacyMaskMode: %{public}d, canvasFollowRotation: %{public}d, enableBFrame: %{public}d,"
4265                "pickerPopUp: %{public}d, fillMode: %{public}d",
4266         strategy.enableDeviceLevelCapture, strategy.keepCaptureDuringCall, strategy.strategyForPrivacyMaskMode,
4267         strategy.canvasFollowRotation, strategy.enableBFrame, static_cast<int32_t>(strategy.pickerPopUp),
4268         static_cast<int32_t>(strategy.fillMode));
4269     captureConfig_.strategy = strategy;
4270     return MSERR_OK;
4271 }
4272 
GetWantAgent(const std::string & callingLabel,int32_t sessionId)4273 std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> ScreenCaptureServer::GetWantAgent(
4274     const std::string& callingLabel,
4275     int32_t sessionId)
4276 {
4277     MEDIA_LOGI("GetWantAgent, setWantAgent");
4278     auto want = std::make_shared<AAFwk::Want>();
4279     AppExecFwk::ElementName element("",
4280         GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"],
4281         "PrivacyControlAbility");
4282     want->SetElement(element);
4283     want->SetParam("appLabel", callingLabel);
4284     want->SetParam("sessionId", sessionId);
4285     std::vector<std::shared_ptr<AAFwk::Want>> wants;
4286     wants.push_back(want);
4287     MEDIA_LOGI("GetWantAgent, setWantAgent set all params");
4288     std::vector<AbilityRuntime::WantAgent::WantAgentConstant::Flags> flags;
4289     flags.push_back(AbilityRuntime::WantAgent::WantAgentConstant::Flags::UPDATE_PRESENT_FLAG);
4290     AbilityRuntime::WantAgent::WantAgentInfo wantAgentInfo(0,
4291         AbilityRuntime::WantAgent::WantAgentConstant::OperationType::START_ABILITY,
4292         flags, wants, nullptr);
4293     MEDIA_LOGI("GetWantAgent, setWantAgent create wantAgentInfo");
4294     MEDIA_LOGI("GetWantAgent, setWantAgent get wantAgent");
4295     return OHOS::AbilityRuntime::WantAgent::WantAgentHelper::GetWantAgent(wantAgentInfo);
4296 }
4297 
SetPublishRequest(const std::shared_ptr<NotificationLocalLiveViewContent> & localLiveViewContent,int32_t notificationId,NotificationRequest & request)4298 void ScreenCaptureServer::SetPublishRequest(
4299     const std::shared_ptr<NotificationLocalLiveViewContent>& localLiveViewContent,
4300     int32_t notificationId, NotificationRequest& request)
4301 {
4302     std::shared_ptr<NotificationContent> notificationContent =
4303         std::make_shared<NotificationContent>(localLiveViewContent);
4304 
4305         request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
4306         request.SetNotificationId(notificationId);
4307         request.SetContent(notificationContent);
4308         request.SetCreatorUid(AV_SCREEN_CAPTURE_SESSION_UID);
4309         request.SetOwnerUid(AV_SCREEN_CAPTURE_SESSION_UID);
4310         request.SetUnremovable(true);
4311         request.SetInProgress(true);
4312         std::shared_ptr<PixelMap> pixelMapTotalSpr = GetPixelMap(ICON_PATH_NOTIFICATION);
4313         request.SetLittleIcon(pixelMapTotalSpr);
4314         request.SetBadgeIconStyle(NotificationRequest::BadgeStyle::LITTLE);
4315 }
4316 
SystemPrivacyProtected(ScreenId & virtualScreenId,bool systemPrivacyProtectionSwitch)4317 void ScreenCaptureServer::SystemPrivacyProtected(ScreenId& virtualScreenId, bool systemPrivacyProtectionSwitch)
4318 {
4319     std::vector<ScreenId> screenIds;
4320     screenIds.push_back(virtualScreenId);
4321     auto ret = ScreenManager::GetInstance().SetScreenSkipProtectedWindow(screenIds, systemPrivacyProtectionSwitch);
4322     if (ret == DMError::DM_OK || ret == DMError::DM_ERROR_DEVICE_NOT_SUPPORT) {
4323         MEDIA_LOGI("SystemPrivacyProtected SetScreenSkipProtectedWindow success");
4324     } else {
4325         MEDIA_LOGI("SystemPrivacyProtected SetScreenSkipProtectedWindow failed, ret: %{public}d", ret);
4326     }
4327 }
4328 
AppPrivacyProtected(ScreenId & virtualScreenId,bool appPrivacyProtectionSwitch)4329 void ScreenCaptureServer::AppPrivacyProtected(ScreenId& virtualScreenId, bool appPrivacyProtectionSwitch)
4330 {
4331     std::vector<std::string> privacyWindowTags;
4332     privacyWindowTags.push_back("TAG_SCREEN_PROTECTION_SENSITIVE_APP");
4333     auto ret = ScreenManager::GetInstance().SetScreenPrivacyWindowTagSwitch(virtualScreenId,
4334         privacyWindowTags, appPrivacyProtectionSwitch);
4335     if (ret == DMError::DM_OK || ret == DMError::DM_ERROR_DEVICE_NOT_SUPPORT) {
4336         MEDIA_LOGI("AppPrivacyProtected SetScreenSkipProtectedWindow success");
4337     } else {
4338         MEDIA_LOGI("AppPrivacyProtected SetScreenSkipProtectedWindow failed, ret: %{public}d", ret);
4339     }
4340 }
4341 
ScreenCaptureObserverCallBack(std::weak_ptr<ScreenCaptureServer> screenCaptureServer)4342 ScreenCaptureObserverCallBack::ScreenCaptureObserverCallBack(
4343     std::weak_ptr<ScreenCaptureServer> screenCaptureServer): taskQueObserverCb_("NotifyStopSc")
4344 {
4345     screenCaptureServer_ = screenCaptureServer;
4346     taskQueObserverCb_.Start();
4347     MEDIA_LOGD("0x%{public}06" PRIXPTR " ScreenCaptureObserverCallBack Instances create", FAKE_POINTER(this));
4348 }
4349 
StopAndRelease(AVScreenCaptureStateCode state)4350 bool ScreenCaptureObserverCallBack::StopAndRelease(AVScreenCaptureStateCode state)
4351 {
4352     MEDIA_LOGI("ScreenCaptureObserverCallBack::StopAndRelease");
4353     auto scrServer = screenCaptureServer_.lock();
4354     if (scrServer && !scrServer->IsTelInCallSkipList()) {
4355         if (scrServer->GetSCServerCaptureState() == AVScreenCaptureState::STOPPED) {
4356             MEDIA_LOGI("StopAndRelease repeat, capture is STOPPED.");
4357             return true;
4358         }
4359         scrServer->StopScreenCaptureByEvent(state);
4360         scrServer->Release();
4361     }
4362     return true;
4363 }
4364 
NotifyStopAndRelease(AVScreenCaptureStateCode state)4365 bool ScreenCaptureObserverCallBack::NotifyStopAndRelease(AVScreenCaptureStateCode state)
4366 {
4367     MEDIA_LOGI("ScreenCaptureObserverCallBack::NotifyStopAndRelease START.");
4368     auto task = std::make_shared<TaskHandler<void>>([&, this, state] {
4369         StopAndRelease(state);
4370     });
4371     int32_t res = taskQueObserverCb_.EnqueueTask(task);
4372     CHECK_AND_RETURN_RET_LOG(res == MSERR_OK, false, "NotifyStopAndRelease EnqueueTask failed.");
4373     return true;
4374 }
4375 
4376 #ifdef SUPPORT_CALL
TelCallStateUpdated(bool isInCall)4377 bool ScreenCaptureObserverCallBack::TelCallStateUpdated(bool isInCall)
4378 {
4379     MEDIA_LOGI("ScreenCaptureObserverCallBack::TelCallStateUpdated InCall:%{public}d", isInCall);
4380     auto scrServer = screenCaptureServer_.lock();
4381     if (scrServer && !scrServer->IsTelInCallSkipList()) {
4382         if (scrServer->GetSCServerCaptureState() == AVScreenCaptureState::STOPPED) {
4383             MEDIA_LOGI("TelCallStateUpdated: capture is STOPPED.");
4384             return true;
4385         }
4386         scrServer->TelCallStateUpdated(isInCall);
4387     }
4388     return true;
4389 }
4390 
NotifyTelCallStateUpdated(bool isInCall)4391 bool ScreenCaptureObserverCallBack::NotifyTelCallStateUpdated(bool isInCall)
4392 {
4393     MEDIA_LOGD("ScreenCaptureObserverCallBack::NotifyTelCallStateUpdated START InCall:%{public}d", isInCall);
4394     auto task = std::make_shared<TaskHandler<void>>([&, this, isInCall] {
4395         TelCallStateUpdated(isInCall);
4396     });
4397     int32_t res = taskQueObserverCb_.EnqueueTask(task);
4398     CHECK_AND_RETURN_RET_LOG(res == MSERR_OK, false, "NotifyTelCallStateUpdated EnqueueTask failed.");
4399     return true;
4400 }
4401 #endif
4402 
Release()4403 void ScreenCaptureObserverCallBack::Release()
4404 {
4405     std::lock_guard<std::mutex> lock(mutex_);
4406     taskQueObserverCb_.Stop();
4407 }
4408 
~ScreenCaptureObserverCallBack()4409 ScreenCaptureObserverCallBack::~ScreenCaptureObserverCallBack()
4410 {
4411     std::lock_guard<std::mutex> lock(mutex_);
4412     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
4413 }
4414 
OnBufferAvailable()4415 void ScreenCapBufferConsumerListener::OnBufferAvailable()
4416 {
4417     MediaTrace trace("ScreenCapConsumer::OnBufferAvailable", HITRACE_LEVEL_DEBUG);
4418     MEDIA_LOGD("ScreenCapConsumer: 0x%{public}06" PRIXPTR " OnBufferAvailable start.", FAKE_POINTER(this));
4419     {
4420         std::lock_guard<std::mutex> lock(bufferAvailableWorkerMtx_);
4421         messageQueueSCB_.push({SCBufferMessageType::GET_BUFFER, "Get buffer!"});
4422         MEDIA_LOGD("ScreenCapConsumer: 0x%{public}06" PRIXPTR " queue size: %{public}" PRId64, FAKE_POINTER(this),
4423             static_cast<uint64_t>(messageQueueSCB_.size()));
4424         bufferAvailableWorkerCv_.notify_one();
4425     }
4426 }
4427 
OnBufferAvailableAction()4428 void ScreenCapBufferConsumerListener::OnBufferAvailableAction()
4429 {
4430     MediaTrace trace("ScreenCapConsumer::OnBufferAvailableAction", HITRACE_LEVEL_DEBUG);
4431     MEDIA_LOGD("OnBufferAvailableAction: 0x%{public}06" PRIXPTR " start.", FAKE_POINTER(this));
4432     CHECK_AND_RETURN(consumer_ != nullptr);
4433     int64_t timestamp = 0;
4434     OHOS::Rect damage;
4435     OHOS::sptr<OHOS::SurfaceBuffer> buffer = nullptr;
4436     sptr<SyncFence> acquireFence = SyncFence::INVALID_FENCE;
4437     int32_t acquireBufferRet = consumer_->AcquireBuffer(buffer, acquireFence, timestamp, damage);
4438     if (acquireBufferRet != GSERROR_OK) {
4439         MEDIA_LOGE("OnBufferAvailableAction AcquireBuffer Fail Code %{public}d", acquireBufferRet);
4440     }
4441     MEDIA_LOGD("OnBufferAvailableAction: 0x%{public}06" PRIXPTR " after AcquireBuffer.", FAKE_POINTER(this));
4442     int32_t flushFence = -1;
4443     if (acquireFence != nullptr && acquireFence != SyncFence::INVALID_FENCE) {
4444         acquireFence->Wait(1000); // 1000 ms
4445         flushFence = acquireFence->Get();
4446     }
4447     CHECK_AND_RETURN_LOG(buffer != nullptr, "Acquire SurfaceBuffer failed");
4448     if ((buffer->GetUsage() & BUFFER_USAGE_MEM_MMZ_CACHE) != 0) {
4449         MEDIA_LOGD("OnBufferAvailableAction cache enable");
4450         buffer->InvalidateCache();
4451     }
4452     void *addr = buffer->GetVirAddr();
4453     if (addr == nullptr) {
4454         MEDIA_LOGE("Acquire SurfaceBuffer address invalid");
4455         int32_t releaseBufferRet = consumer_->ReleaseBuffer(buffer, -1); // -1 not wait
4456         if (releaseBufferRet != GSERROR_OK) {
4457             MEDIA_LOGE("OnBufferAvailableAction: 0x%{public}06" PRIXPTR " ReleaseBuffer Fail Code %{public}d",
4458                 FAKE_POINTER(this), releaseBufferRet);
4459         }
4460         return;
4461     }
4462     MEDIA_LOGD("OnBufferAvailableAction SurfaceBuffer size: %{public}u", buffer->GetSize());
4463     {
4464         std::unique_lock<std::mutex> lock(bufferMutex_);
4465         if (availBuffers_.size() > MAX_BUFFER_SIZE) {
4466             MEDIA_LOGE("OnBufferAvailableAction consume slow, drop video frame");
4467             int32_t releaseBufferRet = consumer_->ReleaseBuffer(buffer, -1); // -1 not wait
4468             if (releaseBufferRet != GSERROR_OK) {
4469                 MEDIA_LOGE("OnBufferAvailableAction: 0x%{public}06" PRIXPTR " consume slow ReleaseBuffer "
4470                     "Fail Code %{public}d", FAKE_POINTER(this), releaseBufferRet);
4471             }
4472             return;
4473         }
4474         availBuffers_.push(std::make_unique<SurfaceBufferEntry>(buffer, flushFence, timestamp, damage));
4475     }
4476     bufferCond_.notify_all();
4477     ProcessVideoBufferCallBack();
4478 }
4479 
SurfaceBufferThreadRun()4480 void ScreenCapBufferConsumerListener::SurfaceBufferThreadRun()
4481 {
4482     SCBufferMessage message;
4483     std::string threadName = std::string("OS_SCBufferAvailableWorker");
4484     MEDIA_LOGD("0x%{public}06" PRIXPTR " BufferAvailableWorker name: %{public}s",
4485         FAKE_POINTER(this), threadName.c_str());
4486     pthread_setname_np(pthread_self(), threadName.c_str());
4487     while (true) {
4488         {
4489             std::unique_lock<std::mutex> lock(bufferAvailableWorkerMtx_);
4490             bufferAvailableWorkerCv_.wait(lock, [&]() { return !messageQueueSCB_.empty(); });
4491             message = messageQueueSCB_.front();
4492             if (static_cast<uint64_t>(messageQueueSCB_.size()) > MAX_MESSAGE_QUEUE_SIZE &&
4493                 message.type == SCBufferMessageType::GET_BUFFER) {
4494                 messageQueueSCB_.pop();
4495                 MEDIA_LOGE("0x%{public}06" PRIXPTR " BufferAvailableWorker skip get buffer", FAKE_POINTER(this));
4496                 continue;
4497             }
4498             messageQueueSCB_.pop();
4499         }
4500         if (message.type == SCBufferMessageType::EXIT) {
4501             break;
4502         }
4503         if (message.type == SCBufferMessageType::GET_BUFFER) {
4504             OnBufferAvailableAction();
4505         }
4506     }
4507     MEDIA_LOGD("ScreenCapBufferConsumerListener::SurfaceBufferThreadRun End.");
4508 }
4509 
StartBufferThread()4510 int32_t ScreenCapBufferConsumerListener::StartBufferThread()
4511 {
4512     if (isSurfaceCbInThreadStopped_.load()) {
4513         surfaceCbInThread_ = new (std::nothrow) std::thread([this]() {
4514             isSurfaceCbInThreadStopped_.store(false);
4515             this->SurfaceBufferThreadRun();
4516             isSurfaceCbInThreadStopped_.store(true);
4517         });
4518         CHECK_AND_RETURN_RET_LOG(surfaceCbInThread_ != nullptr, MSERR_UNKNOWN, "new surface thread failed");
4519     }
4520     MEDIA_LOGI("ScreenCapBufferConsumerListener::StartBufferThread End.");
4521     return MSERR_OK;
4522 }
4523 
ProcessVideoBufferCallBack()4524 void ScreenCapBufferConsumerListener::ProcessVideoBufferCallBack()
4525 {
4526     std::lock_guard<std::mutex> lock(mutex_);
4527     CHECK_AND_RETURN_LOG(screenCaptureCb_ != nullptr, "no consumer, will drop video frame");
4528     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " OnBufferAvailable end.", FAKE_POINTER(this));
4529     screenCaptureCb_->OnVideoBufferAvailable(true);
4530 }
4531 
AcquireVideoBuffer(sptr<OHOS::SurfaceBuffer> & surfaceBuffer,int32_t & fence,int64_t & timestamp,OHOS::Rect & damage)4532 int32_t ScreenCapBufferConsumerListener::AcquireVideoBuffer(sptr<OHOS::SurfaceBuffer> &surfaceBuffer, int32_t &fence,
4533     int64_t &timestamp, OHOS::Rect &damage)
4534 {
4535     MediaTrace trace("ScreenCaptureServer::AcquireVideoBuffer", HITRACE_LEVEL_DEBUG);
4536     using namespace std::chrono_literals;
4537     std::unique_lock<std::mutex> lock(bufferMutex_);
4538     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " AcquireVideoBuffer start, fence:%{public}d, "
4539         "timestamp:%{public}" PRId64, FAKE_POINTER(this), fence, timestamp);
4540     if (!bufferCond_.wait_for(
4541         lock, std::chrono::milliseconds(OPERATION_TIMEOUT_IN_MS), [this] { return !availBuffers_.empty(); })) {
4542         return MSERR_UNKNOWN;
4543     }
4544     surfaceBuffer = availBuffers_.front()->buffer;
4545     fence = availBuffers_.front()->flushFence;
4546     timestamp = availBuffers_.front()->timeStamp;
4547     damage = availBuffers_.front()->damageRect;
4548     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " AcquireVideoBuffer end.", FAKE_POINTER(this));
4549     return MSERR_OK;
4550 }
4551 
StopBufferThread()4552 void ScreenCapBufferConsumerListener::StopBufferThread()
4553 {
4554     std::lock_guard<std::mutex> lock(bufferAvailableWorkerMtx_);
4555     messageQueueSCB_.push({SCBufferMessageType::EXIT, ""});
4556     MEDIA_LOGI("StopBufferThread: 0x%{public}06" PRIXPTR " EXIT.", FAKE_POINTER(this));
4557     bufferAvailableWorkerCv_.notify_one();
4558 }
4559 
~ScreenCapBufferConsumerListener()4560 ScreenCapBufferConsumerListener::~ScreenCapBufferConsumerListener()
4561 {
4562     MEDIA_LOGD("ScreenCapBufferConsumerListener: 0x%{public}06" PRIXPTR " Destroy.", FAKE_POINTER(this));
4563     StopBufferThread();
4564     if (surfaceCbInThread_ && surfaceCbInThread_->joinable()) {
4565         surfaceCbInThread_->join();
4566         delete surfaceCbInThread_;
4567         surfaceCbInThread_ = nullptr;
4568     }
4569     std::unique_lock<std::mutex> lock(bufferMutex_);
4570     ReleaseBuffer();
4571 }
4572 
ReleaseBuffer()4573 int32_t ScreenCapBufferConsumerListener::ReleaseBuffer()
4574 {
4575     while (!availBuffers_.empty()) {
4576         if (consumer_ != nullptr) {
4577             int32_t releaseBufferRet = consumer_->ReleaseBuffer(availBuffers_.front()->buffer, -1);  // -1 not wait
4578             if (releaseBufferRet != GSERROR_OK) {
4579                 MEDIA_LOGE("ScreenCapBufferConsumerListener: 0x%{public}06" PRIXPTR " ReleaseBuffer "
4580                     "Fail Code %{public}d", FAKE_POINTER(this), releaseBufferRet);
4581             }
4582         }
4583         availBuffers_.pop();
4584     }
4585     return MSERR_OK;
4586 }
4587 
ReleaseVideoBuffer()4588 int32_t ScreenCapBufferConsumerListener::ReleaseVideoBuffer()
4589 {
4590     MediaTrace trace("ScreenCaptureServer::ReleaseVideoBuffer", HITRACE_LEVEL_DEBUG);
4591     std::unique_lock<std::mutex> lock(bufferMutex_);
4592     MEDIA_LOGD("ScreenCapBufferConsumerListener: 0x%{public}06" PRIXPTR " ReleaseVideoBuffer start.",
4593         FAKE_POINTER(this));
4594     CHECK_AND_RETURN_RET_LOG(!availBuffers_.empty(), MSERR_OK, "buffer queue is empty, no video frame to release");
4595 
4596     if (consumer_ != nullptr) {
4597         int32_t releaseBufferRet = consumer_->ReleaseBuffer(availBuffers_.front()->buffer, -1); // -1 not wait
4598         if (releaseBufferRet != GSERROR_OK) {
4599             MEDIA_LOGE("ScreenCapBufferConsumerListener: 0x%{public}06" PRIXPTR " ReleaseVideoBuffer "
4600                 "Fail Code %{public}d", FAKE_POINTER(this), releaseBufferRet);
4601         }
4602     }
4603     availBuffers_.pop();
4604     MEDIA_LOGD("ScreenCapBufferConsumerListener: 0x%{public}06" PRIXPTR " ReleaseVideoBuffer end.", FAKE_POINTER(this));
4605     return MSERR_OK;
4606 }
4607 
Release()4608 int32_t ScreenCapBufferConsumerListener::Release()
4609 {
4610     std::unique_lock<std::mutex> lock(bufferMutex_);
4611     MEDIA_LOGI("ScreenCapBufferConsumerListener Release");
4612     return ReleaseBuffer();
4613 }
4614 
SetAudioSource(std::shared_ptr<AudioDataSource> audioSource)4615 void ScreenRendererAudioStateChangeCallback::SetAudioSource(std::shared_ptr<AudioDataSource> audioSource)
4616 {
4617     audioSource_ = audioSource;
4618 }
4619 
SetAppName(std::string appName)4620 void ScreenRendererAudioStateChangeCallback::SetAppName(std::string appName)
4621 {
4622     appName_ = appName;
4623 }
4624 
OnRendererStateChange(const std::vector<std::shared_ptr<AudioRendererChangeInfo>> & audioRendererChangeInfos)4625 void ScreenRendererAudioStateChangeCallback::OnRendererStateChange(
4626     const std::vector<std::shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos)
4627 {
4628     MEDIA_LOGD("ScreenRendererAudioStateChangeCallback IN");
4629     CHECK_AND_RETURN(audioSource_ != nullptr);
4630     auto screenCaptureServer = audioSource_->GetScreenCaptureServer();
4631     CHECK_AND_RETURN(screenCaptureServer != nullptr);
4632     CHECK_AND_RETURN(screenCaptureServer->GetSCServerCaptureState() != AVScreenCaptureState::STOPPED);
4633     audioSource_->SpeakerStateUpdate(audioRendererChangeInfos);
4634 #ifdef SUPPORT_CALL
4635     audioSource_->TelCallAudioStateUpdate(audioRendererChangeInfos);
4636 #endif
4637     std::string region = Global::I18n::LocaleConfig::GetSystemRegion();
4638     if (GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"]
4639             .compare(appName_) == 0 && region == "CN") {
4640         audioSource_->VoIPStateUpdate(audioRendererChangeInfos);
4641     }
4642 }
4643 
SpeakerStateUpdate(const std::vector<std::shared_ptr<AudioRendererChangeInfo>> & audioRendererChangeInfos)4644 void AudioDataSource::SpeakerStateUpdate(
4645     const std::vector<std::shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos)
4646 {
4647     (void)audioRendererChangeInfos;
4648     std::vector<std::shared_ptr<AudioRendererChangeInfo>> allAudioRendererChangeInfos;
4649     AudioStreamManager::GetInstance()->GetCurrentRendererChangeInfos(allAudioRendererChangeInfos);
4650     uint32_t changeInfoSize = allAudioRendererChangeInfos.size();
4651     if (changeInfoSize == 0) {
4652         return;
4653     }
4654     bool speakerAlive = HasSpeakerStream(allAudioRendererChangeInfos);
4655     if (speakerAlive != speakerAliveStatus_) {
4656         speakerAliveStatus_ = speakerAlive;
4657         CHECK_AND_RETURN(screenCaptureServer_ != nullptr);
4658         screenCaptureServer_->OnSpeakerAliveStatusChanged(speakerAlive);
4659         if (speakerAlive) {
4660             MEDIA_LOGI("HEADSET Change to Speaker.");
4661         } else {
4662             MEDIA_LOGI("Speaker Change to HEADSET.");
4663         }
4664     }
4665 }
4666 
4667 #ifdef SUPPORT_CALL
TelCallAudioStateUpdate(const std::vector<std::shared_ptr<AudioRendererChangeInfo>> & audioRendererChangeInfos)4668 void AudioDataSource::TelCallAudioStateUpdate(
4669     const std::vector<std::shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos)
4670 {
4671     CHECK_AND_RETURN(screenCaptureServer_ != nullptr);
4672     (void)audioRendererChangeInfos;
4673     std::vector<std::shared_ptr<AudioRendererChangeInfo>> allAudioRendererChangeInfos;
4674     AudioStreamManager::GetInstance()->GetCurrentRendererChangeInfos(allAudioRendererChangeInfos);
4675     for (const std::shared_ptr<AudioRendererChangeInfo> &changeInfo: allAudioRendererChangeInfos) {
4676         if (!changeInfo) {
4677             continue;
4678         }
4679         MEDIA_LOGD("Client pid : %{public}d, State : %{public}d, usage : %{public}d",
4680             changeInfo->clientPid, static_cast<int32_t>(changeInfo->rendererState),
4681             static_cast<int32_t>(changeInfo->rendererInfo.streamUsage));
4682         if (changeInfo->rendererInfo.streamUsage ==
4683             AudioStandard::StreamUsage::STREAM_USAGE_VOICE_MODEM_COMMUNICATION &&
4684             (changeInfo->rendererState == RendererState::RENDERER_RUNNING ||
4685             changeInfo->rendererState == RendererState::RENDERER_PREPARED)) {
4686             screenCaptureServer_->TelCallAudioStateUpdated(true);
4687             return;
4688         }
4689     }
4690     screenCaptureServer_->TelCallAudioStateUpdated(false);
4691 }
4692 #endif
4693 
HasSpeakerStream(const std::vector<std::shared_ptr<AudioRendererChangeInfo>> & audioRendererChangeInfos)4694 bool AudioDataSource::HasSpeakerStream(
4695     const std::vector<std::shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos)
4696 {
4697     uint32_t changeInfoIndex = 0;
4698     uint32_t headSetCount = 0;
4699     bool hasSpeakerStream = true;
4700     for (const std::shared_ptr<AudioRendererChangeInfo> &changeInfo: audioRendererChangeInfos) {
4701         if (!changeInfo) {
4702             continue;
4703         }
4704         MEDIA_LOGD("ChangeInfo Id: %{public}d, Client pid : %{public}d, State : %{public}d, DeviceType : %{public}d",
4705             changeInfoIndex, changeInfo->clientPid, static_cast<int32_t>(changeInfo->rendererState),
4706             static_cast<int32_t>(changeInfo->outputDeviceInfo.deviceType_));
4707         if (changeInfo->outputDeviceInfo.deviceType_ == DEVICE_TYPE_WIRED_HEADSET ||
4708             changeInfo->outputDeviceInfo.deviceType_ == DEVICE_TYPE_WIRED_HEADPHONES ||
4709             changeInfo->outputDeviceInfo.deviceType_ == DEVICE_TYPE_BLUETOOTH_SCO ||
4710             changeInfo->outputDeviceInfo.deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP ||
4711             changeInfo->outputDeviceInfo.deviceType_ == DEVICE_TYPE_USB_HEADSET ||
4712             changeInfo->outputDeviceInfo.deviceType_ == DEVICE_TYPE_USB_ARM_HEADSET) {
4713             headSetCount++;
4714         }
4715         changeInfoIndex++;
4716     }
4717     if (headSetCount == changeInfoIndex) { // only if all streams in headset
4718         hasSpeakerStream = false;
4719     }
4720     return hasSpeakerStream;
4721 }
4722 
VoIPStateUpdate(const std::vector<std::shared_ptr<AudioRendererChangeInfo>> & audioRendererChangeInfos)4723 void AudioDataSource::VoIPStateUpdate(
4724     const std::vector<std::shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos)
4725 {
4726     std::lock_guard<std::mutex> lock(voipStatusChangeMutex_);
4727     (void)audioRendererChangeInfos;
4728     std::vector<std::shared_ptr<AudioRendererChangeInfo>> allAudioRendererChangeInfos;
4729     AudioStreamManager::GetInstance()->GetCurrentRendererChangeInfos(allAudioRendererChangeInfos);
4730     bool isInVoIPCall = HasVoIPStream(allAudioRendererChangeInfos);
4731     if (isInVoIPCall_.load() == isInVoIPCall) {
4732         return;
4733     }
4734     isInVoIPCall_.store(isInVoIPCall);
4735     CHECK_AND_RETURN(screenCaptureServer_ != nullptr);
4736     screenCaptureServer_->OnVoIPStatusChanged(isInVoIPCall);
4737 }
4738 
HasVoIPStream(const std::vector<std::shared_ptr<AudioRendererChangeInfo>> & audioRendererChangeInfos)4739 bool AudioDataSource::HasVoIPStream(
4740     const std::vector<std::shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos)
4741 {
4742     for (const std::shared_ptr<AudioRendererChangeInfo> &changeInfo: audioRendererChangeInfos) {
4743         if (!changeInfo) {
4744             continue;
4745         }
4746         MEDIA_LOGD("Client pid : %{public}d, State : %{public}d, usage : %{public}d",
4747             changeInfo->clientPid, static_cast<int32_t>(changeInfo->rendererState),
4748             static_cast<int32_t>(changeInfo->rendererInfo.streamUsage));
4749         if (changeInfo->rendererState == RendererState::RENDERER_RUNNING &&
4750 		    (changeInfo->rendererInfo.streamUsage == AudioStandard::StreamUsage::STREAM_USAGE_VOICE_COMMUNICATION ||
4751 		    changeInfo->rendererInfo.streamUsage == AudioStandard::StreamUsage::STREAM_USAGE_VIDEO_COMMUNICATION)) {
4752             return true;
4753         }
4754     }
4755     return false;
4756 }
4757 
SetAppPid(int32_t appid)4758 void AudioDataSource::SetAppPid(int32_t appid)
4759 {
4760     appPid_ = appid;
4761 }
4762 
GetAppPid()4763 int32_t AudioDataSource::GetAppPid()
4764 {
4765     return appPid_ ;
4766 }
4767 
GetIsInVoIPCall()4768 bool AudioDataSource::GetIsInVoIPCall()
4769 {
4770     return isInVoIPCall_.load();
4771 }
4772 
IsInWaitMicSyncState()4773 bool AudioDataSource::IsInWaitMicSyncState()
4774 {
4775     return isInWaitMicSyncState_;
4776 }
4777 
GetSpeakerAliveStatus()4778 bool AudioDataSource::GetSpeakerAliveStatus()
4779 {
4780     return speakerAliveStatus_;
4781 }
4782 
SetAppName(std::string appName)4783 void AudioDataSource::SetAppName(std::string appName)
4784 {
4785     appName_ = appName;
4786 }
4787 
RegisterAudioRendererEventListener(const int32_t clientPid,const std::shared_ptr<AudioRendererStateChangeCallback> & callback)4788 int32_t AudioDataSource::RegisterAudioRendererEventListener(const int32_t clientPid,
4789     const std::shared_ptr<AudioRendererStateChangeCallback> &callback)
4790 {
4791     CHECK_AND_RETURN_RET_LOG(callback != nullptr, MSERR_INVALID_VAL, "audio callback is null");
4792     int32_t ret = AudioStreamManager::GetInstance()->RegisterAudioRendererEventListener(clientPid, callback);
4793     std::vector<std::shared_ptr<AudioRendererChangeInfo>> audioRendererChangeInfos;
4794     SpeakerStateUpdate(audioRendererChangeInfos);
4795     std::string region = Global::I18n::LocaleConfig::GetSystemRegion();
4796     if (GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"]
4797             .compare(appName_) == 0 && region == "CN") {
4798         VoIPStateUpdate(audioRendererChangeInfos);
4799     }
4800     return ret;
4801 }
4802 
UnregisterAudioRendererEventListener(const int32_t clientPid)4803 int32_t AudioDataSource::UnregisterAudioRendererEventListener(const int32_t clientPid)
4804 {
4805     MEDIA_LOGI("client id: %{public}d", clientPid);
4806     return AudioStreamManager::GetInstance()->UnregisterAudioRendererEventListener(clientPid);
4807 }
4808 
SetVideoFirstFramePts(int64_t firstFramePts)4809 void AudioDataSource::SetVideoFirstFramePts(int64_t firstFramePts)
4810 {
4811     firstVideoFramePts_.store(firstFramePts);
4812     struct timespec timestamp = {0, 0};
4813     clock_gettime(CLOCK_MONOTONIC, &timestamp);
4814     int64_t curTime = timestamp.tv_sec * SEC_TO_NS + timestamp.tv_nsec;
4815     MEDIA_LOGI("SetVideoFirstFramePts video to ScreenCapture timeWindow: %{public}" PRId64
4816         " firstVideoFramePts: %{public}" PRId64, curTime - firstFramePts, firstFramePts);
4817 }
4818 
SetAudioFirstFramePts(int64_t firstFramePts)4819 void AudioDataSource::SetAudioFirstFramePts(int64_t firstFramePts)
4820 {
4821     if (firstAudioFramePts_.load() == -1) {
4822         firstAudioFramePts_.store(firstFramePts);
4823         MEDIA_LOGI("firstAudioFramePts_: %{public}" PRId64, firstFramePts);
4824     }
4825 }
4826 
GetScreenCaptureServer()4827 ScreenCaptureServer* AudioDataSource::GetScreenCaptureServer()
4828 {
4829     return screenCaptureServer_;
4830 }
4831 
WriteInnerAudio(std::shared_ptr<AVBuffer> & buffer,uint32_t length,std::shared_ptr<AudioBuffer> & innerAudioBuffer)4832 AudioDataSourceReadAtActionState AudioDataSource::WriteInnerAudio(std::shared_ptr<AVBuffer> &buffer,
4833     uint32_t length, std::shared_ptr<AudioBuffer> &innerAudioBuffer)
4834 {
4835     std::shared_ptr<AVMemory> &bufferMem = buffer->memory_;
4836     if (buffer->memory_ == nullptr || innerAudioBuffer == nullptr) {
4837         MEDIA_LOGE("buffer->memory_ or innerAudioBuffer nullptr");
4838         return AudioDataSourceReadAtActionState::RETRY_SKIP;
4839     }
4840     std::shared_ptr<AudioBuffer> tmp = nullptr;
4841     SetAudioFirstFramePts(innerAudioBuffer->timestamp); // update firstAudioFramePts in case re-sync
4842     return MixModeBufferWrite(innerAudioBuffer, tmp, bufferMem);
4843 }
4844 
WriteMicAudio(std::shared_ptr<AVBuffer> & buffer,uint32_t length,std::shared_ptr<AudioBuffer> & micAudioBuffer)4845 AudioDataSourceReadAtActionState AudioDataSource::WriteMicAudio(std::shared_ptr<AVBuffer> &buffer, uint32_t length,
4846     std::shared_ptr<AudioBuffer> &micAudioBuffer)
4847 {
4848     std::shared_ptr<AVMemory> &bufferMem = buffer->memory_;
4849     if (buffer->memory_ == nullptr || micAudioBuffer == nullptr) {
4850         MEDIA_LOGE("buffer->memory_ or innerAudioBuffer nullptr");
4851         return AudioDataSourceReadAtActionState::RETRY_SKIP;
4852     }
4853     std::shared_ptr<AudioBuffer> tmp = nullptr;
4854     SetAudioFirstFramePts(micAudioBuffer->timestamp);
4855     return MixModeBufferWrite(tmp, micAudioBuffer, bufferMem);
4856 }
4857 
WriteMixAudio(std::shared_ptr<AVBuffer> & buffer,uint32_t length,std::shared_ptr<AudioBuffer> & innerAudioBuffer,std::shared_ptr<AudioBuffer> & micAudioBuffer)4858 AudioDataSourceReadAtActionState AudioDataSource::WriteMixAudio(std::shared_ptr<AVBuffer> &buffer, uint32_t length,
4859     std::shared_ptr<AudioBuffer> &innerAudioBuffer, std::shared_ptr<AudioBuffer> &micAudioBuffer)
4860 {
4861     std::shared_ptr<AVMemory> &bufferMem = buffer->memory_;
4862     if (buffer->memory_ == nullptr || innerAudioBuffer == nullptr || micAudioBuffer == nullptr) {
4863         MEDIA_LOGE("buffer->memory_ or innerAudioBuffer nullptr");
4864         return AudioDataSourceReadAtActionState::RETRY_SKIP;
4865     }
4866     SetAudioFirstFramePts(std::min(micAudioBuffer->timestamp, innerAudioBuffer->timestamp));
4867     return MixModeBufferWrite(innerAudioBuffer, micAudioBuffer, bufferMem);
4868 }
4869 
HandleMicBeforeInnerSync(std::shared_ptr<AVBuffer> & buffer,uint32_t length,std::shared_ptr<AudioBuffer> & innerAudioBuffer,std::shared_ptr<AudioBuffer> & micAudioBuffer)4870 AudioDataSourceReadAtActionState AudioDataSource::HandleMicBeforeInnerSync(std::shared_ptr<AVBuffer> &buffer,
4871     uint32_t length, std::shared_ptr<AudioBuffer> &innerAudioBuffer, std::shared_ptr<AudioBuffer> &micAudioBuffer)
4872 {
4873     int64_t timeDiff = innerAudioBuffer->timestamp - micAudioBuffer->timestamp;
4874     if (timeDiff > MAX_MIC_BEFORE_INNER_TIME_IN_NS) {
4875         // Drop mic data Before 40ms
4876         if (micAudioBuffer && screenCaptureServer_->GetMicAudioCapture()) {
4877             screenCaptureServer_->GetMicAudioCapture()->DropBufferUntil(innerAudioBuffer->timestamp);
4878             MEDIA_LOGI("mic before inner timeDiff: %{public}" PRId64 " DropBufferUntil inner time: %{public}" PRId64
4879                 " lastAudioPts: %{public}" PRId64, timeDiff,
4880                 innerAudioBuffer->timestamp, lastWriteAudioFramePts_.load());
4881         }
4882         return AudioDataSourceReadAtActionState::SKIP_WITHOUT_LOG;
4883     }
4884     return WriteMicAudio(buffer, length, micAudioBuffer);
4885 }
4886 
InnerMicAudioSync(std::shared_ptr<AVBuffer> & buffer,uint32_t length,std::shared_ptr<AudioBuffer> & innerAudioBuffer,std::shared_ptr<AudioBuffer> & micAudioBuffer)4887 AudioDataSourceReadAtActionState AudioDataSource::InnerMicAudioSync(std::shared_ptr<AVBuffer> &buffer,
4888     uint32_t length, std::shared_ptr<AudioBuffer> &innerAudioBuffer, std::shared_ptr<AudioBuffer> &micAudioBuffer)
4889 {
4890     int64_t timeWindow = micAudioBuffer->timestamp - innerAudioBuffer->timestamp;
4891     if (timeWindow <= NEG_AUDIO_INTERVAL_IN_NS) { // mic before inner
4892         return HandleMicBeforeInnerSync(buffer, length, innerAudioBuffer, micAudioBuffer);
4893     }
4894     if (timeWindow > NEG_AUDIO_INTERVAL_IN_NS && timeWindow < AUDIO_INTERVAL_IN_NS) { // Write mix
4895         return WriteMixAudio(buffer, length, innerAudioBuffer, micAudioBuffer);
4896     } else { // Write Inner data Before mic timeWindow >= AUDIO_INTERVAL_IN_NS
4897         return WriteInnerAudio(buffer, length, innerAudioBuffer);
4898     }
4899 }
4900 
VideoAudioSyncMixMode(std::shared_ptr<AVBuffer> & buffer,uint32_t length,int64_t timeWindow,std::shared_ptr<AudioBuffer> & innerAudioBuffer,std::shared_ptr<AudioBuffer> & micAudioBuffer)4901 AudioDataSourceReadAtActionState AudioDataSource::VideoAudioSyncMixMode(std::shared_ptr<AVBuffer> &buffer,
4902     uint32_t length, int64_t timeWindow, std::shared_ptr<AudioBuffer> &innerAudioBuffer,
4903     std::shared_ptr<AudioBuffer> &micAudioBuffer)
4904 {
4905     if (timeWindow <= NEG_AUDIO_INTERVAL_IN_NS) { // video before audio add 0
4906         if (micAudioBuffer && screenCaptureServer_->GetMicAudioCapture() && mixModeAddAudioMicFrame_) {
4907             MEDIA_LOGI("mic AddBufferFrom timeWindow: %{public}" PRId64, timeWindow);
4908             screenCaptureServer_->GetMicAudioCapture()->AddBufferFrom(-1 * timeWindow,
4909                 length, firstVideoFramePts_.load());
4910         }
4911         if (innerAudioBuffer && screenCaptureServer_->GetInnerAudioCapture() && !mixModeAddAudioMicFrame_) {
4912             MEDIA_LOGI("inner AddBufferFrom timeWindow: %{public}" PRId64, timeWindow);
4913             screenCaptureServer_->GetInnerAudioCapture()->AddBufferFrom(-1 * timeWindow,
4914                 length, firstVideoFramePts_.load());
4915         }
4916         SetAudioFirstFramePts(firstVideoFramePts_.load());
4917         return AudioDataSourceReadAtActionState::SKIP_WITHOUT_LOG;
4918     }
4919     if (timeWindow >= AUDIO_INTERVAL_IN_NS) { // video after audio drop audio
4920         if (micAudioBuffer && screenCaptureServer_->GetMicAudioCapture()) {
4921             screenCaptureServer_->GetMicAudioCapture()->DropBufferUntil(firstVideoFramePts_.load());
4922         }
4923         if (innerAudioBuffer && screenCaptureServer_->GetInnerAudioCapture()) {
4924             screenCaptureServer_->GetInnerAudioCapture()->DropBufferUntil(firstVideoFramePts_.load());
4925         }
4926         SetAudioFirstFramePts(firstVideoFramePts_.load());
4927         return AudioDataSourceReadAtActionState::SKIP_WITHOUT_LOG;
4928     } else { // (timeWindow > NEG_AUDIO_INTERVAL_IN_NS && timeWindow < AUDIO_INTERVAL_IN_NS)
4929         return ReadWriteAudioBufferMixCore(buffer, length, innerAudioBuffer, micAudioBuffer);
4930     }
4931 }
4932 
GetFirstAudioTime(std::shared_ptr<AudioBuffer> & innerAudioBuffer,std::shared_ptr<AudioBuffer> & micAudioBuffer)4933 int64_t AudioDataSource::GetFirstAudioTime(std::shared_ptr<AudioBuffer> &innerAudioBuffer,
4934     std::shared_ptr<AudioBuffer> &micAudioBuffer)
4935 {
4936     if (innerAudioBuffer && micAudioBuffer) {
4937         if (innerAudioBuffer->timestamp > micAudioBuffer->timestamp) {
4938             mixModeAddAudioMicFrame_ = true;
4939             return micAudioBuffer->timestamp;
4940         }
4941         mixModeAddAudioMicFrame_ = false;
4942         return innerAudioBuffer->timestamp;
4943     }
4944     if (innerAudioBuffer && !micAudioBuffer) {
4945         mixModeAddAudioMicFrame_ = false;
4946         return innerAudioBuffer->timestamp;
4947     }
4948     if (!innerAudioBuffer && micAudioBuffer) {
4949         mixModeAddAudioMicFrame_ = true;
4950         return micAudioBuffer->timestamp;
4951     }
4952     return -1;
4953 }
4954 
ReadWriteAudioBufferMixCore(std::shared_ptr<AVBuffer> & buffer,uint32_t length,std::shared_ptr<AudioBuffer> & innerAudioBuffer,std::shared_ptr<AudioBuffer> & micAudioBuffer)4955 AudioDataSourceReadAtActionState AudioDataSource::ReadWriteAudioBufferMixCore(std::shared_ptr<AVBuffer> &buffer,
4956     uint32_t length, std::shared_ptr<AudioBuffer> &innerAudioBuffer, std::shared_ptr<AudioBuffer> &micAudioBuffer)
4957 {
4958     if (innerAudioBuffer == nullptr && micAudioBuffer) {
4959         return WriteMicAudio(buffer, length, micAudioBuffer);
4960     }
4961     if (innerAudioBuffer && micAudioBuffer == nullptr) {
4962         if (screenCaptureServer_->IsStopAcquireAudioBufferFlag() && isInWaitMicSyncState_) {
4963             return WriteInnerAudio(buffer, length, innerAudioBuffer);
4964         }
4965         if (screenCaptureServer_->IsMicrophoneSwitchTurnOn()) {
4966             int64_t currentAudioTime = 0;
4967             screenCaptureServer_->GetInnerAudioCapture()->GetCurrentAudioTime(currentAudioTime);
4968             if (currentAudioTime - innerAudioBuffer->timestamp < MAX_INNER_AUDIO_TIMEOUT_IN_NS) { // 2s
4969                 isInWaitMicSyncState_ = true;
4970                 return AudioDataSourceReadAtActionState::RETRY_IN_INTERVAL;
4971             } else {
4972                 MEDIA_LOGD("isInWaitMicSyncState close");
4973                 isInWaitMicSyncState_ = false;
4974             }
4975         }
4976         return WriteInnerAudio(buffer, length, innerAudioBuffer);
4977     }
4978     if (innerAudioBuffer && micAudioBuffer) {
4979         return InnerMicAudioSync(buffer, length, innerAudioBuffer, micAudioBuffer);
4980     }
4981     if (innerAudioBuffer == nullptr && micAudioBuffer == nullptr) {
4982         MEDIA_LOGD("acquire none inner mic buffer");
4983         return AudioDataSourceReadAtActionState::SKIP_WITHOUT_LOG;
4984     }
4985     return AudioDataSourceReadAtActionState::OK;
4986 }
4987 
ReadWriteAudioBufferMix(std::shared_ptr<AVBuffer> & buffer,uint32_t length,std::shared_ptr<AudioBuffer> & innerAudioBuffer,std::shared_ptr<AudioBuffer> & micAudioBuffer)4988 AudioDataSourceReadAtActionState AudioDataSource::ReadWriteAudioBufferMix(std::shared_ptr<AVBuffer> &buffer,
4989     uint32_t length, std::shared_ptr<AudioBuffer> &innerAudioBuffer, std::shared_ptr<AudioBuffer> &micAudioBuffer)
4990 {
4991     if (screenCaptureServer_->IsMicrophoneCaptureRunning()) {
4992         if (screenCaptureServer_->GetMicAudioCapture()->AcquireAudioBuffer(micAudioBuffer) != MSERR_OK) {
4993             MEDIA_LOGD("micAudioCapture AcquireAudioBuffer failed");
4994         }
4995     }
4996     if (screenCaptureServer_->IsInnerCaptureRunning()) {
4997         if (screenCaptureServer_->GetInnerAudioCapture()->AcquireAudioBuffer(innerAudioBuffer) != MSERR_OK) {
4998             MEDIA_LOGD("innerAudioCapture AcquireAudioBuffer failed");
4999         }
5000     }
5001     CHECK_AND_RETURN_RET_NOLOG(innerAudioBuffer || micAudioBuffer, AudioDataSourceReadAtActionState::SKIP_WITHOUT_LOG);
5002     if (screenCaptureServer_->IsSCRecorderFileWithVideo() && firstAudioFramePts_ == -1) { // video audio sync
5003         int64_t audioTime = GetFirstAudioTime(innerAudioBuffer, micAudioBuffer);
5004         CHECK_AND_RETURN_RET_NOLOG(audioTime != -1, AudioDataSourceReadAtActionState::SKIP_WITHOUT_LOG);
5005         struct timespec timestamp = {0, 0};
5006         clock_gettime(CLOCK_MONOTONIC, &timestamp);
5007         int64_t curTime = timestamp.tv_sec * SEC_TO_NS + timestamp.tv_nsec;
5008         MEDIA_LOGI("ReadWriteAudioBufferMix audio to ScreenCapture timeWindow: %{public}" PRId64
5009             " firstAudioFramePts: %{public}" PRId64, curTime - audioTime, audioTime);
5010         int64_t timeWindow = firstVideoFramePts_.load() - audioTime;
5011         MEDIA_LOGI("ReadWriteAudioBufferMix video to audio timeWindow: %{public}" PRId64, timeWindow);
5012         return VideoAudioSyncMixMode(buffer, length, timeWindow, innerAudioBuffer, micAudioBuffer);
5013     }
5014     return ReadWriteAudioBufferMixCore(buffer, length, innerAudioBuffer, micAudioBuffer);
5015 }
5016 
ReadAtMixMode(std::shared_ptr<AVBuffer> buffer,uint32_t length)5017 AudioDataSourceReadAtActionState AudioDataSource::ReadAtMixMode(std::shared_ptr<AVBuffer> buffer, uint32_t length)
5018 {
5019     std::shared_ptr<AudioBuffer> innerAudioBuffer = nullptr;
5020     std::shared_ptr<AudioBuffer> micAudioBuffer = nullptr;
5021     AudioDataSourceReadAtActionState ret = ReadWriteAudioBufferMix(buffer, length, innerAudioBuffer, micAudioBuffer);
5022     MEDIA_LOGD("AudioDataSource ReadAtMixMode ret: %{public}d", static_cast<int32_t>(ret));
5023     return ret;
5024 }
5025 
ReadAtMicMode(std::shared_ptr<AVBuffer> buffer,uint32_t length)5026 AudioDataSourceReadAtActionState AudioDataSource::ReadAtMicMode(std::shared_ptr<AVBuffer> buffer, uint32_t length)
5027 {
5028     if (screenCaptureServer_->GetMicAudioCapture() == nullptr) {
5029         return AudioDataSourceReadAtActionState::INVALID;
5030     }
5031     if (screenCaptureServer_->IsMicrophoneCaptureRunning()) {
5032         std::shared_ptr<AudioBuffer> micAudioBuffer = nullptr;
5033         if (screenCaptureServer_->GetMicAudioCapture()->AcquireAudioBuffer(micAudioBuffer) != MSERR_OK) {
5034             MEDIA_LOGD("micAudioCapture AcquireAudioBuffer failed");
5035             return AudioDataSourceReadAtActionState::RETRY_SKIP;
5036         }
5037         MEDIA_LOGD("AcquireAudioBuffer mic success");
5038         std::shared_ptr<AVMemory> &bufferMem = buffer->memory_;
5039         if (buffer->memory_ == nullptr || micAudioBuffer == nullptr) {
5040             MEDIA_LOGE("buffer->memory_ or micAudioBuffer nullptr");
5041             return AudioDataSourceReadAtActionState::RETRY_SKIP;
5042         }
5043         MEDIA_LOGD("ABuffer write mic cur:%{public}" PRId64 " last: %{public}" PRId64,
5044             micAudioBuffer->timestamp, lastWriteAudioFramePts_.load());
5045         lastWriteAudioFramePts_.store(micAudioBuffer->timestamp);
5046         lastWriteType_ = AVScreenCaptureMixBufferType::MIC;
5047         bufferMem->Write(reinterpret_cast<uint8_t*>(micAudioBuffer->buffer), micAudioBuffer->length, 0);
5048         CHECK_AND_RETURN_RET_LOG(screenCaptureServer_->ReleaseMicAudioBuffer() == MSERR_OK,
5049             AudioDataSourceReadAtActionState::RETRY_SKIP, "micAudioCapture ReleaseAudioBuffer failed");
5050         micAudioBuffer = nullptr;
5051         return AudioDataSourceReadAtActionState::OK;
5052     }
5053     return AudioDataSourceReadAtActionState::RETRY_SKIP;
5054 }
5055 
VideoAudioSyncInnerMode(std::shared_ptr<AVBuffer> & buffer,uint32_t length,int64_t timeWindow,std::shared_ptr<AudioBuffer> & innerAudioBuffer)5056 AudioDataSourceReadAtActionState AudioDataSource::VideoAudioSyncInnerMode(std::shared_ptr<AVBuffer> &buffer,
5057     uint32_t length, int64_t timeWindow, std::shared_ptr<AudioBuffer> &innerAudioBuffer)
5058 {
5059     if (timeWindow <= NEG_AUDIO_INTERVAL_IN_NS) { // video before audio add 0
5060         if (innerAudioBuffer && screenCaptureServer_->GetInnerAudioCapture()) {
5061             screenCaptureServer_->GetInnerAudioCapture()->AddBufferFrom(-1 * timeWindow,
5062                 length, firstVideoFramePts_.load());
5063         }
5064         SetAudioFirstFramePts(firstVideoFramePts_.load());
5065         return AudioDataSourceReadAtActionState::SKIP_WITHOUT_LOG;
5066     }
5067     if (timeWindow >= AUDIO_INTERVAL_IN_NS) { // video after audio drop audio
5068         if (screenCaptureServer_->GetInnerAudioCapture()) {
5069             screenCaptureServer_->GetInnerAudioCapture()->DropBufferUntil(firstVideoFramePts_.load());
5070         }
5071         SetAudioFirstFramePts(firstVideoFramePts_.load());
5072         return AudioDataSourceReadAtActionState::SKIP_WITHOUT_LOG;
5073     } else { // (timeWindow > NEG_AUDIO_INTERVAL_IN_NS && timeWindow < AUDIO_INTERVAL_IN_NS)
5074         std::shared_ptr<AVMemory> &bufferMem = buffer->memory_;
5075         if (buffer->memory_ == nullptr || innerAudioBuffer == nullptr) {
5076             MEDIA_LOGE("buffer->memory_ or innerAudioBuffer nullptr");
5077             return AudioDataSourceReadAtActionState::SKIP_WITHOUT_LOG;
5078         }
5079         MEDIA_LOGD("ABuffer write inner cur:%{public}" PRId64 " last: %{public}" PRId64,
5080             innerAudioBuffer->timestamp, lastWriteAudioFramePts_.load());
5081         lastWriteAudioFramePts_.store(innerAudioBuffer->timestamp);
5082         bufferMem->Write(reinterpret_cast<uint8_t*>(innerAudioBuffer->buffer), innerAudioBuffer->length, 0);
5083         SetAudioFirstFramePts(innerAudioBuffer->timestamp);
5084         lastWriteType_ = AVScreenCaptureMixBufferType::INNER;
5085         screenCaptureServer_->ReleaseInnerAudioBuffer();
5086         innerAudioBuffer = nullptr;
5087         return AudioDataSourceReadAtActionState::OK;
5088     }
5089 }
5090 
ReadAtInnerMode(std::shared_ptr<AVBuffer> buffer,uint32_t length)5091 AudioDataSourceReadAtActionState AudioDataSource::ReadAtInnerMode(std::shared_ptr<AVBuffer> buffer, uint32_t length)
5092 {
5093     if (screenCaptureServer_->GetInnerAudioCapture() == nullptr) {
5094         return AudioDataSourceReadAtActionState::INVALID;
5095     }
5096     if (screenCaptureServer_->IsInnerCaptureRunning()) {
5097         std::shared_ptr<AudioBuffer> innerAudioBuffer = nullptr;
5098         if (screenCaptureServer_->GetInnerAudioCapture()->AcquireAudioBuffer(innerAudioBuffer) != MSERR_OK) {
5099             MEDIA_LOGD("innerAudioCapture AcquireAudioBuffer failed");
5100             return AudioDataSourceReadAtActionState::RETRY_SKIP;
5101         }
5102         MEDIA_LOGD("AcquireAudioBuffer inner success");
5103         if (screenCaptureServer_->IsSCRecorderFileWithVideo() && firstAudioFramePts_ == -1 && innerAudioBuffer) {
5104             int64_t timeWindow = firstVideoFramePts_.load() - innerAudioBuffer->timestamp;
5105             return VideoAudioSyncInnerMode(buffer, length, timeWindow, innerAudioBuffer);
5106         }
5107         std::shared_ptr<AVMemory> &bufferMem = buffer->memory_;
5108         if (buffer->memory_ == nullptr || innerAudioBuffer == nullptr) {
5109             MEDIA_LOGE("buffer->memory_ or innerAudioBuffer nullptr");
5110             return AudioDataSourceReadAtActionState::RETRY_SKIP;
5111         }
5112         MEDIA_LOGD("ABuffer write inner cur:%{public}" PRId64 " last: %{public}" PRId64,
5113             innerAudioBuffer->timestamp, lastWriteAudioFramePts_.load());
5114         lastWriteAudioFramePts_.store(innerAudioBuffer->timestamp);
5115         lastWriteType_ = AVScreenCaptureMixBufferType::INNER;
5116         bufferMem->Write(reinterpret_cast<uint8_t*>(innerAudioBuffer->buffer), innerAudioBuffer->length, 0);
5117         CHECK_AND_RETURN_RET_LOG(screenCaptureServer_->ReleaseInnerAudioBuffer() == MSERR_OK,
5118             AudioDataSourceReadAtActionState::RETRY_SKIP, "innerAudioCapture ReleaseAudioBuffer failed");
5119         innerAudioBuffer = nullptr;
5120         return AudioDataSourceReadAtActionState::OK;
5121     }
5122     return AudioDataSourceReadAtActionState::RETRY_SKIP;
5123 }
5124 
ReadAt(std::shared_ptr<AVBuffer> buffer,uint32_t length)5125 AudioDataSourceReadAtActionState AudioDataSource::ReadAt(std::shared_ptr<AVBuffer> buffer, uint32_t length)
5126 {
5127     MEDIA_LOGD("AudioDataSource ReadAt start");
5128     CHECK_AND_RETURN_RET_LOG(screenCaptureServer_ != nullptr, AudioDataSourceReadAtActionState::RETRY_SKIP,
5129         "ReadAt screenCaptureServer null");
5130     if (screenCaptureServer_->GetSCServerCaptureState() != AVScreenCaptureState::STARTED) {
5131         return AudioDataSourceReadAtActionState::SKIP_WITHOUT_LOG;
5132     }
5133     if (screenCaptureServer_->IsSCRecorderFileWithVideo() && firstVideoFramePts_.load() == -1) { // video frame not come
5134         return AudioDataSourceReadAtActionState::SKIP_WITHOUT_LOG;
5135     }
5136     if (type_ == AVScreenCaptureMixMode::MIX_MODE) {
5137         return ReadAtMixMode(buffer, length);
5138     }
5139     if (type_ == AVScreenCaptureMixMode::INNER_MODE) {
5140         return ReadAtInnerMode(buffer, length);
5141     }
5142     if (type_ == AVScreenCaptureMixMode::MIC_MODE) {
5143         return ReadAtMicMode(buffer, length);
5144     }
5145     return AudioDataSourceReadAtActionState::RETRY_SKIP;
5146 }
5147 
HandlePastMicBuffer(std::shared_ptr<AudioBuffer> & micAudioBuffer)5148 void AudioDataSource::HandlePastMicBuffer(std::shared_ptr<AudioBuffer> &micAudioBuffer)
5149 {
5150     if (micAudioBuffer->timestamp < lastWriteAudioFramePts_.load() &&
5151         micAudioBuffer->timestamp < lastMicAudioFramePts_.load() + AUDIO_MIC_TOO_CLOSE_LIMIT_IN_NS &&
5152         lastWriteType_ == AVScreenCaptureMixBufferType::INNER) { // drop past mic data when switch,keep when mic stable
5153         screenCaptureServer_->ReleaseMicAudioBuffer();
5154         MEDIA_LOGD("ABuffer drop mix mic error cur:%{public}" PRId64 " last: %{public}" PRId64,
5155             micAudioBuffer->timestamp, lastWriteAudioFramePts_.load());
5156         micAudioBuffer = nullptr;
5157     }
5158 }
5159 
HandleSwitchToSpeakerOptimise(std::shared_ptr<AudioBuffer> & innerAudioBuffer,std::shared_ptr<AudioBuffer> & micAudioBuffer)5160 void AudioDataSource::HandleSwitchToSpeakerOptimise(std::shared_ptr<AudioBuffer> &innerAudioBuffer,
5161     std::shared_ptr<AudioBuffer> &micAudioBuffer)
5162 {
5163     if (speakerAliveStatus_ && screenCaptureServer_ && screenCaptureServer_->IsMicrophoneSwitchTurnOn() &&
5164         !isInVoIPCall_ && micAudioBuffer->buffer &&
5165         lastWriteType_ == AVScreenCaptureMixBufferType::MIX && screenCaptureServer_->GetInnerAudioCapture()) {
5166         if (stableStopInnerSwitchCount_ > INNER_SWITCH_MIC_REQUIRE_COUNT) { // optimise inner while use speaker
5167             MEDIA_LOGI("ABuffer stop mix inner optimise cur:%{public}" PRId64 " last: %{public}" PRId64,
5168                 innerAudioBuffer->timestamp, lastWriteAudioFramePts_.load());
5169             innerAudioBuffer = nullptr;
5170             screenCaptureServer_->StopInnerAudioCapture();
5171             stableStopInnerSwitchCount_ = 0;
5172         } else {
5173             stableStopInnerSwitchCount_++;
5174         }
5175     }
5176 }
5177 
HandleBufferTimeStamp(std::shared_ptr<AudioBuffer> & innerAudioBuffer,std::shared_ptr<AudioBuffer> & micAudioBuffer)5178 void AudioDataSource::HandleBufferTimeStamp(std::shared_ptr<AudioBuffer> &innerAudioBuffer,
5179     std::shared_ptr<AudioBuffer> &micAudioBuffer)
5180 {
5181     if (micAudioBuffer) {
5182         HandlePastMicBuffer(micAudioBuffer);
5183     }
5184     if (innerAudioBuffer) {
5185         if (innerAudioBuffer->timestamp < lastWriteAudioFramePts_.load()
5186             && !GetIsInVoIPCall() && speakerAliveStatus_) {
5187             screenCaptureServer_->ReleaseInnerAudioBuffer(); // drop past inner data while use speaker
5188             MEDIA_LOGD("ABuffer drop mix inner error cur:%{public}" PRId64 " last: %{public}" PRId64,
5189                 innerAudioBuffer->timestamp, lastWriteAudioFramePts_.load());
5190             innerAudioBuffer = nullptr;
5191         }
5192     }
5193     if (innerAudioBuffer && micAudioBuffer) {
5194         HandleSwitchToSpeakerOptimise(innerAudioBuffer, micAudioBuffer);
5195     }
5196 }
5197 
MixModeBufferWrite(std::shared_ptr<AudioBuffer> & innerAudioBuffer,std::shared_ptr<AudioBuffer> & micAudioBuffer,std::shared_ptr<AVMemory> & bufferMem)5198 AudioDataSourceReadAtActionState AudioDataSource::MixModeBufferWrite(std::shared_ptr<AudioBuffer> &innerAudioBuffer,
5199     std::shared_ptr<AudioBuffer> &micAudioBuffer, std::shared_ptr<AVMemory> &bufferMem)
5200 {
5201     HandleBufferTimeStamp(innerAudioBuffer, micAudioBuffer);
5202     if (innerAudioBuffer && innerAudioBuffer->buffer && micAudioBuffer && micAudioBuffer->buffer) {
5203         char* mixData = new char[innerAudioBuffer->length];
5204         char* srcData[2] = {nullptr};
5205         srcData[0] = reinterpret_cast<char*>(innerAudioBuffer->buffer);
5206         srcData[1] = reinterpret_cast<char*>(micAudioBuffer->buffer);
5207         int channels = 2;
5208         MixAudio(srcData, mixData, channels, innerAudioBuffer->length);
5209         MEDIA_LOGD("ABuffer write mix mix cur:%{public}" PRId64 " mic:%{public}" PRId64 " last: %{public}" PRId64,
5210             innerAudioBuffer->timestamp, micAudioBuffer->timestamp, lastWriteAudioFramePts_.load());
5211         lastWriteAudioFramePts_.store(innerAudioBuffer->timestamp);
5212         lastMicAudioFramePts_.store(micAudioBuffer->timestamp);
5213         lastWriteType_ = AVScreenCaptureMixBufferType::MIX;
5214         bufferMem->Write(reinterpret_cast<uint8_t*>(mixData), innerAudioBuffer->length, 0);
5215         delete[] mixData;
5216     } else if (innerAudioBuffer && innerAudioBuffer->buffer) {
5217         MEDIA_LOGD("ABuffer write mix inner cur:%{public}" PRId64 " last: %{public}" PRId64,
5218                 innerAudioBuffer->timestamp, lastWriteAudioFramePts_.load());
5219         lastWriteAudioFramePts_.store(innerAudioBuffer->timestamp);
5220         lastWriteType_ = AVScreenCaptureMixBufferType::INNER;
5221         stableStopInnerSwitchCount_ = 0;
5222         bufferMem->Write(reinterpret_cast<uint8_t*>(innerAudioBuffer->buffer), innerAudioBuffer->length, 0);
5223     } else if (micAudioBuffer && micAudioBuffer->buffer) {
5224         MEDIA_LOGD("ABuffer write mix mic cur:%{public}" PRId64 " last: %{public}" PRId64,
5225             micAudioBuffer->timestamp, lastWriteAudioFramePts_.load());
5226         lastWriteAudioFramePts_.store(micAudioBuffer->timestamp);
5227         lastMicAudioFramePts_.store(micAudioBuffer->timestamp);
5228         lastWriteType_ = AVScreenCaptureMixBufferType::MIC;
5229         stableStopInnerSwitchCount_ = 0;
5230         bufferMem->Write(reinterpret_cast<uint8_t*>(micAudioBuffer->buffer), micAudioBuffer->length, 0);
5231     } else {
5232         MEDIA_LOGE("without buffer write");
5233         return AudioDataSourceReadAtActionState::RETRY_SKIP;
5234     }
5235     if (innerAudioBuffer) {
5236         screenCaptureServer_->ReleaseInnerAudioBuffer();
5237         innerAudioBuffer = nullptr;
5238     }
5239     if (micAudioBuffer) {
5240         screenCaptureServer_->ReleaseMicAudioBuffer();
5241         micAudioBuffer = nullptr;
5242     }
5243     return AudioDataSourceReadAtActionState::OK;
5244 }
5245 
GetSize(int64_t & size)5246 int32_t AudioDataSource::GetSize(int64_t &size)
5247 {
5248     size_t bufferLen = 0;
5249     int32_t ret = MSERR_UNKNOWN;
5250     if (screenCaptureServer_->GetInnerAudioCapture()) {
5251         ret = screenCaptureServer_->GetInnerAudioCaptureBufferSize(bufferLen);
5252         MEDIA_LOGD("AudioDataSource::GetSize : %{public}zu", bufferLen);
5253         if (ret == MSERR_OK) {
5254             size = static_cast<int64_t>(bufferLen);
5255             return ret;
5256         }
5257     }
5258     if (screenCaptureServer_->GetMicAudioCapture()) {
5259         ret = screenCaptureServer_->GetMicAudioCaptureBufferSize(bufferLen);
5260         MEDIA_LOGD("AudioDataSource::GetSize : %{public}zu", bufferLen);
5261         if (ret == MSERR_OK) {
5262             size = static_cast<int64_t>(bufferLen);
5263             return ret;
5264         }
5265     }
5266     return ret;
5267 }
5268 
MixAudio(char ** srcData,char * mixData,int channels,int bufferSize)5269 void AudioDataSource::MixAudio(char** srcData, char* mixData, int channels, int bufferSize)
5270 {
5271     MEDIA_LOGD("AudioDataSource MixAudio");
5272     int const max = 32767;
5273     int const min = -32768;
5274     double const splitNum = 32;
5275     int const doubleChannels = 2;
5276     double coefficient = 1;
5277     int totalNum = 0;
5278     if (channels == 0) {
5279         return;
5280     }
5281     for (totalNum = 0; totalNum < bufferSize / channels; totalNum++) {
5282         int temp = 0;
5283         for (int channelNum = 0; channelNum < channels; channelNum++) {
5284             temp += *reinterpret_cast<short*>(srcData[channelNum] + totalNum * channels);
5285         }
5286         int32_t output = static_cast<int32_t>(temp * coefficient);
5287         if (output > max) {
5288             coefficient = static_cast<double>(max) / static_cast<double>(output);
5289             output = max;
5290         }
5291         if (output < min) {
5292             coefficient = static_cast<double>(min) / static_cast<double>(output);
5293             output = min;
5294         }
5295         if (coefficient < 1) {
5296             coefficient += (static_cast<double>(1) - coefficient) / splitNum;
5297         }
5298         *reinterpret_cast<short*>(mixData + totalNum * doubleChannels) = static_cast<short>(output);
5299     }
5300 }
5301 } // namespace Media
5302 } // namespace OHOS
5303