sdp.c 18 KB

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