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.

273 lines
8.7KB

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