zmqsend.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2013 Stefano Sabatini
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "config.h"
  21. #include <zmq.h>
  22. #include "libavutil/mem.h"
  23. #include "libavutil/bprint.h"
  24. #if HAVE_UNISTD_H
  25. #include <unistd.h> /* getopt */
  26. #endif
  27. #if !HAVE_GETOPT
  28. #include "compat/getopt.c"
  29. #endif
  30. /**
  31. * @file
  32. * zmq message sender example, meant to be used with the zmq filters
  33. */
  34. static void usage(void)
  35. {
  36. printf("send message to ZMQ recipient, to use with the zmq filters\n");
  37. printf("usage: zmqsend [OPTIONS]\n");
  38. printf("\n"
  39. "Options:\n"
  40. "-b ADDRESS set bind address\n"
  41. "-h print this help\n"
  42. "-i INFILE set INFILE as input file, stdin if omitted\n");
  43. }
  44. int main(int argc, char **argv)
  45. {
  46. AVBPrint src;
  47. char *src_buf, *recv_buf;
  48. int c;
  49. int recv_buf_size, ret = 0;
  50. void *zmq_ctx, *socket;
  51. const char *bind_address = "tcp://localhost:5555";
  52. const char *infilename = NULL;
  53. FILE *infile = NULL;
  54. zmq_msg_t msg;
  55. while ((c = getopt(argc, argv, "b:hi:")) != -1) {
  56. switch (c) {
  57. case 'b':
  58. bind_address = optarg;
  59. break;
  60. case 'h':
  61. usage();
  62. return 0;
  63. case 'i':
  64. infilename = optarg;
  65. break;
  66. case '?':
  67. return 1;
  68. }
  69. }
  70. if (!infilename || !strcmp(infilename, "-")) {
  71. infilename = "stdin";
  72. infile = stdin;
  73. } else {
  74. infile = fopen(infilename, "r");
  75. }
  76. if (!infile) {
  77. av_log(NULL, AV_LOG_ERROR,
  78. "Impossible to open input file '%s': %s\n", infilename, strerror(errno));
  79. return 1;
  80. }
  81. zmq_ctx = zmq_ctx_new();
  82. if (!zmq_ctx) {
  83. av_log(NULL, AV_LOG_ERROR,
  84. "Could not create ZMQ context: %s\n", zmq_strerror(errno));
  85. return 1;
  86. }
  87. socket = zmq_socket(zmq_ctx, ZMQ_REQ);
  88. if (!socket) {
  89. av_log(NULL, AV_LOG_ERROR,
  90. "Could not create ZMQ socket: %s\n", zmq_strerror(errno));
  91. ret = 1;
  92. goto end;
  93. }
  94. if (zmq_connect(socket, bind_address) == -1) {
  95. av_log(NULL, AV_LOG_ERROR, "Could not bind ZMQ responder to address '%s': %s\n",
  96. bind_address, zmq_strerror(errno));
  97. ret = 1;
  98. goto end;
  99. }
  100. /* grab the input and store it in src */
  101. av_bprint_init(&src, 1, AV_BPRINT_SIZE_UNLIMITED);
  102. while ((c = fgetc(infile)) != EOF)
  103. av_bprint_chars(&src, c, 1);
  104. av_bprint_chars(&src, 0, 1);
  105. if (!av_bprint_is_complete(&src)) {
  106. av_log(NULL, AV_LOG_ERROR, "Could not allocate a buffer for the source string\n");
  107. av_bprint_finalize(&src, NULL);
  108. ret = 1;
  109. goto end;
  110. }
  111. av_bprint_finalize(&src, &src_buf);
  112. if (zmq_send(socket, src_buf, strlen(src_buf), 0) == -1) {
  113. av_log(NULL, AV_LOG_ERROR, "Could not send message: %s\n", zmq_strerror(errno));
  114. ret = 1;
  115. goto end;
  116. }
  117. if (zmq_msg_init(&msg) == -1) {
  118. av_log(NULL, AV_LOG_ERROR,
  119. "Could not initialize receiving message: %s\n", zmq_strerror(errno));
  120. ret = 1;
  121. goto end;
  122. }
  123. if (zmq_msg_recv(&msg, socket, 0) == -1) {
  124. av_log(NULL, AV_LOG_ERROR,
  125. "Could not receive message: %s\n", zmq_strerror(errno));
  126. zmq_msg_close(&msg);
  127. ret = 1;
  128. goto end;
  129. }
  130. recv_buf_size = zmq_msg_size(&msg) + 1;
  131. recv_buf = av_malloc(recv_buf_size);
  132. if (!recv_buf) {
  133. av_log(NULL, AV_LOG_ERROR,
  134. "Could not allocate receiving message buffer\n");
  135. zmq_msg_close(&msg);
  136. ret = 1;
  137. goto end;
  138. }
  139. memcpy(recv_buf, zmq_msg_data(&msg), recv_buf_size - 1);
  140. recv_buf[recv_buf_size-1] = 0;
  141. printf("%s\n", recv_buf);
  142. zmq_msg_close(&msg);
  143. av_free(recv_buf);
  144. end:
  145. zmq_close(socket);
  146. zmq_ctx_destroy(zmq_ctx);
  147. return ret;
  148. }