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
32 #include <arpa/inet.h>
34 #include <sys/ioctl.h>
36 #ifdef HAVE_SYS_FILIO_H
37 #include <sys/filio.h>
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>
52 struct pa_rtsp_context {
55 pa_rtsp_cb_t callback;
57 const char* useragent;
58 pa_headerlist* headers;
69 * read one line from the file descriptor
70 * timeout: msec unit, -1 for infinite
71 * if CR comes then following LF is expected
72 * returned string in line is always null terminated, maxlen-1 is maximum string length
74 static int pa_read_line(pa_iochannel* io, char *line, int maxlen, int timeout)
83 fd = pa_iochannel_get_recv_fd(io);
90 for (i=0; i<maxlen; ++i) {
91 if (!poll(&pfds, 1, timeout))
94 rval = read(fd, &ch, 1);
99 /*ERRMSG("%s:read error: %s\n", __func__, strerror(errno));*/
104 /*INFMSG("%s:disconnected on the other end\n", __func__);*/
119 if (count >= maxlen-1)
128 static int pa_rtsp_exec(pa_rtsp_context* c, const char* cmd,
129 const char* content_type, const char* content,
131 pa_headerlist* headers) {
142 buf = pa_strbuf_new();
143 pa_strbuf_printf(buf, "%s %s RTSP/1.0\r\nCSeq: %d\r\n", cmd, c->url, ++c->cseq);
145 pa_strbuf_printf(buf, "Session: %s\r\n", c->session);
147 /* Add the headers */
149 hdrs = pa_headerlist_to_string(headers);
150 pa_strbuf_puts(buf, hdrs);
154 if (content_type && content) {
155 pa_strbuf_printf(buf, "Content-Type: %s\r\nContent-Length: %d\r\n",
156 content_type, (int)strlen(content));
159 pa_strbuf_printf(buf, "User-Agent: %s\r\n", c->useragent);
162 hdrs = pa_headerlist_to_string(c->headers);
163 pa_strbuf_puts(buf, hdrs);
167 pa_strbuf_puts(buf, "\r\n");
169 if (content_type && content) {
170 pa_strbuf_puts(buf, content);
173 /* Our packet is created... now we can send it :) */
174 hdrs = pa_strbuf_tostring_free(buf);
175 l = pa_iochannel_write(c->io, hdrs, strlen(hdrs));
182 pa_rtsp_context* pa_rtsp_context_new(const char* useragent) {
185 c = pa_xnew0(pa_rtsp_context, 1);
186 c->headers = pa_headerlist_new();
189 c->useragent = useragent;
191 c->useragent = "PulseAudio RTSP Client";
197 void pa_rtsp_context_free(pa_rtsp_context* c) {
200 pa_socket_client_unref(c->sc);
203 pa_xfree(c->localip);
204 pa_xfree(c->session);
205 pa_xfree(c->transport);
206 pa_headerlist_free(c->headers);
212 static void io_callback(PA_GCC_UNUSED pa_iochannel *io, void *userdata) {
214 pa_headerlist* response_headers = NULL;
220 char delimiters[] = " ";
221 pa_rtsp_context *c = userdata;
224 /* TODO: convert this to a pa_ioline based reader */
225 if (STATE_CONNECT == c->state) {
226 response_headers = pa_headerlist_new();
229 /* read in any response headers */
230 if (pa_read_line(c->io, response, sizeof(response), timeout) > 0) {
231 const char* token_state = NULL;
234 pa_xfree(pa_split(response, delimiters, &token_state));
235 token = pa_split(response, delimiters, &token_state);
236 if (!token || strcmp(token, "200")) {
238 pa_log("Invalid Response");
239 /* TODO: Bail out completely */
244 /* We want to return the headers? */
245 if (!response_headers) {
246 /* We have no storage, so just clear out the response. */
247 while (pa_read_line(c->io, response, sizeof(response), timeout) > 0);
249 /* TODO: Move header reading into the headerlist. */
251 buf = pa_strbuf_new();
252 while (pa_read_line(c->io, response, sizeof(response), timeout) > 0) {
253 /* If the first character is a space, it's a continuation header */
254 if (header && ' ' == response[0]) {
255 /* Add this line to the buffer (sans the space. */
256 pa_strbuf_puts(buf, &(response[1]));
261 /* This is not a continuation header so let's dump the full
262 header/value into our proplist */
263 pa_headerlist_puts(response_headers, header, pa_strbuf_tostring_free(buf));
265 buf = pa_strbuf_new();
268 delimpos = strstr(response, ":");
270 pa_log("Invalid response header");
274 if (strlen(delimpos) > 1) {
275 /* Cut our line off so we can copy the header name out */
278 /* Trim the front of any spaces */
279 while (' ' == *delimpos)
282 pa_strbuf_puts(buf, delimpos);
284 /* Cut our line off so we can copy the header name out */
288 /* Save the header name */
289 header = pa_xstrdup(response);
291 /* We will have a header left from our looping itteration, so add it in :) */
293 /* This is not a continuation header so let's dump it into our proplist */
294 pa_headerlist_puts(response_headers, header, pa_strbuf_tostring(buf));
300 /* Deal with a CONNECT response */
301 if (STATE_CONNECT == c->state) {
302 const char* token_state = NULL;
303 const char* pc = NULL;
304 c->session = pa_xstrdup(pa_headerlist_gets(response_headers, "Session"));
305 c->transport = pa_xstrdup(pa_headerlist_gets(response_headers, "Transport"));
307 if (!c->session || !c->transport) {
308 pa_headerlist_free(response_headers);
312 /* Now parse out the server port component of the response. */
315 while ((token = pa_split(c->transport, delimiters, &token_state))) {
316 if ((pc = strstr(token, "="))) {
317 if (0 == strncmp(token, "server_port", 11)) {
318 pa_atou(pc+1, &c->port);
326 /* Error no server_port in response */
327 pa_headerlist_free(response_headers);
332 /* Call our callback */
334 c->callback(c, c->state, response_headers, c->userdata);
337 if (response_headers)
338 pa_headerlist_free(response_headers);
341 if (do_read(u) < 0 || do_write(u) < 0) {
344 pa_iochannel_free(u->io);
348 pa_module_unload_request(u->module);
353 static void on_connection(pa_socket_client *sc, pa_iochannel *io, void *userdata) {
354 pa_rtsp_context *c = userdata;
357 struct sockaddr_in in;
358 struct sockaddr_in6 in6;
360 socklen_t sa_len = sizeof(sa);
364 pa_assert(c->sc == sc);
366 pa_socket_client_unref(c->sc);
370 pa_log("Connection failed: %s", pa_cstrerror(errno));
375 pa_iochannel_set_callback(c->io, io_callback, c);
377 /* Get the local IP address for use externally */
378 if (0 == getsockname(pa_iochannel_get_recv_fd(io), &sa.sa, &sa_len)) {
379 char buf[INET6_ADDRSTRLEN];
380 const char *res = NULL;
382 if (AF_INET == sa.sa.sa_family) {
383 res = inet_ntop(sa.sa.sa_family, &sa.in.sin_addr, buf, sizeof(buf));
384 } else if (AF_INET6 == sa.sa.sa_family) {
385 res = inet_ntop(AF_INET6, &sa.in6.sin6_addr, buf, sizeof(buf));
388 c->localip = pa_xstrdup(res);
392 int pa_rtsp_connect(pa_rtsp_context *c, pa_mainloop_api *mainloop, const char* hostname, uint16_t port) {
398 if (!(c->sc = pa_socket_client_new_string(mainloop, hostname, port))) {
399 pa_log("failed to connect to server '%s:%d'", hostname, port);
403 pa_socket_client_set_callback(c->sc, on_connection, c);
404 c->state = STATE_CONNECT;
408 void pa_rtsp_set_callback(pa_rtsp_context *c, pa_rtsp_cb_t callback, void *userdata) {
411 c->callback = callback;
412 c->userdata = userdata;
415 void pa_rtsp_disconnect(pa_rtsp_context *c) {
419 pa_iochannel_free(c->io);
424 const char* pa_rtsp_localip(pa_rtsp_context* c) {
430 uint32_t pa_rtsp_serverport(pa_rtsp_context* c) {
436 void pa_rtsp_set_url(pa_rtsp_context* c, const char* url) {
439 c->url = pa_xstrdup(url);
442 void pa_rtsp_add_header(pa_rtsp_context *c, const char* key, const char* value)
448 pa_headerlist_puts(c->headers, key, value);
451 void pa_rtsp_remove_header(pa_rtsp_context *c, const char* key)
456 pa_headerlist_remove(c->headers, key);
459 int pa_rtsp_announce(pa_rtsp_context *c, const char* sdp) {
464 c->state = STATE_ANNOUNCE;
465 return pa_rtsp_exec(c, "ANNOUNCE", "application/sdp", sdp, 1, NULL);
469 int pa_rtsp_setup(pa_rtsp_context* c) {
470 pa_headerlist* headers;
475 headers = pa_headerlist_new();
476 pa_headerlist_puts(headers, "Transport", "RTP/AVP/TCP;unicast;interleaved=0-1;mode=record");
478 c->state = STATE_SETUP;
479 rv = pa_rtsp_exec(c, "SETUP", NULL, NULL, 1, headers);
480 pa_headerlist_free(headers);
485 int pa_rtsp_record(pa_rtsp_context* c) {
486 pa_headerlist* headers;
491 /* No seesion in progres */
495 headers = pa_headerlist_new();
496 pa_headerlist_puts(headers, "Range", "npt=0-");
497 pa_headerlist_puts(headers, "RTP-Info", "seq=0;rtptime=0");
499 c->state = STATE_RECORD;
500 rv = pa_rtsp_exec(c, "RECORD", NULL, NULL, 1, headers);
501 pa_headerlist_free(headers);
506 int pa_rtsp_teardown(pa_rtsp_context *c) {
509 c->state = STATE_TEARDOWN;
510 return pa_rtsp_exec(c, "TEARDOWN", NULL, NULL, 0, NULL);
514 int pa_rtsp_setparameter(pa_rtsp_context *c, const char* param) {
519 c->state = STATE_SET_PARAMETER;
520 return pa_rtsp_exec(c, "SET_PARAMETER", "text/parameters", param, 1, NULL);
524 int pa_rtsp_flush(pa_rtsp_context *c) {
525 pa_headerlist* headers;
530 headers = pa_headerlist_new();
531 pa_headerlist_puts(headers, "RTP-Info", "seq=0;rtptime=0");
533 c->state = STATE_FLUSH;
534 rv = pa_rtsp_exec(c, "FLUSH", NULL, NULL, 1, headers);
535 pa_headerlist_free(headers);