Disable unused function, fixes the warning:
[libav.git] / libavformat / sdp.c
CommitLineData
c5388c07
LA
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"
759d98d0 23#include "rtp.h"
c5388c07 24
7ba32703 25#ifdef CONFIG_RTP_MUXER
c5388c07
LA
26#define MAX_EXTRADATA_SIZE ((INT_MAX - 10) / 2)
27
28struct 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
43static 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
54static 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
68static 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
93static 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
102static 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
4901263a
LA
114static 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
c5388c07
LA
135static 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:
21b37480 141 if (c->extradata_size) {
4901263a 142 config = extradata2config(c->extradata, c->extradata_size);
c5388c07
LA
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;
4901263a 149 case CODEC_ID_AAC:
21b37480 150 if (c->extradata_size) {
4901263a
LA
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;
c5388c07
LA
169 default:
170 /* Nothing special to do, here... */
171 break;
172 }
173
174 av_free(config);
175
176 return buff;
177}
178
179static void sdp_write_media(char *buff, int size, AVCodecContext *c, const char *dest_addr, int port, int ttl)
180{
181 const char *type;
182 int payload_type;
183
184 payload_type = rtp_get_payload_type(c);
185 if (payload_type < 0) {
186 payload_type = 96; /* FIXME: how to assign a private pt? rtp.c is broken too */
187 }
188
189 switch (c->codec_type) {
190 case CODEC_TYPE_VIDEO : type = "video" ; break;
191 case CODEC_TYPE_AUDIO : type = "audio" ; break;
192 case CODEC_TYPE_SUBTITLE: type = "text" ; break;
193 default : type = "application"; break;
194 }
195
196 av_strlcatf(buff, size, "m=%s %d RTP/AVP %d\r\n", type, port, payload_type);
197 dest_write(buff, size, dest_addr, ttl);
198
199 sdp_media_attributes(buff, size, c, payload_type);
200}
201
8767b80f 202int avf_sdp_create(AVFormatContext *ac[], int n_files, char *buff, int size)
c5388c07 203{
c5388c07
LA
204 struct sdp_session_level s;
205 int i, j, port, ttl;
206 char dst[32];
207
5f2cbb53 208 memset(buff, 0, size);
c5388c07
LA
209 memset(&s, 0, sizeof(struct sdp_session_level));
210 s.user = "-";
211 s.src_addr = "127.0.0.1"; /* FIXME: Properly set this */
212 s.name = ac[0]->title;
213
214 port = 0;
215 ttl = 0;
216 if (n_files == 1) {
217 port = get_address(dst, sizeof(dst), &ttl, ac[0]->filename);
218 if (port > 0) {
219 s.dst_addr = dst;
220 s.ttl = ttl;
221 }
222 }
8767b80f 223 sdp_write_header(buff, size, &s);
c5388c07
LA
224
225 dst[0] = 0;
226 for (i = 0; i < n_files; i++) {
227 if (n_files != 1) {
228 port = get_address(dst, sizeof(dst), &ttl, ac[i]->filename);
229 }
230 for (j = 0; j < ac[i]->nb_streams; j++) {
8767b80f 231 sdp_write_media(buff, size,
c5388c07
LA
232 ac[i]->streams[j]->codec, dst[0] ? dst : NULL,
233 (port > 0) ? port + j * 2 : 0, ttl);
234 if (port <= 0) {
8767b80f 235 av_strlcatf(buff, size,
c5388c07
LA
236 "a=control:streamid=%d\r\n", i + j);
237 }
238 }
239 }
240
8767b80f 241 return 0;
c5388c07 242}
7ba32703 243#else
8767b80f 244int avf_sdp_create(AVFormatContext *ac[], int n_files, char *buff, int size)
7ba32703 245{
8767b80f 246 return AVERROR(ENOSYS);
7ba32703
LA
247}
248#endif