• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PLAYER
23 #include "player_service_stub.h"
24 #ifdef PLAYER_USE_MEMORY_MANAGE
25 #include "player_service_stub_mem.h"
26 #endif
27 #endif
28 #ifdef SUPPORT_METADATA
29 #include "avmetadatahelper_service_stub.h"
30 #endif
31 #ifdef SUPPORT_CODEC
32 #include "avcodec_service_stub.h"
33 #include "avcodeclist_service_stub.h"
34 #endif
35 #ifdef SUPPORT_SCREEN_CAPTURE
36 #include "screen_capture_service_stub.h"
37 #endif
38 #include "monitor_service_stub.h"
39 #include "media_log.h"
40 #include "media_errors.h"
41 #include "media_dfx.h"
42 #include "service_dump_manager.h"
43 #include "player_xcollie.h"
44 
45 namespace {
46 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "MediaServerManager"};
47 }
48 
49 namespace OHOS {
50 namespace Media {
51 constexpr uint32_t SERVER_MAX_NUMBER = 16;
GetInstance()52 MediaServerManager &MediaServerManager::GetInstance()
53 {
54     static MediaServerManager instance;
55     return instance;
56 }
57 
WriteInfo(int32_t fd,std::string & dumpString,std::vector<Dumper> dumpers,bool needDetail)58 int32_t WriteInfo(int32_t fd, std::string &dumpString, std::vector<Dumper> dumpers, bool needDetail)
59 {
60     int32_t i = 0;
61     for (auto iter : dumpers) {
62         dumpString += "-----Instance #" + std::to_string(i) + ": ";
63         dumpString += "pid = ";
64         dumpString += std::to_string(iter.pid_);
65         dumpString += " uid = ";
66         dumpString += std::to_string(iter.uid_);
67         dumpString += "-----\n";
68         if (fd != -1) {
69             write(fd, dumpString.c_str(), dumpString.size());
70             dumpString.clear();
71         }
72         i++;
73         if (needDetail && iter.entry_(fd) != MSERR_OK) {
74             return OHOS::INVALID_OPERATION;
75         }
76     }
77     if (fd != -1) {
78         write(fd, dumpString.c_str(), dumpString.size());
79     } else {
80         MEDIA_LOGI("%{public}s", dumpString.c_str());
81     }
82     dumpString.clear();
83 
84     return OHOS::NO_ERROR;
85 }
86 
Dump(int32_t fd,const std::vector<std::u16string> & args)87 int32_t MediaServerManager::Dump(int32_t fd, const std::vector<std::u16string> &args)
88 {
89     std::string dumpString;
90     std::unordered_set<std::u16string> argSets;
91     for (decltype(args.size()) index = 0; index < args.size(); ++index) {
92         argSets.insert(args[index]);
93     }
94 
95     dumpString += "------------------PlayerServer------------------\n";
96     auto ret = WriteInfo(fd, dumpString, dumperTbl_[StubType::PLAYER],
97         argSets.find(u"player") != argSets.end());
98     CHECK_AND_RETURN_RET_LOG(ret == NO_ERROR,
99         OHOS::INVALID_OPERATION, "Failed to write PlayerServer information");
100 
101     dumpString += "------------------RecorderServer------------------\n";
102     ret =  WriteInfo(fd, dumpString, dumperTbl_[StubType::RECORDER],
103         argSets.find(u"recorder") != argSets.end());
104     CHECK_AND_RETURN_RET_LOG(ret == NO_ERROR,
105         OHOS::INVALID_OPERATION, "Failed to write RecorderServer information");
106 
107     dumpString += "------------------CodecServer------------------\n";
108     ret = WriteInfo(fd, dumpString, dumperTbl_[StubType::AVCODEC],
109         argSets.find(u"codec") != argSets.end());
110     CHECK_AND_RETURN_RET_LOG(ret == NO_ERROR,
111         OHOS::INVALID_OPERATION, "Failed to write CodecServer information");
112 
113     dumpString += "------------------AVMetaServer------------------\n";
114     ret = WriteInfo(fd, dumpString, dumperTbl_[StubType::AVMETADATAHELPER], false);
115     CHECK_AND_RETURN_RET_LOG(ret == NO_ERROR,
116         OHOS::INVALID_OPERATION, "Failed to write AVMetaServer information");
117 
118     ret = ServiceDumpManager::GetInstance().Dump(fd, argSets);
119     CHECK_AND_RETURN_RET_LOG(ret == NO_ERROR,
120         OHOS::INVALID_OPERATION, "Failed to write dfx dump information");
121 
122     ret = PlayerXCollie::GetInstance().Dump(fd);
123     CHECK_AND_RETURN_RET_LOG(ret == NO_ERROR,
124         OHOS::INVALID_OPERATION, "Failed to write xcollie dump information");
125 
126     ret = MonitorServiceStub::GetInstance()->DumpInfo(fd,
127         argSets.find(u"monitor") != argSets.end());
128     CHECK_AND_RETURN_RET_LOG(ret == NO_ERROR,
129         OHOS::INVALID_OPERATION, "Failed to write monitor dump information");
130     return OHOS::NO_ERROR;
131 }
132 
MediaServerManager()133 MediaServerManager::MediaServerManager()
134 {
135     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
136 }
137 
~MediaServerManager()138 MediaServerManager::~MediaServerManager()
139 {
140     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
141 }
142 
CreateStubObject(StubType type)143 sptr<IRemoteObject> MediaServerManager::CreateStubObject(StubType type)
144 {
145     std::lock_guard<std::mutex> lock(mutex_);
146     switch (type) {
147 #ifdef SUPPORT_RECORDER
148         case RECORDER: {
149             return CreateRecorderStubObject();
150         }
151         case RECORDERPROFILES: {
152             return CreateRecorderProfilesStubObject();
153         }
154 #endif
155 #ifdef SUPPORT_PLAYER
156         case PLAYER: {
157             return CreatePlayerStubObject();
158         }
159 #endif
160 #ifdef SUPPORT_METADATA
161         case AVMETADATAHELPER: {
162             return CreateAVMetadataHelperStubObject();
163         }
164 #endif
165 #ifdef SUPPORT_CODEC
166         case AVCODECLIST: {
167             return CreateAVCodecListStubObject();
168         }
169         case AVCODEC: {
170             return CreateAVCodecStubObject();
171         }
172 #endif
173 #ifdef SUPPORT_SCREEN_CAPTURE
174         case SCREEN_CAPTURE: {
175             return CreateScreenCaptureStubObject();
176         }
177 #endif
178         case MONITOR: {
179             return GetMonitorStubObject();
180         }
181         default: {
182             MEDIA_LOGE("default case, media server manager failed");
183             return nullptr;
184         }
185     }
186 }
187 
188 #ifdef SUPPORT_PLAYER
CreatePlayerStubObject()189 sptr<IRemoteObject> MediaServerManager::CreatePlayerStubObject()
190 {
191     CHECK_AND_RETURN_RET_LOG(playerStubMap_.size() < SERVER_MAX_NUMBER,
192         nullptr, "The number of player services(%{public}zu) has reached the upper limit."
193         "Please release the applied resources.", playerStubMap_.size());
194 
195 #ifdef PLAYER_USE_MEMORY_MANAGE
196     sptr<PlayerServiceStub> playerStub = PlayerServiceStubMem::Create();
197 #else
198     sptr<PlayerServiceStub> playerStub = PlayerServiceStub::Create();
199 #endif
200     CHECK_AND_RETURN_RET_LOG(playerStub != nullptr, nullptr, "failed to create PlayerServiceStub");
201 
202     sptr<IRemoteObject> object = playerStub->AsObject();
203     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to create PlayerServiceStub");
204 
205     pid_t pid = IPCSkeleton::GetCallingPid();
206     playerStubMap_[object] = pid;
207 
208     Dumper dumper;
209     dumper.entry_ = [player = playerStub](int32_t fd) -> int32_t {
210         return player->DumpInfo(fd);
211     };
212     dumper.pid_ = pid;
213     dumper.uid_ = IPCSkeleton::GetCallingUid();
214     dumper.remoteObject_ = object;
215     dumperTbl_[StubType::PLAYER].emplace_back(dumper);
216     MEDIA_LOGD("The number of player services(%{public}zu) pid(%{public}d).",
217         playerStubMap_.size(), pid);
218     MediaTrace::CounterTrace("The number of player", playerStubMap_.size());
219     (void)Dump(-1, std::vector<std::u16string>());
220     return object;
221 }
222 #endif
223 
224 #ifdef SUPPORT_RECORDER
CreateRecorderStubObject()225 sptr<IRemoteObject> MediaServerManager::CreateRecorderStubObject()
226 {
227     constexpr uint32_t recorderMax = 2;
228     CHECK_AND_RETURN_RET_LOG(recorderStubMap_.size() < recorderMax,
229         nullptr, "The number of recorder services(%{public}zu) has reached the upper limit."
230         "Please release the applied resources.", recorderStubMap_.size());
231 
232     sptr<RecorderServiceStub> recorderStub = RecorderServiceStub::Create();
233     CHECK_AND_RETURN_RET_LOG(recorderStub != nullptr, nullptr, "failed to create RecorderServiceStub");
234 
235     sptr<IRemoteObject> object = recorderStub->AsObject();
236     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to create RecorderServiceStub");
237 
238     pid_t pid = IPCSkeleton::GetCallingPid();
239     recorderStubMap_[object] = pid;
240 
241     Dumper dumper;
242     dumper.entry_ = [recorder = recorderStub](int32_t fd) -> int32_t {
243         return recorder->DumpInfo(fd);
244     };
245     dumper.pid_ = pid;
246     dumper.uid_ = IPCSkeleton::GetCallingUid();
247     dumper.remoteObject_ = object;
248     dumperTbl_[StubType::RECORDER].emplace_back(dumper);
249     MEDIA_LOGD("The number of recorder services(%{public}zu) pid(%{public}d).",
250         recorderStubMap_.size(), pid);
251     (void)Dump(-1, std::vector<std::u16string>());
252     return object;
253 }
254 
CreateRecorderProfilesStubObject()255 sptr<IRemoteObject> MediaServerManager::CreateRecorderProfilesStubObject()
256 {
257     CHECK_AND_RETURN_RET_LOG(recorderProfilesStubMap_.size() < SERVER_MAX_NUMBER,
258         nullptr, "The number of recorder_profiles services(%{public}zu) has reached the upper limit."
259         "Please release the applied resources.", recorderProfilesStubMap_.size());
260 
261     sptr<RecorderProfilesServiceStub> recorderProfilesStub = RecorderProfilesServiceStub::Create();
262     CHECK_AND_RETURN_RET_LOG(recorderProfilesStub != nullptr, nullptr,
263         "failed to create recorderProfilesStub");
264 
265     sptr<IRemoteObject> object = recorderProfilesStub->AsObject();
266     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to create recorderProfilesStub");
267 
268     pid_t pid = IPCSkeleton::GetCallingPid();
269     recorderProfilesStubMap_[object] = pid;
270     MEDIA_LOGD("The number of recorder_profiles services(%{public}zu).", recorderProfilesStubMap_.size());
271     return object;
272 }
273 #endif
274 
275 #ifdef SUPPORT_METADATA
CreateAVMetadataHelperStubObject()276 sptr<IRemoteObject> MediaServerManager::CreateAVMetadataHelperStubObject()
277 {
278     constexpr uint32_t metadataHelperNumMax = 32;
279     CHECK_AND_RETURN_RET_LOG(avMetadataHelperStubMap_.size() < metadataHelperNumMax,
280         nullptr, "The number of avmetadatahelper services(%{public}zu) has reached the upper limit."
281         "Please release the applied resources.", avMetadataHelperStubMap_.size());
282 
283     sptr<AVMetadataHelperServiceStub> avMetadataHelperStub = AVMetadataHelperServiceStub::Create();
284     CHECK_AND_RETURN_RET_LOG(avMetadataHelperStub != nullptr, nullptr,
285         "failed to create AVMetadataHelperServiceStub");
286 
287     sptr<IRemoteObject> object = avMetadataHelperStub->AsObject();
288     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr,
289         "failed to create AVMetadataHelperServiceStub");
290 
291     pid_t pid = IPCSkeleton::GetCallingPid();
292     avMetadataHelperStubMap_[object] = pid;
293 
294     Dumper dumper;
295     dumper.pid_ = pid;
296     dumper.uid_ = IPCSkeleton::GetCallingUid();
297     dumper.remoteObject_ = object;
298     dumperTbl_[StubType::AVMETADATAHELPER].emplace_back(dumper);
299 
300     MEDIA_LOGD("The number of avmetadatahelper services(%{public}zu) pid(%{public}d).",
301         avMetadataHelperStubMap_.size(), pid);
302     (void)Dump(-1, std::vector<std::u16string>());
303     return object;
304 }
305 #endif
306 
307 #ifdef SUPPORT_CODEC
CreateAVCodecListStubObject()308 sptr<IRemoteObject> MediaServerManager::CreateAVCodecListStubObject()
309 {
310     CHECK_AND_RETURN_RET_LOG(avCodecListStubMap_.size() < SERVER_MAX_NUMBER,
311         nullptr, "The number of codeclist services(%{public}zu) has reached the upper limit."
312             "Please release the applied resources.", avCodecListStubMap_.size());
313 
314     sptr<AVCodecListServiceStub> avCodecListStub = AVCodecListServiceStub::Create();
315     CHECK_AND_RETURN_RET_LOG(avCodecListStub != nullptr, nullptr,
316         "failed to create AVCodecListServiceStub");
317 
318     sptr<IRemoteObject> object = avCodecListStub->AsObject();
319     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr,
320         "failed to create AVMetadataHelperServiceStub");
321 
322     pid_t pid = IPCSkeleton::GetCallingPid();
323     avCodecListStubMap_[object] = pid;
324     MEDIA_LOGD("The number of codeclist services(%{public}zu).", avCodecListStubMap_.size());
325     return object;
326 }
327 
CreateAVCodecStubObject()328 sptr<IRemoteObject> MediaServerManager::CreateAVCodecStubObject()
329 {
330     CHECK_AND_RETURN_RET_LOG(avCodecStubMap_.size() < SERVER_MAX_NUMBER,
331         nullptr, "The number of avcodec services(%{public}zu) has reached the upper limit."
332             "Please release the applied resources.", avCodecStubMap_.size());
333 
334     sptr<AVCodecServiceStub> avCodecHelperStub = AVCodecServiceStub::Create();
335     CHECK_AND_RETURN_RET_LOG(avCodecHelperStub != nullptr, nullptr, "failed to create AVCodecServiceStub");
336 
337     sptr<IRemoteObject> object = avCodecHelperStub->AsObject();
338     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to create AVCodecServiceStub");
339 
340     pid_t pid = IPCSkeleton::GetCallingPid();
341     avCodecStubMap_[object] = pid;
342 
343     Dumper dumper;
344     dumper.entry_ = [avcodec = avCodecHelperStub](int32_t fd) -> int32_t {
345         return avcodec->DumpInfo(fd);
346     };
347     dumper.pid_ = pid;
348     dumper.uid_ = IPCSkeleton::GetCallingUid();
349     dumper.remoteObject_ = object;
350     dumperTbl_[StubType::AVCODEC].emplace_back(dumper);
351     MEDIA_LOGD("The number of avcodec services(%{public}zu).", avCodecStubMap_.size());
352     (void)Dump(-1, std::vector<std::u16string>());
353     return object;
354 }
355 #endif
356 
357 #ifdef SUPPORT_SCREEN_CAPTURE
CreateScreenCaptureStubObject()358 sptr<IRemoteObject> MediaServerManager::CreateScreenCaptureStubObject()
359 {
360     CHECK_AND_RETURN_RET_LOG(screenCaptureStubMap_.size() < SERVER_MAX_NUMBER,
361         nullptr, "The number of screen capture services(%{public}zu) has reached the upper limit."
362             "Please release the applied resources.", screenCaptureStubMap_.size());
363 
364     sptr<ScreenCaptureServiceStub> screenCaptureStub = ScreenCaptureServiceStub::Create();
365     CHECK_AND_RETURN_RET_LOG(screenCaptureStub != nullptr, nullptr,
366         "failed to create ScreenCaptureServiceStub");
367 
368     sptr<IRemoteObject> object = screenCaptureStub->AsObject();
369     CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to create ScreenCaptureServiceStub");
370 
371     pid_t pid = IPCSkeleton::GetCallingPid();
372     screenCaptureStubMap_[object] = pid;
373     MEDIA_LOGD("The number of screen capture services(%{public}zu).", screenCaptureStubMap_.size());
374     return object;
375 }
376 #endif
377 
GetMonitorStubObject()378 sptr<IRemoteObject> MediaServerManager::GetMonitorStubObject()
379 {
380     sptr<MonitorServiceStub> monitorStub = MonitorServiceStub::GetInstance();
381     CHECK_AND_RETURN_RET_LOG(monitorStub != nullptr, nullptr, "failed to create MonitorServiceStub");
382     sptr<IRemoteObject> object = monitorStub->AsObject();
383     return object;
384 }
385 
DestroyStubObject(StubType type,sptr<IRemoteObject> object)386 void MediaServerManager::DestroyStubObject(StubType type, sptr<IRemoteObject> object)
387 {
388     std::lock_guard<std::mutex> lock(mutex_);
389     pid_t pid = IPCSkeleton::GetCallingPid();
390     DestroyDumper(type, object);
391     switch (type) {
392         case RECORDER: {
393             for (auto it = recorderStubMap_.begin(); it != recorderStubMap_.end(); it++) {
394                 if (it->first == object) {
395                     MEDIA_LOGD("destroy recorder stub services(%{public}zu) pid(%{public}d).",
396                         recorderStubMap_.size(), pid);
397                     (void)recorderStubMap_.erase(it);
398                     return;
399                 }
400             }
401             MEDIA_LOGE("find recorder object failed, pid(%{public}d).", pid);
402             break;
403         }
404         case PLAYER: {
405             for (auto it = playerStubMap_.begin(); it != playerStubMap_.end(); it++) {
406                 if (it->first == object) {
407                     MEDIA_LOGD("destroy player stub services(%{public}zu) pid(%{public}d).",
408                         playerStubMap_.size(), pid);
409                     (void)playerStubMap_.erase(it);
410                     MediaTrace::CounterTrace("The number of player", playerStubMap_.size());
411                     return;
412                 }
413             }
414             MEDIA_LOGE("find player object failed, pid(%{public}d).", pid);
415             break;
416         }
417         case AVMETADATAHELPER: {
418             for (auto it = avMetadataHelperStubMap_.begin(); it != avMetadataHelperStubMap_.end(); it++) {
419                 if (it->first == object) {
420                     MEDIA_LOGD("destroy avmetadatahelper stub services(%{public}zu) pid(%{public}d).",
421                         avMetadataHelperStubMap_.size(), pid);
422                     (void)avMetadataHelperStubMap_.erase(it);
423                     return;
424                 }
425             }
426             MEDIA_LOGE("find avmetadatahelper object failed, pid(%{public}d).", pid);
427             break;
428         }
429         case AVCODEC: {
430             for (auto it = avCodecStubMap_.begin(); it != avCodecStubMap_.end(); it++) {
431                 if (it->first == object) {
432                     MEDIA_LOGD("destroy avcodec stub services(%{public}zu) pid(%{public}d).",
433                         avCodecStubMap_.size(), pid);
434                     (void)avCodecStubMap_.erase(it);
435                     return;
436                 }
437             }
438             MEDIA_LOGE("find avcodec object failed, pid(%{public}d).", pid);
439             break;
440         }
441         case AVCODECLIST: {
442             for (auto it = avCodecListStubMap_.begin(); it != avCodecListStubMap_.end(); it++) {
443                 if (it->first == object) {
444                     MEDIA_LOGD("destroy avcodeclist stub services(%{public}zu) pid(%{public}d).",
445                         avCodecListStubMap_.size(), pid);
446                     (void)avCodecListStubMap_.erase(it);
447                     return;
448                 }
449             }
450             MEDIA_LOGE("find avcodeclist object failed, pid(%{public}d).", pid);
451             break;
452         }
453         case RECORDERPROFILES: {
454             for (auto it = recorderProfilesStubMap_.begin(); it != recorderProfilesStubMap_.end(); it++) {
455                 if (it->first == object) {
456                     MEDIA_LOGD("destroy mediaprofile stub services(%{public}zu) pid(%{public}d).",
457                         recorderProfilesStubMap_.size(), pid);
458                     (void)recorderProfilesStubMap_.erase(it);
459                     return;
460                 }
461             }
462             MEDIA_LOGE("find mediaprofile object failed, pid(%{public}d).", pid);
463             break;
464         }
465         case SCREEN_CAPTURE: {
466             for (auto it = screenCaptureStubMap_.begin(); it != screenCaptureStubMap_.end(); it++) {
467                 if (it->first == object) {
468                     MEDIA_LOGD("destroy screen capture stub services(%{public}zu) pid(%{public}d).",
469                         screenCaptureStubMap_.size(), pid);
470                     (void)screenCaptureStubMap_.erase(it);
471                     return;
472                 }
473             }
474             MEDIA_LOGE("find screen capture object failed, pid(%{public}d).", pid);
475             break;
476         }
477         default: {
478             MEDIA_LOGE("default case, media server manager failed, pid(%{public}d).", pid);
479             break;
480         }
481     }
482 }
483 
DestroyStubObjectForPid(pid_t pid)484 void MediaServerManager::DestroyStubObjectForPid(pid_t pid)
485 {
486     std::lock_guard<std::mutex> lock(mutex_);
487     MEDIA_LOGD("recorder stub services(%{public}zu) pid(%{public}d).", recorderStubMap_.size(), pid);
488     DestroyDumperForPid(pid);
489     for (auto itRecorder = recorderStubMap_.begin(); itRecorder != recorderStubMap_.end();) {
490         if (itRecorder->second == pid) {
491             executor_.Commit(itRecorder->first);
492             itRecorder = recorderStubMap_.erase(itRecorder);
493         } else {
494             itRecorder++;
495         }
496     }
497     MEDIA_LOGD("recorder stub services(%{public}zu).", recorderStubMap_.size());
498 
499     MEDIA_LOGD("player stub services(%{public}zu) pid(%{public}d).", playerStubMap_.size(), pid);
500     for (auto itPlayer = playerStubMap_.begin(); itPlayer != playerStubMap_.end();) {
501         if (itPlayer->second == pid) {
502             executor_.Commit(itPlayer->first);
503             itPlayer = playerStubMap_.erase(itPlayer);
504         } else {
505             itPlayer++;
506         }
507     }
508     MEDIA_LOGD("player stub services(%{public}zu).", playerStubMap_.size());
509 
510     MEDIA_LOGD("avmetadatahelper stub services(%{public}zu) pid(%{public}d).", avMetadataHelperStubMap_.size(), pid);
511     for (auto itAvMetadata = avMetadataHelperStubMap_.begin(); itAvMetadata != avMetadataHelperStubMap_.end();) {
512         if (itAvMetadata->second == pid) {
513             executor_.Commit(itAvMetadata->first);
514             itAvMetadata = avMetadataHelperStubMap_.erase(itAvMetadata);
515         } else {
516             itAvMetadata++;
517         }
518     }
519     MEDIA_LOGD("avmetadatahelper stub services(%{public}zu).", avMetadataHelperStubMap_.size());
520 
521     MEDIA_LOGD("avcodec stub services(%{public}zu) pid(%{public}d).", avCodecStubMap_.size(), pid);
522     for (auto itAvCodec = avCodecStubMap_.begin(); itAvCodec != avCodecStubMap_.end();) {
523         if (itAvCodec->second == pid) {
524             executor_.Commit(itAvCodec->first);
525             itAvCodec = avCodecStubMap_.erase(itAvCodec);
526         } else {
527             itAvCodec++;
528         }
529     }
530     MEDIA_LOGD("avcodec stub services(%{public}zu).", avCodecStubMap_.size());
531 
532     MEDIA_LOGD("avcodeclist stub services(%{public}zu) pid(%{public}d).", avCodecListStubMap_.size(), pid);
533     for (auto itAvCodecList = avCodecListStubMap_.begin(); itAvCodecList != avCodecListStubMap_.end();) {
534         if (itAvCodecList->second == pid) {
535             executor_.Commit(itAvCodecList->first);
536             itAvCodecList = avCodecListStubMap_.erase(itAvCodecList);
537         } else {
538             itAvCodecList++;
539         }
540     }
541     MEDIA_LOGD("avcodeclist stub services(%{public}zu).", avCodecListStubMap_.size());
542 
543     MEDIA_LOGD("mediaprofile stub services(%{public}zu) pid(%{public}d).", recorderProfilesStubMap_.size(), pid);
544     for (auto itMediaProfile = recorderProfilesStubMap_.begin(); itMediaProfile != recorderProfilesStubMap_.end();) {
545         if (itMediaProfile->second == pid) {
546             executor_.Commit(itMediaProfile->first);
547             itMediaProfile = recorderProfilesStubMap_.erase(itMediaProfile);
548         } else {
549             itMediaProfile++;
550         }
551     }
552     MEDIA_LOGD("mediaprofile stub services(%{public}zu).", recorderProfilesStubMap_.size());
553 
554     MEDIA_LOGD("screencapture stub services(%{public}zu) pid(%{public}d).", screenCaptureStubMap_.size(), pid);
555     for (auto itScreenCapture = screenCaptureStubMap_.begin(); itScreenCapture != screenCaptureStubMap_.end();) {
556         if (itScreenCapture->second == pid) {
557             executor_.Commit(itScreenCapture->first);
558             itScreenCapture = screenCaptureStubMap_.erase(itScreenCapture);
559         } else {
560             itScreenCapture++;
561         }
562     }
563     MEDIA_LOGD("screencapture stub services(%{public}zu).", screenCaptureStubMap_.size());
564     MonitorServiceStub::GetInstance()->OnClientDie(pid);
565     executor_.Clear();
566 }
567 
DestroyDumper(StubType type,sptr<IRemoteObject> object)568 void MediaServerManager::DestroyDumper(StubType type, sptr<IRemoteObject> object)
569 {
570     for (auto it = dumperTbl_[type].begin(); it != dumperTbl_[type].end(); it++) {
571         if (it->remoteObject_ == object) {
572             (void)dumperTbl_[type].erase(it);
573             MEDIA_LOGD("MediaServerManager::DestroyDumper");
574             (void)Dump(-1, std::vector<std::u16string>());
575             return;
576         }
577     }
578 }
579 
DestroyDumperForPid(pid_t pid)580 void MediaServerManager::DestroyDumperForPid(pid_t pid)
581 {
582     for (auto &dumpers : dumperTbl_) {
583         for (auto it = dumpers.second.begin(); it != dumpers.second.end();) {
584             if (it->pid_ == pid) {
585                 it = dumpers.second.erase(it);
586                 MEDIA_LOGD("MediaServerManager::DestroyDumperForPid");
587             } else {
588                 it++;
589             }
590         }
591     }
592     (void)Dump(-1, std::vector<std::u16string>());
593 }
594 
Commit(sptr<IRemoteObject> obj)595 void MediaServerManager::AsyncExecutor::Commit(sptr<IRemoteObject> obj)
596 {
597     std::lock_guard<std::mutex> lock(listMutex_);
598     freeList_.push_back(obj);
599 }
600 
Clear()601 void MediaServerManager::AsyncExecutor::Clear()
602 {
603     std::thread(&MediaServerManager::AsyncExecutor::HandleAsyncExecution, this).detach();
604 }
605 
HandleAsyncExecution()606 void MediaServerManager::AsyncExecutor::HandleAsyncExecution()
607 {
608     std::list<sptr<IRemoteObject>> tempList;
609     {
610         std::lock_guard<std::mutex> lock(listMutex_);
611         freeList_.swap(tempList);
612     }
613     tempList.clear();
614 }
615 } // namespace Media
616 } // namespace OHOS
617