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.h"
17 #include "iservice_registry.h"
18 #include "media_log.h"
19 #include "media_errors.h"
20 #include "system_ability_definition.h"
21 #include "media_server_manager.h"
22 #include "mem_mgr_client.h"
23 #include "mem_mgr_proxy.h"
24 
25 namespace {
26 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_PLAYER, "MediaServer"};
27 }
28 
29 namespace OHOS {
30 namespace Media {
31 constexpr int32_t SYSTEM_STATUS_START = 1;
32 constexpr int32_t SYSTEM_STATUS_STOP = 0;
33 constexpr int32_t SYSTEM_PROCESS_TYPE = 1;
34 
REGISTER_SYSTEM_ABILITY_BY_ID(MediaServer,PLAYER_DISTRIBUTED_SERVICE_ID,true)35 REGISTER_SYSTEM_ABILITY_BY_ID(MediaServer, PLAYER_DISTRIBUTED_SERVICE_ID, true)
36 MediaServer::MediaServer(int32_t systemAbilityId, bool runOnCreate)
37     : SystemAbility(systemAbilityId, runOnCreate)
38 {
39     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
40 }
41 
~MediaServer()42 MediaServer::~MediaServer()
43 {
44     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
45 }
46 
OnDump()47 void MediaServer::OnDump()
48 {
49     MEDIA_LOGD("MediaServer OnDump");
50 }
51 
OnStart()52 void MediaServer::OnStart()
53 {
54     MEDIA_LOGD("MediaServer OnStart");
55     bool res = Publish(this);
56     MEDIA_LOGD("MediaServer OnStart res=%{public}d", res);
57     AddSystemAbilityListener(MEMORY_MANAGER_SA_ID);
58 }
59 
OnStop()60 void MediaServer::OnStop()
61 {
62     MEDIA_LOGD("MediaServer OnStop");
63     Memory::MemMgrClient::GetInstance().NotifyProcessStatus(getpid(),
64         SYSTEM_PROCESS_TYPE, SYSTEM_STATUS_STOP, OHOS::PLAYER_DISTRIBUTED_SERVICE_ID);
65 }
66 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)67 void MediaServer::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
68 {
69     MEDIA_LOGD("OnAddSystemAbility systemAbilityId:%{public}d", systemAbilityId);
70     if (systemAbilityId == MEMORY_MANAGER_SA_ID) {
71         Memory::MemMgrClient::GetInstance().NotifyProcessStatus(getpid(),
72             SYSTEM_PROCESS_TYPE, SYSTEM_STATUS_START, OHOS::PLAYER_DISTRIBUTED_SERVICE_ID);
73         MediaServerManager::GetInstance().NotifyMemMgrLoaded();
74     }
75 }
76 
GetSubSystemAbility(IStandardMediaService::MediaSystemAbility subSystemId,const sptr<IRemoteObject> & listener)77 sptr<IRemoteObject> MediaServer::GetSubSystemAbility(IStandardMediaService::MediaSystemAbility subSystemId,
78     const sptr<IRemoteObject> &listener)
79 {
80     int32_t ret = MediaServiceStub::SetDeathListener(listener);
81     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, nullptr, "failed set death listener");
82 
83     switch (subSystemId) {
84         case MediaSystemAbility::MEDIA_RECORDER: {
85             return MediaServerManager::GetInstance().CreateStubObject(MediaServerManager::RECORDER);
86         }
87         case MediaSystemAbility::MEDIA_TRANSCODER: {
88             return MediaServerManager::GetInstance().CreateStubObject(MediaServerManager::TRANSCODER);
89         }
90         case MediaSystemAbility::MEDIA_PLAYER: {
91             return MediaServerManager::GetInstance().CreateStubObject(MediaServerManager::PLAYER);
92         }
93         case MediaSystemAbility::MEDIA_AVMETADATAHELPER: {
94             return MediaServerManager::GetInstance().CreateStubObject(MediaServerManager::AVMETADATAHELPER);
95         }
96         case MediaSystemAbility::MEDIA_CODECLIST: {
97             return MediaServerManager::GetInstance().CreateStubObject(MediaServerManager::AVCODECLIST);
98         }
99         case MediaSystemAbility::MEDIA_AVCODEC: {
100             return MediaServerManager::GetInstance().CreateStubObject(MediaServerManager::AVCODEC);
101         }
102         case MediaSystemAbility::RECORDER_PROFILES: {
103             return MediaServerManager::GetInstance().CreateStubObject(MediaServerManager::RECORDERPROFILES);
104         }
105         case MediaSystemAbility::MEDIA_MONITOR: {
106             return MediaServerManager::GetInstance().CreateStubObject(MediaServerManager::MONITOR);
107         }
108         case MediaSystemAbility::MEDIA_SCREEN_CAPTURE: {
109             return MediaServerManager::GetInstance().CreateStubObject(MediaServerManager::SCREEN_CAPTURE);
110         }
111         case MediaSystemAbility::MEDIA_SCREEN_CAPTURE_CONTROLLER: {
112             return MediaServerManager::GetInstance().CreateStubObject(MediaServerManager::SCREEN_CAPTURE_CONTROLLER);
113         }
114         case MediaSystemAbility::MEDIA_SCREEN_CAPTURE_MONITOR: {
115             return MediaServerManager::GetInstance().CreateStubObject(MediaServerManager::SCREEN_CAPTURE_MONITOR);
116         }
117         default: {
118             MEDIA_LOGE("default case, media client need check subSystemId");
119             return nullptr;
120         }
121     }
122 }
123 
Dump(int32_t fd,const std::vector<std::u16string> & args)124 int32_t MediaServer::Dump(int32_t fd, const std::vector<std::u16string> &args)
125 {
126     CHECK_AND_RETURN_RET_LOG(fd > 0, OHOS::INVALID_OPERATION, "Failed to check fd.");
127 
128     auto ret = MediaServerManager::GetInstance().Dump(fd, args);
129     CHECK_AND_RETURN_RET_LOG(ret == NO_ERROR,
130         OHOS::INVALID_OPERATION, "Failed to call MediaServerManager::Dump.");
131     return OHOS::NO_ERROR;
132 }
133 } // namespace Media
134 } // namespace OHOS
135