audio-encoders.cpp 3.7 KB

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