sapdec.c 6.7 KB

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