streaming-helpers.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. bool StreamSettingsUI::IsServiceOutputHasNetworkFeatures()
  57. {
  58. if (IsCustomService())
  59. return ui_customServer->text().startsWith("rtmp");
  60. Json service = get_service_from_json(
  61. GetServicesJson(), QT_TO_UTF8(ui_service->currentText()));
  62. if (!service["recommended"]["output"].is_string())
  63. return true;
  64. if (service["recommended"]["output"].string_value().compare(
  65. "rtmp_output") == 0)
  66. return true;
  67. return false;
  68. }
  69. void StreamSettingsUI::UpdateMoreInfoLink()
  70. {
  71. if (IsCustomService()) {
  72. ui_moreInfoButton->hide();
  73. return;
  74. }
  75. QString serviceName = ui_service->currentText();
  76. Json service = get_service_from_json(GetServicesJson(),
  77. QT_TO_UTF8(serviceName));
  78. const std::string &more_info_link =
  79. service["more_info_link"].string_value();
  80. if (more_info_link.empty()) {
  81. ui_moreInfoButton->hide();
  82. } else {
  83. ui_moreInfoButton->setTargetUrl(QUrl(more_info_link.c_str()));
  84. ui_moreInfoButton->show();
  85. }
  86. }
  87. void StreamSettingsUI::UpdateKeyLink()
  88. {
  89. QString serviceName = ui_service->currentText();
  90. QString customServer = ui_customServer->text().trimmed();
  91. Json service = get_service_from_json(GetServicesJson(),
  92. QT_TO_UTF8(serviceName));
  93. std::string streamKeyLink = service["stream_key_link"].string_value();
  94. if (customServer.contains("fbcdn.net") && IsCustomService()) {
  95. streamKeyLink =
  96. "https://www.facebook.com/live/producer?ref=OBS";
  97. }
  98. if (serviceName == "Dacast") {
  99. ui_streamKeyLabel->setText(
  100. QTStr("Basic.AutoConfig.StreamPage.EncoderKey"));
  101. } else {
  102. ui_streamKeyLabel->setText(
  103. QTStr("Basic.AutoConfig.StreamPage.StreamKey"));
  104. }
  105. if (streamKeyLink.empty()) {
  106. ui_streamKeyButton->hide();
  107. } else {
  108. ui_streamKeyButton->setTargetUrl(QUrl(streamKeyLink.c_str()));
  109. ui_streamKeyButton->show();
  110. }
  111. }
  112. void StreamSettingsUI::LoadServices(bool showAll)
  113. {
  114. auto &services = GetServicesJson()["services"].array_items();
  115. ui_service->blockSignals(true);
  116. ui_service->clear();
  117. QStringList names;
  118. for (const Json &service : services) {
  119. if (!showAll && !service["common"].bool_value())
  120. continue;
  121. names.push_back(service["name"].string_value().c_str());
  122. }
  123. if (showAll)
  124. names.sort(Qt::CaseInsensitive);
  125. for (QString &name : names)
  126. ui_service->addItem(name);
  127. if (!showAll) {
  128. ui_service->addItem(
  129. QTStr("Basic.AutoConfig.StreamPage.Service.ShowAll"),
  130. QVariant((int)ListOpt::ShowAll));
  131. }
  132. ui_service->insertItem(
  133. 0, QTStr("Basic.AutoConfig.StreamPage.Service.Custom"),
  134. QVariant((int)ListOpt::Custom));
  135. if (!lastService.isEmpty()) {
  136. int idx = ui_service->findText(lastService);
  137. if (idx != -1)
  138. ui_service->setCurrentIndex(idx);
  139. }
  140. ui_service->blockSignals(false);
  141. }
  142. void StreamSettingsUI::UpdateServerList()
  143. {
  144. QString serviceName = ui_service->currentText();
  145. bool showMore = ui_service->currentData().toInt() ==
  146. (int)ListOpt::ShowAll;
  147. if (showMore) {
  148. LoadServices(true);
  149. ui_service->showPopup();
  150. return;
  151. } else {
  152. lastService = serviceName;
  153. }
  154. Json service = get_service_from_json(GetServicesJson(),
  155. QT_TO_UTF8(serviceName));
  156. ui_server->clear();
  157. auto &servers = service["servers"].array_items();
  158. for (const Json &entry : servers) {
  159. ui_server->addItem(entry["name"].string_value().c_str(),
  160. entry["url"].string_value().c_str());
  161. }
  162. }