]> code.delx.au - pulseaudio/blob - src/modules/rtp/rtsp_client.c
Remove pa_bool_t and replace it with bool.
[pulseaudio] / src / modules / rtp / rtsp_client.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2008 Colin Guthrie
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 <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <sys/ioctl.h>
31 #include <netinet/in.h>
32
33 #ifdef HAVE_SYS_FILIO_H
34 #include <sys/filio.h>
35 #endif
36
37 #include <pulse/xmalloc.h>
38
39 #include <pulsecore/core-error.h>
40 #include <pulsecore/core-util.h>
41 #include <pulsecore/log.h>
42 #include <pulsecore/macro.h>
43 #include <pulsecore/strbuf.h>
44 #include <pulsecore/ioline.h>
45 #include <pulsecore/arpa-inet.h>
46
47 #include "rtsp_client.h"
48
49 struct pa_rtsp_client {
50 pa_mainloop_api *mainloop;
51 char *hostname;
52 uint16_t port;
53
54 pa_socket_client *sc;
55 pa_ioline *ioline;
56
57 pa_rtsp_cb_t callback;
58
59 void *userdata;
60 const char *useragent;
61
62 pa_rtsp_state state;
63 uint8_t waiting;
64
65 pa_headerlist* headers;
66 char *last_header;
67 pa_strbuf *header_buffer;
68 pa_headerlist* response_headers;
69
70 char *localip;
71 char *url;
72 uint16_t rtp_port;
73 uint32_t cseq;
74 char *session;
75 char *transport;
76 };
77
78 pa_rtsp_client* pa_rtsp_client_new(pa_mainloop_api *mainloop, const char* hostname, uint16_t port, const char* useragent) {
79 pa_rtsp_client *c;
80
81 pa_assert(mainloop);
82 pa_assert(hostname);
83 pa_assert(port > 0);
84
85 c = pa_xnew0(pa_rtsp_client, 1);
86 c->mainloop = mainloop;
87 c->hostname = pa_xstrdup(hostname);
88 c->port = port;
89 c->headers = pa_headerlist_new();
90
91 if (useragent)
92 c->useragent = useragent;
93 else
94 c->useragent = "PulseAudio RTSP Client";
95
96 return c;
97 }
98
99 void pa_rtsp_client_free(pa_rtsp_client* c) {
100 pa_assert(c);
101
102 if (c->sc)
103 pa_socket_client_unref(c->sc);
104
105 pa_rtsp_disconnect(c);
106
107 pa_xfree(c->hostname);
108 pa_xfree(c->url);
109 pa_xfree(c->localip);
110 pa_xfree(c->session);
111 pa_xfree(c->transport);
112 pa_xfree(c->last_header);
113 if (c->header_buffer)
114 pa_strbuf_free(c->header_buffer);
115 if (c->response_headers)
116 pa_headerlist_free(c->response_headers);
117 pa_headerlist_free(c->headers);
118
119 pa_xfree(c);
120 }
121
122 static void headers_read(pa_rtsp_client *c) {
123 char* token;
124 char delimiters[] = ";";
125
126 pa_assert(c);
127 pa_assert(c->response_headers);
128 pa_assert(c->callback);
129
130 /* Deal with a SETUP response */
131 if (STATE_SETUP == c->state) {
132 const char* token_state = NULL;
133 const char* pc = NULL;
134 c->session = pa_xstrdup(pa_headerlist_gets(c->response_headers, "Session"));
135 c->transport = pa_xstrdup(pa_headerlist_gets(c->response_headers, "Transport"));
136
137 if (!c->session || !c->transport) {
138 pa_log("Invalid SETUP response.");
139 return;
140 }
141
142 /* Now parse out the server port component of the response. */
143 while ((token = pa_split(c->transport, delimiters, &token_state))) {
144 if ((pc = strchr(token, '='))) {
145 if (0 == strncmp(token, "server_port", 11)) {
146 uint32_t p;
147
148 if (pa_atou(pc + 1, &p) < 0 || p <= 0 || p > 0xffff) {
149 pa_log("Invalid SETUP response (invalid server_port).");
150 pa_xfree(token);
151 return;
152 }
153
154 c->rtp_port = p;
155 pa_xfree(token);
156 break;
157 }
158 }
159 pa_xfree(token);
160 }
161 if (0 == c->rtp_port) {
162 /* Error no server_port in response */
163 pa_log("Invalid SETUP response (no port number).");
164 return;
165 }
166 }
167
168 /* Call our callback */
169 c->callback(c, c->state, c->response_headers, c->userdata);
170 }
171
172 static void line_callback(pa_ioline *line, const char *s, void *userdata) {
173 char *delimpos;
174 char *s2, *s2p;
175
176 pa_rtsp_client *c = userdata;
177 pa_assert(line);
178 pa_assert(c);
179 pa_assert(c->callback);
180
181 if (!s) {
182 /* Keep the ioline/iochannel open as they will be freed automatically */
183 c->ioline = NULL;
184 c->callback(c, STATE_DISCONNECTED, NULL, c->userdata);
185 return;
186 }
187
188 s2 = pa_xstrdup(s);
189 /* Trim trailing carriage returns */
190 s2p = s2 + strlen(s2) - 1;
191 while (s2p >= s2 && '\r' == *s2p) {
192 *s2p = '\0';
193 s2p -= 1;
194 }
195 if (c->waiting && pa_streq(s2, "RTSP/1.0 200 OK")) {
196 c->waiting = 0;
197 if (c->response_headers)
198 pa_headerlist_free(c->response_headers);
199 c->response_headers = pa_headerlist_new();
200 goto exit;
201 }
202 if (c->waiting) {
203 pa_log_warn("Unexpected response: %s", s2);
204 goto exit;;
205 }
206 if (!strlen(s2)) {
207 /* End of headers */
208 /* We will have a header left from our looping iteration, so add it in :) */
209 if (c->last_header) {
210 char *tmp = pa_strbuf_tostring_free(c->header_buffer);
211 /* This is not a continuation header so let's dump it into our proplist */
212 pa_headerlist_puts(c->response_headers, c->last_header, tmp);
213 pa_xfree(tmp);
214 pa_xfree(c->last_header);
215 c->last_header = NULL;
216 c->header_buffer = NULL;
217 }
218
219 pa_log_debug("Full response received. Dispatching");
220 headers_read(c);
221 c->waiting = 1;
222 goto exit;
223 }
224
225 /* Read and parse a header (we know it's not empty) */
226 /* TODO: Move header reading into the headerlist. */
227
228 /* If the first character is a space, it's a continuation header */
229 if (c->last_header && ' ' == s2[0]) {
230 pa_assert(c->header_buffer);
231
232 /* Add this line to the buffer (sans the space. */
233 pa_strbuf_puts(c->header_buffer, &(s2[1]));
234 goto exit;
235 }
236
237 if (c->last_header) {
238 char *tmp = pa_strbuf_tostring_free(c->header_buffer);
239 /* This is not a continuation header so let's dump the full
240 header/value into our proplist */
241 pa_headerlist_puts(c->response_headers, c->last_header, tmp);
242 pa_xfree(tmp);
243 pa_xfree(c->last_header);
244 c->last_header = NULL;
245 c->header_buffer = NULL;
246 }
247
248 delimpos = strstr(s2, ":");
249 if (!delimpos) {
250 pa_log_warn("Unexpected response when expecting header: %s", s);
251 goto exit;
252 }
253
254 pa_assert(!c->header_buffer);
255 pa_assert(!c->last_header);
256
257 c->header_buffer = pa_strbuf_new();
258 if (strlen(delimpos) > 1) {
259 /* Cut our line off so we can copy the header name out */
260 *delimpos++ = '\0';
261
262 /* Trim the front of any spaces */
263 while (' ' == *delimpos)
264 ++delimpos;
265
266 pa_strbuf_puts(c->header_buffer, delimpos);
267 } else {
268 /* Cut our line off so we can copy the header name out */
269 *delimpos = '\0';
270 }
271
272 /* Save the header name */
273 c->last_header = pa_xstrdup(s2);
274 exit:
275 pa_xfree(s2);
276 }
277
278 static void on_connection(pa_socket_client *sc, pa_iochannel *io, void *userdata) {
279 pa_rtsp_client *c = userdata;
280 union {
281 struct sockaddr sa;
282 struct sockaddr_in in;
283 struct sockaddr_in6 in6;
284 } sa;
285 socklen_t sa_len = sizeof(sa);
286
287 pa_assert(sc);
288 pa_assert(c);
289 pa_assert(STATE_CONNECT == c->state);
290 pa_assert(c->sc == sc);
291 pa_socket_client_unref(c->sc);
292 c->sc = NULL;
293
294 if (!io) {
295 pa_log("Connection failed: %s", pa_cstrerror(errno));
296 return;
297 }
298 pa_assert(!c->ioline);
299
300 c->ioline = pa_ioline_new(io);
301 pa_ioline_set_callback(c->ioline, line_callback, c);
302
303 /* Get the local IP address for use externally */
304 if (0 == getsockname(pa_iochannel_get_recv_fd(io), &sa.sa, &sa_len)) {
305 char buf[INET6_ADDRSTRLEN];
306 const char *res = NULL;
307
308 if (AF_INET == sa.sa.sa_family) {
309 if ((res = inet_ntop(sa.sa.sa_family, &sa.in.sin_addr, buf, sizeof(buf)))) {
310 c->localip = pa_xstrdup(res);
311 }
312 } else if (AF_INET6 == sa.sa.sa_family) {
313 if ((res = inet_ntop(AF_INET6, &sa.in6.sin6_addr, buf, sizeof(buf)))) {
314 c->localip = pa_sprintf_malloc("[%s]", res);
315 }
316 }
317 }
318 pa_log_debug("Established RTSP connection from local ip %s", c->localip);
319
320 if (c->callback)
321 c->callback(c, c->state, NULL, c->userdata);
322 }
323
324 int pa_rtsp_connect(pa_rtsp_client *c) {
325 pa_assert(c);
326 pa_assert(!c->sc);
327
328 pa_xfree(c->session);
329 c->session = NULL;
330
331 pa_log_debug("Attempting to connect to server '%s:%d'", c->hostname, c->port);
332 if (!(c->sc = pa_socket_client_new_string(c->mainloop, true, c->hostname, c->port))) {
333 pa_log("failed to connect to server '%s:%d'", c->hostname, c->port);
334 return -1;
335 }
336
337 pa_socket_client_set_callback(c->sc, on_connection, c);
338 c->waiting = 1;
339 c->state = STATE_CONNECT;
340 return 0;
341 }
342
343 void pa_rtsp_set_callback(pa_rtsp_client *c, pa_rtsp_cb_t callback, void *userdata) {
344 pa_assert(c);
345
346 c->callback = callback;
347 c->userdata = userdata;
348 }
349
350 void pa_rtsp_disconnect(pa_rtsp_client *c) {
351 pa_assert(c);
352
353 if (c->ioline)
354 pa_ioline_close(c->ioline);
355 c->ioline = NULL;
356 }
357
358 const char* pa_rtsp_localip(pa_rtsp_client* c) {
359 pa_assert(c);
360
361 return c->localip;
362 }
363
364 uint32_t pa_rtsp_serverport(pa_rtsp_client* c) {
365 pa_assert(c);
366
367 return c->rtp_port;
368 }
369
370 void pa_rtsp_set_url(pa_rtsp_client* c, const char* url) {
371 pa_assert(c);
372
373 c->url = pa_xstrdup(url);
374 }
375
376 void pa_rtsp_add_header(pa_rtsp_client *c, const char* key, const char* value) {
377 pa_assert(c);
378 pa_assert(key);
379 pa_assert(value);
380
381 pa_headerlist_puts(c->headers, key, value);
382 }
383
384 void pa_rtsp_remove_header(pa_rtsp_client *c, const char* key) {
385 pa_assert(c);
386 pa_assert(key);
387
388 pa_headerlist_remove(c->headers, key);
389 }
390
391 static int rtsp_exec(pa_rtsp_client* c, const char* cmd,
392 const char* content_type, const char* content,
393 int expect_response,
394 pa_headerlist* headers) {
395 pa_strbuf* buf;
396 char* hdrs;
397
398 pa_assert(c);
399 pa_assert(c->url);
400 pa_assert(cmd);
401 pa_assert(c->ioline);
402
403 pa_log_debug("Sending command: %s", cmd);
404
405 buf = pa_strbuf_new();
406 pa_strbuf_printf(buf, "%s %s RTSP/1.0\r\nCSeq: %d\r\n", cmd, c->url, ++c->cseq);
407 if (c->session)
408 pa_strbuf_printf(buf, "Session: %s\r\n", c->session);
409
410 /* Add the headers */
411 if (headers) {
412 hdrs = pa_headerlist_to_string(headers);
413 pa_strbuf_puts(buf, hdrs);
414 pa_xfree(hdrs);
415 }
416
417 if (content_type && content) {
418 pa_strbuf_printf(buf, "Content-Type: %s\r\nContent-Length: %d\r\n",
419 content_type, (int)strlen(content));
420 }
421
422 pa_strbuf_printf(buf, "User-Agent: %s\r\n", c->useragent);
423
424 if (c->headers) {
425 hdrs = pa_headerlist_to_string(c->headers);
426 pa_strbuf_puts(buf, hdrs);
427 pa_xfree(hdrs);
428 }
429
430 pa_strbuf_puts(buf, "\r\n");
431
432 if (content_type && content) {
433 pa_strbuf_puts(buf, content);
434 }
435
436 /* Our packet is created... now we can send it :) */
437 hdrs = pa_strbuf_tostring_free(buf);
438 /*pa_log_debug("Submitting request:");
439 pa_log_debug(hdrs);*/
440 pa_ioline_puts(c->ioline, hdrs);
441 pa_xfree(hdrs);
442
443 return 0;
444 }
445
446 int pa_rtsp_announce(pa_rtsp_client *c, const char* sdp) {
447 pa_assert(c);
448 if (!sdp)
449 return -1;
450
451 c->state = STATE_ANNOUNCE;
452 return rtsp_exec(c, "ANNOUNCE", "application/sdp", sdp, 1, NULL);
453 }
454
455 int pa_rtsp_setup(pa_rtsp_client* c) {
456 pa_headerlist* headers;
457 int rv;
458
459 pa_assert(c);
460
461 headers = pa_headerlist_new();
462 pa_headerlist_puts(headers, "Transport", "RTP/AVP/TCP;unicast;interleaved=0-1;mode=record");
463
464 c->state = STATE_SETUP;
465 rv = rtsp_exec(c, "SETUP", NULL, NULL, 1, headers);
466 pa_headerlist_free(headers);
467 return rv;
468 }
469
470 int pa_rtsp_record(pa_rtsp_client* c, uint16_t* seq, uint32_t* rtptime) {
471 pa_headerlist* headers;
472 int rv;
473 char *info;
474
475 pa_assert(c);
476 if (!c->session) {
477 /* No session in progress */
478 return -1;
479 }
480
481 /* Todo: Generate these values randomly as per spec */
482 *seq = *rtptime = 0;
483
484 headers = pa_headerlist_new();
485 pa_headerlist_puts(headers, "Range", "npt=0-");
486 info = pa_sprintf_malloc("seq=%u;rtptime=%u", *seq, *rtptime);
487 pa_headerlist_puts(headers, "RTP-Info", info);
488 pa_xfree(info);
489
490 c->state = STATE_RECORD;
491 rv = rtsp_exec(c, "RECORD", NULL, NULL, 1, headers);
492 pa_headerlist_free(headers);
493 return rv;
494 }
495
496 int pa_rtsp_teardown(pa_rtsp_client *c) {
497 pa_assert(c);
498
499 c->state = STATE_TEARDOWN;
500 return rtsp_exec(c, "TEARDOWN", NULL, NULL, 0, NULL);
501 }
502
503 int pa_rtsp_setparameter(pa_rtsp_client *c, const char* param) {
504 pa_assert(c);
505 if (!param)
506 return -1;
507
508 c->state = STATE_SET_PARAMETER;
509 return rtsp_exec(c, "SET_PARAMETER", "text/parameters", param, 1, NULL);
510 }
511
512 int pa_rtsp_flush(pa_rtsp_client *c, uint16_t seq, uint32_t rtptime) {
513 pa_headerlist* headers;
514 int rv;
515 char *info;
516
517 pa_assert(c);
518
519 headers = pa_headerlist_new();
520 info = pa_sprintf_malloc("seq=%u;rtptime=%u", seq, rtptime);
521 pa_headerlist_puts(headers, "RTP-Info", info);
522 pa_xfree(info);
523
524 c->state = STATE_FLUSH;
525 rv = rtsp_exec(c, "FLUSH", NULL, NULL, 1, headers);
526 pa_headerlist_free(headers);
527 return rv;
528 }