00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "libavutil/avstring.h"
00022 #include "libavutil/base64.h"
00023 #include "avformat.h"
00024 #include "internal.h"
00025 #include "avc.h"
00026 #include "rtp.h"
00027
00028 #if CONFIG_RTP_MUXER
00029 #define MAX_EXTRADATA_SIZE ((INT_MAX - 10) / 2)
00030
00031 struct sdp_session_level {
00032 int sdp_version;
00033 int id;
00034 int version;
00035 int start_time;
00037 int end_time;
00039 int ttl;
00040 const char *user;
00041 const char *src_addr;
00042 const char *dst_addr;
00043 const char *name;
00044 };
00045
00046 static void sdp_write_address(char *buff, int size, const char *dest_addr, int ttl)
00047 {
00048 if (dest_addr) {
00049 if (ttl > 0) {
00050 av_strlcatf(buff, size, "c=IN IP4 %s/%d\r\n", dest_addr, ttl);
00051 } else {
00052 av_strlcatf(buff, size, "c=IN IP4 %s\r\n", dest_addr);
00053 }
00054 }
00055 }
00056
00057 static void sdp_write_header(char *buff, int size, struct sdp_session_level *s)
00058 {
00059 av_strlcatf(buff, size, "v=%d\r\n"
00060 "o=- %d %d IN IP4 %s\r\n"
00061 "t=%d %d\r\n"
00062 "s=%s\r\n"
00063 "a=tool:libavformat " AV_STRINGIFY(LIBAVFORMAT_VERSION) "\r\n",
00064 s->sdp_version,
00065 s->id, s->version, s->src_addr,
00066 s->start_time, s->end_time,
00067 s->name);
00068 sdp_write_address(buff, size, s->dst_addr, s->ttl);
00069 }
00070
00071 static int sdp_get_address(char *dest_addr, int size, int *ttl, const char *url)
00072 {
00073 int port;
00074 const char *p;
00075
00076 url_split(NULL, 0, NULL, 0, dest_addr, size, &port, NULL, 0, url);
00077
00078 *ttl = 0;
00079 p = strchr(url, '?');
00080 if (p) {
00081 char buff[64];
00082 int is_multicast = find_info_tag(buff, sizeof(buff), "multicast", p);
00083
00084 if (is_multicast) {
00085 if (find_info_tag(buff, sizeof(buff), "ttl", p)) {
00086 *ttl = strtol(buff, NULL, 10);
00087 } else {
00088 *ttl = 5;
00089 }
00090 }
00091 }
00092
00093 return port;
00094 }
00095
00096 #define MAX_PSET_SIZE 1024
00097 static char *extradata2psets(AVCodecContext *c)
00098 {
00099 char *psets, *p;
00100 const uint8_t *r;
00101 const char *pset_string = "; sprop-parameter-sets=";
00102
00103 if (c->extradata_size > MAX_EXTRADATA_SIZE) {
00104 av_log(c, AV_LOG_ERROR, "Too much extradata!\n");
00105
00106 return NULL;
00107 }
00108
00109 psets = av_mallocz(MAX_PSET_SIZE);
00110 if (psets == NULL) {
00111 av_log(c, AV_LOG_ERROR, "Cannot allocate memory for the parameter sets.\n");
00112 return NULL;
00113 }
00114 memcpy(psets, pset_string, strlen(pset_string));
00115 p = psets + strlen(pset_string);
00116 r = ff_avc_find_startcode(c->extradata, c->extradata + c->extradata_size);
00117 while (r < c->extradata + c->extradata_size) {
00118 const uint8_t *r1;
00119
00120 while (!*(r++));
00121 r1 = ff_avc_find_startcode(r, c->extradata + c->extradata_size);
00122 if (p != (psets + strlen(pset_string))) {
00123 *p = ',';
00124 p++;
00125 }
00126 if (av_base64_encode(p, MAX_PSET_SIZE - (p - psets), r, r1 - r) == NULL) {
00127 av_log(c, AV_LOG_ERROR, "Cannot Base64-encode %td %td!\n", MAX_PSET_SIZE - (p - psets), r1 - r);
00128 av_free(psets);
00129
00130 return NULL;
00131 }
00132 p += strlen(p);
00133 r = r1;
00134 }
00135
00136 return psets;
00137 }
00138
00139 static char *extradata2config(AVCodecContext *c)
00140 {
00141 char *config;
00142
00143 if (c->extradata_size > MAX_EXTRADATA_SIZE) {
00144 av_log(c, AV_LOG_ERROR, "Too much extradata!\n");
00145
00146 return NULL;
00147 }
00148 config = av_malloc(10 + c->extradata_size * 2);
00149 if (config == NULL) {
00150 av_log(c, AV_LOG_ERROR, "Cannot allocate memory for the config info.\n");
00151 return NULL;
00152 }
00153 memcpy(config, "; config=", 9);
00154 ff_data_to_hex(config + 9, c->extradata, c->extradata_size);
00155 config[9 + c->extradata_size * 2] = 0;
00156
00157 return config;
00158 }
00159
00160 static char *sdp_write_media_attributes(char *buff, int size, AVCodecContext *c, int payload_type)
00161 {
00162 char *config = NULL;
00163
00164 switch (c->codec_id) {
00165 case CODEC_ID_H264:
00166 if (c->extradata_size) {
00167 config = extradata2psets(c);
00168 }
00169 av_strlcatf(buff, size, "a=rtpmap:%d H264/90000\r\n"
00170 "a=fmtp:%d packetization-mode=1%s\r\n",
00171 payload_type,
00172 payload_type, config ? config : "");
00173 break;
00174 case CODEC_ID_MPEG4:
00175 if (c->extradata_size) {
00176 config = extradata2config(c);
00177 }
00178 av_strlcatf(buff, size, "a=rtpmap:%d MP4V-ES/90000\r\n"
00179 "a=fmtp:%d profile-level-id=1%s\r\n",
00180 payload_type,
00181 payload_type, config ? config : "");
00182 break;
00183 case CODEC_ID_AAC:
00184 if (c->extradata_size) {
00185 config = extradata2config(c);
00186 } else {
00187
00188
00189
00190 av_log(c, AV_LOG_ERROR, "AAC with no global headers is currently not supported.\n");
00191 return NULL;
00192 }
00193 if (config == NULL) {
00194 return NULL;
00195 }
00196 av_strlcatf(buff, size, "a=rtpmap:%d MPEG4-GENERIC/%d/%d\r\n"
00197 "a=fmtp:%d profile-level-id=1;"
00198 "mode=AAC-hbr;sizelength=13;indexlength=3;"
00199 "indexdeltalength=3%s\r\n",
00200 payload_type, c->sample_rate, c->channels,
00201 payload_type, config);
00202 break;
00203 case CODEC_ID_PCM_S16BE:
00204 if (payload_type >= 96)
00205 av_strlcatf(buff, size, "a=rtpmap:%d L16/%d/%d\r\n",
00206 payload_type,
00207 c->sample_rate, c->channels);
00208 break;
00209 case CODEC_ID_PCM_MULAW:
00210 if (payload_type >= 96)
00211 av_strlcatf(buff, size, "a=rtpmap:%d PCMU/%d/%d\r\n",
00212 payload_type,
00213 c->sample_rate, c->channels);
00214 break;
00215 case CODEC_ID_PCM_ALAW:
00216 if (payload_type >= 96)
00217 av_strlcatf(buff, size, "a=rtpmap:%d PCMA/%d/%d\r\n",
00218 payload_type,
00219 c->sample_rate, c->channels);
00220 break;
00221 default:
00222
00223 break;
00224 }
00225
00226 av_free(config);
00227
00228 return buff;
00229 }
00230
00231 static void sdp_write_media(char *buff, int size, AVCodecContext *c, const char *dest_addr, int port, int ttl)
00232 {
00233 const char *type;
00234 int payload_type;
00235
00236 payload_type = ff_rtp_get_payload_type(c);
00237 if (payload_type < 0) {
00238 payload_type = 96;
00239 }
00240
00241 switch (c->codec_type) {
00242 case CODEC_TYPE_VIDEO : type = "video" ; break;
00243 case CODEC_TYPE_AUDIO : type = "audio" ; break;
00244 case CODEC_TYPE_SUBTITLE: type = "text" ; break;
00245 default : type = "application"; break;
00246 }
00247
00248 av_strlcatf(buff, size, "m=%s %d RTP/AVP %d\r\n", type, port, payload_type);
00249 sdp_write_address(buff, size, dest_addr, ttl);
00250 if (c->bit_rate) {
00251 av_strlcatf(buff, size, "b=AS:%d\r\n", c->bit_rate / 1000);
00252 }
00253
00254 sdp_write_media_attributes(buff, size, c, payload_type);
00255 }
00256
00257 int avf_sdp_create(AVFormatContext *ac[], int n_files, char *buff, int size)
00258 {
00259 AVMetadataTag *title = av_metadata_get(ac[0]->metadata, "title", NULL, 0);
00260 struct sdp_session_level s;
00261 int i, j, port, ttl;
00262 char dst[32];
00263
00264 memset(buff, 0, size);
00265 memset(&s, 0, sizeof(struct sdp_session_level));
00266 s.user = "-";
00267 s.src_addr = "127.0.0.1";
00268 s.name = title ? title->value : "No Name";
00269
00270 port = 0;
00271 ttl = 0;
00272 if (n_files == 1) {
00273 port = sdp_get_address(dst, sizeof(dst), &ttl, ac[0]->filename);
00274 if (port > 0) {
00275 s.dst_addr = dst;
00276 s.ttl = ttl;
00277 }
00278 }
00279 sdp_write_header(buff, size, &s);
00280
00281 dst[0] = 0;
00282 for (i = 0; i < n_files; i++) {
00283 if (n_files != 1) {
00284 port = sdp_get_address(dst, sizeof(dst), &ttl, ac[i]->filename);
00285 }
00286 for (j = 0; j < ac[i]->nb_streams; j++) {
00287 sdp_write_media(buff, size,
00288 ac[i]->streams[j]->codec, dst[0] ? dst : NULL,
00289 (port > 0) ? port + j * 2 : 0, ttl);
00290 if (port <= 0) {
00291 av_strlcatf(buff, size,
00292 "a=control:streamid=%d\r\n", i + j);
00293 }
00294 }
00295 }
00296
00297 return 0;
00298 }
00299 #else
00300 int avf_sdp_create(AVFormatContext *ac[], int n_files, char *buff, int size)
00301 {
00302 return AVERROR(ENOSYS);
00303 }
00304 #endif