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_callback_proxy.h"
17 #include "avsession_log.h"
18 #include "avsession_errors.h"
19
20 namespace OHOS::AVSession {
AVCastControllerCallbackProxy(const sptr<IRemoteObject> & impl)21 AVCastControllerCallbackProxy::AVCastControllerCallbackProxy(const sptr<IRemoteObject>& impl)
22 : IRemoteProxy<IAVCastControllerCallback>(impl)
23 {
24 SLOGD("construct");
25 }
26
OnCastPlaybackStateChange(const AVPlaybackState & state)27 void AVCastControllerCallbackProxy::OnCastPlaybackStateChange(const AVPlaybackState& state)
28 {
29 SLOGI("OnCastPlaybackStateChange in proxy for state %{public}d", state.GetState());
30 MessageParcel parcel;
31 CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
32 CHECK_AND_RETURN_LOG(parcel.WriteParcelable(&state), "write PlaybackState failed");
33
34 MessageParcel reply;
35 MessageOption option = { MessageOption::TF_ASYNC };
36 auto remote = Remote();
37 CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
38 SLOGI("OnCastPlaybackStateChange in proxy to send request for state %{public}d", state.GetState());
39 CHECK_AND_RETURN_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_ON_CAST_PLAYBACK_STATE_CHANGE,
40 parcel, reply, option) == 0,
41 "send request failed");
42 SLOGI("OnCastPlaybackStateChange done in proxy for state %{public}d", state.GetState());
43 }
44
OnMediaItemChange(const AVQueueItem & avQueueItem)45 void AVCastControllerCallbackProxy::OnMediaItemChange(const AVQueueItem& avQueueItem)
46 {
47 SLOGI("OnMediaItemChange in proxy");
48 MessageParcel parcel;
49 parcel.SetMaxCapacity(defaultIpcCapacity);
50 CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
51 CHECK_AND_RETURN_LOG(parcel.WriteParcelable(&avQueueItem), "Write avQueueItem failed");
52
53 MessageParcel reply;
54 MessageOption option = { MessageOption::TF_ASYNC };
55 auto remote = Remote();
56 CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
57 SLOGI("OnMediaItemChange in proxy to send request");
58 CHECK_AND_RETURN_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_ON_MEDIA_ITEM_CHANGE, parcel, reply, option) == 0,
59 "send request failed");
60 SLOGI("OnMediaItemChange in proxy done");
61 }
62
OnPlayNext()63 void AVCastControllerCallbackProxy::OnPlayNext()
64 {
65 MessageParcel parcel;
66 CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
67
68 MessageParcel reply;
69 MessageOption option = { MessageOption::TF_ASYNC };
70 auto remote = Remote();
71 CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
72 CHECK_AND_RETURN_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_ON_PLAY_NEXT, parcel, reply, option) == 0,
73 "send request failed");
74 }
75
OnPlayPrevious()76 void AVCastControllerCallbackProxy::OnPlayPrevious()
77 {
78 MessageParcel parcel;
79 CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
80
81 MessageParcel reply;
82 MessageOption option = { MessageOption::TF_ASYNC };
83 auto remote = Remote();
84 CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
85 CHECK_AND_RETURN_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_ON_PLAY_PREVIOUS, parcel, reply, option) == 0,
86 "send request failed");
87 }
88
OnSeekDone(const int32_t seekNumber)89 void AVCastControllerCallbackProxy::OnSeekDone(const int32_t seekNumber)
90 {
91 MessageParcel parcel;
92 CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
93 CHECK_AND_RETURN_LOG(parcel.WriteInt32(seekNumber), "write seekNumber failed");
94
95 MessageParcel reply;
96 MessageOption option = { MessageOption::TF_ASYNC };
97 auto remote = Remote();
98 CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
99 CHECK_AND_RETURN_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_ON_SEEK_DONE, parcel, reply, option) == 0,
100 "send request failed");
101 }
102
OnVideoSizeChange(const int32_t width,const int32_t height)103 void AVCastControllerCallbackProxy::OnVideoSizeChange(const int32_t width, const int32_t height)
104 {
105 MessageParcel parcel;
106 CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
107 CHECK_AND_RETURN_LOG(parcel.WriteInt32(width), "write width failed");
108 CHECK_AND_RETURN_LOG(parcel.WriteInt32(height), "write height failed");
109
110 MessageParcel reply;
111 MessageOption option = { MessageOption::TF_ASYNC };
112 auto remote = Remote();
113 CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
114 CHECK_AND_RETURN_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_ON_VIDEO_SIZE_CHANGE, parcel, reply, option) == 0,
115 "send request failed");
116 }
117
OnPlayerError(const int32_t errorCode,const std::string & errorMsg)118 void AVCastControllerCallbackProxy::OnPlayerError(const int32_t errorCode, const std::string& errorMsg)
119 {
120 MessageParcel parcel;
121 CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
122 CHECK_AND_RETURN_LOG(parcel.WriteInt32(errorCode), "write time failed");
123 CHECK_AND_RETURN_LOG(parcel.WriteString(errorMsg), "write time failed");
124
125 MessageParcel reply;
126 MessageOption option = { MessageOption::TF_ASYNC };
127 auto remote = Remote();
128 CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
129 CHECK_AND_RETURN_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_ON_ERROR, parcel, reply, option) == 0,
130 "send request failed");
131 }
132
OnEndOfStream(const int32_t isLooping)133 void AVCastControllerCallbackProxy::OnEndOfStream(const int32_t isLooping)
134 {
135 MessageParcel parcel;
136 CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
137 CHECK_AND_RETURN_LOG(parcel.WriteInt32(isLooping), "write isLooping failed");
138
139 MessageParcel reply;
140 MessageOption option = { MessageOption::TF_ASYNC };
141 auto remote = Remote();
142 CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
143 CHECK_AND_RETURN_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_ON_END_OF_STREAM, parcel, reply, option) == 0,
144 "send request failed");
145 }
146
OnPlayRequest(const AVQueueItem & avQueueItem)147 void AVCastControllerCallbackProxy::OnPlayRequest(const AVQueueItem& avQueueItem)
148 {
149 SLOGI("OnPlayRequest in proxy");
150 MessageParcel parcel;
151 CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
152 CHECK_AND_RETURN_LOG(parcel.WriteParcelable(&avQueueItem), "Write avQueueItem failed");
153
154 MessageParcel reply;
155 MessageOption option = { MessageOption::TF_ASYNC };
156 auto remote = Remote();
157 CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
158 SLOGI("OnPlayRequest in proxy to send request");
159 CHECK_AND_RETURN_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_ON_PLAY_REQUEST, parcel, reply, option) == 0,
160 "send request failed");
161 SLOGI("OnPlayRequest in proxy done");
162 }
163
OnKeyRequest(const std::string & assetId,const std::vector<uint8_t> & keyRequestData)164 void AVCastControllerCallbackProxy::OnKeyRequest(const std::string &assetId, const std::vector<uint8_t> &keyRequestData)
165 {
166 SLOGI("OnKeyRequest in proxy");
167 MessageParcel parcel;
168 MessageParcel reply;
169 MessageOption option = { MessageOption::TF_ASYNC };
170 auto len = keyRequestData.size();
171 if (len > parcel.GetDataCapacity()) {
172 SLOGI("OnKeyRequest SetDataCapacity totalSize: %{public}d", static_cast<int>(len));
173 parcel.SetMaxCapacity(len + len);
174 parcel.SetDataCapacity(len);
175 }
176 CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
177 CHECK_AND_RETURN_LOG(parcel.WriteString(assetId), "write assetId failed");
178 CHECK_AND_RETURN_LOG(parcel.WriteInt32(keyRequestData.size()), "Write keyRequestData failed");
179 if (keyRequestData.size() != 0) {
180 if (!parcel.WriteBuffer(keyRequestData.data(), keyRequestData.size())) {
181 SLOGE("AVCastControllerCallbackProxy OnKeyRequest write keyRequestData failed");
182 return;
183 }
184 }
185 auto remote = Remote();
186 CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
187 SLOGI("OnKeyRequest in proxy to send request");
188 CHECK_AND_RETURN_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_ON_KEY_REQUEST, parcel, reply, option) == 0,
189 "send request failed");
190 SLOGI("OnKeyRequest in proxy done");
191 }
192
OnCastValidCommandChanged(const std::vector<int32_t> & cmds)193 void AVCastControllerCallbackProxy::OnCastValidCommandChanged(const std::vector<int32_t> &cmds)
194 {
195 SLOGI("OnCastValidCommandChanged in proxy");
196 MessageParcel parcel;
197 CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
198 CHECK_AND_RETURN_LOG(parcel.WriteInt32Vector(cmds), "Write vector failed");
199
200 MessageParcel reply;
201 MessageOption option = { MessageOption::TF_ASYNC };
202 auto remote = Remote();
203 CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
204 SLOGI("OnCastValidCommandChanged in proxy to send request");
205 CHECK_AND_RETURN_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_ON_VALID_COMMAND_CHANGED, parcel, reply, option) == 0,
206 "send request failed");
207 SLOGI("OnCastValidCommandChanged in proxy done");
208 }
209
onDataSrcRead(std::shared_ptr<AVSharedMemory> mem,uint32_t length,int64_t pos)210 int32_t AVCastControllerCallbackProxy::onDataSrcRead(std::shared_ptr<AVSharedMemory> mem,
211 uint32_t length, int64_t pos)
212 {
213 SLOGI("OnDataSrcRead in proxy");
214 MessageParcel parcel;
215 CHECK_AND_RETURN_RET_LOG(parcel.WriteInterfaceToken(GetDescriptor()), 0, "write interface token failed");
216 CHECK_AND_RETURN_RET_LOG(WriteAVSharedMemoryToParcel(mem, parcel) == AVSESSION_SUCCESS, 0, "write mem failed");
217 CHECK_AND_RETURN_RET_LOG(parcel.WriteInt32(length), 0, "Write length failed");
218 CHECK_AND_RETURN_RET_LOG(parcel.WriteInt64(pos), 0, "Write pos failed");
219
220 MessageParcel reply;
221 MessageOption option = { MessageOption::TF_SYNC };
222 auto remote = Remote();
223 CHECK_AND_RETURN_RET_LOG(remote != nullptr, 0, "get remote service failed");
224 SLOGI("OnDataSrcRead in proxy to send request");
225 CHECK_AND_RETURN_RET_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_ON_DATA_SRC_READ, parcel, reply, option) == 0,
226 0, "send request failed");
227 SLOGI("OnDataSrcRead in proxy done");
228 return reply.ReadInt32();
229 }
230 } // namespace OHOS::AVSession
231