Introduce an SDP generator
[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"
23
24#define MAX_EXTRADATA_SIZE ((INT_MAX - 10) / 2)
25
26struct sdp_session_level {
27 int sdp_version; /**< protocol version (currently 0) */
28 int id; /**< session id */
29 int version; /**< session version */
30 int start_time; /**< session start time (NTP time, in seconds),
31 or 0 in case of permanent session */
32 int end_time; /**< session end time (NTP time, in seconds),
33 or 0 if the session is not bounded */
34 int ttl; /**< TTL, in case of multicast stream */
35 const char *user; /**< username of the session's creator */
36 const char *src_addr; /**< IP address of the machine from which the session was created */
37 const char *dst_addr; /**< destination IP address (can be multicast) */
38 const char *name; /**< session name (can be an empty string) */
39};
40
41static void dest_write(char *buff, int size, const char *dest_addr, int ttl)
42{
43 if (dest_addr) {
44 if (ttl > 0) {
45 av_strlcatf(buff, size, "c=IN IP4 %s/%d\r\n", dest_addr, ttl);
46 } else {
47 av_strlcatf(buff, size, "c=IN IP4 %s\r\n", dest_addr);
48 }
49 }
50}
51
52static void sdp_write_header(char *buff, int size, struct sdp_session_level *s)
53{
54 av_strlcatf(buff, size, "v=%d\r\n"
55 "o=- %d %d IN IPV4 %s\r\n"
56 "t=%d %d\r\n"
57 "s=%s\r\n"
58 "a=tool:libavformat\r\n",
59 s->sdp_version,
60 s->id, s->version, s->src_addr,
61 s->start_time, s->end_time,
62 s->name[0] ? s->name : "No Name");
63 dest_write(buff, size, s->dst_addr, s->ttl);
64}
65
66static int get_address(char *dest_addr, int size, int *ttl, const char *url)
67{
68 int port;
69 const char *p;
70
71 url_split(NULL, 0, NULL, 0, dest_addr, size, &port, NULL, 0, url);
72
73 *ttl = 0;
74 p = strchr(url, '?');
75 if (p) {
76 char buff[64];
77 int is_multicast = find_info_tag(buff, sizeof(buff), "multicast", p);
78
79 if (is_multicast) {
80 if (find_info_tag(buff, sizeof(buff), "ttl", p)) {
81 *ttl = strtol(buff, NULL, 10);
82 } else {
83 *ttl = 5;
84 }
85 }
86 }
87
88 return port;
89}
90
91static void digit_to_char(char *dst, uint8_t src)
92{
93 if (src < 10) {
94 *dst = '0' + src;
95 } else {
96 *dst = 'A' + src - 10;
97 }
98}
99
100static char *data_to_hex(char *buff, const uint8_t *src, int s)
101{
102 int i;
103
104 for(i = 0; i < s; i++) {
105 digit_to_char(buff + 2 * i, src[i] >> 4);
106 digit_to_char(buff + 2 * i + 1, src[i] & 0xF);
107 }
108
109 return buff;
110}
111
112static char *sdp_media_attributes(char *buff, int size, AVCodecContext *c, int payload_type)
113{
114 char *config = NULL;
115
116 switch (c->codec_id) {
117 case CODEC_ID_MPEG4:
118 if (c->flags & CODEC_FLAG_GLOBAL_HEADER) {
119 if (c->extradata_size > MAX_EXTRADATA_SIZE) {
120 av_log(NULL, AV_LOG_ERROR, "Too many extra data!\n");
121
122 return NULL;
123 }
124 config = av_malloc(10 + c->extradata_size * 2);
125 if (config == NULL) {
126 av_log(NULL, AV_LOG_ERROR, "Cannot allocate memory for the config info\n");
127 return NULL;
128 }
129 memcpy(config, "; config=", 9);
130 data_to_hex(config + 9, c->extradata, c->extradata_size);
131 config[9 + c->extradata_size * 2] = 0;
132 }
133 av_strlcatf(buff, size, "a=rtpmap:%d MP4V-ES/90000\r\n"
134 "a=fmtp:%d profile-level-id=1%s\r\n",
135 payload_type,
136 payload_type, config ? config : "");
137 break;
138 default:
139 /* Nothing special to do, here... */
140 break;
141 }
142
143 av_free(config);
144
145 return buff;
146}
147
148static void sdp_write_media(char *buff, int size, AVCodecContext *c, const char *dest_addr, int port, int ttl)
149{
150 const char *type;
151 int payload_type;
152
153 payload_type = rtp_get_payload_type(c);
154 if (payload_type < 0) {
155 payload_type = 96; /* FIXME: how to assign a private pt? rtp.c is broken too */
156 }
157
158 switch (c->codec_type) {
159 case CODEC_TYPE_VIDEO : type = "video" ; break;
160 case CODEC_TYPE_AUDIO : type = "audio" ; break;
161 case CODEC_TYPE_SUBTITLE: type = "text" ; break;
162 default : type = "application"; break;
163 }
164
165 av_strlcatf(buff, size, "m=%s %d RTP/AVP %d\r\n", type, port, payload_type);
166 dest_write(buff, size, dest_addr, ttl);
167
168 sdp_media_attributes(buff, size, c, payload_type);
169}
170
171#define SDP_BUFFER_SIZE 2048
172char *avf_sdp_create(AVFormatContext *ac[], int n_files)
173{
174 char *buff;
175 struct sdp_session_level s;
176 int i, j, port, ttl;
177 char dst[32];
178
179 buff = av_mallocz(SDP_BUFFER_SIZE);
180 if (buff == NULL) {
181 return NULL;
182 }
183
184 memset(&s, 0, sizeof(struct sdp_session_level));
185 s.user = "-";
186 s.src_addr = "127.0.0.1"; /* FIXME: Properly set this */
187 s.name = ac[0]->title;
188
189 port = 0;
190 ttl = 0;
191 if (n_files == 1) {
192 port = get_address(dst, sizeof(dst), &ttl, ac[0]->filename);
193 if (port > 0) {
194 s.dst_addr = dst;
195 s.ttl = ttl;
196 }
197 }
198 sdp_write_header(buff, SDP_BUFFER_SIZE, &s);
199
200 dst[0] = 0;
201 for (i = 0; i < n_files; i++) {
202 if (n_files != 1) {
203 port = get_address(dst, sizeof(dst), &ttl, ac[i]->filename);
204 }
205 for (j = 0; j < ac[i]->nb_streams; j++) {
206 sdp_write_media(buff, SDP_BUFFER_SIZE,
207 ac[i]->streams[j]->codec, dst[0] ? dst : NULL,
208 (port > 0) ? port + j * 2 : 0, ttl);
209 if (port <= 0) {
210 av_strlcatf(buff, SDP_BUFFER_SIZE,
211 "a=control:streamid=%d\r\n", i + j);
212 }
213 }
214 }
215
216 return buff;
217}