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