You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
6.7KB

  1. /*
  2. * Session Announcement Protocol (RFC 2974) demuxer
  3. * Copyright (c) 2010 Martin Storsjo
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #include "libavutil/avstring.h"
  23. #include "libavutil/intreadwrite.h"
  24. #include "network.h"
  25. #include "os_support.h"
  26. #include "internal.h"
  27. #include "avio_internal.h"
  28. #if HAVE_POLL_H
  29. #include <poll.h>
  30. #endif
  31. #include <sys/time.h>
  32. struct SAPState {
  33. URLContext *ann_fd;
  34. AVFormatContext *sdp_ctx;
  35. AVIOContext sdp_pb;
  36. uint16_t hash;
  37. char *sdp;
  38. int eof;
  39. };
  40. static int sap_probe(AVProbeData *p)
  41. {
  42. if (av_strstart(p->filename, "sap:", NULL))
  43. return AVPROBE_SCORE_MAX;
  44. return 0;
  45. }
  46. static int sap_read_close(AVFormatContext *s)
  47. {
  48. struct SAPState *sap = s->priv_data;
  49. if (sap->sdp_ctx)
  50. av_close_input_stream(sap->sdp_ctx);
  51. if (sap->ann_fd)
  52. url_close(sap->ann_fd);
  53. av_freep(&sap->sdp);
  54. ff_network_close();
  55. return 0;
  56. }
  57. static int sap_read_header(AVFormatContext *s,
  58. AVFormatParameters *ap)
  59. {
  60. struct SAPState *sap = s->priv_data;
  61. char host[1024], path[1024], url[1024];
  62. uint8_t recvbuf[1500];
  63. int port;
  64. int ret, i;
  65. AVInputFormat* infmt;
  66. if (!ff_network_init())
  67. return AVERROR(EIO);
  68. av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port,
  69. path, sizeof(path), s->filename);
  70. if (port < 0)
  71. port = 9875;
  72. if (!host[0]) {
  73. /* Listen for announcements on sap.mcast.net if no host was specified */
  74. av_strlcpy(host, "224.2.127.254", sizeof(host));
  75. }
  76. ff_url_join(url, sizeof(url), "udp", NULL, host, port, "?localport=%d",
  77. port);
  78. ret = url_open(&sap->ann_fd, url, URL_RDONLY);
  79. if (ret)
  80. goto fail;
  81. while (1) {
  82. int addr_type, auth_len;
  83. int pos;
  84. ret = url_read(sap->ann_fd, recvbuf, sizeof(recvbuf) - 1);
  85. if (ret == AVERROR(EAGAIN))
  86. continue;
  87. if (ret < 0)
  88. goto fail;
  89. recvbuf[ret] = '\0'; /* Null terminate for easier parsing */
  90. if (ret < 8) {
  91. av_log(s, AV_LOG_WARNING, "Received too short packet\n");
  92. continue;
  93. }
  94. if ((recvbuf[0] & 0xe0) != 0x20) {
  95. av_log(s, AV_LOG_WARNING, "Unsupported SAP version packet "
  96. "received\n");
  97. continue;
  98. }
  99. if (recvbuf[0] & 0x04) {
  100. av_log(s, AV_LOG_WARNING, "Received stream deletion "
  101. "announcement\n");
  102. continue;
  103. }
  104. addr_type = recvbuf[0] & 0x10;
  105. auth_len = recvbuf[1];
  106. sap->hash = AV_RB16(&recvbuf[2]);
  107. pos = 4;
  108. if (addr_type)
  109. pos += 16; /* IPv6 */
  110. else
  111. pos += 4; /* IPv4 */
  112. pos += auth_len * 4;
  113. if (pos + 4 >= ret) {
  114. av_log(s, AV_LOG_WARNING, "Received too short packet\n");
  115. continue;
  116. }
  117. #define MIME "application/sdp"
  118. if (strcmp(&recvbuf[pos], MIME) == 0) {
  119. pos += strlen(MIME) + 1;
  120. } else if (strncmp(&recvbuf[pos], "v=0\r\n", 5) == 0) {
  121. // Direct SDP without a mime type
  122. } else {
  123. av_log(s, AV_LOG_WARNING, "Unsupported mime type %s\n",
  124. &recvbuf[pos]);
  125. continue;
  126. }
  127. sap->sdp = av_strdup(&recvbuf[pos]);
  128. break;
  129. }
  130. av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sap->sdp);
  131. ffio_init_context(&sap->sdp_pb, sap->sdp, strlen(sap->sdp), 0, NULL, NULL,
  132. NULL, NULL);
  133. infmt = av_find_input_format("sdp");
  134. if (!infmt)
  135. goto fail;
  136. sap->sdp_ctx = avformat_alloc_context();
  137. if (!sap->sdp_ctx) {
  138. ret = AVERROR(ENOMEM);
  139. goto fail;
  140. }
  141. sap->sdp_ctx->max_delay = s->max_delay;
  142. ap->prealloced_context = 1;
  143. ret = av_open_input_stream(&sap->sdp_ctx, &sap->sdp_pb, "temp.sdp",
  144. infmt, ap);
  145. if (ret < 0)
  146. goto fail;
  147. if (sap->sdp_ctx->ctx_flags & AVFMTCTX_NOHEADER)
  148. s->ctx_flags |= AVFMTCTX_NOHEADER;
  149. for (i = 0; i < sap->sdp_ctx->nb_streams; i++) {
  150. AVStream *st = av_new_stream(s, i);
  151. if (!st) {
  152. ret = AVERROR(ENOMEM);
  153. goto fail;
  154. }
  155. avcodec_copy_context(st->codec, sap->sdp_ctx->streams[i]->codec);
  156. st->time_base = sap->sdp_ctx->streams[i]->time_base;
  157. }
  158. return 0;
  159. fail:
  160. sap_read_close(s);
  161. return ret;
  162. }
  163. static int sap_fetch_packet(AVFormatContext *s, AVPacket *pkt)
  164. {
  165. struct SAPState *sap = s->priv_data;
  166. int fd = url_get_file_handle(sap->ann_fd);
  167. int n, ret;
  168. struct pollfd p = {fd, POLLIN, 0};
  169. uint8_t recvbuf[1500];
  170. if (sap->eof)
  171. return AVERROR_EOF;
  172. while (1) {
  173. n = poll(&p, 1, 0);
  174. if (n <= 0 || !(p.revents & POLLIN))
  175. break;
  176. ret = url_read(sap->ann_fd, recvbuf, sizeof(recvbuf));
  177. if (ret >= 8) {
  178. uint16_t hash = AV_RB16(&recvbuf[2]);
  179. /* Should ideally check the source IP address, too */
  180. if (recvbuf[0] & 0x04 && hash == sap->hash) {
  181. /* Stream deletion */
  182. sap->eof = 1;
  183. return AVERROR_EOF;
  184. }
  185. }
  186. }
  187. ret = av_read_frame(sap->sdp_ctx, pkt);
  188. if (ret < 0)
  189. return ret;
  190. if (s->ctx_flags & AVFMTCTX_NOHEADER) {
  191. while (sap->sdp_ctx->nb_streams > s->nb_streams) {
  192. int i = s->nb_streams;
  193. AVStream *st = av_new_stream(s, i);
  194. if (!st) {
  195. av_free_packet(pkt);
  196. return AVERROR(ENOMEM);
  197. }
  198. avcodec_copy_context(st->codec, sap->sdp_ctx->streams[i]->codec);
  199. st->time_base = sap->sdp_ctx->streams[i]->time_base;
  200. }
  201. }
  202. return ret;
  203. }
  204. AVInputFormat ff_sap_demuxer = {
  205. "sap",
  206. NULL_IF_CONFIG_SMALL("SAP input format"),
  207. sizeof(struct SAPState),
  208. sap_probe,
  209. sap_read_header,
  210. sap_fetch_packet,
  211. sap_read_close,
  212. .flags = AVFMT_NOFILE,
  213. };