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