sapdec.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. avformat_close_input(&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. &s->interrupt_callback, NULL);
  81. if (ret)
  82. goto fail;
  83. while (1) {
  84. int addr_type, auth_len;
  85. int pos;
  86. ret = ffurl_read(sap->ann_fd, recvbuf, sizeof(recvbuf) - 1);
  87. if (ret == AVERROR(EAGAIN))
  88. continue;
  89. if (ret < 0)
  90. goto fail;
  91. recvbuf[ret] = '\0'; /* Null terminate for easier parsing */
  92. if (ret < 8) {
  93. av_log(s, AV_LOG_WARNING, "Received too short packet\n");
  94. continue;
  95. }
  96. if ((recvbuf[0] & 0xe0) != 0x20) {
  97. av_log(s, AV_LOG_WARNING, "Unsupported SAP version packet "
  98. "received\n");
  99. continue;
  100. }
  101. if (recvbuf[0] & 0x04) {
  102. av_log(s, AV_LOG_WARNING, "Received stream deletion "
  103. "announcement\n");
  104. continue;
  105. }
  106. addr_type = recvbuf[0] & 0x10;
  107. auth_len = recvbuf[1];
  108. sap->hash = AV_RB16(&recvbuf[2]);
  109. pos = 4;
  110. if (addr_type)
  111. pos += 16; /* IPv6 */
  112. else
  113. pos += 4; /* IPv4 */
  114. pos += auth_len * 4;
  115. if (pos + 4 >= ret) {
  116. av_log(s, AV_LOG_WARNING, "Received too short packet\n");
  117. continue;
  118. }
  119. #define MIME "application/sdp"
  120. if (strcmp(&recvbuf[pos], MIME) == 0) {
  121. pos += strlen(MIME) + 1;
  122. } else if (strncmp(&recvbuf[pos], "v=0\r\n", 5) == 0) {
  123. // Direct SDP without a mime type
  124. } else {
  125. av_log(s, AV_LOG_WARNING, "Unsupported mime type %s\n",
  126. &recvbuf[pos]);
  127. continue;
  128. }
  129. sap->sdp = av_strdup(&recvbuf[pos]);
  130. break;
  131. }
  132. av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sap->sdp);
  133. ffio_init_context(&sap->sdp_pb, sap->sdp, strlen(sap->sdp), 0, NULL, NULL,
  134. NULL, NULL);
  135. infmt = av_find_input_format("sdp");
  136. if (!infmt)
  137. goto fail;
  138. sap->sdp_ctx = avformat_alloc_context();
  139. if (!sap->sdp_ctx) {
  140. ret = AVERROR(ENOMEM);
  141. goto fail;
  142. }
  143. sap->sdp_ctx->max_delay = s->max_delay;
  144. sap->sdp_ctx->pb = &sap->sdp_pb;
  145. sap->sdp_ctx->interrupt_callback = s->interrupt_callback;
  146. ret = avformat_open_input(&sap->sdp_ctx, "temp.sdp", infmt, NULL);
  147. if (ret < 0)
  148. goto fail;
  149. if (sap->sdp_ctx->ctx_flags & AVFMTCTX_NOHEADER)
  150. s->ctx_flags |= AVFMTCTX_NOHEADER;
  151. for (i = 0; i < sap->sdp_ctx->nb_streams; i++) {
  152. AVStream *st = avformat_new_stream(s, NULL);
  153. if (!st) {
  154. ret = AVERROR(ENOMEM);
  155. goto fail;
  156. }
  157. st->id = i;
  158. avcodec_copy_context(st->codec, sap->sdp_ctx->streams[i]->codec);
  159. st->time_base = sap->sdp_ctx->streams[i]->time_base;
  160. }
  161. return 0;
  162. fail:
  163. sap_read_close(s);
  164. return ret;
  165. }
  166. static int sap_fetch_packet(AVFormatContext *s, AVPacket *pkt)
  167. {
  168. struct SAPState *sap = s->priv_data;
  169. int fd = ffurl_get_file_handle(sap->ann_fd);
  170. int n, ret;
  171. struct pollfd p = {fd, POLLIN, 0};
  172. uint8_t recvbuf[1500];
  173. if (sap->eof)
  174. return AVERROR_EOF;
  175. while (1) {
  176. n = poll(&p, 1, 0);
  177. if (n <= 0 || !(p.revents & POLLIN))
  178. break;
  179. ret = ffurl_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 = avformat_new_stream(s, NULL);
  197. if (!st) {
  198. av_free_packet(pkt);
  199. return AVERROR(ENOMEM);
  200. }
  201. st->id = i;
  202. avcodec_copy_context(st->codec, sap->sdp_ctx->streams[i]->codec);
  203. st->time_base = sap->sdp_ctx->streams[i]->time_base;
  204. }
  205. }
  206. return ret;
  207. }
  208. AVInputFormat ff_sap_demuxer = {
  209. .name = "sap",
  210. .long_name = NULL_IF_CONFIG_SMALL("SAP input format"),
  211. .priv_data_size = sizeof(struct SAPState),
  212. .read_probe = sap_probe,
  213. .read_header = sap_read_header,
  214. .read_packet = sap_fetch_packet,
  215. .read_close = sap_read_close,
  216. .flags = AVFMT_NOFILE,
  217. };