media-io.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 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/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. /* TODO: handle format */
  52. if (!info) // || !info->format)
  53. return NULL;
  54. output = bmalloc(sizeof(struct media_output));
  55. da_init(output->connections);
  56. memcpy(&output->info, info, sizeof(struct media_output_info));
  57. if (pthread_mutex_init(&output->mutex, NULL) != 0) {
  58. bfree(output);
  59. return NULL;
  60. }
  61. return output;
  62. }
  63. void media_output_data(media_output_t output, const void *data)
  64. {
  65. size_t i;
  66. pthread_mutex_lock(&output->mutex);
  67. for (i = 0; i < output->connections.num; i++) {
  68. media_input_t input = output->connections.array[i];
  69. input->info.on_input(input->info.obj, data);
  70. }
  71. pthread_mutex_unlock(&output->mutex);
  72. }
  73. void media_output_destroy(media_output_t output)
  74. {
  75. if (output) {
  76. da_free(output->connections);
  77. pthread_mutex_destroy(&output->mutex);
  78. bfree(output);
  79. }
  80. }
  81. /* ------------------------------------------------------------------------- */
  82. media_t media_open(void)
  83. {
  84. struct media_data *media = bmalloc(sizeof(struct media_data));
  85. da_init(media->outputs);
  86. return media;
  87. }
  88. bool media_add_input(media_t media, media_input_t input)
  89. {
  90. media_output_t *outputs = media->outputs.array;
  91. size_t i;
  92. for (i = 0; i < media->outputs.num; i++) {
  93. media_output_t output = outputs[i];
  94. if (strcmp(output->info.format, input->info.format) == 0) {
  95. da_push_back(output->connections, input);
  96. return true;
  97. }
  98. }
  99. return false;
  100. }
  101. void media_add_output(media_t media, media_output_t output)
  102. {
  103. da_push_back(media->outputs, output);
  104. }
  105. void media_remove_input(media_t media, media_input_t input)
  106. {
  107. if (!input->connection)
  108. return;
  109. da_erase_item(input->connection->connections, input);
  110. }
  111. void media_remove_output(media_t media, media_output_t output)
  112. {
  113. da_erase_item(media->outputs, output);
  114. }
  115. void media_close(media_t media)
  116. {
  117. if (media) {
  118. da_free(media->outputs);
  119. bfree(media);
  120. }
  121. }