1 /*
2 * Copyright (C) 2021 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 "media_server_manager.h"
17 #include <unordered_set>
18 #ifdef SUPPORT_RECORDER
19 #include "recorder_service_stub.h"
20 #include "recorder_profiles_service_stub.h"
21 #endif
22 #ifdef SUPPORT_TRANSCODER
23 #include "transcoder_service_stub.h"
24 #endif
25 #ifdef SUPPORT_PLAYER
26 #include "player_service_stub.h"
27 #ifdef PLAYER_USE_MEMORY_MANAGE
28 #include "player_service_stub_mem.h"
29 #endif
30 #endif
31 #ifdef SUPPORT_METADATA
32 #include "avmetadatahelper_service_stub.h"
33 #endif
34 #ifdef SUPPORT_SCREEN_CAPTURE
35 #include "screen_capture_service_stub.h"
36 #include "screen_capture_monitor_service_stub.h"
37 #include "screen_capture_controller_stub.h"
38 #endif
39 #include "monitor_service_stub.h"
40 #include "media_log.h"
41 #include "media_errors.h"
42 #include "media_dfx.h"
43 #include "service_dump_manager.h"
44 #include "player_xcollie.h"
45
46 namespace {
47 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_PLAYER, "MediaServerManager"};
48 }
49
50 namespace OHOS {
51 namespace Media {
52 constexpr uint32_t SERVER_MAX_NUMBER = 16;
GetInstance()53 MediaServerManager &MediaServerManager::GetInstance()
54 {
55 static MediaServerManager instance;
56 return instance;
57 }
58
WriteInfo(int32_t fd,std::string & dumpString,std::vector<Dumper> dumpers,bool needDetail)59 int32_t WriteInfo(int32_t fd, std::string &dumpString, std::vector<Dumper> dumpers, bool needDetail)
60 {
61 int32_t i = 0;
62 for (auto iter : dumpers) {
63 dumpString += "-----Instance #" + std::to_string(i) + ": ";
64 dumpString += "pid = ";
65 dumpString += std::to_string(iter.pid_);
66 dumpString += " uid = ";
67 dumpString += std::to_string(iter.uid_);
68 dumpString += "-----\n";
69 if (fd != -1) {
70 write(fd, dumpString.c_str(), dumpString.size());
71 dumpString.clear();
72 }
73 i++;
74 if (needDetail && iter.entry_(fd) != MSERR_OK) {
75 return OHOS::INVALID_OPERATION;
76 }
77 }
78 if (fd != -1) {
79 write(fd, dumpString.c_str(), dumpString.size());
80 } else {
81 MEDIA_LOGI_NO_RELEASE("%{public}s", dumpString.c_str());
82 }
83 dumpString.clear();
84
85 return OHOS::NO_ERROR;
86 }
87
Dump(int32_t fd,const std::vector<std::u16string> & args)88 int32_t MediaServerManager::Dump(int32_t fd, const std::vector<std::u16string> &args)
89 {
90 std::string dumpString;
91 std::unordered_set<std::u16string> argSets;
92 for (decltype(args.size()) index = 0; index < args.size(); ++index) {
93 argSets.insert(args[index]);
94 }
95
96 dumpString += "------------------PlayerServer------------------\n";
97 auto ret = WriteInfo(fd, dumpString, dumperTbl_[StubType::PLAYER],
98 argSets.find(u"player") != argSets.end());
99 CHECK_AND_RETURN_RET_LOG(ret == NO_ERROR,
100 OHOS::INVALID_OPERATION, "Failed to write PlayerServer information");
101
102 dumpString += "------------------RecorderServer------------------\n";
103 ret = WriteInfo(fd, dumpString, dumperTbl_[StubType::RECORDER],
104 argSets.find(u"recorder") != argSets.end());
105 CHECK_AND_RETURN_RET_LOG(ret == NO_ERROR,
106 OHOS::INVALID_OPERATION, "Failed to write RecorderServer information");
107
108 dumpString += "------------------CodecServer------------------\n";
109 ret = WriteInfo(fd, dumpString, dumperTbl_[StubType::AVCODEC],
110 argSets.find(u"codec") != argSets.end());
111 CHECK_AND_RETURN_RET_LOG(ret == NO_ERROR,
112 OHOS::INVALID_OPERATION, "Failed to write CodecServer information");
113
114 dumpString += "------------------AVMetaServer------------------\n";
115 ret = WriteInfo(fd, dumpString, dumperTbl_[StubType::AVMETADATAHELPER], false);
116 CHECK_AND_RETURN_RET_LOG(ret == NO_ERROR,
117 OHOS::INVALID_OPERATION, "Failed to write AVMetaServer information");
118
119 dumpString += "------------------TranscoderServer------------------\n";
120 ret = WriteInfo(fd, dumpString, dumperTbl_[StubType::TRANSCODER],
121 argSets.find(u"transcoder") != argSets.end());
122 CHECK_AND_RETURN_RET_LOG(ret == NO_ERROR,
123 OHOS::INVALID_OPERATION, "Failed to write Transcoder information");
124
125 dumpString += "------------------ScreenCaptureServer------------------\n";
126 ret = WriteInfo(fd, dumpString, dumperTbl_[StubType::SCREEN_CAPTURE],
127 argSets.find(u"screencapture") != argSets.end());
128 CHECK_AND_RETURN_RET_LOG(ret == NO_ERROR,
129 OHOS::INVALID_OPERATION, "Failed to write ScreenCapture information");
130
131 ret = ServiceDumpManager::GetInstance().Dump(fd, argSets);
132 CHECK_AND_RETURN_RET_LOG(ret == NO_ERROR,
133 OHOS::INVALID_OPERATION, "Failed to write dfx dump information");
134
135 ret = PlayerXCollie::GetInstance().Dump(fd);
136 CHECK_AND_RETURN_RET_LOG(ret == NO_ERROR,
137 OHOS::INVALID_OPERATION, "Failed to write xcollie dump information");
138
139 ret = MonitorServiceStub::GetInstance()->DumpInfo(fd,
140 argSets.find(u"monitor") != argSets.end());
141 CHECK_AND_RETURN_RET_LOG(ret == NO_ERROR,
142 OHOS::INVALID_OPERATION, "Failed to write monitor dump information");
143 return OHOS::NO_ERROR;
144 }
145
MediaServerManager()146 MediaServerManager::MediaServerManager()
147 {
148 MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
149 }
150
~MediaServerManager()151 MediaServerManager::~MediaServerManager()
152 {
153 dumperTbl_.clear();
154 recorderStubMap_.clear();
155 playerStubMap_.clear();
156 avMetadataHelperStubMap_.clear();
157 avCodecListStubMap_.clear();
158 avCodecStubMap_.clear();
159 recorderProfilesStubMap_.clear();
160 screenCaptureStubMap_.clear();
161 screenCaptureMonitorStubMap_.clear();
162 screenCaptureControllerStubMap_.clear();
163 MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
164 }
165
CreateStubObject(StubType type)166 sptr<IRemoteObject> MediaServerManager::CreateStubObject(StubType type)
167 {
168 std::lock_guard<std::mutex> lock(mutex_);
169 switch (type) {
170 #ifdef SUPPORT_RECORDER
171 case RECORDER:
172 return CreateRecorderStubObject();
173 case RECORDERPROFILES:
174 return CreateRecorderProfilesStubObject();
175 #endif
176 #ifdef SUPPORT_TRANSCODER
177 case TRANSCODER:
178 return CreateTransCoderStubObject();
179 #endif
180 #ifdef SUPPORT_PLAYER
181 case PLAYER:
182 return CreatePlayerStubObject();
183 #endif
184 #ifdef SUPPORT_METADATA
185 case AVMETADATAHELPER:
186 return CreateAVMetadataHelperStubObject();
187 #endif
188 #ifdef SUPPORT_SCREEN_CAPTURE
189 case SCREEN_CAPTURE:
190 return CreateScreenCaptureStubObject();
191 case SCREEN_CAPTURE_MONITOR:
192 return CreateScreenCaptureMonitorStubObject();
193 case SCREEN_CAPTURE_CONTROLLER:
194 return CreateScreenCaptureControllerStubObject();
195 #endif
196 case MONITOR:
197 return GetMonitorStubObject();
198 default:
199 MEDIA_LOGE("default case, media server manager failed");
200 return nullptr;
201 }
202 }
203
204 #ifdef SUPPORT_PLAYER
CreatePlayerStubObject()205 sptr<IRemoteObject> MediaServerManager::CreatePlayerStubObject()
206 {
207 #ifdef PLAYER_USE_MEMORY_MANAGE
208 sptr<PlayerServiceStub> playerStub;
209 if (isMemMgrLoaded_.load()) {
210 playerStub = PlayerServiceStubMem::Create();
211 } else {
212 MEDIA_LOGW("create player stub object when memmgr has not been loaded");
213 playerStub = PlayerServiceStub::Create();
214 }
215 #else
216 sptr<PlayerServiceStub> playerStub = PlayerServiceStub::Create();
217 #endif
218 CHECK_AND_RETURN_RET_LOG(playerStub != nullptr, nullptr, "failed to create PlayerServiceStub");
219
220 sptr<IRemoteObject> object = playerStub->AsObject();
221 CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to create PlayerServiceStub");
222
223 pid_t pid = IPCSkeleton::GetCallingPid();
224 playerStubMap_[object] = pid;
225
226 Dumper dumper;
227 dumper.entry_ = [player = playerStub](int32_t fd) -> int32_t {
228 return player->DumpInfo(fd);
229 };
230 dumper.pid_ = pid;
231 dumper.uid_ = IPCSkeleton::GetCallingUid();
232 dumper.remoteObject_ = object;
233 dumperTbl_[StubType::PLAYER].emplace_back(dumper);
234 MEDIA_LOGD("The number of player services(%{public}zu) pid(%{public}d).",
235 playerStubMap_.size(), pid);
236 MediaTrace::CounterTrace("The number of player", playerStubMap_.size());
237 (void)Dump(-1, std::vector<std::u16string>());
238 return object;
239 }
240 #endif
241
242 #ifdef SUPPORT_RECORDER
CreateRecorderStubObject()243 sptr<IRemoteObject> MediaServerManager::CreateRecorderStubObject()
244 {
245 sptr<RecorderServiceStub> recorderStub = RecorderServiceStub::Create();
246 CHECK_AND_RETURN_RET_LOG(recorderStub != nullptr, nullptr, "failed to create RecorderServiceStub");
247
248 sptr<IRemoteObject> object = recorderStub->AsObject();
249 CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to create RecorderServiceStub");
250
251 pid_t pid = IPCSkeleton::GetCallingPid();
252 recorderStubMap_[object] = pid;
253
254 Dumper dumper;
255 dumper.entry_ = [recorder = recorderStub](int32_t fd) -> int32_t {
256 return recorder->DumpInfo(fd);
257 };
258 dumper.pid_ = pid;
259 dumper.uid_ = IPCSkeleton::GetCallingUid();
260 dumper.remoteObject_ = object;
261 dumperTbl_[StubType::RECORDER].emplace_back(dumper);
262 MEDIA_LOGD("The number of recorder services(%{public}zu) pid(%{public}d).",
263 recorderStubMap_.size(), pid);
264 (void)Dump(-1, std::vector<std::u16string>());
265 return object;
266 }
267
CreateRecorderProfilesStubObject()268 sptr<IRemoteObject> MediaServerManager::CreateRecorderProfilesStubObject()
269 {
270 CHECK_AND_RETURN_RET_LOG(recorderProfilesStubMap_.size() < SERVER_MAX_NUMBER,
271 nullptr, "The number of recorder_profiles services(%{public}zu) has reached the upper limit."
272 "Please release the applied resources.", recorderProfilesStubMap_.size());
273
274 sptr<RecorderProfilesServiceStub> recorderProfilesStub = RecorderProfilesServiceStub::Create();
275 CHECK_AND_RETURN_RET_LOG(recorderProfilesStub != nullptr, nullptr,
276 "failed to create recorderProfilesStub");
277
278 sptr<IRemoteObject> object = recorderProfilesStub->AsObject();
279 CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to create recorderProfilesStub");
280
281 pid_t pid = IPCSkeleton::GetCallingPid();
282 recorderProfilesStubMap_[object] = pid;
283 MEDIA_LOGD("The number of recorder_profiles services(%{public}zu).", recorderProfilesStubMap_.size());
284 return object;
285 }
286 #endif
287
288 #ifdef SUPPORT_TRANSCODER
CreateTransCoderStubObject()289 sptr<IRemoteObject> MediaServerManager::CreateTransCoderStubObject()
290 {
291 sptr<TransCoderServiceStub> transCoderStub = TransCoderServiceStub::Create();
292 CHECK_AND_RETURN_RET_LOG(transCoderStub != nullptr, nullptr, "failed to create TransCoderServiceStub");
293
294 sptr<IRemoteObject> object = transCoderStub->AsObject();
295 CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to create TransCoderServiceStub");
296
297 pid_t pid = IPCSkeleton::GetCallingPid();
298 transCoderStubMap_[object] = pid;
299
300 Dumper dumper;
301 dumper.entry_ = [transCoder = transCoderStub](int32_t fd) -> int32_t {
302 return transCoder->DumpInfo(fd);
303 };
304 dumper.pid_ = pid;
305 dumper.uid_ = IPCSkeleton::GetCallingUid();
306 dumper.remoteObject_ = object;
307 dumperTbl_[StubType::TRANSCODER].emplace_back(dumper);
308 MEDIA_LOGD("The number of transCoder services(%{public}zu) pid(%{public}d).",
309 transCoderStubMap_.size(), pid);
310 (void)Dump(-1, std::vector<std::u16string>());
311 return object;
312 }
313 #endif
314
315 #ifdef SUPPORT_METADATA
CreateAVMetadataHelperStubObject()316 sptr<IRemoteObject> MediaServerManager::CreateAVMetadataHelperStubObject()
317 {
318 sptr<AVMetadataHelperServiceStub> avMetadataHelperStub = AVMetadataHelperServiceStub::Create();
319 CHECK_AND_RETURN_RET_LOG(avMetadataHelperStub != nullptr, nullptr,
320 "failed to create AVMetadataHelperServiceStub");
321
322 sptr<IRemoteObject> object = avMetadataHelperStub->AsObject();
323 CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr,
324 "failed to create AVMetadataHelperServiceStub");
325
326 pid_t pid = IPCSkeleton::GetCallingPid();
327 avMetadataHelperStubMap_[object] = pid;
328
329 Dumper dumper;
330 dumper.pid_ = pid;
331 dumper.uid_ = IPCSkeleton::GetCallingUid();
332 dumper.remoteObject_ = object;
333 dumperTbl_[StubType::AVMETADATAHELPER].emplace_back(dumper);
334
335 MEDIA_LOGD("The number of avmetadatahelper services(%{public}zu) pid(%{public}d).",
336 avMetadataHelperStubMap_.size(), pid);
337 (void)Dump(-1, std::vector<std::u16string>());
338 return object;
339 }
340 #endif
341
342 #ifdef SUPPORT_SCREEN_CAPTURE
CreateScreenCaptureStubObject()343 sptr<IRemoteObject> MediaServerManager::CreateScreenCaptureStubObject()
344 {
345 sptr<ScreenCaptureServiceStub> screenCaptureStub = ScreenCaptureServiceStub::Create();
346 CHECK_AND_RETURN_RET_LOG(screenCaptureStub != nullptr, nullptr,
347 "failed to create ScreenCaptureServiceStub");
348
349 sptr<IRemoteObject> object = screenCaptureStub->AsObject();
350 CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to create ScreenCaptureServiceStub");
351
352 pid_t pid = IPCSkeleton::GetCallingPid();
353 screenCaptureStubMap_[object] = pid;
354
355 Dumper dumper;
356 dumper.pid_ = pid;
357 dumper.uid_ = IPCSkeleton::GetCallingUid();
358 dumper.remoteObject_ = object;
359 dumperTbl_[StubType::SCREEN_CAPTURE].emplace_back(dumper);
360 MEDIA_LOGD("The number of screen capture services(%{public}zu).", screenCaptureStubMap_.size());
361 (void)Dump(-1, std::vector<std::u16string>());
362 return object;
363 }
364
CreateScreenCaptureMonitorStubObject()365 sptr<IRemoteObject> MediaServerManager::CreateScreenCaptureMonitorStubObject()
366 {
367 sptr<ScreenCaptureMonitorServiceStub> screenCaptureMonitorStub = ScreenCaptureMonitorServiceStub::Create();
368 CHECK_AND_RETURN_RET_LOG(screenCaptureMonitorStub != nullptr, nullptr,
369 "failed to create ScreenCaptureMonitorServiceStub");
370
371 sptr<IRemoteObject> object = screenCaptureMonitorStub->AsObject();
372 CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to create ScreenCaptureMonitorServiceStub");
373
374 pid_t pid = IPCSkeleton::GetCallingPid();
375 screenCaptureMonitorStubMap_[object] = pid;
376
377 Dumper dumper;
378 dumper.pid_ = pid;
379 dumper.uid_ = IPCSkeleton::GetCallingUid();
380 dumper.remoteObject_ = object;
381 dumperTbl_[StubType::SCREEN_CAPTURE_MONITOR].emplace_back(dumper);
382 MEDIA_LOGD("The number of screen capture monitor services(%{public}zu).", screenCaptureMonitorStubMap_.size());
383 (void)Dump(-1, std::vector<std::u16string>());
384 return object;
385 }
386
CreateScreenCaptureControllerStubObject()387 sptr<IRemoteObject> MediaServerManager::CreateScreenCaptureControllerStubObject()
388 {
389 sptr<ScreenCaptureControllerStub> screenCaptureControllerStub = ScreenCaptureControllerStub::Create();
390 CHECK_AND_RETURN_RET_LOG(screenCaptureControllerStub != nullptr, nullptr,
391 "failed to create ScreenCaptureControllerStub");
392
393 sptr<IRemoteObject> object = screenCaptureControllerStub->AsObject();
394 CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to create screenCaptureControllerStub");
395
396 pid_t pid = IPCSkeleton::GetCallingPid();
397 screenCaptureControllerStubMap_[object] = pid;
398 MEDIA_LOGD("The number of screen capture services(%{public}zu).", screenCaptureControllerStubMap_.size());
399 MEDIA_LOGI("MediaServerManager::CreateScreenCaptureControllerStubObject() end");
400 return object;
401 }
402 #endif
403
GetMonitorStubObject()404 sptr<IRemoteObject> MediaServerManager::GetMonitorStubObject()
405 {
406 sptr<MonitorServiceStub> monitorStub = MonitorServiceStub::GetInstance();
407 CHECK_AND_RETURN_RET_LOG(monitorStub != nullptr, nullptr, "failed to create MonitorServiceStub");
408 sptr<IRemoteObject> object = monitorStub->AsObject();
409 return object;
410 }
411
DestroyAVCodecStub(StubType type,sptr<IRemoteObject> object,pid_t pid)412 void MediaServerManager::DestroyAVCodecStub(StubType type, sptr<IRemoteObject> object, pid_t pid)
413 {
414 switch (type) {
415 case AVCODEC: {
416 for (auto it = avCodecStubMap_.begin(); it != avCodecStubMap_.end(); it++) {
417 if (it->first == object) {
418 MEDIA_LOGD("destroy avcodec stub services(%{public}zu) pid(%{public}d).",
419 avCodecStubMap_.size(), pid);
420 (void)avCodecStubMap_.erase(it);
421 return;
422 }
423 }
424 MEDIA_LOGE("find avcodec object failed, pid(%{public}d).", pid);
425 break;
426 }
427 case AVCODECLIST: {
428 for (auto it = avCodecListStubMap_.begin(); it != avCodecListStubMap_.end(); it++) {
429 if (it->first == object) {
430 MEDIA_LOGD("destroy avcodeclist stub services(%{public}zu) pid(%{public}d).",
431 avCodecListStubMap_.size(), pid);
432 (void)avCodecListStubMap_.erase(it);
433 return;
434 }
435 }
436 MEDIA_LOGE("find avcodeclist object failed, pid(%{public}d).", pid);
437 break;
438 }
439 default:
440 break;
441 }
442 }
443
DestroyAVTransCoderStub(StubType type,sptr<IRemoteObject> object,pid_t pid)444 void MediaServerManager::DestroyAVTransCoderStub(StubType type, sptr<IRemoteObject> object, pid_t pid)
445 {
446 switch (type) {
447 case TRANSCODER: {
448 for (auto it = transCoderStubMap_.begin(); it != transCoderStubMap_.end(); it++) {
449 if (it->first == object) {
450 MEDIA_LOGD("destroy transCoder stub services(%{public}zu) pid(%{public}d).",
451 transCoderStubMap_.size(), pid);
452 (void)transCoderStubMap_.erase(it);
453 return;
454 }
455 }
456 MEDIA_LOGE("find transCoder object failed, pid(%{public}d).", pid);
457 break;
458 }
459 default:
460 break;
461 }
462 }
463
DestroyAVPlayerStub(StubType type,sptr<IRemoteObject> object,pid_t pid)464 void MediaServerManager::DestroyAVPlayerStub(StubType type, sptr<IRemoteObject> object, pid_t pid)
465 {
466 switch (type) {
467 case PLAYER: {
468 for (auto it = playerStubMap_.begin(); it != playerStubMap_.end(); it++) {
469 if (it->first == object) {
470 MEDIA_LOGD("destroy player stub services(%{public}zu) pid(%{public}d).",
471 playerStubMap_.size(), pid);
472 (void)playerStubMap_.erase(it);
473 MediaTrace::CounterTrace("The number of player", playerStubMap_.size());
474 return;
475 }
476 }
477 MEDIA_LOGE("find player object failed, pid(%{public}d).", pid);
478 break;
479 }
480 case AVMETADATAHELPER: {
481 for (auto it = avMetadataHelperStubMap_.begin(); it != avMetadataHelperStubMap_.end(); it++) {
482 if (it->first == object) {
483 MEDIA_LOGD("destroy avmetadatahelper stub services(%{public}zu) pid(%{public}d).",
484 avMetadataHelperStubMap_.size(), pid);
485 (void)avMetadataHelperStubMap_.erase(it);
486 return;
487 }
488 }
489 MEDIA_LOGE("find avmetadatahelper object failed, pid(%{public}d).", pid);
490 break;
491 }
492 default:
493 break;
494 }
495 }
496
DestroyAVRecorderStub(StubType type,sptr<IRemoteObject> object,pid_t pid)497 void MediaServerManager::DestroyAVRecorderStub(StubType type, sptr<IRemoteObject> object, pid_t pid)
498 {
499 switch (type) {
500 case RECORDER: {
501 for (auto it = recorderStubMap_.begin(); it != recorderStubMap_.end(); it++) {
502 if (it->first == object) {
503 MEDIA_LOGD("destroy recorder stub services(%{public}zu) pid(%{public}d).",
504 recorderStubMap_.size(), pid);
505 (void)recorderStubMap_.erase(it);
506 return;
507 }
508 }
509 MEDIA_LOGE("find recorder object failed, pid(%{public}d).", pid);
510 break;
511 }
512 case RECORDERPROFILES: {
513 for (auto it = recorderProfilesStubMap_.begin(); it != recorderProfilesStubMap_.end(); it++) {
514 if (it->first == object) {
515 MEDIA_LOGD("destroy mediaprofile stub services(%{public}zu) pid(%{public}d).",
516 recorderProfilesStubMap_.size(), pid);
517 (void)recorderProfilesStubMap_.erase(it);
518 return;
519 }
520 }
521 MEDIA_LOGE("find mediaprofile object failed, pid(%{public}d).", pid);
522 break;
523 }
524 default:
525 break;
526 }
527 }
528
DestroyAVScreenCaptureStub(StubType type,sptr<IRemoteObject> object,pid_t pid)529 void MediaServerManager::DestroyAVScreenCaptureStub(StubType type, sptr<IRemoteObject> object, pid_t pid)
530 {
531 switch (type) {
532 case SCREEN_CAPTURE: {
533 for (auto it = screenCaptureStubMap_.begin(); it != screenCaptureStubMap_.end(); it++) {
534 if (it->first == object) {
535 MEDIA_LOGD("destroy screen capture stub services(%{public}zu) pid(%{public}d).",
536 screenCaptureStubMap_.size(), pid);
537 (void)screenCaptureStubMap_.erase(it);
538 return;
539 }
540 }
541 MEDIA_LOGE("find screen capture object failed, pid(%{public}d).", pid);
542 break;
543 }
544 case SCREEN_CAPTURE_MONITOR: {
545 for (auto it = screenCaptureMonitorStubMap_.begin(); it != screenCaptureMonitorStubMap_.end(); it++) {
546 if (it->first == object) {
547 MEDIA_LOGD("destroy screen capture monitor stub services(%{public}zu) pid(%{public}d).",
548 screenCaptureMonitorStubMap_.size(), pid);
549 (void)screenCaptureMonitorStubMap_.erase(it);
550 return;
551 }
552 }
553 MEDIA_LOGE("find screen capture monitor object failed, pid(%{public}d).", pid);
554 break;
555 }
556 case SCREEN_CAPTURE_CONTROLLER: {
557 for (auto it = screenCaptureControllerStubMap_.begin();
558 it != screenCaptureControllerStubMap_.end(); it++) {
559 if (it->first == object) {
560 MEDIA_LOGD("destroy screen capture controller stub services(%{public}zu) pid(%{public}d).",
561 screenCaptureControllerStubMap_.size(), pid);
562 (void)screenCaptureControllerStubMap_.erase(it);
563 return;
564 }
565 }
566 MEDIA_LOGE("find screen capture controller object failed, pid(%{public}d).", pid);
567 break;
568 }
569 default:
570 break;
571 }
572 }
573
DestroyStubObject(StubType type,sptr<IRemoteObject> object)574 void MediaServerManager::DestroyStubObject(StubType type, sptr<IRemoteObject> object)
575 {
576 std::lock_guard<std::mutex> lock(mutex_);
577 pid_t pid = IPCSkeleton::GetCallingPid();
578 DestroyDumper(type, object);
579 switch (type) {
580 case TRANSCODER:
581 DestroyAVTransCoderStub(type, object, pid);
582 break;
583 case RECORDER:
584 case RECORDERPROFILES:
585 DestroyAVRecorderStub(type, object, pid);
586 break;
587 case PLAYER:
588 case AVMETADATAHELPER:
589 DestroyAVPlayerStub(type, object, pid);
590 break;
591 case AVCODEC:
592 case AVCODECLIST:
593 DestroyAVCodecStub(type, object, pid);
594 break;
595 case SCREEN_CAPTURE:
596 case SCREEN_CAPTURE_MONITOR:
597 case SCREEN_CAPTURE_CONTROLLER:
598 DestroyAVScreenCaptureStub(type, object, pid);
599 break;
600 default: {
601 MEDIA_LOGE("default case, media server manager failed, pid(%{public}d).", pid);
602 break;
603 }
604 }
605 }
606
DestroyAVScreenCaptureStubForPid(pid_t pid)607 void MediaServerManager::DestroyAVScreenCaptureStubForPid(pid_t pid)
608 {
609 MEDIA_LOGD("ScreenCapture stub services(%{public}zu) pid(%{public}d).", screenCaptureStubMap_.size(), pid);
610 for (auto itScreenCapture = screenCaptureStubMap_.begin(); itScreenCapture != screenCaptureStubMap_.end();) {
611 if (itScreenCapture->second == pid) {
612 executor_.Commit(itScreenCapture->first);
613 itScreenCapture = screenCaptureStubMap_.erase(itScreenCapture);
614 } else {
615 itScreenCapture++;
616 }
617 }
618 MEDIA_LOGD("ScreenCapture stub services(%{public}zu).", screenCaptureStubMap_.size());
619 MEDIA_LOGD("ScreenCapture monitor stub services(%{public}zu) pid(%{public}d).",
620 screenCaptureMonitorStubMap_.size(), pid);
621 for (auto itScreenCaptureMonitor = screenCaptureMonitorStubMap_.begin();
622 itScreenCaptureMonitor != screenCaptureMonitorStubMap_.end();) {
623 if (itScreenCaptureMonitor->second == pid) {
624 executor_.Commit(itScreenCaptureMonitor->first);
625 itScreenCaptureMonitor = screenCaptureMonitorStubMap_.erase(itScreenCaptureMonitor);
626 } else {
627 itScreenCaptureMonitor++;
628 }
629 }
630 MEDIA_LOGD("ScreenCapture monitor stub services(%{public}zu).", screenCaptureMonitorStubMap_.size());
631 MEDIA_LOGD("ScreenCapture controller stub services(%{public}zu) pid(%{public}d).",
632 screenCaptureControllerStubMap_.size(), pid);
633 for (auto itScreenCaptureController = screenCaptureControllerStubMap_.begin();
634 itScreenCaptureController != screenCaptureControllerStubMap_.end();) {
635 if (itScreenCaptureController->second == pid) {
636 executor_.Commit(itScreenCaptureController->first);
637 itScreenCaptureController = screenCaptureControllerStubMap_.erase(itScreenCaptureController);
638 } else {
639 itScreenCaptureController++;
640 }
641 }
642 MEDIA_LOGD("ScreenCapture controller stub services(%{public}zu).", screenCaptureControllerStubMap_.size());
643 }
644
DestroyAVTranscoderStubForPid(pid_t pid)645 void MediaServerManager::DestroyAVTranscoderStubForPid(pid_t pid)
646 {
647 MEDIA_LOGD("AVTranscoder stub services(%{public}zu) pid(%{public}d).", transCoderStubMap_.size(), pid);
648 for (auto itTranscoder = transCoderStubMap_.begin(); itTranscoder != transCoderStubMap_.end();) {
649 if (itTranscoder->second == pid) {
650 executor_.Commit(itTranscoder->first);
651 itTranscoder = transCoderStubMap_.erase(itTranscoder);
652 } else {
653 itTranscoder++;
654 }
655 }
656 MEDIA_LOGD("AVTranscoder stub services(%{public}zu).", transCoderStubMap_.size());
657 }
658
DestroyAVCodecStubForPid(pid_t pid)659 void MediaServerManager::DestroyAVCodecStubForPid(pid_t pid)
660 {
661 MEDIA_LOGD("avcodec stub services(%{public}zu) pid(%{public}d).", avCodecStubMap_.size(), pid);
662 for (auto itAvCodec = avCodecStubMap_.begin(); itAvCodec != avCodecStubMap_.end();) {
663 if (itAvCodec->second == pid) {
664 executor_.Commit(itAvCodec->first);
665 itAvCodec = avCodecStubMap_.erase(itAvCodec);
666 } else {
667 itAvCodec++;
668 }
669 }
670 MEDIA_LOGD("avcodec stub services(%{public}zu).", avCodecStubMap_.size());
671
672 MEDIA_LOGD("avcodeclist stub services(%{public}zu) pid(%{public}d).", avCodecListStubMap_.size(), pid);
673 for (auto itAvCodecList = avCodecListStubMap_.begin(); itAvCodecList != avCodecListStubMap_.end();) {
674 if (itAvCodecList->second == pid) {
675 executor_.Commit(itAvCodecList->first);
676 itAvCodecList = avCodecListStubMap_.erase(itAvCodecList);
677 } else {
678 itAvCodecList++;
679 }
680 }
681 MEDIA_LOGD("avcodeclist stub services(%{public}zu).", avCodecListStubMap_.size());
682 }
683
DestroyAVPlayerStubForPid(pid_t pid)684 void MediaServerManager::DestroyAVPlayerStubForPid(pid_t pid)
685 {
686 MEDIA_LOGD("player stub services(%{public}zu) pid(%{public}d).", playerStubMap_.size(), pid);
687 for (auto itPlayer = playerStubMap_.begin(); itPlayer != playerStubMap_.end();) {
688 if (itPlayer->second == pid) {
689 executor_.Commit(itPlayer->first);
690 itPlayer = playerStubMap_.erase(itPlayer);
691 } else {
692 itPlayer++;
693 }
694 }
695 MEDIA_LOGD("player stub services(%{public}zu).", playerStubMap_.size());
696
697 MEDIA_LOGD("avmetadatahelper stub services(%{public}zu) pid(%{public}d).", avMetadataHelperStubMap_.size(), pid);
698 for (auto itAvMetadata = avMetadataHelperStubMap_.begin(); itAvMetadata != avMetadataHelperStubMap_.end();) {
699 if (itAvMetadata->second == pid) {
700 executor_.Commit(itAvMetadata->first);
701 itAvMetadata = avMetadataHelperStubMap_.erase(itAvMetadata);
702 } else {
703 itAvMetadata++;
704 }
705 }
706 MEDIA_LOGD("avmetadatahelper stub services(%{public}zu).", avMetadataHelperStubMap_.size());
707 }
708
DestroyAVRecorderStubForPid(pid_t pid)709 void MediaServerManager::DestroyAVRecorderStubForPid(pid_t pid)
710 {
711 MEDIA_LOGD("recorder stub services(%{public}zu) pid(%{public}d).", recorderStubMap_.size(), pid);
712 for (auto itRecorder = recorderStubMap_.begin(); itRecorder != recorderStubMap_.end();) {
713 if (itRecorder->second == pid) {
714 executor_.Commit(itRecorder->first);
715 itRecorder = recorderStubMap_.erase(itRecorder);
716 } else {
717 itRecorder++;
718 }
719 }
720 MEDIA_LOGD("recorder stub services(%{public}zu).", recorderStubMap_.size());
721
722 MEDIA_LOGD("mediaprofile stub services(%{public}zu) pid(%{public}d).", recorderProfilesStubMap_.size(), pid);
723 for (auto itMediaProfile = recorderProfilesStubMap_.begin(); itMediaProfile != recorderProfilesStubMap_.end();) {
724 if (itMediaProfile->second == pid) {
725 executor_.Commit(itMediaProfile->first);
726 itMediaProfile = recorderProfilesStubMap_.erase(itMediaProfile);
727 } else {
728 itMediaProfile++;
729 }
730 }
731 MEDIA_LOGD("mediaprofile stub services(%{public}zu).", recorderProfilesStubMap_.size());
732 }
733
DestroyStubObjectForPid(pid_t pid)734 void MediaServerManager::DestroyStubObjectForPid(pid_t pid)
735 {
736 std::lock_guard<std::mutex> lock(mutex_);
737 DestroyDumperForPid(pid);
738 DestroyAVRecorderStubForPid(pid);
739 DestroyAVPlayerStubForPid(pid);
740 DestroyAVCodecStubForPid(pid);
741 DestroyAVTranscoderStubForPid(pid);
742 DestroyAVScreenCaptureStubForPid(pid);
743 MonitorServiceStub::GetInstance()->OnClientDie(pid);
744 executor_.Clear();
745 }
746
DestroyDumper(StubType type,sptr<IRemoteObject> object)747 void MediaServerManager::DestroyDumper(StubType type, sptr<IRemoteObject> object)
748 {
749 for (auto it = dumperTbl_[type].begin(); it != dumperTbl_[type].end(); it++) {
750 if (it->remoteObject_ == object) {
751 (void)dumperTbl_[type].erase(it);
752 MEDIA_LOGD("MediaServerManager::DestroyDumper");
753 (void)Dump(-1, std::vector<std::u16string>());
754 return;
755 }
756 }
757 }
758
DestroyDumperForPid(pid_t pid)759 void MediaServerManager::DestroyDumperForPid(pid_t pid)
760 {
761 for (auto &dumpers : dumperTbl_) {
762 for (auto it = dumpers.second.begin(); it != dumpers.second.end();) {
763 if (it->pid_ == pid) {
764 it = dumpers.second.erase(it);
765 MEDIA_LOGD("MediaServerManager::DestroyDumperForPid");
766 } else {
767 it++;
768 }
769 }
770 }
771 (void)Dump(-1, std::vector<std::u16string>());
772 }
773
NotifyMemMgrLoaded()774 void MediaServerManager::NotifyMemMgrLoaded()
775 {
776 isMemMgrLoaded_.store(true);
777 }
778
Commit(sptr<IRemoteObject> obj)779 void MediaServerManager::AsyncExecutor::Commit(sptr<IRemoteObject> obj)
780 {
781 std::lock_guard<std::mutex> lock(listMutex_);
782 freeList_.push_back(obj);
783 }
784
Clear()785 void MediaServerManager::AsyncExecutor::Clear()
786 {
787 std::thread(&MediaServerManager::AsyncExecutor::HandleAsyncExecution, this).detach();
788 }
789
HandleAsyncExecution()790 void MediaServerManager::AsyncExecutor::HandleAsyncExecution()
791 {
792 std::list<sptr<IRemoteObject>> tempList;
793 {
794 std::lock_guard<std::mutex> lock(listMutex_);
795 freeList_.swap(tempList);
796 }
797 tempList.clear();
798 }
799 } // namespace Media
800 } // namespace OHOS
801