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