Sending translation for Finnish
[profile/ivi/pulseaudio.git] / src / modules / rtp / rtsp_client.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright 2008 Colin Guthrie
5
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.
10
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.
15
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 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
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
34 #ifdef HAVE_SYS_FILIO_H
35 #include <sys/filio.h>
36 #endif
37
38 #include <pulse/xmalloc.h>
39
40 #include <pulsecore/core-error.h>
41 #include <pulsecore/core-util.h>
42 #include <pulsecore/socket-util.h>
43 #include <pulsecore/log.h>
44 #include <pulsecore/macro.h>
45 #include <pulsecore/strbuf.h>
46 #include <pulsecore/ioline.h>
47
48 #ifdef HAVE_POLL_H
49 #include <poll.h>
50 #else
51 #include <pulsecore/poll.h>
52 #endif
53
54 #include "rtsp_client.h"
55
56 struct pa_rtsp_client {
57     pa_mainloop_api *mainloop;
58     char *hostname;
59     uint16_t port;
60
61     pa_socket_client *sc;
62     pa_iochannel *io;
63     pa_ioline *ioline;
64
65     pa_rtsp_cb_t callback;
66
67     void *userdata;
68     const char *useragent;
69
70     pa_rtsp_state state;
71     uint8_t waiting;
72
73     pa_headerlist* headers;
74     char *last_header;
75     pa_strbuf *header_buffer;
76     pa_headerlist* response_headers;
77
78     char *localip;
79     char *url;
80     uint16_t rtp_port;
81     uint32_t cseq;
82     char *session;
83     char *transport;
84 };
85
86 pa_rtsp_client* pa_rtsp_client_new(pa_mainloop_api *mainloop, const char* hostname, uint16_t port, const char* useragent) {
87     pa_rtsp_client *c;
88
89     pa_assert(mainloop);
90     pa_assert(hostname);
91     pa_assert(port > 0);
92
93     c = pa_xnew0(pa_rtsp_client, 1);
94     c->mainloop = mainloop;
95     c->hostname = pa_xstrdup(hostname);
96     c->port = port;
97     c->headers = pa_headerlist_new();
98
99     if (useragent)
100         c->useragent = useragent;
101     else
102         c->useragent = "PulseAudio RTSP Client";
103
104     return c;
105 }
106
107
108 void pa_rtsp_client_free(pa_rtsp_client* c) {
109     pa_assert(c);
110
111     if (c->sc)
112         pa_socket_client_unref(c->sc);
113     if (c->ioline)
114         pa_ioline_close(c->ioline);
115     else if (c->io)
116         pa_iochannel_free(c->io);
117
118     pa_xfree(c->hostname);
119     pa_xfree(c->url);
120     pa_xfree(c->localip);
121     pa_xfree(c->session);
122     pa_xfree(c->transport);
123     pa_xfree(c->last_header);
124     if (c->header_buffer)
125         pa_strbuf_free(c->header_buffer);
126     if (c->response_headers)
127         pa_headerlist_free(c->response_headers);
128     pa_headerlist_free(c->headers);
129
130     pa_xfree(c);
131 }
132
133
134 static void headers_read(pa_rtsp_client *c) {
135     char* token;
136     char delimiters[] = ";";
137
138     pa_assert(c);
139     pa_assert(c->response_headers);
140     pa_assert(c->callback);
141
142     /* Deal with a SETUP response */
143     if (STATE_SETUP == c->state) {
144         const char* token_state = NULL;
145         const char* pc = NULL;
146         c->session = pa_xstrdup(pa_headerlist_gets(c->response_headers, "Session"));
147         c->transport = pa_xstrdup(pa_headerlist_gets(c->response_headers, "Transport"));
148
149         if (!c->session || !c->transport) {
150             pa_log("Invalid SETUP response.");
151             return;
152         }
153
154         /* Now parse out the server port component of the response. */
155         while ((token = pa_split(c->transport, delimiters, &token_state))) {
156             if ((pc = strstr(token, "="))) {
157                 if (0 == strncmp(token, "server_port", 11)) {
158                     pa_atou(pc+1, (uint32_t*)(&c->rtp_port));
159                     pa_xfree(token);
160                     break;
161                 }
162             }
163             pa_xfree(token);
164         }
165         if (0 == c->rtp_port) {
166             /* Error no server_port in response */
167             pa_log("Invalid SETUP response (no port number).");
168             return;
169         }
170     }
171
172     /* Call our callback */
173     c->callback(c, c->state, c->response_headers, c->userdata);
174 }
175
176
177 static void line_callback(pa_ioline *line, const char *s, void *userdata) {
178     char *delimpos;
179     char *s2, *s2p;
180
181     pa_rtsp_client *c = userdata;
182     pa_assert(line);
183     pa_assert(c);
184     pa_assert(c->callback);
185
186     if (!s) {
187         /* Keep the ioline/iochannel open as they will be freed automatically */
188         c->ioline = NULL;
189         c->io = NULL;
190         c->callback(c, STATE_DISCONNECTED, NULL, c->userdata);
191         return;
192     }
193
194     s2 = pa_xstrdup(s);
195     /* Trim trailing carriage returns */
196     s2p = s2 + strlen(s2) - 1;
197     while (s2p >= s2 && '\r' == *s2p) {
198         *s2p = '\0';
199         s2p -= 1;
200     }
201     if (c->waiting && 0 == strcmp("RTSP/1.0 200 OK", s2)) {
202         c->waiting = 0;
203         if (c->response_headers)
204             pa_headerlist_free(c->response_headers);
205         c->response_headers = pa_headerlist_new();
206         goto exit;
207     }
208     if (c->waiting) {
209         pa_log_warn("Unexpected response: %s", s2);
210         goto exit;;
211     }
212     if (!strlen(s2)) {
213         /* End of headers */
214         /* We will have a header left from our looping itteration, so add it in :) */
215         if (c->last_header) {
216             /* This is not a continuation header so let's dump it into our proplist */
217             pa_headerlist_puts(c->response_headers, c->last_header, pa_strbuf_tostring_free(c->header_buffer));
218             pa_xfree(c->last_header);
219             c->last_header = NULL;
220             c->header_buffer= NULL;
221         }
222
223         pa_log_debug("Full response received. Dispatching");
224         headers_read(c);
225         c->waiting = 1;
226         goto exit;
227     }
228
229     /* Read and parse a header (we know it's not empty) */
230     /* TODO: Move header reading into the headerlist. */
231
232     /* If the first character is a space, it's a continuation header */
233     if (c->last_header && ' ' == s2[0]) {
234         pa_assert(c->header_buffer);
235
236         /* Add this line to the buffer (sans the space. */
237         pa_strbuf_puts(c->header_buffer, &(s2[1]));
238         goto exit;
239     }
240
241     if (c->last_header) {
242         /* This is not a continuation header so let's dump the full
243           header/value into our proplist */
244         pa_headerlist_puts(c->response_headers, c->last_header, pa_strbuf_tostring_free(c->header_buffer));
245         pa_xfree(c->last_header);
246         c->last_header = NULL;
247         c->header_buffer = NULL;
248     }
249
250     delimpos = strstr(s2, ":");
251     if (!delimpos) {
252         pa_log_warn("Unexpected response when expecting header: %s", s);
253         goto exit;
254     }
255
256     pa_assert(!c->header_buffer);
257     pa_assert(!c->last_header);
258
259     c->header_buffer = pa_strbuf_new();
260     if (strlen(delimpos) > 1) {
261         /* Cut our line off so we can copy the header name out */
262         *delimpos++ = '\0';
263
264         /* Trim the front of any spaces */
265         while (' ' == *delimpos)
266             ++delimpos;
267
268         pa_strbuf_puts(c->header_buffer, delimpos);
269     } else {
270         /* Cut our line off so we can copy the header name out */
271         *delimpos = '\0';
272     }
273
274     /* Save the header name */
275     c->last_header = pa_xstrdup(s2);
276   exit:
277     pa_xfree(s2);
278 }
279
280
281 static void on_connection(pa_socket_client *sc, pa_iochannel *io, void *userdata) {
282     pa_rtsp_client *c = userdata;
283     union {
284         struct sockaddr sa;
285         struct sockaddr_in in;
286         struct sockaddr_in6 in6;
287     } sa;
288     socklen_t sa_len = sizeof(sa);
289
290     pa_assert(sc);
291     pa_assert(c);
292     pa_assert(STATE_CONNECT == c->state);
293     pa_assert(c->sc == sc);
294     pa_socket_client_unref(c->sc);
295     c->sc = NULL;
296
297     if (!io) {
298         pa_log("Connection failed: %s", pa_cstrerror(errno));
299         return;
300     }
301     pa_assert(!c->io);
302     c->io = io;
303
304     c->ioline = pa_ioline_new(io);
305     pa_ioline_set_callback(c->ioline, line_callback, c);
306
307     /* Get the local IP address for use externally */
308     if (0 == getsockname(pa_iochannel_get_recv_fd(io), &sa.sa, &sa_len)) {
309         char buf[INET6_ADDRSTRLEN];
310         const char *res = NULL;
311
312         if (AF_INET == sa.sa.sa_family) {
313             if ((res = inet_ntop(sa.sa.sa_family, &sa.in.sin_addr, buf, sizeof(buf)))) {
314                 c->localip = pa_xstrdup(res);
315             }
316         } else if (AF_INET6 == sa.sa.sa_family) {
317             if ((res = inet_ntop(AF_INET6, &sa.in6.sin6_addr, buf, sizeof(buf)))) {
318                 c->localip = pa_sprintf_malloc("[%s]", res);
319             }
320         }
321     }
322     pa_log_debug("Established RTSP connection from local ip %s", c->localip);
323
324     if (c->callback)
325         c->callback(c, c->state, NULL, c->userdata);
326 }
327
328 int pa_rtsp_connect(pa_rtsp_client *c) {
329     pa_assert(c);
330     pa_assert(!c->sc);
331
332     pa_xfree(c->session);
333     c->session = NULL;
334
335     if (!(c->sc = pa_socket_client_new_string(c->mainloop, c->hostname, c->port))) {
336         pa_log("failed to connect to server '%s:%d'", c->hostname, c->port);
337         return -1;
338     }
339
340     pa_socket_client_set_callback(c->sc, on_connection, c);
341     c->waiting = 1;
342     c->state = STATE_CONNECT;
343     return 0;
344 }
345
346 void pa_rtsp_set_callback(pa_rtsp_client *c, pa_rtsp_cb_t callback, void *userdata) {
347     pa_assert(c);
348
349     c->callback = callback;
350     c->userdata = userdata;
351 }
352
353 void pa_rtsp_disconnect(pa_rtsp_client *c) {
354     pa_assert(c);
355
356     if (c->ioline)
357         pa_ioline_close(c->ioline);
358     else if (c->io)
359         pa_iochannel_free(c->io);
360     c->io = NULL;
361     c->ioline = NULL;
362 }
363
364
365 const char* pa_rtsp_localip(pa_rtsp_client* c) {
366     pa_assert(c);
367
368     return c->localip;
369 }
370
371 uint32_t pa_rtsp_serverport(pa_rtsp_client* c) {
372     pa_assert(c);
373
374     return c->rtp_port;
375 }
376
377 void pa_rtsp_set_url(pa_rtsp_client* c, const char* url) {
378     pa_assert(c);
379
380     c->url = pa_xstrdup(url);
381 }
382
383 void pa_rtsp_add_header(pa_rtsp_client *c, const char* key, const char* value)
384 {
385     pa_assert(c);
386     pa_assert(key);
387     pa_assert(value);
388
389     pa_headerlist_puts(c->headers, key, value);
390 }
391
392 void pa_rtsp_remove_header(pa_rtsp_client *c, const char* key)
393 {
394     pa_assert(c);
395     pa_assert(key);
396
397     pa_headerlist_remove(c->headers, key);
398 }
399
400 static int rtsp_exec(pa_rtsp_client* c, const char* cmd,
401                         const char* content_type, const char* content,
402                         int expect_response,
403                         pa_headerlist* headers) {
404     pa_strbuf* buf;
405     char* hdrs;
406     ssize_t l;
407
408     pa_assert(c);
409     pa_assert(c->url);
410
411     if (!cmd)
412         return -1;
413
414     pa_log_debug("Sending command: %s", cmd);
415
416     buf = pa_strbuf_new();
417     pa_strbuf_printf(buf, "%s %s RTSP/1.0\r\nCSeq: %d\r\n", cmd, c->url, ++c->cseq);
418     if (c->session)
419         pa_strbuf_printf(buf, "Session: %s\r\n", c->session);
420
421     /* Add the headers */
422     if (headers) {
423         hdrs = pa_headerlist_to_string(headers);
424         pa_strbuf_puts(buf, hdrs);
425         pa_xfree(hdrs);
426     }
427
428     if (content_type && content) {
429         pa_strbuf_printf(buf, "Content-Type: %s\r\nContent-Length: %d\r\n",
430           content_type, (int)strlen(content));
431     }
432
433     pa_strbuf_printf(buf, "User-Agent: %s\r\n", c->useragent);
434
435     if (c->headers) {
436         hdrs = pa_headerlist_to_string(c->headers);
437         pa_strbuf_puts(buf, hdrs);
438         pa_xfree(hdrs);
439     }
440
441     pa_strbuf_puts(buf, "\r\n");
442
443     if (content_type && content) {
444         pa_strbuf_puts(buf, content);
445     }
446
447     /* Our packet is created... now we can send it :) */
448     hdrs = pa_strbuf_tostring_free(buf);
449     /*pa_log_debug("Submitting request:");
450     pa_log_debug(hdrs);*/
451     l = pa_iochannel_write(c->io, hdrs, strlen(hdrs));
452     pa_xfree(hdrs);
453
454     return 0;
455 }
456
457
458 int pa_rtsp_announce(pa_rtsp_client *c, const char* sdp) {
459     pa_assert(c);
460     if (!sdp)
461         return -1;
462
463     c->state = STATE_ANNOUNCE;
464     return rtsp_exec(c, "ANNOUNCE", "application/sdp", sdp, 1, NULL);
465 }
466
467
468 int pa_rtsp_setup(pa_rtsp_client* c) {
469     pa_headerlist* headers;
470     int rv;
471
472     pa_assert(c);
473
474     headers = pa_headerlist_new();
475     pa_headerlist_puts(headers, "Transport", "RTP/AVP/TCP;unicast;interleaved=0-1;mode=record");
476
477     c->state = STATE_SETUP;
478     rv = rtsp_exec(c, "SETUP", NULL, NULL, 1, headers);
479     pa_headerlist_free(headers);
480     return rv;
481 }
482
483
484 int pa_rtsp_record(pa_rtsp_client* c, uint16_t* seq, uint32_t* rtptime) {
485     pa_headerlist* headers;
486     int rv;
487     char *info;
488
489     pa_assert(c);
490     if (!c->session) {
491         /* No seesion in progres */
492         return -1;
493     }
494
495     /* Todo: Generate these values randomly as per spec */
496     *seq = *rtptime = 0;
497
498     headers = pa_headerlist_new();
499     pa_headerlist_puts(headers, "Range", "npt=0-");
500     info = pa_sprintf_malloc("seq=%u;rtptime=%u", *seq, *rtptime);
501     pa_headerlist_puts(headers, "RTP-Info", info);
502     pa_xfree(info);
503
504     c->state = STATE_RECORD;
505     rv = rtsp_exec(c, "RECORD", NULL, NULL, 1, headers);
506     pa_headerlist_free(headers);
507     return rv;
508 }
509
510
511 int pa_rtsp_teardown(pa_rtsp_client *c) {
512     pa_assert(c);
513
514     c->state = STATE_TEARDOWN;
515     return rtsp_exec(c, "TEARDOWN", NULL, NULL, 0, NULL);
516 }
517
518
519 int pa_rtsp_setparameter(pa_rtsp_client *c, const char* param) {
520     pa_assert(c);
521     if (!param)
522         return -1;
523
524     c->state = STATE_SET_PARAMETER;
525     return rtsp_exec(c, "SET_PARAMETER", "text/parameters", param, 1, NULL);
526 }
527
528
529 int pa_rtsp_flush(pa_rtsp_client *c, uint16_t seq, uint32_t rtptime) {
530     pa_headerlist* headers;
531     int rv;
532     char *info;
533
534     pa_assert(c);
535
536     headers = pa_headerlist_new();
537     info = pa_sprintf_malloc("seq=%u;rtptime=%u", seq, rtptime);
538     pa_headerlist_puts(headers, "RTP-Info", info);
539     pa_xfree(info);
540
541     c->state = STATE_FLUSH;
542     rv = rtsp_exec(c, "FLUSH", NULL, NULL, 1, headers);
543     pa_headerlist_free(headers);
544     return rv;
545 }