sdp.c 10 KB

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