• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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_monitor_manager.h"
17 #include "log.h"
18 #include "parameters.h"
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21 #include "monitor_error.h"
22 #include "media_monitor_base.h"
23 #include "media_monitor_client.h"
24 #include "media_monitor_death_recipient.h"
25 
26 namespace {
27 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_FOUNDATION, "MediaMonitorManager"};
28 }
29 
30 namespace OHOS {
31 namespace Media {
32 namespace MediaMonitor {
33 
34 using namespace std;
35 
36 static mutex g_mmProxyMutex;
37 static sptr<IMediaMonitor> g_mmProxy = nullptr;
38 constexpr int MAX_DUMP_TIME = 90;
39 
MediaMonitorManager()40 MediaMonitorManager::MediaMonitorManager()
41 {
42     versionType_ = OHOS::system::GetParameter("const.logsystem.versiontype", COMMERCIAL_VERSION);
43     MEDIA_LOG_I("version type:%{public}s", versionType_.c_str());
44 }
45 
GetInstance()46 MediaMonitorManager& MediaMonitorManager::GetInstance()
47 {
48     static MediaMonitorManager monitorManager;
49     return monitorManager;
50 }
51 
GetMediaMonitorProxy()52 static const sptr<IMediaMonitor> GetMediaMonitorProxy()
53 {
54     MEDIA_LOG_D("Start to get media monitor manager proxy.");
55     lock_guard<mutex> lock(g_mmProxyMutex);
56 
57     if (g_mmProxy == nullptr) {
58         auto smmgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
59         FALSE_RETURN_V_MSG_E(smmgr != nullptr, nullptr, "smmgr init failed.");
60 
61         sptr<IRemoteObject> object = smmgr->CheckSystemAbility(MEDIA_MONITOR_SERVICE_ID);
62         FALSE_RETURN_V_MSG_E(object != nullptr, nullptr, "Object is NULL.");
63 
64         g_mmProxy = iface_cast<IMediaMonitor>(object);
65         FALSE_RETURN_V_MSG_E(g_mmProxy != nullptr, nullptr, "Init g_mmProxy is NULL.");
66 
67         MEDIA_LOG_I("Init g_mmProxy is assigned.");
68         pid_t pid = 0;
69         sptr<MediaMonitorDeathRecipient> deathRecipient = new(std::nothrow) MediaMonitorDeathRecipient(pid);
70         if (deathRecipient != nullptr) {
71             deathRecipient->SetNotifyCb(std::bind(&MediaMonitorManager::MediaMonitorDied,
72                 std::placeholders::_1));
73             MEDIA_LOG_I("Register media monitor death recipient");
74             bool result = object->AddDeathRecipient(deathRecipient);
75             if (!result) {
76                 MEDIA_LOG_E("failed to add deathRecipient");
77             }
78         }
79     }
80     return g_mmProxy;
81 }
82 
MediaMonitorDied(pid_t pid)83 void MediaMonitorManager::MediaMonitorDied(pid_t pid)
84 {
85     MEDIA_LOG_I("media monitor died");
86     lock_guard<mutex> lock(g_mmProxyMutex);
87     if (g_mmProxy == nullptr) {
88         MEDIA_LOG_I("media monitor has already died!");
89         return;
90     }
91     g_mmProxy = nullptr;
92 }
93 
WriteLogMsg(std::shared_ptr<EventBean> & bean)94 void MediaMonitorManager::WriteLogMsg(std::shared_ptr<EventBean> &bean)
95 {
96     MEDIA_LOG_D("Write event to media monitor");
97     sptr<IMediaMonitor> gamp = GetMediaMonitorProxy();
98     if (gamp == nullptr) {
99         MEDIA_LOG_E("gamp is nullptr.");
100         return;
101     }
102     gamp->WriteLogMsg(bean);
103 }
104 
GetAudioRouteMsg(std::map<PreferredType,shared_ptr<MonitorDeviceInfo>> & preferredDevices)105 void MediaMonitorManager::GetAudioRouteMsg(std::map<PreferredType, shared_ptr<MonitorDeviceInfo>> &preferredDevices)
106 {
107     MEDIA_LOG_D("Get audio route devices");
108     sptr<IMediaMonitor> gamp = GetMediaMonitorProxy();
109     if (gamp == nullptr) {
110         MEDIA_LOG_E("gamp is nullptr.");
111         return;
112     }
113     gamp->GetAudioRouteMsg(preferredDevices);
114 }
115 
GetAudioExcludedDevicesMsg(std::map<AudioDeviceUsage,std::vector<std::shared_ptr<MonitorDeviceInfo>>> & excludedDevices)116 void MediaMonitorManager::GetAudioExcludedDevicesMsg(std::map<AudioDeviceUsage,
117     std::vector<std::shared_ptr<MonitorDeviceInfo>>> &excludedDevices)
118 {
119     MEDIA_LOG_D("Get audio excluded devices");
120     sptr<IMediaMonitor> gamp = GetMediaMonitorProxy();
121     if (gamp == nullptr) {
122         MEDIA_LOG_E("gamp is nullptr.");
123         return;
124     }
125     gamp->GetAudioExcludedDevicesMsg(excludedDevices);
126 }
127 
WriteAudioBuffer(const std::string & fileName,void * ptr,size_t size)128 void MediaMonitorManager::WriteAudioBuffer(const std::string &fileName, void *ptr, size_t size)
129 {
130     if (!dumpEnable_) {
131         MEDIA_LOG_D("dump status error return");
132         return;
133     }
134 
135     if (versionType_ != BETA_VERSION) {
136         return;
137     }
138 
139     int duration = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()) - dumpStartTime_;
140     if (duration > MAX_DUMP_TIME) {
141         MEDIA_LOG_I("dump duration > 90 return");
142         dumpEnable_ = false;
143         dumpStartTime_ = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
144         return;
145     }
146 
147     FALSE_RETURN_MSG(ptr != nullptr, "in data is empty");
148     sptr<IMediaMonitor> gamp = GetMediaMonitorProxy();
149     FALSE_RETURN_MSG(gamp != nullptr, "gamp is nullptr");
150     int32_t ret = gamp->WriteAudioBuffer(fileName, ptr, size);
151     MEDIA_LOG_D("write audio buffer ret %{public}d", ret);
152 }
153 
GetMediaParameters(const std::vector<std::string> & subKeys,std::vector<std::pair<std::string,std::string>> & result)154 int32_t MediaMonitorManager::GetMediaParameters(const std::vector<std::string> &subKeys,
155     std::vector<std::pair<std::string, std::string>> &result)
156 {
157     if (versionType_ != BETA_VERSION) {
158         MEDIA_LOG_E("version type is commercial");
159         return ERROR;
160     }
161 
162     bool match = false;
163     std::string matchKey;
164     std::string value = "false";
165     for (auto it = subKeys.begin(); it != subKeys.end(); it++) {
166         if (*it == BETA_DUMP_TYPE || *it == DEFAULT_DUMP_TYPE) {
167             match = true;
168             matchKey = *it;
169             break;
170         }
171     }
172     FALSE_RETURN_V_MSG_E(match, ERROR, "get media param invalid param");
173 
174     if (dumpEnable_ == false) {
175         result.emplace_back(std::make_pair(matchKey, value));
176         MEDIA_LOG_I("get media param: close");
177         return SUCCESS;
178     }
179 
180     sptr<IMediaMonitor> gamp = GetMediaMonitorProxy();
181     if (gamp == nullptr) {
182         MEDIA_LOG_E("gamp is nullptr.");
183         return ERROR;
184     }
185 
186     int32_t status = 0;
187     int32_t ret = gamp->GetPcmDumpStatus(status);
188     if (ret != SUCCESS) {
189         MEDIA_LOG_E("get dump media param failed");
190         return ERROR;
191     }
192     if (status > 0) {
193         value = "true";
194     }
195 
196     result.emplace_back(std::make_pair(matchKey, value));
197     MEDIA_LOG_I("get media param: %{public}s", value.c_str());
198     return SUCCESS;
199 }
200 
SetMediaParameters(const std::vector<std::pair<std::string,std::string>> & kvpairs)201 int32_t MediaMonitorManager::SetMediaParameters(const std::vector<std::pair<std::string, std::string>> &kvpairs)
202 {
203     if (versionType_ != BETA_VERSION) {
204         MEDIA_LOG_E("version type is commercial");
205         return ERROR;
206     }
207     std::string dumpType;
208     std::string dumpEnable;
209     for (auto it = kvpairs.begin(); it != kvpairs.end(); ++it) {
210         if (it->first == DEFAULT_DUMP_TYPE || it->first == BETA_DUMP_TYPE) {
211             dumpType = it->first;
212             dumpEnable = it->second;
213             break;
214         }
215         return ERROR;
216     }
217 
218     if (dumpEnable_ && dumpEnable == "true") {
219         int duration = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()) - dumpStartTime_;
220         if (duration < MAX_DUMP_TIME) {
221             MEDIA_LOG_E("set dump media param failed, already dumping");
222             return ERROR;
223         }
224     }
225     dumpType_ = dumpType;
226     dumpEnable_ = (dumpEnable == "true") ? true : false;
227     if (dumpEnable_) {
228         dumpStartTime_ = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
229     }
230 
231     MEDIA_LOG_I("set dump media param, %{public}d %{public}s", dumpEnable_, dumpType_.c_str());
232     sptr<IMediaMonitor> gamp = GetMediaMonitorProxy();
233     if (gamp == nullptr) {
234         MEDIA_LOG_E("gamp is nullptr.");
235         return ERROR;
236     }
237 
238     int32_t ret = gamp->SetMediaParameters(dumpType, dumpEnable);
239     if (ret != SUCCESS) {
240         MEDIA_LOG_E("set dump media param failed");
241     }
242     return ret;
243 }
244 
ErasePreferredDeviceByType(const PreferredType preferredType)245 int32_t MediaMonitorManager::ErasePreferredDeviceByType(const PreferredType preferredType)
246 {
247     MEDIA_LOG_D("Erase preferred device by type");
248     sptr<IMediaMonitor> gamp = GetMediaMonitorProxy();
249     if (gamp == nullptr) {
250         MEDIA_LOG_E("gamp is nullptr.");
251         return ERROR;
252     }
253     int32_t ret = gamp->ErasePreferredDeviceByType(preferredType);
254     return ret;
255 }
256 } // namespace MediaMonitor
257 } // namespace Media
258 } // namespace OHOS