dacast.c 4.0 KB

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