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.

284 lines
9.0KB

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