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     static void DetachCallback();
39 
40     std::shared_ptr<AVSession> CreateSession(const std::string& tag, int32_t type,
41                                              const AppExecFwk::ElementName& elementName) override;
42 
43     int32_t CreateSession(const std::string& tag, int32_t type, const AppExecFwk::ElementName& elementName,
44                           std::shared_ptr<AVSession>& session) override;
45 
46     int32_t GetAllSessionDescriptors(std::vector<AVSessionDescriptor>& descriptors) override;
47 
48     int32_t CreateController(const std::string& sessionId, std::shared_ptr<AVSessionController>& controller) override;
49 
50 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
51     int32_t GetAVCastController(const std::string& sessionId,
52         std::shared_ptr<AVCastController>& castController) override;
53 #endif
54 
55     int32_t GetActivatedSessionDescriptors(std::vector<AVSessionDescriptor>& activatedSessions) override;
56 
57     int32_t GetSessionDescriptorsBySessionId(const std::string& sessionId, AVSessionDescriptor& descriptor) override;
58 
59     int32_t GetHistoricalSessionDescriptors(int32_t maxSize, std::vector<AVSessionDescriptor>& descriptors) override;
60 
61     int32_t GetHistoricalAVQueueInfos(int32_t maxSize, int32_t maxAppSize,
62         std::vector<AVQueueInfo>& avQueueInfos) override;
63 
64     int32_t RegisterSessionListener(const std::shared_ptr<SessionListener>& listener) override;
65 
66     int32_t RegisterSessionListenerForAllUsers(const std::shared_ptr<SessionListener>& listener) override;
67 
68     int32_t RegisterServiceDeathCallback(const DeathCallback& callback) override;
69 
70     int32_t UnregisterServiceDeathCallback() override;
71 
72     int32_t SendSystemAVKeyEvent(const MMI::KeyEvent& keyEvent) override;
73 
74     int32_t SendSystemControlCommand(const AVControlCommand& command) override;
75 
76     int32_t CastAudio(const SessionToken& token,
77                       const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors) override;
78 
79     int32_t CastAudioForAll(const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors) override;
80 
81     int32_t StartAVPlayback(const std::string& bundleName, const std::string& assetId) override;
82 
83     int32_t Close(void) override;
84 
85 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
86     int32_t StartCastDiscovery(int32_t castDeviceCapability, std::vector<std::string> drmSchemes) override;
87 
88     int32_t StopCastDiscovery() override;
89 
90     int32_t SetDiscoverable(const bool enable) override;
91 
92     int32_t StartDeviceLogging(int32_t fd, uint32_t maxSize) override;
93 
94     int32_t StopDeviceLogging() override;
95 
96     int32_t StartCast(const SessionToken& sessionToken, const OutputDeviceInfo& outputDeviceInfo) override;
97 
98     int32_t StopCast(const SessionToken& sessionToken) override;
99 #endif
100 
101 private:
102     sptr<AVSessionServiceProxy> GetService();
103 
104     void OnServiceDie();
105 
106     void RegisterClientDeathObserver();
107 
108     sptr<ISessionListener> GetSessionListenerClient(const std::shared_ptr<SessionListener>& listener);
109 
110     std::mutex lock_;
111     sptr<AVSessionServiceProxy> service_;
112     sptr<ServiceDeathRecipient> serviceDeathRecipient_;
113     std::map<int32_t, sptr<ISessionListener>> listenerMapByUserId_;
114     static sptr<ClientDeathStub> clientDeath_;
115     DeathCallback deathCallback_;
116     static constexpr int userIdForAllUsers_ = -1;
117 };
118 
119 class ServiceDeathRecipient : public IRemoteObject::DeathRecipient {
120 public:
121     explicit ServiceDeathRecipient(const std::function<void()>& callback);
122 
123     void OnRemoteDied(const wptr<IRemoteObject>& object) override;
124 
125 private:
126     std::function<void()> callback_;
127 };
128 } // namespace OHOS::AVSession
129 #endif // OHOS_AVSESSION_MANAGER_IMPL_H
130