sapenc.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 "libavutil/time.h"
  27. #include "internal.h"
  28. #include "network.h"
  29. #include "os_support.h"
  30. #include "rtpenc_chain.h"
  31. #include "url.h"
  32. struct SAPState {
  33. uint8_t *ann;
  34. int ann_size;
  35. URLContext *ann_fd;
  36. int64_t last_time;
  37. };
  38. static int sap_write_close(AVFormatContext *s)
  39. {
  40. struct SAPState *sap = s->priv_data;
  41. int i;
  42. for (i = 0; i < s->nb_streams; i++) {
  43. AVFormatContext *rtpctx = s->streams[i]->priv_data;
  44. if (!rtpctx)
  45. continue;
  46. av_write_trailer(rtpctx);
  47. avio_close(rtpctx->pb);
  48. avformat_free_context(rtpctx);
  49. s->streams[i]->priv_data = NULL;
  50. }
  51. if (sap->last_time && sap->ann && sap->ann_fd) {
  52. sap->ann[0] |= 4; /* Session deletion*/
  53. ffurl_write(sap->ann_fd, sap->ann, sap->ann_size);
  54. }
  55. av_freep(&sap->ann);
  56. if (sap->ann_fd)
  57. ffurl_close(sap->ann_fd);
  58. ff_network_close();
  59. return 0;
  60. }
  61. static int sap_write_header(AVFormatContext *s)
  62. {
  63. struct SAPState *sap = s->priv_data;
  64. char host[1024], path[1024], url[1024], announce_addr[50] = "";
  65. char *option_list;
  66. int port = 9875, base_port = 5004, i, pos = 0, same_port = 0, ttl = 255;
  67. AVFormatContext **contexts = NULL;
  68. int ret = 0;
  69. struct sockaddr_storage localaddr;
  70. socklen_t addrlen = sizeof(localaddr);
  71. int udp_fd;
  72. if (!ff_network_init())
  73. return AVERROR(EIO);
  74. /* extract hostname and port */
  75. av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &base_port,
  76. path, sizeof(path), s->filename);
  77. if (base_port < 0)
  78. base_port = 5004;
  79. /* search for options */
  80. option_list = strrchr(path, '?');
  81. if (option_list) {
  82. char buf[50];
  83. if (av_find_info_tag(buf, sizeof(buf), "announce_port", option_list)) {
  84. port = strtol(buf, NULL, 10);
  85. }
  86. if (av_find_info_tag(buf, sizeof(buf), "same_port", option_list)) {
  87. same_port = strtol(buf, NULL, 10);
  88. }
  89. if (av_find_info_tag(buf, sizeof(buf), "ttl", option_list)) {
  90. ttl = strtol(buf, NULL, 10);
  91. }
  92. if (av_find_info_tag(buf, sizeof(buf), "announce_addr", option_list)) {
  93. av_strlcpy(announce_addr, buf, sizeof(announce_addr));
  94. }
  95. }
  96. if (!announce_addr[0]) {
  97. struct addrinfo hints = { 0 }, *ai = NULL;
  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. ret = ff_rtp_chain_mux_open(&contexts[i], s, s->streams[i], fd, 0);
  141. if (ret < 0)
  142. goto fail;
  143. s->streams[i]->priv_data = contexts[i];
  144. av_strlcpy(contexts[i]->filename, url, sizeof(contexts[i]->filename));
  145. }
  146. ff_url_join(url, sizeof(url), "udp", NULL, announce_addr, port,
  147. "?ttl=%d&connect=1", ttl);
  148. ret = ffurl_open(&sap->ann_fd, url, AVIO_FLAG_WRITE,
  149. &s->interrupt_callback, NULL);
  150. if (ret) {
  151. ret = AVERROR(EIO);
  152. goto fail;
  153. }
  154. udp_fd = ffurl_get_file_handle(sap->ann_fd);
  155. if (getsockname(udp_fd, (struct sockaddr*) &localaddr, &addrlen)) {
  156. ret = AVERROR(EIO);
  157. goto fail;
  158. }
  159. if (localaddr.ss_family != AF_INET
  160. #if HAVE_STRUCT_SOCKADDR_IN6
  161. && localaddr.ss_family != AF_INET6
  162. #endif
  163. ) {
  164. av_log(s, AV_LOG_ERROR, "Unsupported protocol family\n");
  165. ret = AVERROR(EIO);
  166. goto fail;
  167. }
  168. sap->ann_size = 8192;
  169. sap->ann = av_mallocz(sap->ann_size);
  170. if (!sap->ann) {
  171. ret = AVERROR(EIO);
  172. goto fail;
  173. }
  174. sap->ann[pos] = (1 << 5);
  175. #if HAVE_STRUCT_SOCKADDR_IN6
  176. if (localaddr.ss_family == AF_INET6)
  177. sap->ann[pos] |= 0x10;
  178. #endif
  179. pos++;
  180. sap->ann[pos++] = 0; /* Authentication length */
  181. AV_WB16(&sap->ann[pos], av_get_random_seed());
  182. pos += 2;
  183. if (localaddr.ss_family == AF_INET) {
  184. memcpy(&sap->ann[pos], &((struct sockaddr_in*)&localaddr)->sin_addr,
  185. sizeof(struct in_addr));
  186. pos += sizeof(struct in_addr);
  187. #if HAVE_STRUCT_SOCKADDR_IN6
  188. } else {
  189. memcpy(&sap->ann[pos], &((struct sockaddr_in6*)&localaddr)->sin6_addr,
  190. sizeof(struct in6_addr));
  191. pos += sizeof(struct in6_addr);
  192. #endif
  193. }
  194. av_strlcpy(&sap->ann[pos], "application/sdp", sap->ann_size - pos);
  195. pos += strlen(&sap->ann[pos]) + 1;
  196. if (av_sdp_create(contexts, s->nb_streams, &sap->ann[pos],
  197. sap->ann_size - pos)) {
  198. ret = AVERROR_INVALIDDATA;
  199. goto fail;
  200. }
  201. av_freep(&contexts);
  202. av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", &sap->ann[pos]);
  203. pos += strlen(&sap->ann[pos]);
  204. sap->ann_size = pos;
  205. if (sap->ann_size > sap->ann_fd->max_packet_size) {
  206. av_log(s, AV_LOG_ERROR, "Announcement too large to send in one "
  207. "packet\n");
  208. goto fail;
  209. }
  210. return 0;
  211. fail:
  212. av_free(contexts);
  213. sap_write_close(s);
  214. return ret;
  215. }
  216. static int sap_write_packet(AVFormatContext *s, AVPacket *pkt)
  217. {
  218. AVFormatContext *rtpctx;
  219. struct SAPState *sap = s->priv_data;
  220. int64_t now = av_gettime();
  221. if (!sap->last_time || now - sap->last_time > 5000000) {
  222. int ret = ffurl_write(sap->ann_fd, sap->ann, sap->ann_size);
  223. /* Don't abort even if we get "Destination unreachable" */
  224. if (ret < 0 && ret != AVERROR(ECONNREFUSED))
  225. return ret;
  226. sap->last_time = now;
  227. }
  228. rtpctx = s->streams[pkt->stream_index]->priv_data;
  229. return ff_write_chained(rtpctx, 0, pkt, s);
  230. }
  231. AVOutputFormat ff_sap_muxer = {
  232. .name = "sap",
  233. .long_name = NULL_IF_CONFIG_SMALL("SAP output"),
  234. .priv_data_size = sizeof(struct SAPState),
  235. .audio_codec = AV_CODEC_ID_AAC,
  236. .video_codec = AV_CODEC_ID_MPEG4,
  237. .write_header = sap_write_header,
  238. .write_packet = sap_write_packet,
  239. .write_trailer = sap_write_close,
  240. .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
  241. };