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