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 "data_ability_observer_stub.h" 24 #include "dm_constants.h" 25 #include "i_wifi_device_callback.h" 26 #include "interaction/device_kit/dm_kit.h" 27 #include "interaction/scene/base_scene.h" 28 #include "system_ability_status_change_stub.h" 29 #include "utils/data_share_helper.h" 30 #include "utils/utils.h" 31 #include "wfd_def.h" 32 #include "wfd_msg.h" 33 #include "wfd_trust_list_manager.h" 34 #include "wifi_device.h" 35 #include "wifi_p2p.h" 36 37 namespace OHOS { 38 namespace Sharing { 39 40 class WfdSinkScene final : public BaseScene, 41 public std::enable_shared_from_this<WfdSinkScene> { 42 public: 43 WfdSinkScene(); 44 ~WfdSinkScene(); 45 46 public: 47 class WfdP2pCallback : public Wifi::IWifiP2pCallback { 48 public: AsObject()49 sptr<IRemoteObject> AsObject() override { return nullptr; } 50 51 public: WfdP2pCallback(std::weak_ptr<WfdSinkScene> parent)52 explicit WfdP2pCallback(std::weak_ptr<WfdSinkScene> parent) : parent_(parent) {} 53 54 void OnP2pStateChanged(int32_t state) override; 55 void OnP2pPersistentGroupsChanged(void) override; 56 void OnP2pDiscoveryChanged(bool isChange) override; 57 void OnP2pGcJoinGroup(const OHOS::Wifi::GcInfo &info) override; 58 void OnP2pGcLeaveGroup(const OHOS::Wifi::GcInfo &info) override; 59 void OnP2pThisDeviceChanged(const Wifi::WifiP2pDevice &device) override; 60 void OnP2pConnectionChanged(const Wifi::WifiP2pLinkedInfo &info) override; 61 void OnConfigChanged(Wifi::CfgType type, char *data, int32_t dataLen) override; 62 void OnP2pPeersChanged(const std::vector<Wifi::WifiP2pDevice> &device) override; 63 void OnP2pPrivatePeersChanged(const std::string &priWfdInfo) override; 64 void OnP2pActionResult(Wifi::P2pActionCallback action, Wifi::ErrCode code) override; 65 void OnP2pServicesChanged(const std::vector<Wifi::WifiP2pServiceInfo> &srvInfo) override; 66 void OnP2pChrErrCodeReport(const int errCode) override; 67 68 private: 69 std::weak_ptr<WfdSinkScene> parent_; 70 WfdTrustListManager wfdTrustListManager_; 71 }; 72 73 class WifiCallback : public Wifi::IWifiDeviceCallBack { 74 public: AsObject()75 sptr<IRemoteObject> AsObject() override { return nullptr; } 76 77 public: WifiCallback(std::weak_ptr<WfdSinkScene> parent)78 explicit WifiCallback(std::weak_ptr<WfdSinkScene> parent) : parent_(parent) {} 79 80 void OnWifiStateChanged(int state) override; 81 void OnWifiConnectionChanged(int state, const OHOS::Wifi::WifiLinkedInfo &info) override; 82 void OnWifiRssiChanged(int rssi) override; 83 void OnWifiWpsStateChanged(int state, const std::string &pinCode) override; 84 void OnStreamChanged(int direction) override; 85 void OnDeviceConfigChanged(OHOS::Wifi::ConfigChange value) override; 86 87 private: 88 std::weak_ptr<WfdSinkScene> parent_; 89 }; 90 91 friend class WfdP2pCallback; 92 93 class WfdSystemAbilityListener : public SystemAbilityStatusChangeStub { 94 public: WfdSystemAbilityListener(std::weak_ptr<WfdSinkScene> scene)95 explicit WfdSystemAbilityListener(std::weak_ptr<WfdSinkScene> scene) : scene_(scene) {} 96 97 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 98 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 99 100 private: 101 std::weak_ptr<WfdSinkScene> scene_; 102 }; 103 104 class DeviceNameObserver : public AAFwk::DataAbilityObserverStub { 105 public: DeviceNameObserver(std::weak_ptr<WfdSinkScene> scene)106 explicit DeviceNameObserver(std::weak_ptr<WfdSinkScene> scene) : scene_(scene) {} 107 void OnChange() override; 108 109 private: 110 std::weak_ptr<WfdSinkScene> scene_; 111 }; 112 113 protected: SetIpcAdapter(const std::weak_ptr<IpcMsgAdapter> & adapter)114 void SetIpcAdapter(const std::weak_ptr<IpcMsgAdapter> &adapter) final 115 { 116 SHARING_LOGD("trace."); 117 ipcAdapter_ = adapter; 118 auto ipcAdapter = ipcAdapter_.lock(); 119 if (ipcAdapter) { 120 ipcAdapter->SetListener(shared_from_this()); 121 } 122 } 123 124 protected: 125 void Release(); 126 127 // impl BaseScene 128 void Initialize() final; 129 void OnRemoteDied() final; 130 void OnInnerEvent(SharingEvent &event) final; 131 void OnInnerError(uint32_t contextId, uint32_t agentId, SharingErrorCode errorCode, 132 std::string message = "wfd inner error") final; 133 void OnInnerError(std::string deviceId, SharingErrorCode errorCode, std::string message = "wfd inner error"); 134 void OnInnerDestroy(uint32_t contextId, uint32_t agentId, AgentType agentType) final; 135 136 // interactionMgr -> interaction -> scene directly 137 void OnDomainMsg(std::shared_ptr<BaseDomainMsg> &msg) final; 138 139 // impl IpcMsgAdapterListener 140 void OnRequest(std::shared_ptr<BaseMsg> msg, std::shared_ptr<BaseMsg> &reply) final; 141 142 private: 143 void InitP2pName(); 144 void RegisterP2pListener(); 145 void RegisterWifiStatusChangeListener(); 146 147 void WfdP2pStop(); 148 void WfdP2pStart(); 149 150 void ErrorCodeFiltering(int32_t &code); 151 void P2pRemoveClient(ConnectionInfo &connectionInfo); 152 153 void OnP2pPeerDisconnected(std::string &mac); 154 void OnP2pPeerConnected(ConnectionInfo &connectionInfo); 155 void OnConnectionChanged(ConnectionInfo &connectionInfo); 156 void OnP2pPeerDisconnected(ConnectionInfo &connectionInfo); 157 158 void OnDecoderDied(ConnectionInfo &connectionInfo); 159 void OnDecoderAccelerationDone(ConnectionInfo &connectionInfo); 160 161 void OnWifiAbilityResume(); 162 void OnWifiAbilityDied(); 163 void RegisterWfdAbilityListener(); 164 void UnRegisterWfdAbilityListener(); 165 void RegisterDevNameObserver(); 166 void UnRegisterDevNameObserver(); 167 168 int32_t HandleStop(std::shared_ptr<WfdSinkStopReq> &msg, std::shared_ptr<WfdCommonRsp> &reply); 169 int32_t HandleStart(std::shared_ptr<WfdSinkStartReq> &msg, std::shared_ptr<WfdCommonRsp> &reply); 170 171 int32_t HandleSetSceneType(std::shared_ptr<SetSceneTypeReq> &msg, std::shared_ptr<WfdCommonRsp> &reply); 172 int32_t HandleSetMediaFormat(std::shared_ptr<SetMediaFormatReq> &msg, std::shared_ptr<WfdCommonRsp> &reply); 173 int32_t HandleAppendSurface(std::shared_ptr<WfdAppendSurfaceReq> &msg, std::shared_ptr<WfdCommonRsp> &reply); 174 int32_t HandleRemoveSurface(std::shared_ptr<WfdRemoveSurfaceReq> &msg, std::shared_ptr<WfdCommonRsp> &reply); 175 176 int32_t HandleMute(std::shared_ptr<MuteReq> &msg, std::shared_ptr<WfdCommonRsp> &reply); 177 int32_t HandlePlay(std::shared_ptr<WfdPlayReq> &msg, std::shared_ptr<WfdCommonRsp> &reply); 178 int32_t HandleUnMute(std::shared_ptr<UnMuteReq> &msg, std::shared_ptr<WfdCommonRsp> &reply); 179 int32_t HandlePause(std::shared_ptr<WfdPauseReq> &msg, std::shared_ptr<WfdCommonRsp> &reply); 180 int32_t HandleClose(std::shared_ptr<WfdCloseReq> &msg, std::shared_ptr<WfdCommonRsp> &reply); 181 int32_t HandleGetConfig(std::shared_ptr<GetSinkConfigReq> &msg, std::shared_ptr<GetSinkConfigRsp> &reply); 182 183 int32_t HandleGetBoundDevices(std::shared_ptr<WfdGetBoundDevicesReq> &msg, 184 std::shared_ptr<WfdGetBoundDevicesRsp> &reply); 185 int32_t HandleDeleteBoundDevice(std::shared_ptr<WfdDeleteBoundDeviceReq> &msg, 186 std::shared_ptr<WfdCommonRsp> &reply); 187 188 private: 189 std::atomic_bool isSinkRunning_ = false; 190 std::atomic_bool isInitialized_ = false; 191 192 int32_t ctrlPort_ = DEFAULT_WFD_CTRLPORT; 193 int32_t surfaceMaximum_ = SURFACE_MAX_NUMBER; 194 int32_t accessDevMaximum_ = ACCESS_DEV_MAX_NUMBER; 195 int32_t foregroundMaximum_ = FOREGROUND_SURFACE_MAX_NUMBER; 196 197 std::string localIp_; 198 ConnectionInfo currentConnectDev_; 199 std::mutex mutex_; 200 std::shared_ptr<Wifi::WifiP2p> p2pInstance_; 201 std::map<uint64_t, std::shared_ptr<DevSurfaceItem>> devSurfaceItemMap_; 202 std::unordered_map<std::string, std::shared_ptr<ConnectionInfo>> devConnectionMap_; 203 204 CodecId audioCodecId_ = CodecId::CODEC_AAC; 205 CodecId videoCodecId_ = CodecId::CODEC_H264; 206 AudioFormat audioFormatId_ = AudioFormat::AUDIO_NONE; 207 VideoFormat videoFormatId_ = VideoFormat::VIDEO_NONE; 208 WfdParamsInfo wfdParamsInfo_; 209 WfdTrustListManager wfdTrustListManager_; 210 211 sptr<AAFwk::IDataAbilityObserver> deviceNameObserver_ = nullptr; 212 sptr<ISystemAbilityStatusChange> sysAbilityListener_ = nullptr; 213 }; 214 215 } // namespace Sharing 216 } // namespace OHOS 217 #endif 218