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.

698 lines
25KB

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