From 41e31ab204ca48ea749c416eb270ebfa2f74b086 Mon Sep 17 00:00:00 2001 From: Colin Guthrie Date: Wed, 7 May 2008 01:23:16 +0000 Subject: Rename rtsp.{c,h} to rtsp_client.{c,h}. Renate pa_rtsp_context to pa_rtsp_client. git-svn-id: file:///home/lennart/svn/public/pulseaudio/branches/coling@2376 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/rtp/rtsp_client.c | 507 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 507 insertions(+) create mode 100644 src/modules/rtp/rtsp_client.c (limited to 'src/modules/rtp/rtsp_client.c') diff --git a/src/modules/rtp/rtsp_client.c b/src/modules/rtp/rtsp_client.c new file mode 100644 index 00000000..c22f801e --- /dev/null +++ b/src/modules/rtp/rtsp_client.c @@ -0,0 +1,507 @@ +/* $Id$ */ + +/*** + This file is part of PulseAudio. + + Copyright 2008 Colin Guthrie + + PulseAudio is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published + by the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + PulseAudio is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with PulseAudio; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include +#include +#include + +#ifdef HAVE_SYS_FILIO_H +#include +#endif + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "rtsp_client.h" + +struct pa_rtsp_client { + pa_socket_client *sc; + pa_iochannel *io; + pa_ioline *ioline; + + pa_rtsp_cb_t callback; + + void *userdata; + const char *useragent; + + pa_rtsp_state state; + uint8_t waiting; + + pa_headerlist* headers; + char *last_header; + pa_strbuf *header_buffer; + pa_headerlist* response_headers; + + char *localip; + char *url; + uint32_t port; + uint32_t cseq; + char *session; + char *transport; +}; + +static int pa_rtsp_exec(pa_rtsp_client* c, const char* cmd, + const char* content_type, const char* content, + int expect_response, + pa_headerlist* headers) { + pa_strbuf* buf; + char* hdrs; + ssize_t l; + + pa_assert(c); + pa_assert(c->url); + + if (!cmd) + return -1; + + buf = pa_strbuf_new(); + pa_strbuf_printf(buf, "%s %s RTSP/1.0\r\nCSeq: %d\r\n", cmd, c->url, ++c->cseq); + if (c->session) + pa_strbuf_printf(buf, "Session: %s\r\n", c->session); + + /* Add the headers */ + if (headers) { + hdrs = pa_headerlist_to_string(headers); + pa_strbuf_puts(buf, hdrs); + pa_xfree(hdrs); + } + + if (content_type && content) { + pa_strbuf_printf(buf, "Content-Type: %s\r\nContent-Length: %d\r\n", + content_type, (int)strlen(content)); + } + + pa_strbuf_printf(buf, "User-Agent: %s\r\n", c->useragent); + + if (c->headers) { + hdrs = pa_headerlist_to_string(c->headers); + pa_strbuf_puts(buf, hdrs); + pa_xfree(hdrs); + } + + pa_strbuf_puts(buf, "\r\n"); + + if (content_type && content) { + pa_strbuf_puts(buf, content); + } + + /* Our packet is created... now we can send it :) */ + hdrs = pa_strbuf_tostring_free(buf); + /*pa_log_debug("Submitting request:"); + pa_log_debug(hdrs);*/ + l = pa_iochannel_write(c->io, hdrs, strlen(hdrs)); + pa_xfree(hdrs); + + return 0; +} + + +pa_rtsp_client* pa_rtsp_client_new(const char* useragent) { + pa_rtsp_client *c; + + c = pa_xnew0(pa_rtsp_client, 1); + c->headers = pa_headerlist_new(); + + if (useragent) + c->useragent = useragent; + else + c->useragent = "PulseAudio RTSP Client"; + + return c; +} + + +void pa_rtsp_client_free(pa_rtsp_client* c) { + if (c) { + if (c->sc) + pa_socket_client_unref(c->sc); + + pa_xfree(c->url); + pa_xfree(c->localip); + pa_xfree(c->session); + pa_xfree(c->transport); + pa_xfree(c->last_header); + if (c->header_buffer) + pa_strbuf_free(c->header_buffer); + if (c->response_headers) + pa_headerlist_free(c->response_headers); + pa_headerlist_free(c->headers); + } + pa_xfree(c); +} + + +static void headers_read(pa_rtsp_client *c) { + char* token; + char delimiters[] = ";"; + + pa_assert(c); + pa_assert(c->response_headers); + + /* Deal with a SETUP response */ + if (STATE_SETUP == c->state) { + const char* token_state = NULL; + const char* pc = NULL; + c->session = pa_xstrdup(pa_headerlist_gets(c->response_headers, "Session")); + c->transport = pa_xstrdup(pa_headerlist_gets(c->response_headers, "Transport")); + + if (!c->session || !c->transport) { + pa_headerlist_free(c->response_headers); + c->response_headers = NULL; + pa_log("Invalid SETUP response."); + return; + } + + /* Now parse out the server port component of the response. */ + while ((token = pa_split(c->transport, delimiters, &token_state))) { + if ((pc = strstr(token, "="))) { + if (0 == strncmp(token, "server_port", 11)) { + pa_atou(pc+1, &c->port); + pa_xfree(token); + break; + } + } + pa_xfree(token); + } + if (0 == c->port) { + /* Error no server_port in response */ + pa_headerlist_free(c->response_headers); + c->response_headers = NULL; + pa_log("Invalid SETUP response (no port number)."); + return; + } + } + + /* Call our callback */ + if (c->callback) + c->callback(c, c->state, c->response_headers, c->userdata); + + pa_headerlist_free(c->response_headers); + c->response_headers = NULL; +} + + +static void line_callback(pa_ioline *line, const char *s, void *userdata) { + char *delimpos; + char *s2, *s2p; + + pa_rtsp_client *c = userdata; + pa_assert(line); + pa_assert(c); + pa_assert(s); + + s2 = pa_xstrdup(s); + /* Trim trailing carriage returns */ + s2p = s2 + strlen(s2) - 1; + while (s2p >= s2 && '\r' == *s2p) { + *s2p = '\0'; + s2p -= 1; + } + if (c->waiting && 0 == strcmp("RTSP/1.0 200 OK", s2)) { + c->waiting = 0; + pa_assert(!c->response_headers); + c->response_headers = pa_headerlist_new(); + goto exit; + } + if (c->waiting) { + pa_log_warn("Unexpected response: %s", s2); + goto exit;; + } + if (!strlen(s2)) { + /* End of headers */ + /* We will have a header left from our looping itteration, so add it in :) */ + if (c->last_header) { + /* This is not a continuation header so let's dump it into our proplist */ + pa_headerlist_puts(c->response_headers, c->last_header, pa_strbuf_tostring_free(c->header_buffer)); + pa_xfree(c->last_header); + c->last_header = NULL; + c->header_buffer= NULL; + } + + pa_log_debug("Full response received. Dispatching"); + headers_read(c); + c->waiting = 1; + goto exit; + } + + /* Read and parse a header (we know it's not empty) */ + /* TODO: Move header reading into the headerlist. */ + + /* If the first character is a space, it's a continuation header */ + if (c->last_header && ' ' == s2[0]) { + pa_assert(c->header_buffer); + + /* Add this line to the buffer (sans the space. */ + pa_strbuf_puts(c->header_buffer, &(s2[1])); + goto exit; + } + + if (c->last_header) { + /* This is not a continuation header so let's dump the full + header/value into our proplist */ + pa_headerlist_puts(c->response_headers, c->last_header, pa_strbuf_tostring_free(c->header_buffer)); + pa_xfree(c->last_header); + c->last_header = NULL; + c->header_buffer = NULL; + } + + delimpos = strstr(s2, ":"); + if (!delimpos) { + pa_log_warn("Unexpected response when expecting header: %s", s); + goto exit; + } + + pa_assert(!c->header_buffer); + pa_assert(!c->last_header); + + c->header_buffer = pa_strbuf_new(); + if (strlen(delimpos) > 1) { + /* Cut our line off so we can copy the header name out */ + *delimpos++ = '\0'; + + /* Trim the front of any spaces */ + while (' ' == *delimpos) + ++delimpos; + + pa_strbuf_puts(c->header_buffer, delimpos); + } else { + /* Cut our line off so we can copy the header name out */ + *delimpos = '\0'; + } + + /* Save the header name */ + c->last_header = pa_xstrdup(s2); + exit: + pa_xfree(s2); +} + + +static void on_connection(pa_socket_client *sc, pa_iochannel *io, void *userdata) { + pa_rtsp_client *c = userdata; + union { + struct sockaddr sa; + struct sockaddr_in in; + struct sockaddr_in6 in6; + } sa; + socklen_t sa_len = sizeof(sa); + + pa_assert(sc); + pa_assert(c); + pa_assert(c->sc == sc); + + pa_socket_client_unref(c->sc); + c->sc = NULL; + + if (!io) { + pa_log("Connection failed: %s", pa_cstrerror(errno)); + return; + } + pa_assert(!c->io); + c->io = io; + + c->ioline = pa_ioline_new(io); + pa_ioline_set_callback(c->ioline, line_callback, c); + + /* Get the local IP address for use externally */ + if (0 == getsockname(pa_iochannel_get_recv_fd(io), &sa.sa, &sa_len)) { + char buf[INET6_ADDRSTRLEN]; + const char *res = NULL; + + if (AF_INET == sa.sa.sa_family) { + res = inet_ntop(sa.sa.sa_family, &sa.in.sin_addr, buf, sizeof(buf)); + } else if (AF_INET6 == sa.sa.sa_family) { + res = inet_ntop(AF_INET6, &sa.in6.sin6_addr, buf, sizeof(buf)); + } + if (res) + c->localip = pa_xstrdup(res); + } + pa_log_debug("Established RTSP connection from local ip %s", c->localip); + + c->waiting = 1; + c->state = STATE_CONNECT; + if (c->callback) + c->callback(c, c->state, NULL, c->userdata); +} + +int pa_rtsp_connect(pa_rtsp_client *c, pa_mainloop_api *mainloop, const char* hostname, uint16_t port) { + pa_assert(c); + pa_assert(mainloop); + pa_assert(hostname); + pa_assert(port > 0); + + if (!(c->sc = pa_socket_client_new_string(mainloop, hostname, port))) { + pa_log("failed to connect to server '%s:%d'", hostname, port); + return -1; + } + + pa_socket_client_set_callback(c->sc, on_connection, c); + c->state = STATE_CONNECT; + return 0; +} + +void pa_rtsp_set_callback(pa_rtsp_client *c, pa_rtsp_cb_t callback, void *userdata) { + pa_assert(c); + + c->callback = callback; + c->userdata = userdata; +} + +void pa_rtsp_disconnect(pa_rtsp_client *c) { + pa_assert(c); + + if (c->io) + pa_iochannel_free(c->io); + c->io = NULL; +} + + +const char* pa_rtsp_localip(pa_rtsp_client* c) { + pa_assert(c); + + return c->localip; +} + +uint32_t pa_rtsp_serverport(pa_rtsp_client* c) { + pa_assert(c); + + return c->port; +} + +void pa_rtsp_set_url(pa_rtsp_client* c, const char* url) { + pa_assert(c); + + c->url = pa_xstrdup(url); +} + +void pa_rtsp_add_header(pa_rtsp_client *c, const char* key, const char* value) +{ + pa_assert(c); + pa_assert(key); + pa_assert(value); + + pa_headerlist_puts(c->headers, key, value); +} + +void pa_rtsp_remove_header(pa_rtsp_client *c, const char* key) +{ + pa_assert(c); + pa_assert(key); + + pa_headerlist_remove(c->headers, key); +} + +int pa_rtsp_announce(pa_rtsp_client *c, const char* sdp) { + pa_assert(c); + if (!sdp) + return -1; + + c->state = STATE_ANNOUNCE; + return pa_rtsp_exec(c, "ANNOUNCE", "application/sdp", sdp, 1, NULL); +} + + +int pa_rtsp_setup(pa_rtsp_client* c) { + pa_headerlist* headers; + int rv; + + pa_assert(c); + + headers = pa_headerlist_new(); + pa_headerlist_puts(headers, "Transport", "RTP/AVP/TCP;unicast;interleaved=0-1;mode=record"); + + c->state = STATE_SETUP; + rv = pa_rtsp_exec(c, "SETUP", NULL, NULL, 1, headers); + pa_headerlist_free(headers); + return rv; +} + + +int pa_rtsp_record(pa_rtsp_client* c) { + pa_headerlist* headers; + int rv; + + pa_assert(c); + if (!c->session) { + /* No seesion in progres */ + return -1; + } + + headers = pa_headerlist_new(); + pa_headerlist_puts(headers, "Range", "npt=0-"); + pa_headerlist_puts(headers, "RTP-Info", "seq=0;rtptime=0"); + + c->state = STATE_RECORD; + rv = pa_rtsp_exec(c, "RECORD", NULL, NULL, 1, headers); + pa_headerlist_free(headers); + return rv; +} + + +int pa_rtsp_teardown(pa_rtsp_client *c) { + pa_assert(c); + + c->state = STATE_TEARDOWN; + return pa_rtsp_exec(c, "TEARDOWN", NULL, NULL, 0, NULL); +} + + +int pa_rtsp_setparameter(pa_rtsp_client *c, const char* param) { + pa_assert(c); + if (!param) + return -1; + + c->state = STATE_SET_PARAMETER; + return pa_rtsp_exec(c, "SET_PARAMETER", "text/parameters", param, 1, NULL); +} + + +int pa_rtsp_flush(pa_rtsp_client *c) { + pa_headerlist* headers; + int rv; + + pa_assert(c); + + headers = pa_headerlist_new(); + pa_headerlist_puts(headers, "RTP-Info", "seq=0;rtptime=0"); + + c->state = STATE_FLUSH; + rv = pa_rtsp_exec(c, "FLUSH", NULL, NULL, 1, headers); + pa_headerlist_free(headers); + return rv; +} -- cgit From 4dd318519fbec1811a16dca05aca859da74b60c2 Mon Sep 17 00:00:00 2001 From: Colin Guthrie Date: Sun, 11 May 2008 13:32:09 +0000 Subject: Do not assert on NULL values of s. This means the connection was closed. This change somehow kills the mainloop with an assert, so I need to sort that out. git-svn-id: file:///home/lennart/svn/public/pulseaudio/branches/coling@2399 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/rtp/rtsp_client.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src/modules/rtp/rtsp_client.c') diff --git a/src/modules/rtp/rtsp_client.c b/src/modules/rtp/rtsp_client.c index c22f801e..5665c9f6 100644 --- a/src/modules/rtp/rtsp_client.c +++ b/src/modules/rtp/rtsp_client.c @@ -224,7 +224,14 @@ static void line_callback(pa_ioline *line, const char *s, void *userdata) { pa_rtsp_client *c = userdata; pa_assert(line); pa_assert(c); - pa_assert(s); + + if (!s) { + pa_log_warn("Connection closed"); + pa_ioline_unref(c->ioline); + c->ioline = NULL; + pa_rtsp_disconnect(c); + return; + } s2 = pa_xstrdup(s); /* Trim trailing carriage returns */ -- cgit From 899492c31581f5591cd9437052dda15ad02ec0ac Mon Sep 17 00:00:00 2001 From: Colin Guthrie Date: Sun, 11 May 2008 14:18:48 +0000 Subject: Add a new callback structure to propigate when the RTSP connection dies git-svn-id: file:///home/lennart/svn/public/pulseaudio/branches/coling@2402 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/rtp/rtsp_client.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/modules/rtp/rtsp_client.c') diff --git a/src/modules/rtp/rtsp_client.c b/src/modules/rtp/rtsp_client.c index 5665c9f6..22f0f0c1 100644 --- a/src/modules/rtp/rtsp_client.c +++ b/src/modules/rtp/rtsp_client.c @@ -173,6 +173,7 @@ static void headers_read(pa_rtsp_client *c) { pa_assert(c); pa_assert(c->response_headers); + pa_assert(c->callback); /* Deal with a SETUP response */ if (STATE_SETUP == c->state) { @@ -209,8 +210,7 @@ static void headers_read(pa_rtsp_client *c) { } /* Call our callback */ - if (c->callback) - c->callback(c, c->state, c->response_headers, c->userdata); + c->callback(c, c->state, c->response_headers, c->userdata); pa_headerlist_free(c->response_headers); c->response_headers = NULL; @@ -224,12 +224,13 @@ static void line_callback(pa_ioline *line, const char *s, void *userdata) { pa_rtsp_client *c = userdata; pa_assert(line); pa_assert(c); + pa_assert(c->callback); if (!s) { - pa_log_warn("Connection closed"); pa_ioline_unref(c->ioline); c->ioline = NULL; pa_rtsp_disconnect(c); + c->callback(c, STATE_DISCONNECTED, NULL, c->userdata); return; } -- cgit From 4b7b7b15d73a5f2a98229b12406b4397563d2983 Mon Sep 17 00:00:00 2001 From: Colin Guthrie Date: Sun, 11 May 2008 15:12:20 +0000 Subject: Fix up IPv6 address format to enclose it in [] git-svn-id: file:///home/lennart/svn/public/pulseaudio/branches/coling@2406 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/rtp/rtsp_client.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src/modules/rtp/rtsp_client.c') diff --git a/src/modules/rtp/rtsp_client.c b/src/modules/rtp/rtsp_client.c index 22f0f0c1..193248eb 100644 --- a/src/modules/rtp/rtsp_client.c +++ b/src/modules/rtp/rtsp_client.c @@ -352,12 +352,14 @@ static void on_connection(pa_socket_client *sc, pa_iochannel *io, void *userdata const char *res = NULL; if (AF_INET == sa.sa.sa_family) { - res = inet_ntop(sa.sa.sa_family, &sa.in.sin_addr, buf, sizeof(buf)); + if ((res = inet_ntop(sa.sa.sa_family, &sa.in.sin_addr, buf, sizeof(buf)))) { + c->localip = pa_xstrdup(res); + } } else if (AF_INET6 == sa.sa.sa_family) { - res = inet_ntop(AF_INET6, &sa.in6.sin6_addr, buf, sizeof(buf)); + if ((res = inet_ntop(AF_INET6, &sa.in6.sin6_addr, buf, sizeof(buf)))) { + c->localip = pa_sprintf_malloc("[%s]", res); + } } - if (res) - c->localip = pa_xstrdup(res); } pa_log_debug("Established RTSP connection from local ip %s", c->localip); -- cgit From 92166846913ebb5e86f36352e20c9ca4f4bf23ae Mon Sep 17 00:00:00 2001 From: Colin Guthrie Date: Sun, 11 May 2008 16:40:26 +0000 Subject: Do not prefix internal function rtsp_exec. Change port to be 16 bits Do not free stuff on closure as this happens further up the stack. git-svn-id: file:///home/lennart/svn/public/pulseaudio/branches/coling@2410 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/rtp/rtsp_client.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) (limited to 'src/modules/rtp/rtsp_client.c') diff --git a/src/modules/rtp/rtsp_client.c b/src/modules/rtp/rtsp_client.c index 193248eb..24839729 100644 --- a/src/modules/rtp/rtsp_client.c +++ b/src/modules/rtp/rtsp_client.c @@ -70,13 +70,13 @@ struct pa_rtsp_client { char *localip; char *url; - uint32_t port; + uint16_t port; uint32_t cseq; char *session; char *transport; }; -static int pa_rtsp_exec(pa_rtsp_client* c, const char* cmd, +static int rtsp_exec(pa_rtsp_client* c, const char* cmd, const char* content_type, const char* content, int expect_response, pa_headerlist* headers) { @@ -193,7 +193,7 @@ static void headers_read(pa_rtsp_client *c) { while ((token = pa_split(c->transport, delimiters, &token_state))) { if ((pc = strstr(token, "="))) { if (0 == strncmp(token, "server_port", 11)) { - pa_atou(pc+1, &c->port); + pa_atou(pc+1, (uint32_t*)(&c->port)); pa_xfree(token); break; } @@ -227,9 +227,6 @@ static void line_callback(pa_ioline *line, const char *s, void *userdata) { pa_assert(c->callback); if (!s) { - pa_ioline_unref(c->ioline); - c->ioline = NULL; - pa_rtsp_disconnect(c); c->callback(c, STATE_DISCONNECTED, NULL, c->userdata); return; } @@ -442,7 +439,7 @@ int pa_rtsp_announce(pa_rtsp_client *c, const char* sdp) { return -1; c->state = STATE_ANNOUNCE; - return pa_rtsp_exec(c, "ANNOUNCE", "application/sdp", sdp, 1, NULL); + return rtsp_exec(c, "ANNOUNCE", "application/sdp", sdp, 1, NULL); } @@ -456,7 +453,7 @@ int pa_rtsp_setup(pa_rtsp_client* c) { pa_headerlist_puts(headers, "Transport", "RTP/AVP/TCP;unicast;interleaved=0-1;mode=record"); c->state = STATE_SETUP; - rv = pa_rtsp_exec(c, "SETUP", NULL, NULL, 1, headers); + rv = rtsp_exec(c, "SETUP", NULL, NULL, 1, headers); pa_headerlist_free(headers); return rv; } @@ -477,7 +474,7 @@ int pa_rtsp_record(pa_rtsp_client* c) { pa_headerlist_puts(headers, "RTP-Info", "seq=0;rtptime=0"); c->state = STATE_RECORD; - rv = pa_rtsp_exec(c, "RECORD", NULL, NULL, 1, headers); + rv = rtsp_exec(c, "RECORD", NULL, NULL, 1, headers); pa_headerlist_free(headers); return rv; } @@ -487,7 +484,7 @@ int pa_rtsp_teardown(pa_rtsp_client *c) { pa_assert(c); c->state = STATE_TEARDOWN; - return pa_rtsp_exec(c, "TEARDOWN", NULL, NULL, 0, NULL); + return rtsp_exec(c, "TEARDOWN", NULL, NULL, 0, NULL); } @@ -497,7 +494,7 @@ int pa_rtsp_setparameter(pa_rtsp_client *c, const char* param) { return -1; c->state = STATE_SET_PARAMETER; - return pa_rtsp_exec(c, "SET_PARAMETER", "text/parameters", param, 1, NULL); + return rtsp_exec(c, "SET_PARAMETER", "text/parameters", param, 1, NULL); } @@ -511,7 +508,7 @@ int pa_rtsp_flush(pa_rtsp_client *c) { pa_headerlist_puts(headers, "RTP-Info", "seq=0;rtptime=0"); c->state = STATE_FLUSH; - rv = pa_rtsp_exec(c, "FLUSH", NULL, NULL, 1, headers); + rv = rtsp_exec(c, "FLUSH", NULL, NULL, 1, headers); pa_headerlist_free(headers); return rv; } -- cgit From 3767cdb6d16c5817eb489129585fb353e3ad6afa Mon Sep 17 00:00:00 2001 From: Colin Guthrie Date: Sun, 11 May 2008 17:02:19 +0000 Subject: Do tidy up on disconnection. Only clear IO related stuff if this free() was triggered deliberatly (i.e. not by server side disconnect) git-svn-id: file:///home/lennart/svn/public/pulseaudio/branches/coling@2411 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/rtp/rtsp_client.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/modules/rtp/rtsp_client.c') diff --git a/src/modules/rtp/rtsp_client.c b/src/modules/rtp/rtsp_client.c index 24839729..f9fe9bfe 100644 --- a/src/modules/rtp/rtsp_client.c +++ b/src/modules/rtp/rtsp_client.c @@ -151,6 +151,10 @@ void pa_rtsp_client_free(pa_rtsp_client* c) { if (c) { if (c->sc) pa_socket_client_unref(c->sc); + if (c->ioline) + pa_ioline_close(c->ioline); + else if (c->io) + pa_iochannel_free(c->io); pa_xfree(c->url); pa_xfree(c->localip); @@ -227,6 +231,10 @@ static void line_callback(pa_ioline *line, const char *s, void *userdata) { pa_assert(c->callback); if (!s) { + /* Keep the ioline/iochannel open as they will be freed automatically */ + c->ioline = NULL; + c->io = NULL; + pa_rtsp_client_free(c); c->callback(c, STATE_DISCONNECTED, NULL, c->userdata); return; } -- cgit From 5f527dc47944bbd97a49e8d89427d09850b28e5d Mon Sep 17 00:00:00 2001 From: Colin Guthrie Date: Mon, 9 Jun 2008 21:59:41 +0000 Subject: Add seq and rtptime params to record/flush with a view to using these for timing and device suspension git-svn-id: file:///home/lennart/svn/public/pulseaudio/branches/coling@2500 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/rtp/rtsp_client.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'src/modules/rtp/rtsp_client.c') diff --git a/src/modules/rtp/rtsp_client.c b/src/modules/rtp/rtsp_client.c index f9fe9bfe..70040428 100644 --- a/src/modules/rtp/rtsp_client.c +++ b/src/modules/rtp/rtsp_client.c @@ -47,6 +47,8 @@ #include #include #include +#include +#include #include "rtsp_client.h" @@ -467,9 +469,10 @@ int pa_rtsp_setup(pa_rtsp_client* c) { } -int pa_rtsp_record(pa_rtsp_client* c) { +int pa_rtsp_record(pa_rtsp_client* c, uint16_t* seq, uint32_t* rtptime) { pa_headerlist* headers; int rv; + char *info; pa_assert(c); if (!c->session) { @@ -477,9 +480,14 @@ int pa_rtsp_record(pa_rtsp_client* c) { return -1; } + /* Todo: Generate these values randomly as per spec */ + *seq = *rtptime = 0; + headers = pa_headerlist_new(); pa_headerlist_puts(headers, "Range", "npt=0-"); - pa_headerlist_puts(headers, "RTP-Info", "seq=0;rtptime=0"); + info = pa_sprintf_malloc("seq=%u;rtptime=%u", *seq, *rtptime); + pa_headerlist_puts(headers, "RTP-Info", info); + pa_xfree(info); c->state = STATE_RECORD; rv = rtsp_exec(c, "RECORD", NULL, NULL, 1, headers); @@ -506,14 +514,17 @@ int pa_rtsp_setparameter(pa_rtsp_client *c, const char* param) { } -int pa_rtsp_flush(pa_rtsp_client *c) { +int pa_rtsp_flush(pa_rtsp_client *c, uint16_t seq, uint32_t rtptime) { pa_headerlist* headers; int rv; + char *info; pa_assert(c); headers = pa_headerlist_new(); - pa_headerlist_puts(headers, "RTP-Info", "seq=0;rtptime=0"); + info = pa_sprintf_malloc("seq=%u;rtptime=%u", seq, rtptime); + pa_headerlist_puts(headers, "RTP-Info", info); + pa_xfree(info); c->state = STATE_FLUSH; rv = rtsp_exec(c, "FLUSH", NULL, NULL, 1, headers); -- cgit From 19dcb529ad3baa8e8e506ddfa703e952044e0f60 Mon Sep 17 00:00:00 2001 From: Colin Guthrie Date: Mon, 9 Jun 2008 22:01:23 +0000 Subject: Remove unneeded headers accidentially added in r2500. git-svn-id: file:///home/lennart/svn/public/pulseaudio/branches/coling@2501 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/rtp/rtsp_client.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/modules/rtp/rtsp_client.c') diff --git a/src/modules/rtp/rtsp_client.c b/src/modules/rtp/rtsp_client.c index 70040428..c0ca49fa 100644 --- a/src/modules/rtp/rtsp_client.c +++ b/src/modules/rtp/rtsp_client.c @@ -47,8 +47,6 @@ #include #include #include -#include -#include #include "rtsp_client.h" -- cgit From d86fc75e0cbc9d102dc000d2781f9dfddc89fbbf Mon Sep 17 00:00:00 2001 From: Colin Guthrie Date: Tue, 10 Jun 2008 23:49:35 +0000 Subject: Change the API of the RTSP client a bit. * Store the mainloop, hostname and port internally on construction * This should allow use to easily reconnect if disconnected although this has thus far proved unreliable. The changes look like more than they are due to moving a function around. git-svn-id: file:///home/lennart/svn/public/pulseaudio/branches/coling@2502 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/modules/rtp/rtsp_client.c | 155 +++++++++++++++++++++++------------------- 1 file changed, 84 insertions(+), 71 deletions(-) (limited to 'src/modules/rtp/rtsp_client.c') diff --git a/src/modules/rtp/rtsp_client.c b/src/modules/rtp/rtsp_client.c index c0ca49fa..88f77818 100644 --- a/src/modules/rtp/rtsp_client.c +++ b/src/modules/rtp/rtsp_client.c @@ -51,6 +51,10 @@ #include "rtsp_client.h" struct pa_rtsp_client { + pa_mainloop_api *mainloop; + char *hostname; + uint16_t port; + pa_socket_client *sc; pa_iochannel *io; pa_ioline *ioline; @@ -70,72 +74,23 @@ struct pa_rtsp_client { char *localip; char *url; - uint16_t port; + uint16_t rtp_port; uint32_t cseq; char *session; char *transport; }; -static int rtsp_exec(pa_rtsp_client* c, const char* cmd, - const char* content_type, const char* content, - int expect_response, - pa_headerlist* headers) { - pa_strbuf* buf; - char* hdrs; - ssize_t l; - - pa_assert(c); - pa_assert(c->url); - - if (!cmd) - return -1; - - buf = pa_strbuf_new(); - pa_strbuf_printf(buf, "%s %s RTSP/1.0\r\nCSeq: %d\r\n", cmd, c->url, ++c->cseq); - if (c->session) - pa_strbuf_printf(buf, "Session: %s\r\n", c->session); - - /* Add the headers */ - if (headers) { - hdrs = pa_headerlist_to_string(headers); - pa_strbuf_puts(buf, hdrs); - pa_xfree(hdrs); - } - - if (content_type && content) { - pa_strbuf_printf(buf, "Content-Type: %s\r\nContent-Length: %d\r\n", - content_type, (int)strlen(content)); - } - - pa_strbuf_printf(buf, "User-Agent: %s\r\n", c->useragent); - - if (c->headers) { - hdrs = pa_headerlist_to_string(c->headers); - pa_strbuf_puts(buf, hdrs); - pa_xfree(hdrs); - } - - pa_strbuf_puts(buf, "\r\n"); - - if (content_type && content) { - pa_strbuf_puts(buf, content); - } - - /* Our packet is created... now we can send it :) */ - hdrs = pa_strbuf_tostring_free(buf); - /*pa_log_debug("Submitting request:"); - pa_log_debug(hdrs);*/ - l = pa_iochannel_write(c->io, hdrs, strlen(hdrs)); - pa_xfree(hdrs); - - return 0; -} - - -pa_rtsp_client* pa_rtsp_client_new(const char* useragent) { +pa_rtsp_client* pa_rtsp_client_new(pa_mainloop_api *mainloop, const char* hostname, uint16_t port, const char* useragent) { pa_rtsp_client *c; + pa_assert(mainloop); + pa_assert(hostname); + pa_assert(port > 0); + c = pa_xnew0(pa_rtsp_client, 1); + c->mainloop = mainloop; + c->hostname = pa_xstrdup(hostname); + c->port = port; c->headers = pa_headerlist_new(); if (useragent) @@ -156,6 +111,7 @@ void pa_rtsp_client_free(pa_rtsp_client* c) { else if (c->io) pa_iochannel_free(c->io); + pa_xfree(c->hostname); pa_xfree(c->url); pa_xfree(c->localip); pa_xfree(c->session); @@ -197,14 +153,14 @@ static void headers_read(pa_rtsp_client *c) { while ((token = pa_split(c->transport, delimiters, &token_state))) { if ((pc = strstr(token, "="))) { if (0 == strncmp(token, "server_port", 11)) { - pa_atou(pc+1, (uint32_t*)(&c->port)); + pa_atou(pc+1, (uint32_t*)(&c->rtp_port)); pa_xfree(token); break; } } pa_xfree(token); } - if (0 == c->port) { + if (0 == c->rtp_port) { /* Error no server_port in response */ pa_headerlist_free(c->response_headers); c->response_headers = NULL; @@ -234,7 +190,6 @@ static void line_callback(pa_ioline *line, const char *s, void *userdata) { /* Keep the ioline/iochannel open as they will be freed automatically */ c->ioline = NULL; c->io = NULL; - pa_rtsp_client_free(c); c->callback(c, STATE_DISCONNECTED, NULL, c->userdata); return; } @@ -336,8 +291,8 @@ static void on_connection(pa_socket_client *sc, pa_iochannel *io, void *userdata pa_assert(sc); pa_assert(c); + pa_assert(STATE_CONNECT == c->state); pa_assert(c->sc == sc); - pa_socket_client_unref(c->sc); c->sc = NULL; @@ -368,24 +323,24 @@ static void on_connection(pa_socket_client *sc, pa_iochannel *io, void *userdata } pa_log_debug("Established RTSP connection from local ip %s", c->localip); - c->waiting = 1; - c->state = STATE_CONNECT; if (c->callback) c->callback(c, c->state, NULL, c->userdata); } -int pa_rtsp_connect(pa_rtsp_client *c, pa_mainloop_api *mainloop, const char* hostname, uint16_t port) { +int pa_rtsp_connect(pa_rtsp_client *c) { pa_assert(c); - pa_assert(mainloop); - pa_assert(hostname); - pa_assert(port > 0); + pa_assert(!c->sc); + + pa_xfree(c->session); + c->session = NULL; - if (!(c->sc = pa_socket_client_new_string(mainloop, hostname, port))) { - pa_log("failed to connect to server '%s:%d'", hostname, port); + if (!(c->sc = pa_socket_client_new_string(c->mainloop, c->hostname, c->port))) { + pa_log("failed to connect to server '%s:%d'", c->hostname, c->port); return -1; } pa_socket_client_set_callback(c->sc, on_connection, c); + c->waiting = 1; c->state = STATE_CONNECT; return 0; } @@ -415,7 +370,7 @@ const char* pa_rtsp_localip(pa_rtsp_client* c) { uint32_t pa_rtsp_serverport(pa_rtsp_client* c) { pa_assert(c); - return c->port; + return c->rtp_port; } void pa_rtsp_set_url(pa_rtsp_client* c, const char* url) { @@ -441,6 +396,64 @@ void pa_rtsp_remove_header(pa_rtsp_client *c, const char* key) pa_headerlist_remove(c->headers, key); } +static int rtsp_exec(pa_rtsp_client* c, const char* cmd, + const char* content_type, const char* content, + int expect_response, + pa_headerlist* headers) { + pa_strbuf* buf; + char* hdrs; + ssize_t l; + + pa_assert(c); + pa_assert(c->url); + + if (!cmd) + return -1; + + pa_log_debug("Sending command: %s", cmd); + + buf = pa_strbuf_new(); + pa_strbuf_printf(buf, "%s %s RTSP/1.0\r\nCSeq: %d\r\n", cmd, c->url, ++c->cseq); + if (c->session) + pa_strbuf_printf(buf, "Session: %s\r\n", c->session); + + /* Add the headers */ + if (headers) { + hdrs = pa_headerlist_to_string(headers); + pa_strbuf_puts(buf, hdrs); + pa_xfree(hdrs); + } + + if (content_type && content) { + pa_strbuf_printf(buf, "Content-Type: %s\r\nContent-Length: %d\r\n", + content_type, (int)strlen(content)); + } + + pa_strbuf_printf(buf, "User-Agent: %s\r\n", c->useragent); + + if (c->headers) { + hdrs = pa_headerlist_to_string(c->headers); + pa_strbuf_puts(buf, hdrs); + pa_xfree(hdrs); + } + + pa_strbuf_puts(buf, "\r\n"); + + if (content_type && content) { + pa_strbuf_puts(buf, content); + } + + /* Our packet is created... now we can send it :) */ + hdrs = pa_strbuf_tostring_free(buf); + /*pa_log_debug("Submitting request:"); + pa_log_debug(hdrs);*/ + l = pa_iochannel_write(c->io, hdrs, strlen(hdrs)); + pa_xfree(hdrs); + + return 0; +} + + int pa_rtsp_announce(pa_rtsp_client *c, const char* sdp) { pa_assert(c); if (!sdp) -- cgit From c3d8bb5b34c45f4dda594cc1d8107cac468fa232 Mon Sep 17 00:00:00 2001 From: Colin Guthrie Date: Sun, 3 Aug 2008 20:56:21 +0100 Subject: Remove $Id$ lines left over from SVN --- src/modules/rtp/rtsp_client.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/modules/rtp/rtsp_client.c') diff --git a/src/modules/rtp/rtsp_client.c b/src/modules/rtp/rtsp_client.c index 88f77818..9eb3d964 100644 --- a/src/modules/rtp/rtsp_client.c +++ b/src/modules/rtp/rtsp_client.c @@ -1,5 +1,3 @@ -/* $Id$ */ - /*** This file is part of PulseAudio. -- cgit