• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_POLICY_SERVER_H
17 #define ST_AUDIO_POLICY_SERVER_H
18 
19 #include <mutex>
20 #include <pthread.h>
21 
22 #include "audio_interrupt_callback.h"
23 #include "audio_policy_manager_stub.h"
24 #include "audio_policy_service.h"
25 #include "audio_session_callback.h"
26 #include "i_audio_volume_key_event_callback.h"
27 #include "iremote_stub.h"
28 #include "system_ability.h"
29 #include "audio_service_dump.h"
30 
31 namespace OHOS {
32 namespace AudioStandard {
33 class AudioPolicyServer : public SystemAbility, public AudioPolicyManagerStub, public AudioSessionCallback {
34     DECLARE_SYSTEM_ABILITY(AudioPolicyServer);
35 public:
36     DISALLOW_COPY_AND_MOVE(AudioPolicyServer);
37 
38     explicit AudioPolicyServer(int32_t systemAbilityId, bool runOnCreate = true);
39 
40     virtual ~AudioPolicyServer() = default;
41 
42     void OnDump() override;
43     void OnStart() override;
44     void OnStop() override;
45 
46     int32_t SetStreamVolume(AudioStreamType streamType, float volume) override;
47 
48     float GetStreamVolume(AudioStreamType streamType) override;
49 
50     int32_t SetStreamMute(AudioStreamType streamType, bool mute) override;
51 
52     bool GetStreamMute(AudioStreamType streamType) override;
53 
54     bool IsStreamActive(AudioStreamType streamType) override;
55 
56     std::vector<sptr<AudioDeviceDescriptor>> GetDevices(DeviceFlag deviceFlag) override;
57 
58     int32_t SetDeviceActive(InternalDeviceType deviceType, bool active) override;
59 
60     bool IsDeviceActive(InternalDeviceType deviceType) override;
61 
62     int32_t SetRingerMode(AudioRingerMode ringMode) override;
63 
64     AudioRingerMode GetRingerMode() override;
65 
66     int32_t SetAudioScene(AudioScene audioScene) override;
67 
68     AudioScene GetAudioScene() override;
69 
70     int32_t SetRingerModeCallback(const int32_t clientId, const sptr<IRemoteObject> &object) override;
71 
72     int32_t UnsetRingerModeCallback(const int32_t clientId) override;
73 
74     int32_t SetDeviceChangeCallback(const int32_t clientId, const sptr<IRemoteObject> &object) override;
75 
76     int32_t UnsetDeviceChangeCallback(const int32_t clientId) override;
77 
78     int32_t SetAudioInterruptCallback(const uint32_t sessionID, const sptr<IRemoteObject> &object) override;
79 
80     int32_t UnsetAudioInterruptCallback(const uint32_t sessionID) override;
81 
82     int32_t ActivateAudioInterrupt(const AudioInterrupt &audioInterrupt) override;
83 
84     int32_t DeactivateAudioInterrupt(const AudioInterrupt &audioInterrupt) override;
85 
86     int32_t SetAudioManagerInterruptCallback(const uint32_t clientID, const sptr<IRemoteObject> &object) override;
87 
88     int32_t UnsetAudioManagerInterruptCallback(const uint32_t clientID) override;
89 
90     int32_t RequestAudioFocus(const uint32_t clientID, const AudioInterrupt &audioInterrupt) override;
91 
92     int32_t AbandonAudioFocus(const uint32_t clientID, const AudioInterrupt &audioInterrupt) override;
93 
94     AudioStreamType GetStreamInFocus() override;
95 
96     int32_t GetSessionInfoInFocus(AudioInterrupt &audioInterrupt) override;
97 
98     int32_t SetVolumeKeyEventCallback(const int32_t clientPid, const sptr<IRemoteObject> &object) override;
99 
100     int32_t UnsetVolumeKeyEventCallback(const int32_t clientPid) override;
101 
102     void OnSessionRemoved(const uint32_t sessionID) override;
103 
104     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
105 protected:
106     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
107     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
108 private:
109     void PrintOwnersLists();
110     int32_t ProcessFocusEntry(const AudioInterrupt &incomingInterrupt);
111     bool ProcessCurActiveInterrupt(std::list<AudioInterrupt>::iterator &iterActive, const AudioInterrupt &incoming);
112     bool ProcessPendingInterrupt(std::list<AudioInterrupt>::iterator &iterPending, const AudioInterrupt &incoming);
113     void AddToCurActiveList(const AudioInterrupt &audioInterrupt);
114     void UnduckCurActiveList(const AudioInterrupt &exitingInterrupt);
115     void ResumeUnduckPendingList(const AudioInterrupt &exitingInterrupt);
116     void NotifyFocusGranted(const uint32_t clientID, const AudioInterrupt &audioInterrupt);
117     int32_t NotifyFocusAbandoned(const uint32_t clientID, const AudioInterrupt &audioInterrupt);
118     int32_t SetStreamVolume(AudioStreamType streamType, float volume, bool isUpdateUi);
119     void RegisterAudioServerDeathRecipient();
120     void AudioServerDied(pid_t pid);
121     void GetPolicyData(PolicyData &policyData);
122     void SubscribeKeyEvents();
123     void InitKVStore();
124     void ConnectServiceAdapter();
125 
126     static float MapVolumeToHDI(int32_t volume);
127     static int32_t ConvertVolumeToInt(float volume);
128 
129     AudioPolicyService& mPolicyService;
130     std::unordered_map<int32_t, std::shared_ptr<VolumeKeyEventCallback>> volumeChangeCbsMap_;
131     std::mutex ringerModeMutex_;
132     std::mutex interruptMutex_;
133     std::mutex volumeKeyEventMutex_;
134     uint32_t clientOnFocus_;
135     std::unique_ptr<AudioInterrupt> focussedAudioInterruptInfo_;
136 
137     std::unordered_map<uint32_t, std::shared_ptr<AudioInterruptCallback>> policyListenerCbsMap_;
138     std::unordered_map<uint32_t, std::shared_ptr<AudioInterruptCallback>> audioManagerListenerCbsMap_;
139     std::list<AudioInterrupt> curActiveOwnersList_;
140     std::list<AudioInterrupt> pendingOwnersList_;
141     std::unordered_map<AudioStreamType, int32_t> interruptPriorityMap_;
142     std::unordered_map<int32_t, std::shared_ptr<AudioRingerModeCallback>> ringerModeListenerCbsMap_;
143     static constexpr int32_t MAX_VOLUME_LEVEL = 15;
144     static constexpr int32_t MIN_VOLUME_LEVEL = 0;
145     static constexpr int32_t CONST_FACTOR = 100;
146     static constexpr int32_t VOLUME_CHANGE_FACTOR = 1;
147     static constexpr int32_t FIRST_PRIORITY = 1;
148     static constexpr int32_t SECOND_PRIORITY = 2;
149     static constexpr int32_t THIRD_PRIORITY = 3;
150     static constexpr int32_t VOLUME_KEY_DURATION = 0;
151 };
152 } // namespace AudioStandard
153 } // namespace OHOS
154 #endif // ST_AUDIO_POLICY_SERVER_H
155