http_multiclient.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2015 Stephan Holljes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. /**
  23. * @file
  24. * libavformat multi-client network API usage example.
  25. *
  26. * @example http_multiclient.c
  27. * This example will serve a file without decoding or demuxing it over http.
  28. * Multiple clients can connect and will receive the same file.
  29. */
  30. #include <libavformat/avformat.h>
  31. #include <libavutil/opt.h>
  32. #include <unistd.h>
  33. void process_client(AVIOContext *client, const char *in_uri)
  34. {
  35. AVIOContext *input = NULL;
  36. uint8_t buf[1024];
  37. int ret, n, reply_code;
  38. char *resource = NULL;
  39. while ((ret = avio_handshake(client)) > 0) {
  40. av_opt_get(client, "resource", AV_OPT_SEARCH_CHILDREN, &resource);
  41. // check for strlen(resource) is necessary, because av_opt_get()
  42. // may return empty string.
  43. if (resource && strlen(resource))
  44. break;
  45. }
  46. if (ret < 0)
  47. goto end;
  48. av_log(client, AV_LOG_TRACE, "resource=%p\n", resource);
  49. if (resource && resource[0] == '/' && !strcmp((resource + 1), in_uri)) {
  50. reply_code = 200;
  51. } else {
  52. reply_code = AVERROR_HTTP_NOT_FOUND;
  53. }
  54. if ((ret = av_opt_set_int(client, "reply_code", reply_code, AV_OPT_SEARCH_CHILDREN)) < 0) {
  55. av_log(client, AV_LOG_ERROR, "Failed to set reply_code: %s.\n", av_err2str(ret));
  56. goto end;
  57. }
  58. av_log(client, AV_LOG_TRACE, "Set reply code to %d\n", reply_code);
  59. while ((ret = avio_handshake(client)) > 0);
  60. if (ret < 0)
  61. goto end;
  62. fprintf(stderr, "Handshake performed.\n");
  63. if (reply_code != 200)
  64. goto end;
  65. fprintf(stderr, "Opening input file.\n");
  66. if ((ret = avio_open2(&input, in_uri, AVIO_FLAG_READ, NULL, NULL)) < 0) {
  67. av_log(input, AV_LOG_ERROR, "Failed to open input: %s: %s.\n", in_uri,
  68. av_err2str(ret));
  69. goto end;
  70. }
  71. for(;;) {
  72. n = avio_read(input, buf, sizeof(buf));
  73. if (n < 0) {
  74. if (n == AVERROR_EOF)
  75. break;
  76. av_log(input, AV_LOG_ERROR, "Error reading from input: %s.\n",
  77. av_err2str(n));
  78. break;
  79. }
  80. avio_write(client, buf, n);
  81. avio_flush(client);
  82. }
  83. end:
  84. fprintf(stderr, "Flushing client\n");
  85. avio_flush(client);
  86. fprintf(stderr, "Closing client\n");
  87. avio_close(client);
  88. fprintf(stderr, "Closing input\n");
  89. avio_close(input);
  90. }
  91. int main(int argc, char **argv)
  92. {
  93. av_log_set_level(AV_LOG_TRACE);
  94. AVDictionary *options = NULL;
  95. AVIOContext *client = NULL, *server = NULL;
  96. const char *in_uri, *out_uri;
  97. int ret, pid;
  98. if (argc < 3) {
  99. printf("usage: %s input http://hostname[:port]\n"
  100. "API example program to serve http to multiple clients.\n"
  101. "\n", argv[0]);
  102. return 1;
  103. }
  104. in_uri = argv[1];
  105. out_uri = argv[2];
  106. av_register_all();
  107. avformat_network_init();
  108. if ((ret = av_dict_set(&options, "listen", "2", 0)) < 0) {
  109. fprintf(stderr, "Failed to set listen mode for server: %s\n", av_err2str(ret));
  110. return ret;
  111. }
  112. if ((ret = avio_open2(&server, out_uri, AVIO_FLAG_WRITE, NULL, &options)) < 0) {
  113. fprintf(stderr, "Failed to open server: %s\n", av_err2str(ret));
  114. return ret;
  115. }
  116. fprintf(stderr, "Entering main loop.\n");
  117. for(;;) {
  118. if ((ret = avio_accept(server, &client)) < 0)
  119. goto end;
  120. fprintf(stderr, "Accepted client, forking process.\n");
  121. // XXX: Since we don't reap our children and don't ignore signals
  122. // this produces zombie processes.
  123. pid = fork();
  124. if (pid < 0) {
  125. perror("Fork failed");
  126. ret = AVERROR(errno);
  127. goto end;
  128. }
  129. if (pid == 0) {
  130. fprintf(stderr, "In child.\n");
  131. process_client(client, in_uri);
  132. avio_close(server);
  133. exit(0);
  134. }
  135. if (pid > 0)
  136. avio_close(client);
  137. }
  138. end:
  139. avio_close(server);
  140. if (ret < 0 && ret != AVERROR_EOF) {
  141. fprintf(stderr, "Some errors occurred: %s\n", av_err2str(ret));
  142. return 1;
  143. }
  144. return 0;
  145. }