video-io.c 6.8 KB

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