obs-missing-files.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /******************************************************************************
  2. Copyright (C) 2019 by Dillon Pentz <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "util/threading.h"
  15. #include "util/dstr.h"
  16. #include "obs-missing-files.h"
  17. #include "obs.h"
  18. struct obs_missing_file {
  19. volatile long ref;
  20. char *file_path;
  21. obs_missing_file_cb callback;
  22. int src_type;
  23. void *src;
  24. char *src_name;
  25. void *data;
  26. };
  27. struct obs_missing_files {
  28. DARRAY(struct obs_missing_file *) files;
  29. };
  30. obs_missing_files_t *obs_missing_files_create()
  31. {
  32. struct obs_missing_files *files =
  33. bzalloc(sizeof(struct obs_missing_files));
  34. return files;
  35. }
  36. void obs_missing_files_destroy(obs_missing_files_t *files)
  37. {
  38. for (size_t i = 0; i < files->files.num; i++) {
  39. obs_missing_file_release(files->files.array[i]);
  40. }
  41. da_free(files->files);
  42. bfree(files);
  43. }
  44. void obs_missing_files_add_file(obs_missing_files_t *files,
  45. obs_missing_file_t *file)
  46. {
  47. da_insert(files->files, files->files.num, &file);
  48. }
  49. size_t obs_missing_files_count(obs_missing_files_t *files)
  50. {
  51. return files->files.num;
  52. }
  53. obs_missing_file_t *obs_missing_files_get_file(obs_missing_files_t *files,
  54. int idx)
  55. {
  56. return files->files.array[idx];
  57. }
  58. void obs_missing_files_append(obs_missing_files_t *dst,
  59. obs_missing_files_t *src)
  60. {
  61. for (size_t i = 0; i < src->files.num; i++) {
  62. obs_missing_file_t *file = src->files.array[i];
  63. obs_missing_files_add_file(dst, file);
  64. os_atomic_inc_long(&file->ref);
  65. }
  66. }
  67. obs_missing_file_t *obs_missing_file_create(const char *path,
  68. obs_missing_file_cb callback,
  69. int src_type, void *src, void *data)
  70. {
  71. struct obs_missing_file *file =
  72. bzalloc(sizeof(struct obs_missing_file));
  73. file->file_path = bstrdup(path);
  74. file->callback = callback;
  75. file->src_type = src_type;
  76. file->src = src;
  77. file->data = data;
  78. file->ref = 1;
  79. switch (src_type) {
  80. case OBS_MISSING_FILE_SOURCE:
  81. file->src_name = bstrdup(obs_source_get_name(src));
  82. break;
  83. case OBS_MISSING_FILE_SCRIPT:
  84. break;
  85. }
  86. return file;
  87. }
  88. void obs_missing_file_release(obs_missing_file_t *file)
  89. {
  90. if (!file)
  91. return;
  92. if (os_atomic_dec_long(&file->ref) == 0)
  93. obs_missing_file_destroy(file);
  94. }
  95. void obs_missing_file_destroy(obs_missing_file_t *file)
  96. {
  97. switch (file->src_type) {
  98. case OBS_MISSING_FILE_SOURCE:
  99. bfree(file->src_name);
  100. break;
  101. case OBS_MISSING_FILE_SCRIPT:
  102. break;
  103. }
  104. bfree(file->file_path);
  105. bfree(file);
  106. }
  107. void obs_missing_file_issue_callback(obs_missing_file_t *file,
  108. const char *new_path)
  109. {
  110. switch (file->src_type) {
  111. case OBS_MISSING_FILE_SOURCE:
  112. obs_source_replace_missing_file(file->callback,
  113. (obs_source_t *)file->src,
  114. new_path, file->data);
  115. break;
  116. case OBS_MISSING_FILE_SCRIPT:
  117. break;
  118. }
  119. }
  120. const char *obs_missing_file_get_path(obs_missing_file_t *file)
  121. {
  122. return file->file_path;
  123. }
  124. const char *obs_missing_file_get_source_name(obs_missing_file_t *file)
  125. {
  126. return file->src_name;
  127. }