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.

838 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) {
  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. char *config, *encoded_config;
  313. const uint8_t *header_start[3];
  314. int headers_len, header_len[3], config_len;
  315. int first_header_size;
  316. switch (par->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(s, AV_LOG_ERROR, "Unsupported Xiph codec ID\n");
  325. return NULL;
  326. }
  327. if (avpriv_split_xiph_headers(par->extradata, par->extradata_size,
  328. first_header_size, header_start,
  329. header_len) < 0) {
  330. av_log(s, 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(s, AV_LOG_ERROR,
  366. "Not enough memory for configuration string\n");
  367. return NULL;
  368. }
  369. static int latm_context2profilelevel(AVCodecParameters *par)
  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 (par->sample_rate <= 24000) {
  378. if (par->channels <= 2)
  379. profile_level = 0x28; // AAC Profile, Level 1
  380. } else if (par->sample_rate <= 48000) {
  381. if (par->channels <= 2) {
  382. profile_level = 0x29; // AAC Profile, Level 2
  383. } else if (par->channels <= 5) {
  384. profile_level = 0x2A; // AAC Profile, Level 4
  385. }
  386. } else if (par->sample_rate <= 96000) {
  387. if (par->channels <= 5) {
  388. profile_level = 0x2B; // AAC Profile, Level 5
  389. }
  390. }
  391. return profile_level;
  392. }
  393. static char *latm_context2config(AVFormatContext *s, AVCodecParameters *par)
  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] == par->sample_rate)
  403. break;
  404. if (rate_index == 16) {
  405. av_log(s, 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] = par->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(s, 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, AVStream *st, int payload_type, AVFormatContext *fmt)
  424. {
  425. char *config = NULL;
  426. AVCodecParameters *p = st->codecpar;
  427. switch (p->codec_id) {
  428. case AV_CODEC_ID_DIRAC:
  429. av_strlcatf(buff, size, "a=rtpmap:%d VC2/90000\r\n", payload_type);
  430. break;
  431. case AV_CODEC_ID_H264: {
  432. int mode = 1;
  433. if (fmt && fmt->oformat && fmt->oformat->priv_class &&
  434. av_opt_flag_is_set(fmt->priv_data, "rtpflags", "h264_mode0"))
  435. mode = 0;
  436. if (p->extradata_size) {
  437. config = extradata2psets(fmt, p);
  438. }
  439. av_strlcatf(buff, size, "a=rtpmap:%d H264/90000\r\n"
  440. "a=fmtp:%d packetization-mode=%d%s\r\n",
  441. payload_type,
  442. payload_type, mode, config ? config : "");
  443. break;
  444. }
  445. case AV_CODEC_ID_H261:
  446. {
  447. const char *pic_fmt = NULL;
  448. /* only QCIF and CIF are specified as supported in RFC 4587 */
  449. if (p->width == 176 && p->height == 144)
  450. pic_fmt = "QCIF=1";
  451. else if (p->width == 352 && p->height == 288)
  452. pic_fmt = "CIF=1";
  453. if (payload_type >= RTP_PT_PRIVATE)
  454. av_strlcatf(buff, size, "a=rtpmap:%d H261/90000\r\n", payload_type);
  455. if (pic_fmt)
  456. av_strlcatf(buff, size, "a=fmtp:%d %s\r\n", payload_type, pic_fmt);
  457. break;
  458. }
  459. case AV_CODEC_ID_H263:
  460. case AV_CODEC_ID_H263P:
  461. /* a=framesize is required by 3GPP TS 26.234 (PSS). It
  462. * actually specifies the maximum video size, but we only know
  463. * the current size. This is required for playback on Android
  464. * stagefright and on Samsung bada. */
  465. if (!fmt || !fmt->oformat->priv_class ||
  466. !av_opt_flag_is_set(fmt->priv_data, "rtpflags", "rfc2190") ||
  467. p->codec_id == AV_CODEC_ID_H263P)
  468. av_strlcatf(buff, size, "a=rtpmap:%d H263-2000/90000\r\n"
  469. "a=framesize:%d %d-%d\r\n",
  470. payload_type,
  471. payload_type, p->width, p->height);
  472. break;
  473. case AV_CODEC_ID_HEVC:
  474. if (p->extradata_size)
  475. config = extradata2psets_hevc(p);
  476. av_strlcatf(buff, size, "a=rtpmap:%d H265/90000\r\n", payload_type);
  477. if (config)
  478. av_strlcatf(buff, size, "a=fmtp:%d %s\r\n",
  479. payload_type, config);
  480. break;
  481. case AV_CODEC_ID_MPEG4:
  482. if (p->extradata_size) {
  483. config = extradata2config(fmt, p);
  484. }
  485. av_strlcatf(buff, size, "a=rtpmap:%d MP4V-ES/90000\r\n"
  486. "a=fmtp:%d profile-level-id=1%s\r\n",
  487. payload_type,
  488. payload_type, config ? config : "");
  489. break;
  490. case AV_CODEC_ID_AAC:
  491. if (fmt && fmt->oformat && fmt->oformat->priv_class &&
  492. av_opt_flag_is_set(fmt->priv_data, "rtpflags", "latm")) {
  493. config = latm_context2config(fmt, p);
  494. if (!config)
  495. return NULL;
  496. av_strlcatf(buff, size, "a=rtpmap:%d MP4A-LATM/%d/%d\r\n"
  497. "a=fmtp:%d profile-level-id=%d;cpresent=0;config=%s\r\n",
  498. payload_type, p->sample_rate, p->channels,
  499. payload_type, latm_context2profilelevel(p), config);
  500. } else {
  501. if (p->extradata_size) {
  502. config = extradata2config(fmt, p);
  503. } else {
  504. /* FIXME: maybe we can forge config information based on the
  505. * codec parameters...
  506. */
  507. av_log(fmt, AV_LOG_ERROR, "AAC with no global headers is currently not supported.\n");
  508. return NULL;
  509. }
  510. if (!config) {
  511. return NULL;
  512. }
  513. av_strlcatf(buff, size, "a=rtpmap:%d MPEG4-GENERIC/%d/%d\r\n"
  514. "a=fmtp:%d profile-level-id=1;"
  515. "mode=AAC-hbr;sizelength=13;indexlength=3;"
  516. "indexdeltalength=3%s\r\n",
  517. payload_type, p->sample_rate, p->channels,
  518. payload_type, config);
  519. }
  520. break;
  521. case AV_CODEC_ID_PCM_S16BE:
  522. if (payload_type >= RTP_PT_PRIVATE)
  523. av_strlcatf(buff, size, "a=rtpmap:%d L16/%d/%d\r\n",
  524. payload_type,
  525. p->sample_rate, p->channels);
  526. break;
  527. case AV_CODEC_ID_PCM_MULAW:
  528. if (payload_type >= RTP_PT_PRIVATE)
  529. av_strlcatf(buff, size, "a=rtpmap:%d PCMU/%d/%d\r\n",
  530. payload_type,
  531. p->sample_rate, p->channels);
  532. break;
  533. case AV_CODEC_ID_PCM_ALAW:
  534. if (payload_type >= RTP_PT_PRIVATE)
  535. av_strlcatf(buff, size, "a=rtpmap:%d PCMA/%d/%d\r\n",
  536. payload_type,
  537. p->sample_rate, p->channels);
  538. break;
  539. case AV_CODEC_ID_AMR_NB:
  540. av_strlcatf(buff, size, "a=rtpmap:%d AMR/%d/%d\r\n"
  541. "a=fmtp:%d octet-align=1\r\n",
  542. payload_type, p->sample_rate, p->channels,
  543. payload_type);
  544. break;
  545. case AV_CODEC_ID_AMR_WB:
  546. av_strlcatf(buff, size, "a=rtpmap:%d AMR-WB/%d/%d\r\n"
  547. "a=fmtp:%d octet-align=1\r\n",
  548. payload_type, p->sample_rate, p->channels,
  549. payload_type);
  550. break;
  551. case AV_CODEC_ID_VORBIS:
  552. if (p->extradata_size)
  553. config = xiph_extradata2config(fmt, p);
  554. else
  555. av_log(fmt, AV_LOG_ERROR, "Vorbis configuration info missing\n");
  556. if (!config)
  557. return NULL;
  558. av_strlcatf(buff, size, "a=rtpmap:%d vorbis/%d/%d\r\n"
  559. "a=fmtp:%d configuration=%s\r\n",
  560. payload_type, p->sample_rate, p->channels,
  561. payload_type, config);
  562. break;
  563. case AV_CODEC_ID_THEORA: {
  564. const char *pix_fmt;
  565. switch (p->format) {
  566. case AV_PIX_FMT_YUV420P:
  567. pix_fmt = "YCbCr-4:2:0";
  568. break;
  569. case AV_PIX_FMT_YUV422P:
  570. pix_fmt = "YCbCr-4:2:2";
  571. break;
  572. case AV_PIX_FMT_YUV444P:
  573. pix_fmt = "YCbCr-4:4:4";
  574. break;
  575. default:
  576. av_log(fmt, AV_LOG_ERROR, "Unsupported pixel format.\n");
  577. return NULL;
  578. }
  579. if (p->extradata_size)
  580. config = xiph_extradata2config(fmt, p);
  581. else
  582. av_log(fmt, AV_LOG_ERROR, "Theora configuration info missing\n");
  583. if (!config)
  584. return NULL;
  585. av_strlcatf(buff, size, "a=rtpmap:%d theora/90000\r\n"
  586. "a=fmtp:%d delivery-method=inline; "
  587. "width=%d; height=%d; sampling=%s; "
  588. "configuration=%s\r\n",
  589. payload_type, payload_type,
  590. p->width, p->height, pix_fmt, config);
  591. break;
  592. }
  593. case AV_CODEC_ID_VP8:
  594. av_strlcatf(buff, size, "a=rtpmap:%d VP8/90000\r\n",
  595. payload_type);
  596. break;
  597. case AV_CODEC_ID_VP9:
  598. av_strlcatf(buff, size, "a=rtpmap:%d VP9/90000\r\n",
  599. payload_type);
  600. break;
  601. case AV_CODEC_ID_MJPEG:
  602. if (payload_type >= RTP_PT_PRIVATE)
  603. av_strlcatf(buff, size, "a=rtpmap:%d JPEG/90000\r\n",
  604. payload_type);
  605. break;
  606. case AV_CODEC_ID_ADPCM_G722:
  607. if (payload_type >= RTP_PT_PRIVATE)
  608. av_strlcatf(buff, size, "a=rtpmap:%d G722/%d/%d\r\n",
  609. payload_type,
  610. 8000, p->channels);
  611. break;
  612. case AV_CODEC_ID_ADPCM_G726: {
  613. if (payload_type >= RTP_PT_PRIVATE)
  614. av_strlcatf(buff, size, "a=rtpmap:%d G726-%d/%d\r\n",
  615. payload_type,
  616. p->bits_per_coded_sample*8,
  617. p->sample_rate);
  618. break;
  619. }
  620. case AV_CODEC_ID_ILBC:
  621. av_strlcatf(buff, size, "a=rtpmap:%d iLBC/%d\r\n"
  622. "a=fmtp:%d mode=%d\r\n",
  623. payload_type, p->sample_rate,
  624. payload_type, p->block_align == 38 ? 20 : 30);
  625. break;
  626. case AV_CODEC_ID_SPEEX:
  627. av_strlcatf(buff, size, "a=rtpmap:%d speex/%d\r\n",
  628. payload_type, p->sample_rate);
  629. if (st->codec) {
  630. const char *mode;
  631. uint64_t vad_option;
  632. if (st->codec->flags & AV_CODEC_FLAG_QSCALE)
  633. mode = "on";
  634. else if (!av_opt_get_int(st->codec, "vad", AV_OPT_FLAG_ENCODING_PARAM, &vad_option) && vad_option)
  635. mode = "vad";
  636. else
  637. mode = "off";
  638. av_strlcatf(buff, size, "a=fmtp:%d vbr=%s\r\n",
  639. payload_type, mode);
  640. }
  641. break;
  642. case AV_CODEC_ID_OPUS:
  643. /* The opus RTP draft says that all opus streams MUST be declared
  644. as stereo, to avoid negotiation failures. The actual number of
  645. channels can change on a packet-by-packet basis. The number of
  646. channels a receiver prefers to receive or a sender plans to send
  647. can be declared via fmtp parameters (both default to mono), but
  648. receivers MUST be able to receive and process stereo packets. */
  649. av_strlcatf(buff, size, "a=rtpmap:%d opus/48000/2\r\n",
  650. payload_type);
  651. if (p->channels == 2) {
  652. av_strlcatf(buff, size, "a=fmtp:%d sprop-stereo=1\r\n",
  653. payload_type);
  654. }
  655. break;
  656. default:
  657. /* Nothing special to do here... */
  658. break;
  659. }
  660. av_free(config);
  661. return buff;
  662. }
  663. void ff_sdp_write_media(char *buff, int size, AVStream *st, int idx,
  664. const char *dest_addr, const char *dest_type,
  665. int port, int ttl, AVFormatContext *fmt)
  666. {
  667. AVCodecParameters *p = st->codecpar;
  668. const char *type;
  669. int payload_type;
  670. payload_type = ff_rtp_get_payload_type(fmt, st->codecpar, idx);
  671. switch (p->codec_type) {
  672. case AVMEDIA_TYPE_VIDEO : type = "video" ; break;
  673. case AVMEDIA_TYPE_AUDIO : type = "audio" ; break;
  674. case AVMEDIA_TYPE_SUBTITLE: type = "text" ; break;
  675. default : type = "application"; break;
  676. }
  677. av_strlcatf(buff, size, "m=%s %d RTP/AVP %d\r\n", type, port, payload_type);
  678. sdp_write_address(buff, size, dest_addr, dest_type, ttl);
  679. if (p->bit_rate) {
  680. av_strlcatf(buff, size, "b=AS:%"PRId64"\r\n", p->bit_rate / 1000);
  681. }
  682. sdp_write_media_attributes(buff, size, st, payload_type, fmt);
  683. }
  684. int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
  685. {
  686. AVDictionaryEntry *title = av_dict_get(ac[0]->metadata, "title", NULL, 0);
  687. struct sdp_session_level s = { 0 };
  688. int i, j, port, ttl, is_multicast, index = 0;
  689. char dst[32], dst_type[5];
  690. memset(buf, 0, size);
  691. s.user = "-";
  692. s.src_addr = "127.0.0.1"; /* FIXME: Properly set this */
  693. s.src_type = "IP4";
  694. s.name = title ? title->value : "No Name";
  695. port = 0;
  696. ttl = 0;
  697. if (n_files == 1) {
  698. port = sdp_get_address(dst, sizeof(dst), &ttl, ac[0]->filename);
  699. is_multicast = resolve_destination(dst, sizeof(dst), dst_type,
  700. sizeof(dst_type));
  701. if (!is_multicast)
  702. ttl = 0;
  703. if (dst[0]) {
  704. s.dst_addr = dst;
  705. s.dst_type = dst_type;
  706. s.ttl = ttl;
  707. if (!strcmp(dst_type, "IP6")) {
  708. s.src_addr = "::1";
  709. s.src_type = "IP6";
  710. }
  711. }
  712. }
  713. sdp_write_header(buf, size, &s);
  714. dst[0] = 0;
  715. for (i = 0; i < n_files; i++) {
  716. if (n_files != 1) {
  717. port = sdp_get_address(dst, sizeof(dst), &ttl, ac[i]->filename);
  718. is_multicast = resolve_destination(dst, sizeof(dst), dst_type,
  719. sizeof(dst_type));
  720. if (!is_multicast)
  721. ttl = 0;
  722. }
  723. for (j = 0; j < ac[i]->nb_streams; j++) {
  724. ff_sdp_write_media(buf, size, ac[i]->streams[j], index++,
  725. dst[0] ? dst : NULL, dst_type,
  726. (port > 0) ? port + j * 2 : 0,
  727. ttl, ac[i]);
  728. if (port <= 0) {
  729. av_strlcatf(buf, size,
  730. "a=control:streamid=%d\r\n", i + j);
  731. }
  732. if (ac[i]->pb && ac[i]->pb->av_class) {
  733. uint8_t *crypto_suite = NULL, *crypto_params = NULL;
  734. av_opt_get(ac[i]->pb, "srtp_out_suite", AV_OPT_SEARCH_CHILDREN,
  735. &crypto_suite);
  736. av_opt_get(ac[i]->pb, "srtp_out_params", AV_OPT_SEARCH_CHILDREN,
  737. &crypto_params);
  738. if (crypto_suite && crypto_suite[0])
  739. av_strlcatf(buf, size,
  740. "a=crypto:1 %s inline:%s\r\n",
  741. crypto_suite, crypto_params);
  742. av_free(crypto_suite);
  743. av_free(crypto_params);
  744. }
  745. }
  746. }
  747. return 0;
  748. }
  749. #else
  750. int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
  751. {
  752. return AVERROR(ENOSYS);
  753. }
  754. void ff_sdp_write_media(char *buff, int size, AVStream *st, int idx,
  755. const char *dest_addr, const char *dest_type,
  756. int port, int ttl, AVFormatContext *fmt)
  757. {
  758. }
  759. #endif