| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907 |
- #include "capture_audio_capturer.h"
- #include "../base/logger.h"
- #include "../base/media_common.h"
- #include <algorithm>
- #include <cmath>
- #ifdef _WIN32
- #include <windows.h>
- #include <mmdeviceapi.h>
- #include <audioclient.h>
- #include <endpointvolume.h>
- #pragma comment(lib, "ole32.lib")
- #endif
- extern "C" {
- #include <libavformat/avformat.h>
- #include <libavdevice/avdevice.h>
- #include <libswresample/swresample.h>
- #include <libavutil/opt.h>
- #include <libavutil/channel_layout.h>
- }
- namespace av {
- namespace capture {
- AudioCapturer::AudioCapturer() : audioParams_(CapturerType::AUDIO_MIC) {
- AV_LOGGER_DEBUG("创建音频采集器");
-
- // 注册设备
- avdevice_register_all();
-
- lastLevelUpdate_ = std::chrono::steady_clock::now();
- }
- AudioCapturer::~AudioCapturer() {
- close();
- AV_LOGGER_DEBUG("音频采集器已销毁");
- }
- ErrorCode AudioCapturer::initialize(const CapturerParams& params) {
- if (params.mediaType != MediaType::AUDIO) {
- AV_LOGGER_ERROR("参数媒体类型不是音频");
- return ErrorCode::INVALID_PARAMS;
- }
-
- audioParams_ = static_cast<const AudioCaptureParams&>(params);
-
- if (!validateParams(audioParams_)) {
- return ErrorCode::INVALID_PARAMS;
- }
-
- ErrorCode result = ErrorCode::SUCCESS;
-
- if (audioParams_.type == CapturerType::AUDIO_MIC) {
- result = initializeMicrophone();
- } else if (audioParams_.type == CapturerType::AUDIO_SYSTEM ||
- audioParams_.type == CapturerType::AUDIO_LOOPBACK) {
- result = initializeSystemAudio();
- } else {
- AV_LOGGER_ERROR("不支持的音频采集器类型");
- return ErrorCode::NOT_SUPPORTED;
- }
-
- if (result == ErrorCode::SUCCESS) {
- setState(CapturerState::INITIALIZED);
- AV_LOGGER_INFOF("音频采集器初始化成功: {}Hz, {}ch, {}",
- audioParams_.sampleRate, audioParams_.channels,
- av_get_sample_fmt_name(audioParams_.sampleFormat));
- }
-
- return result;
- }
- ErrorCode AudioCapturer::start() {
- std::lock_guard<std::mutex> lock(captureMutex_);
-
- if (getState() != CapturerState::INITIALIZED) {
- AV_LOGGER_ERROR("采集器状态无效,无法启动");
- return ErrorCode::INVALID_STATE;
- }
-
- shouldStop_ = false;
-
- // 启动采集线程
- try {
- captureThread_ = std::thread(&AudioCapturer::captureThreadFunc, this);
- setState(CapturerState::STARTED);
-
- AV_LOGGER_INFO("音频采集已启动");
- return ErrorCode::SUCCESS;
- } catch (const std::exception& e) {
- AV_LOGGER_ERRORF("启动音频采集线程失败: {}", e.what());
- return ErrorCode::THREAD_ERROR;
- }
- }
- ErrorCode AudioCapturer::stop() {
- std::lock_guard<std::mutex> lock(captureMutex_);
-
- if (getState() != CapturerState::STARTED) {
- return ErrorCode::SUCCESS;
- }
-
- shouldStop_ = true;
-
- // 唤醒暂停的线程
- {
- std::lock_guard<std::mutex> pauseLock(pauseMutex_);
- paused_ = false;
- pauseCondition_.notify_all();
- }
-
- // 等待线程结束
- if (captureThread_.joinable()) {
- captureThread_.join();
- }
-
- setState(CapturerState::STOPPED);
- AV_LOGGER_INFO("音频采集已停止");
-
- return ErrorCode::SUCCESS;
- }
- ErrorCode AudioCapturer::pause() {
- if (getState() != CapturerState::STARTED) {
- return ErrorCode::INVALID_STATE;
- }
-
- paused_ = true;
- AV_LOGGER_INFO("音频采集已暂停");
-
- return ErrorCode::SUCCESS;
- }
- ErrorCode AudioCapturer::resume() {
- if (getState() != CapturerState::STARTED) {
- return ErrorCode::INVALID_STATE;
- }
-
- {
- std::lock_guard<std::mutex> lock(pauseMutex_);
- paused_ = false;
- pauseCondition_.notify_all();
- }
-
- AV_LOGGER_INFO("音频采集已恢复");
-
- return ErrorCode::SUCCESS;
- }
- ErrorCode AudioCapturer::reset() {
- ErrorCode result = stop();
- if (result != ErrorCode::SUCCESS) {
- return result;
- }
-
- // 清空帧队列
- {
- std::lock_guard<std::mutex> lock(queueMutex_);
- while (!frameQueue_.empty()) {
- frameQueue_.pop();
- }
- }
-
- resetStats();
- audioLevel_ = 0.0f;
- setState(CapturerState::INITIALIZED);
-
- AV_LOGGER_INFO("音频采集器已重置");
-
- return ErrorCode::SUCCESS;
- }
- ErrorCode AudioCapturer::close() {
- stop();
-
- // 清理资源
- cleanupResampler();
- cleanupAudioProcessing();
-
- if (codecCtx_) {
- avcodec_free_context(&codecCtx_);
- codecCtx_ = nullptr;
- }
-
- if (formatCtx_) {
- avformat_close_input(&formatCtx_);
- formatCtx_ = nullptr;
- }
-
- codec_ = nullptr;
- audioStreamIndex_ = -1;
-
- setState(CapturerState::IDLE);
- AV_LOGGER_INFO("音频采集器已关闭");
-
- return ErrorCode::SUCCESS;
- }
- std::vector<std::string> AudioCapturer::getAvailableDevices() const {
- std::vector<std::string> devices;
- auto deviceInfos = getDetailedDeviceInfo();
-
- for (const auto& info : deviceInfos) {
- devices.push_back(info.name);
- }
-
- return devices;
- }
- std::string AudioCapturer::getCurrentDevice() const {
- return audioParams_.deviceName;
- }
- std::vector<AudioDeviceInfo> AudioCapturer::getDetailedDeviceInfo() const {
- std::lock_guard<std::mutex> lock(deviceCacheMutex_);
-
- if (!devicesCached_) {
- if (audioParams_.type == CapturerType::AUDIO_MIC) {
- cachedDevices_ = enumerateMicrophones();
- } else {
- cachedDevices_ = enumerateSystemAudioDevices();
- }
- devicesCached_ = true;
- }
-
- return cachedDevices_;
- }
- ErrorCode AudioCapturer::setAudioParams(int sampleRate, int channels, AVSampleFormat sampleFormat) {
- if (getState() == CapturerState::STARTED) {
- AV_LOGGER_ERROR("无法在采集过程中修改音频参数");
- return ErrorCode::INVALID_STATE;
- }
-
- audioParams_.sampleRate = sampleRate;
- audioParams_.channels = channels;
- audioParams_.sampleFormat = sampleFormat;
-
- AV_LOGGER_INFOF("音频参数已更新: {}Hz, {}ch, {}",
- sampleRate, channels, av_get_sample_fmt_name(sampleFormat));
-
- return ErrorCode::SUCCESS;
- }
- ErrorCode AudioCapturer::setVolume(float volume) {
- if (volume < 0.0f || volume > 2.0f) {
- AV_LOGGER_ERROR("音量值超出范围 (0.0-2.0)");
- return ErrorCode::INVALID_PARAMS;
- }
-
- currentVolume_ = volume;
- audioParams_.volume = volume;
-
- AV_LOGGER_INFOF("音量已设置为: {:.2f}", volume);
-
- return ErrorCode::SUCCESS;
- }
- float AudioCapturer::getVolume() const {
- return currentVolume_;
- }
- ErrorCode AudioCapturer::setNoiseReduction(bool enable) {
- noiseReductionEnabled_ = enable;
- audioParams_.enableNoiseReduction = enable;
-
- AV_LOGGER_INFOF("Noise reduction {}", enable ? "enabled" : "disabled");
-
- return ErrorCode::SUCCESS;
- }
- ErrorCode AudioCapturer::setEchoCancellation(bool enable) {
- echoCancellationEnabled_ = enable;
- audioParams_.enableEchoCancellation = enable;
-
- AV_LOGGER_INFOF("Echo cancellation {}", enable ? "enabled" : "disabled");
-
- return ErrorCode::SUCCESS;
- }
- AudioCaptureParams AudioCapturer::getCurrentParams() const {
- return audioParams_;
- }
- float AudioCapturer::getAudioLevel() const {
- return audioLevel_.load();
- }
- bool AudioCapturer::validateParams(const CapturerParams& params) {
- const auto& audioParams = static_cast<const AudioCaptureParams&>(params);
-
- if (audioParams.sampleRate <= 0 || audioParams.sampleRate > 192000) {
- AV_LOGGER_ERROR("采样率无效");
- return false;
- }
-
- if (audioParams.channels <= 0 || audioParams.channels > 8) {
- AV_LOGGER_ERROR("声道数无效");
- return false;
- }
-
- if (audioParams.bufferSize <= 0 || audioParams.bufferSize > 8192) {
- AV_LOGGER_ERROR("缓冲区大小无效");
- return false;
- }
-
- if (audioParams.volume < 0.0f || audioParams.volume > 2.0f) {
- AV_LOGGER_ERROR("音量值无效");
- return false;
- }
-
- return true;
- }
- ErrorCode AudioCapturer::initializeMicrophone() {
- AV_LOGGER_INFOF("初始化麦克风采集器: 索引={}", audioParams_.micIndex);
-
- #ifdef _WIN32
- return setupDirectSoundMicrophone();
- #elif defined(__linux__)
- return setupALSAMicrophone();
- #elif defined(__APPLE__)
- return setupCoreAudioMicrophone();
- #else
- AV_LOGGER_ERROR("当前平台不支持麦克风采集");
- return ErrorCode::NOT_SUPPORTED;
- #endif
- }
- ErrorCode AudioCapturer::initializeSystemAudio() {
- AV_LOGGER_INFO("初始化系统音频采集器");
-
- #ifdef _WIN32
- return setupWASAPISystemAudio();
- #elif defined(__linux__)
- return setupPulseAudioCapture();
- #elif defined(__APPLE__)
- return setupCoreAudioSystemCapture();
- #else
- AV_LOGGER_ERROR("当前平台不支持系统音频采集");
- return ErrorCode::NOT_SUPPORTED;
- #endif
- }
- ErrorCode AudioCapturer::openInputDevice() {
- const AVInputFormat* inputFormat = getPlatformInputFormat();
- if (!inputFormat) {
- AV_LOGGER_ERROR("获取音频输入格式失败");
- return ErrorCode::NOT_SUPPORTED;
- }
-
- std::string deviceName = getPlatformDeviceName();
- if (deviceName.empty()) {
- AV_LOGGER_ERROR("获取音频设备名称失败");
- return ErrorCode::DEVICE_NOT_FOUND;
- }
-
- AV_LOGGER_INFOF("打开音频输入设备: {} (格式: {})", deviceName, inputFormat->name);
-
- // 设置输入选项
- AVDictionary* options = nullptr;
-
- // 设置音频参数
- av_dict_set(&options, "sample_rate", std::to_string(audioParams_.sampleRate).c_str(), 0);
- av_dict_set(&options, "channels", std::to_string(audioParams_.channels).c_str(), 0);
-
- // 设置缓冲区大小
- av_dict_set(&options, "audio_buffer_size", std::to_string(audioParams_.bufferSize).c_str(), 0);
-
- // 打开输入
- int ret = avformat_open_input(&formatCtx_, deviceName.c_str(), inputFormat, &options);
- av_dict_free(&options);
-
- if (ret < 0) {
- AV_LOGGER_ERRORF("打开音频输入设备失败: {} (设备: {})",
- ffmpeg_utils::errorToString(ret), deviceName);
- return static_cast<ErrorCode>(ret);
- }
-
- // 查找流信息
- ret = avformat_find_stream_info(formatCtx_, nullptr);
- if (ret < 0) {
- AV_LOGGER_ERRORF("查找音频流信息失败: {}", ffmpeg_utils::errorToString(ret));
- return static_cast<ErrorCode>(ret);
- }
-
- // 查找音频流
- audioStreamIndex_ = av_find_best_stream(formatCtx_, AVMEDIA_TYPE_AUDIO, -1, -1, &codec_, 0);
- if (audioStreamIndex_ < 0) {
- AV_LOGGER_ERROR("未找到音频流");
- return ErrorCode::STREAM_NOT_FOUND;
- }
-
- // 创建解码上下文
- codecCtx_ = avcodec_alloc_context3(codec_);
- if (!codecCtx_) {
- AV_LOGGER_ERROR("分配音频解码上下文失败");
- return ErrorCode::MEMORY_ALLOC_FAILED;
- }
-
- // 复制流参数到解码上下文
- ret = avcodec_parameters_to_context(codecCtx_, formatCtx_->streams[audioStreamIndex_]->codecpar);
- if (ret < 0) {
- AV_LOGGER_ERRORF("复制音频流参数失败: {}", ffmpeg_utils::errorToString(ret));
- return static_cast<ErrorCode>(ret);
- }
-
- // 打开解码器
- ret = avcodec_open2(codecCtx_, codec_, nullptr);
- if (ret < 0) {
- AV_LOGGER_ERRORF("打开音频解码器失败: {}", ffmpeg_utils::errorToString(ret));
- return static_cast<ErrorCode>(ret);
- }
-
- // 设置音频重采样
- return setupAudioResampling();
- }
- ErrorCode AudioCapturer::setupAudioResampling() {
- AVSampleFormat srcFormat = codecCtx_->sample_fmt;
- int srcSampleRate = codecCtx_->sample_rate;
- int srcChannels = codecCtx_->ch_layout.nb_channels;
- AVChannelLayout srcChannelLayout = codecCtx_->ch_layout;
-
- AVSampleFormat dstFormat = audioParams_.sampleFormat;
- int dstSampleRate = audioParams_.sampleRate;
- int dstChannels = audioParams_.channels;
- AVChannelLayout dstChannelLayout;
- av_channel_layout_default(&dstChannelLayout, dstChannels);
-
- needResampling_ = (srcFormat != dstFormat) ||
- (srcSampleRate != dstSampleRate) ||
- (srcChannels != dstChannels);
-
- if (needResampling_) {
- AV_LOGGER_INFOF("需要音频重采样: {}Hz,{}ch,{} -> {}Hz,{}ch,{}",
- srcSampleRate, srcChannels, av_get_sample_fmt_name(srcFormat),
- dstSampleRate, dstChannels, av_get_sample_fmt_name(dstFormat));
-
- swrCtx_ = swr_alloc();
- if (!swrCtx_) {
- AV_LOGGER_ERROR("分配音频重采样器失败");
- return ErrorCode::MEMORY_ALLOC_FAILED;
- }
-
- // 设置重采样参数
- av_opt_set_chlayout(swrCtx_, "in_chlayout", &srcChannelLayout, 0);
- av_opt_set_int(swrCtx_, "in_sample_rate", srcSampleRate, 0);
- av_opt_set_sample_fmt(swrCtx_, "in_sample_fmt", srcFormat, 0);
-
- av_opt_set_chlayout(swrCtx_, "out_chlayout", &dstChannelLayout, 0);
- av_opt_set_int(swrCtx_, "out_sample_rate", dstSampleRate, 0);
- av_opt_set_sample_fmt(swrCtx_, "out_sample_fmt", dstFormat, 0);
-
- // 初始化重采样器
- int ret = swr_init(swrCtx_);
- if (ret < 0) {
- AV_LOGGER_ERRORF("初始化音频重采样器失败: {}", ffmpeg_utils::errorToString(ret));
- cleanupResampler();
- return static_cast<ErrorCode>(ret);
- }
-
- // 创建重采样输出帧
- resampledFrame_ = makeAVFrame();
- if (!resampledFrame_) {
- return ErrorCode::MEMORY_ALLOC_FAILED;
- }
-
- resampledFrame_->format = dstFormat;
- resampledFrame_->sample_rate = dstSampleRate;
- av_channel_layout_copy(&resampledFrame_->ch_layout, &dstChannelLayout);
- }
-
- return ErrorCode::SUCCESS;
- }
- void AudioCapturer::captureThreadFunc() {
- AV_LOGGER_INFO("音频采集线程已启动");
-
- while (!shouldStop_) {
- // 检查暂停状态
- {
- std::unique_lock<std::mutex> lock(pauseMutex_);
- pauseCondition_.wait(lock, [this] { return !paused_ || shouldStop_; });
- }
-
- if (shouldStop_) {
- break;
- }
-
- ErrorCode result = captureFrame();
- if (result != ErrorCode::SUCCESS) {
- onError(result, "采集音频帧失败");
-
- // 短暂休眠后重试
- std::this_thread::sleep_for(std::chrono::milliseconds(5));
- }
- }
-
- AV_LOGGER_INFO("音频采集线程已退出");
- }
- ErrorCode AudioCapturer::captureFrame() {
- AVPacket* packet = av_packet_alloc();
- if (!packet) {
- return ErrorCode::MEMORY_ALLOC_FAILED;
- }
-
- // 读取包
- int ret = av_read_frame(formatCtx_, packet);
- if (ret < 0) {
- av_packet_free(&packet);
- if (ret == AVERROR_EOF) {
- AV_LOGGER_WARNING("音频流结束");
- return ErrorCode::END_OF_STREAM;
- } else {
- AV_LOGGER_ERRORF("读取音频帧失败: {}", ffmpeg_utils::errorToString(ret));
- return static_cast<ErrorCode>(ret);
- }
- }
-
- // 检查是否是音频包
- if (packet->stream_index != audioStreamIndex_) {
- av_packet_free(&packet);
- return ErrorCode::SUCCESS;
- }
-
- // 发送包到解码器
- ret = avcodec_send_packet(codecCtx_, packet);
- av_packet_free(&packet);
-
- if (ret < 0) {
- AV_LOGGER_ERRORF("发送音频包到解码器失败: {}", ffmpeg_utils::errorToString(ret));
- return static_cast<ErrorCode>(ret);
- }
-
- // 接收解码后的帧
- AVFramePtr frame = makeAVFrame();
- if (!frame) {
- return ErrorCode::MEMORY_ALLOC_FAILED;
- }
-
- ret = avcodec_receive_frame(codecCtx_, frame.get());
- if (ret == AVERROR(EAGAIN)) {
- return ErrorCode::SUCCESS; // 需要更多输入
- } else if (ret < 0) {
- AV_LOGGER_ERRORF("接收音频解码帧失败: {}", ffmpeg_utils::errorToString(ret));
- return static_cast<ErrorCode>(ret);
- }
-
- // 音频处理
- AVFramePtr processedFrame = processAudioFrame(frame);
- if (!processedFrame) {
- return ErrorCode::PROCESSING_ERROR;
- }
-
- // 计算音频电平
- calculateAudioLevel(processedFrame);
-
- // 回调
- onFrameCaptured(processedFrame);
-
- return ErrorCode::SUCCESS;
- }
- AVFramePtr AudioCapturer::processAudioFrame(const AVFramePtr& frame) {
- if (!frame) {
- return nullptr;
- }
-
- AVFramePtr processedFrame = std::move(const_cast<AVFramePtr&>(frame));
-
- // 重采样
- if (needResampling_) {
- processedFrame = resampleAudioFrame(processedFrame);
- if (!processedFrame) {
- return nullptr;
- }
- }
-
- // 音量控制
- if (currentVolume_ != 1.0f) {
- processedFrame = applyVolumeControl(processedFrame);
- }
-
- // 降噪处理
- if (noiseReductionEnabled_) {
- processedFrame = applyNoiseReduction(processedFrame);
- }
-
- return processedFrame;
- }
- AVFramePtr AudioCapturer::resampleAudioFrame(const AVFramePtr& frame) {
- if (!frame || !swrCtx_ || !resampledFrame_) {
- return nullptr;
- }
-
- // 计算输出采样数
- int outSamples = swr_get_out_samples(swrCtx_, frame->nb_samples);
- resampledFrame_->nb_samples = outSamples;
-
- // 重新分配缓冲区(如果需要)
- if (av_frame_get_buffer(resampledFrame_.get(), 0) < 0) {
- AV_LOGGER_ERROR("分配重采样缓冲区失败");
- return nullptr;
- }
-
- // 执行重采样
- int convertedSamples = swr_convert(swrCtx_,
- resampledFrame_->data, outSamples,
- (const uint8_t**)frame->data, frame->nb_samples);
-
- if (convertedSamples < 0) {
- AV_LOGGER_ERRORF("音频重采样失败: {}", ffmpeg_utils::errorToString(convertedSamples));
- return nullptr;
- }
-
- resampledFrame_->nb_samples = convertedSamples;
-
- // 复制时间戳等信息
- av_frame_copy_props(resampledFrame_.get(), frame.get());
-
- // 创建新的frame并复制数据
- AVFramePtr outputFrame = makeAVFrame();
- if (!outputFrame) {
- return nullptr;
- }
-
- av_frame_ref(outputFrame.get(), resampledFrame_.get());
- return outputFrame;
- }
- AVFramePtr AudioCapturer::applyVolumeControl(const AVFramePtr& frame) {
- if (!frame || currentVolume_ == 1.0f) {
- return nullptr;
- }
-
- // 简单的音量控制实现
- AVSampleFormat format = static_cast<AVSampleFormat>(frame->format);
- int channels = frame->ch_layout.nb_channels;
- int samples = frame->nb_samples;
-
- if (format == AV_SAMPLE_FMT_S16) {
- int16_t* data = reinterpret_cast<int16_t*>(frame->data[0]);
- for (int i = 0; i < samples * channels; ++i) {
- data[i] = static_cast<int16_t>(data[i] * currentVolume_);
- }
- } else if (format == AV_SAMPLE_FMT_FLT) {
- float* data = reinterpret_cast<float*>(frame->data[0]);
- for (int i = 0; i < samples * channels; ++i) {
- data[i] *= currentVolume_;
- }
- }
-
- return nullptr;
- }
- AVFramePtr AudioCapturer::applyNoiseReduction(const AVFramePtr& frame) {
- // 简单的降噪实现(实际应用中需要更复杂的算法)
- if (!frame) {
- return nullptr;
- }
-
- // 这里可以实现噪声门限、频谱减法等降噪算法
- // 目前只是一个占位符实现
-
- return nullptr;
- }
- void AudioCapturer::calculateAudioLevel(const AVFramePtr& frame) {
- if (!frame) {
- return;
- }
-
- auto now = std::chrono::steady_clock::now();
- auto elapsed = std::chrono::duration<double>(now - lastLevelUpdate_).count();
-
- if (elapsed < LEVEL_UPDATE_INTERVAL) {
- return;
- }
-
- std::lock_guard<std::mutex> lock(levelMutex_);
-
- AVSampleFormat format = static_cast<AVSampleFormat>(frame->format);
- int channels = frame->ch_layout.nb_channels;
- int samples = frame->nb_samples;
-
- double sum = 0.0;
- int totalSamples = samples * channels;
-
- if (format == AV_SAMPLE_FMT_S16) {
- const int16_t* data = reinterpret_cast<const int16_t*>(frame->data[0]);
- for (int i = 0; i < totalSamples; ++i) {
- sum += std::abs(data[i]) / 32768.0;
- }
- } else if (format == AV_SAMPLE_FMT_FLT) {
- const float* data = reinterpret_cast<const float*>(frame->data[0]);
- for (int i = 0; i < totalSamples; ++i) {
- sum += std::abs(data[i]);
- }
- }
-
- float level = static_cast<float>(sum / totalSamples);
- audioLevel_.store(std::min<float>(level, 1.0f));
-
- lastLevelUpdate_ = now;
- }
- void AudioCapturer::cleanupResampler() {
- if (swrCtx_) {
- swr_free(&swrCtx_);
- swrCtx_ = nullptr;
- }
-
- resampledFrame_.reset();
- needResampling_ = false;
- }
- void AudioCapturer::cleanupAudioProcessing() {
- // 清理音频处理相关资源
- noiseReductionEnabled_ = false;
- echoCancellationEnabled_ = false;
- currentVolume_ = 1.0f;
- audioLevel_ = 0.0f;
- }
- std::vector<AudioDeviceInfo> AudioCapturer::enumerateMicrophones() const {
- #ifdef _WIN32
- return enumerateDirectSoundDevices();
- #elif defined(__linux__)
- return enumerateALSADevices();
- #elif defined(__APPLE__)
- return enumerateCoreAudioDevices();
- #else
- return {};
- #endif
- }
- std::vector<AudioDeviceInfo> AudioCapturer::enumerateSystemAudioDevices() const {
- #ifdef _WIN32
- return enumerateWASAPIDevices();
- #elif defined(__linux__)
- return enumeratePulseAudioDevices();
- #elif defined(__APPLE__)
- return enumerateCoreAudioDevices();
- #else
- return {};
- #endif
- }
- const AVInputFormat* AudioCapturer::getPlatformInputFormat() const {
- #ifdef _WIN32
- if (audioParams_.type == CapturerType::AUDIO_MIC) {
- return av_find_input_format("dshow");
- } else {
- return av_find_input_format("dshow"); // WASAPI通过dshow访问
- }
- #elif defined(__linux__)
- if (audioParams_.type == CapturerType::AUDIO_MIC) {
- return av_find_input_format("alsa");
- } else {
- return av_find_input_format("pulse");
- }
- #elif defined(__APPLE__)
- return av_find_input_format("avfoundation");
- #endif
-
- return nullptr;
- }
- std::string AudioCapturer::getPlatformDeviceName() const {
- #ifdef _WIN32
- if (audioParams_.type == CapturerType::AUDIO_MIC) {
- if (!audioParams_.deviceName.empty()) {
- return "audio=" + audioParams_.deviceName;
- } else {
- return "audio=" + std::to_string(audioParams_.micIndex);
- }
- } else {
- return "audio=" + (audioParams_.audioDevice.empty() ? "@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\\wave_{00000000-0000-0000-0000-000000000000}" : audioParams_.audioDevice);
- }
- #elif defined(__linux__)
- if (audioParams_.type == CapturerType::AUDIO_MIC) {
- if (!audioParams_.deviceName.empty()) {
- return audioParams_.deviceName;
- } else {
- return "hw:" + std::to_string(audioParams_.micIndex);
- }
- } else {
- return audioParams_.audioDevice.empty() ? "default" : audioParams_.audioDevice;
- }
- #elif defined(__APPLE__)
- if (audioParams_.type == CapturerType::AUDIO_MIC) {
- return ":" + std::to_string(audioParams_.micIndex);
- } else {
- return ":none";
- }
- #endif
-
- return "";
- }
- #ifdef _WIN32
- std::vector<AudioDeviceInfo> AudioCapturer::enumerateDirectSoundDevices() const {
- std::vector<AudioDeviceInfo> devices;
-
- // 简化的DirectSound设备枚举
- AudioDeviceInfo device;
- device.id = "0";
- device.name = "默认麦克风";
- device.description = "DirectSound麦克风设备";
- device.isDefault = true;
- device.isInput = true;
-
- // 添加常见采样率
- device.supportedSampleRates = {8000, 16000, 22050, 44100, 48000};
-
- // 添加常见声道数
- device.supportedChannels = {1, 2};
-
- // 添加支持的采样格式
- device.supportedFormats = {
- AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT
- };
-
- devices.push_back(device);
-
- return devices;
- }
- std::vector<AudioDeviceInfo> AudioCapturer::enumerateWASAPIDevices() const {
- std::vector<AudioDeviceInfo> devices;
-
- // 简化的WASAPI设备枚举
- AudioDeviceInfo device;
- device.id = "wasapi_default";
- device.name = "默认系统音频";
- device.description = "WASAPI系统音频设备";
- device.isDefault = true;
- device.isInput = false;
-
- // 添加常见采样率
- device.supportedSampleRates = {44100, 48000, 96000};
-
- // 添加常见声道数
- device.supportedChannels = {2, 6, 8};
-
- // 添加支持的采样格式
- device.supportedFormats = {
- AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT
- };
-
- devices.push_back(device);
-
- return devices;
- }
- ErrorCode AudioCapturer::setupDirectSoundMicrophone() {
- AV_LOGGER_INFO("设置DirectSound麦克风");
- return openInputDevice();
- }
- ErrorCode AudioCapturer::setupWASAPISystemAudio() {
- AV_LOGGER_INFO("设置WASAPI系统音频");
- return openInputDevice();
- }
- #endif
- // AudioCaptureFactory 实现
- std::unique_ptr<AudioCapturer> AudioCapturer::AudioCaptureFactory::createMicrophone(int micIndex) {
- auto capturer = std::make_unique<AudioCapturer>();
-
- AudioCaptureParams params(CapturerType::AUDIO_MIC);
- params.micIndex = micIndex;
-
- ErrorCode result = capturer->initialize(params);
- if (result != ErrorCode::SUCCESS) {
- AV_LOGGER_ERRORF("创建麦克风采集器失败: {}", static_cast<int>(result));
- return nullptr;
- }
-
- return capturer;
- }
- std::unique_ptr<AudioCapturer> AudioCapturer::AudioCaptureFactory::createSystemAudio(bool loopback) {
- auto capturer = std::make_unique<AudioCapturer>();
-
- AudioCaptureParams params(loopback ? CapturerType::AUDIO_LOOPBACK : CapturerType::AUDIO_SYSTEM);
- params.captureLoopback = loopback;
-
- ErrorCode result = capturer->initialize(params);
- if (result != ErrorCode::SUCCESS) {
- AV_LOGGER_ERRORF("创建系统音频采集器失败: {}", static_cast<int>(result));
- return nullptr;
- }
-
- return capturer;
- }
- std::unique_ptr<AudioCapturer> AudioCapturer::AudioCaptureFactory::createBestMicrophone() {
- return createMicrophone(0); // 默认使用第一个麦克风
- }
- } // namespace capture
- } // namespace av
|