• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SERVICE_H
17 #define OHOS_AVSESSION_SERVICE_H
18 
19 #include <atomic>
20 #include <mutex>
21 #include <map>
22 
23 #include "iremote_stub.h"
24 #include "system_ability.h"
25 #include "audio_info.h"
26 #include "avsession_service_stub.h"
27 #include "avsession_item.h"
28 #include "avcontroller_item.h"
29 #include "session_container.h"
30 #include "iclient_death.h"
31 #include "isession_listener.h"
32 #include "focus_session_strategy.h"
33 #include "background_audio_controller.h"
34 #include "ability_manager_adapter.h"
35 #include "device_manager.h"
36 #include "dm_device_info.h"
37 #include "audio_adapter.h"
38 #include "remote_session_command_process.h"
39 #include "i_avsession_service_listener.h"
40 #include "avqueue_info.h"
41 #include "migrate/migrate_avsession_server.h"
42 
43 namespace OHOS::AVSession {
44 class AVSessionDumper;
45 
46 class ClientDeathRecipient : public IRemoteObject::DeathRecipient {
47 public:
48     explicit ClientDeathRecipient(const std::function<void()>& callback);
49 
50     void OnRemoteDied(const wptr<IRemoteObject>& object) override;
51 
52 private:
53     std::function<void()> callback_;
54 };
55 
56 class AVSessionInitDMCallback : public OHOS::DistributedHardware::DmInitCallback {
57 public:
58     AVSessionInitDMCallback() = default;
59     ~AVSessionInitDMCallback() override = default;
OnRemoteDied()60     void OnRemoteDied() override {};
61 };
62 
63 class AudioDeviceDescriptorComp {
64 public:
operator()65     bool operator()(const AudioStandard::AudioDeviceDescriptor& left,
66                     const AudioStandard::AudioDeviceDescriptor& right) const
67     {
68         return left.networkId_ == right.networkId_;
69     }
70 };
71 
72 class AVSessionService : public SystemAbility, public AVSessionServiceStub, public IAVSessionServiceListener {
73     DECLARE_SYSTEM_ABILITY(AVSessionService);
74 
75 public:
76     DISALLOW_COPY_AND_MOVE(AVSessionService);
77 
78     explicit AVSessionService(int32_t systemAbilityId, bool runOnCreate = true);
79 
80     ~AVSessionService() override;
81 
82     void OnDump() override;
83 
84     void OnStart() override;
85 
86     void OnStop() override;
87 
88     void PullMigrateStub();
89 
90     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
91 
92     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
93 
94     sptr<IRemoteObject> CreateSessionInner(const std::string& tag, int32_t type,
95                                            const AppExecFwk::ElementName& elementName) override;
96 
97     int32_t GetAllSessionDescriptors(std::vector<AVSessionDescriptor>& descriptors) override;
98 
99     int32_t GetSessionDescriptorsBySessionId(const std::string& sessionId, AVSessionDescriptor& descriptor) override;
100 
101     int32_t GetHistoricalSessionDescriptors(int32_t maxSize, std::vector<AVSessionDescriptor>& descriptors) override;
102 
103     int32_t GetHistoricalAVQueueInfos(int32_t maxSize, int32_t maxAppSize,
104                                       std::vector<AVQueueInfo>& avQueueInfos) override;
105 
106     int32_t StartAVPlayback(const std::string& bundleName, const std::string& assetId) override;
107 
108     int32_t CreateControllerInner(const std::string& sessionId, sptr<IRemoteObject>& object) override;
109 
110 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
111     int32_t GetAVCastControllerInner(const std::string& sessionId, sptr<IRemoteObject>& object) override;
112 #endif
113 
114     int32_t RegisterSessionListener(const sptr<ISessionListener>& listener) override;
115 
116     int32_t SendSystemAVKeyEvent(const MMI::KeyEvent& keyEvent) override;
117 
118     int32_t SendSystemControlCommand(const AVControlCommand& command) override;
119 
120     int32_t RegisterClientDeathObserver(const sptr<IClientDeath>& observer) override;
121 
122     void OnClientDied(pid_t pid);
123 
124     void HandleSessionRelease(std::string sessionId);
125 
126     void HandleCallStartEvent();
127 
128     void HandleControllerRelease(AVControllerItem& controller);
129 
130     std::int32_t Dump(std::int32_t fd, const std::vector<std::u16string>& args) override;
131 
132     int32_t CastAudio(const SessionToken& token,
133                       const std::vector<AudioStandard::AudioDeviceDescriptor>& sinkAudioDescriptors) override;
134 
135     int32_t CastAudioForAll(const std::vector<AudioStandard::AudioDeviceDescriptor>& sinkAudioDescriptors) override;
136 
NotifyAudioSessionCheckTrigger(const int32_t uid)137     void NotifyAudioSessionCheckTrigger(const int32_t uid)
138     {
139         return NotifyAudioSessionCheck(uid);
140     }
141 
142     void SuperLauncher(std::string deviceId, std::string serviceName,
143         std::string extraInfo, const std::string& state);
144 
145 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
146     void ReleaseCastSession() override;
147 
148     void CreateSessionByCast(const int64_t castHandle) override;
149 
150     void NotifyDeviceAvailable(const OutputDeviceInfo& castOutputDeviceInfo) override;
151 
152     void NotifyDeviceOffline(const std::string& deviceId) override;
153 
154     int32_t StartCast(const SessionToken& sessionToken, const OutputDeviceInfo& outputDeviceInfo) override;
155 
156     int32_t StopCast(const SessionToken& sessionToken) override;
157 
158     int32_t checkEnableCast(bool enable) override;
159 #endif
160 
161     int32_t Close(void) override;
162 
163     void AddAvQueueInfoToFile(AVSessionItem& session);
164 
165 private:
166     void CheckInitCast();
167 
168     static SessionContainer& GetContainer();
169 
170     std::string AllocSessionId();
171 
172     bool AbilityHasSession(pid_t pid);
173 
174     sptr<AVControllerItem> GetPresentController(pid_t pid, const std::string& sessionId);
175 
176     void NotifySessionCreate(const AVSessionDescriptor& descriptor);
177     void NotifySessionRelease(const AVSessionDescriptor& descriptor);
178     void NotifyTopSessionChanged(const AVSessionDescriptor& descriptor);
179     void NotifyAudioSessionCheck(const int32_t uid);
180     void NotifySystemUI(const AVSessionDescriptor* historyDescriptor);
181     void NotifyDeviceChange(const DeviceChangeAction& deviceChangeAction);
182 
183     void AddClientDeathObserver(pid_t pid, const sptr<IClientDeath>& observer);
184     void RemoveClientDeathObserver(pid_t pid);
185 
186     void AddSessionListener(pid_t pid, const sptr<ISessionListener>& listener);
187     void RemoveSessionListener(pid_t pid);
188 
189     void AddInnerSessionListener(SessionListener* listener);
190     void RemoveInnerSessionListener(SessionListener* listener);
191 
192     sptr<AVSessionItem> SelectSessionByUid(const AudioStandard::AudioRendererChangeInfo& info);
193 
194     void OutputDeviceChangeListener(const AudioRendererChangeInfos& infos);
195 
196     sptr<AVSessionItem> CreateSessionInner(const std::string& tag, int32_t type, bool thirdPartyApp,
197                                            const AppExecFwk::ElementName& elementName);
198 
199     sptr<AVSessionItem> CreateNewSession(const std::string& tag, int32_t type, bool thirdPartyApp,
200                                          const AppExecFwk::ElementName& elementName);
201 
202     sptr<AVControllerItem> CreateNewControllerForSession(pid_t pid, sptr<AVSessionItem>& session);
203 
204     int32_t CancelCastAudioForClientExit(pid_t pid, const sptr<AVSessionItem>& session);
205 
206     void ClearSessionForClientDiedNoLock(pid_t pid);
207 
208     void ClearControllerForClientDiedNoLock(pid_t pid);
209 
210     void ClearSessionNoLock(const std::string& sessionId);
211 
212     void InitKeyEvent();
213 
214     void InitAudio();
215 
216     void InitAMS();
217 
218     void InitDM();
219 
220     void InitBMS();
221 
222     bool SelectFocusSession(const FocusSessionStrategy::FocusSessionChangeInfo& info);
223 
224     void RefreshFocusSessionSort(sptr<AVSessionItem> &session);
225 
226     void UpdateTopSession(const sptr<AVSessionItem>& newTopSession);
227 
228     void HandleFocusSession(const FocusSessionStrategy::FocusSessionChangeInfo& info);
229 
230     void HandleDeviceChange(const AudioStandard::DeviceChangeAction& deviceChangeAction);
231 
232     __attribute__((no_sanitize("cfi"))) std::shared_ptr<RemoteSessionCommandProcess> GetService(
233         const std::string& deviceId);
234 
235     int32_t CastAudioProcess(const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors,
236                              const std::string& sourceSessionInfo,
237                              sptr<AVSessionItem>& session);
238 
239     int32_t CastAudioInner(const std::vector<AudioStandard::AudioDeviceDescriptor>& sinkAudioDescriptors,
240                            const std::string& sourceSessionInfo,
241                            const sptr<AVSessionItem>& session);
242 
243     int32_t CancelCastAudioInner(const std::vector<AudioStandard::AudioDeviceDescriptor>& sinkDevices,
244                                  const std::string& sourceSessionInfo,
245                                  const sptr<AVSessionItem>& session);
246 
247     int32_t RemoteCastAudioInner(const std::string& sourceSessionInfo, std::string& sinkSessionInfo);
248 
249     int32_t RemoteCancelCastAudioInner(const std::string& sessionInfo);
250 
251     int32_t ProcessCastAudioCommand(const RemoteServiceCommand command, const std::string& input,
252                                     std::string& output) override;
253 
254     int32_t CastAudioForNewSession(const sptr<AVSessionItem>& session);
255 
256     bool IsLocalDevice(const std::string& networkId);
257 
258     int32_t GetLocalNetworkId(std::string& networkId);
259 
260     int32_t GetTrustedDeviceName(const std::string& networkId, std::string& deviceName);
261 
262     int32_t GetTrustedDevicesInfo(std::vector<OHOS::DistributedHardware::DmDeviceInfo>& deviceList);
263 
264     int32_t SetBasicInfo(std::string& sessionInfo);
265 
266     void SetDeviceInfo(const std::vector<AudioStandard::AudioDeviceDescriptor>& castAudioDescriptors,
267                            sptr<AVSessionItem>& session);
268 
269     int32_t GetAudioDescriptor(const std::string deviceId,
270                                std::vector<AudioStandard::AudioDeviceDescriptor>& audioDeviceDescriptors);
271 
272     bool GetAudioDescriptorByDeviceId(const std::vector<sptr<AudioStandard::AudioDeviceDescriptor>>& descriptors,
273                                       const std::string& deviceId,
274                                       AudioStandard::AudioDeviceDescriptor& audioDescriptor);
275 
276     void GetDeviceInfo(const sptr<AVSessionItem>& session,
277                            const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors,
278                            std::vector<AudioStandard::AudioDeviceDescriptor>& castSinkDescriptors,
279                            std::vector<AudioStandard::AudioDeviceDescriptor>& cancelSinkDescriptors);
280 
281     int32_t SelectOutputDevice(const int32_t uid, const AudioStandard::AudioDeviceDescriptor& descriptor);
282 
283     int32_t StartDefaultAbilityByCall(std::string& sessionId);
284 
285     int32_t StartAbilityByCall(const std::string& sessionIdNeeded, std::string& sessionId);
286 
287     void HandleEventHandlerCallBack();
288 
289     bool IsHistoricalSession(const std::string& sessionId);
290 
291     void DeleteHistoricalRecord(const std::string& bundleName);
292 
293     void DeleteAVQueueInfoRecord(const std::string& bundleName);
294 
295     const nlohmann::json& GetSubNode(const nlohmann::json& node, const std::string& name);
296 
297     void refreshSortFileOnCreateSession(const std::string& sessionId, const std::string& sessionType,
298         const AppExecFwk::ElementName& elementName);
299 
300     bool LoadStringFromFileEx(const std::string& filePath, std::string& content);
301 
302     bool SaveStringToFileEx(const std::string& filePath, const std::string& content);
303 
304     bool CheckStringAndCleanFile(const std::string& filePath);
305 
306     void ClearClientResources(pid_t pid);
307 
308     bool SaveAvQueueInfo(std::string& oldContent, const std::string &bundleName, const AVMetaData& meta);
309 
310     int32_t GetHistoricalSessionDescriptorsFromFile(std::vector<AVSessionDescriptor>& descriptors);
311 
312     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> CreateWantAgent(
313         const AVSessionDescriptor* histroyDescriptor);
314 
315     std::atomic<uint32_t> sessionSeqNum_ {};
316 
317     std::recursive_mutex sessionAndControllerLock_;
318     sptr<AVSessionItem> topSession_;
319     std::map<pid_t, std::list<sptr<AVControllerItem>>> controllers_;
320 
321     std::recursive_mutex clientDeathObserversLock_;
322     std::map<pid_t, sptr<IClientDeath>> clientDeathObservers_;
323 
324     std::recursive_mutex sessionListenersLock_;
325     std::map<pid_t, sptr<ISessionListener>> sessionListeners_;
326     std::list<SessionListener*> innerSessionListeners_;
327 
328     std::recursive_mutex abilityManagerLock_;
329     std::map<std::string, std::shared_ptr<AbilityManagerAdapter>> abilityManager_;
330 
331     FocusSessionStrategy focusSessionStrategy_;
332     BackgroundAudioController backgroundAudioController_;
333 
334     std::recursive_mutex castAudioSessionMapLock_;
335     std::map<std::string, std::string> castAudioSessionMap_;
336 
337     std::recursive_mutex isAllSessionCastLock_;
338     bool isAllSessionCast_ {};
339 
340     std::recursive_mutex outputDeviceIdLock_;
341     std::string outputDeviceId_;
342 
343     std::unique_ptr<AVSessionDumper> dumpHelper_ {};
344     friend class AVSessionDumper;
345 
346     std::recursive_mutex sortFileReadWriteLock_;
347     std::recursive_mutex avQueueFileReadWriteLock_;
348     std::mutex fileCheckLock_;
349 
350     std::shared_ptr<MigrateAVSessionServer> migrateAVSession_;
351 
352 #ifdef CASTPLUS_CAST_ENGINE_ENABLE
353     std::recursive_mutex castDeviceInfoMapLock_;
354     std::map<std::string, DeviceInfo> castDeviceInfoMap_;
355 #endif
356 
357     static constexpr const char *SORT_FILE_NAME = "sortinfo";
358     static constexpr const char *ABILITY_FILE_NAME = "abilityinfo";
359     static constexpr const char *DEFAULT_SESSION_ID = "default";
360     static constexpr const char *DEFAULT_BUNDLE_NAME = "com.example.himusicdemo";
361     static constexpr const char *DEFAULT_ABILITY_NAME = "MainAbility";
362     static constexpr const int32_t SYSTEMUI_LIVEVIEW_TYPECODE_MDEDIACONTROLLER = 2;
363     static constexpr const char *AVQUEUE_FILE_NAME = "avqueueinfo";
364 
365     const std::string AVSESSION_FILE_DIR = "/data/service/el1/public/av_session/";
366     const std::string MEDIA_CONTROL_BUNDLENAME = "com.ohos.mediacontroller";
367     const std::string MEDIA_CONTROL_ABILITYNAME = "com.ohos.mediacontroller.avplayer.mainability";
368 
369     int32_t pressCount_ {};
370     int32_t maxHistoryNums = 10;
371     bool isFirstPress_ = true;
372     bool isSourceInCast_ = false;
373     bool isInCast_ = false;
374 
375     void *migrateStubFuncHandle_ = nullptr;
376 
377     const int32_t ONE_CLICK = 1;
378     const int32_t DOUBLE_CLICK = 2;
379     const int32_t THREE_CLICK = 3;
380     const int32_t unSetHistoryNum = 3;
381     const int32_t CLICK_TIMEOUT = 500;
382     const int32_t defMaxHistoryNum = 10;
383     const int32_t maxFileLength = 32 * 1024 * 1024;
384     const int32_t maxAVQueueInfoLen = 5;
385     const int32_t allocSpace = 2;
386 };
387 } // namespace OHOS::AVSession
388 #endif // OHOS_AVSESSION_SERVICE_H
389