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