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