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 AVSESSION_DISTRIBUTEDOBJECT_ADAPTER_H
17 #define AVSESSION_DISTRIBUTEDOBJECT_ADAPTER_H
18 
19 #include "distributed_objectstore.h"
20 #include "avmeta_data.h"
21 #include "avmedia_description.h"
22 #include "avqueue_item.h"
23 
24 namespace OHOS::AVSession {
25 class DistributedObjectAdapter {
26 public:
27     using ObjectCallbackType = std::function<void(const std::vector<std::string>& keys)>;
28     using ObjectDisconnectNotifier = std::function<void(const std::string& deviceId)>;
29 
30     explicit DistributedObjectAdapter(const std::string& name);
31 
32     ~DistributedObjectAdapter();
33 
34     int32_t CreateObject();
35 
36     int32_t PutData(const std::string& key, std::vector<uint8_t>& data);
37 
38     int32_t GetData(const std::string& key, std::vector<uint8_t>& data);
39 
40     void SetCallback(const ObjectCallbackType& callback);
41 
42     void SetDisconnectNotifier(const ObjectDisconnectNotifier& notifer);
43 
44     class AVSessionObjectWatcher : public ObjectStore::ObjectWatcher {
45     public:
46         explicit AVSessionObjectWatcher(const ObjectCallbackType& callback);
47         void OnChanged(const std::string &name, const std::vector<std::string> &keys) override;
48         virtual ~AVSessionObjectWatcher() = default;
49     private:
50         ObjectCallbackType callback_;
51     };
52 
53     class AVSessionStatusNotifier : public ObjectStore::StatusNotifier {
54     public:
55         explicit AVSessionStatusNotifier(const ObjectDisconnectNotifier& notifier);
56         void OnChanged(const std::string &name, const std::string &networkId,
57                        const std::string &onlineStatus) override;
58         virtual ~AVSessionStatusNotifier() = default;
59     private:
60         ObjectDisconnectNotifier notifier_;
61     };
62 
63 private:
64     ObjectStore::DistributedObject *object_ {};
65     std::string name_;
66 
67     static inline const char AVSESSION_NAME[] = "ohos.avsession";
68     static inline const char DOBJECT_KEY_META[] = "meta";
69     static inline const char DOBJECT_KEY_STATE[] = "state";
70     static inline const char DOBJECT_KEY_CMD[] = "cmd";
71 };
72 }
73 #endif // AVSESSION_DISTRIBUTEDOBJECT_ADAPTER_H