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