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<PerferredType,std::shared_ptr<MonitorDeviceInfo>> & perferredDevices)50 int32_t MediaMonitorClient::GetAudioRouteMsg(std::map<PerferredType,
51 std::shared_ptr<MonitorDeviceInfo>> &perferredDevices)
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 PerferredType perferredType = static_cast<PerferredType>(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 perferredDevices.emplace(perferredType, 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 FALSE_RETURN_V_MSG_E(dumpBufferWrap_ != nullptr, ERROR, "dump buffer wrap error");
84
85 int32_t ret = GetInputBuffer(bufferPtr, size);
86 FALSE_RETURN_V_MSG_E(ret == SUCCESS, ERROR, "get buffer failed.");
87 FALSE_RETURN_V_MSG_E(bufferPtr != nullptr, ERROR, "get buffer is nullptr.");
88
89 int32_t bufferCapacitySize = dumpBufferWrap_->GetCapacity(bufferPtr.get());
90 FALSE_RETURN_V_MSG_E(bufferCapacitySize > 0, ERROR, "get buffer capacity error");
91 int32_t writeSize = dumpBufferWrap_->Write(bufferPtr.get(), static_cast<uint8_t*>(ptr), size);
92 FALSE_RETURN_V_MSG_E(writeSize > 0, ERROR, "write buffer error");
93
94 uint64_t bufferId = dumpBufferWrap_->GetUniqueId(bufferPtr.get());
95 ret = InputBufferFilled(fileName, bufferId, writeSize);
96 FALSE_RETURN_V_MSG_E(ret == SUCCESS, ERROR, "write buffer error %{public}d", ret);
97 return SUCCESS;
98 }
99
GetInputBuffer(std::shared_ptr<DumpBuffer> & buffer,int32_t size)100 int32_t MediaMonitorClient::GetInputBuffer(std::shared_ptr<DumpBuffer> &buffer, int32_t size)
101 {
102 MessageParcel data;
103 MessageParcel reply;
104 MessageOption option;
105
106 data.WriteInterfaceToken(GetDescriptor());
107 data.WriteInt32(size);
108 int32_t error = Remote()->SendRequest(
109 static_cast<uint32_t>(MediaMonitorInterfaceCode::GET_INPUT_BUFFER), data, reply, option);
110 FALSE_RETURN_V_MSG_E(error == ERR_NONE, error, "get pcm buffer failed %{public}d", error);
111 FALSE_RETURN_V_MSG_E(dumpBufferWrap_ != nullptr, error, "dump buffer error");
112
113 DumpBuffer *bufferPtr = dumpBufferWrap_->NewDumpBuffer();
114 FALSE_RETURN_V_MSG_E(bufferPtr != nullptr, error, "new dump buffer error");
115
116 buffer = std::shared_ptr<DumpBuffer>(bufferPtr, [this](DumpBuffer *ptr) {
117 dumpBufferWrap_->DestroyDumpBuffer(ptr);
118 });
119 void *replyPtr = reinterpret_cast<void *>(&reply);
120 if (dumpBufferWrap_->ReadFromParcel(buffer.get(), replyPtr) == false) {
121 MEDIA_LOG_E("read data failed");
122 return reply.ReadInt32();
123 }
124 return reply.ReadInt32();
125 }
126
InputBufferFilled(const std::string & fileName,uint64_t bufferId,int32_t size)127 int32_t MediaMonitorClient::InputBufferFilled(const std::string &fileName, uint64_t bufferId, int32_t size)
128 {
129 MessageParcel data;
130 MessageParcel reply;
131 MessageOption option;
132
133 data.WriteInterfaceToken(GetDescriptor());
134 data.WriteString(fileName);
135 data.WriteUint64(bufferId);
136 data.WriteInt32(size);
137 int32_t error = Remote()->SendRequest(
138 static_cast<uint32_t>(MediaMonitorInterfaceCode::INPUT_BUFFER_FILL), data, reply, option);
139 FALSE_RETURN_V_MSG_E(error == ERR_NONE, error, "send request error %{public}d", error);
140 return reply.ReadInt32();
141 }
142
SetMediaParameters(const std::string & dumpType,const std::string & dumpEnable)143 int32_t MediaMonitorClient::SetMediaParameters(const std::string &dumpType, const std::string &dumpEnable)
144 {
145 MessageParcel data;
146 MessageParcel reply;
147 MessageOption option;
148
149 if (dumpEnable == "true") {
150 dumpBufferWrap_ = std::make_shared<DumpBufferWrap>();
151 bool ret = dumpBufferWrap_->Open();
152 if (!ret) {
153 MEDIA_LOG_E("load dumpbuffer failed");
154 dumpBufferWrap_ = nullptr;
155 return ERROR;
156 }
157 } else {
158 dumpBufferWrap_ = nullptr;
159 }
160
161 data.WriteInterfaceToken(GetDescriptor());
162 data.WriteString(dumpType);
163 data.WriteString(dumpEnable);
164 int32_t error = Remote()->SendRequest(
165 static_cast<uint32_t>(MediaMonitorInterfaceCode::SET_MEDIA_PARAMS), data, reply, option);
166 FALSE_RETURN_V_MSG_E(error == ERR_NONE, error, "set media param error %{public}d", error);
167 return reply.ReadInt32();
168 }
169
ErasePreferredDeviceByType(const PerferredType preferredType)170 int32_t MediaMonitorClient::ErasePreferredDeviceByType(const PerferredType preferredType)
171 {
172 MessageParcel data;
173 MessageParcel reply;
174 MessageOption option;
175
176 data.WriteInterfaceToken(GetDescriptor());
177 data.WriteInt32(static_cast<int32_t>(preferredType));
178 int32_t error = Remote()->SendRequest(
179 static_cast<uint32_t>(MediaMonitorInterfaceCode::ERASE_PREFERRED_DEVICE), data, reply, option);
180 FALSE_RETURN_V_MSG_E(error == ERR_NONE, error, "erase preferred device error %{public}d", error);
181 return reply.ReadInt32();
182 }
183 } // namespace MediaMonitor
184 } // namespace Media
185 } // namespace OHOS