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 "session.h" 29 #include "system_ability.h" 30 31 using SharedRLock = std::shared_lock<std::shared_mutex>; 32 using SharedWLock = std::lock_guard<std::shared_mutex>; 33 34 namespace OHOS { 35 namespace CastEngine { 36 namespace CastEngineService { 37 class CastSessionManagerService : public SystemAbility, public CastSessionManagerServiceStub { 38 DECLARE_SYSTEM_ABILITY(CastSessionManagerService); 39 40 public: 41 CastSessionManagerService(int32_t saId, bool runOnCreate); 42 ~CastSessionManagerService() override; 43 44 void OnStart() override; 45 void OnStop() override; 46 47 int32_t RegisterListener(sptr<ICastServiceListenerImpl> listener) override; 48 int32_t UnregisterListener() override; 49 int32_t Release() override; 50 int32_t SetLocalDevice(const CastLocalDevice &localDevice) override; 51 int32_t CreateCastSession(const CastSessionProperty &property, sptr<ICastSessionImpl> &castSession) override; 52 int32_t SetSinkSessionCapacity(int sessionCapacity) override; 53 int32_t StartDiscovery(int protocols) override; 54 int32_t SetDiscoverable(bool enable) override; 55 int32_t StopDiscovery() override; 56 void ReleaseServiceResources(pid_t pid); 57 int32_t GetCastSession(std::string sessionId, sptr<ICastSessionImpl> &castSession) override; 58 59 bool DestroyCastSession(int32_t sessionId); 60 void ReportServiceDieLocked(); 61 void ReportDeviceFound(const std::vector<CastRemoteDevice> &deviceList); 62 void ReportSessionCreate(const sptr<ICastSessionImpl> &castSession); 63 void ReportDeviceOffline(const std::string &deviceId); 64 sptr<ICastSessionImpl> GetCastSessionInner(std::string sessionId); 65 66 private: 67 class CastEngineClientDeathRecipient : public IRemoteObject::DeathRecipient { 68 public: CastEngineClientDeathRecipient(wptr<CastSessionManagerService> service, pid_t pid)69 CastEngineClientDeathRecipient(wptr<CastSessionManagerService> service, pid_t pid) 70 : service_(service), pid_(pid) {}; 71 void OnRemoteDied(const wptr<IRemoteObject> &object) override; 72 73 private: 74 wptr<CastSessionManagerService> service_; 75 pid_t pid_; 76 }; 77 78 pid_t myPid_; 79 std::shared_mutex mutex_; 80 std::vector<std::pair<pid_t, sptr<ICastServiceListenerImpl>>> listeners_; 81 CastLocalDevice localDevice_{}; 82 ServiceStatus serviceStatus_{ ServiceStatus::DISCONNECTED }; 83 int sessionCapacity_{ 0 }; 84 std::map<int32_t, sptr<ICastSessionImpl>> sessionMap_; 85 std::atomic<int> sessionIndex_{ 0 }; 86 std::unordered_map<pid_t, sptr<IRemoteObject::DeathRecipient>> deathRecipientMap_; 87 std::atomic<bool> hasServer_{ false }; 88 89 void AddClientDeathRecipientLocked(pid_t pid, sptr<ICastServiceListenerImpl> listener); 90 void RemoveClientDeathRecipientLocked(pid_t pid, sptr<ICastServiceListenerImpl> listener); 91 bool AddListenerLocked(sptr<ICastServiceListenerImpl> listener); 92 int32_t ReleaseLocked(); 93 int32_t RemoveListenerLocked(pid_t pid); 94 void ClearListenersLocked(); 95 bool HasListenerLocked(); 96 }; 97 } // namespace CastEngineService 98 } // namespace CastEngine 99 } // namespace OHOS 100 101 #endif