• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 ST_AUDIO_INTERRUPT_ZONE_H
17 #define ST_AUDIO_INTERRUPT_ZONE_H
18 
19 #include <set>
20 #include <list>
21 #include <string>
22 #include <unordered_map>
23 #include <functional>
24 #include "audio_interrupt_info.h"
25 #include "audio_interrupt_callback.h"
26 #include "audio_policy_client.h"
27 #include "i_audio_interrupt_event_dispatcher.h"
28 
29 namespace OHOS {
30 namespace AudioStandard {
31 enum class AudioZoneFocusStrategy {
32     LOCAL_FOCUS_STRATEGY = 0,
33     DISTRIBUTED_FOCUS_STRATEGY,
34 };
35 
36 using GetZoneIdFunc = std::function<int32_t(int32_t, const std::string &, const std::string &)>;
37 using AudioFocusList = std::list<std::pair<AudioInterrupt, AudioFocuState>>;
38 using AudioFocusIterator = std::list<AudioFocusList::iterator>;
39 
40 typedef struct {
41     int32_t zoneId; // Zone ID value should 0 on local device.
42     AudioZoneFocusStrategy focusStrategy;
43     std::set<int32_t> pids; // When Zone ID is 0, there does not need to be a value.
44     std::set<uint32_t> interruptCbStreamIdsMap;
45     std::set<int32_t> audioPolicyClientProxyCBClientPidMap;
46     std::unordered_map<uint32_t /* streamId */, std::shared_ptr<AudioInterruptCallback>> interruptCbsMap;
47     std::unordered_map<int32_t /* clientPid */, sptr<IAudioPolicyClient>> audioPolicyClientProxyCBMap;
48     AudioFocusList audioFocusInfoList;
49 } AudioInterruptZone;
50 
51 class AudioInterruptService;
52 
53 class AudioInterruptZoneManager {
54 protected:
55     friend class AudioInterruptService;
56     AudioInterruptZoneManager();
57     virtual ~AudioInterruptZoneManager();
58 
59     void InitService(AudioInterruptService *service);
60     int32_t CreateAudioInterruptZone(const int32_t zoneId, AudioZoneFocusStrategy focusStrategy =
61         AudioZoneFocusStrategy::LOCAL_FOCUS_STRATEGY, bool checkPermission = true);
62     int32_t ReleaseAudioInterruptZone(const int32_t zoneId, GetZoneIdFunc func);
63     int32_t MigrateAudioInterruptZone(const int32_t zoneId, GetZoneIdFunc func);
64     int32_t InjectInterruptToAudiotZone(const int32_t zoneId, const AudioFocusList &interrupts);
65     int32_t InjectInterruptToAudiotZone(const int32_t zoneId, const std::string &deviceTag,
66         const AudioFocusList &interrupts);
67     int32_t GetAudioFocusInfoList(const int32_t zoneId, AudioFocusList &focusInfoList);
68     int32_t GetAudioFocusInfoList(const int32_t zoneId, const std::string &deviceTag,
69         AudioFocusList &focusInfoList);
70 
71 private:
72     bool CheckAudioInterruptZonePermission();
73     int32_t FindZoneByPid(int32_t pid);
74     void RemoveAudioZoneInterrupts(int32_t zoneId, const AudioFocusIterator &focus);
75     void TryActiveAudioFocusForZone(int32_t zoneId, AudioFocusList &activeFocusList);
76     void TryResumeAudioFocusForZone(int32_t zoneId);
77     AudioFocusIterator QueryAudioFocusFromZone(int32_t zoneId, const std::string &deviceTag);
78     void ForceStopAudioFocusInZone(int32_t zoneId, const AudioInterrupt &audioInterrupt);
79     void ForceStopAllAudioFocusInZone(std::shared_ptr<AudioInterruptZone> &zone);
80 
81     AudioInterruptService *service_ = nullptr;
82 };
83 } // namespace AudioStandard
84 } // namespace OHOS
85 
86 #endif // ST_AUDIO_INTERRUPT_ZONE_H
87