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 BLUETOOTH_AUDIO_CAPTURE_SOURCE_H 17 #define BLUETOOTH_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 "audio_proxy_manager.h" 25 #include "util/audio_running_lock.h" 26 #include "util/callback_wrapper.h" 27 #include "capturer_clock_manager.h" 28 29 namespace OHOS { 30 namespace AudioStandard { 31 typedef struct OHOS::HDI::Audio_Bluetooth::AudioSampleAttributes BtAudioSampleAttributes; 32 typedef struct OHOS::HDI::Audio_Bluetooth::AudioDeviceDescriptor BtAudioDeviceDescriptor; 33 typedef enum OHOS::HDI::Audio_Bluetooth::AudioFormat BtAudioFormat; 34 typedef struct OHOS::HDI::Audio_Bluetooth::AudioCapture BtAudioCapture; 35 36 class BluetoothAudioCaptureSource : public IAudioCaptureSource { 37 public: 38 explicit BluetoothAudioCaptureSource(const uint32_t captureId); 39 ~BluetoothAudioCaptureSource(); 40 41 int32_t Init(const IAudioSourceAttr &attr) override; 42 void DeInit(void) override; 43 bool IsInited(void) override; 44 45 int32_t Start(void) override; 46 int32_t Stop(void) override; 47 int32_t Resume(void) override; 48 int32_t Pause(void) override; 49 int32_t Flush(void) override; 50 int32_t Reset(void) override; 51 int32_t CaptureFrame(char *frame, uint64_t requestBytes, uint64_t &replyBytes) override; 52 int32_t CaptureFrameWithEc(FrameDesc *fdesc, uint64_t &replyBytes, FrameDesc *fdescEc, 53 uint64_t &replyBytesEc) override; 54 55 std::string GetAudioParameter(const AudioParamKey key, const std::string &condition) override; 56 57 int32_t SetVolume(float left, float right) override; 58 int32_t GetVolume(float &left, float &right) override; 59 int32_t SetMute(bool isMute) override; 60 int32_t GetMute(bool &isMute) override; 61 62 uint64_t GetTransactionId(void) override; 63 int32_t GetPresentationPosition(uint64_t &frames, int64_t &timeSec, int64_t &timeNanoSec) override; 64 float GetMaxAmplitude(void) override; 65 66 int32_t SetAudioScene(AudioScene audioScene, bool scoExcludeFlag = false) override; 67 68 int32_t UpdateActiveDevice(DeviceType inputDevice) override; 69 void RegistCallback(uint32_t type, std::shared_ptr<IAudioSourceCallback> callback) override; 70 71 int32_t UpdateAppsUid(const int32_t appsUid[PA_MAX_OUTPUTS_PER_SOURCE], const size_t size) final; 72 int32_t UpdateAppsUid(const std::vector<int32_t> &appsUid) final; 73 74 void SetInvalidState(void) override; 75 76 void DumpInfo(std::string &dumpString) override; 77 78 void SetDmDeviceType(uint16_t dmDeviceType, DeviceType deviceType) override; 79 80 private: 81 static BtAudioFormat ConvertToHdiFormat(AudioSampleFormat format); 82 void InitAudioSampleAttr(BtAudioSampleAttributes ¶m); 83 void InitDeviceDesc(BtAudioDeviceDescriptor &deviceDesc); 84 void SetAudioRouteInfoForEnhanceChain(void); 85 int32_t CreateCapture(void); 86 void InitLatencyMeasurement(void); 87 void DeInitLatencyMeasurement(void); 88 void CheckLatencySignal(uint8_t *frame, size_t replyBytes); 89 void CheckUpdateState(char *frame, size_t replyBytes); 90 int32_t DoStop(void); 91 bool IsValidState(void); 92 93 private: 94 static constexpr uint32_t AUDIO_CHANNELCOUNT = 2; 95 static constexpr uint32_t AUDIO_SAMPLE_RATE_48K = 48000; 96 static constexpr uint32_t DEEP_BUFFER_CAPTURE_PERIOD_SIZE = 4096; 97 static constexpr uint16_t GET_MAX_AMPLITUDE_FRAMES_THRESHOLD = 10; 98 static constexpr int32_t HALF_FACTOR = 2; 99 static constexpr uint32_t AUDIO_BUFFER_SIZE = 16 * 1024; 100 #ifdef FEATURE_POWER_MANAGER 101 static constexpr const char *RUNNING_LOCK_NAME = "AudioBluetoothCapture"; 102 static constexpr int32_t RUNNING_LOCK_TIMEOUTMS_LASTING = -1; 103 #endif 104 105 uint32_t captureId_ = HDI_INVALID_ID; 106 std::string halName_ = ""; 107 IAudioSourceAttr attr_ = {}; 108 SourceCallbackWrapper callback_ = {}; 109 bool sourceInited_ = false; 110 bool started_ = false; 111 bool paused_ = false; 112 bool validState_ = true; 113 float leftVolume_ = 0.0; 114 float rightVolume_ = 0.0; 115 std::mutex statusMutex_; 116 uint32_t hdiCaptureId_ = 0; 117 std::string adapterNameCase_ = "bt_hdap"; 118 BtAudioCapture *audioCapture_ = nullptr; 119 // for signal detect 120 std::shared_ptr<SignalDetectAgent> signalDetectAgent_ = nullptr; 121 bool signalDetected_ = false; 122 std::mutex signalDetectMutex_; 123 // for get amplitude 124 float maxAmplitude_ = 0; 125 int64_t lastGetMaxAmplitudeTime_ = 0; 126 int64_t last10FrameStartTime_ = 0; 127 bool startUpdate_ = false; 128 int captureFrameNum_ = 0; 129 // for dfx log 130 int32_t logMode_ = 0; 131 std::string logUtilsTag_ = "A2dpSource"; 132 mutable int64_t volumeDataCount_ = 0; 133 #ifdef FEATURE_POWER_MANAGER 134 std::shared_ptr<AudioRunningLock> runningLock_; 135 #endif 136 FILE *dumpFile_ = nullptr; 137 std::string dumpFileName_ = ""; 138 DeviceType currentActiveDevice_ = DEVICE_TYPE_BLUETOOTH_A2DP_IN; 139 bool muteState_ = false; 140 141 std::shared_ptr<AudioSourceClock> audioSrcClock_ = nullptr; 142 }; 143 144 } // namespace AudioStandard 145 } // namespace OHOS 146 147 #endif // BLUETOOTH_AUDIO_CAPTURE_SOURCE_H 148