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.

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