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(int fd
, char *line
, int maxlen
, int timeout
)
69 for (i
=0; i
<maxlen
; ++i
) {
70 if (!poll(&pfds
, 1, timeout
))
73 rval
= read(fd
, &ch
, 1);
78 //ERRMSG("%s:read error: %s\n", __func__, strerror(errno));
83 //INFMSG("%s:disconnected on the other end\n", __func__);
98 if (count
>= maxlen
-1)
107 static int pa_rtsp_exec(pa_rtsp_context
* c
, const char* cmd
,
108 const char* content_type
, const char* content
,
110 pa_headerlist
* headers
, pa_headerlist
** response_headers
) {
117 const char* token_state
;
129 buf
= pa_strbuf_new();
130 pa_strbuf_printf(buf
, "%s %s RTSP/1.0\r\nCSeq: %d\r\n", cmd
, c
->url
, ++c
->cseq
);
132 pa_strbuf_printf(buf
, "Session: %s\r\n", c
->session
);
136 hdrs
= pa_headerlist_to_string(headers
);
137 pa_strbuf_puts(buf
, hdrs
);
141 if (content_type
&& content
) {
142 pa_strbuf_printf(buf
, "Content-Type: %s\r\nContent-Length: %d\r\n",
143 content_type
, (int)strlen(content
));
146 pa_strbuf_printf(buf
, "User-Agent: %s\r\n", c
->useragent
);
149 hdrs
= pa_headerlist_to_string(c
->headers
);
150 pa_strbuf_puts(buf
, hdrs
);
154 pa_strbuf_puts(buf
, "\r\n");
156 if (content_type
&& content
) {
157 pa_strbuf_puts(buf
, content
);
160 // Our packet is created... now we can send it :)
161 hdrs
= pa_strbuf_tostring_free(buf
);
162 l
= pa_write(c
->fd
, hdrs
, strlen(hdrs
), NULL
);
165 // Do we expect a response?
166 if (!expect_response
)
170 if (pa_read_line(c
->fd
, response
, sizeof(response
), timeout
) <= 0) {
171 //ERRMSG("%s: request failed\n",__func__);
176 delimiters
[1] = '\0';
178 pa_xfree(pa_split(response
, delimiters
, &token_state
));
179 token
= pa_split(response
, delimiters
, &token_state
);
180 if (!token
|| strcmp(token
, "200")) {
182 //ERRMSG("%s: request failed, error %s\n",__func__,token);
187 // We want to return the headers?
188 if (!response_headers
)
190 // We have no storage, so just clear out the response.
191 while (pa_read_line(c
->fd
, response
, sizeof(response
), timeout
) > 0) {
192 // Reduce timeout for future requests
199 buf
= pa_strbuf_new();
200 while (pa_read_line(c
->fd
, response
, sizeof(response
), timeout
) > 0) {
201 // Reduce timeout for future requests
204 // If the first character is a space, it's a continuation header
205 if (header
&& ' ' == response
[0]) {
206 // Add this line to the buffer (sans the space.
207 pa_strbuf_puts(buf
, &(response
[1]));
212 // This is not a continuation header so let's dump the full header/value into our proplist
213 pa_headerlist_puts(*response_headers
, header
, pa_strbuf_tostring_free(buf
));
216 buf
= pa_strbuf_new();
219 delimpos
= strstr(response
, ":");
221 //ERRMSG("%s: Request failed, bad header\n",__func__);
225 if (strlen(delimpos
) > 1) {
226 // Cut our line off so we can copy the header name out
229 // Trim the front of any spaces
230 while (' ' == *delimpos
)
233 pa_strbuf_puts(buf
, delimpos
);
235 // Cut our line off so we can copy the header name out
239 // Save the header name
240 header
= pa_xstrdup(response
);
242 // We will have a header left from our looping itteration, so add it in :)
244 // This is not a continuation header so let's dump it into our proplist
245 pa_headerlist_puts(*response_headers
, header
, pa_strbuf_tostring(buf
));
253 pa_rtsp_context
* pa_rtsp_context_new(const char* useragent
) {
256 c
= pa_xnew0(pa_rtsp_context
, 1);
258 c
->headers
= pa_headerlist_new();
261 c
->useragent
= useragent
;
263 c
->useragent
= "PulseAudio RTSP Client";
269 void pa_rtsp_context_destroy(pa_rtsp_context
* c
) {
272 pa_xfree(c
->session
);
273 pa_xfree(c
->transport
);
274 pa_headerlist_free(c
->headers
);
280 int pa_rtsp_connect(pa_rtsp_context
*c
, const char* hostname
, uint16_t port
, const char* sid
) {
281 struct sockaddr_in sa
;
282 struct sockaddr_in name
;
283 socklen_t namelen
= sizeof(name
);
284 struct hostent
*host
= NULL
;
292 memset(&sa
, 0, sizeof(sa
));
293 sa
.sin_family
= AF_INET
;
294 sa
.sin_port
= htons(port
);
296 host
= gethostbyname(hostname
);
298 unsigned int addr
= inet_addr(hostname
);
299 if (addr
!= INADDR_NONE
)
300 host
= gethostbyaddr((char*)&addr
, 4, AF_INET
);
304 memcpy(&sa
.sin_addr
, host
->h_addr
, sizeof(struct in_addr
));
306 if ((c
->fd
= socket(sa
.sin_family
, SOCK_STREAM
, 0)) < 0) {
307 pa_log("socket(): %s", pa_cstrerror(errno
));
311 // Q: is FD_CLOEXEC reqd?
312 pa_make_fd_cloexec(c
->fd
);
313 pa_make_tcp_socket_low_delay(c
->fd
);
315 if ((r
= connect(c
->fd
, &sa
, sizeof(struct sockaddr_in
))) < 0) {
317 if (WSAGetLastError() != EWOULDBLOCK
) {
318 pa_log_debug("connect(): %d", WSAGetLastError());
320 if (errno
!= EINPROGRESS
) {
321 pa_log_debug("connect(): %s (%d)", pa_cstrerror(errno
), errno
);
329 if (0 != getsockname(c
->fd
, (struct sockaddr
*)&name
, &namelen
)) {
334 memcpy(&c
->local_addr
, &name
.sin_addr
, sizeof(struct in_addr
));
335 c
->url
= pa_sprintf_malloc("rtsp://%s/%s", inet_ntoa(name
.sin_addr
), sid
);
341 void pa_rtsp_disconnect(pa_rtsp_context
*c
) {
351 const char* pa_rtsp_localip(pa_rtsp_context
* c
) {
356 return inet_ntoa(c
->local_addr
);
360 int pa_rtsp_announce(pa_rtsp_context
*c
, const char* sdp
) {
365 return pa_rtsp_exec(c
, "ANNOUNCE", "application/sdp", sdp
, 1, NULL
, NULL
);
369 int pa_rtsp_setup(pa_rtsp_context
* c
, pa_headerlist
** response_headers
) {
370 pa_headerlist
* headers
;
371 pa_headerlist
* rheaders
;
374 const char* token_state
;
379 headers
= pa_headerlist_new();
380 rheaders
= pa_headerlist_new();
381 pa_headerlist_puts(headers
, "Transport", "RTP/AVP/TCP;unicast;interleaved=0-1;mode=record");
383 if (!pa_rtsp_exec(c
, "SETUP", NULL
, NULL
, 1, headers
, &rheaders
)) {
384 pa_headerlist_free(headers
);
385 pa_headerlist_free(rheaders
);
388 pa_headerlist_free(headers
);
390 c
->session
= pa_xstrdup(pa_headerlist_gets(rheaders
, "Session"));
391 c
->transport
= pa_xstrdup(pa_headerlist_gets(rheaders
, "Transport"));
393 if (!c
->session
|| !c
->transport
) {
394 pa_headerlist_free(rheaders
);
398 // Now parse out the server port component of the response.
401 delimiters
[1] = '\0';
403 while ((token
= pa_split(c
->transport
, delimiters
, &token_state
))) {
404 if ((pc
= strstr(token
, "="))) {
405 if (0 == strncmp(token
, "server_port", 11)) {
406 pa_atou(pc
+1, &c
->port
);
414 // Error no server_port in response
415 pa_headerlist_free(rheaders
);
419 *response_headers
= rheaders
;
424 int pa_rtsp_record(pa_rtsp_context
* c
) {
425 pa_headerlist
* headers
;
430 // No seesion in progres
434 headers
= pa_headerlist_new();
435 pa_headerlist_puts(headers
, "Range", "npt=0-");
436 pa_headerlist_puts(headers
, "RTP-Info", "seq=0;rtptime=0");
438 rv
= pa_rtsp_exec(c
, "RECORD", NULL
, NULL
, 1, headers
, NULL
);
439 pa_headerlist_free(headers
);
444 int pa_rtsp_teardown(pa_rtsp_context
*c
) {
447 return pa_rtsp_exec(c
, "TEARDOWN", NULL
, NULL
, 0, NULL
, NULL
);
451 int pa_rtsp_setparameter(pa_rtsp_context
*c
, const char* param
) {
456 return pa_rtsp_exec(c
, "SET_PARAMETER", "text/parameters", param
, 1, NULL
, NULL
);
460 int pa_rtsp_flush(pa_rtsp_context
*c
) {
461 pa_headerlist
* headers
;
466 headers
= pa_headerlist_new();
467 pa_headerlist_puts(headers
, "RTP-Info", "seq=0;rtptime=0");
469 rv
= pa_rtsp_exec(c
, "FLUSH", NULL
, NULL
, 1, headers
, NULL
);
470 pa_headerlist_free(headers
);