You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

265 lines
8.3KB

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