• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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_EC_MANAGER_H
17 #define ST_AUDIO_EC_MANAGER_H
18 
19 #include <bitset>
20 #include <list>
21 #include <string>
22 #include <unordered_map>
23 #include <unordered_set>
24 #include <mutex>
25 #include "singleton.h"
26 #include "audio_group_handle.h"
27 #include "audio_ec_info.h"
28 #include "audio_manager_base.h"
29 #include "audio_module_info.h"
30 #include "audio_router_center.h"
31 #include "audio_policy_manager_factory.h"
32 
33 #include "audio_policy_config_manager.h"
34 #include "audio_active_device.h"
35 #include "audio_iohandle_map.h"
36 
37 namespace OHOS {
38 namespace AudioStandard {
39 
40 class AudioEcManager {
41 public:
GetInstance()42     static AudioEcManager& GetInstance()
43     {
44         static AudioEcManager instance;
45         return instance;
46     }
47 
48     void Init(int32_t ecEnableState, int32_t micRefEnableState);
49     void CloseNormalSource();
50     AudioEcInfo GetAudioEcInfo();
51     void ResetAudioEcInfo();
52 
53     void PresetArmIdleInput(const string &address);
54     void ActivateArmDevice(const string &address, const DeviceRole role);
55     void CloseUsbArmDevice(const AudioDeviceDescriptor &device);
56     void GetTargetSourceTypeAndMatchingFlag(SourceType source, SourceType &targetSource, bool &useMatchingPropInfo);
57 
58     int32_t FetchTargetInfoForSessionAdd(const SessionInfo sessionInfo, PipeStreamPropInfo &targetInfo,
59         SourceType &targetSourceType);
60 
61     void ReloadSourceForSession(SessionInfo sessionInfo);
62 
63     void SetDpSinkModuleInfo(const AudioModuleInfo &moduleInfo);
64     void SetPrimaryMicModuleInfo(const AudioModuleInfo &moduleInfo);
65     SourceType GetSourceOpened();
66     bool GetEcFeatureEnable();
67     bool GetMicRefFeatureEnable();
68     void UpdateStreamEcAndMicRefInfo(AudioModuleInfo &moduleInfo, SourceType sourceType);
69     void SetOpenedNormalSource(SourceType sourceType);
70     void PrepareNormalSource(AudioModuleInfo &moduleInfo, std::shared_ptr<AudioStreamDescriptor> &streamDesc);
71     void SetOpenedNormalSourceSessionId(uint64_t sessionId);
72     uint64_t GetOpenedNormalSourceSessionId();
73     int32_t ReloadNormalSource(SessionInfo &sessionInfo, PipeStreamPropInfo &targetInfo, SourceType targetSource);
74     void UpdateStreamEcInfo(AudioModuleInfo &moduleInfo, SourceType sourceType);
75     void UpdateStreamMicRefInfo(AudioModuleInfo &moduleInfo, SourceType sourceType);
76 private:
AudioEcManager()77     AudioEcManager() : audioPolicyManager_(AudioPolicyManagerFactory::GetAudioPolicyManager()),
78         audioRouterCenter_(AudioRouterCenter::GetAudioRouterCenter()),
79         audioIOHandleMap_(AudioIOHandleMap::GetInstance()),
80         audioActiveDevice_(AudioActiveDevice::GetInstance()),
81         audioConfigManager_(AudioPolicyConfigManager::GetInstance()) {}
~AudioEcManager()82     ~AudioEcManager() {}
83 
84     void UpdateEnhanceEffectState(SourceType source);
85     void UpdateStreamCommonInfo(AudioModuleInfo &moduleInfo, PipeStreamPropInfo &targetInfo, SourceType sourceType);
86     void UpdateAudioEcInfo(const AudioDeviceDescriptor &inputDevice, const AudioDeviceDescriptor &outputDevice);
87     void UpdateModuleInfoForEc(AudioModuleInfo &moduleInfo);
88     void UpdateModuleInfoForMicRef(AudioModuleInfo &moduleInfo, SourceType sourceType);
89     std::string ShouldOpenMicRef(SourceType source);
90 
91     EcType GetEcType(const DeviceType inputDevice, const DeviceType outputDevice);
92     std::string GetEcSamplingRate(const std::string &halName, std::shared_ptr<PipeStreamPropInfo> &outModuleInfo);
93     std::string GetEcFormat(const std::string &halName, std::shared_ptr<PipeStreamPropInfo> &outModuleInfo);
94     std::string GetEcChannels(const std::string &halName, std::shared_ptr<PipeStreamPropInfo> &outModuleInfo);
95 
96     int32_t GetPipeInfoByDeviceTypeForEc(const std::string &role, const DeviceType deviceType,
97         std::shared_ptr<AdapterPipeInfo> &pipeInfo);
98 
99     void UpdateArmModuleInfo(const string &address, const DeviceRole role, AudioModuleInfo &moduleInfo);
100     std::string GetHalNameForDevice(const std::string &role, const DeviceType deviceType);
101     std::string GetPipeNameByDeviceForEc(const std::string &role, const DeviceType deviceType);
102 private:
103     bool isEcFeatureEnable_ = false;
104     bool isMicRefFeatureEnable_ = false;
105 
106     AudioModuleInfo usbSinkModuleInfo_ = {};
107     AudioModuleInfo usbSourceModuleInfo_ = {};
108     AudioModuleInfo primaryMicModuleInfo_ = {};
109     AudioModuleInfo dpSinkModuleInfo_ = {};
110     SourceType normalSourceOpened_ = SOURCE_TYPE_INVALID;
111     uint64_t sessionIdUsedToOpenSource_ = 0;
112 
113     std::mutex audioEcInfoMutex_;
114     AudioEcInfo audioEcInfo_;
115     std::string activeArmInputAddr_;
116     std::string activeArmOutputAddr_;
117 
118     bool isMicRefVoipUpOn_ = false;
119     bool isMicRefRecordOn_ = false;
120 
121     IAudioPolicyInterface& audioPolicyManager_;
122     AudioRouterCenter& audioRouterCenter_;
123     AudioIOHandleMap& audioIOHandleMap_;
124     AudioActiveDevice& audioActiveDevice_;
125     AudioPolicyConfigManager& audioConfigManager_;
126 };
127 
128 }
129 }
130 #endif
131