sdp.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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. static const char pset_string[] = "; sprop-parameter-sets=";
  141. static const char profile_string[] = "; profile-level-id=";
  142. uint8_t *orig_extradata = NULL;
  143. int orig_extradata_size = 0;
  144. const uint8_t *sps = NULL, *sps_end;
  145. if (c->extradata_size > MAX_EXTRADATA_SIZE) {
  146. av_log(c, AV_LOG_ERROR, "Too much extradata!\n");
  147. return NULL;
  148. }
  149. if (c->extradata[0] == 1) {
  150. uint8_t *dummy_p;
  151. int dummy_int;
  152. AVBitStreamFilterContext *bsfc= av_bitstream_filter_init("h264_mp4toannexb");
  153. if (!bsfc) {
  154. av_log(c, AV_LOG_ERROR, "Cannot open the h264_mp4toannexb BSF!\n");
  155. return NULL;
  156. }
  157. orig_extradata_size = c->extradata_size;
  158. orig_extradata = av_mallocz(orig_extradata_size +
  159. FF_INPUT_BUFFER_PADDING_SIZE);
  160. if (!orig_extradata) {
  161. av_bitstream_filter_close(bsfc);
  162. return NULL;
  163. }
  164. memcpy(orig_extradata, c->extradata, orig_extradata_size);
  165. av_bitstream_filter_filter(bsfc, c, NULL, &dummy_p, &dummy_int, NULL, 0, 0);
  166. av_bitstream_filter_close(bsfc);
  167. }
  168. psets = av_mallocz(MAX_PSET_SIZE);
  169. if (psets == NULL) {
  170. av_log(c, AV_LOG_ERROR, "Cannot allocate memory for the parameter sets.\n");
  171. av_free(orig_extradata);
  172. return NULL;
  173. }
  174. memcpy(psets, pset_string, strlen(pset_string));
  175. p = psets + strlen(pset_string);
  176. r = ff_avc_find_startcode(c->extradata, c->extradata + c->extradata_size);
  177. while (r < c->extradata + c->extradata_size) {
  178. const uint8_t *r1;
  179. uint8_t nal_type;
  180. while (!*(r++));
  181. nal_type = *r & 0x1f;
  182. r1 = ff_avc_find_startcode(r, c->extradata + c->extradata_size);
  183. if (nal_type != 7 && nal_type != 8) { /* Only output SPS and PPS */
  184. r = r1;
  185. continue;
  186. }
  187. if (p != (psets + strlen(pset_string))) {
  188. *p = ',';
  189. p++;
  190. }
  191. if (!sps) {
  192. sps = r;
  193. sps_end = r1;
  194. }
  195. if (av_base64_encode(p, MAX_PSET_SIZE - (p - psets), r, r1 - r) == NULL) {
  196. av_log(c, AV_LOG_ERROR, "Cannot Base64-encode %td %td!\n", MAX_PSET_SIZE - (p - psets), r1 - r);
  197. av_free(psets);
  198. return NULL;
  199. }
  200. p += strlen(p);
  201. r = r1;
  202. }
  203. if (sps && sps_end - sps >= 4) {
  204. memcpy(p, profile_string, strlen(profile_string));
  205. p += strlen(p);
  206. ff_data_to_hex(p, sps + 1, 3, 0);
  207. p[6] = '\0';
  208. }
  209. if (orig_extradata) {
  210. av_free(c->extradata);
  211. c->extradata = orig_extradata;
  212. c->extradata_size = orig_extradata_size;
  213. }
  214. return psets;
  215. }
  216. static char *extradata2config(AVCodecContext *c)
  217. {
  218. char *config;
  219. if (c->extradata_size > MAX_EXTRADATA_SIZE) {
  220. av_log(c, AV_LOG_ERROR, "Too much extradata!\n");
  221. return NULL;
  222. }
  223. config = av_malloc(10 + c->extradata_size * 2);
  224. if (config == NULL) {
  225. av_log(c, AV_LOG_ERROR, "Cannot allocate memory for the config info.\n");
  226. return NULL;
  227. }
  228. memcpy(config, "; config=", 9);
  229. ff_data_to_hex(config + 9, c->extradata, c->extradata_size, 0);
  230. config[9 + c->extradata_size * 2] = 0;
  231. return config;
  232. }
  233. static char *xiph_extradata2config(AVCodecContext *c)
  234. {
  235. char *config, *encoded_config;
  236. uint8_t *header_start[3];
  237. int headers_len, header_len[3], config_len;
  238. int first_header_size;
  239. switch (c->codec_id) {
  240. case AV_CODEC_ID_THEORA:
  241. first_header_size = 42;
  242. break;
  243. case AV_CODEC_ID_VORBIS:
  244. first_header_size = 30;
  245. break;
  246. default:
  247. av_log(c, AV_LOG_ERROR, "Unsupported Xiph codec ID\n");
  248. return NULL;
  249. }
  250. if (avpriv_split_xiph_headers(c->extradata, c->extradata_size,
  251. first_header_size, header_start,
  252. header_len) < 0) {
  253. av_log(c, AV_LOG_ERROR, "Extradata corrupt.\n");
  254. return NULL;
  255. }
  256. headers_len = header_len[0] + header_len[2];
  257. config_len = 4 + // count
  258. 3 + // ident
  259. 2 + // packet size
  260. 1 + // header count
  261. 2 + // header size
  262. headers_len; // and the rest
  263. config = av_malloc(config_len);
  264. if (!config)
  265. goto xiph_fail;
  266. encoded_config = av_malloc(AV_BASE64_SIZE(config_len));
  267. if (!encoded_config) {
  268. av_free(config);
  269. goto xiph_fail;
  270. }
  271. config[0] = config[1] = config[2] = 0;
  272. config[3] = 1;
  273. config[4] = (RTP_XIPH_IDENT >> 16) & 0xff;
  274. config[5] = (RTP_XIPH_IDENT >> 8) & 0xff;
  275. config[6] = (RTP_XIPH_IDENT ) & 0xff;
  276. config[7] = (headers_len >> 8) & 0xff;
  277. config[8] = headers_len & 0xff;
  278. config[9] = 2;
  279. config[10] = header_len[0];
  280. config[11] = 0; // size of comment header; nonexistent
  281. memcpy(config + 12, header_start[0], header_len[0]);
  282. memcpy(config + 12 + header_len[0], header_start[2], header_len[2]);
  283. av_base64_encode(encoded_config, AV_BASE64_SIZE(config_len),
  284. config, config_len);
  285. av_free(config);
  286. return encoded_config;
  287. xiph_fail:
  288. av_log(c, AV_LOG_ERROR,
  289. "Not enough memory for configuration string\n");
  290. return NULL;
  291. }
  292. static int latm_context2profilelevel(AVCodecContext *c)
  293. {
  294. /* MP4A-LATM
  295. * The RTP payload format specification is described in RFC 3016
  296. * The encoding specifications are provided in ISO/IEC 14496-3 */
  297. int profile_level = 0x2B;
  298. /* TODO: AAC Profile only supports AAC LC Object Type.
  299. * Different Object Types should implement different Profile Levels */
  300. if (c->sample_rate <= 24000) {
  301. if (c->channels <= 2)
  302. profile_level = 0x28; // AAC Profile, Level 1
  303. } else if (c->sample_rate <= 48000) {
  304. if (c->channels <= 2) {
  305. profile_level = 0x29; // AAC Profile, Level 2
  306. } else if (c->channels <= 5) {
  307. profile_level = 0x2A; // AAC Profile, Level 4
  308. }
  309. } else if (c->sample_rate <= 96000) {
  310. if (c->channels <= 5) {
  311. profile_level = 0x2B; // AAC Profile, Level 5
  312. }
  313. }
  314. return profile_level;
  315. }
  316. static char *latm_context2config(AVCodecContext *c)
  317. {
  318. /* MP4A-LATM
  319. * The RTP payload format specification is described in RFC 3016
  320. * The encoding specifications are provided in ISO/IEC 14496-3 */
  321. uint8_t config_byte[6];
  322. int rate_index;
  323. char *config;
  324. for (rate_index = 0; rate_index < 16; rate_index++)
  325. if (avpriv_mpeg4audio_sample_rates[rate_index] == c->sample_rate)
  326. break;
  327. if (rate_index == 16) {
  328. av_log(c, AV_LOG_ERROR, "Unsupported sample rate\n");
  329. return NULL;
  330. }
  331. config_byte[0] = 0x40;
  332. config_byte[1] = 0;
  333. config_byte[2] = 0x20 | rate_index;
  334. config_byte[3] = c->channels << 4;
  335. config_byte[4] = 0x3f;
  336. config_byte[5] = 0xc0;
  337. config = av_malloc(6*2+1);
  338. if (!config) {
  339. av_log(c, AV_LOG_ERROR, "Cannot allocate memory for the config info.\n");
  340. return NULL;
  341. }
  342. ff_data_to_hex(config, config_byte, 6, 1);
  343. config[12] = 0;
  344. return config;
  345. }
  346. static char *sdp_write_media_attributes(char *buff, int size, AVCodecContext *c, int payload_type, AVFormatContext *fmt)
  347. {
  348. char *config = NULL;
  349. switch (c->codec_id) {
  350. case AV_CODEC_ID_H264: {
  351. int mode = 1;
  352. if (fmt && fmt->oformat->priv_class &&
  353. av_opt_flag_is_set(fmt->priv_data, "rtpflags", "h264_mode0"))
  354. mode = 0;
  355. if (c->extradata_size) {
  356. config = extradata2psets(c);
  357. }
  358. av_strlcatf(buff, size, "a=rtpmap:%d H264/90000\r\n"
  359. "a=fmtp:%d packetization-mode=%d%s\r\n",
  360. payload_type,
  361. payload_type, mode, config ? config : "");
  362. break;
  363. }
  364. case AV_CODEC_ID_H263:
  365. case AV_CODEC_ID_H263P:
  366. /* a=framesize is required by 3GPP TS 26.234 (PSS). It
  367. * actually specifies the maximum video size, but we only know
  368. * the current size. This is required for playback on Android
  369. * stagefright and on Samsung bada. */
  370. if (!fmt || !fmt->oformat->priv_class ||
  371. !av_opt_flag_is_set(fmt->priv_data, "rtpflags", "rfc2190") ||
  372. c->codec_id == AV_CODEC_ID_H263P)
  373. av_strlcatf(buff, size, "a=rtpmap:%d H263-2000/90000\r\n"
  374. "a=framesize:%d %d-%d\r\n",
  375. payload_type,
  376. payload_type, c->width, c->height);
  377. break;
  378. case AV_CODEC_ID_MPEG4:
  379. if (c->extradata_size) {
  380. config = extradata2config(c);
  381. }
  382. av_strlcatf(buff, size, "a=rtpmap:%d MP4V-ES/90000\r\n"
  383. "a=fmtp:%d profile-level-id=1%s\r\n",
  384. payload_type,
  385. payload_type, config ? config : "");
  386. break;
  387. case AV_CODEC_ID_AAC:
  388. if (fmt && fmt->oformat && fmt->oformat->priv_class &&
  389. av_opt_flag_is_set(fmt->priv_data, "rtpflags", "latm")) {
  390. config = latm_context2config(c);
  391. if (!config)
  392. return NULL;
  393. av_strlcatf(buff, size, "a=rtpmap:%d MP4A-LATM/%d/%d\r\n"
  394. "a=fmtp:%d profile-level-id=%d;cpresent=0;config=%s\r\n",
  395. payload_type, c->sample_rate, c->channels,
  396. payload_type, latm_context2profilelevel(c), config);
  397. } else {
  398. if (c->extradata_size) {
  399. config = extradata2config(c);
  400. } else {
  401. /* FIXME: maybe we can forge config information based on the
  402. * codec parameters...
  403. */
  404. av_log(c, AV_LOG_ERROR, "AAC with no global headers is currently not supported.\n");
  405. return NULL;
  406. }
  407. if (config == NULL) {
  408. return NULL;
  409. }
  410. av_strlcatf(buff, size, "a=rtpmap:%d MPEG4-GENERIC/%d/%d\r\n"
  411. "a=fmtp:%d profile-level-id=1;"
  412. "mode=AAC-hbr;sizelength=13;indexlength=3;"
  413. "indexdeltalength=3%s\r\n",
  414. payload_type, c->sample_rate, c->channels,
  415. payload_type, config);
  416. }
  417. break;
  418. case AV_CODEC_ID_PCM_S16BE:
  419. if (payload_type >= RTP_PT_PRIVATE)
  420. av_strlcatf(buff, size, "a=rtpmap:%d L16/%d/%d\r\n",
  421. payload_type,
  422. c->sample_rate, c->channels);
  423. break;
  424. case AV_CODEC_ID_PCM_MULAW:
  425. if (payload_type >= RTP_PT_PRIVATE)
  426. av_strlcatf(buff, size, "a=rtpmap:%d PCMU/%d/%d\r\n",
  427. payload_type,
  428. c->sample_rate, c->channels);
  429. break;
  430. case AV_CODEC_ID_PCM_ALAW:
  431. if (payload_type >= RTP_PT_PRIVATE)
  432. av_strlcatf(buff, size, "a=rtpmap:%d PCMA/%d/%d\r\n",
  433. payload_type,
  434. c->sample_rate, c->channels);
  435. break;
  436. case AV_CODEC_ID_AMR_NB:
  437. av_strlcatf(buff, size, "a=rtpmap:%d AMR/%d/%d\r\n"
  438. "a=fmtp:%d octet-align=1\r\n",
  439. payload_type, c->sample_rate, c->channels,
  440. payload_type);
  441. break;
  442. case AV_CODEC_ID_AMR_WB:
  443. av_strlcatf(buff, size, "a=rtpmap:%d AMR-WB/%d/%d\r\n"
  444. "a=fmtp:%d octet-align=1\r\n",
  445. payload_type, c->sample_rate, c->channels,
  446. payload_type);
  447. break;
  448. case AV_CODEC_ID_VORBIS:
  449. if (c->extradata_size)
  450. config = xiph_extradata2config(c);
  451. else
  452. av_log(c, AV_LOG_ERROR, "Vorbis configuration info missing\n");
  453. if (!config)
  454. return NULL;
  455. av_strlcatf(buff, size, "a=rtpmap:%d vorbis/%d/%d\r\n"
  456. "a=fmtp:%d configuration=%s\r\n",
  457. payload_type, c->sample_rate, c->channels,
  458. payload_type, config);
  459. break;
  460. case AV_CODEC_ID_THEORA: {
  461. const char *pix_fmt;
  462. if (c->extradata_size)
  463. config = xiph_extradata2config(c);
  464. else
  465. av_log(c, AV_LOG_ERROR, "Theora configuation info missing\n");
  466. if (!config)
  467. return NULL;
  468. switch (c->pix_fmt) {
  469. case PIX_FMT_YUV420P:
  470. pix_fmt = "YCbCr-4:2:0";
  471. break;
  472. case PIX_FMT_YUV422P:
  473. pix_fmt = "YCbCr-4:2:2";
  474. break;
  475. case PIX_FMT_YUV444P:
  476. pix_fmt = "YCbCr-4:4:4";
  477. break;
  478. default:
  479. av_log(c, AV_LOG_ERROR, "Unsupported pixel format.\n");
  480. return NULL;
  481. }
  482. av_strlcatf(buff, size, "a=rtpmap:%d theora/90000\r\n"
  483. "a=fmtp:%d delivery-method=inline; "
  484. "width=%d; height=%d; sampling=%s; "
  485. "configuration=%s\r\n",
  486. payload_type, payload_type,
  487. c->width, c->height, pix_fmt, config);
  488. break;
  489. }
  490. case AV_CODEC_ID_VP8:
  491. av_strlcatf(buff, size, "a=rtpmap:%d VP8/90000\r\n",
  492. payload_type);
  493. break;
  494. case AV_CODEC_ID_MJPEG:
  495. if (payload_type >= RTP_PT_PRIVATE)
  496. av_strlcatf(buff, size, "a=rtpmap:%d JPEG/90000\r\n",
  497. payload_type);
  498. break;
  499. case AV_CODEC_ID_ADPCM_G722:
  500. if (payload_type >= RTP_PT_PRIVATE)
  501. av_strlcatf(buff, size, "a=rtpmap:%d G722/%d/%d\r\n",
  502. payload_type,
  503. 8000, c->channels);
  504. break;
  505. case AV_CODEC_ID_ADPCM_G726: {
  506. if (payload_type >= RTP_PT_PRIVATE)
  507. av_strlcatf(buff, size, "a=rtpmap:%d G726-%d/%d\r\n",
  508. payload_type,
  509. c->bits_per_coded_sample*8,
  510. c->sample_rate);
  511. break;
  512. }
  513. case AV_CODEC_ID_ILBC:
  514. av_strlcatf(buff, size, "a=rtpmap:%d iLBC/%d\r\n"
  515. "a=fmtp:%d mode=%d\r\n",
  516. payload_type, c->sample_rate,
  517. payload_type, c->block_align == 38 ? 20 : 30);
  518. break;
  519. case AV_CODEC_ID_SPEEX:
  520. av_strlcatf(buff, size, "a=rtpmap:%d speex/%d\r\n",
  521. payload_type, c->sample_rate);
  522. break;
  523. default:
  524. /* Nothing special to do here... */
  525. break;
  526. }
  527. av_free(config);
  528. return buff;
  529. }
  530. 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)
  531. {
  532. const char *type;
  533. int payload_type;
  534. payload_type = ff_rtp_get_payload_type(fmt, c);
  535. switch (c->codec_type) {
  536. case AVMEDIA_TYPE_VIDEO : type = "video" ; break;
  537. case AVMEDIA_TYPE_AUDIO : type = "audio" ; break;
  538. case AVMEDIA_TYPE_SUBTITLE: type = "text" ; break;
  539. default : type = "application"; break;
  540. }
  541. av_strlcatf(buff, size, "m=%s %d RTP/AVP %d\r\n", type, port, payload_type);
  542. sdp_write_address(buff, size, dest_addr, dest_type, ttl);
  543. if (c->bit_rate) {
  544. av_strlcatf(buff, size, "b=AS:%d\r\n", c->bit_rate / 1000);
  545. }
  546. sdp_write_media_attributes(buff, size, c, payload_type, fmt);
  547. }
  548. int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
  549. {
  550. AVDictionaryEntry *title = av_dict_get(ac[0]->metadata, "title", NULL, 0);
  551. struct sdp_session_level s = { 0 };
  552. int i, j, port, ttl, is_multicast;
  553. char dst[32], dst_type[5];
  554. memset(buf, 0, size);
  555. s.user = "-";
  556. s.src_addr = "127.0.0.1"; /* FIXME: Properly set this */
  557. s.src_type = "IP4";
  558. s.name = title ? title->value : "No Name";
  559. port = 0;
  560. ttl = 0;
  561. if (n_files == 1) {
  562. port = sdp_get_address(dst, sizeof(dst), &ttl, ac[0]->filename);
  563. is_multicast = resolve_destination(dst, sizeof(dst), dst_type,
  564. sizeof(dst_type));
  565. if (!is_multicast)
  566. ttl = 0;
  567. if (dst[0]) {
  568. s.dst_addr = dst;
  569. s.dst_type = dst_type;
  570. s.ttl = ttl;
  571. if (!strcmp(dst_type, "IP6")) {
  572. s.src_addr = "::1";
  573. s.src_type = "IP6";
  574. }
  575. }
  576. }
  577. sdp_write_header(buf, size, &s);
  578. dst[0] = 0;
  579. for (i = 0; i < n_files; i++) {
  580. if (n_files != 1) {
  581. port = sdp_get_address(dst, sizeof(dst), &ttl, ac[i]->filename);
  582. is_multicast = resolve_destination(dst, sizeof(dst), dst_type,
  583. sizeof(dst_type));
  584. if (!is_multicast)
  585. ttl = 0;
  586. }
  587. for (j = 0; j < ac[i]->nb_streams; j++) {
  588. ff_sdp_write_media(buf, size,
  589. ac[i]->streams[j]->codec, dst[0] ? dst : NULL,
  590. dst_type, (port > 0) ? port + j * 2 : 0, ttl,
  591. ac[i]);
  592. if (port <= 0) {
  593. av_strlcatf(buf, size,
  594. "a=control:streamid=%d\r\n", i + j);
  595. }
  596. }
  597. }
  598. return 0;
  599. }
  600. #else
  601. int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
  602. {
  603. return AVERROR(ENOSYS);
  604. }
  605. 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)
  606. {
  607. }
  608. #endif