capture_audio_capturer.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. #include "capture_audio_capturer.h"
  2. #include "../base/logger.h"
  3. #include "../base/media_common.h"
  4. #include <algorithm>
  5. #include <cmath>
  6. #ifdef _WIN32
  7. #include <windows.h>
  8. #include <mmdeviceapi.h>
  9. #include <audioclient.h>
  10. #include <endpointvolume.h>
  11. #pragma comment(lib, "ole32.lib")
  12. #endif
  13. extern "C" {
  14. #include <libavformat/avformat.h>
  15. #include <libavdevice/avdevice.h>
  16. #include <libswresample/swresample.h>
  17. #include <libavutil/opt.h>
  18. #include <libavutil/channel_layout.h>
  19. }
  20. namespace av {
  21. namespace capture {
  22. AudioCapturer::AudioCapturer() : audioParams_(CapturerType::AUDIO_MIC) {
  23. AV_LOGGER_DEBUG("创建音频采集器");
  24. // 注册设备
  25. avdevice_register_all();
  26. lastLevelUpdate_ = std::chrono::steady_clock::now();
  27. }
  28. AudioCapturer::~AudioCapturer() {
  29. close();
  30. AV_LOGGER_DEBUG("音频采集器已销毁");
  31. }
  32. ErrorCode AudioCapturer::initialize(const CapturerParams& params) {
  33. if (params.mediaType != MediaType::AUDIO) {
  34. AV_LOGGER_ERROR("参数媒体类型不是音频");
  35. return ErrorCode::INVALID_PARAMS;
  36. }
  37. audioParams_ = static_cast<const AudioCaptureParams&>(params);
  38. if (!validateParams(audioParams_)) {
  39. return ErrorCode::INVALID_PARAMS;
  40. }
  41. ErrorCode result = ErrorCode::SUCCESS;
  42. if (audioParams_.type == CapturerType::AUDIO_MIC) {
  43. result = initializeMicrophone();
  44. } else if (audioParams_.type == CapturerType::AUDIO_SYSTEM ||
  45. audioParams_.type == CapturerType::AUDIO_LOOPBACK) {
  46. result = initializeSystemAudio();
  47. } else {
  48. AV_LOGGER_ERROR("不支持的音频采集器类型");
  49. return ErrorCode::NOT_SUPPORTED;
  50. }
  51. if (result == ErrorCode::SUCCESS) {
  52. setState(CapturerState::INITIALIZED);
  53. AV_LOGGER_INFOF("音频采集器初始化成功: {}Hz, {}ch, {}",
  54. audioParams_.sampleRate, audioParams_.channels,
  55. av_get_sample_fmt_name(audioParams_.sampleFormat));
  56. }
  57. return result;
  58. }
  59. ErrorCode AudioCapturer::start() {
  60. std::lock_guard<std::mutex> lock(captureMutex_);
  61. if (getState() != CapturerState::INITIALIZED) {
  62. AV_LOGGER_ERROR("采集器状态无效,无法启动");
  63. return ErrorCode::INVALID_STATE;
  64. }
  65. shouldStop_ = false;
  66. // 启动采集线程
  67. try {
  68. captureThread_ = std::thread(&AudioCapturer::captureThreadFunc, this);
  69. setState(CapturerState::STARTED);
  70. AV_LOGGER_INFO("音频采集已启动");
  71. return ErrorCode::SUCCESS;
  72. } catch (const std::exception& e) {
  73. AV_LOGGER_ERRORF("启动音频采集线程失败: {}", e.what());
  74. return ErrorCode::THREAD_ERROR;
  75. }
  76. }
  77. ErrorCode AudioCapturer::stop() {
  78. std::lock_guard<std::mutex> lock(captureMutex_);
  79. if (getState() != CapturerState::STARTED) {
  80. return ErrorCode::SUCCESS;
  81. }
  82. shouldStop_ = true;
  83. // 唤醒暂停的线程
  84. {
  85. std::lock_guard<std::mutex> pauseLock(pauseMutex_);
  86. paused_ = false;
  87. pauseCondition_.notify_all();
  88. }
  89. // 等待线程结束
  90. if (captureThread_.joinable()) {
  91. captureThread_.join();
  92. }
  93. setState(CapturerState::STOPPED);
  94. AV_LOGGER_INFO("音频采集已停止");
  95. return ErrorCode::SUCCESS;
  96. }
  97. ErrorCode AudioCapturer::pause() {
  98. if (getState() != CapturerState::STARTED) {
  99. return ErrorCode::INVALID_STATE;
  100. }
  101. paused_ = true;
  102. AV_LOGGER_INFO("音频采集已暂停");
  103. return ErrorCode::SUCCESS;
  104. }
  105. ErrorCode AudioCapturer::resume() {
  106. if (getState() != CapturerState::STARTED) {
  107. return ErrorCode::INVALID_STATE;
  108. }
  109. {
  110. std::lock_guard<std::mutex> lock(pauseMutex_);
  111. paused_ = false;
  112. pauseCondition_.notify_all();
  113. }
  114. AV_LOGGER_INFO("音频采集已恢复");
  115. return ErrorCode::SUCCESS;
  116. }
  117. ErrorCode AudioCapturer::reset() {
  118. ErrorCode result = stop();
  119. if (result != ErrorCode::SUCCESS) {
  120. return result;
  121. }
  122. // 清空帧队列
  123. {
  124. std::lock_guard<std::mutex> lock(queueMutex_);
  125. while (!frameQueue_.empty()) {
  126. frameQueue_.pop();
  127. }
  128. }
  129. resetStats();
  130. audioLevel_ = 0.0f;
  131. setState(CapturerState::INITIALIZED);
  132. AV_LOGGER_INFO("音频采集器已重置");
  133. return ErrorCode::SUCCESS;
  134. }
  135. ErrorCode AudioCapturer::close() {
  136. stop();
  137. // 清理资源
  138. cleanupResampler();
  139. cleanupAudioProcessing();
  140. if (codecCtx_) {
  141. avcodec_free_context(&codecCtx_);
  142. codecCtx_ = nullptr;
  143. }
  144. if (formatCtx_) {
  145. avformat_close_input(&formatCtx_);
  146. formatCtx_ = nullptr;
  147. }
  148. codec_ = nullptr;
  149. audioStreamIndex_ = -1;
  150. setState(CapturerState::IDLE);
  151. AV_LOGGER_INFO("音频采集器已关闭");
  152. return ErrorCode::SUCCESS;
  153. }
  154. std::vector<std::string> AudioCapturer::getAvailableDevices() const {
  155. std::vector<std::string> devices;
  156. auto deviceInfos = getDetailedDeviceInfo();
  157. for (const auto& info : deviceInfos) {
  158. devices.push_back(info.name);
  159. }
  160. return devices;
  161. }
  162. std::string AudioCapturer::getCurrentDevice() const {
  163. return audioParams_.deviceName;
  164. }
  165. std::vector<AudioDeviceInfo> AudioCapturer::getDetailedDeviceInfo() const {
  166. std::lock_guard<std::mutex> lock(deviceCacheMutex_);
  167. if (!devicesCached_) {
  168. if (audioParams_.type == CapturerType::AUDIO_MIC) {
  169. cachedDevices_ = enumerateMicrophones();
  170. } else {
  171. cachedDevices_ = enumerateSystemAudioDevices();
  172. }
  173. devicesCached_ = true;
  174. }
  175. return cachedDevices_;
  176. }
  177. ErrorCode AudioCapturer::setAudioParams(int sampleRate, int channels, AVSampleFormat sampleFormat) {
  178. if (getState() == CapturerState::STARTED) {
  179. AV_LOGGER_ERROR("无法在采集过程中修改音频参数");
  180. return ErrorCode::INVALID_STATE;
  181. }
  182. audioParams_.sampleRate = sampleRate;
  183. audioParams_.channels = channels;
  184. audioParams_.sampleFormat = sampleFormat;
  185. AV_LOGGER_INFOF("音频参数已更新: {}Hz, {}ch, {}",
  186. sampleRate, channels, av_get_sample_fmt_name(sampleFormat));
  187. return ErrorCode::SUCCESS;
  188. }
  189. ErrorCode AudioCapturer::setVolume(float volume) {
  190. if (volume < 0.0f || volume > 2.0f) {
  191. AV_LOGGER_ERROR("音量值超出范围 (0.0-2.0)");
  192. return ErrorCode::INVALID_PARAMS;
  193. }
  194. currentVolume_ = volume;
  195. audioParams_.volume = volume;
  196. AV_LOGGER_INFOF("音量已设置为: {:.2f}", volume);
  197. return ErrorCode::SUCCESS;
  198. }
  199. float AudioCapturer::getVolume() const {
  200. return currentVolume_;
  201. }
  202. ErrorCode AudioCapturer::setNoiseReduction(bool enable) {
  203. noiseReductionEnabled_ = enable;
  204. audioParams_.enableNoiseReduction = enable;
  205. AV_LOGGER_INFOF("Noise reduction {}", enable ? "enabled" : "disabled");
  206. return ErrorCode::SUCCESS;
  207. }
  208. ErrorCode AudioCapturer::setEchoCancellation(bool enable) {
  209. echoCancellationEnabled_ = enable;
  210. audioParams_.enableEchoCancellation = enable;
  211. AV_LOGGER_INFOF("Echo cancellation {}", enable ? "enabled" : "disabled");
  212. return ErrorCode::SUCCESS;
  213. }
  214. AudioCaptureParams AudioCapturer::getCurrentParams() const {
  215. return audioParams_;
  216. }
  217. float AudioCapturer::getAudioLevel() const {
  218. return audioLevel_.load();
  219. }
  220. bool AudioCapturer::validateParams(const CapturerParams& params) {
  221. const auto& audioParams = static_cast<const AudioCaptureParams&>(params);
  222. if (audioParams.sampleRate <= 0 || audioParams.sampleRate > 192000) {
  223. AV_LOGGER_ERROR("采样率无效");
  224. return false;
  225. }
  226. if (audioParams.channels <= 0 || audioParams.channels > 8) {
  227. AV_LOGGER_ERROR("声道数无效");
  228. return false;
  229. }
  230. if (audioParams.bufferSize <= 0 || audioParams.bufferSize > 8192) {
  231. AV_LOGGER_ERROR("缓冲区大小无效");
  232. return false;
  233. }
  234. if (audioParams.volume < 0.0f || audioParams.volume > 2.0f) {
  235. AV_LOGGER_ERROR("音量值无效");
  236. return false;
  237. }
  238. return true;
  239. }
  240. ErrorCode AudioCapturer::initializeMicrophone() {
  241. AV_LOGGER_INFOF("初始化麦克风采集器: 索引={}", audioParams_.micIndex);
  242. #ifdef _WIN32
  243. return setupDirectSoundMicrophone();
  244. #elif defined(__linux__)
  245. return setupALSAMicrophone();
  246. #elif defined(__APPLE__)
  247. return setupCoreAudioMicrophone();
  248. #else
  249. AV_LOGGER_ERROR("当前平台不支持麦克风采集");
  250. return ErrorCode::NOT_SUPPORTED;
  251. #endif
  252. }
  253. ErrorCode AudioCapturer::initializeSystemAudio() {
  254. AV_LOGGER_INFO("初始化系统音频采集器");
  255. #ifdef _WIN32
  256. return setupWASAPISystemAudio();
  257. #elif defined(__linux__)
  258. return setupPulseAudioCapture();
  259. #elif defined(__APPLE__)
  260. return setupCoreAudioSystemCapture();
  261. #else
  262. AV_LOGGER_ERROR("当前平台不支持系统音频采集");
  263. return ErrorCode::NOT_SUPPORTED;
  264. #endif
  265. }
  266. ErrorCode AudioCapturer::openInputDevice() {
  267. const AVInputFormat* inputFormat = getPlatformInputFormat();
  268. if (!inputFormat) {
  269. AV_LOGGER_ERROR("获取音频输入格式失败");
  270. return ErrorCode::NOT_SUPPORTED;
  271. }
  272. std::string deviceName = getPlatformDeviceName();
  273. if (deviceName.empty()) {
  274. AV_LOGGER_ERROR("获取音频设备名称失败");
  275. return ErrorCode::DEVICE_NOT_FOUND;
  276. }
  277. AV_LOGGER_INFOF("打开音频输入设备: {} (格式: {})", deviceName, inputFormat->name);
  278. // 设置输入选项
  279. AVDictionary* options = nullptr;
  280. // 设置音频参数
  281. av_dict_set(&options, "sample_rate", std::to_string(audioParams_.sampleRate).c_str(), 0);
  282. av_dict_set(&options, "channels", std::to_string(audioParams_.channels).c_str(), 0);
  283. // 设置缓冲区大小
  284. av_dict_set(&options, "audio_buffer_size", std::to_string(audioParams_.bufferSize).c_str(), 0);
  285. // 尝试列出可用设备
  286. av_dict_set(&options, "list_devices", "true", 0);
  287. // 打开输入
  288. int ret = avformat_open_input(&formatCtx_, deviceName.c_str(), inputFormat, &options);
  289. av_dict_free(&options);
  290. if (ret < 0) {
  291. AV_LOGGER_ERRORF("打开音频输入设备失败: {} (设备: {})",
  292. ffmpeg_utils::errorToString(ret), deviceName);
  293. // 如果是设备不存在错误,尝试使用默认设备
  294. if (ret == AVERROR(EIO) || ret == AVERROR(ENOENT)) {
  295. AV_LOGGER_WARNING("尝试使用默认音频设备");
  296. AVDictionary* defaultOptions = nullptr;
  297. av_dict_set(&defaultOptions, "sample_rate", std::to_string(audioParams_.sampleRate).c_str(), 0);
  298. av_dict_set(&defaultOptions, "channels", std::to_string(audioParams_.channels).c_str(), 0);
  299. ret = avformat_open_input(&formatCtx_, "audio=default", inputFormat, &defaultOptions);
  300. av_dict_free(&defaultOptions);
  301. if (ret < 0) {
  302. AV_LOGGER_ERRORF("打开默认音频设备也失败: {}", ffmpeg_utils::errorToString(ret));
  303. return static_cast<ErrorCode>(ret);
  304. }
  305. AV_LOGGER_INFO("成功打开默认音频设备");
  306. } else {
  307. return static_cast<ErrorCode>(ret);
  308. }
  309. }
  310. // 查找流信息
  311. ret = avformat_find_stream_info(formatCtx_, nullptr);
  312. if (ret < 0) {
  313. AV_LOGGER_ERRORF("查找音频流信息失败: {}", ffmpeg_utils::errorToString(ret));
  314. return static_cast<ErrorCode>(ret);
  315. }
  316. // 查找音频流
  317. audioStreamIndex_ = av_find_best_stream(formatCtx_, AVMEDIA_TYPE_AUDIO, -1, -1, &codec_, 0);
  318. if (audioStreamIndex_ < 0) {
  319. AV_LOGGER_ERROR("未找到音频流");
  320. return ErrorCode::STREAM_NOT_FOUND;
  321. }
  322. // 创建解码上下文
  323. codecCtx_ = avcodec_alloc_context3(codec_);
  324. if (!codecCtx_) {
  325. AV_LOGGER_ERROR("分配音频解码上下文失败");
  326. return ErrorCode::MEMORY_ALLOC_FAILED;
  327. }
  328. // 复制流参数到解码上下文
  329. ret = avcodec_parameters_to_context(codecCtx_, formatCtx_->streams[audioStreamIndex_]->codecpar);
  330. if (ret < 0) {
  331. AV_LOGGER_ERRORF("复制音频流参数失败: {}", ffmpeg_utils::errorToString(ret));
  332. return static_cast<ErrorCode>(ret);
  333. }
  334. // 打开解码器
  335. ret = avcodec_open2(codecCtx_, codec_, nullptr);
  336. if (ret < 0) {
  337. AV_LOGGER_ERRORF("打开音频解码器失败: {}", ffmpeg_utils::errorToString(ret));
  338. return static_cast<ErrorCode>(ret);
  339. }
  340. // 设置音频重采样
  341. return setupAudioResampling();
  342. }
  343. ErrorCode AudioCapturer::setupAudioResampling() {
  344. AVSampleFormat srcFormat = codecCtx_->sample_fmt;
  345. int srcSampleRate = codecCtx_->sample_rate;
  346. int srcChannels = codecCtx_->ch_layout.nb_channels;
  347. AVChannelLayout srcChannelLayout = codecCtx_->ch_layout;
  348. AVSampleFormat dstFormat = audioParams_.sampleFormat;
  349. int dstSampleRate = audioParams_.sampleRate;
  350. int dstChannels = audioParams_.channels;
  351. AVChannelLayout dstChannelLayout;
  352. av_channel_layout_default(&dstChannelLayout, dstChannels);
  353. needResampling_ = (srcFormat != dstFormat) ||
  354. (srcSampleRate != dstSampleRate) ||
  355. (srcChannels != dstChannels);
  356. if (needResampling_) {
  357. AV_LOGGER_INFOF("需要音频重采样: {}Hz,{}ch,{} -> {}Hz,{}ch,{}",
  358. srcSampleRate, srcChannels, av_get_sample_fmt_name(srcFormat),
  359. dstSampleRate, dstChannels, av_get_sample_fmt_name(dstFormat));
  360. swrCtx_ = swr_alloc();
  361. if (!swrCtx_) {
  362. AV_LOGGER_ERROR("分配音频重采样器失败");
  363. return ErrorCode::MEMORY_ALLOC_FAILED;
  364. }
  365. // 设置重采样参数
  366. av_opt_set_chlayout(swrCtx_, "in_chlayout", &srcChannelLayout, 0);
  367. av_opt_set_int(swrCtx_, "in_sample_rate", srcSampleRate, 0);
  368. av_opt_set_sample_fmt(swrCtx_, "in_sample_fmt", srcFormat, 0);
  369. av_opt_set_chlayout(swrCtx_, "out_chlayout", &dstChannelLayout, 0);
  370. av_opt_set_int(swrCtx_, "out_sample_rate", dstSampleRate, 0);
  371. av_opt_set_sample_fmt(swrCtx_, "out_sample_fmt", dstFormat, 0);
  372. // 初始化重采样器
  373. int ret = swr_init(swrCtx_);
  374. if (ret < 0) {
  375. AV_LOGGER_ERRORF("初始化音频重采样器失败: {}", ffmpeg_utils::errorToString(ret));
  376. cleanupResampler();
  377. return static_cast<ErrorCode>(ret);
  378. }
  379. // 创建重采样输出帧
  380. resampledFrame_ = makeAVFrame();
  381. if (!resampledFrame_) {
  382. return ErrorCode::MEMORY_ALLOC_FAILED;
  383. }
  384. resampledFrame_->format = dstFormat;
  385. resampledFrame_->sample_rate = dstSampleRate;
  386. av_channel_layout_copy(&resampledFrame_->ch_layout, &dstChannelLayout);
  387. }
  388. return ErrorCode::SUCCESS;
  389. }
  390. void AudioCapturer::captureThreadFunc() {
  391. AV_LOGGER_INFO("音频采集线程已启动");
  392. while (!shouldStop_) {
  393. // 检查暂停状态
  394. {
  395. std::unique_lock<std::mutex> lock(pauseMutex_);
  396. pauseCondition_.wait(lock, [this] { return !paused_ || shouldStop_; });
  397. }
  398. if (shouldStop_) {
  399. break;
  400. }
  401. ErrorCode result = captureFrame();
  402. if (result != ErrorCode::SUCCESS) {
  403. onError(result, "采集音频帧失败");
  404. // 短暂休眠后重试
  405. std::this_thread::sleep_for(std::chrono::milliseconds(5));
  406. }
  407. }
  408. AV_LOGGER_INFO("音频采集线程已退出");
  409. }
  410. ErrorCode AudioCapturer::captureFrame() {
  411. AVPacket* packet = av_packet_alloc();
  412. if (!packet) {
  413. return ErrorCode::MEMORY_ALLOC_FAILED;
  414. }
  415. // 读取包
  416. int ret = av_read_frame(formatCtx_, packet);
  417. if (ret < 0) {
  418. av_packet_free(&packet);
  419. if (ret == AVERROR_EOF) {
  420. AV_LOGGER_WARNING("音频流结束");
  421. return ErrorCode::END_OF_STREAM;
  422. } else {
  423. AV_LOGGER_ERRORF("读取音频帧失败: {}", ffmpeg_utils::errorToString(ret));
  424. return static_cast<ErrorCode>(ret);
  425. }
  426. }
  427. // 检查是否是音频包
  428. if (packet->stream_index != audioStreamIndex_) {
  429. av_packet_free(&packet);
  430. return ErrorCode::SUCCESS;
  431. }
  432. // 发送包到解码器
  433. ret = avcodec_send_packet(codecCtx_, packet);
  434. av_packet_free(&packet);
  435. if (ret < 0) {
  436. AV_LOGGER_ERRORF("发送音频包到解码器失败: {}", ffmpeg_utils::errorToString(ret));
  437. return static_cast<ErrorCode>(ret);
  438. }
  439. // 接收解码后的帧
  440. AVFramePtr frame = makeAVFrame();
  441. if (!frame) {
  442. return ErrorCode::MEMORY_ALLOC_FAILED;
  443. }
  444. ret = avcodec_receive_frame(codecCtx_, frame.get());
  445. if (ret == AVERROR(EAGAIN)) {
  446. return ErrorCode::SUCCESS; // 需要更多输入
  447. } else if (ret < 0) {
  448. AV_LOGGER_ERRORF("接收音频解码帧失败: {}", ffmpeg_utils::errorToString(ret));
  449. return static_cast<ErrorCode>(ret);
  450. }
  451. // 音频处理
  452. AVFramePtr processedFrame = processAudioFrame(frame);
  453. if (!processedFrame) {
  454. return ErrorCode::PROCESSING_ERROR;
  455. }
  456. // 计算音频电平
  457. calculateAudioLevel(processedFrame);
  458. // 回调
  459. onFrameCaptured(processedFrame);
  460. return ErrorCode::SUCCESS;
  461. }
  462. AVFramePtr AudioCapturer::processAudioFrame(const AVFramePtr& frame) {
  463. if (!frame) {
  464. return nullptr;
  465. }
  466. AVFramePtr processedFrame = std::move(const_cast<AVFramePtr&>(frame));
  467. // 重采样
  468. if (needResampling_) {
  469. processedFrame = resampleAudioFrame(processedFrame);
  470. if (!processedFrame) {
  471. return nullptr;
  472. }
  473. }
  474. // 音量控制
  475. if (currentVolume_ != 1.0f) {
  476. processedFrame = applyVolumeControl(processedFrame);
  477. }
  478. // 降噪处理
  479. if (noiseReductionEnabled_) {
  480. processedFrame = applyNoiseReduction(processedFrame);
  481. }
  482. return processedFrame;
  483. }
  484. AVFramePtr AudioCapturer::resampleAudioFrame(const AVFramePtr& frame) {
  485. if (!frame || !swrCtx_ || !resampledFrame_) {
  486. return nullptr;
  487. }
  488. // 计算输出采样数
  489. int outSamples = swr_get_out_samples(swrCtx_, frame->nb_samples);
  490. resampledFrame_->nb_samples = outSamples;
  491. // 重新分配缓冲区(如果需要)
  492. if (av_frame_get_buffer(resampledFrame_.get(), 0) < 0) {
  493. AV_LOGGER_ERROR("分配重采样缓冲区失败");
  494. return nullptr;
  495. }
  496. // 执行重采样
  497. int convertedSamples = swr_convert(swrCtx_,
  498. resampledFrame_->data, outSamples,
  499. (const uint8_t**)frame->data, frame->nb_samples);
  500. if (convertedSamples < 0) {
  501. AV_LOGGER_ERRORF("音频重采样失败: {}", ffmpeg_utils::errorToString(convertedSamples));
  502. return nullptr;
  503. }
  504. resampledFrame_->nb_samples = convertedSamples;
  505. // 复制时间戳等信息
  506. av_frame_copy_props(resampledFrame_.get(), frame.get());
  507. // 创建新的frame并复制数据
  508. AVFramePtr outputFrame = makeAVFrame();
  509. if (!outputFrame) {
  510. return nullptr;
  511. }
  512. av_frame_ref(outputFrame.get(), resampledFrame_.get());
  513. return outputFrame;
  514. }
  515. AVFramePtr AudioCapturer::applyVolumeControl(const AVFramePtr& frame) {
  516. if (!frame || currentVolume_ == 1.0f) {
  517. return nullptr;
  518. }
  519. // 简单的音量控制实现
  520. AVSampleFormat format = static_cast<AVSampleFormat>(frame->format);
  521. int channels = frame->ch_layout.nb_channels;
  522. int samples = frame->nb_samples;
  523. if (format == AV_SAMPLE_FMT_S16) {
  524. int16_t* data = reinterpret_cast<int16_t*>(frame->data[0]);
  525. for (int i = 0; i < samples * channels; ++i) {
  526. data[i] = static_cast<int16_t>(data[i] * currentVolume_);
  527. }
  528. } else if (format == AV_SAMPLE_FMT_FLT) {
  529. float* data = reinterpret_cast<float*>(frame->data[0]);
  530. for (int i = 0; i < samples * channels; ++i) {
  531. data[i] *= currentVolume_;
  532. }
  533. }
  534. return nullptr;
  535. }
  536. AVFramePtr AudioCapturer::applyNoiseReduction(const AVFramePtr& frame) {
  537. // 简单的降噪实现(实际应用中需要更复杂的算法)
  538. if (!frame) {
  539. return nullptr;
  540. }
  541. // 这里可以实现噪声门限、频谱减法等降噪算法
  542. // 目前只是一个占位符实现
  543. return nullptr;
  544. }
  545. void AudioCapturer::calculateAudioLevel(const AVFramePtr& frame) {
  546. if (!frame) {
  547. return;
  548. }
  549. auto now = std::chrono::steady_clock::now();
  550. auto elapsed = std::chrono::duration<double>(now - lastLevelUpdate_).count();
  551. if (elapsed < LEVEL_UPDATE_INTERVAL) {
  552. return;
  553. }
  554. std::lock_guard<std::mutex> lock(levelMutex_);
  555. AVSampleFormat format = static_cast<AVSampleFormat>(frame->format);
  556. int channels = frame->ch_layout.nb_channels;
  557. int samples = frame->nb_samples;
  558. double sum = 0.0;
  559. int totalSamples = samples * channels;
  560. if (format == AV_SAMPLE_FMT_S16) {
  561. const int16_t* data = reinterpret_cast<const int16_t*>(frame->data[0]);
  562. for (int i = 0; i < totalSamples; ++i) {
  563. sum += std::abs(data[i]) / 32768.0;
  564. }
  565. } else if (format == AV_SAMPLE_FMT_FLT) {
  566. const float* data = reinterpret_cast<const float*>(frame->data[0]);
  567. for (int i = 0; i < totalSamples; ++i) {
  568. sum += std::abs(data[i]);
  569. }
  570. }
  571. float level = static_cast<float>(sum / totalSamples);
  572. audioLevel_.store(std::min<float>(level, 1.0f));
  573. lastLevelUpdate_ = now;
  574. }
  575. void AudioCapturer::cleanupResampler() {
  576. if (swrCtx_) {
  577. swr_free(&swrCtx_);
  578. swrCtx_ = nullptr;
  579. }
  580. resampledFrame_.reset();
  581. needResampling_ = false;
  582. }
  583. void AudioCapturer::cleanupAudioProcessing() {
  584. // 清理音频处理相关资源
  585. noiseReductionEnabled_ = false;
  586. echoCancellationEnabled_ = false;
  587. currentVolume_ = 1.0f;
  588. audioLevel_ = 0.0f;
  589. }
  590. std::vector<AudioDeviceInfo> AudioCapturer::enumerateMicrophones() const {
  591. #ifdef _WIN32
  592. return enumerateDirectSoundDevices();
  593. #elif defined(__linux__)
  594. return enumerateALSADevices();
  595. #elif defined(__APPLE__)
  596. return enumerateCoreAudioDevices();
  597. #else
  598. return {};
  599. #endif
  600. }
  601. std::vector<AudioDeviceInfo> AudioCapturer::enumerateSystemAudioDevices() const {
  602. #ifdef _WIN32
  603. return enumerateWASAPIDevices();
  604. #elif defined(__linux__)
  605. return enumeratePulseAudioDevices();
  606. #elif defined(__APPLE__)
  607. return enumerateCoreAudioDevices();
  608. #else
  609. return {};
  610. #endif
  611. }
  612. const AVInputFormat* AudioCapturer::getPlatformInputFormat() const {
  613. #ifdef _WIN32
  614. if (audioParams_.type == CapturerType::AUDIO_MIC) {
  615. return av_find_input_format("dshow");
  616. } else {
  617. return av_find_input_format("dshow"); // WASAPI通过dshow访问
  618. }
  619. #elif defined(__linux__)
  620. if (audioParams_.type == CapturerType::AUDIO_MIC) {
  621. return av_find_input_format("alsa");
  622. } else {
  623. return av_find_input_format("pulse");
  624. }
  625. #elif defined(__APPLE__)
  626. return av_find_input_format("avfoundation");
  627. #endif
  628. return nullptr;
  629. }
  630. std::string AudioCapturer::getPlatformDeviceName() const {
  631. #ifdef _WIN32
  632. if (audioParams_.type == CapturerType::AUDIO_MIC) {
  633. if (!audioParams_.deviceName.empty()) {
  634. return "audio=" + audioParams_.deviceName;
  635. } else {
  636. // 优先尝试默认设备,如果失败再尝试索引设备
  637. if (audioParams_.micIndex == 0) {
  638. return "audio=default";
  639. } else {
  640. return "audio=" + std::to_string(audioParams_.micIndex);
  641. }
  642. }
  643. } else {
  644. return "audio=" + (audioParams_.audioDevice.empty() ? "@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\\wave_{00000000-0000-0000-0000-000000000000}" : audioParams_.audioDevice);
  645. }
  646. #elif defined(__linux__)
  647. if (audioParams_.type == CapturerType::AUDIO_MIC) {
  648. if (!audioParams_.deviceName.empty()) {
  649. return audioParams_.deviceName;
  650. } else {
  651. return "hw:" + std::to_string(audioParams_.micIndex);
  652. }
  653. } else {
  654. return audioParams_.audioDevice.empty() ? "default" : audioParams_.audioDevice;
  655. }
  656. #elif defined(__APPLE__)
  657. if (audioParams_.type == CapturerType::AUDIO_MIC) {
  658. return ":" + std::to_string(audioParams_.micIndex);
  659. } else {
  660. return ":none";
  661. }
  662. #endif
  663. return "";
  664. }
  665. #ifdef _WIN32
  666. std::vector<AudioDeviceInfo> AudioCapturer::enumerateDirectSoundDevices() const {
  667. std::vector<AudioDeviceInfo> devices;
  668. // 尝试使用FFmpeg的设备枚举功能
  669. const AVInputFormat* inputFormat = av_find_input_format("dshow");
  670. if (!inputFormat) {
  671. AV_LOGGER_WARNING("DirectShow输入格式不可用");
  672. return devices;
  673. }
  674. AVFormatContext* formatCtx = nullptr;
  675. AVDictionary* options = nullptr;
  676. // 设置列出设备选项
  677. av_dict_set(&options, "list_devices", "true", 0);
  678. // 尝试列出音频设备
  679. int ret = avformat_open_input(&formatCtx, "audio=dummy", inputFormat, &options);
  680. av_dict_free(&options);
  681. if (formatCtx) {
  682. avformat_close_input(&formatCtx);
  683. }
  684. // 添加默认设备作为后备
  685. AudioDeviceInfo defaultDevice;
  686. defaultDevice.id = "default";
  687. defaultDevice.name = "默认音频设备";
  688. defaultDevice.description = "系统默认音频输入设备";
  689. defaultDevice.isDefault = true;
  690. defaultDevice.isInput = true;
  691. // 添加常见采样率
  692. defaultDevice.supportedSampleRates = {8000, 16000, 22050, 44100, 48000};
  693. // 添加常见声道数
  694. defaultDevice.supportedChannels = {1, 2};
  695. // 添加支持的采样格式
  696. defaultDevice.supportedFormats = {
  697. AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT
  698. };
  699. devices.push_back(defaultDevice);
  700. // 如果没有找到其他设备,至少提供一个编号设备
  701. if (devices.size() == 1) {
  702. AudioDeviceInfo device;
  703. device.id = "0";
  704. device.name = "音频设备 0";
  705. device.description = "DirectShow音频设备 0";
  706. device.isDefault = false;
  707. device.isInput = true;
  708. device.supportedSampleRates = {8000, 16000, 22050, 44100, 48000};
  709. device.supportedChannels = {1, 2};
  710. device.supportedFormats = {AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT};
  711. devices.push_back(device);
  712. }
  713. return devices;
  714. }
  715. std::vector<AudioDeviceInfo> AudioCapturer::enumerateWASAPIDevices() const {
  716. std::vector<AudioDeviceInfo> devices;
  717. // 简化的WASAPI设备枚举
  718. AudioDeviceInfo device;
  719. device.id = "wasapi_default";
  720. device.name = "默认系统音频";
  721. device.description = "WASAPI系统音频设备";
  722. device.isDefault = true;
  723. device.isInput = false;
  724. // 添加常见采样率
  725. device.supportedSampleRates = {44100, 48000, 96000};
  726. // 添加常见声道数
  727. device.supportedChannels = {2, 6, 8};
  728. // 添加支持的采样格式
  729. device.supportedFormats = {
  730. AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT
  731. };
  732. devices.push_back(device);
  733. return devices;
  734. }
  735. ErrorCode AudioCapturer::setupDirectSoundMicrophone() {
  736. AV_LOGGER_INFO("设置DirectSound麦克风");
  737. return openInputDevice();
  738. }
  739. ErrorCode AudioCapturer::setupWASAPISystemAudio() {
  740. AV_LOGGER_INFO("设置WASAPI系统音频");
  741. return openInputDevice();
  742. }
  743. #endif
  744. // AudioCaptureFactory 实现
  745. std::unique_ptr<AudioCapturer> AudioCapturer::AudioCaptureFactory::createMicrophone(int micIndex) {
  746. auto capturer = std::make_unique<AudioCapturer>();
  747. AudioCaptureParams params(CapturerType::AUDIO_MIC);
  748. params.micIndex = micIndex;
  749. ErrorCode result = capturer->initialize(params);
  750. if (result != ErrorCode::SUCCESS) {
  751. AV_LOGGER_ERRORF("创建麦克风采集器失败: {}", static_cast<int>(result));
  752. return nullptr;
  753. }
  754. return capturer;
  755. }
  756. std::unique_ptr<AudioCapturer> AudioCapturer::AudioCaptureFactory::createSystemAudio(bool loopback) {
  757. auto capturer = std::make_unique<AudioCapturer>();
  758. AudioCaptureParams params(loopback ? CapturerType::AUDIO_LOOPBACK : CapturerType::AUDIO_SYSTEM);
  759. params.captureLoopback = loopback;
  760. ErrorCode result = capturer->initialize(params);
  761. if (result != ErrorCode::SUCCESS) {
  762. AV_LOGGER_ERRORF("创建系统音频采集器失败: {}", static_cast<int>(result));
  763. return nullptr;
  764. }
  765. return capturer;
  766. }
  767. std::unique_ptr<AudioCapturer> AudioCapturer::AudioCaptureFactory::createBestMicrophone() {
  768. return createMicrophone(0); // 默认使用第一个麦克风
  769. }
  770. } // namespace capture
  771. } // namespace av