service-ingest.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include <util/platform.h>
  2. #include <obs-module.h>
  3. #include <util/dstr.h>
  4. #include <jansson.h>
  5. #include "service-ingest.h"
  6. extern const char *get_module_name(void);
  7. void init_service_data(struct service_ingests *si)
  8. {
  9. da_init(si->cur_ingests);
  10. pthread_mutex_init(&si->mutex, NULL);
  11. }
  12. static void free_ingests(struct service_ingests *si)
  13. {
  14. for (size_t i = 0; i < si->cur_ingests.num; i++) {
  15. struct ingest *ingest = si->cur_ingests.array + i;
  16. bfree(ingest->name);
  17. bfree(ingest->url);
  18. bfree(ingest->rtmps_url);
  19. }
  20. da_free(si->cur_ingests);
  21. }
  22. static bool load_ingests(struct service_ingests *si, const char *json, bool write_file)
  23. {
  24. json_t *root;
  25. json_t *ingests;
  26. bool success = false;
  27. char *cache_old;
  28. char *cache_new;
  29. size_t count;
  30. root = json_loads(json, 0, NULL);
  31. if (!root)
  32. goto finish;
  33. ingests = json_object_get(root, "ingests");
  34. if (!ingests)
  35. goto finish;
  36. count = json_array_size(ingests);
  37. if (count <= 1 && si->cur_ingests.num)
  38. goto finish;
  39. free_ingests(si);
  40. for (size_t i = 0; i < count; i++) {
  41. json_t *item = json_array_get(ingests, i);
  42. json_t *item_name = json_object_get(item, "name");
  43. json_t *item_url = json_object_get(item, "url_template");
  44. json_t *item_rtmps_url = json_object_get(item, "url_template_secure");
  45. struct ingest ingest = {0};
  46. struct dstr url = {0};
  47. struct dstr rtmps_url = {0};
  48. if (!item_name || !item_url)
  49. continue;
  50. const char *url_str = json_string_value(item_url);
  51. const char *rtmps_url_str = json_string_value(item_rtmps_url);
  52. const char *name_str = json_string_value(item_name);
  53. /* At the moment they currently mis-spell "deprecated",
  54. * but that may change in the future, so blacklist both */
  55. if (strstr(name_str, "deprecated") != NULL || strstr(name_str, "depracated") != NULL)
  56. continue;
  57. dstr_copy(&url, url_str);
  58. dstr_replace(&url, "/{stream_key}", "");
  59. dstr_copy(&rtmps_url, rtmps_url_str);
  60. dstr_replace(&rtmps_url, "/{stream_key}", "");
  61. ingest.name = bstrdup(name_str);
  62. ingest.url = url.array;
  63. ingest.rtmps_url = rtmps_url.array;
  64. da_push_back(si->cur_ingests, &ingest);
  65. }
  66. if (!si->cur_ingests.num)
  67. goto finish;
  68. success = true;
  69. if (!write_file)
  70. goto finish;
  71. cache_old = obs_module_config_path(si->cache_old_filename);
  72. cache_new = obs_module_config_path(si->cache_new_filename);
  73. os_quick_write_utf8_file(cache_new, json, strlen(json), false);
  74. os_safe_replace(cache_old, cache_new, NULL);
  75. bfree(cache_old);
  76. bfree(cache_new);
  77. finish:
  78. if (root)
  79. json_decref(root);
  80. return success;
  81. }
  82. static bool ingest_update(void *param, struct file_download_data *data)
  83. {
  84. struct service_ingests *service = param;
  85. bool success;
  86. pthread_mutex_lock(&service->mutex);
  87. success = load_ingests(service, (const char *)data->buffer.array, true);
  88. pthread_mutex_unlock(&service->mutex);
  89. if (success) {
  90. os_atomic_set_bool(&service->ingests_refreshed, true);
  91. os_atomic_set_bool(&service->ingests_loaded, true);
  92. }
  93. return true;
  94. }
  95. void service_ingests_refresh(struct service_ingests *si, int seconds, const char *log_prefix, const char *file_url)
  96. {
  97. if (os_atomic_load_bool(&si->ingests_refreshed))
  98. return;
  99. if (!os_atomic_load_bool(&si->ingests_refreshing)) {
  100. os_atomic_set_bool(&si->ingests_refreshing, true);
  101. si->update_info = update_info_create_single(log_prefix, get_module_name(), file_url, ingest_update, si);
  102. }
  103. /* wait five seconds max when loading ingests for the first time */
  104. if (!os_atomic_load_bool(&si->ingests_loaded)) {
  105. for (int i = 0; i < seconds * 100; i++) {
  106. if (os_atomic_load_bool(&si->ingests_refreshed)) {
  107. break;
  108. }
  109. os_sleep_ms(10);
  110. }
  111. }
  112. }
  113. void load_service_data(struct service_ingests *si, const char *cache_filename, struct ingest *def)
  114. {
  115. char *service_cache = obs_module_config_path(cache_filename);
  116. pthread_mutex_lock(&si->mutex);
  117. da_push_back(si->cur_ingests, def);
  118. pthread_mutex_unlock(&si->mutex);
  119. if (os_file_exists(service_cache)) {
  120. char *data = os_quick_read_utf8_file(service_cache);
  121. bool success;
  122. pthread_mutex_lock(&si->mutex);
  123. success = load_ingests(si, data, false);
  124. pthread_mutex_unlock(&si->mutex);
  125. if (success) {
  126. os_atomic_set_bool(&si->ingests_loaded, true);
  127. }
  128. bfree(data);
  129. }
  130. bfree(service_cache);
  131. }
  132. void unload_service_data(struct service_ingests *si)
  133. {
  134. update_info_destroy(si->update_info);
  135. free_ingests(si);
  136. pthread_mutex_destroy(&si->mutex);
  137. }
  138. struct ingest get_ingest(struct service_ingests *si, size_t idx)
  139. {
  140. struct ingest ingest;
  141. if (si->cur_ingests.num <= idx) {
  142. ingest.name = NULL;
  143. ingest.url = NULL;
  144. ingest.rtmps_url = NULL;
  145. } else {
  146. ingest = *(struct ingest *)(si->cur_ingests.array + idx);
  147. }
  148. return ingest;
  149. }