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