• 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 
HandleGetAVPlaybackState(MessageParcel & data,MessageParcel & reply)63 int32_t AVSessionControllerStub::HandleGetAVPlaybackState(MessageParcel& data, MessageParcel& reply)
64 {
65     AVPlaybackState state;
66     int32_t ret = GetAVPlaybackState(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 AVPlaybackState failed");
70     }
71     return ERR_NONE;
72 }
73 
HandleSendControlCommand(MessageParcel & data,MessageParcel & reply)74 int32_t AVSessionControllerStub::HandleSendControlCommand(MessageParcel& data, MessageParcel& reply)
75 {
76     AVSESSION_TRACE_SYNC_START("AVSessionControllerStub::SendControlCommand");
77     sptr<AVControlCommand> cmd = data.ReadParcelable<AVControlCommand>();
78     if (cmd == nullptr) {
79         CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write SendCommand ret failed");
80     } else {
81         CHECK_AND_PRINT_LOG(reply.WriteInt32(SendControlCommand(*cmd)), "write SendCommand ret failed");
82     }
83     return ERR_NONE;
84 }
85 
HandleSendCommonCommand(MessageParcel & data,MessageParcel & reply)86 int32_t AVSessionControllerStub::HandleSendCommonCommand(MessageParcel& data, MessageParcel& reply)
87 {
88     AVSESSION_TRACE_SYNC_START("AVSessionControllerStub::SendCommonCommand");
89     auto commonCommand = data.ReadString();
90     sptr commandArgs = data.ReadParcelable<AAFwk::WantParams>();
91     if (commandArgs == nullptr) {
92         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "WriteInt32 result failed");
93         return ERR_NONE;
94     }
95     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(SendCommonCommand(commonCommand, *commandArgs)),
96         ERR_NONE, "WriteInt32 result failed");
97     return ERR_NONE;
98 }
99 
HandleGetAVMetaData(MessageParcel & data,MessageParcel & reply)100 int32_t AVSessionControllerStub::HandleGetAVMetaData(MessageParcel& data, MessageParcel& reply)
101 {
102     AVMetaData metaData;
103     int32_t ret = GetAVMetaData(metaData);
104     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
105     if (ret == AVSESSION_SUCCESS) {
106         CHECK_AND_PRINT_LOG(reply.WriteParcelable(&metaData), "write AVMetaData failed");
107     }
108     return ERR_NONE;
109 }
110 
HandleGetAVQueueItems(MessageParcel & data,MessageParcel & reply)111 int32_t AVSessionControllerStub::HandleGetAVQueueItems(MessageParcel& data, MessageParcel& reply)
112 {
113     std::vector<AVQueueItem> items;
114     int32_t ret = GetAVQueueItems(items);
115     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
116     if (ret == AVSESSION_SUCCESS) {
117         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(items.size()), ERR_NONE, "write items num int32 failed");
118         for (auto &parcelable : items) {
119             CHECK_AND_RETURN_RET_LOG(reply.WriteParcelable(&parcelable), ERR_NONE, "Write items failed");
120         }
121     }
122     return ERR_NONE;
123 }
124 
HandleGetAVQueueTitle(MessageParcel & data,MessageParcel & reply)125 int32_t AVSessionControllerStub::HandleGetAVQueueTitle(MessageParcel& data, MessageParcel& reply)
126 {
127     std::string title;
128     int32_t ret = GetAVQueueTitle(title);
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.WriteString(title), "write title failed");
132     }
133     return ERR_NONE;
134 }
135 
HandleSkipToQueueItem(MessageParcel & data,MessageParcel & reply)136 int32_t AVSessionControllerStub::HandleSkipToQueueItem(MessageParcel& data, MessageParcel& reply)
137 {
138     int32_t itemId;
139     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(itemId), ERR_NONE, "read itemId int32 failed");
140     int32_t ret = SkipToQueueItem(itemId);
141     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
142     return ERR_NONE;
143 }
144 
HandleGetExtras(MessageParcel & data,MessageParcel & reply)145 int32_t AVSessionControllerStub::HandleGetExtras(MessageParcel& data, MessageParcel& reply)
146 {
147     AVSESSION_TRACE_SYNC_START("AVSessionControllerStub::HandleGetExtras");
148     AAFwk::WantParams extras;
149     int32_t ret = GetExtras(extras);
150     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
151     if (ret == AVSESSION_SUCCESS) {
152         CHECK_AND_PRINT_LOG(reply.WriteParcelable(&extras), "write title failed");
153     }
154     return ERR_NONE;
155 }
156 
HandleSendAVKeyEvent(MessageParcel & data,MessageParcel & reply)157 int32_t AVSessionControllerStub::HandleSendAVKeyEvent(MessageParcel& data, MessageParcel& reply)
158 {
159     AVSESSION_TRACE_SYNC_START("AVSessionControllerStub::SendAVKeyEvent");
160     std::shared_ptr<MMI::KeyEvent> event = MMI::KeyEvent::Create();
161     if (event == nullptr) {
162         SLOGI("malloc keyEvent failed");
163         CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_NO_MEMORY), "write SendAVKeyEvent ret failed");
164         return ERR_NONE;
165     }
166 
167     event->ReadFromParcel(data);
168     if (!event->IsValid()) {
169         CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write SendAVKeyEvent ret failed");
170     } else {
171         CHECK_AND_PRINT_LOG(reply.WriteInt32(SendAVKeyEvent(*(event.get()))),
172             "write SendAVKeyEvent ret failed");
173     }
174     return ERR_NONE;
175 }
176 
HandleGetLaunchAbility(MessageParcel & data,MessageParcel & reply)177 int32_t AVSessionControllerStub::HandleGetLaunchAbility(MessageParcel& data, MessageParcel& reply)
178 {
179     AbilityRuntime::WantAgent::WantAgent ability;
180     int32_t ret = GetLaunchAbility(ability);
181     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
182     if (ret == AVSESSION_SUCCESS) {
183         CHECK_AND_PRINT_LOG(reply.WriteParcelable(&ability), "write LaunchAbility failed");
184     }
185     return ERR_NONE;
186 }
187 
HandleGetValidCommands(MessageParcel & data,MessageParcel & reply)188 int32_t AVSessionControllerStub::HandleGetValidCommands(MessageParcel& data, MessageParcel& reply)
189 {
190     std::vector<int32_t> cmds;
191     int32_t ret = GetValidCommands(cmds);
192     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
193     if (ret == AVSESSION_SUCCESS) {
194         CHECK_AND_PRINT_LOG(reply.WriteInt32Vector(cmds), "write int32 vector failed");
195     }
196     return ERR_NONE;
197 }
198 
HandleSetMetaFilter(MessageParcel & data,MessageParcel & reply)199 int32_t AVSessionControllerStub::HandleSetMetaFilter(MessageParcel& data, MessageParcel& reply)
200 {
201     std::string str = data.ReadString();
202     if (str.length() != AVMetaData::META_KEY_MAX) {
203         CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write SetMetaFilter ret failed");
204         return ERR_NONE;
205     }
206     if (str.find_first_not_of("01") != std::string::npos) {
207         SLOGI("mask string not 0 or 1");
208         CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write int32 failed");
209         return ERR_NONE;
210     }
211     AVMetaData::MetaMaskType filter(str);
212     CHECK_AND_PRINT_LOG(reply.WriteInt32(SetMetaFilter(filter)), "write int32 failed");
213     return ERR_NONE;
214 }
215 
HandleSetPlaybackFilter(MessageParcel & data,MessageParcel & reply)216 int32_t AVSessionControllerStub::HandleSetPlaybackFilter(MessageParcel& data, MessageParcel& reply)
217 {
218     std::string str = data.ReadString();
219     if (str.length() != AVPlaybackState::PLAYBACK_KEY_MAX) {
220         CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write SetPlaybackFilter ret failed");
221         return ERR_NONE;
222     }
223     if (str.find_first_not_of("01") != std::string::npos) {
224         SLOGI("mask string not all 0 or 1");
225         CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write int32 failed");
226         return ERR_NONE;
227     }
228     AVPlaybackState::PlaybackStateMaskType filter(str);
229     CHECK_AND_PRINT_LOG(reply.WriteInt32(SetPlaybackFilter(filter)), "write int32 failed");
230     return ERR_NONE;
231 }
232 
HandleIsSessionActive(MessageParcel & data,MessageParcel & reply)233 int32_t AVSessionControllerStub::HandleIsSessionActive(MessageParcel& data, MessageParcel& reply)
234 {
235     bool isActive = false;
236     int32_t ret = IsSessionActive(isActive);
237     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
238     if (ret == AVSESSION_SUCCESS) {
239         CHECK_AND_PRINT_LOG(reply.WriteBool(isActive), "write bool failed");
240     }
241     return ERR_NONE;
242 }
243 
HandleGetSessionId(MessageParcel & data,MessageParcel & reply)244 int32_t AVSessionControllerStub::HandleGetSessionId(MessageParcel& data, MessageParcel& reply)
245 {
246     CHECK_AND_PRINT_LOG(reply.WriteString(GetSessionId()), "write int32_t failed");
247     return ERR_NONE;
248 }
249 } // namespace OHOS::AVSession