1 /* 2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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_SHARING_AGENT_H 17 #define OHOS_SHARING_AGENT_H 18 19 #include <map> 20 #include "agent/session/base_session.h" 21 #include "agent_def.h" 22 #include "common/event_comm.h" 23 24 namespace OHOS { 25 namespace Sharing { 26 27 class IAgentListener { 28 public: 29 using Ptr = std::shared_ptr<IAgentListener>; 30 31 virtual ~IAgentListener() = default; 32 33 virtual void OnAgentNotify(AgentStatusMsg::Ptr &statusMsg) = 0; 34 }; 35 36 class Agent : public IdentifierInfo, 37 public ISessionListener, 38 public HandleEventBase, 39 public std::enable_shared_from_this<Agent> { 40 public: 41 using Ptr = std::shared_ptr<Agent>; 42 43 explicit Agent(AgentType agentType); 44 SetDestroy()45 void SetDestroy() 46 { 47 destroy_ = true; 48 } 49 GetDestroy()50 bool GetDestroy() 51 { 52 return destroy_; 53 } 54 UpdateMediaChannelId(uint32_t mediaChannelId)55 void UpdateMediaChannelId(uint32_t mediaChannelId) 56 { 57 mediaChannelId_ = mediaChannelId; 58 } 59 SetAgentListener(std::weak_ptr<IAgentListener> agentListener)60 void SetAgentListener(std::weak_ptr<IAgentListener> agentListener) 61 { 62 agentListener_ = agentListener; 63 } 64 GetMediaChannelId()65 uint32_t GetMediaChannelId() 66 { 67 return mediaChannelId_; 68 } 69 GetAgentType()70 AgentType GetAgentType() 71 { 72 return agentType_; 73 } 74 75 public: 76 int32_t HandleEvent(SharingEvent &event) override; 77 SharingErrorCode CreateSession(const std::string &className); 78 79 protected: 80 void PushNextStep(SessionStatusMsg::Ptr &statusMsg); 81 void HandleSessionError(SessionStatusMsg::Ptr &statusMsg); 82 void UpdateSessionStatus(SessionStatusMsg::Ptr &statusMsg); 83 void PopNextStep(AgentRunStep step, AgentRunningStatus status); 84 void OnSessionNotify(SessionStatusMsg::Ptr &statusMsg) override; 85 void SetRunningStatus(AgentRunStep step, AgentRunningStatus status); 86 87 AgentRunStep GetRunStep(EventType eventType); 88 AgentRunStepWeight GetRunStepWeight(AgentRunStep runStep); 89 SharingErrorCode CheckRunStep(SharingEvent &event, bool &isCached); 90 91 private: 92 uint32_t NotifyPrivateEvent(SessionStatusMsg::Ptr &statusMsg); 93 94 void HandleProsumerState(SessionStatusMsg::Ptr &statusMsg); 95 void HandleProsumerError(SessionStatusMsg::Ptr &statusMsg); 96 97 void SendContextEvent(SessionStatusMsg::Ptr &statusMsg, EventType eventType); 98 void SendChannelEvent(SessionStatusMsg::Ptr &statusMsg, EventType eventType); 99 void SendInteractionEvent(SessionStatusMsg::Ptr &statusMsg, EventType eventType); 100 void SendChannelSetVolumeEvent(SessionStatusMsg::Ptr &statusMsg, EventType eventType); 101 void SendChannelSceneTypeEvent(SessionStatusMsg::Ptr &statusMsg, EventType eventType); 102 void SendChannelKeyRedirectEvent(SessionStatusMsg::Ptr &statusMsg, EventType eventType); 103 void SendChannelAppendSurfaceEvent(SessionStatusMsg::Ptr &statusMsg, EventType eventType); 104 void SendChannelRemoveSurfaceEvent(SessionStatusMsg::Ptr &statusMsg, EventType eventType); 105 106 protected: 107 bool destroy_ = false; 108 uint32_t prosumerId_ = INVALID_ID; 109 uint32_t mediaChannelId_ = INVALID_ID; 110 111 std::mutex runStepMutex_; 112 std::weak_ptr<IAgentListener> agentListener_; 113 std::map<AgentRunStepKey, SharingEvent> runEvents_; 114 115 AgentType agentType_ = SRC_AGENT; 116 BaseSession::Ptr session_ = nullptr; 117 AgentRunStep runStep_ = AGENT_STEP_IDLE; 118 AgentRunningStatus runningStatus_ = AGENT_STATUS_IDLE; 119 }; 120 121 } // namespace Sharing 122 } // namespace OHOS 123 #endif