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.

551 lines
19KB

  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 "libavcodec/xiph.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 char *sdp_write_media_attributes(char *buff, int size, AVCodecContext *c, int payload_type)
  264. {
  265. char *config = NULL;
  266. switch (c->codec_id) {
  267. case CODEC_ID_H264:
  268. if (c->extradata_size) {
  269. config = extradata2psets(c);
  270. }
  271. av_strlcatf(buff, size, "a=rtpmap:%d H264/90000\r\n"
  272. "a=fmtp:%d packetization-mode=1%s\r\n",
  273. payload_type,
  274. payload_type, config ? config : "");
  275. break;
  276. case CODEC_ID_H263:
  277. case CODEC_ID_H263P:
  278. /* a=framesize is required by 3GPP TS 26.234 (PSS). It
  279. * actually specifies the maximum video size, but we only know
  280. * the current size. This is required for playback on Android
  281. * stagefright and on Samsung bada. */
  282. av_strlcatf(buff, size, "a=rtpmap:%d H263-2000/90000\r\n"
  283. "a=framesize:%d %d-%d\r\n",
  284. payload_type,
  285. payload_type, c->width, c->height);
  286. break;
  287. case CODEC_ID_MPEG4:
  288. if (c->extradata_size) {
  289. config = extradata2config(c);
  290. }
  291. av_strlcatf(buff, size, "a=rtpmap:%d MP4V-ES/90000\r\n"
  292. "a=fmtp:%d profile-level-id=1%s\r\n",
  293. payload_type,
  294. payload_type, config ? config : "");
  295. break;
  296. case CODEC_ID_AAC:
  297. if (c->extradata_size) {
  298. config = extradata2config(c);
  299. } else {
  300. /* FIXME: maybe we can forge config information based on the
  301. * codec parameters...
  302. */
  303. av_log(c, AV_LOG_ERROR, "AAC with no global headers is currently not supported.\n");
  304. return NULL;
  305. }
  306. if (config == NULL) {
  307. return NULL;
  308. }
  309. av_strlcatf(buff, size, "a=rtpmap:%d MPEG4-GENERIC/%d/%d\r\n"
  310. "a=fmtp:%d profile-level-id=1;"
  311. "mode=AAC-hbr;sizelength=13;indexlength=3;"
  312. "indexdeltalength=3%s\r\n",
  313. payload_type, c->sample_rate, c->channels,
  314. payload_type, config);
  315. break;
  316. case CODEC_ID_PCM_S16BE:
  317. if (payload_type >= RTP_PT_PRIVATE)
  318. av_strlcatf(buff, size, "a=rtpmap:%d L16/%d/%d\r\n",
  319. payload_type,
  320. c->sample_rate, c->channels);
  321. break;
  322. case CODEC_ID_PCM_MULAW:
  323. if (payload_type >= RTP_PT_PRIVATE)
  324. av_strlcatf(buff, size, "a=rtpmap:%d PCMU/%d/%d\r\n",
  325. payload_type,
  326. c->sample_rate, c->channels);
  327. break;
  328. case CODEC_ID_PCM_ALAW:
  329. if (payload_type >= RTP_PT_PRIVATE)
  330. av_strlcatf(buff, size, "a=rtpmap:%d PCMA/%d/%d\r\n",
  331. payload_type,
  332. c->sample_rate, c->channels);
  333. break;
  334. case CODEC_ID_AMR_NB:
  335. av_strlcatf(buff, size, "a=rtpmap:%d AMR/%d/%d\r\n"
  336. "a=fmtp:%d octet-align=1\r\n",
  337. payload_type, c->sample_rate, c->channels,
  338. payload_type);
  339. break;
  340. case CODEC_ID_AMR_WB:
  341. av_strlcatf(buff, size, "a=rtpmap:%d AMR-WB/%d/%d\r\n"
  342. "a=fmtp:%d octet-align=1\r\n",
  343. payload_type, c->sample_rate, c->channels,
  344. payload_type);
  345. break;
  346. case CODEC_ID_VORBIS:
  347. if (c->extradata_size)
  348. config = xiph_extradata2config(c);
  349. else
  350. av_log(c, AV_LOG_ERROR, "Vorbis configuration info missing\n");
  351. if (!config)
  352. return NULL;
  353. av_strlcatf(buff, size, "a=rtpmap:%d vorbis/%d/%d\r\n"
  354. "a=fmtp:%d configuration=%s\r\n",
  355. payload_type, c->sample_rate, c->channels,
  356. payload_type, config);
  357. break;
  358. case CODEC_ID_THEORA: {
  359. const char *pix_fmt;
  360. if (c->extradata_size)
  361. config = xiph_extradata2config(c);
  362. else
  363. av_log(c, AV_LOG_ERROR, "Theora configuation info missing\n");
  364. if (!config)
  365. return NULL;
  366. switch (c->pix_fmt) {
  367. case PIX_FMT_YUV420P:
  368. pix_fmt = "YCbCr-4:2:0";
  369. break;
  370. case PIX_FMT_YUV422P:
  371. pix_fmt = "YCbCr-4:2:2";
  372. break;
  373. case PIX_FMT_YUV444P:
  374. pix_fmt = "YCbCr-4:4:4";
  375. break;
  376. default:
  377. av_log(c, AV_LOG_ERROR, "Unsupported pixel format.\n");
  378. return NULL;
  379. }
  380. av_strlcatf(buff, size, "a=rtpmap:%d theora/90000\r\n"
  381. "a=fmtp:%d delivery-method=inline; "
  382. "width=%d; height=%d; sampling=%s; "
  383. "configuration=%s\r\n",
  384. payload_type, payload_type,
  385. c->width, c->height, pix_fmt, config);
  386. break;
  387. }
  388. case CODEC_ID_VP8:
  389. av_strlcatf(buff, size, "a=rtpmap:%d VP8/90000\r\n",
  390. payload_type);
  391. break;
  392. case CODEC_ID_ADPCM_G722:
  393. if (payload_type >= RTP_PT_PRIVATE)
  394. av_strlcatf(buff, size, "a=rtpmap:%d G722/%d/%d\r\n",
  395. payload_type,
  396. 8000, c->channels);
  397. break;
  398. default:
  399. /* Nothing special to do here... */
  400. break;
  401. }
  402. av_free(config);
  403. return buff;
  404. }
  405. void ff_sdp_write_media(char *buff, int size, AVCodecContext *c, const char *dest_addr, const char *dest_type, int port, int ttl)
  406. {
  407. const char *type;
  408. int payload_type;
  409. payload_type = ff_rtp_get_payload_type(c);
  410. if (payload_type < 0) {
  411. payload_type = RTP_PT_PRIVATE + (c->codec_type == AVMEDIA_TYPE_AUDIO);
  412. }
  413. switch (c->codec_type) {
  414. case AVMEDIA_TYPE_VIDEO : type = "video" ; break;
  415. case AVMEDIA_TYPE_AUDIO : type = "audio" ; break;
  416. case AVMEDIA_TYPE_SUBTITLE: type = "text" ; break;
  417. default : type = "application"; break;
  418. }
  419. av_strlcatf(buff, size, "m=%s %d RTP/AVP %d\r\n", type, port, payload_type);
  420. sdp_write_address(buff, size, dest_addr, dest_type, ttl);
  421. if (c->bit_rate) {
  422. av_strlcatf(buff, size, "b=AS:%d\r\n", c->bit_rate / 1000);
  423. }
  424. sdp_write_media_attributes(buff, size, c, payload_type);
  425. }
  426. int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
  427. {
  428. AVDictionaryEntry *title = av_dict_get(ac[0]->metadata, "title", NULL, 0);
  429. struct sdp_session_level s;
  430. int i, j, port, ttl, is_multicast;
  431. char dst[32], dst_type[5];
  432. memset(buf, 0, size);
  433. memset(&s, 0, sizeof(struct sdp_session_level));
  434. s.user = "-";
  435. s.src_addr = "127.0.0.1"; /* FIXME: Properly set this */
  436. s.src_type = "IP4";
  437. s.name = title ? title->value : "No Name";
  438. port = 0;
  439. ttl = 0;
  440. if (n_files == 1) {
  441. port = sdp_get_address(dst, sizeof(dst), &ttl, ac[0]->filename);
  442. is_multicast = resolve_destination(dst, sizeof(dst), dst_type,
  443. sizeof(dst_type));
  444. if (!is_multicast)
  445. ttl = 0;
  446. if (dst[0]) {
  447. s.dst_addr = dst;
  448. s.dst_type = dst_type;
  449. s.ttl = ttl;
  450. if (!strcmp(dst_type, "IP6")) {
  451. s.src_addr = "::1";
  452. s.src_type = "IP6";
  453. }
  454. }
  455. }
  456. sdp_write_header(buf, size, &s);
  457. dst[0] = 0;
  458. for (i = 0; i < n_files; i++) {
  459. if (n_files != 1) {
  460. port = sdp_get_address(dst, sizeof(dst), &ttl, ac[i]->filename);
  461. is_multicast = resolve_destination(dst, sizeof(dst), dst_type,
  462. sizeof(dst_type));
  463. if (!is_multicast)
  464. ttl = 0;
  465. }
  466. for (j = 0; j < ac[i]->nb_streams; j++) {
  467. ff_sdp_write_media(buf, size,
  468. ac[i]->streams[j]->codec, dst[0] ? dst : NULL,
  469. dst_type, (port > 0) ? port + j * 2 : 0, ttl);
  470. if (port <= 0) {
  471. av_strlcatf(buf, size,
  472. "a=control:streamid=%d\r\n", i + j);
  473. }
  474. }
  475. }
  476. return 0;
  477. }
  478. #else
  479. int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
  480. {
  481. return AVERROR(ENOSYS);
  482. }
  483. void ff_sdp_write_media(char *buff, int size, AVCodecContext *c, const char *dest_addr, const char *dest_type, int port, int ttl)
  484. {
  485. }
  486. #endif
  487. #if FF_API_SDP_CREATE
  488. int avf_sdp_create(AVFormatContext *ac[], int n_files, char *buff, int size)
  489. {
  490. return av_sdp_create(ac, n_files, buff, size);
  491. }
  492. #endif