2 * copyright (c) 2007 Luca Abeni
4 * This file is part of FFmpeg.
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.
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.
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
25 #ifdef CONFIG_RTP_MUXER
26 #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) */
43 static void dest_write(char *buff, int size, const char *dest_addr, int ttl)
47 av_strlcatf(buff, size, "c=IN IP4 %s/%d\r\n", dest_addr, ttl);
49 av_strlcatf(buff, size, "c=IN IP4 %s\r\n", dest_addr);
54 static void sdp_write_header(char *buff, int size, struct sdp_session_level *s)
56 av_strlcatf(buff, size, "v=%d\r\n"
57 "o=- %d %d IN IPV4 %s\r\n"
60 "a=tool:libavformat\r\n",
62 s->id, s->version, s->src_addr,
63 s->start_time, s->end_time,
64 s->name[0] ? s->name : "No Name");
65 dest_write(buff, size, s->dst_addr, s->ttl);
68 static int get_address(char *dest_addr, int size, int *ttl, const char *url)
73 url_split(NULL, 0, NULL, 0, dest_addr, size, &port, NULL, 0, url);
79 int is_multicast = find_info_tag(buff, sizeof(buff), "multicast", p);
82 if (find_info_tag(buff, sizeof(buff), "ttl", p)) {
83 *ttl = strtol(buff, NULL, 10);
93 static void digit_to_char(char *dst, uint8_t src)
98 *dst = 'A' + src - 10;
102 static char *data_to_hex(char *buff, const uint8_t *src, int s)
106 for(i = 0; i < s; i++) {
107 digit_to_char(buff + 2 * i, src[i] >> 4);
108 digit_to_char(buff + 2 * i + 1, src[i] & 0xF);
114 static char *extradata2config(const uint8_t *extradata, int extradata_size)
118 if (extradata_size > MAX_EXTRADATA_SIZE) {
119 av_log(NULL, AV_LOG_ERROR, "Too many extra data!\n");
123 config = av_malloc(10 + extradata_size * 2);
124 if (config == NULL) {
125 av_log(NULL, AV_LOG_ERROR, "Cannot allocate memory for the config info\n");
128 memcpy(config, "; config=", 9);
129 data_to_hex(config + 9, extradata, extradata_size);
130 config[9 + extradata_size * 2] = 0;
135 static char *sdp_media_attributes(char *buff, int size, AVCodecContext *c, int payload_type)
139 switch (c->codec_id) {
141 if (c->extradata_size) {
142 config = extradata2config(c->extradata, c->extradata_size);
144 av_strlcatf(buff, size, "a=rtpmap:%d MP4V-ES/90000\r\n"
145 "a=fmtp:%d profile-level-id=1%s\r\n",
147 payload_type, config ? config : "");
150 if (c->extradata_size) {
151 config = extradata2config(c->extradata, c->extradata_size);
153 /* FIXME: maybe we can forge config information based on the
154 * codec parameters...
156 av_log(NULL, AV_LOG_ERROR, "AAC with no global headers is currently not supported\n");
159 if (config == NULL) {
162 av_strlcatf(buff, size, "a=rtpmap:%d MPEG4-GENERIC/%d/%d\r\n"
163 "a=fmtp:%d profile-level-id=1;"
164 "mode=AAC-hbr;sizelength=13;indexlength=3;"
165 "indexdeltalength=3%s\r\n",
166 payload_type, c->sample_rate, c->channels,
167 payload_type, config);
169 case CODEC_ID_PCM_S16BE:
170 if (payload_type >= 96)
171 av_strlcatf(buff, size, "a=rtpmap:%d L16/%d/%d\r\n",
173 c->sample_rate, c->channels);
175 case CODEC_ID_PCM_MULAW:
176 if (payload_type >= 96)
177 av_strlcatf(buff, size, "a=rtpmap:%d PCMU/%d/%d\r\n",
179 c->sample_rate, c->channels);
181 case CODEC_ID_PCM_ALAW:
182 if (payload_type >= 96)
183 av_strlcatf(buff, size, "a=rtpmap:%d PCMA/%d/%d\r\n",
185 c->sample_rate, c->channels);
188 /* Nothing special to do, here... */
197 static void sdp_write_media(char *buff, int size, AVCodecContext *c, const char *dest_addr, int port, int ttl)
202 payload_type = rtp_get_payload_type(c);
203 if (payload_type < 0) {
204 payload_type = 96; /* FIXME: how to assign a private pt? rtp.c is broken too */
207 switch (c->codec_type) {
208 case CODEC_TYPE_VIDEO : type = "video" ; break;
209 case CODEC_TYPE_AUDIO : type = "audio" ; break;
210 case CODEC_TYPE_SUBTITLE: type = "text" ; break;
211 default : type = "application"; break;
214 av_strlcatf(buff, size, "m=%s %d RTP/AVP %d\r\n", type, port, payload_type);
215 dest_write(buff, size, dest_addr, ttl);
217 sdp_media_attributes(buff, size, c, payload_type);
220 int avf_sdp_create(AVFormatContext *ac[], int n_files, char *buff, int size)
222 struct sdp_session_level s;
226 memset(buff, 0, size);
227 memset(&s, 0, sizeof(struct sdp_session_level));
229 s.src_addr = "127.0.0.1"; /* FIXME: Properly set this */
230 s.name = ac[0]->title;
235 port = get_address(dst, sizeof(dst), &ttl, ac[0]->filename);
241 sdp_write_header(buff, size, &s);
244 for (i = 0; i < n_files; i++) {
246 port = get_address(dst, sizeof(dst), &ttl, ac[i]->filename);
248 for (j = 0; j < ac[i]->nb_streams; j++) {
249 sdp_write_media(buff, size,
250 ac[i]->streams[j]->codec, dst[0] ? dst : NULL,
251 (port > 0) ? port + j * 2 : 0, ttl);
253 av_strlcatf(buff, size,
254 "a=control:streamid=%d\r\n", i + j);
262 int avf_sdp_create(AVFormatContext *ac[], int n_files, char *buff, int size)
264 return AVERROR(ENOSYS);