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.

816 lines
29KB

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