1
0

captions.cpp 10 KB

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