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
GetAVMetaData(AVMetaData & metaData)104 int32_t RemoteSessionSyncerImpl::GetAVMetaData(AVMetaData& metaData)
105 {
106 std::vector<uint8_t> dataVector;
107 CHECK_AND_RETURN_RET_LOG(GetData(METADATA_KEY, dataVector) == AVSESSION_SUCCESS, AVSESSION_ERROR, "get data error");
108 CHECK_AND_RETURN_RET_LOG(dataVector.size() <= RECEIVE_DATA_SIZE_MAX, AVSESSION_ERROR, "get data size over range");
109 DefaultAllocator allocator;
110 uint8_t *allocateData = reinterpret_cast<uint8_t*>(allocator.Alloc(dataVector.size()));
111 CHECK_AND_RETURN_RET_LOG(allocateData != nullptr, AVSESSION_ERROR, "alloc data fail");
112 std::copy(dataVector.begin(), dataVector.end(), allocateData);
113 Parcel parcelData;
114 CHECK_AND_RETURN_RET_LOG(parcelData.ParseFrom(reinterpret_cast<uintptr_t>(allocateData), dataVector.size()),
115 AVSESSION_ERROR, "parse parcel error");
116 AVMetaData *data = AVMetaData::Unmarshalling(parcelData);
117 CHECK_AND_RETURN_RET_LOG(data != nullptr, AVSESSION_ERROR, "Unmarshalling error");
118 metaData = *data;
119 delete data;
120 return AVSESSION_SUCCESS;
121 }
122
PutAVPlaybackState(const AVPlaybackState & state)123 int32_t RemoteSessionSyncerImpl::PutAVPlaybackState(const AVPlaybackState& state)
124 {
125 AVSESSION_TRACE_SYNC_START("RemoteSessionSyncerImpl::PutAVPlaybackState");
126 Parcel data;
127 CHECK_AND_RETURN_RET_LOG(state.Marshalling(data), AVSESSION_ERROR, "state Marshalling error");
128 uint8_t *parcelData = reinterpret_cast<uint8_t*>(data.GetData());
129 std::vector<uint8_t> dataVector(data.GetDataSize());
130 std::copy(parcelData, parcelData + data.GetDataSize(), dataVector.begin());
131
132 CHECK_AND_RETURN_RET_LOG(PutData(PLAYBACK_STATE_KEY, dataVector) == AVSESSION_SUCCESS, AVSESSION_ERROR,
133 "put data error");
134 return AVSESSION_SUCCESS;
135 }
136
GetAVPlaybackState(AVPlaybackState & state)137 int32_t RemoteSessionSyncerImpl::GetAVPlaybackState(AVPlaybackState& state)
138 {
139 std::vector<uint8_t> dataVector;
140 CHECK_AND_RETURN_RET_LOG(GetData(PLAYBACK_STATE_KEY, dataVector) == AVSESSION_SUCCESS, AVSESSION_ERROR,
141 "get data error");
142 CHECK_AND_RETURN_RET_LOG(dataVector.size() <= RECEIVE_DATA_SIZE_MAX, AVSESSION_ERROR, "get data size over range");
143 DefaultAllocator allocator;
144 uint8_t *allocateData = reinterpret_cast<uint8_t*>(allocator.Alloc(dataVector.size()));
145 CHECK_AND_RETURN_RET_LOG(allocateData != nullptr, AVSESSION_ERROR, "alloc data fail");
146 std::copy(dataVector.begin(), dataVector.end(), allocateData);
147 Parcel parcelData;
148 CHECK_AND_RETURN_RET_LOG(parcelData.ParseFrom(reinterpret_cast<uintptr_t>(allocateData), dataVector.size()),
149 AVSESSION_ERROR, "parse parcel error");
150 AVPlaybackState *data = AVPlaybackState::Unmarshalling(parcelData);
151 CHECK_AND_RETURN_RET_LOG(data != nullptr, AVSESSION_ERROR, "Unmarshalling error");
152 state = *data;
153 delete data;
154 return AVSESSION_SUCCESS;
155 }
156
PutControlCommand(const AVControlCommand & command)157 int32_t RemoteSessionSyncerImpl::PutControlCommand(const AVControlCommand& command)
158 {
159 AVSESSION_TRACE_SYNC_START("RemoteSessionSyncerImpl::PutControlCommand");
160 Parcel data;
161 CHECK_AND_RETURN_RET_LOG(command.Marshalling(data), AVSESSION_ERROR, "command Marshalling error");
162 uint8_t *parcelData = reinterpret_cast<uint8_t*>(data.GetData());
163 std::vector<uint8_t> dataVector(data.GetDataSize());
164 std::copy(parcelData, parcelData + data.GetDataSize(), dataVector.begin());
165
166 CHECK_AND_RETURN_RET_LOG(PutData(CONTROL_COMMAND_KEY, dataVector) == AVSESSION_SUCCESS, AVSESSION_ERROR,
167 "put data error");
168 return AVSESSION_SUCCESS;
169 }
170
GetControlCommand(AVControlCommand & command)171 int32_t RemoteSessionSyncerImpl::GetControlCommand(AVControlCommand& command)
172 {
173 std::vector<uint8_t> dataVector;
174 CHECK_AND_RETURN_RET_LOG(GetData(CONTROL_COMMAND_KEY, dataVector) == AVSESSION_SUCCESS, AVSESSION_ERROR,
175 "get data error");
176 CHECK_AND_RETURN_RET_LOG(dataVector.size() <= RECEIVE_DATA_SIZE_MAX, AVSESSION_ERROR, "get data size over range");
177 DefaultAllocator allocator;
178 uint8_t *allocateData = reinterpret_cast<uint8_t*>(allocator.Alloc(dataVector.size()));
179 CHECK_AND_RETURN_RET_LOG(allocateData != nullptr, AVSESSION_ERROR, "alloc data fail");
180 std::copy(dataVector.begin(), dataVector.end(), allocateData);
181 Parcel parcelData;
182 CHECK_AND_RETURN_RET_LOG(parcelData.ParseFrom(reinterpret_cast<uintptr_t>(allocateData), dataVector.size()),
183 AVSESSION_ERROR, "parse parcel error");
184 AVControlCommand *data = AVControlCommand::Unmarshalling(parcelData);
185 CHECK_AND_RETURN_RET_LOG(data != nullptr, AVSESSION_ERROR, "Unmarshalling error");
186 command = *data;
187 delete data;
188 return AVSESSION_SUCCESS;
189 }
190
PutCommonCommand(const std::string & commonCommand,const AAFwk::WantParams & commandArgs)191 int32_t RemoteSessionSyncerImpl::PutCommonCommand(const std::string& commonCommand,
192 const AAFwk::WantParams& commandArgs)
193 {
194 AVSESSION_TRACE_SYNC_START("RemoteSessionSyncerImpl::PutCommonCommand");
195 Parcel data;
196 CHECK_AND_RETURN_RET_LOG(data.WriteString(commonCommand), ERR_MARSHALLING, "Write commonCommand string failed");
197 CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&commandArgs), ERR_MARSHALLING, "Write commandArgs failed");
198
199 uint8_t *parcelData = reinterpret_cast<uint8_t*>(data.GetData());
200 std::vector<uint8_t> dataVector(data.GetDataSize());
201 std::copy(parcelData, parcelData + data.GetDataSize(), dataVector.begin());
202
203 CHECK_AND_RETURN_RET_LOG(PutData(COMMON_COMMAND_KEY, dataVector) == AVSESSION_SUCCESS, AVSESSION_ERROR,
204 "Put data error");
205 return AVSESSION_SUCCESS;
206 }
207
GetCommonCommand(std::string & commonCommand,AAFwk::WantParams & commandArgs)208 int32_t RemoteSessionSyncerImpl::GetCommonCommand(std::string& commonCommand, AAFwk::WantParams& commandArgs)
209 {
210 std::vector<uint8_t> dataVector;
211 CHECK_AND_RETURN_RET_LOG(GetData(COMMON_COMMAND_KEY, dataVector) == AVSESSION_SUCCESS, AVSESSION_ERROR,
212 "Get data error");
213 CHECK_AND_RETURN_RET_LOG(dataVector.size() <= RECEIVE_DATA_SIZE_MAX, AVSESSION_ERROR, "Get data size over range");
214 DefaultAllocator allocator;
215 uint8_t *allocateData = reinterpret_cast<uint8_t*>(allocator.Alloc(dataVector.size()));
216 CHECK_AND_RETURN_RET_LOG(allocateData != nullptr, AVSESSION_ERROR, "Alloc data fail");
217 std::copy(dataVector.begin(), dataVector.end(), allocateData);
218
219 Parcel parcelData;
220 CHECK_AND_RETURN_RET_LOG(parcelData.ParseFrom(reinterpret_cast<uintptr_t>(allocateData), dataVector.size()),
221 AVSESSION_ERROR, "Parse parcel error");
222
223 std::string commandString = parcelData.ReadString();
224 commonCommand = std::string(commandString);
225 AAFwk::WantParams *argsData = parcelData.ReadParcelable<AAFwk::WantParams>();
226 commandArgs = AAFwk::WantParams(*argsData);
227 delete argsData;
228 return AVSESSION_SUCCESS;
229 }
230
PutSessionEvent(const std::string & event,const AAFwk::WantParams & args)231 int32_t RemoteSessionSyncerImpl::PutSessionEvent(const std::string& event, const AAFwk::WantParams& args)
232 {
233 AVSESSION_TRACE_SYNC_START("RemoteSessionSyncerImpl::PutSessionEvent");
234 Parcel data;
235 CHECK_AND_RETURN_RET_LOG(data.WriteString(event), ERR_MARSHALLING, "write event string failed");
236 CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&args), ERR_MARSHALLING, "Write WantParams failed");
237
238 uint8_t *parcelData = reinterpret_cast<uint8_t*>(data.GetData());
239 std::vector<uint8_t> dataVector(data.GetDataSize());
240 std::copy(parcelData, parcelData + data.GetDataSize(), dataVector.begin());
241
242 CHECK_AND_RETURN_RET_LOG(PutData(SESSION_EVENT_KEY, dataVector) == AVSESSION_SUCCESS,
243 AVSESSION_ERROR, "put data error");
244 return AVSESSION_SUCCESS;
245 }
246
GetSessionEvent(std::string & event,AAFwk::WantParams & args)247 int32_t RemoteSessionSyncerImpl::GetSessionEvent(std::string& event, AAFwk::WantParams& args)
248 {
249 std::vector<uint8_t> dataVector;
250 CHECK_AND_RETURN_RET_LOG(GetData(SESSION_EVENT_KEY, dataVector) == AVSESSION_SUCCESS,
251 AVSESSION_ERROR, "get data error");
252 CHECK_AND_RETURN_RET_LOG(dataVector.size() <= RECEIVE_DATA_SIZE_MAX, AVSESSION_ERROR, "get data size over range");
253 DefaultAllocator allocator;
254 uint8_t *allocateData = reinterpret_cast<uint8_t*>(allocator.Alloc(dataVector.size()));
255 CHECK_AND_RETURN_RET_LOG(allocateData != nullptr, AVSESSION_ERROR, "alloc data fail");
256 std::copy(dataVector.begin(), dataVector.end(), allocateData);
257
258 Parcel parcelData;
259 CHECK_AND_RETURN_RET_LOG(parcelData.ParseFrom(reinterpret_cast<uintptr_t>(allocateData), dataVector.size()),
260 AVSESSION_ERROR, "parse parcel error");
261
262 std::string eventData = parcelData.ReadString();
263 event = std::string(eventData);
264 AAFwk::WantParams *argsData = parcelData.ReadParcelable<AAFwk::WantParams>();
265 args = AAFwk::WantParams(*argsData);
266
267 delete argsData;
268 return AVSESSION_SUCCESS;
269 }
270
PutAVQueueItems(const std::vector<AVQueueItem> & items)271 int32_t RemoteSessionSyncerImpl::PutAVQueueItems(const std::vector<AVQueueItem>& items)
272 {
273 AVSESSION_TRACE_SYNC_START("RemoteSessionSyncerImpl::PutAVQueueItems");
274 Parcel data;
275
276 CHECK_AND_RETURN_RET_LOG(data.WriteInt32(items.size()), AVSESSION_ERROR, "write items num int32 failed");
277 for (auto &parcelable : items) {
278 CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&parcelable), AVSESSION_ERROR, "Write items failed");
279 }
280
281 uint8_t *parcelData = reinterpret_cast<uint8_t*>(data.GetData());
282 std::vector<uint8_t> dataVector(data.GetDataSize());
283 std::copy(parcelData, parcelData + data.GetDataSize(), dataVector.begin());
284
285 CHECK_AND_RETURN_RET_LOG(PutData(QUEUE_ITEMS_KEY, dataVector) == AVSESSION_SUCCESS,
286 AVSESSION_ERROR, "put data error");
287 return AVSESSION_SUCCESS;
288 }
289
GetAVQueueItems(std::vector<AVQueueItem> & items)290 int32_t RemoteSessionSyncerImpl::GetAVQueueItems(std::vector<AVQueueItem>& items)
291 {
292 std::vector<uint8_t> dataVector;
293 CHECK_AND_RETURN_RET_LOG(GetData(QUEUE_ITEMS_KEY, dataVector) == AVSESSION_SUCCESS,
294 AVSESSION_ERROR, "get data error");
295 CHECK_AND_RETURN_RET_LOG(dataVector.size() <= RECEIVE_DATA_SIZE_MAX, AVSESSION_ERROR, "get data size over range");
296 DefaultAllocator allocator;
297 uint8_t *allocateData = reinterpret_cast<uint8_t*>(allocator.Alloc(dataVector.size()));
298 CHECK_AND_RETURN_RET_LOG(allocateData != nullptr, AVSESSION_ERROR, "alloc data fail");
299 std::copy(dataVector.begin(), dataVector.end(), allocateData);
300
301 Parcel parcelData;
302 CHECK_AND_RETURN_RET_LOG(parcelData.ParseFrom(reinterpret_cast<uintptr_t>(allocateData), dataVector.size()),
303 AVSESSION_ERROR, "parse parcel error");
304
305 std::vector<AVQueueItem> items_;
306 int32_t itemNum = parcelData.ReadInt32();
307 CHECK_AND_RETURN_RET_LOG(itemNum >= 0, AVSESSION_ERROR, "parse int32 itemNum failed");
308 for (int32_t i = 0; i < itemNum; i++) {
309 AVQueueItem *item = parcelData.ReadParcelable<AVQueueItem>();
310 CHECK_AND_RETURN_RET_LOG(item != nullptr, ERR_UNMARSHALLING, "read parcelable AVQueueItem failed");
311 items_.emplace_back(*item);
312 delete item;
313 }
314 items = items_;
315 return AVSESSION_SUCCESS;
316 }
317
PutAVQueueTitle(const std::string & title)318 int32_t RemoteSessionSyncerImpl::PutAVQueueTitle(const std::string& title)
319 {
320 AVSESSION_TRACE_SYNC_START("RemoteSessionSyncerImpl::PutAVQueueTitle");
321 Parcel data;
322 CHECK_AND_RETURN_RET_LOG(data.WriteString(title), ERR_MARSHALLING, "write title string failed");
323
324 uint8_t *parcelData = reinterpret_cast<uint8_t*>(data.GetData());
325 std::vector<uint8_t> dataVector(data.GetDataSize());
326 std::copy(parcelData, parcelData + data.GetDataSize(), dataVector.begin());
327
328 CHECK_AND_RETURN_RET_LOG(PutData(QUEUE_TITLE_KEY, dataVector) == AVSESSION_SUCCESS,
329 AVSESSION_ERROR, "put data error");
330 return AVSESSION_SUCCESS;
331 }
332
GetAVQueueTitle(std::string & title)333 int32_t RemoteSessionSyncerImpl::GetAVQueueTitle(std::string& title)
334 {
335 std::vector<uint8_t> dataVector;
336 CHECK_AND_RETURN_RET_LOG(GetData(QUEUE_TITLE_KEY, dataVector) == AVSESSION_SUCCESS,
337 AVSESSION_ERROR, "get data error");
338 CHECK_AND_RETURN_RET_LOG(dataVector.size() <= RECEIVE_DATA_SIZE_MAX, AVSESSION_ERROR, "get data size over range");
339 DefaultAllocator allocator;
340 uint8_t *allocateData = reinterpret_cast<uint8_t*>(allocator.Alloc(dataVector.size()));
341 CHECK_AND_RETURN_RET_LOG(allocateData != nullptr, AVSESSION_ERROR, "alloc data fail");
342 std::copy(dataVector.begin(), dataVector.end(), allocateData);
343
344 Parcel parcelData;
345 CHECK_AND_RETURN_RET_LOG(parcelData.ParseFrom(reinterpret_cast<uintptr_t>(allocateData), dataVector.size()),
346 AVSESSION_ERROR, "parse parcel error");
347
348 std::string titleDate = parcelData.ReadString();
349 title = std::string(titleDate);
350 return AVSESSION_SUCCESS;
351 }
352
PutExtras(const AAFwk::WantParams & extras)353 int32_t RemoteSessionSyncerImpl::PutExtras(const AAFwk::WantParams& extras)
354 {
355 AVSESSION_TRACE_SYNC_START("RemoteSessionSyncerImpl::PutExtras");
356 Parcel data;
357 CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&extras), ERR_MARSHALLING, "Write extras failed");
358
359 uint8_t *parcelData = reinterpret_cast<uint8_t*>(data.GetData());
360 std::vector<uint8_t> dataVector(data.GetDataSize());
361 std::copy(parcelData, parcelData + data.GetDataSize(), dataVector.begin());
362
363 CHECK_AND_RETURN_RET_LOG(PutData(EXTRAS_KEY, dataVector) == AVSESSION_SUCCESS,
364 AVSESSION_ERROR, "put data error");
365 return AVSESSION_SUCCESS;
366 }
367
GetExtras(AAFwk::WantParams & extras)368 int32_t RemoteSessionSyncerImpl::GetExtras(AAFwk::WantParams& extras)
369 {
370 std::vector<uint8_t> dataVector;
371 CHECK_AND_RETURN_RET_LOG(GetData(EXTRAS_KEY, dataVector) == AVSESSION_SUCCESS,
372 AVSESSION_ERROR, "get data error");
373 CHECK_AND_RETURN_RET_LOG(dataVector.size() <= RECEIVE_DATA_SIZE_MAX, AVSESSION_ERROR, "get data size over range");
374 DefaultAllocator allocator;
375 uint8_t *allocateData = reinterpret_cast<uint8_t*>(allocator.Alloc(dataVector.size()));
376 CHECK_AND_RETURN_RET_LOG(allocateData != nullptr, AVSESSION_ERROR, "alloc data fail");
377 std::copy(dataVector.begin(), dataVector.end(), allocateData);
378
379 Parcel parcelData;
380 CHECK_AND_RETURN_RET_LOG(parcelData.ParseFrom(reinterpret_cast<uintptr_t>(allocateData), dataVector.size()),
381 AVSESSION_ERROR, "parse parcel error");
382 AAFwk::WantParams *extrasData = parcelData.ReadParcelable<AAFwk::WantParams>();
383 extras = AAFwk::WantParams(*extrasData);
384
385 delete extrasData;
386 return AVSESSION_SUCCESS;
387 }
388
RegisterDataNotifier(const ObjectDataNotifier & notifier)389 int32_t RemoteSessionSyncerImpl::RegisterDataNotifier(const ObjectDataNotifier& notifier)
390 {
391 CHECK_AND_RETURN_RET_LOG(objectStore_ != nullptr && object_ != nullptr, AVSESSION_ERROR,
392 "objectStore_ or object_ is nullptr");
393 objectDataNotifier_ = notifier;
394 objectStore_->Watch(object_, shared_from_this());
395 return AVSESSION_SUCCESS;
396 }
397
RegisterDisconnectNotifier(const ObjectDisconnectNotifier & notifier)398 int32_t RemoteSessionSyncerImpl::RegisterDisconnectNotifier(const ObjectDisconnectNotifier& notifier)
399 {
400 CHECK_AND_RETURN_RET_LOG(objectStore_ != nullptr, AVSESSION_ERROR, "objectStore_ is nullptr");
401 objectDisconnectNotifier_ = notifier;
402 objectStore_->SetStatusNotifier(shared_from_this());
403 return AVSESSION_SUCCESS;
404 }
405
Destroy()406 void RemoteSessionSyncerImpl::Destroy()
407 {
408 auto ret = objectStore_->UnWatch(object_);
409 CHECK_AND_RETURN_LOG(ret == ObjectStore::SUCCESS, "UnWatch error");
410 ret = objectStore_->DeleteObject(objectName_);
411 CHECK_AND_RETURN_LOG(ret == ObjectStore::SUCCESS, "DeleteObject error");
412 SLOGI("Destroy");
413 }
414
~RemoteSessionSyncerImpl()415 RemoteSessionSyncerImpl::~RemoteSessionSyncerImpl()
416 {
417 SLOGI("RemoteSessionSyncerImpl");
418 }
419 } // namespace OHOS::AVSession