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_stub.h"
17 #include "media_monitor_ipc_interface_code.h"
18 #include "log.h"
19 #include "monitor_error.h"
20
21 namespace {
22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_FOUNDATION, "MediaMonitorStub"};
23 }
24
25 namespace OHOS {
26 namespace Media {
27 namespace MediaMonitor {
28
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)29 int32_t MediaMonitorStub::OnRemoteRequest(
30 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
31 {
32 FALSE_RETURN_V_MSG_E(data.ReadInterfaceToken() == GetDescriptor(), -1, "ReadInterfaceToken failed");
33 if (code <= static_cast<uint32_t>(MediaMonitorInterfaceCode ::MEDIA_MONITOR_CODE_MAX)) {
34 (this->*handlers[code])(data, reply);
35 return MediaMonitorErr::SUCCESS;
36 }
37 MEDIA_LOG_E("default case, need check MediaMonitorStub");
38 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
39 }
40
HandleWriteLogMsg(MessageParcel & data,MessageParcel & reply)41 void MediaMonitorStub::HandleWriteLogMsg(MessageParcel &data, MessageParcel &reply)
42 {
43 std::shared_ptr<EventBean> eventBean = std::make_shared<EventBean>();
44 eventBean->ReadFromParcel(data);
45 WriteLogMsg(eventBean);
46 }
47
HandleGetAudioRouteMsg(MessageParcel & data,MessageParcel & reply)48 void MediaMonitorStub::HandleGetAudioRouteMsg(MessageParcel &data, MessageParcel &reply)
49 {
50 MEDIA_LOG_I("HandleGetAudioRouteMsg in");
51 std::map<PreferredType, std::shared_ptr<MonitorDeviceInfo>> preferredDevices;
52
53 int32_t ret = GetAudioRouteMsg(preferredDevices);
54
55 int32_t mapSize = static_cast<int32_t>(preferredDevices.size());
56 reply.WriteInt32(mapSize);
57 for (auto &it : preferredDevices) {
58 reply.WriteInt32(static_cast<int32_t>(it.first));
59 reply.WriteInt32(it.second->deviceType_);
60 reply.WriteString(it.second->deviceName_);
61 reply.WriteString(it.second->address_);
62 reply.WriteInt32(it.second->deviceCategory_);
63 reply.WriteInt32(it.second->usageOrSourceType_);
64 }
65 reply.WriteInt32(ret);
66 MEDIA_LOG_I("MediaMonitorStub::HandleGetAudioRouteMsg out");
67 }
68
HandleGetAudioExcludedDevicesMsg(MessageParcel & data,MessageParcel & reply)69 void MediaMonitorStub::HandleGetAudioExcludedDevicesMsg(MessageParcel &data, MessageParcel &reply)
70 {
71 MEDIA_LOG_I("HandleGetAudioExcludedDevicesMsg in");
72 std::map<AudioDeviceUsage, std::vector<std::shared_ptr<MonitorDeviceInfo>>> excludedDevices;
73 int32_t ret = GetAudioExcludedDevicesMsg(excludedDevices);
74 int32_t mapSize = static_cast<int32_t>(excludedDevices.size());
75 reply.WriteInt32(mapSize);
76 for (auto &it : excludedDevices) {
77 reply.WriteInt32(static_cast<int32_t>(it.first));
78 reply.WriteInt32(static_cast<int32_t>(it.second.size()));
79 for (auto &device : it.second) {
80 reply.WriteInt32(device->deviceType_);
81 reply.WriteInt32(device->deviceCategory_);
82 reply.WriteInt32(device->usageOrSourceType_);
83 reply.WriteInt32(device->audioDeviceUsage_);
84 reply.WriteString(device->deviceName_);
85 reply.WriteString(device->address_);
86 reply.WriteString(device->networkId_);
87 }
88 }
89 reply.WriteInt32(ret);
90 MEDIA_LOG_I("HandleGetAudioExcludedDevicesMsg out");
91 }
92
HandleSetMediaParams(MessageParcel & data,MessageParcel & reply)93 void MediaMonitorStub::HandleSetMediaParams(MessageParcel &data, MessageParcel &reply)
94 {
95 std::string dumpType;
96 std::string dumpEnable;
97 if (data.ReadString(dumpType) == false) {
98 MEDIA_LOG_E("MediaMonitorStub::HandleSetMediaParams read type failed");
99 reply.WriteInt32(ERR_INVALID_PARAM);
100 return;
101 }
102 if (data.ReadString(dumpEnable) == false) {
103 MEDIA_LOG_E("MediaMonitorStub::HandleSetMediaParams read type failed");
104 reply.WriteInt32(ERR_INVALID_PARAM);
105 return;
106 }
107 int32_t ret = SetMediaParameters(dumpType, dumpEnable);
108 reply.WriteInt32(ret);
109 }
110
HandleGetInputBuffer(MessageParcel & data,MessageParcel & reply)111 void MediaMonitorStub::HandleGetInputBuffer(MessageParcel &data, MessageParcel &reply)
112 {
113 int32_t size;
114 std::shared_ptr<DumpBuffer> buffer = nullptr;
115 if (data.ReadInt32(size) == false) {
116 MEDIA_LOG_E("MediaMonitorStub::HandleGetInputBuffer get size failed");
117 reply.WriteInt32(ERR_INVALID_PARAM);
118 return;
119 }
120 int32_t ret = GetInputBuffer(buffer, size);
121 if (buffer == nullptr) {
122 MEDIA_LOG_E("MediaMonitorStub::HandleGetInputBuffer failed");
123 reply.WriteInt32(ret);
124 return;
125 }
126 if (dumpBufferWrap_ == nullptr) {
127 reply.WriteInt32(ERR_OPERATION_FAILED);
128 return;
129 }
130 void *replyPtr = reinterpret_cast<void *>(&reply);
131 if (dumpBufferWrap_->WriteToParcel(buffer.get(), replyPtr) == false) {
132 MEDIA_LOG_E("MediaMonitorStub::HandleGetInputBuffer write data failed");
133 reply.WriteInt32(ERR_OPERATION_FAILED);
134 return;
135 }
136 reply.WriteInt32(ret);
137 }
138
HandleInputBufferFilled(MessageParcel & data,MessageParcel & reply)139 void MediaMonitorStub::HandleInputBufferFilled(MessageParcel &data, MessageParcel &reply)
140 {
141 std::string fileName;
142 uint64_t bufferId = 0;
143 int32_t size = 0;
144 if (data.ReadString(fileName) == false) {
145 MEDIA_LOG_E("MediaMonitorStub::HandleInputBufferFilled read name failed");
146 reply.WriteInt32(ERR_INVALID_PARAM);
147 return;
148 }
149 if (data.ReadUint64(bufferId) == false) {
150 MEDIA_LOG_E("MediaMonitorStub::HandleInputBufferFilled read id failed");
151 reply.WriteInt32(ERR_INVALID_PARAM);
152 return;
153 }
154
155 if (data.ReadInt32(size) == false) {
156 MEDIA_LOG_E("MediaMonitorStub::HandleInputBufferFilled read size failed");
157 reply.WriteInt32(ERR_INVALID_PARAM);
158 return;
159 }
160 int32_t ret = InputBufferFilled(fileName, bufferId, size);
161 reply.WriteInt32(ret);
162 }
163
HandleGetPcmDumpStatus(MessageParcel & data,MessageParcel & reply)164 void MediaMonitorStub::HandleGetPcmDumpStatus(MessageParcel &data, MessageParcel &reply)
165 {
166 int32_t status = 0;
167 int32_t ret = GetPcmDumpStatus(status);
168 reply.WriteInt32(status);
169 reply.WriteInt32(ret);
170 }
171
HandleErasePreferredDeviceByType(MessageParcel & data,MessageParcel & reply)172 void MediaMonitorStub::HandleErasePreferredDeviceByType(MessageParcel &data, MessageParcel &reply)
173 {
174 int32_t preferredType;
175 if (data.ReadInt32(preferredType) == false) {
176 MEDIA_LOG_E("MediaMonitorStub::HandleErasePreferredDeviceByType read type failed");
177 reply.WriteInt32(ERR_INVALID_PARAM);
178 return;
179 }
180 int32_t ret = ErasePreferredDeviceByType(static_cast<PreferredType>(preferredType));
181 reply.WriteInt32(ret);
182 }
183 } // namespace MediaMonitor
184 } // namespace Media
185 } // namespace OHOS