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.

324 lines
10KB

  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 "avstring.h"
  21. #include "avformat.h"
  22. #include "avc.h"
  23. #include "base64.h"
  24. #include "rtp.h"
  25. #ifdef CONFIG_RTP_MUXER
  26. #define MAX_EXTRADATA_SIZE ((INT_MAX - 10) / 2)
  27. struct sdp_session_level {
  28. int sdp_version; /**< protocol version (currently 0) */
  29. int id; /**< session id */
  30. int version; /**< session version */
  31. int start_time; /**< session start time (NTP time, in seconds),
  32. or 0 in case of permanent session */
  33. int end_time; /**< session end time (NTP time, in seconds),
  34. or 0 if the session is not bounded */
  35. int ttl; /**< TTL, in case of multicast stream */
  36. const char *user; /**< username of the session's creator */
  37. const char *src_addr; /**< IP address of the machine from which the session was created */
  38. const char *dst_addr; /**< destination IP address (can be multicast) */
  39. const char *name; /**< session name (can be an empty string) */
  40. };
  41. static void dest_write(char *buff, int size, const char *dest_addr, int ttl)
  42. {
  43. if (dest_addr) {
  44. if (ttl > 0) {
  45. av_strlcatf(buff, size, "c=IN IP4 %s/%d\r\n", dest_addr, ttl);
  46. } else {
  47. av_strlcatf(buff, size, "c=IN IP4 %s\r\n", dest_addr);
  48. }
  49. }
  50. }
  51. static void sdp_write_header(char *buff, int size, struct sdp_session_level *s)
  52. {
  53. av_strlcatf(buff, size, "v=%d\r\n"
  54. "o=- %d %d IN IPV4 %s\r\n"
  55. "t=%d %d\r\n"
  56. "s=%s\r\n"
  57. "a=tool:libavformat\r\n",
  58. s->sdp_version,
  59. s->id, s->version, s->src_addr,
  60. s->start_time, s->end_time,
  61. s->name[0] ? s->name : "No Name");
  62. dest_write(buff, size, s->dst_addr, s->ttl);
  63. }
  64. static int get_address(char *dest_addr, int size, int *ttl, const char *url)
  65. {
  66. int port;
  67. const char *p;
  68. url_split(NULL, 0, NULL, 0, dest_addr, size, &port, NULL, 0, url);
  69. *ttl = 0;
  70. p = strchr(url, '?');
  71. if (p) {
  72. char buff[64];
  73. int is_multicast = find_info_tag(buff, sizeof(buff), "multicast", p);
  74. if (is_multicast) {
  75. if (find_info_tag(buff, sizeof(buff), "ttl", p)) {
  76. *ttl = strtol(buff, NULL, 10);
  77. } else {
  78. *ttl = 5;
  79. }
  80. }
  81. }
  82. return port;
  83. }
  84. #define MAX_PSET_SIZE 1024
  85. static char *extradata2psets(AVCodecContext *c)
  86. {
  87. char *psets, *p;
  88. const uint8_t *r;
  89. const char *pset_string = "; sprop-parameter-sets=";
  90. if (c->extradata_size > MAX_EXTRADATA_SIZE) {
  91. av_log(c, AV_LOG_ERROR, "Too many extra data!\n");
  92. return NULL;
  93. }
  94. psets = av_mallocz(MAX_PSET_SIZE);
  95. if (psets == NULL) {
  96. av_log(c, AV_LOG_ERROR, "Cannot allocate memory for the parameter sets\n");
  97. return NULL;
  98. }
  99. memcpy(psets, pset_string, strlen(pset_string));
  100. p = psets + strlen(pset_string);
  101. r = ff_avc_find_startcode(c->extradata, c->extradata + c->extradata_size);
  102. while (r < c->extradata + c->extradata_size) {
  103. const uint8_t *r1;
  104. while (!*(r++));
  105. r1 = ff_avc_find_startcode(r, c->extradata + c->extradata_size);
  106. if (p != (psets + strlen(pset_string))) {
  107. *p = ',';
  108. p++;
  109. }
  110. if (av_base64_encode(p, MAX_PSET_SIZE - (p - psets), r, r1 - r) == NULL) {
  111. av_log(c, AV_LOG_ERROR, "Cannot BASE64 encode %d %d!\n", MAX_PSET_SIZE - (p - psets), r1 - r);
  112. av_free(psets);
  113. return NULL;
  114. }
  115. p += strlen(p);
  116. r = r1;
  117. }
  118. return psets;
  119. }
  120. static void digit_to_char(char *dst, uint8_t src)
  121. {
  122. if (src < 10) {
  123. *dst = '0' + src;
  124. } else {
  125. *dst = 'A' + src - 10;
  126. }
  127. }
  128. static char *data_to_hex(char *buff, const uint8_t *src, int s)
  129. {
  130. int i;
  131. for(i = 0; i < s; i++) {
  132. digit_to_char(buff + 2 * i, src[i] >> 4);
  133. digit_to_char(buff + 2 * i + 1, src[i] & 0xF);
  134. }
  135. return buff;
  136. }
  137. static char *extradata2config(AVCodecContext *c)
  138. {
  139. char *config;
  140. if (c->extradata_size > MAX_EXTRADATA_SIZE) {
  141. av_log(c, AV_LOG_ERROR, "Too many extra data!\n");
  142. return NULL;
  143. }
  144. config = av_malloc(10 + c->extradata_size * 2);
  145. if (config == NULL) {
  146. av_log(c, AV_LOG_ERROR, "Cannot allocate memory for the config info\n");
  147. return NULL;
  148. }
  149. memcpy(config, "; config=", 9);
  150. data_to_hex(config + 9, c->extradata, c->extradata_size);
  151. config[9 + c->extradata_size * 2] = 0;
  152. return config;
  153. }
  154. static char *sdp_media_attributes(char *buff, int size, AVCodecContext *c, int payload_type)
  155. {
  156. char *config = NULL;
  157. switch (c->codec_id) {
  158. case CODEC_ID_H264:
  159. if (c->extradata_size) {
  160. config = extradata2psets(c);
  161. }
  162. av_strlcatf(buff, size, "a=rtpmap:%d H264/90000\r\n"
  163. "a=fmtp:%d packetization-mode=1%s\r\n",
  164. payload_type,
  165. payload_type, config ? config : "");
  166. break;
  167. case CODEC_ID_MPEG4:
  168. if (c->extradata_size) {
  169. config = extradata2config(c);
  170. }
  171. av_strlcatf(buff, size, "a=rtpmap:%d MP4V-ES/90000\r\n"
  172. "a=fmtp:%d profile-level-id=1%s\r\n",
  173. payload_type,
  174. payload_type, config ? config : "");
  175. break;
  176. case CODEC_ID_AAC:
  177. if (c->extradata_size) {
  178. config = extradata2config(c);
  179. } else {
  180. /* FIXME: maybe we can forge config information based on the
  181. * codec parameters...
  182. */
  183. av_log(c, AV_LOG_ERROR, "AAC with no global headers is currently not supported\n");
  184. return NULL;
  185. }
  186. if (config == NULL) {
  187. return NULL;
  188. }
  189. av_strlcatf(buff, size, "a=rtpmap:%d MPEG4-GENERIC/%d/%d\r\n"
  190. "a=fmtp:%d profile-level-id=1;"
  191. "mode=AAC-hbr;sizelength=13;indexlength=3;"
  192. "indexdeltalength=3%s\r\n",
  193. payload_type, c->sample_rate, c->channels,
  194. payload_type, config);
  195. break;
  196. case CODEC_ID_PCM_S16BE:
  197. if (payload_type >= 96)
  198. av_strlcatf(buff, size, "a=rtpmap:%d L16/%d/%d\r\n",
  199. payload_type,
  200. c->sample_rate, c->channels);
  201. break;
  202. case CODEC_ID_PCM_MULAW:
  203. if (payload_type >= 96)
  204. av_strlcatf(buff, size, "a=rtpmap:%d PCMU/%d/%d\r\n",
  205. payload_type,
  206. c->sample_rate, c->channels);
  207. break;
  208. case CODEC_ID_PCM_ALAW:
  209. if (payload_type >= 96)
  210. av_strlcatf(buff, size, "a=rtpmap:%d PCMA/%d/%d\r\n",
  211. payload_type,
  212. c->sample_rate, c->channels);
  213. break;
  214. default:
  215. /* Nothing special to do, here... */
  216. break;
  217. }
  218. av_free(config);
  219. return buff;
  220. }
  221. static void sdp_write_media(char *buff, int size, AVCodecContext *c, const char *dest_addr, int port, int ttl)
  222. {
  223. const char *type;
  224. int payload_type;
  225. payload_type = rtp_get_payload_type(c);
  226. if (payload_type < 0) {
  227. payload_type = 96; /* FIXME: how to assign a private pt? rtp.c is broken too */
  228. }
  229. switch (c->codec_type) {
  230. case CODEC_TYPE_VIDEO : type = "video" ; break;
  231. case CODEC_TYPE_AUDIO : type = "audio" ; break;
  232. case CODEC_TYPE_SUBTITLE: type = "text" ; break;
  233. default : type = "application"; break;
  234. }
  235. av_strlcatf(buff, size, "m=%s %d RTP/AVP %d\r\n", type, port, payload_type);
  236. dest_write(buff, size, dest_addr, ttl);
  237. if (c->bit_rate) {
  238. av_strlcatf(buff, size, "b=AS:%d\r\n", c->bit_rate / 1000);
  239. }
  240. sdp_media_attributes(buff, size, c, payload_type);
  241. }
  242. int avf_sdp_create(AVFormatContext *ac[], int n_files, char *buff, int size)
  243. {
  244. struct sdp_session_level s;
  245. int i, j, port, ttl;
  246. char dst[32];
  247. memset(buff, 0, size);
  248. memset(&s, 0, sizeof(struct sdp_session_level));
  249. s.user = "-";
  250. s.src_addr = "127.0.0.1"; /* FIXME: Properly set this */
  251. s.name = ac[0]->title;
  252. port = 0;
  253. ttl = 0;
  254. if (n_files == 1) {
  255. port = get_address(dst, sizeof(dst), &ttl, ac[0]->filename);
  256. if (port > 0) {
  257. s.dst_addr = dst;
  258. s.ttl = ttl;
  259. }
  260. }
  261. sdp_write_header(buff, size, &s);
  262. dst[0] = 0;
  263. for (i = 0; i < n_files; i++) {
  264. if (n_files != 1) {
  265. port = get_address(dst, sizeof(dst), &ttl, ac[i]->filename);
  266. }
  267. for (j = 0; j < ac[i]->nb_streams; j++) {
  268. sdp_write_media(buff, size,
  269. ac[i]->streams[j]->codec, dst[0] ? dst : NULL,
  270. (port > 0) ? port + j * 2 : 0, ttl);
  271. if (port <= 0) {
  272. av_strlcatf(buff, size,
  273. "a=control:streamid=%d\r\n", i + j);
  274. }
  275. }
  276. }
  277. return 0;
  278. }
  279. #else
  280. int avf_sdp_create(AVFormatContext *ac[], int n_files, char *buff, int size)
  281. {
  282. return AVERROR(ENOSYS);
  283. }
  284. #endif