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>
48 #include <pulsecore/poll.h>
50 #include "rtsp_client.h"
52 struct pa_rtsp_client
{
53 pa_mainloop_api
*mainloop
;
60 pa_rtsp_cb_t callback
;
63 const char *useragent
;
68 pa_headerlist
* headers
;
70 pa_strbuf
*header_buffer
;
71 pa_headerlist
* response_headers
;
81 pa_rtsp_client
* pa_rtsp_client_new(pa_mainloop_api
*mainloop
, const char* hostname
, uint16_t port
, const char* useragent
) {
88 c
= pa_xnew0(pa_rtsp_client
, 1);
89 c
->mainloop
= mainloop
;
90 c
->hostname
= pa_xstrdup(hostname
);
92 c
->headers
= pa_headerlist_new();
95 c
->useragent
= useragent
;
97 c
->useragent
= "PulseAudio RTSP Client";
103 void pa_rtsp_client_free(pa_rtsp_client
* c
) {
107 pa_socket_client_unref(c
->sc
);
109 pa_rtsp_disconnect(c
);
111 pa_xfree(c
->hostname
);
113 pa_xfree(c
->localip
);
114 pa_xfree(c
->session
);
115 pa_xfree(c
->transport
);
116 pa_xfree(c
->last_header
);
117 if (c
->header_buffer
)
118 pa_strbuf_free(c
->header_buffer
);
119 if (c
->response_headers
)
120 pa_headerlist_free(c
->response_headers
);
121 pa_headerlist_free(c
->headers
);
127 static void headers_read(pa_rtsp_client
*c
) {
129 char delimiters
[] = ";";
132 pa_assert(c
->response_headers
);
133 pa_assert(c
->callback
);
135 /* Deal with a SETUP response */
136 if (STATE_SETUP
== c
->state
) {
137 const char* token_state
= NULL
;
138 const char* pc
= NULL
;
139 c
->session
= pa_xstrdup(pa_headerlist_gets(c
->response_headers
, "Session"));
140 c
->transport
= pa_xstrdup(pa_headerlist_gets(c
->response_headers
, "Transport"));
142 if (!c
->session
|| !c
->transport
) {
143 pa_log("Invalid SETUP response.");
147 /* Now parse out the server port component of the response. */
148 while ((token
= pa_split(c
->transport
, delimiters
, &token_state
))) {
149 if ((pc
= strstr(token
, "="))) {
150 if (0 == strncmp(token
, "server_port", 11)) {
151 pa_atou(pc
+1, (uint32_t*)(&c
->rtp_port
));
158 if (0 == c
->rtp_port
) {
159 /* Error no server_port in response */
160 pa_log("Invalid SETUP response (no port number).");
165 /* Call our callback */
166 c
->callback(c
, c
->state
, c
->response_headers
, c
->userdata
);
170 static void line_callback(pa_ioline
*line
, const char *s
, void *userdata
) {
174 pa_rtsp_client
*c
= userdata
;
177 pa_assert(c
->callback
);
180 /* Keep the ioline/iochannel open as they will be freed automatically */
182 c
->callback(c
, STATE_DISCONNECTED
, NULL
, c
->userdata
);
187 /* Trim trailing carriage returns */
188 s2p
= s2
+ strlen(s2
) - 1;
189 while (s2p
>= s2
&& '\r' == *s2p
) {
193 if (c
->waiting
&& 0 == strcmp("RTSP/1.0 200 OK", s2
)) {
195 if (c
->response_headers
)
196 pa_headerlist_free(c
->response_headers
);
197 c
->response_headers
= pa_headerlist_new();
201 pa_log_warn("Unexpected response: %s", s2
);
206 /* We will have a header left from our looping iteration, so add it in :) */
207 if (c
->last_header
) {
208 char *tmp
= pa_strbuf_tostring_free(c
->header_buffer
);
209 /* This is not a continuation header so let's dump it into our proplist */
210 pa_headerlist_puts(c
->response_headers
, c
->last_header
, tmp
);
212 pa_xfree(c
->last_header
);
213 c
->last_header
= NULL
;
214 c
->header_buffer
= NULL
;
217 pa_log_debug("Full response received. Dispatching");
223 /* Read and parse a header (we know it's not empty) */
224 /* TODO: Move header reading into the headerlist. */
226 /* If the first character is a space, it's a continuation header */
227 if (c
->last_header
&& ' ' == s2
[0]) {
228 pa_assert(c
->header_buffer
);
230 /* Add this line to the buffer (sans the space. */
231 pa_strbuf_puts(c
->header_buffer
, &(s2
[1]));
235 if (c
->last_header
) {
236 char *tmp
= pa_strbuf_tostring_free(c
->header_buffer
);
237 /* This is not a continuation header so let's dump the full
238 header/value into our proplist */
239 pa_headerlist_puts(c
->response_headers
, c
->last_header
, tmp
);
241 pa_xfree(c
->last_header
);
242 c
->last_header
= NULL
;
243 c
->header_buffer
= NULL
;
246 delimpos
= strstr(s2
, ":");
248 pa_log_warn("Unexpected response when expecting header: %s", s
);
252 pa_assert(!c
->header_buffer
);
253 pa_assert(!c
->last_header
);
255 c
->header_buffer
= pa_strbuf_new();
256 if (strlen(delimpos
) > 1) {
257 /* Cut our line off so we can copy the header name out */
260 /* Trim the front of any spaces */
261 while (' ' == *delimpos
)
264 pa_strbuf_puts(c
->header_buffer
, delimpos
);
266 /* Cut our line off so we can copy the header name out */
270 /* Save the header name */
271 c
->last_header
= pa_xstrdup(s2
);
277 static void on_connection(pa_socket_client
*sc
, pa_iochannel
*io
, void *userdata
) {
278 pa_rtsp_client
*c
= userdata
;
281 struct sockaddr_in in
;
282 struct sockaddr_in6 in6
;
284 socklen_t sa_len
= sizeof(sa
);
288 pa_assert(STATE_CONNECT
== c
->state
);
289 pa_assert(c
->sc
== sc
);
290 pa_socket_client_unref(c
->sc
);
294 pa_log("Connection failed: %s", pa_cstrerror(errno
));
297 pa_assert(!c
->ioline
);
299 c
->ioline
= pa_ioline_new(io
);
300 pa_ioline_set_callback(c
->ioline
, line_callback
, c
);
302 /* Get the local IP address for use externally */
303 if (0 == getsockname(pa_iochannel_get_recv_fd(io
), &sa
.sa
, &sa_len
)) {
304 char buf
[INET6_ADDRSTRLEN
];
305 const char *res
= NULL
;
307 if (AF_INET
== sa
.sa
.sa_family
) {
308 if ((res
= inet_ntop(sa
.sa
.sa_family
, &sa
.in
.sin_addr
, buf
, sizeof(buf
)))) {
309 c
->localip
= pa_xstrdup(res
);
311 } else if (AF_INET6
== sa
.sa
.sa_family
) {
312 if ((res
= inet_ntop(AF_INET6
, &sa
.in6
.sin6_addr
, buf
, sizeof(buf
)))) {
313 c
->localip
= pa_sprintf_malloc("[%s]", res
);
317 pa_log_debug("Established RTSP connection from local ip %s", c
->localip
);
320 c
->callback(c
, c
->state
, NULL
, c
->userdata
);
323 int pa_rtsp_connect(pa_rtsp_client
*c
) {
327 pa_xfree(c
->session
);
330 if (!(c
->sc
= pa_socket_client_new_string(c
->mainloop
, TRUE
, c
->hostname
, c
->port
))) {
331 pa_log("failed to connect to server '%s:%d'", c
->hostname
, c
->port
);
335 pa_socket_client_set_callback(c
->sc
, on_connection
, c
);
337 c
->state
= STATE_CONNECT
;
341 void pa_rtsp_set_callback(pa_rtsp_client
*c
, pa_rtsp_cb_t callback
, void *userdata
) {
344 c
->callback
= callback
;
345 c
->userdata
= userdata
;
348 void pa_rtsp_disconnect(pa_rtsp_client
*c
) {
352 pa_ioline_close(c
->ioline
);
357 const char* pa_rtsp_localip(pa_rtsp_client
* c
) {
363 uint32_t pa_rtsp_serverport(pa_rtsp_client
* c
) {
369 void pa_rtsp_set_url(pa_rtsp_client
* c
, const char* url
) {
372 c
->url
= pa_xstrdup(url
);
375 void pa_rtsp_add_header(pa_rtsp_client
*c
, const char* key
, const char* value
) {
380 pa_headerlist_puts(c
->headers
, key
, value
);
383 void pa_rtsp_remove_header(pa_rtsp_client
*c
, const char* key
) {
387 pa_headerlist_remove(c
->headers
, key
);
390 static int rtsp_exec(pa_rtsp_client
* c
, const char* cmd
,
391 const char* content_type
, const char* content
,
393 pa_headerlist
* headers
) {
400 pa_assert(c
->ioline
);
402 pa_log_debug("Sending command: %s", cmd
);
404 buf
= pa_strbuf_new();
405 pa_strbuf_printf(buf
, "%s %s RTSP/1.0\r\nCSeq: %d\r\n", cmd
, c
->url
, ++c
->cseq
);
407 pa_strbuf_printf(buf
, "Session: %s\r\n", c
->session
);
409 /* Add the headers */
411 hdrs
= pa_headerlist_to_string(headers
);
412 pa_strbuf_puts(buf
, hdrs
);
416 if (content_type
&& content
) {
417 pa_strbuf_printf(buf
, "Content-Type: %s\r\nContent-Length: %d\r\n",
418 content_type
, (int)strlen(content
));
421 pa_strbuf_printf(buf
, "User-Agent: %s\r\n", c
->useragent
);
424 hdrs
= pa_headerlist_to_string(c
->headers
);
425 pa_strbuf_puts(buf
, hdrs
);
429 pa_strbuf_puts(buf
, "\r\n");
431 if (content_type
&& content
) {
432 pa_strbuf_puts(buf
, content
);
435 /* Our packet is created... now we can send it :) */
436 hdrs
= pa_strbuf_tostring_free(buf
);
437 /*pa_log_debug("Submitting request:");
438 pa_log_debug(hdrs);*/
439 pa_ioline_puts(c
->ioline
, hdrs
);
446 int pa_rtsp_announce(pa_rtsp_client
*c
, const char* sdp
) {
451 c
->state
= STATE_ANNOUNCE
;
452 return rtsp_exec(c
, "ANNOUNCE", "application/sdp", sdp
, 1, NULL
);
456 int pa_rtsp_setup(pa_rtsp_client
* c
) {
457 pa_headerlist
* headers
;
462 headers
= pa_headerlist_new();
463 pa_headerlist_puts(headers
, "Transport", "RTP/AVP/TCP;unicast;interleaved=0-1;mode=record");
465 c
->state
= STATE_SETUP
;
466 rv
= rtsp_exec(c
, "SETUP", NULL
, NULL
, 1, headers
);
467 pa_headerlist_free(headers
);
472 int pa_rtsp_record(pa_rtsp_client
* c
, uint16_t* seq
, uint32_t* rtptime
) {
473 pa_headerlist
* headers
;
479 /* No session in progress */
483 /* Todo: Generate these values randomly as per spec */
486 headers
= pa_headerlist_new();
487 pa_headerlist_puts(headers
, "Range", "npt=0-");
488 info
= pa_sprintf_malloc("seq=%u;rtptime=%u", *seq
, *rtptime
);
489 pa_headerlist_puts(headers
, "RTP-Info", info
);
492 c
->state
= STATE_RECORD
;
493 rv
= rtsp_exec(c
, "RECORD", NULL
, NULL
, 1, headers
);
494 pa_headerlist_free(headers
);
499 int pa_rtsp_teardown(pa_rtsp_client
*c
) {
502 c
->state
= STATE_TEARDOWN
;
503 return rtsp_exec(c
, "TEARDOWN", NULL
, NULL
, 0, NULL
);
507 int pa_rtsp_setparameter(pa_rtsp_client
*c
, const char* param
) {
512 c
->state
= STATE_SET_PARAMETER
;
513 return rtsp_exec(c
, "SET_PARAMETER", "text/parameters", param
, 1, NULL
);
517 int pa_rtsp_flush(pa_rtsp_client
*c
, uint16_t seq
, uint32_t rtptime
) {
518 pa_headerlist
* headers
;
524 headers
= pa_headerlist_new();
525 info
= pa_sprintf_malloc("seq=%u;rtptime=%u", seq
, rtptime
);
526 pa_headerlist_puts(headers
, "RTP-Info", info
);
529 c
->state
= STATE_FLUSH
;
530 rv
= rtsp_exec(c
, "FLUSH", NULL
, NULL
, 1, headers
);
531 pa_headerlist_free(headers
);