1
0

obs-missing-files.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 = bzalloc(sizeof(struct obs_missing_files));
  33. return files;
  34. }
  35. void obs_missing_files_destroy(obs_missing_files_t *files)
  36. {
  37. for (size_t i = 0; i < files->files.num; i++) {
  38. obs_missing_file_release(files->files.array[i]);
  39. }
  40. da_free(files->files);
  41. bfree(files);
  42. }
  43. void obs_missing_files_add_file(obs_missing_files_t *files, obs_missing_file_t *file)
  44. {
  45. da_insert(files->files, files->files.num, &file);
  46. }
  47. size_t obs_missing_files_count(obs_missing_files_t *files)
  48. {
  49. return files->files.num;
  50. }
  51. obs_missing_file_t *obs_missing_files_get_file(obs_missing_files_t *files, int idx)
  52. {
  53. return files->files.array[idx];
  54. }
  55. void obs_missing_files_append(obs_missing_files_t *dst, obs_missing_files_t *src)
  56. {
  57. for (size_t i = 0; i < src->files.num; i++) {
  58. obs_missing_file_t *file = src->files.array[i];
  59. obs_missing_files_add_file(dst, file);
  60. os_atomic_inc_long(&file->ref);
  61. }
  62. }
  63. obs_missing_file_t *obs_missing_file_create(const char *path, obs_missing_file_cb callback, int src_type, void *src,
  64. void *data)
  65. {
  66. struct obs_missing_file *file = bzalloc(sizeof(struct obs_missing_file));
  67. file->file_path = bstrdup(path);
  68. file->callback = callback;
  69. file->src_type = src_type;
  70. file->src = src;
  71. file->data = data;
  72. file->ref = 1;
  73. switch (src_type) {
  74. case OBS_MISSING_FILE_SOURCE:
  75. file->src_name = bstrdup(obs_source_get_name(src));
  76. break;
  77. case OBS_MISSING_FILE_SCRIPT:
  78. break;
  79. }
  80. return file;
  81. }
  82. void obs_missing_file_release(obs_missing_file_t *file)
  83. {
  84. if (!file)
  85. return;
  86. if (os_atomic_dec_long(&file->ref) == 0)
  87. obs_missing_file_destroy(file);
  88. }
  89. void obs_missing_file_destroy(obs_missing_file_t *file)
  90. {
  91. switch (file->src_type) {
  92. case OBS_MISSING_FILE_SOURCE:
  93. bfree(file->src_name);
  94. break;
  95. case OBS_MISSING_FILE_SCRIPT:
  96. break;
  97. }
  98. bfree(file->file_path);
  99. bfree(file);
  100. }
  101. void obs_missing_file_issue_callback(obs_missing_file_t *file, const char *new_path)
  102. {
  103. switch (file->src_type) {
  104. case OBS_MISSING_FILE_SOURCE:
  105. obs_source_replace_missing_file(file->callback, (obs_source_t *)file->src, new_path, file->data);
  106. break;
  107. case OBS_MISSING_FILE_SCRIPT:
  108. break;
  109. }
  110. }
  111. const char *obs_missing_file_get_path(obs_missing_file_t *file)
  112. {
  113. return file->file_path;
  114. }
  115. const char *obs_missing_file_get_source_name(obs_missing_file_t *file)
  116. {
  117. return file->src_name;
  118. }