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.

253 lines
7.9KB

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