00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avformat.h"
00023 #include "libavutil/parseutils.h"
00024 #include "libavutil/random_seed.h"
00025 #include "libavutil/avstring.h"
00026 #include "libavutil/intreadwrite.h"
00027 #include "internal.h"
00028 #include "network.h"
00029 #include "os_support.h"
00030 #include "rtpenc_chain.h"
00031 #include "url.h"
00032
00033 struct SAPState {
00034 uint8_t *ann;
00035 int ann_size;
00036 URLContext *ann_fd;
00037 int64_t last_time;
00038 };
00039
00040 static int sap_write_close(AVFormatContext *s)
00041 {
00042 struct SAPState *sap = s->priv_data;
00043 int i;
00044
00045 for (i = 0; i < s->nb_streams; i++) {
00046 AVFormatContext *rtpctx = s->streams[i]->priv_data;
00047 if (!rtpctx)
00048 continue;
00049 av_write_trailer(rtpctx);
00050 avio_close(rtpctx->pb);
00051 avformat_free_context(rtpctx);
00052 s->streams[i]->priv_data = NULL;
00053 }
00054
00055 if (sap->last_time && sap->ann && sap->ann_fd) {
00056 sap->ann[0] |= 4;
00057 ffurl_write(sap->ann_fd, sap->ann, sap->ann_size);
00058 }
00059
00060 av_freep(&sap->ann);
00061 if (sap->ann_fd)
00062 ffurl_close(sap->ann_fd);
00063 ff_network_close();
00064 return 0;
00065 }
00066
00067 static int sap_write_header(AVFormatContext *s)
00068 {
00069 struct SAPState *sap = s->priv_data;
00070 char host[1024], path[1024], url[1024], announce_addr[50] = "";
00071 char *option_list;
00072 int port = 9875, base_port = 5004, i, pos = 0, same_port = 0, ttl = 255;
00073 AVFormatContext **contexts = NULL;
00074 int ret = 0;
00075 struct sockaddr_storage localaddr;
00076 socklen_t addrlen = sizeof(localaddr);
00077 int udp_fd;
00078
00079 if (!ff_network_init())
00080 return AVERROR(EIO);
00081
00082
00083 av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &base_port,
00084 path, sizeof(path), s->filename);
00085 if (base_port < 0)
00086 base_port = 5004;
00087
00088
00089 option_list = strrchr(path, '?');
00090 if (option_list) {
00091 char buf[50];
00092 if (av_find_info_tag(buf, sizeof(buf), "announce_port", option_list)) {
00093 port = strtol(buf, NULL, 10);
00094 }
00095 if (av_find_info_tag(buf, sizeof(buf), "same_port", option_list)) {
00096 same_port = strtol(buf, NULL, 10);
00097 }
00098 if (av_find_info_tag(buf, sizeof(buf), "ttl", option_list)) {
00099 ttl = strtol(buf, NULL, 10);
00100 }
00101 if (av_find_info_tag(buf, sizeof(buf), "announce_addr", option_list)) {
00102 av_strlcpy(announce_addr, buf, sizeof(announce_addr));
00103 }
00104 }
00105
00106 if (!announce_addr[0]) {
00107 struct addrinfo hints, *ai = NULL;
00108 memset(&hints, 0, sizeof(hints));
00109 hints.ai_family = AF_UNSPEC;
00110 if (getaddrinfo(host, NULL, &hints, &ai)) {
00111 av_log(s, AV_LOG_ERROR, "Unable to resolve %s\n", host);
00112 ret = AVERROR(EIO);
00113 goto fail;
00114 }
00115 if (ai->ai_family == AF_INET) {
00116
00117 av_strlcpy(announce_addr, "224.2.127.254", sizeof(announce_addr));
00118 #if HAVE_STRUCT_SOCKADDR_IN6
00119 } else if (ai->ai_family == AF_INET6) {
00120
00121
00122
00123 av_strlcpy(announce_addr, "ff0e::2:7ffe", sizeof(announce_addr));
00124 #endif
00125 } else {
00126 freeaddrinfo(ai);
00127 av_log(s, AV_LOG_ERROR, "Host %s resolved to unsupported "
00128 "address family\n", host);
00129 ret = AVERROR(EIO);
00130 goto fail;
00131 }
00132 freeaddrinfo(ai);
00133 }
00134
00135 contexts = av_mallocz(sizeof(AVFormatContext*) * s->nb_streams);
00136 if (!contexts) {
00137 ret = AVERROR(ENOMEM);
00138 goto fail;
00139 }
00140
00141 s->start_time_realtime = av_gettime();
00142 for (i = 0; i < s->nb_streams; i++) {
00143 URLContext *fd;
00144
00145 ff_url_join(url, sizeof(url), "rtp", NULL, host, base_port,
00146 "?ttl=%d", ttl);
00147 if (!same_port)
00148 base_port += 2;
00149 ret = ffurl_open(&fd, url, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
00150 if (ret) {
00151 ret = AVERROR(EIO);
00152 goto fail;
00153 }
00154 s->streams[i]->priv_data = contexts[i] =
00155 ff_rtp_chain_mux_open(s, s->streams[i], fd, 0);
00156 av_strlcpy(contexts[i]->filename, url, sizeof(contexts[i]->filename));
00157 }
00158
00159 ff_url_join(url, sizeof(url), "udp", NULL, announce_addr, port,
00160 "?ttl=%d&connect=1", ttl);
00161 ret = ffurl_open(&sap->ann_fd, url, AVIO_FLAG_WRITE,
00162 &s->interrupt_callback, NULL);
00163 if (ret) {
00164 ret = AVERROR(EIO);
00165 goto fail;
00166 }
00167
00168 udp_fd = ffurl_get_file_handle(sap->ann_fd);
00169 if (getsockname(udp_fd, (struct sockaddr*) &localaddr, &addrlen)) {
00170 ret = AVERROR(EIO);
00171 goto fail;
00172 }
00173 if (localaddr.ss_family != AF_INET
00174 #if HAVE_STRUCT_SOCKADDR_IN6
00175 && localaddr.ss_family != AF_INET6
00176 #endif
00177 ) {
00178 av_log(s, AV_LOG_ERROR, "Unsupported protocol family\n");
00179 ret = AVERROR(EIO);
00180 goto fail;
00181 }
00182 sap->ann_size = 8192;
00183 sap->ann = av_mallocz(sap->ann_size);
00184 if (!sap->ann) {
00185 ret = AVERROR(EIO);
00186 goto fail;
00187 }
00188 sap->ann[pos] = (1 << 5);
00189 #if HAVE_STRUCT_SOCKADDR_IN6
00190 if (localaddr.ss_family == AF_INET6)
00191 sap->ann[pos] |= 0x10;
00192 #endif
00193 pos++;
00194 sap->ann[pos++] = 0;
00195 AV_WB16(&sap->ann[pos], av_get_random_seed());
00196 pos += 2;
00197 if (localaddr.ss_family == AF_INET) {
00198 memcpy(&sap->ann[pos], &((struct sockaddr_in*)&localaddr)->sin_addr,
00199 sizeof(struct in_addr));
00200 pos += sizeof(struct in_addr);
00201 #if HAVE_STRUCT_SOCKADDR_IN6
00202 } else {
00203 memcpy(&sap->ann[pos], &((struct sockaddr_in6*)&localaddr)->sin6_addr,
00204 sizeof(struct in6_addr));
00205 pos += sizeof(struct in6_addr);
00206 #endif
00207 }
00208
00209 av_strlcpy(&sap->ann[pos], "application/sdp", sap->ann_size - pos);
00210 pos += strlen(&sap->ann[pos]) + 1;
00211
00212 if (av_sdp_create(contexts, s->nb_streams, &sap->ann[pos],
00213 sap->ann_size - pos)) {
00214 ret = AVERROR_INVALIDDATA;
00215 goto fail;
00216 }
00217 av_freep(&contexts);
00218 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", &sap->ann[pos]);
00219 pos += strlen(&sap->ann[pos]);
00220 sap->ann_size = pos;
00221
00222 if (sap->ann_size > sap->ann_fd->max_packet_size) {
00223 av_log(s, AV_LOG_ERROR, "Announcement too large to send in one "
00224 "packet\n");
00225 goto fail;
00226 }
00227
00228 return 0;
00229
00230 fail:
00231 av_free(contexts);
00232 sap_write_close(s);
00233 return ret;
00234 }
00235
00236 static int sap_write_packet(AVFormatContext *s, AVPacket *pkt)
00237 {
00238 AVFormatContext *rtpctx;
00239 struct SAPState *sap = s->priv_data;
00240 int64_t now = av_gettime();
00241
00242 if (!sap->last_time || now - sap->last_time > 5000000) {
00243 int ret = ffurl_write(sap->ann_fd, sap->ann, sap->ann_size);
00244
00245 if (ret < 0 && ret != AVERROR(ECONNREFUSED))
00246 return ret;
00247 sap->last_time = now;
00248 }
00249 rtpctx = s->streams[pkt->stream_index]->priv_data;
00250 return ff_write_chained(rtpctx, 0, pkt, s);
00251 }
00252
00253 AVOutputFormat ff_sap_muxer = {
00254 .name = "sap",
00255 .long_name = NULL_IF_CONFIG_SMALL("SAP output format"),
00256 .priv_data_size = sizeof(struct SAPState),
00257 .audio_codec = CODEC_ID_AAC,
00258 .video_codec = CODEC_ID_MPEG4,
00259 .write_header = sap_write_header,
00260 .write_packet = sap_write_packet,
00261 .write_trailer = sap_write_close,
00262 .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
00263 };
00264