Add a RTSP client impelmentation.
[pulseaudio-mirror.git] / src / modules / rtp / rtsp.c
blob3fd1ba0f2b072a0a976b8dfd4296360c43f97a0b
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(int fd, char *line, int maxlen, int timeout)
60 int i, rval;
61 int count;
62 char ch;
63 struct pollfd pfds;
64 count = 0;
65 *line = 0;
66 pfds.events = POLLIN;
67 pfds.fd = fd;
69 for (i=0; i<maxlen; ++i) {
70 if (!poll(&pfds, 1, timeout))
71 return 0;
73 rval = read(fd, &ch, 1);
75 if (-1 == rval) {
76 if (EAGAIN == errno)
77 return 0;
78 //ERRMSG("%s:read error: %s\n", __func__, strerror(errno));
79 return -1;
82 if (0 == rval) {
83 //INFMSG("%s:disconnected on the other end\n", __func__);
84 return -1;
87 if ('\n' == ch) {
88 *line = 0;
89 return count;
92 if ('\r' == ch)
93 continue;
95 *line++ = ch;
96 count++;
98 if (count >= maxlen-1)
99 break;
102 *line = 0;
103 return count;
107 static int pa_rtsp_exec(pa_rtsp_context* c, const char* cmd,
108 const char* content_type, const char* content,
109 int expect_response,
110 pa_headerlist* headers, pa_headerlist** response_headers) {
111 pa_strbuf* buf;
112 char* hdrs;
113 ssize_t l;
114 char response[1024];
115 int timeout;
116 char* token;
117 const char* token_state;
118 char delimiters[2];
119 char* header;
120 char* delimpos;
123 pa_assert(c);
124 pa_assert(c->url);
126 if (!cmd)
127 return 0;
129 buf = pa_strbuf_new();
130 pa_strbuf_printf(buf, "%s %s RTSP/1.0\r\nCSeq: %d\r\n", cmd, c->url, ++c->cseq);
131 if (c->session)
132 pa_strbuf_printf(buf, "Session: %s\r\n", c->session);
134 // Add the headers
135 if (headers) {
136 hdrs = pa_headerlist_to_string(headers);
137 pa_strbuf_puts(buf, hdrs);
138 pa_xfree(hdrs);
141 if (content_type && content) {
142 pa_strbuf_printf(buf, "Content-Type: %s\r\nContent-Length: %d\r\n",
143 content_type, (int)strlen(content));
146 pa_strbuf_printf(buf, "User-Agent: %s\r\n", c->useragent);
148 if (c->headers) {
149 hdrs = pa_headerlist_to_string(c->headers);
150 pa_strbuf_puts(buf, hdrs);
151 pa_xfree(hdrs);
154 pa_strbuf_puts(buf, "\r\n");
156 if (content_type && content) {
157 pa_strbuf_puts(buf, content);
160 // Our packet is created... now we can send it :)
161 hdrs = pa_strbuf_tostring_free(buf);
162 l = pa_write(c->fd, hdrs, strlen(hdrs), NULL);
163 pa_xfree(hdrs);
165 // Do we expect a response?
166 if (!expect_response)
167 return 1;
169 timeout = 5000;
170 if (pa_read_line(c->fd, response, sizeof(response), timeout) <= 0) {
171 //ERRMSG("%s: request failed\n",__func__);
172 return 0;
175 delimiters[0] = ' ';
176 delimiters[1] = '\0';
177 token_state = NULL;
178 pa_xfree(pa_split(response, delimiters, &token_state));
179 token = pa_split(response, delimiters, &token_state);
180 if (!token || strcmp(token, "200")) {
181 pa_xfree(token);
182 //ERRMSG("%s: request failed, error %s\n",__func__,token);
183 return 0;
185 pa_xfree(token);
187 // We want to return the headers?
188 if (!response_headers)
190 // We have no storage, so just clear out the response.
191 while (pa_read_line(c->fd, response, sizeof(response), timeout) > 0) {
192 // Reduce timeout for future requests
193 timeout = 1000;
195 return 1;
198 header = NULL;
199 buf = pa_strbuf_new();
200 while (pa_read_line(c->fd, response, sizeof(response), timeout) > 0) {
201 // Reduce timeout for future requests
202 timeout = 1000;
204 // If the first character is a space, it's a continuation header
205 if (header && ' ' == response[0]) {
206 // Add this line to the buffer (sans the space.
207 pa_strbuf_puts(buf, &(response[1]));
208 continue;
211 if (header) {
212 // This is not a continuation header so let's dump the full header/value into our proplist
213 pa_headerlist_puts(*response_headers, header, pa_strbuf_tostring_free(buf));
214 pa_xfree(header);
215 //header = NULL;
216 buf = pa_strbuf_new();
219 delimpos = strstr(response, ":");
220 if (!delimpos) {
221 //ERRMSG("%s: Request failed, bad header\n",__func__);
222 return 0;
225 if (strlen(delimpos) > 1) {
226 // Cut our line off so we can copy the header name out
227 *delimpos++ = '\0';
229 // Trim the front of any spaces
230 while (' ' == *delimpos)
231 ++delimpos;
233 pa_strbuf_puts(buf, delimpos);
234 } else {
235 // Cut our line off so we can copy the header name out
236 *delimpos = '\0';
239 // Save the header name
240 header = pa_xstrdup(response);
242 // We will have a header left from our looping itteration, so add it in :)
243 if (header) {
244 // This is not a continuation header so let's dump it into our proplist
245 pa_headerlist_puts(*response_headers, header, pa_strbuf_tostring(buf));
247 pa_strbuf_free(buf);
249 return 1;
253 pa_rtsp_context* pa_rtsp_context_new(const char* useragent) {
254 pa_rtsp_context *c;
256 c = pa_xnew0(pa_rtsp_context, 1);
257 c->fd = -1;
258 c->headers = pa_headerlist_new();
260 if (useragent)
261 c->useragent = useragent;
262 else
263 c->useragent = "PulseAudio RTSP Client";
265 return c;
269 void pa_rtsp_context_destroy(pa_rtsp_context* c) {
270 if (c) {
271 pa_xfree(c->url);
272 pa_xfree(c->session);
273 pa_xfree(c->transport);
274 pa_headerlist_free(c->headers);
276 pa_xfree(c);
280 int pa_rtsp_connect(pa_rtsp_context *c, const char* hostname, uint16_t port, const char* sid) {
281 struct sockaddr_in sa;
282 struct sockaddr_in name;
283 socklen_t namelen = sizeof(name);
284 struct hostent *host = NULL;
285 int r;
287 pa_assert(c);
288 pa_assert(hostname);
289 pa_assert(port > 0);
290 pa_assert(sid);
292 memset(&sa, 0, sizeof(sa));
293 sa.sin_family = AF_INET;
294 sa.sin_port = htons(port);
296 host = gethostbyname(hostname);
297 if (!host) {
298 unsigned int addr = inet_addr(hostname);
299 if (addr != INADDR_NONE)
300 host = gethostbyaddr((char*)&addr, 4, AF_INET);
301 if (!host)
302 return 0;
304 memcpy(&sa.sin_addr, host->h_addr, sizeof(struct in_addr));
306 if ((c->fd = socket(sa.sin_family, SOCK_STREAM, 0)) < 0) {
307 pa_log("socket(): %s", pa_cstrerror(errno));
308 return 0;
311 // Q: is FD_CLOEXEC reqd?
312 pa_make_fd_cloexec(c->fd);
313 pa_make_tcp_socket_low_delay(c->fd);
315 if ((r = connect(c->fd, &sa, sizeof(struct sockaddr_in))) < 0) {
316 #ifdef OS_IS_WIN32
317 if (WSAGetLastError() != EWOULDBLOCK) {
318 pa_log_debug("connect(): %d", WSAGetLastError());
319 #else
320 if (errno != EINPROGRESS) {
321 pa_log_debug("connect(): %s (%d)", pa_cstrerror(errno), errno);
322 #endif
323 pa_close(c->fd);
324 c->fd = -1;
325 return 0;
329 if (0 != getsockname(c->fd, (struct sockaddr*)&name, &namelen)) {
330 pa_close(c->fd);
331 c->fd = -1;
332 return 0;
334 memcpy(&c->local_addr, &name.sin_addr, sizeof(struct in_addr));
335 c->url = pa_sprintf_malloc("rtsp://%s/%s", inet_ntoa(name.sin_addr), sid);
337 return 1;
341 void pa_rtsp_disconnect(pa_rtsp_context *c) {
342 pa_assert(c);
344 if (c->fd < 0)
345 return;
346 pa_close(c->fd);
347 c->fd = -1;
351 const char* pa_rtsp_localip(pa_rtsp_context* c) {
352 pa_assert(c);
354 if (c->fd < 0)
355 return NULL;
356 return inet_ntoa(c->local_addr);
360 int pa_rtsp_announce(pa_rtsp_context *c, const char* sdp) {
361 pa_assert(c);
362 if (!sdp)
363 return 0;
365 return pa_rtsp_exec(c, "ANNOUNCE", "application/sdp", sdp, 1, NULL, NULL);
369 int pa_rtsp_setup(pa_rtsp_context* c, pa_headerlist** response_headers) {
370 pa_headerlist* headers;
371 pa_headerlist* rheaders;
372 char delimiters[2];
373 char* token;
374 const char* token_state;
375 const char* pc;
377 pa_assert(c);
379 headers = pa_headerlist_new();
380 rheaders = pa_headerlist_new();
381 pa_headerlist_puts(headers, "Transport", "RTP/AVP/TCP;unicast;interleaved=0-1;mode=record");
383 if (!pa_rtsp_exec(c, "SETUP", NULL, NULL, 1, headers, &rheaders)) {
384 pa_headerlist_free(headers);
385 pa_headerlist_free(rheaders);
386 return 0;
388 pa_headerlist_free(headers);
390 c->session = pa_xstrdup(pa_headerlist_gets(rheaders, "Session"));
391 c->transport = pa_xstrdup(pa_headerlist_gets(rheaders, "Transport"));
393 if (!c->session || !c->transport) {
394 pa_headerlist_free(rheaders);
395 return 0;
398 // Now parse out the server port component of the response.
399 c->port = 0;
400 delimiters[0] = ';';
401 delimiters[1] = '\0';
402 token_state = NULL;
403 while ((token = pa_split(c->transport, delimiters, &token_state))) {
404 if ((pc = strstr(token, "="))) {
405 if (0 == strncmp(token, "server_port", 11)) {
406 pa_atou(pc+1, &c->port);
407 pa_xfree(token);
408 break;
411 pa_xfree(token);
413 if (0 == c->port) {
414 // Error no server_port in response
415 pa_headerlist_free(rheaders);
416 return 0;
419 *response_headers = rheaders;
420 return 1;
424 int pa_rtsp_record(pa_rtsp_context* c) {
425 pa_headerlist* headers;
426 int rv;
428 pa_assert(c);
429 if (!c->session) {
430 // No seesion in progres
431 return 0;
434 headers = pa_headerlist_new();
435 pa_headerlist_puts(headers, "Range", "npt=0-");
436 pa_headerlist_puts(headers, "RTP-Info", "seq=0;rtptime=0");
438 rv = pa_rtsp_exec(c, "RECORD", NULL, NULL, 1, headers, NULL);
439 pa_headerlist_free(headers);
440 return rv;
444 int pa_rtsp_teardown(pa_rtsp_context *c) {
445 pa_assert(c);
447 return pa_rtsp_exec(c, "TEARDOWN", NULL, NULL, 0, NULL, NULL);
451 int pa_rtsp_setparameter(pa_rtsp_context *c, const char* param) {
452 pa_assert(c);
453 if (!param)
454 return 0;
456 return pa_rtsp_exec(c, "SET_PARAMETER", "text/parameters", param, 1, NULL, NULL);
460 int pa_rtsp_flush(pa_rtsp_context *c) {
461 pa_headerlist* headers;
462 int rv;
464 pa_assert(c);
466 headers = pa_headerlist_new();
467 pa_headerlist_puts(headers, "RTP-Info", "seq=0;rtptime=0");
469 rv = pa_rtsp_exec(c, "FLUSH", NULL, NULL, 1, headers, NULL);
470 pa_headerlist_free(headers);
471 return rv;