• 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 FAST_AUDIO_CAPTURE_SOURCE_H
17 #define FAST_AUDIO_CAPTURE_SOURCE_H
18 
19 #include "source/i_audio_capture_source.h"
20 #include <iostream>
21 #include <cstring>
22 #include "v5_0/iaudio_manager.h"
23 #include "util/audio_running_lock.h"
24 #include "util/callback_wrapper.h"
25 
26 namespace OHOS {
27 namespace AudioStandard {
28 class FastAudioCaptureSource : public IAudioCaptureSource {
29 public:
30     FastAudioCaptureSource() = default;
31     ~FastAudioCaptureSource();
32 
33     int32_t Init(const IAudioSourceAttr &attr) override;
34     void DeInit(void) override;
35     bool IsInited(void) override;
36 
37     int32_t Start(void) override;
38     int32_t Stop(void) override;
39     int32_t Resume(void) override;
40     int32_t Pause(void) override;
41     int32_t Flush(void) override;
42     int32_t Reset(void) override;
43     int32_t CaptureFrame(char *frame, uint64_t requestBytes, uint64_t &replyBytes) override;
44     int32_t CaptureFrameWithEc(FrameDesc *fdesc, uint64_t &replyBytes, FrameDesc *fdescEc,
45         uint64_t &replyBytesEc) override;
46 
47     std::string GetAudioParameter(const AudioParamKey key, const std::string &condition) override;
48 
49     int32_t SetVolume(float left, float right) override;
50     int32_t GetVolume(float &left, float &right) override;
51     int32_t SetMute(bool isMute) override;
52     int32_t GetMute(bool &isMute) override;
53 
54     uint64_t GetTransactionId(void) override;
55     int32_t GetPresentationPosition(uint64_t &frames, int64_t &timeSec, int64_t &timeNanoSec) override;
56     float GetMaxAmplitude(void) override;
57 
58     int32_t SetAudioScene(AudioScene audioScene, bool scoExcludeFlag = false) override;
59 
60     int32_t UpdateActiveDevice(DeviceType inputDevice) override;
61     void RegistCallback(uint32_t type, std::shared_ptr<IAudioSourceCallback> callback) override;
62 
63     int32_t UpdateAppsUid(const int32_t appsUid[PA_MAX_OUTPUTS_PER_SOURCE], const size_t size) final;
64     int32_t UpdateAppsUid(const std::vector<int32_t> &appsUid) final;
65 
66     void DumpInfo(std::string &dumpString) override;
67 
68     void SetDmDeviceType(uint16_t dmDeviceType, DeviceType deviceType) override;
69 
70 private:
71     int32_t GetMmapBufferInfo(int &fd, uint32_t &totalSizeInframe, uint32_t &spanSizeInframe,
72         uint32_t &byteSizePerFrame, uint32_t &syncInfoSize) override;
73     int32_t GetMmapHandlePosition(uint64_t &frames, int64_t &timeSec, int64_t &timeNanoSec) override;
74 
75     static uint32_t PcmFormatToBit(AudioSampleFormat format);
76     static AudioFormat ConvertToHdiFormat(AudioSampleFormat format);
77     static enum AudioInputType ConvertToHDIAudioInputType(int32_t sourceType);
78     static AudioCategory GetAudioCategory(AudioScene audioScene);
79     void InitAudioSampleAttr(struct AudioSampleAttributes &param);
80     void InitDeviceDesc(struct AudioDeviceDescriptor &deviceDesc);
81     void InitSceneDesc(struct AudioSceneDescriptor &sceneDesc, AudioScene audioScene);
82     int32_t CreateCapture(void);
83     int32_t DoSetInputRoute(DeviceType inputDevice);
84 
85     // low latency
86     int32_t PrepareMmapBuffer(void);
87     int32_t CheckPositionTime(void);
88 
89 private:
90     static constexpr uint32_t AUDIO_CHANNELCOUNT = 2;
91     static constexpr int32_t MAX_GET_POSITION_TRY_COUNT = 50;
92     static constexpr int64_t GENERAL_MAX_GET_POSITION_HANDLE_TIME = 10000000; // 10ms = 10ns * 1000 * 1000
93     static constexpr int64_t VOIP_MAX_GET_POSITION_HANDLE_TIME = 20000000; // 20ms = 20ns * 1000 * 1000
94     static constexpr int32_t MAX_GET_POSITION_WAIT_TIME = 2000000; // 2000000us
95     static constexpr int32_t INVALID_FD = -1;
96 #ifdef FEATURE_POWER_MANAGER
97     static constexpr const char *RUNNING_LOCK_NAME = "AudioFastCapture";
98     static constexpr int32_t RUNNING_LOCK_TIMEOUTMS_LASTING = -1;
99 #endif
100 
101     IAudioSourceAttr attr_ = {};
102     SourceCallbackWrapper callback_ = {};
103     bool sourceInited_ = false;
104     bool started_ = false;
105     bool paused_ = false;
106     std::mutex statusMutex_;
107     uint32_t hdiCaptureId_ = 0;
108     struct IAudioCapture *audioCapture_ = nullptr;
109 #ifdef FEATURE_POWER_MANAGER
110     std::shared_ptr<AudioRunningLock> runningLock_;
111 #endif
112     AudioScene currentAudioScene_ = AUDIO_SCENE_INVALID;
113     std::atomic<bool> isCheckPositionSuccess_ = true;
114 
115     // low latency
116     int32_t bufferFd_ = INVALID_FD;
117     uint32_t bufferTotalFrameSize_ = 0;
118     uint32_t eachReadFrameSize_ = 0;
119     size_t bufferSize_ = 0;
120     uint32_t syncInfoSize_ = 0;
121 };
122 
123 } // namespace AudioStandard
124 } // namespace OHOS
125 
126 #endif // FAST_AUDIO_CAPTURE_SOURCE_H
127