]> git.sesse.net Git - ffmpeg/blob - libavformat/sdp.c
url_write is part of the public API so it may not be under ifdef.
[ffmpeg] / libavformat / sdp.c
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
21 #include "avstring.h"
22 #include "avformat.h"
23 #include "rtp.h"
24
25 #ifdef CONFIG_RTP_MUXER
26 #define MAX_EXTRADATA_SIZE ((INT_MAX - 10) / 2)
27
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
43 static void dest_write(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
54 static void sdp_write_header(char *buff, int size, struct sdp_session_level *s)
55 {
56     av_strlcatf(buff, size, "v=%d\r\n"
57                             "o=- %d %d IN IPV4 %s\r\n"
58                             "t=%d %d\r\n"
59                             "s=%s\r\n"
60                             "a=tool:libavformat\r\n",
61                             s->sdp_version,
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);
66 }
67
68 static int get_address(char *dest_addr, int size, int *ttl, const char *url)
69 {
70     int port;
71     const char *p;
72
73     url_split(NULL, 0, NULL, 0, dest_addr, size, &port, NULL, 0, url);
74
75     *ttl = 0;
76     p = strchr(url, '?');
77     if (p) {
78         char buff[64];
79         int is_multicast = find_info_tag(buff, sizeof(buff), "multicast", p);
80
81         if (is_multicast) {
82             if (find_info_tag(buff, sizeof(buff), "ttl", p)) {
83                 *ttl = strtol(buff, NULL, 10);
84             } else {
85                 *ttl = 5;
86             }
87         }
88     }
89
90     return port;
91 }
92
93 static void digit_to_char(char *dst, uint8_t src)
94 {
95     if (src < 10) {
96         *dst = '0' + src;
97     } else {
98         *dst = 'A' + src - 10;
99     }
100 }
101
102 static char *data_to_hex(char *buff, const uint8_t *src, int s)
103 {
104     int i;
105
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);
109     }
110
111     return buff;
112 }
113
114 static char *extradata2config(const uint8_t *extradata, int extradata_size)
115 {
116     char *config;
117
118     if (extradata_size > MAX_EXTRADATA_SIZE) {
119         av_log(NULL, AV_LOG_ERROR, "Too many extra data!\n");
120
121         return NULL;
122     }
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");
126         return NULL;
127     }
128     memcpy(config, "; config=", 9);
129     data_to_hex(config + 9, extradata, extradata_size);
130     config[9 + extradata_size * 2] = 0;
131
132     return config;
133 }
134
135 static char *sdp_media_attributes(char *buff, int size, AVCodecContext *c, int payload_type)
136 {
137     char *config = NULL;
138
139     switch (c->codec_id) {
140         case CODEC_ID_MPEG4:
141             if (c->extradata_size) {
142                 config = extradata2config(c->extradata, c->extradata_size);
143             }
144             av_strlcatf(buff, size, "a=rtpmap:%d MP4V-ES/90000\r\n"
145                                     "a=fmtp:%d profile-level-id=1%s\r\n",
146                                      payload_type,
147                                      payload_type, config ? config : "");
148             break;
149         case CODEC_ID_AAC:
150             if (c->extradata_size) {
151                 config = extradata2config(c->extradata, c->extradata_size);
152             } else {
153                 /* FIXME: maybe we can forge config information based on the
154                  *        codec parameters...
155                  */
156                 av_log(NULL, AV_LOG_ERROR, "AAC with no global headers is currently not supported\n");
157                 return NULL;
158             }
159             if (config == NULL) {
160                 return NULL;
161             }
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);
168             break;
169         case CODEC_ID_PCM_S16BE:
170             if (payload_type >= 96)
171                 av_strlcatf(buff, size, "a=rtpmap:%d L16/%d/%d\r\n",
172                                          payload_type,
173                                          c->sample_rate, c->channels);
174             break;
175         case CODEC_ID_PCM_MULAW:
176             if (payload_type >= 96)
177                 av_strlcatf(buff, size, "a=rtpmap:%d PCMU/%d/%d\r\n",
178                                          payload_type,
179                                          c->sample_rate, c->channels);
180             break;
181         case CODEC_ID_PCM_ALAW:
182             if (payload_type >= 96)
183                 av_strlcatf(buff, size, "a=rtpmap:%d PCMA/%d/%d\r\n",
184                                          payload_type,
185                                          c->sample_rate, c->channels);
186             break;
187         default:
188             /* Nothing special to do, here... */
189             break;
190     }
191
192     av_free(config);
193
194     return buff;
195 }
196
197 static void sdp_write_media(char *buff, int size, AVCodecContext *c, const char *dest_addr, int port, int ttl)
198 {
199     const char *type;
200     int payload_type;
201
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 */
205     }
206
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;
212     }
213
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);
216
217     sdp_media_attributes(buff, size, c, payload_type);
218 }
219
220 int avf_sdp_create(AVFormatContext *ac[], int n_files, char *buff, int size)
221 {
222     struct sdp_session_level s;
223     int i, j, port, ttl;
224     char dst[32];
225
226     memset(buff, 0, size);
227     memset(&s, 0, sizeof(struct sdp_session_level));
228     s.user = "-";
229     s.src_addr = "127.0.0.1";    /* FIXME: Properly set this */
230     s.name = ac[0]->title;
231
232     port = 0;
233     ttl = 0;
234     if (n_files == 1) {
235         port = get_address(dst, sizeof(dst), &ttl, ac[0]->filename);
236         if (port > 0) {
237             s.dst_addr = dst;
238             s.ttl = ttl;
239         }
240     }
241     sdp_write_header(buff, size, &s);
242
243     dst[0] = 0;
244     for (i = 0; i < n_files; i++) {
245         if (n_files != 1) {
246             port = get_address(dst, sizeof(dst), &ttl, ac[i]->filename);
247         }
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);
252             if (port <= 0) {
253                 av_strlcatf(buff, size,
254                                    "a=control:streamid=%d\r\n", i + j);
255             }
256         }
257     }
258
259     return 0;
260 }
261 #else
262 int avf_sdp_create(AVFormatContext *ac[], int n_files, char *buff, int size)
263 {
264     return AVERROR(ENOSYS);
265 }
266 #endif