1 /* 2 * Copyright (c) 2022-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_AVSESSION_MANAGER_IMPL_H 17 #define OHOS_AVSESSION_MANAGER_IMPL_H 18 19 #include <string> 20 #include <memory> 21 #include <mutex> 22 23 #include "iremote_object.h" 24 #include "av_session.h" 25 #include "avsession_service_proxy.h" 26 #include "avsession_info.h" 27 #include "client_death_stub.h" 28 #include "isession_listener.h" 29 #include "avsession_manager.h" 30 #include "avsession_controller.h" 31 32 namespace OHOS::AVSession { 33 class ServiceDeathRecipient; 34 class AVSessionManagerImpl : public AVSessionManager { 35 public: 36 AVSessionManagerImpl(); 37 38 std::shared_ptr<AVSession> CreateSession(const std::string& tag, int32_t type, 39 const AppExecFwk::ElementName& elementName) override; 40 41 int32_t GetAllSessionDescriptors(std::vector<AVSessionDescriptor>& descriptors) override; 42 43 int32_t CreateController(const std::string& sessionId, std::shared_ptr<AVSessionController>& controller) override; 44 45 #ifdef CASTPLUS_CAST_ENGINE_ENABLE 46 int32_t GetAVCastController(const std::string& sessionId, 47 std::shared_ptr<AVCastController>& castController) override; 48 #endif 49 50 int32_t GetActivatedSessionDescriptors(std::vector<AVSessionDescriptor>& activatedSessions) override; 51 52 int32_t GetSessionDescriptorsBySessionId(const std::string& sessionId, AVSessionDescriptor& descriptor) override; 53 54 int32_t GetHistoricalSessionDescriptors(int32_t maxSize, std::vector<AVSessionDescriptor>& descriptors) override; 55 56 int32_t GetHistoricalAVQueueInfos(int32_t maxSize, int32_t maxAppSize, 57 std::vector<AVQueueInfo>& avQueueInfo) override; 58 59 int32_t RegisterSessionListener(const std::shared_ptr<SessionListener>& listener) override; 60 61 int32_t RegisterServiceDeathCallback(const DeathCallback& callback) override; 62 63 int32_t UnregisterServiceDeathCallback() override; 64 65 int32_t SendSystemAVKeyEvent(const MMI::KeyEvent& keyEvent) override; 66 67 int32_t SendSystemControlCommand(const AVControlCommand& command) override; 68 69 int32_t CastAudio(const SessionToken& token, 70 const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors) override; 71 72 int32_t CastAudioForAll(const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors) override; 73 74 int32_t StartAVPlayback(const std::string& bundleName, const std::string& assetId) override; 75 76 int32_t Close(void) override; 77 78 #ifdef CASTPLUS_CAST_ENGINE_ENABLE 79 int32_t StartCastDiscovery(int32_t castDeviceCapability) override; 80 81 int32_t StopCastDiscovery() override; 82 83 int32_t SetDiscoverable(const bool enable) override; 84 85 int32_t StartCast(const SessionToken& sessionToken, const OutputDeviceInfo& outputDeviceInfo) override; 86 87 int32_t StopCast(const SessionToken& sessionToken) override; 88 #endif 89 90 private: 91 sptr<AVSessionServiceProxy> GetService(); 92 93 void OnServiceDie(); 94 95 void RegisterClientDeathObserver(); 96 97 std::mutex lock_; 98 sptr<AVSessionServiceProxy> service_; 99 sptr<ServiceDeathRecipient> serviceDeathRecipient_; 100 sptr<ISessionListener> listener_; 101 sptr<ClientDeathStub> clientDeath_; 102 DeathCallback deathCallback_; 103 }; 104 105 class ServiceDeathRecipient : public IRemoteObject::DeathRecipient { 106 public: 107 explicit ServiceDeathRecipient(const std::function<void()>& callback); 108 109 void OnRemoteDied(const wptr<IRemoteObject>& object) override; 110 111 private: 112 std::function<void()> callback_; 113 }; 114 } // namespace OHOS::AVSession 115 #endif // OHOS_AVSESSION_MANAGER_IMPL_H 116