1 /*
2 * Copyright (c) 2023 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
23 namespace OHOS::AVSession {
CheckInterfaceToken(MessageParcel & data)24 bool AVCastControllerStub::CheckInterfaceToken(MessageParcel& data)
25 {
26 auto localDescriptor = IAVCastController::GetDescriptor();
27 auto remoteDescriptor = data.ReadInterfaceToken();
28 if (remoteDescriptor != localDescriptor) {
29 SLOGI("interface token is not equal");
30 return false;
31 }
32 return true;
33 }
34
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)35 int32_t AVCastControllerStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
36 MessageOption &option)
37 {
38 if (!CheckInterfaceToken(data)) {
39 return AVSESSION_ERROR;
40 }
41 if (code < CAST_CONTROLLER_CMD_MAX) {
42 return (this->*handlers[code])(data, reply);
43 }
44 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
45 }
46
HandleRegisterCallbackInner(MessageParcel & data,MessageParcel & reply)47 int32_t AVCastControllerStub::HandleRegisterCallbackInner(MessageParcel& data, MessageParcel& reply)
48 {
49 auto remoteObject = data.ReadRemoteObject();
50 if (remoteObject == nullptr) {
51 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write RegisterCallback ret failed");
52 } else {
53 CHECK_AND_PRINT_LOG(reply.WriteInt32(RegisterCallbackInner(remoteObject)),
54 "write RegisterCallback ret failed");
55 }
56 return ERR_NONE;
57 }
58
HandleSendControlCommand(MessageParcel & data,MessageParcel & reply)59 int32_t AVCastControllerStub::HandleSendControlCommand(MessageParcel& data, MessageParcel& reply)
60 {
61 AVSESSION_TRACE_SYNC_START("AVCastControllerStub::SendControlCommand");
62 sptr<AVCastControlCommand> cmd = data.ReadParcelable<AVCastControlCommand>();
63 if (cmd == nullptr) {
64 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING),
65 ERR_UNMARSHALLING, "write SendCommand ret failed");
66 } else {
67 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(SendControlCommand(*cmd)),
68 ERR_UNMARSHALLING, "write SendCommand ret failed");
69 }
70 return ERR_NONE;
71 }
72
HandleStart(MessageParcel & data,MessageParcel & reply)73 int32_t AVCastControllerStub::HandleStart(MessageParcel& data, MessageParcel& reply)
74 {
75 sptr<AVQueueItem> avQueueItem = data.ReadParcelable<AVQueueItem>();
76 AVFileDescriptor avFileDescriptor;
77 avFileDescriptor.fd_ = data.ReadFileDescriptor();
78 if (avQueueItem == nullptr) {
79 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write Start ret failed");
80 } else {
81 avQueueItem->GetDescription()->SetFdSrc(avFileDescriptor);
82 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(Start(*avQueueItem)),
83 ERR_NONE, "Write mediaInfoHolder failed");
84 }
85 return ERR_NONE;
86 }
87
HandlePrepare(MessageParcel & data,MessageParcel & reply)88 int32_t AVCastControllerStub::HandlePrepare(MessageParcel& data, MessageParcel& reply)
89 {
90 sptr<AVQueueItem> avQueueItem = data.ReadParcelable<AVQueueItem>();
91 if (avQueueItem == nullptr) {
92 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write prepare ret failed");
93 } else {
94 if (data.ReadBool()) {
95 SLOGD("Need get fd from proxy");
96 AVFileDescriptor avFileDescriptor;
97 avFileDescriptor.fd_ = data.ReadFileDescriptor();
98 SLOGD("Prepare received fd %{public}d", avFileDescriptor.fd_);
99 avQueueItem->GetDescription()->SetFdSrc(avFileDescriptor);
100 }
101 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(Prepare(*avQueueItem)),
102 ERR_NONE, "Write mediaInfoHolder failed");
103 }
104 return ERR_NONE;
105 }
106
HandleGetDuration(MessageParcel & data,MessageParcel & reply)107 int32_t AVCastControllerStub::HandleGetDuration(MessageParcel& data, MessageParcel& reply)
108 {
109 int32_t duration;
110 int32_t ret = GetDuration(duration);
111 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_UNMARSHALLING, "write int32 failed");
112 if (ret == AVSESSION_SUCCESS) {
113 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(duration), ERR_UNMARSHALLING, "write int32 failed");
114 }
115 return ERR_NONE;
116 }
117
HandleGetCastAVPlayBackState(MessageParcel & data,MessageParcel & reply)118 int32_t AVCastControllerStub::HandleGetCastAVPlayBackState(MessageParcel& data, MessageParcel& reply)
119 {
120 AVPlaybackState state;
121 int32_t ret = GetCastAVPlaybackState(state);
122 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
123 if (ret == AVSESSION_SUCCESS) {
124 CHECK_AND_PRINT_LOG(reply.WriteParcelable(&state), "write CastAVPlaybackState failed");
125 }
126 return ERR_NONE;
127 }
128
HandleGetCurrentItem(MessageParcel & data,MessageParcel & reply)129 int32_t AVCastControllerStub::HandleGetCurrentItem(MessageParcel& data, MessageParcel& reply)
130 {
131 AVQueueItem currentItem;
132 int32_t ret = GetCurrentItem(currentItem);
133 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
134 if (ret == AVSESSION_SUCCESS) {
135 CHECK_AND_RETURN_RET_LOG(reply.WriteParcelable(¤tItem), ERR_NONE, "Write currentItem failed");
136 }
137 return ERR_NONE;
138 }
139
HandleSetDisplaySurface(MessageParcel & data,MessageParcel & reply)140 int32_t AVCastControllerStub::HandleSetDisplaySurface(MessageParcel& data, MessageParcel& reply)
141 {
142 AVSESSION_TRACE_SYNC_START("AVSessionControllerStub::HandleSetDisplaySurface");
143 sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
144 if (remoteObj == nullptr) {
145 SLOGE("BufferProducer is null");
146 return ERR_NULL_OBJECT;
147 }
148
149 sptr<IBufferProducer> producer = iface_cast<IBufferProducer>(remoteObj);
150
151 auto pSurface = Surface::CreateSurfaceAsProducer(producer);
152 CHECK_AND_RETURN_RET_LOG(pSurface != nullptr, AVSESSION_ERROR, "Surface provider is null");
153 auto surfaceInstance = SurfaceUtils::GetInstance();
154 CHECK_AND_RETURN_RET_LOG(surfaceInstance != nullptr, AVSESSION_ERROR, "Surface instance is null");
155 surfaceInstance->Add(pSurface->GetUniqueId(), pSurface);
156 uint64_t uniqueId = pSurface->GetUniqueId();
157
158 auto surfaceId = std::to_string(uniqueId);
159 SLOGI("Get surface id uint64_t: %{public}lu, get the string: %{public}s", uniqueId, surfaceId.c_str());
160 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(SetDisplaySurface(surfaceId)),
161 AVSESSION_ERROR, "WriteInt32 result failed");
162 return ERR_NONE;
163 }
164
HandleSetCastPlaybackFilter(MessageParcel & data,MessageParcel & reply)165 int32_t AVCastControllerStub::HandleSetCastPlaybackFilter(MessageParcel& data, MessageParcel& reply)
166 {
167 std::string str = data.ReadString();
168 if (str.length() != AVPlaybackState::PLAYBACK_KEY_MAX) {
169 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write SetCastPlaybackFilter ret failed");
170 return ERR_NONE;
171 }
172 if (str.find_first_not_of("01") != std::string::npos) {
173 SLOGI("mask string not all 0 or 1");
174 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write int32 failed");
175 return ERR_NONE;
176 }
177 AVPlaybackState::PlaybackStateMaskType filter(str);
178 CHECK_AND_PRINT_LOG(reply.WriteInt32(SetCastPlaybackFilter(filter)), "write int32 failed");
179 return ERR_NONE;
180 }
181
HandleAddAvailableCommand(MessageParcel & data,MessageParcel & reply)182 int32_t AVCastControllerStub::HandleAddAvailableCommand(MessageParcel& data, MessageParcel& reply)
183 {
184 int32_t cmd = data.ReadInt32();
185 int32_t ret = AddAvailableCommand(cmd);
186 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 failed");
187 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ERR_NONE, "AddAvailableCommand failed");
188 return ERR_NONE;
189 }
190
HandleRemoveAvailableCommand(MessageParcel & data,MessageParcel & reply)191 int32_t AVCastControllerStub::HandleRemoveAvailableCommand(MessageParcel& data, MessageParcel& reply)
192 {
193 int32_t cmd = data.ReadInt32();
194 int32_t ret = RemoveAvailableCommand(cmd);
195 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 failed");
196 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ERR_NONE, "RemoveAvailableCommand failed");
197 return ERR_NONE;
198 }
199
HandleGetValidCommands(MessageParcel & data,MessageParcel & reply)200 int32_t AVCastControllerStub::HandleGetValidCommands(MessageParcel& data, MessageParcel& reply)
201 {
202 std::vector<int32_t> cmds;
203 int32_t ret = GetValidCommands(cmds);
204 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
205 if (ret == AVSESSION_SUCCESS) {
206 CHECK_AND_PRINT_LOG(reply.WriteInt32Vector(cmds), "write cmd int32 vector failed");
207 }
208 return ERR_NONE;
209 }
210
HandleDestroy(MessageParcel & data,MessageParcel & reply)211 int32_t AVCastControllerStub::HandleDestroy(MessageParcel& data, MessageParcel& reply)
212 {
213 CHECK_AND_PRINT_LOG(reply.WriteInt32(Destroy()), "write release() ret failed");
214 return ERR_NONE;
215 }
216 } // namespace OHOS::AVSession
217