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;
65 pa_rtsp_cb_t callback;
68 const char *useragent;
73 pa_headerlist* headers;
75 pa_strbuf *header_buffer;
76 pa_headerlist* response_headers;
86 pa_rtsp_client* pa_rtsp_client_new(pa_mainloop_api *mainloop, const char* hostname, uint16_t port, const char* useragent) {
93 c = pa_xnew0(pa_rtsp_client, 1);
94 c->mainloop = mainloop;
95 c->hostname = pa_xstrdup(hostname);
97 c->headers = pa_headerlist_new();
100 c->useragent = useragent;
102 c->useragent = "PulseAudio RTSP Client";
108 void pa_rtsp_client_free(pa_rtsp_client* c) {
112 pa_socket_client_unref(c->sc);
114 pa_rtsp_disconnect(c);
116 pa_xfree(c->hostname);
118 pa_xfree(c->localip);
119 pa_xfree(c->session);
120 pa_xfree(c->transport);
121 pa_xfree(c->last_header);
122 if (c->header_buffer)
123 pa_strbuf_free(c->header_buffer);
124 if (c->response_headers)
125 pa_headerlist_free(c->response_headers);
126 pa_headerlist_free(c->headers);
132 static void headers_read(pa_rtsp_client *c) {
134 char delimiters[] = ";";
137 pa_assert(c->response_headers);
138 pa_assert(c->callback);
140 /* Deal with a SETUP response */
141 if (STATE_SETUP == c->state) {
142 const char* token_state = NULL;
143 const char* pc = NULL;
144 c->session = pa_xstrdup(pa_headerlist_gets(c->response_headers, "Session"));
145 c->transport = pa_xstrdup(pa_headerlist_gets(c->response_headers, "Transport"));
147 if (!c->session || !c->transport) {
148 pa_log("Invalid SETUP response.");
152 /* Now parse out the server port component of the response. */
153 while ((token = pa_split(c->transport, delimiters, &token_state))) {
154 if ((pc = strstr(token, "="))) {
155 if (0 == strncmp(token, "server_port", 11)) {
156 pa_atou(pc+1, (uint32_t*)(&c->rtp_port));
163 if (0 == c->rtp_port) {
164 /* Error no server_port in response */
165 pa_log("Invalid SETUP response (no port number).");
170 /* Call our callback */
171 c->callback(c, c->state, c->response_headers, c->userdata);
175 static void line_callback(pa_ioline *line, const char *s, void *userdata) {
179 pa_rtsp_client *c = userdata;
182 pa_assert(c->callback);
185 /* Keep the ioline/iochannel open as they will be freed automatically */
187 c->callback(c, STATE_DISCONNECTED, NULL, c->userdata);
192 /* Trim trailing carriage returns */
193 s2p = s2 + strlen(s2) - 1;
194 while (s2p >= s2 && '\r' == *s2p) {
198 if (c->waiting && 0 == strcmp("RTSP/1.0 200 OK", s2)) {
200 if (c->response_headers)
201 pa_headerlist_free(c->response_headers);
202 c->response_headers = pa_headerlist_new();
206 pa_log_warn("Unexpected response: %s", s2);
211 /* We will have a header left from our looping iteration, so add it in :) */
212 if (c->last_header) {
213 char *tmp = pa_strbuf_tostring_free(c->header_buffer);
214 /* This is not a continuation header so let's dump it into our proplist */
215 pa_headerlist_puts(c->response_headers, c->last_header, tmp);
217 pa_xfree(c->last_header);
218 c->last_header = NULL;
219 c->header_buffer = NULL;
222 pa_log_debug("Full response received. Dispatching");
228 /* Read and parse a header (we know it's not empty) */
229 /* TODO: Move header reading into the headerlist. */
231 /* If the first character is a space, it's a continuation header */
232 if (c->last_header && ' ' == s2[0]) {
233 pa_assert(c->header_buffer);
235 /* Add this line to the buffer (sans the space. */
236 pa_strbuf_puts(c->header_buffer, &(s2[1]));
240 if (c->last_header) {
241 char *tmp = pa_strbuf_tostring_free(c->header_buffer);
242 /* This is not a continuation header so let's dump the full
243 header/value into our proplist */
244 pa_headerlist_puts(c->response_headers, c->last_header, tmp);
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));
302 pa_assert(!c->ioline);
304 c->ioline = pa_ioline_new(io);
305 pa_ioline_set_callback(c->ioline, line_callback, c);
307 /* Get the local IP address for use externally */
308 if (0 == getsockname(pa_iochannel_get_recv_fd(io), &sa.sa, &sa_len)) {
309 char buf[INET6_ADDRSTRLEN];
310 const char *res = NULL;
312 if (AF_INET == sa.sa.sa_family) {
313 if ((res = inet_ntop(sa.sa.sa_family, &sa.in.sin_addr, buf, sizeof(buf)))) {
314 c->localip = pa_xstrdup(res);
316 } else if (AF_INET6 == sa.sa.sa_family) {
317 if ((res = inet_ntop(AF_INET6, &sa.in6.sin6_addr, buf, sizeof(buf)))) {
318 c->localip = pa_sprintf_malloc("[%s]", res);
322 pa_log_debug("Established RTSP connection from local ip %s", c->localip);
325 c->callback(c, c->state, NULL, c->userdata);
328 int pa_rtsp_connect(pa_rtsp_client *c) {
332 pa_xfree(c->session);
335 if (!(c->sc = pa_socket_client_new_string(c->mainloop, TRUE, c->hostname, c->port))) {
336 pa_log("failed to connect to server '%s:%d'", c->hostname, c->port);
340 pa_socket_client_set_callback(c->sc, on_connection, c);
342 c->state = STATE_CONNECT;
346 void pa_rtsp_set_callback(pa_rtsp_client *c, pa_rtsp_cb_t callback, void *userdata) {
349 c->callback = callback;
350 c->userdata = userdata;
353 void pa_rtsp_disconnect(pa_rtsp_client *c) {
357 pa_ioline_close(c->ioline);
362 const char* pa_rtsp_localip(pa_rtsp_client* c) {
368 uint32_t pa_rtsp_serverport(pa_rtsp_client* c) {
374 void pa_rtsp_set_url(pa_rtsp_client* c, const char* url) {
377 c->url = pa_xstrdup(url);
380 void pa_rtsp_add_header(pa_rtsp_client *c, const char* key, const char* value)
386 pa_headerlist_puts(c->headers, key, value);
389 void pa_rtsp_remove_header(pa_rtsp_client *c, const char* key)
394 pa_headerlist_remove(c->headers, key);
397 static int rtsp_exec(pa_rtsp_client* c, const char* cmd,
398 const char* content_type, const char* content,
400 pa_headerlist* headers) {
407 pa_assert(c->ioline);
409 pa_log_debug("Sending command: %s", cmd);
411 buf = pa_strbuf_new();
412 pa_strbuf_printf(buf, "%s %s RTSP/1.0\r\nCSeq: %d\r\n", cmd, c->url, ++c->cseq);
414 pa_strbuf_printf(buf, "Session: %s\r\n", c->session);
416 /* Add the headers */
418 hdrs = pa_headerlist_to_string(headers);
419 pa_strbuf_puts(buf, hdrs);
423 if (content_type && content) {
424 pa_strbuf_printf(buf, "Content-Type: %s\r\nContent-Length: %d\r\n",
425 content_type, (int)strlen(content));
428 pa_strbuf_printf(buf, "User-Agent: %s\r\n", c->useragent);
431 hdrs = pa_headerlist_to_string(c->headers);
432 pa_strbuf_puts(buf, hdrs);
436 pa_strbuf_puts(buf, "\r\n");
438 if (content_type && content) {
439 pa_strbuf_puts(buf, content);
442 /* Our packet is created... now we can send it :) */
443 hdrs = pa_strbuf_tostring_free(buf);
444 /*pa_log_debug("Submitting request:");
445 pa_log_debug(hdrs);*/
446 pa_ioline_puts(c->ioline, hdrs);
453 int pa_rtsp_announce(pa_rtsp_client *c, const char* sdp) {
458 c->state = STATE_ANNOUNCE;
459 return rtsp_exec(c, "ANNOUNCE", "application/sdp", sdp, 1, NULL);
463 int pa_rtsp_setup(pa_rtsp_client* c) {
464 pa_headerlist* headers;
469 headers = pa_headerlist_new();
470 pa_headerlist_puts(headers, "Transport", "RTP/AVP/TCP;unicast;interleaved=0-1;mode=record");
472 c->state = STATE_SETUP;
473 rv = rtsp_exec(c, "SETUP", NULL, NULL, 1, headers);
474 pa_headerlist_free(headers);
479 int pa_rtsp_record(pa_rtsp_client* c, uint16_t* seq, uint32_t* rtptime) {
480 pa_headerlist* headers;
486 /* No session in progress */
490 /* Todo: Generate these values randomly as per spec */
493 headers = pa_headerlist_new();
494 pa_headerlist_puts(headers, "Range", "npt=0-");
495 info = pa_sprintf_malloc("seq=%u;rtptime=%u", *seq, *rtptime);
496 pa_headerlist_puts(headers, "RTP-Info", info);
499 c->state = STATE_RECORD;
500 rv = rtsp_exec(c, "RECORD", NULL, NULL, 1, headers);
501 pa_headerlist_free(headers);
506 int pa_rtsp_teardown(pa_rtsp_client *c) {
509 c->state = STATE_TEARDOWN;
510 return rtsp_exec(c, "TEARDOWN", NULL, NULL, 0, NULL);
514 int pa_rtsp_setparameter(pa_rtsp_client *c, const char* param) {
519 c->state = STATE_SET_PARAMETER;
520 return rtsp_exec(c, "SET_PARAMETER", "text/parameters", param, 1, NULL);
524 int pa_rtsp_flush(pa_rtsp_client *c, uint16_t seq, uint32_t rtptime) {
525 pa_headerlist* headers;
531 headers = pa_headerlist_new();
532 info = pa_sprintf_malloc("seq=%u;rtptime=%u", seq, rtptime);
533 pa_headerlist_puts(headers, "RTP-Info", info);
536 c->state = STATE_FLUSH;
537 rv = rtsp_exec(c, "FLUSH", NULL, NULL, 1, headers);
538 pa_headerlist_free(headers);