sapdec.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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_POLL_H
  28. #include <poll.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. struct pollfd p = {fd, POLLIN, 0};
  168. uint8_t recvbuf[1500];
  169. if (sap->eof)
  170. return AVERROR_EOF;
  171. while (1) {
  172. n = poll(&p, 1, 0);
  173. if (n <= 0 || !(p.revents & POLLIN))
  174. break;
  175. ret = url_read(sap->ann_fd, recvbuf, sizeof(recvbuf));
  176. if (ret >= 8) {
  177. uint16_t hash = AV_RB16(&recvbuf[2]);
  178. /* Should ideally check the source IP address, too */
  179. if (recvbuf[0] & 0x04 && hash == sap->hash) {
  180. /* Stream deletion */
  181. sap->eof = 1;
  182. return AVERROR_EOF;
  183. }
  184. }
  185. }
  186. ret = av_read_frame(sap->sdp_ctx, pkt);
  187. if (ret < 0)
  188. return ret;
  189. if (s->ctx_flags & AVFMTCTX_NOHEADER) {
  190. while (sap->sdp_ctx->nb_streams > s->nb_streams) {
  191. int i = s->nb_streams;
  192. AVStream *st = av_new_stream(s, i);
  193. if (!st) {
  194. av_free_packet(pkt);
  195. return AVERROR(ENOMEM);
  196. }
  197. avcodec_copy_context(st->codec, sap->sdp_ctx->streams[i]->codec);
  198. st->time_base = sap->sdp_ctx->streams[i]->time_base;
  199. }
  200. }
  201. return ret;
  202. }
  203. AVInputFormat ff_sap_demuxer = {
  204. "sap",
  205. NULL_IF_CONFIG_SMALL("SAP input format"),
  206. sizeof(struct SAPState),
  207. sap_probe,
  208. sap_read_header,
  209. sap_fetch_packet,
  210. sap_read_close,
  211. .flags = AVFMT_NOFILE,
  212. };