• 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_ADAPTER_MANAGER_H
17 #define ST_AUDIO_ADAPTER_MANAGER_H
18 
19 #include <list>
20 #include <unordered_map>
21 
22 #include "audio_service_adapter.h"
23 #include "distributed_kv_data_manager.h"
24 #include "iaudio_policy_interface.h"
25 #include "types.h"
26 #include "media_log.h"
27 
28 namespace OHOS {
29 namespace AudioStandard {
30 using namespace OHOS::DistributedKv;
31 
32 class AudioAdapterManager : public IAudioPolicyInterface {
33 public:
34     static constexpr std::string_view HDI_SINK = "libmodule-hdi-sink.z.so";
35     static constexpr std::string_view HDI_SOURCE = "libmodule-hdi-source.z.so";
36     static constexpr std::string_view PIPE_SINK = "libmodule-pipe-sink.z.so";
37     static constexpr std::string_view PIPE_SOURCE = "libmodule-pipe-source.z.so";
38     static constexpr uint32_t KVSTORE_CONNECT_RETRY_COUNT = 5;
39     static constexpr uint32_t KVSTORE_CONNECT_RETRY_DELAY_TIME = 200000;
40     static constexpr float MAX_VOLUME = 1.0f;
41     static constexpr float MIN_VOLUME = 0.0f;
42 
43     bool Init();
44     void Deinit(void);
45     void InitKVStore();
46     bool ConnectServiceAdapter();
47 
48     std::string GetPolicyManagerName();
49 
GetInstance()50     static IAudioPolicyInterface& GetInstance()
51     {
52         static AudioAdapterManager audioAdapterManager;
53         return audioAdapterManager;
54     }
55 
56     int32_t SetStreamVolume(AudioStreamType streamType, float volume);
57 
58     float GetStreamVolume(AudioStreamType streamType);
59 
60     int32_t SetStreamMute(AudioStreamType streamType, bool mute);
61 
62     bool GetStreamMute(AudioStreamType streamType) const;
63 
64     bool IsStreamActive(AudioStreamType streamType);
65 
66     AudioIOHandle OpenAudioPort(const AudioModuleInfo &audioModuleInfo);
67 
68     int32_t CloseAudioPort(AudioIOHandle ioHandle);
69 
70     int32_t SetDeviceActive(AudioIOHandle ioHandle, InternalDeviceType deviceType, std::string name, bool active);
71 
72     int32_t SetRingerMode(AudioRingerMode ringerMode);
73 
74     AudioRingerMode GetRingerMode(void) const;
75 
76     int32_t SetAudioSessionCallback(AudioSessionCallback *callback);
77 
78     int32_t SuspendAudioDevice(std::string &name, bool isSuspend);
79 
~AudioAdapterManager()80     virtual ~AudioAdapterManager() {}
81 
82 private:
83     struct UserData {
84         AudioAdapterManager *thiz;
85         AudioStreamType streamType;
86         float volume;
87         bool mute;
88         bool isCorked;
89         uint32_t idx;
90     };
91 
AudioAdapterManager()92     AudioAdapterManager()
93         : mRingerMode(RINGER_MODE_NORMAL),
94           mAudioPolicyKvStore(nullptr)
95     {
96         mVolumeMap[STREAM_MUSIC] = MAX_VOLUME;
97         mVolumeMap[STREAM_RING] = MAX_VOLUME;
98         mVolumeMap[STREAM_VOICE_CALL] = MAX_VOLUME;
99         mVolumeMap[STREAM_VOICE_ASSISTANT] = MAX_VOLUME;
100     }
101 
102     bool ConnectToPulseAudio(void);
103     std::string GetModuleArgs(const AudioModuleInfo &audioModuleInfo) const;
104     std::string GetStreamNameByStreamType(AudioStreamType streamType);
105     AudioStreamType GetStreamIDByType(std::string streamType);
106     AudioStreamType GetStreamForVolumeMap(AudioStreamType streamType);
107     bool InitAudioPolicyKvStore(bool& isFirstBoot);
108     void InitVolumeMap(bool isFirstBoot);
109     bool LoadVolumeMap(void);
110     void WriteVolumeToKvStore(AudioStreamType streamType, float volume);
111     bool LoadVolumeFromKvStore(AudioStreamType streamType);
112     void InitRingerMode(bool isFirstBoot);
113     bool LoadRingerMode(void);
114     void WriteRingerModeToKvStore(AudioRingerMode ringerMode);
115 
116     std::unique_ptr<AudioServiceAdapter> mAudioServiceAdapter;
117     std::unordered_map<AudioStreamType, float> mVolumeMap;
118     AudioRingerMode mRingerMode;
119     std::shared_ptr<SingleKvStore> mAudioPolicyKvStore;
120 
121     AudioSessionCallback *sessionCallback_;
122     friend class PolicyCallbackImpl;
123 };
124 
125 class PolicyCallbackImpl : public AudioServiceAdapterCallback {
126 public:
PolicyCallbackImpl(std::unique_ptr<AudioAdapterManager> & audioAdapterManager)127     explicit PolicyCallbackImpl(std::unique_ptr<AudioAdapterManager> &audioAdapterManager)
128     {
129         audioAdapterManager_ = std::move(audioAdapterManager);
130     }
131 
~PolicyCallbackImpl()132     ~PolicyCallbackImpl()
133     {
134         audioAdapterManager_ = nullptr;
135     }
136 
OnGetVolumeCb(std::string streamType)137     float OnGetVolumeCb(std::string streamType)
138     {
139         AudioStreamType streamForVolumeMap = audioAdapterManager_->GetStreamForVolumeMap(
140             audioAdapterManager_->GetStreamIDByType(streamType));
141         if (audioAdapterManager_->mRingerMode != RINGER_MODE_NORMAL) {
142             if (streamForVolumeMap == STREAM_RING) {
143                 return AudioAdapterManager::MIN_VOLUME;
144             }
145         }
146 
147         return audioAdapterManager_->mVolumeMap[streamForVolumeMap];
148     }
149 
OnSessionRemoved(const uint32_t sessionID)150     void OnSessionRemoved(const uint32_t sessionID)
151     {
152         MEDIA_DEBUG_LOG("AudioAdapterManager: PolicyCallbackImpl OnSessionRemoved: Session ID %{public}d", sessionID);
153         if (audioAdapterManager_->sessionCallback_ == nullptr) {
154             MEDIA_DEBUG_LOG("AudioAdapterManager: PolicyCallbackImpl audioAdapterManager_->sessionCallback_ == nullptr"
155                             "not firing OnSessionRemoved");
156         } else {
157             audioAdapterManager_->sessionCallback_->OnSessionRemoved(sessionID);
158         }
159     }
160 private:
161     std::unique_ptr<AudioAdapterManager> audioAdapterManager_;
162 };
163 } // namespace AudioStandard
164 } // namespace OHOS
165 #endif // ST_PULSEAUDIO_ADAPTER_MANAGER_H
166