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 <iomanip>
17 #include <sstream>
18 #include "avsession_trace.h"
19 #include "hash_calculator.h"
20 #include "parcel.h"
21 #include "remote_session_syncer_impl.h"
22
23 namespace OHOS::AVSession {
RemoteSessionSyncerImpl(const std::string & sourceSessionId,const std::string & sourceDevice,const std::string & sinkDevice)24 RemoteSessionSyncerImpl::RemoteSessionSyncerImpl(const std::string& sourceSessionId, const std::string& sourceDevice,
25 const std::string& sinkDevice)
26 : object_(nullptr), objectStore_(nullptr), sourceSessionId_(sourceSessionId), sourceDevice_(sourceDevice),
27 sinkDevice_(sinkDevice)
28 {
29 SLOGI("construct");
30 }
31
OnChanged(const std::string & sessionId,const std::vector<std::string> & keys)32 void RemoteSessionSyncerImpl::OnChanged(const std::string &sessionId, const std::vector<std::string>& keys)
33 {
34 SLOGI("sessionId is %{public}s key is %{public}s", sessionId.c_str(), keys[0].c_str());
35 CHECK_AND_RETURN_LOG(objectDataNotifier_ != nullptr, "objectDataNotifier_ is nullptr");
36 for (const auto& key : keys) {
37 CHECK_AND_RETURN_LOG(categoryMap.count(key) > 0, "key is not exist");
38 objectDataNotifier_(categoryMap.at(key), sinkDevice_);
39 }
40 }
41
OnChanged(const std::string & name,const std::string & networkId,const std::string & onlineStatus)42 void RemoteSessionSyncerImpl::OnChanged(const std::string &name, const std::string &networkId,
43 const std::string &onlineStatus)
44 {
45 SLOGI("%{public}.6s %{public}s", networkId.c_str(), onlineStatus.c_str());
46 CHECK_AND_RETURN_LOG(onlineStatus == "offline", "state is online");
47 CHECK_AND_RETURN_LOG(objectDisconnectNotifier_ != nullptr, "objectDataNotifier_ is nullptr");
48 objectDisconnectNotifier_(networkId);
49 }
50
Init()51 int32_t RemoteSessionSyncerImpl::Init()
52 {
53 SLOGI("start");
54 objectStore_ = DistributedObjectStore::GetInstance("av_session");
55 CHECK_AND_RETURN_RET_LOG(objectStore_ != nullptr, AVSESSION_ERROR, "objectStore_ is nullptr");
56
57 std::string object = sourceSessionId_ + sourceDevice_ + sinkDevice_;
58 HashCalculator hashCalculator;
59 CHECK_AND_RETURN_RET_LOG(hashCalculator.Init() == AVSESSION_SUCCESS, AVSESSION_ERROR, "hash init failed");
60 CHECK_AND_RETURN_RET_LOG(hashCalculator.Update(std::vector<uint8_t>(object.begin(), object.end())) ==
61 AVSESSION_SUCCESS, AVSESSION_ERROR, "hash update failed");
62 std::vector<uint8_t> hash;
63 CHECK_AND_RETURN_RET_LOG(hashCalculator.GetResult(hash) == AVSESSION_SUCCESS, AVSESSION_ERROR,
64 "hash get result failed");
65 std::stringstream stream;
66 int32_t width = 2;
67 for (const auto& byte : hash) {
68 stream << std::uppercase << std::hex << std::setfill('0') << std::setw(width) << static_cast<int>(byte);
69 }
70 objectName_ = stream.str();
71 SLOGI("sourceSessionId is %{public}s, sourceDevice is %{public}s, sinkDevice is %{public}s, object is %{public}s",
72 sourceSessionId_.c_str(), sourceDevice_.c_str(), sinkDevice_.c_str(), objectName_.c_str());
73 object_ = objectStore_->CreateObject(objectName_);
74 CHECK_AND_RETURN_RET_LOG(object_ != nullptr, AVSESSION_ERROR, "object_ is nullptr");
75 SLOGI("init object success");
76 return AVSESSION_SUCCESS;
77 }
78
PutData(const std::string & key,std::vector<uint8_t> & data)79 int32_t RemoteSessionSyncerImpl::PutData(const std::string &key, std::vector<uint8_t> &data)
80 {
81 CHECK_AND_RETURN_RET_LOG(object_ != nullptr, AVSESSION_ERROR, "object is nullptr");
82 return object_->PutComplex(key, data) == ObjectStore::SUCCESS ? AVSESSION_SUCCESS : AVSESSION_ERROR;
83 }
84
GetData(const std::string & key,std::vector<uint8_t> & data)85 int32_t RemoteSessionSyncerImpl::GetData(const std::string &key, std::vector<uint8_t> &data)
86 {
87 CHECK_AND_RETURN_RET_LOG(object_ != nullptr, AVSESSION_ERROR, "object is nullptr");
88 return object_->GetComplex(key, data) == ObjectStore::SUCCESS ? AVSESSION_SUCCESS : AVSESSION_ERROR;
89 }
90
PutAVMetaData(const AVMetaData & metaData)91 int32_t RemoteSessionSyncerImpl::PutAVMetaData(const AVMetaData& metaData)
92 {
93 AVSESSION_TRACE_SYNC_START("RemoteSessionSyncerImpl::PutAVMetaData");
94 Parcel data;
95 CHECK_AND_RETURN_RET_LOG(metaData.Marshalling(data), AVSESSION_ERROR, "metaData Marshalling error");
96 uint8_t *parcelData = reinterpret_cast<uint8_t*>(data.GetData());
97 std::vector<uint8_t> dataVector(data.GetDataSize());
98 std::copy(parcelData, parcelData + data.GetDataSize(), dataVector.begin());
99
100 CHECK_AND_RETURN_RET_LOG(PutData(METADATA_KEY, dataVector) == AVSESSION_SUCCESS, AVSESSION_ERROR, "put data error");
101 return AVSESSION_SUCCESS;
102 }
103
104 // LCOV_EXCL_START
GetAVMetaData(AVMetaData & metaData)105 int32_t RemoteSessionSyncerImpl::GetAVMetaData(AVMetaData& metaData)
106 {
107 std::vector<uint8_t> dataVector;
108 CHECK_AND_RETURN_RET_LOG(GetData(METADATA_KEY, dataVector) == AVSESSION_SUCCESS, AVSESSION_ERROR, "get data error");
109 CHECK_AND_RETURN_RET_LOG(dataVector.size() <= RECEIVE_DATA_SIZE_MAX, AVSESSION_ERROR, "get data size over range");
110 DefaultAllocator allocator;
111 uint8_t *allocateData = reinterpret_cast<uint8_t*>(allocator.Alloc(dataVector.size()));
112 CHECK_AND_RETURN_RET_LOG(allocateData != nullptr, AVSESSION_ERROR, "alloc data fail");
113 std::copy(dataVector.begin(), dataVector.end(), allocateData);
114 Parcel parcelData;
115 CHECK_AND_RETURN_RET_LOG(parcelData.ParseFrom(reinterpret_cast<uintptr_t>(allocateData), dataVector.size()),
116 AVSESSION_ERROR, "parse parcel error");
117 AVMetaData *data = AVMetaData::Unmarshalling(parcelData);
118 CHECK_AND_RETURN_RET_LOG(data != nullptr, AVSESSION_ERROR, "Unmarshalling error");
119 metaData = *data;
120 delete data;
121 data = nullptr;
122 return AVSESSION_SUCCESS;
123 }
124 // LCOV_EXCL_STOP
125
126 // LCOV_EXCL_START
PutAVPlaybackState(const AVPlaybackState & state)127 int32_t RemoteSessionSyncerImpl::PutAVPlaybackState(const AVPlaybackState& state)
128 {
129 AVSESSION_TRACE_SYNC_START("RemoteSessionSyncerImpl::PutAVPlaybackState");
130 Parcel data;
131 CHECK_AND_RETURN_RET_LOG(state.Marshalling(data), AVSESSION_ERROR, "state Marshalling error");
132 uint8_t *parcelData = reinterpret_cast<uint8_t*>(data.GetData());
133 std::vector<uint8_t> dataVector(data.GetDataSize());
134 std::copy(parcelData, parcelData + data.GetDataSize(), dataVector.begin());
135
136 CHECK_AND_RETURN_RET_LOG(PutData(PLAYBACK_STATE_KEY, dataVector) == AVSESSION_SUCCESS, AVSESSION_ERROR,
137 "put data error");
138 return AVSESSION_SUCCESS;
139 }
140 // LCOV_EXCL_STOP
141
142 // LCOV_EXCL_START
GetAVPlaybackState(AVPlaybackState & state)143 int32_t RemoteSessionSyncerImpl::GetAVPlaybackState(AVPlaybackState& state)
144 {
145 std::vector<uint8_t> dataVector;
146 CHECK_AND_RETURN_RET_LOG(GetData(PLAYBACK_STATE_KEY, dataVector) == AVSESSION_SUCCESS, AVSESSION_ERROR,
147 "get data error");
148 CHECK_AND_RETURN_RET_LOG(dataVector.size() <= RECEIVE_DATA_SIZE_MAX, AVSESSION_ERROR, "get data size over range");
149 DefaultAllocator allocator;
150 uint8_t *allocateData = reinterpret_cast<uint8_t*>(allocator.Alloc(dataVector.size()));
151 CHECK_AND_RETURN_RET_LOG(allocateData != nullptr, AVSESSION_ERROR, "alloc data fail");
152 std::copy(dataVector.begin(), dataVector.end(), allocateData);
153 Parcel parcelData;
154 CHECK_AND_RETURN_RET_LOG(parcelData.ParseFrom(reinterpret_cast<uintptr_t>(allocateData), dataVector.size()),
155 AVSESSION_ERROR, "parse parcel error");
156 AVPlaybackState *data = AVPlaybackState::Unmarshalling(parcelData);
157 CHECK_AND_RETURN_RET_LOG(data != nullptr, AVSESSION_ERROR, "Unmarshalling error");
158 state = *data;
159 delete data;
160 data = nullptr;
161 return AVSESSION_SUCCESS;
162 }
163 // LCOV_EXCL_STOP
164
165 // LCOV_EXCL_START
PutControlCommand(const AVControlCommand & command)166 int32_t RemoteSessionSyncerImpl::PutControlCommand(const AVControlCommand& command)
167 {
168 AVSESSION_TRACE_SYNC_START("RemoteSessionSyncerImpl::PutControlCommand");
169 Parcel data;
170 CHECK_AND_RETURN_RET_LOG(command.Marshalling(data), AVSESSION_ERROR, "command Marshalling error");
171 uint8_t *parcelData = reinterpret_cast<uint8_t*>(data.GetData());
172 std::vector<uint8_t> dataVector(data.GetDataSize());
173 std::copy(parcelData, parcelData + data.GetDataSize(), dataVector.begin());
174
175 CHECK_AND_RETURN_RET_LOG(PutData(CONTROL_COMMAND_KEY, dataVector) == AVSESSION_SUCCESS, AVSESSION_ERROR,
176 "put data error");
177 return AVSESSION_SUCCESS;
178 }
179 // LCOV_EXCL_STOP
180
181 // LCOV_EXCL_START
GetControlCommand(AVControlCommand & command)182 int32_t RemoteSessionSyncerImpl::GetControlCommand(AVControlCommand& command)
183 {
184 std::vector<uint8_t> dataVector;
185 CHECK_AND_RETURN_RET_LOG(GetData(CONTROL_COMMAND_KEY, dataVector) == AVSESSION_SUCCESS, AVSESSION_ERROR,
186 "get data error");
187 CHECK_AND_RETURN_RET_LOG(dataVector.size() <= RECEIVE_DATA_SIZE_MAX, AVSESSION_ERROR, "get data size over range");
188 DefaultAllocator allocator;
189 uint8_t *allocateData = reinterpret_cast<uint8_t*>(allocator.Alloc(dataVector.size()));
190 CHECK_AND_RETURN_RET_LOG(allocateData != nullptr, AVSESSION_ERROR, "alloc data fail");
191 std::copy(dataVector.begin(), dataVector.end(), allocateData);
192 Parcel parcelData;
193 CHECK_AND_RETURN_RET_LOG(parcelData.ParseFrom(reinterpret_cast<uintptr_t>(allocateData), dataVector.size()),
194 AVSESSION_ERROR, "parse parcel error");
195 AVControlCommand *data = AVControlCommand::Unmarshalling(parcelData);
196 CHECK_AND_RETURN_RET_LOG(data != nullptr, AVSESSION_ERROR, "Unmarshalling error");
197 command = *data;
198 delete data;
199 data = nullptr;
200 return AVSESSION_SUCCESS;
201 }
202 // LCOV_EXCL_STOP
203
204 // LCOV_EXCL_START
PutCommonCommand(const std::string & commonCommand,const AAFwk::WantParams & commandArgs)205 int32_t RemoteSessionSyncerImpl::PutCommonCommand(const std::string& commonCommand,
206 const AAFwk::WantParams& commandArgs)
207 {
208 AVSESSION_TRACE_SYNC_START("RemoteSessionSyncerImpl::PutCommonCommand");
209 Parcel data;
210 CHECK_AND_RETURN_RET_LOG(data.WriteString(commonCommand), ERR_MARSHALLING, "Write commonCommand string failed");
211 CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&commandArgs), ERR_MARSHALLING, "Write commandArgs failed");
212
213 uint8_t *parcelData = reinterpret_cast<uint8_t*>(data.GetData());
214 std::vector<uint8_t> dataVector(data.GetDataSize());
215 std::copy(parcelData, parcelData + data.GetDataSize(), dataVector.begin());
216
217 CHECK_AND_RETURN_RET_LOG(PutData(COMMON_COMMAND_KEY, dataVector) == AVSESSION_SUCCESS, AVSESSION_ERROR,
218 "Put data error");
219 return AVSESSION_SUCCESS;
220 }
221 // LCOV_EXCL_STOP
222
223 // LCOV_EXCL_START
GetCommonCommand(std::string & commonCommand,AAFwk::WantParams & commandArgs)224 int32_t RemoteSessionSyncerImpl::GetCommonCommand(std::string& commonCommand, AAFwk::WantParams& commandArgs)
225 {
226 std::vector<uint8_t> dataVector;
227 CHECK_AND_RETURN_RET_LOG(GetData(COMMON_COMMAND_KEY, dataVector) == AVSESSION_SUCCESS, AVSESSION_ERROR,
228 "Get data error");
229 CHECK_AND_RETURN_RET_LOG(dataVector.size() <= RECEIVE_DATA_SIZE_MAX, AVSESSION_ERROR, "Get data size over range");
230 DefaultAllocator allocator;
231 uint8_t *allocateData = reinterpret_cast<uint8_t*>(allocator.Alloc(dataVector.size()));
232 CHECK_AND_RETURN_RET_LOG(allocateData != nullptr, AVSESSION_ERROR, "Alloc data fail");
233 std::copy(dataVector.begin(), dataVector.end(), allocateData);
234
235 Parcel parcelData;
236 CHECK_AND_RETURN_RET_LOG(parcelData.ParseFrom(reinterpret_cast<uintptr_t>(allocateData), dataVector.size()),
237 AVSESSION_ERROR, "Parse parcel error");
238
239 std::string commandString = parcelData.ReadString();
240 commonCommand = std::string(commandString);
241 AAFwk::WantParams *argsData = parcelData.ReadParcelable<AAFwk::WantParams>();
242 if (argsData == nullptr) {
243 SLOGE("GetCommonCommand: read parcelable commonCommand failed");
244 delete argsData;
245 argsData = nullptr;
246 return AVSESSION_ERROR;
247 }
248 commandArgs = AAFwk::WantParams(*argsData);
249 delete argsData;
250 argsData = nullptr;
251 return AVSESSION_SUCCESS;
252 }
253 // LCOV_EXCL_STOP
254
255 // LCOV_EXCL_START
PutSessionEvent(const std::string & event,const AAFwk::WantParams & args)256 int32_t RemoteSessionSyncerImpl::PutSessionEvent(const std::string& event, const AAFwk::WantParams& args)
257 {
258 AVSESSION_TRACE_SYNC_START("RemoteSessionSyncerImpl::PutSessionEvent");
259 Parcel data;
260 CHECK_AND_RETURN_RET_LOG(data.WriteString(event), ERR_MARSHALLING, "write event string failed");
261 CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&args), ERR_MARSHALLING, "Write WantParams failed");
262
263 uint8_t *parcelData = reinterpret_cast<uint8_t*>(data.GetData());
264 std::vector<uint8_t> dataVector(data.GetDataSize());
265 std::copy(parcelData, parcelData + data.GetDataSize(), dataVector.begin());
266
267 CHECK_AND_RETURN_RET_LOG(PutData(SESSION_EVENT_KEY, dataVector) == AVSESSION_SUCCESS,
268 AVSESSION_ERROR, "put data error");
269 return AVSESSION_SUCCESS;
270 }
271 // LCOV_EXCL_STOP
272
273 // LCOV_EXCL_START
GetSessionEvent(std::string & event,AAFwk::WantParams & args)274 int32_t RemoteSessionSyncerImpl::GetSessionEvent(std::string& event, AAFwk::WantParams& args)
275 {
276 std::vector<uint8_t> dataVector;
277 CHECK_AND_RETURN_RET_LOG(GetData(SESSION_EVENT_KEY, dataVector) == AVSESSION_SUCCESS,
278 AVSESSION_ERROR, "get data error");
279 CHECK_AND_RETURN_RET_LOG(dataVector.size() <= RECEIVE_DATA_SIZE_MAX, AVSESSION_ERROR, "get data size over range");
280 DefaultAllocator allocator;
281 uint8_t *allocateData = reinterpret_cast<uint8_t*>(allocator.Alloc(dataVector.size()));
282 CHECK_AND_RETURN_RET_LOG(allocateData != nullptr, AVSESSION_ERROR, "alloc data fail");
283 std::copy(dataVector.begin(), dataVector.end(), allocateData);
284
285 Parcel parcelData;
286 CHECK_AND_RETURN_RET_LOG(parcelData.ParseFrom(reinterpret_cast<uintptr_t>(allocateData), dataVector.size()),
287 AVSESSION_ERROR, "parse parcel error");
288
289 std::string eventData = parcelData.ReadString();
290 event = std::string(eventData);
291 AAFwk::WantParams *argsData = parcelData.ReadParcelable<AAFwk::WantParams>();
292 if (argsData == nullptr) {
293 SLOGE("GetSessionEvent: read parcelable sessionEvent failed");
294 delete argsData;
295 argsData = nullptr;
296 return AVSESSION_ERROR;
297 }
298 args = AAFwk::WantParams(*argsData);
299
300 delete argsData;
301 argsData = nullptr;
302 return AVSESSION_SUCCESS;
303 }
304 // LCOV_EXCL_STOP
305
306 // LCOV_EXCL_START
PutAVQueueItems(const std::vector<AVQueueItem> & items)307 int32_t RemoteSessionSyncerImpl::PutAVQueueItems(const std::vector<AVQueueItem>& items)
308 {
309 AVSESSION_TRACE_SYNC_START("RemoteSessionSyncerImpl::PutAVQueueItems");
310 Parcel data;
311
312 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(items.size()), AVSESSION_ERROR, "write items num int32 failed");
313 for (auto &parcelable : items) {
314 CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&parcelable), AVSESSION_ERROR, "Write items failed");
315 }
316
317 uint8_t *parcelData = reinterpret_cast<uint8_t*>(data.GetData());
318 std::vector<uint8_t> dataVector(data.GetDataSize());
319 std::copy(parcelData, parcelData + data.GetDataSize(), dataVector.begin());
320
321 CHECK_AND_RETURN_RET_LOG(PutData(QUEUE_ITEMS_KEY, dataVector) == AVSESSION_SUCCESS,
322 AVSESSION_ERROR, "put data error");
323 return AVSESSION_SUCCESS;
324 }
325 // LCOV_EXCL_STOP
326
327 // LCOV_EXCL_START
GetAVQueueItems(std::vector<AVQueueItem> & items)328 int32_t RemoteSessionSyncerImpl::GetAVQueueItems(std::vector<AVQueueItem>& items)
329 {
330 std::vector<uint8_t> dataVector;
331 CHECK_AND_RETURN_RET_LOG(GetData(QUEUE_ITEMS_KEY, dataVector) == AVSESSION_SUCCESS,
332 AVSESSION_ERROR, "get data error");
333 CHECK_AND_RETURN_RET_LOG(dataVector.size() <= RECEIVE_DATA_SIZE_MAX, AVSESSION_ERROR, "get data size over range");
334 DefaultAllocator allocator;
335 uint8_t *allocateData = reinterpret_cast<uint8_t*>(allocator.Alloc(dataVector.size()));
336 CHECK_AND_RETURN_RET_LOG(allocateData != nullptr, AVSESSION_ERROR, "alloc data fail");
337 std::copy(dataVector.begin(), dataVector.end(), allocateData);
338
339 Parcel parcelData;
340 CHECK_AND_RETURN_RET_LOG(parcelData.ParseFrom(reinterpret_cast<uintptr_t>(allocateData), dataVector.size()),
341 AVSESSION_ERROR, "parse parcel error");
342
343 std::vector<AVQueueItem> items_;
344 int32_t itemNum = parcelData.ReadInt32();
345 int32_t maxItemNum = 1000;
346 CHECK_AND_RETURN_RET_LOG((itemNum >= 0) && (itemNum <= maxItemNum), AVSESSION_ERROR, "parse int32 itemNum failed");
347 for (int32_t i = 0; i < itemNum; i++) {
348 AVQueueItem *item = parcelData.ReadParcelable<AVQueueItem>();
349 if (item == nullptr) {
350 SLOGE("GetAVQueueItems: read parcelable AVQueueItem failed");
351 delete item;
352 item = nullptr;
353 return ERR_UNMARSHALLING;
354 }
355 items_.emplace_back(*item);
356 delete item;
357 item = nullptr;
358 }
359 items = items_;
360 return AVSESSION_SUCCESS;
361 }
362 // LCOV_EXCL_STOP
363
364 // LCOV_EXCL_START
PutAVQueueTitle(const std::string & title)365 int32_t RemoteSessionSyncerImpl::PutAVQueueTitle(const std::string& title)
366 {
367 AVSESSION_TRACE_SYNC_START("RemoteSessionSyncerImpl::PutAVQueueTitle");
368 Parcel data;
369 CHECK_AND_RETURN_RET_LOG(data.WriteString(title), ERR_MARSHALLING, "write title string failed");
370
371 uint8_t *parcelData = reinterpret_cast<uint8_t*>(data.GetData());
372 std::vector<uint8_t> dataVector(data.GetDataSize());
373 std::copy(parcelData, parcelData + data.GetDataSize(), dataVector.begin());
374
375 CHECK_AND_RETURN_RET_LOG(PutData(QUEUE_TITLE_KEY, dataVector) == AVSESSION_SUCCESS,
376 AVSESSION_ERROR, "put data error");
377 return AVSESSION_SUCCESS;
378 }
379 // LCOV_EXCL_STOP
380
381 // LCOV_EXCL_START
GetAVQueueTitle(std::string & title)382 int32_t RemoteSessionSyncerImpl::GetAVQueueTitle(std::string& title)
383 {
384 std::vector<uint8_t> dataVector;
385 CHECK_AND_RETURN_RET_LOG(GetData(QUEUE_TITLE_KEY, dataVector) == AVSESSION_SUCCESS,
386 AVSESSION_ERROR, "get data error");
387 CHECK_AND_RETURN_RET_LOG(dataVector.size() <= RECEIVE_DATA_SIZE_MAX, AVSESSION_ERROR, "get data size over range");
388 DefaultAllocator allocator;
389 uint8_t *allocateData = reinterpret_cast<uint8_t*>(allocator.Alloc(dataVector.size()));
390 CHECK_AND_RETURN_RET_LOG(allocateData != nullptr, AVSESSION_ERROR, "alloc data fail");
391 std::copy(dataVector.begin(), dataVector.end(), allocateData);
392
393 Parcel parcelData;
394 CHECK_AND_RETURN_RET_LOG(parcelData.ParseFrom(reinterpret_cast<uintptr_t>(allocateData), dataVector.size()),
395 AVSESSION_ERROR, "parse parcel error");
396
397 std::string titleDate = parcelData.ReadString();
398 title = std::string(titleDate);
399 return AVSESSION_SUCCESS;
400 }
401 // LCOV_EXCL_STOP
402
403 // LCOV_EXCL_START
PutExtras(const AAFwk::WantParams & extras)404 int32_t RemoteSessionSyncerImpl::PutExtras(const AAFwk::WantParams& extras)
405 {
406 AVSESSION_TRACE_SYNC_START("RemoteSessionSyncerImpl::PutExtras");
407 Parcel data;
408 CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&extras), ERR_MARSHALLING, "Write extras failed");
409
410 uint8_t *parcelData = reinterpret_cast<uint8_t*>(data.GetData());
411 std::vector<uint8_t> dataVector(data.GetDataSize());
412 std::copy(parcelData, parcelData + data.GetDataSize(), dataVector.begin());
413
414 CHECK_AND_RETURN_RET_LOG(PutData(EXTRAS_KEY, dataVector) == AVSESSION_SUCCESS,
415 AVSESSION_ERROR, "put data error");
416 return AVSESSION_SUCCESS;
417 }
418 // LCOV_EXCL_STOP
419
420 // LCOV_EXCL_START
GetExtras(AAFwk::WantParams & extras)421 int32_t RemoteSessionSyncerImpl::GetExtras(AAFwk::WantParams& extras)
422 {
423 std::vector<uint8_t> dataVector;
424 CHECK_AND_RETURN_RET_LOG(GetData(EXTRAS_KEY, dataVector) == AVSESSION_SUCCESS,
425 AVSESSION_ERROR, "get data error");
426 CHECK_AND_RETURN_RET_LOG(dataVector.size() <= RECEIVE_DATA_SIZE_MAX, AVSESSION_ERROR, "get data size over range");
427 DefaultAllocator allocator;
428 uint8_t *allocateData = reinterpret_cast<uint8_t*>(allocator.Alloc(dataVector.size()));
429 CHECK_AND_RETURN_RET_LOG(allocateData != nullptr, AVSESSION_ERROR, "alloc data fail");
430 std::copy(dataVector.begin(), dataVector.end(), allocateData);
431
432 Parcel parcelData;
433 CHECK_AND_RETURN_RET_LOG(parcelData.ParseFrom(reinterpret_cast<uintptr_t>(allocateData), dataVector.size()),
434 AVSESSION_ERROR, "parse parcel error");
435 AAFwk::WantParams *extrasData = parcelData.ReadParcelable<AAFwk::WantParams>();
436 if (extrasData == nullptr) {
437 SLOGE("GetSessionEvent: read parcelable sessionEvent failed");
438 delete extrasData;
439 extrasData = nullptr;
440 return AVSESSION_ERROR;
441 }
442 extras = AAFwk::WantParams(*extrasData);
443
444 delete extrasData;
445 extrasData = nullptr;
446 return AVSESSION_SUCCESS;
447 }
448 // LCOV_EXCL_STOP
449
450 // LCOV_EXCL_START
RegisterDataNotifier(const ObjectDataNotifier & notifier)451 int32_t RemoteSessionSyncerImpl::RegisterDataNotifier(const ObjectDataNotifier& notifier)
452 {
453 CHECK_AND_RETURN_RET_LOG(objectStore_ != nullptr && object_ != nullptr, AVSESSION_ERROR,
454 "objectStore_ or object_ is nullptr");
455 objectDataNotifier_ = notifier;
456 objectStore_->Watch(object_, shared_from_this());
457 return AVSESSION_SUCCESS;
458 }
459 // LCOV_EXCL_STOP
460
461 // LCOV_EXCL_START
RegisterDisconnectNotifier(const ObjectDisconnectNotifier & notifier)462 int32_t RemoteSessionSyncerImpl::RegisterDisconnectNotifier(const ObjectDisconnectNotifier& notifier)
463 {
464 CHECK_AND_RETURN_RET_LOG(objectStore_ != nullptr, AVSESSION_ERROR, "objectStore_ is nullptr");
465 objectDisconnectNotifier_ = notifier;
466 objectStore_->SetStatusNotifier(shared_from_this());
467 return AVSESSION_SUCCESS;
468 }
469 // LCOV_EXCL_STOP
470
471 // LCOV_EXCL_START
Destroy()472 void RemoteSessionSyncerImpl::Destroy()
473 {
474 auto ret = objectStore_->UnWatch(object_);
475 CHECK_AND_RETURN_LOG(ret == ObjectStore::SUCCESS, "UnWatch error");
476 ret = objectStore_->DeleteObject(objectName_);
477 CHECK_AND_RETURN_LOG(ret == ObjectStore::SUCCESS, "DeleteObject error");
478 SLOGI("Destroy");
479 }
480 // LCOV_EXCL_STOP
481
482 // LCOV_EXCL_START
~RemoteSessionSyncerImpl()483 RemoteSessionSyncerImpl::~RemoteSessionSyncerImpl()
484 {
485 SLOGI("RemoteSessionSyncerImpl");
486 }
487 // LCOV_EXCL_STOP
488 } // namespace OHOS::AVSession