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