PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/ffmpeg-stm/libavformat/sdp.c

http://servetome.googlecode.com/
C | 372 lines | 300 code | 41 blank | 31 comment | 51 complexity | e2ae9c83fa0f86378f1e429db1af6d89 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, LGPL-2.1, LGPL-3.0, CC-BY-SA-3.0
  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. ff_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. psets = av_mallocz(MAX_PSET_SIZE);
  134. if (psets == NULL) {
  135. av_log(c, AV_LOG_ERROR, "Cannot allocate memory for the parameter sets.\n");
  136. return NULL;
  137. }
  138. memcpy(psets, pset_string, strlen(pset_string));
  139. p = psets + strlen(pset_string);
  140. r = ff_avc_find_startcode(c->extradata, c->extradata + c->extradata_size);
  141. while (r < c->extradata + c->extradata_size) {
  142. const uint8_t *r1;
  143. uint8_t nal_type;
  144. while (!*(r++));
  145. nal_type = *r & 0x1f;
  146. r1 = ff_avc_find_startcode(r, c->extradata + c->extradata_size);
  147. if (nal_type != 7 && nal_type != 8) { /* Only output SPS and PPS */
  148. r = r1;
  149. continue;
  150. }
  151. if (p != (psets + strlen(pset_string))) {
  152. *p = ',';
  153. p++;
  154. }
  155. if (av_base64_encode(p, MAX_PSET_SIZE - (p - psets), r, r1 - r) == NULL) {
  156. av_log(c, AV_LOG_ERROR, "Cannot Base64-encode %td %td!\n", MAX_PSET_SIZE - (p - psets), r1 - r);
  157. av_free(psets);
  158. return NULL;
  159. }
  160. p += strlen(p);
  161. r = r1;
  162. }
  163. return psets;
  164. }
  165. static char *extradata2config(AVCodecContext *c)
  166. {
  167. char *config;
  168. if (c->extradata_size > MAX_EXTRADATA_SIZE) {
  169. av_log(c, AV_LOG_ERROR, "Too much extradata!\n");
  170. return NULL;
  171. }
  172. config = av_malloc(10 + c->extradata_size * 2);
  173. if (config == NULL) {
  174. av_log(c, AV_LOG_ERROR, "Cannot allocate memory for the config info.\n");
  175. return NULL;
  176. }
  177. memcpy(config, "; config=", 9);
  178. ff_data_to_hex(config + 9, c->extradata, c->extradata_size, 0);
  179. config[9 + c->extradata_size * 2] = 0;
  180. return config;
  181. }
  182. static char *sdp_write_media_attributes(char *buff, int size, AVCodecContext *c, int payload_type)
  183. {
  184. char *config = NULL;
  185. switch (c->codec_id) {
  186. case CODEC_ID_H264:
  187. if (c->extradata_size) {
  188. config = extradata2psets(c);
  189. }
  190. av_strlcatf(buff, size, "a=rtpmap:%d H264/90000\r\n"
  191. "a=fmtp:%d packetization-mode=1%s\r\n",
  192. payload_type,
  193. payload_type, config ? config : "");
  194. break;
  195. case CODEC_ID_H263:
  196. case CODEC_ID_H263P:
  197. av_strlcatf(buff, size, "a=rtpmap:%d H263-2000/90000\r\n", payload_type);
  198. break;
  199. case CODEC_ID_MPEG4:
  200. if (c->extradata_size) {
  201. config = extradata2config(c);
  202. }
  203. av_strlcatf(buff, size, "a=rtpmap:%d MP4V-ES/90000\r\n"
  204. "a=fmtp:%d profile-level-id=1%s\r\n",
  205. payload_type,
  206. payload_type, config ? config : "");
  207. break;
  208. case CODEC_ID_AAC:
  209. if (c->extradata_size) {
  210. config = extradata2config(c);
  211. } else {
  212. /* FIXME: maybe we can forge config information based on the
  213. * codec parameters...
  214. */
  215. av_log(c, AV_LOG_ERROR, "AAC with no global headers is currently not supported.\n");
  216. return NULL;
  217. }
  218. if (config == NULL) {
  219. return NULL;
  220. }
  221. av_strlcatf(buff, size, "a=rtpmap:%d MPEG4-GENERIC/%d/%d\r\n"
  222. "a=fmtp:%d profile-level-id=1;"
  223. "mode=AAC-hbr;sizelength=13;indexlength=3;"
  224. "indexdeltalength=3%s\r\n",
  225. payload_type, c->sample_rate, c->channels,
  226. payload_type, config);
  227. break;
  228. case CODEC_ID_PCM_S16BE:
  229. if (payload_type >= RTP_PT_PRIVATE)
  230. av_strlcatf(buff, size, "a=rtpmap:%d L16/%d/%d\r\n",
  231. payload_type,
  232. c->sample_rate, c->channels);
  233. break;
  234. case CODEC_ID_PCM_MULAW:
  235. if (payload_type >= RTP_PT_PRIVATE)
  236. av_strlcatf(buff, size, "a=rtpmap:%d PCMU/%d/%d\r\n",
  237. payload_type,
  238. c->sample_rate, c->channels);
  239. break;
  240. case CODEC_ID_PCM_ALAW:
  241. if (payload_type >= RTP_PT_PRIVATE)
  242. av_strlcatf(buff, size, "a=rtpmap:%d PCMA/%d/%d\r\n",
  243. payload_type,
  244. c->sample_rate, c->channels);
  245. break;
  246. case CODEC_ID_AMR_NB:
  247. av_strlcatf(buff, size, "a=rtpmap:%d AMR/%d/%d\r\n"
  248. "a=fmtp:%d octet-align=1\r\n",
  249. payload_type, c->sample_rate, c->channels,
  250. payload_type);
  251. break;
  252. case CODEC_ID_AMR_WB:
  253. av_strlcatf(buff, size, "a=rtpmap:%d AMR-WB/%d/%d\r\n"
  254. "a=fmtp:%d octet-align=1\r\n",
  255. payload_type, c->sample_rate, c->channels,
  256. payload_type);
  257. break;
  258. default:
  259. /* Nothing special to do here... */
  260. break;
  261. }
  262. av_free(config);
  263. return buff;
  264. }
  265. static void sdp_write_media(char *buff, int size, AVCodecContext *c, const char *dest_addr, int port, int ttl)
  266. {
  267. const char *type;
  268. int payload_type;
  269. payload_type = ff_rtp_get_payload_type(c);
  270. if (payload_type < 0) {
  271. payload_type = RTP_PT_PRIVATE + (c->codec_type == AVMEDIA_TYPE_AUDIO);
  272. }
  273. switch (c->codec_type) {
  274. case AVMEDIA_TYPE_VIDEO : type = "video" ; break;
  275. case AVMEDIA_TYPE_AUDIO : type = "audio" ; break;
  276. case AVMEDIA_TYPE_SUBTITLE: type = "text" ; break;
  277. default : type = "application"; break;
  278. }
  279. av_strlcatf(buff, size, "m=%s %d RTP/AVP %d\r\n", type, port, payload_type);
  280. sdp_write_address(buff, size, dest_addr, ttl);
  281. if (c->bit_rate) {
  282. av_strlcatf(buff, size, "b=AS:%d\r\n", c->bit_rate / 1000);
  283. }
  284. sdp_write_media_attributes(buff, size, c, payload_type);
  285. }
  286. int avf_sdp_create(AVFormatContext *ac[], int n_files, char *buff, int size)
  287. {
  288. AVMetadataTag *title = av_metadata_get(ac[0]->metadata, "title", NULL, 0);
  289. struct sdp_session_level s;
  290. int i, j, port, ttl;
  291. char dst[32];
  292. memset(buff, 0, size);
  293. memset(&s, 0, sizeof(struct sdp_session_level));
  294. s.user = "-";
  295. s.src_addr = "127.0.0.1"; /* FIXME: Properly set this */
  296. s.name = title ? title->value : "No Name";
  297. port = 0;
  298. ttl = 0;
  299. if (n_files == 1) {
  300. port = sdp_get_address(dst, sizeof(dst), &ttl, ac[0]->filename);
  301. resolve_destination(dst, sizeof(dst));
  302. if (dst[0]) {
  303. s.dst_addr = dst;
  304. s.ttl = ttl;
  305. }
  306. }
  307. sdp_write_header(buff, size, &s);
  308. dst[0] = 0;
  309. for (i = 0; i < n_files; i++) {
  310. if (n_files != 1) {
  311. port = sdp_get_address(dst, sizeof(dst), &ttl, ac[i]->filename);
  312. resolve_destination(dst, sizeof(dst));
  313. }
  314. for (j = 0; j < ac[i]->nb_streams; j++) {
  315. sdp_write_media(buff, size,
  316. ac[i]->streams[j]->codec, dst[0] ? dst : NULL,
  317. (port > 0) ? port + j * 2 : 0, ttl);
  318. if (port <= 0) {
  319. av_strlcatf(buff, size,
  320. "a=control:streamid=%d\r\n", i + j);
  321. }
  322. }
  323. }
  324. return 0;
  325. }
  326. #else
  327. int avf_sdp_create(AVFormatContext *ac[], int n_files, char *buff, int size)
  328. {
  329. return AVERROR(ENOSYS);
  330. }
  331. #endif