Fix svn properties and some minor indentation
[pulseaudio-mirror.git] / src / modules / rtp / rtsp.c
blobd84407248fa4528fcf47255ff308c2760d920240
1 /* $Id$ */
3 /***
4 This file is part of PulseAudio.
6 Copyright 2008 Colin Guthrie
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
28 #include <fcntl.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <arpa/inet.h>
33 #include <unistd.h>
34 #include <sys/ioctl.h>
36 #ifdef HAVE_SYS_FILIO_H
37 #include <sys/filio.h>
38 #endif
40 #include <pulse/xmalloc.h>
42 #include <pulsecore/core-error.h>
43 #include <pulsecore/core-util.h>
44 #include <pulsecore/socket-util.h>
45 #include <pulsecore/log.h>
46 #include <pulsecore/macro.h>
47 #include <pulsecore/strbuf.h>
48 #include <pulsecore/poll.h>
50 #include "rtsp.h"
53 * read one line from the file descriptor
54 * timeout: msec unit, -1 for infinite
55 * if CR comes then following LF is expected
56 * returned string in line is always null terminated, maxlen-1 is maximum string length
58 static int pa_read_line(pa_iochannel* io, char *line, int maxlen, int timeout)
60 int i, rval;
61 int count;
62 int fd;
63 char ch;
64 struct pollfd pfds;
66 pa_assert(io);
67 fd = pa_iochannel_get_recv_fd(io);
69 count = 0;
70 *line = 0;
71 pfds.events = POLLIN;
72 pfds.fd = fd;
74 for (i=0; i<maxlen; ++i) {
75 if (!poll(&pfds, 1, timeout))
76 return 0;
78 rval = read(fd, &ch, 1);
80 if (-1 == rval) {
81 if (EAGAIN == errno)
82 return 0;
83 /*ERRMSG("%s:read error: %s\n", __func__, strerror(errno));*/
84 return -1;
87 if (0 == rval) {
88 /*INFMSG("%s:disconnected on the other end\n", __func__);*/
89 return -1;
92 if ('\n' == ch) {
93 *line = 0;
94 return count;
97 if ('\r' == ch)
98 continue;
100 *line++ = ch;
101 count++;
103 if (count >= maxlen-1)
104 break;
107 *line = 0;
108 return count;
112 static int pa_rtsp_exec(pa_rtsp_context* c, const char* cmd,
113 const char* content_type, const char* content,
114 int expect_response,
115 pa_headerlist* headers, pa_headerlist** response_headers) {
116 pa_strbuf* buf;
117 char* hdrs;
118 ssize_t l;
119 char response[1024];
120 int timeout;
121 char* token;
122 const char* token_state;
123 char delimiters[2];
124 char* header;
125 char* delimpos;
128 pa_assert(c);
129 pa_assert(c->url);
131 if (!cmd)
132 return -1;
134 buf = pa_strbuf_new();
135 pa_strbuf_printf(buf, "%s %s RTSP/1.0\r\nCSeq: %d\r\n", cmd, c->url, ++c->cseq);
136 if (c->session)
137 pa_strbuf_printf(buf, "Session: %s\r\n", c->session);
139 /* Add the headers */
140 if (headers) {
141 hdrs = pa_headerlist_to_string(headers);
142 pa_strbuf_puts(buf, hdrs);
143 pa_xfree(hdrs);
146 if (content_type && content) {
147 pa_strbuf_printf(buf, "Content-Type: %s\r\nContent-Length: %d\r\n",
148 content_type, (int)strlen(content));
151 pa_strbuf_printf(buf, "User-Agent: %s\r\n", c->useragent);
153 if (c->headers) {
154 hdrs = pa_headerlist_to_string(c->headers);
155 pa_strbuf_puts(buf, hdrs);
156 pa_xfree(hdrs);
159 pa_strbuf_puts(buf, "\r\n");
161 if (content_type && content) {
162 pa_strbuf_puts(buf, content);
165 /* Our packet is created... now we can send it :) */
166 hdrs = pa_strbuf_tostring_free(buf);
167 l = pa_iochannel_write(c->io, hdrs, strlen(hdrs));
168 pa_xfree(hdrs);
170 /* Do we expect a response? */
171 if (!expect_response)
172 return 0;
174 timeout = 5000;
175 if (pa_read_line(c->io, response, sizeof(response), timeout) <= 0) {
176 /*ERRMSG("%s: request failed\n",__func__);*/
177 return -1;
180 delimiters[0] = ' ';
181 delimiters[1] = '\0';
182 token_state = NULL;
183 pa_xfree(pa_split(response, delimiters, &token_state));
184 token = pa_split(response, delimiters, &token_state);
185 if (!token || strcmp(token, "200")) {
186 pa_xfree(token);
187 /*ERRMSG("%s: request failed, error %s\n",__func__,token);*/
188 return -1;
190 pa_xfree(token);
192 /* We want to return the headers? */
193 if (!response_headers)
195 /* We have no storage, so just clear out the response. */
196 while (pa_read_line(c->io, response, sizeof(response), timeout) > 0) {
197 /* Reduce timeout for future requests */
198 timeout = 1000;
200 return 0;
203 /* TODO: Move header reading into the headerlist. */
204 header = NULL;
205 buf = pa_strbuf_new();
206 while (pa_read_line(c->io, response, sizeof(response), timeout) > 0) {
207 /* Reduce timeout for future requests */
208 timeout = 1000;
210 /* If the first character is a space, it's a continuation header */
211 if (header && ' ' == response[0]) {
212 /* Add this line to the buffer (sans the space. */
213 pa_strbuf_puts(buf, &(response[1]));
214 continue;
217 if (header) {
218 /* This is not a continuation header so let's dump the full
219 header/value into our proplist */
220 pa_headerlist_puts(*response_headers, header, pa_strbuf_tostring_free(buf));
221 pa_xfree(header);
222 buf = pa_strbuf_new();
225 delimpos = strstr(response, ":");
226 if (!delimpos) {
227 /*ERRMSG("%s: Request failed, bad header\n",__func__);*/
228 return -1;
231 if (strlen(delimpos) > 1) {
232 /* Cut our line off so we can copy the header name out */
233 *delimpos++ = '\0';
235 /* Trim the front of any spaces */
236 while (' ' == *delimpos)
237 ++delimpos;
239 pa_strbuf_puts(buf, delimpos);
240 } else {
241 /* Cut our line off so we can copy the header name out */
242 *delimpos = '\0';
245 /* Save the header name */
246 header = pa_xstrdup(response);
248 /* We will have a header left from our looping itteration, so add it in :) */
249 if (header) {
250 /* This is not a continuation header so let's dump it into our proplist */
251 pa_headerlist_puts(*response_headers, header, pa_strbuf_tostring(buf));
253 pa_strbuf_free(buf);
255 return 0;
259 pa_rtsp_context* pa_rtsp_context_new(const char* useragent) {
260 pa_rtsp_context *c;
262 c = pa_xnew0(pa_rtsp_context, 1);
263 c->headers = pa_headerlist_new();
265 if (useragent)
266 c->useragent = useragent;
267 else
268 c->useragent = "PulseAudio RTSP Client";
270 return c;
274 void pa_rtsp_context_free(pa_rtsp_context* c) {
275 if (c) {
276 if (c->sc)
277 pa_socket_client_unref(c->sc);
279 pa_xfree(c->url);
280 pa_xfree(c->localip);
281 pa_xfree(c->session);
282 pa_xfree(c->transport);
283 pa_headerlist_free(c->headers);
285 pa_xfree(c);
290 static void on_connection(pa_socket_client *sc, pa_iochannel *io, void *userdata) {
291 pa_rtsp_context *c = userdata;
292 union {
293 struct sockaddr sa;
294 struct sockaddr_in in;
295 struct sockaddr_in6 in6;
296 } sa;
297 socklen_t sa_len = sizeof(sa);
299 pa_assert(sc);
300 pa_assert(c);
301 pa_assert(c->sc == sc);
303 pa_socket_client_unref(c->sc);
304 c->sc = NULL;
306 if (!io) {
307 pa_log("Connection failed: %s", pa_cstrerror(errno));
308 return;
310 pa_assert(!c->io);
311 c->io = io;
313 /* Get the local IP address for use externally */
314 if (0 == getsockname(pa_iochannel_get_recv_fd(io), &sa.sa, &sa_len)) {
315 char buf[INET6_ADDRSTRLEN];
316 const char *res = NULL;
318 if (AF_INET == sa.sa.sa_family) {
319 res = inet_ntop(sa.sa.sa_family, &sa.in.sin_addr, buf, sizeof(buf));
320 } else if (AF_INET6 == sa.sa.sa_family) {
321 res = inet_ntop(AF_INET6, &sa.in6.sin6_addr, buf, sizeof(buf));
323 if (res)
324 c->localip = pa_xstrdup(res);
328 int pa_rtsp_connect(pa_rtsp_context *c, pa_mainloop_api *mainloop, const char* hostname, uint16_t port) {
329 pa_assert(c);
330 pa_assert(mainloop);
331 pa_assert(hostname);
332 pa_assert(port > 0);
334 if (!(c->sc = pa_socket_client_new_string(mainloop, hostname, port))) {
335 pa_log("failed to connect to server '%s:%d'", hostname, port);
336 return -1;
339 pa_socket_client_set_callback(c->sc, on_connection, c);
340 return 0;
344 void pa_rtsp_disconnect(pa_rtsp_context *c) {
345 pa_assert(c);
347 if (c->io)
348 pa_iochannel_free(c->io);
349 c->io = NULL;
353 const char* pa_rtsp_localip(pa_rtsp_context* c) {
354 pa_assert(c);
356 return c->localip;
360 void pa_rtsp_set_url(pa_rtsp_context* c, const char* url) {
361 pa_assert(c);
363 c->url = pa_xstrdup(url);
366 int pa_rtsp_announce(pa_rtsp_context *c, const char* sdp) {
367 pa_assert(c);
368 if (!sdp)
369 return -1;
371 return pa_rtsp_exec(c, "ANNOUNCE", "application/sdp", sdp, 1, NULL, NULL);
375 int pa_rtsp_setup(pa_rtsp_context* c, pa_headerlist** response_headers) {
376 pa_headerlist* headers;
377 pa_headerlist* rheaders;
378 char delimiters[2];
379 char* token;
380 const char* token_state;
381 const char* pc;
383 pa_assert(c);
385 headers = pa_headerlist_new();
386 rheaders = pa_headerlist_new();
387 pa_headerlist_puts(headers, "Transport", "RTP/AVP/TCP;unicast;interleaved=0-1;mode=record");
389 if (pa_rtsp_exec(c, "SETUP", NULL, NULL, 1, headers, &rheaders)) {
390 pa_headerlist_free(headers);
391 pa_headerlist_free(rheaders);
392 return -1;
394 pa_headerlist_free(headers);
396 c->session = pa_xstrdup(pa_headerlist_gets(rheaders, "Session"));
397 c->transport = pa_xstrdup(pa_headerlist_gets(rheaders, "Transport"));
399 if (!c->session || !c->transport) {
400 pa_headerlist_free(rheaders);
401 return -1;
404 /* Now parse out the server port component of the response. */
405 c->port = 0;
406 delimiters[0] = ';';
407 delimiters[1] = '\0';
408 token_state = NULL;
409 while ((token = pa_split(c->transport, delimiters, &token_state))) {
410 if ((pc = strstr(token, "="))) {
411 if (0 == strncmp(token, "server_port", 11)) {
412 pa_atou(pc+1, &c->port);
413 pa_xfree(token);
414 break;
417 pa_xfree(token);
419 if (0 == c->port) {
420 /* Error no server_port in response */
421 pa_headerlist_free(rheaders);
422 return -1;
425 *response_headers = rheaders;
426 return 0;
430 int pa_rtsp_record(pa_rtsp_context* c) {
431 pa_headerlist* headers;
432 int rv;
434 pa_assert(c);
435 if (!c->session) {
436 /* No seesion in progres */
437 return -1;
440 headers = pa_headerlist_new();
441 pa_headerlist_puts(headers, "Range", "npt=0-");
442 pa_headerlist_puts(headers, "RTP-Info", "seq=0;rtptime=0");
444 rv = pa_rtsp_exec(c, "RECORD", NULL, NULL, 1, headers, NULL);
445 pa_headerlist_free(headers);
446 return rv;
450 int pa_rtsp_teardown(pa_rtsp_context *c) {
451 pa_assert(c);
453 return pa_rtsp_exec(c, "TEARDOWN", NULL, NULL, 0, NULL, NULL);
457 int pa_rtsp_setparameter(pa_rtsp_context *c, const char* param) {
458 pa_assert(c);
459 if (!param)
460 return -1;
462 return pa_rtsp_exec(c, "SET_PARAMETER", "text/parameters", param, 1, NULL, NULL);
466 int pa_rtsp_flush(pa_rtsp_context *c) {
467 pa_headerlist* headers;
468 int rv;
470 pa_assert(c);
472 headers = pa_headerlist_new();
473 pa_headerlist_puts(headers, "RTP-Info", "seq=0;rtptime=0");
475 rv = pa_rtsp_exec(c, "FLUSH", NULL, NULL, 1, headers, NULL);
476 pa_headerlist_free(headers);
477 return rv;