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