sapenc.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Session Announcement Protocol (RFC 2974) muxer
  3. * Copyright (c) 2010 Martin Storsjo
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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/parseutils.h"
  23. #include "libavutil/random_seed.h"
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/intreadwrite.h"
  26. #include "internal.h"
  27. #include "network.h"
  28. #include "os_support.h"
  29. #include "rtpenc_chain.h"
  30. struct SAPState {
  31. uint8_t *ann;
  32. int ann_size;
  33. URLContext *ann_fd;
  34. int64_t last_time;
  35. };
  36. static int sap_write_close(AVFormatContext *s)
  37. {
  38. struct SAPState *sap = s->priv_data;
  39. int i;
  40. for (i = 0; i < s->nb_streams; i++) {
  41. AVFormatContext *rtpctx = s->streams[i]->priv_data;
  42. if (!rtpctx)
  43. continue;
  44. av_write_trailer(rtpctx);
  45. avio_close(rtpctx->pb);
  46. avformat_free_context(rtpctx);
  47. s->streams[i]->priv_data = NULL;
  48. }
  49. if (sap->last_time && sap->ann && sap->ann_fd) {
  50. sap->ann[0] |= 4; /* Session deletion*/
  51. url_write(sap->ann_fd, sap->ann, sap->ann_size);
  52. }
  53. av_freep(&sap->ann);
  54. if (sap->ann_fd)
  55. url_close(sap->ann_fd);
  56. ff_network_close();
  57. return 0;
  58. }
  59. static int sap_write_header(AVFormatContext *s)
  60. {
  61. struct SAPState *sap = s->priv_data;
  62. char host[1024], path[1024], url[1024], announce_addr[50] = "";
  63. char *option_list;
  64. int port = 9875, base_port = 5004, i, pos = 0, same_port = 0, ttl = 255;
  65. AVFormatContext **contexts = NULL;
  66. int ret = 0;
  67. struct sockaddr_storage localaddr;
  68. socklen_t addrlen = sizeof(localaddr);
  69. int udp_fd;
  70. if (!ff_network_init())
  71. return AVERROR(EIO);
  72. /* extract hostname and port */
  73. av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &base_port,
  74. path, sizeof(path), s->filename);
  75. if (base_port < 0)
  76. base_port = 5004;
  77. /* search for options */
  78. option_list = strrchr(path, '?');
  79. if (option_list) {
  80. char buf[50];
  81. if (av_find_info_tag(buf, sizeof(buf), "announce_port", option_list)) {
  82. port = strtol(buf, NULL, 10);
  83. }
  84. if (av_find_info_tag(buf, sizeof(buf), "same_port", option_list)) {
  85. same_port = strtol(buf, NULL, 10);
  86. }
  87. if (av_find_info_tag(buf, sizeof(buf), "ttl", option_list)) {
  88. ttl = strtol(buf, NULL, 10);
  89. }
  90. if (av_find_info_tag(buf, sizeof(buf), "announce_addr", option_list)) {
  91. av_strlcpy(announce_addr, buf, sizeof(announce_addr));
  92. }
  93. }
  94. if (!announce_addr[0]) {
  95. struct addrinfo hints, *ai = NULL;
  96. memset(&hints, 0, sizeof(hints));
  97. hints.ai_family = AF_UNSPEC;
  98. if (getaddrinfo(host, NULL, &hints, &ai)) {
  99. av_log(s, AV_LOG_ERROR, "Unable to resolve %s\n", host);
  100. ret = AVERROR(EIO);
  101. goto fail;
  102. }
  103. if (ai->ai_family == AF_INET) {
  104. /* Also known as sap.mcast.net */
  105. av_strlcpy(announce_addr, "224.2.127.254", sizeof(announce_addr));
  106. #if HAVE_STRUCT_SOCKADDR_IN6
  107. } else if (ai->ai_family == AF_INET6) {
  108. /* With IPv6, you can use the same destination in many different
  109. * multicast subnets, to choose how far you want it routed.
  110. * This one is intended to be routed globally. */
  111. av_strlcpy(announce_addr, "ff0e::2:7ffe", sizeof(announce_addr));
  112. #endif
  113. } else {
  114. freeaddrinfo(ai);
  115. av_log(s, AV_LOG_ERROR, "Host %s resolved to unsupported "
  116. "address family\n", host);
  117. ret = AVERROR(EIO);
  118. goto fail;
  119. }
  120. freeaddrinfo(ai);
  121. }
  122. contexts = av_mallocz(sizeof(AVFormatContext*) * s->nb_streams);
  123. if (!contexts) {
  124. ret = AVERROR(ENOMEM);
  125. goto fail;
  126. }
  127. s->start_time_realtime = av_gettime();
  128. for (i = 0; i < s->nb_streams; i++) {
  129. URLContext *fd;
  130. ff_url_join(url, sizeof(url), "rtp", NULL, host, base_port,
  131. "?ttl=%d", ttl);
  132. if (!same_port)
  133. base_port += 2;
  134. ret = url_open(&fd, url, URL_WRONLY);
  135. if (ret) {
  136. ret = AVERROR(EIO);
  137. goto fail;
  138. }
  139. s->streams[i]->priv_data = contexts[i] =
  140. ff_rtp_chain_mux_open(s, s->streams[i], fd, 0);
  141. av_strlcpy(contexts[i]->filename, url, sizeof(contexts[i]->filename));
  142. }
  143. ff_url_join(url, sizeof(url), "udp", NULL, announce_addr, port,
  144. "?ttl=%d&connect=1", ttl);
  145. ret = url_open(&sap->ann_fd, url, URL_WRONLY);
  146. if (ret) {
  147. ret = AVERROR(EIO);
  148. goto fail;
  149. }
  150. udp_fd = url_get_file_handle(sap->ann_fd);
  151. if (getsockname(udp_fd, (struct sockaddr*) &localaddr, &addrlen)) {
  152. ret = AVERROR(EIO);
  153. goto fail;
  154. }
  155. if (localaddr.ss_family != AF_INET
  156. #if HAVE_STRUCT_SOCKADDR_IN6
  157. && localaddr.ss_family != AF_INET6
  158. #endif
  159. ) {
  160. av_log(s, AV_LOG_ERROR, "Unsupported protocol family\n");
  161. ret = AVERROR(EIO);
  162. goto fail;
  163. }
  164. sap->ann_size = 8192;
  165. sap->ann = av_mallocz(sap->ann_size);
  166. if (!sap->ann) {
  167. ret = AVERROR(EIO);
  168. goto fail;
  169. }
  170. sap->ann[pos] = (1 << 5);
  171. #if HAVE_STRUCT_SOCKADDR_IN6
  172. if (localaddr.ss_family == AF_INET6)
  173. sap->ann[pos] |= 0x10;
  174. #endif
  175. pos++;
  176. sap->ann[pos++] = 0; /* Authentication length */
  177. AV_WB16(&sap->ann[pos], av_get_random_seed());
  178. pos += 2;
  179. if (localaddr.ss_family == AF_INET) {
  180. memcpy(&sap->ann[pos], &((struct sockaddr_in*)&localaddr)->sin_addr,
  181. sizeof(struct in_addr));
  182. pos += sizeof(struct in_addr);
  183. #if HAVE_STRUCT_SOCKADDR_IN6
  184. } else {
  185. memcpy(&sap->ann[pos], &((struct sockaddr_in6*)&localaddr)->sin6_addr,
  186. sizeof(struct in6_addr));
  187. pos += sizeof(struct in6_addr);
  188. #endif
  189. }
  190. av_strlcpy(&sap->ann[pos], "application/sdp", sap->ann_size - pos);
  191. pos += strlen(&sap->ann[pos]) + 1;
  192. if (avf_sdp_create(contexts, s->nb_streams, &sap->ann[pos],
  193. sap->ann_size - pos)) {
  194. ret = AVERROR_INVALIDDATA;
  195. goto fail;
  196. }
  197. av_freep(&contexts);
  198. av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", &sap->ann[pos]);
  199. pos += strlen(&sap->ann[pos]);
  200. sap->ann_size = pos;
  201. if (sap->ann_size > url_get_max_packet_size(sap->ann_fd)) {
  202. av_log(s, AV_LOG_ERROR, "Announcement too large to send in one "
  203. "packet\n");
  204. goto fail;
  205. }
  206. return 0;
  207. fail:
  208. av_free(contexts);
  209. sap_write_close(s);
  210. return ret;
  211. }
  212. static int sap_write_packet(AVFormatContext *s, AVPacket *pkt)
  213. {
  214. AVFormatContext *rtpctx;
  215. struct SAPState *sap = s->priv_data;
  216. int64_t now = av_gettime();
  217. if (!sap->last_time || now - sap->last_time > 5000000) {
  218. int ret = url_write(sap->ann_fd, sap->ann, sap->ann_size);
  219. /* Don't abort even if we get "Destination unreachable" */
  220. if (ret < 0 && ret != AVERROR(ECONNREFUSED))
  221. return ret;
  222. sap->last_time = now;
  223. }
  224. rtpctx = s->streams[pkt->stream_index]->priv_data;
  225. return ff_write_chained(rtpctx, 0, pkt, s);
  226. }
  227. AVOutputFormat ff_sap_muxer = {
  228. "sap",
  229. NULL_IF_CONFIG_SMALL("SAP output format"),
  230. NULL,
  231. NULL,
  232. sizeof(struct SAPState),
  233. CODEC_ID_AAC,
  234. CODEC_ID_MPEG4,
  235. sap_write_header,
  236. sap_write_packet,
  237. sap_write_close,
  238. .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
  239. };