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>
53 * read one line from the file descriptor
54 * timeout: msec unit, -1 for infinite
55 * if CR comes then following LF is expected
56 * returned string in line is always null terminated, maxlen-1 is maximum string length
58 static int pa_read_line(pa_iochannel
* io
, char *line
, int maxlen
, int timeout
)
67 fd
= pa_iochannel_get_recv_fd(io
);
74 for (i
=0; i
<maxlen
; ++i
) {
75 if (!poll(&pfds
, 1, timeout
))
78 rval
= read(fd
, &ch
, 1);
83 /*ERRMSG("%s:read error: %s\n", __func__, strerror(errno));*/
88 /*INFMSG("%s:disconnected on the other end\n", __func__);*/
103 if (count
>= maxlen
-1)
112 static int pa_rtsp_exec(pa_rtsp_context
* c
, const char* cmd
,
113 const char* content_type
, const char* content
,
115 pa_headerlist
* headers
, pa_headerlist
** response_headers
) {
122 const char* token_state
;
134 buf
= pa_strbuf_new();
135 pa_strbuf_printf(buf
, "%s %s RTSP/1.0\r\nCSeq: %d\r\n", cmd
, c
->url
, ++c
->cseq
);
137 pa_strbuf_printf(buf
, "Session: %s\r\n", c
->session
);
139 /* Add the headers */
141 hdrs
= pa_headerlist_to_string(headers
);
142 pa_strbuf_puts(buf
, hdrs
);
146 if (content_type
&& content
) {
147 pa_strbuf_printf(buf
, "Content-Type: %s\r\nContent-Length: %d\r\n",
148 content_type
, (int)strlen(content
));
151 pa_strbuf_printf(buf
, "User-Agent: %s\r\n", c
->useragent
);
154 hdrs
= pa_headerlist_to_string(c
->headers
);
155 pa_strbuf_puts(buf
, hdrs
);
159 pa_strbuf_puts(buf
, "\r\n");
161 if (content_type
&& content
) {
162 pa_strbuf_puts(buf
, content
);
165 /* Our packet is created... now we can send it :) */
166 hdrs
= pa_strbuf_tostring_free(buf
);
167 l
= pa_iochannel_write(c
->io
, hdrs
, strlen(hdrs
));
170 /* Do we expect a response? */
171 if (!expect_response
)
175 if (pa_read_line(c
->io
, response
, sizeof(response
), timeout
) <= 0) {
176 /*ERRMSG("%s: request failed\n",__func__);*/
181 delimiters
[1] = '\0';
183 pa_xfree(pa_split(response
, delimiters
, &token_state
));
184 token
= pa_split(response
, delimiters
, &token_state
);
185 if (!token
|| strcmp(token
, "200")) {
187 /*ERRMSG("%s: request failed, error %s\n",__func__,token);*/
192 /* We want to return the headers? */
193 if (!response_headers
)
195 /* We have no storage, so just clear out the response. */
196 while (pa_read_line(c
->io
, response
, sizeof(response
), timeout
) > 0) {
197 /* Reduce timeout for future requests */
203 /* TODO: Move header reading into the headerlist. */
205 buf
= pa_strbuf_new();
206 while (pa_read_line(c
->io
, response
, sizeof(response
), timeout
) > 0) {
207 /* Reduce timeout for future requests */
210 /* If the first character is a space, it's a continuation header */
211 if (header
&& ' ' == response
[0]) {
212 /* Add this line to the buffer (sans the space. */
213 pa_strbuf_puts(buf
, &(response
[1]));
218 /* This is not a continuation header so let's dump the full
219 header/value into our proplist */
220 pa_headerlist_puts(*response_headers
, header
, pa_strbuf_tostring_free(buf
));
222 buf
= pa_strbuf_new();
225 delimpos
= strstr(response
, ":");
227 /*ERRMSG("%s: Request failed, bad header\n",__func__);*/
231 if (strlen(delimpos
) > 1) {
232 /* Cut our line off so we can copy the header name out */
235 /* Trim the front of any spaces */
236 while (' ' == *delimpos
)
239 pa_strbuf_puts(buf
, delimpos
);
241 /* Cut our line off so we can copy the header name out */
245 /* Save the header name */
246 header
= pa_xstrdup(response
);
248 /* We will have a header left from our looping itteration, so add it in :) */
250 /* This is not a continuation header so let's dump it into our proplist */
251 pa_headerlist_puts(*response_headers
, header
, pa_strbuf_tostring(buf
));
259 pa_rtsp_context
* pa_rtsp_context_new(const char* useragent
) {
262 c
= pa_xnew0(pa_rtsp_context
, 1);
263 c
->headers
= pa_headerlist_new();
266 c
->useragent
= useragent
;
268 c
->useragent
= "PulseAudio RTSP Client";
274 void pa_rtsp_context_free(pa_rtsp_context
* c
) {
277 pa_socket_client_unref(c
->sc
);
280 pa_xfree(c
->localip
);
281 pa_xfree(c
->session
);
282 pa_xfree(c
->transport
);
283 pa_headerlist_free(c
->headers
);
290 static void on_connection(pa_socket_client
*sc
, pa_iochannel
*io
, void *userdata
) {
291 pa_rtsp_context
*c
= userdata
;
294 struct sockaddr_in in
;
295 struct sockaddr_in6 in6
;
297 socklen_t sa_len
= sizeof(sa
);
301 pa_assert(c
->sc
== sc
);
303 pa_socket_client_unref(c
->sc
);
307 pa_log("Connection failed: %s", pa_cstrerror(errno
));
313 /* Get the local IP address for use externally */
314 if (0 == getsockname(pa_iochannel_get_recv_fd(io
), &sa
.sa
, &sa_len
)) {
315 char buf
[INET6_ADDRSTRLEN
];
316 const char *res
= NULL
;
318 if (AF_INET
== sa
.sa
.sa_family
) {
319 res
= inet_ntop(sa
.sa
.sa_family
, &sa
.in
.sin_addr
, buf
, sizeof(buf
));
320 } else if (AF_INET6
== sa
.sa
.sa_family
) {
321 res
= inet_ntop(AF_INET6
, &sa
.in6
.sin6_addr
, buf
, sizeof(buf
));
324 c
->localip
= pa_xstrdup(res
);
328 int pa_rtsp_connect(pa_rtsp_context
*c
, pa_mainloop_api
*mainloop
, const char* hostname
, uint16_t port
) {
334 if (!(c
->sc
= pa_socket_client_new_string(mainloop
, hostname
, port
))) {
335 pa_log("failed to connect to server '%s:%d'", hostname
, port
);
339 pa_socket_client_set_callback(c
->sc
, on_connection
, c
);
344 void pa_rtsp_disconnect(pa_rtsp_context
*c
) {
348 pa_iochannel_free(c
->io
);
353 const char* pa_rtsp_localip(pa_rtsp_context
* c
) {
360 void pa_rtsp_set_url(pa_rtsp_context
* c
, const char* url
) {
363 c
->url
= pa_xstrdup(url
);
366 int pa_rtsp_announce(pa_rtsp_context
*c
, const char* sdp
) {
371 return pa_rtsp_exec(c
, "ANNOUNCE", "application/sdp", sdp
, 1, NULL
, NULL
);
375 int pa_rtsp_setup(pa_rtsp_context
* c
, pa_headerlist
** response_headers
) {
376 pa_headerlist
* headers
;
377 pa_headerlist
* rheaders
;
380 const char* token_state
;
385 headers
= pa_headerlist_new();
386 rheaders
= pa_headerlist_new();
387 pa_headerlist_puts(headers
, "Transport", "RTP/AVP/TCP;unicast;interleaved=0-1;mode=record");
389 if (pa_rtsp_exec(c
, "SETUP", NULL
, NULL
, 1, headers
, &rheaders
)) {
390 pa_headerlist_free(headers
);
391 pa_headerlist_free(rheaders
);
394 pa_headerlist_free(headers
);
396 c
->session
= pa_xstrdup(pa_headerlist_gets(rheaders
, "Session"));
397 c
->transport
= pa_xstrdup(pa_headerlist_gets(rheaders
, "Transport"));
399 if (!c
->session
|| !c
->transport
) {
400 pa_headerlist_free(rheaders
);
404 /* Now parse out the server port component of the response. */
407 delimiters
[1] = '\0';
409 while ((token
= pa_split(c
->transport
, delimiters
, &token_state
))) {
410 if ((pc
= strstr(token
, "="))) {
411 if (0 == strncmp(token
, "server_port", 11)) {
412 pa_atou(pc
+1, &c
->port
);
420 /* Error no server_port in response */
421 pa_headerlist_free(rheaders
);
425 *response_headers
= rheaders
;
430 int pa_rtsp_record(pa_rtsp_context
* c
) {
431 pa_headerlist
* headers
;
436 /* No seesion in progres */
440 headers
= pa_headerlist_new();
441 pa_headerlist_puts(headers
, "Range", "npt=0-");
442 pa_headerlist_puts(headers
, "RTP-Info", "seq=0;rtptime=0");
444 rv
= pa_rtsp_exec(c
, "RECORD", NULL
, NULL
, 1, headers
, NULL
);
445 pa_headerlist_free(headers
);
450 int pa_rtsp_teardown(pa_rtsp_context
*c
) {
453 return pa_rtsp_exec(c
, "TEARDOWN", NULL
, NULL
, 0, NULL
, NULL
);
457 int pa_rtsp_setparameter(pa_rtsp_context
*c
, const char* param
) {
462 return pa_rtsp_exec(c
, "SET_PARAMETER", "text/parameters", param
, 1, NULL
, NULL
);
466 int pa_rtsp_flush(pa_rtsp_context
*c
) {
467 pa_headerlist
* headers
;
472 headers
= pa_headerlist_new();
473 pa_headerlist_puts(headers
, "RTP-Info", "seq=0;rtptime=0");
475 rv
= pa_rtsp_exec(c
, "FLUSH", NULL
, NULL
, 1, headers
, NULL
);
476 pa_headerlist_free(headers
);