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.

268 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]->info);
  48. av_free(rtpctx->streams[0]);
  49. av_free(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. url_write(sap->ann_fd, sap->ann, sap->ann_size);
  55. }
  56. av_freep(&sap->ann);
  57. if (sap->ann_fd)
  58. url_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. if (!ff_network_init())
  74. return AVERROR(EIO);
  75. /* extract hostname and port */
  76. av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &base_port,
  77. path, sizeof(path), s->filename);
  78. if (base_port < 0)
  79. base_port = 5004;
  80. /* search for options */
  81. option_list = strrchr(path, '?');
  82. if (option_list) {
  83. char buf[50];
  84. if (find_info_tag(buf, sizeof(buf), "announce_port", option_list)) {
  85. port = strtol(buf, NULL, 10);
  86. }
  87. if (find_info_tag(buf, sizeof(buf), "same_port", option_list)) {
  88. same_port = strtol(buf, NULL, 10);
  89. }
  90. if (find_info_tag(buf, sizeof(buf), "ttl", option_list)) {
  91. ttl = strtol(buf, NULL, 10);
  92. }
  93. if (find_info_tag(buf, sizeof(buf), "announce_addr", option_list)) {
  94. av_strlcpy(announce_addr, buf, sizeof(announce_addr));
  95. }
  96. }
  97. if (!announce_addr[0]) {
  98. struct addrinfo hints, *ai = NULL;
  99. memset(&hints, 0, sizeof(hints));
  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. s->start_time_realtime = av_gettime();
  131. for (i = 0; i < s->nb_streams; i++) {
  132. URLContext *fd;
  133. ff_url_join(url, sizeof(url), "rtp", NULL, host, base_port,
  134. "?ttl=%d", ttl);
  135. if (!same_port)
  136. base_port += 2;
  137. ret = url_open(&fd, url, URL_WRONLY);
  138. if (ret) {
  139. ret = AVERROR(EIO);
  140. goto fail;
  141. }
  142. s->streams[i]->priv_data = contexts[i] =
  143. ff_rtp_chain_mux_open(s, s->streams[i], fd, 0);
  144. av_strlcpy(contexts[i]->filename, url, sizeof(contexts[i]->filename));
  145. }
  146. ff_url_join(url, sizeof(url), "udp", NULL, announce_addr, port,
  147. "?ttl=%d&connect=1", ttl);
  148. ret = url_open(&sap->ann_fd, url, URL_WRONLY);
  149. if (ret) {
  150. ret = AVERROR(EIO);
  151. goto fail;
  152. }
  153. udp_fd = url_get_file_handle(sap->ann_fd);
  154. if (getsockname(udp_fd, (struct sockaddr*) &localaddr, &addrlen)) {
  155. ret = AVERROR(EIO);
  156. goto fail;
  157. }
  158. if (localaddr.ss_family != AF_INET
  159. #if HAVE_STRUCT_SOCKADDR_IN6
  160. && localaddr.ss_family != AF_INET6
  161. #endif
  162. ) {
  163. av_log(s, AV_LOG_ERROR, "Unsupported protocol family\n");
  164. ret = AVERROR(EIO);
  165. goto fail;
  166. }
  167. sap->ann_size = 8192;
  168. sap->ann = av_mallocz(sap->ann_size);
  169. if (!sap->ann) {
  170. ret = AVERROR(EIO);
  171. goto fail;
  172. }
  173. sap->ann[pos] = (1 << 5);
  174. #if HAVE_STRUCT_SOCKADDR_IN6
  175. if (localaddr.ss_family == AF_INET6)
  176. sap->ann[pos] |= 0x10;
  177. #endif
  178. pos++;
  179. sap->ann[pos++] = 0; /* Authentication length */
  180. AV_WB16(&sap->ann[pos], av_get_random_seed());
  181. pos += 2;
  182. if (localaddr.ss_family == AF_INET) {
  183. memcpy(&sap->ann[pos], &((struct sockaddr_in*)&localaddr)->sin_addr,
  184. sizeof(struct in_addr));
  185. pos += sizeof(struct in_addr);
  186. #if HAVE_STRUCT_SOCKADDR_IN6
  187. } else {
  188. memcpy(&sap->ann[pos], &((struct sockaddr_in6*)&localaddr)->sin6_addr,
  189. sizeof(struct in6_addr));
  190. pos += sizeof(struct in6_addr);
  191. #endif
  192. }
  193. av_strlcpy(&sap->ann[pos], "application/sdp", sap->ann_size - pos);
  194. pos += strlen(&sap->ann[pos]) + 1;
  195. if (avf_sdp_create(contexts, s->nb_streams, &sap->ann[pos],
  196. sap->ann_size - pos)) {
  197. ret = AVERROR_INVALIDDATA;
  198. goto fail;
  199. }
  200. av_freep(&contexts);
  201. av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", &sap->ann[pos]);
  202. pos += strlen(&sap->ann[pos]);
  203. sap->ann_size = pos;
  204. if (sap->ann_size > url_get_max_packet_size(sap->ann_fd)) {
  205. av_log(s, AV_LOG_ERROR, "Announcement too large to send in one "
  206. "packet\n");
  207. goto fail;
  208. }
  209. return 0;
  210. fail:
  211. av_free(contexts);
  212. sap_write_close(s);
  213. return ret;
  214. }
  215. static int sap_write_packet(AVFormatContext *s, AVPacket *pkt)
  216. {
  217. AVFormatContext *rtpctx;
  218. struct SAPState *sap = s->priv_data;
  219. int64_t now = av_gettime();
  220. if (!sap->last_time || now - sap->last_time > 5000000) {
  221. int ret = url_write(sap->ann_fd, sap->ann, sap->ann_size);
  222. /* Don't abort even if we get "Destination unreachable" */
  223. if (ret < 0 && ret != FF_NETERROR(ECONNREFUSED))
  224. return ret;
  225. sap->last_time = now;
  226. }
  227. rtpctx = s->streams[pkt->stream_index]->priv_data;
  228. return ff_write_chained(rtpctx, 0, pkt, s);
  229. }
  230. AVOutputFormat ff_sap_muxer = {
  231. "sap",
  232. NULL_IF_CONFIG_SMALL("SAP output format"),
  233. NULL,
  234. NULL,
  235. sizeof(struct SAPState),
  236. CODEC_ID_AAC,
  237. CODEC_ID_MPEG4,
  238. sap_write_header,
  239. sap_write_packet,
  240. sap_write_close,
  241. .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
  242. };