• 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_client.h"
17 #include "monitor_error.h"
18 #include "log.h"
19 
20 namespace {
21 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_FOUNDATION, "MediaMonitorClient"};
22 }
23 
24 using namespace std;
25 
26 namespace OHOS {
27 namespace Media {
28 namespace MediaMonitor {
29 
30 constexpr int MAX_MAP_SIZE = 1000;
31 
MediaMonitorClient(const sptr<IRemoteObject> & impl)32 MediaMonitorClient::MediaMonitorClient(const sptr<IRemoteObject> &impl)
33     : IRemoteProxy<IMediaMonitor>(impl)
34 {
35     MEDIA_LOG_I("MediaMonitorClient::MediaMonitorClient");
36 }
37 
WriteLogMsg(std::shared_ptr<EventBean> & bean)38 void MediaMonitorClient::WriteLogMsg(std::shared_ptr<EventBean> &bean)
39 {
40     MessageParcel data;
41     MessageParcel reply;
42     MessageOption option(MessageOption::TF_ASYNC);
43 
44     data.WriteInterfaceToken(GetDescriptor());
45     bean->WriteToParcel(data);
46     Remote()->SendRequest(
47         static_cast<uint32_t>(MediaMonitorInterfaceCode::WRITE_LOG_MSG), data, reply, option);
48 }
49 
GetAudioRouteMsg(std::map<PreferredType,std::shared_ptr<MonitorDeviceInfo>> & preferredDevices)50 int32_t MediaMonitorClient::GetAudioRouteMsg(std::map<PreferredType,
51     std::shared_ptr<MonitorDeviceInfo>> &preferredDevices)
52 {
53     MessageParcel data;
54     MessageParcel reply;
55     MessageOption option;
56 
57     data.WriteInterfaceToken(GetDescriptor());
58     int32_t error = Remote()->SendRequest(
59         static_cast<uint32_t>(MediaMonitorInterfaceCode::GET_AUDIO_ROUTE_MSG), data, reply, option);
60     FALSE_RETURN_V_MSG_E(error == ERR_NONE, error, "get Audio Route failed");
61 
62     int32_t mapSize = reply.ReadInt32();
63     if (mapSize > MAX_MAP_SIZE) {
64         MEDIA_LOG_E("The size of mapSize exceeds the maximum value");
65         return ERR_INVALID_OPERATION;
66     }
67     for (int32_t index = 0; index < mapSize; index++) {
68         shared_ptr<MonitorDeviceInfo> deviceInfo = std::make_shared<MonitorDeviceInfo>();
69         PreferredType preferredType = static_cast<PreferredType>(reply.ReadInt32());
70         deviceInfo->deviceType_ = reply.ReadInt32();
71         deviceInfo->deviceName_ = reply.ReadString();
72         deviceInfo->address_ = reply.ReadString();
73         deviceInfo->deviceCategory_ = reply.ReadInt32();
74         deviceInfo->usageOrSourceType_ = reply.ReadInt32();
75         preferredDevices.emplace(preferredType, deviceInfo);
76     }
77     return reply.ReadInt32();
78 }
79 
WriteAudioBuffer(const std::string & fileName,void * ptr,size_t size)80 int32_t MediaMonitorClient::WriteAudioBuffer(const std::string &fileName, void *ptr, size_t size)
81 {
82     std::shared_ptr<DumpBuffer> bufferPtr = nullptr;
83 
84     int32_t ret = GetInputBuffer(bufferPtr, size);
85     FALSE_RETURN_V_MSG_E(ret == SUCCESS, ERROR, "get buffer failed.");
86     FALSE_RETURN_V_MSG_E(bufferPtr != nullptr, ERROR, "get buffer is nullptr.");
87 
88     std::shared_ptr<DumpBufferWrap> tmpBufferWrap = dumpBufferWrap_;
89     FALSE_RETURN_V_MSG_E(tmpBufferWrap != nullptr, ERROR, "buffer wrap is nullptr.");
90 
91     int32_t bufferCapacitySize = tmpBufferWrap->GetCapacity(bufferPtr.get());
92     FALSE_RETURN_V_MSG_E(bufferCapacitySize > 0, ERROR, "get buffer capacity error");
93     int32_t writeSize = tmpBufferWrap->Write(bufferPtr.get(), static_cast<uint8_t*>(ptr), size);
94     FALSE_RETURN_V_MSG_E(writeSize > 0, ERROR, "write buffer error");
95 
96     uint64_t bufferId = tmpBufferWrap->GetUniqueId(bufferPtr.get());
97     ret = InputBufferFilled(fileName, bufferId, writeSize);
98     FALSE_RETURN_V_MSG_E(ret == SUCCESS, ERROR, "write buffer error %{public}d", ret);
99     return SUCCESS;
100 }
101 
GetInputBuffer(std::shared_ptr<DumpBuffer> & buffer,int32_t size)102 int32_t MediaMonitorClient::GetInputBuffer(std::shared_ptr<DumpBuffer> &buffer, int32_t size)
103 {
104     MessageParcel data;
105     MessageParcel reply;
106     MessageOption option;
107 
108     data.WriteInterfaceToken(GetDescriptor());
109     data.WriteInt32(size);
110     int32_t error = Remote()->SendRequest(
111         static_cast<uint32_t>(MediaMonitorInterfaceCode::GET_INPUT_BUFFER), data, reply, option);
112     FALSE_RETURN_V_MSG_E(error == ERR_NONE, error, "get pcm buffer failed %{public}d", error);
113 
114     std::shared_ptr<DumpBufferWrap> tmpBufferWrap = dumpBufferWrap_;
115     FALSE_RETURN_V_MSG_E(tmpBufferWrap != nullptr, ERROR, "buffer wrap is nullptr.");
116 
117     DumpBuffer *bufferPtr = tmpBufferWrap->NewDumpBuffer();
118     FALSE_RETURN_V_MSG_E(bufferPtr != nullptr, error, "new dump buffer error");
119 
120     buffer = std::shared_ptr<DumpBuffer>(bufferPtr, [tmpBufferWrap](DumpBuffer *ptr) {
121         if (tmpBufferWrap != nullptr && ptr != nullptr) {
122             tmpBufferWrap->DestroyDumpBuffer(ptr);
123         }
124     });
125 
126     void *replyPtr = reinterpret_cast<void *>(&reply);
127     if (tmpBufferWrap->ReadFromParcel(buffer.get(), replyPtr) == false) {
128         MEDIA_LOG_E("read data failed");
129         return reply.ReadInt32();
130     }
131     return reply.ReadInt32();
132 }
133 
InputBufferFilled(const std::string & fileName,uint64_t bufferId,int32_t size)134 int32_t MediaMonitorClient::InputBufferFilled(const std::string &fileName, uint64_t bufferId, int32_t size)
135 {
136     MessageParcel data;
137     MessageParcel reply;
138     MessageOption option;
139 
140     data.WriteInterfaceToken(GetDescriptor());
141     data.WriteString(fileName);
142     data.WriteUint64(bufferId);
143     data.WriteInt32(size);
144     int32_t error = Remote()->SendRequest(
145         static_cast<uint32_t>(MediaMonitorInterfaceCode::INPUT_BUFFER_FILL), data, reply, option);
146     FALSE_RETURN_V_MSG_E(error == ERR_NONE, error, "send request error %{public}d", error);
147     return reply.ReadInt32();
148 }
149 
SetMediaParameters(const std::string & dumpType,const std::string & dumpEnable)150 int32_t MediaMonitorClient::SetMediaParameters(const std::string &dumpType, const std::string &dumpEnable)
151 {
152     MessageParcel data;
153     MessageParcel reply;
154     MessageOption option;
155 
156     FALSE_RETURN_V_MSG_E(LoadDumpBufferWrap(dumpEnable) == SUCCESS, ERROR, "load buffer wrap error");
157 
158     data.WriteInterfaceToken(GetDescriptor());
159     data.WriteString(dumpType);
160     data.WriteString(dumpEnable);
161     int32_t error = Remote()->SendRequest(
162         static_cast<uint32_t>(MediaMonitorInterfaceCode::SET_MEDIA_PARAMS), data, reply, option);
163     FALSE_RETURN_V_MSG_E(error == ERR_NONE, error, "set media param error %{public}d", error);
164     return reply.ReadInt32();
165 }
166 
GetPcmDumpStatus(int32_t & dumpEnable)167 int32_t MediaMonitorClient::GetPcmDumpStatus(int32_t &dumpEnable)
168 {
169     MessageParcel data;
170     MessageParcel reply;
171     MessageOption option;
172 
173     data.WriteInterfaceToken(GetDescriptor());
174     int32_t error = Remote()->SendRequest(
175         static_cast<uint32_t>(MediaMonitorInterfaceCode::GET_DUMP_STATUS), data, reply, option);
176     FALSE_RETURN_V_MSG_E(error == ERR_NONE, error, "get media param error %{public}d", error);
177     dumpEnable = reply.ReadInt32();
178     MEDIA_LOG_I("get media param %d", dumpEnable);
179     return reply.ReadInt32();
180 }
181 
ErasePreferredDeviceByType(const PreferredType preferredType)182 int32_t MediaMonitorClient::ErasePreferredDeviceByType(const PreferredType preferredType)
183 {
184     MessageParcel data;
185     MessageParcel reply;
186     MessageOption option;
187 
188     data.WriteInterfaceToken(GetDescriptor());
189     data.WriteInt32(static_cast<int32_t>(preferredType));
190     int32_t error = Remote()->SendRequest(
191         static_cast<uint32_t>(MediaMonitorInterfaceCode::ERASE_PREFERRED_DEVICE), data, reply, option);
192     FALSE_RETURN_V_MSG_E(error == ERR_NONE, error, "erase preferred device error %{public}d", error);
193     return reply.ReadInt32();
194 }
195 
LoadDumpBufferWrap(const std::string & dumpEnable)196 int32_t MediaMonitorClient::LoadDumpBufferWrap(const std::string &dumpEnable)
197 {
198     bool flag = (dumpEnable == "true") ? true : false;
199     if (flag && dumpBufferWrap_ != nullptr) {
200         return SUCCESS;
201     }
202 
203     if (flag) {
204         dumpBufferWrap_ = std::make_shared<DumpBufferWrap>();
205         bool ret = dumpBufferWrap_->Open();
206         if (!ret) {
207             MEDIA_LOG_E("load dumpbuffer failed");
208             dumpBufferWrap_ = nullptr;
209             return ERROR;
210         }
211     } else {
212         dumpBufferWrap_ = nullptr;
213     }
214     return SUCCESS;
215 }
216 
GetAudioExcludedDevicesMsg(std::map<AudioDeviceUsage,std::vector<std::shared_ptr<MonitorDeviceInfo>>> & excludedDevices)217 int32_t MediaMonitorClient::GetAudioExcludedDevicesMsg(std::map<AudioDeviceUsage,
218     std::vector<std::shared_ptr<MonitorDeviceInfo>>> &excludedDevices)
219 {
220     MessageParcel data;
221     MessageParcel reply;
222     MessageOption option;
223 
224     data.WriteInterfaceToken(GetDescriptor());
225     int32_t error = Remote()->SendRequest(
226         static_cast<uint32_t>(MediaMonitorInterfaceCode::GET_EXCLUDED_DEVICES_MSG), data, reply, option);
227     FALSE_RETURN_V_MSG_E(error == ERR_NONE, error, "get excluded devices failed");
228 
229     int32_t mapSize = reply.ReadInt32();
230     if (mapSize > MAX_MAP_SIZE) {
231         MEDIA_LOG_E("The size of mapSize exceeds the maximum value");
232         return ERR_INVALID_OPERATION;
233     }
234     for (int32_t index = 0; index < mapSize; index++) {
235         AudioDeviceUsage deviceUsage = static_cast<AudioDeviceUsage>(reply.ReadInt32());
236         int32_t size = reply.ReadInt32();
237         FALSE_RETURN_V_MSG_E(size >= 0 && size <= MAX_MAP_SIZE, ERR_INVALID_OPERATION,
238             "The size of vector is invalid");
239         std::vector<std::shared_ptr<MonitorDeviceInfo>> deviceInfoVec;
240         for (int32_t i = 0; i < size; i++) {
241             shared_ptr<MonitorDeviceInfo> deviceInfo = make_shared<MonitorDeviceInfo>();
242             deviceInfo->deviceType_ = reply.ReadInt32();
243             deviceInfo->deviceCategory_ = reply.ReadInt32();
244             deviceInfo->usageOrSourceType_ = reply.ReadInt32();
245             deviceInfo->audioDeviceUsage_ = reply.ReadInt32();
246             deviceInfo->deviceName_ = reply.ReadString();
247             deviceInfo->address_ = reply.ReadString();
248             deviceInfo->networkId_ = reply.ReadString();
249             deviceInfoVec.push_back(deviceInfo);
250         }
251         excludedDevices.emplace(deviceUsage, deviceInfoVec);
252     }
253     return reply.ReadInt32();
254 }
255 } // namespace MediaMonitor
256 } // namespace Media
257 } // namespace OHOS