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.

267 lines
8.1KB

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