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.

248 lines
7.9KB

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