• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 AUDIO_CAPTURE_SOURCE_H
17 #define AUDIO_CAPTURE_SOURCE_H
18 
19 #include "source/i_audio_capture_source.h"
20 #include <iostream>
21 #include <cstring>
22 #include <mutex>
23 #include <thread>
24 #include "v4_0/iaudio_manager.h"
25 #include "audio_utils.h"
26 #include "util/audio_running_lock.h"
27 #include "util/ring_buffer_handler.h"
28 #include "util/callback_wrapper.h"
29 
30 namespace OHOS {
31 namespace AudioStandard {
32 class AudioCaptureSource : public IAudioCaptureSource {
33 public:
34     explicit AudioCaptureSource(const uint32_t captureId, const std::string &halName = "primary");
35     ~AudioCaptureSource();
36 
37     int32_t Init(const IAudioSourceAttr &attr) override;
38     void DeInit(void) override;
39     bool IsInited(void) override;
40 
41     int32_t Start(void) override;
42     int32_t Stop(void) override;
43     int32_t Resume(void) override;
44     int32_t Pause(void) override;
45     int32_t Flush(void) override;
46     int32_t Reset(void) override;
47     int32_t CaptureFrame(char *frame, uint64_t requestBytes, uint64_t &replyBytes) override;
48     int32_t CaptureFrameWithEc(FrameDesc *fdesc, uint64_t &replyBytes, FrameDesc *fdescEc,
49         uint64_t &replyBytesEc) override;
50 
51     std::string GetAudioParameter(const AudioParamKey key, const std::string &condition) override;
52 
53     int32_t SetVolume(float left, float right) override;
54     int32_t GetVolume(float &left, float &right) override;
55     int32_t SetMute(bool isMute) override;
56     int32_t GetMute(bool &isMute) override;
57 
58     uint64_t GetTransactionId(void) override;
59     int32_t GetPresentationPosition(uint64_t &frames, int64_t &timeSec, int64_t &timeNanoSec) override;
60     float GetMaxAmplitude(void) override;
61 
62     int32_t SetAudioScene(AudioScene audioScene, DeviceType activeDevice) override;
63 
64     int32_t UpdateActiveDevice(DeviceType inputDevice) override;
65     int32_t UpdateSourceType(SourceType sourceType) override;
66     void RegistCallback(uint32_t type, IAudioSourceCallback *callback) override;
67     void RegistCallback(uint32_t type, std::shared_ptr<IAudioSourceCallback> callback) override;
68 
69     int32_t UpdateAppsUid(const int32_t appsUid[PA_MAX_OUTPUTS_PER_SOURCE], const size_t size) final;
70     int32_t UpdateAppsUid(const std::vector<int32_t> &appsUid) final;
71 
72     void SetAddress(const std::string &address) override;
73 
74     void DumpInfo(std::string &dumpString) override;
75 
76 private:
77     static AudioFormat ConvertToHdiFormat(AudioSampleFormat format);
78     static uint64_t GetChannelLayoutByChannelCount(uint32_t channelCount);
79     static enum AudioInputType ConvertToHDIAudioInputType(int32_t sourceType);
80     static AudioSampleFormat ParseAudioFormat(const std::string &format);
81     static AudioCategory GetAudioCategory(AudioScene audioScene);
82     static int32_t GetByteSizeByFormat(AudioSampleFormat format);
83     static bool IsFormalSourceType(int32_t sourceType);
84     uint32_t GetUniqueId(void) const;
85     uint32_t GetUniqueIdBySourceType(void) const;
86     void InitEcOrMicRefAttr(const IAudioSourceAttr &attr);
87     void InitAudioSampleAttr(struct AudioSampleAttributes &param);
88     void InitDeviceDesc(struct AudioDeviceDescriptor &deviceDesc);
89     void InitSceneDesc(struct AudioSceneDescriptor &sceneDesc, AudioScene audioScene);
90     void SetAudioRouteInfoForEnhanceChain(void);
91     int32_t CreateCapture(void);
92     int32_t DoSetInputRoute(DeviceType inputDevice);
93     int32_t InitCapture(void);
94     void InitLatencyMeasurement(void);
95     void DeInitLatencyMeasurement(void);
96     void CheckLatencySignal(uint8_t *frame, size_t replyBytes);
97     void CheckUpdateState(char *frame, size_t replyBytes);
98     bool IsNonblockingSource(const std::string &adapterName);
99     int32_t NonblockingStart(void);
100     int32_t NonblockingStop(void);
101     int32_t NonblockingCaptureFrameWithEc(FrameDesc *fdescEc, uint64_t &replyBytesEc);
102     void CaptureFrameOnlyEc(std::vector<uint8_t> &ecData);
103     void CaptureThreadLoop(void);
104     int32_t UpdateActiveDeviceWithoutLock(DeviceType inputDevice);
105     int32_t DoStop(void);
106     void DumpData(char *frame, uint64_t &replyBytes);
107 
108 private:
109     static constexpr uint32_t AUDIO_CHANNELCOUNT = 2;
110     static constexpr uint32_t AUDIO_SAMPLE_RATE_48K = 48000;
111     static constexpr uint32_t DEEP_BUFFER_CAPTURE_PERIOD_SIZE = 4096;
112     static constexpr uint32_t STEREO_CHANNEL_COUNT = 2;
113     static constexpr float MAX_VOLUME_LEVEL = 15.0f;
114     static constexpr uint16_t GET_MAX_AMPLITUDE_FRAMES_THRESHOLD = 10;
115     static constexpr int32_t HALF_FACTOR = 2;
116     static constexpr uint32_t AUDIO_BUFFER_SIZE = 16 * 1024;
117     static constexpr uint32_t USB_DEFAULT_BUFFER_SIZE = 3840;
118     static constexpr uint32_t FRAME_TIME_LEN_MS = 20; // 20ms
119 #ifdef FEATURE_POWER_MANAGER
120     static constexpr const char *RUNNING_LOCK_NAME = "AudioPrimaryCapture";
121     static constexpr const char *RUNNING_LOCK_NAME_WAKEUP = "AudioWakeupCapture";
122     static constexpr int32_t RUNNING_LOCK_TIMEOUTMS_LASTING = -1;
123 #endif
124 
125     uint32_t captureId_ = HDI_INVALID_ID;
126     const std::string halName_ = "";
127     IAudioSourceAttr attr_ = {};
128     std::mutex callbackMutex_;
129     SourceCallbackWrapper callback_ = {};
130     bool sourceInited_ = false;
131     bool captureInited_ = false;
132     bool started_ = false;
133     bool paused_ = false;
134     float leftVolume_ = MAX_VOLUME_LEVEL;
135     float rightVolume_ = MAX_VOLUME_LEVEL;
136     std::mutex statusMutex_;
137     uint32_t openMic_ = 0;
138     uint32_t hdiCaptureId_ = 0;
139     std::string adapterNameCase_ = "";
140     struct IAudioCapture *audioCapture_ = nullptr;
141     // for signal detect
142     std::shared_ptr<SignalDetectAgent> signalDetectAgent_ = nullptr;
143     bool signalDetected_ = false;
144     std::mutex signalDetectMutex_;
145     // for get amplitude
146     float maxAmplitude_ = 0;
147     int64_t lastGetMaxAmplitudeTime_ = 0;
148     int64_t last10FrameStartTime_ = 0;
149     bool startUpdate_ = false;
150     int captureFrameNum_ = 0;
151     // for dfx log
152     int32_t logMode_ = 0;
153     std::string logUtilsTag_ = "";
154     mutable int64_t volumeDataCount_ = 0;
155     // for ec and mic_ref
156     std::unique_ptr<std::thread> captureThread_ = nullptr;
157     bool isCaptureThreadRunning_ = false;
158     std::shared_ptr<RingBufferHandler> ringBufferHandler_ = nullptr;
159 #ifdef FEATURE_POWER_MANAGER
160     std::shared_ptr<AudioRunningLock> runningLock_;
161 #endif
162     FILE *dumpFile_ = nullptr;
163     std::string dumpFileName_ = "";
164     DeviceType currentActiveDevice_ = DEVICE_TYPE_INVALID;
165     AudioScene currentAudioScene_ = AUDIO_SCENE_INVALID;
166     std::atomic<bool> muteState_ = false;
167     std::string address_ = "";
168 };
169 
170 } // namespace AudioStandard
171 } // namespace OHOS
172 
173 #endif // AUDIO_CAPTURE_SOURCE_H
174