audio-encoders.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include <algorithm>
  2. #include <iomanip>
  3. #include <map>
  4. #include <memory>
  5. #include <mutex>
  6. #include <sstream>
  7. #include <string>
  8. #include <vector>
  9. #include "audio-encoders.hpp"
  10. using namespace std;
  11. static const string encoders[] = {
  12. "ffmpeg_aac",
  13. "mf_aac",
  14. "libfdk_aac",
  15. "CoreAudio_AAC",
  16. };
  17. static const string &fallbackEncoder = encoders[0];
  18. static const char *NullToEmpty(const char *str)
  19. {
  20. return str ? str : "";
  21. }
  22. static const char *EncoderName(const char *id)
  23. {
  24. return NullToEmpty(obs_encoder_get_display_name(id));
  25. }
  26. static map<int, const char*> bitrateMap;
  27. static once_flag populateBitrateMap;
  28. static void HandleIntProperty(obs_property_t *prop, const char *id)
  29. {
  30. const int max_ = obs_property_int_max(prop);
  31. const int step = obs_property_int_step(prop);
  32. for (int i = obs_property_int_min(prop); i <= max_; i += step)
  33. bitrateMap[i] = id;
  34. }
  35. static void HandleListProperty(obs_property_t *prop, const char *id)
  36. {
  37. obs_combo_format format = obs_property_list_format(prop);
  38. if (format != OBS_COMBO_FORMAT_INT) {
  39. blog(LOG_ERROR, "Encoder '%s' (%s) returned bitrate "
  40. "OBS_PROPERTY_LIST property of unhandled "
  41. "format %d",
  42. EncoderName(id), id, static_cast<int>(format));
  43. return;
  44. }
  45. const size_t count = obs_property_list_item_count(prop);
  46. for (size_t i = 0; i < count; i++) {
  47. int bitrate = static_cast<int>(
  48. obs_property_list_item_int(prop, i));
  49. bitrateMap[bitrate] = id;
  50. }
  51. }
  52. static void HandleEncoderProperties(const char *id)
  53. {
  54. auto DestroyProperties = [](obs_properties_t *props)
  55. {
  56. obs_properties_destroy(props);
  57. };
  58. std::unique_ptr<obs_properties_t, decltype(DestroyProperties)> props{
  59. obs_get_encoder_properties(id),
  60. DestroyProperties};
  61. if (!props) {
  62. blog(LOG_ERROR, "Failed to get properties for encoder "
  63. "'%s' (%s)",
  64. EncoderName(id), id);
  65. return;
  66. }
  67. obs_property_t *bitrate = obs_properties_get(props.get(), "bitrate");
  68. obs_property_type type = obs_property_get_type(bitrate);
  69. switch (type) {
  70. case OBS_PROPERTY_INT:
  71. return HandleIntProperty(bitrate, id);
  72. case OBS_PROPERTY_LIST:
  73. return HandleListProperty(bitrate, id);
  74. default: break;
  75. }
  76. blog(LOG_ERROR, "Encoder '%s' (%s) returned bitrate property "
  77. "of unhandled type %d", EncoderName(id), id,
  78. static_cast<int>(type));
  79. }
  80. static const char *GetCodec(const char *id)
  81. {
  82. return NullToEmpty(obs_get_encoder_codec(id));
  83. }
  84. static const string aac_ = "AAC";
  85. static void PopulateBitrateMap()
  86. {
  87. call_once(populateBitrateMap, []()
  88. {
  89. HandleEncoderProperties(fallbackEncoder.c_str());
  90. const char *id = nullptr;
  91. for (size_t i = 0; obs_enum_encoder_types(i, &id); i++) {
  92. auto Compare = [=](const string &val)
  93. {
  94. return val == NullToEmpty(id);
  95. };
  96. if (find_if(begin(encoders), end(encoders), Compare) !=
  97. end(encoders))
  98. continue;
  99. if (aac_ != GetCodec(id))
  100. continue;
  101. HandleEncoderProperties(id);
  102. }
  103. for (auto &encoder : encoders) {
  104. if (encoder == fallbackEncoder)
  105. continue;
  106. if (aac_ != GetCodec(encoder.c_str()))
  107. continue;
  108. HandleEncoderProperties(encoder.c_str());
  109. }
  110. if (bitrateMap.empty()) {
  111. blog(LOG_ERROR, "Could not enumerate any AAC encoder "
  112. "bitrates");
  113. return;
  114. }
  115. ostringstream ss;
  116. for (auto &entry : bitrateMap)
  117. ss << "\n " << setw(3) << entry.first
  118. << " kbit/s: '" << EncoderName(entry.second) << "' ("
  119. << entry.second << ')';
  120. blog(LOG_INFO, "AAC encoder bitrate mapping:%s",
  121. ss.str().c_str());
  122. });
  123. }
  124. const map<int, const char*> &GetAACEncoderBitrateMap()
  125. {
  126. PopulateBitrateMap();
  127. return bitrateMap;
  128. }
  129. const char *GetAACEncoderForBitrate(int bitrate)
  130. {
  131. auto &map_ = GetAACEncoderBitrateMap();
  132. auto res = map_.find(bitrate);
  133. if (res == end(map_))
  134. return NULL;
  135. return res->second;
  136. }