• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 "avsession_stub.h"
17 #include "avsession_callback_proxy.h"
18 #include "avsession_trace.h"
19 
20 namespace OHOS::AVSession {
CheckInterfaceToken(MessageParcel & data)21 bool AVSessionStub::CheckInterfaceToken(MessageParcel& data)
22 {
23     auto localDescriptor = IAVSession::GetDescriptor();
24     auto remoteDescriptor = data.ReadInterfaceToken();
25     if (remoteDescriptor != localDescriptor) {
26         SLOGI("interface token is not equal");
27         return false;
28     }
29     return true;
30 }
31 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)32 int32_t AVSessionStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
33 {
34     if (!CheckInterfaceToken(data)) {
35         return AVSESSION_ERROR;
36     }
37     SLOGI("cmd code is %{public}d", code);
38     if (code < SESSION_CMD_MAX) {
39         return (this->*handlers[code])(data, reply);
40     }
41     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
42 }
43 
HandleGetSessionId(MessageParcel & data,MessageParcel & reply)44 int32_t AVSessionStub::HandleGetSessionId(MessageParcel& data, MessageParcel& reply)
45 {
46     CHECK_AND_PRINT_LOG(reply.WriteString(GetSessionId()), "write int32_t failed");
47     return ERR_NONE;
48 }
49 
HandleGetSessionType(MessageParcel & data,MessageParcel & reply)50 int32_t AVSessionStub::HandleGetSessionType(MessageParcel& data, MessageParcel& reply)
51 {
52     CHECK_AND_PRINT_LOG(reply.WriteString(GetSessionType()), "write int32_t failed");
53     return ERR_NONE;
54 }
55 
HandleRegisterCallbackInner(MessageParcel & data,MessageParcel & reply)56 int32_t AVSessionStub::HandleRegisterCallbackInner(MessageParcel& data, MessageParcel& reply)
57 {
58     auto remoteObject = data.ReadRemoteObject();
59     if (remoteObject == nullptr) {
60         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32_t failed");
61         return ERR_NONE;
62     }
63     auto callback = iface_cast<AVSessionCallbackProxy>(remoteObject);
64     CHECK_AND_RETURN_RET_LOG(callback != nullptr, ERR_NONE, "callback is nullptr");
65     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(RegisterCallbackInner(callback)),
66                              ERR_NONE, "write int32_t failed");
67     return ERR_NONE;
68 }
69 
HandleDestroy(MessageParcel & data,MessageParcel & reply)70 int32_t AVSessionStub::HandleDestroy(MessageParcel& data, MessageParcel& reply)
71 {
72     int32_t ret = Destroy();
73     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_SUCCESS), ERR_NONE, "write int32_t failed");
74     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "Destroy failed");
75     return ERR_NONE;
76 }
77 
HandleGetAVPlaybackState(MessageParcel & data,MessageParcel & reply)78 int32_t AVSessionStub::HandleGetAVPlaybackState(MessageParcel& data, MessageParcel& reply)
79 {
80     AVPlaybackState avPlaybackState;
81     int32_t ret = GetAVPlaybackState(avPlaybackState);
82     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
83     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "GetAVPlaybackState failed");
84     CHECK_AND_RETURN_RET_LOG(reply.WriteParcelable(&avPlaybackState), ERR_NONE, "write avPlaybackState failed");
85     return ERR_NONE;
86 }
87 
HandleSetAVPlaybackState(MessageParcel & data,MessageParcel & reply)88 int32_t AVSessionStub::HandleSetAVPlaybackState(MessageParcel& data, MessageParcel& reply)
89 {
90     AVSESSION_TRACE_SYNC_START("AVSessionStub::SetAVPlaybackState");
91     sptr avPlaybackState = data.ReadParcelable<AVPlaybackState>();
92     if (avPlaybackState == nullptr) {
93         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "WriteInt32 result failed");
94         return ERR_NONE;
95     }
96     int32_t ret = SetAVPlaybackState(*avPlaybackState);
97     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
98     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "GetAVPlaybackState failed");
99     return ERR_NONE;
100 }
101 
HandleSetAVMetaData(MessageParcel & data,MessageParcel & reply)102 int32_t AVSessionStub::HandleSetAVMetaData(MessageParcel& data, MessageParcel& reply)
103 {
104     AVSESSION_TRACE_SYNC_START("AVSessionStub::SetAVMetaData");
105     sptr avMetaData = data.ReadParcelable<AVMetaData>();
106     if (avMetaData == nullptr) {
107         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "WriteInt32 result failed");
108         return ERR_NONE;
109     }
110     int32_t ret = SetAVMetaData(*avMetaData);
111     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
112     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "GetAVPlaybackState failed");
113     return ERR_NONE;
114 }
115 
HandleSetLaunchAbility(MessageParcel & data,MessageParcel & reply)116 int32_t AVSessionStub::HandleSetLaunchAbility(MessageParcel& data, MessageParcel& reply)
117 {
118     sptr want = data.ReadParcelable<AbilityRuntime::WantAgent::WantAgent>();
119     if (want == nullptr) {
120         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "WriteInt32 result failed");
121         return ERR_NONE;
122     }
123     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(SetLaunchAbility(*want)), ERR_NONE, "WriteInt32 result failed");
124     return ERR_NONE;
125 }
126 
HandleGetAVMetaData(MessageParcel & data,MessageParcel & reply)127 int32_t AVSessionStub::HandleGetAVMetaData(MessageParcel& data, MessageParcel& reply)
128 {
129     AVMetaData avMetaData;
130     int32_t ret = GetAVMetaData(avMetaData);
131     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
132     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ERR_NONE, "GetAVMetaData failed");
133     CHECK_AND_RETURN_RET_LOG(reply.WriteParcelable(&avMetaData), ERR_NONE, "write avMetaData failed");
134     return ERR_NONE;
135 }
136 
HandleGetAVQueueItems(MessageParcel & data,MessageParcel & reply)137 int32_t AVSessionStub::HandleGetAVQueueItems(MessageParcel& data, MessageParcel& reply)
138 {
139     std::vector<AVQueueItem> avQueueItems;
140     int32_t ret = GetAVQueueItems(avQueueItems);
141     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
142     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ERR_NONE, "GetAVQueueItems failed");
143     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(avQueueItems.size()), ERR_NONE, "write items num int32 failed");
144     for (auto &parcelable : avQueueItems) {
145         CHECK_AND_RETURN_RET_LOG(reply.WriteParcelable(&parcelable), ERR_NONE, "Write items failed");
146     }
147     return ERR_NONE;
148 }
149 
HandleSetAVQueueItems(MessageParcel & data,MessageParcel & reply)150 int32_t AVSessionStub::HandleSetAVQueueItems(MessageParcel& data, MessageParcel& reply)
151 {
152     AVSESSION_TRACE_SYNC_START("AVSessionStub::SetAVQueueItems");
153     int32_t maxQueueItemLength = 1000; // The maximum allowed playlist size is 1000
154     std::vector<AVQueueItem> items_;
155     int32_t itemNum = data.ReadInt32();
156     CHECK_AND_RETURN_RET_LOG((itemNum >= 0) && (itemNum < maxQueueItemLength),
157         ERR_UNMARSHALLING, "read int32 itemNum failed");
158     for (int32_t i = 0; i < itemNum; i++) {
159         AVQueueItem *item = data.ReadParcelable<AVQueueItem>();
160         CHECK_AND_RETURN_RET_LOG(item != nullptr, ERR_UNMARSHALLING, "read parcelable AVQueueItem failed");
161         items_.emplace_back(*item);
162         delete item;
163     }
164     int32_t ret = SetAVQueueItems(items_);
165     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
166     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "SetAVQueueItems failed");
167     return ERR_NONE;
168 }
169 
HandleGetAVQueueTitle(MessageParcel & data,MessageParcel & reply)170 int32_t AVSessionStub::HandleGetAVQueueTitle(MessageParcel& data, MessageParcel& reply)
171 {
172     std::string title;
173     int32_t ret = GetAVQueueTitle(title);
174     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
175     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ERR_NONE, "GetAVQueueTitle failed");
176     CHECK_AND_RETURN_RET_LOG(reply.WriteString(title), ERR_NONE, "write title string failed");
177     return ERR_NONE;
178 }
179 
HandleSetAVQueueTitle(MessageParcel & data,MessageParcel & reply)180 int32_t AVSessionStub::HandleSetAVQueueTitle(MessageParcel& data, MessageParcel& reply)
181 {
182     AVSESSION_TRACE_SYNC_START("AVSessionStub::SetAVQueueTitle");
183     std::string title;
184     CHECK_AND_RETURN_RET_LOG(data.ReadString(title), ERR_NONE, "read title string failed");
185     int32_t ret = SetAVQueueTitle(title);
186     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
187     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "SetAVQueueTitle failed");
188     return ERR_NONE;
189 }
190 
HandleGetExtras(MessageParcel & data,MessageParcel & reply)191 int32_t AVSessionStub::HandleGetExtras(MessageParcel& data, MessageParcel& reply)
192 {
193     AVSESSION_TRACE_SYNC_START("AVSessionStub::HandleGetExtras");
194     AAFwk::WantParams extras;
195     int32_t ret = GetExtras(extras);
196     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
197     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ERR_NONE, "GetExtras failed");
198     CHECK_AND_RETURN_RET_LOG(reply.WriteParcelable(&extras), ERR_NONE, "write extras failed");
199     return ERR_NONE;
200 }
201 
HandleSetExtras(MessageParcel & data,MessageParcel & reply)202 int32_t AVSessionStub::HandleSetExtras(MessageParcel& data, MessageParcel& reply)
203 {
204     AVSESSION_TRACE_SYNC_START("AVSessionStub::HandleSetExtras");
205     sptr extrasWant = data.ReadParcelable<AAFwk::WantParams>();
206     if (extrasWant == nullptr) {
207         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "WriteInt32 result failed");
208         return ERR_NONE;
209     }
210     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(SetExtras(*extrasWant)), ERR_NONE, "WriteInt32 result failed");
211     return ERR_NONE;
212 }
213 
HandleGetController(MessageParcel & data,MessageParcel & reply)214 int32_t AVSessionStub::HandleGetController(MessageParcel& data, MessageParcel& reply)
215 {
216     sptr<IRemoteObject> controller = GetControllerInner();
217     if (controller == nullptr) {
218         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "write int32 failed");
219         return ERR_NONE;
220     }
221     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_SUCCESS), ERR_NONE, "write int32 failed");
222     CHECK_AND_RETURN_RET_LOG(reply.WriteRemoteObject(controller), ERR_NONE, "write object failed");
223     return ERR_NONE;
224 }
225 
226 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
HandleGetAVCastController(MessageParcel & data,MessageParcel & reply)227 int32_t AVSessionStub::HandleGetAVCastController(MessageParcel& data, MessageParcel& reply)
228 {
229     sptr<IRemoteObject> castController = GetAVCastControllerInner();
230     if (castController == nullptr) {
231         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "write int32 failed");
232         return ERR_NONE;
233     }
234     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_SUCCESS), ERR_NONE, "write int32 failed");
235     CHECK_AND_RETURN_RET_LOG(reply.WriteRemoteObject(castController), ERR_NONE, "write object failed");
236     return ERR_NONE;
237 }
238 #endif
239 
HandleActivate(MessageParcel & data,MessageParcel & reply)240 int32_t AVSessionStub::HandleActivate(MessageParcel& data, MessageParcel& reply)
241 {
242     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(Activate()), ERR_NONE, "WriteInt32 failed");
243     return ERR_NONE;
244 }
245 
HandleDeactivate(MessageParcel & data,MessageParcel & reply)246 int32_t AVSessionStub::HandleDeactivate(MessageParcel& data, MessageParcel& reply)
247 {
248     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(Deactivate()), ERR_NONE, "WriteInt32 failed");
249     return ERR_NONE;
250 }
251 
HandleIsActive(MessageParcel & data,MessageParcel & reply)252 int32_t AVSessionStub::HandleIsActive(MessageParcel& data, MessageParcel& reply)
253 {
254     CHECK_AND_RETURN_RET_LOG(reply.WriteBool(IsActive()), ERR_NONE, "WriteBool failed");
255     return ERR_NONE;
256 }
257 
HandleAddSupportCommand(MessageParcel & data,MessageParcel & reply)258 int32_t AVSessionStub::HandleAddSupportCommand(MessageParcel& data, MessageParcel& reply)
259 {
260     int32_t ret = AddSupportCommand(data.ReadInt32());
261     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 failed");
262     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ERR_NONE, "AddSupportCommand failed");
263     return ERR_NONE;
264 }
265 
HandleDeleteSupportCommand(MessageParcel & data,MessageParcel & reply)266 int32_t AVSessionStub::HandleDeleteSupportCommand(MessageParcel& data, MessageParcel& reply)
267 {
268     int32_t ret = DeleteSupportCommand(data.ReadInt32());
269     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 failed");
270     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ERR_NONE, "DeleteSupportCommand failed");
271     return ERR_NONE;
272 }
273 
HandleSetSessionEvent(MessageParcel & data,MessageParcel & reply)274 int32_t AVSessionStub::HandleSetSessionEvent(MessageParcel& data, MessageParcel& reply)
275 {
276     auto event = data.ReadString();
277     sptr want = data.ReadParcelable<AAFwk::WantParams>();
278     if (want == nullptr) {
279         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "WriteInt32 result failed");
280         return ERR_NONE;
281     }
282     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(SetSessionEvent(event, *want)), ERR_NONE, "WriteInt32 result failed");
283     return ERR_NONE;
284 }
285 
286 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
HandleReleaseCast(MessageParcel & data,MessageParcel & reply)287 int32_t AVSessionStub::HandleReleaseCast(MessageParcel& data, MessageParcel& reply)
288 {
289     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ReleaseCast()), ERR_NONE, "WriteInt32 failed");
290     return ERR_NONE;
291 }
292 #endif
293 } // namespace OHOS::AVSession
294