1
0

YoutubeApiWrappers.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. #include "YoutubeApiWrappers.hpp"
  2. #include <utility/RemoteTextThread.hpp>
  3. #include <utility/obf.h>
  4. #include <widgets/OBSBasic.hpp>
  5. #include <qt-wrappers.hpp>
  6. #include <ui-config.h>
  7. #include <QFile>
  8. #include <QMimeDatabase>
  9. #include "moc_YoutubeApiWrappers.cpp"
  10. using namespace json11;
  11. /* ------------------------------------------------------------------------- */
  12. #define YOUTUBE_LIVE_API_URL "https://www.googleapis.com/youtube/v3"
  13. #define YOUTUBE_LIVE_STREAM_URL YOUTUBE_LIVE_API_URL "/liveStreams"
  14. #define YOUTUBE_LIVE_BROADCAST_URL YOUTUBE_LIVE_API_URL "/liveBroadcasts"
  15. #define YOUTUBE_LIVE_BROADCAST_TRANSITION_URL YOUTUBE_LIVE_BROADCAST_URL "/transition"
  16. #define YOUTUBE_LIVE_BROADCAST_BIND_URL YOUTUBE_LIVE_BROADCAST_URL "/bind"
  17. #define YOUTUBE_LIVE_CHANNEL_URL YOUTUBE_LIVE_API_URL "/channels"
  18. #define YOUTUBE_LIVE_TOKEN_URL "https://oauth2.googleapis.com/token"
  19. #define YOUTUBE_LIVE_VIDEOCATEGORIES_URL YOUTUBE_LIVE_API_URL "/videoCategories"
  20. #define YOUTUBE_LIVE_VIDEOS_URL YOUTUBE_LIVE_API_URL "/videos"
  21. #define YOUTUBE_LIVE_THUMBNAIL_URL "https://www.googleapis.com/upload/youtube/v3/thumbnails/set"
  22. #define DEFAULT_BROADCASTS_PER_QUERY "50" // acceptable values are 0 to 50, inclusive
  23. /* ------------------------------------------------------------------------- */
  24. bool IsYouTubeService(const std::string &service)
  25. {
  26. auto it = find_if(youtubeServices.begin(), youtubeServices.end(),
  27. [&service](const Auth::Def &yt) { return service == yt.service; });
  28. return it != youtubeServices.end();
  29. }
  30. bool IsUserSignedIntoYT()
  31. {
  32. Auth *auth = OBSBasic::Get()->GetAuth();
  33. if (auth) {
  34. YoutubeApiWrappers *apiYouTube(dynamic_cast<YoutubeApiWrappers *>(auth));
  35. if (apiYouTube) {
  36. return true;
  37. }
  38. }
  39. return false;
  40. }
  41. bool YoutubeApiWrappers::GetTranslatedError(QString &error_message)
  42. {
  43. QString translated = QTStr("YouTube.Errors." + lastErrorReason.toUtf8());
  44. // No translation found
  45. if (translated.startsWith("YouTube.Errors."))
  46. return false;
  47. error_message = translated;
  48. return true;
  49. }
  50. YoutubeApiWrappers::YoutubeApiWrappers(const Def &d) : YoutubeAuth(d) {}
  51. bool YoutubeApiWrappers::TryInsertCommand(const char *url, const char *content_type, std::string request_type,
  52. const char *data, Json &json_out, long *error_code, int data_size)
  53. {
  54. long httpStatusCode = 0;
  55. #ifdef _DEBUG
  56. blog(LOG_DEBUG, "YouTube API command URL: %s", url);
  57. if (data && data[0] == '{') // only log JSON data
  58. blog(LOG_DEBUG, "YouTube API command data: %s", data);
  59. #endif
  60. if (token.empty())
  61. return false;
  62. std::string output;
  63. std::string error;
  64. // Increase timeout by the time it takes to transfer `data_size` at 1 Mbps
  65. int timeout = 60 + data_size / 125000;
  66. bool success = GetRemoteFile(url, output, error, &httpStatusCode, content_type, request_type, data,
  67. {"Authorization: Bearer " + token}, nullptr, timeout, false, data_size);
  68. if (error_code)
  69. *error_code = httpStatusCode;
  70. if (!success || output.empty()) {
  71. if (!error.empty())
  72. blog(LOG_WARNING, "YouTube API request failed: %s", error.c_str());
  73. return false;
  74. }
  75. json_out = Json::parse(output, error);
  76. #ifdef _DEBUG
  77. blog(LOG_DEBUG, "YouTube API command answer: %s", json_out.dump().c_str());
  78. #endif
  79. if (!error.empty()) {
  80. return false;
  81. }
  82. return httpStatusCode < 400;
  83. }
  84. bool YoutubeApiWrappers::UpdateAccessToken()
  85. {
  86. if (refresh_token.empty()) {
  87. return false;
  88. }
  89. std::string clientid = YOUTUBE_CLIENTID;
  90. std::string secret = YOUTUBE_SECRET;
  91. deobfuscate_str(&clientid[0], YOUTUBE_CLIENTID_HASH);
  92. deobfuscate_str(&secret[0], YOUTUBE_SECRET_HASH);
  93. std::string r_token = QUrl::toPercentEncoding(refresh_token.c_str()).toStdString();
  94. const QString url = YOUTUBE_LIVE_TOKEN_URL;
  95. const QString data_template = "client_id=%1"
  96. "&client_secret=%2"
  97. "&refresh_token=%3"
  98. "&grant_type=refresh_token";
  99. const QString data =
  100. data_template.arg(QString(clientid.c_str()), QString(secret.c_str()), QString(r_token.c_str()));
  101. Json json_out;
  102. bool success =
  103. TryInsertCommand(QT_TO_UTF8(url), "application/x-www-form-urlencoded", "", QT_TO_UTF8(data), json_out);
  104. if (!success || json_out.object_items().find("error") != json_out.object_items().end())
  105. return false;
  106. token = json_out["access_token"].string_value();
  107. return token.empty() ? false : true;
  108. }
  109. bool YoutubeApiWrappers::InsertCommand(const char *url, const char *content_type, std::string request_type,
  110. const char *data, Json &json_out, int data_size)
  111. {
  112. long error_code;
  113. bool success = TryInsertCommand(url, content_type, request_type, data, json_out, &error_code, data_size);
  114. if (error_code == 401) {
  115. // Attempt to update access token and try again
  116. if (!UpdateAccessToken())
  117. return false;
  118. success = TryInsertCommand(url, content_type, request_type, data, json_out, &error_code, data_size);
  119. }
  120. if (json_out.object_items().find("error") != json_out.object_items().end()) {
  121. blog(LOG_ERROR, "YouTube API error:\n\tHTTP status: %ld\n\tURL: %s\n\tJSON: %s", error_code, url,
  122. json_out.dump().c_str());
  123. lastError = json_out["error"]["code"].int_value();
  124. lastErrorReason = QString(json_out["error"]["errors"][0]["reason"].string_value().c_str());
  125. lastErrorMessage = QString(json_out["error"]["message"].string_value().c_str());
  126. // The existence of an error implies non-success even if the HTTP status code disagrees.
  127. success = false;
  128. }
  129. return success;
  130. }
  131. bool YoutubeApiWrappers::GetChannelDescription(ChannelDescription &channel_description)
  132. {
  133. lastErrorMessage.clear();
  134. lastErrorReason.clear();
  135. const QByteArray url = YOUTUBE_LIVE_CHANNEL_URL "?part=snippet,contentDetails,statistics"
  136. "&mine=true";
  137. Json json_out;
  138. if (!InsertCommand(url, "application/json", "", nullptr, json_out)) {
  139. return false;
  140. }
  141. if (json_out["pageInfo"]["totalResults"].int_value() == 0) {
  142. lastErrorMessage = QTStr("YouTube.Auth.NoChannels");
  143. return false;
  144. }
  145. channel_description.id = QString(json_out["items"][0]["id"].string_value().c_str());
  146. channel_description.title = QString(json_out["items"][0]["snippet"]["title"].string_value().c_str());
  147. return channel_description.id.isEmpty() ? false : true;
  148. }
  149. bool YoutubeApiWrappers::InsertBroadcast(BroadcastDescription &broadcast)
  150. {
  151. lastErrorMessage.clear();
  152. lastErrorReason.clear();
  153. const QByteArray url = YOUTUBE_LIVE_BROADCAST_URL "?part=snippet,status,contentDetails";
  154. const Json data = Json::object{
  155. {"snippet",
  156. Json::object{
  157. {"title", QT_TO_UTF8(broadcast.title)},
  158. {"description", QT_TO_UTF8(broadcast.description)},
  159. {"scheduledStartTime", QT_TO_UTF8(broadcast.schedul_date_time)},
  160. }},
  161. {"status",
  162. Json::object{
  163. {"privacyStatus", QT_TO_UTF8(broadcast.privacy)},
  164. {"selfDeclaredMadeForKids", broadcast.made_for_kids},
  165. }},
  166. {"contentDetails",
  167. Json::object{
  168. {"latencyPreference", QT_TO_UTF8(broadcast.latency)},
  169. {"enableAutoStart", broadcast.auto_start},
  170. {"enableAutoStop", broadcast.auto_stop},
  171. {"enableDvr", broadcast.dvr},
  172. {"projection", QT_TO_UTF8(broadcast.projection)},
  173. {
  174. "monitorStream",
  175. Json::object{
  176. {"enableMonitorStream", false},
  177. },
  178. },
  179. }},
  180. };
  181. Json json_out;
  182. if (!InsertCommand(url, "application/json", "", data.dump().c_str(), json_out)) {
  183. return false;
  184. }
  185. broadcast.id = QString(json_out["id"].string_value().c_str());
  186. return broadcast.id.isEmpty() ? false : true;
  187. }
  188. bool YoutubeApiWrappers::InsertStream(StreamDescription &stream)
  189. {
  190. lastErrorMessage.clear();
  191. lastErrorReason.clear();
  192. const QByteArray url = YOUTUBE_LIVE_STREAM_URL "?part=snippet,cdn,status,contentDetails";
  193. const Json data = Json::object{
  194. {"snippet",
  195. Json::object{
  196. {"title", QT_TO_UTF8(stream.title)},
  197. }},
  198. {"cdn",
  199. Json::object{
  200. {"frameRate", "variable"},
  201. {"ingestionType", "rtmp"},
  202. {"resolution", "variable"},
  203. }},
  204. {"contentDetails", Json::object{{"isReusable", false}}},
  205. };
  206. Json json_out;
  207. if (!InsertCommand(url, "application/json", "", data.dump().c_str(), json_out)) {
  208. return false;
  209. }
  210. stream.id = QString(json_out["id"].string_value().c_str());
  211. stream.name = QString(json_out["cdn"]["ingestionInfo"]["streamName"].string_value().c_str());
  212. return stream.id.isEmpty() ? false : true;
  213. }
  214. bool YoutubeApiWrappers::BindStream(const QString broadcast_id, const QString stream_id)
  215. {
  216. lastErrorMessage.clear();
  217. lastErrorReason.clear();
  218. const QString url_template = YOUTUBE_LIVE_BROADCAST_BIND_URL "?id=%1"
  219. "&streamId=%2"
  220. "&part=id,snippet,contentDetails,status";
  221. const QString url = url_template.arg(broadcast_id, stream_id);
  222. const Json data = Json::object{};
  223. this->broadcast_id = broadcast_id;
  224. Json json_out;
  225. return InsertCommand(QT_TO_UTF8(url), "application/json", "", data.dump().c_str(), json_out);
  226. }
  227. bool YoutubeApiWrappers::GetBroadcastsList(Json &json_out, const QString &page, const QString &status)
  228. {
  229. lastErrorMessage.clear();
  230. lastErrorReason.clear();
  231. QByteArray url = YOUTUBE_LIVE_BROADCAST_URL "?part=snippet,contentDetails,status"
  232. "&broadcastType=all&maxResults=" DEFAULT_BROADCASTS_PER_QUERY;
  233. if (status.isEmpty())
  234. url += "&mine=true";
  235. else
  236. url += "&broadcastStatus=" + status.toUtf8();
  237. if (!page.isEmpty())
  238. url += "&pageToken=" + page.toUtf8();
  239. return InsertCommand(url, "application/json", "", nullptr, json_out);
  240. }
  241. bool YoutubeApiWrappers::GetVideoCategoriesList(QVector<CategoryDescription> &category_list_out)
  242. {
  243. lastErrorMessage.clear();
  244. lastErrorReason.clear();
  245. const QString url_template = YOUTUBE_LIVE_VIDEOCATEGORIES_URL "?part=snippet"
  246. "&regionCode=%1"
  247. "&hl=%2";
  248. /*
  249. * All OBS locale regions aside from "US" are missing category id 29
  250. * ("Nonprofits & Activism"), but it is still available to channels
  251. * set to those regions via the YouTube Studio website.
  252. * To work around this inconsistency with the API all locales will
  253. * use the "US" region and only set the language part for localisation.
  254. * It is worth noting that none of the regions available on YouTube
  255. * feature any category not also available to the "US" region.
  256. */
  257. QString url = url_template.arg("US", QLocale().name());
  258. Json json_out;
  259. if (!InsertCommand(QT_TO_UTF8(url), "application/json", "", nullptr, json_out)) {
  260. if (lastErrorReason != "unsupportedLanguageCode" && lastErrorReason != "invalidLanguage")
  261. return false;
  262. // Try again with en-US if YouTube error indicates an unsupported locale
  263. url = url_template.arg("US", "en_US");
  264. if (!InsertCommand(QT_TO_UTF8(url), "application/json", "", nullptr, json_out))
  265. return false;
  266. }
  267. category_list_out = {};
  268. for (auto &j : json_out["items"].array_items()) {
  269. // Assignable only.
  270. if (j["snippet"]["assignable"].bool_value()) {
  271. category_list_out.push_back(
  272. {j["id"].string_value().c_str(), j["snippet"]["title"].string_value().c_str()});
  273. }
  274. }
  275. return category_list_out.isEmpty() ? false : true;
  276. }
  277. bool YoutubeApiWrappers::SetVideoCategory(const QString &video_id, const QString &video_title,
  278. const QString &video_description, const QString &categorie_id)
  279. {
  280. lastErrorMessage.clear();
  281. lastErrorReason.clear();
  282. const QByteArray url = YOUTUBE_LIVE_VIDEOS_URL "?part=snippet";
  283. const Json data = Json::object{
  284. {"id", QT_TO_UTF8(video_id)},
  285. {"snippet",
  286. Json::object{
  287. {"title", QT_TO_UTF8(video_title)},
  288. {"description", QT_TO_UTF8(video_description)},
  289. {"categoryId", QT_TO_UTF8(categorie_id)},
  290. }},
  291. };
  292. Json json_out;
  293. return InsertCommand(url, "application/json", "PUT", data.dump().c_str(), json_out);
  294. }
  295. bool YoutubeApiWrappers::SetVideoThumbnail(const QString &video_id, const QString &thumbnail_file)
  296. {
  297. lastErrorMessage.clear();
  298. lastErrorReason.clear();
  299. // Make sure the file hasn't been deleted since originally selecting it
  300. if (!QFile::exists(thumbnail_file)) {
  301. lastErrorMessage = QTStr("YouTube.Actions.Error.FileMissing");
  302. return false;
  303. }
  304. QFile thumbFile(thumbnail_file);
  305. if (!thumbFile.open(QFile::ReadOnly)) {
  306. lastErrorMessage = QTStr("YouTube.Actions.Error.FileOpeningFailed");
  307. return false;
  308. }
  309. const QByteArray fileContents = thumbFile.readAll();
  310. const QString mime = QMimeDatabase().mimeTypeForData(fileContents).name();
  311. const QString url = YOUTUBE_LIVE_THUMBNAIL_URL "?videoId=" + video_id;
  312. Json json_out;
  313. return InsertCommand(QT_TO_UTF8(url), QT_TO_UTF8(mime), "POST", fileContents.constData(), json_out,
  314. fileContents.size());
  315. }
  316. bool YoutubeApiWrappers::StartBroadcast(const QString &broadcast_id)
  317. {
  318. lastErrorMessage.clear();
  319. lastErrorReason.clear();
  320. Json json_out;
  321. if (!FindBroadcast(broadcast_id, json_out))
  322. return false;
  323. auto lifeCycleStatus = json_out["items"][0]["status"]["lifeCycleStatus"].string_value();
  324. if (lifeCycleStatus == "live" || lifeCycleStatus == "liveStarting")
  325. // Broadcast is already (going to be) live
  326. return true;
  327. else if (lifeCycleStatus == "testStarting") {
  328. // User will need to wait a few seconds before attempting to start broadcast
  329. lastErrorMessage = QTStr("YouTube.Actions.Error.BroadcastTestStarting");
  330. lastErrorReason.clear();
  331. return false;
  332. }
  333. // Only reset if broadcast has monitoring enabled and is not already in "testing" mode
  334. auto monitorStreamEnabled =
  335. json_out["items"][0]["contentDetails"]["monitorStream"]["enableMonitorStream"].bool_value();
  336. if (lifeCycleStatus != "testing" && monitorStreamEnabled && !ResetBroadcast(broadcast_id, json_out))
  337. return false;
  338. const QString url_template = YOUTUBE_LIVE_BROADCAST_TRANSITION_URL "?id=%1"
  339. "&broadcastStatus=%2"
  340. "&part=status";
  341. const QString live = url_template.arg(broadcast_id, "live");
  342. bool success = InsertCommand(QT_TO_UTF8(live), "application/json", "POST", "{}", json_out);
  343. // Return a success if the command failed, but was redundant (broadcast already live)
  344. return success || lastErrorReason == "redundantTransition";
  345. }
  346. bool YoutubeApiWrappers::StartLatestBroadcast()
  347. {
  348. return StartBroadcast(this->broadcast_id);
  349. }
  350. bool YoutubeApiWrappers::StopBroadcast(const QString &broadcast_id)
  351. {
  352. lastErrorMessage.clear();
  353. lastErrorReason.clear();
  354. const QString url_template = YOUTUBE_LIVE_BROADCAST_TRANSITION_URL "?id=%1"
  355. "&broadcastStatus=complete"
  356. "&part=status";
  357. const QString url = url_template.arg(broadcast_id);
  358. Json json_out;
  359. bool success = InsertCommand(QT_TO_UTF8(url), "application/json", "POST", "{}", json_out);
  360. // Return a success if the command failed, but was redundant (broadcast already stopped)
  361. return success || lastErrorReason == "redundantTransition";
  362. }
  363. bool YoutubeApiWrappers::StopLatestBroadcast()
  364. {
  365. return StopBroadcast(this->broadcast_id);
  366. }
  367. void YoutubeApiWrappers::SetBroadcastId(QString &broadcast_id)
  368. {
  369. this->broadcast_id = broadcast_id;
  370. }
  371. QString YoutubeApiWrappers::GetBroadcastId()
  372. {
  373. return this->broadcast_id;
  374. }
  375. bool YoutubeApiWrappers::ResetBroadcast(const QString &broadcast_id, json11::Json &json_out)
  376. {
  377. lastErrorMessage.clear();
  378. lastErrorReason.clear();
  379. auto snippet = json_out["items"][0]["snippet"];
  380. auto status = json_out["items"][0]["status"];
  381. auto contentDetails = json_out["items"][0]["contentDetails"];
  382. auto monitorStream = contentDetails["monitorStream"];
  383. const Json data = Json::object{
  384. {"id", QT_TO_UTF8(broadcast_id)},
  385. {"snippet",
  386. Json::object{
  387. {"title", snippet["title"]},
  388. {"description", snippet["description"]},
  389. {"scheduledStartTime", snippet["scheduledStartTime"]},
  390. {"scheduledEndTime", snippet["scheduledEndTime"]},
  391. }},
  392. {"status",
  393. Json::object{
  394. {"privacyStatus", status["privacyStatus"]},
  395. {"madeForKids", status["madeForKids"]},
  396. {"selfDeclaredMadeForKids", status["selfDeclaredMadeForKids"]},
  397. }},
  398. {"contentDetails",
  399. Json::object{
  400. {
  401. "monitorStream",
  402. Json::object{
  403. {"enableMonitorStream", false},
  404. {"broadcastStreamDelayMs", monitorStream["broadcastStreamDelayMs"]},
  405. },
  406. },
  407. {"enableAutoStart", contentDetails["enableAutoStart"]},
  408. {"enableAutoStop", contentDetails["enableAutoStop"]},
  409. {"enableClosedCaptions", contentDetails["enableClosedCaptions"]},
  410. {"enableDvr", contentDetails["enableDvr"]},
  411. {"enableContentEncryption", contentDetails["enableContentEncryption"]},
  412. {"enableEmbed", contentDetails["enableEmbed"]},
  413. {"recordFromStart", contentDetails["recordFromStart"]},
  414. {"startWithSlate", contentDetails["startWithSlate"]},
  415. }},
  416. };
  417. const QString put = YOUTUBE_LIVE_BROADCAST_URL "?part=id,snippet,contentDetails,status";
  418. return InsertCommand(QT_TO_UTF8(put), "application/json", "PUT", data.dump().c_str(), json_out);
  419. }
  420. bool YoutubeApiWrappers::FindBroadcast(const QString &id, json11::Json &json_out)
  421. {
  422. lastErrorMessage.clear();
  423. lastErrorReason.clear();
  424. QByteArray url = YOUTUBE_LIVE_BROADCAST_URL "?part=id,snippet,contentDetails,status"
  425. "&broadcastType=all&maxResults=1";
  426. url += "&id=" + id.toUtf8();
  427. if (!InsertCommand(url, "application/json", "", nullptr, json_out))
  428. return false;
  429. auto items = json_out["items"].array_items();
  430. if (items.size() != 1) {
  431. lastErrorMessage = QTStr("YouTube.Actions.Error.BroadcastNotFound");
  432. return false;
  433. }
  434. return true;
  435. }
  436. bool YoutubeApiWrappers::FindStream(const QString &id, json11::Json &json_out)
  437. {
  438. lastErrorMessage.clear();
  439. lastErrorReason.clear();
  440. QByteArray url = YOUTUBE_LIVE_STREAM_URL "?part=id,snippet,cdn,status"
  441. "&maxResults=1";
  442. url += "&id=" + id.toUtf8();
  443. if (!InsertCommand(url, "application/json", "", nullptr, json_out))
  444. return false;
  445. auto items = json_out["items"].array_items();
  446. if (items.size() != 1) {
  447. lastErrorMessage = "No active broadcast found.";
  448. return false;
  449. }
  450. return true;
  451. }