• 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_SERVICE_H
17 #define ST_AUDIO_POLICY_SERVICE_H
18 
19 #include "audio_info.h"
20 #include "audio_policy_manager_factory.h"
21 #include "device_status_listener.h"
22 #include "iaudio_policy_interface.h"
23 #include "iport_observer.h"
24 #include "parser_factory.h"
25 
26 #include <bitset>
27 #include <list>
28 #include <string>
29 #include <unordered_map>
30 
31 namespace OHOS {
32 namespace AudioStandard {
33 class AudioPolicyService : public IPortObserver, public IDeviceStatusObserver {
34 public:
GetAudioPolicyService()35     static AudioPolicyService& GetAudioPolicyService()
36     {
37         static AudioPolicyService audioPolicyService;
38         return audioPolicyService;
39     }
40 
41     bool Init(void);
42     void Deinit(void);
43     void InitKVStore();
44     bool ConnectServiceAdapter();
45 
46     int32_t SetStreamVolume(AudioStreamType streamType, float volume) const;
47 
48     float GetStreamVolume(AudioStreamType streamType) const;
49 
50     int32_t SetStreamMute(AudioStreamType streamType, bool mute) const;
51 
52     bool GetStreamMute(AudioStreamType streamType) const;
53 
54     bool IsStreamActive(AudioStreamType streamType) const;
55 
56     std::vector<sptr<AudioDeviceDescriptor>> GetDevices(DeviceFlag deviceFlag);
57 
58     int32_t SetDeviceActive(InternalDeviceType deviceType, bool active);
59 
60     bool IsDeviceActive(InternalDeviceType deviceType) const;
61 
62     int32_t SetRingerMode(AudioRingerMode ringMode);
63 
64     bool IsAudioInterruptEnabled() const;
65 
GetAudioFocusMap()66     auto& GetAudioFocusMap() const
67     {
68         return focusMap_;
69     }
70 
71     AudioRingerMode GetRingerMode() const;
72 
73     int32_t SetAudioScene(AudioScene audioScene);
74 
75     AudioScene GetAudioScene() const;
76 
77     // Parser callbacks
78     void OnXmlParsingCompleted(const std::unordered_map<ClassType, std::list<AudioModuleInfo>> &xmldata);
79 
80     void OnAudioInterruptEnable(bool enable);
81 
82     void OnDeviceStatusUpdated(DeviceType deviceType, bool connected, void *privData);
83 
84     void OnServiceConnected(AudioServiceIndex serviceIndex);
85 
86     int32_t SetAudioSessionCallback(AudioSessionCallback *callback);
87 
88     int32_t SetDeviceChangeCallback(const int32_t clientId, const sptr<IRemoteObject> &object);
89 
90     int32_t UnsetDeviceChangeCallback(const int32_t clientId);
91 
92 private:
AudioPolicyService()93     AudioPolicyService()
94         : mAudioPolicyManager(AudioPolicyManagerFactory::GetAudioPolicyManager()),
95           mConfigParser(ParserFactory::GetInstance().CreateParser(*this))
96     {
97         mDeviceStatusListener = std::make_unique<DeviceStatusListener>(*this);
98     }
99 
100     ~AudioPolicyService();
101 
102     std::string GetPortName(InternalDeviceType deviceType);
103 
104     AudioIOHandle GetAudioIOHandle(InternalDeviceType deviceType);
105 
106     InternalDeviceType GetDeviceType(const std::string &deviceName);
107 
108     InternalDeviceType GetCurrentActiveDevice(DeviceRole role) const;
109 
110     DeviceRole GetDeviceRole(DeviceType deviceType) const;
111 
112     DeviceRole GetDeviceRole(const std::string &role);
113 
114     int32_t ActivateNewDevice(DeviceType deviceType);
115 
116     DeviceType FetchHighPriorityDevice();
117 
118     void UpdateConnectedDevices(DeviceType deviceType, std::vector<sptr<AudioDeviceDescriptor>> &desc, bool status);
119 
120     void TriggerDeviceChangedCallback(const std::vector<sptr<AudioDeviceDescriptor>> &devChangeDesc, bool connection);
121 
122     bool interruptEnabled_ = true;
123     int32_t mDefaultDeviceCount = 0;
124     std::bitset<MIN_SERVICE_COUNT> serviceFlag_;
125     DeviceType mCurrentActiveDevice = DEVICE_TYPE_NONE;
126     IAudioPolicyInterface& mAudioPolicyManager;
127     Parser& mConfigParser;
128     std::unique_ptr<DeviceStatusListener> mDeviceStatusListener;
129     std::vector<sptr<AudioDeviceDescriptor>> mConnectedDevices;
130     std::unordered_map<int32_t, sptr<IStandardAudioPolicyManagerListener>> deviceChangeCallbackMap_;
131     AudioScene mAudioScene = AUDIO_SCENE_DEFAULT;
132     std::map<std::pair<AudioStreamType, AudioStreamType>, AudioFocusEntry> focusMap_ = {};
133     std::unordered_map<ClassType, std::list<AudioModuleInfo>> deviceClassInfo_ = {};
134     std::unordered_map<std::string, AudioIOHandle> mIOHandles = {};
135     std::vector<DeviceType> ioDeviceList = {
136         DEVICE_TYPE_BLUETOOTH_A2DP,
137         DEVICE_TYPE_USB_HEADSET,
138         DEVICE_TYPE_WIRED_HEADSET
139     };
140     std::vector<DeviceType> priorityList = {
141         DEVICE_TYPE_BLUETOOTH_A2DP,
142         DEVICE_TYPE_USB_HEADSET,
143         DEVICE_TYPE_WIRED_HEADSET,
144         DEVICE_TYPE_SPEAKER
145     };
146 };
147 } // namespace AudioStandard
148 } // namespace OHOS
149 
150 #endif // ST_AUDIO_POLICY_SERVICE_H
151