streaming-helpers.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include "streaming-helpers.hpp"
  2. #include "qt-wrappers.hpp"
  3. #include "obs-app.hpp"
  4. #include "../plugins/rtmp-services/rtmp-format-ver.h"
  5. #include <util/platform.h>
  6. #include <util/util.hpp>
  7. #include <obs.h>
  8. using namespace json11;
  9. static Json open_json_file(const char *path)
  10. {
  11. BPtr<char> file_data = os_quick_read_utf8_file(path);
  12. if (!file_data)
  13. return Json();
  14. std::string err;
  15. Json json = Json::parse(file_data, err);
  16. if (json["format_version"].int_value() != RTMP_SERVICES_FORMAT_VERSION)
  17. return Json();
  18. return json;
  19. }
  20. static inline bool name_matches(const Json &service, const char *name)
  21. {
  22. if (service["name"].string_value() == name)
  23. return true;
  24. auto &alt_names = service["alt_names"].array_items();
  25. for (const Json &alt_name : alt_names) {
  26. if (alt_name.string_value() == name) {
  27. return true;
  28. }
  29. }
  30. return false;
  31. }
  32. Json get_services_json()
  33. {
  34. obs_module_t *mod = obs_get_module("rtmp-services");
  35. Json root;
  36. BPtr<char> file = obs_module_get_config_path(mod, "services.json");
  37. if (file)
  38. root = open_json_file(file);
  39. if (root.is_null()) {
  40. file = obs_find_module_file(mod, "services.json");
  41. if (file)
  42. root = open_json_file(file);
  43. }
  44. return root;
  45. }
  46. Json get_service_from_json(const Json &root, const char *name)
  47. {
  48. auto &services = root["services"].array_items();
  49. for (const Json &service : services) {
  50. if (name_matches(service, name)) {
  51. return service;
  52. }
  53. }
  54. return Json();
  55. }
  56. void StreamSettingsUI::UpdateMoreInfoLink()
  57. {
  58. if (IsCustomService()) {
  59. ui_moreInfoButton->hide();
  60. return;
  61. }
  62. QString serviceName = ui_service->currentText();
  63. Json service = get_service_from_json(GetServicesJson(),
  64. QT_TO_UTF8(serviceName));
  65. const std::string &more_info_link =
  66. service["more_info_link"].string_value();
  67. if (more_info_link.empty()) {
  68. ui_moreInfoButton->hide();
  69. } else {
  70. ui_moreInfoButton->setTargetUrl(QUrl(more_info_link.c_str()));
  71. ui_moreInfoButton->show();
  72. }
  73. }
  74. void StreamSettingsUI::UpdateKeyLink()
  75. {
  76. QString serviceName = ui_service->currentText();
  77. QString customServer = ui_customServer->text().trimmed();
  78. Json service = get_service_from_json(GetServicesJson(),
  79. QT_TO_UTF8(serviceName));
  80. std::string streamKeyLink = service["stream_key_link"].string_value();
  81. if (customServer.contains("fbcdn.net") && IsCustomService()) {
  82. streamKeyLink =
  83. "https://www.facebook.com/live/producer?ref=OBS";
  84. }
  85. if (serviceName == "Dacast") {
  86. ui_streamKeyLabel->setText(
  87. QTStr("Basic.AutoConfig.StreamPage.EncoderKey"));
  88. } else {
  89. ui_streamKeyLabel->setText(
  90. QTStr("Basic.AutoConfig.StreamPage.StreamKey"));
  91. }
  92. if (streamKeyLink.empty()) {
  93. ui_streamKeyButton->hide();
  94. } else {
  95. ui_streamKeyButton->setTargetUrl(QUrl(streamKeyLink.c_str()));
  96. ui_streamKeyButton->show();
  97. }
  98. }
  99. void StreamSettingsUI::LoadServices(bool showAll)
  100. {
  101. auto &services = GetServicesJson()["services"].array_items();
  102. ui_service->blockSignals(true);
  103. ui_service->clear();
  104. QStringList names;
  105. for (const Json &service : services) {
  106. if (!showAll && !service["common"].bool_value())
  107. continue;
  108. names.push_back(service["name"].string_value().c_str());
  109. }
  110. if (showAll)
  111. names.sort(Qt::CaseInsensitive);
  112. for (QString &name : names)
  113. ui_service->addItem(name);
  114. if (!showAll) {
  115. ui_service->addItem(
  116. QTStr("Basic.AutoConfig.StreamPage.Service.ShowAll"),
  117. QVariant((int)ListOpt::ShowAll));
  118. }
  119. ui_service->insertItem(
  120. 0, QTStr("Basic.AutoConfig.StreamPage.Service.Custom"),
  121. QVariant((int)ListOpt::Custom));
  122. if (!lastService.isEmpty()) {
  123. int idx = ui_service->findText(lastService);
  124. if (idx != -1)
  125. ui_service->setCurrentIndex(idx);
  126. }
  127. ui_service->blockSignals(false);
  128. }
  129. void StreamSettingsUI::UpdateServerList()
  130. {
  131. QString serviceName = ui_service->currentText();
  132. bool showMore = ui_service->currentData().toInt() ==
  133. (int)ListOpt::ShowAll;
  134. if (showMore) {
  135. LoadServices(true);
  136. ui_service->showPopup();
  137. return;
  138. } else {
  139. lastService = serviceName;
  140. }
  141. Json service = get_service_from_json(GetServicesJson(),
  142. QT_TO_UTF8(serviceName));
  143. ui_server->clear();
  144. auto &servers = service["servers"].array_items();
  145. for (const Json &entry : servers) {
  146. ui_server->addItem(entry["name"].string_value().c_str(),
  147. entry["url"].string_value().c_str());
  148. }
  149. }