]> code.delx.au - pulseaudio/blob - src/modules/rtp/sdp.c
Use pa_hashmap_remove_and_free() where appropriate
[pulseaudio] / src / modules / rtp / sdp.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2006 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <time.h>
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <string.h>
32
33 #include <pulse/xmalloc.h>
34 #include <pulse/util.h>
35
36 #include <pulsecore/core-util.h>
37 #include <pulsecore/log.h>
38 #include <pulsecore/macro.h>
39 #include <pulsecore/arpa-inet.h>
40
41 #include "sdp.h"
42 #include "rtp.h"
43
44 char *pa_sdp_build(int af, const void *src, const void *dst, const char *name, uint16_t port, uint8_t payload, const pa_sample_spec *ss) {
45 uint32_t ntp;
46 char buf_src[64], buf_dst[64], un[64];
47 const char *u, *f;
48
49 pa_assert(src);
50 pa_assert(dst);
51
52 #ifdef HAVE_IPV6
53 pa_assert(af == AF_INET || af == AF_INET6);
54 #else
55 pa_assert(af == AF_INET);
56 #endif
57
58 pa_assert_se(f = pa_rtp_format_to_string(ss->format));
59
60 if (!(u = pa_get_user_name(un, sizeof(un))))
61 u = "-";
62
63 ntp = (uint32_t) time(NULL) + 2208988800U;
64
65 pa_assert_se(inet_ntop(af, src, buf_src, sizeof(buf_src)));
66 pa_assert_se(inet_ntop(af, dst, buf_dst, sizeof(buf_dst)));
67
68 return pa_sprintf_malloc(
69 PA_SDP_HEADER
70 "o=%s %lu 0 IN %s %s\n"
71 "s=%s\n"
72 "c=IN %s %s\n"
73 "t=%lu 0\n"
74 "a=recvonly\n"
75 "m=audio %u RTP/AVP %i\n"
76 "a=rtpmap:%i %s/%u/%u\n"
77 "a=type:broadcast\n",
78 u, (unsigned long) ntp, af == AF_INET ? "IP4" : "IP6", buf_src,
79 name,
80 af == AF_INET ? "IP4" : "IP6", buf_dst,
81 (unsigned long) ntp,
82 port, payload,
83 payload, f, ss->rate, ss->channels);
84 }
85
86 static pa_sample_spec *parse_sdp_sample_spec(pa_sample_spec *ss, char *c) {
87 unsigned rate, channels;
88 pa_assert(ss);
89 pa_assert(c);
90
91 if (pa_startswith(c, "L16/")) {
92 ss->format = PA_SAMPLE_S16BE;
93 c += 4;
94 } else if (pa_startswith(c, "L8/")) {
95 ss->format = PA_SAMPLE_U8;
96 c += 3;
97 } else if (pa_startswith(c, "PCMA/")) {
98 ss->format = PA_SAMPLE_ALAW;
99 c += 5;
100 } else if (pa_startswith(c, "PCMU/")) {
101 ss->format = PA_SAMPLE_ULAW;
102 c += 5;
103 } else
104 return NULL;
105
106 if (sscanf(c, "%u/%u", &rate, &channels) == 2) {
107 ss->rate = (uint32_t) rate;
108 ss->channels = (uint8_t) channels;
109 } else if (sscanf(c, "%u", &rate) == 2) {
110 ss->rate = (uint32_t) rate;
111 ss->channels = 1;
112 } else
113 return NULL;
114
115 if (!pa_sample_spec_valid(ss))
116 return NULL;
117
118 return ss;
119 }
120
121 pa_sdp_info *pa_sdp_parse(const char *t, pa_sdp_info *i, int is_goodbye) {
122 uint16_t port = 0;
123 bool ss_valid = false;
124
125 pa_assert(t);
126 pa_assert(i);
127
128 i->origin = i->session_name = NULL;
129 i->salen = 0;
130 i->payload = 255;
131
132 if (!pa_startswith(t, PA_SDP_HEADER)) {
133 pa_log("Failed to parse SDP data: invalid header.");
134 goto fail;
135 }
136
137 t += sizeof(PA_SDP_HEADER)-1;
138
139 while (*t) {
140 size_t l;
141
142 l = strcspn(t, "\n");
143
144 if (l <= 2) {
145 pa_log("Failed to parse SDP data: line too short: >%s<.", t);
146 goto fail;
147 }
148
149 if (pa_startswith(t, "o="))
150 i->origin = pa_xstrndup(t+2, l-2);
151 else if (pa_startswith(t, "s="))
152 i->session_name = pa_xstrndup(t+2, l-2);
153 else if (pa_startswith(t, "c=IN IP4 ")) {
154 char a[64];
155 size_t k;
156
157 k = l-8 > sizeof(a) ? sizeof(a) : l-8;
158
159 pa_strlcpy(a, t+9, k);
160 a[strcspn(a, "/")] = 0;
161
162 if (inet_pton(AF_INET, a, &((struct sockaddr_in*) &i->sa)->sin_addr) <= 0) {
163 pa_log("Failed to parse SDP data: bad address: >%s<.", a);
164 goto fail;
165 }
166
167 ((struct sockaddr_in*) &i->sa)->sin_family = AF_INET;
168 ((struct sockaddr_in*) &i->sa)->sin_port = 0;
169 i->salen = sizeof(struct sockaddr_in);
170 #ifdef HAVE_IPV6
171 } else if (pa_startswith(t, "c=IN IP6 ")) {
172 char a[64];
173 size_t k;
174
175 k = l-8 > sizeof(a) ? sizeof(a) : l-8;
176
177 pa_strlcpy(a, t+9, k);
178 a[strcspn(a, "/")] = 0;
179
180 if (inet_pton(AF_INET6, a, &((struct sockaddr_in6*) &i->sa)->sin6_addr) <= 0) {
181 pa_log("Failed to parse SDP data: bad address: >%s<.", a);
182 goto fail;
183 }
184
185 ((struct sockaddr_in6*) &i->sa)->sin6_family = AF_INET6;
186 ((struct sockaddr_in6*) &i->sa)->sin6_port = 0;
187 i->salen = sizeof(struct sockaddr_in6);
188 #endif
189 } else if (pa_startswith(t, "m=audio ")) {
190
191 if (i->payload > 127) {
192 int _port, _payload;
193
194 if (sscanf(t+8, "%i RTP/AVP %i", &_port, &_payload) == 2) {
195
196 if (_port <= 0 || _port > 0xFFFF) {
197 pa_log("Failed to parse SDP data: invalid port %i.", _port);
198 goto fail;
199 }
200
201 if (_payload < 0 || _payload > 127) {
202 pa_log("Failed to parse SDP data: invalid payload %i.", _payload);
203 goto fail;
204 }
205
206 port = (uint16_t) _port;
207 i->payload = (uint8_t) _payload;
208
209 if (pa_rtp_sample_spec_from_payload(i->payload, &i->sample_spec))
210 ss_valid = true;
211 }
212 }
213 } else if (pa_startswith(t, "a=rtpmap:")) {
214
215 if (i->payload <= 127) {
216 char c[64];
217 int _payload;
218
219 if (sscanf(t+9, "%i %64c", &_payload, c) == 2) {
220
221 if (_payload < 0 || _payload > 127) {
222 pa_log("Failed to parse SDP data: invalid payload %i.", _payload);
223 goto fail;
224 }
225 if (_payload == i->payload) {
226
227 c[strcspn(c, "\n")] = 0;
228
229 if (parse_sdp_sample_spec(&i->sample_spec, c))
230 ss_valid = true;
231 }
232 }
233 }
234 }
235
236 t += l;
237
238 if (*t == '\n')
239 t++;
240 }
241
242 if (!i->origin || (!is_goodbye && (!i->salen || i->payload > 127 || !ss_valid || port == 0))) {
243 pa_log("Failed to parse SDP data: missing data.");
244 goto fail;
245 }
246
247 if (((struct sockaddr*) &i->sa)->sa_family == AF_INET)
248 ((struct sockaddr_in*) &i->sa)->sin_port = htons(port);
249 else
250 ((struct sockaddr_in6*) &i->sa)->sin6_port = htons(port);
251
252 return i;
253
254 fail:
255 pa_xfree(i->origin);
256 pa_xfree(i->session_name);
257
258 return NULL;
259 }
260
261 void pa_sdp_info_destroy(pa_sdp_info *i) {
262 pa_assert(i);
263
264 pa_xfree(i->origin);
265 pa_xfree(i->session_name);
266 }