1
0

window-youtube-actions.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. #include "window-basic-main.hpp"
  2. #include "moc_window-youtube-actions.cpp"
  3. #include "obs-app.hpp"
  4. #include "youtube-api-wrappers.hpp"
  5. #include <qt-wrappers.hpp>
  6. #include <QToolTip>
  7. #include <QDateTime>
  8. #include <QDesktopServices>
  9. #include <QFileInfo>
  10. #include <QStandardPaths>
  11. #include <QImageReader>
  12. const QString SchedulDateAndTimeFormat = "yyyy-MM-dd'T'hh:mm:ss'Z'";
  13. const QString RepresentSchedulDateAndTimeFormat = "dddd, MMMM d, yyyy h:m";
  14. const QString IndexOfGamingCategory = "20";
  15. OBSYoutubeActions::OBSYoutubeActions(QWidget *parent, Auth *auth,
  16. bool broadcastReady)
  17. : QDialog(parent),
  18. ui(new Ui::OBSYoutubeActions),
  19. apiYouTube(dynamic_cast<YoutubeApiWrappers *>(auth)),
  20. workerThread(new WorkerThread(apiYouTube)),
  21. broadcastReady(broadcastReady)
  22. {
  23. setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
  24. ui->setupUi(this);
  25. ui->privacyBox->addItem(QTStr("YouTube.Actions.Privacy.Public"),
  26. "public");
  27. ui->privacyBox->addItem(QTStr("YouTube.Actions.Privacy.Unlisted"),
  28. "unlisted");
  29. ui->privacyBox->addItem(QTStr("YouTube.Actions.Privacy.Private"),
  30. "private");
  31. ui->latencyBox->addItem(QTStr("YouTube.Actions.Latency.Normal"),
  32. "normal");
  33. ui->latencyBox->addItem(QTStr("YouTube.Actions.Latency.Low"), "low");
  34. ui->latencyBox->addItem(QTStr("YouTube.Actions.Latency.UltraLow"),
  35. "ultraLow");
  36. UpdateOkButtonStatus();
  37. connect(ui->title, &QLineEdit::textChanged, this,
  38. [&](const QString &) { this->UpdateOkButtonStatus(); });
  39. connect(ui->privacyBox, &QComboBox::currentTextChanged, this,
  40. [&](const QString &) { this->UpdateOkButtonStatus(); });
  41. connect(ui->yesMakeForKids, &QRadioButton::toggled, this,
  42. [&](bool) { this->UpdateOkButtonStatus(); });
  43. connect(ui->notMakeForKids, &QRadioButton::toggled, this,
  44. [&](bool) { this->UpdateOkButtonStatus(); });
  45. connect(ui->tabWidget, &QTabWidget::currentChanged, this,
  46. [&](int) { this->UpdateOkButtonStatus(); });
  47. connect(ui->pushButton, &QPushButton::clicked, this,
  48. &OBSYoutubeActions::OpenYouTubeDashboard);
  49. connect(ui->helpAutoStartStop, &QLabel::linkActivated, this,
  50. [](const QString &) {
  51. QToolTip::showText(
  52. QCursor::pos(),
  53. QTStr("YouTube.Actions.AutoStartStop.TT"));
  54. });
  55. connect(ui->help360Video, &QLabel::linkActivated, this,
  56. [](const QString &link) { QDesktopServices::openUrl(link); });
  57. connect(ui->helpMadeForKids, &QLabel::linkActivated, this,
  58. [](const QString &link) { QDesktopServices::openUrl(link); });
  59. ui->scheduledTime->setVisible(false);
  60. #if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
  61. connect(ui->checkScheduledLater, &QCheckBox::checkStateChanged, this,
  62. [&](Qt::CheckState state)
  63. #else
  64. connect(ui->checkScheduledLater, &QCheckBox::stateChanged, this,
  65. [&](int state)
  66. #endif
  67. {
  68. const bool checked = (state == Qt::Checked);
  69. ui->scheduledTime->setVisible(checked);
  70. if (checked) {
  71. ui->checkAutoStart->setVisible(true);
  72. ui->checkAutoStop->setVisible(true);
  73. ui->helpAutoStartStop->setVisible(true);
  74. ui->checkAutoStart->setChecked(false);
  75. ui->checkAutoStop->setChecked(false);
  76. } else {
  77. ui->checkAutoStart->setVisible(false);
  78. ui->checkAutoStop->setVisible(false);
  79. ui->helpAutoStartStop->setVisible(false);
  80. ui->checkAutoStart->setChecked(true);
  81. ui->checkAutoStop->setChecked(true);
  82. }
  83. UpdateOkButtonStatus();
  84. });
  85. ui->checkAutoStart->setVisible(false);
  86. ui->checkAutoStop->setVisible(false);
  87. ui->helpAutoStartStop->setVisible(false);
  88. ui->scheduledTime->setDateTime(QDateTime::currentDateTime());
  89. auto thumbSelectionHandler = [&]() {
  90. if (thumbnailFile.isEmpty()) {
  91. QString filePath = OpenFile(
  92. this,
  93. QTStr("YouTube.Actions.Thumbnail.SelectFile"),
  94. QStandardPaths::writableLocation(
  95. QStandardPaths::PicturesLocation),
  96. QString("Images (*.png *.jpg *.jpeg *.gif)"));
  97. if (!filePath.isEmpty()) {
  98. QFileInfo tFile(filePath);
  99. if (!tFile.exists()) {
  100. return ShowErrorDialog(
  101. this,
  102. QTStr("YouTube.Actions.Error.FileMissing"));
  103. } else if (tFile.size() > 2 * 1024 * 1024) {
  104. return ShowErrorDialog(
  105. this,
  106. QTStr("YouTube.Actions.Error.FileTooLarge"));
  107. }
  108. thumbnailFile = filePath;
  109. ui->selectedFileName->setText(thumbnailFile);
  110. ui->selectFileButton->setText(QTStr(
  111. "YouTube.Actions.Thumbnail.ClearFile"));
  112. QImageReader imgReader(filePath);
  113. imgReader.setAutoTransform(true);
  114. const QImage newImage = imgReader.read();
  115. ui->thumbnailPreview->setPixmap(
  116. QPixmap::fromImage(newImage).scaled(
  117. 160, 90, Qt::KeepAspectRatio,
  118. Qt::SmoothTransformation));
  119. }
  120. } else {
  121. thumbnailFile.clear();
  122. ui->selectedFileName->setText(QTStr(
  123. "YouTube.Actions.Thumbnail.NoFileSelected"));
  124. ui->selectFileButton->setText(
  125. QTStr("YouTube.Actions.Thumbnail.SelectFile"));
  126. ui->thumbnailPreview->setPixmap(
  127. GetPlaceholder().pixmap(QSize(16, 16)));
  128. }
  129. };
  130. connect(ui->selectFileButton, &QPushButton::clicked, this,
  131. thumbSelectionHandler);
  132. connect(ui->thumbnailPreview, &ClickableLabel::clicked, this,
  133. thumbSelectionHandler);
  134. if (!apiYouTube) {
  135. blog(LOG_DEBUG, "YouTube API auth NOT found.");
  136. Cancel();
  137. return;
  138. }
  139. const char *name = config_get_string(OBSBasic::Get()->Config(),
  140. "YouTube", "ChannelName");
  141. this->setWindowTitle(QTStr("YouTube.Actions.WindowTitle").arg(name));
  142. QVector<CategoryDescription> category_list;
  143. if (!apiYouTube->GetVideoCategoriesList(category_list)) {
  144. ShowErrorDialog(
  145. parent,
  146. apiYouTube->GetLastError().isEmpty()
  147. ? QTStr("YouTube.Actions.Error.General")
  148. : QTStr("YouTube.Actions.Error.Text")
  149. .arg(apiYouTube->GetLastError()));
  150. Cancel();
  151. return;
  152. }
  153. for (auto &category : category_list) {
  154. ui->categoryBox->addItem(category.title, category.id);
  155. if (category.id == IndexOfGamingCategory) {
  156. ui->categoryBox->setCurrentText(category.title);
  157. }
  158. }
  159. connect(ui->okButton, &QPushButton::clicked, this,
  160. &OBSYoutubeActions::InitBroadcast);
  161. connect(ui->saveButton, &QPushButton::clicked, this,
  162. &OBSYoutubeActions::ReadyBroadcast);
  163. connect(ui->cancelButton, &QPushButton::clicked, this, [&]() {
  164. blog(LOG_DEBUG, "YouTube live broadcast creation cancelled.");
  165. // Close the dialog.
  166. Cancel();
  167. });
  168. qDeleteAll(ui->scrollAreaWidgetContents->findChildren<QWidget *>(
  169. QString(), Qt::FindDirectChildrenOnly));
  170. // Add label indicating loading state
  171. QLabel *loadingLabel = new QLabel();
  172. loadingLabel->setTextFormat(Qt::RichText);
  173. loadingLabel->setAlignment(Qt::AlignHCenter);
  174. loadingLabel->setText(
  175. QString("<big>%1</big>")
  176. .arg(QTStr("YouTube.Actions.EventsLoading")));
  177. ui->scrollAreaWidgetContents->layout()->addWidget(loadingLabel);
  178. // Delete "loading..." label on completion
  179. connect(workerThread, &WorkerThread::finished, this, [&] {
  180. QLayoutItem *item =
  181. ui->scrollAreaWidgetContents->layout()->takeAt(0);
  182. item->widget()->deleteLater();
  183. });
  184. connect(workerThread, &WorkerThread::failed, this, [&]() {
  185. auto last_error = apiYouTube->GetLastError();
  186. if (last_error.isEmpty())
  187. last_error = QTStr("YouTube.Actions.Error.YouTubeApi");
  188. if (!apiYouTube->GetTranslatedError(last_error))
  189. last_error = QTStr("YouTube.Actions.Error.Text")
  190. .arg(last_error);
  191. ShowErrorDialog(this, last_error);
  192. QDialog::reject();
  193. });
  194. connect(workerThread, &WorkerThread::new_item, this,
  195. [&](const QString &title, const QString &dateTimeString,
  196. const QString &broadcast, const QString &status,
  197. bool astart, bool astop) {
  198. ClickableLabel *label = new ClickableLabel();
  199. label->setTextFormat(Qt::RichText);
  200. if (status == "live" || status == "testing") {
  201. // Resumable stream
  202. label->setText(
  203. QString("<big>%1</big><br/>%2")
  204. .arg(title,
  205. QTStr("YouTube.Actions.Stream.Resume")));
  206. } else if (dateTimeString.isEmpty()) {
  207. // The broadcast created by YouTube Studio has no start time.
  208. // Yes this does violate the restrictions set in YouTube's API
  209. // But why would YouTube care about consistency?
  210. label->setText(
  211. QString("<big>%1</big><br/>%2")
  212. .arg(title,
  213. QTStr("YouTube.Actions.Stream.YTStudio")));
  214. } else {
  215. label->setText(
  216. QString("<big>%1</big><br/>%2")
  217. .arg(title,
  218. QTStr("YouTube.Actions.Stream.ScheduledFor")
  219. .arg(dateTimeString)));
  220. }
  221. label->setAlignment(Qt::AlignHCenter);
  222. label->setMargin(4);
  223. connect(label, &ClickableLabel::clicked, this,
  224. [&, label, broadcast, astart, astop]() {
  225. for (QWidget *i :
  226. ui->scrollAreaWidgetContents->findChildren<
  227. QWidget *>(
  228. QString(),
  229. Qt::FindDirectChildrenOnly)) {
  230. i->setProperty("class", "");
  231. i->style()->unpolish(i);
  232. i->style()->polish(i);
  233. }
  234. label->setProperty("class",
  235. "row-selected");
  236. label->style()->unpolish(label);
  237. label->style()->polish(label);
  238. this->selectedBroadcast = broadcast;
  239. this->autostart = astart;
  240. this->autostop = astop;
  241. UpdateOkButtonStatus();
  242. });
  243. ui->scrollAreaWidgetContents->layout()->addWidget(
  244. label);
  245. if (selectedBroadcast == broadcast)
  246. label->clicked();
  247. });
  248. workerThread->start();
  249. OBSBasic *main = OBSBasic::Get();
  250. bool rememberSettings = config_get_bool(main->activeConfiguration,
  251. "YouTube", "RememberSettings");
  252. if (rememberSettings)
  253. LoadSettings();
  254. // Switch to events page and select readied broadcast once loaded
  255. if (broadcastReady) {
  256. ui->tabWidget->setCurrentIndex(1);
  257. selectedBroadcast = apiYouTube->GetBroadcastId();
  258. }
  259. #ifdef __APPLE__
  260. // MacOS theming issues
  261. this->resize(this->width() + 200, this->height() + 120);
  262. #endif
  263. valid = true;
  264. }
  265. void OBSYoutubeActions::showEvent(QShowEvent *event)
  266. {
  267. QDialog::showEvent(event);
  268. if (thumbnailFile.isEmpty())
  269. ui->thumbnailPreview->setPixmap(
  270. GetPlaceholder().pixmap(QSize(16, 16)));
  271. }
  272. OBSYoutubeActions::~OBSYoutubeActions()
  273. {
  274. workerThread->stop();
  275. workerThread->wait();
  276. delete workerThread;
  277. }
  278. void WorkerThread::run()
  279. {
  280. if (!pending)
  281. return;
  282. json11::Json broadcasts;
  283. for (QString broadcastStatus : {"active", "upcoming"}) {
  284. if (!apiYouTube->GetBroadcastsList(broadcasts, "",
  285. broadcastStatus)) {
  286. emit failed();
  287. return;
  288. }
  289. while (pending) {
  290. auto items = broadcasts["items"].array_items();
  291. for (auto item : items) {
  292. QString status = QString::fromStdString(
  293. item["status"]["lifeCycleStatus"]
  294. .string_value());
  295. if (status == "live" || status == "testing") {
  296. // Check that the attached liveStream is offline (reconnectable)
  297. QString stream_id = QString::fromStdString(
  298. item["contentDetails"]
  299. ["boundStreamId"]
  300. .string_value());
  301. json11::Json stream;
  302. if (!apiYouTube->FindStream(stream_id,
  303. stream))
  304. continue;
  305. if (stream["status"]["streamStatus"] ==
  306. "active")
  307. continue;
  308. }
  309. QString title = QString::fromStdString(
  310. item["snippet"]["title"].string_value());
  311. QString scheduledStartTime =
  312. QString::fromStdString(
  313. item["snippet"]
  314. ["scheduledStartTime"]
  315. .string_value());
  316. QString broadcast = QString::fromStdString(
  317. item["id"].string_value());
  318. // Treat already started streams as autostart for UI purposes
  319. bool astart =
  320. status == "live" ||
  321. item["contentDetails"]["enableAutoStart"]
  322. .bool_value();
  323. bool astop =
  324. item["contentDetails"]["enableAutoStop"]
  325. .bool_value();
  326. QDateTime utcDTime = QDateTime::fromString(
  327. scheduledStartTime,
  328. SchedulDateAndTimeFormat);
  329. // DateTime parser means that input datetime is a local, so we need to move it
  330. QDateTime dateTime = utcDTime.addSecs(
  331. utcDTime.offsetFromUtc());
  332. QString dateTimeString = QLocale().toString(
  333. dateTime,
  334. QString("%1 %2").arg(
  335. QLocale().dateFormat(
  336. QLocale::LongFormat),
  337. QLocale().timeFormat(
  338. QLocale::ShortFormat)));
  339. emit new_item(title, dateTimeString, broadcast,
  340. status, astart, astop);
  341. }
  342. auto nextPageToken =
  343. broadcasts["nextPageToken"].string_value();
  344. if (nextPageToken.empty() || items.empty())
  345. break;
  346. else {
  347. if (!pending)
  348. return;
  349. if (!apiYouTube->GetBroadcastsList(
  350. broadcasts,
  351. QString::fromStdString(
  352. nextPageToken),
  353. broadcastStatus)) {
  354. emit failed();
  355. return;
  356. }
  357. }
  358. }
  359. }
  360. emit ready();
  361. }
  362. void OBSYoutubeActions::UpdateOkButtonStatus()
  363. {
  364. bool enable = false;
  365. if (ui->tabWidget->currentIndex() == 0) {
  366. enable = !ui->title->text().isEmpty() &&
  367. !ui->privacyBox->currentText().isEmpty() &&
  368. (ui->yesMakeForKids->isChecked() ||
  369. ui->notMakeForKids->isChecked());
  370. ui->okButton->setEnabled(enable);
  371. ui->saveButton->setEnabled(enable);
  372. if (ui->checkScheduledLater->checkState() == Qt::Checked) {
  373. ui->okButton->setText(
  374. QTStr("YouTube.Actions.Create_Schedule"));
  375. ui->saveButton->setText(
  376. QTStr("YouTube.Actions.Create_Schedule_Ready"));
  377. } else {
  378. ui->okButton->setText(
  379. QTStr("YouTube.Actions.Create_GoLive"));
  380. ui->saveButton->setText(
  381. QTStr("YouTube.Actions.Create_Ready"));
  382. }
  383. ui->pushButton->setVisible(false);
  384. } else {
  385. enable = !selectedBroadcast.isEmpty();
  386. ui->okButton->setEnabled(enable);
  387. ui->saveButton->setEnabled(enable);
  388. ui->okButton->setText(QTStr("YouTube.Actions.Choose_GoLive"));
  389. ui->saveButton->setText(QTStr("YouTube.Actions.Choose_Ready"));
  390. ui->pushButton->setVisible(true);
  391. }
  392. }
  393. bool OBSYoutubeActions::CreateEventAction(YoutubeApiWrappers *api,
  394. BroadcastDescription &broadcast,
  395. StreamDescription &stream,
  396. bool stream_later,
  397. bool ready_broadcast)
  398. {
  399. YoutubeApiWrappers *apiYouTube = api;
  400. UiToBroadcast(broadcast);
  401. if (stream_later) {
  402. // DateTime parser means that input datetime is a local, so we need to move it
  403. auto dateTime = ui->scheduledTime->dateTime();
  404. auto utcDTime = dateTime.addSecs(-dateTime.offsetFromUtc());
  405. broadcast.schedul_date_time =
  406. utcDTime.toString(SchedulDateAndTimeFormat);
  407. } else {
  408. // stream now is always autostart/autostop
  409. broadcast.auto_start = true;
  410. broadcast.auto_stop = true;
  411. broadcast.schedul_date_time =
  412. QDateTime::currentDateTimeUtc().toString(
  413. SchedulDateAndTimeFormat);
  414. }
  415. autostart = broadcast.auto_start;
  416. autostop = broadcast.auto_stop;
  417. blog(LOG_DEBUG, "Scheduled date and time: %s",
  418. broadcast.schedul_date_time.toStdString().c_str());
  419. if (!apiYouTube->InsertBroadcast(broadcast)) {
  420. blog(LOG_DEBUG, "No broadcast created.");
  421. return false;
  422. }
  423. if (!apiYouTube->SetVideoCategory(broadcast.id, broadcast.title,
  424. broadcast.description,
  425. broadcast.category.id)) {
  426. blog(LOG_DEBUG, "No category set.");
  427. return false;
  428. }
  429. if (!thumbnailFile.isEmpty()) {
  430. blog(LOG_INFO, "Uploading thumbnail file \"%s\"...",
  431. thumbnailFile.toStdString().c_str());
  432. if (!apiYouTube->SetVideoThumbnail(broadcast.id,
  433. thumbnailFile)) {
  434. blog(LOG_DEBUG, "No thumbnail set.");
  435. return false;
  436. }
  437. }
  438. if (!stream_later || ready_broadcast) {
  439. stream = {"", "", "OBS Studio Video Stream"};
  440. if (!apiYouTube->InsertStream(stream)) {
  441. blog(LOG_DEBUG, "No stream created.");
  442. return false;
  443. }
  444. json11::Json json;
  445. if (!apiYouTube->BindStream(broadcast.id, stream.id, json)) {
  446. blog(LOG_DEBUG, "No stream binded.");
  447. return false;
  448. }
  449. if (broadcast.privacy != "private") {
  450. const std::string apiLiveChatId =
  451. json["snippet"]["liveChatId"].string_value();
  452. apiYouTube->SetChatId(broadcast.id, apiLiveChatId);
  453. } else {
  454. apiYouTube->ResetChat();
  455. }
  456. }
  457. #ifdef YOUTUBE_ENABLED
  458. if (OBSBasic::Get()->GetYouTubeAppDock())
  459. OBSBasic::Get()->GetYouTubeAppDock()->BroadcastCreated(
  460. broadcast.id.toStdString().c_str());
  461. #endif
  462. return true;
  463. }
  464. bool OBSYoutubeActions::ChooseAnEventAction(YoutubeApiWrappers *api,
  465. StreamDescription &stream)
  466. {
  467. YoutubeApiWrappers *apiYouTube = api;
  468. json11::Json json;
  469. if (!apiYouTube->FindBroadcast(selectedBroadcast, json)) {
  470. blog(LOG_DEBUG, "No broadcast found.");
  471. return false;
  472. }
  473. std::string boundStreamId =
  474. json["items"]
  475. .array_items()[0]["contentDetails"]["boundStreamId"]
  476. .string_value();
  477. std::string broadcastPrivacy =
  478. json["items"]
  479. .array_items()[0]["status"]["privacyStatus"]
  480. .string_value();
  481. std::string apiLiveChatId =
  482. json["items"]
  483. .array_items()[0]["snippet"]["liveChatId"]
  484. .string_value();
  485. stream.id = boundStreamId.c_str();
  486. if (!stream.id.isEmpty() && apiYouTube->FindStream(stream.id, json)) {
  487. auto item = json["items"].array_items()[0];
  488. auto streamName = item["cdn"]["ingestionInfo"]["streamName"]
  489. .string_value();
  490. auto title = item["snippet"]["title"].string_value();
  491. stream.name = streamName.c_str();
  492. stream.title = title.c_str();
  493. api->SetBroadcastId(selectedBroadcast);
  494. } else {
  495. stream = {"", "", "OBS Studio Video Stream"};
  496. if (!apiYouTube->InsertStream(stream)) {
  497. blog(LOG_DEBUG, "No stream created.");
  498. return false;
  499. }
  500. if (!apiYouTube->BindStream(selectedBroadcast, stream.id,
  501. json)) {
  502. blog(LOG_DEBUG, "No stream binded.");
  503. return false;
  504. }
  505. }
  506. if (broadcastPrivacy != "private")
  507. apiYouTube->SetChatId(selectedBroadcast, apiLiveChatId);
  508. else
  509. apiYouTube->ResetChat();
  510. #ifdef YOUTUBE_ENABLED
  511. if (OBSBasic::Get()->GetYouTubeAppDock())
  512. OBSBasic::Get()->GetYouTubeAppDock()->BroadcastSelected(
  513. selectedBroadcast.toStdString().c_str());
  514. #endif
  515. return true;
  516. }
  517. void OBSYoutubeActions::ShowErrorDialog(QWidget *parent, QString text)
  518. {
  519. QMessageBox dlg(parent);
  520. dlg.setWindowFlags(dlg.windowFlags() & ~Qt::WindowCloseButtonHint);
  521. dlg.setWindowTitle(QTStr("YouTube.Actions.Error.Title"));
  522. dlg.setText(text);
  523. dlg.setTextFormat(Qt::RichText);
  524. dlg.setIcon(QMessageBox::Warning);
  525. dlg.setStandardButtons(QMessageBox::StandardButton::Ok);
  526. dlg.exec();
  527. }
  528. void OBSYoutubeActions::InitBroadcast()
  529. {
  530. BroadcastDescription broadcast;
  531. StreamDescription stream;
  532. QMessageBox msgBox(this);
  533. msgBox.setWindowFlags(msgBox.windowFlags() &
  534. ~Qt::WindowCloseButtonHint);
  535. msgBox.setWindowTitle(QTStr("YouTube.Actions.Notify.Title"));
  536. msgBox.setText(QTStr("YouTube.Actions.Notify.CreatingBroadcast"));
  537. msgBox.setStandardButtons(QMessageBox::StandardButtons());
  538. bool success = false;
  539. auto action = [&]() {
  540. if (ui->tabWidget->currentIndex() == 0) {
  541. success = this->CreateEventAction(
  542. apiYouTube, broadcast, stream,
  543. ui->checkScheduledLater->isChecked());
  544. } else {
  545. success = this->ChooseAnEventAction(apiYouTube, stream);
  546. if (success)
  547. broadcast.id = this->selectedBroadcast;
  548. };
  549. QMetaObject::invokeMethod(&msgBox, "accept",
  550. Qt::QueuedConnection);
  551. };
  552. QScopedPointer<QThread> thread(CreateQThread(action));
  553. thread->start();
  554. msgBox.exec();
  555. thread->wait();
  556. if (success) {
  557. if (ui->tabWidget->currentIndex() == 0) {
  558. // Stream later usecase.
  559. if (ui->checkScheduledLater->isChecked()) {
  560. QMessageBox msg(this);
  561. msg.setWindowTitle(QTStr(
  562. "YouTube.Actions.EventCreated.Title"));
  563. msg.setText(QTStr(
  564. "YouTube.Actions.EventCreated.Text"));
  565. msg.setStandardButtons(QMessageBox::Ok);
  566. msg.exec();
  567. // Close dialog without start streaming.
  568. Cancel();
  569. } else {
  570. // Stream now usecase.
  571. blog(LOG_DEBUG, "New valid stream: %s",
  572. QT_TO_UTF8(stream.name));
  573. emit ok(QT_TO_UTF8(broadcast.id),
  574. QT_TO_UTF8(stream.id),
  575. QT_TO_UTF8(stream.name), true, true,
  576. true);
  577. Accept();
  578. }
  579. } else {
  580. // Stream to precreated broadcast usecase.
  581. emit ok(QT_TO_UTF8(broadcast.id), QT_TO_UTF8(stream.id),
  582. QT_TO_UTF8(stream.name), autostart, autostop,
  583. true);
  584. Accept();
  585. }
  586. } else {
  587. // Fail.
  588. auto last_error = apiYouTube->GetLastError();
  589. if (last_error.isEmpty())
  590. last_error = QTStr("YouTube.Actions.Error.YouTubeApi");
  591. if (!apiYouTube->GetTranslatedError(last_error))
  592. last_error =
  593. QTStr("YouTube.Actions.Error.NoBroadcastCreated")
  594. .arg(last_error);
  595. ShowErrorDialog(this, last_error);
  596. }
  597. }
  598. void OBSYoutubeActions::ReadyBroadcast()
  599. {
  600. BroadcastDescription broadcast;
  601. StreamDescription stream;
  602. QMessageBox msgBox(this);
  603. msgBox.setWindowFlags(msgBox.windowFlags() &
  604. ~Qt::WindowCloseButtonHint);
  605. msgBox.setWindowTitle(QTStr("YouTube.Actions.Notify.Title"));
  606. msgBox.setText(QTStr("YouTube.Actions.Notify.CreatingBroadcast"));
  607. msgBox.setStandardButtons(QMessageBox::StandardButtons());
  608. bool success = false;
  609. auto action = [&]() {
  610. if (ui->tabWidget->currentIndex() == 0) {
  611. success = this->CreateEventAction(
  612. apiYouTube, broadcast, stream,
  613. ui->checkScheduledLater->isChecked(), true);
  614. } else {
  615. success = this->ChooseAnEventAction(apiYouTube, stream);
  616. if (success)
  617. broadcast.id = this->selectedBroadcast;
  618. };
  619. QMetaObject::invokeMethod(&msgBox, "accept",
  620. Qt::QueuedConnection);
  621. };
  622. QScopedPointer<QThread> thread(CreateQThread(action));
  623. thread->start();
  624. msgBox.exec();
  625. thread->wait();
  626. if (success) {
  627. emit ok(QT_TO_UTF8(broadcast.id), QT_TO_UTF8(stream.id),
  628. QT_TO_UTF8(stream.name), autostart, autostop, false);
  629. Accept();
  630. } else {
  631. // Fail.
  632. auto last_error = apiYouTube->GetLastError();
  633. if (last_error.isEmpty())
  634. last_error = QTStr("YouTube.Actions.Error.YouTubeApi");
  635. if (!apiYouTube->GetTranslatedError(last_error))
  636. last_error =
  637. QTStr("YouTube.Actions.Error.NoBroadcastCreated")
  638. .arg(last_error);
  639. ShowErrorDialog(this, last_error);
  640. }
  641. }
  642. void OBSYoutubeActions::UiToBroadcast(BroadcastDescription &broadcast)
  643. {
  644. broadcast.title = ui->title->text();
  645. // ToDo: UI warning rather than silent truncation
  646. broadcast.description = ui->description->toPlainText().left(5000);
  647. broadcast.privacy = ui->privacyBox->currentData().toString();
  648. broadcast.category.title = ui->categoryBox->currentText();
  649. broadcast.category.id = ui->categoryBox->currentData().toString();
  650. broadcast.made_for_kids = ui->yesMakeForKids->isChecked();
  651. broadcast.latency = ui->latencyBox->currentData().toString();
  652. broadcast.auto_start = ui->checkAutoStart->isChecked();
  653. broadcast.auto_stop = ui->checkAutoStop->isChecked();
  654. broadcast.dvr = ui->checkDVR->isChecked();
  655. broadcast.schedul_for_later = ui->checkScheduledLater->isChecked();
  656. broadcast.projection = ui->check360Video->isChecked() ? "360"
  657. : "rectangular";
  658. if (ui->checkRememberSettings->isChecked())
  659. SaveSettings(broadcast);
  660. }
  661. void OBSYoutubeActions::SaveSettings(BroadcastDescription &broadcast)
  662. {
  663. OBSBasic *main = OBSBasic::Get();
  664. config_set_string(main->activeConfiguration, "YouTube", "Title",
  665. QT_TO_UTF8(broadcast.title));
  666. config_set_string(main->activeConfiguration, "YouTube", "Description",
  667. QT_TO_UTF8(broadcast.description));
  668. config_set_string(main->activeConfiguration, "YouTube", "Privacy",
  669. QT_TO_UTF8(broadcast.privacy));
  670. config_set_string(main->activeConfiguration, "YouTube", "CategoryID",
  671. QT_TO_UTF8(broadcast.category.id));
  672. config_set_string(main->activeConfiguration, "YouTube", "Latency",
  673. QT_TO_UTF8(broadcast.latency));
  674. config_set_bool(main->activeConfiguration, "YouTube", "MadeForKids",
  675. broadcast.made_for_kids);
  676. config_set_bool(main->activeConfiguration, "YouTube", "AutoStart",
  677. broadcast.auto_start);
  678. config_set_bool(main->activeConfiguration, "YouTube", "AutoStop",
  679. broadcast.auto_start);
  680. config_set_bool(main->activeConfiguration, "YouTube", "DVR",
  681. broadcast.dvr);
  682. config_set_bool(main->activeConfiguration, "YouTube",
  683. "ScheduleForLater", broadcast.schedul_for_later);
  684. config_set_string(main->activeConfiguration, "YouTube", "Projection",
  685. QT_TO_UTF8(broadcast.projection));
  686. config_set_string(main->activeConfiguration, "YouTube", "ThumbnailFile",
  687. QT_TO_UTF8(thumbnailFile));
  688. config_set_bool(main->activeConfiguration, "YouTube",
  689. "RememberSettings", true);
  690. }
  691. void OBSYoutubeActions::LoadSettings()
  692. {
  693. OBSBasic *main = OBSBasic::Get();
  694. const char *title = config_get_string(main->activeConfiguration,
  695. "YouTube", "Title");
  696. ui->title->setText(QT_UTF8(title));
  697. const char *desc = config_get_string(main->activeConfiguration,
  698. "YouTube", "Description");
  699. ui->description->setPlainText(QT_UTF8(desc));
  700. const char *priv = config_get_string(main->activeConfiguration,
  701. "YouTube", "Privacy");
  702. int index = ui->privacyBox->findData(priv);
  703. ui->privacyBox->setCurrentIndex(index);
  704. const char *catID = config_get_string(main->activeConfiguration,
  705. "YouTube", "CategoryID");
  706. index = ui->categoryBox->findData(catID);
  707. ui->categoryBox->setCurrentIndex(index);
  708. const char *latency = config_get_string(main->activeConfiguration,
  709. "YouTube", "Latency");
  710. index = ui->latencyBox->findData(latency);
  711. ui->latencyBox->setCurrentIndex(index);
  712. bool dvr = config_get_bool(main->activeConfiguration, "YouTube", "DVR");
  713. ui->checkDVR->setChecked(dvr);
  714. bool forKids = config_get_bool(main->activeConfiguration, "YouTube",
  715. "MadeForKids");
  716. if (forKids)
  717. ui->yesMakeForKids->setChecked(true);
  718. else
  719. ui->notMakeForKids->setChecked(true);
  720. bool schedLater = config_get_bool(main->activeConfiguration, "YouTube",
  721. "ScheduleForLater");
  722. ui->checkScheduledLater->setChecked(schedLater);
  723. bool autoStart = config_get_bool(main->activeConfiguration, "YouTube",
  724. "AutoStart");
  725. ui->checkAutoStart->setChecked(autoStart);
  726. bool autoStop = config_get_bool(main->activeConfiguration, "YouTube",
  727. "AutoStop");
  728. ui->checkAutoStop->setChecked(autoStop);
  729. const char *projection = config_get_string(main->activeConfiguration,
  730. "YouTube", "Projection");
  731. if (projection && *projection) {
  732. if (strcmp(projection, "360") == 0)
  733. ui->check360Video->setChecked(true);
  734. else
  735. ui->check360Video->setChecked(false);
  736. }
  737. const char *thumbFile = config_get_string(main->activeConfiguration,
  738. "YouTube", "ThumbnailFile");
  739. if (thumbFile && *thumbFile) {
  740. QFileInfo tFile(thumbFile);
  741. // Re-check validity before setting path again
  742. if (tFile.exists() && tFile.size() <= 2 * 1024 * 1024) {
  743. thumbnailFile = tFile.absoluteFilePath();
  744. ui->selectedFileName->setText(thumbnailFile);
  745. ui->selectFileButton->setText(
  746. QTStr("YouTube.Actions.Thumbnail.ClearFile"));
  747. QImageReader imgReader(thumbnailFile);
  748. imgReader.setAutoTransform(true);
  749. const QImage newImage = imgReader.read();
  750. ui->thumbnailPreview->setPixmap(
  751. QPixmap::fromImage(newImage).scaled(
  752. 160, 90, Qt::KeepAspectRatio,
  753. Qt::SmoothTransformation));
  754. }
  755. }
  756. }
  757. void OBSYoutubeActions::OpenYouTubeDashboard()
  758. {
  759. ChannelDescription channel;
  760. if (!apiYouTube->GetChannelDescription(channel)) {
  761. blog(LOG_DEBUG, "Could not get channel description.");
  762. ShowErrorDialog(
  763. this,
  764. apiYouTube->GetLastError().isEmpty()
  765. ? QTStr("YouTube.Actions.Error.General")
  766. : QTStr("YouTube.Actions.Error.Text")
  767. .arg(apiYouTube->GetLastError()));
  768. return;
  769. }
  770. //https://studio.youtube.com/channel/UCA9bSfH3KL186kyiUsvi3IA/videos/live?filter=%5B%5D&sort=%7B%22columnType%22%3A%22date%22%2C%22sortOrder%22%3A%22DESCENDING%22%7D
  771. QString uri =
  772. QString("https://studio.youtube.com/channel/%1/videos/live?filter=[]&sort={\"columnType\"%3A\"date\"%2C\"sortOrder\"%3A\"DESCENDING\"}")
  773. .arg(channel.id);
  774. QDesktopServices::openUrl(uri);
  775. }
  776. void OBSYoutubeActions::Cancel()
  777. {
  778. workerThread->stop();
  779. reject();
  780. }
  781. void OBSYoutubeActions::Accept()
  782. {
  783. workerThread->stop();
  784. accept();
  785. }