1 /*
2 * Copyright (c) 2022 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 "avsession_service_stub.h"
17 #include "avsession_log.h"
18 #include "avsession_errors.h"
19 #include "session_listener_proxy.h"
20 #include "client_death_proxy.h"
21 #include "avsession_trace.h"
22 #include "avsession_sysevent.h"
23
24 using namespace OHOS::AudioStandard;
25 namespace OHOS::AVSession {
CheckInterfaceToken(MessageParcel & data)26 bool AVSessionServiceStub::CheckInterfaceToken(MessageParcel& data)
27 {
28 auto localDescriptor = IAVSessionService::GetDescriptor();
29 auto remoteDescriptor = data.ReadInterfaceToken();
30 if (remoteDescriptor != localDescriptor) {
31 SLOGI("interface token is not equal");
32 return false;
33 }
34 return true;
35 }
36
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)37 int32_t AVSessionServiceStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
38 MessageOption& option)
39 {
40 if (!CheckInterfaceToken(data)) {
41 return AVSESSION_ERROR;
42 }
43 if (code < SERVICE_CMD_MAX) {
44 return (this->*handlers[code])(data, reply);
45 }
46 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
47 }
48
HandleCreateSessionInner(MessageParcel & data,MessageParcel & reply)49 int32_t AVSessionServiceStub::HandleCreateSessionInner(MessageParcel& data, MessageParcel& reply)
50 {
51 AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::CreateSessionInner");
52 auto sessionTag = data.ReadString();
53 auto sessionType = data.ReadInt32();
54 sptr elementName = data.ReadParcelable<AppExecFwk::ElementName>();
55 if (elementName == nullptr) {
56 SLOGI("read element name failed");
57 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
58 return ERR_NONE;
59 }
60 auto session = CreateSessionInner(sessionTag, sessionType, *elementName);
61 if (session == nullptr) {
62 SLOGI("session is nullptr");
63 reply.WriteInt32(ERR_UNMARSHALLING);
64 return ERR_NONE;
65 }
66 reply.WriteInt32(AVSESSION_SUCCESS);
67 reply.WriteRemoteObject(session);
68 return ERR_NONE;
69 }
70
HandleGetAllSessionDescriptors(MessageParcel & data,MessageParcel & reply)71 int32_t AVSessionServiceStub::HandleGetAllSessionDescriptors(MessageParcel& data, MessageParcel& reply)
72 {
73 std::vector<AVSessionDescriptor> descriptors;
74 int32_t ret = GetAllSessionDescriptors(descriptors);
75 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
76 CHECK_AND_RETURN_RET_LOG(reply.WriteUint32(descriptors.size()), ERR_NONE, "write size failed");
77 for (const auto& descriptor : descriptors) {
78 if (!descriptor.WriteToParcel(reply)) {
79 SLOGI("write descriptor failed");
80 break;
81 }
82 }
83 return ERR_NONE;
84 }
85
HandleGetSessionDescriptorsById(MessageParcel & data,MessageParcel & reply)86 int32_t AVSessionServiceStub::HandleGetSessionDescriptorsById(MessageParcel& data, MessageParcel& reply)
87 {
88 AVSessionDescriptor descriptor;
89 int32_t ret = GetSessionDescriptorsBySessionId(data.ReadString(), descriptor);
90 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
91 if (ret == AVSESSION_SUCCESS) {
92 CHECK_AND_PRINT_LOG(descriptor.WriteToParcel(reply), "write AVSessionDescriptor failed");
93 }
94 return ERR_NONE;
95 }
96
HandleCreateControllerInner(MessageParcel & data,MessageParcel & reply)97 int32_t AVSessionServiceStub::HandleCreateControllerInner(MessageParcel& data, MessageParcel& reply)
98 {
99 AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::CreateControllerInner");
100 sptr<IRemoteObject> object;
101 int32_t ret = CreateControllerInner(data.ReadString(), object);
102 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
103 if (ret == AVSESSION_SUCCESS) {
104 CHECK_AND_PRINT_LOG(reply.WriteRemoteObject(object), "write object failed");
105 }
106 return ERR_NONE;
107 }
108
HandleRegisterSessionListener(MessageParcel & data,MessageParcel & reply)109 int32_t AVSessionServiceStub::HandleRegisterSessionListener(MessageParcel& data, MessageParcel& reply)
110 {
111 auto remoteObject = data.ReadRemoteObject();
112 if (remoteObject == nullptr) {
113 SLOGI("read remote object failed");
114 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
115 return ERR_NONE;
116 }
117 auto listener = iface_cast<SessionListenerProxy>(remoteObject);
118 if (listener == nullptr) {
119 SLOGI("iface_cast remote object failed");
120 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_INVALID_PARAM), ERR_NONE, "write int32 failed");
121 return ERR_NONE;
122 }
123 if (!reply.WriteInt32(RegisterSessionListener(listener))) {
124 SLOGI("reply write int32 failed");
125 }
126 return ERR_NONE;
127 }
128
HandleSendSystemAVKeyEvent(MessageParcel & data,MessageParcel & reply)129 int32_t AVSessionServiceStub::HandleSendSystemAVKeyEvent(MessageParcel& data, MessageParcel& reply)
130 {
131 AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::SendSystemAVKeyEvent");
132 auto keyEvent = MMI::KeyEvent::Create();
133 if (keyEvent == nullptr) {
134 SLOGI("create keyEvent failed");
135 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_NO_MEMORY), ERR_NONE, "write int32 failed");
136 return ERR_NONE;
137 }
138 if (!keyEvent->ReadFromParcel(data)) {
139 SLOGI("read keyEvent failed");
140 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
141 return ERR_NONE;
142 }
143 if (!keyEvent->IsValid()) {
144 SLOGI("keyEvent is not valid");
145 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_INVALID_PARAM), ERR_NONE, "write int32 failed");
146 return ERR_NONE;
147 }
148 if (!reply.WriteInt32(SendSystemAVKeyEvent(*keyEvent))) {
149 SLOGI("reply write int32 failed");
150 }
151 return ERR_NONE;
152 }
153
HandleSendSystemControlCommand(MessageParcel & data,MessageParcel & reply)154 int32_t AVSessionServiceStub::HandleSendSystemControlCommand(MessageParcel& data, MessageParcel& reply)
155 {
156 AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::SendSystemControlCommand");
157 sptr command = data.ReadParcelable<AVControlCommand>();
158 if (command == nullptr) {
159 SLOGI("read command failed");
160 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
161 HISYSEVENT_FAULT("CONTROL_COMMAND_FAILED", "ERROR_TYPE", "READ_PARCELABLE_FAILED",
162 "ERROR_INFO", "handle send system control command read command failed");
163 return ERR_NONE;
164 }
165 if (!reply.WriteInt32(SendSystemControlCommand(*command))) {
166 SLOGI("reply write int32 failed");
167 }
168 return ERR_NONE;
169 }
170
HandleRegisterClientDeathObserver(MessageParcel & data,MessageParcel & reply)171 int32_t AVSessionServiceStub::HandleRegisterClientDeathObserver(MessageParcel& data, MessageParcel& reply)
172 {
173 auto remoteObject = data.ReadRemoteObject();
174 if (remoteObject == nullptr) {
175 SLOGI("read remote object failed");
176 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
177 return ERR_NONE;
178 }
179 auto clientDeathObserver = iface_cast<ClientDeathProxy>(remoteObject);
180 if (clientDeathObserver == nullptr) {
181 SLOGI("iface_cast remote object failed");
182 reply.WriteInt32(ERR_INVALID_PARAM);
183 return ERR_NONE;
184 }
185 int32_t ret = RegisterClientDeathObserver(clientDeathObserver);
186 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "register clientDeathObserver failed");
187 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
188 return ERR_NONE;
189 }
190
HandleCastAudio(MessageParcel & data,MessageParcel & reply)191 int32_t AVSessionServiceStub::HandleCastAudio(MessageParcel& data, MessageParcel& reply)
192 {
193 AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::CastAudio");
194 SLOGI("start");
195 SessionToken token {};
196 token.sessionId = data.ReadString();
197 token.pid = data.ReadInt32();
198 token.uid = data.ReadInt32();
199 int32_t deviceNum = data.ReadInt32();
200 if (deviceNum > RECEIVE_DEVICE_NUM_MAX) {
201 SLOGI("receive deviceNum over range");
202 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_INVALID_PARAM), ERR_NONE, "write int32 failed");
203 return ERR_NONE;
204 }
205
206 std::vector<AudioDeviceDescriptor> sinkAudioDescriptors;
207 for (int i = 0; i < deviceNum; i++) {
208 auto audioDeviceDescriptor = AudioDeviceDescriptor::Unmarshalling(data);
209 if (audioDeviceDescriptor == nullptr) {
210 SLOGI("read AudioDeviceDescriptor failed");
211 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32 failed");
212 return ERR_NONE;
213 }
214 SLOGI("networkId_: %{public}.6s, role %{public}d", (*audioDeviceDescriptor).networkId_.c_str(),
215 static_cast<int32_t>((*audioDeviceDescriptor).deviceRole_));
216 sinkAudioDescriptors.push_back(*audioDeviceDescriptor);
217 }
218 int32_t ret = CastAudio(token, sinkAudioDescriptors);
219 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "CastAudio failed");
220 SLOGI("CastAudio ret %{public}d", ret);
221 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
222 SLOGI("success");
223 return ERR_NONE;
224 }
225
HandleCastAudioForAll(MessageParcel & data,MessageParcel & reply)226 int32_t AVSessionServiceStub::HandleCastAudioForAll(MessageParcel& data, MessageParcel& reply)
227 {
228 AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::CastAudioForAll");
229 SLOGI("start");
230 int32_t deviceNum = data.ReadInt32();
231 if (deviceNum > RECEIVE_DEVICE_NUM_MAX) {
232 SLOGI("receive deviceNum over range");
233 reply.WriteInt32(ERR_INVALID_PARAM);
234 return ERR_NONE;
235 }
236
237 std::vector<AudioDeviceDescriptor> sinkAudioDescriptors {};
238 for (int i = 0; i < deviceNum; i++) {
239 auto audioDeviceDescriptor = AudioDeviceDescriptor::Unmarshalling(data);
240 if (audioDeviceDescriptor == nullptr) {
241 SLOGI("read AudioDeviceDescriptor failed");
242 reply.WriteInt32(ERR_UNMARSHALLING);
243 return ERR_NONE;
244 }
245 SLOGI("networkId_: %{public}.6s, role %{public}d", (*audioDeviceDescriptor).networkId_.c_str(),
246 static_cast<int32_t>((*audioDeviceDescriptor).deviceRole_));
247 sinkAudioDescriptors.push_back(*audioDeviceDescriptor);
248 }
249 int32_t ret = CastAudioForAll(sinkAudioDescriptors);
250 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "CastAudioForAll failed");
251 SLOGI("CastAudioForAll ret %{public}d", ret);
252 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
253 return ERR_NONE;
254 }
255
HandleRemoteCastAudio(MessageParcel & data,MessageParcel & reply)256 int32_t AVSessionServiceStub::HandleRemoteCastAudio(MessageParcel& data, MessageParcel& reply)
257 {
258 AVSESSION_TRACE_SYNC_START("AVSessionServiceStub::RemoteCastAudio");
259 SLOGI("start");
260 auto command = static_cast<RemoteServiceCommand>(data.ReadInt32());
261 std::string sessionInfo = data.ReadString();
262 std::string output;
263 int32_t ret = ProcessCastAudioCommand(command, sessionInfo, output);
264 SLOGI("RemoteCastAudio ret %{public}d", ret);
265 if (ret != AVSESSION_SUCCESS) {
266 SLOGI("RemoteCastAudio failed");
267 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
268 return ERR_NONE;
269 }
270 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
271 CHECK_AND_RETURN_RET_LOG(reply.WriteString(output), ERR_NONE, "write int32 failed");
272 return ERR_NONE;
273 }
274 } // namespace OHOS::AVSession