• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 
17 #ifndef AUDIO_EFFECT_CHAIN_MANAGER_H
18 #define AUDIO_EFFECT_CHAIN_MANAGER_H
19 
20 #include <cstdio>
21 #include <cstdint>
22 #include <cassert>
23 #include <cstdint>
24 #include <cstddef>
25 #include <map>
26 #include <memory>
27 #include <string>
28 #include <vector>
29 #include <mutex>
30 #include <set>
31 
32 #include "audio_effect.h"
33 #include "audio_effect_chain.h"
34 
35 #ifdef SENSOR_ENABLE
36 #include "audio_head_tracker.h"
37 #endif
38 #include "audio_effect_hdi_param.h"
39 #ifdef WINDOW_MANAGER_ENABLE
40 #include "audio_effect_rotation.h"
41 #endif
42 #include "audio_effect_volume.h"
43 
44 namespace OHOS {
45 namespace AudioStandard {
46 
47 const uint32_t DEFAULT_FRAMELEN = 1440;
48 const uint32_t DEFAULT_NUM_CHANNEL = STEREO;
49 const uint32_t DEFAULT_MCH_NUM_CHANNEL = CHANNEL_6;
50 const uint32_t DSP_MAX_NUM_CHANNEL = CHANNEL_16;
51 const uint64_t DEFAULT_NUM_CHANNELLAYOUT = CH_LAYOUT_STEREO;
52 const uint64_t DEFAULT_MCH_NUM_CHANNELLAYOUT = CH_LAYOUT_5POINT1;
53 const uint32_t BASE_TEN = 10;
54 const std::string DEFAULT_DEVICE_SINK = "Speaker";
55 const std::string BLUETOOTH_DEVICE_SINK = "Bt_Speaker";
56 const uint32_t SIZE_OF_SPATIALIZATION_STATE = 2;
57 const uint32_t MAX_UINT_VOLUME_NUM = 10000;
58 const uint32_t MAX_UINT_DSP_VOLUME = 65535;
59 const std::string DEFAULT_SCENE_TYPE = "SCENE_DEFAULT";
60 const std::string DEFAULT_PRESET_SCENE = "SCENE_MUSIC";
61 
62 struct SessionEffectInfo {
63     std::string sceneMode;
64     std::string sceneType;
65     uint32_t channels;
66     uint64_t channelLayout;
67     std::string spatializationEnabled;
68     int32_t streamUsage;
69     int32_t systemVolumeType;
70 };
71 
72 const std::vector<AudioChannelLayout> AUDIO_EFFECT_SUPPORTED_CHANNELLAYOUTS {
73     CH_LAYOUT_STEREO,
74     CH_LAYOUT_5POINT1,
75     CH_LAYOUT_5POINT1POINT2,
76     CH_LAYOUT_7POINT1,
77     CH_LAYOUT_5POINT1POINT4,
78     CH_LAYOUT_7POINT1POINT2,
79     CH_LAYOUT_7POINT1POINT4
80 };
81 
82 struct EffectBufferAttr {
83     float *bufIn;
84     float *bufOut;
85     int numChans;
86     int frameLen;
87     uint32_t outChannels;
88     uint64_t outChannelLayout;
89 
EffectBufferAttrEffectBufferAttr90     EffectBufferAttr(float *bufIn, float *bufOut, int numChans, int frameLen, uint32_t outChannels,
91         uint64_t outChannelLayout)
92         : bufIn(bufIn),
93           bufOut(bufOut),
94           numChans(numChans),
95           frameLen(frameLen),
96           outChannels(outChannels),
97           outChannelLayout(outChannelLayout)
98     {
99     }
100 };
101 
102 enum SceneTypeOperation {
103     ADD_SCENE_TYPE = 0,
104     REMOVE_SCENE_TYPE = 1,
105 };
106 
107 class AudioEffectChainManager {
108 public:
109     AudioEffectChainManager();
110     ~AudioEffectChainManager();
111     static AudioEffectChainManager *GetInstance();
112     void InitAudioEffectChainManager(std::vector<EffectChain> &effectChains,
113         const EffectChainManagerParam &effectChainManagerParam,
114         std::vector<std::shared_ptr<AudioEffectLibEntry>> &effectLibraryList);
115     void ConstructEffectChainMgrMaps(std::vector<EffectChain> &effectChains,
116         const EffectChainManagerParam &effectChainManagerParam,
117         std::vector<std::shared_ptr<AudioEffectLibEntry>> &effectLibraryList);
118     bool CheckAndAddSessionID(const std::string &sessionID);
119     int32_t CreateAudioEffectChainDynamic(const std::string &sceneType);
120     bool CheckAndRemoveSessionID(const std::string &sessionID);
121     int32_t ReleaseAudioEffectChainDynamic(const std::string &sceneType);
122     bool ExistAudioEffectChain(const std::string &sceneType, const std::string &effectMode);
123     int32_t ApplyAudioEffectChain(const std::string &sceneType, std::unique_ptr<EffectBufferAttr> &bufferAttr);
124     void SetOutputDeviceSink(int32_t device, const std::string &sinkName);
125     bool GetOffloadEnabled();
126     int32_t UpdateMultichannelConfig(const std::string &sceneType);
127     int32_t InitAudioEffectChainDynamic(const std::string &sceneType);
128     int32_t UpdateSpatializationState(AudioSpatializationState spatializationState);
129     int32_t UpdateSpatialDeviceType(AudioSpatialDeviceType spatialDeviceType);
130     int32_t SessionInfoMapAdd(const std::string &sessionID, const SessionEffectInfo &info);
131     int32_t SessionInfoMapDelete(const std::string &sceneType, const std::string &sessionID);
132     int32_t ReturnEffectChannelInfo(const std::string &sceneType, uint32_t &channels, uint64_t &channelLayout);
133     int32_t ReturnMultiChannelInfo(uint32_t *channels, uint64_t *channelLayout);
134     int32_t EffectRotationUpdate(const uint32_t rotationState);
135     int32_t EffectVolumeUpdate(std::shared_ptr<AudioEffectVolume> audioEffectVolume);
136     int32_t StreamVolumeUpdate(const std::string sessionIDString, const float streamVolume);
137     uint32_t GetLatency(const std::string &sessionId);
138     int32_t SetSpatializationSceneType(AudioSpatializationSceneType spatializationSceneType);
139     int32_t SetEffectSystemVolume(const int32_t systemVolumeType, const float systemVolume);
140     void ResetInfo();  // Use for testing temporarily.
141     void UpdateDefaultAudioEffect();
142     bool CheckSceneTypeMatch(const std::string &sinkSceneType, const std::string &sceneType);
143     void UpdateParamExtra(const std::string &mainkey, const std::string &subkey, const std::string &value);
144     void InitHdiState();
145     void UpdateEffectBtOffloadSupported(const bool &isSupported);
146     void UpdateSceneTypeList(const std::string &sceneType, SceneTypeOperation operation);
147     uint32_t GetSceneTypeToChainCount(const std::string &sceneType);
148     int32_t SetAudioEffectProperty(const AudioEffectPropertyArrayV3 &propertyArray);
149     int32_t GetAudioEffectProperty(AudioEffectPropertyArrayV3 &propertyArray);
150     int32_t SetAudioEffectProperty(const AudioEffectPropertyArray &propertyArray);
151     int32_t GetAudioEffectProperty(AudioEffectPropertyArray &propertyArray);
152     void UpdateStreamUsage();
153     int32_t InitEffectBuffer(const std::string &sessionID);
154     int32_t QueryEffectChannelInfo(const std::string &sceneType, uint32_t &channels, uint64_t &channelLayout);
155     int32_t QueryHdiSupportedChannelInfo(uint32_t &channels, uint64_t &channelLayout);
156     void LoadEffectProperties();
157 private:
158     int32_t SetAudioEffectChainDynamic(const std::string &sceneType, const std::string &effectMode);
159     void UpdateSensorState();
160     void DeleteAllChains();
161     void RecoverAllChains();
162     int32_t EffectDspVolumeUpdate(std::shared_ptr<AudioEffectVolume> audioEffectVolume);
163     int32_t EffectApVolumeUpdate(std::shared_ptr<AudioEffectVolume> audioEffectVolume);
164     int32_t SendEffectApVolume(std::shared_ptr<AudioEffectVolume> audioEffectVolume);
165     void SetSpatializationSceneTypeToChains();
166     void SetSpatializationEnabledToChains();
167     void SetSpkOffloadState();
168     void UpdateCurrSceneType(AudioEffectScene &currSceneType, const std::string &sceneType);
169     void FindMaxEffectChannels(const std::string &sceneType, const std::set<std::string> &sessions, uint32_t &channels,
170         uint64_t &channelLayout);
171     int32_t UpdateDeviceInfo(int32_t device, const std::string &sinkName);
172     std::shared_ptr<AudioEffectChain> CreateAudioEffectChain(const std::string &sceneType, bool isPriorScene);
173     bool CheckIfSpkDsp();
174     int32_t CheckAndReleaseCommonEffectChain(const std::string &sceneType);
175     void FindMaxSessionID(uint32_t &maxSessionID, std::string &sceneType,
176         const std::string &scenePairType, std::set<std::string> &sessions);
177     void UpdateCurrSceneTypeAndStreamUsageForDsp();
178     void SendAudioParamToHDI(HdiSetParamCommandCode code, const std::string &value, DeviceType device);
179     void SendAudioParamToARM(HdiSetParamCommandCode code, const std::string &value);
180     std::string GetDeviceTypeName();
181     bool IsEffectChainStop(const std::string &sceneType, const std::string &sessionID);
182     int32_t InitEffectBufferInner(const std::string &sessionID);
183     int32_t InitAudioEffectChainDynamicInner(const std::string &sceneType);
184     int32_t QueryEffectChannelInfoInner(const std::string &sceneType, uint32_t &channels, uint64_t &channelLayout);
185 #ifdef WINDOW_MANAGER_ENABLE
186     int32_t EffectDspRotationUpdate(std::shared_ptr<AudioEffectRotation> audioEffectRotation,
187         const uint32_t rotationState);
188     int32_t EffectApRotationUpdate(std::shared_ptr<AudioEffectRotation> audioEffectRotation,
189         const uint32_t rotationState);
190 #endif
191     int32_t CreateAudioEffectChainDynamicInner(const std::string &sceneType);
192     int32_t ReleaseAudioEffectChainDynamicInner(const std::string &sceneType);
193     bool ExistAudioEffectChainInner(const std::string &sceneType, const std::string &effectMode);
194     int32_t UpdateMultichannelConfigInner(const std::string &sceneType);
195     int32_t UpdateSpatializationStateInner(AudioSpatializationState spatializationState);
196     int32_t SetHdiParam(const AudioEffectScene &sceneType);
197     int32_t ReturnEffectChannelInfoInner(const std::string &sceneType, uint32_t &channels, uint64_t &channelLayout);
198     int32_t EffectVolumeUpdateInner(std::shared_ptr<AudioEffectVolume> audioEffectVolume);
199     void InitHdiStateInner();
200     void UpdateSpatializationEnabled(AudioSpatializationState spatializationState);
201     void ConfigureAudioEffectChain(std::shared_ptr<AudioEffectChain> audioEffectChain,
202         const std::string &effectMode);
203     int32_t NotifyAndCreateAudioEffectChain(const std::string &sceneType);
204     void WaitAndReleaseEffectChain(const std::string &sceneType, const std::string &sceneTypeAndDeviceKey,
205         const std::string &defaultSceneTypeAndDeviceKey, int32_t ret);
206     std::map<std::string, std::shared_ptr<AudioEffectLibEntry>> effectToLibraryEntryMap_;
207     std::map<std::string, std::string> effectToLibraryNameMap_;
208     std::map<std::string, std::vector<std::string>> effectChainToEffectsMap_;
209     std::map<std::string, std::string> sceneTypeAndModeToEffectChainNameMap_;
210     std::map<std::string, std::shared_ptr<AudioEffectChain>> sceneTypeToEffectChainMap_;
211     std::map<std::string, int32_t> sceneTypeToEffectChainCountMap_;
212     std::set<std::string> sessionIDSet_;
213     std::map<std::string, std::set<std::string>> sceneTypeToSessionIDMap_;
214     std::map<std::string, SessionEffectInfo> sessionIDToEffectInfoMap_;
215     std::map<std::string, int32_t> sceneTypeToEffectChainCountBackupMap_;
216     std::set<std::string> sceneTypeToSpecialEffectSet_;
217     std::vector<std::string> priorSceneList_;
218     std::unordered_map<std::string, std::string> effectPropertyMap_;
219     std::unordered_map<std::string, std::string> defaultPropertyMap_;
220     std::vector<std::pair<std::string, int32_t>> sceneTypeCountList_;
221     DeviceType deviceType_ = DEVICE_TYPE_SPEAKER;
222     std::string deviceSink_ = DEFAULT_DEVICE_SINK;
223     std::string deviceClass_ = "";
224     std::string extraSceneType_ = "0";
225     std::string foldState_ = "0";
226     std::string lidState_ = "0";
227     std::string maxSessionIDToSceneType_ = "";
228     std::string maxDefaultSessionIDToSceneType_ = "";
229     bool isInitialized_ = false;
230     std::mutex dynamicMutex_;
231     std::atomic<bool> spatializationEnabled_ = false;
232     bool headTrackingEnabled_ = false;
233     bool btOffloadEnabled_ = false;
234     bool spkOffloadEnabled_ = false;
235     bool initializedLogFlag_ = true;
236     bool btOffloadSupported_ = false;
237     AudioSpatializationSceneType spatializationSceneType_ = SPATIALIZATION_SCENE_TYPE_MUSIC;
238     bool isDefaultEffectChainExisted_ = false;
239     int32_t defaultEffectChainCount_ = 0;
240     int32_t maxEffectChainCount_ = 1;
241     uint32_t maxSessionID_ = 0;
242     AudioSpatialDeviceType spatialDeviceType_{ EARPHONE_TYPE_OTHERS };
243     bool hasLoadedEffectProperties_ = false;
244     std::condition_variable cv_;
245     bool defaultEffectChainCreated_ = false;
246 
247 #ifdef SENSOR_ENABLE
248     std::shared_ptr<HeadTracker> headTracker_;
249 #endif
250 
251     std::shared_ptr<AudioEffectHdiParam> audioEffectHdiParam_;
252     int8_t effectHdiInput_[SEND_HDI_COMMAND_LEN];
253 };
254 }  // namespace AudioStandard
255 }  // namespace OHOS
256 #endif // AUDIO_EFFECT_CHAIN_MANAGER_H
257