api-threadmessage-test.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Permission is hereby granted, free of charge, to any person obtaining a copy
  3. * of this software and associated documentation files (the "Software"), to deal
  4. * in the Software without restriction, including without limitation the rights
  5. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  6. * copies of the Software, and to permit persons to whom the Software is
  7. * furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in
  10. * all copies or substantial portions of the Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. */
  20. /**
  21. * Thread message API test
  22. */
  23. #include "libavutil/avassert.h"
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/frame.h"
  26. #include "libavutil/mem.h"
  27. #include "libavutil/threadmessage.h"
  28. #include "libavutil/thread.h" // not public
  29. struct sender_data {
  30. int id;
  31. pthread_t tid;
  32. int workload;
  33. AVThreadMessageQueue *queue;
  34. };
  35. /* same as sender_data but shuffled for testing purpose */
  36. struct receiver_data {
  37. pthread_t tid;
  38. int workload;
  39. int id;
  40. AVThreadMessageQueue *queue;
  41. };
  42. struct message {
  43. AVFrame *frame;
  44. // we add some junk in the message to make sure the message size is >
  45. // sizeof(void*)
  46. int magic;
  47. };
  48. #define MAGIC 0xdeadc0de
  49. static void free_frame(void *arg)
  50. {
  51. struct message *msg = arg;
  52. av_assert0(msg->magic == MAGIC);
  53. av_frame_free(&msg->frame);
  54. }
  55. static void *sender_thread(void *arg)
  56. {
  57. int i, ret = 0;
  58. struct sender_data *wd = arg;
  59. av_log(NULL, AV_LOG_INFO, "sender #%d: workload=%d\n", wd->id, wd->workload);
  60. for (i = 0; i < wd->workload; i++) {
  61. if (rand() % wd->workload < wd->workload / 10) {
  62. av_log(NULL, AV_LOG_INFO, "sender #%d: flushing the queue\n", wd->id);
  63. av_thread_message_flush(wd->queue);
  64. } else {
  65. char *val;
  66. AVDictionary *meta = NULL;
  67. struct message msg = {
  68. .magic = MAGIC,
  69. .frame = av_frame_alloc(),
  70. };
  71. if (!msg.frame) {
  72. ret = AVERROR(ENOMEM);
  73. break;
  74. }
  75. /* we add some metadata to identify the frames */
  76. val = av_asprintf("frame %d/%d from sender %d",
  77. i + 1, wd->workload, wd->id);
  78. if (!val) {
  79. av_frame_free(&msg.frame);
  80. ret = AVERROR(ENOMEM);
  81. break;
  82. }
  83. ret = av_dict_set(&meta, "sig", val, AV_DICT_DONT_STRDUP_VAL);
  84. if (ret < 0) {
  85. av_frame_free(&msg.frame);
  86. break;
  87. }
  88. msg.frame->metadata = meta;
  89. /* allocate a real frame in order to simulate "real" work */
  90. msg.frame->format = AV_PIX_FMT_RGBA;
  91. msg.frame->width = 320;
  92. msg.frame->height = 240;
  93. ret = av_frame_get_buffer(msg.frame, 0);
  94. if (ret < 0) {
  95. av_frame_free(&msg.frame);
  96. break;
  97. }
  98. /* push the frame in the common queue */
  99. av_log(NULL, AV_LOG_INFO, "sender #%d: sending my work (%d/%d frame:%p)\n",
  100. wd->id, i + 1, wd->workload, msg.frame);
  101. ret = av_thread_message_queue_send(wd->queue, &msg, 0);
  102. if (ret < 0) {
  103. av_frame_free(&msg.frame);
  104. break;
  105. }
  106. }
  107. }
  108. av_log(NULL, AV_LOG_INFO, "sender #%d: my work is done here (%s)\n",
  109. wd->id, av_err2str(ret));
  110. av_thread_message_queue_set_err_recv(wd->queue, ret < 0 ? ret : AVERROR_EOF);
  111. return NULL;
  112. }
  113. static void *receiver_thread(void *arg)
  114. {
  115. int i, ret = 0;
  116. struct receiver_data *rd = arg;
  117. for (i = 0; i < rd->workload; i++) {
  118. if (rand() % rd->workload < rd->workload / 10) {
  119. av_log(NULL, AV_LOG_INFO, "receiver #%d: flushing the queue, "
  120. "discarding %d message(s)\n", rd->id,
  121. av_thread_message_queue_nb_elems(rd->queue));
  122. av_thread_message_flush(rd->queue);
  123. } else {
  124. struct message msg;
  125. AVDictionary *meta;
  126. AVDictionaryEntry *e;
  127. ret = av_thread_message_queue_recv(rd->queue, &msg, 0);
  128. if (ret < 0)
  129. break;
  130. av_assert0(msg.magic == MAGIC);
  131. meta = msg.frame->metadata;
  132. e = av_dict_get(meta, "sig", NULL, 0);
  133. av_log(NULL, AV_LOG_INFO, "got \"%s\" (%p)\n", e->value, msg.frame);
  134. av_frame_free(&msg.frame);
  135. }
  136. }
  137. av_log(NULL, AV_LOG_INFO, "consumed enough (%d), stop\n", i);
  138. av_thread_message_queue_set_err_send(rd->queue, ret < 0 ? ret : AVERROR_EOF);
  139. return NULL;
  140. }
  141. static int get_workload(int minv, int maxv)
  142. {
  143. return maxv == minv ? maxv : rand() % (maxv - minv) + minv;
  144. }
  145. int main(int ac, char **av)
  146. {
  147. int i, ret = 0;
  148. int max_queue_size;
  149. int nb_senders, sender_min_load, sender_max_load;
  150. int nb_receivers, receiver_min_load, receiver_max_load;
  151. struct sender_data *senders;
  152. struct receiver_data *receivers;
  153. AVThreadMessageQueue *queue = NULL;
  154. if (ac != 8) {
  155. av_log(NULL, AV_LOG_ERROR, "%s <max_queue_size> "
  156. "<nb_senders> <sender_min_send> <sender_max_send> "
  157. "<nb_receivers> <receiver_min_recv> <receiver_max_recv>\n", av[0]);
  158. return 1;
  159. }
  160. max_queue_size = atoi(av[1]);
  161. nb_senders = atoi(av[2]);
  162. sender_min_load = atoi(av[3]);
  163. sender_max_load = atoi(av[4]);
  164. nb_receivers = atoi(av[5]);
  165. receiver_min_load = atoi(av[6]);
  166. receiver_max_load = atoi(av[7]);
  167. if (max_queue_size <= 0 ||
  168. nb_senders <= 0 || sender_min_load <= 0 || sender_max_load <= 0 ||
  169. nb_receivers <= 0 || receiver_min_load <= 0 || receiver_max_load <= 0) {
  170. av_log(NULL, AV_LOG_ERROR, "negative values not allowed\n");
  171. return 1;
  172. }
  173. av_log(NULL, AV_LOG_INFO, "qsize:%d / %d senders sending [%d-%d] / "
  174. "%d receivers receiving [%d-%d]\n", max_queue_size,
  175. nb_senders, sender_min_load, sender_max_load,
  176. nb_receivers, receiver_min_load, receiver_max_load);
  177. senders = av_calloc(nb_senders, sizeof(*senders));
  178. receivers = av_calloc(nb_receivers, sizeof(*receivers));
  179. if (!senders || !receivers) {
  180. ret = AVERROR(ENOMEM);
  181. goto end;
  182. }
  183. ret = av_thread_message_queue_alloc(&queue, max_queue_size, sizeof(struct message));
  184. if (ret < 0)
  185. goto end;
  186. av_thread_message_queue_set_free_func(queue, free_frame);
  187. #define SPAWN_THREADS(type) do { \
  188. for (i = 0; i < nb_##type##s; i++) { \
  189. struct type##_data *td = &type##s[i]; \
  190. \
  191. td->id = i; \
  192. td->queue = queue; \
  193. td->workload = get_workload(type##_min_load, type##_max_load); \
  194. \
  195. ret = pthread_create(&td->tid, NULL, type##_thread, td); \
  196. if (ret) { \
  197. const int err = AVERROR(ret); \
  198. av_log(NULL, AV_LOG_ERROR, "Unable to start " AV_STRINGIFY(type) \
  199. " thread: %s\n", av_err2str(err)); \
  200. goto end; \
  201. } \
  202. } \
  203. } while (0)
  204. #define WAIT_THREADS(type) do { \
  205. for (i = 0; i < nb_##type##s; i++) { \
  206. struct type##_data *td = &type##s[i]; \
  207. \
  208. ret = pthread_join(td->tid, NULL); \
  209. if (ret) { \
  210. const int err = AVERROR(ret); \
  211. av_log(NULL, AV_LOG_ERROR, "Unable to join " AV_STRINGIFY(type) \
  212. " thread: %s\n", av_err2str(err)); \
  213. goto end; \
  214. } \
  215. } \
  216. } while (0)
  217. SPAWN_THREADS(receiver);
  218. SPAWN_THREADS(sender);
  219. WAIT_THREADS(sender);
  220. WAIT_THREADS(receiver);
  221. end:
  222. av_thread_message_queue_free(&queue);
  223. av_freep(&senders);
  224. av_freep(&receivers);
  225. if (ret < 0 && ret != AVERROR_EOF) {
  226. av_log(NULL, AV_LOG_ERROR, "Error: %s\n", av_err2str(ret));
  227. return 1;
  228. }
  229. return 0;
  230. }