• 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_controller_stub.h"
17 #include "avsession_errors.h"
18 #include "avsession_log.h"
19 #include "avsession_trace.h"
20 
21 namespace OHOS::AVSession {
CheckInterfaceToken(MessageParcel & data)22 bool AVSessionControllerStub::CheckInterfaceToken(MessageParcel& data)
23 {
24     auto localDescriptor = IAVSessionController::GetDescriptor();
25     auto remoteDescriptor = data.ReadInterfaceToken();
26     if (remoteDescriptor != localDescriptor) {
27         SLOGI("interface token is not equal");
28         return false;
29     }
30     return true;
31 }
32 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)33 int32_t AVSessionControllerStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
34     MessageOption &option)
35 {
36     if (!CheckInterfaceToken(data)) {
37         return AVSESSION_ERROR;
38     }
39     if (code < CONTROLLER_CMD_MAX) {
40         return (this->*handlers[code])(data, reply);
41     }
42     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
43 }
44 
HandleRegisterCallbackInner(MessageParcel & data,MessageParcel & reply)45 int32_t AVSessionControllerStub::HandleRegisterCallbackInner(MessageParcel& data, MessageParcel& reply)
46 {
47     auto remoteObject = data.ReadRemoteObject();
48     if (remoteObject == nullptr) {
49         CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write RegisterCallback ret failed");
50     } else {
51         CHECK_AND_PRINT_LOG(reply.WriteInt32(RegisterCallbackInner(remoteObject)),
52             "write RegisterCallback ret failed");
53     }
54     return ERR_NONE;
55 }
56 
HandleDestroy(MessageParcel & data,MessageParcel & reply)57 int32_t AVSessionControllerStub::HandleDestroy(MessageParcel& data, MessageParcel& reply)
58 {
59     CHECK_AND_PRINT_LOG(reply.WriteInt32(Destroy()), "write release() ret failed");
60     return ERR_NONE;
61 }
62 
HandleGetAVCallState(MessageParcel & data,MessageParcel & reply)63 int32_t AVSessionControllerStub::HandleGetAVCallState(MessageParcel& data, MessageParcel& reply)
64 {
65     AVCallState state;
66     int32_t ret = GetAVCallState(state);
67     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
68     if (ret == AVSESSION_SUCCESS) {
69         CHECK_AND_PRINT_LOG(reply.WriteParcelable(&state), "write AVCallState failed");
70     }
71     return ERR_NONE;
72 }
73 
HandleGetAVCallMetaData(MessageParcel & data,MessageParcel & reply)74 int32_t AVSessionControllerStub::HandleGetAVCallMetaData(MessageParcel& data, MessageParcel& reply)
75 {
76     AVCallMetaData metaData;
77     int32_t ret = GetAVCallMetaData(metaData);
78     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
79     if (ret == AVSESSION_SUCCESS) {
80         CHECK_AND_PRINT_LOG(reply.WriteParcelable(&metaData), "write AVCallMetaData failed");
81     }
82     return ERR_NONE;
83 }
84 
HandleGetAVPlaybackState(MessageParcel & data,MessageParcel & reply)85 int32_t AVSessionControllerStub::HandleGetAVPlaybackState(MessageParcel& data, MessageParcel& reply)
86 {
87     AVPlaybackState state;
88     int32_t ret = GetAVPlaybackState(state);
89     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
90     if (ret == AVSESSION_SUCCESS) {
91         CHECK_AND_PRINT_LOG(reply.WriteParcelable(&state), "write AVPlaybackState failed");
92     }
93     return ERR_NONE;
94 }
95 
HandleSendControlCommand(MessageParcel & data,MessageParcel & reply)96 int32_t AVSessionControllerStub::HandleSendControlCommand(MessageParcel& data, MessageParcel& reply)
97 {
98     AVSESSION_TRACE_SYNC_START("AVSessionControllerStub::SendControlCommand");
99     sptr<AVControlCommand> cmd = data.ReadParcelable<AVControlCommand>();
100     if (cmd == nullptr) {
101         CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write SendCommand ret failed");
102     } else {
103         CHECK_AND_PRINT_LOG(reply.WriteInt32(SendControlCommand(*cmd)), "write SendCommand ret failed");
104     }
105     return ERR_NONE;
106 }
107 
HandleSendCommonCommand(MessageParcel & data,MessageParcel & reply)108 int32_t AVSessionControllerStub::HandleSendCommonCommand(MessageParcel& data, MessageParcel& reply)
109 {
110     AVSESSION_TRACE_SYNC_START("AVSessionControllerStub::SendCommonCommand");
111     auto commonCommand = data.ReadString();
112     sptr commandArgs = data.ReadParcelable<AAFwk::WantParams>();
113     if (commandArgs == nullptr) {
114         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "WriteInt32 result failed");
115         return ERR_NONE;
116     }
117     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(SendCommonCommand(commonCommand, *commandArgs)),
118         ERR_NONE, "WriteInt32 result failed");
119     return ERR_NONE;
120 }
121 
HandleGetAVMetaData(MessageParcel & data,MessageParcel & reply)122 int32_t AVSessionControllerStub::HandleGetAVMetaData(MessageParcel& data, MessageParcel& reply)
123 {
124     AVMetaData metaData;
125     int32_t ret = GetAVMetaData(metaData);
126     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
127     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ERR_NONE, "GetAVMetaData failed");
128 
129     int mediaImageLength = 0;
130     std::vector<uint8_t> mediaImageBuffer;
131     std::shared_ptr<AVSessionPixelMap> mediaPixelMap = metaData.GetMediaImage();
132     if (mediaPixelMap != nullptr) {
133         mediaImageBuffer = mediaPixelMap->GetInnerImgBuffer();
134         mediaImageLength = static_cast<int>(mediaImageBuffer.size());
135         metaData.SetMediaLength(mediaImageLength);
136     }
137 
138     int avQueueImageLength = 0;
139     std::vector<uint8_t> avQueueImageBuffer;
140     std::shared_ptr<AVSessionPixelMap> avQueuePixelMap = metaData.GetAVQueueImage();
141     if (avQueuePixelMap != nullptr) {
142         avQueueImageBuffer = avQueuePixelMap->GetInnerImgBuffer();
143         avQueueImageLength = static_cast<int>(avQueueImageBuffer.size());
144         metaData.SetAVQueueLength(avQueueImageLength);
145     }
146 
147     int twoImageLength = mediaImageLength + avQueueImageLength;
148     if (twoImageLength == 0) {
149         CHECK_AND_PRINT_LOG(reply.WriteInt32(twoImageLength), "write twoImageLength failed");
150         CHECK_AND_PRINT_LOG(reply.WriteParcelable(&metaData), "write AVMetaData failed");
151         return ERR_NONE;
152     }
153 
154     unsigned char *buffer = new (std::nothrow) unsigned char[twoImageLength];
155     if (buffer == nullptr) {
156         SLOGE("new buffer failed of length = %{public}d", twoImageLength);
157         return AVSESSION_ERROR;
158     }
159 
160     for (int i = 0; i < mediaImageLength; i++) {
161         buffer[i] = mediaImageBuffer[i];
162     }
163 
164     for (int j = mediaImageLength, k = 0; j < twoImageLength && k < avQueueImageLength; j++, k++) {
165         buffer[j] = avQueueImageBuffer[k];
166     }
167 
168     if (!reply.WriteInt32(twoImageLength) || !AVMetaData::MarshallingExceptImg(reply, metaData) ||
169         !reply.WriteRawData(buffer, twoImageLength)) {
170         SLOGE("fail to write parcel");
171         delete[] buffer;
172         return AVSESSION_ERROR;
173     }
174     delete[] buffer;
175     return ERR_NONE;
176 }
177 
HandleGetAVQueueItems(MessageParcel & data,MessageParcel & reply)178 int32_t AVSessionControllerStub::HandleGetAVQueueItems(MessageParcel& data, MessageParcel& reply)
179 {
180     std::vector<AVQueueItem> items;
181     int32_t ret = GetAVQueueItems(items);
182     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
183     if (ret == AVSESSION_SUCCESS) {
184         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(items.size()), ERR_NONE, "write items num int32 failed");
185         for (auto &parcelable : items) {
186             CHECK_AND_RETURN_RET_LOG(reply.WriteParcelable(&parcelable), ERR_NONE, "Write items failed");
187         }
188     }
189     return ERR_NONE;
190 }
191 
HandleGetAVQueueTitle(MessageParcel & data,MessageParcel & reply)192 int32_t AVSessionControllerStub::HandleGetAVQueueTitle(MessageParcel& data, MessageParcel& reply)
193 {
194     std::string title;
195     int32_t ret = GetAVQueueTitle(title);
196     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
197     if (ret == AVSESSION_SUCCESS) {
198         CHECK_AND_PRINT_LOG(reply.WriteString(title), "write title failed");
199     }
200     return ERR_NONE;
201 }
202 
HandleSkipToQueueItem(MessageParcel & data,MessageParcel & reply)203 int32_t AVSessionControllerStub::HandleSkipToQueueItem(MessageParcel& data, MessageParcel& reply)
204 {
205     int32_t itemId;
206     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(itemId), ERR_NONE, "read itemId int32 failed");
207     int32_t ret = SkipToQueueItem(itemId);
208     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
209     return ERR_NONE;
210 }
211 
HandleGetExtras(MessageParcel & data,MessageParcel & reply)212 int32_t AVSessionControllerStub::HandleGetExtras(MessageParcel& data, MessageParcel& reply)
213 {
214     AVSESSION_TRACE_SYNC_START("AVSessionControllerStub::HandleGetExtras");
215     AAFwk::WantParams extras;
216     int32_t ret = GetExtras(extras);
217     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
218     if (ret == AVSESSION_SUCCESS) {
219         CHECK_AND_PRINT_LOG(reply.WriteParcelable(&extras), "write title failed");
220     }
221     return ERR_NONE;
222 }
223 
HandleSendAVKeyEvent(MessageParcel & data,MessageParcel & reply)224 int32_t AVSessionControllerStub::HandleSendAVKeyEvent(MessageParcel& data, MessageParcel& reply)
225 {
226     AVSESSION_TRACE_SYNC_START("AVSessionControllerStub::SendAVKeyEvent");
227     std::shared_ptr<MMI::KeyEvent> event = MMI::KeyEvent::Create();
228     if (event == nullptr) {
229         SLOGI("malloc keyEvent failed");
230         CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_NO_MEMORY), "write SendAVKeyEvent ret failed");
231         return ERR_NONE;
232     }
233 
234     event->ReadFromParcel(data);
235     if (!event->IsValid()) {
236         CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write SendAVKeyEvent ret failed");
237     } else {
238         CHECK_AND_PRINT_LOG(reply.WriteInt32(SendAVKeyEvent(*(event.get()))),
239             "write SendAVKeyEvent ret failed");
240     }
241     return ERR_NONE;
242 }
243 
HandleGetLaunchAbility(MessageParcel & data,MessageParcel & reply)244 int32_t AVSessionControllerStub::HandleGetLaunchAbility(MessageParcel& data, MessageParcel& reply)
245 {
246     AbilityRuntime::WantAgent::WantAgent ability;
247     int32_t ret = GetLaunchAbility(ability);
248     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
249     if (ret == AVSESSION_SUCCESS) {
250         CHECK_AND_PRINT_LOG(reply.WriteParcelable(&ability), "write LaunchAbility failed");
251     }
252     return ERR_NONE;
253 }
254 
HandleGetValidCommands(MessageParcel & data,MessageParcel & reply)255 int32_t AVSessionControllerStub::HandleGetValidCommands(MessageParcel& data, MessageParcel& reply)
256 {
257     std::vector<int32_t> cmds;
258     int32_t ret = GetValidCommands(cmds);
259     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
260     if (ret == AVSESSION_SUCCESS) {
261         CHECK_AND_PRINT_LOG(reply.WriteInt32Vector(cmds), "write int32 vector failed");
262     }
263     return ERR_NONE;
264 }
265 
HandleSetAVCallMetaFilter(MessageParcel & data,MessageParcel & reply)266 int32_t AVSessionControllerStub::HandleSetAVCallMetaFilter(MessageParcel& data, MessageParcel& reply)
267 {
268     std::string str = data.ReadString();
269     if (str.length() != AVCallMetaData::AVCALL_META_KEY_MAX) {
270         CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write SetAVCallMetaFilter ret failed");
271         return ERR_NONE;
272     }
273     if (str.find_first_not_of("01") != std::string::npos) {
274         SLOGI("mask string not 0 or 1");
275         CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write int32 failed");
276         return ERR_NONE;
277     }
278     AVCallMetaData::AVCallMetaMaskType filter(str);
279     CHECK_AND_PRINT_LOG(reply.WriteInt32(SetAVCallMetaFilter(filter)), "write int32 failed");
280     return ERR_NONE;
281 }
282 
HandleSetAVCallStateFilter(MessageParcel & data,MessageParcel & reply)283 int32_t AVSessionControllerStub::HandleSetAVCallStateFilter(MessageParcel& data, MessageParcel& reply)
284 {
285     std::string str = data.ReadString();
286     if (str.length() != AVCallState::AVCALL_STATE_KEY_MAX) {
287         CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write SetAVCallStateFilter ret failed");
288         return ERR_NONE;
289     }
290     if (str.find_first_not_of("01") != std::string::npos) {
291         SLOGI("mask string not all 0 or 1");
292         CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write int32 failed");
293         return ERR_NONE;
294     }
295     AVCallState::AVCallStateMaskType filter(str);
296     CHECK_AND_PRINT_LOG(reply.WriteInt32(SetAVCallStateFilter(filter)), "write int32 failed");
297     return ERR_NONE;
298 }
299 
HandleSetMetaFilter(MessageParcel & data,MessageParcel & reply)300 int32_t AVSessionControllerStub::HandleSetMetaFilter(MessageParcel& data, MessageParcel& reply)
301 {
302     std::string str = data.ReadString();
303     if (str.length() != AVMetaData::META_KEY_MAX) {
304         CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write SetMetaFilter ret failed");
305         return ERR_NONE;
306     }
307     if (str.find_first_not_of("01") != std::string::npos) {
308         SLOGI("mask string not 0 or 1");
309         CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write int32 failed");
310         return ERR_NONE;
311     }
312     AVMetaData::MetaMaskType filter(str);
313     CHECK_AND_PRINT_LOG(reply.WriteInt32(SetMetaFilter(filter)), "write int32 failed");
314     return ERR_NONE;
315 }
316 
HandleSetPlaybackFilter(MessageParcel & data,MessageParcel & reply)317 int32_t AVSessionControllerStub::HandleSetPlaybackFilter(MessageParcel& data, MessageParcel& reply)
318 {
319     std::string str = data.ReadString();
320     if (str.length() != AVPlaybackState::PLAYBACK_KEY_MAX) {
321         CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write SetPlaybackFilter ret failed");
322         return ERR_NONE;
323     }
324     if (str.find_first_not_of("01") != std::string::npos) {
325         SLOGI("mask string not all 0 or 1");
326         CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write int32 failed");
327         return ERR_NONE;
328     }
329     AVPlaybackState::PlaybackStateMaskType filter(str);
330     CHECK_AND_PRINT_LOG(reply.WriteInt32(SetPlaybackFilter(filter)), "write int32 failed");
331     return ERR_NONE;
332 }
333 
HandleIsSessionActive(MessageParcel & data,MessageParcel & reply)334 int32_t AVSessionControllerStub::HandleIsSessionActive(MessageParcel& data, MessageParcel& reply)
335 {
336     bool isActive = false;
337     int32_t ret = IsSessionActive(isActive);
338     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
339     if (ret == AVSESSION_SUCCESS) {
340         CHECK_AND_PRINT_LOG(reply.WriteBool(isActive), "write bool failed");
341     }
342     return ERR_NONE;
343 }
344 
HandleGetSessionId(MessageParcel & data,MessageParcel & reply)345 int32_t AVSessionControllerStub::HandleGetSessionId(MessageParcel& data, MessageParcel& reply)
346 {
347     CHECK_AND_PRINT_LOG(reply.WriteString(GetSessionId()), "write int32_t failed");
348     return ERR_NONE;
349 }
350 } // namespace OHOS::AVSession