1 /*
2  * Copyright (c) 2024 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 OHOS_DAUDIO_ECHO_CANNEL_MANAGER_H
17 #define OHOS_DAUDIO_ECHO_CANNEL_MANAGER_H
18 
19 #include <queue>
20 
21 #include "single_instance.h"
22 
23 #include "audio_capturer.h"
24 #include "audio_info.h"
25 #include "aec_effector.h"
26 #include "iaudio_datatrans_callback.h"
27 
28 #include "audio_data.h"
29 #include "audio_param.h"
30 #include "daudio_util.h"
31 
32 namespace OHOS {
33 namespace DistributedHardware {
34 class DAudioEchoCannelManager : public AudioStandard::AudioCapturerReadCallback,
35     public std::enable_shared_from_this<DAudioEchoCannelManager> {
36 public:
37     DAudioEchoCannelManager();
38     ~DAudioEchoCannelManager();
39 
40     int32_t SetUp(const AudioCommonParam param,
41         const std::shared_ptr<IAudioDataTransCallback> &callback);
42     int32_t Start();
43     int32_t Stop();
44     int32_t Release();
45 
46     int32_t OnMicDataReceived(const std::shared_ptr<AudioData> &pipeInData);
47 
48 private:
49     void OnReadData(size_t length) override;
50     void AecProcessData();
51     void CircuitStart();
52     int32_t ProcessMicData(const std::shared_ptr<AudioData> &pipeInData,
53         std::shared_ptr<AudioData> &micOutData);
54 
55     int32_t AudioCaptureSetUp();
56     int32_t AudioCaptureStart();
57     int32_t AudioCaptureStop();
58     int32_t AudioCaptureRelease();
59 
60     int32_t LoadAecProcessor();
61     void UnLoadAecProcessor();
62     int32_t InitAecProcessor();
63     int32_t StartAecProcessor();
64     int32_t StopAecProcessor();
65     int32_t ReleaseAecProcessor();
66 
67 private:
68     const std::string DUMP_DAUDIO_AEC_REFERENCE_FILENAME = "dump_aec_reference_signal.pcm";
69     const std::string DUMP_DAUDIO_AEC_RECORD_FILENAME = "dump_aec_record_signal.pcm";
70     const std::string DUMP_DAUDIO_AEC_CIRCUIT_FILENAME = "dump_aec_circuit.pcm";
71     const std::string DUMP_DAUDIO_AEC_AFTER_PROCESS_FILENAME = "dump_aec_after_process.pcm";
72 
73     std::unique_ptr<AudioStandard::AudioCapturer> audioCapturer_ = nullptr;
74     std::atomic<bool> isAecRunning_ = false;
75     std::thread aecProcessThread_;
76     static constexpr const char* AECTHREADNAME = "AecProcessThread";
77     std::atomic<bool> isCircuitStartRunning_ = false;
78     std::thread circuitStartThread_;
79 
80     std::shared_ptr<IAudioDataTransCallback> devCallback_;
81     void *aecHandler_ = nullptr;
82     AecEffector *aecProcessor_ = nullptr;
83     constexpr static size_t COND_WAIT_TIME_MS = 10;
84     constexpr static size_t WAIT_MIC_DATA_TIME_US = 5000;
85     constexpr static size_t REF_QUEUE_MAX_SIZE = 4;
86     std::queue<std::shared_ptr<AudioData>> refDataQueue_;
87     std::queue<std::shared_ptr<AudioData>> outDataQueue_;
88     std::mutex refQueueMtx_;
89     std::mutex outQueueMtx_;
90     std::condition_variable refQueueCond_;
91     std::atomic<bool> isStarted = false;
92     FILE *dumpFileRef_ = nullptr;
93     FILE *dumpFileRec_ = nullptr;
94     FILE *dumpFileAft_ = nullptr;
95     FILE *dumpFileCir_ = nullptr;
96 };
97 } // namespace DistributedHardware
98 } // namespace OHOS
99 #endif // OHOS_DAUDIO_DMIC_DEV_H
100