sdp.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /*
  2. * copyright (c) 2007 Luca Abeni
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <string.h>
  21. #include "libavutil/avstring.h"
  22. #include "libavutil/base64.h"
  23. #include "libavutil/dict.h"
  24. #include "libavutil/parseutils.h"
  25. #include "libavutil/opt.h"
  26. #include "libavcodec/xiph.h"
  27. #include "libavcodec/mpeg4audio.h"
  28. #include "avformat.h"
  29. #include "internal.h"
  30. #include "avc.h"
  31. #include "rtp.h"
  32. #if CONFIG_NETWORK
  33. #include "network.h"
  34. #endif
  35. #if CONFIG_RTP_MUXER
  36. #define MAX_EXTRADATA_SIZE ((INT_MAX - 10) / 2)
  37. struct sdp_session_level {
  38. int sdp_version; /**< protocol version (currently 0) */
  39. int id; /**< session ID */
  40. int version; /**< session version */
  41. int start_time; /**< session start time (NTP time, in seconds),
  42. or 0 in case of permanent session */
  43. int end_time; /**< session end time (NTP time, in seconds),
  44. or 0 if the session is not bounded */
  45. int ttl; /**< TTL, in case of multicast stream */
  46. const char *user; /**< username of the session's creator */
  47. const char *src_addr; /**< IP address of the machine from which the session was created */
  48. const char *src_type; /**< address type of src_addr */
  49. const char *dst_addr; /**< destination IP address (can be multicast) */
  50. const char *dst_type; /**< destination IP address type */
  51. const char *name; /**< session name (can be an empty string) */
  52. };
  53. static void sdp_write_address(char *buff, int size, const char *dest_addr,
  54. const char *dest_type, int ttl)
  55. {
  56. if (dest_addr) {
  57. if (!dest_type)
  58. dest_type = "IP4";
  59. if (ttl > 0 && !strcmp(dest_type, "IP4")) {
  60. /* The TTL should only be specified for IPv4 multicast addresses,
  61. * not for IPv6. */
  62. av_strlcatf(buff, size, "c=IN %s %s/%d\r\n", dest_type, dest_addr, ttl);
  63. } else {
  64. av_strlcatf(buff, size, "c=IN %s %s\r\n", dest_type, dest_addr);
  65. }
  66. }
  67. }
  68. static void sdp_write_header(char *buff, int size, struct sdp_session_level *s)
  69. {
  70. av_strlcatf(buff, size, "v=%d\r\n"
  71. "o=- %d %d IN %s %s\r\n"
  72. "s=%s\r\n",
  73. s->sdp_version,
  74. s->id, s->version, s->src_type, s->src_addr,
  75. s->name);
  76. sdp_write_address(buff, size, s->dst_addr, s->dst_type, s->ttl);
  77. av_strlcatf(buff, size, "t=%d %d\r\n"
  78. "a=tool:libavformat " AV_STRINGIFY(LIBAVFORMAT_VERSION) "\r\n",
  79. s->start_time, s->end_time);
  80. }
  81. #if CONFIG_NETWORK
  82. static int resolve_destination(char *dest_addr, int size, char *type,
  83. int type_size)
  84. {
  85. struct addrinfo hints = { 0 }, *ai;
  86. int is_multicast;
  87. av_strlcpy(type, "IP4", type_size);
  88. if (!dest_addr[0])
  89. return 0;
  90. /* Resolve the destination, since it must be written
  91. * as a numeric IP address in the SDP. */
  92. if (getaddrinfo(dest_addr, NULL, &hints, &ai))
  93. return 0;
  94. getnameinfo(ai->ai_addr, ai->ai_addrlen, dest_addr, size,
  95. NULL, 0, NI_NUMERICHOST);
  96. #ifdef AF_INET6
  97. if (ai->ai_family == AF_INET6)
  98. av_strlcpy(type, "IP6", type_size);
  99. #endif
  100. is_multicast = ff_is_multicast_address(ai->ai_addr);
  101. freeaddrinfo(ai);
  102. return is_multicast;
  103. }
  104. #else
  105. static int resolve_destination(char *dest_addr, int size, char *type,
  106. int type_size)
  107. {
  108. return 0;
  109. }
  110. #endif
  111. static int sdp_get_address(char *dest_addr, int size, int *ttl, const char *url)
  112. {
  113. int port;
  114. const char *p;
  115. char proto[32];
  116. av_url_split(proto, sizeof(proto), NULL, 0, dest_addr, size, &port, NULL, 0, url);
  117. *ttl = 0;
  118. if (strcmp(proto, "rtp")) {
  119. /* The url isn't for the actual rtp sessions,
  120. * don't parse out anything else than the destination.
  121. */
  122. return 0;
  123. }
  124. p = strchr(url, '?');
  125. if (p) {
  126. char buff[64];
  127. if (av_find_info_tag(buff, sizeof(buff), "ttl", p)) {
  128. *ttl = strtol(buff, NULL, 10);
  129. } else {
  130. *ttl = 5;
  131. }
  132. }
  133. return port;
  134. }
  135. #define MAX_PSET_SIZE 1024
  136. static char *extradata2psets(AVCodecContext *c)
  137. {
  138. char *psets, *p;
  139. const uint8_t *r;
  140. const char *pset_string = "; sprop-parameter-sets=";
  141. uint8_t *orig_extradata = NULL;
  142. int orig_extradata_size = 0;
  143. if (c->extradata_size > MAX_EXTRADATA_SIZE) {
  144. av_log(c, AV_LOG_ERROR, "Too much extradata!\n");
  145. return NULL;
  146. }
  147. if (c->extradata[0] == 1) {
  148. uint8_t *dummy_p;
  149. int dummy_int;
  150. AVBitStreamFilterContext *bsfc= av_bitstream_filter_init("h264_mp4toannexb");
  151. if (!bsfc) {
  152. av_log(c, AV_LOG_ERROR, "Cannot open the h264_mp4toannexb BSF!\n");
  153. return NULL;
  154. }
  155. orig_extradata_size = c->extradata_size;
  156. orig_extradata = av_mallocz(orig_extradata_size +
  157. FF_INPUT_BUFFER_PADDING_SIZE);
  158. if (!orig_extradata) {
  159. av_bitstream_filter_close(bsfc);
  160. return NULL;
  161. }
  162. memcpy(orig_extradata, c->extradata, orig_extradata_size);
  163. av_bitstream_filter_filter(bsfc, c, NULL, &dummy_p, &dummy_int, NULL, 0, 0);
  164. av_bitstream_filter_close(bsfc);
  165. }
  166. psets = av_mallocz(MAX_PSET_SIZE);
  167. if (psets == NULL) {
  168. av_log(c, AV_LOG_ERROR, "Cannot allocate memory for the parameter sets.\n");
  169. av_free(orig_extradata);
  170. return NULL;
  171. }
  172. memcpy(psets, pset_string, strlen(pset_string));
  173. p = psets + strlen(pset_string);
  174. r = ff_avc_find_startcode(c->extradata, c->extradata + c->extradata_size);
  175. while (r < c->extradata + c->extradata_size) {
  176. const uint8_t *r1;
  177. uint8_t nal_type;
  178. while (!*(r++));
  179. nal_type = *r & 0x1f;
  180. r1 = ff_avc_find_startcode(r, c->extradata + c->extradata_size);
  181. if (nal_type != 7 && nal_type != 8) { /* Only output SPS and PPS */
  182. r = r1;
  183. continue;
  184. }
  185. if (p != (psets + strlen(pset_string))) {
  186. *p = ',';
  187. p++;
  188. }
  189. if (av_base64_encode(p, MAX_PSET_SIZE - (p - psets), r, r1 - r) == NULL) {
  190. av_log(c, AV_LOG_ERROR, "Cannot Base64-encode %td %td!\n", MAX_PSET_SIZE - (p - psets), r1 - r);
  191. av_free(psets);
  192. return NULL;
  193. }
  194. p += strlen(p);
  195. r = r1;
  196. }
  197. if (orig_extradata) {
  198. av_free(c->extradata);
  199. c->extradata = orig_extradata;
  200. c->extradata_size = orig_extradata_size;
  201. }
  202. return psets;
  203. }
  204. static char *extradata2config(AVCodecContext *c)
  205. {
  206. char *config;
  207. if (c->extradata_size > MAX_EXTRADATA_SIZE) {
  208. av_log(c, AV_LOG_ERROR, "Too much extradata!\n");
  209. return NULL;
  210. }
  211. config = av_malloc(10 + c->extradata_size * 2);
  212. if (config == NULL) {
  213. av_log(c, AV_LOG_ERROR, "Cannot allocate memory for the config info.\n");
  214. return NULL;
  215. }
  216. memcpy(config, "; config=", 9);
  217. ff_data_to_hex(config + 9, c->extradata, c->extradata_size, 0);
  218. config[9 + c->extradata_size * 2] = 0;
  219. return config;
  220. }
  221. static char *xiph_extradata2config(AVCodecContext *c)
  222. {
  223. char *config, *encoded_config;
  224. uint8_t *header_start[3];
  225. int headers_len, header_len[3], config_len;
  226. int first_header_size;
  227. switch (c->codec_id) {
  228. case AV_CODEC_ID_THEORA:
  229. first_header_size = 42;
  230. break;
  231. case AV_CODEC_ID_VORBIS:
  232. first_header_size = 30;
  233. break;
  234. default:
  235. av_log(c, AV_LOG_ERROR, "Unsupported Xiph codec ID\n");
  236. return NULL;
  237. }
  238. if (avpriv_split_xiph_headers(c->extradata, c->extradata_size,
  239. first_header_size, header_start,
  240. header_len) < 0) {
  241. av_log(c, AV_LOG_ERROR, "Extradata corrupt.\n");
  242. return NULL;
  243. }
  244. headers_len = header_len[0] + header_len[2];
  245. config_len = 4 + // count
  246. 3 + // ident
  247. 2 + // packet size
  248. 1 + // header count
  249. 2 + // header size
  250. headers_len; // and the rest
  251. config = av_malloc(config_len);
  252. if (!config)
  253. goto xiph_fail;
  254. encoded_config = av_malloc(AV_BASE64_SIZE(config_len));
  255. if (!encoded_config) {
  256. av_free(config);
  257. goto xiph_fail;
  258. }
  259. config[0] = config[1] = config[2] = 0;
  260. config[3] = 1;
  261. config[4] = (RTP_XIPH_IDENT >> 16) & 0xff;
  262. config[5] = (RTP_XIPH_IDENT >> 8) & 0xff;
  263. config[6] = (RTP_XIPH_IDENT ) & 0xff;
  264. config[7] = (headers_len >> 8) & 0xff;
  265. config[8] = headers_len & 0xff;
  266. config[9] = 2;
  267. config[10] = header_len[0];
  268. config[11] = 0; // size of comment header; nonexistent
  269. memcpy(config + 12, header_start[0], header_len[0]);
  270. memcpy(config + 12 + header_len[0], header_start[2], header_len[2]);
  271. av_base64_encode(encoded_config, AV_BASE64_SIZE(config_len),
  272. config, config_len);
  273. av_free(config);
  274. return encoded_config;
  275. xiph_fail:
  276. av_log(c, AV_LOG_ERROR,
  277. "Not enough memory for configuration string\n");
  278. return NULL;
  279. }
  280. static int latm_context2profilelevel(AVCodecContext *c)
  281. {
  282. /* MP4A-LATM
  283. * The RTP payload format specification is described in RFC 3016
  284. * The encoding specifications are provided in ISO/IEC 14496-3 */
  285. int profile_level = 0x2B;
  286. /* TODO: AAC Profile only supports AAC LC Object Type.
  287. * Different Object Types should implement different Profile Levels */
  288. if (c->sample_rate <= 24000) {
  289. if (c->channels <= 2)
  290. profile_level = 0x28; // AAC Profile, Level 1
  291. } else if (c->sample_rate <= 48000) {
  292. if (c->channels <= 2) {
  293. profile_level = 0x29; // AAC Profile, Level 2
  294. } else if (c->channels <= 5) {
  295. profile_level = 0x2A; // AAC Profile, Level 4
  296. }
  297. } else if (c->sample_rate <= 96000) {
  298. if (c->channels <= 5) {
  299. profile_level = 0x2B; // AAC Profile, Level 5
  300. }
  301. }
  302. return profile_level;
  303. }
  304. static char *latm_context2config(AVCodecContext *c)
  305. {
  306. /* MP4A-LATM
  307. * The RTP payload format specification is described in RFC 3016
  308. * The encoding specifications are provided in ISO/IEC 14496-3 */
  309. uint8_t config_byte[6];
  310. int rate_index;
  311. char *config;
  312. for (rate_index = 0; rate_index < 16; rate_index++)
  313. if (avpriv_mpeg4audio_sample_rates[rate_index] == c->sample_rate)
  314. break;
  315. if (rate_index == 16) {
  316. av_log(c, AV_LOG_ERROR, "Unsupported sample rate\n");
  317. return NULL;
  318. }
  319. config_byte[0] = 0x40;
  320. config_byte[1] = 0;
  321. config_byte[2] = 0x20 | rate_index;
  322. config_byte[3] = c->channels << 4;
  323. config_byte[4] = 0x3f;
  324. config_byte[5] = 0xc0;
  325. config = av_malloc(6*2+1);
  326. if (!config) {
  327. av_log(c, AV_LOG_ERROR, "Cannot allocate memory for the config info.\n");
  328. return NULL;
  329. }
  330. ff_data_to_hex(config, config_byte, 6, 1);
  331. config[12] = 0;
  332. return config;
  333. }
  334. static char *sdp_write_media_attributes(char *buff, int size, AVCodecContext *c, int payload_type, AVFormatContext *fmt)
  335. {
  336. char *config = NULL;
  337. switch (c->codec_id) {
  338. case AV_CODEC_ID_H264: {
  339. int mode = 1;
  340. if (fmt && fmt->oformat->priv_class &&
  341. av_opt_flag_is_set(fmt->priv_data, "rtpflags", "h264_mode0"))
  342. mode = 0;
  343. if (c->extradata_size) {
  344. config = extradata2psets(c);
  345. }
  346. av_strlcatf(buff, size, "a=rtpmap:%d H264/90000\r\n"
  347. "a=fmtp:%d packetization-mode=%d%s\r\n",
  348. payload_type,
  349. payload_type, mode, config ? config : "");
  350. break;
  351. }
  352. case AV_CODEC_ID_H263:
  353. case AV_CODEC_ID_H263P:
  354. /* a=framesize is required by 3GPP TS 26.234 (PSS). It
  355. * actually specifies the maximum video size, but we only know
  356. * the current size. This is required for playback on Android
  357. * stagefright and on Samsung bada. */
  358. if (!fmt || !fmt->oformat->priv_class ||
  359. !av_opt_flag_is_set(fmt->priv_data, "rtpflags", "rfc2190") ||
  360. c->codec_id == AV_CODEC_ID_H263P)
  361. av_strlcatf(buff, size, "a=rtpmap:%d H263-2000/90000\r\n"
  362. "a=framesize:%d %d-%d\r\n",
  363. payload_type,
  364. payload_type, c->width, c->height);
  365. break;
  366. case AV_CODEC_ID_MPEG4:
  367. if (c->extradata_size) {
  368. config = extradata2config(c);
  369. }
  370. av_strlcatf(buff, size, "a=rtpmap:%d MP4V-ES/90000\r\n"
  371. "a=fmtp:%d profile-level-id=1%s\r\n",
  372. payload_type,
  373. payload_type, config ? config : "");
  374. break;
  375. case AV_CODEC_ID_AAC:
  376. if (fmt && fmt->oformat && fmt->oformat->priv_class &&
  377. av_opt_flag_is_set(fmt->priv_data, "rtpflags", "latm")) {
  378. config = latm_context2config(c);
  379. if (!config)
  380. return NULL;
  381. av_strlcatf(buff, size, "a=rtpmap:%d MP4A-LATM/%d/%d\r\n"
  382. "a=fmtp:%d profile-level-id=%d;cpresent=0;config=%s\r\n",
  383. payload_type, c->sample_rate, c->channels,
  384. payload_type, latm_context2profilelevel(c), config);
  385. } else {
  386. if (c->extradata_size) {
  387. config = extradata2config(c);
  388. } else {
  389. /* FIXME: maybe we can forge config information based on the
  390. * codec parameters...
  391. */
  392. av_log(c, AV_LOG_ERROR, "AAC with no global headers is currently not supported.\n");
  393. return NULL;
  394. }
  395. if (config == NULL) {
  396. return NULL;
  397. }
  398. av_strlcatf(buff, size, "a=rtpmap:%d MPEG4-GENERIC/%d/%d\r\n"
  399. "a=fmtp:%d profile-level-id=1;"
  400. "mode=AAC-hbr;sizelength=13;indexlength=3;"
  401. "indexdeltalength=3%s\r\n",
  402. payload_type, c->sample_rate, c->channels,
  403. payload_type, config);
  404. }
  405. break;
  406. case AV_CODEC_ID_PCM_S16BE:
  407. if (payload_type >= RTP_PT_PRIVATE)
  408. av_strlcatf(buff, size, "a=rtpmap:%d L16/%d/%d\r\n",
  409. payload_type,
  410. c->sample_rate, c->channels);
  411. break;
  412. case AV_CODEC_ID_PCM_MULAW:
  413. if (payload_type >= RTP_PT_PRIVATE)
  414. av_strlcatf(buff, size, "a=rtpmap:%d PCMU/%d/%d\r\n",
  415. payload_type,
  416. c->sample_rate, c->channels);
  417. break;
  418. case AV_CODEC_ID_PCM_ALAW:
  419. if (payload_type >= RTP_PT_PRIVATE)
  420. av_strlcatf(buff, size, "a=rtpmap:%d PCMA/%d/%d\r\n",
  421. payload_type,
  422. c->sample_rate, c->channels);
  423. break;
  424. case AV_CODEC_ID_AMR_NB:
  425. av_strlcatf(buff, size, "a=rtpmap:%d AMR/%d/%d\r\n"
  426. "a=fmtp:%d octet-align=1\r\n",
  427. payload_type, c->sample_rate, c->channels,
  428. payload_type);
  429. break;
  430. case AV_CODEC_ID_AMR_WB:
  431. av_strlcatf(buff, size, "a=rtpmap:%d AMR-WB/%d/%d\r\n"
  432. "a=fmtp:%d octet-align=1\r\n",
  433. payload_type, c->sample_rate, c->channels,
  434. payload_type);
  435. break;
  436. case AV_CODEC_ID_VORBIS:
  437. if (c->extradata_size)
  438. config = xiph_extradata2config(c);
  439. else
  440. av_log(c, AV_LOG_ERROR, "Vorbis configuration info missing\n");
  441. if (!config)
  442. return NULL;
  443. av_strlcatf(buff, size, "a=rtpmap:%d vorbis/%d/%d\r\n"
  444. "a=fmtp:%d configuration=%s\r\n",
  445. payload_type, c->sample_rate, c->channels,
  446. payload_type, config);
  447. break;
  448. case AV_CODEC_ID_THEORA: {
  449. const char *pix_fmt;
  450. if (c->extradata_size)
  451. config = xiph_extradata2config(c);
  452. else
  453. av_log(c, AV_LOG_ERROR, "Theora configuation info missing\n");
  454. if (!config)
  455. return NULL;
  456. switch (c->pix_fmt) {
  457. case PIX_FMT_YUV420P:
  458. pix_fmt = "YCbCr-4:2:0";
  459. break;
  460. case PIX_FMT_YUV422P:
  461. pix_fmt = "YCbCr-4:2:2";
  462. break;
  463. case PIX_FMT_YUV444P:
  464. pix_fmt = "YCbCr-4:4:4";
  465. break;
  466. default:
  467. av_log(c, AV_LOG_ERROR, "Unsupported pixel format.\n");
  468. return NULL;
  469. }
  470. av_strlcatf(buff, size, "a=rtpmap:%d theora/90000\r\n"
  471. "a=fmtp:%d delivery-method=inline; "
  472. "width=%d; height=%d; sampling=%s; "
  473. "configuration=%s\r\n",
  474. payload_type, payload_type,
  475. c->width, c->height, pix_fmt, config);
  476. break;
  477. }
  478. case AV_CODEC_ID_VP8:
  479. av_strlcatf(buff, size, "a=rtpmap:%d VP8/90000\r\n",
  480. payload_type);
  481. break;
  482. case AV_CODEC_ID_ADPCM_G722:
  483. if (payload_type >= RTP_PT_PRIVATE)
  484. av_strlcatf(buff, size, "a=rtpmap:%d G722/%d/%d\r\n",
  485. payload_type,
  486. 8000, c->channels);
  487. break;
  488. case AV_CODEC_ID_ADPCM_G726: {
  489. if (payload_type >= RTP_PT_PRIVATE)
  490. av_strlcatf(buff, size, "a=rtpmap:%d G726-%d/%d\r\n",
  491. payload_type,
  492. c->bits_per_coded_sample*8,
  493. c->sample_rate);
  494. break;
  495. }
  496. case AV_CODEC_ID_ILBC:
  497. av_strlcatf(buff, size, "a=rtpmap:%d iLBC/%d\r\n"
  498. "a=fmtp:%d mode=%d\r\n",
  499. payload_type, c->sample_rate,
  500. payload_type, c->block_align == 38 ? 20 : 30);
  501. break;
  502. default:
  503. /* Nothing special to do here... */
  504. break;
  505. }
  506. av_free(config);
  507. return buff;
  508. }
  509. 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)
  510. {
  511. const char *type;
  512. int payload_type;
  513. payload_type = ff_rtp_get_payload_type(fmt, c);
  514. switch (c->codec_type) {
  515. case AVMEDIA_TYPE_VIDEO : type = "video" ; break;
  516. case AVMEDIA_TYPE_AUDIO : type = "audio" ; break;
  517. case AVMEDIA_TYPE_SUBTITLE: type = "text" ; break;
  518. default : type = "application"; break;
  519. }
  520. av_strlcatf(buff, size, "m=%s %d RTP/AVP %d\r\n", type, port, payload_type);
  521. sdp_write_address(buff, size, dest_addr, dest_type, ttl);
  522. if (c->bit_rate) {
  523. av_strlcatf(buff, size, "b=AS:%d\r\n", c->bit_rate / 1000);
  524. }
  525. sdp_write_media_attributes(buff, size, c, payload_type, fmt);
  526. }
  527. int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
  528. {
  529. AVDictionaryEntry *title = av_dict_get(ac[0]->metadata, "title", NULL, 0);
  530. struct sdp_session_level s = { 0 };
  531. int i, j, port, ttl, is_multicast;
  532. char dst[32], dst_type[5];
  533. memset(buf, 0, size);
  534. s.user = "-";
  535. s.src_addr = "127.0.0.1"; /* FIXME: Properly set this */
  536. s.src_type = "IP4";
  537. s.name = title ? title->value : "No Name";
  538. port = 0;
  539. ttl = 0;
  540. if (n_files == 1) {
  541. port = sdp_get_address(dst, sizeof(dst), &ttl, ac[0]->filename);
  542. is_multicast = resolve_destination(dst, sizeof(dst), dst_type,
  543. sizeof(dst_type));
  544. if (!is_multicast)
  545. ttl = 0;
  546. if (dst[0]) {
  547. s.dst_addr = dst;
  548. s.dst_type = dst_type;
  549. s.ttl = ttl;
  550. if (!strcmp(dst_type, "IP6")) {
  551. s.src_addr = "::1";
  552. s.src_type = "IP6";
  553. }
  554. }
  555. }
  556. sdp_write_header(buf, size, &s);
  557. dst[0] = 0;
  558. for (i = 0; i < n_files; i++) {
  559. if (n_files != 1) {
  560. port = sdp_get_address(dst, sizeof(dst), &ttl, ac[i]->filename);
  561. is_multicast = resolve_destination(dst, sizeof(dst), dst_type,
  562. sizeof(dst_type));
  563. if (!is_multicast)
  564. ttl = 0;
  565. }
  566. for (j = 0; j < ac[i]->nb_streams; j++) {
  567. ff_sdp_write_media(buf, size,
  568. ac[i]->streams[j]->codec, dst[0] ? dst : NULL,
  569. dst_type, (port > 0) ? port + j * 2 : 0, ttl,
  570. ac[i]);
  571. if (port <= 0) {
  572. av_strlcatf(buf, size,
  573. "a=control:streamid=%d\r\n", i + j);
  574. }
  575. }
  576. }
  577. return 0;
  578. }
  579. #else
  580. int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
  581. {
  582. return AVERROR(ENOSYS);
  583. }
  584. 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)
  585. {
  586. }
  587. #endif