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 "v4_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, DeviceType activeDevice) 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 private: 69 int32_t GetMmapBufferInfo(int &fd, uint32_t &totalSizeInframe, uint32_t &spanSizeInframe, 70 uint32_t &byteSizePerFrame) override; 71 int32_t GetMmapHandlePosition(uint64_t &frames, int64_t &timeSec, int64_t &timeNanoSec) override; 72 73 static uint32_t PcmFormatToBit(AudioSampleFormat format); 74 static AudioFormat ConvertToHdiFormat(AudioSampleFormat format); 75 static enum AudioInputType ConvertToHDIAudioInputType(int32_t sourceType); 76 static AudioCategory GetAudioCategory(AudioScene audioScene); 77 void InitAudioSampleAttr(struct AudioSampleAttributes ¶m); 78 void InitDeviceDesc(struct AudioDeviceDescriptor &deviceDesc); 79 void InitSceneDesc(struct AudioSceneDescriptor &sceneDesc, AudioScene audioScene); 80 int32_t CreateCapture(void); 81 int32_t DoSetInputRoute(DeviceType inputDevice); 82 83 // low latency 84 int32_t PrepareMmapBuffer(void); 85 int32_t CheckPositionTime(void); 86 87 private: 88 static constexpr uint32_t AUDIO_CHANNELCOUNT = 2; 89 static constexpr int32_t MAX_GET_POSITION_TRY_COUNT = 50; 90 static constexpr int64_t GENERAL_MAX_GET_POSITION_HANDLE_TIME = 10000000; // 10ms = 10ns * 1000 * 1000 91 static constexpr int64_t VOIP_MAX_GET_POSITION_HANDLE_TIME = 20000000; // 20ms = 20ns * 1000 * 1000 92 static constexpr int32_t MAX_GET_POSITION_WAIT_TIME = 2000000; // 2000000us 93 static constexpr int32_t INVALID_FD = -1; 94 #ifdef FEATURE_POWER_MANAGER 95 static constexpr const char *RUNNING_LOCK_NAME = "AudioFastCapture"; 96 static constexpr int32_t RUNNING_LOCK_TIMEOUTMS_LASTING = -1; 97 #endif 98 99 IAudioSourceAttr attr_ = {}; 100 SourceCallbackWrapper callback_ = {}; 101 bool sourceInited_ = false; 102 bool started_ = false; 103 bool paused_ = false; 104 std::mutex statusMutex_; 105 uint32_t hdiCaptureId_ = 0; 106 struct IAudioCapture *audioCapture_ = nullptr; 107 #ifdef FEATURE_POWER_MANAGER 108 std::shared_ptr<AudioRunningLock> runningLock_; 109 #endif 110 AudioScene currentAudioScene_ = AUDIO_SCENE_INVALID; 111 std::atomic<bool> isCheckPositionSuccess_ = true; 112 113 // low latency 114 int32_t bufferFd_ = INVALID_FD; 115 uint32_t bufferTotalFrameSize_ = 0; 116 uint32_t eachReadFrameSize_ = 0; 117 size_t bufferSize_ = 0; 118 }; 119 120 } // namespace AudioStandard 121 } // namespace OHOS 122 123 #endif // FAST_AUDIO_CAPTURE_SOURCE_H 124