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.

510 lines
17KB

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