2 This file is part of PulseAudio.
4 Copyright 2008 Colin Guthrie
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.
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.
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
30 #include <arpa/inet.h>
32 #include <sys/ioctl.h>
33 #include <netinet/in.h>
35 #ifdef HAVE_SYS_FILIO_H
36 #include <sys/filio.h>
39 #include <pulse/xmalloc.h>
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>
52 #include <pulsecore/poll.h>
55 #include "rtsp_client.h"
57 struct pa_rtsp_client {
58 pa_mainloop_api *mainloop;
66 pa_rtsp_cb_t callback;
69 const char *useragent;
74 pa_headerlist* headers;
76 pa_strbuf *header_buffer;
77 pa_headerlist* response_headers;
87 pa_rtsp_client* pa_rtsp_client_new(pa_mainloop_api *mainloop, const char* hostname, uint16_t port, const char* useragent) {
94 c = pa_xnew0(pa_rtsp_client, 1);
95 c->mainloop = mainloop;
96 c->hostname = pa_xstrdup(hostname);
98 c->headers = pa_headerlist_new();
101 c->useragent = useragent;
103 c->useragent = "PulseAudio RTSP Client";
109 void pa_rtsp_client_free(pa_rtsp_client* c) {
113 pa_socket_client_unref(c->sc);
115 pa_ioline_close(c->ioline);
117 pa_iochannel_free(c->io);
119 pa_xfree(c->hostname);
121 pa_xfree(c->localip);
122 pa_xfree(c->session);
123 pa_xfree(c->transport);
124 pa_xfree(c->last_header);
125 if (c->header_buffer)
126 pa_strbuf_free(c->header_buffer);
127 if (c->response_headers)
128 pa_headerlist_free(c->response_headers);
129 pa_headerlist_free(c->headers);
135 static void headers_read(pa_rtsp_client *c) {
137 char delimiters[] = ";";
140 pa_assert(c->response_headers);
141 pa_assert(c->callback);
143 /* Deal with a SETUP response */
144 if (STATE_SETUP == c->state) {
145 const char* token_state = NULL;
146 const char* pc = NULL;
147 c->session = pa_xstrdup(pa_headerlist_gets(c->response_headers, "Session"));
148 c->transport = pa_xstrdup(pa_headerlist_gets(c->response_headers, "Transport"));
150 if (!c->session || !c->transport) {
151 pa_log("Invalid SETUP response.");
155 /* Now parse out the server port component of the response. */
156 while ((token = pa_split(c->transport, delimiters, &token_state))) {
157 if ((pc = strstr(token, "="))) {
158 if (0 == strncmp(token, "server_port", 11)) {
159 pa_atou(pc+1, (uint32_t*)(&c->rtp_port));
166 if (0 == c->rtp_port) {
167 /* Error no server_port in response */
168 pa_log("Invalid SETUP response (no port number).");
173 /* Call our callback */
174 c->callback(c, c->state, c->response_headers, c->userdata);
178 static void line_callback(pa_ioline *line, const char *s, void *userdata) {
182 pa_rtsp_client *c = userdata;
185 pa_assert(c->callback);
188 /* Keep the ioline/iochannel open as they will be freed automatically */
191 c->callback(c, STATE_DISCONNECTED, NULL, c->userdata);
196 /* Trim trailing carriage returns */
197 s2p = s2 + strlen(s2) - 1;
198 while (s2p >= s2 && '\r' == *s2p) {
202 if (c->waiting && 0 == strcmp("RTSP/1.0 200 OK", s2)) {
204 if (c->response_headers)
205 pa_headerlist_free(c->response_headers);
206 c->response_headers = pa_headerlist_new();
210 pa_log_warn("Unexpected response: %s", s2);
215 /* We will have a header left from our looping iteration, so add it in :) */
216 if (c->last_header) {
217 /* This is not a continuation header so let's dump it into our proplist */
218 pa_headerlist_puts(c->response_headers, c->last_header, pa_strbuf_tostring_free(c->header_buffer));
219 pa_xfree(c->last_header);
220 c->last_header = NULL;
221 c->header_buffer= NULL;
224 pa_log_debug("Full response received. Dispatching");
230 /* Read and parse a header (we know it's not empty) */
231 /* TODO: Move header reading into the headerlist. */
233 /* If the first character is a space, it's a continuation header */
234 if (c->last_header && ' ' == s2[0]) {
235 pa_assert(c->header_buffer);
237 /* Add this line to the buffer (sans the space. */
238 pa_strbuf_puts(c->header_buffer, &(s2[1]));
242 if (c->last_header) {
243 /* This is not a continuation header so let's dump the full
244 header/value into our proplist */
245 pa_headerlist_puts(c->response_headers, c->last_header, pa_strbuf_tostring_free(c->header_buffer));
246 pa_xfree(c->last_header);
247 c->last_header = NULL;
248 c->header_buffer = NULL;
251 delimpos = strstr(s2, ":");
253 pa_log_warn("Unexpected response when expecting header: %s", s);
257 pa_assert(!c->header_buffer);
258 pa_assert(!c->last_header);
260 c->header_buffer = pa_strbuf_new();
261 if (strlen(delimpos) > 1) {
262 /* Cut our line off so we can copy the header name out */
265 /* Trim the front of any spaces */
266 while (' ' == *delimpos)
269 pa_strbuf_puts(c->header_buffer, delimpos);
271 /* Cut our line off so we can copy the header name out */
275 /* Save the header name */
276 c->last_header = pa_xstrdup(s2);
282 static void on_connection(pa_socket_client *sc, pa_iochannel *io, void *userdata) {
283 pa_rtsp_client *c = userdata;
286 struct sockaddr_in in;
287 struct sockaddr_in6 in6;
289 socklen_t sa_len = sizeof(sa);
293 pa_assert(STATE_CONNECT == c->state);
294 pa_assert(c->sc == sc);
295 pa_socket_client_unref(c->sc);
299 pa_log("Connection failed: %s", pa_cstrerror(errno));
305 c->ioline = pa_ioline_new(io);
306 pa_ioline_set_callback(c->ioline, line_callback, c);
308 /* Get the local IP address for use externally */
309 if (0 == getsockname(pa_iochannel_get_recv_fd(io), &sa.sa, &sa_len)) {
310 char buf[INET6_ADDRSTRLEN];
311 const char *res = NULL;
313 if (AF_INET == sa.sa.sa_family) {
314 if ((res = inet_ntop(sa.sa.sa_family, &sa.in.sin_addr, buf, sizeof(buf)))) {
315 c->localip = pa_xstrdup(res);
317 } else if (AF_INET6 == sa.sa.sa_family) {
318 if ((res = inet_ntop(AF_INET6, &sa.in6.sin6_addr, buf, sizeof(buf)))) {
319 c->localip = pa_sprintf_malloc("[%s]", res);
323 pa_log_debug("Established RTSP connection from local ip %s", c->localip);
326 c->callback(c, c->state, NULL, c->userdata);
329 int pa_rtsp_connect(pa_rtsp_client *c) {
333 pa_xfree(c->session);
336 if (!(c->sc = pa_socket_client_new_string(c->mainloop, c->hostname, c->port))) {
337 pa_log("failed to connect to server '%s:%d'", c->hostname, c->port);
341 pa_socket_client_set_callback(c->sc, on_connection, c);
343 c->state = STATE_CONNECT;
347 void pa_rtsp_set_callback(pa_rtsp_client *c, pa_rtsp_cb_t callback, void *userdata) {
350 c->callback = callback;
351 c->userdata = userdata;
354 void pa_rtsp_disconnect(pa_rtsp_client *c) {
358 pa_ioline_close(c->ioline);
360 pa_iochannel_free(c->io);
366 const char* pa_rtsp_localip(pa_rtsp_client* c) {
372 uint32_t pa_rtsp_serverport(pa_rtsp_client* c) {
378 void pa_rtsp_set_url(pa_rtsp_client* c, const char* url) {
381 c->url = pa_xstrdup(url);
384 void pa_rtsp_add_header(pa_rtsp_client *c, const char* key, const char* value)
390 pa_headerlist_puts(c->headers, key, value);
393 void pa_rtsp_remove_header(pa_rtsp_client *c, const char* key)
398 pa_headerlist_remove(c->headers, key);
401 static int rtsp_exec(pa_rtsp_client* c, const char* cmd,
402 const char* content_type, const char* content,
404 pa_headerlist* headers) {
415 pa_log_debug("Sending command: %s", cmd);
417 buf = pa_strbuf_new();
418 pa_strbuf_printf(buf, "%s %s RTSP/1.0\r\nCSeq: %d\r\n", cmd, c->url, ++c->cseq);
420 pa_strbuf_printf(buf, "Session: %s\r\n", c->session);
422 /* Add the headers */
424 hdrs = pa_headerlist_to_string(headers);
425 pa_strbuf_puts(buf, hdrs);
429 if (content_type && content) {
430 pa_strbuf_printf(buf, "Content-Type: %s\r\nContent-Length: %d\r\n",
431 content_type, (int)strlen(content));
434 pa_strbuf_printf(buf, "User-Agent: %s\r\n", c->useragent);
437 hdrs = pa_headerlist_to_string(c->headers);
438 pa_strbuf_puts(buf, hdrs);
442 pa_strbuf_puts(buf, "\r\n");
444 if (content_type && content) {
445 pa_strbuf_puts(buf, content);
448 /* Our packet is created... now we can send it :) */
449 hdrs = pa_strbuf_tostring_free(buf);
450 /*pa_log_debug("Submitting request:");
451 pa_log_debug(hdrs);*/
452 l = pa_iochannel_write(c->io, hdrs, strlen(hdrs));
459 int pa_rtsp_announce(pa_rtsp_client *c, const char* sdp) {
464 c->state = STATE_ANNOUNCE;
465 return rtsp_exec(c, "ANNOUNCE", "application/sdp", sdp, 1, NULL);
469 int pa_rtsp_setup(pa_rtsp_client* 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 = rtsp_exec(c, "SETUP", NULL, NULL, 1, headers);
480 pa_headerlist_free(headers);
485 int pa_rtsp_record(pa_rtsp_client* c, uint16_t* seq, uint32_t* rtptime) {
486 pa_headerlist* headers;
492 /* No session in progress */
496 /* Todo: Generate these values randomly as per spec */
499 headers = pa_headerlist_new();
500 pa_headerlist_puts(headers, "Range", "npt=0-");
501 info = pa_sprintf_malloc("seq=%u;rtptime=%u", *seq, *rtptime);
502 pa_headerlist_puts(headers, "RTP-Info", info);
505 c->state = STATE_RECORD;
506 rv = rtsp_exec(c, "RECORD", NULL, NULL, 1, headers);
507 pa_headerlist_free(headers);
512 int pa_rtsp_teardown(pa_rtsp_client *c) {
515 c->state = STATE_TEARDOWN;
516 return rtsp_exec(c, "TEARDOWN", NULL, NULL, 0, NULL);
520 int pa_rtsp_setparameter(pa_rtsp_client *c, const char* param) {
525 c->state = STATE_SET_PARAMETER;
526 return rtsp_exec(c, "SET_PARAMETER", "text/parameters", param, 1, NULL);
530 int pa_rtsp_flush(pa_rtsp_client *c, uint16_t seq, uint32_t rtptime) {
531 pa_headerlist* headers;
537 headers = pa_headerlist_new();
538 info = pa_sprintf_malloc("seq=%u;rtptime=%u", seq, rtptime);
539 pa_headerlist_puts(headers, "RTP-Info", info);
542 c->state = STATE_FLUSH;
543 rv = rtsp_exec(c, "FLUSH", NULL, NULL, 1, headers);
544 pa_headerlist_free(headers);