ffmpeg搭建http服务器的案例

妖狐艹你老母 2023-01-15 11:29 239阅读 0赞

ffmpeg搭建http服务器的案例:
https://www.ffmpeg.org/doxygen/trunk/http\_multiclient\_8c-example.html\#a17

  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. /** * @file * libavformat multi-client network API usage example. * * @example http_multiclient.c * This example will serve a file without decoding or demuxing it over http. * Multiple clients can connect and will receive the same file. */
  23. #include <libavformat/avformat.h>
  24. #include <libavutil/opt.h>
  25. #include <unistd.h>
  26. static void process_client(AVIOContext *client, const char *in_uri)
  27. {
  28. AVIOContext *input = NULL;
  29. uint8_t buf[1024];
  30. int ret, n, reply_code;
  31. uint8_t *resource = NULL;
  32. while ((ret = avio_handshake(client)) > 0) {
  33. av_opt_get(client, "resource", AV_OPT_SEARCH_CHILDREN, &resource);
  34. // check for strlen(resource) is necessary, because av_opt_get()
  35. // may return empty string.
  36. if (resource && strlen(resource))
  37. break;
  38. av_freep(&resource);
  39. }
  40. if (ret < 0)
  41. goto end;
  42. av_log(client, AV_LOG_TRACE, "resource=%p\n", resource);
  43. if (resource && resource[0] == '/' && !strcmp((resource + 1), in_uri)) {
  44. reply_code = 200;
  45. } else {
  46. reply_code = AVERROR_HTTP_NOT_FOUND;
  47. }
  48. if ((ret = av_opt_set_int(client, "reply_code", reply_code, AV_OPT_SEARCH_CHILDREN)) < 0) {
  49. av_log(client, AV_LOG_ERROR, "Failed to set reply_code: %s.\n", av_err2str(ret));
  50. goto end;
  51. }
  52. av_log(client, AV_LOG_TRACE, "Set reply code to %d\n", reply_code);
  53. while ((ret = avio_handshake(client)) > 0);
  54. if (ret < 0)
  55. goto end;
  56. fprintf(stderr, "Handshake performed.\n");
  57. if (reply_code != 200)
  58. goto end;
  59. fprintf(stderr, "Opening input file.\n");
  60. if ((ret = avio_open2(&input, in_uri, AVIO_FLAG_READ, NULL, NULL)) < 0) {
  61. av_log(input, AV_LOG_ERROR, "Failed to open input: %s: %s.\n", in_uri,
  62. av_err2str(ret));
  63. goto end;
  64. }
  65. for(;;) {
  66. n = avio_read(input, buf, sizeof(buf));
  67. if (n < 0) {
  68. if (n == AVERROR_EOF)
  69. break;
  70. av_log(input, AV_LOG_ERROR, "Error reading from input: %s.\n",
  71. av_err2str(n));
  72. break;
  73. }
  74. avio_write(client, buf, n);
  75. avio_flush(client);
  76. }
  77. end:
  78. fprintf(stderr, "Flushing client\n");
  79. avio_flush(client);
  80. fprintf(stderr, "Closing client\n");
  81. avio_close(client);
  82. fprintf(stderr, "Closing input\n");
  83. avio_close(input);
  84. av_freep(&resource);
  85. }
  86. int main(int argc, char **argv)
  87. {
  88. AVDictionary *options = NULL;
  89. AVIOContext *client = NULL, *server = NULL;
  90. const char *in_uri, *out_uri;
  91. int ret, pid;
  92. av_log_set_level(AV_LOG_TRACE);
  93. if (argc < 3) {
  94. printf("usage: %s input http://hostname[:port]\n"
  95. "API example program to serve http to multiple clients.\n"
  96. "\n", argv[0]);
  97. return 1;
  98. }
  99. in_uri = argv[1];
  100. out_uri = argv[2];
  101. avformat_network_init();
  102. if ((ret = av_dict_set(&options, "listen", "2", 0)) < 0) {
  103. fprintf(stderr, "Failed to set listen mode for server: %s\n", av_err2str(ret));
  104. return ret;
  105. }
  106. if ((ret = avio_open2(&server, out_uri, AVIO_FLAG_WRITE, NULL, &options)) < 0) {
  107. fprintf(stderr, "Failed to open server: %s\n", av_err2str(ret));
  108. return ret;
  109. }
  110. fprintf(stderr, "Entering main loop.\n");
  111. for(;;) {
  112. if ((ret = avio_accept(server, &client)) < 0)
  113. goto end;
  114. fprintf(stderr, "Accepted client, forking process.\n");
  115. // XXX: Since we don't reap our children and don't ignore signals
  116. // this produces zombie processes.
  117. pid = fork();
  118. if (pid < 0) {
  119. perror("Fork failed");
  120. ret = AVERROR(errno);
  121. goto end;
  122. }
  123. if (pid == 0) {
  124. fprintf(stderr, "In child.\n");
  125. process_client(client, in_uri);
  126. avio_close(server);
  127. exit(0);
  128. }
  129. if (pid > 0)
  130. avio_close(client);
  131. }
  132. end:
  133. avio_close(server);
  134. if (ret < 0 && ret != AVERROR_EOF) {
  135. fprintf(stderr, "Some errors occurred: %s\n", av_err2str(ret));
  136. return 1;
  137. }
  138. return 0;
  139. }

发表评论

表情:
评论列表 (有 0 条评论,239人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Http服务器

    一、Nginx介绍 > Nginx是一个高性能的HTTP和方向代理服务,也是一个IMAP/POP3/SMTP服务。 > > 其特点是占用内存少,并发能力强,中国大陆使用

    相关 自己https服务器详解

    研究了一天多,终于明白了域名、SSL证书和项目之间的关系了!!域名和云服务器都是买的腾讯云,SSL证书是在腾讯云里免费领取的。我推荐腾讯云是因为我觉得好用而已,不是为了打广告,