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.

802 lines
28KB

  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 "hevc.h"
  32. #include "rtp.h"
  33. #if CONFIG_NETWORK
  34. #include "network.h"
  35. #endif
  36. #if CONFIG_RTP_MUXER
  37. #define MAX_EXTRADATA_SIZE ((INT_MAX - 10) / 2)
  38. struct sdp_session_level {
  39. int sdp_version; /**< protocol version (currently 0) */
  40. int id; /**< session ID */
  41. int version; /**< session version */
  42. int start_time; /**< session start time (NTP time, in seconds),
  43. or 0 in case of permanent session */
  44. int end_time; /**< session end time (NTP time, in seconds),
  45. or 0 if the session is not bounded */
  46. int ttl; /**< TTL, in case of multicast stream */
  47. const char *user; /**< username of the session's creator */
  48. const char *src_addr; /**< IP address of the machine from which the session was created */
  49. const char *src_type; /**< address type of src_addr */
  50. const char *dst_addr; /**< destination IP address (can be multicast) */
  51. const char *dst_type; /**< destination IP address type */
  52. const char *name; /**< session name (can be an empty string) */
  53. };
  54. static void sdp_write_address(char *buff, int size, const char *dest_addr,
  55. const char *dest_type, int ttl)
  56. {
  57. if (dest_addr) {
  58. if (!dest_type)
  59. dest_type = "IP4";
  60. if (ttl > 0 && !strcmp(dest_type, "IP4")) {
  61. /* The TTL should only be specified for IPv4 multicast addresses,
  62. * not for IPv6. */
  63. av_strlcatf(buff, size, "c=IN %s %s/%d\r\n", dest_type, dest_addr, ttl);
  64. } else {
  65. av_strlcatf(buff, size, "c=IN %s %s\r\n", dest_type, dest_addr);
  66. }
  67. }
  68. }
  69. static void sdp_write_header(char *buff, int size, struct sdp_session_level *s)
  70. {
  71. av_strlcatf(buff, size, "v=%d\r\n"
  72. "o=- %d %d IN %s %s\r\n"
  73. "s=%s\r\n",
  74. s->sdp_version,
  75. s->id, s->version, s->src_type, s->src_addr,
  76. s->name);
  77. sdp_write_address(buff, size, s->dst_addr, s->dst_type, s->ttl);
  78. av_strlcatf(buff, size, "t=%d %d\r\n"
  79. "a=tool:libavformat " AV_STRINGIFY(LIBAVFORMAT_VERSION) "\r\n",
  80. s->start_time, s->end_time);
  81. }
  82. #if CONFIG_NETWORK
  83. static int resolve_destination(char *dest_addr, int size, char *type,
  84. int type_size)
  85. {
  86. struct addrinfo hints = { 0 }, *ai;
  87. int is_multicast;
  88. av_strlcpy(type, "IP4", type_size);
  89. if (!dest_addr[0])
  90. return 0;
  91. /* Resolve the destination, since it must be written
  92. * as a numeric IP address in the SDP. */
  93. if (getaddrinfo(dest_addr, NULL, &hints, &ai))
  94. return 0;
  95. getnameinfo(ai->ai_addr, ai->ai_addrlen, dest_addr, size,
  96. NULL, 0, NI_NUMERICHOST);
  97. #ifdef AF_INET6
  98. if (ai->ai_family == AF_INET6)
  99. av_strlcpy(type, "IP6", type_size);
  100. #endif
  101. is_multicast = ff_is_multicast_address(ai->ai_addr);
  102. freeaddrinfo(ai);
  103. return is_multicast;
  104. }
  105. #else
  106. static int resolve_destination(char *dest_addr, int size, char *type,
  107. int type_size)
  108. {
  109. return 0;
  110. }
  111. #endif
  112. static int sdp_get_address(char *dest_addr, int size, int *ttl, const char *url)
  113. {
  114. int port;
  115. const char *p;
  116. char proto[32];
  117. av_url_split(proto, sizeof(proto), NULL, 0, dest_addr, size, &port, NULL, 0, url);
  118. *ttl = 0;
  119. if (strcmp(proto, "rtp") && strcmp(proto, "srtp")) {
  120. /* The url isn't for the actual rtp sessions,
  121. * don't parse out anything else than the destination.
  122. */
  123. return 0;
  124. }
  125. p = strchr(url, '?');
  126. if (p) {
  127. char buff[64];
  128. if (av_find_info_tag(buff, sizeof(buff), "ttl", p)) {
  129. *ttl = strtol(buff, NULL, 10);
  130. } else {
  131. *ttl = 5;
  132. }
  133. }
  134. return port;
  135. }
  136. #define MAX_PSET_SIZE 1024
  137. static char *extradata2psets(AVCodecContext *c)
  138. {
  139. char *psets, *p;
  140. const uint8_t *r;
  141. static const char pset_string[] = "; sprop-parameter-sets=";
  142. static const char profile_string[] = "; profile-level-id=";
  143. uint8_t *extradata = c->extradata;
  144. int extradata_size = c->extradata_size;
  145. uint8_t *tmpbuf = NULL;
  146. const uint8_t *sps = NULL, *sps_end;
  147. if (c->extradata_size > MAX_EXTRADATA_SIZE) {
  148. av_log(c, AV_LOG_ERROR, "Too much extradata!\n");
  149. return NULL;
  150. }
  151. if (c->extradata[0] == 1) {
  152. if (ff_avc_write_annexb_extradata(c->extradata, &extradata,
  153. &extradata_size))
  154. return NULL;
  155. tmpbuf = extradata;
  156. }
  157. psets = av_mallocz(MAX_PSET_SIZE);
  158. if (!psets) {
  159. av_log(c, AV_LOG_ERROR, "Cannot allocate memory for the parameter sets.\n");
  160. av_free(tmpbuf);
  161. return NULL;
  162. }
  163. memcpy(psets, pset_string, strlen(pset_string));
  164. p = psets + strlen(pset_string);
  165. r = ff_avc_find_startcode(extradata, extradata + extradata_size);
  166. while (r < extradata + extradata_size) {
  167. const uint8_t *r1;
  168. uint8_t nal_type;
  169. while (!*(r++));
  170. nal_type = *r & 0x1f;
  171. r1 = ff_avc_find_startcode(r, extradata + extradata_size);
  172. if (nal_type != 7 && nal_type != 8) { /* Only output SPS and PPS */
  173. r = r1;
  174. continue;
  175. }
  176. if (p != (psets + strlen(pset_string))) {
  177. *p = ',';
  178. p++;
  179. }
  180. if (!sps) {
  181. sps = r;
  182. sps_end = r1;
  183. }
  184. if (!av_base64_encode(p, MAX_PSET_SIZE - (p - psets), r, r1 - r)) {
  185. av_log(c, AV_LOG_ERROR, "Cannot Base64-encode %td %td!\n", MAX_PSET_SIZE - (p - psets), r1 - r);
  186. av_free(psets);
  187. av_free(tmpbuf);
  188. return NULL;
  189. }
  190. p += strlen(p);
  191. r = r1;
  192. }
  193. if (sps && sps_end - sps >= 4) {
  194. memcpy(p, profile_string, strlen(profile_string));
  195. p += strlen(p);
  196. ff_data_to_hex(p, sps + 1, 3, 0);
  197. p[6] = '\0';
  198. }
  199. av_free(tmpbuf);
  200. return psets;
  201. }
  202. static char *extradata2psets_hevc(AVCodecContext *c)
  203. {
  204. char *psets;
  205. uint8_t *extradata = c->extradata;
  206. int extradata_size = c->extradata_size;
  207. uint8_t *tmpbuf = NULL;
  208. int ps_pos[3] = { 0 };
  209. static const char * const ps_names[3] = { "vps", "sps", "pps" };
  210. int num_arrays, num_nalus;
  211. int pos, i, j;
  212. // Convert to hvcc format. Since we need to group multiple NALUs of
  213. // the same type, and we might need to convert from one format to the
  214. // other anyway, we get away with a little less work by using the hvcc
  215. // format.
  216. if (c->extradata[0] != 1) {
  217. AVIOContext *pb;
  218. if (avio_open_dyn_buf(&pb) < 0)
  219. return NULL;
  220. if (ff_isom_write_hvcc(pb, c->extradata, c->extradata_size, 0) < 0) {
  221. avio_close_dyn_buf(pb, &tmpbuf);
  222. goto err;
  223. }
  224. extradata_size = avio_close_dyn_buf(pb, &extradata);
  225. tmpbuf = extradata;
  226. }
  227. if (extradata_size < 23)
  228. goto err;
  229. num_arrays = extradata[22];
  230. pos = 23;
  231. for (i = 0; i < num_arrays; i++) {
  232. int num_nalus, nalu_type;
  233. if (pos + 3 > extradata_size)
  234. goto err;
  235. nalu_type = extradata[pos] & 0x3f;
  236. // Not including libavcodec/hevc.h to avoid confusion between
  237. // NAL_* with the same name for both H264 and HEVC.
  238. if (nalu_type == 32) // VPS
  239. ps_pos[0] = pos;
  240. else if (nalu_type == 33) // SPS
  241. ps_pos[1] = pos;
  242. else if (nalu_type == 34) // PPS
  243. ps_pos[2] = pos;
  244. num_nalus = AV_RB16(&extradata[pos + 1]);
  245. pos += 3;
  246. for (j = 0; j < num_nalus; j++) {
  247. int len;
  248. if (pos + 2 > extradata_size)
  249. goto err;
  250. len = AV_RB16(&extradata[pos]);
  251. pos += 2;
  252. if (pos + len > extradata_size)
  253. goto err;
  254. pos += len;
  255. }
  256. }
  257. if (!ps_pos[0] || !ps_pos[1] || !ps_pos[2])
  258. goto err;
  259. psets = av_mallocz(MAX_PSET_SIZE);
  260. if (!psets)
  261. goto err;
  262. psets[0] = '\0';
  263. for (i = 0; i < 3; i++) {
  264. pos = ps_pos[i];
  265. if (i > 0)
  266. av_strlcat(psets, "; ", MAX_PSET_SIZE);
  267. av_strlcatf(psets, MAX_PSET_SIZE, "sprop-%s=", ps_names[i]);
  268. // Skipping boundary checks in the input here; we've already traversed
  269. // the whole hvcc structure above without issues
  270. num_nalus = AV_RB16(&extradata[pos + 1]);
  271. pos += 3;
  272. for (j = 0; j < num_nalus; j++) {
  273. int len = AV_RB16(&extradata[pos]);
  274. int strpos;
  275. pos += 2;
  276. if (j > 0)
  277. av_strlcat(psets, ",", MAX_PSET_SIZE);
  278. strpos = strlen(psets);
  279. if (!av_base64_encode(psets + strpos, MAX_PSET_SIZE - strpos,
  280. &extradata[pos], len)) {
  281. av_free(psets);
  282. goto err;
  283. }
  284. pos += len;
  285. }
  286. }
  287. av_free(tmpbuf);
  288. return psets;
  289. err:
  290. av_free(tmpbuf);
  291. return NULL;
  292. }
  293. static char *extradata2config(AVCodecContext *c)
  294. {
  295. char *config;
  296. if (c->extradata_size > MAX_EXTRADATA_SIZE) {
  297. av_log(c, AV_LOG_ERROR, "Too much extradata!\n");
  298. return NULL;
  299. }
  300. config = av_malloc(10 + c->extradata_size * 2);
  301. if (!config) {
  302. av_log(c, AV_LOG_ERROR, "Cannot allocate memory for the config info.\n");
  303. return NULL;
  304. }
  305. memcpy(config, "; config=", 9);
  306. ff_data_to_hex(config + 9, c->extradata, c->extradata_size, 0);
  307. config[9 + c->extradata_size * 2] = 0;
  308. return config;
  309. }
  310. static char *xiph_extradata2config(AVCodecContext *c)
  311. {
  312. char *config, *encoded_config;
  313. uint8_t *header_start[3];
  314. int headers_len, header_len[3], config_len;
  315. int first_header_size;
  316. switch (c->codec_id) {
  317. case AV_CODEC_ID_THEORA:
  318. first_header_size = 42;
  319. break;
  320. case AV_CODEC_ID_VORBIS:
  321. first_header_size = 30;
  322. break;
  323. default:
  324. av_log(c, AV_LOG_ERROR, "Unsupported Xiph codec ID\n");
  325. return NULL;
  326. }
  327. if (avpriv_split_xiph_headers(c->extradata, c->extradata_size,
  328. first_header_size, header_start,
  329. header_len) < 0) {
  330. av_log(c, AV_LOG_ERROR, "Extradata corrupt.\n");
  331. return NULL;
  332. }
  333. headers_len = header_len[0] + header_len[2];
  334. config_len = 4 + // count
  335. 3 + // ident
  336. 2 + // packet size
  337. 1 + // header count
  338. 2 + // header size
  339. headers_len; // and the rest
  340. config = av_malloc(config_len);
  341. if (!config)
  342. goto xiph_fail;
  343. encoded_config = av_malloc(AV_BASE64_SIZE(config_len));
  344. if (!encoded_config) {
  345. av_free(config);
  346. goto xiph_fail;
  347. }
  348. config[0] = config[1] = config[2] = 0;
  349. config[3] = 1;
  350. config[4] = (RTP_XIPH_IDENT >> 16) & 0xff;
  351. config[5] = (RTP_XIPH_IDENT >> 8) & 0xff;
  352. config[6] = (RTP_XIPH_IDENT ) & 0xff;
  353. config[7] = (headers_len >> 8) & 0xff;
  354. config[8] = headers_len & 0xff;
  355. config[9] = 2;
  356. config[10] = header_len[0];
  357. config[11] = 0; // size of comment header; nonexistent
  358. memcpy(config + 12, header_start[0], header_len[0]);
  359. memcpy(config + 12 + header_len[0], header_start[2], header_len[2]);
  360. av_base64_encode(encoded_config, AV_BASE64_SIZE(config_len),
  361. config, config_len);
  362. av_free(config);
  363. return encoded_config;
  364. xiph_fail:
  365. av_log(c, AV_LOG_ERROR,
  366. "Not enough memory for configuration string\n");
  367. return NULL;
  368. }
  369. static int latm_context2profilelevel(AVCodecContext *c)
  370. {
  371. /* MP4A-LATM
  372. * The RTP payload format specification is described in RFC 3016
  373. * The encoding specifications are provided in ISO/IEC 14496-3 */
  374. int profile_level = 0x2B;
  375. /* TODO: AAC Profile only supports AAC LC Object Type.
  376. * Different Object Types should implement different Profile Levels */
  377. if (c->sample_rate <= 24000) {
  378. if (c->channels <= 2)
  379. profile_level = 0x28; // AAC Profile, Level 1
  380. } else if (c->sample_rate <= 48000) {
  381. if (c->channels <= 2) {
  382. profile_level = 0x29; // AAC Profile, Level 2
  383. } else if (c->channels <= 5) {
  384. profile_level = 0x2A; // AAC Profile, Level 4
  385. }
  386. } else if (c->sample_rate <= 96000) {
  387. if (c->channels <= 5) {
  388. profile_level = 0x2B; // AAC Profile, Level 5
  389. }
  390. }
  391. return profile_level;
  392. }
  393. static char *latm_context2config(AVCodecContext *c)
  394. {
  395. /* MP4A-LATM
  396. * The RTP payload format specification is described in RFC 3016
  397. * The encoding specifications are provided in ISO/IEC 14496-3 */
  398. uint8_t config_byte[6];
  399. int rate_index;
  400. char *config;
  401. for (rate_index = 0; rate_index < 16; rate_index++)
  402. if (avpriv_mpeg4audio_sample_rates[rate_index] == c->sample_rate)
  403. break;
  404. if (rate_index == 16) {
  405. av_log(c, AV_LOG_ERROR, "Unsupported sample rate\n");
  406. return NULL;
  407. }
  408. config_byte[0] = 0x40;
  409. config_byte[1] = 0;
  410. config_byte[2] = 0x20 | rate_index;
  411. config_byte[3] = c->channels << 4;
  412. config_byte[4] = 0x3f;
  413. config_byte[5] = 0xc0;
  414. config = av_malloc(6*2+1);
  415. if (!config) {
  416. av_log(c, AV_LOG_ERROR, "Cannot allocate memory for the config info.\n");
  417. return NULL;
  418. }
  419. ff_data_to_hex(config, config_byte, 6, 1);
  420. config[12] = 0;
  421. return config;
  422. }
  423. static char *sdp_write_media_attributes(char *buff, int size, AVCodecContext *c, int payload_type, AVFormatContext *fmt)
  424. {
  425. char *config = NULL;
  426. switch (c->codec_id) {
  427. case AV_CODEC_ID_H264: {
  428. int mode = 1;
  429. if (fmt && fmt->oformat && fmt->oformat->priv_class &&
  430. av_opt_flag_is_set(fmt->priv_data, "rtpflags", "h264_mode0"))
  431. mode = 0;
  432. if (c->extradata_size) {
  433. config = extradata2psets(c);
  434. }
  435. av_strlcatf(buff, size, "a=rtpmap:%d H264/90000\r\n"
  436. "a=fmtp:%d packetization-mode=%d%s\r\n",
  437. payload_type,
  438. payload_type, mode, config ? config : "");
  439. break;
  440. }
  441. case AV_CODEC_ID_H263:
  442. case AV_CODEC_ID_H263P:
  443. /* a=framesize is required by 3GPP TS 26.234 (PSS). It
  444. * actually specifies the maximum video size, but we only know
  445. * the current size. This is required for playback on Android
  446. * stagefright and on Samsung bada. */
  447. if (!fmt || !fmt->oformat->priv_class ||
  448. !av_opt_flag_is_set(fmt->priv_data, "rtpflags", "rfc2190") ||
  449. c->codec_id == AV_CODEC_ID_H263P)
  450. av_strlcatf(buff, size, "a=rtpmap:%d H263-2000/90000\r\n"
  451. "a=framesize:%d %d-%d\r\n",
  452. payload_type,
  453. payload_type, c->width, c->height);
  454. break;
  455. case AV_CODEC_ID_HEVC:
  456. if (c->extradata_size)
  457. config = extradata2psets_hevc(c);
  458. av_strlcatf(buff, size, "a=rtpmap:%d H265/90000\r\n", payload_type);
  459. if (config)
  460. av_strlcatf(buff, size, "a=fmtp:%d %s\r\n",
  461. payload_type, config);
  462. break;
  463. case AV_CODEC_ID_MPEG4:
  464. if (c->extradata_size) {
  465. config = extradata2config(c);
  466. }
  467. av_strlcatf(buff, size, "a=rtpmap:%d MP4V-ES/90000\r\n"
  468. "a=fmtp:%d profile-level-id=1%s\r\n",
  469. payload_type,
  470. payload_type, config ? config : "");
  471. break;
  472. case AV_CODEC_ID_AAC:
  473. if (fmt && fmt->oformat->priv_class &&
  474. av_opt_flag_is_set(fmt->priv_data, "rtpflags", "latm")) {
  475. config = latm_context2config(c);
  476. if (!config)
  477. return NULL;
  478. av_strlcatf(buff, size, "a=rtpmap:%d MP4A-LATM/%d/%d\r\n"
  479. "a=fmtp:%d profile-level-id=%d;cpresent=0;config=%s\r\n",
  480. payload_type, c->sample_rate, c->channels,
  481. payload_type, latm_context2profilelevel(c), config);
  482. } else {
  483. if (c->extradata_size) {
  484. config = extradata2config(c);
  485. } else {
  486. /* FIXME: maybe we can forge config information based on the
  487. * codec parameters...
  488. */
  489. av_log(c, AV_LOG_ERROR, "AAC with no global headers is currently not supported.\n");
  490. return NULL;
  491. }
  492. if (!config) {
  493. return NULL;
  494. }
  495. av_strlcatf(buff, size, "a=rtpmap:%d MPEG4-GENERIC/%d/%d\r\n"
  496. "a=fmtp:%d profile-level-id=1;"
  497. "mode=AAC-hbr;sizelength=13;indexlength=3;"
  498. "indexdeltalength=3%s\r\n",
  499. payload_type, c->sample_rate, c->channels,
  500. payload_type, config);
  501. }
  502. break;
  503. case AV_CODEC_ID_PCM_S16BE:
  504. if (payload_type >= RTP_PT_PRIVATE)
  505. av_strlcatf(buff, size, "a=rtpmap:%d L16/%d/%d\r\n",
  506. payload_type,
  507. c->sample_rate, c->channels);
  508. break;
  509. case AV_CODEC_ID_PCM_MULAW:
  510. if (payload_type >= RTP_PT_PRIVATE)
  511. av_strlcatf(buff, size, "a=rtpmap:%d PCMU/%d/%d\r\n",
  512. payload_type,
  513. c->sample_rate, c->channels);
  514. break;
  515. case AV_CODEC_ID_PCM_ALAW:
  516. if (payload_type >= RTP_PT_PRIVATE)
  517. av_strlcatf(buff, size, "a=rtpmap:%d PCMA/%d/%d\r\n",
  518. payload_type,
  519. c->sample_rate, c->channels);
  520. break;
  521. case AV_CODEC_ID_AMR_NB:
  522. av_strlcatf(buff, size, "a=rtpmap:%d AMR/%d/%d\r\n"
  523. "a=fmtp:%d octet-align=1\r\n",
  524. payload_type, c->sample_rate, c->channels,
  525. payload_type);
  526. break;
  527. case AV_CODEC_ID_AMR_WB:
  528. av_strlcatf(buff, size, "a=rtpmap:%d AMR-WB/%d/%d\r\n"
  529. "a=fmtp:%d octet-align=1\r\n",
  530. payload_type, c->sample_rate, c->channels,
  531. payload_type);
  532. break;
  533. case AV_CODEC_ID_VORBIS:
  534. if (c->extradata_size)
  535. config = xiph_extradata2config(c);
  536. else
  537. av_log(c, AV_LOG_ERROR, "Vorbis configuration info missing\n");
  538. if (!config)
  539. return NULL;
  540. av_strlcatf(buff, size, "a=rtpmap:%d vorbis/%d/%d\r\n"
  541. "a=fmtp:%d configuration=%s\r\n",
  542. payload_type, c->sample_rate, c->channels,
  543. payload_type, config);
  544. break;
  545. case AV_CODEC_ID_THEORA: {
  546. const char *pix_fmt;
  547. switch (c->pix_fmt) {
  548. case AV_PIX_FMT_YUV420P:
  549. pix_fmt = "YCbCr-4:2:0";
  550. break;
  551. case AV_PIX_FMT_YUV422P:
  552. pix_fmt = "YCbCr-4:2:2";
  553. break;
  554. case AV_PIX_FMT_YUV444P:
  555. pix_fmt = "YCbCr-4:4:4";
  556. break;
  557. default:
  558. av_log(c, AV_LOG_ERROR, "Unsupported pixel format.\n");
  559. return NULL;
  560. }
  561. if (c->extradata_size)
  562. config = xiph_extradata2config(c);
  563. else
  564. av_log(c, AV_LOG_ERROR, "Theora configuation info missing\n");
  565. if (!config)
  566. return NULL;
  567. av_strlcatf(buff, size, "a=rtpmap:%d theora/90000\r\n"
  568. "a=fmtp:%d delivery-method=inline; "
  569. "width=%d; height=%d; sampling=%s; "
  570. "configuration=%s\r\n",
  571. payload_type, payload_type,
  572. c->width, c->height, pix_fmt, config);
  573. break;
  574. }
  575. case AV_CODEC_ID_VP8:
  576. av_strlcatf(buff, size, "a=rtpmap:%d VP8/90000\r\n",
  577. payload_type);
  578. break;
  579. case AV_CODEC_ID_MJPEG:
  580. if (payload_type >= RTP_PT_PRIVATE)
  581. av_strlcatf(buff, size, "a=rtpmap:%d JPEG/90000\r\n",
  582. payload_type);
  583. break;
  584. case AV_CODEC_ID_ADPCM_G722:
  585. if (payload_type >= RTP_PT_PRIVATE)
  586. av_strlcatf(buff, size, "a=rtpmap:%d G722/%d/%d\r\n",
  587. payload_type,
  588. 8000, c->channels);
  589. break;
  590. case AV_CODEC_ID_ADPCM_G726: {
  591. if (payload_type >= RTP_PT_PRIVATE)
  592. av_strlcatf(buff, size, "a=rtpmap:%d G726-%d/%d\r\n",
  593. payload_type,
  594. c->bits_per_coded_sample*8,
  595. c->sample_rate);
  596. break;
  597. }
  598. case AV_CODEC_ID_ILBC:
  599. av_strlcatf(buff, size, "a=rtpmap:%d iLBC/%d\r\n"
  600. "a=fmtp:%d mode=%d\r\n",
  601. payload_type, c->sample_rate,
  602. payload_type, c->block_align == 38 ? 20 : 30);
  603. break;
  604. case AV_CODEC_ID_SPEEX:
  605. av_strlcatf(buff, size, "a=rtpmap:%d speex/%d\r\n",
  606. payload_type, c->sample_rate);
  607. break;
  608. case AV_CODEC_ID_OPUS:
  609. /* The opus RTP draft says that all opus streams MUST be declared
  610. as stereo, to avoid negotiation failures. The actual number of
  611. channels can change on a packet-by-packet basis. The number of
  612. channels a receiver prefers to receive or a sender plans to send
  613. can be declared via fmtp parameters (both default to mono), but
  614. receivers MUST be able to receive and process stereo packets. */
  615. av_strlcatf(buff, size, "a=rtpmap:%d opus/48000/2\r\n",
  616. payload_type);
  617. if (c->channels == 2) {
  618. av_strlcatf(buff, size, "a=fmtp:%d sprop-stereo:1\r\n",
  619. payload_type);
  620. }
  621. break;
  622. default:
  623. /* Nothing special to do here... */
  624. break;
  625. }
  626. av_free(config);
  627. return buff;
  628. }
  629. void ff_sdp_write_media(char *buff, int size, AVStream *st, int idx,
  630. const char *dest_addr, const char *dest_type,
  631. int port, int ttl, AVFormatContext *fmt)
  632. {
  633. AVCodecContext *c = st->codec;
  634. const char *type;
  635. int payload_type;
  636. payload_type = ff_rtp_get_payload_type(fmt, c, idx);
  637. switch (c->codec_type) {
  638. case AVMEDIA_TYPE_VIDEO : type = "video" ; break;
  639. case AVMEDIA_TYPE_AUDIO : type = "audio" ; break;
  640. case AVMEDIA_TYPE_SUBTITLE: type = "text" ; break;
  641. default : type = "application"; break;
  642. }
  643. av_strlcatf(buff, size, "m=%s %d RTP/AVP %d\r\n", type, port, payload_type);
  644. sdp_write_address(buff, size, dest_addr, dest_type, ttl);
  645. if (c->bit_rate) {
  646. av_strlcatf(buff, size, "b=AS:%d\r\n", c->bit_rate / 1000);
  647. }
  648. sdp_write_media_attributes(buff, size, c, payload_type, fmt);
  649. }
  650. int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
  651. {
  652. AVDictionaryEntry *title = av_dict_get(ac[0]->metadata, "title", NULL, 0);
  653. struct sdp_session_level s = { 0 };
  654. int i, j, port, ttl, is_multicast, index = 0;
  655. char dst[32], dst_type[5];
  656. memset(buf, 0, size);
  657. s.user = "-";
  658. s.src_addr = "127.0.0.1"; /* FIXME: Properly set this */
  659. s.src_type = "IP4";
  660. s.name = title ? title->value : "No Name";
  661. port = 0;
  662. ttl = 0;
  663. if (n_files == 1) {
  664. port = sdp_get_address(dst, sizeof(dst), &ttl, ac[0]->filename);
  665. is_multicast = resolve_destination(dst, sizeof(dst), dst_type,
  666. sizeof(dst_type));
  667. if (!is_multicast)
  668. ttl = 0;
  669. if (dst[0]) {
  670. s.dst_addr = dst;
  671. s.dst_type = dst_type;
  672. s.ttl = ttl;
  673. if (!strcmp(dst_type, "IP6")) {
  674. s.src_addr = "::1";
  675. s.src_type = "IP6";
  676. }
  677. }
  678. }
  679. sdp_write_header(buf, size, &s);
  680. dst[0] = 0;
  681. for (i = 0; i < n_files; i++) {
  682. if (n_files != 1) {
  683. port = sdp_get_address(dst, sizeof(dst), &ttl, ac[i]->filename);
  684. is_multicast = resolve_destination(dst, sizeof(dst), dst_type,
  685. sizeof(dst_type));
  686. if (!is_multicast)
  687. ttl = 0;
  688. }
  689. for (j = 0; j < ac[i]->nb_streams; j++) {
  690. ff_sdp_write_media(buf, size, ac[i]->streams[j], index++,
  691. dst[0] ? dst : NULL, dst_type,
  692. (port > 0) ? port + j * 2 : 0,
  693. ttl, ac[i]);
  694. if (port <= 0) {
  695. av_strlcatf(buf, size,
  696. "a=control:streamid=%d\r\n", i + j);
  697. }
  698. if (ac[i]->pb && ac[i]->pb->av_class) {
  699. uint8_t *crypto_suite = NULL, *crypto_params = NULL;
  700. av_opt_get(ac[i]->pb, "srtp_out_suite", AV_OPT_SEARCH_CHILDREN,
  701. &crypto_suite);
  702. av_opt_get(ac[i]->pb, "srtp_out_params", AV_OPT_SEARCH_CHILDREN,
  703. &crypto_params);
  704. if (crypto_suite && crypto_suite[0])
  705. av_strlcatf(buf, size,
  706. "a=crypto:1 %s inline:%s\r\n",
  707. crypto_suite, crypto_params);
  708. av_free(crypto_suite);
  709. av_free(crypto_params);
  710. }
  711. }
  712. }
  713. return 0;
  714. }
  715. #else
  716. int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
  717. {
  718. return AVERROR(ENOSYS);
  719. }
  720. void ff_sdp_write_media(char *buff, int size, AVStream *st, int idx,
  721. const char *dest_addr, const char *dest_type,
  722. int port, int ttl, AVFormatContext *fmt)
  723. {
  724. }
  725. #endif