captions.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. #include <QMessageBox>
  2. #include <QAction>
  3. #include <windows.h>
  4. #include <obs-frontend-api.h>
  5. #include "captions.hpp"
  6. #include "captions-handler.hpp"
  7. #include "tool-helpers.hpp"
  8. #include <util/dstr.hpp>
  9. #include <util/platform.h>
  10. #include <util/windows/WinHandle.hpp>
  11. #include <util/windows/ComPtr.hpp>
  12. #include <obs-module.h>
  13. #ifdef _MSC_VER
  14. #pragma warning(push)
  15. #pragma warning(disable : 4996)
  16. #endif
  17. #include <sphelper.h>
  18. #ifdef _MSC_VER
  19. #pragma warning(pop)
  20. #endif
  21. #include <unordered_map>
  22. #include <vector>
  23. #include <string>
  24. #include <thread>
  25. #include <mutex>
  26. #include "captions-mssapi.hpp"
  27. #define do_log(type, format, ...) blog(type, "[Captions] " format, \
  28. ##__VA_ARGS__)
  29. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  30. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  31. using namespace std;
  32. #define DEFAULT_HANDLER "mssapi"
  33. struct obs_captions {
  34. string handler_id = DEFAULT_HANDLER;
  35. string source_name;
  36. OBSWeakSource source;
  37. unique_ptr<captions_handler> handler;
  38. LANGID lang_id = GetUserDefaultUILanguage();
  39. std::unordered_map<std::string, captions_handler_info&> handler_types;
  40. inline void register_handler(const char *id,
  41. captions_handler_info &info)
  42. {
  43. handler_types.emplace(id, info);
  44. }
  45. void start();
  46. void stop();
  47. obs_captions();
  48. inline ~obs_captions() {stop();}
  49. };
  50. static obs_captions *captions = nullptr;
  51. /* ------------------------------------------------------------------------- */
  52. struct locale_info {
  53. DStr name;
  54. LANGID id;
  55. inline locale_info() {}
  56. inline locale_info(const locale_info &) = delete;
  57. inline locale_info(locale_info &&li)
  58. : name(std::move(li.name)),
  59. id(li.id)
  60. {}
  61. };
  62. static void get_valid_locale_names(vector<locale_info> &names);
  63. static bool valid_lang(LANGID id);
  64. /* ------------------------------------------------------------------------- */
  65. CaptionsDialog::CaptionsDialog(QWidget *parent) :
  66. QDialog(parent),
  67. ui(new Ui_CaptionsDialog)
  68. {
  69. ui->setupUi(this);
  70. auto cb = [this] (obs_source_t *source)
  71. {
  72. uint32_t caps = obs_source_get_output_flags(source);
  73. QString name = obs_source_get_name(source);
  74. if (caps & OBS_SOURCE_AUDIO)
  75. ui->source->addItem(name);
  76. OBSWeakSource weak = OBSGetWeakRef(source);
  77. if (weak == captions->source)
  78. ui->source->setCurrentText(name);
  79. return true;
  80. };
  81. using cb_t = decltype(cb);
  82. ui->source->blockSignals(true);
  83. ui->source->addItem(QStringLiteral(""));
  84. ui->source->setCurrentIndex(0);
  85. obs_enum_sources([] (void *data, obs_source_t *source) {
  86. return (*static_cast<cb_t*>(data))(source);}, &cb);
  87. ui->source->blockSignals(false);
  88. for (auto &ht : captions->handler_types) {
  89. QString name = ht.second.name().c_str();
  90. QString id = ht.first.c_str();
  91. ui->provider->addItem(name, id);
  92. }
  93. QString qhandler_id = captions->handler_id.c_str();
  94. int idx = ui->provider->findData(qhandler_id);
  95. if (idx != -1)
  96. ui->provider->setCurrentIndex(idx);
  97. ui->enable->blockSignals(true);
  98. ui->enable->setChecked(!!captions->handler);
  99. ui->enable->blockSignals(false);
  100. vector<locale_info> locales;
  101. get_valid_locale_names(locales);
  102. bool set_language = false;
  103. ui->language->blockSignals(true);
  104. for (int idx = 0; idx < (int)locales.size(); idx++) {
  105. locale_info &locale = locales[idx];
  106. ui->language->addItem(locale.name->array, (int)locale.id);
  107. if (locale.id == captions->lang_id) {
  108. ui->language->setCurrentIndex(idx);
  109. set_language = true;
  110. }
  111. }
  112. if (!set_language && locales.size())
  113. ui->language->setCurrentIndex(0);
  114. ui->language->blockSignals(false);
  115. if (!locales.size()) {
  116. ui->source->setEnabled(false);
  117. ui->enable->setEnabled(false);
  118. ui->language->setEnabled(false);
  119. } else if (!set_language) {
  120. bool started = !!captions->handler;
  121. if (started)
  122. captions->stop();
  123. captions->lang_id = locales[0].id;
  124. if (started)
  125. captions->start();
  126. }
  127. }
  128. void CaptionsDialog::on_source_currentIndexChanged(int)
  129. {
  130. bool started = !!captions->handler;
  131. if (started)
  132. captions->stop();
  133. captions->source_name = ui->source->currentText().toUtf8().constData();
  134. captions->source = GetWeakSourceByName(captions->source_name.c_str());
  135. if (started)
  136. captions->start();
  137. }
  138. void CaptionsDialog::on_enable_clicked(bool checked)
  139. {
  140. if (checked) {
  141. captions->start();
  142. if (!captions->handler) {
  143. ui->enable->blockSignals(true);
  144. ui->enable->setChecked(false);
  145. ui->enable->blockSignals(false);
  146. }
  147. } else {
  148. captions->stop();
  149. }
  150. }
  151. void CaptionsDialog::on_language_currentIndexChanged(int)
  152. {
  153. bool started = !!captions->handler;
  154. if (started)
  155. captions->stop();
  156. captions->lang_id = (LANGID)ui->language->currentData().toInt();
  157. if (started)
  158. captions->start();
  159. }
  160. void CaptionsDialog::on_provider_currentIndexChanged(int idx)
  161. {
  162. bool started = !!captions->handler;
  163. if (started)
  164. captions->stop();
  165. captions->handler_id =
  166. ui->provider->itemData(idx).toString().toUtf8().constData();
  167. if (started)
  168. captions->start();
  169. }
  170. /* ------------------------------------------------------------------------- */
  171. static void caption_text(const std::string &text)
  172. {
  173. obs_output *output = obs_frontend_get_streaming_output();
  174. if (output) {
  175. obs_output_output_caption_text1(output, text.c_str());
  176. obs_output_release(output);
  177. }
  178. }
  179. static void audio_capture(void*, obs_source_t*,
  180. const struct audio_data *audio, bool)
  181. {
  182. captions->handler->push_audio(audio);
  183. }
  184. void obs_captions::start()
  185. {
  186. if (!captions->handler && valid_lang(lang_id)) {
  187. wchar_t wname[256];
  188. auto pair = handler_types.find(handler_id);
  189. if (pair == handler_types.end()) {
  190. warn("Failed to find handler '%s'",
  191. handler_id.c_str());
  192. return;
  193. }
  194. if (!LCIDToLocaleName(lang_id, wname, 256, 0)) {
  195. warn("Failed to get locale name: %d",
  196. (int)GetLastError());
  197. return;
  198. }
  199. size_t len = (size_t)wcslen(wname);
  200. string lang_name;
  201. lang_name.resize(len);
  202. for (size_t i = 0; i < len; i++)
  203. lang_name[i] = (char)wname[i];
  204. OBSSource s = OBSGetStrongRef(source);
  205. if (!s) {
  206. warn("Source invalid");
  207. return;
  208. }
  209. try {
  210. captions_handler *h = pair->second.create(caption_text,
  211. lang_name);
  212. handler.reset(h);
  213. OBSSource s = OBSGetStrongRef(source);
  214. obs_source_add_audio_capture_callback(s,
  215. audio_capture, nullptr);
  216. } catch (std::string text) {
  217. QWidget *window =
  218. (QWidget*)obs_frontend_get_main_window();
  219. warn("Failed to create handler: %s", text.c_str());
  220. QMessageBox::warning(window,
  221. obs_module_text("Captions.Error.GenericFail"),
  222. text.c_str());
  223. }
  224. }
  225. }
  226. void obs_captions::stop()
  227. {
  228. OBSSource s = OBSGetStrongRef(source);
  229. if (s)
  230. obs_source_remove_audio_capture_callback(s,
  231. audio_capture, nullptr);
  232. handler.reset();
  233. }
  234. static bool get_locale_name(LANGID id, char *out)
  235. {
  236. wchar_t name[256];
  237. int size = GetLocaleInfoW(id, LOCALE_SENGLISHLANGUAGENAME, name, 256);
  238. if (size <= 0)
  239. return false;
  240. os_wcs_to_utf8(name, 0, out, 256);
  241. return true;
  242. }
  243. static bool valid_lang(LANGID id)
  244. {
  245. ComPtr<ISpObjectToken> token;
  246. wchar_t lang_str[32];
  247. HRESULT hr;
  248. _snwprintf(lang_str, 31, L"language=%x", (int)id);
  249. hr = SpFindBestToken(SPCAT_RECOGNIZERS, lang_str, nullptr, &token);
  250. return SUCCEEDED(hr);
  251. }
  252. static void get_valid_locale_names(vector<locale_info> &locales)
  253. {
  254. locale_info cur;
  255. char locale_name[256];
  256. static const LANGID default_locales[] = {
  257. 0x0409,
  258. 0x0401,
  259. 0x0402,
  260. 0x0403,
  261. 0x0404,
  262. 0x0405,
  263. 0x0406,
  264. 0x0407,
  265. 0x0408,
  266. 0x040a,
  267. 0x040b,
  268. 0x040c,
  269. 0x040d,
  270. 0x040e,
  271. 0x040f,
  272. 0x0410,
  273. 0x0411,
  274. 0x0412,
  275. 0x0413,
  276. 0x0414,
  277. 0x0415,
  278. 0x0416,
  279. 0x0417,
  280. 0x0418,
  281. 0x0419,
  282. 0x041a,
  283. 0
  284. };
  285. /* ---------------------------------- */
  286. LANGID def_id = GetUserDefaultUILanguage();
  287. LANGID id = def_id;
  288. if (valid_lang(id) && get_locale_name(id, locale_name)) {
  289. dstr_copy(cur.name, obs_module_text(
  290. "Captions.CurrentSystemLanguage"));
  291. dstr_replace(cur.name, "%1", locale_name);
  292. cur.id = id;
  293. locales.push_back(std::move(cur));
  294. }
  295. /* ---------------------------------- */
  296. const LANGID *locale = default_locales;
  297. while (*locale) {
  298. id = *locale;
  299. if (id != def_id &&
  300. valid_lang(id) &&
  301. get_locale_name(id, locale_name)) {
  302. dstr_copy(cur.name, locale_name);
  303. cur.id = id;
  304. locales.push_back(std::move(cur));
  305. }
  306. locale++;
  307. }
  308. }
  309. /* ------------------------------------------------------------------------- */
  310. extern captions_handler_info mssapi_info;
  311. obs_captions::obs_captions()
  312. {
  313. register_handler("mssapi", mssapi_info);
  314. }
  315. /* ------------------------------------------------------------------------- */
  316. extern "C" void FreeCaptions()
  317. {
  318. delete captions;
  319. captions = nullptr;
  320. }
  321. static void obs_event(enum obs_frontend_event event, void *)
  322. {
  323. if (event == OBS_FRONTEND_EVENT_EXIT)
  324. FreeCaptions();
  325. }
  326. static void save_caption_data(obs_data_t *save_data, bool saving, void*)
  327. {
  328. if (saving) {
  329. obs_data_t *obj = obs_data_create();
  330. obs_data_set_string(obj, "source",
  331. captions->source_name.c_str());
  332. obs_data_set_bool(obj, "enabled", !!captions->handler);
  333. obs_data_set_int(obj, "lang_id", captions->lang_id);
  334. obs_data_set_string(obj, "provider",
  335. captions->handler_id.c_str());
  336. obs_data_set_obj(save_data, "captions", obj);
  337. obs_data_release(obj);
  338. } else {
  339. captions->stop();
  340. obs_data_t *obj = obs_data_get_obj(save_data, "captions");
  341. if (!obj)
  342. obj = obs_data_create();
  343. obs_data_set_default_int(obj, "lang_id",
  344. GetUserDefaultUILanguage());
  345. obs_data_set_default_string(obj, "provider", DEFAULT_HANDLER);
  346. bool enabled = obs_data_get_bool(obj, "enabled");
  347. captions->source_name = obs_data_get_string(obj, "source");
  348. captions->lang_id = (int)obs_data_get_int(obj, "lang_id");
  349. captions->handler_id = obs_data_get_string(obj, "provider");
  350. captions->source = GetWeakSourceByName(
  351. captions->source_name.c_str());
  352. obs_data_release(obj);
  353. if (enabled)
  354. captions->start();
  355. }
  356. }
  357. extern "C" void InitCaptions()
  358. {
  359. QAction *action = (QAction*)obs_frontend_add_tools_menu_qaction(
  360. obs_module_text("Captions"));
  361. captions = new obs_captions;
  362. auto cb = [] ()
  363. {
  364. obs_frontend_push_ui_translation(obs_module_get_string);
  365. QWidget *window =
  366. (QWidget*)obs_frontend_get_main_window();
  367. CaptionsDialog dialog(window);
  368. dialog.exec();
  369. obs_frontend_pop_ui_translation();
  370. };
  371. obs_frontend_add_save_callback(save_caption_data, nullptr);
  372. obs_frontend_add_event_callback(obs_event, nullptr);
  373. action->connect(action, &QAction::triggered, cb);
  374. }