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 "avcontroller_callback_proxy.h"
17 #include "avsession_log.h"
18
19 namespace OHOS::AVSession {
AVControllerCallbackProxy(const sptr<IRemoteObject> & impl)20 AVControllerCallbackProxy::AVControllerCallbackProxy(const sptr<IRemoteObject>& impl)
21 : IRemoteProxy<IAVControllerCallback>(impl)
22 {
23 SLOGD("construct");
24 }
25
OnSessionDestroy()26 void AVControllerCallbackProxy::OnSessionDestroy()
27 {
28 MessageParcel parcel;
29 CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
30
31 MessageParcel reply;
32 MessageOption option = { MessageOption::TF_ASYNC };
33 auto remote = Remote();
34 CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
35 CHECK_AND_RETURN_LOG(remote->SendRequest(CONTROLLER_CMD_ON_SESSION_DESTROY, parcel, reply, option) == 0,
36 "send request failed");
37 }
38
OnAVCallMetaDataChange(const AVCallMetaData & data)39 void AVControllerCallbackProxy::OnAVCallMetaDataChange(const AVCallMetaData& data)
40 {
41 MessageParcel parcel;
42 CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
43 CHECK_AND_RETURN_LOG(parcel.WriteParcelable(&data), "write AVCallMetaData failed");
44
45 MessageParcel reply;
46 MessageOption option = { MessageOption::TF_ASYNC };
47 auto remote = Remote();
48 CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
49 CHECK_AND_RETURN_LOG(remote->SendRequest(CONTROLLER_CMD_ON_AVCALL_METADATA_CHANGE, parcel, reply, option) == 0,
50 "send request failed");
51 }
52
53
OnAVCallStateChange(const AVCallState & state)54 void AVControllerCallbackProxy::OnAVCallStateChange(const AVCallState& state)
55 {
56 MessageParcel parcel;
57 CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
58 CHECK_AND_RETURN_LOG(parcel.WriteParcelable(&state), "write AVCallState failed");
59
60 MessageParcel reply;
61 MessageOption option = { MessageOption::TF_ASYNC };
62 auto remote = Remote();
63 CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
64 CHECK_AND_RETURN_LOG(remote->SendRequest(CONTROLLER_CMD_ON_AVCALL_STATE_CHANGE, parcel, reply, option) == 0,
65 "send request failed");
66 }
67
68
OnPlaybackStateChange(const AVPlaybackState & state)69 void AVControllerCallbackProxy::OnPlaybackStateChange(const AVPlaybackState& state)
70 {
71 MessageParcel parcel;
72 CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
73 CHECK_AND_RETURN_LOG(parcel.WriteParcelable(&state), "write PlaybackState failed");
74
75 MessageParcel reply;
76 MessageOption option = { MessageOption::TF_ASYNC };
77 auto remote = Remote();
78 CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
79 CHECK_AND_RETURN_LOG(remote->SendRequest(CONTROLLER_CMD_ON_PLAYBACK_STATE_CHANGE, parcel, reply, option) == 0,
80 "send request failed");
81 }
82
GetPixelMapBuffer(AVMetaData & metaData,MessageParcel & parcel)83 int32_t AVControllerCallbackProxy::GetPixelMapBuffer(AVMetaData& metaData, MessageParcel& parcel)
84 {
85 int mediaImageLength = 0;
86 std::vector<uint8_t> mediaImageBuffer;
87 std::shared_ptr<AVSessionPixelMap> mediaPixelMap = metaData.GetMediaImage();
88 if (mediaPixelMap != nullptr) {
89 mediaImageBuffer = mediaPixelMap->GetInnerImgBuffer();
90 mediaImageLength = static_cast<int>(mediaImageBuffer.size());
91 metaData.SetMediaLength(mediaImageLength);
92 }
93
94 int avQueueImageLength = 0;
95 std::vector<uint8_t> avQueueImageBuffer;
96 std::shared_ptr<AVSessionPixelMap> avQueuePixelMap = metaData.GetAVQueueImage();
97 if (avQueuePixelMap != nullptr) {
98 avQueueImageBuffer = avQueuePixelMap->GetInnerImgBuffer();
99 avQueueImageLength = static_cast<int>(avQueueImageBuffer.size());
100 metaData.SetAVQueueLength(avQueueImageLength);
101 }
102
103 int twoImageLength = mediaImageLength + avQueueImageLength;
104 if (twoImageLength == 0) {
105 return 0;
106 }
107
108 unsigned char *buffer = new (std::nothrow) unsigned char[twoImageLength];
109 if (buffer == nullptr) {
110 SLOGE("new buffer failed of length = %{public}d", twoImageLength);
111 return -1;
112 }
113
114 for (int i = 0; i < mediaImageLength; i++) {
115 buffer[i] = mediaImageBuffer[i];
116 }
117
118 for (int j = mediaImageLength, k = 0; j < twoImageLength && k < avQueueImageLength; j++, k++) {
119 buffer[j] = avQueueImageBuffer[k];
120 }
121
122 if (!parcel.WriteInt32(twoImageLength) || !AVMetaData::MarshallingExceptImg(parcel, metaData) ||
123 !parcel.WriteRawData(buffer, twoImageLength)) {
124 SLOGE("fail to write parcel");
125 delete[] buffer;
126 return -1;
127 }
128 delete[] buffer;
129 return twoImageLength;
130 }
131
OnMetaDataChange(const AVMetaData & data)132 void AVControllerCallbackProxy::OnMetaDataChange(const AVMetaData& data)
133 {
134 MessageParcel parcel;
135 CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
136
137 MessageParcel reply;
138 MessageOption option = { MessageOption::TF_ASYNC };
139 auto remote = Remote();
140 CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
141
142 AVMetaData metaData;
143 CHECK_AND_RETURN_LOG(metaData.CopyFrom(data), "avmetadata CopyFrom error");
144
145 int twoImageLength = GetPixelMapBuffer(metaData, parcel);
146 if (twoImageLength == 0) {
147 CHECK_AND_RETURN_LOG(parcel.WriteInt32(twoImageLength), "write twoImageLength failed");
148 CHECK_AND_RETURN_LOG(parcel.WriteParcelable(&data), "write AVMetaData failed");
149 CHECK_AND_RETURN_LOG(remote->SendRequest(CONTROLLER_CMD_ON_METADATA_CHANGE, parcel, reply, option) == 0,
150 "send request failed");
151 return;
152 }
153 if (twoImageLength == -1) {
154 SLOGE("fail to write parcel");
155 return;
156 }
157
158 CHECK_AND_RETURN_LOG(remote->SendRequest(CONTROLLER_CMD_ON_METADATA_CHANGE, parcel, reply, option) == 0,
159 "send request failed");
160 }
161
OnActiveStateChange(bool isActive)162 void AVControllerCallbackProxy::OnActiveStateChange(bool isActive)
163 {
164 MessageParcel parcel;
165 CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
166 CHECK_AND_RETURN_LOG(parcel.WriteBool(isActive), "write bool failed");
167
168 MessageParcel reply;
169 MessageOption option = { MessageOption::TF_ASYNC };
170 auto remote = Remote();
171 CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
172 CHECK_AND_RETURN_LOG(remote->SendRequest(CONTROLLER_CMD_ON_ACTIVE_STATE_CHANGE, parcel, reply, option) == 0,
173 "send request failed");
174 }
175
OnValidCommandChange(const std::vector<int32_t> & cmds)176 void AVControllerCallbackProxy::OnValidCommandChange(const std::vector<int32_t>& cmds)
177 {
178 MessageParcel parcel;
179 CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
180 CHECK_AND_RETURN_LOG(parcel.WriteInt32Vector(cmds), "write int32 vector failed");
181
182 MessageParcel reply;
183 MessageOption option = { MessageOption::TF_ASYNC };
184 auto remote = Remote();
185 CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
186 CHECK_AND_RETURN_LOG(remote->SendRequest(CONTROLLER_CMD_ON_VALID_COMMAND_CHANGE, parcel, reply, option) == 0,
187 "send request failed");
188 }
189
OnOutputDeviceChange(const int32_t connectionState,const OutputDeviceInfo & outputDeviceInfo)190 void AVControllerCallbackProxy::OnOutputDeviceChange(const int32_t connectionState,
191 const OutputDeviceInfo& outputDeviceInfo)
192 {
193 MessageParcel parcel;
194 CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
195 CHECK_AND_RETURN_LOG(parcel.WriteInt32(connectionState), "write connectionState failed");
196
197 int32_t deviceInfoSize = static_cast<int32_t>(outputDeviceInfo.deviceInfos_.size());
198 CHECK_AND_RETURN_LOG(parcel.WriteInt32(deviceInfoSize), "write deviceInfoSize failed");
199 for (DeviceInfo deviceInfo : outputDeviceInfo.deviceInfos_) {
200 CHECK_AND_RETURN_LOG(parcel.WriteInt32(deviceInfo.castCategory_), "write castCategory failed");
201 CHECK_AND_RETURN_LOG(parcel.WriteString(deviceInfo.deviceId_), "write deviceId failed");
202 CHECK_AND_RETURN_LOG(parcel.WriteString(deviceInfo.deviceName_), "write deviceName failed");
203 CHECK_AND_RETURN_LOG(parcel.WriteInt32(deviceInfo.deviceType_), "write deviceType failed");
204 CHECK_AND_RETURN_LOG(parcel.WriteString(deviceInfo.ipAddress_), "write ipAddress failed");
205 CHECK_AND_RETURN_LOG(parcel.WriteInt32(deviceInfo.providerId_), "write providerId failed");
206 CHECK_AND_RETURN_LOG(parcel.WriteInt32(deviceInfo.supportedProtocols_),
207 "write supportedProtocols failed");
208 CHECK_AND_RETURN_LOG(parcel.WriteInt32(deviceInfo.authenticationStatus_),
209 "write authenticationStatus failed");
210 }
211
212 MessageParcel reply;
213 MessageOption option = { MessageOption::TF_ASYNC };
214 auto remote = Remote();
215 CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
216 CHECK_AND_RETURN_LOG(remote->SendRequest(CONTROLLER_CMD_ON_OUTPUT_DEVICE_CHANGE, parcel, reply, option) == 0,
217 "send request failed");
218 }
219
OnSessionEventChange(const std::string & event,const AAFwk::WantParams & args)220 void AVControllerCallbackProxy::OnSessionEventChange(const std::string& event, const AAFwk::WantParams& args)
221 {
222 MessageParcel parcel;
223 CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
224 CHECK_AND_RETURN_LOG(parcel.WriteString(event), "write event string failed");
225 CHECK_AND_RETURN_LOG(parcel.WriteParcelable(&args), "Write Want failed");
226 MessageParcel reply;
227 MessageOption option = { MessageOption::TF_ASYNC };
228 auto remote = Remote();
229 CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
230 CHECK_AND_RETURN_LOG(remote->SendRequest(CONTROLLER_CMD_ON_SET_SESSION_EVENT, parcel, reply, option) == 0,
231 "send request failed");
232 }
233
OnQueueItemsChange(const std::vector<AVQueueItem> & items)234 void AVControllerCallbackProxy::OnQueueItemsChange(const std::vector<AVQueueItem>& items)
235 {
236 MessageParcel parcel;
237 CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
238
239 CHECK_AND_RETURN_LOG(parcel.WriteInt32(items.size()), "write items num int32 failed");
240 for (auto &parcelable : items) {
241 CHECK_AND_RETURN_LOG(parcel.WriteParcelable(&parcelable), "Write items failed");
242 }
243
244 MessageParcel reply;
245 MessageOption option = { MessageOption::TF_ASYNC };
246 auto remote = Remote();
247 CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
248 CHECK_AND_RETURN_LOG(remote->SendRequest(CONTROLLER_CMD_ON_QUEUE_ITEMS_CHANGE, parcel, reply, option) == 0,
249 "send request failed");
250 }
251
OnQueueTitleChange(const std::string & title)252 void AVControllerCallbackProxy::OnQueueTitleChange(const std::string& title)
253 {
254 MessageParcel parcel;
255 CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
256 CHECK_AND_RETURN_LOG(parcel.WriteString(title), "write string failed");
257 MessageParcel reply;
258 MessageOption option = { MessageOption::TF_ASYNC };
259 auto remote = Remote();
260 CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
261 CHECK_AND_RETURN_LOG(remote->SendRequest(CONTROLLER_CMD_ON_QUEUE_TITLE_CHANGE, parcel, reply, option) == 0,
262 "send request failed");
263 }
264
OnExtrasChange(const AAFwk::WantParams & extras)265 void AVControllerCallbackProxy::OnExtrasChange(const AAFwk::WantParams& extras)
266 {
267 MessageParcel parcel;
268 CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
269 CHECK_AND_RETURN_LOG(parcel.WriteParcelable(&extras), "Write extras failed");
270 MessageParcel reply;
271 MessageOption option = { MessageOption::TF_ASYNC };
272 auto remote = Remote();
273 CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
274 CHECK_AND_RETURN_LOG(remote->SendRequest(CONTROLLER_CMD_ON_SET_EXTRAS_EVENT, parcel, reply, option) == 0,
275 "send request failed");
276 }
277 } // namespace OHOS::AVSession
278