sapenc.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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_FLAG_WRITE, &s->interrupt_callback, NULL);
  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_FLAG_WRITE,
  147. &s->interrupt_callback, NULL);
  148. if (ret) {
  149. ret = AVERROR(EIO);
  150. goto fail;
  151. }
  152. udp_fd = ffurl_get_file_handle(sap->ann_fd);
  153. if (getsockname(udp_fd, (struct sockaddr*) &localaddr, &addrlen)) {
  154. ret = AVERROR(EIO);
  155. goto fail;
  156. }
  157. if (localaddr.ss_family != AF_INET
  158. #if HAVE_STRUCT_SOCKADDR_IN6
  159. && localaddr.ss_family != AF_INET6
  160. #endif
  161. ) {
  162. av_log(s, AV_LOG_ERROR, "Unsupported protocol family\n");
  163. ret = AVERROR(EIO);
  164. goto fail;
  165. }
  166. sap->ann_size = 8192;
  167. sap->ann = av_mallocz(sap->ann_size);
  168. if (!sap->ann) {
  169. ret = AVERROR(EIO);
  170. goto fail;
  171. }
  172. sap->ann[pos] = (1 << 5);
  173. #if HAVE_STRUCT_SOCKADDR_IN6
  174. if (localaddr.ss_family == AF_INET6)
  175. sap->ann[pos] |= 0x10;
  176. #endif
  177. pos++;
  178. sap->ann[pos++] = 0; /* Authentication length */
  179. AV_WB16(&sap->ann[pos], av_get_random_seed());
  180. pos += 2;
  181. if (localaddr.ss_family == AF_INET) {
  182. memcpy(&sap->ann[pos], &((struct sockaddr_in*)&localaddr)->sin_addr,
  183. sizeof(struct in_addr));
  184. pos += sizeof(struct in_addr);
  185. #if HAVE_STRUCT_SOCKADDR_IN6
  186. } else {
  187. memcpy(&sap->ann[pos], &((struct sockaddr_in6*)&localaddr)->sin6_addr,
  188. sizeof(struct in6_addr));
  189. pos += sizeof(struct in6_addr);
  190. #endif
  191. }
  192. av_strlcpy(&sap->ann[pos], "application/sdp", sap->ann_size - pos);
  193. pos += strlen(&sap->ann[pos]) + 1;
  194. if (av_sdp_create(contexts, s->nb_streams, &sap->ann[pos],
  195. sap->ann_size - pos)) {
  196. ret = AVERROR_INVALIDDATA;
  197. goto fail;
  198. }
  199. av_freep(&contexts);
  200. av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", &sap->ann[pos]);
  201. pos += strlen(&sap->ann[pos]);
  202. sap->ann_size = pos;
  203. if (sap->ann_size > sap->ann_fd->max_packet_size) {
  204. av_log(s, AV_LOG_ERROR, "Announcement too large to send in one "
  205. "packet\n");
  206. goto fail;
  207. }
  208. return 0;
  209. fail:
  210. av_free(contexts);
  211. sap_write_close(s);
  212. return ret;
  213. }
  214. static int sap_write_packet(AVFormatContext *s, AVPacket *pkt)
  215. {
  216. AVFormatContext *rtpctx;
  217. struct SAPState *sap = s->priv_data;
  218. int64_t now = av_gettime();
  219. if (!sap->last_time || now - sap->last_time > 5000000) {
  220. int ret = ffurl_write(sap->ann_fd, sap->ann, sap->ann_size);
  221. /* Don't abort even if we get "Destination unreachable" */
  222. if (ret < 0 && ret != AVERROR(ECONNREFUSED))
  223. return ret;
  224. sap->last_time = now;
  225. }
  226. rtpctx = s->streams[pkt->stream_index]->priv_data;
  227. return ff_write_chained(rtpctx, 0, pkt, s);
  228. }
  229. AVOutputFormat ff_sap_muxer = {
  230. .name = "sap",
  231. .long_name = NULL_IF_CONFIG_SMALL("SAP output format"),
  232. .priv_data_size = sizeof(struct SAPState),
  233. .audio_codec = CODEC_ID_AAC,
  234. .video_codec = CODEC_ID_MPEG4,
  235. .write_header = sap_write_header,
  236. .write_packet = sap_write_packet,
  237. .write_trailer = sap_write_close,
  238. .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
  239. };