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.

625 lines
21KB

  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/parseutils.h"
  24. #include "libavcodec/xiph.h"
  25. #include "libavcodec/mpeg4audio.h"
  26. #include "avformat.h"
  27. #include "internal.h"
  28. #include "avc.h"
  29. #include "rtp.h"
  30. #if CONFIG_NETWORK
  31. #include "network.h"
  32. #endif
  33. #if CONFIG_RTP_MUXER
  34. #define MAX_EXTRADATA_SIZE ((INT_MAX - 10) / 2)
  35. struct sdp_session_level {
  36. int sdp_version; /**< protocol version (currently 0) */
  37. int id; /**< session ID */
  38. int version; /**< session version */
  39. int start_time; /**< session start time (NTP time, in seconds),
  40. or 0 in case of permanent session */
  41. int end_time; /**< session end time (NTP time, in seconds),
  42. or 0 if the session is not bounded */
  43. int ttl; /**< TTL, in case of multicast stream */
  44. const char *user; /**< username of the session's creator */
  45. const char *src_addr; /**< IP address of the machine from which the session was created */
  46. const char *src_type; /**< address type of src_addr */
  47. const char *dst_addr; /**< destination IP address (can be multicast) */
  48. const char *dst_type; /**< destination IP address type */
  49. const char *name; /**< session name (can be an empty string) */
  50. };
  51. static void sdp_write_address(char *buff, int size, const char *dest_addr,
  52. const char *dest_type, int ttl)
  53. {
  54. if (dest_addr) {
  55. if (!dest_type)
  56. dest_type = "IP4";
  57. if (ttl > 0 && !strcmp(dest_type, "IP4")) {
  58. /* The TTL should only be specified for IPv4 multicast addresses,
  59. * not for IPv6. */
  60. av_strlcatf(buff, size, "c=IN %s %s/%d\r\n", dest_type, dest_addr, ttl);
  61. } else {
  62. av_strlcatf(buff, size, "c=IN %s %s\r\n", dest_type, dest_addr);
  63. }
  64. }
  65. }
  66. static void sdp_write_header(char *buff, int size, struct sdp_session_level *s)
  67. {
  68. av_strlcatf(buff, size, "v=%d\r\n"
  69. "o=- %d %d IN %s %s\r\n"
  70. "s=%s\r\n",
  71. s->sdp_version,
  72. s->id, s->version, s->src_type, s->src_addr,
  73. s->name);
  74. sdp_write_address(buff, size, s->dst_addr, s->dst_type, s->ttl);
  75. av_strlcatf(buff, size, "t=%d %d\r\n"
  76. "a=tool:libavformat " AV_STRINGIFY(LIBAVFORMAT_VERSION) "\r\n",
  77. s->start_time, s->end_time);
  78. }
  79. #if CONFIG_NETWORK
  80. static int resolve_destination(char *dest_addr, int size, char *type,
  81. int type_size)
  82. {
  83. struct addrinfo hints, *ai;
  84. int is_multicast;
  85. av_strlcpy(type, "IP4", type_size);
  86. if (!dest_addr[0])
  87. return 0;
  88. /* Resolve the destination, since it must be written
  89. * as a numeric IP address in the SDP. */
  90. memset(&hints, 0, sizeof(hints));
  91. if (getaddrinfo(dest_addr, NULL, &hints, &ai))
  92. return 0;
  93. getnameinfo(ai->ai_addr, ai->ai_addrlen, dest_addr, size,
  94. NULL, 0, NI_NUMERICHOST);
  95. #ifdef AF_INET6
  96. if (ai->ai_family == AF_INET6)
  97. av_strlcpy(type, "IP6", type_size);
  98. #endif
  99. is_multicast = ff_is_multicast_address(ai->ai_addr);
  100. freeaddrinfo(ai);
  101. return is_multicast;
  102. }
  103. #else
  104. static int resolve_destination(char *dest_addr, int size, char *type,
  105. int type_size)
  106. {
  107. return 0;
  108. }
  109. #endif
  110. static int sdp_get_address(char *dest_addr, int size, int *ttl, const char *url)
  111. {
  112. int port;
  113. const char *p;
  114. char proto[32];
  115. av_url_split(proto, sizeof(proto), NULL, 0, dest_addr, size, &port, NULL, 0, url);
  116. *ttl = 0;
  117. if (strcmp(proto, "rtp")) {
  118. /* The url isn't for the actual rtp sessions,
  119. * don't parse out anything else than the destination.
  120. */
  121. return 0;
  122. }
  123. p = strchr(url, '?');
  124. if (p) {
  125. char buff[64];
  126. if (av_find_info_tag(buff, sizeof(buff), "ttl", p)) {
  127. *ttl = strtol(buff, NULL, 10);
  128. } else {
  129. *ttl = 5;
  130. }
  131. }
  132. return port;
  133. }
  134. #define MAX_PSET_SIZE 1024
  135. static char *extradata2psets(AVCodecContext *c)
  136. {
  137. char *psets, *p;
  138. const uint8_t *r;
  139. const char *pset_string = "; sprop-parameter-sets=";
  140. if (c->extradata_size > MAX_EXTRADATA_SIZE) {
  141. av_log(c, AV_LOG_ERROR, "Too much extradata!\n");
  142. return NULL;
  143. }
  144. if (c->extradata[0] == 1) {
  145. uint8_t *dummy_p;
  146. int dummy_int;
  147. AVBitStreamFilterContext *bsfc= av_bitstream_filter_init("h264_mp4toannexb");
  148. if (!bsfc) {
  149. av_log(c, AV_LOG_ERROR, "Cannot open the h264_mp4toannexb BSF!\n");
  150. return NULL;
  151. }
  152. av_bitstream_filter_filter(bsfc, c, NULL, &dummy_p, &dummy_int, NULL, 0, 0);
  153. av_bitstream_filter_close(bsfc);
  154. }
  155. psets = av_mallocz(MAX_PSET_SIZE);
  156. if (psets == NULL) {
  157. av_log(c, AV_LOG_ERROR, "Cannot allocate memory for the parameter sets.\n");
  158. return NULL;
  159. }
  160. memcpy(psets, pset_string, strlen(pset_string));
  161. p = psets + strlen(pset_string);
  162. r = ff_avc_find_startcode(c->extradata, c->extradata + c->extradata_size);
  163. while (r < c->extradata + c->extradata_size) {
  164. const uint8_t *r1;
  165. uint8_t nal_type;
  166. while (!*(r++));
  167. nal_type = *r & 0x1f;
  168. r1 = ff_avc_find_startcode(r, c->extradata + c->extradata_size);
  169. if (nal_type != 7 && nal_type != 8) { /* Only output SPS and PPS */
  170. r = r1;
  171. continue;
  172. }
  173. if (p != (psets + strlen(pset_string))) {
  174. *p = ',';
  175. p++;
  176. }
  177. if (av_base64_encode(p, MAX_PSET_SIZE - (p - psets), r, r1 - r) == NULL) {
  178. av_log(c, AV_LOG_ERROR, "Cannot Base64-encode %td %td!\n", MAX_PSET_SIZE - (p - psets), r1 - r);
  179. av_free(psets);
  180. return NULL;
  181. }
  182. p += strlen(p);
  183. r = r1;
  184. }
  185. return psets;
  186. }
  187. static char *extradata2config(AVCodecContext *c)
  188. {
  189. char *config;
  190. if (c->extradata_size > MAX_EXTRADATA_SIZE) {
  191. av_log(c, AV_LOG_ERROR, "Too much extradata!\n");
  192. return NULL;
  193. }
  194. config = av_malloc(10 + c->extradata_size * 2);
  195. if (config == NULL) {
  196. av_log(c, AV_LOG_ERROR, "Cannot allocate memory for the config info.\n");
  197. return NULL;
  198. }
  199. memcpy(config, "; config=", 9);
  200. ff_data_to_hex(config + 9, c->extradata, c->extradata_size, 0);
  201. config[9 + c->extradata_size * 2] = 0;
  202. return config;
  203. }
  204. static char *xiph_extradata2config(AVCodecContext *c)
  205. {
  206. char *config, *encoded_config;
  207. uint8_t *header_start[3];
  208. int headers_len, header_len[3], config_len;
  209. int first_header_size;
  210. switch (c->codec_id) {
  211. case CODEC_ID_THEORA:
  212. first_header_size = 42;
  213. break;
  214. case CODEC_ID_VORBIS:
  215. first_header_size = 30;
  216. break;
  217. default:
  218. av_log(c, AV_LOG_ERROR, "Unsupported Xiph codec ID\n");
  219. return NULL;
  220. }
  221. if (ff_split_xiph_headers(c->extradata, c->extradata_size,
  222. first_header_size, header_start,
  223. header_len) < 0) {
  224. av_log(c, AV_LOG_ERROR, "Extradata corrupt.\n");
  225. return NULL;
  226. }
  227. headers_len = header_len[0] + header_len[2];
  228. config_len = 4 + // count
  229. 3 + // ident
  230. 2 + // packet size
  231. 1 + // header count
  232. 2 + // header size
  233. headers_len; // and the rest
  234. config = av_malloc(config_len);
  235. if (!config)
  236. goto xiph_fail;
  237. encoded_config = av_malloc(AV_BASE64_SIZE(config_len));
  238. if (!encoded_config) {
  239. av_free(config);
  240. goto xiph_fail;
  241. }
  242. config[0] = config[1] = config[2] = 0;
  243. config[3] = 1;
  244. config[4] = (RTP_XIPH_IDENT >> 16) & 0xff;
  245. config[5] = (RTP_XIPH_IDENT >> 8) & 0xff;
  246. config[6] = (RTP_XIPH_IDENT ) & 0xff;
  247. config[7] = (headers_len >> 8) & 0xff;
  248. config[8] = headers_len & 0xff;
  249. config[9] = 2;
  250. config[10] = header_len[0];
  251. config[11] = 0; // size of comment header; nonexistent
  252. memcpy(config + 12, header_start[0], header_len[0]);
  253. memcpy(config + 12 + header_len[0], header_start[2], header_len[2]);
  254. av_base64_encode(encoded_config, AV_BASE64_SIZE(config_len),
  255. config, config_len);
  256. av_free(config);
  257. return encoded_config;
  258. xiph_fail:
  259. av_log(c, AV_LOG_ERROR,
  260. "Not enough memory for configuration string\n");
  261. return NULL;
  262. }
  263. static int latm_context2profilelevel(AVCodecContext *c) {
  264. /* MP4A-LATM
  265. * The RTP payload format specification is described in RFC 3016
  266. * The encoding specifications are provided in ISO/IEC 14496-3 */
  267. int profile_level = 0x2B;
  268. /* TODO: AAC Profile only supports AAC LC Object Type.
  269. * Different Object Types should implement different Profile Levels */
  270. if (c->sample_rate <= 24000) {
  271. if (c->channels <= 2)
  272. profile_level = 0x28; // AAC Profile, Level 1
  273. } else if (c->sample_rate <= 48000) {
  274. if (c->channels <= 2) {
  275. profile_level = 0x29; // AAC Profile, Level 2
  276. } else if (c->channels <= 5) {
  277. profile_level = 0x2A; // AAC Profile, Level 4
  278. }
  279. } else if (c->sample_rate <= 96000) {
  280. if (c->channels <= 5) {
  281. profile_level = 0x2B; // AAC Profile, Level 5
  282. }
  283. }
  284. return profile_level;
  285. }
  286. static char *latm_context2config(AVCodecContext *c) {
  287. /* MP4A-LATM
  288. * The RTP payload format specification is described in RFC 3016
  289. * The encoding specifications are provided in ISO/IEC 14496-3 */
  290. uint8_t config_byte[6];
  291. int rate_index;
  292. char *config;
  293. for (rate_index = 0; rate_index < 16; rate_index++)
  294. if (ff_mpeg4audio_sample_rates[rate_index] == c->sample_rate)
  295. break;
  296. if (rate_index == 16) {
  297. av_log(c, AV_LOG_ERROR, "Unsupported sample rate\n");
  298. return NULL;
  299. }
  300. config_byte[0] = 0x40;
  301. config_byte[1] = 0;
  302. config_byte[2] = 0x20 | rate_index;
  303. config_byte[3] = c->channels << 4;
  304. config_byte[4] = 0x3f;
  305. config_byte[5] = 0xc0;
  306. config = av_malloc(6*2+1);
  307. if (!config) {
  308. av_log(c, AV_LOG_ERROR, "Cannot allocate memory for the config info.\n");
  309. return NULL;
  310. }
  311. ff_data_to_hex(config, config_byte, 6, 1);
  312. config[12] = 0;
  313. return config;
  314. }
  315. static char *sdp_write_media_attributes(char *buff, int size, AVCodecContext *c, int payload_type, int flags)
  316. {
  317. char *config = NULL;
  318. switch (c->codec_id) {
  319. case CODEC_ID_H264:
  320. if (c->extradata_size) {
  321. config = extradata2psets(c);
  322. }
  323. av_strlcatf(buff, size, "a=rtpmap:%d H264/90000\r\n"
  324. "a=fmtp:%d packetization-mode=1%s\r\n",
  325. payload_type,
  326. payload_type, config ? config : "");
  327. break;
  328. case CODEC_ID_H263:
  329. case CODEC_ID_H263P:
  330. /* a=framesize is required by 3GPP TS 26.234 (PSS). It
  331. * actually specifies the maximum video size, but we only know
  332. * the current size. This is required for playback on Android
  333. * stagefright and on Samsung bada. */
  334. av_strlcatf(buff, size, "a=rtpmap:%d H263-2000/90000\r\n"
  335. "a=framesize:%d %d-%d\r\n",
  336. payload_type,
  337. payload_type, c->width, c->height);
  338. break;
  339. case CODEC_ID_MPEG4:
  340. if (c->extradata_size) {
  341. config = extradata2config(c);
  342. }
  343. av_strlcatf(buff, size, "a=rtpmap:%d MP4V-ES/90000\r\n"
  344. "a=fmtp:%d profile-level-id=1%s\r\n",
  345. payload_type,
  346. payload_type, config ? config : "");
  347. break;
  348. case CODEC_ID_AAC:
  349. if (flags & AVFMT_FLAG_MP4A_LATM) {
  350. config = latm_context2config(c);
  351. if (!config)
  352. return NULL;
  353. av_strlcatf(buff, size, "a=rtpmap:%d MP4A-LATM/%d/%d\r\n"
  354. "a=fmtp:%d profile-level-id=%d;cpresent=0;config=%s\r\n",
  355. payload_type, c->sample_rate, c->channels,
  356. payload_type, latm_context2profilelevel(c), config);
  357. } else {
  358. if (c->extradata_size) {
  359. config = extradata2config(c);
  360. } else {
  361. /* FIXME: maybe we can forge config information based on the
  362. * codec parameters...
  363. */
  364. av_log(c, AV_LOG_ERROR, "AAC with no global headers is currently not supported.\n");
  365. return NULL;
  366. }
  367. if (config == NULL) {
  368. return NULL;
  369. }
  370. av_strlcatf(buff, size, "a=rtpmap:%d MPEG4-GENERIC/%d/%d\r\n"
  371. "a=fmtp:%d profile-level-id=1;"
  372. "mode=AAC-hbr;sizelength=13;indexlength=3;"
  373. "indexdeltalength=3%s\r\n",
  374. payload_type, c->sample_rate, c->channels,
  375. payload_type, config);
  376. }
  377. break;
  378. case CODEC_ID_PCM_S16BE:
  379. if (payload_type >= RTP_PT_PRIVATE)
  380. av_strlcatf(buff, size, "a=rtpmap:%d L16/%d/%d\r\n",
  381. payload_type,
  382. c->sample_rate, c->channels);
  383. break;
  384. case CODEC_ID_PCM_MULAW:
  385. if (payload_type >= RTP_PT_PRIVATE)
  386. av_strlcatf(buff, size, "a=rtpmap:%d PCMU/%d/%d\r\n",
  387. payload_type,
  388. c->sample_rate, c->channels);
  389. break;
  390. case CODEC_ID_PCM_ALAW:
  391. if (payload_type >= RTP_PT_PRIVATE)
  392. av_strlcatf(buff, size, "a=rtpmap:%d PCMA/%d/%d\r\n",
  393. payload_type,
  394. c->sample_rate, c->channels);
  395. break;
  396. case CODEC_ID_AMR_NB:
  397. av_strlcatf(buff, size, "a=rtpmap:%d AMR/%d/%d\r\n"
  398. "a=fmtp:%d octet-align=1\r\n",
  399. payload_type, c->sample_rate, c->channels,
  400. payload_type);
  401. break;
  402. case CODEC_ID_AMR_WB:
  403. av_strlcatf(buff, size, "a=rtpmap:%d AMR-WB/%d/%d\r\n"
  404. "a=fmtp:%d octet-align=1\r\n",
  405. payload_type, c->sample_rate, c->channels,
  406. payload_type);
  407. break;
  408. case CODEC_ID_VORBIS:
  409. if (c->extradata_size)
  410. config = xiph_extradata2config(c);
  411. else
  412. av_log(c, AV_LOG_ERROR, "Vorbis configuration info missing\n");
  413. if (!config)
  414. return NULL;
  415. av_strlcatf(buff, size, "a=rtpmap:%d vorbis/%d/%d\r\n"
  416. "a=fmtp:%d configuration=%s\r\n",
  417. payload_type, c->sample_rate, c->channels,
  418. payload_type, config);
  419. break;
  420. case CODEC_ID_THEORA: {
  421. const char *pix_fmt;
  422. if (c->extradata_size)
  423. config = xiph_extradata2config(c);
  424. else
  425. av_log(c, AV_LOG_ERROR, "Theora configuation info missing\n");
  426. if (!config)
  427. return NULL;
  428. switch (c->pix_fmt) {
  429. case PIX_FMT_YUV420P:
  430. pix_fmt = "YCbCr-4:2:0";
  431. break;
  432. case PIX_FMT_YUV422P:
  433. pix_fmt = "YCbCr-4:2:2";
  434. break;
  435. case PIX_FMT_YUV444P:
  436. pix_fmt = "YCbCr-4:4:4";
  437. break;
  438. default:
  439. av_log(c, AV_LOG_ERROR, "Unsupported pixel format.\n");
  440. return NULL;
  441. }
  442. av_strlcatf(buff, size, "a=rtpmap:%d theora/90000\r\n"
  443. "a=fmtp:%d delivery-method=inline; "
  444. "width=%d; height=%d; sampling=%s; "
  445. "configuration=%s\r\n",
  446. payload_type, payload_type,
  447. c->width, c->height, pix_fmt, config);
  448. break;
  449. }
  450. case CODEC_ID_VP8:
  451. av_strlcatf(buff, size, "a=rtpmap:%d VP8/90000\r\n",
  452. payload_type);
  453. break;
  454. case CODEC_ID_ADPCM_G722:
  455. if (payload_type >= RTP_PT_PRIVATE)
  456. av_strlcatf(buff, size, "a=rtpmap:%d G722/%d/%d\r\n",
  457. payload_type,
  458. 8000, c->channels);
  459. break;
  460. default:
  461. /* Nothing special to do here... */
  462. break;
  463. }
  464. av_free(config);
  465. return buff;
  466. }
  467. void ff_sdp_write_media(char *buff, int size, AVCodecContext *c, const char *dest_addr, const char *dest_type, int port, int ttl, int flags)
  468. {
  469. const char *type;
  470. int payload_type;
  471. payload_type = ff_rtp_get_payload_type(c);
  472. if (payload_type < 0) {
  473. payload_type = RTP_PT_PRIVATE + (c->codec_type == AVMEDIA_TYPE_AUDIO);
  474. }
  475. switch (c->codec_type) {
  476. case AVMEDIA_TYPE_VIDEO : type = "video" ; break;
  477. case AVMEDIA_TYPE_AUDIO : type = "audio" ; break;
  478. case AVMEDIA_TYPE_SUBTITLE: type = "text" ; break;
  479. default : type = "application"; break;
  480. }
  481. av_strlcatf(buff, size, "m=%s %d RTP/AVP %d\r\n", type, port, payload_type);
  482. sdp_write_address(buff, size, dest_addr, dest_type, ttl);
  483. if (c->bit_rate) {
  484. av_strlcatf(buff, size, "b=AS:%d\r\n", c->bit_rate / 1000);
  485. }
  486. sdp_write_media_attributes(buff, size, c, payload_type, flags);
  487. }
  488. int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
  489. {
  490. AVMetadataTag *title = av_metadata_get(ac[0]->metadata, "title", NULL, 0);
  491. struct sdp_session_level s;
  492. int i, j, port, ttl, is_multicast;
  493. char dst[32], dst_type[5];
  494. memset(buf, 0, size);
  495. memset(&s, 0, sizeof(struct sdp_session_level));
  496. s.user = "-";
  497. s.src_addr = "127.0.0.1"; /* FIXME: Properly set this */
  498. s.src_type = "IP4";
  499. s.name = title ? title->value : "No Name";
  500. port = 0;
  501. ttl = 0;
  502. if (n_files == 1) {
  503. port = sdp_get_address(dst, sizeof(dst), &ttl, ac[0]->filename);
  504. is_multicast = resolve_destination(dst, sizeof(dst), dst_type,
  505. sizeof(dst_type));
  506. if (!is_multicast)
  507. ttl = 0;
  508. if (dst[0]) {
  509. s.dst_addr = dst;
  510. s.dst_type = dst_type;
  511. s.ttl = ttl;
  512. if (!strcmp(dst_type, "IP6")) {
  513. s.src_addr = "::1";
  514. s.src_type = "IP6";
  515. }
  516. }
  517. }
  518. sdp_write_header(buf, size, &s);
  519. dst[0] = 0;
  520. for (i = 0; i < n_files; i++) {
  521. if (n_files != 1) {
  522. port = sdp_get_address(dst, sizeof(dst), &ttl, ac[i]->filename);
  523. is_multicast = resolve_destination(dst, sizeof(dst), dst_type,
  524. sizeof(dst_type));
  525. if (!is_multicast)
  526. ttl = 0;
  527. }
  528. for (j = 0; j < ac[i]->nb_streams; j++) {
  529. ff_sdp_write_media(buf, size,
  530. ac[i]->streams[j]->codec, dst[0] ? dst : NULL,
  531. dst_type, (port > 0) ? port + j * 2 : 0, ttl,
  532. ac[i]->flags);
  533. if (port <= 0) {
  534. av_strlcatf(buf, size,
  535. "a=control:streamid=%d\r\n", i + j);
  536. }
  537. }
  538. }
  539. return 0;
  540. }
  541. #else
  542. int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
  543. {
  544. return AVERROR(ENOSYS);
  545. }
  546. void ff_sdp_write_media(char *buff, int size, AVCodecContext *c, const char *dest_addr, const char *dest_type, int port, int ttl, int flags)
  547. {
  548. }
  549. #endif
  550. #if FF_API_SDP_CREATE
  551. int avf_sdp_create(AVFormatContext *ac[], int n_files, char *buff, int size)
  552. {
  553. return av_sdp_create(ac, n_files, buff, size);
  554. }
  555. #endif