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