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 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/dict.h"
  26. #include "libavutil/intreadwrite.h"
  27. #include "libavutil/time.h"
  28. #include "libavutil/dict.h"
  29. #include "internal.h"
  30. #include "network.h"
  31. #include "os_support.h"
  32. #include "rtpenc_chain.h"
  33. #include "url.h"
  34. struct SAPState {
  35. uint8_t *ann;
  36. int ann_size;
  37. URLContext *ann_fd;
  38. int64_t last_time;
  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->ann);
  58. if (sap->ann_fd)
  59. ffurl_close(sap->ann_fd);
  60. ff_network_close();
  61. return 0;
  62. }
  63. static int sap_write_header(AVFormatContext *s)
  64. {
  65. struct SAPState *sap = s->priv_data;
  66. char host[1024], path[1024], url[1024], announce_addr[50] = "";
  67. char *option_list;
  68. int port = 9875, base_port = 5004, i, pos = 0, same_port = 0, ttl = 255;
  69. AVFormatContext **contexts = NULL;
  70. int ret = 0;
  71. struct sockaddr_storage localaddr;
  72. socklen_t addrlen = sizeof(localaddr);
  73. int udp_fd;
  74. AVDictionaryEntry* title = av_dict_get(s->metadata, "title", NULL, 0);
  75. if (!ff_network_init())
  76. return AVERROR(EIO);
  77. /* extract hostname and port */
  78. av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &base_port,
  79. path, sizeof(path), s->filename);
  80. if (base_port < 0)
  81. base_port = 5004;
  82. /* search for options */
  83. option_list = strrchr(path, '?');
  84. if (option_list) {
  85. char buf[50];
  86. if (av_find_info_tag(buf, sizeof(buf), "announce_port", option_list)) {
  87. port = strtol(buf, NULL, 10);
  88. }
  89. if (av_find_info_tag(buf, sizeof(buf), "same_port", option_list)) {
  90. same_port = strtol(buf, NULL, 10);
  91. }
  92. if (av_find_info_tag(buf, sizeof(buf), "ttl", option_list)) {
  93. ttl = strtol(buf, NULL, 10);
  94. }
  95. if (av_find_info_tag(buf, sizeof(buf), "announce_addr", option_list)) {
  96. av_strlcpy(announce_addr, buf, sizeof(announce_addr));
  97. }
  98. }
  99. if (!announce_addr[0]) {
  100. struct addrinfo hints = { 0 }, *ai = NULL;
  101. hints.ai_family = AF_UNSPEC;
  102. if (getaddrinfo(host, NULL, &hints, &ai)) {
  103. av_log(s, AV_LOG_ERROR, "Unable to resolve %s\n", host);
  104. ret = AVERROR(EIO);
  105. goto fail;
  106. }
  107. if (ai->ai_family == AF_INET) {
  108. /* Also known as sap.mcast.net */
  109. av_strlcpy(announce_addr, "224.2.127.254", sizeof(announce_addr));
  110. #if HAVE_STRUCT_SOCKADDR_IN6
  111. } else if (ai->ai_family == AF_INET6) {
  112. /* With IPv6, you can use the same destination in many different
  113. * multicast subnets, to choose how far you want it routed.
  114. * This one is intended to be routed globally. */
  115. av_strlcpy(announce_addr, "ff0e::2:7ffe", sizeof(announce_addr));
  116. #endif
  117. } else {
  118. freeaddrinfo(ai);
  119. av_log(s, AV_LOG_ERROR, "Host %s resolved to unsupported "
  120. "address family\n", host);
  121. ret = AVERROR(EIO);
  122. goto fail;
  123. }
  124. freeaddrinfo(ai);
  125. }
  126. contexts = av_mallocz(sizeof(AVFormatContext*) * s->nb_streams);
  127. if (!contexts) {
  128. ret = AVERROR(ENOMEM);
  129. goto fail;
  130. }
  131. s->start_time_realtime = av_gettime();
  132. for (i = 0; i < s->nb_streams; i++) {
  133. URLContext *fd;
  134. ff_url_join(url, sizeof(url), "rtp", NULL, host, base_port,
  135. "?ttl=%d", ttl);
  136. if (!same_port)
  137. base_port += 2;
  138. ret = ffurl_open(&fd, url, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
  139. if (ret) {
  140. ret = AVERROR(EIO);
  141. goto fail;
  142. }
  143. ret = ff_rtp_chain_mux_open(&contexts[i], s, s->streams[i], fd, 0, i);
  144. if (ret < 0)
  145. goto fail;
  146. s->streams[i]->priv_data = contexts[i];
  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();
  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. };