Fix up according to Coding Style
[pulseaudio-mirror.git] / src / modules / rtp / rtsp_client.c
blob72702cba6f40a4a30f9bf86f56a37161ca97cb26
1 /***
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
19 USA.
20 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <fcntl.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <arpa/inet.h>
31 #include <unistd.h>
32 #include <sys/ioctl.h>
33 #include <netinet/in.h>
35 #ifdef HAVE_SYS_FILIO_H
36 #include <sys/filio.h>
37 #endif
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;
54 char *hostname;
55 uint16_t port;
57 pa_socket_client *sc;
58 pa_ioline *ioline;
60 pa_rtsp_cb_t callback;
62 void *userdata;
63 const char *useragent;
65 pa_rtsp_state state;
66 uint8_t waiting;
68 pa_headerlist* headers;
69 char *last_header;
70 pa_strbuf *header_buffer;
71 pa_headerlist* response_headers;
73 char *localip;
74 char *url;
75 uint16_t rtp_port;
76 uint32_t cseq;
77 char *session;
78 char *transport;
81 pa_rtsp_client* pa_rtsp_client_new(pa_mainloop_api *mainloop, const char* hostname, uint16_t port, const char* useragent) {
82 pa_rtsp_client *c;
84 pa_assert(mainloop);
85 pa_assert(hostname);
86 pa_assert(port > 0);
88 c = pa_xnew0(pa_rtsp_client, 1);
89 c->mainloop = mainloop;
90 c->hostname = pa_xstrdup(hostname);
91 c->port = port;
92 c->headers = pa_headerlist_new();
94 if (useragent)
95 c->useragent = useragent;
96 else
97 c->useragent = "PulseAudio RTSP Client";
99 return c;
103 void pa_rtsp_client_free(pa_rtsp_client* c) {
104 pa_assert(c);
106 if (c->sc)
107 pa_socket_client_unref(c->sc);
109 pa_rtsp_disconnect(c);
111 pa_xfree(c->hostname);
112 pa_xfree(c->url);
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);
123 pa_xfree(c);
127 static void headers_read(pa_rtsp_client *c) {
128 char* token;
129 char delimiters[] = ";";
131 pa_assert(c);
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.");
144 return;
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));
152 pa_xfree(token);
153 break;
156 pa_xfree(token);
158 if (0 == c->rtp_port) {
159 /* Error no server_port in response */
160 pa_log("Invalid SETUP response (no port number).");
161 return;
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) {
171 char *delimpos;
172 char *s2, *s2p;
174 pa_rtsp_client *c = userdata;
175 pa_assert(line);
176 pa_assert(c);
177 pa_assert(c->callback);
179 if (!s) {
180 /* Keep the ioline/iochannel open as they will be freed automatically */
181 c->ioline = NULL;
182 c->callback(c, STATE_DISCONNECTED, NULL, c->userdata);
183 return;
186 s2 = pa_xstrdup(s);
187 /* Trim trailing carriage returns */
188 s2p = s2 + strlen(s2) - 1;
189 while (s2p >= s2 && '\r' == *s2p) {
190 *s2p = '\0';
191 s2p -= 1;
193 if (c->waiting && 0 == strcmp("RTSP/1.0 200 OK", s2)) {
194 c->waiting = 0;
195 if (c->response_headers)
196 pa_headerlist_free(c->response_headers);
197 c->response_headers = pa_headerlist_new();
198 goto exit;
200 if (c->waiting) {
201 pa_log_warn("Unexpected response: %s", s2);
202 goto exit;;
204 if (!strlen(s2)) {
205 /* End of headers */
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);
211 pa_xfree(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");
218 headers_read(c);
219 c->waiting = 1;
220 goto exit;
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]));
232 goto exit;
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);
240 pa_xfree(tmp);
241 pa_xfree(c->last_header);
242 c->last_header = NULL;
243 c->header_buffer = NULL;
246 delimpos = strstr(s2, ":");
247 if (!delimpos) {
248 pa_log_warn("Unexpected response when expecting header: %s", s);
249 goto exit;
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 */
258 *delimpos++ = '\0';
260 /* Trim the front of any spaces */
261 while (' ' == *delimpos)
262 ++delimpos;
264 pa_strbuf_puts(c->header_buffer, delimpos);
265 } else {
266 /* Cut our line off so we can copy the header name out */
267 *delimpos = '\0';
270 /* Save the header name */
271 c->last_header = pa_xstrdup(s2);
272 exit:
273 pa_xfree(s2);
277 static void on_connection(pa_socket_client *sc, pa_iochannel *io, void *userdata) {
278 pa_rtsp_client *c = userdata;
279 union {
280 struct sockaddr sa;
281 struct sockaddr_in in;
282 struct sockaddr_in6 in6;
283 } sa;
284 socklen_t sa_len = sizeof(sa);
286 pa_assert(sc);
287 pa_assert(c);
288 pa_assert(STATE_CONNECT == c->state);
289 pa_assert(c->sc == sc);
290 pa_socket_client_unref(c->sc);
291 c->sc = NULL;
293 if (!io) {
294 pa_log("Connection failed: %s", pa_cstrerror(errno));
295 return;
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);
319 if (c->callback)
320 c->callback(c, c->state, NULL, c->userdata);
323 int pa_rtsp_connect(pa_rtsp_client *c) {
324 pa_assert(c);
325 pa_assert(!c->sc);
327 pa_xfree(c->session);
328 c->session = NULL;
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);
332 return -1;
335 pa_socket_client_set_callback(c->sc, on_connection, c);
336 c->waiting = 1;
337 c->state = STATE_CONNECT;
338 return 0;
341 void pa_rtsp_set_callback(pa_rtsp_client *c, pa_rtsp_cb_t callback, void *userdata) {
342 pa_assert(c);
344 c->callback = callback;
345 c->userdata = userdata;
348 void pa_rtsp_disconnect(pa_rtsp_client *c) {
349 pa_assert(c);
351 if (c->ioline)
352 pa_ioline_close(c->ioline);
353 c->ioline = NULL;
357 const char* pa_rtsp_localip(pa_rtsp_client* c) {
358 pa_assert(c);
360 return c->localip;
363 uint32_t pa_rtsp_serverport(pa_rtsp_client* c) {
364 pa_assert(c);
366 return c->rtp_port;
369 void pa_rtsp_set_url(pa_rtsp_client* c, const char* url) {
370 pa_assert(c);
372 c->url = pa_xstrdup(url);
375 void pa_rtsp_add_header(pa_rtsp_client *c, const char* key, const char* value) {
376 pa_assert(c);
377 pa_assert(key);
378 pa_assert(value);
380 pa_headerlist_puts(c->headers, key, value);
383 void pa_rtsp_remove_header(pa_rtsp_client *c, const char* key) {
384 pa_assert(c);
385 pa_assert(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,
392 int expect_response,
393 pa_headerlist* headers) {
394 pa_strbuf* buf;
395 char* hdrs;
397 pa_assert(c);
398 pa_assert(c->url);
399 pa_assert(cmd);
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);
406 if (c->session)
407 pa_strbuf_printf(buf, "Session: %s\r\n", c->session);
409 /* Add the headers */
410 if (headers) {
411 hdrs = pa_headerlist_to_string(headers);
412 pa_strbuf_puts(buf, hdrs);
413 pa_xfree(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);
423 if (c->headers) {
424 hdrs = pa_headerlist_to_string(c->headers);
425 pa_strbuf_puts(buf, hdrs);
426 pa_xfree(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);
440 pa_xfree(hdrs);
442 return 0;
446 int pa_rtsp_announce(pa_rtsp_client *c, const char* sdp) {
447 pa_assert(c);
448 if (!sdp)
449 return -1;
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;
458 int rv;
460 pa_assert(c);
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);
468 return rv;
472 int pa_rtsp_record(pa_rtsp_client* c, uint16_t* seq, uint32_t* rtptime) {
473 pa_headerlist* headers;
474 int rv;
475 char *info;
477 pa_assert(c);
478 if (!c->session) {
479 /* No session in progress */
480 return -1;
483 /* Todo: Generate these values randomly as per spec */
484 *seq = *rtptime = 0;
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);
490 pa_xfree(info);
492 c->state = STATE_RECORD;
493 rv = rtsp_exec(c, "RECORD", NULL, NULL, 1, headers);
494 pa_headerlist_free(headers);
495 return rv;
499 int pa_rtsp_teardown(pa_rtsp_client *c) {
500 pa_assert(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) {
508 pa_assert(c);
509 if (!param)
510 return -1;
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;
519 int rv;
520 char *info;
522 pa_assert(c);
524 headers = pa_headerlist_new();
525 info = pa_sprintf_malloc("seq=%u;rtptime=%u", seq, rtptime);
526 pa_headerlist_puts(headers, "RTP-Info", info);
527 pa_xfree(info);
529 c->state = STATE_FLUSH;
530 rv = rtsp_exec(c, "FLUSH", NULL, NULL, 1, headers);
531 pa_headerlist_free(headers);
532 return rv;