OBSBasic_YouTube.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. Zachary Lund <[email protected]>
  4. Philippe Groarke <[email protected]>
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ******************************************************************************/
  16. #include "OBSBasic.hpp"
  17. #ifdef YOUTUBE_ENABLED
  18. #include <dialogs/OBSYoutubeActions.hpp>
  19. #include <docks/YouTubeAppDock.hpp>
  20. #include <utility/YoutubeApiWrappers.hpp>
  21. #endif
  22. #include <qt-wrappers.hpp>
  23. using namespace std;
  24. extern bool cef_js_avail;
  25. #ifdef YOUTUBE_ENABLED
  26. void OBSBasic::YouTubeActionDialogOk(const QString &broadcast_id, const QString &stream_id, const QString &key,
  27. bool autostart, bool autostop, bool start_now)
  28. {
  29. //blog(LOG_DEBUG, "Stream key: %s", QT_TO_UTF8(key));
  30. obs_service_t *service_obj = GetService();
  31. OBSDataAutoRelease settings = obs_service_get_settings(service_obj);
  32. const std::string a_key = QT_TO_UTF8(key);
  33. obs_data_set_string(settings, "key", a_key.c_str());
  34. const std::string b_id = QT_TO_UTF8(broadcast_id);
  35. obs_data_set_string(settings, "broadcast_id", b_id.c_str());
  36. const std::string s_id = QT_TO_UTF8(stream_id);
  37. obs_data_set_string(settings, "stream_id", s_id.c_str());
  38. obs_service_update(service_obj, settings);
  39. autoStartBroadcast = autostart;
  40. autoStopBroadcast = autostop;
  41. broadcastReady = true;
  42. emit BroadcastStreamReady(broadcastReady);
  43. if (start_now)
  44. QMetaObject::invokeMethod(this, "StartStreaming");
  45. }
  46. void OBSBasic::YoutubeStreamCheck(const std::string &key)
  47. {
  48. YoutubeApiWrappers *apiYouTube(dynamic_cast<YoutubeApiWrappers *>(GetAuth()));
  49. if (!apiYouTube) {
  50. /* technically we should never get here -Lain */
  51. QMetaObject::invokeMethod(this, "ForceStopStreaming", Qt::QueuedConnection);
  52. youtubeStreamCheckThread->deleteLater();
  53. blog(LOG_ERROR, "==========================================");
  54. blog(LOG_ERROR, "%s: Uh, hey, we got here", __FUNCTION__);
  55. blog(LOG_ERROR, "==========================================");
  56. return;
  57. }
  58. int timeout = 0;
  59. json11::Json json;
  60. QString id = key.c_str();
  61. while (StreamingActive()) {
  62. if (timeout == 14) {
  63. QMetaObject::invokeMethod(this, "ForceStopStreaming", Qt::QueuedConnection);
  64. break;
  65. }
  66. if (!apiYouTube->FindStream(id, json)) {
  67. QMetaObject::invokeMethod(this, "DisplayStreamStartError", Qt::QueuedConnection);
  68. QMetaObject::invokeMethod(this, "StopStreaming", Qt::QueuedConnection);
  69. break;
  70. }
  71. auto item = json["items"][0];
  72. auto status = item["status"]["streamStatus"].string_value();
  73. if (status == "active") {
  74. emit BroadcastStreamActive();
  75. break;
  76. } else {
  77. QThread::sleep(1);
  78. timeout++;
  79. }
  80. }
  81. youtubeStreamCheckThread->deleteLater();
  82. }
  83. void OBSBasic::ShowYouTubeAutoStartWarning()
  84. {
  85. auto msgBox = []() {
  86. QMessageBox msgbox(App()->GetMainWindow());
  87. msgbox.setWindowTitle(QTStr("YouTube.Actions.AutoStartStreamingWarning.Title"));
  88. msgbox.setText(QTStr("YouTube.Actions.AutoStartStreamingWarning"));
  89. msgbox.setIcon(QMessageBox::Icon::Information);
  90. msgbox.addButton(QMessageBox::Ok);
  91. QCheckBox *cb = new QCheckBox(QTStr("DoNotShowAgain"));
  92. msgbox.setCheckBox(cb);
  93. msgbox.exec();
  94. if (cb->isChecked()) {
  95. config_set_bool(App()->GetUserConfig(), "General", "WarnedAboutYouTubeAutoStart", true);
  96. config_save_safe(App()->GetUserConfig(), "tmp", nullptr);
  97. }
  98. };
  99. bool warned = config_get_bool(App()->GetUserConfig(), "General", "WarnedAboutYouTubeAutoStart");
  100. if (!warned) {
  101. QMetaObject::invokeMethod(App(), "Exec", Qt::QueuedConnection, Q_ARG(VoidFunc, msgBox));
  102. }
  103. }
  104. #endif
  105. void OBSBasic::BroadcastButtonClicked()
  106. {
  107. if (!broadcastReady || (!broadcastActive && !outputHandler->StreamingActive())) {
  108. SetupBroadcast();
  109. return;
  110. }
  111. if (!autoStartBroadcast) {
  112. #ifdef YOUTUBE_ENABLED
  113. std::shared_ptr<YoutubeApiWrappers> ytAuth = dynamic_pointer_cast<YoutubeApiWrappers>(auth);
  114. if (ytAuth.get()) {
  115. if (!ytAuth->StartLatestBroadcast()) {
  116. auto last_error = ytAuth->GetLastError();
  117. if (last_error.isEmpty())
  118. last_error = QTStr("YouTube.Actions.Error.YouTubeApi");
  119. if (!ytAuth->GetTranslatedError(last_error))
  120. last_error = QTStr("YouTube.Actions.Error.BroadcastTransitionFailed")
  121. .arg(last_error, ytAuth->GetBroadcastId());
  122. OBSMessageBox::warning(this, QTStr("Output.BroadcastStartFailed"), last_error, true);
  123. return;
  124. }
  125. }
  126. #endif
  127. broadcastActive = true;
  128. autoStartBroadcast = true; // and clear the flag
  129. emit BroadcastStreamStarted(autoStopBroadcast);
  130. } else if (!autoStopBroadcast) {
  131. #ifdef YOUTUBE_ENABLED
  132. bool confirm = config_get_bool(App()->GetUserConfig(), "BasicWindow", "WarnBeforeStoppingStream");
  133. if (confirm && isVisible()) {
  134. QMessageBox::StandardButton button = OBSMessageBox::question(
  135. this, QTStr("ConfirmStop.Title"), QTStr("YouTube.Actions.AutoStopStreamingWarning"),
  136. QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
  137. if (button == QMessageBox::No)
  138. return;
  139. }
  140. std::shared_ptr<YoutubeApiWrappers> ytAuth = dynamic_pointer_cast<YoutubeApiWrappers>(auth);
  141. if (ytAuth.get()) {
  142. if (!ytAuth->StopLatestBroadcast()) {
  143. auto last_error = ytAuth->GetLastError();
  144. if (last_error.isEmpty())
  145. last_error = QTStr("YouTube.Actions.Error.YouTubeApi");
  146. if (!ytAuth->GetTranslatedError(last_error))
  147. last_error = QTStr("YouTube.Actions.Error.BroadcastTransitionFailed")
  148. .arg(last_error, ytAuth->GetBroadcastId());
  149. OBSMessageBox::warning(this, QTStr("Output.BroadcastStopFailed"), last_error, true);
  150. }
  151. }
  152. #endif
  153. broadcastActive = false;
  154. broadcastReady = false;
  155. autoStopBroadcast = true;
  156. QMetaObject::invokeMethod(this, "StopStreaming");
  157. emit BroadcastStreamReady(broadcastReady);
  158. SetBroadcastFlowEnabled(true);
  159. }
  160. }
  161. void OBSBasic::SetBroadcastFlowEnabled(bool enabled)
  162. {
  163. emit BroadcastFlowEnabled(enabled);
  164. }
  165. void OBSBasic::SetupBroadcast()
  166. {
  167. #ifdef YOUTUBE_ENABLED
  168. Auth *const auth = GetAuth();
  169. if (IsYouTubeService(auth->service())) {
  170. OBSYoutubeActions dialog(this, auth, broadcastReady);
  171. connect(&dialog, &OBSYoutubeActions::ok, this, &OBSBasic::YouTubeActionDialogOk);
  172. dialog.exec();
  173. }
  174. #endif
  175. }
  176. #ifdef YOUTUBE_ENABLED
  177. YouTubeAppDock *OBSBasic::GetYouTubeAppDock()
  178. {
  179. return youtubeAppDock;
  180. }
  181. #ifndef SEC_TO_NSEC
  182. #define SEC_TO_NSEC 1000000000
  183. #endif
  184. void OBSBasic::NewYouTubeAppDock()
  185. {
  186. if (!cef_js_avail)
  187. return;
  188. /* make sure that the youtube app dock can't be immediately recreated.
  189. * dumb hack. blame chromium. or this particular dock. or both. if CEF
  190. * creates/destroys/creates a widget too quickly it can lead to a
  191. * crash. */
  192. uint64_t ts = os_gettime_ns();
  193. if ((ts - lastYouTubeAppDockCreationTime) < (5ULL * SEC_TO_NSEC))
  194. return;
  195. lastYouTubeAppDockCreationTime = ts;
  196. if (youtubeAppDock)
  197. RemoveDockWidget(youtubeAppDock->objectName());
  198. youtubeAppDock = new YouTubeAppDock("YouTube Live Control Panel");
  199. }
  200. void OBSBasic::DeleteYouTubeAppDock()
  201. {
  202. if (!cef_js_avail)
  203. return;
  204. if (youtubeAppDock)
  205. RemoveDockWidget(youtubeAppDock->objectName());
  206. youtubeAppDock = nullptr;
  207. }
  208. #endif