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
HandleGetCurrentItem(MessageParcel& data, MessageParcel& reply)136 int32_t AVCastControllerStub::HandleGetCurrentItem(MessageParcel& data, MessageParcel& reply)
137 {
138 AVQueueItem currentItem;
139 int32_t ret = GetCurrentItem(currentItem);
140 reply.SetMaxCapacity(defaultIpcCapacity);
141 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
142 if (ret == AVSESSION_SUCCESS) {
143 CHECK_AND_RETURN_RET_LOG(reply.WriteParcelable(¤tItem), ERR_NONE, "Write currentItem failed");
144 }
145 return ERR_NONE;
146 }
147
HandleSetDisplaySurface(MessageParcel& data, MessageParcel& reply)148 int32_t AVCastControllerStub::HandleSetDisplaySurface(MessageParcel& data, MessageParcel& reply)
149 {
150 AVSESSION_TRACE_SYNC_START("AVSessionControllerStub::HandleSetDisplaySurface");
151 int32_t err = PermissionChecker::GetInstance().CheckPermission(PermissionChecker::CHECK_SYSTEM_PERMISSION);
152 if (err != ERR_NONE) {
153 SLOGE("SetDisplaySurface: CheckPermission failed");
154 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(err), ERR_NONE, "write int32 failed");
155 return ERR_NONE;
156 }
157 sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
158 if (remoteObj == nullptr) {
159 SLOGE("BufferProducer is null");
160 return ERR_NULL_OBJECT;
161 }
162
163 sptr<IBufferProducer> producer = iface_cast<IBufferProducer>(remoteObj);
164
165 auto pSurface = Surface::CreateSurfaceAsProducer(producer);
166 CHECK_AND_RETURN_RET_LOG(pSurface != nullptr, AVSESSION_ERROR, "Surface provider is null");
167 auto surfaceInstance = SurfaceUtils::GetInstance();
168 CHECK_AND_RETURN_RET_LOG(surfaceInstance != nullptr, AVSESSION_ERROR, "Surface instance is null");
169 surfaceInstance->Add(pSurface->GetUniqueId(), pSurface);
170 uint64_t uniqueId = pSurface->GetUniqueId();
171
172 auto surfaceId = std::to_string(uniqueId);
173 SLOGI("Get surface id uint64_t: %{public}lu, get the string: %{public}s", uniqueId, surfaceId.c_str());
174 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(SetDisplaySurface(surfaceId)),
175 AVSESSION_ERROR, "WriteInt32 result failed");
176 return ERR_NONE;
177 }
178
HandleSetCastPlaybackFilter(MessageParcel& data, MessageParcel& reply)179 int32_t AVCastControllerStub::HandleSetCastPlaybackFilter(MessageParcel& data, MessageParcel& reply)
180 {
181 std::string str = data.ReadString();
182 if (str.length() != AVPlaybackState::PLAYBACK_KEY_MAX) {
183 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write SetCastPlaybackFilter ret failed");
184 return ERR_NONE;
185 }
186 if (str.find_first_not_of("01") != std::string::npos) {
187 SLOGI("mask string not all 0 or 1");
188 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write int32 failed");
189 return ERR_NONE;
190 }
191 AVPlaybackState::PlaybackStateMaskType filter(str);
192 CHECK_AND_PRINT_LOG(reply.WriteInt32(SetCastPlaybackFilter(filter)), "write int32 failed");
193 return ERR_NONE;
194 }
195
HandleProcessMediaKeyResponse(MessageParcel& data, MessageParcel& reply)196 int32_t AVCastControllerStub::HandleProcessMediaKeyResponse(MessageParcel& data, MessageParcel& reply)
197 {
198 std::string assetId = data.ReadString();
199 std::vector<uint8_t> response;
200 uint32_t responseSize = static_cast<uint32_t>(data.ReadInt32());
201 uint32_t responseMaxLen = 8 * 1024 * 1024;
202 CHECK_AND_RETURN_RET_LOG(responseSize < responseMaxLen, AVSESSION_ERROR,
203 "The size of response is too large.");
204 if (responseSize != 0) {
205 const uint8_t *responseBuf = static_cast<const uint8_t *>(data.ReadBuffer(responseSize));
206 if (responseBuf == nullptr) {
207 SLOGE("AVCastControllerStub::HandleProvideKeyResponse read response failed");
208 return IPC_STUB_WRITE_PARCEL_ERR;
209 }
210 response.assign(responseBuf, responseBuf + responseSize);
211 }
212 CHECK_AND_PRINT_LOG(reply.WriteInt32(ProcessMediaKeyResponse(assetId, response)), "write int32 failed");
213 return ERR_NONE;
214 }
215
216 int32_t AVCastControllerStub::HandleAddAvailableCommand(MessageParcel& data, MessageParcel& reply)
217 {
218 int32_t cmd = data.ReadInt32();
219 int32_t ret = AddAvailableCommand(cmd);
220 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 failed");
221 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ERR_NONE, "AddAvailableCommand failed");
222 return ERR_NONE;
223 }
224
225 int32_t AVCastControllerStub::HandleRemoveAvailableCommand(MessageParcel& data, MessageParcel& reply)
226 {
227 int32_t cmd = data.ReadInt32();
228 int32_t ret = RemoveAvailableCommand(cmd);
229 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 failed");
230 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ERR_NONE, "RemoveAvailableCommand failed");
231 return ERR_NONE;
232 }
233
234 int32_t AVCastControllerStub::HandleGetValidCommands(MessageParcel& data, MessageParcel& reply)
235 {
236 std::vector<int32_t> cmds;
237 int32_t ret = GetValidCommands(cmds);
238 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
239 if (ret == AVSESSION_SUCCESS) {
240 CHECK_AND_PRINT_LOG(reply.WriteInt32Vector(cmds), "write cmd int32 vector failed");
241 }
242 return ERR_NONE;
243 }
244
245 int32_t AVCastControllerStub::HandleDestroy(MessageParcel& data, MessageParcel& reply)
246 {
247 CHECK_AND_PRINT_LOG(reply.WriteInt32(Destroy()), "write release() ret failed");
248 return ERR_NONE;
249 }
250 } // namespace OHOS::AVSession
251