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_SOURCE_SCENE_H 17 #define OHOS_SHARING_WFD_SOURCE_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 "sharing_hisysevent.h" 25 #include "timer.h" 26 #include "wfd_def.h" 27 #include "wfd_msg.h" 28 #include "wifi_p2p.h" 29 30 namespace OHOS { 31 namespace Sharing { 32 33 class WfdSourceScene final : public BaseScene, 34 public std::enable_shared_from_this<WfdSourceScene> { 35 public: 36 WfdSourceScene(); 37 ~WfdSourceScene(); 38 39 public: 40 class WfdP2pCallback : public Wifi::IWifiP2pCallback { 41 public: AsObject()42 sptr<IRemoteObject> AsObject() override { return nullptr; } 43 44 public: WfdP2pCallback(std::weak_ptr<WfdSourceScene> scene)45 explicit WfdP2pCallback(std::weak_ptr<WfdSourceScene> scene) : scene_(scene) {} 46 47 void OnP2pStateChanged(int32_t state) override; 48 void OnP2pPersistentGroupsChanged(void) override; 49 void OnP2pDiscoveryChanged(bool isChange) override; 50 void OnP2pGcJoinGroup(const OHOS::Wifi::GcInfo &info) override; 51 void OnP2pGcLeaveGroup(const OHOS::Wifi::GcInfo &info) override; 52 void OnP2pThisDeviceChanged(const Wifi::WifiP2pDevice &device) override; 53 void OnP2pConnectionChanged(const Wifi::WifiP2pLinkedInfo &info) override; 54 void OnConfigChanged(Wifi::CfgType type, char *data, int32_t dataLen) override; 55 void OnP2pPeersChanged(const std::vector<Wifi::WifiP2pDevice> &device) override; 56 void OnP2pPrivatePeersChanged(const std::string &priWfdInfo) override; 57 void OnP2pActionResult(Wifi::P2pActionCallback action, Wifi::ErrCode code) override; 58 void OnP2pServicesChanged(const std::vector<Wifi::WifiP2pServiceInfo> &srvInfo) override; 59 60 private: 61 std::weak_ptr<WfdSourceScene> scene_; 62 }; 63 64 friend class WfdP2pCallback; 65 66 protected: SetIpcAdapter(const std::weak_ptr<IpcMsgAdapter> & adapter)67 void SetIpcAdapter(const std::weak_ptr<IpcMsgAdapter> &adapter) final 68 { 69 SHARING_LOGD("trace."); 70 ipcAdapter_ = adapter; 71 auto ipcAdapter = ipcAdapter_.lock(); 72 if (ipcAdapter) { 73 ipcAdapter->SetListener(shared_from_this()); 74 } 75 } 76 77 protected: 78 void Release(); 79 80 // impl BaseScene 81 void Initialize() final; 82 void OnRemoteDied() final; 83 void OnInnerEvent(SharingEvent &event) final; 84 void OnInnerError(uint32_t contextId, uint32_t agentId, SharingErrorCode errorCode, 85 std::string message = "wfd inner error") final; 86 void OnInnerDestroy(uint32_t contextId, uint32_t agentId, AgentType agentType) final; 87 88 // interactionMgr -> interaction -> scene directly 89 void OnDomainMsg(std::shared_ptr<BaseDomainMsg> &msg) final; 90 91 // impl IpcMsgAdapterListener 92 void OnRequest(std::shared_ptr<BaseMsg> msg, std::shared_ptr<BaseMsg> &reply) final; 93 94 private: 95 void WfdP2pStop(); 96 void WfdP2pStart(); 97 98 void ErrorCodeFiltering(int32_t &code); 99 100 void OnP2pPeerDisconnected(const std::string &mac); 101 void OnP2pPeerConnected(ConnectionInfo &connectionInfo); 102 void OnP2pPeerDisconnected(ConnectionInfo &connectionInfo); 103 104 void OnCheckWfdConnection(); 105 void OnConnectionChanged(ConnectionInfo &connectionInfo); 106 void OnDeviceFound(const std::vector<WfdCastDeviceInfo> &deviceInfos); 107 108 void SetCheckWfdConnectionTimer(); 109 void ResetCheckWfdConnectionTimer(); 110 111 int32_t CreateScreenCapture(); 112 int32_t AppendCast(const std::string &deviceId); 113 int32_t HandleDestroyScreenCapture(std::shared_ptr<DestroyScreenCaptureReq> &msg); 114 int32_t HandleStartDiscovery(std::shared_ptr<WfdSourceStartDiscoveryReq> &msg, 115 std::shared_ptr<WfdCommonRsp> &reply); 116 int32_t HandleStopDiscovery(std::shared_ptr<WfdSourceStopDiscoveryReq> &msg, std::shared_ptr<WfdCommonRsp> &reply); 117 int32_t HandleAddDevice(std::shared_ptr<WfdSourceAddDeviceReq> &msg, std::shared_ptr<WfdCommonRsp> &reply); 118 int32_t HandleRemoveDevice(std::shared_ptr<WfdSourceRemoveDeviceReq> &msg, std::shared_ptr<WfdCommonRsp> &reply); 119 120 private: 121 std::atomic_bool isSourceRunning_ = false; 122 std::atomic_bool isSourceDiscovering = false; 123 124 uint32_t timerId_ = 0; 125 uint32_t agentId_ = INVALID_ID; 126 uint32_t contextId_ = INVALID_ID; 127 128 int32_t ctrlPort_ = DEFAULT_WFD_CTRLPORT; 129 130 uint64_t screenId_ = 0; 131 132 CodecId audioCodecId_ = CodecId::CODEC_AAC; 133 CodecId videoCodecId_ = CodecId::CODEC_H264; 134 AudioFormat audioFormat_ = AudioFormat::AUDIO_48000_16_2; 135 VideoFormat videoFormat_ = VideoFormat::VIDEO_1920X1080_25; 136 137 std::mutex mutex_; 138 SharingHiSysEvent::Ptr p2pSysEvent_ = nullptr; 139 std::shared_ptr<Wifi::WifiP2p> p2pInstance_; 140 std::unique_ptr<ConnectionInfo> connDev_ = nullptr; 141 std::unique_ptr<OHOS::Utils::Timer> timer_ = std::make_unique<OHOS::Utils::Timer>("WfdConnectTimeoutTimer"); 142 }; 143 144 } // namespace Sharing 145 } // namespace OHOS 146 #endif 147