1 /* 2 * Copyright (C) 2023-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 * Description: cast session manager service class 15 * Author: zhangge 16 * Create: 2022-06-15 17 */ 18 19 #ifndef CAST_SESSION_MANAGER_SERVICE_H 20 #define CAST_SESSION_MANAGER_SERVICE_H 21 22 #include <atomic> 23 #include <map> 24 #include <mutex> 25 #include <shared_mutex> 26 27 #include "cast_session_manager_service_stub.h" 28 #include "connection_manager_listener.h" 29 #include "session.h" 30 #include "system_ability_load_callback_stub.h" 31 #include "system_ability.h" 32 33 using SharedRLock = std::shared_lock<std::shared_mutex>; 34 using SharedWLock = std::lock_guard<std::shared_mutex>; 35 36 namespace OHOS { 37 namespace CastEngine { 38 namespace CastEngineService { 39 class CastSessionManagerService : public SystemAbility, public CastSessionManagerServiceStub { 40 DECLARE_SYSTEM_ABILITY(CastSessionManagerService); 41 42 public: 43 CastSessionManagerService(int32_t saId, bool runOnCreate); 44 ~CastSessionManagerService() override; 45 46 void OnStart() override; 47 void OnStop() override; 48 void OnActive(const SystemAbilityOnDemandReason& activeReason) override; 49 50 int32_t RegisterListener(sptr<ICastServiceListenerImpl> listener) override; 51 int32_t UnregisterListener() override; 52 int32_t Release() override; 53 int32_t SetLocalDevice(const CastLocalDevice &localDevice) override; 54 int32_t CreateCastSession(const CastSessionProperty &property, sptr<ICastSessionImpl> &castSession) override; 55 int32_t SetSinkSessionCapacity(int sessionCapacity) override; 56 int32_t StartDiscovery(int protocols, std::vector<std::string> drmSchemes) override; 57 int32_t SetDiscoverable(bool enable) override; 58 int32_t StopDiscovery() override; 59 int32_t StartDeviceLogging(int32_t fd, uint32_t maxSize) override; 60 void ReleaseServiceResources(pid_t pid, uid_t uid); 61 int32_t GetCastSession(std::string sessionId, sptr<ICastSessionImpl> &castSession) override; 62 63 bool DestroyCastSession(int32_t sessionId); 64 void ReportServiceDieLocked(); 65 void ReportDeviceFound(const std::vector<std::pair<CastRemoteDevice, bool>> &deviceList); 66 void ReportSessionCreate(const sptr<ICastSessionImpl> &castSession); 67 void ReportDeviceOffline(const std::string &deviceId); 68 bool LoadSinkSA(const std::string &networkId); 69 sptr<ICastSessionImpl> GetCastSessionInner(std::string sessionId); 70 71 class CastServiceLoadCallback : public SystemAbilityLoadCallbackStub { 72 public: 73 void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject) override; 74 void OnLoadSystemAbilityFail(int32_t systemAbilityId) override; 75 }; 76 77 private: 78 class CastEngineClientDeathRecipient : public IRemoteObject::DeathRecipient { 79 public: CastEngineClientDeathRecipient(wptr<CastSessionManagerService> service,pid_t pid,uid_t uid)80 CastEngineClientDeathRecipient(wptr<CastSessionManagerService> service, pid_t pid, uid_t uid) 81 : service_(service), pid_(pid), uid_(uid) {}; 82 void OnRemoteDied(const wptr<IRemoteObject> &object) override; 83 84 private: 85 wptr<CastSessionManagerService> service_; 86 pid_t pid_; 87 uid_t uid_; 88 }; 89 90 bool CheckAndWaitDevieManagerServiceInit(); 91 92 pid_t myPid_; 93 std::shared_mutex mutex_; 94 std::map<pid_t, std::pair<sptr<ICastServiceListenerImpl>, uid_t>> listeners_; 95 CastLocalDevice localDevice_{}; 96 ServiceStatus serviceStatus_{ ServiceStatus::DISCONNECTED }; 97 int sessionCapacity_{ 0 }; 98 std::map<int32_t, sptr<ICastSessionImpl>> sessionMap_; 99 std::unordered_map<pid_t, sptr<IRemoteObject::DeathRecipient>> deathRecipientMap_; 100 std::atomic<bool> hasServer_{ false }; 101 std::atomic<bool> isUnloading_{ false }; 102 103 void AddClientDeathRecipientLocked(pid_t pid, uid_t uid, sptr<ICastServiceListenerImpl> listener); 104 void RemoveClientDeathRecipientLocked(pid_t pid, sptr<ICastServiceListenerImpl> listener); 105 bool AddListenerLocked(sptr<ICastServiceListenerImpl> listener); 106 int32_t ReleaseLocked(); 107 int32_t RemoveListenerLocked(pid_t pid); 108 void ClearListenersLocked(); 109 bool HasListenerLocked(); 110 }; 111 112 class ConnectionManagerListener : public IConnectionManagerListener { 113 public: ConnectionManagerListener(sptr<CastSessionManagerService> service)114 ConnectionManagerListener(sptr<CastSessionManagerService> service) : service_(service) {} 115 int NotifySessionIsReady() override; 116 void ReportSessionCreate(int castSessionId) override; 117 bool NotifyRemoteDeviceIsReady(int castSessionId, const CastInnerRemoteDevice &device) override; 118 void NotifyDeviceIsOffline(const std::string &deviceId) override; 119 void GrabDevice(int32_t sessionId) override; 120 int32_t GetSessionProtocolType(int sessionId, ProtocolType &protocolType) override; 121 int32_t SetSessionProtocolType(int sessionId, ProtocolType protocolType) override; 122 bool LoadSinkSA(const std::string &networkId) override; 123 124 private: 125 wptr<CastSessionManagerService> service_; 126 }; 127 } // namespace CastEngineService 128 } // namespace CastEngine 129 } // namespace OHOS 130 131 #endif