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.

272 lines
8.6KB

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