Mercurial > libavformat.hg
annotate sdp.c @ 3786:33a94467ea2b libavformat
matroskadec: cosmetics: indentation
author | aurel |
---|---|
date | Wed, 27 Aug 2008 19:58:55 +0000 |
parents | e8d0c5f2ee60 |
children | ca6df1ecb412 |
rev | line source |
---|---|
2284 | 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 | |
3286 | 21 #include "libavutil/avstring.h" |
22 #include "libavutil/base64.h" | |
2284 | 23 #include "avformat.h" |
2961
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
24 #include "avc.h" |
2677
005c0fd8d3eb
Explicitly include rtp.h (needed for rtp_get_payload_type())
lucabe
parents:
2541
diff
changeset
|
25 #include "rtp.h" |
2284 | 26 |
2316
5a4914f78109
Fix linking when RTP is disabled and libraries are dynamic
lucabe
parents:
2284
diff
changeset
|
27 #ifdef CONFIG_RTP_MUXER |
2284 | 28 #define MAX_EXTRADATA_SIZE ((INT_MAX - 10) / 2) |
29 | |
30 struct sdp_session_level { | |
31 int sdp_version; /**< protocol version (currently 0) */ | |
32 int id; /**< session id */ | |
33 int version; /**< session version */ | |
34 int start_time; /**< session start time (NTP time, in seconds), | |
35 or 0 in case of permanent session */ | |
36 int end_time; /**< session end time (NTP time, in seconds), | |
37 or 0 if the session is not bounded */ | |
38 int ttl; /**< TTL, in case of multicast stream */ | |
39 const char *user; /**< username of the session's creator */ | |
40 const char *src_addr; /**< IP address of the machine from which the session was created */ | |
41 const char *dst_addr; /**< destination IP address (can be multicast) */ | |
42 const char *name; /**< session name (can be an empty string) */ | |
43 }; | |
44 | |
45 static void dest_write(char *buff, int size, const char *dest_addr, int ttl) | |
46 { | |
47 if (dest_addr) { | |
48 if (ttl > 0) { | |
49 av_strlcatf(buff, size, "c=IN IP4 %s/%d\r\n", dest_addr, ttl); | |
50 } else { | |
51 av_strlcatf(buff, size, "c=IN IP4 %s\r\n", dest_addr); | |
52 } | |
53 } | |
54 } | |
55 | |
56 static void sdp_write_header(char *buff, int size, struct sdp_session_level *s) | |
57 { | |
58 av_strlcatf(buff, size, "v=%d\r\n" | |
3605
e8d0c5f2ee60
Fix a typo in sdp_write_header(): change "IPV4", which is not a valid
stefano
parents:
3548
diff
changeset
|
59 "o=- %d %d IN IP4 %s\r\n" |
2284 | 60 "t=%d %d\r\n" |
61 "s=%s\r\n" | |
3548 | 62 "a=tool:libavformat " AV_STRINGIFY(LIBAVFORMAT_VERSION) "\r\n", |
2284 | 63 s->sdp_version, |
64 s->id, s->version, s->src_addr, | |
65 s->start_time, s->end_time, | |
66 s->name[0] ? s->name : "No Name"); | |
67 dest_write(buff, size, s->dst_addr, s->ttl); | |
68 } | |
69 | |
70 static int get_address(char *dest_addr, int size, int *ttl, const char *url) | |
71 { | |
72 int port; | |
73 const char *p; | |
74 | |
75 url_split(NULL, 0, NULL, 0, dest_addr, size, &port, NULL, 0, url); | |
76 | |
77 *ttl = 0; | |
78 p = strchr(url, '?'); | |
79 if (p) { | |
80 char buff[64]; | |
81 int is_multicast = find_info_tag(buff, sizeof(buff), "multicast", p); | |
82 | |
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 | |
92 return port; | |
93 } | |
94 | |
2961
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
95 #define MAX_PSET_SIZE 1024 |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
96 static char *extradata2psets(AVCodecContext *c) |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
97 { |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
98 char *psets, *p; |
3051 | 99 const uint8_t *r; |
2961
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
100 const char *pset_string = "; sprop-parameter-sets="; |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
101 |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
102 if (c->extradata_size > MAX_EXTRADATA_SIZE) { |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
103 av_log(c, AV_LOG_ERROR, "Too many extra data!\n"); |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
104 |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
105 return NULL; |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
106 } |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
107 |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
108 psets = av_mallocz(MAX_PSET_SIZE); |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
109 if (psets == NULL) { |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
110 av_log(c, AV_LOG_ERROR, "Cannot allocate memory for the parameter sets\n"); |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
111 return NULL; |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
112 } |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
113 memcpy(psets, pset_string, strlen(pset_string)); |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
114 p = psets + strlen(pset_string); |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
115 r = ff_avc_find_startcode(c->extradata, c->extradata + c->extradata_size); |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
116 while (r < c->extradata + c->extradata_size) { |
3051 | 117 const uint8_t *r1; |
2961
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
118 |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
119 while (!*(r++)); |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
120 r1 = ff_avc_find_startcode(r, c->extradata + c->extradata_size); |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
121 if (p != (psets + strlen(pset_string))) { |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
122 *p = ','; |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
123 p++; |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
124 } |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
125 if (av_base64_encode(p, MAX_PSET_SIZE - (p - psets), r, r1 - r) == NULL) { |
3276
24284961452b
Use correct length modifier for pointer diff argument in av_log() call.
diego
parents:
3113
diff
changeset
|
126 av_log(c, AV_LOG_ERROR, "Cannot BASE64 encode %td %td!\n", MAX_PSET_SIZE - (p - psets), r1 - r); |
2961
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
127 av_free(psets); |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
128 |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
129 return NULL; |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
130 } |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
131 p += strlen(p); |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
132 r = r1; |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
133 } |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
134 |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
135 return psets; |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
136 } |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
137 |
2284 | 138 static void digit_to_char(char *dst, uint8_t src) |
139 { | |
140 if (src < 10) { | |
141 *dst = '0' + src; | |
142 } else { | |
143 *dst = 'A' + src - 10; | |
144 } | |
145 } | |
146 | |
147 static char *data_to_hex(char *buff, const uint8_t *src, int s) | |
148 { | |
149 int i; | |
150 | |
151 for(i = 0; i < s; i++) { | |
152 digit_to_char(buff + 2 * i, src[i] >> 4); | |
153 digit_to_char(buff + 2 * i + 1, src[i] & 0xF); | |
154 } | |
155 | |
156 return buff; | |
157 } | |
158 | |
2916 | 159 static char *extradata2config(AVCodecContext *c) |
2521 | 160 { |
161 char *config; | |
162 | |
2916 | 163 if (c->extradata_size > MAX_EXTRADATA_SIZE) { |
164 av_log(c, AV_LOG_ERROR, "Too many extra data!\n"); | |
2521 | 165 |
166 return NULL; | |
167 } | |
2916 | 168 config = av_malloc(10 + c->extradata_size * 2); |
2521 | 169 if (config == NULL) { |
2916 | 170 av_log(c, AV_LOG_ERROR, "Cannot allocate memory for the config info\n"); |
2521 | 171 return NULL; |
172 } | |
173 memcpy(config, "; config=", 9); | |
2916 | 174 data_to_hex(config + 9, c->extradata, c->extradata_size); |
175 config[9 + c->extradata_size * 2] = 0; | |
2521 | 176 |
177 return config; | |
178 } | |
179 | |
2284 | 180 static char *sdp_media_attributes(char *buff, int size, AVCodecContext *c, int payload_type) |
181 { | |
182 char *config = NULL; | |
183 | |
184 switch (c->codec_id) { | |
2958
b489d30f8685
Add minimal support for H.264 video in the SDP generator
lucabe
parents:
2916
diff
changeset
|
185 case CODEC_ID_H264: |
2961
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
186 if (c->extradata_size) { |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
187 config = extradata2psets(c); |
b9a3b81c5eb8
Support out-of-band parameter sets in SDP for H.264 video
lucabe
parents:
2958
diff
changeset
|
188 } |
2958
b489d30f8685
Add minimal support for H.264 video in the SDP generator
lucabe
parents:
2916
diff
changeset
|
189 av_strlcatf(buff, size, "a=rtpmap:%d H264/90000\r\n" |
b489d30f8685
Add minimal support for H.264 video in the SDP generator
lucabe
parents:
2916
diff
changeset
|
190 "a=fmtp:%d packetization-mode=1%s\r\n", |
b489d30f8685
Add minimal support for H.264 video in the SDP generator
lucabe
parents:
2916
diff
changeset
|
191 payload_type, |
b489d30f8685
Add minimal support for H.264 video in the SDP generator
lucabe
parents:
2916
diff
changeset
|
192 payload_type, config ? config : ""); |
b489d30f8685
Add minimal support for H.264 video in the SDP generator
lucabe
parents:
2916
diff
changeset
|
193 break; |
2284 | 194 case CODEC_ID_MPEG4: |
2541
90609bab3de3
Test extradata_size instead of the CODEC_FLAG_GLOBAL_HEADER flag to check if
lucabe
parents:
2521
diff
changeset
|
195 if (c->extradata_size) { |
2916 | 196 config = extradata2config(c); |
2284 | 197 } |
198 av_strlcatf(buff, size, "a=rtpmap:%d MP4V-ES/90000\r\n" | |
199 "a=fmtp:%d profile-level-id=1%s\r\n", | |
200 payload_type, | |
201 payload_type, config ? config : ""); | |
202 break; | |
2521 | 203 case CODEC_ID_AAC: |
2541
90609bab3de3
Test extradata_size instead of the CODEC_FLAG_GLOBAL_HEADER flag to check if
lucabe
parents:
2521
diff
changeset
|
204 if (c->extradata_size) { |
2916 | 205 config = extradata2config(c); |
2521 | 206 } else { |
207 /* FIXME: maybe we can forge config information based on the | |
208 * codec parameters... | |
209 */ | |
2916 | 210 av_log(c, AV_LOG_ERROR, "AAC with no global headers is currently not supported\n"); |
2521 | 211 return NULL; |
212 } | |
213 if (config == NULL) { | |
214 return NULL; | |
215 } | |
216 av_strlcatf(buff, size, "a=rtpmap:%d MPEG4-GENERIC/%d/%d\r\n" | |
217 "a=fmtp:%d profile-level-id=1;" | |
218 "mode=AAC-hbr;sizelength=13;indexlength=3;" | |
219 "indexdeltalength=3%s\r\n", | |
220 payload_type, c->sample_rate, c->channels, | |
221 payload_type, config); | |
222 break; | |
2729 | 223 case CODEC_ID_PCM_S16BE: |
224 if (payload_type >= 96) | |
225 av_strlcatf(buff, size, "a=rtpmap:%d L16/%d/%d\r\n", | |
226 payload_type, | |
227 c->sample_rate, c->channels); | |
228 break; | |
229 case CODEC_ID_PCM_MULAW: | |
230 if (payload_type >= 96) | |
231 av_strlcatf(buff, size, "a=rtpmap:%d PCMU/%d/%d\r\n", | |
232 payload_type, | |
233 c->sample_rate, c->channels); | |
234 break; | |
235 case CODEC_ID_PCM_ALAW: | |
236 if (payload_type >= 96) | |
237 av_strlcatf(buff, size, "a=rtpmap:%d PCMA/%d/%d\r\n", | |
238 payload_type, | |
239 c->sample_rate, c->channels); | |
240 break; | |
2284 | 241 default: |
242 /* Nothing special to do, here... */ | |
243 break; | |
244 } | |
245 | |
246 av_free(config); | |
247 | |
248 return buff; | |
249 } | |
250 | |
251 static void sdp_write_media(char *buff, int size, AVCodecContext *c, const char *dest_addr, int port, int ttl) | |
252 { | |
253 const char *type; | |
254 int payload_type; | |
255 | |
256 payload_type = rtp_get_payload_type(c); | |
257 if (payload_type < 0) { | |
258 payload_type = 96; /* FIXME: how to assign a private pt? rtp.c is broken too */ | |
259 } | |
260 | |
261 switch (c->codec_type) { | |
262 case CODEC_TYPE_VIDEO : type = "video" ; break; | |
263 case CODEC_TYPE_AUDIO : type = "audio" ; break; | |
264 case CODEC_TYPE_SUBTITLE: type = "text" ; break; | |
265 default : type = "application"; break; | |
266 } | |
267 | |
268 av_strlcatf(buff, size, "m=%s %d RTP/AVP %d\r\n", type, port, payload_type); | |
269 dest_write(buff, size, dest_addr, ttl); | |
3113
f1aecf52bac5
Add some information about the stream bitrate, if available
lucabe
parents:
3051
diff
changeset
|
270 if (c->bit_rate) { |
f1aecf52bac5
Add some information about the stream bitrate, if available
lucabe
parents:
3051
diff
changeset
|
271 av_strlcatf(buff, size, "b=AS:%d\r\n", c->bit_rate / 1000); |
f1aecf52bac5
Add some information about the stream bitrate, if available
lucabe
parents:
3051
diff
changeset
|
272 } |
2284 | 273 |
274 sdp_media_attributes(buff, size, c, payload_type); | |
275 } | |
276 | |
2317
2adc9f64ecfb
Change avf_sdp_create() to get a pre-allocated buffer as input, and to
lucabe
parents:
2316
diff
changeset
|
277 int avf_sdp_create(AVFormatContext *ac[], int n_files, char *buff, int size) |
2284 | 278 { |
279 struct sdp_session_level s; | |
280 int i, j, port, ttl; | |
281 char dst[32]; | |
282 | |
2422 | 283 memset(buff, 0, size); |
2284 | 284 memset(&s, 0, sizeof(struct sdp_session_level)); |
285 s.user = "-"; | |
286 s.src_addr = "127.0.0.1"; /* FIXME: Properly set this */ | |
287 s.name = ac[0]->title; | |
288 | |
289 port = 0; | |
290 ttl = 0; | |
291 if (n_files == 1) { | |
292 port = get_address(dst, sizeof(dst), &ttl, ac[0]->filename); | |
293 if (port > 0) { | |
294 s.dst_addr = dst; | |
295 s.ttl = ttl; | |
296 } | |
297 } | |
2317
2adc9f64ecfb
Change avf_sdp_create() to get a pre-allocated buffer as input, and to
lucabe
parents:
2316
diff
changeset
|
298 sdp_write_header(buff, size, &s); |
2284 | 299 |
300 dst[0] = 0; | |
301 for (i = 0; i < n_files; i++) { | |
302 if (n_files != 1) { | |
303 port = get_address(dst, sizeof(dst), &ttl, ac[i]->filename); | |
304 } | |
305 for (j = 0; j < ac[i]->nb_streams; j++) { | |
2317
2adc9f64ecfb
Change avf_sdp_create() to get a pre-allocated buffer as input, and to
lucabe
parents:
2316
diff
changeset
|
306 sdp_write_media(buff, size, |
2284 | 307 ac[i]->streams[j]->codec, dst[0] ? dst : NULL, |
308 (port > 0) ? port + j * 2 : 0, ttl); | |
309 if (port <= 0) { | |
2317
2adc9f64ecfb
Change avf_sdp_create() to get a pre-allocated buffer as input, and to
lucabe
parents:
2316
diff
changeset
|
310 av_strlcatf(buff, size, |
2284 | 311 "a=control:streamid=%d\r\n", i + j); |
312 } | |
313 } | |
314 } | |
315 | |
2317
2adc9f64ecfb
Change avf_sdp_create() to get a pre-allocated buffer as input, and to
lucabe
parents:
2316
diff
changeset
|
316 return 0; |
2284 | 317 } |
2316
5a4914f78109
Fix linking when RTP is disabled and libraries are dynamic
lucabe
parents:
2284
diff
changeset
|
318 #else |
2317
2adc9f64ecfb
Change avf_sdp_create() to get a pre-allocated buffer as input, and to
lucabe
parents:
2316
diff
changeset
|
319 int avf_sdp_create(AVFormatContext *ac[], int n_files, char *buff, int size) |
2316
5a4914f78109
Fix linking when RTP is disabled and libraries are dynamic
lucabe
parents:
2284
diff
changeset
|
320 { |
2317
2adc9f64ecfb
Change avf_sdp_create() to get a pre-allocated buffer as input, and to
lucabe
parents:
2316
diff
changeset
|
321 return AVERROR(ENOSYS); |
2316
5a4914f78109
Fix linking when RTP is disabled and libraries are dynamic
lucabe
parents:
2284
diff
changeset
|
322 } |
5a4914f78109
Fix linking when RTP is disabled and libraries are dynamic
lucabe
parents:
2284
diff
changeset
|
323 #endif |