dacast.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #include <file-updater/file-updater.h>
  2. #include <util/threading.h>
  3. #include <util/platform.h>
  4. #include <util/dstr.h>
  5. #include <jansson.h>
  6. #include "dacast.h"
  7. #ifndef SEC_TO_NSEC
  8. #define SEC_TO_NSEC 1000000000ULL
  9. #endif
  10. static update_info_t *dacast_update_info = NULL;
  11. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  12. static bool ingests_loaded = false;
  13. struct dacast_ingest_info {
  14. char *key;
  15. uint64_t last_time;
  16. struct dacast_ingest ingest;
  17. };
  18. struct dacast_ingest dacast_invalid_ingest = {"rtmp://dacast", "", "",
  19. "fake_key"};
  20. static DARRAY(struct dacast_ingest_info) cur_ingests;
  21. static void free_ingest(struct dacast_ingest ingest)
  22. {
  23. bfree((void *)ingest.url);
  24. bfree((void *)ingest.username);
  25. bfree((void *)ingest.password);
  26. bfree((void *)ingest.streamkey);
  27. }
  28. static void free_ingests(void)
  29. {
  30. for (size_t i = 0; i < cur_ingests.num; i++) {
  31. struct dacast_ingest_info *info = &cur_ingests.array[i];
  32. bfree(info->key);
  33. free_ingest(info->ingest);
  34. }
  35. da_free(cur_ingests);
  36. }
  37. static struct dacast_ingest_info *find_ingest(const char *key)
  38. {
  39. struct dacast_ingest_info *ret = NULL;
  40. for (size_t i = 0; i < cur_ingests.num; i++) {
  41. struct dacast_ingest_info *info = &cur_ingests.array[i];
  42. if (strcmp(info->key, key) == 0) {
  43. ret = info;
  44. break;
  45. }
  46. }
  47. return (struct dacast_ingest_info *)ret;
  48. }
  49. static bool load_ingests(const char *json, const char *key)
  50. {
  51. json_t *root;
  52. json_t *stream;
  53. bool success = false;
  54. struct dacast_ingest_info *info = find_ingest(key);
  55. if (!info) {
  56. info = da_push_back_new(cur_ingests);
  57. info->key = bstrdup(key);
  58. } else {
  59. free_ingest(info->ingest);
  60. }
  61. root = json_loads(json, 0, NULL);
  62. if (!root)
  63. goto finish;
  64. stream = json_object_get(root, "stream");
  65. if (!stream)
  66. goto finish;
  67. json_t *item_server = json_object_get(stream, "server");
  68. json_t *item_username = json_object_get(stream, "username");
  69. json_t *item_password = json_object_get(stream, "password");
  70. json_t *item_streamkey = json_object_get(stream, "streamkey");
  71. if (!item_server || !item_username || !item_password || !item_streamkey)
  72. goto finish;
  73. const char *server = json_string_value(item_server);
  74. const char *username = json_string_value(item_username);
  75. const char *password = json_string_value(item_password);
  76. const char *streamkey = json_string_value(item_streamkey);
  77. info->ingest.url = bstrdup(server);
  78. info->ingest.username = bstrdup(username);
  79. info->ingest.password = bstrdup(password);
  80. info->ingest.streamkey = bstrdup(streamkey);
  81. info->last_time = os_gettime_ns() / SEC_TO_NSEC;
  82. success = true;
  83. finish:
  84. if (root)
  85. json_decref(root);
  86. return success;
  87. }
  88. static bool dacast_ingest_update(void *param, struct file_download_data *data)
  89. {
  90. bool success;
  91. pthread_mutex_lock(&mutex);
  92. success = load_ingests((const char *)data->buffer.array,
  93. (const char *)param);
  94. pthread_mutex_unlock(&mutex);
  95. if (success) {
  96. os_atomic_set_bool(&ingests_loaded, true);
  97. }
  98. return true;
  99. }
  100. struct dacast_ingest *dacast_ingest(const char *key)
  101. {
  102. pthread_mutex_lock(&mutex);
  103. struct dacast_ingest_info *info = find_ingest(key);
  104. pthread_mutex_unlock(&mutex);
  105. return info == NULL ? &dacast_invalid_ingest : &info->ingest;
  106. }
  107. void init_dacast_data(void)
  108. {
  109. da_init(cur_ingests);
  110. pthread_mutex_init(&mutex, NULL);
  111. }
  112. extern const char *get_module_name(void);
  113. #define TIMEOUT_SEC 3
  114. void dacast_ingests_load_data(const char *server, const char *key)
  115. {
  116. struct dstr uri = {0};
  117. os_atomic_set_bool(&ingests_loaded, false);
  118. dstr_copy(&uri, server);
  119. dstr_cat(&uri, key);
  120. if (dacast_update_info) {
  121. update_info_destroy(dacast_update_info);
  122. dacast_update_info = NULL;
  123. }
  124. dacast_update_info = update_info_create_single(
  125. "[dacast ingest load data] ", get_module_name(), uri.array,
  126. dacast_ingest_update, (void *)key);
  127. if (!os_atomic_load_bool(&ingests_loaded)) {
  128. for (int i = 0; i < TIMEOUT_SEC * 100; i++) {
  129. if (os_atomic_load_bool(&ingests_loaded)) {
  130. break;
  131. }
  132. os_sleep_ms(10);
  133. }
  134. }
  135. dstr_free(&uri);
  136. }
  137. void unload_dacast_data(void)
  138. {
  139. update_info_destroy(dacast_update_info);
  140. free_ingests();
  141. pthread_mutex_destroy(&mutex);
  142. }