audio_encoder.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "audio_encoder.h"
  2. #include <QDebug>
  3. #include <algorithm>
  4. std::vector<std::string> Encoder<MediaType::AUDIO>::_usableEncoders;
  5. const std::vector<std::string>& Encoder<MediaType::AUDIO>::GetUsableEncoders()
  6. {
  7. if (_usableEncoders.empty()) {
  8. _FindUsableEncoders();
  9. }
  10. return _usableEncoders;
  11. }
  12. void Encoder<MediaType::AUDIO>::_FindUsableEncoders()
  13. {
  14. qDebug() << "AudioEncoder: Finding usable encoders...";
  15. Param param;
  16. param.bitRate = 128000; // 128 kbps
  17. Encoder encoder;
  18. AVFormatContext* fmtCtx = nullptr;
  19. int ctxRet = avformat_alloc_output_context2(&fmtCtx, nullptr, nullptr, "test.mp4");
  20. if (ctxRet < 0) { qDebug() << "AudioEncoder: Failed to alloc output context for testing encoders"; return; }
  21. for (const auto& name : _encoderNames) {
  22. param.name = name;
  23. qDebug() << "AudioEncoder: Testing encoder:" << name;
  24. if (encoder.Open(param, fmtCtx)) {
  25. _usableEncoders.push_back(name);
  26. qDebug() << "AudioEncoder: Encoder" << name << "is usable";
  27. } else {
  28. qDebug() << "AudioEncoder: Encoder" << name << "failed to open";
  29. }
  30. encoder.Close();
  31. }
  32. // 简单的优先级排序:AAC优先,其次Opus,最后MP3
  33. if (!_usableEncoders.empty()) {
  34. auto priority = [](const std::string& n) -> int {
  35. if (n == "aac" || n == "libfdk_aac") return 0; // 与mp4/flv/rtmp兼容性最好
  36. if (n == "libopus" || n == "opus") return 1; // 需容器/推流支持
  37. if (n == "libmp3lame") return 2; // 一般不用于flv
  38. return 100;
  39. };
  40. std::sort(_usableEncoders.begin(), _usableEncoders.end(), [&](const std::string& a, const std::string& b) {
  41. int pa = priority(a);
  42. int pb = priority(b);
  43. if (pa != pb) return pa < pb;
  44. return a < b;
  45. });
  46. }
  47. qDebug() << "AudioEncoder: Found" << _usableEncoders.size() << "usable encoders";
  48. Free(fmtCtx, [&fmtCtx] { avformat_free_context(fmtCtx); });
  49. }
  50. bool Encoder<MediaType::AUDIO>::Open(const Param& audioParam, AVFormatContext* fmtCtx)
  51. {
  52. Close();
  53. _isOpen = false;
  54. qDebug() << "AudioEncoder::Open: Initializing audio encoder with bitrate" << audioParam.bitRate
  55. << ", name:" << (audioParam.name.empty() ? "<auto>" : audioParam.name.c_str());
  56. if (!_Init(audioParam, fmtCtx)) {
  57. qDebug() << "AudioEncoder::Open failed: Audio encoder initialization failed";
  58. return false;
  59. }
  60. int ret = avcodec_open2(_codecCtx, _codec, nullptr);
  61. if (ret < 0) {
  62. char errbuf[AV_ERROR_MAX_STRING_SIZE];
  63. av_strerror(ret, errbuf, AV_ERROR_MAX_STRING_SIZE);
  64. qDebug() << "AudioEncoder::Open failed: Cannot open audio codec. Error:" << errbuf << "(" << ret << ")";
  65. qDebug() << "Codec name:" << _codec->name << "ID:" << _codec->id;
  66. qDebug() << "Sample rate:" << _codecCtx->sample_rate << "Channels:" << _codecCtx->ch_layout.nb_channels;
  67. return false;
  68. }
  69. _isOpen = true;
  70. qDebug() << "AudioEncoder::Open: Audio encoder opened successfully";
  71. return true;
  72. }
  73. void Encoder<MediaType::AUDIO>::Close()
  74. {
  75. Free(_codecCtx, [this] { avcodec_free_context(&_codecCtx); });
  76. }
  77. bool Encoder<MediaType::AUDIO>::_Init(const Param& audioParam, AVFormatContext* fmtCtx)
  78. {
  79. // codec 选择:优先按名称,其次回退到容器默认音频编码器,再次回退到AAC
  80. if (!audioParam.name.empty()) {
  81. _codec = avcodec_find_encoder_by_name(audioParam.name.c_str());
  82. }
  83. if (!_codec && fmtCtx && fmtCtx->oformat && fmtCtx->oformat->audio_codec != AV_CODEC_ID_NONE) {
  84. _codec = avcodec_find_encoder(fmtCtx->oformat->audio_codec);
  85. }
  86. if (!_codec) {
  87. _codec = avcodec_find_encoder(AV_CODEC_ID_AAC);
  88. }
  89. if (!_codec) { __DebugPrint("audio avcodec_find_encoder failed"); return false; }
  90. // codeccontext
  91. _codecCtx = avcodec_alloc_context3(_codec);
  92. if (!_codecCtx) { __DebugPrint("avcodec_alloc_context3 failed"); return false; }
  93. _codecCtx->sample_fmt = AV_SAMPLE_FMT_FLTP;
  94. _codecCtx->bit_rate = audioParam.bitRate;
  95. _codecCtx->sample_rate = AUDIO_SAMPLE_RATE;
  96. AVChannelLayout layout;
  97. layout.order = AV_CHANNEL_ORDER_NATIVE;
  98. layout.nb_channels = AUDIO_CHANNEL;
  99. layout.u.mask = (AUDIO_CHANNEL == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO);
  100. av_channel_layout_copy(&_codecCtx->ch_layout, &layout);
  101. if (fmtCtx->oformat->flags & AVFMT_GLOBALHEADER) {
  102. _codecCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  103. }
  104. return true;
  105. }
  106. bool Encoder<MediaType::AUDIO>::PushFrame(AVFrame* frame, bool isEnd, uint64_t pts)
  107. {
  108. if (!isEnd) {
  109. if (!frame) { return false; }
  110. } else {
  111. frame = nullptr;
  112. }
  113. if (frame != nullptr) {
  114. frame->pts = pts;
  115. }
  116. int sendRet = avcodec_send_frame(_codecCtx, frame);
  117. if (sendRet < 0) { __DebugPrint("avcodec_send_frame failed: %d", sendRet); return false; }
  118. return true;
  119. }