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/random_seed.h"
00024 #include "libavutil/avstring.h"
00025 #include "libavutil/intreadwrite.h"
00026 #include "internal.h"
00027 #include "network.h"
00028 #include "os_support.h"
00029 #include "rtpenc_chain.h"
00030
00031 struct SAPState {
00032 uint8_t *ann;
00033 int ann_size;
00034 URLContext *ann_fd;
00035 int64_t last_time;
00036 };
00037
00038 static int sap_write_close(AVFormatContext *s)
00039 {
00040 struct SAPState *sap = s->priv_data;
00041 int i;
00042
00043 for (i = 0; i < s->nb_streams; i++) {
00044 AVFormatContext *rtpctx = s->streams[i]->priv_data;
00045 if (!rtpctx)
00046 continue;
00047 av_write_trailer(rtpctx);
00048 url_fclose(rtpctx->pb);
00049 av_metadata_free(&rtpctx->streams[0]->metadata);
00050 av_metadata_free(&rtpctx->metadata);
00051 av_free(rtpctx->streams[0]);
00052 av_free(rtpctx);
00053 s->streams[i]->priv_data = NULL;
00054 }
00055
00056 if (sap->last_time && sap->ann && sap->ann_fd) {
00057 sap->ann[0] |= 4;
00058 url_write(sap->ann_fd, sap->ann, sap->ann_size);
00059 }
00060
00061 av_freep(&sap->ann);
00062 if (sap->ann_fd)
00063 url_close(sap->ann_fd);
00064 ff_network_close();
00065 return 0;
00066 }
00067
00068 static int sap_write_header(AVFormatContext *s)
00069 {
00070 struct SAPState *sap = s->priv_data;
00071 char host[1024], path[1024], url[1024], announce_addr[50] = "";
00072 char *option_list;
00073 int port = 9875, base_port = 5004, i, pos = 0, same_port = 0, ttl = 255;
00074 AVFormatContext **contexts = NULL;
00075 int ret = 0;
00076 struct sockaddr_storage localaddr;
00077 socklen_t addrlen = sizeof(localaddr);
00078 int udp_fd;
00079
00080 if (!ff_network_init())
00081 return AVERROR(EIO);
00082
00083
00084 av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &base_port,
00085 path, sizeof(path), s->filename);
00086 if (base_port < 0)
00087 base_port = 5004;
00088
00089
00090 option_list = strrchr(path, '?');
00091 if (option_list) {
00092 char buf[50];
00093 if (find_info_tag(buf, sizeof(buf), "announce_port", option_list)) {
00094 port = strtol(buf, NULL, 10);
00095 }
00096 if (find_info_tag(buf, sizeof(buf), "same_port", option_list)) {
00097 same_port = strtol(buf, NULL, 10);
00098 }
00099 if (find_info_tag(buf, sizeof(buf), "ttl", option_list)) {
00100 ttl = strtol(buf, NULL, 10);
00101 }
00102 if (find_info_tag(buf, sizeof(buf), "announce_addr", option_list)) {
00103 av_strlcpy(announce_addr, buf, sizeof(announce_addr));
00104 }
00105 }
00106
00107 if (!announce_addr[0]) {
00108 struct addrinfo hints, *ai = NULL;
00109 memset(&hints, 0, sizeof(hints));
00110 hints.ai_family = AF_UNSPEC;
00111 if (getaddrinfo(host, NULL, &hints, &ai)) {
00112 av_log(s, AV_LOG_ERROR, "Unable to resolve %s\n", host);
00113 ret = AVERROR(EIO);
00114 goto fail;
00115 }
00116 if (ai->ai_family == AF_INET) {
00117
00118 av_strlcpy(announce_addr, "224.2.127.254", sizeof(announce_addr));
00119 #if HAVE_STRUCT_SOCKADDR_IN6
00120 } else if (ai->ai_family == AF_INET6) {
00121
00122
00123
00124 av_strlcpy(announce_addr, "ff0e::2:7ffe", sizeof(announce_addr));
00125 #endif
00126 } else {
00127 freeaddrinfo(ai);
00128 av_log(s, AV_LOG_ERROR, "Host %s resolved to unsupported "
00129 "address family\n", host);
00130 ret = AVERROR(EIO);
00131 goto fail;
00132 }
00133 freeaddrinfo(ai);
00134 }
00135
00136 contexts = av_mallocz(sizeof(AVFormatContext*) * s->nb_streams);
00137 if (!contexts) {
00138 ret = AVERROR(ENOMEM);
00139 goto fail;
00140 }
00141
00142 s->start_time_realtime = av_gettime();
00143 for (i = 0; i < s->nb_streams; i++) {
00144 URLContext *fd;
00145
00146 ff_url_join(url, sizeof(url), "rtp", NULL, host, base_port,
00147 "?ttl=%d", ttl);
00148 if (!same_port)
00149 base_port += 2;
00150 ret = url_open(&fd, url, URL_WRONLY);
00151 if (ret) {
00152 ret = AVERROR(EIO);
00153 goto fail;
00154 }
00155 s->streams[i]->priv_data = contexts[i] =
00156 ff_rtp_chain_mux_open(s, s->streams[i], fd, 0);
00157 av_strlcpy(contexts[i]->filename, url, sizeof(contexts[i]->filename));
00158 }
00159
00160 ff_url_join(url, sizeof(url), "udp", NULL, announce_addr, port,
00161 "?ttl=%d&connect=1", ttl);
00162 ret = url_open(&sap->ann_fd, url, URL_WRONLY);
00163 if (ret) {
00164 ret = AVERROR(EIO);
00165 goto fail;
00166 }
00167
00168 udp_fd = url_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 (avf_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 > url_get_max_packet_size(sap->ann_fd)) {
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 = url_write(sap->ann_fd, sap->ann, sap->ann_size);
00244
00245 if (ret < 0 && ret != FF_NETERROR(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 sap_muxer = {
00254 "sap",
00255 NULL_IF_CONFIG_SMALL("SAP output format"),
00256 NULL,
00257 NULL,
00258 sizeof(struct SAPState),
00259 CODEC_ID_AAC,
00260 CODEC_ID_MPEG4,
00261 sap_write_header,
00262 sap_write_packet,
00263 sap_write_close,
00264 .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
00265 };
00266