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_client.h"
17 #include "avmetadatahelper_client.h"
18 #include "iservice_registry.h"
19 #include "system_ability_definition.h"
20 #include "ipc_skeleton.h"
21 #include "i_standard_monitor_service.h"
22 #include "monitor_client.h"
23 #ifdef SUPPORT_RECORDER
24 #include "i_standard_recorder_service.h"
25 #endif
26 #ifdef SUPPORT_TRANSCODER
27 #include "i_standard_transcoder_service.h"
28 #endif
29 #ifdef SUPPORT_PLAYER
30 #include "i_standard_player_service.h"
31 #endif
32 #ifdef SUPPORT_METADATA
33 #include "i_standard_avmetadatahelper_service.h"
34 #endif
35 #ifdef SUPPORT_SCREEN_CAPTURE
36 #include "i_standard_screen_capture_service.h"
37 #include "i_standard_screen_capture_monitor_service.h"
38 #endif
39 #include "media_log.h"
40 #include "media_errors.h"
41 #include "player_xcollie.h"
42
43 namespace {
44 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_PLAYER, "MediaClient"};
45 }
46
47 namespace OHOS {
48 namespace Media {
49 static MediaClient g_mediaClientInstance;
GetInstance()50 IMediaService &MediaServiceFactory::GetInstance()
51 {
52 return g_mediaClientInstance;
53 }
54
MediaClient()55 MediaClient::MediaClient() noexcept
56 {
57 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
58 }
59
~MediaClient()60 MediaClient::~MediaClient()
61 {
62 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
63 }
64
IsAlived()65 bool MediaClient::IsAlived()
66 {
67 if (mediaProxy_ == nullptr) {
68 mediaProxy_ = GetMediaProxy();
69 }
70
71 return (mediaProxy_ != nullptr) ? true : false;
72 }
73
74 #ifdef SUPPORT_RECORDER
CreateRecorderService()75 std::shared_ptr<IRecorderService> MediaClient::CreateRecorderService()
76 {
77 std::lock_guard<std::mutex> lock(mutex_);
78 CHECK_AND_RETURN_RET_LOG(IsAlived(), nullptr, "media service does not exist.");
79
80 sptr<IRemoteObject> object = mediaProxy_->GetSubSystemAbility(
81 IStandardMediaService::MediaSystemAbility::MEDIA_RECORDER, listenerStub_->AsObject());
82 CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "recorder proxy object is nullptr.");
83
84 sptr<IStandardRecorderService> recorderProxy = iface_cast<IStandardRecorderService>(object);
85 CHECK_AND_RETURN_RET_LOG(recorderProxy != nullptr, nullptr, "recorder proxy is nullptr.");
86
87 std::shared_ptr<RecorderClient> recorder = RecorderClient::Create(recorderProxy);
88 CHECK_AND_RETURN_RET_LOG(recorder != nullptr, nullptr, "failed to create recorder client.");
89
90 recorderClientList_.push_back(recorder);
91 return recorder;
92 }
93
DestroyMediaProfileService(std::shared_ptr<IRecorderProfilesService> recorderProfiles)94 int32_t MediaClient::DestroyMediaProfileService(std::shared_ptr<IRecorderProfilesService> recorderProfiles)
95 {
96 std::lock_guard<std::mutex> lock(mutex_);
97 CHECK_AND_RETURN_RET_LOG(recorderProfiles != nullptr, MSERR_NO_MEMORY, "input recorderProfiles is nullptr.");
98 recorderProfilesClientList_.remove(recorderProfiles);
99 return MSERR_OK;
100 }
101
DestroyRecorderService(std::shared_ptr<IRecorderService> recorder)102 int32_t MediaClient::DestroyRecorderService(std::shared_ptr<IRecorderService> recorder)
103 {
104 std::lock_guard<std::mutex> lock(mutex_);
105 CHECK_AND_RETURN_RET_LOG(recorder != nullptr, MSERR_NO_MEMORY, "input recorder is nullptr.");
106 recorderClientList_.remove(recorder);
107 return MSERR_OK;
108 }
109
CreateRecorderProfilesService()110 std::shared_ptr<IRecorderProfilesService> MediaClient::CreateRecorderProfilesService()
111 {
112 std::lock_guard<std::mutex> lock(mutex_);
113 CHECK_AND_RETURN_RET_LOG(IsAlived(), nullptr, "media service does not exist.");
114
115 sptr<IRemoteObject> object = mediaProxy_->GetSubSystemAbility(
116 IStandardMediaService::MediaSystemAbility::RECORDER_PROFILES, listenerStub_->AsObject());
117 CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "recorderProfiles proxy object is nullptr.");
118
119 sptr<IStandardRecorderProfilesService> recorderProfilesProxy = iface_cast<IStandardRecorderProfilesService>(object);
120 CHECK_AND_RETURN_RET_LOG(recorderProfilesProxy != nullptr, nullptr, "recorderProfiles proxy is nullptr.");
121
122 std::shared_ptr<RecorderProfilesClient> recorderProfiles = RecorderProfilesClient::Create(recorderProfilesProxy);
123 CHECK_AND_RETURN_RET_LOG(recorderProfiles != nullptr, nullptr, "failed to create recorderProfiles client.");
124
125 recorderProfilesClientList_.push_back(recorderProfiles);
126 return recorderProfiles;
127 }
128 #endif
129
130 #ifdef SUPPORT_TRANSCODER
CreateTransCoderService()131 std::shared_ptr<ITransCoderService> MediaClient::CreateTransCoderService()
132 {
133 std::lock_guard<std::mutex> lock(mutex_);
134 CHECK_AND_RETURN_RET_LOG(IsAlived(), nullptr, "media service does not exist.");
135
136 sptr<IRemoteObject> object = mediaProxy_->GetSubSystemAbility(
137 IStandardMediaService::MediaSystemAbility::MEDIA_TRANSCODER, listenerStub_->AsObject());
138 CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "transCoder proxy object is nullptr.");
139
140 sptr<IStandardTransCoderService> transCoderProxy = iface_cast<IStandardTransCoderService>(object);
141 CHECK_AND_RETURN_RET_LOG(transCoderProxy != nullptr, nullptr, "transCoder proxy is nullptr.");
142
143 std::shared_ptr<TransCoderClient> transCoder = TransCoderClient::Create(transCoderProxy);
144 CHECK_AND_RETURN_RET_LOG(transCoder != nullptr, nullptr, "failed to create transCoder client.");
145
146 transCoderClientList_.push_back(transCoder);
147 return transCoder;
148 }
149
DestroyTransCoderService(std::shared_ptr<ITransCoderService> transCoder)150 int32_t MediaClient::DestroyTransCoderService(std::shared_ptr<ITransCoderService> transCoder)
151 {
152 std::lock_guard<std::mutex> lock(mutex_);
153 CHECK_AND_RETURN_RET_LOG(transCoder != nullptr, MSERR_NO_MEMORY, "input transCoder is nullptr.");
154 transCoderClientList_.remove(transCoder);
155 return MSERR_OK;
156 }
157 #endif
158
159 #ifdef SUPPORT_PLAYER
CreatePlayerService()160 std::shared_ptr<IPlayerService> MediaClient::CreatePlayerService()
161 {
162 std::lock_guard<std::mutex> lock(mutex_);
163 CHECK_AND_RETURN_RET_LOG(IsAlived(), nullptr, "media service does not exist.");
164
165 sptr<IRemoteObject> object = mediaProxy_->GetSubSystemAbility(
166 IStandardMediaService::MediaSystemAbility::MEDIA_PLAYER, listenerStub_->AsObject());
167 CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "player proxy object is nullptr.");
168
169 sptr<IStandardPlayerService> playerProxy = iface_cast<IStandardPlayerService>(object);
170 CHECK_AND_RETURN_RET_LOG(playerProxy != nullptr, nullptr, "player proxy is nullptr.");
171
172 std::shared_ptr<PlayerClient> player = PlayerClient::Create(playerProxy);
173 CHECK_AND_RETURN_RET_LOG(player != nullptr, nullptr, "failed to create player client.");
174
175 playerClientList_.push_back(player);
176 return player;
177 }
178
DestroyPlayerService(std::shared_ptr<IPlayerService> player)179 int32_t MediaClient::DestroyPlayerService(std::shared_ptr<IPlayerService> player)
180 {
181 std::lock_guard<std::mutex> lock(mutex_);
182 CHECK_AND_RETURN_RET_LOG(player != nullptr, MSERR_NO_MEMORY, "input player is nullptr.");
183 playerClientList_.remove(player);
184 return MSERR_OK;
185 }
186 #endif
187
188 #ifdef SUPPORT_METADATA
CreateAVMetadataHelperService()189 std::shared_ptr<IAVMetadataHelperService> MediaClient::CreateAVMetadataHelperService()
190 {
191 std::lock_guard<std::mutex> lock(mutex_);
192 CHECK_AND_RETURN_RET_LOG(IsAlived(), nullptr, "media service does not exist.");
193
194 sptr<IRemoteObject> object = mediaProxy_->GetSubSystemAbility(
195 IStandardMediaService::MediaSystemAbility::MEDIA_AVMETADATAHELPER, listenerStub_->AsObject());
196 CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "avmetadatahelper proxy object is nullptr.");
197
198 sptr<IStandardAVMetadataHelperService> avMetadataHelperProxy = iface_cast<IStandardAVMetadataHelperService>(object);
199 CHECK_AND_RETURN_RET_LOG(avMetadataHelperProxy != nullptr, nullptr, "avmetadatahelper proxy is nullptr.");
200
201 std::shared_ptr<AVMetadataHelperClient> avMetadataHelper = AVMetadataHelperClient::Create(avMetadataHelperProxy);
202 CHECK_AND_RETURN_RET_LOG(avMetadataHelper != nullptr, nullptr, "failed to create avmetadatahelper client.");
203
204 avMetadataHelperClientList_.push_back(avMetadataHelper);
205 return avMetadataHelper;
206 }
207
DestroyAVMetadataHelperService(std::shared_ptr<IAVMetadataHelperService> avMetadataHelper)208 int32_t MediaClient::DestroyAVMetadataHelperService(std::shared_ptr<IAVMetadataHelperService> avMetadataHelper)
209 {
210 std::lock_guard<std::mutex> lock(mutex_);
211 CHECK_AND_RETURN_RET_LOG(avMetadataHelper != nullptr, MSERR_NO_MEMORY,
212 "input avmetadatahelper is nullptr.");
213 avMetadataHelperClientList_.remove(avMetadataHelper);
214 return MSERR_OK;
215 }
216 #endif
217
218 #ifdef SUPPORT_SCREEN_CAPTURE
CreateScreenCaptureMonitorService()219 std::shared_ptr<IScreenCaptureMonitorService> MediaClient::CreateScreenCaptureMonitorService()
220 {
221 std::lock_guard<std::mutex> lock(mutex_);
222 CHECK_AND_RETURN_RET_LOG(IsAlived(), nullptr, "media service does not exist.");
223
224 sptr<IRemoteObject> object = mediaProxy_->GetSubSystemAbility(
225 IStandardMediaService::MediaSystemAbility::MEDIA_SCREEN_CAPTURE_MONITOR, listenerStub_->AsObject());
226 CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "screenCaptureMonitor proxy object is nullptr.");
227
228 sptr<IStandardScreenCaptureMonitorService> screenCaptureMonitorProxy =
229 iface_cast<IStandardScreenCaptureMonitorService>(object);
230 CHECK_AND_RETURN_RET_LOG(screenCaptureMonitorProxy != nullptr, nullptr, "screenCaptureMonitor proxy is nullptr.");
231
232 std::shared_ptr<ScreenCaptureMonitorClient> screenCaptureMonitor =
233 ScreenCaptureMonitorClient::Create(screenCaptureMonitorProxy);
234 CHECK_AND_RETURN_RET_LOG(screenCaptureMonitor != nullptr, nullptr, "failed to create screenCaptureMonitor client.");
235 screenCaptureMonitorClientList_.push_back(screenCaptureMonitor);
236 return screenCaptureMonitor;
237 }
238
DestroyScreenCaptureMonitorService(std::shared_ptr<IScreenCaptureMonitorService> screenCaptureMonitor)239 int32_t MediaClient::DestroyScreenCaptureMonitorService(
240 std::shared_ptr<IScreenCaptureMonitorService> screenCaptureMonitor)
241 {
242 std::lock_guard<std::mutex> lock(mutex_);
243 CHECK_AND_RETURN_RET_LOG(screenCaptureMonitor != nullptr, MSERR_NO_MEMORY,
244 "input screenCapture is nullptr.");
245 screenCaptureMonitorClientList_.remove(screenCaptureMonitor);
246 return MSERR_OK;
247 }
248
CreateScreenCaptureService()249 std::shared_ptr<IScreenCaptureService> MediaClient::CreateScreenCaptureService()
250 {
251 std::lock_guard<std::mutex> lock(mutex_);
252 CHECK_AND_RETURN_RET_LOG(IsAlived(), nullptr, "media service does not exist.");
253
254 sptr<IRemoteObject> object = mediaProxy_->GetSubSystemAbility(
255 IStandardMediaService::MediaSystemAbility::MEDIA_SCREEN_CAPTURE, listenerStub_->AsObject());
256 CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "screenCapture proxy object is nullptr.");
257
258 sptr<IStandardScreenCaptureService> screenCaptureProxy = iface_cast<IStandardScreenCaptureService>(object);
259 CHECK_AND_RETURN_RET_LOG(screenCaptureProxy != nullptr, nullptr, "screenCapture proxy is nullptr.");
260
261 std::shared_ptr<ScreenCaptureClient> screenCapture = ScreenCaptureClient::Create(screenCaptureProxy);
262 CHECK_AND_RETURN_RET_LOG(screenCapture != nullptr, nullptr, "failed to create screenCapture client.");
263
264 screenCaptureClientList_.push_back(screenCapture);
265 return screenCapture;
266 }
267
DestroyScreenCaptureService(std::shared_ptr<IScreenCaptureService> screenCapture)268 int32_t MediaClient::DestroyScreenCaptureService(std::shared_ptr<IScreenCaptureService> screenCapture)
269 {
270 std::lock_guard<std::mutex> lock(mutex_);
271 CHECK_AND_RETURN_RET_LOG(screenCapture != nullptr, MSERR_NO_MEMORY,
272 "input screenCapture is nullptr.");
273 screenCaptureClientList_.remove(screenCapture);
274 return MSERR_OK;
275 }
276
CreateScreenCaptureControllerClient()277 std::shared_ptr<IScreenCaptureController> MediaClient::CreateScreenCaptureControllerClient()
278 {
279 std::lock_guard<std::mutex> lock(mutex_);
280 MEDIA_LOGI("MediaClient::CreateScreenCaptureControllerClient() start");
281 CHECK_AND_RETURN_RET_LOG(IsAlived(), nullptr, "media service does not exist.");
282
283 sptr<IRemoteObject> object = mediaProxy_->GetSubSystemAbility(
284 IStandardMediaService::MediaSystemAbility::MEDIA_SCREEN_CAPTURE_CONTROLLER, listenerStub_->AsObject());
285 CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "screenCapture controller proxy object is nullptr.");
286
287 sptr<IStandardScreenCaptureController> controllerProxy = iface_cast<IStandardScreenCaptureController>(object);
288 CHECK_AND_RETURN_RET_LOG(controllerProxy != nullptr, nullptr, "controllerProxy is nullptr.");
289
290 std::shared_ptr<ScreenCaptureControllerClient> controller = ScreenCaptureControllerClient::Create(controllerProxy);
291 CHECK_AND_RETURN_RET_LOG(controller != nullptr, nullptr, "failed to create screenCapture controller.");
292
293 screenCaptureControllerList_.push_back(controller);
294 MEDIA_LOGI("MediaClient::CreateScreenCaptureControllerClient() end");
295 return controller;
296 }
297
DestroyScreenCaptureControllerClient(std::shared_ptr<IScreenCaptureController> controller)298 int32_t MediaClient::DestroyScreenCaptureControllerClient(std::shared_ptr<IScreenCaptureController> controller)
299 {
300 std::lock_guard<std::mutex> lock(mutex_);
301 CHECK_AND_RETURN_RET_LOG(controller != nullptr, MSERR_NO_MEMORY,
302 "input screenCapture controller is nullptr.");
303 screenCaptureControllerList_.remove(controller);
304 return MSERR_OK;
305 }
306 #endif
307
GetMonitorProxy()308 sptr<IStandardMonitorService> MediaClient::GetMonitorProxy()
309 {
310 std::lock_guard<std::mutex> lock(mutex_);
311 CHECK_AND_RETURN_RET_LOG(IsAlived(), nullptr, "media service does not exist.");
312
313 sptr<IRemoteObject> object = mediaProxy_->GetSubSystemAbility(
314 IStandardMediaService::MediaSystemAbility::MEDIA_MONITOR, listenerStub_->AsObject());
315 CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "monitor proxy object is nullptr.");
316
317 sptr<IStandardMonitorService> monitorProxy = iface_cast<IStandardMonitorService>(object);
318 CHECK_AND_RETURN_RET_LOG(monitorProxy != nullptr, nullptr, "monitor proxy is nullptr.");
319
320 return monitorProxy;
321 }
322
GetMediaProxy()323 sptr<IStandardMediaService> MediaClient::GetMediaProxy()
324 {
325 MEDIA_LOGD("enter");
326 sptr<ISystemAbilityManager> samgr = nullptr;
327 samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
328 CHECK_AND_RETURN_RET_LOG(samgr != nullptr, nullptr, "system ability manager is nullptr.");
329 sptr<IRemoteObject> object = nullptr;
330 object = samgr->GetSystemAbility(OHOS::PLAYER_DISTRIBUTED_SERVICE_ID);
331 CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "media object is nullptr.");
332
333 mediaProxy_ = iface_cast<IStandardMediaService>(object);
334 CHECK_AND_RETURN_RET_LOG(mediaProxy_ != nullptr, nullptr, "media proxy is nullptr.");
335
336 pid_t pid = 0;
337 deathRecipient_ = new(std::nothrow) MediaDeathRecipient(pid);
338 CHECK_AND_RETURN_RET_LOG(deathRecipient_ != nullptr, nullptr, "failed to new MediaDeathRecipient.");
339
340 deathRecipient_->SetNotifyCb(std::bind(&MediaClient::MediaServerDied, std::placeholders::_1));
341 bool result = object->AddDeathRecipient(deathRecipient_);
342 if (!result) {
343 MEDIA_LOGE("failed to add deathRecipient");
344 return nullptr;
345 }
346
347 listenerStub_ = new(std::nothrow) MediaListenerStub();
348 CHECK_AND_RETURN_RET_LOG(listenerStub_ != nullptr, nullptr, "failed to new MediaListenerStub");
349 return mediaProxy_;
350 }
351
MediaServerDied(pid_t pid)352 void MediaClient::MediaServerDied(pid_t pid)
353 {
354 MEDIA_LOGE("media server is died, pid:%{public}d!", pid);
355 g_mediaClientInstance.DoMediaServerDied();
356 }
357
AVPlayerServerDied()358 void MediaClient::AVPlayerServerDied()
359 {
360 #ifdef SUPPORT_PLAYER
361 for (auto &it : playerClientList_) {
362 auto player = std::static_pointer_cast<PlayerClient>(it);
363 if (player != nullptr) {
364 player->MediaServerDied();
365 }
366 }
367 #endif
368
369 #ifdef SUPPORT_METADATA
370 for (auto &it : avMetadataHelperClientList_) {
371 auto avMetadataHelper = std::static_pointer_cast<AVMetadataHelperClient>(it);
372 if (avMetadataHelper != nullptr) {
373 avMetadataHelper->MediaServerDied();
374 }
375 }
376 #endif
377 }
378
DoMediaServerDied()379 void MediaClient::DoMediaServerDied()
380 {
381 std::lock_guard<std::mutex> lock(mutex_);
382 if (mediaProxy_ != nullptr) {
383 (void)mediaProxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
384 mediaProxy_ = nullptr;
385 }
386 listenerStub_ = nullptr;
387 deathRecipient_ = nullptr;
388
389 std::shared_ptr<MonitorClient> monitor = MonitorClient::GetInstance();
390 CHECK_AND_RETURN_LOG(monitor != nullptr, "Failed to get monitor Instance!");
391 monitor->MediaServerDied();
392
393 AVPlayerServerDied();
394 #ifdef SUPPORT_RECORDER
395 for (auto &it : recorderClientList_) {
396 auto recorder = std::static_pointer_cast<RecorderClient>(it);
397 if (recorder != nullptr) {
398 recorder->MediaServerDied();
399 }
400 }
401
402 for (auto &it : recorderProfilesClientList_) {
403 auto recorderProfilesClient = std::static_pointer_cast<RecorderProfilesClient>(it);
404 if (recorderProfilesClient != nullptr) {
405 recorderProfilesClient->MediaServerDied();
406 }
407 }
408 #endif
409
410 #ifdef SUPPORT_SCREEN_CAPTURE
411 for (auto &it : screenCaptureClientList_) {
412 auto screenCaptureClient = std::static_pointer_cast<ScreenCaptureClient>(it);
413 if (screenCaptureClient != nullptr) {
414 screenCaptureClient->MediaServerDied();
415 }
416 }
417 for (auto &it : screenCaptureMonitorClientList_) {
418 auto screenCaptureMonitorClient = std::static_pointer_cast<ScreenCaptureMonitorClient>(it);
419 if (screenCaptureMonitorClient != nullptr) {
420 screenCaptureMonitorClient->MediaServerDied();
421 }
422 }
423 for (auto &it : screenCaptureControllerList_) {
424 auto screenCaptureControllerClient = std::static_pointer_cast<ScreenCaptureControllerClient>(it);
425 if (screenCaptureControllerClient != nullptr) {
426 screenCaptureControllerClient->MediaServerDied();
427 }
428 }
429 #endif
430 }
431 } // namespace Media
432 } // namespace OHOS
433