2015-07-02 00:52:59 -07:00
|
|
|
#include <algorithm>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "audio-encoders.hpp"
|
2015-08-10 05:57:39 -07:00
|
|
|
#include "obs-app.hpp"
|
|
|
|
#include "window-main.hpp"
|
2015-07-02 00:52:59 -07:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
static const string encoders[] = {
|
|
|
|
"ffmpeg_aac",
|
2015-06-06 20:08:53 -07:00
|
|
|
"mf_aac",
|
2015-07-02 00:52:59 -07:00
|
|
|
"libfdk_aac",
|
|
|
|
"CoreAudio_AAC",
|
|
|
|
};
|
|
|
|
|
|
|
|
static const string &fallbackEncoder = encoders[0];
|
|
|
|
|
|
|
|
static const char *NullToEmpty(const char *str)
|
|
|
|
{
|
|
|
|
return str ? str : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *EncoderName(const char *id)
|
|
|
|
{
|
|
|
|
return NullToEmpty(obs_encoder_get_display_name(id));
|
|
|
|
}
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
static map<int, const char *> bitrateMap;
|
2015-07-02 00:52:59 -07:00
|
|
|
static once_flag populateBitrateMap;
|
|
|
|
|
|
|
|
static void HandleIntProperty(obs_property_t *prop, const char *id)
|
|
|
|
{
|
|
|
|
const int max_ = obs_property_int_max(prop);
|
|
|
|
const int step = obs_property_int_step(prop);
|
|
|
|
|
|
|
|
for (int i = obs_property_int_min(prop); i <= max_; i += step)
|
|
|
|
bitrateMap[i] = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void HandleListProperty(obs_property_t *prop, const char *id)
|
|
|
|
{
|
|
|
|
obs_combo_format format = obs_property_list_format(prop);
|
|
|
|
if (format != OBS_COMBO_FORMAT_INT) {
|
2019-06-22 22:13:45 -07:00
|
|
|
blog(LOG_ERROR,
|
|
|
|
"Encoder '%s' (%s) returned bitrate "
|
|
|
|
"OBS_PROPERTY_LIST property of unhandled "
|
|
|
|
"format %d",
|
|
|
|
EncoderName(id), id, static_cast<int>(format));
|
2015-07-02 00:52:59 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const size_t count = obs_property_list_item_count(prop);
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
2015-08-10 05:56:29 -07:00
|
|
|
if (obs_property_list_item_disabled(prop, i))
|
|
|
|
continue;
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
int bitrate =
|
|
|
|
static_cast<int>(obs_property_list_item_int(prop, i));
|
2015-07-02 00:52:59 -07:00
|
|
|
bitrateMap[bitrate] = id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
static void HandleSampleRate(obs_property_t *prop, const char *id)
|
2015-08-10 05:57:39 -07:00
|
|
|
{
|
2019-06-22 22:13:45 -07:00
|
|
|
auto ReleaseData = [](obs_data_t *data) { obs_data_release(data); };
|
2015-08-10 05:57:39 -07:00
|
|
|
std::unique_ptr<obs_data_t, decltype(ReleaseData)> data{
|
2019-06-22 22:13:45 -07:00
|
|
|
obs_encoder_defaults(id), ReleaseData};
|
2015-08-10 05:57:39 -07:00
|
|
|
|
|
|
|
if (!data) {
|
2019-06-22 22:13:45 -07:00
|
|
|
blog(LOG_ERROR,
|
|
|
|
"Failed to get defaults for encoder '%s' (%s) "
|
|
|
|
"while populating bitrate map",
|
|
|
|
EncoderName(id), id);
|
2015-08-10 05:57:39 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
auto main = reinterpret_cast<OBSMainWindow *>(App()->GetMainWindow());
|
2015-08-10 05:57:39 -07:00
|
|
|
if (!main) {
|
|
|
|
blog(LOG_ERROR, "Failed to get main window while populating "
|
|
|
|
"bitrate map");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
uint32_t sampleRate =
|
|
|
|
config_get_uint(main->Config(), "Audio", "SampleRate");
|
2015-08-10 05:57:39 -07:00
|
|
|
|
|
|
|
obs_data_set_int(data.get(), "samplerate", sampleRate);
|
|
|
|
|
|
|
|
obs_property_modified(prop, data.get());
|
|
|
|
}
|
|
|
|
|
2015-07-02 00:52:59 -07:00
|
|
|
static void HandleEncoderProperties(const char *id)
|
|
|
|
{
|
2019-06-22 22:13:45 -07:00
|
|
|
auto DestroyProperties = [](obs_properties_t *props) {
|
2015-07-02 00:52:59 -07:00
|
|
|
obs_properties_destroy(props);
|
|
|
|
};
|
|
|
|
std::unique_ptr<obs_properties_t, decltype(DestroyProperties)> props{
|
2019-06-22 22:13:45 -07:00
|
|
|
obs_get_encoder_properties(id), DestroyProperties};
|
2015-07-02 00:52:59 -07:00
|
|
|
|
|
|
|
if (!props) {
|
2019-06-22 22:13:45 -07:00
|
|
|
blog(LOG_ERROR,
|
|
|
|
"Failed to get properties for encoder "
|
|
|
|
"'%s' (%s)",
|
|
|
|
EncoderName(id), id);
|
2015-07-02 00:52:59 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
obs_property_t *samplerate =
|
|
|
|
obs_properties_get(props.get(), "samplerate");
|
2015-08-10 05:57:39 -07:00
|
|
|
if (samplerate)
|
|
|
|
HandleSampleRate(samplerate, id);
|
|
|
|
|
2015-07-02 00:52:59 -07:00
|
|
|
obs_property_t *bitrate = obs_properties_get(props.get(), "bitrate");
|
|
|
|
|
|
|
|
obs_property_type type = obs_property_get_type(bitrate);
|
|
|
|
switch (type) {
|
|
|
|
case OBS_PROPERTY_INT:
|
|
|
|
return HandleIntProperty(bitrate, id);
|
|
|
|
|
|
|
|
case OBS_PROPERTY_LIST:
|
|
|
|
return HandleListProperty(bitrate, id);
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
default:
|
|
|
|
break;
|
2015-07-02 00:52:59 -07:00
|
|
|
}
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
blog(LOG_ERROR,
|
|
|
|
"Encoder '%s' (%s) returned bitrate property "
|
|
|
|
"of unhandled type %d",
|
|
|
|
EncoderName(id), id, static_cast<int>(type));
|
2015-07-02 00:52:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *GetCodec(const char *id)
|
|
|
|
{
|
|
|
|
return NullToEmpty(obs_get_encoder_codec(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
static const string aac_ = "AAC";
|
|
|
|
static void PopulateBitrateMap()
|
|
|
|
{
|
2019-06-22 22:13:45 -07:00
|
|
|
call_once(populateBitrateMap, []() {
|
libobs: Add surround sound audio support
(This commit also modifies the following modules: UI,
deps/media-playback, coreaudio-encoder, decklink, linux-alsa,
linux-pulseaudio, mac-capture, obs-ffmpeg, obs-filters, obs-libfdk,
obs-outputs, win-dshow, and win-wasapi)
Adds surround sound audio support to the core, core plugins, and user
interface.
Compatible streaming services: Twitch, FB 360 live
Compatible protocols: rtmp / mpeg-ts tcp udp
Compatible file formats: mkv mp4 ts (others untested)
Compatible codecs: ffmpeg aac, fdk_aac, CoreAudio aac,
opus, vorbis, pcm (others untested).
Tested streaming servers: wowza, nginx
HLS, mpeg-dash : surround passthrough
Html5 players tested with live surround:
videojs, mediaelement, viblast (hls+dash), hls.js
Decklink: on win32, swap channels order for 5.1 7.1
(due to different channel mapping on wav, mpeg, ffmpeg)
Audio filters: surround working.
Monitoring: surround working (win macOs linux (pulse-audio)).
VST: stereo plugins keep in general only the first two channels.
surround plugins should work (e.g. mcfx does).
OS: win, macOs, linux (alsa, pulse-audio).
Misc: larger audio bitrates unlocked to accommodate more channels
NB: mf-aac only supports mono and stereo + 5.1 on win 10
(not implemented due to lack of usefulness)
Closes jp9000/obs-studio#968
2017-05-26 17:15:54 -07:00
|
|
|
struct obs_audio_info aoi;
|
|
|
|
obs_get_audio_info(&aoi);
|
|
|
|
uint32_t output_channels = get_audio_channels(aoi.speakers);
|
|
|
|
|
2015-07-02 00:52:59 -07:00
|
|
|
HandleEncoderProperties(fallbackEncoder.c_str());
|
|
|
|
|
|
|
|
const char *id = nullptr;
|
|
|
|
for (size_t i = 0; obs_enum_encoder_types(i, &id); i++) {
|
2019-06-22 22:13:45 -07:00
|
|
|
auto Compare = [=](const string &val) {
|
2015-07-02 00:52:59 -07:00
|
|
|
return val == NullToEmpty(id);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (find_if(begin(encoders), end(encoders), Compare) !=
|
2019-06-22 22:13:45 -07:00
|
|
|
end(encoders))
|
2015-07-02 00:52:59 -07:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (aac_ != GetCodec(id))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
HandleEncoderProperties(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &encoder : encoders) {
|
|
|
|
if (encoder == fallbackEncoder)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (aac_ != GetCodec(encoder.c_str()))
|
|
|
|
continue;
|
|
|
|
|
docs/sphinx: Fix various typos
(This modifies UI, libobs, deps/obs-scripting, various cmake files)
Found using:
`codespell -q 3 -S *.ini,./UI/data/locale,./deps/w32-pthreads -L aci,dur,iff,mut,numer,uint`
2019-09-19 04:31:17 -07:00
|
|
|
// disable mf_aac if audio output is not stereo nor mono
|
libobs: Add surround sound audio support
(This commit also modifies the following modules: UI,
deps/media-playback, coreaudio-encoder, decklink, linux-alsa,
linux-pulseaudio, mac-capture, obs-ffmpeg, obs-filters, obs-libfdk,
obs-outputs, win-dshow, and win-wasapi)
Adds surround sound audio support to the core, core plugins, and user
interface.
Compatible streaming services: Twitch, FB 360 live
Compatible protocols: rtmp / mpeg-ts tcp udp
Compatible file formats: mkv mp4 ts (others untested)
Compatible codecs: ffmpeg aac, fdk_aac, CoreAudio aac,
opus, vorbis, pcm (others untested).
Tested streaming servers: wowza, nginx
HLS, mpeg-dash : surround passthrough
Html5 players tested with live surround:
videojs, mediaelement, viblast (hls+dash), hls.js
Decklink: on win32, swap channels order for 5.1 7.1
(due to different channel mapping on wav, mpeg, ffmpeg)
Audio filters: surround working.
Monitoring: surround working (win macOs linux (pulse-audio)).
VST: stereo plugins keep in general only the first two channels.
surround plugins should work (e.g. mcfx does).
OS: win, macOs, linux (alsa, pulse-audio).
Misc: larger audio bitrates unlocked to accommodate more channels
NB: mf-aac only supports mono and stereo + 5.1 on win 10
(not implemented due to lack of usefulness)
Closes jp9000/obs-studio#968
2017-05-26 17:15:54 -07:00
|
|
|
if ((output_channels >= 3) && (encoder == "mf_aac"))
|
|
|
|
continue;
|
|
|
|
|
2015-07-02 00:52:59 -07:00
|
|
|
HandleEncoderProperties(encoder.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bitrateMap.empty()) {
|
|
|
|
blog(LOG_ERROR, "Could not enumerate any AAC encoder "
|
|
|
|
"bitrates");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ostringstream ss;
|
|
|
|
for (auto &entry : bitrateMap)
|
|
|
|
ss << "\n " << setw(3) << entry.first
|
|
|
|
<< " kbit/s: '" << EncoderName(entry.second) << "' ("
|
|
|
|
<< entry.second << ')';
|
|
|
|
|
2016-08-05 15:36:10 -07:00
|
|
|
blog(LOG_DEBUG, "AAC encoder bitrate mapping:%s",
|
2019-06-22 22:13:45 -07:00
|
|
|
ss.str().c_str());
|
2015-07-02 00:52:59 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
const map<int, const char *> &GetAACEncoderBitrateMap()
|
2015-07-02 00:52:59 -07:00
|
|
|
{
|
|
|
|
PopulateBitrateMap();
|
|
|
|
return bitrateMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *GetAACEncoderForBitrate(int bitrate)
|
|
|
|
{
|
|
|
|
auto &map_ = GetAACEncoderBitrateMap();
|
|
|
|
auto res = map_.find(bitrate);
|
|
|
|
if (res == end(map_))
|
|
|
|
return NULL;
|
|
|
|
return res->second;
|
|
|
|
}
|
2015-09-21 18:36:26 -07:00
|
|
|
|
|
|
|
#define INVALID_BITRATE 10000
|
|
|
|
|
|
|
|
int FindClosestAvailableAACBitrate(int bitrate)
|
|
|
|
{
|
|
|
|
auto &map_ = GetAACEncoderBitrateMap();
|
|
|
|
int prev = 0;
|
|
|
|
int next = INVALID_BITRATE;
|
|
|
|
|
|
|
|
for (auto val : map_) {
|
|
|
|
if (next > val.first) {
|
|
|
|
if (val.first == bitrate)
|
|
|
|
return bitrate;
|
|
|
|
|
|
|
|
if (val.first < next && val.first > bitrate)
|
|
|
|
next = val.first;
|
|
|
|
if (val.first > prev && val.first < bitrate)
|
|
|
|
prev = val.first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (next != INVALID_BITRATE)
|
|
|
|
return next;
|
|
|
|
if (prev != 0)
|
|
|
|
return prev;
|
|
|
|
return 192;
|
|
|
|
}
|