1 /* 2 * Copyright (c) 2023 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 OHOS_AVROUTER_IMPL_H 17 #define OHOS_AVROUTER_IMPL_H 18 19 #include "av_router.h" 20 #include "avcast_provider_manager.h" 21 #include "hw_cast_provider.h" 22 #include "avsession_event_handler.h" 23 24 namespace OHOS::AVSession { 25 class AVRouterImpl : public AVRouter { 26 class CastSessionListener : public IAVCastSessionStateListener { 27 public: CastSessionListener(AVRouterImpl * ptr)28 explicit CastSessionListener(AVRouterImpl *ptr) 29 { 30 ptr_ = ptr; 31 } 32 OnCastStateChange(int32_t castState,DeviceInfo deviceInfo)33 void OnCastStateChange(int32_t castState, DeviceInfo deviceInfo) 34 { 35 ptr_->OnCastStateChange(castState, deviceInfo); 36 } 37 OnCastEventRecv(int32_t errorCode,std::string & errorMsg)38 void OnCastEventRecv(int32_t errorCode, std::string& errorMsg) 39 { 40 ptr_->OnCastEventRecv(errorCode, errorMsg); 41 } 42 43 AVRouterImpl *ptr_; 44 }; 45 public: 46 AVRouterImpl(); 47 48 int32_t Init(IAVSessionServiceListener *servicePtr) override; 49 50 int32_t GetLocalDeviceType(); 51 52 bool Release() override; 53 54 int32_t StartDeviceLogging(int32_t fd, uint32_t maxSize) override; 55 56 int32_t StopDeviceLogging() override; 57 58 int32_t StartCastDiscovery(int32_t castDeviceCapability, std::vector<std::string> drmSchemes) override; 59 60 int32_t StopCastDiscovery() override; 61 62 bool IsStopCastDiscovery(pid_t pid) override; 63 64 int32_t SetDiscoverable(const bool enable) override; 65 66 int32_t OnDeviceAvailable(OutputDeviceInfo& castOutputDeviceInfo) override; 67 68 int32_t OnDeviceLogEvent(const DeviceLogEventCode eventId, const int64_t param) override; 69 70 int32_t OnDeviceOffline(const std::string& deviceId) override; 71 72 int32_t OnDeviceStateChange(const DeviceState& deviceState) override; 73 74 void ReleaseCurrentCastSession() override; 75 76 int32_t OnCastSessionCreated(const int32_t castId) override; 77 78 int32_t OnCastServerDied(int32_t providerNumber) override; 79 80 std::shared_ptr<IAVCastControllerProxy> GetRemoteController(const int64_t castHandle) override; 81 82 int64_t StartCast(const OutputDeviceInfo& outputDeviceInfo, 83 std::pair<std::string, std::string>& serviceNameStatePair, std::string sessionId) override; 84 85 int32_t AddDevice(const int32_t castId, const OutputDeviceInfo& outputDeviceInfo, 86 uint32_t spid) override; 87 88 int32_t StopCast(const int64_t castHandle, bool continuePlay = false) override; 89 90 int32_t StopCastSession(const int64_t castHandle) override; 91 92 int32_t RegisterCallback(int64_t castHandleconst, 93 std::shared_ptr<IAVRouterListener> callback, std::string sessionId, DeviceInfo deviceInfo) override; 94 95 int32_t UnRegisterCallback(int64_t castHandleconst, 96 std::shared_ptr<IAVRouterListener> callback, std::string sessionId) override; 97 98 int32_t SetServiceAllConnectState(int64_t castHandle, DeviceInfo deviceInfo) override; 99 100 int32_t GetRemoteNetWorkId(int64_t castHandle, std::string deviceId, std::string &networkId) override; 101 102 int32_t GetRemoteDrmCapabilities(int64_t castHandle, std::string deviceId, 103 std::vector<std::string> &drmCapabilities) override; 104 105 int64_t GetMirrorCastHandle() override; 106 107 void OnCastStateChange(int32_t castState, DeviceInfo deviceInfo); 108 109 void OnCastEventRecv(int32_t errorCode, std::string& errorMsg); 110 111 void DisconnectOtherSession(std::string sessionId, DeviceInfo deviceInfo) override; 112 113 bool IsInMirrorToStreamState() override; 114 115 bool IsRemoteCasting() override; 116 117 void UpdateConnectState(int32_t castState); 118 119 protected: 120 121 private: 122 std::recursive_mutex servicePtrLock_; 123 IAVSessionServiceListener *servicePtr_ = nullptr; 124 std::recursive_mutex providerManagerLock_; 125 std::map<int32_t, std::shared_ptr<AVCastProviderManager>> providerManagerMap_; 126 std::pair<std::string, std::string> castServiceNameStatePair_; 127 const std::string deviceStateConnection = "CONNECT_SUCC"; 128 const int64_t noMirrorCastHandle_ = -1; 129 int32_t providerNumber_ = 0; 130 std::map<int64_t, CastHandleInfo> castHandleToInfoMap_; 131 std::shared_ptr<HwCastProvider> hwProvider_; 132 std::map<std::string, std::shared_ptr<IAVRouterListener>> mirrorSessionMap_; 133 int32_t providerNumberEnableDefault_ = 1; 134 int32_t providerNumberDisable_ = 0; 135 bool cacheStartDiscovery_ = false; 136 bool cacheStartDeviceLogging_ = false; 137 int32_t cacheCastDeviceCapability_ = -1; 138 std::vector<std::string> cacheDrmSchemes_; 139 std::unordered_set<pid_t> cacheStartDiscoveryPids_; 140 std::shared_ptr<CastSessionListener> castSessionListener_; 141 int32_t disconnectStateFromCast_ = 5; 142 int32_t connectStateFromCast_ = 6; 143 const int32_t castEngineServiceRestartWaitTime = 100; 144 int32_t deviceType_ = -1; 145 std::atomic<bool> isInMirrorToStream_ = false; 146 std::atomic<bool> isRemoteCasting_ = false; 147 }; 148 } // namespace OHOS::AVSession 149 #endif // OHOS_AVROUTER_IMPL_H 150