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.

327 lines
11KB

  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. uint8_t nal_type;
  106. while (!*(r++));
  107. nal_type = *r & 0x1f;
  108. r1 = ff_avc_find_startcode(r, c->extradata + c->extradata_size);
  109. if (nal_type != 7 && nal_type != 8) { /* Only output SPS and PPS */
  110. r = r1;
  111. continue;
  112. }
  113. if (p != (psets + strlen(pset_string))) {
  114. *p = ',';
  115. p++;
  116. }
  117. if (av_base64_encode(p, MAX_PSET_SIZE - (p - psets), r, r1 - r) == NULL) {
  118. av_log(c, AV_LOG_ERROR, "Cannot Base64-encode %td %td!\n", MAX_PSET_SIZE - (p - psets), r1 - r);
  119. av_free(psets);
  120. return NULL;
  121. }
  122. p += strlen(p);
  123. r = r1;
  124. }
  125. return psets;
  126. }
  127. static char *extradata2config(AVCodecContext *c)
  128. {
  129. char *config;
  130. if (c->extradata_size > MAX_EXTRADATA_SIZE) {
  131. av_log(c, AV_LOG_ERROR, "Too much extradata!\n");
  132. return NULL;
  133. }
  134. config = av_malloc(10 + c->extradata_size * 2);
  135. if (config == NULL) {
  136. av_log(c, AV_LOG_ERROR, "Cannot allocate memory for the config info.\n");
  137. return NULL;
  138. }
  139. memcpy(config, "; config=", 9);
  140. ff_data_to_hex(config + 9, c->extradata, c->extradata_size);
  141. config[9 + c->extradata_size * 2] = 0;
  142. return config;
  143. }
  144. static char *sdp_write_media_attributes(char *buff, int size, AVCodecContext *c, int payload_type)
  145. {
  146. char *config = NULL;
  147. switch (c->codec_id) {
  148. case CODEC_ID_H264:
  149. if (c->extradata_size) {
  150. config = extradata2psets(c);
  151. }
  152. av_strlcatf(buff, size, "a=rtpmap:%d H264/90000\r\n"
  153. "a=fmtp:%d packetization-mode=1%s\r\n",
  154. payload_type,
  155. payload_type, config ? config : "");
  156. break;
  157. case CODEC_ID_H263:
  158. case CODEC_ID_H263P:
  159. av_strlcatf(buff, size, "a=rtpmap:%d H263-2000/90000\r\n", payload_type);
  160. break;
  161. case CODEC_ID_MPEG4:
  162. if (c->extradata_size) {
  163. config = extradata2config(c);
  164. }
  165. av_strlcatf(buff, size, "a=rtpmap:%d MP4V-ES/90000\r\n"
  166. "a=fmtp:%d profile-level-id=1%s\r\n",
  167. payload_type,
  168. payload_type, config ? config : "");
  169. break;
  170. case CODEC_ID_AAC:
  171. if (c->extradata_size) {
  172. config = extradata2config(c);
  173. } else {
  174. /* FIXME: maybe we can forge config information based on the
  175. * codec parameters...
  176. */
  177. av_log(c, AV_LOG_ERROR, "AAC with no global headers is currently not supported.\n");
  178. return NULL;
  179. }
  180. if (config == NULL) {
  181. return NULL;
  182. }
  183. av_strlcatf(buff, size, "a=rtpmap:%d MPEG4-GENERIC/%d/%d\r\n"
  184. "a=fmtp:%d profile-level-id=1;"
  185. "mode=AAC-hbr;sizelength=13;indexlength=3;"
  186. "indexdeltalength=3%s\r\n",
  187. payload_type, c->sample_rate, c->channels,
  188. payload_type, config);
  189. break;
  190. case CODEC_ID_PCM_S16BE:
  191. if (payload_type >= 96)
  192. av_strlcatf(buff, size, "a=rtpmap:%d L16/%d/%d\r\n",
  193. payload_type,
  194. c->sample_rate, c->channels);
  195. break;
  196. case CODEC_ID_PCM_MULAW:
  197. if (payload_type >= 96)
  198. av_strlcatf(buff, size, "a=rtpmap:%d PCMU/%d/%d\r\n",
  199. payload_type,
  200. c->sample_rate, c->channels);
  201. break;
  202. case CODEC_ID_PCM_ALAW:
  203. if (payload_type >= 96)
  204. av_strlcatf(buff, size, "a=rtpmap:%d PCMA/%d/%d\r\n",
  205. payload_type,
  206. c->sample_rate, c->channels);
  207. break;
  208. case CODEC_ID_AMR_NB:
  209. av_strlcatf(buff, size, "a=rtpmap:%d AMR/%d/%d\r\n"
  210. "a=fmtp:%d octet-align=1\r\n",
  211. payload_type, c->sample_rate, c->channels,
  212. payload_type);
  213. break;
  214. case CODEC_ID_AMR_WB:
  215. av_strlcatf(buff, size, "a=rtpmap:%d AMR-WB/%d/%d\r\n"
  216. "a=fmtp:%d octet-align=1\r\n",
  217. payload_type, c->sample_rate, c->channels,
  218. payload_type);
  219. break;
  220. default:
  221. /* Nothing special to do here... */
  222. break;
  223. }
  224. av_free(config);
  225. return buff;
  226. }
  227. static void sdp_write_media(char *buff, int size, AVCodecContext *c, const char *dest_addr, int port, int ttl)
  228. {
  229. const char *type;
  230. int payload_type;
  231. payload_type = ff_rtp_get_payload_type(c);
  232. if (payload_type < 0) {
  233. payload_type = 96; /* FIXME: how to assign a private pt? rtp.c is broken too */
  234. }
  235. switch (c->codec_type) {
  236. case CODEC_TYPE_VIDEO : type = "video" ; break;
  237. case CODEC_TYPE_AUDIO : type = "audio" ; break;
  238. case CODEC_TYPE_SUBTITLE: type = "text" ; break;
  239. default : type = "application"; break;
  240. }
  241. av_strlcatf(buff, size, "m=%s %d RTP/AVP %d\r\n", type, port, payload_type);
  242. sdp_write_address(buff, size, dest_addr, ttl);
  243. if (c->bit_rate) {
  244. av_strlcatf(buff, size, "b=AS:%d\r\n", c->bit_rate / 1000);
  245. }
  246. sdp_write_media_attributes(buff, size, c, payload_type);
  247. }
  248. int avf_sdp_create(AVFormatContext *ac[], int n_files, char *buff, int size)
  249. {
  250. AVMetadataTag *title = av_metadata_get(ac[0]->metadata, "title", NULL, 0);
  251. struct sdp_session_level s;
  252. int i, j, port, ttl;
  253. char dst[32];
  254. memset(buff, 0, size);
  255. memset(&s, 0, sizeof(struct sdp_session_level));
  256. s.user = "-";
  257. s.src_addr = "127.0.0.1"; /* FIXME: Properly set this */
  258. s.name = title ? title->value : "No Name";
  259. port = 0;
  260. ttl = 0;
  261. if (n_files == 1) {
  262. port = sdp_get_address(dst, sizeof(dst), &ttl, ac[0]->filename);
  263. if (port > 0) {
  264. s.dst_addr = dst;
  265. s.ttl = ttl;
  266. }
  267. }
  268. sdp_write_header(buff, size, &s);
  269. dst[0] = 0;
  270. for (i = 0; i < n_files; i++) {
  271. if (n_files != 1) {
  272. port = sdp_get_address(dst, sizeof(dst), &ttl, ac[i]->filename);
  273. }
  274. for (j = 0; j < ac[i]->nb_streams; j++) {
  275. sdp_write_media(buff, size,
  276. ac[i]->streams[j]->codec, dst[0] ? dst : NULL,
  277. (port > 0) ? port + j * 2 : 0, ttl);
  278. if (port <= 0) {
  279. av_strlcatf(buff, size,
  280. "a=control:streamid=%d\r\n", i + j);
  281. }
  282. }
  283. }
  284. return 0;
  285. }
  286. #else
  287. int avf_sdp_create(AVFormatContext *ac[], int n_files, char *buff, int size)
  288. {
  289. return AVERROR(ENOSYS);
  290. }
  291. #endif