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.

270 lines
8.2KB

  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/random_seed.h"
  23. #include "libavutil/avstring.h"
  24. #include "libavutil/intreadwrite.h"
  25. #include "internal.h"
  26. #include "network.h"
  27. #include "os_support.h"
  28. #include "rtpenc_chain.h"
  29. struct SAPState {
  30. uint8_t *ann;
  31. int ann_size;
  32. URLContext *ann_fd;
  33. int64_t last_time;
  34. };
  35. static int sap_write_close(AVFormatContext *s)
  36. {
  37. struct SAPState *sap = s->priv_data;
  38. int i;
  39. for (i = 0; i < s->nb_streams; i++) {
  40. AVFormatContext *rtpctx = s->streams[i]->priv_data;
  41. if (!rtpctx)
  42. continue;
  43. av_write_trailer(rtpctx);
  44. url_fclose(rtpctx->pb);
  45. av_metadata_free(&rtpctx->streams[0]->metadata);
  46. av_metadata_free(&rtpctx->metadata);
  47. av_free(rtpctx->streams[0]->codec->extradata);
  48. av_free(rtpctx->streams[0]->codec);
  49. av_free(rtpctx->streams[0]->info);
  50. av_free(rtpctx->streams[0]);
  51. av_free(rtpctx);
  52. s->streams[i]->priv_data = NULL;
  53. }
  54. if (sap->last_time && sap->ann && sap->ann_fd) {
  55. sap->ann[0] |= 4; /* Session deletion*/
  56. url_write(sap->ann_fd, sap->ann, sap->ann_size);
  57. }
  58. av_freep(&sap->ann);
  59. if (sap->ann_fd)
  60. url_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. 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 (find_info_tag(buf, sizeof(buf), "announce_port", option_list)) {
  87. port = strtol(buf, NULL, 10);
  88. }
  89. if (find_info_tag(buf, sizeof(buf), "same_port", option_list)) {
  90. same_port = strtol(buf, NULL, 10);
  91. }
  92. if (find_info_tag(buf, sizeof(buf), "ttl", option_list)) {
  93. ttl = strtol(buf, NULL, 10);
  94. }
  95. if (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, *ai = NULL;
  101. memset(&hints, 0, sizeof(hints));
  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. contexts = av_mallocz(sizeof(AVFormatContext*) * s->nb_streams);
  128. if (!contexts) {
  129. ret = AVERROR(ENOMEM);
  130. goto fail;
  131. }
  132. s->start_time_realtime = av_gettime();
  133. for (i = 0; i < s->nb_streams; i++) {
  134. URLContext *fd;
  135. ff_url_join(url, sizeof(url), "rtp", NULL, host, base_port,
  136. "?ttl=%d", ttl);
  137. if (!same_port)
  138. base_port += 2;
  139. ret = url_open(&fd, url, URL_WRONLY);
  140. if (ret) {
  141. ret = AVERROR(EIO);
  142. goto fail;
  143. }
  144. s->streams[i]->priv_data = contexts[i] =
  145. ff_rtp_chain_mux_open(s, s->streams[i], fd, 0);
  146. av_strlcpy(contexts[i]->filename, url, sizeof(contexts[i]->filename));
  147. }
  148. ff_url_join(url, sizeof(url), "udp", NULL, announce_addr, port,
  149. "?ttl=%d&connect=1", ttl);
  150. ret = url_open(&sap->ann_fd, url, URL_WRONLY);
  151. if (ret) {
  152. ret = AVERROR(EIO);
  153. goto fail;
  154. }
  155. udp_fd = url_get_file_handle(sap->ann_fd);
  156. if (getsockname(udp_fd, (struct sockaddr*) &localaddr, &addrlen)) {
  157. ret = AVERROR(EIO);
  158. goto fail;
  159. }
  160. if (localaddr.ss_family != AF_INET
  161. #if HAVE_STRUCT_SOCKADDR_IN6
  162. && localaddr.ss_family != AF_INET6
  163. #endif
  164. ) {
  165. av_log(s, AV_LOG_ERROR, "Unsupported protocol family\n");
  166. ret = AVERROR(EIO);
  167. goto fail;
  168. }
  169. sap->ann_size = 8192;
  170. sap->ann = av_mallocz(sap->ann_size);
  171. if (!sap->ann) {
  172. ret = AVERROR(EIO);
  173. goto fail;
  174. }
  175. sap->ann[pos] = (1 << 5);
  176. #if HAVE_STRUCT_SOCKADDR_IN6
  177. if (localaddr.ss_family == AF_INET6)
  178. sap->ann[pos] |= 0x10;
  179. #endif
  180. pos++;
  181. sap->ann[pos++] = 0; /* Authentication length */
  182. AV_WB16(&sap->ann[pos], av_get_random_seed());
  183. pos += 2;
  184. if (localaddr.ss_family == AF_INET) {
  185. memcpy(&sap->ann[pos], &((struct sockaddr_in*)&localaddr)->sin_addr,
  186. sizeof(struct in_addr));
  187. pos += sizeof(struct in_addr);
  188. #if HAVE_STRUCT_SOCKADDR_IN6
  189. } else {
  190. memcpy(&sap->ann[pos], &((struct sockaddr_in6*)&localaddr)->sin6_addr,
  191. sizeof(struct in6_addr));
  192. pos += sizeof(struct in6_addr);
  193. #endif
  194. }
  195. av_strlcpy(&sap->ann[pos], "application/sdp", sap->ann_size - pos);
  196. pos += strlen(&sap->ann[pos]) + 1;
  197. if (avf_sdp_create(contexts, s->nb_streams, &sap->ann[pos],
  198. sap->ann_size - pos)) {
  199. ret = AVERROR_INVALIDDATA;
  200. goto fail;
  201. }
  202. av_freep(&contexts);
  203. av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", &sap->ann[pos]);
  204. pos += strlen(&sap->ann[pos]);
  205. sap->ann_size = pos;
  206. if (sap->ann_size > url_get_max_packet_size(sap->ann_fd)) {
  207. av_log(s, AV_LOG_ERROR, "Announcement too large to send in one "
  208. "packet\n");
  209. goto fail;
  210. }
  211. return 0;
  212. fail:
  213. av_free(contexts);
  214. sap_write_close(s);
  215. return ret;
  216. }
  217. static int sap_write_packet(AVFormatContext *s, AVPacket *pkt)
  218. {
  219. AVFormatContext *rtpctx;
  220. struct SAPState *sap = s->priv_data;
  221. int64_t now = av_gettime();
  222. if (!sap->last_time || now - sap->last_time > 5000000) {
  223. int ret = url_write(sap->ann_fd, sap->ann, sap->ann_size);
  224. /* Don't abort even if we get "Destination unreachable" */
  225. if (ret < 0 && ret != FF_NETERROR(ECONNREFUSED))
  226. return ret;
  227. sap->last_time = now;
  228. }
  229. rtpctx = s->streams[pkt->stream_index]->priv_data;
  230. return ff_write_chained(rtpctx, 0, pkt, s);
  231. }
  232. AVOutputFormat ff_sap_muxer = {
  233. "sap",
  234. NULL_IF_CONFIG_SMALL("SAP output format"),
  235. NULL,
  236. NULL,
  237. sizeof(struct SAPState),
  238. CODEC_ID_AAC,
  239. CODEC_ID_MPEG4,
  240. sap_write_header,
  241. sap_write_packet,
  242. sap_write_close,
  243. .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
  244. };