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. #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 *sdp_media_attributes(char *buff, int size, AVCodecContext *c, int payload_type)
  99. {
  100. char *config = NULL;
  101. switch (c->codec_id) {
  102. case CODEC_ID_MPEG4:
  103. if (c->flags & CODEC_FLAG_GLOBAL_HEADER) {
  104. if (c->extradata_size > MAX_EXTRADATA_SIZE) {
  105. av_log(NULL, AV_LOG_ERROR, "Too many extra data!\n");
  106. return NULL;
  107. }
  108. config = av_malloc(10 + c->extradata_size * 2);
  109. if (config == NULL) {
  110. av_log(NULL, AV_LOG_ERROR, "Cannot allocate memory for the config info\n");
  111. return NULL;
  112. }
  113. memcpy(config, "; config=", 9);
  114. data_to_hex(config + 9, c->extradata, c->extradata_size);
  115. config[9 + c->extradata_size * 2] = 0;
  116. }
  117. av_strlcatf(buff, size, "a=rtpmap:%d MP4V-ES/90000\r\n"
  118. "a=fmtp:%d profile-level-id=1%s\r\n",
  119. payload_type,
  120. payload_type, config ? config : "");
  121. break;
  122. default:
  123. /* Nothing special to do, here... */
  124. break;
  125. }
  126. av_free(config);
  127. return buff;
  128. }
  129. static void sdp_write_media(char *buff, int size, AVCodecContext *c, const char *dest_addr, int port, int ttl)
  130. {
  131. const char *type;
  132. int payload_type;
  133. payload_type = rtp_get_payload_type(c);
  134. if (payload_type < 0) {
  135. payload_type = 96; /* FIXME: how to assign a private pt? rtp.c is broken too */
  136. }
  137. switch (c->codec_type) {
  138. case CODEC_TYPE_VIDEO : type = "video" ; break;
  139. case CODEC_TYPE_AUDIO : type = "audio" ; break;
  140. case CODEC_TYPE_SUBTITLE: type = "text" ; break;
  141. default : type = "application"; break;
  142. }
  143. av_strlcatf(buff, size, "m=%s %d RTP/AVP %d\r\n", type, port, payload_type);
  144. dest_write(buff, size, dest_addr, ttl);
  145. sdp_media_attributes(buff, size, c, payload_type);
  146. }
  147. int avf_sdp_create(AVFormatContext *ac[], int n_files, char *buff, int size)
  148. {
  149. struct sdp_session_level s;
  150. int i, j, port, ttl;
  151. char dst[32];
  152. memset(&s, 0, sizeof(struct sdp_session_level));
  153. s.user = "-";
  154. s.src_addr = "127.0.0.1"; /* FIXME: Properly set this */
  155. s.name = ac[0]->title;
  156. port = 0;
  157. ttl = 0;
  158. if (n_files == 1) {
  159. port = get_address(dst, sizeof(dst), &ttl, ac[0]->filename);
  160. if (port > 0) {
  161. s.dst_addr = dst;
  162. s.ttl = ttl;
  163. }
  164. }
  165. sdp_write_header(buff, size, &s);
  166. dst[0] = 0;
  167. for (i = 0; i < n_files; i++) {
  168. if (n_files != 1) {
  169. port = get_address(dst, sizeof(dst), &ttl, ac[i]->filename);
  170. }
  171. for (j = 0; j < ac[i]->nb_streams; j++) {
  172. sdp_write_media(buff, size,
  173. ac[i]->streams[j]->codec, dst[0] ? dst : NULL,
  174. (port > 0) ? port + j * 2 : 0, ttl);
  175. if (port <= 0) {
  176. av_strlcatf(buff, size,
  177. "a=control:streamid=%d\r\n", i + j);
  178. }
  179. }
  180. }
  181. return 0;
  182. }
  183. #else
  184. int avf_sdp_create(AVFormatContext *ac[], int n_files, char *buff, int size)
  185. {
  186. return AVERROR(ENOSYS);
  187. }
  188. #endif