video-io.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 "format-conversion.h"
  20. #include "video-io.h"
  21. struct video_input {
  22. struct video_convert_info conversion;
  23. void (*callback)(void *param, const struct video_frame *frame);
  24. void *param;
  25. };
  26. struct video_output {
  27. struct video_output_info info;
  28. pthread_t thread;
  29. pthread_mutex_t data_mutex;
  30. event_t stop_event;
  31. struct video_frame cur_frame;
  32. struct video_frame next_frame;
  33. bool new_frame;
  34. event_t update_event;
  35. uint64_t frame_time;
  36. volatile uint64_t cur_video_time;
  37. bool initialized;
  38. pthread_mutex_t input_mutex;
  39. DARRAY(struct video_input) inputs;
  40. };
  41. /* ------------------------------------------------------------------------- */
  42. static inline void video_swapframes(struct video_output *video)
  43. {
  44. if (video->new_frame) {
  45. video->cur_frame = video->next_frame;
  46. video->new_frame = false;
  47. }
  48. }
  49. static inline void video_output_cur_frame(struct video_output *video)
  50. {
  51. if (!video->cur_frame.data[0])
  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. pthread_mutex_lock(&video->data_mutex);
  73. video_swapframes(video);
  74. video_output_cur_frame(video);
  75. pthread_mutex_unlock(&video->data_mutex);
  76. }
  77. return NULL;
  78. }
  79. /* ------------------------------------------------------------------------- */
  80. static inline bool valid_video_params(struct video_output_info *info)
  81. {
  82. return info->height != 0 && info->width != 0 && info->fps_den != 0 &&
  83. info->fps_num != 0;
  84. }
  85. int video_output_open(video_t *video, struct video_output_info *info)
  86. {
  87. struct video_output *out;
  88. if (!valid_video_params(info))
  89. return VIDEO_OUTPUT_INVALIDPARAM;
  90. out = bzalloc(sizeof(struct video_output));
  91. memcpy(&out->info, info, sizeof(struct video_output_info));
  92. out->frame_time = (uint64_t)(1000000000.0 * (double)info->fps_den /
  93. (double)info->fps_num);
  94. out->initialized = false;
  95. if (pthread_mutex_init(&out->data_mutex, NULL) != 0)
  96. goto fail;
  97. if (pthread_mutex_init(&out->input_mutex, NULL) != 0)
  98. goto fail;
  99. if (event_init(&out->stop_event, EVENT_TYPE_MANUAL) != 0)
  100. goto fail;
  101. if (event_init(&out->update_event, EVENT_TYPE_AUTO) != 0)
  102. goto fail;
  103. if (pthread_create(&out->thread, NULL, video_thread, out) != 0)
  104. goto fail;
  105. out->initialized = true;
  106. *video = out;
  107. return VIDEO_OUTPUT_SUCCESS;
  108. fail:
  109. video_output_close(out);
  110. return VIDEO_OUTPUT_FAIL;
  111. }
  112. void video_output_close(video_t video)
  113. {
  114. if (!video)
  115. return;
  116. video_output_stop(video);
  117. da_free(video->inputs);
  118. event_destroy(&video->update_event);
  119. event_destroy(&video->stop_event);
  120. pthread_mutex_destroy(&video->data_mutex);
  121. pthread_mutex_destroy(&video->input_mutex);
  122. bfree(video);
  123. }
  124. static size_t video_get_input_idx(video_t video,
  125. void (*callback)(void *param, const struct video_frame *frame),
  126. void *param)
  127. {
  128. for (size_t i = 0; i < video->inputs.num; i++) {
  129. struct video_input *input = video->inputs.array+i;
  130. if (input->callback == callback && input->param == param)
  131. return i;
  132. }
  133. return DARRAY_INVALID;
  134. }
  135. void video_output_connect(video_t video,
  136. struct video_convert_info *conversion,
  137. void (*callback)(void *param, const struct video_frame *frame),
  138. void *param)
  139. {
  140. pthread_mutex_lock(&video->input_mutex);
  141. if (video_get_input_idx(video, callback, param) == DARRAY_INVALID) {
  142. struct video_input input;
  143. input.callback = callback;
  144. input.param = param;
  145. /* TODO: conversion */
  146. if (conversion) {
  147. input.conversion = *conversion;
  148. if (input.conversion.width == 0)
  149. input.conversion.width = video->info.width;
  150. if (input.conversion.height == 0)
  151. input.conversion.height = video->info.height;
  152. } else {
  153. input.conversion.format = video->info.format;
  154. input.conversion.width = video->info.width;
  155. input.conversion.height = video->info.height;
  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. video->new_frame = true;
  180. pthread_mutex_unlock(&video->data_mutex);
  181. }
  182. bool video_output_wait(video_t video)
  183. {
  184. event_wait(&video->update_event);
  185. return event_try(&video->stop_event) == EAGAIN;
  186. }
  187. uint64_t video_getframetime(video_t video)
  188. {
  189. return video->frame_time;
  190. }
  191. uint64_t video_gettime(video_t video)
  192. {
  193. return video->cur_video_time;
  194. }
  195. void video_output_stop(video_t video)
  196. {
  197. void *thread_ret;
  198. if (!video)
  199. return;
  200. if (video->initialized) {
  201. event_signal(&video->stop_event);
  202. pthread_join(video->thread, &thread_ret);
  203. event_signal(&video->update_event);
  204. }
  205. }