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.

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