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.

249 lines
7.2KB

  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. #include "rtpdec.h"
  30. #if HAVE_POLL_H
  31. #include <poll.h>
  32. #endif
  33. struct SAPState {
  34. URLContext *ann_fd;
  35. AVFormatContext *sdp_ctx;
  36. AVIOContext sdp_pb;
  37. uint16_t hash;
  38. char *sdp;
  39. int eof;
  40. const URLProtocol **protocols;
  41. };
  42. static int sap_probe(AVProbeData *p)
  43. {
  44. if (av_strstart(p->filename, "sap:", NULL))
  45. return AVPROBE_SCORE_MAX;
  46. return 0;
  47. }
  48. static int sap_read_close(AVFormatContext *s)
  49. {
  50. struct SAPState *sap = s->priv_data;
  51. if (sap->sdp_ctx)
  52. avformat_close_input(&sap->sdp_ctx);
  53. if (sap->ann_fd)
  54. ffurl_close(sap->ann_fd);
  55. av_freep(&sap->protocols);
  56. av_freep(&sap->sdp);
  57. ff_network_close();
  58. return 0;
  59. }
  60. static int sap_read_header(AVFormatContext *s)
  61. {
  62. struct SAPState *sap = s->priv_data;
  63. char host[1024], path[1024], url[1024];
  64. uint8_t recvbuf[RTP_MAX_PACKET_LENGTH];
  65. int port;
  66. int ret, i;
  67. AVInputFormat* infmt;
  68. if (!ff_network_init())
  69. return AVERROR(EIO);
  70. av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port,
  71. path, sizeof(path), s->filename);
  72. if (port < 0)
  73. port = 9875;
  74. if (!host[0]) {
  75. /* Listen for announcements on sap.mcast.net if no host was specified */
  76. av_strlcpy(host, "224.2.127.254", sizeof(host));
  77. }
  78. sap->protocols = ffurl_get_protocols(s->protocol_whitelist,
  79. s->protocol_blacklist);
  80. if (!sap->protocols) {
  81. ret = AVERROR(ENOMEM);
  82. goto fail;
  83. }
  84. ff_url_join(url, sizeof(url), "udp", NULL, host, port, "?localport=%d",
  85. port);
  86. ret = ffurl_open(&sap->ann_fd, url, AVIO_FLAG_READ,
  87. &s->interrupt_callback, NULL, sap->protocols, NULL);
  88. if (ret)
  89. goto fail;
  90. while (1) {
  91. int addr_type, auth_len;
  92. int pos;
  93. ret = ffurl_read(sap->ann_fd, recvbuf, sizeof(recvbuf) - 1);
  94. if (ret == AVERROR(EAGAIN))
  95. continue;
  96. if (ret < 0)
  97. goto fail;
  98. recvbuf[ret] = '\0'; /* Null terminate for easier parsing */
  99. if (ret < 8) {
  100. av_log(s, AV_LOG_WARNING, "Received too short packet\n");
  101. continue;
  102. }
  103. if ((recvbuf[0] & 0xe0) != 0x20) {
  104. av_log(s, AV_LOG_WARNING, "Unsupported SAP version packet "
  105. "received\n");
  106. continue;
  107. }
  108. if (recvbuf[0] & 0x04) {
  109. av_log(s, AV_LOG_WARNING, "Received stream deletion "
  110. "announcement\n");
  111. continue;
  112. }
  113. addr_type = recvbuf[0] & 0x10;
  114. auth_len = recvbuf[1];
  115. sap->hash = AV_RB16(&recvbuf[2]);
  116. pos = 4;
  117. if (addr_type)
  118. pos += 16; /* IPv6 */
  119. else
  120. pos += 4; /* IPv4 */
  121. pos += auth_len * 4;
  122. if (pos + 4 >= ret) {
  123. av_log(s, AV_LOG_WARNING, "Received too short packet\n");
  124. continue;
  125. }
  126. #define MIME "application/sdp"
  127. if (strcmp(&recvbuf[pos], MIME) == 0) {
  128. pos += strlen(MIME) + 1;
  129. } else if (strncmp(&recvbuf[pos], "v=0\r\n", 5) == 0) {
  130. // Direct SDP without a mime type
  131. } else {
  132. av_log(s, AV_LOG_WARNING, "Unsupported mime type %s\n",
  133. &recvbuf[pos]);
  134. continue;
  135. }
  136. sap->sdp = av_strdup(&recvbuf[pos]);
  137. break;
  138. }
  139. av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sap->sdp);
  140. ffio_init_context(&sap->sdp_pb, sap->sdp, strlen(sap->sdp), 0, NULL, NULL,
  141. NULL, NULL);
  142. infmt = av_find_input_format("sdp");
  143. if (!infmt)
  144. goto fail;
  145. sap->sdp_ctx = avformat_alloc_context();
  146. if (!sap->sdp_ctx) {
  147. ret = AVERROR(ENOMEM);
  148. goto fail;
  149. }
  150. sap->sdp_ctx->max_delay = s->max_delay;
  151. sap->sdp_ctx->pb = &sap->sdp_pb;
  152. sap->sdp_ctx->interrupt_callback = s->interrupt_callback;
  153. ret = avformat_open_input(&sap->sdp_ctx, "temp.sdp", infmt, NULL);
  154. if (ret < 0)
  155. goto fail;
  156. if (sap->sdp_ctx->ctx_flags & AVFMTCTX_NOHEADER)
  157. s->ctx_flags |= AVFMTCTX_NOHEADER;
  158. for (i = 0; i < sap->sdp_ctx->nb_streams; i++) {
  159. AVStream *st = avformat_new_stream(s, NULL);
  160. if (!st) {
  161. ret = AVERROR(ENOMEM);
  162. goto fail;
  163. }
  164. st->id = i;
  165. avcodec_parameters_copy(st->codecpar, sap->sdp_ctx->streams[i]->codecpar);
  166. st->time_base = sap->sdp_ctx->streams[i]->time_base;
  167. }
  168. return 0;
  169. fail:
  170. sap_read_close(s);
  171. return ret;
  172. }
  173. static int sap_fetch_packet(AVFormatContext *s, AVPacket *pkt)
  174. {
  175. struct SAPState *sap = s->priv_data;
  176. int fd = ffurl_get_file_handle(sap->ann_fd);
  177. int n, ret;
  178. struct pollfd p = {fd, POLLIN, 0};
  179. uint8_t recvbuf[RTP_MAX_PACKET_LENGTH];
  180. if (sap->eof)
  181. return AVERROR_EOF;
  182. while (1) {
  183. n = poll(&p, 1, 0);
  184. if (n <= 0 || !(p.revents & POLLIN))
  185. break;
  186. ret = ffurl_read(sap->ann_fd, recvbuf, sizeof(recvbuf));
  187. if (ret >= 8) {
  188. uint16_t hash = AV_RB16(&recvbuf[2]);
  189. /* Should ideally check the source IP address, too */
  190. if (recvbuf[0] & 0x04 && hash == sap->hash) {
  191. /* Stream deletion */
  192. sap->eof = 1;
  193. return AVERROR_EOF;
  194. }
  195. }
  196. }
  197. ret = av_read_frame(sap->sdp_ctx, pkt);
  198. if (ret < 0)
  199. return ret;
  200. if (s->ctx_flags & AVFMTCTX_NOHEADER) {
  201. while (sap->sdp_ctx->nb_streams > s->nb_streams) {
  202. int i = s->nb_streams;
  203. AVStream *st = avformat_new_stream(s, NULL);
  204. if (!st) {
  205. av_packet_unref(pkt);
  206. return AVERROR(ENOMEM);
  207. }
  208. st->id = i;
  209. avcodec_parameters_copy(st->codecpar, sap->sdp_ctx->streams[i]->codecpar);
  210. st->time_base = sap->sdp_ctx->streams[i]->time_base;
  211. }
  212. }
  213. return ret;
  214. }
  215. AVInputFormat ff_sap_demuxer = {
  216. .name = "sap",
  217. .long_name = NULL_IF_CONFIG_SMALL("SAP input"),
  218. .priv_data_size = sizeof(struct SAPState),
  219. .read_probe = sap_probe,
  220. .read_header = sap_read_header,
  221. .read_packet = sap_fetch_packet,
  222. .read_close = sap_read_close,
  223. .flags = AVFMT_NOFILE,
  224. };