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.

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