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.

218 lines
6.8KB

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