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.

238 lines
6.9KB

  1. /*
  2. * Session Announcement Protocol (RFC 2974) demuxer
  3. * Copyright (c) 2010 Martin Storsjo
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. #include "url.h"
  29. #if HAVE_POLL_H
  30. #include <poll.h>
  31. #endif
  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. avformat_close_input(&sap->sdp_ctx);
  51. if (sap->ann_fd)
  52. ffurl_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. {
  59. struct SAPState *sap = s->priv_data;
  60. char host[1024], path[1024], url[1024];
  61. uint8_t recvbuf[1500];
  62. int port;
  63. int ret, i;
  64. AVInputFormat* infmt;
  65. if (!ff_network_init())
  66. return AVERROR(EIO);
  67. av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port,
  68. path, sizeof(path), s->filename);
  69. if (port < 0)
  70. port = 9875;
  71. if (!host[0]) {
  72. /* Listen for announcements on sap.mcast.net if no host was specified */
  73. av_strlcpy(host, "224.2.127.254", sizeof(host));
  74. }
  75. ff_url_join(url, sizeof(url), "udp", NULL, host, port, "?localport=%d",
  76. port);
  77. ret = ffurl_open(&sap->ann_fd, url, AVIO_FLAG_READ,
  78. &s->interrupt_callback, NULL);
  79. if (ret)
  80. goto fail;
  81. while (1) {
  82. int addr_type, auth_len;
  83. int pos;
  84. ret = ffurl_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. sap->sdp_ctx->pb = &sap->sdp_pb;
  143. sap->sdp_ctx->interrupt_callback = s->interrupt_callback;
  144. ret = avformat_open_input(&sap->sdp_ctx, "temp.sdp", infmt, NULL);
  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 = avformat_new_stream(s, NULL);
  151. if (!st) {
  152. ret = AVERROR(ENOMEM);
  153. goto fail;
  154. }
  155. st->id = i;
  156. avcodec_copy_context(st->codec, sap->sdp_ctx->streams[i]->codec);
  157. st->time_base = sap->sdp_ctx->streams[i]->time_base;
  158. }
  159. return 0;
  160. fail:
  161. sap_read_close(s);
  162. return ret;
  163. }
  164. static int sap_fetch_packet(AVFormatContext *s, AVPacket *pkt)
  165. {
  166. struct SAPState *sap = s->priv_data;
  167. int fd = ffurl_get_file_handle(sap->ann_fd);
  168. int n, ret;
  169. struct pollfd p = {fd, POLLIN, 0};
  170. uint8_t recvbuf[1500];
  171. if (sap->eof)
  172. return AVERROR_EOF;
  173. while (1) {
  174. n = poll(&p, 1, 0);
  175. if (n <= 0 || !(p.revents & POLLIN))
  176. break;
  177. ret = ffurl_read(sap->ann_fd, recvbuf, sizeof(recvbuf));
  178. if (ret >= 8) {
  179. uint16_t hash = AV_RB16(&recvbuf[2]);
  180. /* Should ideally check the source IP address, too */
  181. if (recvbuf[0] & 0x04 && hash == sap->hash) {
  182. /* Stream deletion */
  183. sap->eof = 1;
  184. return AVERROR_EOF;
  185. }
  186. }
  187. }
  188. ret = av_read_frame(sap->sdp_ctx, pkt);
  189. if (ret < 0)
  190. return ret;
  191. if (s->ctx_flags & AVFMTCTX_NOHEADER) {
  192. while (sap->sdp_ctx->nb_streams > s->nb_streams) {
  193. int i = s->nb_streams;
  194. AVStream *st = avformat_new_stream(s, NULL);
  195. if (!st) {
  196. av_free_packet(pkt);
  197. return AVERROR(ENOMEM);
  198. }
  199. st->id = i;
  200. avcodec_copy_context(st->codec, sap->sdp_ctx->streams[i]->codec);
  201. st->time_base = sap->sdp_ctx->streams[i]->time_base;
  202. }
  203. }
  204. return ret;
  205. }
  206. AVInputFormat ff_sap_demuxer = {
  207. .name = "sap",
  208. .long_name = NULL_IF_CONFIG_SMALL("SAP input format"),
  209. .priv_data_size = sizeof(struct SAPState),
  210. .read_probe = sap_probe,
  211. .read_header = sap_read_header,
  212. .read_packet = sap_fetch_packet,
  213. .read_close = sap_read_close,
  214. .flags = AVFMT_NOFILE,
  215. };