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     if (argsData == nullptr) {
227         SLOGE("GetCommonCommand: read parcelable commonCommand failed");
228         delete argsData;
229         return AVSESSION_ERROR;
230     }
231     commandArgs = AAFwk::WantParams(*argsData);
232     delete argsData;
233     return AVSESSION_SUCCESS;
234 }
235 
PutSessionEvent(const std::string & event,const AAFwk::WantParams & args)236 int32_t RemoteSessionSyncerImpl::PutSessionEvent(const std::string& event, const AAFwk::WantParams& args)
237 {
238     AVSESSION_TRACE_SYNC_START("RemoteSessionSyncerImpl::PutSessionEvent");
239     Parcel data;
240     CHECK_AND_RETURN_RET_LOG(data.WriteString(event), ERR_MARSHALLING, "write event string failed");
241     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&args), ERR_MARSHALLING, "Write WantParams failed");
242 
243     uint8_t *parcelData = reinterpret_cast<uint8_t*>(data.GetData());
244     std::vector<uint8_t> dataVector(data.GetDataSize());
245     std::copy(parcelData, parcelData + data.GetDataSize(), dataVector.begin());
246 
247     CHECK_AND_RETURN_RET_LOG(PutData(SESSION_EVENT_KEY, dataVector) == AVSESSION_SUCCESS,
248         AVSESSION_ERROR, "put data error");
249     return AVSESSION_SUCCESS;
250 }
251 
GetSessionEvent(std::string & event,AAFwk::WantParams & args)252 int32_t RemoteSessionSyncerImpl::GetSessionEvent(std::string& event, AAFwk::WantParams& args)
253 {
254     std::vector<uint8_t> dataVector;
255     CHECK_AND_RETURN_RET_LOG(GetData(SESSION_EVENT_KEY, dataVector) == AVSESSION_SUCCESS,
256         AVSESSION_ERROR, "get data error");
257     CHECK_AND_RETURN_RET_LOG(dataVector.size() <= RECEIVE_DATA_SIZE_MAX, AVSESSION_ERROR, "get data size over range");
258     DefaultAllocator allocator;
259     uint8_t *allocateData = reinterpret_cast<uint8_t*>(allocator.Alloc(dataVector.size()));
260     CHECK_AND_RETURN_RET_LOG(allocateData != nullptr, AVSESSION_ERROR, "alloc data fail");
261     std::copy(dataVector.begin(), dataVector.end(), allocateData);
262 
263     Parcel parcelData;
264     CHECK_AND_RETURN_RET_LOG(parcelData.ParseFrom(reinterpret_cast<uintptr_t>(allocateData), dataVector.size()),
265                              AVSESSION_ERROR, "parse parcel error");
266 
267     std::string eventData = parcelData.ReadString();
268     event = std::string(eventData);
269     AAFwk::WantParams *argsData = parcelData.ReadParcelable<AAFwk::WantParams>();
270     if (argsData == nullptr) {
271         SLOGE("GetSessionEvent: read parcelable sessionEvent failed");
272         delete argsData;
273         return AVSESSION_ERROR;
274     }
275     args = AAFwk::WantParams(*argsData);
276 
277     delete argsData;
278     return AVSESSION_SUCCESS;
279 }
280 
PutAVQueueItems(const std::vector<AVQueueItem> & items)281 int32_t RemoteSessionSyncerImpl::PutAVQueueItems(const std::vector<AVQueueItem>& items)
282 {
283     AVSESSION_TRACE_SYNC_START("RemoteSessionSyncerImpl::PutAVQueueItems");
284     Parcel data;
285 
286     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(items.size()), AVSESSION_ERROR, "write items num int32 failed");
287     for (auto &parcelable : items) {
288         CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&parcelable), AVSESSION_ERROR, "Write items failed");
289     }
290 
291     uint8_t *parcelData = reinterpret_cast<uint8_t*>(data.GetData());
292     std::vector<uint8_t> dataVector(data.GetDataSize());
293     std::copy(parcelData, parcelData + data.GetDataSize(), dataVector.begin());
294 
295     CHECK_AND_RETURN_RET_LOG(PutData(QUEUE_ITEMS_KEY, dataVector) == AVSESSION_SUCCESS,
296         AVSESSION_ERROR, "put data error");
297     return AVSESSION_SUCCESS;
298 }
299 
GetAVQueueItems(std::vector<AVQueueItem> & items)300 int32_t RemoteSessionSyncerImpl::GetAVQueueItems(std::vector<AVQueueItem>& items)
301 {
302     std::vector<uint8_t> dataVector;
303     CHECK_AND_RETURN_RET_LOG(GetData(QUEUE_ITEMS_KEY, dataVector) == AVSESSION_SUCCESS,
304         AVSESSION_ERROR, "get data error");
305     CHECK_AND_RETURN_RET_LOG(dataVector.size() <= RECEIVE_DATA_SIZE_MAX, AVSESSION_ERROR, "get data size over range");
306     DefaultAllocator allocator;
307     uint8_t *allocateData = reinterpret_cast<uint8_t*>(allocator.Alloc(dataVector.size()));
308     CHECK_AND_RETURN_RET_LOG(allocateData != nullptr, AVSESSION_ERROR, "alloc data fail");
309     std::copy(dataVector.begin(), dataVector.end(), allocateData);
310 
311     Parcel parcelData;
312     CHECK_AND_RETURN_RET_LOG(parcelData.ParseFrom(reinterpret_cast<uintptr_t>(allocateData), dataVector.size()),
313                              AVSESSION_ERROR, "parse parcel error");
314 
315     std::vector<AVQueueItem> items_;
316     int32_t itemNum = parcelData.ReadInt32();
317     int32_t maxItemNum = 1000;
318     CHECK_AND_RETURN_RET_LOG((itemNum >= 0) && (itemNum <= maxItemNum), AVSESSION_ERROR, "parse int32 itemNum failed");
319     for (int32_t i = 0; i < itemNum; i++) {
320         AVQueueItem *item = parcelData.ReadParcelable<AVQueueItem>();
321         if (item == nullptr) {
322             SLOGE("GetAVQueueItems: read parcelable AVQueueItem failed");
323             delete item;
324             return ERR_UNMARSHALLING;
325         }
326         items_.emplace_back(*item);
327         delete item;
328     }
329     items = items_;
330     return AVSESSION_SUCCESS;
331 }
332 
PutAVQueueTitle(const std::string & title)333 int32_t RemoteSessionSyncerImpl::PutAVQueueTitle(const std::string& title)
334 {
335     AVSESSION_TRACE_SYNC_START("RemoteSessionSyncerImpl::PutAVQueueTitle");
336     Parcel data;
337     CHECK_AND_RETURN_RET_LOG(data.WriteString(title), ERR_MARSHALLING, "write title string failed");
338 
339     uint8_t *parcelData = reinterpret_cast<uint8_t*>(data.GetData());
340     std::vector<uint8_t> dataVector(data.GetDataSize());
341     std::copy(parcelData, parcelData + data.GetDataSize(), dataVector.begin());
342 
343     CHECK_AND_RETURN_RET_LOG(PutData(QUEUE_TITLE_KEY, dataVector) == AVSESSION_SUCCESS,
344         AVSESSION_ERROR, "put data error");
345     return AVSESSION_SUCCESS;
346 }
347 
GetAVQueueTitle(std::string & title)348 int32_t RemoteSessionSyncerImpl::GetAVQueueTitle(std::string& title)
349 {
350     std::vector<uint8_t> dataVector;
351     CHECK_AND_RETURN_RET_LOG(GetData(QUEUE_TITLE_KEY, dataVector) == AVSESSION_SUCCESS,
352         AVSESSION_ERROR, "get data error");
353     CHECK_AND_RETURN_RET_LOG(dataVector.size() <= RECEIVE_DATA_SIZE_MAX, AVSESSION_ERROR, "get data size over range");
354     DefaultAllocator allocator;
355     uint8_t *allocateData = reinterpret_cast<uint8_t*>(allocator.Alloc(dataVector.size()));
356     CHECK_AND_RETURN_RET_LOG(allocateData != nullptr, AVSESSION_ERROR, "alloc data fail");
357     std::copy(dataVector.begin(), dataVector.end(), allocateData);
358 
359     Parcel parcelData;
360     CHECK_AND_RETURN_RET_LOG(parcelData.ParseFrom(reinterpret_cast<uintptr_t>(allocateData), dataVector.size()),
361                              AVSESSION_ERROR, "parse parcel error");
362 
363     std::string titleDate = parcelData.ReadString();
364     title = std::string(titleDate);
365     return AVSESSION_SUCCESS;
366 }
367 
PutExtras(const AAFwk::WantParams & extras)368 int32_t RemoteSessionSyncerImpl::PutExtras(const AAFwk::WantParams& extras)
369 {
370     AVSESSION_TRACE_SYNC_START("RemoteSessionSyncerImpl::PutExtras");
371     Parcel data;
372     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&extras), ERR_MARSHALLING, "Write extras failed");
373 
374     uint8_t *parcelData = reinterpret_cast<uint8_t*>(data.GetData());
375     std::vector<uint8_t> dataVector(data.GetDataSize());
376     std::copy(parcelData, parcelData + data.GetDataSize(), dataVector.begin());
377 
378     CHECK_AND_RETURN_RET_LOG(PutData(EXTRAS_KEY, dataVector) == AVSESSION_SUCCESS,
379         AVSESSION_ERROR, "put data error");
380     return AVSESSION_SUCCESS;
381 }
382 
GetExtras(AAFwk::WantParams & extras)383 int32_t RemoteSessionSyncerImpl::GetExtras(AAFwk::WantParams& extras)
384 {
385     std::vector<uint8_t> dataVector;
386     CHECK_AND_RETURN_RET_LOG(GetData(EXTRAS_KEY, dataVector) == AVSESSION_SUCCESS,
387         AVSESSION_ERROR, "get data error");
388     CHECK_AND_RETURN_RET_LOG(dataVector.size() <= RECEIVE_DATA_SIZE_MAX, AVSESSION_ERROR, "get data size over range");
389     DefaultAllocator allocator;
390     uint8_t *allocateData = reinterpret_cast<uint8_t*>(allocator.Alloc(dataVector.size()));
391     CHECK_AND_RETURN_RET_LOG(allocateData != nullptr, AVSESSION_ERROR, "alloc data fail");
392     std::copy(dataVector.begin(), dataVector.end(), allocateData);
393 
394     Parcel parcelData;
395     CHECK_AND_RETURN_RET_LOG(parcelData.ParseFrom(reinterpret_cast<uintptr_t>(allocateData), dataVector.size()),
396         AVSESSION_ERROR, "parse parcel error");
397     AAFwk::WantParams *extrasData = parcelData.ReadParcelable<AAFwk::WantParams>();
398     if (extrasData == nullptr) {
399         SLOGE("GetSessionEvent: read parcelable sessionEvent failed");
400         delete extrasData;
401         return AVSESSION_ERROR;
402     }
403     extras = AAFwk::WantParams(*extrasData);
404 
405     delete extrasData;
406     return AVSESSION_SUCCESS;
407 }
408 
RegisterDataNotifier(const ObjectDataNotifier & notifier)409 int32_t RemoteSessionSyncerImpl::RegisterDataNotifier(const ObjectDataNotifier& notifier)
410 {
411     CHECK_AND_RETURN_RET_LOG(objectStore_ != nullptr && object_ != nullptr, AVSESSION_ERROR,
412                              "objectStore_ or object_ is nullptr");
413     objectDataNotifier_ = notifier;
414     objectStore_->Watch(object_, shared_from_this());
415     return AVSESSION_SUCCESS;
416 }
417 
RegisterDisconnectNotifier(const ObjectDisconnectNotifier & notifier)418 int32_t RemoteSessionSyncerImpl::RegisterDisconnectNotifier(const ObjectDisconnectNotifier& notifier)
419 {
420     CHECK_AND_RETURN_RET_LOG(objectStore_ != nullptr, AVSESSION_ERROR, "objectStore_ is nullptr");
421     objectDisconnectNotifier_ = notifier;
422     objectStore_->SetStatusNotifier(shared_from_this());
423     return AVSESSION_SUCCESS;
424 }
425 
Destroy()426 void RemoteSessionSyncerImpl::Destroy()
427 {
428     auto ret = objectStore_->UnWatch(object_);
429     CHECK_AND_RETURN_LOG(ret == ObjectStore::SUCCESS, "UnWatch error");
430     ret = objectStore_->DeleteObject(objectName_);
431     CHECK_AND_RETURN_LOG(ret == ObjectStore::SUCCESS, "DeleteObject error");
432     SLOGI("Destroy");
433 }
434 
~RemoteSessionSyncerImpl()435 RemoteSessionSyncerImpl::~RemoteSessionSyncerImpl()
436 {
437     SLOGI("RemoteSessionSyncerImpl");
438 }
439 } // namespace OHOS::AVSession