00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <string.h>
00022 #include "libavutil/avstring.h"
00023 #include "libavutil/base64.h"
00024 #include "libavutil/dict.h"
00025 #include "libavutil/parseutils.h"
00026 #include "libavutil/opt.h"
00027 #include "libavcodec/xiph.h"
00028 #include "libavcodec/mpeg4audio.h"
00029 #include "avformat.h"
00030 #include "internal.h"
00031 #include "avc.h"
00032 #include "rtp.h"
00033 #if CONFIG_NETWORK
00034 #include "network.h"
00035 #endif
00036
00037 #if CONFIG_RTP_MUXER
00038 #define MAX_EXTRADATA_SIZE ((INT_MAX - 10) / 2)
00039
00040 struct sdp_session_level {
00041 int sdp_version;
00042 int id;
00043 int version;
00044 int start_time;
00046 int end_time;
00048 int ttl;
00049 const char *user;
00050 const char *src_addr;
00051 const char *src_type;
00052 const char *dst_addr;
00053 const char *dst_type;
00054 const char *name;
00055 };
00056
00057 static void sdp_write_address(char *buff, int size, const char *dest_addr,
00058 const char *dest_type, int ttl)
00059 {
00060 if (dest_addr) {
00061 if (!dest_type)
00062 dest_type = "IP4";
00063 if (ttl > 0 && !strcmp(dest_type, "IP4")) {
00064
00065
00066 av_strlcatf(buff, size, "c=IN %s %s/%d\r\n", dest_type, dest_addr, ttl);
00067 } else {
00068 av_strlcatf(buff, size, "c=IN %s %s\r\n", dest_type, dest_addr);
00069 }
00070 }
00071 }
00072
00073 static void sdp_write_header(char *buff, int size, struct sdp_session_level *s)
00074 {
00075 av_strlcatf(buff, size, "v=%d\r\n"
00076 "o=- %d %d IN %s %s\r\n"
00077 "s=%s\r\n",
00078 s->sdp_version,
00079 s->id, s->version, s->src_type, s->src_addr,
00080 s->name);
00081 sdp_write_address(buff, size, s->dst_addr, s->dst_type, s->ttl);
00082 av_strlcatf(buff, size, "t=%d %d\r\n"
00083 "a=tool:libavformat " AV_STRINGIFY(LIBAVFORMAT_VERSION) "\r\n",
00084 s->start_time, s->end_time);
00085 }
00086
00087 #if CONFIG_NETWORK
00088 static int resolve_destination(char *dest_addr, int size, char *type,
00089 int type_size)
00090 {
00091 struct addrinfo hints = { 0 }, *ai;
00092 int is_multicast;
00093
00094 av_strlcpy(type, "IP4", type_size);
00095 if (!dest_addr[0])
00096 return 0;
00097
00098
00099
00100
00101 if (getaddrinfo(dest_addr, NULL, &hints, &ai))
00102 return 0;
00103 getnameinfo(ai->ai_addr, ai->ai_addrlen, dest_addr, size,
00104 NULL, 0, NI_NUMERICHOST);
00105 #ifdef AF_INET6
00106 if (ai->ai_family == AF_INET6)
00107 av_strlcpy(type, "IP6", type_size);
00108 #endif
00109 is_multicast = ff_is_multicast_address(ai->ai_addr);
00110 freeaddrinfo(ai);
00111 return is_multicast;
00112 }
00113 #else
00114 static int resolve_destination(char *dest_addr, int size, char *type,
00115 int type_size)
00116 {
00117 return 0;
00118 }
00119 #endif
00120
00121 static int sdp_get_address(char *dest_addr, int size, int *ttl, const char *url)
00122 {
00123 int port;
00124 const char *p;
00125 char proto[32];
00126
00127 av_url_split(proto, sizeof(proto), NULL, 0, dest_addr, size, &port, NULL, 0, url);
00128
00129 *ttl = 0;
00130
00131 if (strcmp(proto, "rtp")) {
00132
00133
00134
00135 return 0;
00136 }
00137
00138 p = strchr(url, '?');
00139 if (p) {
00140 char buff[64];
00141
00142 if (av_find_info_tag(buff, sizeof(buff), "ttl", p)) {
00143 *ttl = strtol(buff, NULL, 10);
00144 } else {
00145 *ttl = 5;
00146 }
00147 }
00148
00149 return port;
00150 }
00151
00152 #define MAX_PSET_SIZE 1024
00153 static char *extradata2psets(AVCodecContext *c)
00154 {
00155 char *psets, *p;
00156 const uint8_t *r;
00157 const char *pset_string = "; sprop-parameter-sets=";
00158 uint8_t *orig_extradata = NULL;
00159 int orig_extradata_size = 0;
00160
00161 if (c->extradata_size > MAX_EXTRADATA_SIZE) {
00162 av_log(c, AV_LOG_ERROR, "Too much extradata!\n");
00163
00164 return NULL;
00165 }
00166 if (c->extradata[0] == 1) {
00167 uint8_t *dummy_p;
00168 int dummy_int;
00169 AVBitStreamFilterContext *bsfc= av_bitstream_filter_init("h264_mp4toannexb");
00170
00171 if (!bsfc) {
00172 av_log(c, AV_LOG_ERROR, "Cannot open the h264_mp4toannexb BSF!\n");
00173
00174 return NULL;
00175 }
00176
00177 orig_extradata_size = c->extradata_size;
00178 orig_extradata = av_mallocz(orig_extradata_size +
00179 FF_INPUT_BUFFER_PADDING_SIZE);
00180 if (!orig_extradata) {
00181 av_bitstream_filter_close(bsfc);
00182 return NULL;
00183 }
00184 memcpy(orig_extradata, c->extradata, orig_extradata_size);
00185 av_bitstream_filter_filter(bsfc, c, NULL, &dummy_p, &dummy_int, NULL, 0, 0);
00186 av_bitstream_filter_close(bsfc);
00187 }
00188
00189 psets = av_mallocz(MAX_PSET_SIZE);
00190 if (psets == NULL) {
00191 av_log(c, AV_LOG_ERROR, "Cannot allocate memory for the parameter sets.\n");
00192 av_free(orig_extradata);
00193 return NULL;
00194 }
00195 memcpy(psets, pset_string, strlen(pset_string));
00196 p = psets + strlen(pset_string);
00197 r = ff_avc_find_startcode(c->extradata, c->extradata + c->extradata_size);
00198 while (r < c->extradata + c->extradata_size) {
00199 const uint8_t *r1;
00200 uint8_t nal_type;
00201
00202 while (!*(r++));
00203 nal_type = *r & 0x1f;
00204 r1 = ff_avc_find_startcode(r, c->extradata + c->extradata_size);
00205 if (nal_type != 7 && nal_type != 8) {
00206 r = r1;
00207 continue;
00208 }
00209 if (p != (psets + strlen(pset_string))) {
00210 *p = ',';
00211 p++;
00212 }
00213 if (av_base64_encode(p, MAX_PSET_SIZE - (p - psets), r, r1 - r) == NULL) {
00214 av_log(c, AV_LOG_ERROR, "Cannot Base64-encode %td %td!\n", MAX_PSET_SIZE - (p - psets), r1 - r);
00215 av_free(psets);
00216
00217 return NULL;
00218 }
00219 p += strlen(p);
00220 r = r1;
00221 }
00222 if (orig_extradata) {
00223 av_free(c->extradata);
00224 c->extradata = orig_extradata;
00225 c->extradata_size = orig_extradata_size;
00226 }
00227
00228 return psets;
00229 }
00230
00231 static char *extradata2config(AVCodecContext *c)
00232 {
00233 char *config;
00234
00235 if (c->extradata_size > MAX_EXTRADATA_SIZE) {
00236 av_log(c, AV_LOG_ERROR, "Too much extradata!\n");
00237
00238 return NULL;
00239 }
00240 config = av_malloc(10 + c->extradata_size * 2);
00241 if (config == NULL) {
00242 av_log(c, AV_LOG_ERROR, "Cannot allocate memory for the config info.\n");
00243 return NULL;
00244 }
00245 memcpy(config, "; config=", 9);
00246 ff_data_to_hex(config + 9, c->extradata, c->extradata_size, 0);
00247 config[9 + c->extradata_size * 2] = 0;
00248
00249 return config;
00250 }
00251
00252 static char *xiph_extradata2config(AVCodecContext *c)
00253 {
00254 char *config, *encoded_config;
00255 uint8_t *header_start[3];
00256 int headers_len, header_len[3], config_len;
00257 int first_header_size;
00258
00259 switch (c->codec_id) {
00260 case CODEC_ID_THEORA:
00261 first_header_size = 42;
00262 break;
00263 case CODEC_ID_VORBIS:
00264 first_header_size = 30;
00265 break;
00266 default:
00267 av_log(c, AV_LOG_ERROR, "Unsupported Xiph codec ID\n");
00268 return NULL;
00269 }
00270
00271 if (avpriv_split_xiph_headers(c->extradata, c->extradata_size,
00272 first_header_size, header_start,
00273 header_len) < 0) {
00274 av_log(c, AV_LOG_ERROR, "Extradata corrupt.\n");
00275 return NULL;
00276 }
00277
00278 headers_len = header_len[0] + header_len[2];
00279 config_len = 4 +
00280 3 +
00281 2 +
00282 1 +
00283 2 +
00284 headers_len;
00285
00286 config = av_malloc(config_len);
00287 if (!config)
00288 goto xiph_fail;
00289
00290 encoded_config = av_malloc(AV_BASE64_SIZE(config_len));
00291 if (!encoded_config) {
00292 av_free(config);
00293 goto xiph_fail;
00294 }
00295
00296 config[0] = config[1] = config[2] = 0;
00297 config[3] = 1;
00298 config[4] = (RTP_XIPH_IDENT >> 16) & 0xff;
00299 config[5] = (RTP_XIPH_IDENT >> 8) & 0xff;
00300 config[6] = (RTP_XIPH_IDENT ) & 0xff;
00301 config[7] = (headers_len >> 8) & 0xff;
00302 config[8] = headers_len & 0xff;
00303 config[9] = 2;
00304 config[10] = header_len[0];
00305 config[11] = 0;
00306 memcpy(config + 12, header_start[0], header_len[0]);
00307 memcpy(config + 12 + header_len[0], header_start[2], header_len[2]);
00308
00309 av_base64_encode(encoded_config, AV_BASE64_SIZE(config_len),
00310 config, config_len);
00311 av_free(config);
00312
00313 return encoded_config;
00314
00315 xiph_fail:
00316 av_log(c, AV_LOG_ERROR,
00317 "Not enough memory for configuration string\n");
00318 return NULL;
00319 }
00320
00321 static int latm_context2profilelevel(AVCodecContext *c)
00322 {
00323
00324
00325
00326
00327 int profile_level = 0x2B;
00328
00329
00330
00331
00332 if (c->sample_rate <= 24000) {
00333 if (c->channels <= 2)
00334 profile_level = 0x28;
00335 } else if (c->sample_rate <= 48000) {
00336 if (c->channels <= 2) {
00337 profile_level = 0x29;
00338 } else if (c->channels <= 5) {
00339 profile_level = 0x2A;
00340 }
00341 } else if (c->sample_rate <= 96000) {
00342 if (c->channels <= 5) {
00343 profile_level = 0x2B;
00344 }
00345 }
00346
00347 return profile_level;
00348 }
00349
00350 static char *latm_context2config(AVCodecContext *c)
00351 {
00352
00353
00354
00355
00356 uint8_t config_byte[6];
00357 int rate_index;
00358 char *config;
00359
00360 for (rate_index = 0; rate_index < 16; rate_index++)
00361 if (avpriv_mpeg4audio_sample_rates[rate_index] == c->sample_rate)
00362 break;
00363 if (rate_index == 16) {
00364 av_log(c, AV_LOG_ERROR, "Unsupported sample rate\n");
00365 return NULL;
00366 }
00367
00368 config_byte[0] = 0x40;
00369 config_byte[1] = 0;
00370 config_byte[2] = 0x20 | rate_index;
00371 config_byte[3] = c->channels << 4;
00372 config_byte[4] = 0x3f;
00373 config_byte[5] = 0xc0;
00374
00375 config = av_malloc(6*2+1);
00376 if (!config) {
00377 av_log(c, AV_LOG_ERROR, "Cannot allocate memory for the config info.\n");
00378 return NULL;
00379 }
00380 ff_data_to_hex(config, config_byte, 6, 1);
00381 config[12] = 0;
00382
00383 return config;
00384 }
00385
00386 static char *sdp_write_media_attributes(char *buff, int size, AVCodecContext *c, int payload_type, AVFormatContext *fmt)
00387 {
00388 char *config = NULL;
00389
00390 switch (c->codec_id) {
00391 case CODEC_ID_H264:
00392 if (c->extradata_size) {
00393 config = extradata2psets(c);
00394 }
00395 av_strlcatf(buff, size, "a=rtpmap:%d H264/90000\r\n"
00396 "a=fmtp:%d packetization-mode=1%s\r\n",
00397 payload_type,
00398 payload_type, config ? config : "");
00399 break;
00400 case CODEC_ID_H263:
00401 case CODEC_ID_H263P:
00402
00403
00404
00405
00406 if (!fmt || !fmt->oformat->priv_class ||
00407 !av_opt_flag_is_set(fmt->priv_data, "rtpflags", "rfc2190") ||
00408 c->codec_id == CODEC_ID_H263P)
00409 av_strlcatf(buff, size, "a=rtpmap:%d H263-2000/90000\r\n"
00410 "a=framesize:%d %d-%d\r\n",
00411 payload_type,
00412 payload_type, c->width, c->height);
00413 break;
00414 case CODEC_ID_MPEG4:
00415 if (c->extradata_size) {
00416 config = extradata2config(c);
00417 }
00418 av_strlcatf(buff, size, "a=rtpmap:%d MP4V-ES/90000\r\n"
00419 "a=fmtp:%d profile-level-id=1%s\r\n",
00420 payload_type,
00421 payload_type, config ? config : "");
00422 break;
00423 case CODEC_ID_AAC:
00424 if (fmt && fmt->oformat && fmt->oformat->priv_class &&
00425 av_opt_flag_is_set(fmt->priv_data, "rtpflags", "latm")) {
00426 config = latm_context2config(c);
00427 if (!config)
00428 return NULL;
00429 av_strlcatf(buff, size, "a=rtpmap:%d MP4A-LATM/%d/%d\r\n"
00430 "a=fmtp:%d profile-level-id=%d;cpresent=0;config=%s\r\n",
00431 payload_type, c->sample_rate, c->channels,
00432 payload_type, latm_context2profilelevel(c), config);
00433 } else {
00434 if (c->extradata_size) {
00435 config = extradata2config(c);
00436 } else {
00437
00438
00439
00440 av_log(c, AV_LOG_ERROR, "AAC with no global headers is currently not supported.\n");
00441 return NULL;
00442 }
00443 if (config == NULL) {
00444 return NULL;
00445 }
00446 av_strlcatf(buff, size, "a=rtpmap:%d MPEG4-GENERIC/%d/%d\r\n"
00447 "a=fmtp:%d profile-level-id=1;"
00448 "mode=AAC-hbr;sizelength=13;indexlength=3;"
00449 "indexdeltalength=3%s\r\n",
00450 payload_type, c->sample_rate, c->channels,
00451 payload_type, config);
00452 }
00453 break;
00454 case CODEC_ID_PCM_S16BE:
00455 if (payload_type >= RTP_PT_PRIVATE)
00456 av_strlcatf(buff, size, "a=rtpmap:%d L16/%d/%d\r\n",
00457 payload_type,
00458 c->sample_rate, c->channels);
00459 break;
00460 case CODEC_ID_PCM_MULAW:
00461 if (payload_type >= RTP_PT_PRIVATE)
00462 av_strlcatf(buff, size, "a=rtpmap:%d PCMU/%d/%d\r\n",
00463 payload_type,
00464 c->sample_rate, c->channels);
00465 break;
00466 case CODEC_ID_PCM_ALAW:
00467 if (payload_type >= RTP_PT_PRIVATE)
00468 av_strlcatf(buff, size, "a=rtpmap:%d PCMA/%d/%d\r\n",
00469 payload_type,
00470 c->sample_rate, c->channels);
00471 break;
00472 case CODEC_ID_AMR_NB:
00473 av_strlcatf(buff, size, "a=rtpmap:%d AMR/%d/%d\r\n"
00474 "a=fmtp:%d octet-align=1\r\n",
00475 payload_type, c->sample_rate, c->channels,
00476 payload_type);
00477 break;
00478 case CODEC_ID_AMR_WB:
00479 av_strlcatf(buff, size, "a=rtpmap:%d AMR-WB/%d/%d\r\n"
00480 "a=fmtp:%d octet-align=1\r\n",
00481 payload_type, c->sample_rate, c->channels,
00482 payload_type);
00483 break;
00484 case CODEC_ID_VORBIS:
00485 if (c->extradata_size)
00486 config = xiph_extradata2config(c);
00487 else
00488 av_log(c, AV_LOG_ERROR, "Vorbis configuration info missing\n");
00489 if (!config)
00490 return NULL;
00491
00492 av_strlcatf(buff, size, "a=rtpmap:%d vorbis/%d/%d\r\n"
00493 "a=fmtp:%d configuration=%s\r\n",
00494 payload_type, c->sample_rate, c->channels,
00495 payload_type, config);
00496 break;
00497 case CODEC_ID_THEORA: {
00498 const char *pix_fmt;
00499 if (c->extradata_size)
00500 config = xiph_extradata2config(c);
00501 else
00502 av_log(c, AV_LOG_ERROR, "Theora configuation info missing\n");
00503 if (!config)
00504 return NULL;
00505
00506 switch (c->pix_fmt) {
00507 case PIX_FMT_YUV420P:
00508 pix_fmt = "YCbCr-4:2:0";
00509 break;
00510 case PIX_FMT_YUV422P:
00511 pix_fmt = "YCbCr-4:2:2";
00512 break;
00513 case PIX_FMT_YUV444P:
00514 pix_fmt = "YCbCr-4:4:4";
00515 break;
00516 default:
00517 av_log(c, AV_LOG_ERROR, "Unsupported pixel format.\n");
00518 return NULL;
00519 }
00520
00521 av_strlcatf(buff, size, "a=rtpmap:%d theora/90000\r\n"
00522 "a=fmtp:%d delivery-method=inline; "
00523 "width=%d; height=%d; sampling=%s; "
00524 "configuration=%s\r\n",
00525 payload_type, payload_type,
00526 c->width, c->height, pix_fmt, config);
00527 break;
00528 }
00529 case CODEC_ID_VP8:
00530 av_strlcatf(buff, size, "a=rtpmap:%d VP8/90000\r\n",
00531 payload_type);
00532 break;
00533 case CODEC_ID_ADPCM_G722:
00534 if (payload_type >= RTP_PT_PRIVATE)
00535 av_strlcatf(buff, size, "a=rtpmap:%d G722/%d/%d\r\n",
00536 payload_type,
00537 8000, c->channels);
00538 break;
00539 case CODEC_ID_ADPCM_G726: {
00540 if (payload_type >= RTP_PT_PRIVATE)
00541 av_strlcatf(buff, size, "a=rtpmap:%d G726-%d/%d\r\n",
00542 payload_type,
00543 c->bits_per_coded_sample*8,
00544 c->sample_rate);
00545 break;
00546 }
00547 default:
00548
00549 break;
00550 }
00551
00552 av_free(config);
00553
00554 return buff;
00555 }
00556
00557 void ff_sdp_write_media(char *buff, int size, AVCodecContext *c, const char *dest_addr, const char *dest_type, int port, int ttl, AVFormatContext *fmt)
00558 {
00559 const char *type;
00560 int payload_type;
00561
00562 payload_type = ff_rtp_get_payload_type(fmt, c);
00563
00564 switch (c->codec_type) {
00565 case AVMEDIA_TYPE_VIDEO : type = "video" ; break;
00566 case AVMEDIA_TYPE_AUDIO : type = "audio" ; break;
00567 case AVMEDIA_TYPE_SUBTITLE: type = "text" ; break;
00568 default : type = "application"; break;
00569 }
00570
00571 av_strlcatf(buff, size, "m=%s %d RTP/AVP %d\r\n", type, port, payload_type);
00572 sdp_write_address(buff, size, dest_addr, dest_type, ttl);
00573 if (c->bit_rate) {
00574 av_strlcatf(buff, size, "b=AS:%d\r\n", c->bit_rate / 1000);
00575 }
00576
00577 sdp_write_media_attributes(buff, size, c, payload_type, fmt);
00578 }
00579
00580 int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
00581 {
00582 AVDictionaryEntry *title = av_dict_get(ac[0]->metadata, "title", NULL, 0);
00583 struct sdp_session_level s = { 0 };
00584 int i, j, port, ttl, is_multicast;
00585 char dst[32], dst_type[5];
00586
00587 memset(buf, 0, size);
00588 s.user = "-";
00589 s.src_addr = "127.0.0.1";
00590 s.src_type = "IP4";
00591 s.name = title ? title->value : "No Name";
00592
00593 port = 0;
00594 ttl = 0;
00595 if (n_files == 1) {
00596 port = sdp_get_address(dst, sizeof(dst), &ttl, ac[0]->filename);
00597 is_multicast = resolve_destination(dst, sizeof(dst), dst_type,
00598 sizeof(dst_type));
00599 if (!is_multicast)
00600 ttl = 0;
00601 if (dst[0]) {
00602 s.dst_addr = dst;
00603 s.dst_type = dst_type;
00604 s.ttl = ttl;
00605 if (!strcmp(dst_type, "IP6")) {
00606 s.src_addr = "::1";
00607 s.src_type = "IP6";
00608 }
00609 }
00610 }
00611 sdp_write_header(buf, size, &s);
00612
00613 dst[0] = 0;
00614 for (i = 0; i < n_files; i++) {
00615 if (n_files != 1) {
00616 port = sdp_get_address(dst, sizeof(dst), &ttl, ac[i]->filename);
00617 is_multicast = resolve_destination(dst, sizeof(dst), dst_type,
00618 sizeof(dst_type));
00619 if (!is_multicast)
00620 ttl = 0;
00621 }
00622 for (j = 0; j < ac[i]->nb_streams; j++) {
00623 ff_sdp_write_media(buf, size,
00624 ac[i]->streams[j]->codec, dst[0] ? dst : NULL,
00625 dst_type, (port > 0) ? port + j * 2 : 0, ttl,
00626 ac[i]);
00627 if (port <= 0) {
00628 av_strlcatf(buf, size,
00629 "a=control:streamid=%d\r\n", i + j);
00630 }
00631 }
00632 }
00633
00634 return 0;
00635 }
00636 #else
00637 int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
00638 {
00639 return AVERROR(ENOSYS);
00640 }
00641
00642 void ff_sdp_write_media(char *buff, int size, AVCodecContext *c, const char *dest_addr, const char *dest_type, int port, int ttl, AVFormatContext *fmt)
00643 {
00644 }
00645 #endif