]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavformat/sdp.c
Add minimal support for H.264 video in the SDP generator
[frescor/ffmpeg.git] / 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(AVCodecContext *c)
115 {
116     char *config;
117
118     if (c->extradata_size > MAX_EXTRADATA_SIZE) {
119         av_log(c, AV_LOG_ERROR, "Too many extra data!\n");
120
121         return NULL;
122     }
123     config = av_malloc(10 + c->extradata_size * 2);
124     if (config == NULL) {
125         av_log(c, 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, c->extradata, c->extradata_size);
130     config[9 + c->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_H264:
141             av_strlcatf(buff, size, "a=rtpmap:%d H264/90000\r\n"
142                                     "a=fmtp:%d packetization-mode=1%s\r\n",
143                                      payload_type,
144                                      payload_type, config ? config : "");
145             break;
146         case CODEC_ID_MPEG4:
147             if (c->extradata_size) {
148                 config = extradata2config(c);
149             }
150             av_strlcatf(buff, size, "a=rtpmap:%d MP4V-ES/90000\r\n"
151                                     "a=fmtp:%d profile-level-id=1%s\r\n",
152                                      payload_type,
153                                      payload_type, config ? config : "");
154             break;
155         case CODEC_ID_AAC:
156             if (c->extradata_size) {
157                 config = extradata2config(c);
158             } else {
159                 /* FIXME: maybe we can forge config information based on the
160                  *        codec parameters...
161                  */
162                 av_log(c, AV_LOG_ERROR, "AAC with no global headers is currently not supported\n");
163                 return NULL;
164             }
165             if (config == NULL) {
166                 return NULL;
167             }
168             av_strlcatf(buff, size, "a=rtpmap:%d MPEG4-GENERIC/%d/%d\r\n"
169                                     "a=fmtp:%d profile-level-id=1;"
170                                     "mode=AAC-hbr;sizelength=13;indexlength=3;"
171                                     "indexdeltalength=3%s\r\n",
172                                      payload_type, c->sample_rate, c->channels,
173                                      payload_type, config);
174             break;
175         case CODEC_ID_PCM_S16BE:
176             if (payload_type >= 96)
177                 av_strlcatf(buff, size, "a=rtpmap:%d L16/%d/%d\r\n",
178                                          payload_type,
179                                          c->sample_rate, c->channels);
180             break;
181         case CODEC_ID_PCM_MULAW:
182             if (payload_type >= 96)
183                 av_strlcatf(buff, size, "a=rtpmap:%d PCMU/%d/%d\r\n",
184                                          payload_type,
185                                          c->sample_rate, c->channels);
186             break;
187         case CODEC_ID_PCM_ALAW:
188             if (payload_type >= 96)
189                 av_strlcatf(buff, size, "a=rtpmap:%d PCMA/%d/%d\r\n",
190                                          payload_type,
191                                          c->sample_rate, c->channels);
192             break;
193         default:
194             /* Nothing special to do, here... */
195             break;
196     }
197
198     av_free(config);
199
200     return buff;
201 }
202
203 static void sdp_write_media(char *buff, int size, AVCodecContext *c, const char *dest_addr, int port, int ttl)
204 {
205     const char *type;
206     int payload_type;
207
208     payload_type = rtp_get_payload_type(c);
209     if (payload_type < 0) {
210         payload_type = 96;  /* FIXME: how to assign a private pt? rtp.c is broken too */
211     }
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
220     av_strlcatf(buff, size, "m=%s %d RTP/AVP %d\r\n", type, port, payload_type);
221     dest_write(buff, size, dest_addr, ttl);
222
223     sdp_media_attributes(buff, size, c, payload_type);
224 }
225
226 int avf_sdp_create(AVFormatContext *ac[], int n_files, char *buff, int size)
227 {
228     struct sdp_session_level s;
229     int i, j, port, ttl;
230     char dst[32];
231
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 = ac[0]->title;
237
238     port = 0;
239     ttl = 0;
240     if (n_files == 1) {
241         port = get_address(dst, sizeof(dst), &ttl, ac[0]->filename);
242         if (port > 0) {
243             s.dst_addr = dst;
244             s.ttl = ttl;
245         }
246     }
247     sdp_write_header(buff, size, &s);
248
249     dst[0] = 0;
250     for (i = 0; i < n_files; i++) {
251         if (n_files != 1) {
252             port = get_address(dst, sizeof(dst), &ttl, ac[i]->filename);
253         }
254         for (j = 0; j < ac[i]->nb_streams; j++) {
255             sdp_write_media(buff, size,
256                                   ac[i]->streams[j]->codec, dst[0] ? dst : NULL,
257                                   (port > 0) ? port + j * 2 : 0, ttl);
258             if (port <= 0) {
259                 av_strlcatf(buff, size,
260                                    "a=control:streamid=%d\r\n", i + j);
261             }
262         }
263     }
264
265     return 0;
266 }
267 #else
268 int avf_sdp_create(AVFormatContext *ac[], int n_files, char *buff, int size)
269 {
270     return AVERROR(ENOSYS);
271 }
272 #endif