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.

857 lines
30KB

  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 "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(AVFormatContext *s, AVCodecParameters *par)
  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 = par->extradata;
  144. int extradata_size = par->extradata_size;
  145. uint8_t *tmpbuf = NULL;
  146. const uint8_t *sps = NULL, *sps_end;
  147. if (par->extradata_size > MAX_EXTRADATA_SIZE) {
  148. av_log(s, AV_LOG_ERROR, "Too much extradata!\n");
  149. return NULL;
  150. }
  151. if (par->extradata[0] == 1) {
  152. if (ff_avc_write_annexb_extradata(par->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(s, 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(s, AV_LOG_ERROR, "Cannot Base64-encode %"PTRDIFF_SPECIFIER" %"PTRDIFF_SPECIFIER"!\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 && p - psets <= MAX_PSET_SIZE - strlen(profile_string) - 7) {
  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(AVCodecParameters *par)
  203. {
  204. char *psets;
  205. uint8_t *extradata = par->extradata;
  206. int extradata_size = par->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 (par->extradata[0] != 1) {
  217. AVIOContext *pb;
  218. if (avio_open_dyn_buf(&pb) < 0)
  219. return NULL;
  220. if (ff_isom_write_hvcc(pb, par->extradata, par->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 H.264 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(AVFormatContext *s, AVCodecParameters *par)
  294. {
  295. char *config;
  296. if (par->extradata_size > MAX_EXTRADATA_SIZE) {
  297. av_log(s, AV_LOG_ERROR, "Too much extradata!\n");
  298. return NULL;
  299. }
  300. config = av_malloc(10 + par->extradata_size * 2);
  301. if (!config) {
  302. av_log(s, 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, par->extradata, par->extradata_size, 0);
  307. config[9 + par->extradata_size * 2] = 0;
  308. return config;
  309. }
  310. static char *xiph_extradata2config(AVFormatContext *s, AVCodecParameters *par)
  311. {
  312. uint8_t *config;
  313. char *encoded_config;
  314. const uint8_t *header_start[3];
  315. int headers_len, header_len[3], config_len;
  316. int first_header_size;
  317. switch (par->codec_id) {
  318. case AV_CODEC_ID_THEORA:
  319. first_header_size = 42;
  320. break;
  321. case AV_CODEC_ID_VORBIS:
  322. first_header_size = 30;
  323. break;
  324. default:
  325. av_log(s, AV_LOG_ERROR, "Unsupported Xiph codec ID\n");
  326. return NULL;
  327. }
  328. if (avpriv_split_xiph_headers(par->extradata, par->extradata_size,
  329. first_header_size, header_start,
  330. header_len) < 0) {
  331. av_log(s, AV_LOG_ERROR, "Extradata corrupt.\n");
  332. return NULL;
  333. }
  334. headers_len = header_len[0] + header_len[2];
  335. config_len = 4 + // count
  336. 3 + // ident
  337. 2 + // packet size
  338. 1 + // header count
  339. 2 + // header size
  340. headers_len; // and the rest
  341. config = av_malloc(config_len);
  342. if (!config)
  343. goto xiph_fail;
  344. encoded_config = av_malloc(AV_BASE64_SIZE(config_len));
  345. if (!encoded_config) {
  346. av_free(config);
  347. goto xiph_fail;
  348. }
  349. config[0] = config[1] = config[2] = 0;
  350. config[3] = 1;
  351. config[4] = (RTP_XIPH_IDENT >> 16) & 0xff;
  352. config[5] = (RTP_XIPH_IDENT >> 8) & 0xff;
  353. config[6] = (RTP_XIPH_IDENT ) & 0xff;
  354. config[7] = (headers_len >> 8) & 0xff;
  355. config[8] = headers_len & 0xff;
  356. config[9] = 2;
  357. config[10] = header_len[0];
  358. config[11] = 0; // size of comment header; nonexistent
  359. memcpy(config + 12, header_start[0], header_len[0]);
  360. memcpy(config + 12 + header_len[0], header_start[2], header_len[2]);
  361. av_base64_encode(encoded_config, AV_BASE64_SIZE(config_len),
  362. config, config_len);
  363. av_free(config);
  364. return encoded_config;
  365. xiph_fail:
  366. av_log(s, AV_LOG_ERROR,
  367. "Not enough memory for configuration string\n");
  368. return NULL;
  369. }
  370. static int latm_context2profilelevel(AVCodecParameters *par)
  371. {
  372. /* MP4A-LATM
  373. * The RTP payload format specification is described in RFC 3016
  374. * The encoding specifications are provided in ISO/IEC 14496-3 */
  375. int profile_level = 0x2B;
  376. /* TODO: AAC Profile only supports AAC LC Object Type.
  377. * Different Object Types should implement different Profile Levels */
  378. if (par->sample_rate <= 24000) {
  379. if (par->channels <= 2)
  380. profile_level = 0x28; // AAC Profile, Level 1
  381. } else if (par->sample_rate <= 48000) {
  382. if (par->channels <= 2) {
  383. profile_level = 0x29; // AAC Profile, Level 2
  384. } else if (par->channels <= 5) {
  385. profile_level = 0x2A; // AAC Profile, Level 4
  386. }
  387. } else if (par->sample_rate <= 96000) {
  388. if (par->channels <= 5) {
  389. profile_level = 0x2B; // AAC Profile, Level 5
  390. }
  391. }
  392. return profile_level;
  393. }
  394. static char *latm_context2config(AVFormatContext *s, AVCodecParameters *par)
  395. {
  396. /* MP4A-LATM
  397. * The RTP payload format specification is described in RFC 3016
  398. * The encoding specifications are provided in ISO/IEC 14496-3 */
  399. uint8_t config_byte[6];
  400. int rate_index;
  401. char *config;
  402. for (rate_index = 0; rate_index < 16; rate_index++)
  403. if (avpriv_mpeg4audio_sample_rates[rate_index] == par->sample_rate)
  404. break;
  405. if (rate_index == 16) {
  406. av_log(s, AV_LOG_ERROR, "Unsupported sample rate\n");
  407. return NULL;
  408. }
  409. config_byte[0] = 0x40;
  410. config_byte[1] = 0;
  411. config_byte[2] = 0x20 | rate_index;
  412. config_byte[3] = par->channels << 4;
  413. config_byte[4] = 0x3f;
  414. config_byte[5] = 0xc0;
  415. config = av_malloc(6*2+1);
  416. if (!config) {
  417. av_log(s, AV_LOG_ERROR, "Cannot allocate memory for the config info.\n");
  418. return NULL;
  419. }
  420. ff_data_to_hex(config, config_byte, 6, 1);
  421. config[12] = 0;
  422. return config;
  423. }
  424. static char *sdp_write_media_attributes(char *buff, int size, AVStream *st, int payload_type, AVFormatContext *fmt)
  425. {
  426. char *config = NULL;
  427. AVCodecParameters *p = st->codecpar;
  428. switch (p->codec_id) {
  429. case AV_CODEC_ID_DIRAC:
  430. av_strlcatf(buff, size, "a=rtpmap:%d VC2/90000\r\n", payload_type);
  431. break;
  432. case AV_CODEC_ID_H264: {
  433. int mode = 1;
  434. if (fmt && fmt->oformat && fmt->oformat->priv_class &&
  435. av_opt_flag_is_set(fmt->priv_data, "rtpflags", "h264_mode0"))
  436. mode = 0;
  437. if (p->extradata_size) {
  438. config = extradata2psets(fmt, p);
  439. }
  440. av_strlcatf(buff, size, "a=rtpmap:%d H264/90000\r\n"
  441. "a=fmtp:%d packetization-mode=%d%s\r\n",
  442. payload_type,
  443. payload_type, mode, config ? config : "");
  444. break;
  445. }
  446. case AV_CODEC_ID_H261:
  447. {
  448. const char *pic_fmt = NULL;
  449. /* only QCIF and CIF are specified as supported in RFC 4587 */
  450. if (p->width == 176 && p->height == 144)
  451. pic_fmt = "QCIF=1";
  452. else if (p->width == 352 && p->height == 288)
  453. pic_fmt = "CIF=1";
  454. if (payload_type >= RTP_PT_PRIVATE)
  455. av_strlcatf(buff, size, "a=rtpmap:%d H261/90000\r\n", payload_type);
  456. if (pic_fmt)
  457. av_strlcatf(buff, size, "a=fmtp:%d %s\r\n", payload_type, pic_fmt);
  458. break;
  459. }
  460. case AV_CODEC_ID_H263:
  461. case AV_CODEC_ID_H263P:
  462. /* a=framesize is required by 3GPP TS 26.234 (PSS). It
  463. * actually specifies the maximum video size, but we only know
  464. * the current size. This is required for playback on Android
  465. * stagefright and on Samsung bada. */
  466. if (!fmt || !fmt->oformat->priv_class ||
  467. !av_opt_flag_is_set(fmt->priv_data, "rtpflags", "rfc2190") ||
  468. p->codec_id == AV_CODEC_ID_H263P)
  469. av_strlcatf(buff, size, "a=rtpmap:%d H263-2000/90000\r\n"
  470. "a=framesize:%d %d-%d\r\n",
  471. payload_type,
  472. payload_type, p->width, p->height);
  473. break;
  474. case AV_CODEC_ID_HEVC:
  475. if (p->extradata_size)
  476. config = extradata2psets_hevc(p);
  477. av_strlcatf(buff, size, "a=rtpmap:%d H265/90000\r\n", payload_type);
  478. if (config)
  479. av_strlcatf(buff, size, "a=fmtp:%d %s\r\n",
  480. payload_type, config);
  481. break;
  482. case AV_CODEC_ID_MPEG4:
  483. if (p->extradata_size) {
  484. config = extradata2config(fmt, p);
  485. }
  486. av_strlcatf(buff, size, "a=rtpmap:%d MP4V-ES/90000\r\n"
  487. "a=fmtp:%d profile-level-id=1%s\r\n",
  488. payload_type,
  489. payload_type, config ? config : "");
  490. break;
  491. case AV_CODEC_ID_AAC:
  492. if (fmt && fmt->oformat && fmt->oformat->priv_class &&
  493. av_opt_flag_is_set(fmt->priv_data, "rtpflags", "latm")) {
  494. config = latm_context2config(fmt, p);
  495. if (!config)
  496. return NULL;
  497. av_strlcatf(buff, size, "a=rtpmap:%d MP4A-LATM/%d/%d\r\n"
  498. "a=fmtp:%d profile-level-id=%d;cpresent=0;config=%s\r\n",
  499. payload_type, p->sample_rate, p->channels,
  500. payload_type, latm_context2profilelevel(p), config);
  501. } else {
  502. if (p->extradata_size) {
  503. config = extradata2config(fmt, p);
  504. } else {
  505. /* FIXME: maybe we can forge config information based on the
  506. * codec parameters...
  507. */
  508. av_log(fmt, AV_LOG_ERROR, "AAC with no global headers is currently not supported.\n");
  509. return NULL;
  510. }
  511. if (!config) {
  512. return NULL;
  513. }
  514. av_strlcatf(buff, size, "a=rtpmap:%d MPEG4-GENERIC/%d/%d\r\n"
  515. "a=fmtp:%d profile-level-id=1;"
  516. "mode=AAC-hbr;sizelength=13;indexlength=3;"
  517. "indexdeltalength=3%s\r\n",
  518. payload_type, p->sample_rate, p->channels,
  519. payload_type, config);
  520. }
  521. break;
  522. case AV_CODEC_ID_PCM_S16BE:
  523. if (payload_type >= RTP_PT_PRIVATE)
  524. av_strlcatf(buff, size, "a=rtpmap:%d L16/%d/%d\r\n",
  525. payload_type,
  526. p->sample_rate, p->channels);
  527. break;
  528. case AV_CODEC_ID_PCM_S24BE:
  529. if (payload_type >= RTP_PT_PRIVATE)
  530. av_strlcatf(buff, size, "a=rtpmap:%d L24/%d/%d\r\n",
  531. payload_type,
  532. p->sample_rate, p->channels);
  533. break;
  534. case AV_CODEC_ID_PCM_MULAW:
  535. if (payload_type >= RTP_PT_PRIVATE)
  536. av_strlcatf(buff, size, "a=rtpmap:%d PCMU/%d/%d\r\n",
  537. payload_type,
  538. p->sample_rate, p->channels);
  539. break;
  540. case AV_CODEC_ID_PCM_ALAW:
  541. if (payload_type >= RTP_PT_PRIVATE)
  542. av_strlcatf(buff, size, "a=rtpmap:%d PCMA/%d/%d\r\n",
  543. payload_type,
  544. p->sample_rate, p->channels);
  545. break;
  546. case AV_CODEC_ID_AMR_NB:
  547. av_strlcatf(buff, size, "a=rtpmap:%d AMR/%d/%d\r\n"
  548. "a=fmtp:%d octet-align=1\r\n",
  549. payload_type, p->sample_rate, p->channels,
  550. payload_type);
  551. break;
  552. case AV_CODEC_ID_AMR_WB:
  553. av_strlcatf(buff, size, "a=rtpmap:%d AMR-WB/%d/%d\r\n"
  554. "a=fmtp:%d octet-align=1\r\n",
  555. payload_type, p->sample_rate, p->channels,
  556. payload_type);
  557. break;
  558. case AV_CODEC_ID_VORBIS:
  559. if (p->extradata_size)
  560. config = xiph_extradata2config(fmt, p);
  561. else
  562. av_log(fmt, AV_LOG_ERROR, "Vorbis configuration info missing\n");
  563. if (!config)
  564. return NULL;
  565. av_strlcatf(buff, size, "a=rtpmap:%d vorbis/%d/%d\r\n"
  566. "a=fmtp:%d configuration=%s\r\n",
  567. payload_type, p->sample_rate, p->channels,
  568. payload_type, config);
  569. break;
  570. case AV_CODEC_ID_THEORA: {
  571. const char *pix_fmt;
  572. switch (p->format) {
  573. case AV_PIX_FMT_YUV420P:
  574. pix_fmt = "YCbCr-4:2:0";
  575. break;
  576. case AV_PIX_FMT_YUV422P:
  577. pix_fmt = "YCbCr-4:2:2";
  578. break;
  579. case AV_PIX_FMT_YUV444P:
  580. pix_fmt = "YCbCr-4:4:4";
  581. break;
  582. default:
  583. av_log(fmt, AV_LOG_ERROR, "Unsupported pixel format.\n");
  584. return NULL;
  585. }
  586. if (p->extradata_size)
  587. config = xiph_extradata2config(fmt, p);
  588. else
  589. av_log(fmt, AV_LOG_ERROR, "Theora configuration info missing\n");
  590. if (!config)
  591. return NULL;
  592. av_strlcatf(buff, size, "a=rtpmap:%d theora/90000\r\n"
  593. "a=fmtp:%d delivery-method=inline; "
  594. "width=%d; height=%d; sampling=%s; "
  595. "configuration=%s\r\n",
  596. payload_type, payload_type,
  597. p->width, p->height, pix_fmt, config);
  598. break;
  599. }
  600. case AV_CODEC_ID_VP8:
  601. av_strlcatf(buff, size, "a=rtpmap:%d VP8/90000\r\n",
  602. payload_type);
  603. break;
  604. case AV_CODEC_ID_VP9:
  605. av_strlcatf(buff, size, "a=rtpmap:%d VP9/90000\r\n",
  606. payload_type);
  607. break;
  608. case AV_CODEC_ID_MJPEG:
  609. if (payload_type >= RTP_PT_PRIVATE)
  610. av_strlcatf(buff, size, "a=rtpmap:%d JPEG/90000\r\n",
  611. payload_type);
  612. break;
  613. case AV_CODEC_ID_ADPCM_G722:
  614. if (payload_type >= RTP_PT_PRIVATE)
  615. av_strlcatf(buff, size, "a=rtpmap:%d G722/%d/%d\r\n",
  616. payload_type,
  617. 8000, p->channels);
  618. break;
  619. case AV_CODEC_ID_ADPCM_G726: {
  620. if (payload_type >= RTP_PT_PRIVATE)
  621. av_strlcatf(buff, size, "a=rtpmap:%d AAL2-G726-%d/%d\r\n",
  622. payload_type,
  623. p->bits_per_coded_sample*8,
  624. p->sample_rate);
  625. break;
  626. }
  627. case AV_CODEC_ID_ADPCM_G726LE: {
  628. if (payload_type >= RTP_PT_PRIVATE)
  629. av_strlcatf(buff, size, "a=rtpmap:%d G726-%d/%d\r\n",
  630. payload_type,
  631. p->bits_per_coded_sample*8,
  632. p->sample_rate);
  633. break;
  634. }
  635. case AV_CODEC_ID_ILBC:
  636. av_strlcatf(buff, size, "a=rtpmap:%d iLBC/%d\r\n"
  637. "a=fmtp:%d mode=%d\r\n",
  638. payload_type, p->sample_rate,
  639. payload_type, p->block_align == 38 ? 20 : 30);
  640. break;
  641. case AV_CODEC_ID_SPEEX:
  642. av_strlcatf(buff, size, "a=rtpmap:%d speex/%d\r\n",
  643. payload_type, p->sample_rate);
  644. #if FF_API_LAVF_AVCTX
  645. FF_DISABLE_DEPRECATION_WARNINGS
  646. if (st->codec) {
  647. const char *mode;
  648. uint64_t vad_option;
  649. if (st->codec->flags & AV_CODEC_FLAG_QSCALE)
  650. mode = "on";
  651. else if (!av_opt_get_int(st->codec, "vad", AV_OPT_FLAG_ENCODING_PARAM, &vad_option) && vad_option)
  652. mode = "vad";
  653. else
  654. mode = "off";
  655. av_strlcatf(buff, size, "a=fmtp:%d vbr=%s\r\n",
  656. payload_type, mode);
  657. }
  658. FF_ENABLE_DEPRECATION_WARNINGS
  659. #endif
  660. break;
  661. case AV_CODEC_ID_OPUS:
  662. /* The opus RTP draft says that all opus streams MUST be declared
  663. as stereo, to avoid negotiation failures. The actual number of
  664. channels can change on a packet-by-packet basis. The number of
  665. channels a receiver prefers to receive or a sender plans to send
  666. can be declared via fmtp parameters (both default to mono), but
  667. receivers MUST be able to receive and process stereo packets. */
  668. av_strlcatf(buff, size, "a=rtpmap:%d opus/48000/2\r\n",
  669. payload_type);
  670. if (p->channels == 2) {
  671. av_strlcatf(buff, size, "a=fmtp:%d sprop-stereo=1\r\n",
  672. payload_type);
  673. }
  674. break;
  675. default:
  676. /* Nothing special to do here... */
  677. break;
  678. }
  679. av_free(config);
  680. return buff;
  681. }
  682. void ff_sdp_write_media(char *buff, int size, AVStream *st, int idx,
  683. const char *dest_addr, const char *dest_type,
  684. int port, int ttl, AVFormatContext *fmt)
  685. {
  686. AVCodecParameters *p = st->codecpar;
  687. const char *type;
  688. int payload_type;
  689. payload_type = ff_rtp_get_payload_type(fmt, st->codecpar, idx);
  690. switch (p->codec_type) {
  691. case AVMEDIA_TYPE_VIDEO : type = "video" ; break;
  692. case AVMEDIA_TYPE_AUDIO : type = "audio" ; break;
  693. case AVMEDIA_TYPE_SUBTITLE: type = "text" ; break;
  694. default : type = "application"; break;
  695. }
  696. av_strlcatf(buff, size, "m=%s %d RTP/AVP %d\r\n", type, port, payload_type);
  697. sdp_write_address(buff, size, dest_addr, dest_type, ttl);
  698. if (p->bit_rate) {
  699. av_strlcatf(buff, size, "b=AS:%"PRId64"\r\n", p->bit_rate / 1000);
  700. }
  701. sdp_write_media_attributes(buff, size, st, payload_type, fmt);
  702. }
  703. int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
  704. {
  705. AVDictionaryEntry *title = av_dict_get(ac[0]->metadata, "title", NULL, 0);
  706. struct sdp_session_level s = { 0 };
  707. int i, j, port, ttl, is_multicast, index = 0;
  708. char dst[32], dst_type[5];
  709. memset(buf, 0, size);
  710. s.user = "-";
  711. s.src_addr = "127.0.0.1"; /* FIXME: Properly set this */
  712. s.src_type = "IP4";
  713. s.name = title ? title->value : "No Name";
  714. port = 0;
  715. ttl = 0;
  716. if (n_files == 1) {
  717. port = sdp_get_address(dst, sizeof(dst), &ttl, ac[0]->url ? ac[0]->url : "");
  718. is_multicast = resolve_destination(dst, sizeof(dst), dst_type,
  719. sizeof(dst_type));
  720. if (!is_multicast)
  721. ttl = 0;
  722. if (dst[0]) {
  723. s.dst_addr = dst;
  724. s.dst_type = dst_type;
  725. s.ttl = ttl;
  726. if (!strcmp(dst_type, "IP6")) {
  727. s.src_addr = "::1";
  728. s.src_type = "IP6";
  729. }
  730. }
  731. }
  732. sdp_write_header(buf, size, &s);
  733. dst[0] = 0;
  734. for (i = 0; i < n_files; i++) {
  735. if (n_files != 1) {
  736. port = sdp_get_address(dst, sizeof(dst), &ttl, ac[i]->url ? ac[i]->url : "");
  737. is_multicast = resolve_destination(dst, sizeof(dst), dst_type,
  738. sizeof(dst_type));
  739. if (!is_multicast)
  740. ttl = 0;
  741. }
  742. for (j = 0; j < ac[i]->nb_streams; j++) {
  743. ff_sdp_write_media(buf, size, ac[i]->streams[j], index++,
  744. dst[0] ? dst : NULL, dst_type,
  745. (port > 0) ? port + j * 2 : 0,
  746. ttl, ac[i]);
  747. if (port <= 0) {
  748. av_strlcatf(buf, size,
  749. "a=control:streamid=%d\r\n", i + j);
  750. }
  751. if (ac[i]->pb && ac[i]->pb->av_class) {
  752. uint8_t *crypto_suite = NULL, *crypto_params = NULL;
  753. av_opt_get(ac[i]->pb, "srtp_out_suite", AV_OPT_SEARCH_CHILDREN,
  754. &crypto_suite);
  755. av_opt_get(ac[i]->pb, "srtp_out_params", AV_OPT_SEARCH_CHILDREN,
  756. &crypto_params);
  757. if (crypto_suite && crypto_suite[0])
  758. av_strlcatf(buf, size,
  759. "a=crypto:1 %s inline:%s\r\n",
  760. crypto_suite, crypto_params);
  761. av_free(crypto_suite);
  762. av_free(crypto_params);
  763. }
  764. }
  765. }
  766. return 0;
  767. }
  768. #else
  769. int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
  770. {
  771. return AVERROR(ENOSYS);
  772. }
  773. void ff_sdp_write_media(char *buff, int size, AVStream *st, int idx,
  774. const char *dest_addr, const char *dest_type,
  775. int port, int ttl, AVFormatContext *fmt)
  776. {
  777. }
  778. #endif