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_10;
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 
EffectBufferAttrEffectBufferAttr88     EffectBufferAttr(float *bufIn, float *bufOut, int numChans, int frameLen)
89         : bufIn(bufIn), bufOut(bufOut), numChans(numChans), frameLen(frameLen)
90     {
91     }
92 };
93 
94 enum SceneTypeOperation {
95     ADD_SCENE_TYPE = 0,
96     REMOVE_SCENE_TYPE = 1,
97 };
98 
99 class AudioEffectChainManager {
100 public:
101     AudioEffectChainManager();
102     ~AudioEffectChainManager();
103     static AudioEffectChainManager *GetInstance();
104     void InitAudioEffectChainManager(std::vector<EffectChain> &effectChains,
105         const EffectChainManagerParam &effectChainManagerParam,
106         std::vector<std::shared_ptr<AudioEffectLibEntry>> &effectLibraryList);
107     bool CheckAndAddSessionID(const std::string &sessionID);
108     int32_t CreateAudioEffectChainDynamic(const std::string &sceneType);
109     bool CheckAndRemoveSessionID(const std::string &sessionID);
110     int32_t ReleaseAudioEffectChainDynamic(const std::string &sceneType);
111     bool ExistAudioEffectChain(const std::string &sceneType, const std::string &effectMode,
112         const std::string &spatializationEnabled);
113     int32_t ApplyAudioEffectChain(const std::string &sceneType, const std::unique_ptr<EffectBufferAttr> &bufferAttr);
114     void SetOutputDeviceSink(int32_t device, const std::string &sinkName);
115     std::string GetDeviceTypeName();
116     bool GetOffloadEnabled();
117     int32_t UpdateMultichannelConfig(const std::string &sceneType);
118     int32_t InitAudioEffectChainDynamic(const std::string &sceneType);
119     int32_t UpdateSpatializationState(AudioSpatializationState spatializationState);
120     int32_t UpdateSpatialDeviceType(AudioSpatialDeviceType spatialDeviceType);
121     int32_t SetHdiParam(const AudioEffectScene &sceneType);
122     int32_t SessionInfoMapAdd(const std::string &sessionID, const SessionEffectInfo &info);
123     int32_t SessionInfoMapDelete(const std::string &sceneType, const std::string &sessionID);
124     int32_t ReturnEffectChannelInfo(const std::string &sceneType, uint32_t &channels, uint64_t &channelLayout);
125     int32_t ReturnMultiChannelInfo(uint32_t *channels, uint64_t *channelLayout);
126     int32_t EffectRotationUpdate(const uint32_t rotationState);
127     int32_t EffectVolumeUpdate(std::shared_ptr<AudioEffectVolume> audioEffectVolume);
128     int32_t StreamVolumeUpdate(const std::string sessionIDString, const float streamVolume);
129     uint32_t GetLatency(const std::string &sessionId);
130     int32_t SetSpatializationSceneType(AudioSpatializationSceneType spatializationSceneType);
131     int32_t SetEffectSystemVolume(const int32_t systemVolumeType, const float systemVolume);
132     bool GetCurSpatializationEnabled();
133     void ResetEffectBuffer();
134     void ResetInfo();  // Use for testing temporarily.
135     void UpdateDefaultAudioEffect();
136     bool CheckSceneTypeMatch(const std::string &sinkSceneType, const std::string &sceneType);
137     void UpdateSpatializationEnabled(AudioSpatializationState spatializationState);
138     void UpdateExtraSceneType(const std::string &mainkey, const std::string &subkey, const std::string &extraSceneType);
139     void InitHdiState();
140     void UpdateEffectBtOffloadSupported(const bool &isSupported);
141     void UpdateSceneTypeList(const std::string &sceneType, SceneTypeOperation operation);
142     void UpdateStreamUsage();
143     int32_t InitEffectBuffer(const std::string &sessionID);
144 
145 private:
146     int32_t SetAudioEffectChainDynamic(const std::string &sceneType, const std::string &effectMode);
147     void UpdateSensorState();
148     void DeleteAllChains();
149     void RecoverAllChains();
150     int32_t EffectDspVolumeUpdate(std::shared_ptr<AudioEffectVolume> audioEffectVolume);
151     int32_t EffectApVolumeUpdate(std::shared_ptr<AudioEffectVolume> audioEffectVolume);
152     int32_t SendEffectApVolume(std::shared_ptr<AudioEffectVolume> audioEffectVolume);
153     void SetSpatializationSceneTypeToChains();
154     void SetSpatializationEnabledToChains();
155     void SetSpkOffloadState();
156     void UpdateCurrSceneType(AudioEffectScene &currSceneType, std::string &sceneType);
157     void FindMaxEffectChannels(const std::string &sceneType, const std::set<std::string> &sessions, uint32_t &channels,
158         uint64_t &channelLayout);
159     int32_t UpdateDeviceInfo(int32_t device, const std::string &sinkName);
160     std::shared_ptr<AudioEffectChain> CreateAudioEffectChain(const std::string &sceneType, bool isPriorScene);
161     bool CheckIfSpkDsp();
162     void CheckAndReleaseCommonEffectChain(const std::string &sceneType);
163     void FindMaxSessionID(uint32_t &maxSessionID, std::string &sceneType,
164         const std::string &scenePairType, std::set<std::string> &sessions);
165     void UpdateCurrSceneTypeAndStreamUsageForDsp();
166     bool IsEffectChainStop(const std::string &sceneType, const std::string &sessionID);
167     int32_t InitEffectBufferInner(const std::string &sessionID);
168     int32_t InitAudioEffectChainDynamicInner(const std::string &sceneType);
169 #ifdef WINDOW_MANAGER_ENABLE
170     int32_t EffectDspRotationUpdate(std::shared_ptr<AudioEffectRotation> audioEffectRotation,
171         const uint32_t rotationState);
172     int32_t EffectApRotationUpdate(std::shared_ptr<AudioEffectRotation> audioEffectRotation,
173         const uint32_t rotationState);
174 #endif
175     std::map<std::string, std::shared_ptr<AudioEffectLibEntry>> effectToLibraryEntryMap_;
176     std::map<std::string, std::string> effectToLibraryNameMap_;
177     std::map<std::string, std::vector<std::string>> effectChainToEffectsMap_;
178     std::map<std::string, std::string> sceneTypeAndModeToEffectChainNameMap_;
179     std::map<std::string, std::shared_ptr<AudioEffectChain>> sceneTypeToEffectChainMap_;
180     std::map<std::string, int32_t> sceneTypeToEffectChainCountMap_;
181     std::set<std::string> sessionIDSet_;
182     std::map<std::string, std::set<std::string>> sceneTypeToSessionIDMap_;
183     std::map<std::string, SessionEffectInfo> sessionIDToEffectInfoMap_;
184     std::map<std::string, int32_t> sceneTypeToEffectChainCountBackupMap_;
185     std::set<std::string> sceneTypeToSpecialEffectSet_;
186     std::vector<std::string> priorSceneList_;
187     std::vector<std::pair<std::string, int32_t>> sceneTypeCountList_;
188     DeviceType deviceType_ = DEVICE_TYPE_SPEAKER;
189     std::string deviceSink_ = DEFAULT_DEVICE_SINK;
190     std::string deviceClass_ = "";
191     std::string extraSceneType_ = "0";
192     std::string maxSessionIDToSceneType_ = "";
193     std::string maxDefaultSessionIDToSceneType_ = "";
194     bool isInitialized_ = false;
195     std::recursive_mutex dynamicMutex_;
196     std::atomic<bool> spatializationEnabled_ = false;
197     bool headTrackingEnabled_ = false;
198     bool btOffloadEnabled_ = false;
199     bool spkOffloadEnabled_ = false;
200     bool initializedLogFlag_ = true;
201     bool btOffloadSupported_ = false;
202     AudioSpatializationSceneType spatializationSceneType_ = SPATIALIZATION_SCENE_TYPE_DEFAULT;
203     bool isDefaultEffectChainExisted_ = false;
204     bool debugArmFlag_ = false;
205     int32_t defaultEffectChainCount_ = 0;
206     int32_t maxEffectChainCount_ = 1;
207     uint32_t maxSessionID_ = 0;
208     AudioSpatialDeviceType spatialDeviceType_{ EARPHONE_TYPE_OTHERS };
209 
210 #ifdef SENSOR_ENABLE
211     std::shared_ptr<HeadTracker> headTracker_;
212 #endif
213 
214     std::shared_ptr<AudioEffectHdiParam> audioEffectHdiParam_;
215     int8_t effectHdiInput_[SEND_HDI_COMMAND_LEN];
216 };
217 }  // namespace AudioStandard
218 }  // namespace OHOS
219 #endif // AUDIO_EFFECT_CHAIN_MANAGER_H
220