audio-encoders.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #include "obs-app.hpp"
  11. #include "window-main.hpp"
  12. using namespace std;
  13. static const string encoders[] = {
  14. "ffmpeg_aac",
  15. "mf_aac",
  16. "libfdk_aac",
  17. "CoreAudio_AAC",
  18. };
  19. static const string &fallbackEncoder = encoders[0];
  20. static const char *NullToEmpty(const char *str)
  21. {
  22. return str ? str : "";
  23. }
  24. static const char *EncoderName(const char *id)
  25. {
  26. return NullToEmpty(obs_encoder_get_display_name(id));
  27. }
  28. static map<int, const char *> bitrateMap;
  29. static once_flag populateBitrateMap;
  30. static void HandleIntProperty(obs_property_t *prop, const char *id)
  31. {
  32. const int max_ = obs_property_int_max(prop);
  33. const int step = obs_property_int_step(prop);
  34. for (int i = obs_property_int_min(prop); i <= max_; i += step)
  35. bitrateMap[i] = id;
  36. }
  37. static void HandleListProperty(obs_property_t *prop, const char *id)
  38. {
  39. obs_combo_format format = obs_property_list_format(prop);
  40. if (format != OBS_COMBO_FORMAT_INT) {
  41. blog(LOG_ERROR,
  42. "Encoder '%s' (%s) returned bitrate "
  43. "OBS_PROPERTY_LIST property of unhandled "
  44. "format %d",
  45. EncoderName(id), id, static_cast<int>(format));
  46. return;
  47. }
  48. const size_t count = obs_property_list_item_count(prop);
  49. for (size_t i = 0; i < count; i++) {
  50. if (obs_property_list_item_disabled(prop, i))
  51. continue;
  52. int bitrate =
  53. static_cast<int>(obs_property_list_item_int(prop, i));
  54. bitrateMap[bitrate] = id;
  55. }
  56. }
  57. static void HandleSampleRate(obs_property_t *prop, const char *id)
  58. {
  59. auto ReleaseData = [](obs_data_t *data) { obs_data_release(data); };
  60. std::unique_ptr<obs_data_t, decltype(ReleaseData)> data{
  61. obs_encoder_defaults(id), ReleaseData};
  62. if (!data) {
  63. blog(LOG_ERROR,
  64. "Failed to get defaults for encoder '%s' (%s) "
  65. "while populating bitrate map",
  66. EncoderName(id), id);
  67. return;
  68. }
  69. auto main = reinterpret_cast<OBSMainWindow *>(App()->GetMainWindow());
  70. if (!main) {
  71. blog(LOG_ERROR, "Failed to get main window while populating "
  72. "bitrate map");
  73. return;
  74. }
  75. uint32_t sampleRate =
  76. config_get_uint(main->Config(), "Audio", "SampleRate");
  77. obs_data_set_int(data.get(), "samplerate", sampleRate);
  78. obs_property_modified(prop, data.get());
  79. }
  80. static void HandleEncoderProperties(const char *id)
  81. {
  82. auto DestroyProperties = [](obs_properties_t *props) {
  83. obs_properties_destroy(props);
  84. };
  85. std::unique_ptr<obs_properties_t, decltype(DestroyProperties)> props{
  86. obs_get_encoder_properties(id), DestroyProperties};
  87. if (!props) {
  88. blog(LOG_ERROR,
  89. "Failed to get properties for encoder "
  90. "'%s' (%s)",
  91. EncoderName(id), id);
  92. return;
  93. }
  94. obs_property_t *samplerate =
  95. obs_properties_get(props.get(), "samplerate");
  96. if (samplerate)
  97. HandleSampleRate(samplerate, id);
  98. obs_property_t *bitrate = obs_properties_get(props.get(), "bitrate");
  99. obs_property_type type = obs_property_get_type(bitrate);
  100. switch (type) {
  101. case OBS_PROPERTY_INT:
  102. return HandleIntProperty(bitrate, id);
  103. case OBS_PROPERTY_LIST:
  104. return HandleListProperty(bitrate, id);
  105. default:
  106. break;
  107. }
  108. blog(LOG_ERROR,
  109. "Encoder '%s' (%s) returned bitrate property "
  110. "of unhandled type %d",
  111. EncoderName(id), id, static_cast<int>(type));
  112. }
  113. static const char *GetCodec(const char *id)
  114. {
  115. return NullToEmpty(obs_get_encoder_codec(id));
  116. }
  117. static const string aac_ = "AAC";
  118. static void PopulateBitrateMap()
  119. {
  120. call_once(populateBitrateMap, []() {
  121. struct obs_audio_info aoi;
  122. obs_get_audio_info(&aoi);
  123. uint32_t output_channels = get_audio_channels(aoi.speakers);
  124. HandleEncoderProperties(fallbackEncoder.c_str());
  125. const char *id = nullptr;
  126. for (size_t i = 0; obs_enum_encoder_types(i, &id); i++) {
  127. auto Compare = [=](const string &val) {
  128. return val == NullToEmpty(id);
  129. };
  130. if (find_if(begin(encoders), end(encoders), Compare) !=
  131. end(encoders))
  132. continue;
  133. if (aac_ != GetCodec(id))
  134. continue;
  135. HandleEncoderProperties(id);
  136. }
  137. for (auto &encoder : encoders) {
  138. if (encoder == fallbackEncoder)
  139. continue;
  140. if (aac_ != GetCodec(encoder.c_str()))
  141. continue;
  142. // disable mf_aac if audio output is not stereo nor mono
  143. if ((output_channels >= 3) && (encoder == "mf_aac"))
  144. continue;
  145. HandleEncoderProperties(encoder.c_str());
  146. }
  147. if (bitrateMap.empty()) {
  148. blog(LOG_ERROR, "Could not enumerate any AAC encoder "
  149. "bitrates");
  150. return;
  151. }
  152. ostringstream ss;
  153. for (auto &entry : bitrateMap)
  154. ss << "\n " << setw(3) << entry.first
  155. << " kbit/s: '" << EncoderName(entry.second) << "' ("
  156. << entry.second << ')';
  157. blog(LOG_DEBUG, "AAC encoder bitrate mapping:%s",
  158. ss.str().c_str());
  159. });
  160. }
  161. const map<int, const char *> &GetAACEncoderBitrateMap()
  162. {
  163. PopulateBitrateMap();
  164. return bitrateMap;
  165. }
  166. const char *GetAACEncoderForBitrate(int bitrate)
  167. {
  168. auto &map_ = GetAACEncoderBitrateMap();
  169. auto res = map_.find(bitrate);
  170. if (res == end(map_))
  171. return NULL;
  172. return res->second;
  173. }
  174. #define INVALID_BITRATE 10000
  175. int FindClosestAvailableAACBitrate(int bitrate)
  176. {
  177. auto &map_ = GetAACEncoderBitrateMap();
  178. int prev = 0;
  179. int next = INVALID_BITRATE;
  180. for (auto val : map_) {
  181. if (next > val.first) {
  182. if (val.first == bitrate)
  183. return bitrate;
  184. if (val.first < next && val.first > bitrate)
  185. next = val.first;
  186. if (val.first > prev && val.first < bitrate)
  187. prev = val.first;
  188. }
  189. }
  190. if (next != INVALID_BITRATE)
  191. return next;
  192. if (prev != 0)
  193. return prev;
  194. return 192;
  195. }