2017-06-29 19:01:10 +00:00
|
|
|
#include <QMessageBox>
|
2018-12-16 16:14:58 +00:00
|
|
|
#include <QAction>
|
2017-06-29 19:01:10 +00:00
|
|
|
|
|
|
|
#include <windows.h>
|
2017-04-19 19:54:15 +00:00
|
|
|
#include <obs-frontend-api.h>
|
|
|
|
#include "captions.hpp"
|
2017-06-29 19:01:10 +00:00
|
|
|
#include "captions-handler.hpp"
|
2017-04-19 19:54:15 +00:00
|
|
|
#include "tool-helpers.hpp"
|
|
|
|
#include <util/dstr.hpp>
|
|
|
|
#include <util/platform.h>
|
2017-06-29 19:01:10 +00:00
|
|
|
#include <util/windows/WinHandle.hpp>
|
2017-04-19 19:54:15 +00:00
|
|
|
#include <util/windows/ComPtr.hpp>
|
|
|
|
#include <obs-module.h>
|
2018-02-19 19:54:37 +00:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable : 4996)
|
|
|
|
#endif
|
|
|
|
|
2017-06-29 19:01:10 +00:00
|
|
|
#include <sphelper.h>
|
2017-04-19 19:54:15 +00:00
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2017-06-29 19:01:10 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
2017-04-19 19:54:15 +00:00
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
|
|
|
#include <mutex>
|
|
|
|
|
2017-06-29 19:01:10 +00:00
|
|
|
#include "captions-mssapi.hpp"
|
|
|
|
|
2017-04-19 19:54:15 +00:00
|
|
|
#define do_log(type, format, ...) blog(type, "[Captions] " format, \
|
|
|
|
##__VA_ARGS__)
|
|
|
|
|
2017-06-29 19:01:10 +00:00
|
|
|
#define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
|
2017-04-19 19:54:15 +00:00
|
|
|
#define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2017-06-29 19:01:10 +00:00
|
|
|
#define DEFAULT_HANDLER "mssapi"
|
2017-04-19 19:54:15 +00:00
|
|
|
|
2017-06-29 19:01:10 +00:00
|
|
|
struct obs_captions {
|
|
|
|
string handler_id = DEFAULT_HANDLER;
|
2017-04-19 19:54:15 +00:00
|
|
|
string source_name;
|
|
|
|
OBSWeakSource source;
|
2017-06-29 19:01:10 +00:00
|
|
|
unique_ptr<captions_handler> handler;
|
|
|
|
LANGID lang_id = GetUserDefaultUILanguage();
|
2017-04-19 19:54:15 +00:00
|
|
|
|
2017-06-29 19:01:10 +00:00
|
|
|
std::unordered_map<std::string, captions_handler_info&> handler_types;
|
2017-04-19 19:54:15 +00:00
|
|
|
|
2017-06-29 19:01:10 +00:00
|
|
|
inline void register_handler(const char *id,
|
|
|
|
captions_handler_info &info)
|
2017-04-19 19:54:15 +00:00
|
|
|
{
|
2017-06-29 19:01:10 +00:00
|
|
|
handler_types.emplace(id, info);
|
2017-04-19 19:54:15 +00:00
|
|
|
}
|
|
|
|
|
2017-06-29 19:01:10 +00:00
|
|
|
void start();
|
|
|
|
void stop();
|
|
|
|
|
|
|
|
obs_captions();
|
2017-04-19 19:54:15 +00:00
|
|
|
inline ~obs_captions() {stop();}
|
|
|
|
};
|
|
|
|
|
|
|
|
static obs_captions *captions = nullptr;
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
struct locale_info {
|
|
|
|
DStr name;
|
|
|
|
LANGID id;
|
|
|
|
|
|
|
|
inline locale_info() {}
|
|
|
|
inline locale_info(const locale_info &) = delete;
|
|
|
|
inline locale_info(locale_info &&li)
|
|
|
|
: name(std::move(li.name)),
|
|
|
|
id(li.id)
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void get_valid_locale_names(vector<locale_info> &names);
|
|
|
|
static bool valid_lang(LANGID id);
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
CaptionsDialog::CaptionsDialog(QWidget *parent) :
|
|
|
|
QDialog(parent),
|
|
|
|
ui(new Ui_CaptionsDialog)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
2019-07-27 12:47:10 +00:00
|
|
|
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
|
|
|
|
|
2017-04-19 19:54:15 +00:00
|
|
|
auto cb = [this] (obs_source_t *source)
|
|
|
|
{
|
|
|
|
uint32_t caps = obs_source_get_output_flags(source);
|
|
|
|
QString name = obs_source_get_name(source);
|
|
|
|
|
|
|
|
if (caps & OBS_SOURCE_AUDIO)
|
|
|
|
ui->source->addItem(name);
|
|
|
|
|
|
|
|
OBSWeakSource weak = OBSGetWeakRef(source);
|
|
|
|
if (weak == captions->source)
|
|
|
|
ui->source->setCurrentText(name);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
using cb_t = decltype(cb);
|
|
|
|
|
|
|
|
ui->source->blockSignals(true);
|
|
|
|
ui->source->addItem(QStringLiteral(""));
|
|
|
|
ui->source->setCurrentIndex(0);
|
|
|
|
obs_enum_sources([] (void *data, obs_source_t *source) {
|
|
|
|
return (*static_cast<cb_t*>(data))(source);}, &cb);
|
|
|
|
ui->source->blockSignals(false);
|
|
|
|
|
2017-06-29 19:01:10 +00:00
|
|
|
for (auto &ht : captions->handler_types) {
|
|
|
|
QString name = ht.second.name().c_str();
|
|
|
|
QString id = ht.first.c_str();
|
|
|
|
ui->provider->addItem(name, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString qhandler_id = captions->handler_id.c_str();
|
|
|
|
int idx = ui->provider->findData(qhandler_id);
|
|
|
|
if (idx != -1)
|
|
|
|
ui->provider->setCurrentIndex(idx);
|
|
|
|
|
2017-04-19 19:54:15 +00:00
|
|
|
ui->enable->blockSignals(true);
|
2017-06-29 19:01:10 +00:00
|
|
|
ui->enable->setChecked(!!captions->handler);
|
2017-04-19 19:54:15 +00:00
|
|
|
ui->enable->blockSignals(false);
|
|
|
|
|
|
|
|
vector<locale_info> locales;
|
|
|
|
get_valid_locale_names(locales);
|
|
|
|
|
|
|
|
bool set_language = false;
|
|
|
|
|
|
|
|
ui->language->blockSignals(true);
|
|
|
|
for (int idx = 0; idx < (int)locales.size(); idx++) {
|
|
|
|
locale_info &locale = locales[idx];
|
|
|
|
|
|
|
|
ui->language->addItem(locale.name->array, (int)locale.id);
|
|
|
|
|
|
|
|
if (locale.id == captions->lang_id) {
|
|
|
|
ui->language->setCurrentIndex(idx);
|
|
|
|
set_language = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!set_language && locales.size())
|
|
|
|
ui->language->setCurrentIndex(0);
|
|
|
|
|
|
|
|
ui->language->blockSignals(false);
|
|
|
|
|
|
|
|
if (!locales.size()) {
|
|
|
|
ui->source->setEnabled(false);
|
|
|
|
ui->enable->setEnabled(false);
|
|
|
|
ui->language->setEnabled(false);
|
|
|
|
|
|
|
|
} else if (!set_language) {
|
2017-06-29 19:01:10 +00:00
|
|
|
bool started = !!captions->handler;
|
2017-04-19 19:54:15 +00:00
|
|
|
if (started)
|
|
|
|
captions->stop();
|
|
|
|
|
|
|
|
captions->lang_id = locales[0].id;
|
|
|
|
|
|
|
|
if (started)
|
|
|
|
captions->start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CaptionsDialog::on_source_currentIndexChanged(int)
|
|
|
|
{
|
2017-06-29 19:01:10 +00:00
|
|
|
bool started = !!captions->handler;
|
2017-04-19 19:54:15 +00:00
|
|
|
if (started)
|
|
|
|
captions->stop();
|
|
|
|
|
|
|
|
captions->source_name = ui->source->currentText().toUtf8().constData();
|
|
|
|
captions->source = GetWeakSourceByName(captions->source_name.c_str());
|
|
|
|
|
|
|
|
if (started)
|
|
|
|
captions->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CaptionsDialog::on_enable_clicked(bool checked)
|
|
|
|
{
|
2017-06-29 19:01:10 +00:00
|
|
|
if (checked) {
|
2017-04-19 19:54:15 +00:00
|
|
|
captions->start();
|
2017-06-29 19:01:10 +00:00
|
|
|
if (!captions->handler) {
|
|
|
|
ui->enable->blockSignals(true);
|
|
|
|
ui->enable->setChecked(false);
|
|
|
|
ui->enable->blockSignals(false);
|
|
|
|
}
|
|
|
|
} else {
|
2017-04-19 19:54:15 +00:00
|
|
|
captions->stop();
|
2017-06-29 19:01:10 +00:00
|
|
|
}
|
2017-04-19 19:54:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CaptionsDialog::on_language_currentIndexChanged(int)
|
|
|
|
{
|
2017-06-29 19:01:10 +00:00
|
|
|
bool started = !!captions->handler;
|
2017-04-19 19:54:15 +00:00
|
|
|
if (started)
|
|
|
|
captions->stop();
|
|
|
|
|
|
|
|
captions->lang_id = (LANGID)ui->language->currentData().toInt();
|
|
|
|
|
|
|
|
if (started)
|
|
|
|
captions->start();
|
|
|
|
}
|
|
|
|
|
2017-06-29 19:01:10 +00:00
|
|
|
void CaptionsDialog::on_provider_currentIndexChanged(int idx)
|
|
|
|
{
|
|
|
|
bool started = !!captions->handler;
|
|
|
|
if (started)
|
|
|
|
captions->stop();
|
2017-04-19 19:54:15 +00:00
|
|
|
|
2017-06-29 19:01:10 +00:00
|
|
|
captions->handler_id =
|
|
|
|
ui->provider->itemData(idx).toString().toUtf8().constData();
|
2017-04-19 19:54:15 +00:00
|
|
|
|
2017-06-29 19:01:10 +00:00
|
|
|
if (started)
|
|
|
|
captions->start();
|
|
|
|
}
|
2017-04-19 19:54:15 +00:00
|
|
|
|
2017-06-29 19:01:10 +00:00
|
|
|
/* ------------------------------------------------------------------------- */
|
2017-04-19 19:54:15 +00:00
|
|
|
|
2017-06-29 19:01:10 +00:00
|
|
|
static void caption_text(const std::string &text)
|
|
|
|
{
|
|
|
|
obs_output *output = obs_frontend_get_streaming_output();
|
|
|
|
if (output) {
|
|
|
|
obs_output_output_caption_text1(output, text.c_str());
|
|
|
|
obs_output_release(output);
|
2017-04-19 19:54:15 +00:00
|
|
|
}
|
2017-06-29 19:01:10 +00:00
|
|
|
}
|
2017-04-19 19:54:15 +00:00
|
|
|
|
2017-06-29 19:01:10 +00:00
|
|
|
static void audio_capture(void*, obs_source_t*,
|
|
|
|
const struct audio_data *audio, bool)
|
|
|
|
{
|
|
|
|
captions->handler->push_audio(audio);
|
|
|
|
}
|
2017-04-19 19:54:15 +00:00
|
|
|
|
2017-06-29 19:01:10 +00:00
|
|
|
void obs_captions::start()
|
|
|
|
{
|
|
|
|
if (!captions->handler && valid_lang(lang_id)) {
|
|
|
|
wchar_t wname[256];
|
|
|
|
|
|
|
|
auto pair = handler_types.find(handler_id);
|
|
|
|
if (pair == handler_types.end()) {
|
|
|
|
warn("Failed to find handler '%s'",
|
|
|
|
handler_id.c_str());
|
|
|
|
return;
|
|
|
|
}
|
2017-04-19 19:54:15 +00:00
|
|
|
|
2017-06-29 19:01:10 +00:00
|
|
|
if (!LCIDToLocaleName(lang_id, wname, 256, 0)) {
|
|
|
|
warn("Failed to get locale name: %d",
|
|
|
|
(int)GetLastError());
|
|
|
|
return;
|
|
|
|
}
|
2017-04-19 19:54:15 +00:00
|
|
|
|
2017-06-29 19:01:10 +00:00
|
|
|
size_t len = (size_t)wcslen(wname);
|
2017-04-19 19:54:15 +00:00
|
|
|
|
2017-06-29 19:01:10 +00:00
|
|
|
string lang_name;
|
|
|
|
lang_name.resize(len);
|
2017-04-19 19:54:15 +00:00
|
|
|
|
2017-06-29 19:01:10 +00:00
|
|
|
for (size_t i = 0; i < len; i++)
|
|
|
|
lang_name[i] = (char)wname[i];
|
2017-04-19 19:54:15 +00:00
|
|
|
|
2017-06-29 19:01:10 +00:00
|
|
|
OBSSource s = OBSGetStrongRef(source);
|
|
|
|
if (!s) {
|
|
|
|
warn("Source invalid");
|
|
|
|
return;
|
2017-04-19 19:54:15 +00:00
|
|
|
}
|
|
|
|
|
2017-06-29 19:01:10 +00:00
|
|
|
try {
|
|
|
|
captions_handler *h = pair->second.create(caption_text,
|
|
|
|
lang_name);
|
|
|
|
handler.reset(h);
|
2017-04-19 19:54:15 +00:00
|
|
|
|
2017-06-29 19:01:10 +00:00
|
|
|
OBSSource s = OBSGetStrongRef(source);
|
|
|
|
obs_source_add_audio_capture_callback(s,
|
|
|
|
audio_capture, nullptr);
|
2017-04-19 19:54:15 +00:00
|
|
|
|
2017-06-29 19:01:10 +00:00
|
|
|
} catch (std::string text) {
|
|
|
|
QWidget *window =
|
|
|
|
(QWidget*)obs_frontend_get_main_window();
|
2017-04-19 19:54:15 +00:00
|
|
|
|
2017-06-29 19:01:10 +00:00
|
|
|
warn("Failed to create handler: %s", text.c_str());
|
2017-04-19 19:54:15 +00:00
|
|
|
|
2017-06-29 19:01:10 +00:00
|
|
|
QMessageBox::warning(window,
|
|
|
|
obs_module_text("Captions.Error.GenericFail"),
|
|
|
|
text.c_str());
|
2017-04-19 19:54:15 +00:00
|
|
|
|
2017-06-29 19:01:10 +00:00
|
|
|
}
|
2017-04-19 19:54:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void obs_captions::stop()
|
|
|
|
{
|
2017-06-29 19:01:10 +00:00
|
|
|
OBSSource s = OBSGetStrongRef(source);
|
|
|
|
if (s)
|
|
|
|
obs_source_remove_audio_capture_callback(s,
|
|
|
|
audio_capture, nullptr);
|
|
|
|
handler.reset();
|
2017-04-19 19:54:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool get_locale_name(LANGID id, char *out)
|
|
|
|
{
|
|
|
|
wchar_t name[256];
|
|
|
|
|
|
|
|
int size = GetLocaleInfoW(id, LOCALE_SENGLISHLANGUAGENAME, name, 256);
|
|
|
|
if (size <= 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
os_wcs_to_utf8(name, 0, out, 256);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool valid_lang(LANGID id)
|
|
|
|
{
|
|
|
|
ComPtr<ISpObjectToken> token;
|
|
|
|
wchar_t lang_str[32];
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
_snwprintf(lang_str, 31, L"language=%x", (int)id);
|
|
|
|
|
|
|
|
hr = SpFindBestToken(SPCAT_RECOGNIZERS, lang_str, nullptr, &token);
|
|
|
|
return SUCCEEDED(hr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void get_valid_locale_names(vector<locale_info> &locales)
|
|
|
|
{
|
|
|
|
locale_info cur;
|
|
|
|
char locale_name[256];
|
|
|
|
|
|
|
|
static const LANGID default_locales[] = {
|
|
|
|
0x0409,
|
|
|
|
0x0401,
|
|
|
|
0x0402,
|
|
|
|
0x0403,
|
|
|
|
0x0404,
|
|
|
|
0x0405,
|
|
|
|
0x0406,
|
|
|
|
0x0407,
|
|
|
|
0x0408,
|
|
|
|
0x040a,
|
|
|
|
0x040b,
|
|
|
|
0x040c,
|
|
|
|
0x040d,
|
|
|
|
0x040e,
|
|
|
|
0x040f,
|
|
|
|
0x0410,
|
|
|
|
0x0411,
|
|
|
|
0x0412,
|
|
|
|
0x0413,
|
|
|
|
0x0414,
|
|
|
|
0x0415,
|
|
|
|
0x0416,
|
|
|
|
0x0417,
|
|
|
|
0x0418,
|
|
|
|
0x0419,
|
|
|
|
0x041a,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
/* ---------------------------------- */
|
|
|
|
|
|
|
|
LANGID def_id = GetUserDefaultUILanguage();
|
|
|
|
LANGID id = def_id;
|
|
|
|
if (valid_lang(id) && get_locale_name(id, locale_name)) {
|
|
|
|
dstr_copy(cur.name, obs_module_text(
|
|
|
|
"Captions.CurrentSystemLanguage"));
|
|
|
|
dstr_replace(cur.name, "%1", locale_name);
|
|
|
|
cur.id = id;
|
|
|
|
|
|
|
|
locales.push_back(std::move(cur));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------------------------------- */
|
|
|
|
|
|
|
|
const LANGID *locale = default_locales;
|
|
|
|
|
|
|
|
while (*locale) {
|
|
|
|
id = *locale;
|
|
|
|
|
|
|
|
if (id != def_id &&
|
|
|
|
valid_lang(id) &&
|
|
|
|
get_locale_name(id, locale_name)) {
|
|
|
|
|
|
|
|
dstr_copy(cur.name, locale_name);
|
|
|
|
cur.id = id;
|
|
|
|
|
|
|
|
locales.push_back(std::move(cur));
|
|
|
|
}
|
|
|
|
|
|
|
|
locale++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
2017-06-29 19:01:10 +00:00
|
|
|
extern captions_handler_info mssapi_info;
|
|
|
|
|
|
|
|
obs_captions::obs_captions()
|
|
|
|
{
|
|
|
|
register_handler("mssapi", mssapi_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
2017-04-19 19:54:15 +00:00
|
|
|
extern "C" void FreeCaptions()
|
|
|
|
{
|
|
|
|
delete captions;
|
|
|
|
captions = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void obs_event(enum obs_frontend_event event, void *)
|
|
|
|
{
|
|
|
|
if (event == OBS_FRONTEND_EVENT_EXIT)
|
|
|
|
FreeCaptions();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void save_caption_data(obs_data_t *save_data, bool saving, void*)
|
|
|
|
{
|
|
|
|
if (saving) {
|
|
|
|
obs_data_t *obj = obs_data_create();
|
|
|
|
|
|
|
|
obs_data_set_string(obj, "source",
|
|
|
|
captions->source_name.c_str());
|
2017-06-29 19:01:10 +00:00
|
|
|
obs_data_set_bool(obj, "enabled", !!captions->handler);
|
2017-04-19 19:54:15 +00:00
|
|
|
obs_data_set_int(obj, "lang_id", captions->lang_id);
|
2017-06-29 19:01:10 +00:00
|
|
|
obs_data_set_string(obj, "provider",
|
|
|
|
captions->handler_id.c_str());
|
2017-04-19 19:54:15 +00:00
|
|
|
|
|
|
|
obs_data_set_obj(save_data, "captions", obj);
|
|
|
|
obs_data_release(obj);
|
|
|
|
} else {
|
|
|
|
captions->stop();
|
|
|
|
|
|
|
|
obs_data_t *obj = obs_data_get_obj(save_data, "captions");
|
|
|
|
if (!obj)
|
|
|
|
obj = obs_data_create();
|
|
|
|
|
|
|
|
obs_data_set_default_int(obj, "lang_id",
|
|
|
|
GetUserDefaultUILanguage());
|
2017-06-29 19:01:10 +00:00
|
|
|
obs_data_set_default_string(obj, "provider", DEFAULT_HANDLER);
|
2017-04-19 19:54:15 +00:00
|
|
|
|
|
|
|
bool enabled = obs_data_get_bool(obj, "enabled");
|
|
|
|
captions->source_name = obs_data_get_string(obj, "source");
|
|
|
|
captions->lang_id = (int)obs_data_get_int(obj, "lang_id");
|
2017-06-29 19:01:10 +00:00
|
|
|
captions->handler_id = obs_data_get_string(obj, "provider");
|
2017-04-19 19:54:15 +00:00
|
|
|
captions->source = GetWeakSourceByName(
|
|
|
|
captions->source_name.c_str());
|
|
|
|
obs_data_release(obj);
|
|
|
|
|
|
|
|
if (enabled)
|
|
|
|
captions->start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void InitCaptions()
|
|
|
|
{
|
|
|
|
QAction *action = (QAction*)obs_frontend_add_tools_menu_qaction(
|
|
|
|
obs_module_text("Captions"));
|
|
|
|
|
|
|
|
captions = new obs_captions;
|
|
|
|
|
|
|
|
auto cb = [] ()
|
|
|
|
{
|
|
|
|
obs_frontend_push_ui_translation(obs_module_get_string);
|
|
|
|
|
|
|
|
QWidget *window =
|
|
|
|
(QWidget*)obs_frontend_get_main_window();
|
|
|
|
|
|
|
|
CaptionsDialog dialog(window);
|
|
|
|
dialog.exec();
|
|
|
|
|
|
|
|
obs_frontend_pop_ui_translation();
|
|
|
|
};
|
|
|
|
|
|
|
|
obs_frontend_add_save_callback(save_caption_data, nullptr);
|
|
|
|
obs_frontend_add_event_callback(obs_event, nullptr);
|
|
|
|
|
|
|
|
action->connect(action, &QAction::triggered, cb);
|
|
|
|
}
|