captions.cpp 10 KB

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