• 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 #ifdef PC_STANDARD
46 #include <parameters.h>
47 #endif
48 
49 using OHOS::Rosen::DMError;
50 
51 namespace {
52 const std::string DUMP_PATH = "/data/media/screen_capture.bin";
53 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_SCREENCAPTURE, "ScreenCaptureServer"};
54 }
55 
56 namespace OHOS {
57 namespace Media {
58 
59 static const std::string MP4 = "mp4";
60 static const std::string M4A = "m4a";
61 
62 static const std::string USER_CHOICE_ALLOW = "true";
63 static const std::string USER_CHOICE_DENY = "false";
64 static const std::string BUTTON_NAME_MIC = "mic";
65 static const std::string BUTTON_NAME_STOP = "stop";
66 static const std::string ICON_PATH_CAPSULE_STOP = "/etc/screencapture/capsule_stop.svg";
67 static const std::string ICON_PATH_NOTIFICATION = "/etc/screencapture/notification.png";
68 static const std::string ICON_PATH_MIC = "/etc/screencapture/mic.svg";
69 static const std::string ICON_PATH_MIC_OFF = "/etc/screencapture/mic_off.svg";
70 static const std::string ICON_PATH_STOP = "/etc/screencapture/stop.png";
71 static const std::string BACK_GROUND_COLOR = "#E84026";
72 #ifdef PC_STANDARD
73 static const std::string SELECT_ABILITY_NAME = "SelectWindowAbility";
74 static const int32_t SELECT_WINDOW_MISSION_ID_NUM_MAX = 2;
75 #endif
76 static const int32_t SVG_HEIGHT = 80;
77 static const int32_t SVG_WIDTH = 80;
78 static const int32_t MICROPHONE_OFF = 0;
79 static const int32_t MICROPHONE_STATE_COUNT = 2;
80 #ifdef SUPPORT_SCREEN_CAPTURE_WINDOW_NOTIFICATION
81     static const int32_t NOTIFICATION_MAX_TRY_NUM = 3;
82 #endif
83 static const int32_t MOUSE_DEVICE = 5;
84 #ifdef SUPPORT_CALL
85 static const int32_t STOPPED_BY_CALL_API_VERSION_ISOLATION = 16;
86 #endif
87 
88 static const auto NOTIFICATION_SUBSCRIBER = NotificationSubscriber();
89 static constexpr int32_t AUDIO_CHANGE_TIME = 200000; // 200 ms
90 
91 std::map<int32_t, std::weak_ptr<ScreenCaptureServer>> ScreenCaptureServer::serverMap_{};
92 std::map<int32_t, std::pair<int32_t, int32_t>> ScreenCaptureServer::saUidAppUidMap_{};
93 const int32_t ScreenCaptureServer::maxSessionId_ = 16;
94 const int32_t ScreenCaptureServer::maxAppLimit_ = 4;
95 UniqueIDGenerator ScreenCaptureServer::gIdGenerator_(ScreenCaptureServer::maxSessionId_);
96 std::list<int32_t> ScreenCaptureServer::startedSessionIDList_;
97 const int32_t ScreenCaptureServer::maxSessionPerUid_ = 4;
98 const int32_t ScreenCaptureServer::maxSCServerDataTypePerUid_ = 2;
99 std::atomic<int32_t> ScreenCaptureServer::systemScreenRecorderPid_ = -1;
100 
101 std::shared_mutex ScreenCaptureServer::mutexServerMapRWGlobal_;
102 std::shared_mutex ScreenCaptureServer::mutexListRWGlobal_;
103 std::shared_mutex ScreenCaptureServer::mutexSaAppInfoMapGlobal_;
104 
OnConnected()105 void NotificationSubscriber::OnConnected()
106 {
107     MEDIA_LOGI("NotificationSubscriber OnConnected");
108 }
109 
OnDisconnected()110 void NotificationSubscriber::OnDisconnected()
111 {
112     MEDIA_LOGI("NotificationSubscriber OnDisconnected");
113 }
114 
OnResponse(int32_t notificationId,OHOS::sptr<OHOS::Notification::NotificationButtonOption> buttonOption)115 void NotificationSubscriber::OnResponse(int32_t notificationId,
116                                         OHOS::sptr<OHOS::Notification::NotificationButtonOption> buttonOption)
117 {
118     CHECK_AND_RETURN(buttonOption != nullptr);
119     MEDIA_LOGI("NotificationSubscriber OnResponse notificationId : %{public}d, ButtonName : %{public}s ",
120         notificationId, (buttonOption->GetButtonName()).c_str());
121 
122     std::shared_ptr<ScreenCaptureServer> server =
123         ScreenCaptureServer::GetScreenCaptureServerByIdWithLock(notificationId);
124     if (server == nullptr) {
125         MEDIA_LOGW("OnResponse ScreenCaptureServer not exist, notificationId : %{public}d, ButtonName : %{public}s ",
126             notificationId, (buttonOption->GetButtonName()).c_str());
127         return;
128     }
129     if (BUTTON_NAME_STOP.compare(buttonOption->GetButtonName()) == 0) {
130         server->StopScreenCaptureByEvent(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_STOPPED_BY_USER);
131         return;
132     }
133     if (BUTTON_NAME_MIC.compare(buttonOption->GetButtonName()) == 0) {
134         server->UpdateMicrophoneEnabled();
135         return;
136     }
137 }
138 
OnDied()139 void NotificationSubscriber::OnDied()
140 {
141     MEDIA_LOGI("NotificationSubscriber OnDied");
142 }
143 
MouseChangeListener(std::weak_ptr<ScreenCaptureServer> screenCaptureServer)144 MouseChangeListener::MouseChangeListener(std::weak_ptr<ScreenCaptureServer> screenCaptureServer)
145 {
146     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
147     screenCaptureServer_ = screenCaptureServer;
148 }
149 
GetDeviceInfo(int32_t deviceId,std::shared_ptr<InputDeviceInfo> deviceInfo)150 int32_t MouseChangeListener::GetDeviceInfo(int32_t deviceId, std::shared_ptr<InputDeviceInfo> deviceInfo)
151 {
152     MEDIA_LOGI("Get device info by deviceId %{public}d", deviceId);
153     CHECK_AND_RETURN_RET_LOG(deviceInfo != nullptr, MSERR_INVALID_VAL, "Input deviceInfo is nullptr");
154 
155     std::function<void(std::shared_ptr<MMI::InputDevice>)> callback =
156         [&deviceInfo](std::shared_ptr<MMI::InputDevice> device) {
157             if (device) {
158                 deviceInfo->SetType(device->GetType());
159                 deviceInfo->SetName(device->GetName());
160             }
161         };
162     int32_t ret = MMI::InputManager::GetInstance()->GetDevice(
163         deviceId, [&callback](std::shared_ptr<MMI::InputDevice> device) {callback(device);});
164     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret,
165         "Calling method GetDevice failed. Device ID: %{public}d", deviceId);
166     return ret;
167 }
168 
OnDeviceAdded(int32_t deviceId,const std::string & type)169 void MouseChangeListener::OnDeviceAdded(int32_t deviceId, const std::string &type)
170 {
171     MEDIA_LOGI("OnDeviceAdded start.");
172     std::shared_ptr<InputDeviceInfo> deviceInfo = std::make_shared<InputDeviceInfo>();
173     int32_t ret = GetDeviceInfo(deviceId, deviceInfo);
174     CHECK_AND_RETURN_LOG(ret == MSERR_OK, "Get deviceInfo(%{public}d) failed", deviceId);
175     MEDIA_LOGI("Add device type: %{public}d, name:%{public}s", deviceInfo->GetType(), deviceInfo->GetName().c_str());
176 
177     if (deviceInfo->GetType() == MOUSE_DEVICE) {
178         MEDIA_LOGI("Add device is mouse, type (%{public}d)", deviceInfo->GetType());
179         auto scrServer = screenCaptureServer_.lock();
180         CHECK_AND_RETURN_LOG(scrServer != nullptr, "screenCaptureServer is nullptr");
181         ret = scrServer->ShowCursorInner();
182         CHECK_AND_RETURN_LOG(ret == MSERR_OK, "OnDeviceAdded, showCursorInner failed");
183     }
184     MEDIA_LOGI("OnDeviceAdded end.");
185 }
186 
OnDeviceRemoved(int32_t deviceId,const std::string & type)187 void MouseChangeListener::OnDeviceRemoved(int32_t deviceId, const std::string &type)
188 {
189     MEDIA_LOGI("OnDeviceRemoved start, deviceId: %{public}d, type:%{public}s", deviceId, type.c_str());
190     auto scrServer = screenCaptureServer_.lock();
191     CHECK_AND_RETURN_LOG(scrServer != nullptr, "screenCaptureServer is nullptr");
192     int32_t ret = scrServer->ShowCursorInner();
193     CHECK_AND_RETURN_LOG(ret == MSERR_OK, "OnDeviceRemoved ShowCursorInner failed");
194     MEDIA_LOGI("OnDeviceRemoved end.");
195 }
196 
PrivateWindowListenerInScreenCapture(std::weak_ptr<ScreenCaptureServer> screenCaptureServer)197 PrivateWindowListenerInScreenCapture::PrivateWindowListenerInScreenCapture(
198     std::weak_ptr<ScreenCaptureServer> screenCaptureServer)
199 {
200     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
201     screenCaptureServer_ = screenCaptureServer;
202 }
203 
OnPrivateWindow(bool hasPrivate)204 void PrivateWindowListenerInScreenCapture::OnPrivateWindow(bool hasPrivate)
205 {
206     MEDIA_LOGI("PrivateWindowListenerInScreenCapture hasPrivateWindow: %{public}u", hasPrivate);
207     auto scrServer = screenCaptureServer_.lock();
208     if (scrServer) {
209         MEDIA_LOGI("Callback OnDMPrivateWindowChange hasPrivateWindow: %{public}u", hasPrivate);
210         scrServer->OnDMPrivateWindowChange(hasPrivate);
211     }
212 }
213 
OnConnect(Rosen::ScreenId screenId)214 void ScreenConnectListenerForSC::OnConnect(Rosen::ScreenId screenId)
215 {
216     MEDIA_LOGI("ScreenConnectListenerForSC OnConnect screenId: %{public}" PRIu64, screenId);
217 }
218 
OnDisconnect(Rosen::ScreenId screenId)219 void ScreenConnectListenerForSC::OnDisconnect(Rosen::ScreenId screenId)
220 {
221     MEDIA_LOGI("ScreenConnectListenerForSC OnDisconnect screenId: %{public}" PRIu64, screenId);
222     auto callbackPtr = screenCaptureServer_.lock();
223     if (callbackPtr && screenId == screenId_) {
224         MEDIA_LOGI("ScreenConnectListenerForSC OnDisconnect NotifyStateChange: %{public}" PRIu64, screenId_);
225     }
226 }
227 
OnChange(Rosen::ScreenId screenId)228 void ScreenConnectListenerForSC::OnChange(Rosen::ScreenId screenId)
229 {
230     MEDIA_LOGI("ScreenConnectListenerForSC OnChange screenId: %{public}" PRIu64, screenId);
231 }
232 
AddScreenCaptureServerMap(int32_t sessionId,std::weak_ptr<ScreenCaptureServer> server)233 void ScreenCaptureServer::AddScreenCaptureServerMap(int32_t sessionId,
234     std::weak_ptr<ScreenCaptureServer> server)
235 {
236     std::unique_lock<std::shared_mutex> lock(ScreenCaptureServer::mutexServerMapRWGlobal_);
237     ScreenCaptureServer::serverMap_.insert(std::make_pair(sessionId, server));
238     MEDIA_LOGI("AddScreenCaptureServerMap end, serverMap size: %{public}d.",
239         static_cast<uint32_t>(ScreenCaptureServer::serverMap_.size()));
240 }
241 
RemoveScreenCaptureServerMap(int32_t sessionId)242 void ScreenCaptureServer::RemoveScreenCaptureServerMap(int32_t sessionId)
243 {
244     std::unique_lock<std::shared_mutex> lock(ScreenCaptureServer::mutexServerMapRWGlobal_);
245     ScreenCaptureServer::serverMap_.erase(sessionId);
246     int32_t returnId = ScreenCaptureServer::gIdGenerator_.ReturnID(sessionId);
247     if (returnId == -1) {
248         MEDIA_LOGI("RemoveScreenCaptureServerMap returnId: %{public}d is invalid", returnId);
249     }
250     MEDIA_LOGI("RemoveScreenCaptureServerMap end. sessionId: %{public}d, serverMap size: %{public}d.",
251         sessionId, static_cast<uint32_t>(ScreenCaptureServer::serverMap_.size()));
252 }
253 
CheckScreenCaptureSessionIdLimit(int32_t curAppUid)254 bool ScreenCaptureServer::CheckScreenCaptureSessionIdLimit(int32_t curAppUid)
255 {
256     int32_t countForUid = 0;
257     MEDIA_LOGI("CheckScreenCaptureSessionIdLimit start. curAppUid: %{public}d.", curAppUid);
258     {
259         std::unique_lock<std::shared_mutex> lock(ScreenCaptureServer::mutexServerMapRWGlobal_);
260         for (auto iter = ScreenCaptureServer::serverMap_.begin(); iter != ScreenCaptureServer::serverMap_.end();
261             iter++) {
262                 auto iterPtr = (iter->second).lock();
263                 if (iterPtr != nullptr) {
264                     if (curAppUid == iterPtr->GetAppUid()) {
265                         countForUid++;
266                     }
267                     CHECK_AND_RETURN_RET_LOG(countForUid <= ScreenCaptureServer::maxSessionPerUid_, false,
268                         "Create failed, uid(%{public}d) has created too many ScreenCaptureServer instances", curAppUid);
269                 }
270             }
271     }
272     MEDIA_LOGI("CheckScreenCaptureSessionIdLimit end.");
273     return true;
274 }
275 
CheckSCServerSpecifiedDataTypeNum(int32_t curAppUid,DataType dataType)276 bool ScreenCaptureServer::CheckSCServerSpecifiedDataTypeNum(int32_t curAppUid, DataType dataType)
277 {
278     int32_t countForUidDataType = 0;
279     MEDIA_LOGI("CheckSCServerSpecifiedDataTypeNum start. curAppUid: %{public}d, dataType: %{public}d.",
280         curAppUid, dataType);
281     {
282         std::unique_lock<std::shared_mutex> lock(ScreenCaptureServer::mutexServerMapRWGlobal_);
283         for (auto iter = ScreenCaptureServer::serverMap_.begin(); iter != ScreenCaptureServer::serverMap_.end();
284             iter++) {
285                 auto iterPtr = (iter->second).lock();
286                 if (iterPtr != nullptr) {
287                     if (curAppUid == iterPtr->GetAppUid() && dataType == iterPtr->GetSCServerDataType()) {
288                         countForUidDataType++;
289                     }
290                     CHECK_AND_RETURN_RET_LOG(countForUidDataType <= ScreenCaptureServer::maxSCServerDataTypePerUid_,
291                         false, "CheckSCServerSpecifiedDataTypeNum failed,"
292                         "uid(%{public}d) has created too many instances of dataType(%{public}d)", curAppUid, dataType);
293                 }
294             }
295     }
296     MEDIA_LOGI("CheckSCServerSpecifiedDataTypeNum end.");
297     return true;
298 }
299 
CountScreenCaptureAppNum(std::set<int32_t> & appSet)300 void ScreenCaptureServer::CountScreenCaptureAppNum(std::set<int32_t>& appSet)
301 {
302     std::unique_lock<std::shared_mutex> lock(ScreenCaptureServer::mutexServerMapRWGlobal_);
303     for (auto iter = ScreenCaptureServer::serverMap_.begin(); iter != ScreenCaptureServer::serverMap_.end(); iter++) {
304         auto iterPtr = iter->second.lock();
305         if (iterPtr != nullptr) {
306             appSet.insert(iterPtr->GetAppUid());
307         }
308     }
309 }
310 
CheckScreenCaptureAppLimit(int32_t curAppUid)311 bool ScreenCaptureServer::CheckScreenCaptureAppLimit(int32_t curAppUid)
312 {
313     std::set<int32_t> appSet;
314     CountScreenCaptureAppNum(appSet);
315     MEDIA_LOGI("appSet.size(): %{public}d", static_cast<int32_t>(appSet.size()));
316     if (static_cast<int32_t>(appSet.size()) > ScreenCaptureServer::maxAppLimit_) {
317         return false;
318     }
319     return true;
320 }
321 
GetScreenCaptureServerById(int32_t id)322 std::shared_ptr<ScreenCaptureServer> ScreenCaptureServer::GetScreenCaptureServerById(int32_t id)
323 {
324     auto iter = ScreenCaptureServer::serverMap_.find(id);
325     if (iter == ScreenCaptureServer::serverMap_.end()) {
326         return nullptr;
327     }
328     return (iter->second).lock();
329 }
330 
GetScreenCaptureServerByIdWithLock(int32_t id)331 std::shared_ptr<ScreenCaptureServer> ScreenCaptureServer::GetScreenCaptureServerByIdWithLock(int32_t id)
332 {
333     std::unique_lock<std::shared_mutex> lock(ScreenCaptureServer::mutexServerMapRWGlobal_);
334     return GetScreenCaptureServerById(id);
335 }
336 
GetStartedScreenCaptureServerPidList()337 std::list<int32_t> ScreenCaptureServer::GetStartedScreenCaptureServerPidList()
338 {
339     std::list<int32_t> startedScreenCapturePidList{};
340     std::unique_lock<std::shared_mutex> lock(ScreenCaptureServer::mutexServerMapRWGlobal_);
341     for (auto sessionId: ScreenCaptureServer::startedSessionIDList_) {
342         std::shared_ptr<ScreenCaptureServer> currentServer = GetScreenCaptureServerById(sessionId);
343         if (currentServer != nullptr) {
344             startedScreenCapturePidList.push_back(currentServer->GetAppPid() == 0 ? -1 : currentServer->GetAppPid());
345         }
346     }
347     return startedScreenCapturePidList;
348 }
349 
CountStartedScreenCaptureServerNumByPid(int32_t pid)350 int32_t ScreenCaptureServer::CountStartedScreenCaptureServerNumByPid(int32_t pid)
351 {
352     int32_t count = 0;
353     std::unique_lock<std::shared_mutex> lock(ScreenCaptureServer::mutexServerMapRWGlobal_);
354     for (auto sessionId: ScreenCaptureServer::startedSessionIDList_) {
355         std::shared_ptr<ScreenCaptureServer> currentServer = GetScreenCaptureServerById(sessionId);
356         if (currentServer != nullptr && currentServer->GetAppPid() == pid) {
357             count++;
358         }
359     }
360     return count;
361 }
362 
AddStartedSessionIdList(int32_t value)363 void ScreenCaptureServer::AddStartedSessionIdList(int32_t value)
364 {
365     std::unique_lock<std::shared_mutex> lock(ScreenCaptureServer::mutexListRWGlobal_);
366     ScreenCaptureServer::startedSessionIDList_.push_back(value);
367 }
368 
RemoveStartedSessionIdList(int32_t value)369 void ScreenCaptureServer::RemoveStartedSessionIdList(int32_t value)
370 {
371     std::unique_lock<std::shared_mutex> lock(ScreenCaptureServer::mutexListRWGlobal_);
372     ScreenCaptureServer::startedSessionIDList_.remove(value);
373 }
374 
GetAllStartedSessionIdList()375 std::list<int32_t> ScreenCaptureServer::GetAllStartedSessionIdList()
376 {
377     std::unique_lock<std::shared_mutex> lock(ScreenCaptureServer::mutexListRWGlobal_);
378     return GetStartedScreenCaptureServerPidList();
379 }
380 
CheckPidIsScreenRecorder(int32_t pid)381 bool ScreenCaptureServer::CheckPidIsScreenRecorder(int32_t pid)
382 {
383     MEDIA_LOGI("CheckPidIsScreenRecorder ScreenRecorder pid(%{public}d), input pid(%{public}d)",
384         (ScreenCaptureServer::systemScreenRecorderPid_).load(), pid);
385     return pid == (ScreenCaptureServer::systemScreenRecorderPid_).load();
386 }
387 
OnDMPrivateWindowChange(bool hasPrivate)388 void ScreenCaptureServer::OnDMPrivateWindowChange(bool hasPrivate)
389 {
390     MEDIA_LOGI("OnDMPrivateWindowChange hasPrivateWindow: %{public}u", hasPrivate);
391     NotifyStateChange(hasPrivate ?
392         AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_ENTER_PRIVATE_SCENE :
393         AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_EXIT_PRIVATE_SCENE);
394 }
395 
CanScreenCaptureInstanceBeCreate(int32_t appUid)396 bool ScreenCaptureServer::CanScreenCaptureInstanceBeCreate(int32_t appUid)
397 {
398     MEDIA_LOGI("CanScreenCaptureInstanceBeCreate start.");
399     CHECK_AND_RETURN_RET_LOG(ScreenCaptureServer::serverMap_.size() <= ScreenCaptureServer::maxSessionId_, false,
400         "ScreenCaptureInstanceCanBeCreate exceed ScreenCaptureServer instances limit.");
401     MEDIA_LOGI("curAppUid: %{public}d", appUid);
402     CHECK_AND_RETURN_RET_LOG(CheckScreenCaptureAppLimit(appUid), false,
403         "CurScreenCaptureAppNum reach limit, cannot create more app.");
404     return CheckScreenCaptureSessionIdLimit(appUid);
405 }
406 
CreateScreenCaptureNewInstance()407 std::shared_ptr<IScreenCaptureService> ScreenCaptureServer::CreateScreenCaptureNewInstance()
408 {
409     MEDIA_LOGI("CreateScreenCaptureNewInstance");
410     int32_t id = ScreenCaptureServer::gIdGenerator_.GetNewID();
411     CHECK_AND_RETURN_RET_LOG(id != -1, nullptr, "GetNewID failed.");
412     MEDIA_LOGI("CreateScreenCaptureNewInstance newId: %{public}d", id);
413     std::shared_ptr<ScreenCaptureServer> server = std::make_shared<ScreenCaptureServer>();
414     CHECK_AND_RETURN_RET_LOG(server != nullptr, nullptr, "Failed to create ScreenCaptureServer.");
415     server->SetSessionId(id);
416     AddScreenCaptureServerMap(id, server);
417     return std::static_pointer_cast<IScreenCaptureService>(server);
418 }
419 
IsSAServiceCalling()420 bool ScreenCaptureServer::IsSAServiceCalling()
421 {
422     MEDIA_LOGI("ScreenCaptureServer::IsSAServiceCalling START.");
423     const auto tokenId = IPCSkeleton::GetCallingTokenID();
424     const auto tokenTypeFlag = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId);
425     if (tokenTypeFlag == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE ||
426         tokenTypeFlag == Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL) {
427         MEDIA_LOGI("ScreenCaptureServer::IsSAServiceCalling true.");
428         return true;
429     }
430     return false;
431 }
432 
Create()433 std::shared_ptr<IScreenCaptureService> ScreenCaptureServer::Create()
434 {
435     for (auto sessionId: ScreenCaptureServer::startedSessionIDList_) {
436         MEDIA_LOGD("ScreenCaptureServer::Create sessionId: %{public}d", sessionId);
437     }
438     MEDIA_LOGI("ScreenCaptureServer Create start.");
439     return CreateScreenCaptureNewInstance();
440 }
441 
AddSaAppInfoMap(int32_t saUid,int32_t curAppUid)442 void ScreenCaptureServer::AddSaAppInfoMap(int32_t saUid, int32_t curAppUid)
443 {
444     std::unique_lock<std::shared_mutex> lock(ScreenCaptureServer::mutexSaAppInfoMapGlobal_);
445     if (ScreenCaptureServer::saUidAppUidMap_.find(saUid) == ScreenCaptureServer::saUidAppUidMap_.end()) {
446         ScreenCaptureServer::saUidAppUidMap_.insert({saUid, std::make_pair(curAppUid, 1)});
447         MEDIA_LOGI("AddSaAppInfoMap insert SUCCESS! mapSize: %{public}d",
448             static_cast<uint32_t>(ScreenCaptureServer::saUidAppUidMap_.size()));
449     } else {
450         ScreenCaptureServer::saUidAppUidMap_[saUid].second++;
451     }
452 }
453 
RemoveSaAppInfoMap(int32_t saUid)454 void ScreenCaptureServer::RemoveSaAppInfoMap(int32_t saUid)
455 {
456     CHECK_AND_RETURN(saUid != -1);
457     MEDIA_LOGI("RemoveSaAppInfoMap saUid: %{public}d is valid.", saUid);
458     std::unique_lock<std::shared_mutex> lock(ScreenCaptureServer::mutexSaAppInfoMapGlobal_);
459     if (ScreenCaptureServer::saUidAppUidMap_.find(saUid) != ScreenCaptureServer::saUidAppUidMap_.end() &&
460         ScreenCaptureServer::saUidAppUidMap_[saUid].second > 0) {
461         ScreenCaptureServer::saUidAppUidMap_[saUid].second--;
462         if (ScreenCaptureServer::saUidAppUidMap_[saUid].second == 0) {
463             ScreenCaptureServer::saUidAppUidMap_.erase(saUid);
464         }
465     }
466     MEDIA_LOGI("RemoveSaAppInfoMap END! mapSize: %{public}d",
467         static_cast<uint32_t>(ScreenCaptureServer::saUidAppUidMap_.size()));
468 }
469 
CheckSaUid(int32_t saUid,int32_t appUid)470 bool ScreenCaptureServer::CheckSaUid(int32_t saUid, int32_t appUid)
471 {
472     std::unique_lock<std::shared_mutex> lock(ScreenCaptureServer::mutexSaAppInfoMapGlobal_);
473     if (ScreenCaptureServer::saUidAppUidMap_.find(saUid) != ScreenCaptureServer::saUidAppUidMap_.end()) {
474         if (ScreenCaptureServer::saUidAppUidMap_[saUid].first != appUid ||
475             (ScreenCaptureServer::saUidAppUidMap_[saUid].first == appUid &&
476             ScreenCaptureServer::saUidAppUidMap_[saUid].second >= ScreenCaptureServer::maxSessionPerUid_)) {
477                 MEDIA_LOGI("saUid Invalid! saUid: %{public}d linked with appUid: %{public}d, curAppUid: %{public}d",
478                     saUid, ScreenCaptureServer::saUidAppUidMap_[saUid].first, appUid);
479                 return false;
480             }
481     }
482     return true;
483 }
484 
IsSaUidValid(int32_t saUid,int32_t appUid)485 bool ScreenCaptureServer::IsSaUidValid(int32_t saUid, int32_t appUid)
486 {
487     CHECK_AND_RETURN_RET_LOG(saUid >= 0 && appUid >= 0, false, "saUid or appUid is invalid.");
488     CHECK_AND_RETURN_RET_LOG(IsSAServiceCalling(), false, "fake SAServiceCalling!");
489     return CheckSaUid(saUid, appUid);
490 }
491 
SetAndCheckAppInfo(OHOS::AudioStandard::AppInfo & appInfo)492 int32_t ScreenCaptureServer::SetAndCheckAppInfo(OHOS::AudioStandard::AppInfo &appInfo)
493 {
494     std::lock_guard<std::mutex> lock(mutex_);
495     MEDIA_LOGI("ScreenCaptureServer::SetAndCheckAppInfo(appInfo)");
496     const int32_t saUid = IPCSkeleton::GetCallingUid();
497     if (saUid >= 0) {
498         SetSCServerSaUid(saUid);
499     }
500     if (!IsSaUidValid(saUid, appInfo.appUid)) {
501         MEDIA_LOGI("SetAndCheckAppInfo failed, saUid-appUid exists.");
502         SetSCServerSaUid(-1);
503         return MSERR_INVALID_OPERATION;
504     }
505 
506     appInfo_.appUid = appInfo.appUid;
507     appInfo_.appPid = appInfo.appPid;
508     appInfo_.appTokenId = appInfo.appTokenId;
509     appInfo_.appFullTokenId = appInfo.appFullTokenId;
510     appName_ = GetClientBundleName(appInfo_.appUid);
511     AddSaAppInfoMap(saUid, appInfo_.appUid);
512     MEDIA_LOGI("ScreenCaptureServer::SetAndCheckAppInfo end.");
513     return MSERR_OK;
514 }
515 
SetSCServerSaUid(int32_t saUid)516 void ScreenCaptureServer::SetSCServerSaUid(int32_t saUid)
517 {
518     saUid_ = saUid;
519 }
520 
GetSCServerSaUid()521 int32_t ScreenCaptureServer::GetSCServerSaUid()
522 {
523     return saUid_;
524 }
525 
SetAndCheckSaLimit(OHOS::AudioStandard::AppInfo & appInfo)526 int32_t ScreenCaptureServer::SetAndCheckSaLimit(OHOS::AudioStandard::AppInfo &appInfo)
527 {
528     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetAndCheckSaLimit START.", FAKE_POINTER(this));
529     int32_t ret = SetAndCheckAppInfo(appInfo);
530     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "SetAndCheckSaLimit failed, saUid exists.");
531     bool createFlag = CanScreenCaptureInstanceBeCreate(appInfo.appUid);
532     if (!createFlag) {
533         MEDIA_LOGI("SetAndCheckSaLimit failed, cannot create ScreenCapture Instance.");
534         SetSCServerSaUid(-1);
535         return MSERR_INVALID_OPERATION;
536     }
537     MEDIA_LOGI("SetAndCheckSaLimit SUCCESS! appUid: %{public}d, saUid: %{public}d",
538         appInfo.appUid, GetSCServerSaUid());
539     return MSERR_OK;
540 }
541 
SetAndCheckLimit()542 int32_t ScreenCaptureServer::SetAndCheckLimit()
543 {
544     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetAndCheckLimit START.", FAKE_POINTER(this));
545     bool createFlag = CanScreenCaptureInstanceBeCreate(IPCSkeleton::GetCallingUid());
546     CHECK_AND_RETURN_RET_LOG(createFlag, MSERR_INVALID_OPERATION,
547         "SetAndCheckLimit failed, cannot create ScreenCapture Instance.");
548     return MSERR_OK;
549 }
550 
GetRunningScreenCaptureInstancePid(std::list<int32_t> & pidList)551 int32_t ScreenCaptureServer::GetRunningScreenCaptureInstancePid(std::list<int32_t> &pidList)
552 {
553     MEDIA_LOGI("GetRunningScreenCaptureInstancePid in");
554     pidList = GetAllStartedSessionIdList();
555     return MSERR_OK;
556 }
557 
GetChoiceFromJson(Json::Value & root,const std::string & content,std::string key,std::string & value)558 void ScreenCaptureServer::GetChoiceFromJson(Json::Value &root,
559     const std::string &content, std::string key, std::string &value)
560 {
561     Json::Reader reader;
562     bool parsingSuccessful = reader.parse(content, root);
563     if (!parsingSuccessful || root.type() != Json::objectValue) {
564         MEDIA_LOGE("Error parsing the string");
565         return;
566     }
567     const Json::Value keyJson = root[key];
568     if (!keyJson.isNull() && keyJson.isString()) {
569         value = keyJson.asString();
570     }
571 }
572 
SetCaptureConfig(CaptureMode captureMode,int32_t missionId)573 void ScreenCaptureServer::SetCaptureConfig(CaptureMode captureMode, int32_t missionId)
574 {
575     captureConfig_.captureMode = captureMode;
576     if (missionId != -1) { // -1 无效值
577         captureConfig_.videoInfo.videoCapInfo.taskIDs.push_back(missionId);
578     } else {
579         captureConfig_.videoInfo.videoCapInfo.taskIDs = {};
580     }
581 }
582 
PrepareSelectWindow(Json::Value & root,std::shared_ptr<ScreenCaptureServer> & server)583 void ScreenCaptureServer::PrepareSelectWindow(Json::Value &root, std::shared_ptr<ScreenCaptureServer> &server)
584 {
585     if (root.type() != Json::objectValue) {
586         return;
587     }
588     const Json::Value displayIdJson = root["displayId"];
589     if (!displayIdJson.isNull() && displayIdJson.isInt() && displayIdJson.asInt64() >= 0) {
590         uint64_t displayId = static_cast<uint64_t>(displayIdJson.asInt64());
591         MEDIA_LOGI("Report Select DisplayId: %{public}" PRIu64, displayId);
592         server->SetDisplayId(displayId);
593         // 手机模式 missionId displayId 均为-1
594         server->SetCaptureConfig(CaptureMode::CAPTURE_SPECIFIED_SCREEN, -1);
595     }
596     const Json::Value missionIdJson = root["missionId"];
597     if (!missionIdJson.isNull() && missionIdJson.isInt() && missionIdJson.asInt() >= 0) {
598         int32_t missionId = missionIdJson.asInt();
599         MEDIA_LOGI("Report Select MissionId: %{public}d", missionId);
600         server->SetMissionId(missionId);
601         server->SetCaptureConfig(CaptureMode::CAPTURE_SPECIFIED_WINDOW, missionId);
602     }
603 }
604 
ReportAVScreenCaptureUserChoice(int32_t sessionId,const std::string & content)605 int32_t ScreenCaptureServer::ReportAVScreenCaptureUserChoice(int32_t sessionId, const std::string &content)
606 {
607     MEDIA_LOGI("ReportAVScreenCaptureUserChoice sessionId: %{public}d, content: %{public}s",
608         sessionId, content.c_str());
609     std::shared_ptr<ScreenCaptureServer> server = GetScreenCaptureServerByIdWithLock(sessionId);
610     CHECK_AND_RETURN_RET_LOG(server != nullptr, MSERR_UNKNOWN,
611         "ReportAVScreenCaptureUserChoice failed to get instance, sessionId: %{public}d", sessionId);
612     if (server->captureState_ == AVScreenCaptureState::POPUP_WINDOW) {
613         MEDIA_LOGI("ReportAVScreenCaptureUserChoice captureState is %{public}d", AVScreenCaptureState::POPUP_WINDOW);
614         Json::Value root;
615         std::string choice = "false";
616         GetChoiceFromJson(root, content, std::string("choice"), choice);
617         if (USER_CHOICE_ALLOW.compare(choice) == 0) {
618             PrepareSelectWindow(root, server);
619             int32_t ret = server->OnReceiveUserPrivacyAuthority(true);
620             CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret,
621                 "ReportAVScreenCaptureUserChoice user choice is true but start failed");
622             MEDIA_LOGI("ReportAVScreenCaptureUserChoice user choice is true and start success");
623             return MSERR_OK;
624         } else if (USER_CHOICE_DENY.compare(choice) == 0) {
625             return server->OnReceiveUserPrivacyAuthority(false);
626         } else {
627             MEDIA_LOGW("ReportAVScreenCaptureUserChoice user choice is not support");
628         }
629     }
630     return MSERR_UNKNOWN;
631 }
632 
GetAppPid()633 int32_t ScreenCaptureServer::GetAppPid()
634 {
635     return appInfo_.appPid;
636 }
637 
GetAppUid()638 int32_t ScreenCaptureServer::GetAppUid()
639 {
640     return appInfo_.appUid;
641 }
642 
GetSCServerDataType()643 DataType ScreenCaptureServer::GetSCServerDataType()
644 {
645     return captureConfig_.dataType;
646 }
647 
SetDisplayId(uint64_t displayId)648 void ScreenCaptureServer::SetDisplayId(uint64_t displayId)
649 {
650     captureConfig_.videoInfo.videoCapInfo.displayId = displayId;
651 }
652 
SetMissionId(uint64_t missionId)653 void ScreenCaptureServer::SetMissionId(uint64_t missionId)
654 {
655     missionIds_.emplace_back(missionId);
656 }
657 
GetSCServerCaptureState()658 AVScreenCaptureState ScreenCaptureServer::GetSCServerCaptureState()
659 {
660     return captureState_;
661 }
662 
SetMetaDataReport()663 void ScreenCaptureServer::SetMetaDataReport()
664 {
665     std::shared_ptr<Media::Meta> meta = std::make_shared<Media::Meta>();
666     meta->SetData(Tag::SCREEN_CAPTURE_ERR_CODE, statisticalEventInfo_.errCode);
667     meta->SetData(Tag::SCREEN_CAPTURE_ERR_MSG, statisticalEventInfo_.errMsg);
668     meta->SetData(Tag::SCREEN_CAPTURE_DURATION, statisticalEventInfo_.captureDuration);
669     meta->SetData(Tag::SCREEN_CAPTURE_AV_TYPE, avType_);
670     meta->SetData(Tag::SCREEN_CAPTURE_DATA_TYPE, dataMode_);
671     meta->SetData(Tag::SCREEN_CAPTURE_USER_AGREE, statisticalEventInfo_.userAgree);
672     meta->SetData(Tag::SCREEN_CAPTURE_REQURE_MIC, statisticalEventInfo_.requireMic);
673     meta->SetData(Tag::SCREEN_CAPTURE_ENABLE_MIC, statisticalEventInfo_.enableMic);
674     meta->SetData(Tag::SCREEN_CAPTURE_VIDEO_RESOLUTION, statisticalEventInfo_.videoResolution);
675     meta->SetData(Tag::SCREEN_CAPTURE_STOP_REASON, statisticalEventInfo_.stopReason);
676     meta->SetData(Tag::SCREEN_CAPTURE_START_LATENCY, statisticalEventInfo_.startLatency);
677     AppendMediaInfo(meta, instanceId_);
678     ReportMediaInfo(instanceId_);
679 }
680 
ScreenCaptureServer()681 ScreenCaptureServer::ScreenCaptureServer()
682 {
683     MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
684     InitAppInfo();
685     instanceId_ = OHOS::HiviewDFX::HiTraceChain::GetId().GetChainId();
686     CreateMediaInfo(SCREEN_CAPTRUER, IPCSkeleton::GetCallingUid(), instanceId_);
687 }
688 
~ScreenCaptureServer()689 ScreenCaptureServer::~ScreenCaptureServer()
690 {
691     MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
692     ReleaseInner();
693     CloseFd();
694 }
695 
SetSessionId(int32_t sessionId)696 void ScreenCaptureServer::SetSessionId(int32_t sessionId)
697 {
698     sessionId_ = sessionId;
699     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR "sessionId: %{public}d", FAKE_POINTER(this), sessionId_);
700 }
701 
SetCaptureMode(CaptureMode captureMode)702 int32_t ScreenCaptureServer::SetCaptureMode(CaptureMode captureMode)
703 {
704     MediaTrace trace("ScreenCaptureServer::SetCaptureMode");
705     std::lock_guard<std::mutex> lock(mutex_);
706     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::CREATED, MSERR_INVALID_OPERATION,
707         "SetCaptureMode failed, capture is not CREATED, state:%{public}d, mode:%{public}d", captureState_, captureMode);
708     MEDIA_LOGI("ScreenCaptureServer::SetCaptureMode start, captureMode:%{public}d", captureMode);
709     int32_t ret = CheckCaptureMode(captureMode);
710     CHECK_AND_RETURN_RET(ret == MSERR_OK, ret);
711     captureConfig_.captureMode = captureMode;
712     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetCaptureMode OK.", FAKE_POINTER(this));
713     return MSERR_OK;
714 }
715 
SetDataType(DataType dataType)716 int32_t ScreenCaptureServer::SetDataType(DataType dataType)
717 {
718     MediaTrace trace("ScreenCaptureServer::SetDataType");
719     std::lock_guard<std::mutex> lock(mutex_);
720     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::CREATED, MSERR_INVALID_OPERATION,
721         "SetDataType failed, capture is not CREATED, state:%{public}d, dataType:%{public}d", captureState_, dataType);
722     MEDIA_LOGI("ScreenCaptureServer::SetDataType start, dataType:%{public}d", dataType);
723     int32_t ret = CheckDataType(dataType);
724     CHECK_AND_RETURN_RET(ret == MSERR_OK, ret);
725     captureConfig_.dataType = dataType;
726     CHECK_AND_RETURN_RET_LOG(CheckSCServerSpecifiedDataTypeNum(GetAppUid(), GetSCServerDataType()),
727         MSERR_INVALID_OPERATION,
728         "ScreenCaptureServer: 0x%{public}06" PRIXPTR "SetDataType failed.", FAKE_POINTER(this));
729     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetDataType OK.", FAKE_POINTER(this));
730     return MSERR_OK;
731 }
732 
SetRecorderInfo(RecorderInfo recorderInfo)733 int32_t ScreenCaptureServer::SetRecorderInfo(RecorderInfo recorderInfo)
734 {
735     std::lock_guard<std::mutex> lock(mutex_);
736     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::CREATED, MSERR_INVALID_OPERATION,
737         "SetRecorderInfo failed, capture is not CREATED, state:%{public}d", captureState_);
738     MEDIA_LOGI("ScreenCaptureServer::SetRecorderInfo start");
739     url_ = recorderInfo.url;
740     avType_ = AVScreenCaptureAvType::AV_TYPE;
741 
742     if (MP4.compare(recorderInfo.fileFormat) == 0) {
743         fileFormat_ = OutputFormatType::FORMAT_MPEG_4;
744     } else if (M4A.compare(recorderInfo.fileFormat) == 0) {
745         fileFormat_ = OutputFormatType::FORMAT_M4A;
746     } else {
747         MEDIA_LOGE("invalid fileFormat type");
748         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_,
749             SCREEN_CAPTURE_ERR_INVALID_VAL, "invalid fileFormat type");
750         return MSERR_INVALID_VAL;
751     }
752     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetRecorderInfo OK.", FAKE_POINTER(this));
753     return MSERR_OK;
754 }
755 
SetOutputFile(int32_t outputFd)756 int32_t ScreenCaptureServer::SetOutputFile(int32_t outputFd)
757 {
758     std::lock_guard<std::mutex> lock(mutex_);
759     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::CREATED, MSERR_INVALID_OPERATION,
760         "SetOutputFile failed, capture is not CREATED, state:%{public}d", captureState_);
761     MEDIA_LOGI("ScreenCaptureServer::SetOutputFile start");
762     if (outputFd < 0) {
763         MEDIA_LOGI("invalid outputFd");
764         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_,
765             SCREEN_CAPTURE_ERR_INVALID_VAL, "invalid outputFd");
766         return MSERR_INVALID_VAL;
767     }
768 
769     int flags = fcntl(outputFd, F_GETFL);
770     if (flags == -1) {
771         MEDIA_LOGE("Fail to get File Status Flags");
772         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_INVALID_VAL,
773             "Fail to get File Status Flags");
774         return MSERR_INVALID_VAL;
775     }
776     if ((static_cast<unsigned int>(flags) & (O_RDWR | O_WRONLY)) == 0) {
777         MEDIA_LOGE("File descriptor is not in read-write mode or write-only mode");
778         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_INVALID_VAL,
779             "File descriptor is not in read-write mode or write-only mode");
780         return MSERR_INVALID_VAL;
781     }
782     CloseFd();
783     MEDIA_LOGI("ScreenCaptureServer fd in, fd is %{public}d", outputFd);
784     outputFd_ = dup(outputFd);
785     CHECK_AND_RETURN_RET_LOG(outputFd_ >= 0, MSERR_NO_MEMORY, "dup outputFd failed");
786     MEDIA_LOGI("ScreenCaptureServer fd dup, fd is %{public}d", outputFd_);
787     MEDIA_LOGI("ScreenCaptureServer SetOutputFile End");
788     return MSERR_OK;
789 }
790 
SetScreenCaptureCallback(const std::shared_ptr<ScreenCaptureCallBack> & callback)791 int32_t ScreenCaptureServer::SetScreenCaptureCallback(const std::shared_ptr<ScreenCaptureCallBack> &callback)
792 {
793     MediaTrace trace("ScreenCaptureServer::SetScreenCaptureCallback");
794     std::lock_guard<std::mutex> lock(mutex_);
795     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::CREATED, MSERR_INVALID_OPERATION,
796         "SetScreenCaptureCallback failed, capture is not CREATED, state:%{public}d", captureState_);
797     CHECK_AND_RETURN_RET_LOG(callback != nullptr, MSERR_INVALID_VAL,
798         "SetScreenCaptureCallback failed, callback is nullptr, state:%{public}d", captureState_);
799     MEDIA_LOGI("ScreenCaptureServer::SetScreenCaptureCallback start");
800     screenCaptureCb_ = callback;
801     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetScreenCaptureCallback OK.", FAKE_POINTER(this));
802     return MSERR_OK;
803 }
804 
InitAudioEncInfo(AudioEncInfo audioEncInfo)805 int32_t ScreenCaptureServer::InitAudioEncInfo(AudioEncInfo audioEncInfo)
806 {
807     std::lock_guard<std::mutex> lock(mutex_);
808     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::CREATED, MSERR_INVALID_OPERATION,
809         "InitAudioEncInfo failed, capture is not CREATED, state:%{public}d", captureState_);
810     MEDIA_LOGI("ScreenCaptureServer::InitAudioEncInfo start");
811     MEDIA_LOGD("audioEncInfo audioBitrate:%{public}d, audioCodecformat:%{public}d", audioEncInfo.audioBitrate,
812         audioEncInfo.audioCodecformat);
813     int32_t ret = CheckAudioEncInfo(audioEncInfo);
814     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "InitAudioEncInfo failed, ret:%{public}d", ret);
815     captureConfig_.audioInfo.audioEncInfo = audioEncInfo;
816     return MSERR_OK;
817 }
818 
InitVideoEncInfo(VideoEncInfo videoEncInfo)819 int32_t ScreenCaptureServer::InitVideoEncInfo(VideoEncInfo videoEncInfo)
820 {
821     std::lock_guard<std::mutex> lock(mutex_);
822     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::CREATED, MSERR_INVALID_OPERATION,
823         "InitVideoEncInfo failed, capture is not CREATED, state:%{public}d", captureState_);
824     MEDIA_LOGI("ScreenCaptureServer::InitVideoEncInfo start");
825     MEDIA_LOGD("videoEncInfo videoCodec:%{public}d,  videoBitrate:%{public}d, videoFrameRate:%{public}d",
826         videoEncInfo.videoCodec, videoEncInfo.videoBitrate, videoEncInfo.videoFrameRate);
827     int32_t ret = CheckVideoEncInfo(videoEncInfo);
828     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "InitVideoEncInfo failed, ret:%{public}d", ret);
829     captureConfig_.videoInfo.videoEncInfo = videoEncInfo;
830     return MSERR_OK;
831 }
832 
CheckScreenCapturePermission()833 bool ScreenCaptureServer::CheckScreenCapturePermission()
834 {
835     int result = Security::AccessToken::AccessTokenKit::VerifyAccessToken(appInfo_.appTokenId,
836         "ohos.permission.CAPTURE_SCREEN");
837     if (result == Security::AccessToken::PERMISSION_GRANTED) {
838         MEDIA_LOGI("user have the right to access capture screen!");
839         return true;
840     } else {
841         MEDIA_LOGE("user do not have the right to access capture screen!");
842         return false;
843     }
844 }
845 
IsUserPrivacyAuthorityNeeded()846 bool ScreenCaptureServer::IsUserPrivacyAuthorityNeeded()
847 {
848     MediaTrace trace("ScreenCaptureServer::IsUserPrivacyAuthorityNeeded");
849     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " IsUserPrivacyAuthorityNeeded start, appUid:%{public}d",
850         FAKE_POINTER(this), appInfo_.appUid);
851     if (appInfo_.appUid == ROOT_UID) {
852         MEDIA_LOGI("Root user. Privacy Authority Granted automaticly");
853         return false;
854     }
855     return true;
856 }
857 
CheckCaptureMode(CaptureMode captureMode)858 int32_t ScreenCaptureServer::CheckCaptureMode(CaptureMode captureMode)
859 {
860     MEDIA_LOGD("CheckCaptureMode start, captureMode:%{public}d", captureMode);
861     if ((captureMode > CAPTURE_SPECIFIED_WINDOW) || (captureMode < CAPTURE_HOME_SCREEN)) {
862         MEDIA_LOGE("invalid captureMode:%{public}d", captureMode);
863         return MSERR_INVALID_VAL;
864     }
865     MEDIA_LOGD("ScreenCaptureServer CheckCaptureMode OK.");
866     return MSERR_OK;
867 }
868 
CheckDataType(DataType dataType)869 int32_t ScreenCaptureServer::CheckDataType(DataType dataType)
870 {
871     MEDIA_LOGD("CheckDataType start, dataType:%{public}d", dataType);
872     if ((dataType > DataType::CAPTURE_FILE) || (dataType < DataType::ORIGINAL_STREAM)) {
873         MEDIA_LOGE("invalid dataType:%{public}d", dataType);
874         return MSERR_INVALID_VAL;
875     }
876     if (dataType == DataType::ENCODED_STREAM) {
877         MEDIA_LOGE("not supported dataType:%{public}d", dataType);
878         return MSERR_UNSUPPORT;
879     }
880     MEDIA_LOGD("ScreenCaptureServer CheckDataType OK.");
881     return MSERR_OK;
882 }
883 
CheckAudioCapParam(const AudioCaptureInfo & audioCapInfo)884 int32_t ScreenCaptureServer::CheckAudioCapParam(const AudioCaptureInfo &audioCapInfo)
885 {
886     MEDIA_LOGD("CheckAudioCapParam sampleRate:%{public}d, channels:%{public}d, source:%{public}d, state:%{public}d",
887         audioCapInfo.audioSampleRate, audioCapInfo.audioChannels, audioCapInfo.audioSource, audioCapInfo.state);
888     std::vector<AudioSamplingRate> supportedSamplingRates = AudioStandard::AudioCapturer::GetSupportedSamplingRates();
889     bool foundSupportSample = false;
890     for (auto iter = supportedSamplingRates.begin(); iter != supportedSamplingRates.end(); ++iter) {
891         if (static_cast<AudioSamplingRate>(audioCapInfo.audioSampleRate) == *iter) {
892             foundSupportSample = true;
893         }
894     }
895     if (!foundSupportSample) {
896         MEDIA_LOGE("invalid audioSampleRate:%{public}d", audioCapInfo.audioSampleRate);
897         return MSERR_UNSUPPORT;
898     }
899 
900     std::vector<AudioChannel> supportedChannelList = AudioStandard::AudioCapturer::GetSupportedChannels();
901     bool foundSupportChannel = false;
902     for (auto iter = supportedChannelList.begin(); iter != supportedChannelList.end(); ++iter) {
903         if (static_cast<AudioChannel>(audioCapInfo.audioChannels) == *iter) {
904             foundSupportChannel = true;
905         }
906     }
907     if (!foundSupportChannel) {
908         MEDIA_LOGE("invalid audioChannels:%{public}d", audioCapInfo.audioChannels);
909         return MSERR_UNSUPPORT;
910     }
911 
912     if ((audioCapInfo.audioSource <= SOURCE_INVALID) || (audioCapInfo.audioSource > APP_PLAYBACK)) {
913         MEDIA_LOGE("invalid audioSource:%{public}d", audioCapInfo.audioSource);
914         return MSERR_INVALID_VAL;
915     }
916     MEDIA_LOGD("ScreenCaptureServer CheckAudioCapParam OK.");
917     return MSERR_OK;
918 }
919 
CheckVideoCapParam(const VideoCaptureInfo & videoCapInfo)920 int32_t ScreenCaptureServer::CheckVideoCapParam(const VideoCaptureInfo &videoCapInfo)
921 {
922     MEDIA_LOGD("CheckVideoCapParam width:%{public}d, height:%{public}d, source:%{public}d, state:%{public}d",
923         videoCapInfo.videoFrameWidth, videoCapInfo.videoFrameHeight, videoCapInfo.videoSource, videoCapInfo.state);
924     if ((videoCapInfo.videoFrameWidth <= 0) || (videoCapInfo.videoFrameWidth > VIDEO_FRAME_WIDTH_MAX)) {
925         MEDIA_LOGE("videoCapInfo Width is invalid, videoFrameWidth:%{public}d, videoFrameHeight:%{public}d",
926             videoCapInfo.videoFrameWidth, videoCapInfo.videoFrameHeight);
927         return MSERR_INVALID_VAL;
928     }
929     if ((videoCapInfo.videoFrameHeight <= 0) || (videoCapInfo.videoFrameHeight > VIDEO_FRAME_HEIGHT_MAX)) {
930         MEDIA_LOGE("videoCapInfo Height is invalid, videoFrameWidth:%{public}d, videoFrameHeight:%{public}d",
931             videoCapInfo.videoFrameWidth, videoCapInfo.videoFrameHeight);
932         return MSERR_INVALID_VAL;
933     }
934 
935     if (videoCapInfo.videoSource != VIDEO_SOURCE_SURFACE_RGBA) {
936         MEDIA_LOGE("videoSource is invalid");
937         return MSERR_INVALID_VAL;
938     }
939     MEDIA_LOGD("ScreenCaptureServer CheckVideoCapParam OK.");
940     return MSERR_OK;
941 }
942 
CheckAudioEncParam(const AudioEncInfo & audioEncInfo)943 int32_t ScreenCaptureServer::CheckAudioEncParam(const AudioEncInfo &audioEncInfo)
944 {
945     MEDIA_LOGD("CheckAudioEncParam audioBitrate:%{public}d, audioCodecformat:%{public}d",
946         audioEncInfo.audioBitrate, audioEncInfo.audioCodecformat);
947     if ((audioEncInfo.audioCodecformat >= AudioCodecFormat::AUDIO_CODEC_FORMAT_BUTT) ||
948         (audioEncInfo.audioCodecformat < AudioCodecFormat::AUDIO_DEFAULT)) {
949         MEDIA_LOGE("invalid AudioCodecFormat:%{public}d", audioEncInfo.audioCodecformat);
950         return MSERR_INVALID_VAL;
951     }
952     if (audioEncInfo.audioBitrate < AUDIO_BITRATE_MIN || audioEncInfo.audioBitrate > AUDIO_BITRATE_MAX) {
953         MEDIA_LOGE("invalid audioBitrate:%{public}d", audioEncInfo.audioBitrate);
954         return MSERR_INVALID_VAL;
955     }
956     return MSERR_OK;
957 }
958 
CheckVideoEncParam(const VideoEncInfo & videoEncInfo)959 int32_t ScreenCaptureServer::CheckVideoEncParam(const VideoEncInfo &videoEncInfo)
960 {
961     MEDIA_LOGD("CheckVideoEncParam videoCodec:%{public}d, videoBitrate:%{public}d, videoFrameRate:%{public}d",
962         videoEncInfo.videoCodec, videoEncInfo.videoBitrate, videoEncInfo.videoFrameRate);
963     if ((videoEncInfo.videoCodec >= VideoCodecFormat::VIDEO_CODEC_FORMAT_BUTT) ||
964         (videoEncInfo.videoCodec < VideoCodecFormat::VIDEO_DEFAULT)) {
965         MEDIA_LOGE("invalid VideoCodecFormat:%{public}d", videoEncInfo.videoCodec);
966         return MSERR_INVALID_VAL;
967     }
968     if (videoEncInfo.videoBitrate < VIDEO_BITRATE_MIN || videoEncInfo.videoBitrate > VIDEO_BITRATE_MAX) {
969         MEDIA_LOGE("invalid videoBitrate:%{public}d", videoEncInfo.videoBitrate);
970         return MSERR_INVALID_VAL;
971     }
972     if (videoEncInfo.videoFrameRate < VIDEO_FRAME_RATE_MIN || videoEncInfo.videoFrameRate > VIDEO_FRAME_RATE_MAX) {
973         MEDIA_LOGE("invalid videoFrameRate:%{public}d", videoEncInfo.videoFrameRate);
974         return MSERR_INVALID_VAL;
975     }
976     return MSERR_OK;
977 }
978 
CheckAudioCapInfo(AudioCaptureInfo & audioCapInfo)979 int32_t ScreenCaptureServer::CheckAudioCapInfo(AudioCaptureInfo &audioCapInfo)
980 {
981     MEDIA_LOGD("ScreenCaptureServer CheckAudioCapInfo start, audioChannels:%{public}d, "
982         "audioSampleRate:%{public}d, audioSource:%{public}d, state:%{public}d.",
983         audioCapInfo.audioChannels, audioCapInfo.audioSampleRate, audioCapInfo.audioSource, audioCapInfo.state);
984     if (audioCapInfo.audioChannels == 0 && audioCapInfo.audioSampleRate == 0) {
985         MEDIA_LOGD("audioCap IGNORED sampleRate:%{public}d, channels:%{public}d, source:%{public}d, state:%{public}d",
986             audioCapInfo.audioSampleRate, audioCapInfo.audioChannels, audioCapInfo.audioSource, audioCapInfo.state);
987         audioCapInfo.state = AVScreenCaptureParamValidationState::VALIDATION_IGNORE;
988         return MSERR_OK;
989     }
990     MEDIA_LOGD("CheckAudioCapParam S sampleRate:%{public}d, channels:%{public}d, source:%{public}d, state:%{public}d",
991         audioCapInfo.audioSampleRate, audioCapInfo.audioChannels, audioCapInfo.audioSource, audioCapInfo.state);
992     int32_t ret = CheckAudioCapParam(audioCapInfo);
993     audioCapInfo.state = ret == MSERR_OK ? AVScreenCaptureParamValidationState::VALIDATION_VALID :
994         AVScreenCaptureParamValidationState::VALIDATION_INVALID;
995     MEDIA_LOGD("CheckAudioCapParam E sampleRate:%{public}d, channels:%{public}d, source:%{public}d, state:%{public}d",
996         audioCapInfo.audioSampleRate, audioCapInfo.audioChannels, audioCapInfo.audioSource, audioCapInfo.state);
997     MEDIA_LOGD("ScreenCaptureServer CheckAudioCapInfo end.");
998     return ret;
999 }
1000 
CheckVideoCapInfo(VideoCaptureInfo & videoCapInfo)1001 int32_t ScreenCaptureServer::CheckVideoCapInfo(VideoCaptureInfo &videoCapInfo)
1002 {
1003     MEDIA_LOGD("CheckVideoCapInfo start, videoFrameWidth:%{public}d, videoFrameHeight:%{public}d, "
1004         "videoSource:%{public}d, state:%{public}d, screenCaptureFillMode:%{public}d.", videoCapInfo.videoFrameWidth,
1005         videoCapInfo.videoFrameHeight, videoCapInfo.videoSource, videoCapInfo.state,
1006         videoCapInfo.screenCaptureFillMode);
1007     if (videoCapInfo.videoFrameWidth == 0 && videoCapInfo.videoFrameHeight == 0) {
1008         MEDIA_LOGD("videoCap IGNORED width:%{public}d, height:%{public}d, source:%{public}d, state:%{public}d",
1009             videoCapInfo.videoFrameWidth, videoCapInfo.videoFrameHeight, videoCapInfo.videoSource, videoCapInfo.state);
1010         videoCapInfo.state = AVScreenCaptureParamValidationState::VALIDATION_IGNORE;
1011         return MSERR_OK;
1012     }
1013     MEDIA_LOGD("CheckVideoCapParam S width:%{public}d, height:%{public}d, source:%{public}d, state:%{public}d",
1014         videoCapInfo.videoFrameWidth, videoCapInfo.videoFrameHeight, videoCapInfo.videoSource, videoCapInfo.state);
1015     int32_t ret = CheckVideoCapParam(videoCapInfo);
1016     videoCapInfo.state = ret == MSERR_OK ? AVScreenCaptureParamValidationState::VALIDATION_VALID :
1017         AVScreenCaptureParamValidationState::VALIDATION_INVALID;
1018     MEDIA_LOGD("CheckVideoCapParam E width:%{public}d, height:%{public}d, source:%{public}d, state:%{public}d",
1019         videoCapInfo.videoFrameWidth, videoCapInfo.videoFrameHeight, videoCapInfo.videoSource, videoCapInfo.state);
1020     MEDIA_LOGD("ScreenCaptureServer CheckVideoCapInfo end.");
1021     return ret;
1022 }
1023 
CheckAudioEncInfo(AudioEncInfo & audioEncInfo)1024 int32_t ScreenCaptureServer::CheckAudioEncInfo(AudioEncInfo &audioEncInfo)
1025 {
1026     MEDIA_LOGD("ScreenCaptureServer CheckAudioEncInfo start.");
1027     int32_t ret = CheckAudioEncParam(audioEncInfo);
1028     audioEncInfo.state = ret == MSERR_OK ? AVScreenCaptureParamValidationState::VALIDATION_VALID :
1029         AVScreenCaptureParamValidationState::VALIDATION_INVALID;
1030     MEDIA_LOGD("ScreenCaptureServer CheckAudioEncInfo end, state: %{public}d.", audioEncInfo.state);
1031     return ret;
1032 }
1033 
CheckVideoEncInfo(VideoEncInfo & videoEncInfo)1034 int32_t ScreenCaptureServer::CheckVideoEncInfo(VideoEncInfo &videoEncInfo)
1035 {
1036     MEDIA_LOGD("ScreenCaptureServer CheckVideoEncInfo start.");
1037     int32_t ret = CheckVideoEncParam(videoEncInfo);
1038     videoEncInfo.state = ret == MSERR_OK ? AVScreenCaptureParamValidationState::VALIDATION_VALID :
1039         AVScreenCaptureParamValidationState::VALIDATION_INVALID;
1040     MEDIA_LOGD("ScreenCaptureServer CheckVideoEncInfo end, state: %{public}d.", videoEncInfo.state);
1041     return ret;
1042 }
1043 
CheckAllParams()1044 int32_t ScreenCaptureServer::CheckAllParams()
1045 {
1046     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " CheckAllParams start, dataType:%{public}d.",
1047         FAKE_POINTER(this), captureConfig_.dataType);
1048     int32_t ret = CheckDataType(captureConfig_.dataType);
1049     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "CheckAllParams CheckDataType failed, ret:%{public}d", ret);
1050 
1051     ret = CheckCaptureMode(captureConfig_.captureMode);
1052     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "CheckAllParams CheckCaptureMode failed, ret:%{public}d", ret);
1053 
1054     if (captureConfig_.dataType == DataType::ORIGINAL_STREAM) {
1055         if (isSurfaceMode_) {
1056             dataMode_ = AVScreenCaptureDataMode::SUFFACE_MODE;
1057         } else {
1058             dataMode_ = AVScreenCaptureDataMode::BUFFER_MODE;
1059         }
1060         return CheckCaptureStreamParams();
1061     }
1062     if (captureConfig_.dataType == DataType::CAPTURE_FILE) {
1063         dataMode_ = AVScreenCaptureDataMode::FILE_MODE;
1064         return CheckCaptureFileParams();
1065     }
1066     return MSERR_INVALID_VAL;
1067 }
1068 
CheckCaptureStreamParams()1069 int32_t ScreenCaptureServer::CheckCaptureStreamParams()
1070 {
1071     // For original stream:
1072     // 1. Any of innerCapInfo/videoCapInfo should be not invalid and should not be both ignored
1073     // 2. micCapInfo should not be invalid
1074     // 3. For surface mode, videoCapInfo should be valid
1075     CheckAudioCapInfo(captureConfig_.audioInfo.micCapInfo);
1076     CheckAudioCapInfo(captureConfig_.audioInfo.innerCapInfo);
1077     CheckVideoCapInfo(captureConfig_.videoInfo.videoCapInfo);
1078     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " CheckCaptureStreamParams start, isSurfaceMode:%{public}s,"
1079         " videoCapInfo.state:%{public}d, innerCapInfo.state:%{public}d.", FAKE_POINTER(this),
1080         isSurfaceMode_ ? "true" : "false", captureConfig_.videoInfo.videoCapInfo.state,
1081         captureConfig_.audioInfo.innerCapInfo.state);
1082     if (isSurfaceMode_) {
1083         // surface mode, surface must not nullptr and videoCapInfo must valid.
1084         if (surface_ == nullptr ||
1085             captureConfig_.videoInfo.videoCapInfo.state != AVScreenCaptureParamValidationState::VALIDATION_VALID) {
1086             FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_INVALID_VAL,
1087                 "video Cap state fault, videoCapInfo is invalid");
1088             return MSERR_INVALID_VAL;
1089         }
1090     }
1091     if (captureConfig_.audioInfo.innerCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_INVALID ||
1092         captureConfig_.videoInfo.videoCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_INVALID) {
1093         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_INVALID_VAL,
1094             "audio inner cap or video cap state invalid");
1095         return MSERR_INVALID_VAL;
1096     }
1097     if (captureConfig_.audioInfo.innerCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_IGNORE &&
1098         captureConfig_.videoInfo.videoCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_IGNORE) {
1099         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_INVALID_VAL,
1100             "audio inner cap or video cap state ignore");
1101         return MSERR_INVALID_VAL;
1102     }
1103     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " CheckCaptureStreamParams OK.", FAKE_POINTER(this));
1104     return MSERR_OK;
1105 }
1106 
CheckCaptureFileParams()1107 int32_t ScreenCaptureServer::CheckCaptureFileParams()
1108 {
1109     // For capture file:
1110     // 1. All of innerCapInfo/videoCapInfo/audioEncInfo/videoEncInfo should be be valid
1111     // 2. micCapInfo should not be invalid
1112     CheckAudioCapInfo(captureConfig_.audioInfo.micCapInfo);
1113     CheckAudioCapInfo(captureConfig_.audioInfo.innerCapInfo);
1114     CheckAudioEncInfo(captureConfig_.audioInfo.audioEncInfo);
1115     CheckVideoCapInfo(captureConfig_.videoInfo.videoCapInfo);
1116     if (captureConfig_.videoInfo.videoCapInfo.state != AVScreenCaptureParamValidationState::VALIDATION_IGNORE) {
1117         CheckVideoEncInfo(captureConfig_.videoInfo.videoEncInfo);
1118     }
1119     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " CheckCaptureFileParams start, "
1120         "innerCapInfo.state:%{public}d, videoCapInfo.state:%{public}d, audioEncInfo.state:%{public}d, "
1121         "videoEncInfo.state:%{public}d, micCapInfo.state:%{public}d.", FAKE_POINTER(this),
1122         captureConfig_.audioInfo.innerCapInfo.state, captureConfig_.videoInfo.videoCapInfo.state,
1123         captureConfig_.audioInfo.audioEncInfo.state, captureConfig_.videoInfo.videoEncInfo.state,
1124         captureConfig_.audioInfo.micCapInfo.state);
1125 
1126     if (captureConfig_.audioInfo.innerCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_INVALID ||
1127         captureConfig_.videoInfo.videoCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_INVALID ||
1128         captureConfig_.audioInfo.audioEncInfo.state == AVScreenCaptureParamValidationState::VALIDATION_INVALID ||
1129         captureConfig_.videoInfo.videoEncInfo.state == AVScreenCaptureParamValidationState::VALIDATION_INVALID) {
1130         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_INVALID_VAL,
1131             "innerCap audioEnc videoCap videoEnc state invalid");
1132         return MSERR_INVALID_VAL;
1133     }
1134     if (captureConfig_.audioInfo.micCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_INVALID) {
1135         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_INVALID_VAL,
1136             "audio mic cap state invalid");
1137         return MSERR_INVALID_VAL;
1138     }
1139     if (captureConfig_.audioInfo.micCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_IGNORE) {
1140         return MSERR_OK;
1141     }
1142     const AudioCaptureInfo &micCapInfo = captureConfig_.audioInfo.micCapInfo;
1143     const AudioCaptureInfo &innerCapInfo = captureConfig_.audioInfo.innerCapInfo;
1144     if (micCapInfo.audioSampleRate == innerCapInfo.audioSampleRate &&
1145         micCapInfo.audioChannels == innerCapInfo.audioChannels) {
1146         return MSERR_OK;
1147     }
1148     MEDIA_LOGE("CheckCaptureFileParams failed, inner and mic param not consistent");
1149     FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_INVALID_VAL,
1150         "CheckCaptureFileParams failed, inner and mic param not consistent");
1151     return MSERR_INVALID_VAL;
1152 }
1153 
1154 // Should call in ipc thread
InitAppInfo()1155 void ScreenCaptureServer::InitAppInfo()
1156 {
1157     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " InitAppInfo start.", FAKE_POINTER(this));
1158     appInfo_.appTokenId = IPCSkeleton::GetCallingTokenID();
1159     appInfo_.appFullTokenId = IPCSkeleton::GetCallingFullTokenID();
1160     appInfo_.appUid = IPCSkeleton::GetCallingUid();
1161     appInfo_.appPid = IPCSkeleton::GetCallingPid();
1162     appName_ = GetClientBundleName(appInfo_.appUid);
1163     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " InitAppInfo end.", FAKE_POINTER(this));
1164 }
1165 
GetCurrentMillisecond()1166 int64_t ScreenCaptureServer::GetCurrentMillisecond()
1167 {
1168     std::chrono::system_clock::duration duration = std::chrono::system_clock::now().time_since_epoch();
1169     int64_t time = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
1170     return time;
1171 }
1172 
SetErrorInfo(int32_t errCode,const std::string & errMsg,StopReason stopReason,bool userAgree)1173 void ScreenCaptureServer::SetErrorInfo(int32_t errCode, const std::string &errMsg, StopReason stopReason,
1174     bool userAgree)
1175 {
1176     statisticalEventInfo_.errCode = errCode;
1177     statisticalEventInfo_.errMsg = errMsg;
1178     statisticalEventInfo_.stopReason = stopReason;
1179     statisticalEventInfo_.userAgree = userAgree;
1180 }
1181 
CheckPrivacyWindowSkipPermission()1182 bool ScreenCaptureServer::CheckPrivacyWindowSkipPermission()
1183 {
1184     MEDIA_LOGI("ScreenCaptureServer::CheckPrivacyWindowSkipPermission() START.");
1185     int result = Security::AccessToken::AccessTokenKit::VerifyAccessToken(appInfo_.appTokenId,
1186         "ohos.permission.EXEMPT_CAPTURE_SCREEN_AUTHORIZE");
1187     if (result == Security::AccessToken::PERMISSION_GRANTED) {
1188         MEDIA_LOGI("CheckPrivacyWindowSkipPermission: user have the right to skip privacywindow");
1189         return true;
1190     }
1191     MEDIA_LOGD("CheckPrivacyWindowSkipPermission: user do not have the right to skip privacywindow");
1192     return false;
1193 }
1194 
RequestUserPrivacyAuthority()1195 int32_t ScreenCaptureServer::RequestUserPrivacyAuthority()
1196 {
1197     MediaTrace trace("ScreenCaptureServer::RequestUserPrivacyAuthority");
1198     // If Root is treated as whitelisted, how to guarantee RequestUserPrivacyAuthority function by TDD cases.
1199     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " RequestUserPrivacyAuthority start.", FAKE_POINTER(this));
1200 
1201     if (isPrivacyAuthorityEnabled_) {
1202         if (GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"]
1203                 .compare(appName_) != 0) {
1204             return StartPrivacyWindow();
1205         } else {
1206             MEDIA_LOGI("ScreenCaptureServer::RequestUserPrivacyAuthority support screenrecorder");
1207             return MSERR_OK;
1208         }
1209     }
1210 
1211     MEDIA_LOGI("privacy notification window not support, go on to check CAPTURE_SCREEN permission");
1212     return CheckScreenCapturePermission() ? MSERR_OK : MSERR_INVALID_OPERATION;
1213 }
1214 
OnReceiveUserPrivacyAuthority(bool isAllowed)1215 int32_t ScreenCaptureServer::OnReceiveUserPrivacyAuthority(bool isAllowed)
1216 {
1217     // Should callback be running in seperate thread?
1218     std::lock_guard<std::mutex> lock(mutex_);
1219     MEDIA_LOGI("OnReceiveUserPrivacyAuthority start, isAllowed:%{public}d, state:%{public}d", isAllowed, captureState_);
1220     if (screenCaptureCb_ == nullptr) {
1221         MEDIA_LOGE("OnReceiveUserPrivacyAuthority failed, screenCaptureCb is nullptr, state:%{public}d", captureState_);
1222         captureState_ = AVScreenCaptureState::STOPPED;
1223         SetErrorInfo(MSERR_UNKNOWN, "OnReceiveUserPrivacyAuthority failed, screenCaptureCb is nullptr",
1224             StopReason::RECEIVE_USER_PRIVACY_AUTHORITY_FAILED, IsUserPrivacyAuthorityNeeded());
1225         return MSERR_UNKNOWN;
1226     }
1227 
1228     if (captureState_ != AVScreenCaptureState::POPUP_WINDOW) {
1229         MEDIA_LOGE("OnReceiveUserPrivacyAuthority failed, capture is not POPUP_WINDOW");
1230         screenCaptureCb_->OnError(ScreenCaptureErrorType::SCREEN_CAPTURE_ERROR_INTERNAL,
1231             AVScreenCaptureErrorCode::SCREEN_CAPTURE_ERR_UNKNOWN);
1232         StopScreenCaptureInner(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INVLID);
1233         return MSERR_UNKNOWN;
1234     }
1235     if (!isAllowed) {
1236         captureState_ = AVScreenCaptureState::CREATED;
1237         screenCaptureCb_->OnStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_CANCELED);
1238         return MSERR_UNKNOWN;
1239     }
1240     captureState_ = AVScreenCaptureState::STARTING;
1241     int32_t ret = OnStartScreenCapture();
1242     PostStartScreenCapture(ret == MSERR_OK);
1243     return ret;
1244 }
1245 
StartAudioCapture()1246 int32_t ScreenCaptureServer::StartAudioCapture()
1247 {
1248     int32_t ret = MSERR_UNKNOWN;
1249     if (isMicrophoneSwitchTurnOn_) {
1250         ret = StartStreamMicAudioCapture();
1251         if (ret != MSERR_OK) {
1252             MEDIA_LOGE("StartAudioCapture StartStreamMicAudioCapture failed");
1253         }
1254     }
1255     ret = StartStreamInnerAudioCapture();
1256     if (ret != MSERR_OK) {
1257         MEDIA_LOGE("StartStreamInnerAudioCapture failed");
1258         StopMicAudioCapture();
1259         return ret;
1260     }
1261     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartAudioCapture OK.", FAKE_POINTER(this));
1262     return MSERR_OK;
1263 }
1264 
GenerateThreadNameByPrefix(std::string threadName)1265 std::string ScreenCaptureServer::GenerateThreadNameByPrefix(std::string threadName)
1266 {
1267     return threadName + std::to_string(sessionId_);
1268 }
1269 
StartStreamInnerAudioCapture()1270 int32_t ScreenCaptureServer::StartStreamInnerAudioCapture()
1271 {
1272     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartStreamInnerAudioCapture start, dataType:%{public}d,"
1273         " innerCapInfo.state:%{public}d.",
1274         FAKE_POINTER(this), captureConfig_.dataType, captureConfig_.audioInfo.innerCapInfo.state);
1275     std::shared_ptr<AudioCapturerWrapper> innerCapture;
1276     if (captureConfig_.audioInfo.innerCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID) {
1277         MediaTrace trace("ScreenCaptureServer::StartAudioCaptureInner");
1278         innerCapture = std::make_shared<AudioCapturerWrapper>(captureConfig_.audioInfo.innerCapInfo, screenCaptureCb_,
1279             std::string(GenerateThreadNameByPrefix("OS_SInnAd")), contentFilter_);
1280         int32_t ret = innerCapture->Start(appInfo_);
1281         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "StartAudioCapture innerCapture failed");
1282     }
1283     innerAudioCapture_ = innerCapture;
1284     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartStreamInnerAudioCapture OK.", FAKE_POINTER(this));
1285     return MSERR_OK;
1286 }
1287 
StartStreamMicAudioCapture()1288 int32_t ScreenCaptureServer::StartStreamMicAudioCapture()
1289 {
1290     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartStreamMicAudioCapture start, dataType:%{public}d, "
1291         "micCapInfo.state:%{public}d.",
1292         FAKE_POINTER(this), captureConfig_.dataType, captureConfig_.audioInfo.micCapInfo.state);
1293     std::shared_ptr<AudioCapturerWrapper> micCapture;
1294     if (captureConfig_.audioInfo.micCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID) {
1295         MediaTrace trace("ScreenCaptureServer::StartAudioCaptureMic");
1296         ScreenCaptureContentFilter contentFilterMic;
1297         micCapture = std::make_shared<AudioCapturerWrapper>(captureConfig_.audioInfo.micCapInfo, screenCaptureCb_,
1298             std::string(GenerateThreadNameByPrefix("OS_SMicAd")), contentFilterMic);
1299         int32_t ret = micCapture->Start(appInfo_);
1300         if (ret != MSERR_OK) {
1301             MEDIA_LOGE("StartStreamMicAudioCapture failed");
1302             NotifyStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_MIC_UNAVAILABLE);
1303             return ret;
1304         }
1305     }
1306     micAudioCapture_ = micCapture;
1307     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartStreamMicAudioCapture OK.", FAKE_POINTER(this));
1308     return MSERR_OK;
1309 }
1310 
StartFileInnerAudioCapture()1311 int32_t ScreenCaptureServer::StartFileInnerAudioCapture()
1312 {
1313     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartFileInnerAudioCapture start, dataType:%{public}d, "
1314         "innerCapInfo.state:%{public}d.",
1315         FAKE_POINTER(this), captureConfig_.dataType, captureConfig_.audioInfo.innerCapInfo.state);
1316     std::shared_ptr<AudioCapturerWrapper> innerCapture;
1317     if (captureConfig_.audioInfo.innerCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID) {
1318         MediaTrace trace("ScreenCaptureServer::StartFileInnerAudioCaptureInner");
1319         innerCapture = std::make_shared<AudioCapturerWrapper>(captureConfig_.audioInfo.innerCapInfo, screenCaptureCb_,
1320             std::string(GenerateThreadNameByPrefix("OS_FInnAd")), contentFilter_);
1321         int32_t ret = innerCapture->Start(appInfo_);
1322         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "StartFileInnerAudioCapture failed");
1323         if (audioSource_ && audioSource_->GetSpeakerAliveStatus() && !audioSource_->GetIsInVoIPCall() &&
1324             micAudioCapture_ && micAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
1325             ret = innerCapture->Pause();
1326             CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "StartAudioCapture innerCapture Pause failed");
1327         }
1328     }
1329     innerAudioCapture_ = innerCapture;
1330     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartFileInnerAudioCapture OK.", FAKE_POINTER(this));
1331     return MSERR_OK;
1332 }
1333 
StartFileMicAudioCapture()1334 int32_t ScreenCaptureServer::StartFileMicAudioCapture()
1335 {
1336     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartFileMicAudioCapture start, dataType:%{public}d, "
1337         "micCapInfo.state:%{public}d.",
1338         FAKE_POINTER(this), captureConfig_.dataType, captureConfig_.audioInfo.micCapInfo.state);
1339 #ifdef SUPPORT_CALL
1340     if (InCallObserver::GetInstance().IsInCall(true) && !IsTelInCallSkipList()) {
1341         MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " skip creating micAudioCapture", FAKE_POINTER(this));
1342         return MSERR_OK;
1343     }
1344 #endif
1345     std::shared_ptr<AudioCapturerWrapper> micCapture;
1346     if (captureConfig_.audioInfo.micCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID) {
1347         MediaTrace trace("ScreenCaptureServer::StartFileMicAudioCaptureInner");
1348         ScreenCaptureContentFilter contentFilterMic;
1349         micCapture = std::make_shared<AudioCapturerWrapper>(captureConfig_.audioInfo.micCapInfo, screenCaptureCb_,
1350             std::string(GenerateThreadNameByPrefix("OS_FMicAd")), contentFilterMic);
1351         if (audioSource_) {
1352             micCapture->SetIsInVoIPCall(audioSource_->GetIsInVoIPCall());
1353         }
1354         int32_t ret = micCapture->Start(appInfo_);
1355         if (ret != MSERR_OK) {
1356             MEDIA_LOGE("StartFileMicAudioCapture micCapture failed");
1357             NotifyStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_MIC_UNAVAILABLE);
1358             return ret;
1359         }
1360     }
1361     micAudioCapture_ = micCapture;
1362     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartFileMicAudioCapture OK.", FAKE_POINTER(this));
1363     return MSERR_OK;
1364 }
1365 
StartScreenCaptureStream()1366 int32_t ScreenCaptureServer::StartScreenCaptureStream()
1367 {
1368     MediaTrace trace("ScreenCaptureServer::StartScreenCaptureStream");
1369     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartScreenCaptureStream start, dataType:%{public}d.",
1370         FAKE_POINTER(this), captureConfig_.dataType);
1371     CHECK_AND_RETURN_RET(captureConfig_.dataType == DataType::ORIGINAL_STREAM, MSERR_INVALID_OPERATION);
1372     int32_t ret = StartAudioCapture();
1373     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "StartAudioCapture failed, ret:%{public}d, dataType:%{public}d",
1374         ret, captureConfig_.dataType);
1375 
1376     ret = StartStreamVideoCapture();
1377     if (ret != MSERR_OK) {
1378         StopAudioCapture();
1379         MEDIA_LOGE("StartScreenCaptureStream failed");
1380         return ret;
1381     }
1382     MEDIA_LOGI("StartScreenCaptureStream success");
1383     return ret;
1384 }
1385 
StartScreenCaptureFile()1386 int32_t ScreenCaptureServer::StartScreenCaptureFile()
1387 {
1388     CHECK_AND_RETURN_RET(captureConfig_.dataType == DataType::CAPTURE_FILE, MSERR_INVALID_OPERATION);
1389 
1390     MEDIA_LOGI("StartScreenCaptureFile S");
1391     int32_t ret = InitRecorder();
1392     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "InitRecorder failed, ret:%{public}d, dataType:%{public}d",
1393         ret, captureConfig_.dataType);
1394 
1395     ON_SCOPE_EXIT(0) {
1396         if (recorder_ != nullptr) {
1397             recorder_->Release();
1398             recorder_ = nullptr;
1399             consumer_ = nullptr;
1400         }
1401     };
1402     std::string virtualScreenName = "screen_capture_file";
1403     ret = CreateVirtualScreen(virtualScreenName, consumer_);
1404     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "CreateVirtualScreen failed, ret:%{public}d, dataType:%{public}d",
1405         ret, captureConfig_.dataType);
1406 
1407     ON_SCOPE_EXIT(1) {
1408         DestroyVirtualScreen();
1409     };
1410 
1411     if (isMicrophoneSwitchTurnOn_) {
1412         int32_t retMic = StartFileMicAudioCapture();
1413         if (retMic != MSERR_OK) {
1414             MEDIA_LOGE("StartScreenCaptureFile StartFileMicAudioCapture failed");
1415             // not return, if start mic capture failed, inner capture will be started
1416         }
1417     }
1418     int32_t retInner = StartFileInnerAudioCapture();
1419     CHECK_AND_RETURN_RET_LOG(retInner == MSERR_OK, retInner, "StartFileInnerAudioCapture failed, ret:%{public}d,"
1420         "dataType:%{public}d", retInner, captureConfig_.dataType);
1421     MEDIA_LOGI("StartScreenCaptureFile RecorderServer S");
1422     ret = recorder_->Start();
1423     if (ret != MSERR_OK) {
1424         StopAudioCapture();
1425         MEDIA_LOGE("StartScreenCaptureFile recorder start failed");
1426     }
1427     MEDIA_LOGI("StartScreenCaptureFile RecorderServer E");
1428     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "recorder failed, ret:%{public}d, dataType:%{public}d",
1429         ret, captureConfig_.dataType);
1430     CANCEL_SCOPE_EXIT_GUARD(1);
1431     CANCEL_SCOPE_EXIT_GUARD(0);
1432 
1433     MEDIA_LOGI("StartScreenCaptureFile E");
1434     return ret;
1435 }
1436 
OnStartScreenCapture()1437 int32_t ScreenCaptureServer::OnStartScreenCapture()
1438 {
1439     MediaTrace trace("ScreenCaptureServer::OnStartScreenCapture");
1440     MEDIA_LOGI("OnStartScreenCapture start, dataType:%{public}d", captureConfig_.dataType);
1441     int32_t ret = MSERR_UNSUPPORT;
1442     if (captureConfig_.dataType == DataType::ORIGINAL_STREAM) {
1443         ret = StartScreenCaptureStream();
1444     } else if (captureConfig_.dataType == DataType::CAPTURE_FILE) {
1445         ret = StartScreenCaptureFile();
1446     }
1447     if (ret == MSERR_OK) {
1448         int64_t endTime = GetCurrentMillisecond();
1449         statisticalEventInfo_.startLatency = static_cast<int32_t>(endTime - startTime_);
1450         MEDIA_LOGI("OnStartScreenCapture start success, dataType:%{public}d", captureConfig_.dataType);
1451     } else {
1452         MEDIA_LOGE("OnStartScreenCapture start failed, dataType:%{public}d", captureConfig_.dataType);
1453         statisticalEventInfo_.startLatency = -1; // latency -1 means invalid
1454     }
1455     return ret;
1456 }
1457 
ResSchedReportData(int64_t value,std::unordered_map<std::string,std::string> payload)1458 void ScreenCaptureServer::ResSchedReportData(int64_t value, std::unordered_map<std::string, std::string> payload)
1459 {
1460     payload["uid"] = std::to_string(appInfo_.appUid);
1461     payload["pid"] = std::to_string(appInfo_.appPid);
1462     uint32_t type = ResourceSchedule::ResType::RES_TYPE_REPORT_SCREEN_CAPTURE;
1463     ResourceSchedule::ResSchedClient::GetInstance().ReportData(type, value, payload);
1464 }
1465 
RegisterPrivateWindowListener()1466 void ScreenCaptureServer::RegisterPrivateWindowListener()
1467 {
1468     std::weak_ptr<ScreenCaptureServer> screenCaptureServer(shared_from_this());
1469     displayListener_ = new PrivateWindowListenerInScreenCapture(screenCaptureServer);
1470     DisplayManager::GetInstance().RegisterPrivateWindowListener(displayListener_);
1471 }
1472 
RegisterScreenConnectListener()1473 void ScreenCaptureServer::RegisterScreenConnectListener()
1474 {
1475     std::weak_ptr<ScreenCaptureServer> screenCaptureServer(shared_from_this());
1476     screenConnectListener_ = sptr<ScreenConnectListenerForSC>::MakeSptr(displayScreenId_, screenCaptureServer);
1477     MEDIA_LOGI("RegisterScreenConnectListener screenId: %{public}" PRIu64, displayScreenId_);
1478     ScreenManager::GetInstance().RegisterScreenListener(screenConnectListener_);
1479 }
1480 
SetMouseChangeListener(std::shared_ptr<MouseChangeListener> listener)1481 void ScreenCaptureServer::SetMouseChangeListener(std::shared_ptr<MouseChangeListener> listener)
1482 {
1483     mouseChangeListener_ = listener;
1484 }
1485 
GetMouseChangeListener()1486 std::shared_ptr<MouseChangeListener> ScreenCaptureServer::GetMouseChangeListener()
1487 {
1488     return mouseChangeListener_;
1489 }
1490 
RegisterMMISystemAbilityListener()1491 bool ScreenCaptureServer::RegisterMMISystemAbilityListener()
1492 {
1493     MEDIA_LOGI("RegisterMMISystemAbilityListener start.");
1494     if (mmiListener_ != nullptr) {
1495         MEDIA_LOGI("RegisterMMISystemAbilityListener already registered");
1496         return true;
1497     }
1498 
1499     auto abilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
1500     CHECK_AND_RETURN_RET_LOG(abilityManager != nullptr, false,
1501         "RegisterMMISystemAbilityListener abilityManager is nullptr.");
1502     std::weak_ptr<ScreenCaptureServer> screenCaptureServer(shared_from_this());
1503     sptr<ISystemAbilityStatusChange> listener(new (std::nothrow) MMISystemAbilityListener(screenCaptureServer));
1504     CHECK_AND_RETURN_RET_LOG(listener != nullptr, false, "create listener failed.");
1505     int32_t ret = abilityManager->SubscribeSystemAbility(MULTIMODAL_INPUT_SERVICE_ID, listener);
1506     CHECK_AND_RETURN_RET_LOG(ret == ERR_OK, false, "failed to subscribe systemAbility, ret:%{public}d", ret);
1507 
1508     mmiListener_ = listener;
1509     MEDIA_LOGI("RegisterMMISystemAbilityListener end.");
1510     return true;
1511 }
1512 
UnRegisterMMISystemAbilityListener()1513 bool ScreenCaptureServer::UnRegisterMMISystemAbilityListener()
1514 {
1515     MEDIA_LOGI("UnRegisterMMISystemAbilityListener start.");
1516     if (mmiListener_ == nullptr) {
1517         MEDIA_LOGI("mmiListener already unregistered.");
1518         return true;
1519     }
1520 
1521     auto abilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
1522     CHECK_AND_RETURN_RET_LOG(abilityManager != nullptr, false,
1523         "UnRegisterMMISystemAbilityListener abilityManager is nullptr.");
1524     int32_t ret = abilityManager->UnSubscribeSystemAbility(MULTIMODAL_INPUT_SERVICE_ID, mmiListener_);
1525     mmiListener_ = nullptr;
1526     CHECK_AND_RETURN_RET_LOG(ret == ERR_OK, false, "failed to unsubscribe systemAbility, ret:%{public}d", ret);
1527     MEDIA_LOGI("UnRegisterMMISystemAbilityListener end.");
1528     return true;
1529 }
1530 
MMISystemAbilityListener(std::weak_ptr<ScreenCaptureServer> screenCaptureServer)1531 MMISystemAbilityListener::MMISystemAbilityListener(std::weak_ptr<ScreenCaptureServer> screenCaptureServer)
1532 {
1533     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
1534     screenCaptureServer_ = screenCaptureServer;
1535 }
1536 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)1537 void MMISystemAbilityListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
1538 {
1539     MEDIA_LOGI("OnAddSystemAbility start.");
1540     auto scrServer = screenCaptureServer_.lock();
1541     CHECK_AND_RETURN_LOG(scrServer != nullptr, "screenCaptureServer is nullptr");
1542 
1543     int32_t ret = MMI::InputManager::GetInstance()->UnregisterDevListener("change",
1544         scrServer->GetMouseChangeListener());
1545     scrServer->SetMouseChangeListener(nullptr);
1546     CHECK_AND_RETURN_LOG(ret == MSERR_OK, "OnAddSystemAbility UnRegisterMMISystemAbilityListener failed");
1547 
1548     std::shared_ptr<MouseChangeListener> listener = std::make_shared<MouseChangeListener>(scrServer);
1549     ret = MMI::InputManager::GetInstance()->RegisterDevListener("change", listener);
1550     CHECK_AND_RETURN_LOG(ret == MSERR_OK, "OnAddSystemAbility RegisterDevListener failed");
1551     scrServer->SetMouseChangeListener(listener);
1552 
1553     scrServer->ShowCursorInner();
1554     MEDIA_LOGI("OnAddSystemAbility end.");
1555 }
1556 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)1557 void MMISystemAbilityListener::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
1558 {
1559     MEDIA_LOGI("OnRemoveSystemAbility success.");
1560 }
1561 
RegisterMouseChangeListener(std::string type)1562 int32_t ScreenCaptureServer::RegisterMouseChangeListener(std::string type)
1563 {
1564     MEDIA_LOGI("RegisterMouseChangeListener start.");
1565     if (mouseChangeListener_ != nullptr) {
1566         MEDIA_LOGI("RegisterMouseChangeListener mouseChangeListener already registered");
1567         return MSERR_OK;
1568     }
1569 
1570     std::weak_ptr<ScreenCaptureServer> screenCaptureServer(shared_from_this());
1571     mouseChangeListener_ = std::make_shared<MouseChangeListener>(screenCaptureServer);
1572     int32_t ret = MMI::InputManager::GetInstance()->RegisterDevListener(type, mouseChangeListener_);
1573     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "RegisterMouseChangeListener failed");
1574     MEDIA_LOGI("RegisterMouseChangeListener end.");
1575     return ret;
1576 }
1577 
UnRegisterMouseChangeListener(std::string type)1578 int32_t ScreenCaptureServer::UnRegisterMouseChangeListener(std::string type)
1579 {
1580     MEDIA_LOGI("UnRegisterMouseChangeListener start.");
1581     if (mouseChangeListener_ == nullptr) {
1582         MEDIA_LOGI("RegisterMouseChangeListener mouseChangeListener already unregistered");
1583         return MSERR_OK;
1584     }
1585 
1586     int32_t ret = MMI::InputManager::GetInstance()->UnregisterDevListener(type, mouseChangeListener_);
1587     mouseChangeListener_ = nullptr;
1588     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "UnRegisterMouseChangeListener failed");
1589     MEDIA_LOGI("UnRegisterMouseChangeListener end.");
1590     return ret;
1591 }
1592 
PostStartScreenCaptureSuccessAction()1593 void ScreenCaptureServer::PostStartScreenCaptureSuccessAction()
1594 {
1595     std::unordered_map<std::string, std::string> payload;
1596     int64_t value = ResourceSchedule::ResType::ScreenCaptureStatus::START_SCREEN_CAPTURE;
1597     ResSchedReportData(value, payload);
1598     captureState_ = AVScreenCaptureState::STARTED;
1599     AddStartedSessionIdList(this->sessionId_);
1600     MEDIA_LOGI("sessionId: %{public}d is pushed, now the size of startedSessionIDList_ is: %{public}d",
1601         this->sessionId_, static_cast<uint32_t>(ScreenCaptureServer::startedSessionIDList_.size()));
1602     SetSystemScreenRecorderStatus(true);
1603     ScreenCaptureMonitorServer::GetInstance()->CallOnScreenCaptureStarted(appInfo_.appPid);
1604     NotifyStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_STARTED);
1605     if (displayScreenId_ != SCREEN_ID_INVALID) {
1606         NotifyDisplaySelected(displayScreenId_);
1607     }
1608 }
1609 
IsFirstStartPidInstance(int32_t pid)1610 bool ScreenCaptureServer::IsFirstStartPidInstance(int32_t pid)
1611 {
1612     std::list<int32_t> pidList{};
1613     ScreenCaptureServer::GetRunningScreenCaptureInstancePid(pidList);
1614     std::list<int32_t>::iterator iter = find(pidList.begin(), pidList.end(), pid);
1615     if (iter == pidList.end()) {
1616         MEDIA_LOGD("ScreenCaptureServer::IsFirstStartPidInstance firstPid: %{public}d", pid);
1617         return true;
1618     }
1619     MEDIA_LOGD("ScreenCaptureServer::IsFirstStartPidInstance pid: %{public}d exists", pid);
1620     return false;
1621 }
1622 
FirstPidUpdatePrivacyUsingPermissionState(int32_t pid)1623 bool ScreenCaptureServer::FirstPidUpdatePrivacyUsingPermissionState(int32_t pid)
1624 {
1625     if (IsFirstStartPidInstance(pid)) {
1626         return UpdatePrivacyUsingPermissionState(START_VIDEO);
1627     }
1628     return true;
1629 }
1630 
NotifyStateChange(AVScreenCaptureStateCode stateCode)1631 void ScreenCaptureServer::NotifyStateChange(AVScreenCaptureStateCode stateCode)
1632 {
1633     if (screenCaptureCb_ != nullptr) {
1634         MEDIA_LOGD("NotifyStateChange stateCode: %{public}d", stateCode);
1635         screenCaptureCb_->OnStateChange(stateCode);
1636     }
1637 }
1638 
NotifyDisplaySelected(uint64_t displayId)1639 void ScreenCaptureServer::NotifyDisplaySelected(uint64_t displayId)
1640 {
1641     if (screenCaptureCb_ != nullptr) {
1642         MEDIA_LOGD("NotifyDisplaySelected displayId: (%{public}" PRIu64 ")", displayId);
1643         screenCaptureCb_->OnDisplaySelected(displayId);
1644     }
1645 }
1646 
PostStartScreenCapture(bool isSuccess)1647 void ScreenCaptureServer::PostStartScreenCapture(bool isSuccess)
1648 {
1649     CHECK_AND_RETURN(screenCaptureCb_ != nullptr);
1650     MediaTrace trace("ScreenCaptureServer::PostStartScreenCapture.");
1651     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " PostStartScreenCapture start, isSuccess:%{public}s, "
1652         "dataType:%{public}d.", FAKE_POINTER(this), isSuccess ? "true" : "false", captureConfig_.dataType);
1653     if (isSuccess) {
1654         MEDIA_LOGI("PostStartScreenCapture handle success");
1655 #ifdef SUPPORT_SCREEN_CAPTURE_WINDOW_NOTIFICATION
1656         if (isPrivacyAuthorityEnabled_ &&
1657             GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"]
1658                 .compare(appName_) != 0 && !isScreenCaptureAuthority_) {
1659             if (TryNotificationOnPostStartScreenCapture() == MSERR_UNKNOWN) {
1660                 return;
1661             }
1662         }
1663 #endif
1664         if (!FirstPidUpdatePrivacyUsingPermissionState(appInfo_.appPid)) {
1665             MEDIA_LOGE("UpdatePrivacyUsingPermissionState START failed, dataType:%{public}d", captureConfig_.dataType);
1666             captureState_ = AVScreenCaptureState::STARTED;
1667             screenCaptureCb_->OnError(ScreenCaptureErrorType::SCREEN_CAPTURE_ERROR_INTERNAL,
1668                 AVScreenCaptureErrorCode::SCREEN_CAPTURE_ERR_UNKNOWN);
1669             StopScreenCaptureInner(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INVLID);
1670             return;
1671         }
1672         MEDIA_LOGI("PostStartScreenCaptureSuccessAction START.");
1673         PostStartScreenCaptureSuccessAction();
1674     } else {
1675         MEDIA_LOGE("PostStartScreenCapture handle failure");
1676         if (isPrivacyAuthorityEnabled_) {
1677             screenCaptureCb_->OnError(ScreenCaptureErrorType::SCREEN_CAPTURE_ERROR_INTERNAL,
1678                 AVScreenCaptureErrorCode::SCREEN_CAPTURE_ERR_UNKNOWN);
1679         }
1680         StopScreenCaptureInner(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INVLID);
1681         isPrivacyAuthorityEnabled_ = false;
1682         isSurfaceMode_ = false;
1683         captureState_ = AVScreenCaptureState::STOPPED;
1684         SetErrorInfo(MSERR_UNKNOWN, "PostStartScreenCapture handle failure",
1685             StopReason::POST_START_SCREENCAPTURE_HANDLE_FAILURE, IsUserPrivacyAuthorityNeeded());
1686         return;
1687     }
1688     RegisterPrivateWindowListener();
1689     RegisterScreenConnectListener();
1690     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " PostStartScreenCapture end.", FAKE_POINTER(this));
1691 }
1692 
1693 #ifdef SUPPORT_SCREEN_CAPTURE_WINDOW_NOTIFICATION
TryStartNotification()1694 int32_t ScreenCaptureServer::TryStartNotification()
1695 {
1696     int32_t tryTimes;
1697     for (tryTimes = 1; tryTimes <= NOTIFICATION_MAX_TRY_NUM; tryTimes++) {
1698         int32_t ret = StartNotification();
1699         if (ret == MSERR_OK) {
1700             break;
1701         }
1702     }
1703     return tryTimes;
1704 }
1705 
TryNotificationOnPostStartScreenCapture()1706 int32_t ScreenCaptureServer::TryNotificationOnPostStartScreenCapture()
1707 {
1708     int32_t tryTimes = TryStartNotification();
1709     if (tryTimes > NOTIFICATION_MAX_TRY_NUM) {
1710         captureState_ = AVScreenCaptureState::STARTED;
1711         if (screenCaptureCb_ != nullptr) {
1712             screenCaptureCb_->OnError(ScreenCaptureErrorType::SCREEN_CAPTURE_ERROR_INTERNAL,
1713                 AVScreenCaptureErrorCode::SCREEN_CAPTURE_ERR_UNKNOWN);
1714         }
1715         StopScreenCaptureInner(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INVLID);
1716         return MSERR_UNKNOWN;
1717     }
1718     return MSERR_OK;
1719 }
1720 #endif
1721 
InitAudioCap(AudioCaptureInfo audioInfo)1722 int32_t ScreenCaptureServer::InitAudioCap(AudioCaptureInfo audioInfo)
1723 {
1724     MediaTrace trace("ScreenCaptureServer::InitAudioCap");
1725     std::lock_guard<std::mutex> lock(mutex_);
1726     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " InitAudioCap start, audioChannels:%{public}d, "
1727         "audioSampleRate:%{public}d, audioSource:%{public}d, state:%{public}d.", FAKE_POINTER(this),
1728         audioInfo.audioChannels, audioInfo.audioSampleRate, audioInfo.audioSource, audioInfo.state);
1729     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::CREATED, MSERR_INVALID_OPERATION,
1730         "InitAudioCap failed, capture is not CREATED, state:%{public}d", captureState_);
1731 
1732     int ret = CheckAudioCapInfo(audioInfo);
1733     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "InitAudioCap CheckAudioCapInfo failed, audioSource:%{public}d",
1734         audioInfo.audioSource);
1735     if (audioInfo.audioSource == AudioCaptureSourceType::SOURCE_DEFAULT ||
1736         audioInfo.audioSource == AudioCaptureSourceType::MIC) {
1737         captureConfig_.audioInfo.micCapInfo = audioInfo;
1738         statisticalEventInfo_.requireMic = true;
1739     } else if (audioInfo.audioSource == AudioCaptureSourceType::ALL_PLAYBACK ||
1740         audioInfo.audioSource == AudioCaptureSourceType::APP_PLAYBACK) {
1741         captureConfig_.audioInfo.innerCapInfo = audioInfo;
1742         avType_ = (avType_ == AVScreenCaptureAvType::INVALID_TYPE) ? AVScreenCaptureAvType::AUDIO_TYPE :
1743             AVScreenCaptureAvType::AV_TYPE;
1744     }
1745     MEDIA_LOGI("InitAudioCap success sampleRate:%{public}d, channels:%{public}d, source:%{public}d, state:%{public}d",
1746         audioInfo.audioSampleRate, audioInfo.audioChannels, audioInfo.audioSource, audioInfo.state);
1747     return MSERR_OK;
1748 }
1749 
InitVideoCap(VideoCaptureInfo videoInfo)1750 int32_t ScreenCaptureServer::InitVideoCap(VideoCaptureInfo videoInfo)
1751 {
1752     MediaTrace trace("ScreenCaptureServer::InitVideoCap");
1753     std::lock_guard<std::mutex> lock(mutex_);
1754     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::CREATED, MSERR_INVALID_OPERATION,
1755         "InitVideoCap failed, capture is not CREATED, state:%{public}d", captureState_);
1756 
1757     int ret = CheckVideoCapInfo(videoInfo);
1758     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "InitVideoCap CheckVideoCapInfo failed");
1759     captureConfig_.videoInfo.videoCapInfo = videoInfo;
1760     avType_ = (avType_ == AVScreenCaptureAvType::AUDIO_TYPE) ? AVScreenCaptureAvType::AV_TYPE :
1761         AVScreenCaptureAvType::VIDEO_TYPE;
1762     statisticalEventInfo_.videoResolution = std::to_string(videoInfo.videoFrameWidth) + " * " +
1763         std::to_string(videoInfo.videoFrameHeight);
1764     MEDIA_LOGI("InitVideoCap success width:%{public}d, height:%{public}d, source:%{public}d, state:%{public}d",
1765         videoInfo.videoFrameWidth, videoInfo.videoFrameHeight, videoInfo.videoSource, videoInfo.state);
1766     return MSERR_OK;
1767 }
1768 
InitRecorderInfo(std::shared_ptr<IRecorderService> & recorder,AudioCaptureInfo audioInfo)1769 int32_t ScreenCaptureServer::InitRecorderInfo(std::shared_ptr<IRecorderService> &recorder, AudioCaptureInfo audioInfo)
1770 {
1771     CHECK_AND_RETURN_RET_LOG(recorder != nullptr, MSERR_UNKNOWN, "init InitRecorderInfo failed");
1772     int32_t ret = MSERR_OK;
1773     if (captureConfig_.videoInfo.videoCapInfo.state != AVScreenCaptureParamValidationState::VALIDATION_IGNORE) {
1774         ret = recorder_->SetVideoSource(captureConfig_.videoInfo.videoCapInfo.videoSource, videoSourceId_);
1775         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetVideoSource failed");
1776     }
1777     ret = recorder->SetOutputFormat(fileFormat_); // Change to REC_CONFIGURED
1778     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetOutputFormat failed");
1779     ret = recorder->SetAudioEncoder(audioSourceId_, captureConfig_.audioInfo.audioEncInfo.audioCodecformat);
1780     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetAudioEncoder failed");
1781     ret = recorder->SetAudioSampleRate(audioSourceId_, audioInfo.audioSampleRate);
1782     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetAudioSampleRate failed");
1783     ret = recorder->SetAudioChannels(audioSourceId_, audioInfo.audioChannels);
1784     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetAudioChannels failed");
1785     ret = recorder->SetAudioEncodingBitRate(audioSourceId_, captureConfig_.audioInfo.audioEncInfo.audioBitrate);
1786     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetAudioEncodingBitRate failed");
1787     if (captureConfig_.videoInfo.videoCapInfo.state != AVScreenCaptureParamValidationState::VALIDATION_IGNORE) {
1788         ret = recorder->SetVideoEncoder(videoSourceId_, captureConfig_.videoInfo.videoEncInfo.videoCodec);
1789         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetVideoEncoder failed");
1790         ret = recorder->SetVideoSize(videoSourceId_, captureConfig_.videoInfo.videoCapInfo.videoFrameWidth,
1791             captureConfig_.videoInfo.videoCapInfo.videoFrameHeight);
1792         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetVideoSize failed");
1793         ret = recorder->SetVideoFrameRate(videoSourceId_, captureConfig_.videoInfo.videoEncInfo.videoFrameRate);
1794         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetVideoFrameRate failed");
1795         ret = recorder->SetVideoEncodingBitRate(videoSourceId_, captureConfig_.videoInfo.videoEncInfo.videoBitrate);
1796         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetVideoEncodingBitRate failed");
1797     }
1798     return MSERR_OK;
1799 }
1800 
InitRecorder()1801 int32_t ScreenCaptureServer::InitRecorder()
1802 {
1803     CHECK_AND_RETURN_RET_LOG(outputFd_ > 0, MSERR_INVALID_OPERATION, "the outputFd is invalid");
1804     MEDIA_LOGI("InitRecorder start");
1805     MediaTrace trace("ScreenCaptureServer::InitRecorder");
1806     recorder_ = Media::RecorderServer::Create();
1807     CHECK_AND_RETURN_RET_LOG(recorder_ != nullptr, MSERR_UNKNOWN, "init Recoder failed");
1808     ON_SCOPE_EXIT(0) {
1809         recorder_->Release();
1810     };
1811     int32_t ret;
1812     AudioCaptureInfo audioInfo;
1813     if (captureConfig_.audioInfo.innerCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID &&
1814         captureConfig_.audioInfo.micCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID) {
1815         MEDIA_LOGI("InitRecorder prepare to SetAudioDataSource");
1816         audioInfo = captureConfig_.audioInfo.innerCapInfo;
1817         audioSource_ = std::make_unique<AudioDataSource>(AVScreenCaptureMixMode::MIX_MODE, this);
1818         captureCallback_ = std::make_shared<ScreenRendererAudioStateChangeCallback>();
1819         audioSource_->SetAppPid(appInfo_.appPid);
1820         audioSource_->SetAppName(appName_);
1821         captureCallback_->SetAppName(appName_);
1822         captureCallback_->SetAudioSource(audioSource_);
1823         audioSource_->RegisterAudioRendererEventListener(appInfo_.appPid, captureCallback_);
1824         ret = recorder_->SetAudioDataSource(audioSource_, audioSourceId_);
1825         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetAudioDataSource failed");
1826     } else if (captureConfig_.audioInfo.innerCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID) {
1827         audioInfo = captureConfig_.audioInfo.innerCapInfo;
1828         MEDIA_LOGI("InitRecorder prepare to SetAudioSource inner");
1829         audioSource_ = std::make_unique<AudioDataSource>(AVScreenCaptureMixMode::INNER_MODE, this);
1830         ret = recorder_->SetAudioDataSource(audioSource_, audioSourceId_);
1831         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetAudioDataSource failed");
1832     } else if (captureConfig_.audioInfo.micCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID) {
1833         audioInfo = captureConfig_.audioInfo.micCapInfo;
1834         MEDIA_LOGI("InitRecorder prepare to SetAudioSource mic");
1835         audioSource_ = std::make_unique<AudioDataSource>(AVScreenCaptureMixMode::MIC_MODE, this);
1836         ret = recorder_->SetAudioDataSource(audioSource_, audioSourceId_);
1837         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetAudioDataSource failed");
1838     } else {
1839         MEDIA_LOGE("InitRecorder not VALIDATION_VALID");
1840         return MSERR_UNKNOWN;
1841     }
1842     MEDIA_LOGI("InitRecorder recorder SetAudioDataSource ret:%{public}d", ret);
1843     ret = InitRecorderInfo(recorder_, audioInfo);
1844     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "InitRecorderInfo failed");
1845     ret = recorder_->SetOutputFile(outputFd_);
1846     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "SetOutputFile failed");
1847     ret = recorder_->Prepare();
1848     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_UNKNOWN, "recorder Prepare failed");
1849     if (captureConfig_.videoInfo.videoCapInfo.state != AVScreenCaptureParamValidationState::VALIDATION_IGNORE) {
1850         consumer_ = recorder_->GetSurface(videoSourceId_);
1851         CHECK_AND_RETURN_RET_LOG(consumer_ != nullptr, MSERR_UNKNOWN, "recorder GetSurface failed");
1852     }
1853     CANCEL_SCOPE_EXIT_GUARD(0);
1854     MEDIA_LOGI("InitRecorder success");
1855     return MSERR_OK;
1856 }
1857 
UpdatePrivacyUsingPermissionState(VideoPermissionState state)1858 bool ScreenCaptureServer::UpdatePrivacyUsingPermissionState(VideoPermissionState state)
1859 {
1860     MediaTrace trace("ScreenCaptureServer::UpdatePrivacyUsingPermissionState");
1861     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " UpdatePrivacyUsingPermissionState start, "
1862         "state: %{public}d, uid: %{public}d", FAKE_POINTER(this), state, appInfo_.appUid);
1863     if (!IsUserPrivacyAuthorityNeeded()) {
1864         MEDIA_LOGI("Using Permission Ignored. state: %{public}d, uid: %{public}d", state, appInfo_.appUid);
1865         return true;
1866     }
1867 
1868     int res = 0;
1869     if (state == START_VIDEO) {
1870         res = PrivacyKit::StartUsingPermission(appInfo_.appTokenId, "ohos.permission.CAPTURE_SCREEN", appInfo_.appPid);
1871         if (res != 0) {
1872             MEDIA_LOGE("start using perm error");
1873             return false;
1874         }
1875         res = PrivacyKit::AddPermissionUsedRecord(appInfo_.appTokenId, "ohos.permission.CAPTURE_SCREEN", 1, 0);
1876         if (res != 0) {
1877             MEDIA_LOGE("add screen capture record error: %{public}d", res);
1878             return false;
1879         }
1880     } else if (state == STOP_VIDEO) {
1881         res = PrivacyKit::StopUsingPermission(appInfo_.appTokenId, "ohos.permission.CAPTURE_SCREEN", appInfo_.appPid);
1882         if (res != 0) {
1883             MEDIA_LOGE("stop using perm error");
1884             return false;
1885         }
1886     }
1887     return true;
1888 }
1889 
StartScreenCaptureInner(bool isPrivacyAuthorityEnabled)1890 int32_t ScreenCaptureServer::StartScreenCaptureInner(bool isPrivacyAuthorityEnabled)
1891 {
1892     MEDIA_LOGI("StartScreenCaptureInner S, appUid:%{public}d, appPid:%{public}d, isPrivacyAuthorityEnabled:%{public}d"
1893         ", isSurfaceMode:%{public}d, dataType:%{public}d", appInfo_.appUid, appInfo_.appPid, isPrivacyAuthorityEnabled,
1894         isSurfaceMode_, captureConfig_.dataType);
1895     MediaTrace trace("ScreenCaptureServer::StartScreenCaptureInner");
1896     int32_t ret = RegisterServerCallbacks();
1897     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "RegisterServerCallbacks failed");
1898 
1899     ret = CheckAllParams();
1900     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "StartScreenCaptureInner failed, invalid params");
1901 
1902     sptr<Rosen::Display> display = Rosen::DisplayManager::GetInstance().GetDefaultDisplaySync();
1903     CHECK_AND_RETURN_RET_LOG(display != nullptr, MSERR_UNKNOWN, "GetDefaultDisplaySync failed");
1904     density_ = display->GetVirtualPixelRatio();
1905 
1906     appName_ = GetClientBundleName(appInfo_.appUid);
1907     callingLabel_ = GetBundleResourceLabel(appName_);
1908     MEDIA_LOGD("StartScreenCaptureInner callingLabel: %{public}s", callingLabel_.c_str());
1909 
1910     isPrivacyAuthorityEnabled_ = isPrivacyAuthorityEnabled;
1911     captureState_ = AVScreenCaptureState::POPUP_WINDOW;
1912     isScreenCaptureAuthority_ = CheckPrivacyWindowSkipPermission();
1913     if (!isScreenCaptureAuthority_ && IsUserPrivacyAuthorityNeeded()) {
1914         ret = RequestUserPrivacyAuthority();
1915         if (ret != MSERR_OK) {
1916             captureState_ = AVScreenCaptureState::STOPPED;
1917             SetErrorInfo(ret, "StartScreenCaptureInner RequestUserPrivacyAuthority failed",
1918                 StopReason::REQUEST_USER_PRIVACY_AUTHORITY_FAILED, IsUserPrivacyAuthorityNeeded());
1919             MEDIA_LOGE("StartScreenCaptureInner RequestUserPrivacyAuthority failed");
1920             return ret;
1921         }
1922 
1923         if (isPrivacyAuthorityEnabled_ &&
1924             GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"]
1925                 .compare(appName_) != 0) {
1926             MEDIA_LOGI("Wait for user interactions to ALLOW/DENY capture");
1927             return MSERR_OK;
1928         }
1929         MEDIA_LOGI("privacy notification window not support, app has CAPTURE_SCREEN permission and go on");
1930     } else {
1931         MEDIA_LOGI("Privacy Authority granted automatically and go on"); // for root and skip permission
1932     }
1933 
1934     ret = OnStartScreenCapture();
1935     PostStartScreenCapture(ret == MSERR_OK);
1936 
1937     MEDIA_LOGI("StartScreenCaptureInner E, appUid:%{public}d, appPid:%{public}d", appInfo_.appUid, appInfo_.appPid);
1938     return ret;
1939 }
1940 
IsTelInCallSkipList()1941 bool ScreenCaptureServer::IsTelInCallSkipList()
1942 {
1943     MEDIA_LOGI("ScreenCaptureServer::IsTelInCallSkipList isCalledBySystemApp : %{public}d", isCalledBySystemApp_);
1944     if (isCalledBySystemApp_ &&
1945         GetScreenCaptureSystemParam()["const.multimedia.screencapture.hiviewcarebundlename"]
1946             .compare(appName_) == 0) {
1947         MEDIA_LOGI("ScreenCaptureServer::IsTelInCallSkipList true");
1948         return true;
1949     }
1950     return false;
1951 }
1952 
RegisterServerCallbacks()1953 int32_t ScreenCaptureServer::RegisterServerCallbacks()
1954 {
1955     std::weak_ptr<ScreenCaptureServer> wpScreenCaptureServer(shared_from_this());
1956     screenCaptureObserverCb_ = std::make_shared<ScreenCaptureObserverCallBack>(wpScreenCaptureServer);
1957 #ifdef SUPPORT_CALL
1958     uint64_t tokenId = IPCSkeleton::GetCallingFullTokenID();
1959     isCalledBySystemApp_ = OHOS::Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(tokenId);
1960     MEDIA_LOGI("ScreenCaptureServer::RegisterServerCallbacks isCalledBySystemApp: %{public}d", isCalledBySystemApp_);
1961     apiVersion_ = GetApiInfo(appInfo_.appUid);
1962     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " API Version is %{public}d", FAKE_POINTER(this),
1963         apiVersion_);
1964     if (apiVersion_ < STOPPED_BY_CALL_API_VERSION_ISOLATION && InCallObserver::GetInstance().IsInCall(true) &&
1965         !IsTelInCallSkipList()) {
1966         MEDIA_LOGI("ScreenCaptureServer Start InCall Abort");
1967         NotifyStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_STOPPED_BY_CALL);
1968         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_UNSUPPORT,
1969             "ScreenCaptureServer Start InCall Abort");
1970         return MSERR_UNSUPPORT;
1971     }
1972     MEDIA_LOGI("ScreenCaptureServer Start RegisterScreenCaptureCallBack");
1973     InCallObserver::GetInstance().RegisterInCallObserverCallBack(screenCaptureObserverCb_);
1974 #endif
1975     AccountObserver::GetInstance().RegisterAccountObserverCallBack(screenCaptureObserverCb_);
1976     return MSERR_OK;
1977 }
1978 
1979 #ifdef PC_STANDARD
CheckCaptureSpecifiedWindowForSelectWindow()1980 bool ScreenCaptureServer::CheckCaptureSpecifiedWindowForSelectWindow()
1981 {
1982     if (captureConfig_.captureMode == CAPTURE_SPECIFIED_WINDOW &&
1983         captureConfig_.videoInfo.videoCapInfo.taskIDs.size() < SELECT_WINDOW_MISSION_ID_NUM_MAX) {
1984             return true;
1985     }
1986     return false;
1987 }
1988 
SendConfigToUIParams(AAFwk::Want & want)1989 void ScreenCaptureServer::SendConfigToUIParams(AAFwk::Want& want)
1990 {
1991     want.SetParam("displayId", static_cast<int32_t>(captureConfig_.videoInfo.videoCapInfo.displayId));
1992     if (captureConfig_.captureMode == CAPTURE_SPECIFIED_SCREEN) {
1993         MEDIA_LOGI("CAPTURE_SPECIFIED_SCREEN, displayId: %{public}" PRId64 " missionId is dropped.",
1994             captureConfig_.videoInfo.videoCapInfo.displayId);
1995         captureConfig_.videoInfo.videoCapInfo.taskIDs = {};
1996         want.SetParam("missionId", -1); // -1 无效值
1997     } else if (CheckCaptureSpecifiedWindowForSelectWindow() &&
1998         captureConfig_.videoInfo.videoCapInfo.taskIDs.size() == 1) {
1999         MEDIA_LOGI("CAPTURE_SPECIFIED_WINDOW, missionId: %{public}d",
2000             *(captureConfig_.videoInfo.videoCapInfo.taskIDs.begin()));
2001         want.SetParam("missionId", *(captureConfig_.videoInfo.videoCapInfo.taskIDs.begin()));
2002     } else if (CheckCaptureSpecifiedWindowForSelectWindow() &&
2003         captureConfig_.videoInfo.videoCapInfo.taskIDs.size() == 0) {
2004         want.SetParam("missionId", -1); // -1 无效值
2005     }
2006 }
2007 #endif
2008 
StartPrivacyWindow()2009 int32_t ScreenCaptureServer::StartPrivacyWindow()
2010 {
2011     std::string comStr = "{\"ability.want.params.uiExtensionType\":\"sys/commonUI\",\"sessionId\":\"";
2012     comStr += std::to_string(sessionId_);
2013     comStr += "\",\"callerUid\":\"";
2014     comStr += std::to_string(appInfo_.appUid);
2015     comStr += "\",\"appLabel\":\"";
2016     comStr += callingLabel_;
2017     comStr += "\"}";
2018 
2019     AAFwk::Want want;
2020     ErrCode ret = ERR_INVALID_VALUE;
2021 #ifdef PC_STANDARD
2022     if (captureConfig_.captureMode == CAPTURE_SPECIFIED_SCREEN || CheckCaptureSpecifiedWindowForSelectWindow()) {
2023         AppExecFwk::ElementName element("",
2024             GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"],
2025             SELECT_ABILITY_NAME); // DeviceID
2026         want.SetElement(element);
2027         want.SetParam("params", comStr);
2028         want.SetParam("appLabel", callingLabel_);
2029         want.SetParam("sessionId", sessionId_);
2030         SendConfigToUIParams(want);
2031         ret = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
2032         MEDIA_LOGI("StartAbility end %{public}d, DeviceType : PC", ret);
2033     } else {
2034         want.SetElementName(GetScreenCaptureSystemParam()["const.multimedia.screencapture.dialogconnectionbundlename"],
2035             GetScreenCaptureSystemParam()["const.multimedia.screencapture.dialogconnectionabilityname"]);
2036         connection_ = sptr<UIExtensionAbilityConnection>(new (std::nothrow) UIExtensionAbilityConnection(comStr));
2037         ret = OHOS::AAFwk::ExtensionManagerClient::GetInstance().ConnectServiceExtensionAbility(want, connection_,
2038             nullptr, -1);
2039         MEDIA_LOGI("ConnectServiceExtensionAbility end %{public}d, DeviceType : PC", ret);
2040     }
2041 #else
2042     want.SetElementName(GetScreenCaptureSystemParam()["const.multimedia.screencapture.dialogconnectionbundlename"],
2043                         GetScreenCaptureSystemParam()["const.multimedia.screencapture.dialogconnectionabilityname"]);
2044     connection_ = sptr<UIExtensionAbilityConnection>(new (std::nothrow) UIExtensionAbilityConnection(comStr));
2045     ret = OHOS::AAFwk::ExtensionManagerClient::GetInstance().ConnectServiceExtensionAbility(want, connection_,
2046         nullptr, -1);
2047     MEDIA_LOGI("ConnectServiceExtensionAbility end %{public}d, Device : Phone", ret);
2048 #endif
2049     return ret;
2050 }
2051 
StartNotification()2052 int32_t ScreenCaptureServer::StartNotification()
2053 {
2054     int32_t result = NotificationHelper::SubscribeLocalLiveViewNotification(NOTIFICATION_SUBSCRIBER);
2055     MEDIA_LOGD("Screencapture service PublishNotification, result %{public}d", result);
2056     NotificationRequest request;
2057     localLiveViewContent_ = GetLocalLiveViewContent();
2058 
2059     std::shared_ptr<NotificationContent> content =
2060         std::make_shared<NotificationContent>(localLiveViewContent_);
2061 
2062     request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
2063     notificationId_ = sessionId_;
2064     request.SetNotificationId(notificationId_);
2065     request.SetContent(content);
2066     request.SetCreatorUid(AV_SCREEN_CAPTURE_SESSION_UID);
2067     request.SetOwnerUid(AV_SCREEN_CAPTURE_SESSION_UID);
2068     request.SetUnremovable(true);
2069     request.SetRemoveAllowed(false);
2070     request.SetInProgress(true);
2071 
2072     std::shared_ptr<PixelMap> pixelMapTotalSpr = GetPixelMap(ICON_PATH_NOTIFICATION);
2073     request.SetLittleIcon(pixelMapTotalSpr);
2074     request.SetBadgeIconStyle(NotificationRequest::BadgeStyle::LITTLE);
2075 
2076     result = NotificationHelper::PublishNotification(request);
2077     MEDIA_LOGI("Screencapture service UpdateMicrophoneEnabled uid %{public}d, result %{public}d",
2078         AV_SCREEN_CAPTURE_SESSION_UID, result);
2079     return result;
2080 }
2081 
GetStringByResourceName(const char * name)2082 std::string ScreenCaptureServer::GetStringByResourceName(const char* name)
2083 {
2084     std::string resourceContext;
2085     CHECK_AND_RETURN_RET_LOG(resourceManager_ != nullptr, resourceContext, "resourceManager is null");
2086     if (strcmp(name, NOTIFICATION_SCREEN_RECORDING_TITLE_ID) == 0) {
2087         resourceManager_->GetStringByName(NOTIFICATION_SCREEN_RECORDING_TITLE_ID, resourceContext);
2088         MEDIA_LOGD("get NOTIFICATION_SCREEN_RECORDING_TITLE_ID: %{public}s", resourceContext.c_str());
2089     } else {
2090         MEDIA_LOGE("resource name is error.");
2091     }
2092     return resourceContext;
2093 }
2094 
RefreshResConfig()2095 void ScreenCaptureServer::RefreshResConfig()
2096 {
2097     std::string language = Global::I18n::LocaleConfig::GetSystemLanguage();
2098     UErrorCode status = U_ZERO_ERROR;
2099     icu::Locale locale = icu::Locale::forLanguageTag(language, status);
2100     if (status != U_ZERO_ERROR) {
2101         MEDIA_LOGE("forLanguageTag failed, errCode:%{public}d", status);
2102     }
2103     if (resConfig_) {
2104         resConfig_->SetLocaleInfo(locale.getLanguage(), locale.getScript(), locale.getCountry());
2105         if (resourceManager_) {
2106             resourceManager_->UpdateResConfig(*resConfig_);
2107         }
2108     }
2109 }
2110 
InitResourceManager()2111 void ScreenCaptureServer::InitResourceManager()
2112 {
2113     if (resourceManager_ == nullptr) {
2114         resourceManager_ = Global::Resource::GetSystemResourceManagerNoSandBox();
2115     }
2116     if (resConfig_ == nullptr) {
2117         resConfig_ = Global::Resource::CreateResConfig();
2118     }
2119     RefreshResConfig();
2120 }
2121 
GetLocalLiveViewContent()2122 std::shared_ptr<NotificationLocalLiveViewContent> ScreenCaptureServer::GetLocalLiveViewContent()
2123 {
2124     std::shared_ptr<NotificationLocalLiveViewContent> localLiveViewContent =
2125         std::make_shared<NotificationLocalLiveViewContent>();
2126     localLiveViewContent->SetType(1);
2127     InitResourceManager();
2128     std::string recordingScreenTitleStr = GetStringByResourceName(NOTIFICATION_SCREEN_RECORDING_TITLE_ID);
2129     std::string from = "%s";
2130     std::string to = QUOTATION_MARKS_STRING + callingLabel_ + QUOTATION_MARKS_STRING;
2131     size_t startPos = recordingScreenTitleStr.find(from);
2132     if (startPos != std::string::npos) {
2133         recordingScreenTitleStr.replace(startPos, from.length(), to);
2134         liveViewText_ = recordingScreenTitleStr;
2135     }
2136     MEDIA_LOGD("GetLocalLiveViewContent liveViewText: %{public}s", liveViewText_.c_str());
2137     localLiveViewContent->SetText(liveViewText_);
2138 
2139     auto capsule = NotificationCapsule();
2140     capsule.SetBackgroundColor(BACK_GROUND_COLOR);
2141     capsulePxSize_ = static_cast<int32_t>(capsuleVpSize_ * density_);
2142     std::shared_ptr<PixelMap> pixelMapCapSpr = GetPixelMapSvg(ICON_PATH_CAPSULE_STOP, capsulePxSize_, capsulePxSize_);
2143     capsule.SetIcon(pixelMapCapSpr);
2144 
2145     localLiveViewContent->SetCapsule(capsule);
2146     localLiveViewContent->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::CAPSULE);
2147 
2148     auto countTime = NotificationTime();
2149     countTime.SetInitialTime(1);
2150     countTime.SetIsCountDown(false);
2151     countTime.SetIsPaused(false);
2152     countTime.SetIsInTitle(true);
2153 
2154     localLiveViewContent->SetTime(countTime);
2155     localLiveViewContent->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::TIME);
2156 
2157     auto basicButton = NotificationLocalLiveViewButton();
2158     basicButton.addSingleButtonName(BUTTON_NAME_STOP);
2159     std::shared_ptr<PixelMap> pixelMapStopSpr = GetPixelMap(ICON_PATH_STOP);
2160     basicButton.addSingleButtonIcon(pixelMapStopSpr);
2161 
2162     localLiveViewContent->SetButton(basicButton);
2163     localLiveViewContent->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::BUTTON);
2164     return localLiveViewContent;
2165 }
2166 
GetPixelMap(std::string path)2167 std::shared_ptr<PixelMap> ScreenCaptureServer::GetPixelMap(std::string path)
2168 {
2169     uint32_t errorCode = 0;
2170     SourceOptions opts;
2171     opts.formatHint = "image/png";
2172     std::unique_ptr<ImageSource> imageSource =
2173         ImageSource::CreateImageSource(path, opts, errorCode);
2174     CHECK_AND_RETURN_RET_LOG(imageSource != nullptr, nullptr, "GetPixelMap CreateImageSource failed");
2175     DecodeOptions decodeOpts;
2176     std::unique_ptr<PixelMap> pixelMap = imageSource->CreatePixelMap(decodeOpts, errorCode);
2177     std::shared_ptr<PixelMap> pixelMapSpr = std::move(pixelMap);
2178     return pixelMapSpr;
2179 }
2180 
GetPixelMapSvg(std::string path,int32_t width,int32_t height)2181 std::shared_ptr<PixelMap> ScreenCaptureServer::GetPixelMapSvg(std::string path, int32_t width, int32_t height)
2182 {
2183     uint32_t errorCode = 0;
2184     SourceOptions opts;
2185     opts.formatHint = "image/svg+xml";
2186     std::unique_ptr<ImageSource> imageSource =
2187         ImageSource::CreateImageSource(path, opts, errorCode);
2188     CHECK_AND_RETURN_RET_LOG(imageSource != nullptr, nullptr, "GetPixelMapSvg CreateImageSource failed");
2189     DecodeOptions decodeOpts;
2190     decodeOpts.desiredSize.width = width;
2191     decodeOpts.desiredSize.height = height;
2192     std::unique_ptr<PixelMap> pixelMap = imageSource->CreatePixelMap(decodeOpts, errorCode);
2193     std::shared_ptr<PixelMap> pixelMapSpr = std::move(pixelMap);
2194     return pixelMapSpr;
2195 }
2196 
UpdateMicrophoneEnabled()2197 void ScreenCaptureServer::UpdateMicrophoneEnabled()
2198 {
2199     UpdateLiveViewContent();
2200     NotificationRequest request;
2201 
2202     std::shared_ptr<NotificationContent> content =
2203         std::make_shared<NotificationContent>(localLiveViewContent_);
2204 
2205     request.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
2206     request.SetNotificationId(notificationId_);
2207     request.SetContent(content);
2208     request.SetCreatorUid(AV_SCREEN_CAPTURE_SESSION_UID);
2209     request.SetOwnerUid(AV_SCREEN_CAPTURE_SESSION_UID);
2210     request.SetUnremovable(true);
2211     request.SetInProgress(true);
2212 
2213     std::shared_ptr<PixelMap> pixelMapTotalSpr = GetPixelMap(ICON_PATH_CAPSULE_STOP);
2214     request.SetLittleIcon(pixelMapTotalSpr);
2215     request.SetBadgeIconStyle(NotificationRequest::BadgeStyle::LITTLE);
2216 
2217     int32_t result = NotificationHelper::PublishNotification(request);
2218     MEDIA_LOGI("Screencapture service UpdateMicrophoneEnabled uid %{public}d, result %{public}d",
2219         AV_SCREEN_CAPTURE_SESSION_UID, result);
2220     micCount_.store(micCount_.load() + 1);
2221 }
2222 
UpdateLiveViewContent()2223 void ScreenCaptureServer::UpdateLiveViewContent()
2224 {
2225     localLiveViewContent_->SetType(1);
2226     localLiveViewContent_->SetText(liveViewText_);
2227 
2228     auto capsule = NotificationCapsule();
2229     capsule.SetBackgroundColor(BACK_GROUND_COLOR);
2230     std::shared_ptr<PixelMap> pixelMapCapSpr = GetPixelMap(ICON_PATH_CAPSULE_STOP);
2231     capsule.SetIcon(pixelMapCapSpr);
2232 
2233     localLiveViewContent_->SetCapsule(capsule);
2234     localLiveViewContent_->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::CAPSULE);
2235 
2236     auto countTime = NotificationTime();
2237     countTime.SetIsCountDown(false);
2238     countTime.SetIsPaused(false);
2239     countTime.SetIsInTitle(true);
2240 
2241     localLiveViewContent_->SetTime(countTime);
2242     localLiveViewContent_->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::TIME);
2243 
2244     auto basicButton = NotificationLocalLiveViewButton();
2245     basicButton.addSingleButtonName(BUTTON_NAME_MIC);
2246     if (micCount_.load() % MICROPHONE_STATE_COUNT == MICROPHONE_OFF) {
2247         std::shared_ptr<PixelMap> pixelMapSpr = GetPixelMapSvg(ICON_PATH_MIC_OFF, SVG_HEIGHT, SVG_WIDTH);
2248         basicButton.addSingleButtonIcon(pixelMapSpr);
2249     } else {
2250         std::shared_ptr<PixelMap> pixelMapSpr = GetPixelMapSvg(ICON_PATH_MIC, SVG_HEIGHT, SVG_WIDTH);
2251         basicButton.addSingleButtonIcon(pixelMapSpr);
2252     }
2253 
2254     basicButton.addSingleButtonName(BUTTON_NAME_STOP);
2255     std::shared_ptr<PixelMap> pixelMapStopSpr = GetPixelMap(ICON_PATH_STOP);
2256     basicButton.addSingleButtonIcon(pixelMapStopSpr);
2257 
2258     localLiveViewContent_->SetButton(basicButton);
2259     localLiveViewContent_->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::BUTTON);
2260 }
2261 
GetDumpFlag()2262 void ScreenCaptureServer::GetDumpFlag()
2263 {
2264     const std::string dumpTag = "sys.media.screenCapture.dump.enable";
2265     std::string dumpEnable;
2266     int32_t dumpRes = OHOS::system::GetStringParameter(dumpTag, dumpEnable, "false");
2267     isDump_ = (dumpEnable == "true");
2268     MEDIA_LOGI("get dump flag, dumpRes: %{public}d, isDump_: %{public}d", dumpRes, isDump_);
2269 }
2270 
StartScreenCapture(bool isPrivacyAuthorityEnabled)2271 int32_t ScreenCaptureServer::StartScreenCapture(bool isPrivacyAuthorityEnabled)
2272 {
2273     MediaTrace trace("ScreenCaptureServer::StartScreenCapture");
2274     std::lock_guard<std::mutex> lock(mutex_);
2275     startTime_ = GetCurrentMillisecond();
2276     statisticalEventInfo_.enableMic = isMicrophoneSwitchTurnOn_;
2277     GetDumpFlag();
2278     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartScreenCapture start, "
2279         "isPrivacyAuthorityEnabled:%{public}s, captureState:%{public}d.",
2280         FAKE_POINTER(this), isPrivacyAuthorityEnabled ? "true" : "false", captureState_);
2281     CHECK_AND_RETURN_RET_LOG(
2282         captureState_ == AVScreenCaptureState::CREATED || captureState_ == AVScreenCaptureState::STOPPED,
2283         MSERR_INVALID_OPERATION, "StartScreenCapture failed, not in CREATED or STOPPED, state:%{public}d",
2284         captureState_);
2285     MEDIA_LOGI("StartScreenCapture isPrivacyAuthorityEnabled:%{public}d", isPrivacyAuthorityEnabled);
2286     isSurfaceMode_ = false;
2287     return StartScreenCaptureInner(isPrivacyAuthorityEnabled);
2288 }
2289 
StartScreenCaptureWithSurface(sptr<Surface> surface,bool isPrivacyAuthorityEnabled)2290 int32_t ScreenCaptureServer::StartScreenCaptureWithSurface(sptr<Surface> surface, bool isPrivacyAuthorityEnabled)
2291 {
2292     std::lock_guard<std::mutex> lock(mutex_);
2293     CHECK_AND_RETURN_RET_LOG(
2294         captureState_ == AVScreenCaptureState::CREATED || captureState_ == AVScreenCaptureState::STOPPED,
2295         MSERR_INVALID_OPERATION, "StartScreenCaptureWithSurface failed, not in CREATED or STOPPED, state:%{public}d",
2296         captureState_);
2297     MEDIA_LOGI("StartScreenCaptureWithSurface isPrivacyAuthorityEnabled:%{public}d", isPrivacyAuthorityEnabled);
2298     if (surface == nullptr) {
2299         MEDIA_LOGE("surface is nullptr");
2300         return MSERR_INVALID_OPERATION;
2301     }
2302     surface_ = surface;
2303     isSurfaceMode_ = true;
2304     dataMode_ = AVScreenCaptureDataMode::SUFFACE_MODE;
2305     return StartScreenCaptureInner(isPrivacyAuthorityEnabled);
2306 }
2307 
StartStreamVideoCapture()2308 int32_t ScreenCaptureServer::StartStreamVideoCapture()
2309 {
2310     MediaTrace trace("ScreenCaptureServer::StartStreamVideoCapture");
2311     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartStreamVideoCapture start, state:%{public}d, "
2312         "dataType:%{public}d, isSurfaceMode:%{public}s.", FAKE_POINTER(this),
2313         captureConfig_.videoInfo.videoCapInfo.state, captureConfig_.dataType, isSurfaceMode_ ? "true" : "false");
2314     if (captureConfig_.videoInfo.videoCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_IGNORE) {
2315         MEDIA_LOGI("StartStreamVideoCapture is ignored");
2316         return MSERR_OK;
2317     }
2318     CHECK_AND_RETURN_RET_LOG(
2319         captureConfig_.videoInfo.videoCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID,
2320         MSERR_INVALID_VAL, "StartStreamVideoCapture failed, invalid param, dataType:%{public}d",
2321         captureConfig_.dataType);
2322 
2323     int32_t ret = StartStreamHomeVideoCapture();
2324     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret,
2325         "StartStreamHomeVideoCapture failed, isSurfaceMode:%{public}d, dataType:%{public}d",
2326         isSurfaceMode_, captureConfig_.dataType);
2327     MEDIA_LOGI("StartStreamVideoCapture end.");
2328     return MSERR_OK;
2329 }
2330 
StartStreamHomeVideoCapture()2331 int32_t ScreenCaptureServer::StartStreamHomeVideoCapture()
2332 {
2333     MediaTrace trace("ScreenCaptureServer::StartStreamHomeVideoCapture");
2334     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartStreamHomeVideoCapture start, "
2335         "isSurfaceMode: %{public}s.", FAKE_POINTER(this), isSurfaceMode_ ? "true" : "false");
2336     std::string virtualScreenName = "screen_capture";
2337     if (isSurfaceMode_) {
2338         int32_t ret = CreateVirtualScreen(virtualScreenName, surface_);
2339         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "create virtual screen with input surface failed");
2340         return MSERR_OK;
2341     }
2342 
2343     ON_SCOPE_EXIT(0) {
2344         DestroyVirtualScreen();
2345         if (consumer_ != nullptr && surfaceCb_ != nullptr) {
2346             consumer_->UnregisterConsumerListener();
2347         }
2348         consumer_ = nullptr;
2349         if (surfaceCb_ != nullptr) {
2350             (static_cast<ScreenCapBufferConsumerListener *>(surfaceCb_.GetRefPtr()))->StopBufferThread();
2351             (static_cast<ScreenCapBufferConsumerListener *>(surfaceCb_.GetRefPtr()))->Release();
2352             surfaceCb_ = nullptr;
2353         }
2354     };
2355     consumer_ = OHOS::Surface::CreateSurfaceAsConsumer();
2356     CHECK_AND_RETURN_RET_LOG(consumer_ != nullptr, MSERR_UNKNOWN, "CreateSurfaceAsConsumer failed");
2357     MEDIA_LOGI("ScreenCaptureServer consumer_ BUFFER_USAGE_CPU_READ BUFFER_USAGE_MEM_MMZ_CACHE S");
2358     consumer_->SetDefaultUsage(BUFFER_USAGE_CPU_READ | BUFFER_USAGE_MEM_MMZ_CACHE);
2359     MEDIA_LOGI("ScreenCaptureServer consumer_ BUFFER_USAGE_CPU_READ BUFFER_USAGE_MEM_MMZ_CACHE E");
2360     auto producer = consumer_->GetProducer();
2361     CHECK_AND_RETURN_RET_LOG(producer != nullptr, MSERR_UNKNOWN, "GetProducer failed");
2362     auto producerSurface = OHOS::Surface::CreateSurfaceAsProducer(producer);
2363     CHECK_AND_RETURN_RET_LOG(producerSurface != nullptr, MSERR_UNKNOWN, "CreateSurfaceAsProducer failed");
2364     surfaceCb_ = OHOS::sptr<ScreenCapBufferConsumerListener>::MakeSptr(consumer_, screenCaptureCb_);
2365     CHECK_AND_RETURN_RET_LOG(surfaceCb_ != nullptr, MSERR_UNKNOWN, "MakeSptr surfaceCb_ failed");
2366     consumer_->RegisterConsumerListener(surfaceCb_);
2367     MEDIA_LOGD("StartStreamHomeVideoCapture producerSurface: %{public}" PRIu64, producerSurface->GetUniqueId());
2368     int32_t ret = CreateVirtualScreen(virtualScreenName, producerSurface);
2369     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "create virtual screen without input surface failed");
2370     CANCEL_SCOPE_EXIT_GUARD(0);
2371     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartStreamHomeVideoCapture OK.", FAKE_POINTER(this));
2372     MEDIA_LOGI("ScreenCaptureServer:StartStreamHomeVideoCapture surfaceCb_: 0x%{public}06" PRIXPTR,
2373         FAKE_POINTER(surfaceCb_.GetRefPtr()));
2374     if (!isSurfaceMode_) {
2375         ret = (static_cast<ScreenCapBufferConsumerListener*>(surfaceCb_.GetRefPtr()))->StartBufferThread();
2376         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "start buffer thread failed");
2377     }
2378     return MSERR_OK;
2379 }
2380 
CreateVirtualScreen(const std::string & name,sptr<OHOS::Surface> consumer)2381 int32_t ScreenCaptureServer::CreateVirtualScreen(const std::string &name, sptr<OHOS::Surface> consumer)
2382 {
2383     MediaTrace trace("ScreenCaptureServer::CreateVirtualScreen");
2384     MEDIA_LOGI("CreateVirtualScreen Start");
2385     isConsumerStart_ = false;
2386     VirtualScreenOption virScrOption = InitVirtualScreenOption(name, consumer);
2387     sptr<Rosen::Display> display = Rosen::DisplayManager::GetInstance().GetDefaultDisplaySync();
2388     if (display != nullptr) {
2389         MEDIA_LOGI("get displayInfo width:%{public}d,height:%{public}d, density:%{public}f", display->GetWidth(),
2390                    display->GetHeight(), display->GetVirtualPixelRatio());
2391         virScrOption.density_ = display->GetVirtualPixelRatio();
2392     }
2393     if (missionIds_.size() > 0 && captureConfig_.captureMode == CAPTURE_SPECIFIED_WINDOW) {
2394         virScrOption.missionIds_ = missionIds_;
2395     } else if (captureConfig_.videoInfo.videoCapInfo.taskIDs.size() > 0 &&
2396         captureConfig_.captureMode == CAPTURE_SPECIFIED_WINDOW) {
2397         GetMissionIds(missionIds_);
2398         virScrOption.missionIds_ = missionIds_;
2399     }
2400     virtualScreenId_ = ScreenManager::GetInstance().CreateVirtualScreen(virScrOption);
2401     CHECK_AND_RETURN_RET_LOG(virtualScreenId_ >= 0, MSERR_UNKNOWN, "CreateVirtualScreen failed, invalid screenId");
2402 
2403     if (!showCursor_) {
2404         MEDIA_LOGI("CreateVirtualScreen without cursor");
2405         int32_t ret = ShowCursorInner();
2406         if (ret != MSERR_OK) {
2407             MEDIA_LOGE("CreateVirtualScreen SetVirtualScreenBlackList failed");
2408         }
2409     }
2410     MEDIA_LOGI("CreateVirtualScreen success, screenId: %{public}" PRIu64, virtualScreenId_);
2411     return PrepareVirtualScreenMirror();
2412 }
2413 
PrepareVirtualScreenMirror()2414 int32_t ScreenCaptureServer::PrepareVirtualScreenMirror()
2415 {
2416     for (size_t i = 0; i < contentFilter_.windowIDsVec.size(); i++) {
2417         MEDIA_LOGD("After CreateVirtualScreen windowIDsVec value :%{public}" PRIu64, contentFilter_.windowIDsVec[i]);
2418     }
2419     SetScreenScaleMode();
2420     Rosen::DisplayManager::GetInstance().SetVirtualScreenBlackList(virtualScreenId_, contentFilter_.windowIDsVec,
2421         surfaceIdList_);
2422     MEDIA_LOGI("PrepareVirtualScreenMirror screenId: %{public}" PRIu64, virtualScreenId_);
2423     auto screen = ScreenManager::GetInstance().GetScreenById(virtualScreenId_);
2424     if (screen == nullptr) {
2425         MEDIA_LOGE("GetScreenById failed");
2426         DestroyVirtualScreen();
2427         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_UNKNOWN,
2428             "GetScreenById failed");
2429         return MSERR_UNKNOWN;
2430     }
2431     if (canvasRotation_) {
2432         SetCanvasRotationInner();
2433     }
2434     SkipPrivacyModeInner();
2435     int32_t ret = MakeVirtualScreenMirror();
2436     if (ret != MSERR_OK) {
2437         MEDIA_LOGE("MakeVirtualScreenMirror failed");
2438         DestroyVirtualScreen();
2439         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_UNKNOWN,
2440             "MakeVirtualScreenMirror failed");
2441         return MSERR_UNKNOWN;
2442     }
2443     isConsumerStart_ = true;
2444     return MSERR_OK;
2445 }
2446 
GetDisplayIdOfWindows(uint64_t displayId)2447 uint64_t ScreenCaptureServer::GetDisplayIdOfWindows(uint64_t displayId)
2448 {
2449     uint64_t defaultDisplayIdValue = displayId;
2450     if (missionIds_.size() > 0) {
2451         std::unordered_map<uint64_t, uint64_t> windowDisplayIdMap;
2452         auto ret = WindowManager::GetInstance().GetDisplayIdByWindowId(missionIds_, windowDisplayIdMap);
2453         MEDIA_LOGI("MakeVirtualScreenMirror 0x%{public}06" PRIXPTR
2454             "GetWindowDisplayIds ret:%{public}d", FAKE_POINTER(this), ret);
2455         for (const auto& pair : windowDisplayIdMap) {
2456             MEDIA_LOGD("MakeVirtualScreenMirror 0x%{public}06" PRIXPTR " WindowId:%{public}" PRIu64
2457                 " in DisplayId:%{public}" PRIu64, FAKE_POINTER(this), pair.first, pair.second);
2458             defaultDisplayIdValue = pair.second;
2459         }
2460         MEDIA_LOGI("MakeVirtualScreenMirror 0x%{public}06" PRIXPTR
2461             " For Specific Window %{public}" PRIu64, FAKE_POINTER(this), defaultDisplayIdValue);
2462     }
2463     return defaultDisplayIdValue;
2464 }
2465 
2466 #ifdef PC_STANDARD
IsHopper()2467 bool ScreenCaptureServer::IsHopper()
2468 {
2469     std::string foldScreenFlag = system::GetParameter("const.window.foldscreen.type", "0,0,0,0");
2470     if (foldScreenFlag.empty()) {
2471         MEDIA_LOGI("foldscreen type is empty");
2472         return false;
2473     }
2474     MEDIA_LOGI("foldscreen type is %{public}s", foldScreenFlag.c_str());
2475     return foldScreenFlag[0] == '5';
2476 }
2477 #endif
2478 
MakeVirtualScreenMirrorForWindow(sptr<Rosen::Display> defaultDisplay,std::vector<ScreenId> mirrorIds)2479 int32_t ScreenCaptureServer::MakeVirtualScreenMirrorForWindow(sptr<Rosen::Display> defaultDisplay,
2480     std::vector<ScreenId> mirrorIds)
2481 {
2482     ScreenId mirrorGroup = defaultDisplay->GetScreenId();
2483     uint64_t defaultDisplayId = GetDisplayIdOfWindows(defaultDisplay->GetScreenId());
2484     DMError ret = ScreenManager::GetInstance().MakeMirror(defaultDisplayId, mirrorIds, mirrorGroup);
2485     CHECK_AND_RETURN_RET_LOG(ret == DMError::DM_OK, MSERR_UNKNOWN,
2486         "MakeVirtualScreenMirror failed, captureMode:%{public}d, ret:%{public}d", captureConfig_.captureMode, ret);
2487     MEDIA_LOGI("MakeVirtualScreenMirror window screen success, screenId:%{public}" PRIu64, defaultDisplayId);
2488     displayScreenId_ = defaultDisplayId;
2489     return MSERR_OK;
2490 }
2491 
MakeVirtualScreenMirrorForHomeScreen(sptr<Rosen::Display> defaultDisplay,std::vector<ScreenId> mirrorIds)2492 int32_t ScreenCaptureServer::MakeVirtualScreenMirrorForHomeScreen(sptr<Rosen::Display> defaultDisplay,
2493     std::vector<ScreenId> mirrorIds)
2494 {
2495     ScreenId mirrorGroup = defaultDisplay->GetScreenId();
2496     MEDIA_LOGI("MakeVirtualScreenMirror DefaultDisplay, screenId:%{public}" PRIu64, mirrorGroup);
2497     DMError ret = ScreenManager::GetInstance().MakeMirror(defaultDisplay->GetScreenId(), mirrorIds, mirrorGroup);
2498     CHECK_AND_RETURN_RET_LOG(ret == DMError::DM_OK, MSERR_UNKNOWN,
2499         "MakeVirtualScreenMirror failed, captureMode:%{public}d, ret:%{public}d", captureConfig_.captureMode, ret);
2500     displayScreenId_ = defaultDisplay->GetScreenId();
2501     MEDIA_LOGI("MakeVirtualScreenMirror default screen success, screenId:%{public}" PRIu64,
2502         defaultDisplay->GetScreenId());
2503     return MSERR_OK;
2504 }
2505 
2506 #ifdef PC_STANDARD
MakeVirtualScreenMirrorForHomeScreenForHopper(sptr<Rosen::Display> defaultDisplay,std::vector<ScreenId> mirrorIds)2507 int32_t ScreenCaptureServer::MakeVirtualScreenMirrorForHomeScreenForHopper(sptr<Rosen::Display> defaultDisplay,
2508     std::vector<ScreenId> mirrorIds)
2509 {
2510     ScreenId mirrorGroup = defaultDisplay->GetScreenId();
2511     MEDIA_LOGI("MakeVirtualScreenMirror DefaultDisplay, screenId:%{public}" PRIu64, mirrorGroup);
2512     DMError ret = ScreenManager::GetInstance().MakeMirrorForRecord(defaultDisplay->GetScreenId(), mirrorIds,
2513         mirrorGroup);
2514     CHECK_AND_RETURN_RET_LOG(ret == DMError::DM_OK, MSERR_UNKNOWN,
2515         "MakeMirrorForRecord failed, captureMode:%{public}d, ret:%{public}d", captureConfig_.captureMode, ret);
2516     displayScreenId_ = defaultDisplay->GetScreenId();
2517     MEDIA_LOGI("MakeVirtualScreenMirror default screen success, screenId:%{public}" PRIu64,
2518         defaultDisplay->GetScreenId());
2519     return MSERR_OK;
2520 }
2521 #endif
2522 
MakeVirtualScreenMirrorForSpecifiedScreen(sptr<Rosen::Display> defaultDisplay,std::vector<ScreenId> mirrorIds)2523 int32_t ScreenCaptureServer::MakeVirtualScreenMirrorForSpecifiedScreen(sptr<Rosen::Display> defaultDisplay,
2524     std::vector<ScreenId> mirrorIds)
2525 {
2526     std::vector<sptr<Screen>> screens;
2527     DMError ret = ScreenManager::GetInstance().GetAllScreens(screens);
2528     CHECK_AND_RETURN_RET_LOG(screens.size() > 0, MSERR_UNKNOWN,
2529         "MakeVirtualScreenMirror failed to GetAllScreens, ret:%{public}d", ret);
2530     for (uint32_t i = 0; i < screens.size() ; i++) {
2531         if (screens[i]->GetId() == captureConfig_.videoInfo.videoCapInfo.displayId) {
2532             ScreenId mirrorGroup = defaultDisplay->GetScreenId();
2533             ret = ScreenManager::GetInstance().MakeMirror(screens[i]->GetId(), mirrorIds, mirrorGroup);
2534             CHECK_AND_RETURN_RET_LOG(ret == DMError::DM_OK, MSERR_UNKNOWN,
2535                 "MakeVirtualScreenMirror failed to MakeMirror for CAPTURE_SPECIFIED_SCREEN, ret:%{public}d", ret);
2536             displayScreenId_ = screens[i]->GetId();
2537             MEDIA_LOGI("MakeVirtualScreenMirror extend screen success, screenId:%{public}" PRIu64,
2538                 captureConfig_.videoInfo.videoCapInfo.displayId);
2539             return MSERR_OK;
2540         }
2541     }
2542     MEDIA_LOGE("MakeVirtualScreenMirror failed to find screenId:%{public}" PRIu64,
2543         captureConfig_.videoInfo.videoCapInfo.displayId);
2544     FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_UNKNOWN,
2545         "MakeVirtualScreenMirror failed to find screenId");
2546     return MSERR_UNKNOWN;
2547 }
2548 
2549 #ifdef PC_STANDARD
MakeVirtualScreenMirrorForSpecifiedScreenForHopper(sptr<Rosen::Display> defaultDisplay,std::vector<ScreenId> mirrorIds)2550 int32_t ScreenCaptureServer::MakeVirtualScreenMirrorForSpecifiedScreenForHopper(sptr<Rosen::Display> defaultDisplay,
2551     std::vector<ScreenId> mirrorIds)
2552 {
2553     std::vector<Rosen::DisplayId> allDisplayIds = Rosen::DisplayManager::GetInstance().GetAllDisplayIds();
2554     CHECK_AND_RETURN_RET_LOG(allDisplayIds.size() > 0, MSERR_UNKNOWN,
2555         "MakeVirtualScreenMirror failed to GetAllDisplayIds, allDisplayIds is empty");
2556     for (uint32_t i = 0; i < allDisplayIds.size() ; i++) {
2557         if (allDisplayIds[i] == captureConfig_.videoInfo.videoCapInfo.displayId) {
2558             ScreenId mirrorGroup = defaultDisplay->GetScreenId();
2559             DMError ret = ScreenManager::GetInstance().MakeMirrorForRecord(allDisplayIds[i], mirrorIds, mirrorGroup);
2560             CHECK_AND_RETURN_RET_LOG(ret == DMError::DM_OK, MSERR_UNKNOWN,
2561                 "MakeVirtualScreenMirror failed to MakeMirrorForRecord for CAPTURE_SPECIFIED_SCREEN, ret:%{public}d",
2562                 ret);
2563             displayScreenId_ = allDisplayIds[i];
2564             MEDIA_LOGI("MakeVirtualScreenMirrorForHopper extend screen success, displayId:%{public}" PRIu64,
2565                 captureConfig_.videoInfo.videoCapInfo.displayId);
2566             return MSERR_OK;
2567         }
2568     }
2569     MEDIA_LOGE("MakeVirtualScreenMirror failed to find displayId:%{public}" PRIu64,
2570         captureConfig_.videoInfo.videoCapInfo.displayId);
2571     FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_UNKNOWN,
2572         "MakeVirtualScreenMirror failed to find displayId");
2573     return MSERR_UNKNOWN;
2574 }
2575 #endif
2576 
MakeVirtualScreenMirror()2577 int32_t ScreenCaptureServer::MakeVirtualScreenMirror()
2578 {
2579     MediaTrace trace("ScreenCaptureServer::MakeVirtualScreenMirror");
2580     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " MakeVirtualScreenMirror start.", FAKE_POINTER(this));
2581     CHECK_AND_RETURN_RET_LOG(virtualScreenId_ >= 0 && virtualScreenId_ != SCREEN_ID_INVALID, MSERR_UNKNOWN,
2582         "MakeVirtualScreenMirror failed, invalid screenId");
2583     std::vector<ScreenId> mirrorIds;
2584     mirrorIds.push_back(virtualScreenId_);
2585     sptr<Rosen::Display> defaultDisplay = Rosen::DisplayManager::GetInstance().GetDefaultDisplaySync();
2586     CHECK_AND_RETURN_RET_LOG(defaultDisplay != nullptr, MSERR_UNKNOWN,
2587         "MakeVirtualScreenMirror GetDefaultDisplaySync failed");
2588     if (captureConfig_.captureMode == CAPTURE_SPECIFIED_WINDOW) {
2589         return MakeVirtualScreenMirrorForWindow(defaultDisplay, mirrorIds);
2590     }
2591 #ifdef PC_STANDARD
2592     if (IsHopper()) {
2593         if (captureConfig_.captureMode != CAPTURE_SPECIFIED_SCREEN) {
2594             return MakeVirtualScreenMirrorForHomeScreenForHopper(defaultDisplay, mirrorIds);
2595         }
2596         return MakeVirtualScreenMirrorForSpecifiedScreenForHopper(defaultDisplay, mirrorIds);
2597     }
2598 #endif
2599     if (captureConfig_.captureMode != CAPTURE_SPECIFIED_SCREEN) {
2600         return MakeVirtualScreenMirrorForHomeScreen(defaultDisplay, mirrorIds);
2601     }
2602     return MakeVirtualScreenMirrorForSpecifiedScreen(defaultDisplay, mirrorIds);
2603 }
2604 
DestroyVirtualScreen()2605 void ScreenCaptureServer::DestroyVirtualScreen()
2606 {
2607     MediaTrace trace("ScreenCaptureServer::DestroyVirtualScreen");
2608     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " DestroyVirtualScreen start.", FAKE_POINTER(this));
2609     if (virtualScreenId_ >= 0 && virtualScreenId_ != SCREEN_ID_INVALID) {
2610         if (isConsumerStart_) {
2611             std::vector<ScreenId> screenIds;
2612             screenIds.push_back(virtualScreenId_);
2613             ScreenManager::GetInstance().StopMirror(screenIds);
2614         }
2615         ScreenManager::GetInstance().DestroyVirtualScreen(virtualScreenId_);
2616         virtualScreenId_ = SCREEN_ID_INVALID;
2617         isConsumerStart_ = false;
2618     }
2619     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " DestroyVirtualScreen end.", FAKE_POINTER(this));
2620 }
2621 
CloseFd()2622 void ScreenCaptureServer::CloseFd()
2623 {
2624     MediaTrace trace("ScreenCaptureServer::CloseFd");
2625     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " CloseFd, fd is %{public}d", FAKE_POINTER(this),
2626         outputFd_);
2627     if (outputFd_ >= 0) {
2628         (void)::close(outputFd_);
2629         outputFd_ = -1;
2630     }
2631     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " CloseFd end.", FAKE_POINTER(this));
2632 }
2633 
InitVirtualScreenOption(const std::string & name,sptr<OHOS::Surface> consumer)2634 VirtualScreenOption ScreenCaptureServer::InitVirtualScreenOption(const std::string &name, sptr<OHOS::Surface> consumer)
2635 {
2636     MediaTrace trace("ScreenCaptureServer::InitVirtualScreenOption");
2637     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " InitVirtualScreenOption start, name:%{public}s.",
2638         FAKE_POINTER(this), name.c_str());
2639     VirtualScreenOption virScrOption = {
2640         .name_ = name,
2641         .width_ = captureConfig_.videoInfo.videoCapInfo.videoFrameWidth,
2642         .height_ = captureConfig_.videoInfo.videoCapInfo.videoFrameHeight,
2643         .density_ = 0,
2644         .surface_ = consumer,
2645         .flags_ = 0,
2646         .isForShot_ = true,
2647         .missionIds_ = {},
2648         .virtualScreenType_ = VirtualScreenType::SCREEN_RECORDING,
2649     };
2650     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " InitVirtualScreenOption end.", FAKE_POINTER(this));
2651     return virScrOption;
2652 }
2653 
GetMissionIds(std::vector<uint64_t> & missionIds)2654 int32_t ScreenCaptureServer::GetMissionIds(std::vector<uint64_t> &missionIds)
2655 {
2656     int32_t size = static_cast<int32_t>(captureConfig_.videoInfo.videoCapInfo.taskIDs.size());
2657     std::list<int32_t> taskIDListTemp = captureConfig_.videoInfo.videoCapInfo.taskIDs;
2658     for (int32_t i = 0; i < size; i++) {
2659         int32_t taskId = taskIDListTemp.front();
2660         taskIDListTemp.pop_front();
2661         MEDIA_LOGD("ScreenCaptureServer::GetMissionIds taskId : %{public}s", std::to_string(taskId).c_str());
2662         uint64_t uintNum = static_cast<uint64_t>(taskId);
2663         missionIds.push_back(uintNum);
2664     }
2665     return MSERR_OK;
2666 }
2667 
AcquireAudioBuffer(std::shared_ptr<AudioBuffer> & audioBuffer,AudioCaptureSourceType type)2668 int32_t ScreenCaptureServer::AcquireAudioBuffer(std::shared_ptr<AudioBuffer> &audioBuffer, AudioCaptureSourceType type)
2669 {
2670     MediaTrace trace("ScreenCaptureServer::AcquireAudioBuffer");
2671     std::unique_lock<std::mutex> lock(mutex_);
2672     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " AcquireAudioBuffer start, state:%{public}d, "
2673         "type:%{public}d.", FAKE_POINTER(this), captureState_, type);
2674     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::STARTED, MSERR_INVALID_OPERATION,
2675         "AcquireAudioBuffer failed, capture is not STARTED, state:%{public}d, type:%{public}d", captureState_, type);
2676 
2677     if (((type == AudioCaptureSourceType::MIC) || (type == AudioCaptureSourceType::SOURCE_DEFAULT)) &&
2678         micAudioCapture_ != nullptr && micAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
2679         return micAudioCapture_->AcquireAudioBuffer(audioBuffer);
2680     }
2681     if (((type == AudioCaptureSourceType::ALL_PLAYBACK) || (type == AudioCaptureSourceType::APP_PLAYBACK)) &&
2682         innerAudioCapture_ != nullptr && innerAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
2683         return innerAudioCapture_->AcquireAudioBuffer(audioBuffer);
2684     }
2685     MEDIA_LOGE("AcquireAudioBuffer failed, source type not support, type:%{public}d", type);
2686     FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_UNKNOWN,
2687         "AcquireAudioBuffer failed, source type not support");
2688     return MSERR_UNKNOWN;
2689 }
2690 
AcquireAudioBufferMix(std::shared_ptr<AudioBuffer> & innerAudioBuffer,std::shared_ptr<AudioBuffer> & micAudioBuffer,AVScreenCaptureMixMode type)2691 int32_t ScreenCaptureServer::AcquireAudioBufferMix(std::shared_ptr<AudioBuffer> &innerAudioBuffer,
2692     std::shared_ptr<AudioBuffer> &micAudioBuffer, AVScreenCaptureMixMode type)
2693 {
2694     if (captureState_ != AVScreenCaptureState::STARTED) {
2695         return MSERR_INVALID_STATE;
2696     }
2697     if (type == AVScreenCaptureMixMode::MIX_MODE && micAudioCapture_ != nullptr &&
2698         micAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
2699         if (micAudioCapture_->AcquireAudioBuffer(micAudioBuffer) != MSERR_OK) {
2700             MEDIA_LOGE("micAudioCapture AcquireAudioBuffer failed");
2701         }
2702     }
2703     if (type == AVScreenCaptureMixMode::MIX_MODE && innerAudioCapture_ != nullptr &&
2704         innerAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
2705         if (innerAudioCapture_->AcquireAudioBuffer(innerAudioBuffer) != MSERR_OK) {
2706             MEDIA_LOGE("innerAudioCapture AcquireAudioBuffer failed");
2707         }
2708     }
2709     if (type == AVScreenCaptureMixMode::MIC_MODE && micAudioCapture_ != nullptr &&
2710         micAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
2711         if (micAudioCapture_->AcquireAudioBuffer(micAudioBuffer) != MSERR_OK) {
2712             MEDIA_LOGE("micAudioCapture AcquireAudioBuffer failed");
2713         }
2714     }
2715     if (type == AVScreenCaptureMixMode::INNER_MODE && innerAudioCapture_ != nullptr &&
2716         innerAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
2717         if (innerAudioCapture_->AcquireAudioBuffer(innerAudioBuffer) != MSERR_OK) {
2718             MEDIA_LOGE("innerAudioCapture AcquireAudioBuffer failed");
2719         }
2720     }
2721     return MSERR_OK;
2722 }
2723 
ReleaseAudioBuffer(AudioCaptureSourceType type)2724 int32_t ScreenCaptureServer::ReleaseAudioBuffer(AudioCaptureSourceType type)
2725 {
2726     MediaTrace trace("ScreenCaptureServer::ReleaseAudioBuffer");
2727     std::unique_lock<std::mutex> lock(mutex_);
2728     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " ReleaseAudioBuffer start, state:%{public}d, "
2729         "type:%{public}d.", FAKE_POINTER(this), captureState_, type);
2730     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::STARTED, MSERR_INVALID_OPERATION,
2731         "ReleaseAudioBuffer failed, capture is not STARTED, state:%{public}d, type:%{public}d", captureState_, type);
2732 
2733     if (((type == AudioCaptureSourceType::MIC) || (type == AudioCaptureSourceType::SOURCE_DEFAULT)) &&
2734         micAudioCapture_ != nullptr && micAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
2735         return micAudioCapture_->ReleaseAudioBuffer();
2736     }
2737     if (((type == AudioCaptureSourceType::ALL_PLAYBACK) || (type == AudioCaptureSourceType::APP_PLAYBACK)) &&
2738         innerAudioCapture_ != nullptr && innerAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
2739         return innerAudioCapture_->ReleaseAudioBuffer();
2740     }
2741     MEDIA_LOGE("ReleaseAudioBuffer failed, source type not support, type:%{public}d", type);
2742     FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_UNKNOWN,
2743         "ReleaseAudioBuffer failed, source type not support");
2744     return MSERR_UNKNOWN;
2745 }
2746 
ReleaseAudioBufferMix(AVScreenCaptureMixMode type)2747 int32_t ScreenCaptureServer::ReleaseAudioBufferMix(AVScreenCaptureMixMode type)
2748 {
2749     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::STARTED, MSERR_INVALID_OPERATION,
2750         "ReleaseAudioBuffer failed, capture is not STARTED, state:%{public}d, type:%{public}d", captureState_, type);
2751     if (type == AVScreenCaptureMixMode::MIX_MODE && micAudioCapture_ != nullptr &&
2752         micAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
2753         if (micAudioCapture_->ReleaseAudioBuffer() != MSERR_OK) {
2754             MEDIA_LOGE("micAudioCapture ReleaseAudioBuffer failed");
2755         }
2756     }
2757     if (type == AVScreenCaptureMixMode::MIX_MODE && innerAudioCapture_ != nullptr &&
2758         innerAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
2759         if (innerAudioCapture_->ReleaseAudioBuffer() != MSERR_OK) {
2760             MEDIA_LOGE("innerAudioCapture ReleaseAudioBuffer failed");
2761         }
2762     }
2763     if (type == AVScreenCaptureMixMode::MIC_MODE && micAudioCapture_ != nullptr &&
2764         micAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
2765         if (micAudioCapture_->ReleaseAudioBuffer() != MSERR_OK) {
2766             MEDIA_LOGE("micAudioCapture ReleaseAudioBuffer failed");
2767         }
2768     }
2769     if (type == AVScreenCaptureMixMode::INNER_MODE && innerAudioCapture_ != nullptr &&
2770         innerAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
2771         if (innerAudioCapture_->ReleaseAudioBuffer() != MSERR_OK) {
2772             MEDIA_LOGE("innerAudioCapture ReleaseAudioBuffer failed");
2773         }
2774     }
2775     return MSERR_OK;
2776 }
2777 
ReleaseInnerAudioBuffer()2778 int32_t ScreenCaptureServer::ReleaseInnerAudioBuffer()
2779 {
2780     CHECK_AND_RETURN_RET_LOG(innerAudioCapture_, MSERR_UNKNOWN, "innerAudioCapture_ is nullptr");
2781     int32_t ret = innerAudioCapture_->ReleaseAudioBuffer();
2782     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "innerAudioCapture ReleaseAudioBuffer failed");
2783     return ret;
2784 }
2785 
ReleaseMicAudioBuffer()2786 int32_t ScreenCaptureServer::ReleaseMicAudioBuffer()
2787 {
2788     CHECK_AND_RETURN_RET_LOG(micAudioCapture_, MSERR_UNKNOWN, "micAudioCapture_ is nullptr");
2789     int32_t ret = micAudioCapture_->ReleaseAudioBuffer();
2790     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "micAudioCapture ReleaseAudioBuffer failed");
2791     return ret;
2792 }
2793 
GetInnerAudioCaptureBufferSize(size_t & size)2794 int32_t ScreenCaptureServer::GetInnerAudioCaptureBufferSize(size_t &size)
2795 {
2796     if (innerAudioCapture_ == nullptr) {
2797         MEDIA_LOGE("innerAudioCapture_ is nullptr");
2798         return MSERR_UNKNOWN;
2799     }
2800     int32_t ret = innerAudioCapture_->GetBufferSize(size);
2801     return ret;
2802 }
2803 
GetMicAudioCaptureBufferSize(size_t & size)2804 int32_t ScreenCaptureServer::GetMicAudioCaptureBufferSize(size_t &size)
2805 {
2806     if (micAudioCapture_ == nullptr) {
2807         MEDIA_LOGE("micAudioCapture_ is nullptr");
2808         return MSERR_UNKNOWN;
2809     }
2810     int32_t ret = micAudioCapture_->GetBufferSize(size);
2811     return ret;
2812 }
2813 
AcquireVideoBuffer(sptr<OHOS::SurfaceBuffer> & surfaceBuffer,int32_t & fence,int64_t & timestamp,OHOS::Rect & damage)2814 int32_t ScreenCaptureServer::AcquireVideoBuffer(sptr<OHOS::SurfaceBuffer> &surfaceBuffer, int32_t &fence,
2815                                                 int64_t &timestamp, OHOS::Rect &damage)
2816 {
2817     MediaTrace trace("ScreenCaptureServer::AcquireVideoBuffer");
2818     std::unique_lock<std::mutex> lock(mutex_);
2819     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " AcquireVideoBuffer start, state:%{public}d, "
2820         "fence:%{public}d, timestamp:%{public}" PRId64, FAKE_POINTER(this), captureState_, fence, timestamp);
2821     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::STARTED, MSERR_INVALID_OPERATION,
2822         "AcquireVideoBuffer failed, capture is not STARTED, state:%{public}d", captureState_);
2823 
2824     CHECK_AND_RETURN_RET_LOG(surfaceCb_ != nullptr, MSERR_NO_MEMORY, "AcquireVideoBuffer failed, callback is nullptr");
2825     (static_cast<ScreenCapBufferConsumerListener *>(surfaceCb_.GetRefPtr()))->
2826         AcquireVideoBuffer(surfaceBuffer, fence, timestamp, damage);
2827     if (isDump_ && surfaceBuffer != nullptr) {
2828         void* addr = surfaceBuffer->GetVirAddr();
2829         uint32_t bufferSize = surfaceBuffer->GetSize();
2830         FILE *desFile = fopen(DUMP_PATH.c_str(), "wb+");
2831         if (desFile && addr != nullptr) {
2832             (void)fwrite(addr, 1, bufferSize, desFile);
2833             (void)fclose(desFile);
2834         } else if (desFile) {
2835             (void)fclose(desFile);
2836         }
2837     }
2838     if (surfaceBuffer != nullptr) {
2839         MEDIA_LOGD("getcurrent surfaceBuffer info, size:%{public}u", surfaceBuffer->GetSize());
2840         return MSERR_OK;
2841     }
2842     FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_UNKNOWN,
2843         "AcquireVideoBuffer fault");
2844     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " AcquireVideoBuffer end.", FAKE_POINTER(this));
2845     return MSERR_UNKNOWN;
2846 }
2847 
ReleaseVideoBuffer()2848 int32_t ScreenCaptureServer::ReleaseVideoBuffer()
2849 {
2850     MediaTrace trace("ScreenCaptureServer::ReleaseVideoBuffer");
2851     std::unique_lock<std::mutex> lock(mutex_);
2852     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " ReleaseVideoBuffer start, state:%{public}d.",
2853         FAKE_POINTER(this), captureState_);
2854     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::STARTED, MSERR_INVALID_OPERATION,
2855         "ReleaseVideoBuffer failed, capture is not STARTED, state:%{public}d", captureState_);
2856 
2857     CHECK_AND_RETURN_RET_LOG(surfaceCb_ != nullptr, MSERR_NO_MEMORY, "ReleaseVideoBuffer failed, callback is nullptr");
2858     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " ReleaseVideoBuffer end.", FAKE_POINTER(this));
2859     return (static_cast<ScreenCapBufferConsumerListener *>(surfaceCb_.GetRefPtr()))->ReleaseVideoBuffer();
2860 }
2861 
ExcludeContent(ScreenCaptureContentFilter & contentFilter)2862 int32_t ScreenCaptureServer::ExcludeContent(ScreenCaptureContentFilter &contentFilter)
2863 {
2864     std::unique_lock<std::mutex> lock(mutex_);
2865     CHECK_AND_RETURN_RET_LOG(captureState_ != AVScreenCaptureState::STOPPED, MSERR_INVALID_OPERATION,
2866         "ExcludeContent failed, capture is STOPPED");
2867 
2868     MEDIA_LOGI("ScreenCaptureServer::ExcludeContent start");
2869     contentFilter_ = contentFilter;
2870     if (captureState_ == AVScreenCaptureState::STARTED) {
2871         Rosen::DisplayManager::GetInstance().SetVirtualScreenBlackList(virtualScreenId_, contentFilter_.windowIDsVec,
2872             surfaceIdList_);
2873     }
2874     int32_t ret = MSERR_OK;
2875     if (innerAudioCapture_ != nullptr) {
2876         ret = innerAudioCapture_->UpdateAudioCapturerConfig(contentFilter_);
2877     }
2878 
2879     // For the moment, not support:
2880     // For STREAM, should call AudioCapturer interface to make effect when start
2881     // For CAPTURE FILE, should call Recorder interface to make effect when start
2882     if (ret != MSERR_OK) {
2883         MEDIA_LOGE("ScreenCaptureServer::ExcludeContent UpdateAudioCapturerConfig failed");
2884         FaultScreenCaptureEventWrite(appName_, instanceId_, avType_, dataMode_, SCREEN_CAPTURE_ERR_UNSUPPORT,
2885             "ExcludeContent failed, UpdateAudioCapturerConfig failed");
2886     }
2887     return ret;
2888 }
2889 
SetMicrophoneEnabled(bool isMicrophone)2890 int32_t ScreenCaptureServer::SetMicrophoneEnabled(bool isMicrophone)
2891 {
2892     MediaTrace trace("ScreenCaptureServer::SetMicrophoneEnabled");
2893     std::lock_guard<std::mutex> lock(mutex_);
2894     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetMicrophoneEnabled isMicrophoneSwitchTurnOn_:"
2895         "%{public}d, new isMicrophone:%{public}d", FAKE_POINTER(this), isMicrophoneSwitchTurnOn_, isMicrophone);
2896     int32_t ret = MSERR_UNKNOWN;
2897     isMicrophoneSwitchTurnOn_ = isMicrophone;
2898     if (isMicrophone) {
2899         statisticalEventInfo_.enableMic = true;
2900     }
2901     if (captureState_ != AVScreenCaptureState::STARTED) {
2902         return MSERR_OK;
2903     }
2904     if (isMicrophone) {
2905         ret = SetMicrophoneOn();
2906         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "SetMicrophoneOn failed");
2907         NotifyStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_MIC_UNMUTED_BY_USER);
2908     } else {
2909         ret = SetMicrophoneOff();
2910         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "SetMicrophoneOff failed");
2911         NotifyStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_MIC_MUTED_BY_USER);
2912     }
2913     // For CAPTURE FILE, should call Recorder interface to make effect
2914     MEDIA_LOGI("SetMicrophoneEnabled OK.");
2915     return MSERR_OK;
2916 }
2917 
SetMicrophoneOn()2918 int32_t ScreenCaptureServer::SetMicrophoneOn()
2919 {
2920 #ifdef SUPPORT_CALL
2921     if (InCallObserver::GetInstance().IsInCall(true) && !IsTelInCallSkipList()) {
2922         MEDIA_LOGE("Try SetMicrophoneOn But In Call");
2923         NotifyStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_MIC_UNAVAILABLE);
2924         return MSERR_UNKNOWN;
2925     }
2926 #endif
2927     int32_t ret = MSERR_UNKNOWN;
2928     if (!micAudioCapture_) {
2929         if (captureConfig_.dataType == DataType::ORIGINAL_STREAM) {
2930             ret = StartStreamMicAudioCapture();
2931             CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "SetMicrophoneOn StartStreamMicAudioCapture failed");
2932         } else if (captureConfig_.dataType == DataType::CAPTURE_FILE) {
2933             ret = StartFileMicAudioCapture();
2934             CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "StartFileMicAudioCapture failed");
2935         }
2936     } else if (micAudioCapture_->GetAudioCapturerState() == CAPTURER_PAUSED) {
2937         ret = micAudioCapture_->Resume();
2938         if (ret != MSERR_OK) {
2939             MEDIA_LOGE("micAudioCapture Resume failed");
2940             NotifyStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_MIC_UNAVAILABLE);
2941             return ret;
2942         }
2943     } else if (micAudioCapture_->GetAudioCapturerState() != CAPTURER_RECORDING) {
2944         MEDIA_LOGE("AudioCapturerState invalid");
2945     }
2946     if (captureConfig_.dataType == DataType::CAPTURE_FILE) {
2947         CHECK_AND_RETURN_RET_LOG(micAudioCapture_ && micAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING,
2948             MSERR_OK, "micAudioCapture is not recording");
2949         if (innerAudioCapture_ && innerAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING &&
2950             audioSource_ && audioSource_->GetSpeakerAliveStatus() && !audioSource_->GetIsInVoIPCall()) {
2951             usleep(AUDIO_CHANGE_TIME);
2952             ret = innerAudioCapture_->Pause();
2953             CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "innerAudioCapture Pause failed");
2954         }
2955     }
2956     return MSERR_OK;
2957 }
2958 
SetMicrophoneOff()2959 int32_t ScreenCaptureServer::SetMicrophoneOff()
2960 {
2961     int32_t ret = MSERR_UNKNOWN;
2962     if (innerAudioCapture_ && innerAudioCapture_->GetAudioCapturerState() == CAPTURER_PAUSED) {
2963         ret = innerAudioCapture_->Resume();
2964         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "innerAudioCapture Resume failed");
2965     }
2966     if (micAudioCapture_ && micAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
2967         usleep(AUDIO_CHANGE_TIME);
2968         ret = micAudioCapture_->Pause();
2969         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "micAudioCapture Pause failed");
2970     }
2971     return MSERR_OK;
2972 }
2973 
OnSpeakerAliveStatusChanged(bool speakerAliveStatus)2974 int32_t ScreenCaptureServer::OnSpeakerAliveStatusChanged(bool speakerAliveStatus)
2975 {
2976     int32_t ret = MSERR_UNKNOWN;
2977     CHECK_AND_RETURN_RET_LOG(innerAudioCapture_, MSERR_UNKNOWN, "innerAudioCapture_ is nullptr");
2978     if (!speakerAliveStatus && innerAudioCapture_->GetAudioCapturerState() == CAPTURER_PAUSED) {
2979         ret = innerAudioCapture_->Resume();
2980         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "innerAudioCapture Resume failed");
2981     } else if (speakerAliveStatus && micAudioCapture_ &&
2982         micAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING && audioSource_ &&
2983         !audioSource_->GetIsInVoIPCall() && innerAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
2984         ret = innerAudioCapture_->Pause();
2985         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "innerAudioCapture Pause failed");
2986     }
2987     return MSERR_OK;
2988 }
2989 
ReStartMicForVoIPStatusSwitch()2990 int32_t ScreenCaptureServer::ReStartMicForVoIPStatusSwitch()
2991 {
2992     int32_t ret = MSERR_OK;
2993     StopMicAudioCapture();
2994     if (isMicrophoneSwitchTurnOn_) {
2995 #ifdef SUPPORT_CALL
2996         if (InCallObserver::GetInstance().IsInCall(true) && !IsTelInCallSkipList()) {
2997             MEDIA_LOGE("Try ReStartMicForVoIPStatusSwitch But In Call");
2998             NotifyStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_MIC_UNAVAILABLE);
2999             return MSERR_UNKNOWN;
3000         }
3001 #endif
3002         ret = StartMicAudioCapture();
3003         if (ret != MSERR_OK) {
3004             MEDIA_LOGE("OnVoIPStatusChanged StartMicAudioCapture failed, ret: %{public}d", ret);
3005         }
3006     }
3007     return ret;
3008 }
3009 
OnVoIPStatusChanged(bool isInVoIPCall)3010 int32_t ScreenCaptureServer::OnVoIPStatusChanged(bool isInVoIPCall)
3011 {
3012     MEDIA_LOGI("OnVoIPStatusChanged, isInVoIPCall:%{public}d", isInVoIPCall);
3013     int32_t ret = MSERR_UNKNOWN;
3014     if (isInVoIPCall) {
3015         CHECK_AND_RETURN_RET_LOG(innerAudioCapture_, MSERR_UNKNOWN, "innerAudioCapture is nullptr");
3016         if (innerAudioCapture_->GetAudioCapturerState() == CAPTURER_PAUSED) {
3017             ret = innerAudioCapture_->Resume();
3018             CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "innerAudioCapture Resume failed");
3019         }
3020         usleep(AUDIO_CHANGE_TIME);
3021         ReStartMicForVoIPStatusSwitch();
3022     } else {
3023         ReStartMicForVoIPStatusSwitch();
3024         CHECK_AND_RETURN_RET_LOG(innerAudioCapture_, MSERR_UNKNOWN, "innerAudioCapture is nullptr");
3025         if (innerAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING &&
3026             micAudioCapture_ && micAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING &&
3027             audioSource_ && audioSource_->GetSpeakerAliveStatus()) {
3028             usleep(AUDIO_CHANGE_TIME);
3029             ret = innerAudioCapture_->Pause();
3030             CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "innerAudioCapture Pause failed");
3031         }
3032     }
3033     return MSERR_OK;
3034 }
3035 
3036 #ifdef SUPPORT_CALL
TelCallStateUpdated(bool isInTelCall)3037 int32_t ScreenCaptureServer::TelCallStateUpdated(bool isInTelCall)
3038 {
3039     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " API Version is %{public}d", FAKE_POINTER(this),
3040         apiVersion_);
3041     if (apiVersion_ < STOPPED_BY_CALL_API_VERSION_ISOLATION && isInTelCall) {
3042         StopScreenCaptureByEvent(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_STOPPED_BY_CALL);
3043         Release();
3044         return MSERR_OK;
3045     }
3046     if (isInTelCall_.load() == isInTelCall) {
3047         return MSERR_OK;
3048     }
3049     isInTelCall_.store(isInTelCall);
3050     if (isInTelCall) {
3051         return OnTelCallStart();
3052     }
3053     return OnTelCallStop();
3054 }
3055 
TelCallAudioStateUpdated(bool isInTelCallAudio)3056 int32_t ScreenCaptureServer::TelCallAudioStateUpdated(bool isInTelCallAudio)
3057 {
3058     if (IsTelInCallSkipList() || isInTelCallAudio_.load() == isInTelCallAudio) {
3059         return MSERR_OK;
3060     }
3061     isInTelCallAudio_.store(isInTelCallAudio);
3062     if (isInTelCallAudio) {
3063         return OnTelCallStart();
3064     }
3065     return OnTelCallStop();
3066 }
3067 
OnTelCallStart()3068 int32_t ScreenCaptureServer::OnTelCallStart()
3069 {
3070     MEDIA_LOGI("OnTelCallStart InTelCall:%{public}d, Audio:%{public}d", isInTelCall_.load(), isInTelCallAudio_.load());
3071     int32_t ret = MSERR_OK;
3072     if (!isInTelCall_.load() && !isInTelCallAudio_.load()) {
3073         return ret;
3074     }
3075     if (innerAudioCapture_ && innerAudioCapture_->GetAudioCapturerState() == CAPTURER_PAUSED) {
3076         ret = innerAudioCapture_->Resume();
3077         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "innerAudioCapture Resume failed");
3078     }
3079     if (micAudioCapture_) {
3080         usleep(AUDIO_CHANGE_TIME);
3081         micAudioCapture_->SetIsInTelCall(true);
3082         if (micAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING) {
3083             ret = micAudioCapture_->Pause();
3084             CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "micAudioCapture Pause failed");
3085         }
3086     }
3087     return MSERR_OK;
3088 }
3089 
OnTelCallStop()3090 int32_t ScreenCaptureServer::OnTelCallStop()
3091 {
3092     MEDIA_LOGI("OnTelCallStop InTelCall:%{public}d, Audio:%{public}d", isInTelCall_.load(), isInTelCallAudio_.load());
3093     int32_t ret = MSERR_OK;
3094     if (isInTelCall_.load() || isInTelCallAudio_.load()) {
3095         return ret;
3096     }
3097     if (micAudioCapture_) {
3098         micAudioCapture_->SetIsInTelCall(false);
3099         if (isMicrophoneSwitchTurnOn_ && micAudioCapture_->GetAudioCapturerState() == CAPTURER_PAUSED) {
3100             ret = micAudioCapture_->Resume();
3101             if (ret != MSERR_OK) {
3102                 MEDIA_LOGE("micAudioCapture Resume failed");
3103                 NotifyStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_MIC_UNAVAILABLE);
3104                 return ret;
3105             }
3106         }
3107     }
3108     if (!micAudioCapture_ && isMicrophoneSwitchTurnOn_) {
3109         if (captureConfig_.dataType == DataType::ORIGINAL_STREAM) {
3110             ret = StartStreamMicAudioCapture();
3111             CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "SetMicrophoneOn StartStreamMicAudioCapture failed");
3112         } else if (captureConfig_.dataType == DataType::CAPTURE_FILE) {
3113             ret = StartFileMicAudioCapture();
3114             CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "StartFileMicAudioCapture failed");
3115         }
3116     }
3117     if (captureConfig_.dataType == DataType::CAPTURE_FILE) {
3118         CHECK_AND_RETURN_RET_LOG(micAudioCapture_ && micAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING,
3119             MSERR_OK, "micAudioCapture is not recording");
3120         usleep(AUDIO_CHANGE_TIME);
3121         if (innerAudioCapture_ && innerAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING &&
3122             audioSource_ && audioSource_->GetSpeakerAliveStatus() && !audioSource_->GetIsInVoIPCall()) {
3123             ret = innerAudioCapture_->Pause();
3124             CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "innerAudioCapture Pause failed");
3125         }
3126     }
3127     return MSERR_OK;
3128 }
3129 #endif
3130 
GetMicWorkingState()3131 bool ScreenCaptureServer::GetMicWorkingState()
3132 {
3133     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " GetMicWorkingState", FAKE_POINTER(this));
3134     if (micAudioCapture_ != nullptr) {
3135         return micAudioCapture_->GetAudioCapturerState() == CAPTURER_RECORDING;
3136     }
3137     return false;
3138 }
3139 
SetCanvasRotation(bool canvasRotation)3140 int32_t ScreenCaptureServer::SetCanvasRotation(bool canvasRotation)
3141 {
3142     MediaTrace trace("ScreenCaptureServer::SetCanvasRotation");
3143     std::lock_guard<std::mutex> lock(mutex_);
3144     canvasRotation_ = canvasRotation;
3145     MEDIA_LOGI("ScreenCaptureServer::SetCanvasRotation, canvasRotation:%{public}d", canvasRotation);
3146     if (captureState_ != AVScreenCaptureState::STARTED) { // Before Start
3147         return MSERR_OK;
3148     }
3149     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetCanvasRotation end.", FAKE_POINTER(this));
3150     return SetCanvasRotationInner();
3151 }
3152 
SetCanvasRotationInner()3153 int32_t ScreenCaptureServer::SetCanvasRotationInner()
3154 {
3155     MediaTrace trace("ScreenCaptureServer::SetCanvasRotationInner");
3156     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetCanvasRotationInner start.", FAKE_POINTER(this));
3157     CHECK_AND_RETURN_RET_LOG(virtualScreenId_ != SCREEN_ID_INVALID, MSERR_INVALID_VAL,
3158                              "SetCanvasRotation failed virtual screen not init");
3159     auto ret = ScreenManager::GetInstance().SetVirtualMirrorScreenCanvasRotation(virtualScreenId_, canvasRotation_);
3160     CHECK_AND_RETURN_RET_LOG(ret == DMError::DM_OK, MSERR_UNSUPPORT,
3161                              "SetVirtualMirrorScreenCanvasRotation failed, ret: %{public}d", ret);
3162     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetCanvasRotationInner OK.", FAKE_POINTER(this));
3163     return MSERR_OK;
3164 }
3165 
ShowCursor(bool showCursor)3166 int32_t ScreenCaptureServer::ShowCursor(bool showCursor)
3167 {
3168     MediaTrace trace("ScreenCaptureServer::ShowCursor");
3169     std::lock_guard<std::mutex> lock(mutex_);
3170     if (showCursor == showCursor_) {
3171         return MSERR_OK;
3172     }
3173     showCursor_ = showCursor;
3174     MEDIA_LOGI("ScreenCaptureServer::ShowCursor, showCursor:%{public}d", showCursor_);
3175     if (!showCursor_) {
3176         bool isRegsterMMI = RegisterMMISystemAbilityListener();
3177         if (!isRegsterMMI) {
3178             MEDIA_LOGE("Resiter MMI failed");
3179         }
3180         RegisterMouseChangeListener("change");
3181     } else {
3182         UnRegisterMMISystemAbilityListener();
3183         UnRegisterMouseChangeListener("change");
3184     }
3185     if (captureState_ != AVScreenCaptureState::STARTED) {
3186         MEDIA_LOGI("ScreenCaptureServer::ShowCursor, virtual screen not created, return ok.");
3187         return MSERR_OK;
3188     }
3189     return ShowCursorInner();
3190 }
3191 
ShowCursorInner()3192 int32_t ScreenCaptureServer::ShowCursorInner()
3193 {
3194     MediaTrace trace("ScreenCaptureServer::ShowCursorInner");
3195     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " ShowCursorInner start.", FAKE_POINTER(this));
3196     CHECK_AND_RETURN_RET_LOG(virtualScreenId_ != SCREEN_ID_INVALID, MSERR_INVALID_VAL,
3197         "ShowCursorInner failed, virtual screen not init");
3198     if (!showCursor_) {
3199         MEDIA_LOGI("ScreenCaptureServer 0x%{public}06" PRIXPTR " ShowCursorInner not show cursor", FAKE_POINTER(this));
3200         uint64_t surfaceId = {};
3201         int32_t ret = MMI::InputManager::GetInstance()->GetCursorSurfaceId(surfaceId);
3202         CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "GetCursorSurfaceId failed");
3203         MEDIA_LOGI("GetCursorSurfaceId success, surfaceId: %{public}" PRIu64, surfaceId);
3204         surfaceIdList_ = {};
3205         surfaceIdList_.push_back(surfaceId);
3206     } else {
3207         MEDIA_LOGI("ScreenCaptureServer 0x%{public}06" PRIXPTR " ShowCursorInner, show cursor", FAKE_POINTER(this));
3208         surfaceIdList_ = {};
3209     }
3210     Rosen::DisplayManager::GetInstance().SetVirtualScreenBlackList(virtualScreenId_, contentFilter_.windowIDsVec,
3211         surfaceIdList_);
3212     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " ShowCursorInner OK.", FAKE_POINTER(this));
3213     return MSERR_OK;
3214 }
3215 
ResizeCanvas(int32_t width,int32_t height)3216 int32_t ScreenCaptureServer::ResizeCanvas(int32_t width, int32_t height)
3217 {
3218     MediaTrace trace("ScreenCaptureServer::ResizeCanvas");
3219     std::lock_guard<std::mutex> lock(mutex_);
3220     MEDIA_LOGI("ScreenCaptureServer::ResizeCanvas start, Width:%{public}d, Height:%{public}d", width, height);
3221     if (captureState_ != AVScreenCaptureState::STARTED) {
3222         MEDIA_LOGE("ResizeCanvas captureState_ invalid, captureState_:%{public}d", captureState_);
3223         return MSERR_INVALID_OPERATION;
3224     }
3225     if ((width <= 0) || (width > VIDEO_FRAME_WIDTH_MAX)) {
3226         MEDIA_LOGE("ResizeCanvas Width is invalid, Width:%{public}d, Height:%{public}d", width, height);
3227         return MSERR_INVALID_VAL;
3228     }
3229     if ((height <= 0) || (height > VIDEO_FRAME_HEIGHT_MAX)) {
3230         MEDIA_LOGE("ResizeCanvas Height is invalid, Width:%{public}d, Height:%{public}d", width, height);
3231         return MSERR_INVALID_VAL;
3232     }
3233     if (captureConfig_.dataType != DataType::ORIGINAL_STREAM) {
3234         MEDIA_LOGE("ResizeCanvas dataType invalid, dataType:%{public}d", captureConfig_.dataType);
3235         return MSERR_INVALID_OPERATION;
3236     }
3237 
3238     auto resizeRet = ScreenManager::GetInstance().ResizeVirtualScreen(virtualScreenId_, width, height);
3239     MEDIA_LOGI("ScreenCaptureServer::ResizeCanvas, ResizeVirtualScreen end, ret: %{public}d ", resizeRet);
3240     CHECK_AND_RETURN_RET_LOG(resizeRet == DMError::DM_OK, MSERR_UNSUPPORT, "ResizeVirtualScreen failed");
3241 
3242     return MSERR_OK;
3243 }
3244 
SkipPrivacyMode(std::vector<uint64_t> & windowIDsVec)3245 int32_t ScreenCaptureServer::SkipPrivacyMode(std::vector<uint64_t> &windowIDsVec)
3246 {
3247     MediaTrace trace("ScreenCaptureServer::SkipPrivacyMode");
3248     std::lock_guard<std::mutex> lock(mutex_);
3249     MEDIA_LOGI("ScreenCaptureServer::SkipPrivacyMode, windowIDsVec size:%{public}d",
3250         static_cast<int32_t>(windowIDsVec.size()));
3251     for (size_t i = 0; i < windowIDsVec.size(); i++) {
3252         MEDIA_LOGD("SkipPrivacyMode windowIDsVec value :%{public}" PRIu64, windowIDsVec[i]);
3253     }
3254     skipPrivacyWindowIDsVec_.assign(windowIDsVec.begin(), windowIDsVec.end());
3255     if (captureState_ != AVScreenCaptureState::STARTED) { // Before Start
3256         return MSERR_OK;
3257     }
3258     return SkipPrivacyModeInner();
3259 }
3260 
SkipPrivacyModeInner()3261 int32_t ScreenCaptureServer::SkipPrivacyModeInner()
3262 {
3263     MediaTrace trace("ScreenCaptureServer::SkipPrivacyModeInner");
3264     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SkipPrivacyModeInner start.", FAKE_POINTER(this));
3265     CHECK_AND_RETURN_RET_LOG(virtualScreenId_ != SCREEN_ID_INVALID, MSERR_INVALID_VAL,
3266                              "SkipPrivacyMode failed virtual screen not init");
3267     auto ret = Rosen::DisplayManager::GetInstance().SetVirtualScreenSecurityExemption(virtualScreenId_,
3268         appInfo_.appPid, skipPrivacyWindowIDsVec_);
3269     CHECK_AND_RETURN_RET_LOG(ret == DMError::DM_OK, MSERR_UNSUPPORT,
3270         "SkipPrivacyModeInner failed, ret: %{public}d", ret);
3271     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SkipPrivacyModeInner OK.", FAKE_POINTER(this));
3272     return MSERR_OK;
3273 }
3274 
SetMaxVideoFrameRate(int32_t frameRate)3275 int32_t ScreenCaptureServer::SetMaxVideoFrameRate(int32_t frameRate)
3276 {
3277     MediaTrace trace("ScreenCaptureServer::SetMaxVideoFrameRate");
3278     std::lock_guard<std::mutex> lock(mutex_);
3279     MEDIA_LOGI("ScreenCaptureServer::SetMaxVideoFrameRate start, frameRate:%{public}d", frameRate);
3280     if (captureState_ != AVScreenCaptureState::STARTED) {
3281         MEDIA_LOGE("SetMaxVideoFrameRate captureState_ invalid, captureState_:%{public}d", captureState_);
3282         return MSERR_INVALID_OPERATION;
3283     }
3284     if (frameRate <= 0) {
3285         MEDIA_LOGE("SetMaxVideoFrameRate frameRate is invalid, frameRate:%{public}d", frameRate);
3286         return MSERR_INVALID_VAL;
3287     }
3288 
3289     uint32_t actualRefreshRate = 0;
3290     auto res = ScreenManager::GetInstance().SetVirtualScreenMaxRefreshRate(virtualScreenId_,
3291         static_cast<uint32_t>(frameRate), actualRefreshRate);
3292 
3293     CHECK_AND_RETURN_RET_LOG(res == DMError::DM_OK, MSERR_UNSUPPORT, "SetMaxVideoFrameRate failed");
3294 
3295     MEDIA_LOGI("ScreenCaptureServer::SetMaxVideoFrameRate end, frameRate:%{public}d, actualRefreshRate:%{public}u",
3296         frameRate, actualRefreshRate);
3297     return MSERR_OK;
3298 }
3299 
GetScreenScaleMode(const AVScreenCaptureFillMode & fillMode)3300 ScreenScaleMode ScreenCaptureServer::GetScreenScaleMode(const AVScreenCaptureFillMode &fillMode)
3301 {
3302     MEDIA_LOGI("ScreenCaptureServer::GetScreenScaleMode in, fillMode: %{public}d", fillMode);
3303     static const std::map<AVScreenCaptureFillMode, ScreenScaleMode> modeMap = {
3304         {PRESERVE_ASPECT_RATIO, ScreenScaleMode::UNISCALE_MODE},
3305         {SCALE_TO_FILL, ScreenScaleMode::FILL_MODE}
3306     };
3307     ScreenScaleMode scaleMode = ScreenScaleMode::UNISCALE_MODE;
3308     auto iter = modeMap.find(fillMode);
3309     if (iter != modeMap.end()) {
3310         scaleMode = iter->second;
3311     }
3312     if (GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"]
3313             .compare(appName_) == 0) {
3314         scaleMode = ScreenScaleMode::FILL_MODE;
3315     }
3316     MEDIA_LOGI("ScreenCaptureServer::GetScreenScaleMode succeed, scaleMode: %{public}d", scaleMode);
3317     return scaleMode;
3318 }
3319 
SetScreenScaleMode()3320 int32_t ScreenCaptureServer::SetScreenScaleMode()
3321 {
3322     MediaTrace trace("ScreenCaptureServer::SetScreenScaleMode");
3323     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetScreenScaleMode start.", FAKE_POINTER(this));
3324     CHECK_AND_RETURN_RET_LOG(virtualScreenId_ != SCREEN_ID_INVALID, MSERR_INVALID_VAL,
3325                              "SetScreenScaleMode failed virtual screen not init");
3326     auto ret = ScreenManager::GetInstance().SetVirtualMirrorScreenScaleMode(
3327         virtualScreenId_, GetScreenScaleMode(captureConfig_.videoInfo.videoCapInfo.screenCaptureFillMode));
3328     if (ret != DMError::DM_OK) {
3329         MEDIA_LOGW("SetScreenScaleMode failed, ret: %{public}d", ret);
3330         return static_cast<int32_t>(ret);
3331     }
3332     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " SetScreenScaleMode OK.", FAKE_POINTER(this));
3333     return MSERR_OK;
3334 }
3335 
StopAudioCapture()3336 int32_t ScreenCaptureServer::StopAudioCapture()
3337 {
3338     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StopAudioCapture start.", FAKE_POINTER(this));
3339     StopMicAudioCapture();
3340     StopInnerAudioCapture();
3341     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StopAudioCapture end.", FAKE_POINTER(this));
3342     return MSERR_OK;
3343 }
3344 
StartMicAudioCapture()3345 int32_t ScreenCaptureServer::StartMicAudioCapture()
3346 {
3347     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartMicAudioCapture start, dataType:%{public}d, "
3348         "micCapInfo.state:%{public}d.",
3349         FAKE_POINTER(this), captureConfig_.dataType, captureConfig_.audioInfo.micCapInfo.state);
3350     CHECK_AND_RETURN_RET_NOLOG(micAudioCapture_ != nullptr, MSERR_OK);
3351     if (captureConfig_.audioInfo.micCapInfo.state == AVScreenCaptureParamValidationState::VALIDATION_VALID) {
3352         MediaTrace trace("ScreenCaptureServer::StartMicAudioCapture");
3353         if (audioSource_) {
3354             micAudioCapture_->SetIsInVoIPCall(audioSource_->GetIsInVoIPCall());
3355         }
3356         int32_t ret = micAudioCapture_->Start(appInfo_);
3357         if (ret != MSERR_OK) {
3358             MEDIA_LOGE("StartMicAudioCapture failed");
3359             NotifyStateChange(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_MIC_UNAVAILABLE);
3360             return ret;
3361         }
3362     }
3363     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StartMicAudioCapture OK.", FAKE_POINTER(this));
3364     return MSERR_OK;
3365 }
3366 
StopInnerAudioCapture()3367 int32_t ScreenCaptureServer::StopInnerAudioCapture()
3368 {
3369     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StopInnerAudioCapture start.", FAKE_POINTER(this));
3370     if (innerAudioCapture_ != nullptr) {
3371         MediaTrace trace("ScreenCaptureServer::StopInnerAudioCapture");
3372         innerAudioCapture_->Stop();
3373     }
3374     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StopInnerAudioCapture end.", FAKE_POINTER(this));
3375     return MSERR_OK;
3376 }
3377 
StopMicAudioCapture()3378 int32_t ScreenCaptureServer::StopMicAudioCapture()
3379 {
3380     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StopMicAudioCapture start.", FAKE_POINTER(this));
3381     if (micAudioCapture_ != nullptr) {
3382         MediaTrace trace("ScreenCaptureServer::StopAudioCaptureMic");
3383         micAudioCapture_->Stop();
3384     }
3385     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StopMicAudioCapture end.", FAKE_POINTER(this));
3386     return MSERR_OK;
3387 }
3388 
StopVideoCapture()3389 int32_t ScreenCaptureServer::StopVideoCapture()
3390 {
3391     MediaTrace trace("ScreenCaptureServer::StopVideoCapture");
3392     MEDIA_LOGI("StopVideoCapture");
3393     if ((virtualScreenId_ < 0) || ((consumer_ == nullptr) && !isSurfaceMode_) || !isConsumerStart_) {
3394         MEDIA_LOGI("StopVideoCapture IGNORED, video capture not start");
3395         if (surfaceCb_ != nullptr) {
3396             (static_cast<ScreenCapBufferConsumerListener *>(surfaceCb_.GetRefPtr()))->StopBufferThread();
3397             (static_cast<ScreenCapBufferConsumerListener *>(surfaceCb_.GetRefPtr()))->Release();
3398             surfaceCb_ = nullptr;
3399         }
3400         return MSERR_OK;
3401     }
3402 
3403     DestroyVirtualScreen();
3404     if (consumer_ != nullptr) {
3405         consumer_->UnregisterConsumerListener();
3406         consumer_ = nullptr;
3407     }
3408 
3409     if (surfaceCb_ != nullptr) {
3410         (static_cast<ScreenCapBufferConsumerListener *>(surfaceCb_.GetRefPtr()))->StopBufferThread();
3411         (static_cast<ScreenCapBufferConsumerListener *>(surfaceCb_.GetRefPtr()))->Release();
3412         surfaceCb_ = nullptr;
3413     }
3414     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StopVideoCapture end.", FAKE_POINTER(this));
3415     return MSERR_OK;
3416 }
3417 
StopScreenCaptureRecorder()3418 int32_t ScreenCaptureServer::StopScreenCaptureRecorder()
3419 {
3420     MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances StopScreenCaptureRecorder S", FAKE_POINTER(this));
3421     MediaTrace trace("ScreenCaptureServer::StopScreenCaptureRecorder");
3422     int32_t ret = MSERR_OK;
3423     if (recorder_ != nullptr) {
3424         ret = recorder_->Stop(false);
3425         if (ret != MSERR_OK) {
3426             MEDIA_LOGE("StopScreenCaptureRecorder recorder stop failed, ret:%{public}d", ret);
3427         }
3428         DestroyVirtualScreen();
3429         recorder_->Release();
3430         recorder_ = nullptr;
3431         StopAudioCapture();
3432     }
3433     UnRegisterMouseChangeListener("change");
3434     UnRegisterMMISystemAbilityListener();
3435     showCursor_ = true;
3436     surfaceIdList_ = {};
3437     captureCallback_ = nullptr;
3438     isConsumerStart_ = false;
3439     return ret;
3440 }
3441 
StopScreenCaptureByEvent(AVScreenCaptureStateCode stateCode)3442 int32_t ScreenCaptureServer::StopScreenCaptureByEvent(AVScreenCaptureStateCode stateCode)
3443 {
3444     MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances StopScreenCaptureByEvent S", FAKE_POINTER(this));
3445     MediaTrace trace("ScreenCaptureServer::StopScreenCaptureByEvent");
3446     if (captureState_ == AVScreenCaptureState::STOPPED) {
3447         MEDIA_LOGI("StopScreenCaptureByEvent repeat, capture is STOPPED.");
3448         return MSERR_OK;
3449     }
3450     std::lock_guard<std::mutex> lock(mutex_);
3451     return StopScreenCaptureInner(stateCode);
3452 }
3453 
SetSystemScreenRecorderStatus(bool status)3454 void ScreenCaptureServer::SetSystemScreenRecorderStatus(bool status)
3455 {
3456     if (GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"]
3457             .compare(appName_) != 0) {
3458         return;
3459     }
3460     if (status) {
3461         (ScreenCaptureServer::systemScreenRecorderPid_).store(appInfo_.appPid);
3462         ScreenCaptureMonitorServer::GetInstance()->SetSystemScreenRecorderStatus(true);
3463     } else {
3464         ScreenCaptureMonitorServer::GetInstance()->SetSystemScreenRecorderStatus(false);
3465     }
3466 }
3467 
StopScreenCaptureInner(AVScreenCaptureStateCode stateCode)3468 int32_t ScreenCaptureServer::StopScreenCaptureInner(AVScreenCaptureStateCode stateCode)
3469 {
3470     MediaTrace trace("ScreenCaptureServer::StopScreenCaptureInner");
3471     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StopScreenCaptureInner start, stateCode:%{public}d.",
3472         FAKE_POINTER(this), stateCode);
3473     if (screenCaptureCb_ != nullptr) {
3474         (static_cast<ScreenCaptureListenerCallback *>(screenCaptureCb_.get()))->Stop();
3475     }
3476     if (audioSource_ && audioSource_->GetAppPid() > 0) { // DataType::CAPTURE_FILE
3477         audioSource_->UnregisterAudioRendererEventListener(audioSource_->GetAppPid());
3478     }
3479     DisplayManager::GetInstance().UnregisterPrivateWindowListener(displayListener_);
3480     if (captureState_ == AVScreenCaptureState::CREATED || captureState_ == AVScreenCaptureState::POPUP_WINDOW ||
3481         captureState_ == AVScreenCaptureState::STARTING) {
3482         StopNotStartedScreenCapture(stateCode);
3483         return MSERR_OK;
3484     }
3485     CHECK_AND_RETURN_RET(captureState_ != AVScreenCaptureState::STOPPED, MSERR_OK);
3486     int32_t ret = MSERR_OK;
3487     if (captureConfig_.dataType == DataType::CAPTURE_FILE) {
3488         ret = StopScreenCaptureRecorder();
3489     } else if (captureConfig_.dataType == DataType::ORIGINAL_STREAM) {
3490         int32_t retAudio = StopAudioCapture();
3491         int32_t retVideo = StopVideoCapture();
3492         ret = (retAudio == MSERR_OK && retVideo == MSERR_OK) ? MSERR_OK : MSERR_STOP_FAILED;
3493     } else {
3494         MEDIA_LOGW("StopScreenCaptureInner unsupport and ignore");
3495         return MSERR_OK;
3496     }
3497     CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::STARTED, ret, "state:%{public}d", captureState_);
3498     SetErrorInfo(MSERR_OK, "normal stopped", StopReason::NORMAL_STOPPED, IsUserPrivacyAuthorityNeeded());
3499     PostStopScreenCapture(stateCode);
3500 #ifdef SUPPORT_CALL
3501     InCallObserver::GetInstance().UnregisterInCallObserverCallBack(screenCaptureObserverCb_);
3502 #endif
3503     AccountObserver::GetInstance().UnregisterAccountObserverCallBack(screenCaptureObserverCb_);
3504     if (screenCaptureObserverCb_) {
3505         screenCaptureObserverCb_->Release();
3506     }
3507     ScreenManager::GetInstance().UnregisterScreenListener(screenConnectListener_);
3508     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " StopScreenCaptureInner end.", FAKE_POINTER(this));
3509     return ret;
3510 }
3511 
StopNotStartedScreenCapture(AVScreenCaptureStateCode stateCode)3512 void ScreenCaptureServer::StopNotStartedScreenCapture(AVScreenCaptureStateCode stateCode)
3513 {
3514     DestroyPopWindow();
3515     captureState_ = AVScreenCaptureState::STOPPED;
3516     SetSystemScreenRecorderStatus(false);
3517     ScreenCaptureMonitorServer::GetInstance()->CallOnScreenCaptureFinished(appInfo_.appPid);
3518     if (screenCaptureCb_ != nullptr && stateCode != AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INVLID) {
3519         screenCaptureCb_->OnStateChange(stateCode);
3520     }
3521     isSurfaceMode_ = false;
3522     surface_ = nullptr;
3523     SetErrorInfo(MSERR_OK, "normal stopped", StopReason::NORMAL_STOPPED, IsUserPrivacyAuthorityNeeded());
3524 }
3525 
DestroyPopWindow()3526 bool ScreenCaptureServer::DestroyPopWindow()
3527 {
3528     if (captureState_ != AVScreenCaptureState::POPUP_WINDOW) {
3529         MEDIA_LOGI("window not pop up, no need to destroy.");
3530         return true;
3531     }
3532 #ifdef PC_STANDARD
3533     if (captureConfig_.captureMode == CAPTURE_SPECIFIED_SCREEN || CheckCaptureSpecifiedWindowForSelectWindow()) {
3534         MEDIA_LOGI("DestroyPopWindow end, type: picker, deviceType: PC.");
3535         return true;
3536     } else {
3537         if (connection_ != nullptr) {
3538             MEDIA_LOGI("DestroyPopWindow close dialog, deviceType: PC.");
3539             return connection_->CloseDialog();
3540         }
3541     }
3542 #else
3543     if (connection_ != nullptr) {
3544         MEDIA_LOGI("DestroyPopWindow close dialog, deviceType: Phone.");
3545         return connection_->CloseDialog();
3546     }
3547 #endif
3548     return true;
3549 }
3550 
IsLastStartedPidInstance(int32_t pid)3551 bool ScreenCaptureServer::IsLastStartedPidInstance(int32_t pid)
3552 {
3553     MEDIA_LOGI("ScreenCaptureServer::IsLastStartedPidInstance START.");
3554     if (CountStartedScreenCaptureServerNumByPid(pid) != 1) {
3555         MEDIA_LOGD("pid: %{public}d exists more than one instance.", pid);
3556         return false;
3557     }
3558     return true;
3559 }
3560 
LastPidUpdatePrivacyUsingPermissionState(int32_t pid)3561 bool ScreenCaptureServer::LastPidUpdatePrivacyUsingPermissionState(int32_t pid)
3562 {
3563     if (IsLastStartedPidInstance(pid)) {
3564         return UpdatePrivacyUsingPermissionState(STOP_VIDEO);
3565     }
3566     return true;
3567 }
3568 
PostStopScreenCapture(AVScreenCaptureStateCode stateCode)3569 void ScreenCaptureServer::PostStopScreenCapture(AVScreenCaptureStateCode stateCode)
3570 {
3571     MediaTrace trace("ScreenCaptureServer::PostStopScreenCapture");
3572     MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR " PostStopScreenCapture start, stateCode:%{public}d.",
3573         FAKE_POINTER(this), stateCode);
3574     SetSystemScreenRecorderStatus(false);
3575     ScreenCaptureMonitorServer::GetInstance()->CallOnScreenCaptureFinished(appInfo_.appPid);
3576     if (screenCaptureCb_ != nullptr && stateCode != AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INVLID) {
3577         screenCaptureCb_->OnStateChange(stateCode);
3578     }
3579 #ifdef SUPPORT_SCREEN_CAPTURE_WINDOW_NOTIFICATION
3580     if (isPrivacyAuthorityEnabled_ &&
3581         GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"]
3582             .compare(appName_) != 0 && !isScreenCaptureAuthority_) {
3583         // Remove real time notification
3584         int32_t ret = NotificationHelper::CancelNotification(notificationId_);
3585         MEDIA_LOGI("StopScreenCaptureInner CancelNotification id:%{public}d, ret:%{public}d ", notificationId_, ret);
3586         micCount_.store(0);
3587     }
3588 #endif
3589     isPrivacyAuthorityEnabled_ = false;
3590 
3591     if (!LastPidUpdatePrivacyUsingPermissionState(appInfo_.appPid)) {
3592         MEDIA_LOGE("UpdatePrivacyUsingPermissionState STOP failed, dataType:%{public}d", captureConfig_.dataType);
3593     }
3594     std::unordered_map<std::string, std::string> payload;
3595     int64_t value = ResourceSchedule::ResType::ScreenCaptureStatus::STOP_SCREEN_CAPTURE;
3596     ResSchedReportData(value, payload);
3597     RemoveStartedSessionIdList(this->sessionId_);
3598     MEDIA_LOGI("PostStopScreenCapture sessionId: %{public}d is removed from list, list_size is %{public}d.",
3599         this->sessionId_, static_cast<uint32_t>(ScreenCaptureServer::startedSessionIDList_.size()));
3600     captureState_ = AVScreenCaptureState::STOPPED;
3601 }
3602 
StopScreenCapture()3603 int32_t ScreenCaptureServer::StopScreenCapture()
3604 {
3605     MediaTrace trace("ScreenCaptureServer::StopScreenCapture");
3606     MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances StopScreenCapture S", FAKE_POINTER(this));
3607 
3608     if (captureState_ == AVScreenCaptureState::STOPPED) {
3609         MEDIA_LOGI("StopScreenCapture repeat, capture is STOPPED.");
3610         return MSERR_OK;
3611     }
3612     std::lock_guard<std::mutex> lock(mutex_);
3613     int32_t ret = StopScreenCaptureInner(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INVLID);
3614     if (statisticalEventInfo_.startLatency == -1) {
3615         statisticalEventInfo_.captureDuration = -1; // latency -1 means invalid
3616     } else {
3617         int64_t endTime = GetCurrentMillisecond();
3618         statisticalEventInfo_.captureDuration = static_cast<int32_t>(endTime - startTime_ -
3619             statisticalEventInfo_.startLatency);
3620     }
3621 
3622     MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances StopScreenCapture E", FAKE_POINTER(this));
3623     isScreenCaptureAuthority_ = false;
3624     return ret;
3625 }
3626 
Release()3627 void ScreenCaptureServer::Release()
3628 {
3629     ReleaseInner();
3630 }
3631 
ReleaseInner()3632 void ScreenCaptureServer::ReleaseInner()
3633 {
3634     MediaTrace trace("ScreenCaptureServer::ReleaseInner");
3635     MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances ReleaseInner S", FAKE_POINTER(this));
3636     if (captureState_ != AVScreenCaptureState::STOPPED) {
3637         std::lock_guard<std::mutex> lock(mutex_);
3638         if (captureState_ != AVScreenCaptureState::STOPPED) {
3639             StopScreenCaptureInner(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INVLID);
3640             MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances ReleaseInner Stop done, sessionId:%{public}d",
3641                 FAKE_POINTER(this), sessionId_);
3642         }
3643     }
3644     MEDIA_LOGI("ScreenCaptureServer::ReleaseInner before RemoveScreenCaptureServerMap");
3645 
3646     RemoveSaAppInfoMap(saUid_);
3647     RemoveScreenCaptureServerMap(sessionId_);
3648     sessionId_ = SESSION_ID_INVALID;
3649     MEDIA_LOGD("ReleaseInner removeMap success, mapSize: %{public}d",
3650         static_cast<int32_t>(ScreenCaptureServer::serverMap_.size()));
3651     skipPrivacyWindowIDsVec_.clear();
3652     SetMetaDataReport();
3653     MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances ReleaseInner E", FAKE_POINTER(this));
3654 }
3655 
ScreenCaptureObserverCallBack(std::weak_ptr<ScreenCaptureServer> screenCaptureServer)3656 ScreenCaptureObserverCallBack::ScreenCaptureObserverCallBack(
3657     std::weak_ptr<ScreenCaptureServer> screenCaptureServer): taskQueObserverCb_("NotifyStopSc")
3658 {
3659     screenCaptureServer_ = screenCaptureServer;
3660     taskQueObserverCb_.Start();
3661     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
3662 }
3663 
StopAndRelease(AVScreenCaptureStateCode state)3664 bool ScreenCaptureObserverCallBack::StopAndRelease(AVScreenCaptureStateCode state)
3665 {
3666     MEDIA_LOGI("ScreenCaptureObserverCallBack::StopAndRelease");
3667     auto scrServer = screenCaptureServer_.lock();
3668     if (scrServer && !scrServer->IsTelInCallSkipList()) {
3669         if (scrServer->GetSCServerCaptureState() == AVScreenCaptureState::STOPPED) {
3670             MEDIA_LOGI("StopAndRelease repeat, capture is STOPPED.");
3671             return true;
3672         }
3673         scrServer->StopScreenCaptureByEvent(state);
3674         scrServer->Release();
3675     }
3676     return true;
3677 }
3678 
NotifyStopAndRelease(AVScreenCaptureStateCode state)3679 bool ScreenCaptureObserverCallBack::NotifyStopAndRelease(AVScreenCaptureStateCode state)
3680 {
3681     MEDIA_LOGI("ScreenCaptureObserverCallBack::NotifyStopAndRelease START.");
3682     auto task = std::make_shared<TaskHandler<void>>([&, this, state] {
3683         StopAndRelease(state);
3684     });
3685     int32_t res = taskQueObserverCb_.EnqueueTask(task);
3686     CHECK_AND_RETURN_RET_LOG(res == MSERR_OK, false, "NotifyStopAndRelease EnqueueTask failed.");
3687     return true;
3688 }
3689 
3690 #ifdef SUPPORT_CALL
TelCallStateUpdated(bool isInCall)3691 bool ScreenCaptureObserverCallBack::TelCallStateUpdated(bool isInCall)
3692 {
3693     MEDIA_LOGI("ScreenCaptureObserverCallBack::TelCallStateUpdated InCall:%{public}d", isInCall);
3694     auto scrServer = screenCaptureServer_.lock();
3695     if (scrServer && !scrServer->IsTelInCallSkipList()) {
3696         scrServer->TelCallStateUpdated(isInCall);
3697     }
3698     return true;
3699 }
3700 
NotifyTelCallStateUpdated(bool isInCall)3701 bool ScreenCaptureObserverCallBack::NotifyTelCallStateUpdated(bool isInCall)
3702 {
3703     MEDIA_LOGD("ScreenCaptureObserverCallBack::NotifyTelCallStateUpdated START InCall:%{public}d", isInCall);
3704     bool ret = true;
3705     auto task = std::make_shared<TaskHandler<void>>([&, this, isInCall] {
3706         ret = TelCallStateUpdated(isInCall);
3707         return ret;
3708     });
3709     int32_t res = taskQueObserverCb_.EnqueueTask(task);
3710     CHECK_AND_RETURN_RET_LOG(res == MSERR_OK, false, "NotifyTelCallStateUpdated EnqueueTask failed.");
3711     return true;
3712 }
3713 #endif
3714 
Release()3715 void ScreenCaptureObserverCallBack::Release()
3716 {
3717     std::lock_guard<std::mutex> lock(mutex_);
3718     taskQueObserverCb_.Stop();
3719 }
3720 
~ScreenCaptureObserverCallBack()3721 ScreenCaptureObserverCallBack::~ScreenCaptureObserverCallBack()
3722 {
3723     std::lock_guard<std::mutex> lock(mutex_);
3724     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
3725 }
3726 
OnBufferAvailable()3727 void ScreenCapBufferConsumerListener::OnBufferAvailable()
3728 {
3729     MediaTrace trace("ScreenCapConsumer::OnBufferAvailable");
3730     MEDIA_LOGD("ScreenCapConsumer: 0x%{public}06" PRIXPTR " OnBufferAvailable start.", FAKE_POINTER(this));
3731     {
3732         std::lock_guard<std::mutex> lock(bufferAvailableWorkerMtx_);
3733         messageQueueSCB_.push({SCBufferMessageType::GET_BUFFER, "Get buffer!"});
3734         MEDIA_LOGD("ScreenCapConsumer: 0x%{public}06" PRIXPTR " queue size: %{public}" PRId64, FAKE_POINTER(this),
3735             static_cast<uint64_t>(messageQueueSCB_.size()));
3736         bufferAvailableWorkerCv_.notify_one();
3737     }
3738 }
3739 
OnBufferAvailableAction()3740 void ScreenCapBufferConsumerListener::OnBufferAvailableAction()
3741 {
3742     MediaTrace trace("ScreenCapConsumer::OnBufferAvailableAction");
3743     MEDIA_LOGD("OnBufferAvailableAction: 0x%{public}06" PRIXPTR " start.", FAKE_POINTER(this));
3744     CHECK_AND_RETURN(consumer_ != nullptr);
3745     int64_t timestamp = 0;
3746     OHOS::Rect damage;
3747     OHOS::sptr<OHOS::SurfaceBuffer> buffer = nullptr;
3748     sptr<SyncFence> acquireFence = SyncFence::INVALID_FENCE;
3749     int32_t acquireBufferRet = consumer_->AcquireBuffer(buffer, acquireFence, timestamp, damage);
3750     if (acquireBufferRet != GSERROR_OK) {
3751         MEDIA_LOGE("OnBufferAvailableAction AcquireBuffer Fail Code %{public}d", acquireBufferRet);
3752     }
3753     MEDIA_LOGD("OnBufferAvailableAction: 0x%{public}06" PRIXPTR " after AcquireBuffer.", FAKE_POINTER(this));
3754     int32_t flushFence = -1;
3755     if (acquireFence != nullptr && acquireFence != SyncFence::INVALID_FENCE) {
3756         acquireFence->Wait(1000); // 1000 ms
3757         flushFence = acquireFence->Get();
3758     }
3759     CHECK_AND_RETURN_LOG(buffer != nullptr, "Acquire SurfaceBuffer failed");
3760     if ((buffer->GetUsage() & BUFFER_USAGE_MEM_MMZ_CACHE) != 0) {
3761         MEDIA_LOGD("OnBufferAvailableAction cache enable");
3762         buffer->InvalidateCache();
3763     }
3764     void *addr = buffer->GetVirAddr();
3765     if (addr == nullptr) {
3766         MEDIA_LOGE("Acquire SurfaceBuffer address invalid");
3767         int32_t releaseBufferRet = consumer_->ReleaseBuffer(buffer, -1); // -1 not wait
3768         if (releaseBufferRet != GSERROR_OK) {
3769             MEDIA_LOGE("OnBufferAvailableAction: 0x%{public}06" PRIXPTR " ReleaseBuffer Fail Code %{public}d",
3770                 FAKE_POINTER(this), releaseBufferRet);
3771         }
3772         return;
3773     }
3774     MEDIA_LOGD("OnBufferAvailableAction SurfaceBuffer size: %{public}u", buffer->GetSize());
3775     {
3776         std::unique_lock<std::mutex> lock(bufferMutex_);
3777         if (availBuffers_.size() > MAX_BUFFER_SIZE) {
3778             MEDIA_LOGE("OnBufferAvailableAction consume slow, drop video frame");
3779             int32_t releaseBufferRet = consumer_->ReleaseBuffer(buffer, -1); // -1 not wait
3780             if (releaseBufferRet != GSERROR_OK) {
3781                 MEDIA_LOGE("OnBufferAvailableAction: 0x%{public}06" PRIXPTR " consume slow ReleaseBuffer "
3782                     "Fail Code %{public}d", FAKE_POINTER(this), releaseBufferRet);
3783             }
3784             return;
3785         }
3786         availBuffers_.push(std::make_unique<SurfaceBufferEntry>(buffer, flushFence, timestamp, damage));
3787     }
3788     bufferCond_.notify_all();
3789     ProcessVideoBufferCallBack();
3790 }
3791 
SurfaceBufferThreadRun()3792 void ScreenCapBufferConsumerListener::SurfaceBufferThreadRun()
3793 {
3794     SCBufferMessage message;
3795     std::string threadName = std::string("OS_SCBufferAvailableWorker");
3796     MEDIA_LOGD("0x%{public}06" PRIXPTR " BufferAvailableWorker name: %{public}s",
3797         FAKE_POINTER(this), threadName.c_str());
3798     pthread_setname_np(pthread_self(), threadName.c_str());
3799     while (true) {
3800         {
3801             std::unique_lock<std::mutex> lock(bufferAvailableWorkerMtx_);
3802             bufferAvailableWorkerCv_.wait(lock, [&]() { return !messageQueueSCB_.empty(); });
3803             message = messageQueueSCB_.front();
3804             if (static_cast<uint64_t>(messageQueueSCB_.size()) > MAX_MESSAGE_QUEUE_SIZE &&
3805                 message.type == SCBufferMessageType::GET_BUFFER) {
3806                 messageQueueSCB_.pop();
3807                 MEDIA_LOGE("0x%{public}06" PRIXPTR " BufferAvailableWorker skip get buffer", FAKE_POINTER(this));
3808                 continue;
3809             }
3810             messageQueueSCB_.pop();
3811         }
3812         if (message.type == SCBufferMessageType::EXIT) {
3813             break;
3814         }
3815         if (message.type == SCBufferMessageType::GET_BUFFER) {
3816             OnBufferAvailableAction();
3817         }
3818     }
3819     MEDIA_LOGD("ScreenCapBufferConsumerListener::SurfaceBufferThreadRun End.");
3820 }
3821 
StartBufferThread()3822 int32_t ScreenCapBufferConsumerListener::StartBufferThread()
3823 {
3824     if (isSurfaceCbInThreadStopped_.load()) {
3825         surfaceCbInThread_ = new (std::nothrow) std::thread([this]() {
3826             isSurfaceCbInThreadStopped_.store(false);
3827             this->SurfaceBufferThreadRun();
3828             isSurfaceCbInThreadStopped_.store(true);
3829         });
3830         CHECK_AND_RETURN_RET_LOG(surfaceCbInThread_ != nullptr, MSERR_UNKNOWN, "new surface thread failed");
3831     }
3832     MEDIA_LOGI("ScreenCapBufferConsumerListener::StartBufferThread End.");
3833     return MSERR_OK;
3834 }
3835 
ProcessVideoBufferCallBack()3836 void ScreenCapBufferConsumerListener::ProcessVideoBufferCallBack()
3837 {
3838     std::lock_guard<std::mutex> lock(mutex_);
3839     CHECK_AND_RETURN_LOG(screenCaptureCb_ != nullptr, "no consumer, will drop video frame");
3840     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " OnBufferAvailable end.", FAKE_POINTER(this));
3841     screenCaptureCb_->OnVideoBufferAvailable(true);
3842 }
3843 
AcquireVideoBuffer(sptr<OHOS::SurfaceBuffer> & surfaceBuffer,int32_t & fence,int64_t & timestamp,OHOS::Rect & damage)3844 int32_t ScreenCapBufferConsumerListener::AcquireVideoBuffer(sptr<OHOS::SurfaceBuffer> &surfaceBuffer, int32_t &fence,
3845     int64_t &timestamp, OHOS::Rect &damage)
3846 {
3847     MediaTrace trace("ScreenCaptureServer::AcquireVideoBuffer");
3848     using namespace std::chrono_literals;
3849     std::unique_lock<std::mutex> lock(bufferMutex_);
3850     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " AcquireVideoBuffer start, fence:%{public}d, "
3851         "timestamp:%{public}" PRId64, FAKE_POINTER(this), fence, timestamp);
3852     if (!bufferCond_.wait_for(
3853         lock, std::chrono::milliseconds(OPERATION_TIMEOUT_IN_MS), [this] { return !availBuffers_.empty(); })) {
3854         return MSERR_UNKNOWN;
3855     }
3856     surfaceBuffer = availBuffers_.front()->buffer;
3857     fence = availBuffers_.front()->flushFence;
3858     timestamp = availBuffers_.front()->timeStamp;
3859     damage = availBuffers_.front()->damageRect;
3860     MEDIA_LOGD("ScreenCaptureServer: 0x%{public}06" PRIXPTR " AcquireVideoBuffer end.", FAKE_POINTER(this));
3861     return MSERR_OK;
3862 }
3863 
StopBufferThread()3864 void ScreenCapBufferConsumerListener::StopBufferThread()
3865 {
3866     std::lock_guard<std::mutex> lock(bufferAvailableWorkerMtx_);
3867     messageQueueSCB_.push({SCBufferMessageType::EXIT, ""});
3868     MEDIA_LOGI("StopBufferThread: 0x%{public}06" PRIXPTR " EXIT.", FAKE_POINTER(this));
3869     bufferAvailableWorkerCv_.notify_one();
3870 }
3871 
~ScreenCapBufferConsumerListener()3872 ScreenCapBufferConsumerListener::~ScreenCapBufferConsumerListener()
3873 {
3874     MEDIA_LOGD("ScreenCapBufferConsumerListener: 0x%{public}06" PRIXPTR " Destroy.", FAKE_POINTER(this));
3875     StopBufferThread();
3876     if (surfaceCbInThread_ && surfaceCbInThread_->joinable()) {
3877         surfaceCbInThread_->join();
3878         delete surfaceCbInThread_;
3879         surfaceCbInThread_ = nullptr;
3880     }
3881     std::unique_lock<std::mutex> lock(bufferMutex_);
3882     ReleaseBuffer();
3883 }
3884 
ReleaseBuffer()3885 int32_t ScreenCapBufferConsumerListener::ReleaseBuffer()
3886 {
3887     while (!availBuffers_.empty()) {
3888         if (consumer_ != nullptr) {
3889             int32_t releaseBufferRet = consumer_->ReleaseBuffer(availBuffers_.front()->buffer, -1);  // -1 not wait
3890             if (releaseBufferRet != GSERROR_OK) {
3891                 MEDIA_LOGE("ScreenCapBufferConsumerListener: 0x%{public}06" PRIXPTR " ReleaseBuffer "
3892                     "Fail Code %{public}d", FAKE_POINTER(this), releaseBufferRet);
3893             }
3894         }
3895         availBuffers_.pop();
3896     }
3897     return MSERR_OK;
3898 }
3899 
ReleaseVideoBuffer()3900 int32_t ScreenCapBufferConsumerListener::ReleaseVideoBuffer()
3901 {
3902     MediaTrace trace("ScreenCaptureServer::ReleaseVideoBuffer");
3903     std::unique_lock<std::mutex> lock(bufferMutex_);
3904     MEDIA_LOGD("ScreenCapBufferConsumerListener: 0x%{public}06" PRIXPTR " ReleaseVideoBuffer start.",
3905         FAKE_POINTER(this));
3906     CHECK_AND_RETURN_RET_LOG(!availBuffers_.empty(), MSERR_OK, "buffer queue is empty, no video frame to release");
3907 
3908     if (consumer_ != nullptr) {
3909         int32_t releaseBufferRet = consumer_->ReleaseBuffer(availBuffers_.front()->buffer, -1); // -1 not wait
3910         if (releaseBufferRet != GSERROR_OK) {
3911             MEDIA_LOGE("ScreenCapBufferConsumerListener: 0x%{public}06" PRIXPTR " ReleaseVideoBuffer "
3912                 "Fail Code %{public}d", FAKE_POINTER(this), releaseBufferRet);
3913         }
3914     }
3915     availBuffers_.pop();
3916     MEDIA_LOGD("ScreenCapBufferConsumerListener: 0x%{public}06" PRIXPTR " ReleaseVideoBuffer end.", FAKE_POINTER(this));
3917     return MSERR_OK;
3918 }
3919 
Release()3920 int32_t ScreenCapBufferConsumerListener::Release()
3921 {
3922     std::unique_lock<std::mutex> lock(bufferMutex_);
3923     MEDIA_LOGI("ScreenCapBufferConsumerListener Release");
3924     return ReleaseBuffer();
3925 }
3926 
SetAudioSource(std::shared_ptr<AudioDataSource> audioSource)3927 void ScreenRendererAudioStateChangeCallback::SetAudioSource(std::shared_ptr<AudioDataSource> audioSource)
3928 {
3929     audioSource_ = audioSource;
3930 }
3931 
SetAppName(std::string appName)3932 void ScreenRendererAudioStateChangeCallback::SetAppName(std::string appName)
3933 {
3934     appName_ = appName;
3935 }
3936 
OnRendererStateChange(const std::vector<std::shared_ptr<AudioRendererChangeInfo>> & audioRendererChangeInfos)3937 void ScreenRendererAudioStateChangeCallback::OnRendererStateChange(
3938     const std::vector<std::shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos)
3939 {
3940     MEDIA_LOGD("ScreenRendererAudioStateChangeCallback IN");
3941     CHECK_AND_RETURN(audioSource_ != nullptr);
3942     audioSource_->SpeakerStateUpdate(audioRendererChangeInfos);
3943 #ifdef SUPPORT_CALL
3944     audioSource_->TelCallAudioStateUpdate(audioRendererChangeInfos);
3945 #endif
3946     std::string region = Global::I18n::LocaleConfig::GetSystemRegion();
3947     if (GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"]
3948             .compare(appName_) == 0 && region == "CN") {
3949         audioSource_->VoIPStateUpdate(audioRendererChangeInfos);
3950     }
3951 }
3952 
SpeakerStateUpdate(const std::vector<std::shared_ptr<AudioRendererChangeInfo>> & audioRendererChangeInfos)3953 void AudioDataSource::SpeakerStateUpdate(
3954     const std::vector<std::shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos)
3955 {
3956     (void)audioRendererChangeInfos;
3957     std::vector<std::shared_ptr<AudioRendererChangeInfo>> allAudioRendererChangeInfos;
3958     AudioStreamManager::GetInstance()->GetCurrentRendererChangeInfos(allAudioRendererChangeInfos);
3959     uint32_t changeInfoSize = allAudioRendererChangeInfos.size();
3960     if (changeInfoSize == 0) {
3961         return;
3962     }
3963     bool speakerAlive = HasSpeakerStream(allAudioRendererChangeInfos);
3964     if (speakerAlive != speakerAliveStatus_) {
3965         speakerAliveStatus_ = speakerAlive;
3966         CHECK_AND_RETURN(screenCaptureServer_ != nullptr);
3967         screenCaptureServer_->OnSpeakerAliveStatusChanged(speakerAlive);
3968         if (speakerAlive) {
3969             MEDIA_LOGI("HEADSET Change to Speaker.");
3970         } else {
3971             MEDIA_LOGI("Speaker Change to HEADSET.");
3972         }
3973     }
3974 }
3975 
3976 #ifdef SUPPORT_CALL
TelCallAudioStateUpdate(const std::vector<std::shared_ptr<AudioRendererChangeInfo>> & audioRendererChangeInfos)3977 void AudioDataSource::TelCallAudioStateUpdate(
3978     const std::vector<std::shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos)
3979 {
3980     CHECK_AND_RETURN(screenCaptureServer_ != nullptr);
3981     (void)audioRendererChangeInfos;
3982     std::vector<std::shared_ptr<AudioRendererChangeInfo>> allAudioRendererChangeInfos;
3983     AudioStreamManager::GetInstance()->GetCurrentRendererChangeInfos(allAudioRendererChangeInfos);
3984     for (const std::shared_ptr<AudioRendererChangeInfo> &changeInfo: allAudioRendererChangeInfos) {
3985         if (!changeInfo) {
3986             continue;
3987         }
3988         MEDIA_LOGD("Client pid : %{public}d, State : %{public}d, usage : %{public}d",
3989             changeInfo->clientPid, static_cast<int32_t>(changeInfo->rendererState),
3990             static_cast<int32_t>(changeInfo->rendererInfo.streamUsage));
3991         if (changeInfo->rendererInfo.streamUsage ==
3992             AudioStandard::StreamUsage::STREAM_USAGE_VOICE_MODEM_COMMUNICATION &&
3993             (changeInfo->rendererState == RendererState::RENDERER_RUNNING ||
3994             changeInfo->rendererState == RendererState::RENDERER_PREPARED)) {
3995             screenCaptureServer_->TelCallAudioStateUpdated(true);
3996             return;
3997         }
3998     }
3999     screenCaptureServer_->TelCallAudioStateUpdated(false);
4000 }
4001 #endif
4002 
HasSpeakerStream(const std::vector<std::shared_ptr<AudioRendererChangeInfo>> & audioRendererChangeInfos)4003 bool AudioDataSource::HasSpeakerStream(
4004     const std::vector<std::shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos)
4005 {
4006     uint32_t changeInfoIndex = 0;
4007     uint32_t headSetCount = 0;
4008     bool hasSpeakerStream = true;
4009     for (const std::shared_ptr<AudioRendererChangeInfo> &changeInfo: audioRendererChangeInfos) {
4010         if (!changeInfo) {
4011             continue;
4012         }
4013         MEDIA_LOGD("ChangeInfo Id: %{public}d, Client pid : %{public}d, State : %{public}d, DeviceType : %{public}d",
4014             changeInfoIndex, changeInfo->clientPid, static_cast<int32_t>(changeInfo->rendererState),
4015             static_cast<int32_t>(changeInfo->outputDeviceInfo.deviceType_));
4016         if (changeInfo->outputDeviceInfo.deviceType_ == DEVICE_TYPE_WIRED_HEADSET ||
4017             changeInfo->outputDeviceInfo.deviceType_ == DEVICE_TYPE_WIRED_HEADPHONES ||
4018             changeInfo->outputDeviceInfo.deviceType_ == DEVICE_TYPE_BLUETOOTH_SCO ||
4019             changeInfo->outputDeviceInfo.deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP ||
4020             changeInfo->outputDeviceInfo.deviceType_ == DEVICE_TYPE_USB_HEADSET ||
4021             changeInfo->outputDeviceInfo.deviceType_ == DEVICE_TYPE_USB_ARM_HEADSET) {
4022             headSetCount++;
4023         }
4024         changeInfoIndex++;
4025     }
4026     if (headSetCount == changeInfoIndex) { // only if all streams in headset
4027         hasSpeakerStream = false;
4028     }
4029     return hasSpeakerStream;
4030 }
4031 
VoIPStateUpdate(const std::vector<std::shared_ptr<AudioRendererChangeInfo>> & audioRendererChangeInfos)4032 void AudioDataSource::VoIPStateUpdate(
4033     const std::vector<std::shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos)
4034 {
4035     std::lock_guard<std::mutex> lock(voipStatusChangeMutex_);
4036     (void)audioRendererChangeInfos;
4037     std::vector<std::shared_ptr<AudioRendererChangeInfo>> allAudioRendererChangeInfos;
4038     AudioStreamManager::GetInstance()->GetCurrentRendererChangeInfos(allAudioRendererChangeInfos);
4039     bool isInVoIPCall = HasVoIPStream(allAudioRendererChangeInfos);
4040     if (isInVoIPCall_.load() == isInVoIPCall) {
4041         return;
4042     }
4043     isInVoIPCall_.store(isInVoIPCall);
4044     CHECK_AND_RETURN(screenCaptureServer_ != nullptr);
4045     screenCaptureServer_->OnVoIPStatusChanged(isInVoIPCall);
4046 }
4047 
HasVoIPStream(const std::vector<std::shared_ptr<AudioRendererChangeInfo>> & audioRendererChangeInfos)4048 bool AudioDataSource::HasVoIPStream(
4049     const std::vector<std::shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos)
4050 {
4051     for (const std::shared_ptr<AudioRendererChangeInfo> &changeInfo: audioRendererChangeInfos) {
4052         if (!changeInfo) {
4053             continue;
4054         }
4055         MEDIA_LOGD("Client pid : %{public}d, State : %{public}d, usage : %{public}d",
4056             changeInfo->clientPid, static_cast<int32_t>(changeInfo->rendererState),
4057             static_cast<int32_t>(changeInfo->rendererInfo.streamUsage));
4058         if (changeInfo->rendererState == RendererState::RENDERER_RUNNING &&
4059             changeInfo->rendererInfo.streamUsage == AudioStandard::StreamUsage::STREAM_USAGE_VOICE_COMMUNICATION) {
4060             return true;
4061         }
4062     }
4063     return false;
4064 }
4065 
SetAppPid(int32_t appid)4066 void AudioDataSource::SetAppPid(int32_t appid)
4067 {
4068     appPid_ = appid;
4069 }
4070 
GetAppPid()4071 int32_t AudioDataSource::GetAppPid()
4072 {
4073     return appPid_ ;
4074 }
4075 
GetIsInVoIPCall()4076 bool AudioDataSource::GetIsInVoIPCall()
4077 {
4078     return isInVoIPCall_.load();
4079 }
4080 
GetSpeakerAliveStatus()4081 bool AudioDataSource::GetSpeakerAliveStatus()
4082 {
4083     return speakerAliveStatus_;
4084 }
4085 
SetAppName(std::string appName)4086 void AudioDataSource::SetAppName(std::string appName)
4087 {
4088     appName_ = appName;
4089 }
4090 
RegisterAudioRendererEventListener(const int32_t clientPid,const std::shared_ptr<AudioRendererStateChangeCallback> & callback)4091 int32_t AudioDataSource::RegisterAudioRendererEventListener(const int32_t clientPid,
4092     const std::shared_ptr<AudioRendererStateChangeCallback> &callback)
4093 {
4094     CHECK_AND_RETURN_RET_LOG(callback != nullptr, MSERR_INVALID_VAL, "audio callback is null");
4095     int32_t ret = AudioStreamManager::GetInstance()->RegisterAudioRendererEventListener(clientPid, callback);
4096     std::vector<std::shared_ptr<AudioRendererChangeInfo>> audioRendererChangeInfos;
4097     SpeakerStateUpdate(audioRendererChangeInfos);
4098     std::string region = Global::I18n::LocaleConfig::GetSystemRegion();
4099     if (GetScreenCaptureSystemParam()["const.multimedia.screencapture.screenrecorderbundlename"]
4100             .compare(appName_) == 0 && region == "CN") {
4101         VoIPStateUpdate(audioRendererChangeInfos);
4102     }
4103     return ret;
4104 }
4105 
UnregisterAudioRendererEventListener(const int32_t clientPid)4106 int32_t AudioDataSource::UnregisterAudioRendererEventListener(const int32_t clientPid)
4107 {
4108     MEDIA_LOGI("client id: %{public}d", clientPid);
4109     return AudioStreamManager::GetInstance()->UnregisterAudioRendererEventListener(clientPid);
4110 }
4111 
ReadAt(std::shared_ptr<AVBuffer> buffer,uint32_t length)4112 int32_t AudioDataSource::ReadAt(std::shared_ptr<AVBuffer> buffer, uint32_t length)
4113 {
4114     MEDIA_LOGD("AudioDataSource ReadAt start");
4115     std::shared_ptr<AudioBuffer> innerAudioBuffer = nullptr;
4116     std::shared_ptr<AudioBuffer> micAudioBuffer = nullptr;
4117     int32_t ret = MSERR_OK;
4118     if (screenCaptureServer_ == nullptr) {
4119         return MSERR_UNKNOWN;
4120     }
4121     ret = screenCaptureServer_->AcquireAudioBufferMix(innerAudioBuffer, micAudioBuffer, type_);
4122     if (ret != MSERR_OK) {
4123         return ret;
4124     }
4125     MEDIA_LOGD("AcquireAudioBufferMix sucess");
4126     std::shared_ptr<AVMemory> &bufferMem = buffer->memory_;
4127     if (buffer->memory_ == nullptr) {
4128         MEDIA_LOGE("buffer->memory_ is nullptr");
4129         return MSERR_INVALID_VAL;
4130     }
4131     if (type_ == AVScreenCaptureMixMode::MIX_MODE) {
4132         return MixModeBufferWrite(innerAudioBuffer, micAudioBuffer, bufferMem);
4133     } else if (type_ == AVScreenCaptureMixMode::INNER_MODE && innerAudioBuffer != nullptr) {
4134         bufferMem->Write(reinterpret_cast<uint8_t*>(innerAudioBuffer->buffer), innerAudioBuffer->length, 0);
4135         return screenCaptureServer_->ReleaseAudioBufferMix(type_);
4136     } else if (type_ == AVScreenCaptureMixMode::MIC_MODE && micAudioBuffer != nullptr) {
4137         bufferMem->Write(reinterpret_cast<uint8_t*>(micAudioBuffer->buffer), micAudioBuffer->length, 0);
4138         return screenCaptureServer_->ReleaseAudioBufferMix(type_);
4139     }
4140     return MSERR_UNKNOWN;
4141 }
4142 
MixModeBufferWrite(std::shared_ptr<AudioBuffer> & innerAudioBuffer,std::shared_ptr<AudioBuffer> & micAudioBuffer,std::shared_ptr<AVMemory> & bufferMem)4143 int32_t AudioDataSource::MixModeBufferWrite(std::shared_ptr<AudioBuffer> &innerAudioBuffer,
4144     std::shared_ptr<AudioBuffer> &micAudioBuffer, std::shared_ptr<AVMemory> &bufferMem)
4145 {
4146     if (innerAudioBuffer && innerAudioBuffer->buffer && micAudioBuffer && micAudioBuffer->buffer) {
4147         char* mixData = new char[innerAudioBuffer->length];
4148         char* srcData[2] = {nullptr};
4149         srcData[0] = reinterpret_cast<char*>(innerAudioBuffer->buffer);
4150         srcData[1] = reinterpret_cast<char*>(micAudioBuffer->buffer);
4151         int channels = 2;
4152         MixAudio(srcData, mixData, channels, innerAudioBuffer->length);
4153         bufferMem->Write(reinterpret_cast<uint8_t*>(mixData), innerAudioBuffer->length, 0);
4154         delete[] mixData;
4155     } else if (innerAudioBuffer && innerAudioBuffer->buffer) {
4156         bufferMem->Write(reinterpret_cast<uint8_t*>(innerAudioBuffer->buffer), innerAudioBuffer->length, 0);
4157     } else if (micAudioBuffer && micAudioBuffer->buffer) {
4158         bufferMem->Write(reinterpret_cast<uint8_t*>(micAudioBuffer->buffer), micAudioBuffer->length, 0);
4159     } else {
4160         MEDIA_LOGE("without buffer write");
4161         return MSERR_UNKNOWN;
4162     }
4163     if (innerAudioBuffer) {
4164         if (screenCaptureServer_->ReleaseInnerAudioBuffer() != MSERR_OK) {
4165             MEDIA_LOGE("ReleaseInnerAudioBuffer failed");
4166         }
4167     }
4168     if (micAudioBuffer) {
4169         if (screenCaptureServer_->ReleaseMicAudioBuffer() != MSERR_OK) {
4170             MEDIA_LOGE("ReleaseMicAudioBuffer failed");
4171         }
4172     }
4173     return MSERR_OK;
4174 }
4175 
GetSize(int64_t & size)4176 int32_t AudioDataSource::GetSize(int64_t &size)
4177 {
4178     size_t bufferLen = 0;
4179     int32_t ret = screenCaptureServer_->GetInnerAudioCaptureBufferSize(bufferLen);
4180     MEDIA_LOGD("AudioDataSource::GetSize : %{public}zu", bufferLen);
4181     if (ret == MSERR_OK) {
4182         size = static_cast<int64_t>(bufferLen);
4183         return ret;
4184     }
4185     ret = screenCaptureServer_->GetMicAudioCaptureBufferSize(bufferLen);
4186     MEDIA_LOGD("AudioDataSource::GetSize : %{public}zu", bufferLen);
4187     if (ret == MSERR_OK) {
4188         size = static_cast<int64_t>(bufferLen);
4189         return ret;
4190     }
4191     return ret;
4192 }
4193 
MixAudio(char ** srcData,char * mixData,int channels,int bufferSize)4194 void AudioDataSource::MixAudio(char** srcData, char* mixData, int channels, int bufferSize)
4195 {
4196     MEDIA_LOGD("AudioDataSource MixAudio");
4197     int const max = 32767;
4198     int const min = -32768;
4199     double const splitNum = 32;
4200     int const doubleChannels = 2;
4201     double coefficient = 1;
4202     int totalNum = 0;
4203     if (channels == 0) {
4204         return;
4205     }
4206     for (totalNum = 0; totalNum < bufferSize / channels; totalNum++) {
4207         int temp = 0;
4208         for (int channelNum = 0; channelNum < channels; channelNum++) {
4209             temp += *reinterpret_cast<short*>(srcData[channelNum] + totalNum * channels);
4210         }
4211         int32_t output = static_cast<int32_t>(temp * coefficient);
4212         if (output > max) {
4213             coefficient = static_cast<double>(max) / static_cast<double>(output);
4214             output = max;
4215         }
4216         if (output < min) {
4217             coefficient = static_cast<double>(min) / static_cast<double>(output);
4218             output = min;
4219         }
4220         if (coefficient < 1) {
4221             coefficient += (static_cast<double>(1) - coefficient) / splitNum;
4222         }
4223         *reinterpret_cast<short*>(mixData + totalNum * doubleChannels) = static_cast<short>(output);
4224     }
4225 }
4226 } // namespace Media
4227 } // namespace OHOS
4228