zmqsend.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 c, *src_buf, *recv_buf;
  48. int recv_buf_size, ret = 0;
  49. void *zmq_ctx, *socket;
  50. const char *bind_address = "tcp://localhost:5555";
  51. const char *infilename = NULL;
  52. FILE *infile = NULL;
  53. zmq_msg_t msg;
  54. while ((c = getopt(argc, argv, "b:hi:")) != -1) {
  55. switch (c) {
  56. case 'b':
  57. bind_address = optarg;
  58. break;
  59. case 'h':
  60. usage();
  61. return 0;
  62. case 'i':
  63. infilename = optarg;
  64. break;
  65. case '?':
  66. return 1;
  67. }
  68. }
  69. if (!infilename || !strcmp(infilename, "-")) {
  70. infilename = "stdin";
  71. infile = stdin;
  72. } else {
  73. infile = fopen(infilename, "r");
  74. }
  75. if (!infile) {
  76. av_log(NULL, AV_LOG_ERROR,
  77. "Impossible to open input file '%s': %s\n", infilename, strerror(errno));
  78. return 1;
  79. }
  80. zmq_ctx = zmq_ctx_new();
  81. if (!zmq_ctx) {
  82. av_log(NULL, AV_LOG_ERROR,
  83. "Could not create ZMQ context: %s\n", zmq_strerror(errno));
  84. return 1;
  85. }
  86. socket = zmq_socket(zmq_ctx, ZMQ_REQ);
  87. if (!socket) {
  88. av_log(NULL, AV_LOG_ERROR,
  89. "Could not create ZMQ socket: %s\n", zmq_strerror(errno));
  90. ret = 1;
  91. goto end;
  92. }
  93. if (zmq_connect(socket, bind_address) == -1) {
  94. av_log(NULL, AV_LOG_ERROR, "Could not bind ZMQ responder to address '%s': %s\n",
  95. bind_address, zmq_strerror(errno));
  96. ret = 1;
  97. goto end;
  98. }
  99. /* grab the input and store it in src */
  100. av_bprint_init(&src, 1, AV_BPRINT_SIZE_UNLIMITED);
  101. while ((c = fgetc(infile)) != EOF)
  102. av_bprint_chars(&src, c, 1);
  103. av_bprint_chars(&src, 0, 1);
  104. if (!av_bprint_is_complete(&src)) {
  105. av_log(NULL, AV_LOG_ERROR, "Could not allocate a buffer for the source string\n");
  106. av_bprint_finalize(&src, NULL);
  107. ret = 1;
  108. goto end;
  109. }
  110. av_bprint_finalize(&src, &src_buf);
  111. if (zmq_send(socket, src_buf, strlen(src_buf), 0) == -1) {
  112. av_log(NULL, AV_LOG_ERROR, "Could not send message: %s\n", zmq_strerror(errno));
  113. ret = 1;
  114. goto end;
  115. }
  116. if (zmq_msg_init(&msg) == -1) {
  117. av_log(NULL, AV_LOG_ERROR,
  118. "Could not initialize receiving message: %s\n", zmq_strerror(errno));
  119. ret = 1;
  120. goto end;
  121. }
  122. if (zmq_msg_recv(&msg, socket, 0) == -1) {
  123. av_log(NULL, AV_LOG_ERROR,
  124. "Could not receive message: %s\n", zmq_strerror(errno));
  125. zmq_msg_close(&msg);
  126. ret = 1;
  127. goto end;
  128. }
  129. recv_buf_size = zmq_msg_size(&msg) + 1;
  130. recv_buf = av_malloc(recv_buf_size);
  131. if (!recv_buf) {
  132. av_log(NULL, AV_LOG_ERROR,
  133. "Could not allocate receiving message buffer\n");
  134. zmq_msg_close(&msg);
  135. ret = 1;
  136. goto end;
  137. }
  138. memcpy(recv_buf, zmq_msg_data(&msg), recv_buf_size);
  139. recv_buf[recv_buf_size-1] = 0;
  140. printf("%s\n", recv_buf);
  141. zmq_msg_close(&msg);
  142. av_free(recv_buf);
  143. end:
  144. zmq_close(socket);
  145. zmq_ctx_destroy(zmq_ctx);
  146. return ret;
  147. }