1
0

media-io.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[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 3 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/darray.h"
  16. #include "../util/bmem.h"
  17. #include "media-io.h"
  18. /* TODO: Incomplete */
  19. struct media_input {
  20. struct media_input_info info;
  21. struct media_output *connection;
  22. };
  23. struct media_output {
  24. struct media_output_info info;
  25. DARRAY(media_input_t) connections;
  26. pthread_mutex_t mutex;
  27. };
  28. struct media_data {
  29. DARRAY(media_output_t) outputs;
  30. };
  31. /* ------------------------------------------------------------------------- */
  32. media_input_t media_input_create(struct media_input_info *info)
  33. {
  34. struct media_input *input;
  35. if (!info || !info->format || !info->on_input)
  36. return NULL;
  37. input = bmalloc(sizeof(struct media_input));
  38. input->connection = NULL;
  39. memcpy(&input->info, info, sizeof(struct media_input_info));
  40. return input;
  41. }
  42. void media_input_destroy(media_input_t input)
  43. {
  44. if (input)
  45. bfree(input);
  46. }
  47. /* ------------------------------------------------------------------------- */
  48. media_output_t media_output_create(struct media_output_info *info)
  49. {
  50. struct media_output *output;
  51. if (!info || !info->format)
  52. return NULL;
  53. output = bmalloc(sizeof(struct media_output));
  54. da_init(output->connections);
  55. memcpy(&output->info, info, sizeof(struct media_output_info));
  56. if (pthread_mutex_init(&output->mutex, NULL) != 0) {
  57. bfree(output);
  58. return NULL;
  59. }
  60. return output;
  61. }
  62. void media_output_data(media_output_t output, const void *data)
  63. {
  64. size_t i;
  65. pthread_mutex_lock(&output->mutex);
  66. for (i = 0; i < output->connections.num; i++) {
  67. media_input_t input = output->connections.array[i];
  68. input->info.on_input(input->info.obj, data);
  69. }
  70. pthread_mutex_unlock(&output->mutex);
  71. }
  72. void media_output_destroy(media_output_t output)
  73. {
  74. if (output) {
  75. da_free(output->connections);
  76. pthread_mutex_destroy(&output->mutex);
  77. bfree(output);
  78. }
  79. }
  80. /* ------------------------------------------------------------------------- */
  81. media_t media_open(void)
  82. {
  83. struct media_data *media = bmalloc(sizeof(struct media_data));
  84. da_init(media->outputs);
  85. return media;
  86. }
  87. bool media_add_input(media_t media, media_input_t input)
  88. {
  89. media_output_t *outputs = media->outputs.array;
  90. size_t i;
  91. for (i = 0; i < media->outputs.num; i++) {
  92. media_output_t output = outputs[i];
  93. if (strcmp(output->info.format, input->info.format) == 0) {
  94. da_push_back(output->connections, input);
  95. return true;
  96. }
  97. }
  98. return false;
  99. }
  100. void media_add_output(media_t media, media_output_t output)
  101. {
  102. da_push_back(media->outputs, output);
  103. }
  104. void media_remove_input(media_t media, media_input_t input)
  105. {
  106. if (!input->connection)
  107. return;
  108. da_erase_item(input->connection->connections, input);
  109. }
  110. void media_remove_output(media_t media, media_output_t output)
  111. {
  112. da_erase_item(media->outputs, output);
  113. }
  114. void media_close(media_t media)
  115. {
  116. if (media) {
  117. da_free(media->outputs);
  118. bfree(media);
  119. }
  120. }