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 #ifndef OHOS_REMOTE_SESSION_SYNCER_IMPL_H
17 #define OHOS_REMOTE_SESSION_SYNCER_IMPL_H
18 
19 #include "avsession_info.h"
20 #include "avmeta_data.h"
21 #include "avplayback_state.h"
22 #include "avmedia_description.h"
23 #include "avqueue_item.h"
24 #include "avcontrol_command.h"
25 #include "remote_session_syncer.h"
26 #include "distributed_object.h"
27 #include "distributed_objectstore.h"
28 #include "objectstore_errors.h"
29 #include "avsession_errors.h"
30 #include "avsession_log.h"
31 
32 namespace OHOS::AVSession {
33 using namespace ObjectStore;
34 class RemoteSessionSyncerImpl : public RemoteSessionSyncer, public ObjectWatcher, public StatusNotifier,
35     public std::enable_shared_from_this<RemoteSessionSyncerImpl> {
36 public:
37     RemoteSessionSyncerImpl(const std::string& sourceSessionId, const std::string& sourceDevice,
38                             const std::string& sinkDevice);
39 
40     int32_t Init() override;
41 
42     int32_t PutAVMetaData(const AVMetaData& metaData) override;
43 
44     int32_t GetAVMetaData(AVMetaData& metaData) override;
45 
46     int32_t PutAVPlaybackState(const AVPlaybackState& state) override;
47 
48     int32_t GetAVPlaybackState(AVPlaybackState& state) override;
49 
50     int32_t PutControlCommand(const AVControlCommand& command) override;
51 
52     int32_t GetControlCommand(AVControlCommand& command) override;
53 
54     int32_t PutSessionEvent(const std::string& event, const AAFwk::WantParams& args) override;
55 
56     int32_t GetSessionEvent(std::string& event, AAFwk::WantParams& args) override;
57 
58     int32_t PutCommonCommand(const std::string& commonCommand, const AAFwk::WantParams& commandArgs) override;
59 
60     int32_t GetCommonCommand(std::string& commonCommand, AAFwk::WantParams& commandArgs) override;
61 
62     int32_t PutAVQueueItems(const std::vector<AVQueueItem>& items) override;
63 
64     int32_t GetAVQueueItems(std::vector<AVQueueItem>& items) override;
65 
66     int32_t PutAVQueueTitle(const std::string& title) override;
67 
68     int32_t GetAVQueueTitle(std::string& title) override;
69 
70     int32_t PutExtras(const AAFwk::WantParams& extras) override;
71 
72     int32_t GetExtras(AAFwk::WantParams& extras) override;
73 
74     int32_t RegisterDataNotifier(const ObjectDataNotifier& notifier) override;
75 
76     int32_t RegisterDisconnectNotifier(const ObjectDisconnectNotifier& notifier) override;
77 
78     void OnChanged(const std::string &sessionId, const std::vector<std::string> &keys) override;
79 
80     void OnChanged(const std::string &name, const std::string &networkId, const std::string &onlineStatus) override;
81 
82     void Destroy() override;
83 
84     ~RemoteSessionSyncerImpl() override;
85 
86     static constexpr char METADATA_KEY[] = "metaData";
87     static constexpr char PLAYBACK_STATE_KEY[] = "playbackState";
88     static constexpr char CONTROL_COMMAND_KEY[] = "controlCommand";
89     static constexpr char COMMON_COMMAND_KEY[] = "commonCommand";
90     static constexpr char SESSION_EVENT_KEY[] = "sessionEvent";
91     static constexpr char QUEUE_ITEMS_KEY[] = "queueItems";
92     static constexpr char QUEUE_TITLE_KEY[] = "queueTitle";
93     static constexpr char EXTRAS_KEY[] = "extras";
94     static constexpr int RECEIVE_DATA_SIZE_MAX = 500 * 1024;
95     const std::map<std::string, SessionDataCategory> categoryMap {
96             {METADATA_KEY, SESSION_DATA_META},
97             {PLAYBACK_STATE_KEY, SESSION_DATA_PLAYBACK_STATE},
98             {CONTROL_COMMAND_KEY, SESSION_DATA_CONTROL_COMMAND},
99             {COMMON_COMMAND_KEY, SESSION_DATA_COMMON_COMMAND},
100             {SESSION_EVENT_KEY, SESSION_DATA_SET_EVENT},
101             {QUEUE_ITEMS_KEY, SESSION_DATA_QUEUE_ITEMS},
102             {QUEUE_TITLE_KEY, SESSION_DATA_QUEUE_TITLE},
103             {EXTRAS_KEY, SESSION_DATA_EXTRAS},
104     };
105 
106 private:
107     int32_t PutData(const std::string &key, std::vector<uint8_t> &data);
108 
109     int32_t GetData(const std::string &key, std::vector<uint8_t> &data);
110 
111     std::string objectName_;
112     DistributedObject *object_;
113     DistributedObjectStore *objectStore_;
114     ObjectDataNotifier objectDataNotifier_;
115     ObjectDisconnectNotifier objectDisconnectNotifier_;
116     std::string sourceSessionId_;
117     std::string sourceDevice_;
118     std::string sinkDevice_;
119 };
120 } // namespace OHOS::AVSession
121 #endif // OHOS_REMOTE_SESSION_SYNCER_IMPL_H