]>
code.delx.au - pulseaudio/blob - src/modules/rtp/rtsp_client.c
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 char *tmp
= pa_strbuf_tostring_free(c
->header_buffer
);
218 /* This is not a continuation header so let's dump it into our proplist */
219 pa_headerlist_puts(c
->response_headers
, c
->last_header
, tmp
);
221 pa_xfree(c
->last_header
);
222 c
->last_header
= NULL
;
223 c
->header_buffer
= NULL
;
226 pa_log_debug("Full response received. Dispatching");
232 /* Read and parse a header (we know it's not empty) */
233 /* TODO: Move header reading into the headerlist. */
235 /* If the first character is a space, it's a continuation header */
236 if (c
->last_header
&& ' ' == s2
[0]) {
237 pa_assert(c
->header_buffer
);
239 /* Add this line to the buffer (sans the space. */
240 pa_strbuf_puts(c
->header_buffer
, &(s2
[1]));
244 if (c
->last_header
) {
245 char *tmp
= pa_strbuf_tostring_free(c
->header_buffer
);
246 /* This is not a continuation header so let's dump the full
247 header/value into our proplist */
248 pa_headerlist_puts(c
->response_headers
, c
->last_header
, tmp
);
250 pa_xfree(c
->last_header
);
251 c
->last_header
= NULL
;
252 c
->header_buffer
= NULL
;
255 delimpos
= strstr(s2
, ":");
257 pa_log_warn("Unexpected response when expecting header: %s", s
);
261 pa_assert(!c
->header_buffer
);
262 pa_assert(!c
->last_header
);
264 c
->header_buffer
= pa_strbuf_new();
265 if (strlen(delimpos
) > 1) {
266 /* Cut our line off so we can copy the header name out */
269 /* Trim the front of any spaces */
270 while (' ' == *delimpos
)
273 pa_strbuf_puts(c
->header_buffer
, delimpos
);
275 /* Cut our line off so we can copy the header name out */
279 /* Save the header name */
280 c
->last_header
= pa_xstrdup(s2
);
286 static void on_connection(pa_socket_client
*sc
, pa_iochannel
*io
, void *userdata
) {
287 pa_rtsp_client
*c
= userdata
;
290 struct sockaddr_in in
;
291 struct sockaddr_in6 in6
;
293 socklen_t sa_len
= sizeof(sa
);
297 pa_assert(STATE_CONNECT
== c
->state
);
298 pa_assert(c
->sc
== sc
);
299 pa_socket_client_unref(c
->sc
);
303 pa_log("Connection failed: %s", pa_cstrerror(errno
));
309 c
->ioline
= pa_ioline_new(io
);
310 pa_ioline_set_callback(c
->ioline
, line_callback
, c
);
312 /* Get the local IP address for use externally */
313 if (0 == getsockname(pa_iochannel_get_recv_fd(io
), &sa
.sa
, &sa_len
)) {
314 char buf
[INET6_ADDRSTRLEN
];
315 const char *res
= NULL
;
317 if (AF_INET
== sa
.sa
.sa_family
) {
318 if ((res
= inet_ntop(sa
.sa
.sa_family
, &sa
.in
.sin_addr
, buf
, sizeof(buf
)))) {
319 c
->localip
= pa_xstrdup(res
);
321 } else if (AF_INET6
== sa
.sa
.sa_family
) {
322 if ((res
= inet_ntop(AF_INET6
, &sa
.in6
.sin6_addr
, buf
, sizeof(buf
)))) {
323 c
->localip
= pa_sprintf_malloc("[%s]", res
);
327 pa_log_debug("Established RTSP connection from local ip %s", c
->localip
);
330 c
->callback(c
, c
->state
, NULL
, c
->userdata
);
333 int pa_rtsp_connect(pa_rtsp_client
*c
) {
337 pa_xfree(c
->session
);
340 if (!(c
->sc
= pa_socket_client_new_string(c
->mainloop
, TRUE
, c
->hostname
, c
->port
))) {
341 pa_log("failed to connect to server '%s:%d'", c
->hostname
, c
->port
);
345 pa_socket_client_set_callback(c
->sc
, on_connection
, c
);
347 c
->state
= STATE_CONNECT
;
351 void pa_rtsp_set_callback(pa_rtsp_client
*c
, pa_rtsp_cb_t callback
, void *userdata
) {
354 c
->callback
= callback
;
355 c
->userdata
= userdata
;
358 void pa_rtsp_disconnect(pa_rtsp_client
*c
) {
362 pa_ioline_close(c
->ioline
);
364 pa_iochannel_free(c
->io
);
370 const char* pa_rtsp_localip(pa_rtsp_client
* c
) {
376 uint32_t pa_rtsp_serverport(pa_rtsp_client
* c
) {
382 void pa_rtsp_set_url(pa_rtsp_client
* c
, const char* url
) {
385 c
->url
= pa_xstrdup(url
);
388 void pa_rtsp_add_header(pa_rtsp_client
*c
, const char* key
, const char* value
)
394 pa_headerlist_puts(c
->headers
, key
, value
);
397 void pa_rtsp_remove_header(pa_rtsp_client
*c
, const char* key
)
402 pa_headerlist_remove(c
->headers
, key
);
405 static int rtsp_exec(pa_rtsp_client
* c
, const char* cmd
,
406 const char* content_type
, const char* content
,
408 pa_headerlist
* headers
) {
419 pa_log_debug("Sending command: %s", cmd
);
421 buf
= pa_strbuf_new();
422 pa_strbuf_printf(buf
, "%s %s RTSP/1.0\r\nCSeq: %d\r\n", cmd
, c
->url
, ++c
->cseq
);
424 pa_strbuf_printf(buf
, "Session: %s\r\n", c
->session
);
426 /* Add the headers */
428 hdrs
= pa_headerlist_to_string(headers
);
429 pa_strbuf_puts(buf
, hdrs
);
433 if (content_type
&& content
) {
434 pa_strbuf_printf(buf
, "Content-Type: %s\r\nContent-Length: %d\r\n",
435 content_type
, (int)strlen(content
));
438 pa_strbuf_printf(buf
, "User-Agent: %s\r\n", c
->useragent
);
441 hdrs
= pa_headerlist_to_string(c
->headers
);
442 pa_strbuf_puts(buf
, hdrs
);
446 pa_strbuf_puts(buf
, "\r\n");
448 if (content_type
&& content
) {
449 pa_strbuf_puts(buf
, content
);
452 /* Our packet is created... now we can send it :) */
453 hdrs
= pa_strbuf_tostring_free(buf
);
454 /*pa_log_debug("Submitting request:");
455 pa_log_debug(hdrs);*/
456 l
= pa_iochannel_write(c
->io
, hdrs
, strlen(hdrs
));
459 /* FIXME: this is broken, not necessarily all bytes are written */
465 int pa_rtsp_announce(pa_rtsp_client
*c
, const char* sdp
) {
470 c
->state
= STATE_ANNOUNCE
;
471 return rtsp_exec(c
, "ANNOUNCE", "application/sdp", sdp
, 1, NULL
);
475 int pa_rtsp_setup(pa_rtsp_client
* c
) {
476 pa_headerlist
* headers
;
481 headers
= pa_headerlist_new();
482 pa_headerlist_puts(headers
, "Transport", "RTP/AVP/TCP;unicast;interleaved=0-1;mode=record");
484 c
->state
= STATE_SETUP
;
485 rv
= rtsp_exec(c
, "SETUP", NULL
, NULL
, 1, headers
);
486 pa_headerlist_free(headers
);
491 int pa_rtsp_record(pa_rtsp_client
* c
, uint16_t* seq
, uint32_t* rtptime
) {
492 pa_headerlist
* headers
;
498 /* No session in progress */
502 /* Todo: Generate these values randomly as per spec */
505 headers
= pa_headerlist_new();
506 pa_headerlist_puts(headers
, "Range", "npt=0-");
507 info
= pa_sprintf_malloc("seq=%u;rtptime=%u", *seq
, *rtptime
);
508 pa_headerlist_puts(headers
, "RTP-Info", info
);
511 c
->state
= STATE_RECORD
;
512 rv
= rtsp_exec(c
, "RECORD", NULL
, NULL
, 1, headers
);
513 pa_headerlist_free(headers
);
518 int pa_rtsp_teardown(pa_rtsp_client
*c
) {
521 c
->state
= STATE_TEARDOWN
;
522 return rtsp_exec(c
, "TEARDOWN", NULL
, NULL
, 0, NULL
);
526 int pa_rtsp_setparameter(pa_rtsp_client
*c
, const char* param
) {
531 c
->state
= STATE_SET_PARAMETER
;
532 return rtsp_exec(c
, "SET_PARAMETER", "text/parameters", param
, 1, NULL
);
536 int pa_rtsp_flush(pa_rtsp_client
*c
, uint16_t seq
, uint32_t rtptime
) {
537 pa_headerlist
* headers
;
543 headers
= pa_headerlist_new();
544 info
= pa_sprintf_malloc("seq=%u;rtptime=%u", seq
, rtptime
);
545 pa_headerlist_puts(headers
, "RTP-Info", info
);
548 c
->state
= STATE_FLUSH
;
549 rv
= rtsp_exec(c
, "FLUSH", NULL
, NULL
, 1, headers
);
550 pa_headerlist_free(headers
);