1 /*
2 * Copyright (c) 2023-2025 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 "avcast_controller_stub.h"
17 #include "avsession_errors.h"
18 #include "avsession_log.h"
19 #include "avsession_trace.h"
20 #include "ipc_skeleton.h"
21 #include "media_info_holder.h"
22 #include "surface_utils.h"
23 #include "session_xcollie.h"
24 #include "permission_checker.h"
25
26 namespace OHOS::AVSession {
CheckInterfaceToken(MessageParcel & data)27 bool AVCastControllerStub::CheckInterfaceToken(MessageParcel& data)
28 {
29 auto localDescriptor = IAVCastController::GetDescriptor();
30 auto remoteDescriptor = data.ReadInterfaceToken();
31 if (remoteDescriptor != localDescriptor) {
32 SLOGI("interface token is not equal");
33 return false;
34 }
35 return true;
36 }
37
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)38 int32_t AVCastControllerStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
39 MessageOption &option)
40 {
41 CHECK_AND_RETURN_RET_LOG(IPCSkeleton::IsLocalCalling(), AVSESSION_ERROR, "forbid rpc remote request");
42 if (code >= static_cast<uint32_t>(IAVCastController::CAST_CONTROLLER_CMD_SEND_CONTROL_COMMAND)
43 && code < static_cast<uint32_t>(IAVCastController::CAST_CONTROLLER_CMD_MAX)) {
44 SessionXCollie sessionXCollie(mapCodeToFuncNameXCollie[code]);
45 }
46 if (!CheckInterfaceToken(data)) {
47 return AVSESSION_ERROR;
48 }
49 if (code >= static_cast<uint32_t>(IAVCastController::CAST_CONTROLLER_CMD_SEND_CONTROL_COMMAND)
50 && code < static_cast<uint32_t>(IAVCastController::CAST_CONTROLLER_CMD_MAX)) {
51 return handlers[code](data, reply);
52 }
53 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
54 }
55
HandleRegisterCallbackInner(MessageParcel & data,MessageParcel & reply)56 int32_t AVCastControllerStub::HandleRegisterCallbackInner(MessageParcel& data, MessageParcel& reply)
57 {
58 auto remoteObject = data.ReadRemoteObject();
59 if (remoteObject == nullptr) {
60 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write RegisterCallback ret failed");
61 } else {
62 CHECK_AND_PRINT_LOG(reply.WriteInt32(RegisterCallbackInner(remoteObject)),
63 "write RegisterCallback ret failed");
64 }
65 return ERR_NONE;
66 }
67
HandleSendControlCommand(MessageParcel & data,MessageParcel & reply)68 int32_t AVCastControllerStub::HandleSendControlCommand(MessageParcel& data, MessageParcel& reply)
69 {
70 AVSESSION_TRACE_SYNC_START("AVCastControllerStub::SendControlCommand");
71 sptr<AVCastControlCommand> cmd = data.ReadParcelable<AVCastControlCommand>();
72 if (cmd == nullptr) {
73 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING),
74 ERR_UNMARSHALLING, "write SendCommand ret failed");
75 } else {
76 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(SendControlCommand(*cmd)),
77 ERR_UNMARSHALLING, "write SendCommand ret failed");
78 }
79 return ERR_NONE;
80 }
81
HandleSendCustomData(MessageParcel & data,MessageParcel & reply)82 int32_t AVCastControllerStub::HandleSendCustomData(MessageParcel& data, MessageParcel& reply)
83 {
84 AVSESSION_TRACE_SYNC_START("AVCastControllerStub::SendCustomData");
85 sptr customData = data.ReadParcelable<AAFwk::WantParams>();
86 if (customData == nullptr) {
87 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING),
88 ERR_UNMARSHALLING, "write SendCustomData ret failed");
89 } else {
90 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(SendCustomData(*customData)),
91 ERR_UNMARSHALLING, "write SendCustomData ret failed");
92 }
93 return ERR_NONE;
94 }
95
HandleStart(MessageParcel & data,MessageParcel & reply)96 int32_t AVCastControllerStub::HandleStart(MessageParcel& data, MessageParcel& reply)
97 {
98 sptr<AVQueueItem> avQueueItem = data.ReadParcelable<AVQueueItem>();
99 AVFileDescriptor avFileDescriptor;
100 avFileDescriptor.fd_ = data.ReadFileDescriptor();
101 if (avQueueItem == nullptr) {
102 close(avFileDescriptor.fd_);
103 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write Start ret failed");
104 } else {
105 avQueueItem->GetDescription()->SetFdSrc(avFileDescriptor);
106 close(avFileDescriptor.fd_);
107 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(Start(*avQueueItem)),
108 ERR_NONE, "Write mediaInfoHolder failed");
109 }
110 return ERR_NONE;
111 }
112
HandlePrepare(MessageParcel & data,MessageParcel & reply)113 int32_t AVCastControllerStub::HandlePrepare(MessageParcel& data, MessageParcel& reply)
114 {
115 sptr<AVQueueItem> avQueueItem = data.ReadParcelable<AVQueueItem>();
116 if (avQueueItem == nullptr) {
117 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write prepare ret failed");
118 } else {
119 if (data.ReadBool()) {
120 SLOGD("Need get fd from proxy");
121 AVFileDescriptor avFileDescriptor;
122 avFileDescriptor.fd_ = data.ReadFileDescriptor();
123 SLOGD("Prepare received fd %{public}d", avFileDescriptor.fd_);
124 avQueueItem->GetDescription()->SetFdSrc(avFileDescriptor);
125 close(avFileDescriptor.fd_);
126 }
127 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(Prepare(*avQueueItem)),
128 ERR_NONE, "Write mediaInfoHolder failed");
129 }
130 return ERR_NONE;
131 }
132
HandleGetDuration(MessageParcel & data,MessageParcel & reply)133 int32_t AVCastControllerStub::HandleGetDuration(MessageParcel& data, MessageParcel& reply)
134 {
135 int32_t duration = 0;
136 int32_t ret = GetDuration(duration);
137 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_UNMARSHALLING, "write int32 failed");
138 if (ret == AVSESSION_SUCCESS) {
139 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(duration), ERR_UNMARSHALLING, "write int32 failed");
140 }
141 return ERR_NONE;
142 }
143
HandleGetCastAVPlayBackState(MessageParcel & data,MessageParcel & reply)144 int32_t AVCastControllerStub::HandleGetCastAVPlayBackState(MessageParcel& data, MessageParcel& reply)
145 {
146 AVPlaybackState state;
147 int32_t ret = GetCastAVPlaybackState(state);
148 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
149 if (ret == AVSESSION_SUCCESS) {
150 CHECK_AND_PRINT_LOG(reply.WriteParcelable(&state), "write CastAVPlaybackState failed");
151 }
152 return ERR_NONE;
153 }
154
HandleGetSupportedDecoders(MessageParcel & data,MessageParcel & reply)155 int32_t AVCastControllerStub::HandleGetSupportedDecoders(MessageParcel& data, MessageParcel& reply)
156 {
157 std::vector<std::string> decoderTypes;
158 int32_t ret = GetSupportedDecoders(decoderTypes);
159 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
160 if (ret == AVSESSION_SUCCESS) {
161 CHECK_AND_PRINT_LOG(reply.WriteStringVector(decoderTypes), "write GetSupportedDecoders failed");
162 }
163 return ERR_NONE;
164 }
165
HandleGetRecommendedResolutionLevel(MessageParcel & data,MessageParcel & reply)166 int32_t AVCastControllerStub::HandleGetRecommendedResolutionLevel(MessageParcel& data, MessageParcel& reply)
167 {
168 std::string decoderType = data.ReadString();
169 CHECK_AND_RETURN_RET_LOG(!decoderType.empty(), ERR_NONE, "decoderType is empty");
170 ResolutionLevel resolutionLevel = ResolutionLevel::RESOLUTION_480P;
171 int32_t ret = GetRecommendedResolutionLevel(decoderType, resolutionLevel);
172 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
173 if (ret == AVSESSION_SUCCESS) {
174 int32_t resolutionLevelInt = static_cast<int32_t>(resolutionLevel);
175 CHECK_AND_PRINT_LOG(reply.WriteInt32(resolutionLevelInt), "write CastAVPlaybackState failed");
176 }
177 return ERR_NONE;
178 }
179
HandleGetSupportedHdrCapabilities(MessageParcel & data,MessageParcel & reply)180 int32_t AVCastControllerStub::HandleGetSupportedHdrCapabilities(MessageParcel& data, MessageParcel& reply)
181 {
182 std::vector<HDRFormat> hdrFormats;
183 int32_t ret = GetSupportedHdrCapabilities(hdrFormats);
184 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
185 if (ret == AVSESSION_SUCCESS) {
186 std::vector<int32_t> hdrFormatsInt;
187 for (auto it = hdrFormats.begin(); it != hdrFormats.end(); it++) {
188 hdrFormatsInt.emplace_back(static_cast<int32_t>(*it));
189 }
190 CHECK_AND_PRINT_LOG(reply.WriteInt32Vector(hdrFormatsInt), "write GetSupportedHdrCapabilities failed");
191 }
192 return ERR_NONE;
193 }
194
HandleGetSupportedPlaySpeeds(MessageParcel & data,MessageParcel & reply)195 int32_t AVCastControllerStub::HandleGetSupportedPlaySpeeds(MessageParcel& data, MessageParcel& reply)
196 {
197 std::vector<float> playSpeeds;
198 int32_t ret = GetSupportedPlaySpeeds(playSpeeds);
199 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
200 if (ret == AVSESSION_SUCCESS) {
201 CHECK_AND_PRINT_LOG(reply.WriteFloatVector(playSpeeds), "write GetSupportedPlaySpeeds failed");
202 }
203 return ERR_NONE;
204 }
205
HandleGetCurrentItem(MessageParcel & data,MessageParcel & reply)206 int32_t AVCastControllerStub::HandleGetCurrentItem(MessageParcel& data, MessageParcel& reply)
207 {
208 AVQueueItem currentItem;
209 int32_t ret = GetCurrentItem(currentItem);
210 reply.SetMaxCapacity(defaultIpcCapacity);
211 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
212 if (ret == AVSESSION_SUCCESS) {
213 CHECK_AND_RETURN_RET_LOG(reply.WriteParcelable(¤tItem), ERR_NONE, "Write currentItem failed");
214 }
215 return ERR_NONE;
216 }
217
HandleSetDisplaySurface(MessageParcel & data,MessageParcel & reply)218 int32_t AVCastControllerStub::HandleSetDisplaySurface(MessageParcel& data, MessageParcel& reply)
219 {
220 AVSESSION_TRACE_SYNC_START("AVSessionControllerStub::HandleSetDisplaySurface");
221 int32_t err = PermissionChecker::GetInstance().CheckPermission(PermissionChecker::CHECK_SYSTEM_PERMISSION);
222 if (err != ERR_NONE) {
223 SLOGE("SetDisplaySurface: CheckPermission failed");
224 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
225 return ERR_NONE;
226 }
227
228 sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
229 if (remoteObj == nullptr) {
230 SLOGE("BufferProducer is null");
231 return ERR_NULL_OBJECT;
232 }
233
234 sptr<IBufferProducer> producer = iface_cast<IBufferProducer>(remoteObj);
235
236 auto pSurface = Surface::CreateSurfaceAsProducer(producer);
237 CHECK_AND_RETURN_RET_LOG(pSurface != nullptr, AVSESSION_ERROR, "Surface provider is null");
238 auto surfaceInstance = SurfaceUtils::GetInstance();
239 CHECK_AND_RETURN_RET_LOG(surfaceInstance != nullptr, AVSESSION_ERROR, "Surface instance is null");
240 surfaceInstance->Add(pSurface->GetUniqueId(), pSurface);
241 uint64_t uniqueId = pSurface->GetUniqueId();
242
243 auto surfaceId = std::to_string(uniqueId);
244 SLOGI("Get surface id uint64_t: %{public}" PRIu64 ", get the string: %{public}s", uniqueId, surfaceId.c_str());
245 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(SetDisplaySurface(surfaceId)),
246 AVSESSION_ERROR, "WriteInt32 result failed");
247 return ERR_NONE;
248 }
249
HandleSetCastPlaybackFilter(MessageParcel & data,MessageParcel & reply)250 int32_t AVCastControllerStub::HandleSetCastPlaybackFilter(MessageParcel& data, MessageParcel& reply)
251 {
252 std::string str = data.ReadString();
253 if (str.length() != AVPlaybackState::PLAYBACK_KEY_MAX) {
254 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write SetCastPlaybackFilter ret failed");
255 return ERR_NONE;
256 }
257 if (str.find_first_not_of("01") != std::string::npos) {
258 SLOGI("mask string not all 0 or 1");
259 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write int32 failed");
260 return ERR_NONE;
261 }
262 AVPlaybackState::PlaybackStateMaskType filter(str);
263 CHECK_AND_PRINT_LOG(reply.WriteInt32(SetCastPlaybackFilter(filter)), "write int32 failed");
264 return ERR_NONE;
265 }
266
HandleProcessMediaKeyResponse(MessageParcel & data,MessageParcel & reply)267 int32_t AVCastControllerStub::HandleProcessMediaKeyResponse(MessageParcel& data, MessageParcel& reply)
268 {
269 std::string assetId = data.ReadString();
270 std::vector<uint8_t> response;
271 uint32_t responseSize = static_cast<uint32_t>(data.ReadInt32());
272 uint32_t responseMaxLen = 8 * 1024 * 1024;
273 CHECK_AND_RETURN_RET_LOG(responseSize < responseMaxLen, AVSESSION_ERROR,
274 "The size of response is too large.");
275 if (responseSize != 0) {
276 const uint8_t *responseBuf = static_cast<const uint8_t *>(data.ReadBuffer(responseSize));
277 if (responseBuf == nullptr) {
278 SLOGE("AVCastControllerStub::HandleProvideKeyResponse read response failed");
279 return IPC_STUB_WRITE_PARCEL_ERR;
280 }
281 response.assign(responseBuf, responseBuf + responseSize);
282 }
283 CHECK_AND_PRINT_LOG(reply.WriteInt32(ProcessMediaKeyResponse(assetId, response)), "write int32 failed");
284 return ERR_NONE;
285 }
286
HandleAddAvailableCommand(MessageParcel & data,MessageParcel & reply)287 int32_t AVCastControllerStub::HandleAddAvailableCommand(MessageParcel& data, MessageParcel& reply)
288 {
289 int32_t cmd = data.ReadInt32();
290 int32_t ret = AddAvailableCommand(cmd);
291 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 failed");
292 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ERR_NONE, "AddAvailableCommand failed");
293 return ERR_NONE;
294 }
295
HandleRemoveAvailableCommand(MessageParcel & data,MessageParcel & reply)296 int32_t AVCastControllerStub::HandleRemoveAvailableCommand(MessageParcel& data, MessageParcel& reply)
297 {
298 int32_t cmd = data.ReadInt32();
299 int32_t ret = RemoveAvailableCommand(cmd);
300 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 failed");
301 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ERR_NONE, "RemoveAvailableCommand failed");
302 return ERR_NONE;
303 }
304
HandleGetValidCommands(MessageParcel & data,MessageParcel & reply)305 int32_t AVCastControllerStub::HandleGetValidCommands(MessageParcel& data, MessageParcel& reply)
306 {
307 std::vector<int32_t> cmds;
308 int32_t ret = GetValidCommands(cmds);
309 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
310 if (ret == AVSESSION_SUCCESS) {
311 CHECK_AND_PRINT_LOG(reply.WriteInt32Vector(cmds), "write cmd int32 vector failed");
312 }
313 return ERR_NONE;
314 }
315
HandleDestroy(MessageParcel & data,MessageParcel & reply)316 int32_t AVCastControllerStub::HandleDestroy(MessageParcel& data, MessageParcel& reply)
317 {
318 CHECK_AND_PRINT_LOG(reply.WriteInt32(Destroy()), "write release() ret failed");
319 return ERR_NONE;
320 }
321 } // namespace OHOS::AVSession
322