video-io.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 <assert.h>
  15. #include "../util/bmem.h"
  16. #include "../util/platform.h"
  17. #include "../util/threading.h"
  18. #include "../util/darray.h"
  19. #include "video-io.h"
  20. struct video_input {
  21. struct video_convert_info conversion;
  22. void (*callback)(void *param, const struct video_frame *frame);
  23. void *param;
  24. };
  25. struct video_output {
  26. struct video_output_info info;
  27. pthread_t thread;
  28. pthread_mutex_t data_mutex;
  29. event_t stop_event;
  30. struct video_frame *cur_frame;
  31. struct video_frame *next_frame;
  32. event_t update_event;
  33. uint64_t frame_time;
  34. volatile uint64_t cur_video_time;
  35. bool initialized;
  36. pthread_mutex_t input_mutex;
  37. DARRAY(struct video_input) inputs;
  38. };
  39. /* ------------------------------------------------------------------------- */
  40. static inline void video_swapframes(struct video_output *video)
  41. {
  42. pthread_mutex_lock(&video->data_mutex);
  43. if (video->next_frame) {
  44. video->cur_frame = video->next_frame;
  45. video->next_frame = NULL;
  46. }
  47. pthread_mutex_unlock(&video->data_mutex);
  48. }
  49. static inline void video_output_cur_frame(struct video_output *video)
  50. {
  51. if (!video->cur_frame)
  52. return;
  53. pthread_mutex_lock(&video->input_mutex);
  54. /* TODO: conversion */
  55. for (size_t i = 0; i < video->inputs.num; i++) {
  56. struct video_input *input = video->inputs.array+i;
  57. input->callback(input->param, video->cur_frame);
  58. }
  59. pthread_mutex_unlock(&video->input_mutex);
  60. }
  61. static void *video_thread(void *param)
  62. {
  63. struct video_output *video = param;
  64. uint64_t cur_time = os_gettime_ns();
  65. while (event_try(&video->stop_event) == EAGAIN) {
  66. /* wait half a frame, update frame */
  67. os_sleepto_ns(cur_time += (video->frame_time/2));
  68. video->cur_video_time = cur_time;
  69. event_signal(&video->update_event);
  70. /* wait another half a frame, swap and output frames */
  71. os_sleepto_ns(cur_time += (video->frame_time/2));
  72. video_swapframes(video);
  73. video_output_cur_frame(video);
  74. }
  75. return NULL;
  76. }
  77. /* ------------------------------------------------------------------------- */
  78. static inline bool valid_video_params(struct video_output_info *info)
  79. {
  80. return info->height != 0 && info->width != 0 && info->fps_den != 0 &&
  81. info->fps_num != 0;
  82. }
  83. int video_output_open(video_t *video, struct video_output_info *info)
  84. {
  85. struct video_output *out;
  86. if (!valid_video_params(info))
  87. return VIDEO_OUTPUT_INVALIDPARAM;
  88. out = bmalloc(sizeof(struct video_output));
  89. memset(out, 0, sizeof(struct video_output));
  90. memcpy(&out->info, info, sizeof(struct video_output_info));
  91. out->frame_time = (uint64_t)(1000000000.0 * (double)info->fps_den /
  92. (double)info->fps_num);
  93. out->initialized = false;
  94. if (pthread_mutex_init(&out->data_mutex, NULL) != 0)
  95. goto fail;
  96. if (pthread_mutex_init(&out->input_mutex, NULL) != 0)
  97. goto fail;
  98. if (event_init(&out->stop_event, EVENT_TYPE_MANUAL) != 0)
  99. goto fail;
  100. if (event_init(&out->update_event, EVENT_TYPE_AUTO) != 0)
  101. goto fail;
  102. if (pthread_create(&out->thread, NULL, video_thread, out) != 0)
  103. goto fail;
  104. out->initialized = true;
  105. *video = out;
  106. return VIDEO_OUTPUT_SUCCESS;
  107. fail:
  108. video_output_close(out);
  109. return VIDEO_OUTPUT_FAIL;
  110. }
  111. void video_output_close(video_t video)
  112. {
  113. if (!video)
  114. return;
  115. video_output_stop(video);
  116. da_free(video->inputs);
  117. event_destroy(&video->update_event);
  118. event_destroy(&video->stop_event);
  119. pthread_mutex_destroy(&video->data_mutex);
  120. pthread_mutex_destroy(&video->input_mutex);
  121. bfree(video);
  122. }
  123. static size_t video_get_input_idx(video_t video,
  124. void (*callback)(void *param, const struct video_frame *frame),
  125. void *param)
  126. {
  127. for (size_t i = 0; i < video->inputs.num; i++) {
  128. struct video_input *input = video->inputs.array+i;
  129. if (input->callback == callback && input->param == param)
  130. return i;
  131. }
  132. return DARRAY_INVALID;
  133. }
  134. void video_output_connect(video_t video,
  135. struct video_convert_info *conversion,
  136. void (*callback)(void *param, const struct video_frame *frame),
  137. void *param)
  138. {
  139. pthread_mutex_lock(&video->input_mutex);
  140. if (video_get_input_idx(video, callback, param) != DARRAY_INVALID) {
  141. struct video_input input;
  142. input.callback = callback;
  143. input.param = param;
  144. /* TODO: conversion */
  145. if (conversion) {
  146. input.conversion = *conversion;
  147. if (input.conversion.width == 0)
  148. input.conversion.width = video->info.width;
  149. if (input.conversion.height == 0)
  150. input.conversion.height = video->info.height;
  151. } else {
  152. input.conversion.format = video->info.format;
  153. input.conversion.width = video->info.width;
  154. input.conversion.height = video->info.height;
  155. input.conversion.row_align = 1;
  156. }
  157. da_push_back(video->inputs, &input);
  158. }
  159. pthread_mutex_unlock(&video->input_mutex);
  160. }
  161. void video_output_disconnect(video_t video,
  162. void (*callback)(void *param, const struct video_frame *frame),
  163. void *param)
  164. {
  165. pthread_mutex_lock(&video->input_mutex);
  166. size_t idx = video_get_input_idx(video, callback, param);
  167. if (idx != DARRAY_INVALID)
  168. da_erase(video->inputs, idx);
  169. pthread_mutex_unlock(&video->input_mutex);
  170. }
  171. const struct video_output_info *video_output_getinfo(video_t video)
  172. {
  173. return &video->info;
  174. }
  175. void video_output_frame(video_t video, struct video_frame *frame)
  176. {
  177. pthread_mutex_lock(&video->data_mutex);
  178. video->next_frame = frame;
  179. pthread_mutex_unlock(&video->data_mutex);
  180. }
  181. bool video_output_wait(video_t video)
  182. {
  183. event_wait(&video->update_event);
  184. return event_try(&video->stop_event) == EAGAIN;
  185. }
  186. uint64_t video_getframetime(video_t video)
  187. {
  188. return video->frame_time;
  189. }
  190. uint64_t video_gettime(video_t video)
  191. {
  192. return video->cur_video_time;
  193. }
  194. void video_output_stop(video_t video)
  195. {
  196. void *thread_ret;
  197. if (!video)
  198. return;
  199. if (video->initialized) {
  200. event_signal(&video->stop_event);
  201. pthread_join(video->thread, &thread_ret);
  202. event_signal(&video->update_event);
  203. }
  204. }