twitch.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include <file-updater/file-updater.h>
  2. #include <util/threading.h>
  3. #include <util/platform.h>
  4. #include <obs-module.h>
  5. #include <util/dstr.h>
  6. #include <jansson.h>
  7. #include "twitch.h"
  8. static update_info_t *twitch_update_info = NULL;
  9. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  10. struct ingest {
  11. char *name;
  12. char *url;
  13. };
  14. static DARRAY(struct ingest) cur_ingests;
  15. static void free_ingests(void)
  16. {
  17. for (size_t i = 0; i < cur_ingests.num; i++) {
  18. struct ingest *ingest = cur_ingests.array + i;
  19. bfree(ingest->name);
  20. bfree(ingest->url);
  21. }
  22. da_free(cur_ingests);
  23. }
  24. static void load_ingests(const char *json, bool write_file)
  25. {
  26. json_t *root;
  27. json_t *ingests;
  28. bool success = false;
  29. char *cache_old;
  30. char *cache_new;
  31. size_t count;
  32. root = json_loads(json, 0, NULL);
  33. if (!root)
  34. goto finish;
  35. ingests = json_object_get(root, "ingests");
  36. if (!ingests)
  37. goto finish;
  38. count = json_array_size(ingests);
  39. if (count <= 1 && cur_ingests.num)
  40. goto finish;
  41. free_ingests();
  42. for (size_t i = 0; i < count; i++) {
  43. json_t *item = json_array_get(ingests, i);
  44. json_t *item_name = json_object_get(item, "name");
  45. json_t *item_url = json_object_get(item, "url_template");
  46. struct ingest ingest = {0};
  47. struct dstr url = {0};
  48. if (!item_name || !item_url)
  49. continue;
  50. const char *url_str = json_string_value(item_url);
  51. const char *name_str = json_string_value(item_name);
  52. /* At the moment they currently mis-spell "deprecated",
  53. * but that may change in the future, so blacklist both */
  54. if (strstr(name_str, "deprecated") != NULL ||
  55. strstr(name_str, "depracated") != NULL)
  56. continue;
  57. dstr_copy(&url, url_str);
  58. dstr_replace(&url, "/{stream_key}", "");
  59. ingest.name = bstrdup(name_str);
  60. ingest.url = url.array;
  61. da_push_back(cur_ingests, &ingest);
  62. }
  63. if (!write_file || !cur_ingests.num)
  64. goto finish;
  65. cache_old = obs_module_config_path("twitch_ingests.json");
  66. cache_new = obs_module_config_path("twitch_ingests.new.json");
  67. os_quick_write_utf8_file(cache_new, json, strlen(json), false);
  68. os_safe_replace(cache_old, cache_new, NULL);
  69. bfree(cache_old);
  70. bfree(cache_new);
  71. finish:
  72. if (root)
  73. json_decref(root);
  74. }
  75. static bool twitch_ingest_update(void *param, struct file_download_data *data)
  76. {
  77. pthread_mutex_lock(&mutex);
  78. load_ingests(data->buffer.array, true);
  79. pthread_mutex_unlock(&mutex);
  80. UNUSED_PARAMETER(param);
  81. return true;
  82. }
  83. void twitch_ingests_lock(void)
  84. {
  85. pthread_mutex_lock(&mutex);
  86. }
  87. void twitch_ingests_unlock(void)
  88. {
  89. pthread_mutex_unlock(&mutex);
  90. }
  91. size_t twitch_ingest_count(void)
  92. {
  93. return cur_ingests.num;
  94. }
  95. struct twitch_ingest twitch_ingest(size_t idx)
  96. {
  97. struct twitch_ingest ingest;
  98. if (cur_ingests.num <= idx) {
  99. ingest.name = NULL;
  100. ingest.url = NULL;
  101. } else {
  102. ingest = *(struct twitch_ingest*)(cur_ingests.array + idx);
  103. }
  104. return ingest;
  105. }
  106. void init_twitch_data(void)
  107. {
  108. da_init(cur_ingests);
  109. pthread_mutex_init(&mutex, NULL);
  110. }
  111. void load_twitch_data(const char *module_str)
  112. {
  113. char *twitch_cache = obs_module_config_path("twitch_ingests.json");
  114. if (os_file_exists(twitch_cache)) {
  115. char *data = os_quick_read_utf8_file(twitch_cache);
  116. pthread_mutex_lock(&mutex);
  117. load_ingests(data, false);
  118. pthread_mutex_unlock(&mutex);
  119. bfree(data);
  120. }
  121. twitch_update_info = update_info_create_single(
  122. "[twitch ingest update] ",
  123. module_str,
  124. "https://ingest.twitch.tv/api/v2/ingests",
  125. twitch_ingest_update, NULL);
  126. bfree(twitch_cache);
  127. }
  128. void unload_twitch_data(void)
  129. {
  130. update_info_destroy(twitch_update_info);
  131. free_ingests();
  132. pthread_mutex_destroy(&mutex);
  133. }