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.

391 lines
13KB

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