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_WFD_SINK_SCENE_H 17 #define OHOS_SHARING_WFD_SINK_SCENE_H 18 19 #include <mutex> 20 #include <unordered_map> 21 #include <unordered_set> 22 #include "agent/agent_def.h" 23 #include "interaction/scene/base_scene.h" 24 #include "wfd_def.h" 25 #include "wfd_msg.h" 26 #include "wifi_p2p.h" 27 28 namespace OHOS { 29 namespace Sharing { 30 31 class WfdSinkScene final : public BaseScene, 32 public std::enable_shared_from_this<WfdSinkScene> { 33 public: 34 WfdSinkScene(); 35 ~WfdSinkScene(); 36 37 public: 38 class WfdP2pCallback : public Wifi::IWifiP2pCallback { 39 public: AsObject()40 sptr<IRemoteObject> AsObject() override { return nullptr; } 41 42 public: WfdP2pCallback(std::weak_ptr<WfdSinkScene> parent)43 explicit WfdP2pCallback(std::weak_ptr<WfdSinkScene> parent) : parent_(parent) {} 44 45 void OnP2pStateChanged(int32_t state) override; 46 void OnP2pPersistentGroupsChanged(void) override; 47 void OnP2pDiscoveryChanged(bool isChange) override; 48 void OnP2pGcJoinGroup(const OHOS::Wifi::GcInfo &info) override; 49 void OnP2pGcLeaveGroup(const OHOS::Wifi::GcInfo &info) override; 50 void OnP2pThisDeviceChanged(const Wifi::WifiP2pDevice &device) override; 51 void OnP2pConnectionChanged(const Wifi::WifiP2pLinkedInfo &info) override; 52 void OnConfigChanged(Wifi::CfgType type, char *data, int32_t dataLen) override; 53 void OnP2pPeersChanged(const std::vector<Wifi::WifiP2pDevice> &device) override; 54 void OnP2pPrivatePeersChanged(const std::string &priWfdInfo) override; 55 void OnP2pActionResult(Wifi::P2pActionCallback action, Wifi::ErrCode code) override; 56 void OnP2pServicesChanged(const std::vector<Wifi::WifiP2pServiceInfo> &srvInfo) override; 57 58 private: 59 std::weak_ptr<WfdSinkScene> parent_; 60 }; 61 62 friend class WfdP2pCallback; 63 64 protected: SetIpcAdapter(const std::weak_ptr<IpcMsgAdapter> & adapter)65 void SetIpcAdapter(const std::weak_ptr<IpcMsgAdapter> &adapter) final 66 { 67 SHARING_LOGD("trace."); 68 ipcAdapter_ = adapter; 69 auto ipcAdapter = ipcAdapter_.lock(); 70 if (ipcAdapter) { 71 ipcAdapter->SetListener(shared_from_this()); 72 } 73 } 74 75 protected: 76 void Release(); 77 78 // impl BaseScene 79 void Initialize() final; 80 void OnRemoteDied() final; 81 void OnInnerEvent(SharingEvent &event) final; 82 void OnInnerError(uint32_t contextId, uint32_t agentId, SharingErrorCode errorCode, 83 std::string message = "wfd inner error") final; 84 void OnInnerError(std::string deviceId, SharingErrorCode errorCode, std::string message = "wfd inner error"); 85 void OnInnerDestroy(uint32_t contextId, uint32_t agentId, AgentType agentType) final; 86 87 // interactionMgr -> interaction -> scene directly 88 void OnDomainMsg(std::shared_ptr<BaseDomainMsg> &msg) final; 89 90 // impl IpcMsgAdapterListener 91 void OnRequest(std::shared_ptr<BaseMsg> msg, std::shared_ptr<BaseMsg> &reply) final; 92 93 private: 94 void WfdP2pStop(); 95 void WfdP2pStart(); 96 97 void ErrorCodeFiltering(int32_t &code); 98 void P2pRemoveClient(ConnectionInfo &connectionInfo); 99 100 void OnP2pPeerDisconnected(std::string &mac); 101 void OnP2pPeerConnected(ConnectionInfo &connectionInfo); 102 void OnConnectionChanged(ConnectionInfo &connectionInfo); 103 void OnP2pPeerDisconnected(ConnectionInfo &connectionInfo); 104 105 void OnDecoderDied(ConnectionInfo &connectionInfo); 106 void OnDecoderAccelerationDone(ConnectionInfo &connectionInfo); 107 108 int32_t HandleStop(std::shared_ptr<WfdSinkStopReq> &msg, std::shared_ptr<WfdCommonRsp> &reply); 109 int32_t HandleStart(std::shared_ptr<WfdSinkStartReq> &msg, std::shared_ptr<WfdCommonRsp> &reply); 110 111 int32_t HandleSetSceneType(std::shared_ptr<SetSceneTypeReq> &msg, std::shared_ptr<WfdCommonRsp> &reply); 112 int32_t HandleSetMediaFormat(std::shared_ptr<SetMediaFormatReq> &msg, std::shared_ptr<WfdCommonRsp> &reply); 113 int32_t HandleAppendSurface(std::shared_ptr<WfdAppendSurfaceReq> &msg, std::shared_ptr<WfdCommonRsp> &reply); 114 int32_t HandleRemoveSurface(std::shared_ptr<WfdRemoveSurfaceReq> &msg, std::shared_ptr<WfdCommonRsp> &reply); 115 116 int32_t HandleMute(std::shared_ptr<MuteReq> &msg, std::shared_ptr<WfdCommonRsp> &reply); 117 int32_t HandlePlay(std::shared_ptr<WfdPlayReq> &msg, std::shared_ptr<WfdCommonRsp> &reply); 118 int32_t HandleUnMute(std::shared_ptr<UnMuteReq> &msg, std::shared_ptr<WfdCommonRsp> &reply); 119 int32_t HandlePause(std::shared_ptr<WfdPauseReq> &msg, std::shared_ptr<WfdCommonRsp> &reply); 120 int32_t HandleClose(std::shared_ptr<WfdCloseReq> &msg, std::shared_ptr<WfdCommonRsp> &reply); 121 int32_t HandleGetConfig(std::shared_ptr<GetSinkConfigReq> &msg, std::shared_ptr<GetSinkConfigRsp> &reply); 122 123 private: 124 std::atomic_bool isSinkRunning_ = false; 125 126 int32_t ctrlPort_ = DEFAULT_WFD_CTRLPORT; 127 int32_t surfaceMaximum_ = SURFACE_MAX_NUMBER; 128 int32_t accessDevMaximum_ = ACCESS_DEV_MAX_NUMBER; 129 int32_t foregroundMaximum_ = FOREGROUND_SURFACE_MAX_NUMBER; 130 131 std::mutex mutex_; 132 std::shared_ptr<Wifi::WifiP2p> p2pInstance_; 133 std::map<uint64_t, std::shared_ptr<DevSurfaceItem>> devSurfaceItemMap_; 134 std::unordered_map<std::string, std::shared_ptr<ConnectionInfo>> devConnectionMap_; 135 136 CodecId audioCodecId_ = CodecId::CODEC_AAC; 137 CodecId videoCodecId_ = CodecId::CODEC_H264; 138 AudioFormat audioFormatId_ = AudioFormat::AUDIO_48000_16_2; 139 VideoFormat videoFormatId_ = VideoFormat::VIDEO_1920X1080_25; 140 }; 141 142 } // namespace Sharing 143 } // namespace OHOS 144 #endif 145