You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

727 lines
26KB

  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") && strcmp(proto, "srtp")) {
  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) {
  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)) {
  196. av_log(c, AV_LOG_ERROR, "Cannot Base64-encode %"PTRDIFF_SPECIFIER" %"PTRDIFF_SPECIFIER"!\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) {
  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 && 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_H261:
  365. {
  366. const char *pic_fmt = NULL;
  367. /* only QCIF and CIF are specified as supported in RFC 4587 */
  368. if (c->width == 176 && c->height == 144)
  369. pic_fmt = "QCIF=1";
  370. if (c->width == 352 && c->height == 288)
  371. pic_fmt = "CIF=1";
  372. av_strlcatf(buff, size, "a=rtpmap:%d H261/90000\r\n", payload_type);
  373. if (pic_fmt)
  374. av_strlcatf(buff, size, "a=fmtp:%d %s\r\n", payload_type, pic_fmt);
  375. break;
  376. }
  377. case AV_CODEC_ID_H263:
  378. case AV_CODEC_ID_H263P:
  379. /* a=framesize is required by 3GPP TS 26.234 (PSS). It
  380. * actually specifies the maximum video size, but we only know
  381. * the current size. This is required for playback on Android
  382. * stagefright and on Samsung bada. */
  383. if (!fmt || !fmt->oformat->priv_class ||
  384. !av_opt_flag_is_set(fmt->priv_data, "rtpflags", "rfc2190") ||
  385. c->codec_id == AV_CODEC_ID_H263P)
  386. av_strlcatf(buff, size, "a=rtpmap:%d H263-2000/90000\r\n"
  387. "a=framesize:%d %d-%d\r\n",
  388. payload_type,
  389. payload_type, c->width, c->height);
  390. break;
  391. case AV_CODEC_ID_MPEG4:
  392. if (c->extradata_size) {
  393. config = extradata2config(c);
  394. }
  395. av_strlcatf(buff, size, "a=rtpmap:%d MP4V-ES/90000\r\n"
  396. "a=fmtp:%d profile-level-id=1%s\r\n",
  397. payload_type,
  398. payload_type, config ? config : "");
  399. break;
  400. case AV_CODEC_ID_AAC:
  401. if (fmt && fmt->oformat && fmt->oformat->priv_class &&
  402. av_opt_flag_is_set(fmt->priv_data, "rtpflags", "latm")) {
  403. config = latm_context2config(c);
  404. if (!config)
  405. return NULL;
  406. av_strlcatf(buff, size, "a=rtpmap:%d MP4A-LATM/%d/%d\r\n"
  407. "a=fmtp:%d profile-level-id=%d;cpresent=0;config=%s\r\n",
  408. payload_type, c->sample_rate, c->channels,
  409. payload_type, latm_context2profilelevel(c), config);
  410. } else {
  411. if (c->extradata_size) {
  412. config = extradata2config(c);
  413. } else {
  414. /* FIXME: maybe we can forge config information based on the
  415. * codec parameters...
  416. */
  417. av_log(c, AV_LOG_ERROR, "AAC with no global headers is currently not supported.\n");
  418. return NULL;
  419. }
  420. if (!config) {
  421. return NULL;
  422. }
  423. av_strlcatf(buff, size, "a=rtpmap:%d MPEG4-GENERIC/%d/%d\r\n"
  424. "a=fmtp:%d profile-level-id=1;"
  425. "mode=AAC-hbr;sizelength=13;indexlength=3;"
  426. "indexdeltalength=3%s\r\n",
  427. payload_type, c->sample_rate, c->channels,
  428. payload_type, config);
  429. }
  430. break;
  431. case AV_CODEC_ID_PCM_S16BE:
  432. if (payload_type >= RTP_PT_PRIVATE)
  433. av_strlcatf(buff, size, "a=rtpmap:%d L16/%d/%d\r\n",
  434. payload_type,
  435. c->sample_rate, c->channels);
  436. break;
  437. case AV_CODEC_ID_PCM_MULAW:
  438. if (payload_type >= RTP_PT_PRIVATE)
  439. av_strlcatf(buff, size, "a=rtpmap:%d PCMU/%d/%d\r\n",
  440. payload_type,
  441. c->sample_rate, c->channels);
  442. break;
  443. case AV_CODEC_ID_PCM_ALAW:
  444. if (payload_type >= RTP_PT_PRIVATE)
  445. av_strlcatf(buff, size, "a=rtpmap:%d PCMA/%d/%d\r\n",
  446. payload_type,
  447. c->sample_rate, c->channels);
  448. break;
  449. case AV_CODEC_ID_AMR_NB:
  450. av_strlcatf(buff, size, "a=rtpmap:%d AMR/%d/%d\r\n"
  451. "a=fmtp:%d octet-align=1\r\n",
  452. payload_type, c->sample_rate, c->channels,
  453. payload_type);
  454. break;
  455. case AV_CODEC_ID_AMR_WB:
  456. av_strlcatf(buff, size, "a=rtpmap:%d AMR-WB/%d/%d\r\n"
  457. "a=fmtp:%d octet-align=1\r\n",
  458. payload_type, c->sample_rate, c->channels,
  459. payload_type);
  460. break;
  461. case AV_CODEC_ID_VORBIS:
  462. if (c->extradata_size)
  463. config = xiph_extradata2config(c);
  464. else
  465. av_log(c, AV_LOG_ERROR, "Vorbis configuration info missing\n");
  466. if (!config)
  467. return NULL;
  468. av_strlcatf(buff, size, "a=rtpmap:%d vorbis/%d/%d\r\n"
  469. "a=fmtp:%d configuration=%s\r\n",
  470. payload_type, c->sample_rate, c->channels,
  471. payload_type, config);
  472. break;
  473. case AV_CODEC_ID_THEORA: {
  474. const char *pix_fmt;
  475. switch (c->pix_fmt) {
  476. case AV_PIX_FMT_YUV420P:
  477. pix_fmt = "YCbCr-4:2:0";
  478. break;
  479. case AV_PIX_FMT_YUV422P:
  480. pix_fmt = "YCbCr-4:2:2";
  481. break;
  482. case AV_PIX_FMT_YUV444P:
  483. pix_fmt = "YCbCr-4:4:4";
  484. break;
  485. default:
  486. av_log(c, AV_LOG_ERROR, "Unsupported pixel format.\n");
  487. return NULL;
  488. }
  489. if (c->extradata_size)
  490. config = xiph_extradata2config(c);
  491. else
  492. av_log(c, AV_LOG_ERROR, "Theora configuation info missing\n");
  493. if (!config)
  494. return NULL;
  495. av_strlcatf(buff, size, "a=rtpmap:%d theora/90000\r\n"
  496. "a=fmtp:%d delivery-method=inline; "
  497. "width=%d; height=%d; sampling=%s; "
  498. "configuration=%s\r\n",
  499. payload_type, payload_type,
  500. c->width, c->height, pix_fmt, config);
  501. break;
  502. }
  503. case AV_CODEC_ID_VP8:
  504. av_strlcatf(buff, size, "a=rtpmap:%d VP8/90000\r\n",
  505. payload_type);
  506. break;
  507. case AV_CODEC_ID_MJPEG:
  508. if (payload_type >= RTP_PT_PRIVATE)
  509. av_strlcatf(buff, size, "a=rtpmap:%d JPEG/90000\r\n",
  510. payload_type);
  511. break;
  512. case AV_CODEC_ID_ADPCM_G722:
  513. if (payload_type >= RTP_PT_PRIVATE)
  514. av_strlcatf(buff, size, "a=rtpmap:%d G722/%d/%d\r\n",
  515. payload_type,
  516. 8000, c->channels);
  517. break;
  518. case AV_CODEC_ID_ADPCM_G726: {
  519. if (payload_type >= RTP_PT_PRIVATE)
  520. av_strlcatf(buff, size, "a=rtpmap:%d G726-%d/%d\r\n",
  521. payload_type,
  522. c->bits_per_coded_sample*8,
  523. c->sample_rate);
  524. break;
  525. }
  526. case AV_CODEC_ID_ILBC:
  527. av_strlcatf(buff, size, "a=rtpmap:%d iLBC/%d\r\n"
  528. "a=fmtp:%d mode=%d\r\n",
  529. payload_type, c->sample_rate,
  530. payload_type, c->block_align == 38 ? 20 : 30);
  531. break;
  532. case AV_CODEC_ID_SPEEX:
  533. av_strlcatf(buff, size, "a=rtpmap:%d speex/%d\r\n",
  534. payload_type, c->sample_rate);
  535. if (c->codec) {
  536. const char *mode;
  537. uint64_t vad_option;
  538. if (c->flags & CODEC_FLAG_QSCALE)
  539. mode = "on";
  540. else if (!av_opt_get_int(c, "vad", AV_OPT_FLAG_ENCODING_PARAM, &vad_option) && vad_option)
  541. mode = "vad";
  542. else
  543. mode = "off";
  544. av_strlcatf(buff, size, "a=fmtp:%d vbr=%s\r\n",
  545. payload_type, mode);
  546. }
  547. break;
  548. case AV_CODEC_ID_OPUS:
  549. av_strlcatf(buff, size, "a=rtpmap:%d opus/48000\r\n",
  550. payload_type);
  551. break;
  552. default:
  553. /* Nothing special to do here... */
  554. break;
  555. }
  556. av_free(config);
  557. return buff;
  558. }
  559. void ff_sdp_write_media(char *buff, int size, AVStream *st, int idx,
  560. const char *dest_addr, const char *dest_type,
  561. int port, int ttl, AVFormatContext *fmt)
  562. {
  563. AVCodecContext *c = st->codec;
  564. const char *type;
  565. int payload_type;
  566. payload_type = ff_rtp_get_payload_type(fmt, c, idx);
  567. switch (c->codec_type) {
  568. case AVMEDIA_TYPE_VIDEO : type = "video" ; break;
  569. case AVMEDIA_TYPE_AUDIO : type = "audio" ; break;
  570. case AVMEDIA_TYPE_SUBTITLE: type = "text" ; break;
  571. default : type = "application"; break;
  572. }
  573. av_strlcatf(buff, size, "m=%s %d RTP/AVP %d\r\n", type, port, payload_type);
  574. sdp_write_address(buff, size, dest_addr, dest_type, ttl);
  575. if (c->bit_rate) {
  576. av_strlcatf(buff, size, "b=AS:%d\r\n", c->bit_rate / 1000);
  577. }
  578. sdp_write_media_attributes(buff, size, c, payload_type, fmt);
  579. }
  580. int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
  581. {
  582. AVDictionaryEntry *title = av_dict_get(ac[0]->metadata, "title", NULL, 0);
  583. struct sdp_session_level s = { 0 };
  584. int i, j, port, ttl, is_multicast, index = 0;
  585. char dst[32], dst_type[5];
  586. memset(buf, 0, size);
  587. s.user = "-";
  588. s.src_addr = "127.0.0.1"; /* FIXME: Properly set this */
  589. s.src_type = "IP4";
  590. s.name = title ? title->value : "No Name";
  591. port = 0;
  592. ttl = 0;
  593. if (n_files == 1) {
  594. port = sdp_get_address(dst, sizeof(dst), &ttl, ac[0]->filename);
  595. is_multicast = resolve_destination(dst, sizeof(dst), dst_type,
  596. sizeof(dst_type));
  597. if (!is_multicast)
  598. ttl = 0;
  599. if (dst[0]) {
  600. s.dst_addr = dst;
  601. s.dst_type = dst_type;
  602. s.ttl = ttl;
  603. if (!strcmp(dst_type, "IP6")) {
  604. s.src_addr = "::1";
  605. s.src_type = "IP6";
  606. }
  607. }
  608. }
  609. sdp_write_header(buf, size, &s);
  610. dst[0] = 0;
  611. for (i = 0; i < n_files; i++) {
  612. if (n_files != 1) {
  613. port = sdp_get_address(dst, sizeof(dst), &ttl, ac[i]->filename);
  614. is_multicast = resolve_destination(dst, sizeof(dst), dst_type,
  615. sizeof(dst_type));
  616. if (!is_multicast)
  617. ttl = 0;
  618. }
  619. for (j = 0; j < ac[i]->nb_streams; j++) {
  620. ff_sdp_write_media(buf, size, ac[i]->streams[j], index++,
  621. dst[0] ? dst : NULL, dst_type,
  622. (port > 0) ? port + j * 2 : 0,
  623. ttl, ac[i]);
  624. if (port <= 0) {
  625. av_strlcatf(buf, size,
  626. "a=control:streamid=%d\r\n", i + j);
  627. }
  628. if (ac[i]->pb && ac[i]->pb->av_class) {
  629. uint8_t *crypto_suite = NULL, *crypto_params = NULL;
  630. av_opt_get(ac[i]->pb, "srtp_out_suite", AV_OPT_SEARCH_CHILDREN,
  631. &crypto_suite);
  632. av_opt_get(ac[i]->pb, "srtp_out_params", AV_OPT_SEARCH_CHILDREN,
  633. &crypto_params);
  634. if (crypto_suite && crypto_suite[0])
  635. av_strlcatf(buf, size,
  636. "a=crypto:1 %s inline:%s\r\n",
  637. crypto_suite, crypto_params);
  638. av_free(crypto_suite);
  639. av_free(crypto_params);
  640. }
  641. }
  642. }
  643. return 0;
  644. }
  645. #else
  646. int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
  647. {
  648. return AVERROR(ENOSYS);
  649. }
  650. void ff_sdp_write_media(char *buff, int size, AVStream *st, int idx,
  651. const char *dest_addr, const char *dest_type,
  652. int port, int ttl, AVFormatContext *fmt)
  653. {
  654. }
  655. #endif