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.

240 lines
6.8KB

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