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.

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