sapenc.c 8.0 KB

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