• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_proxy.h"
17 #include "avcontroller_callback_client.h"
18 #include "avsession_errors.h"
19 #include "avsession_log.h"
20 #include "avsession_trace.h"
21 
22 namespace OHOS::AVSession {
AVSessionControllerProxy(const sptr<IRemoteObject> & impl)23 AVSessionControllerProxy::AVSessionControllerProxy(const sptr<IRemoteObject>& impl)
24     : IRemoteProxy<IAVSessionController>(impl)
25 {
26     SLOGD("construct");
27 }
28 
~AVSessionControllerProxy()29 AVSessionControllerProxy::~AVSessionControllerProxy()
30 {
31     SLOGI("destroy");
32     Destroy();
33 }
34 
GetAVPlaybackState(AVPlaybackState & state)35 int32_t AVSessionControllerProxy::GetAVPlaybackState(AVPlaybackState& state)
36 {
37     CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
38     MessageParcel parcel;
39     CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
40         "write interface token failed");
41 
42     auto remote = Remote();
43     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
44     MessageParcel reply;
45     MessageOption option;
46     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_GET_AV_PLAYBACK_STATE, parcel, reply, option) == 0,
47         ERR_IPC_SEND_REQUEST, "send request failed");
48 
49     int32_t ret = AVSESSION_ERROR;
50     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
51     if (ret == AVSESSION_SUCCESS) {
52         sptr<AVPlaybackState> state_ = reply.ReadParcelable<AVPlaybackState>();
53         CHECK_AND_RETURN_RET_LOG(state_ != nullptr, ERR_UNMARSHALLING, "read AVPlaybackState failed");
54         state = *state_;
55         currentState_ = *state_;
56     }
57     return ret;
58 }
59 
GetAVMetaData(AVMetaData & data)60 int32_t AVSessionControllerProxy::GetAVMetaData(AVMetaData& data)
61 {
62     CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
63     MessageParcel parcel;
64     CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
65         "write interface token failed");
66 
67     auto remote = Remote();
68     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
69     MessageParcel reply;
70     MessageOption option;
71     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_GET_AV_META_DATA, parcel, reply, option) == 0,
72         ERR_IPC_SEND_REQUEST, "send request failed");
73 
74     int32_t ret = AVSESSION_ERROR;
75     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
76     if (ret == AVSESSION_SUCCESS) {
77         sptr<AVMetaData> data_ = reply.ReadParcelable<AVMetaData>();
78         CHECK_AND_RETURN_RET_LOG(data_ != nullptr, ERR_UNMARSHALLING, "read AVMetaData failed");
79         data = *data_;
80     }
81     return ret;
82 }
83 
SendAVKeyEvent(const MMI::KeyEvent & keyEvent)84 int32_t AVSessionControllerProxy::SendAVKeyEvent(const MMI::KeyEvent& keyEvent)
85 {
86     AVSESSION_TRACE_SYNC_START("AVSessionControllerProxy::SendAVKeyEvent");
87     CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
88     CHECK_AND_RETURN_RET_LOG(keyEvent.IsValid(), ERR_COMMAND_NOT_SUPPORT, "keyEvent not valid");
89     bool isActive {};
90     CHECK_AND_RETURN_RET_LOG(IsSessionActive(isActive) == AVSESSION_SUCCESS &&
91         isActive, ERR_SESSION_DEACTIVE, "session is deactivate");
92 
93     MessageParcel parcel;
94     CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
95         "write interface token failed");
96     CHECK_AND_RETURN_RET_LOG(keyEvent.WriteToParcel(parcel), ERR_MARSHALLING, "write keyEvent failed");
97 
98     auto remote = Remote();
99     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
100     MessageParcel reply;
101     MessageOption option;
102     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_SEND_AV_KEYEVENT, parcel, reply, option) == 0,
103         ERR_IPC_SEND_REQUEST, "send request failed");
104 
105     int32_t ret = AVSESSION_ERROR;
106     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
107 }
108 
GetLaunchAbility(AbilityRuntime::WantAgent::WantAgent & ability)109 int32_t AVSessionControllerProxy::GetLaunchAbility(AbilityRuntime::WantAgent::WantAgent& ability)
110 {
111     CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
112     MessageParcel parcel;
113     CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
114         "write interface token failed");
115 
116     auto remote = Remote();
117     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
118     MessageParcel reply;
119     MessageOption option;
120     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_GET_LAUNCH_ABILITY, parcel, reply, option) == 0,
121         ERR_IPC_SEND_REQUEST, "send request failed");
122 
123     int32_t ret = AVSESSION_ERROR;
124     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
125     if (ret == AVSESSION_SUCCESS) {
126         sptr<AbilityRuntime::WantAgent::WantAgent> ability_ =
127             reply.ReadParcelable<AbilityRuntime::WantAgent::WantAgent>();
128         CHECK_AND_RETURN_RET_LOG(ability_ != nullptr, ERR_UNMARSHALLING, "read LaunchAbility failed");
129         ability = *ability_;
130     }
131     return ret;
132 }
133 
GetValidCommands(std::vector<int32_t> & cmds)134 int32_t AVSessionControllerProxy::GetValidCommands(std::vector<int32_t>& cmds)
135 {
136     CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
137     MessageParcel parcel;
138     CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
139         "write interface token failed");
140 
141     auto remote = Remote();
142     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
143     MessageParcel reply;
144     MessageOption option;
145     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_GET_VALID_COMMANDS, parcel, reply, option) == 0,
146         ERR_IPC_SEND_REQUEST, "send request failed");
147 
148     int32_t ret = AVSESSION_ERROR;
149     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
150     if (ret == AVSESSION_SUCCESS) {
151         CHECK_AND_RETURN_RET_LOG(reply.ReadInt32Vector(&cmds), ERR_UNMARSHALLING, "read int32 vector failed");
152     }
153     return ret;
154 }
155 
IsSessionActive(bool & isActive)156 int32_t AVSessionControllerProxy::IsSessionActive(bool& isActive)
157 {
158     CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
159     MessageParcel parcel;
160     CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
161         "write interface token failed");
162 
163     auto remote = Remote();
164     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
165     MessageParcel reply;
166     MessageOption option;
167     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_IS_SESSION_ACTIVE, parcel, reply, option) == 0,
168         ERR_IPC_SEND_REQUEST, "send request failed");
169 
170     int32_t ret = AVSESSION_ERROR;
171     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
172     if (ret == AVSESSION_SUCCESS) {
173         CHECK_AND_RETURN_RET_LOG(reply.ReadBool(isActive), ERR_UNMARSHALLING, "read bool failed");
174     }
175     return ret;
176 }
177 
SendControlCommand(const AVControlCommand & cmd)178 int32_t AVSessionControllerProxy::SendControlCommand(const AVControlCommand& cmd)
179 {
180     AVSESSION_TRACE_SYNC_START("AVSessionControllerProxy::SendControlCommand");
181     CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
182     CHECK_AND_RETURN_RET_LOG(cmd.IsValid(), ERR_COMMAND_NOT_SUPPORT, "command not valid");
183     bool isActive {};
184     CHECK_AND_RETURN_RET_LOG(IsSessionActive(isActive) == AVSESSION_SUCCESS &&
185         isActive, ERR_SESSION_DEACTIVE, "session is deactivate");
186     MessageParcel parcel;
187     CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
188         "write interface token failed");
189     CHECK_AND_RETURN_RET_LOG(parcel.WriteParcelable(&cmd), ERR_MARSHALLING, "write cmd failed");
190 
191     auto remote = Remote();
192     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
193     MessageParcel reply;
194     MessageOption option;
195     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_SEND_CONTROL_COMMAND, parcel, reply, option) == 0,
196         ERR_IPC_SEND_REQUEST, "send request failed");
197 
198     int32_t ret = AVSESSION_ERROR;
199     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
200 }
201 
SetMetaFilter(const AVMetaData::MetaMaskType & filter)202 int32_t AVSessionControllerProxy::SetMetaFilter(const AVMetaData::MetaMaskType& filter)
203 {
204     CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
205     MessageParcel parcel;
206     CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
207         "write interface token failed");
208     CHECK_AND_RETURN_RET_LOG(parcel.WriteString(filter.to_string()), ERR_MARSHALLING, "write filter failed");
209 
210     auto remote = Remote();
211     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
212     MessageParcel reply;
213     MessageOption option;
214     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_SET_META_FILTER, parcel, reply, option) == 0,
215         ERR_IPC_SEND_REQUEST, "send request failed");
216 
217     int32_t ret = AVSESSION_ERROR;
218     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
219 }
220 
SetPlaybackFilter(const AVPlaybackState::PlaybackStateMaskType & filter)221 int32_t AVSessionControllerProxy::SetPlaybackFilter(const AVPlaybackState::PlaybackStateMaskType& filter)
222 {
223     CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
224     MessageParcel parcel;
225     CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
226         "write interface token failed");
227     CHECK_AND_RETURN_RET_LOG(parcel.WriteString(filter.to_string()), ERR_MARSHALLING, "write filter failed");
228 
229     auto remote = Remote();
230     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
231     MessageParcel reply;
232     MessageOption option;
233     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_SET_PLAYBACK_FILTER, parcel, reply, option) == 0,
234         ERR_IPC_SEND_REQUEST, "send request failed");
235 
236     int32_t ret = AVSESSION_ERROR;
237     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
238 }
239 
RegisterCallback(const std::shared_ptr<AVControllerCallback> & callback)240 int32_t AVSessionControllerProxy::RegisterCallback(const std::shared_ptr<AVControllerCallback>& callback)
241 {
242     CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
243 
244     sptr<AVControllerCallbackClient> callback_;
245     callback_ = new(std::nothrow) AVControllerCallbackClient(callback);
246     CHECK_AND_RETURN_RET_LOG(callback_ != nullptr, ERR_NO_MEMORY, "new AVControllerCallbackClient failed");
247 
248     callback_->AddListenerForPlaybackState([this](const AVPlaybackState& state) { currentState_ = state; });
249 
250     return RegisterCallbackInner(callback_);
251 }
252 
RegisterCallbackInner(const sptr<IRemoteObject> & callback)253 int32_t AVSessionControllerProxy::RegisterCallbackInner(const sptr<IRemoteObject>& callback)
254 {
255     MessageParcel parcel;
256     CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
257         "write interface token failed");
258     CHECK_AND_RETURN_RET_LOG(parcel.WriteRemoteObject(callback), ERR_MARSHALLING,
259         "write remote object failed");
260 
261     auto remote = Remote();
262     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
263     MessageParcel reply;
264     MessageOption option;
265     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_REGISTER_CALLBACK, parcel, reply, option) == 0,
266         ERR_IPC_SEND_REQUEST, "send request failed");
267 
268     int32_t ret = AVSESSION_ERROR;
269     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
270 }
271 
Destroy()272 int32_t AVSessionControllerProxy::Destroy()
273 {
274     CHECK_AND_RETURN_RET_LOG(!isDestroy_, ERR_CONTROLLER_NOT_EXIST, "controller is destroy");
275     MessageParcel parcel;
276     CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
277         "write interface token failed");
278 
279     auto remote = Remote();
280     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
281     MessageParcel reply;
282     MessageOption option;
283     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_DESTROY, parcel, reply, option) == 0,
284         ERR_IPC_SEND_REQUEST, "send request failed");
285     isDestroy_ = true;
286 
287     int32_t ret = AVSESSION_ERROR;
288     return reply.ReadInt32(ret) ? ret : AVSESSION_ERROR;
289 }
290 
GetSessionId()291 std::string AVSessionControllerProxy::GetSessionId()
292 {
293     CHECK_AND_RETURN_RET_LOG(!isDestroy_, "", "controller is destroy");
294     MessageParcel parcel;
295     CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "", "write interface token failed");
296 
297     auto remote = Remote();
298     CHECK_AND_RETURN_RET_LOG(remote != nullptr, "", "get remote service failed");
299     MessageParcel reply;
300     MessageOption option;
301     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CONTROLLER_CMD_GET_SESSION_ID, parcel, reply, option) == 0,
302         "", "send request failed");
303 
304     std::string result;
305     return reply.ReadString(result) ? result : "";
306 }
307 
GetRealPlaybackPosition()308 int64_t AVSessionControllerProxy::GetRealPlaybackPosition()
309 {
310     auto position = currentState_.GetPosition();
311     CHECK_AND_RETURN_RET_LOG(position.updateTime_ > 0, 0, "playbackState not update");
312     auto now = std::chrono::system_clock::now();
313     auto nowMS = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
314 
315     int64_t currentSysTime = nowMS.time_since_epoch().count();
316     SLOGI("elapsedTime:%{public}" PRId64 ", currentSysTime:%{public}" PRId64 ", updateTime:%{public}" PRId64,
317           position.elapsedTime_, currentSysTime, position.updateTime_);
318 
319     return (position.elapsedTime_ + (currentSysTime - position.updateTime_));
320 }
321 
IsDestroy()322 bool AVSessionControllerProxy::IsDestroy()
323 {
324     return isDestroy_;
325 }
326 }