1 /*
2  *  Copyright (C) 2006 Benjamin Zores
3  *   based on the Freebox patch for xine by Vincent Mussard
4  *   but with many enhancements for better RTSP RFC compliance.
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software Foundation,
18  *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #define _BSD_SOURCE
22 
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include <inttypes.h>
30 
31 #include "config.h"
32 
33 #if !HAVE_WINSOCK2_H
34 #include <netdb.h>
35 #include <netinet/in.h>
36 #include <sys/socket.h>
37 #include <arpa/inet.h>
38 #else
39 #include <winsock2.h>
40 #include <ws2tcpip.h>
41 #endif
42 
43 #include "mp_msg.h"
44 #include "rtsp.h"
45 #include "rtsp_rtp.h"
46 #include "rtsp_session.h"
47 #include "stream/network.h"
48 #include "stream/freesdp/common.h"
49 #include "stream/freesdp/parser.h"
50 #include "libavutil/avstring.h"
51 
52 #define RTSP_DEFAULT_PORT 31336
53 #define MAX_LENGTH 256
54 
55 #define RTSP_ACCEPT_SDP "Accept: application/sdp"
56 #define RTSP_CONTENT_LENGTH "Content-length"
57 #define RTSP_CONTENT_TYPE "Content-Type"
58 #define RTSP_APPLICATION_SDP "application/sdp"
59 #define RTSP_RANGE "Range: "
60 #define RTSP_NPT_NOW "npt=now-"
61 #define RTSP_MEDIA_CONTAINER_MPEG_TS "33"
62 #define RTSP_TRANSPORT_REQUEST "Transport: RTP/AVP;%s;%s%i-%i;mode=\"PLAY\""
63 
64 #define RTSP_TRANSPORT_MULTICAST "multicast"
65 #define RTSP_TRANSPORT_UNICAST "unicast"
66 
67 #define RTSP_MULTICAST_PORT "port="
68 #define RTSP_UNICAST_CLIENT_PORT "client_port="
69 #define RTSP_UNICAST_SERVER_PORT "server_port="
70 #define RTSP_SETUP_DESTINATION "destination="
71 
72 #define RTSP_SESSION "Session"
73 #define RTSP_TRANSPORT "Transport"
74 
75 /* hardcoded RTCP RR - this is _NOT_ RFC compliant */
76 #define RTCP_RR_SIZE 32
77 #define RTCP_RR "\201\311\0\7(.JD\31+\306\343\0\0\0\0\0\0/E\0\0\2&\0\0\0\0\0\0\0\0\201"
78 #define RTCP_SEND_FREQUENCY 1024
79 
80 int rtsp_port = 0;
81 char *rtsp_destination = NULL;
82 
83 void
rtcp_send_rr(rtsp_t * s,struct rtp_rtsp_session_t * st)84 rtcp_send_rr (rtsp_t *s, struct rtp_rtsp_session_t *st)
85 {
86   if (st->rtcp_socket == -1)
87     return;
88 
89   /* send RTCP RR every RTCP_SEND_FREQUENCY packets
90    * FIXME : NOT CORRECT, HARDCODED, BUT MAKES SOME SERVERS HAPPY
91    * not rfc compliant
92    * http://www.faqs.org/rfcs/rfc1889.html chapter 6 for RTCP
93    */
94 
95   if (st->count == RTCP_SEND_FREQUENCY)
96   {
97     send (st->rtcp_socket, RTCP_RR, RTCP_RR_SIZE, DEFAULT_SEND_FLAGS);
98 
99     /* ping RTSP server to keep connection alive.
100        we use OPTIONS instead of PING as not all servers support it */
101     rtsp_request_options (s, "*");
102     st->count = 0;
103   }
104   else
105     st->count++;
106 }
107 
108 static struct rtp_rtsp_session_t *
rtp_session_new(void)109 rtp_session_new (void)
110 {
111   struct rtp_rtsp_session_t *st = NULL;
112 
113   st = malloc (sizeof (struct rtp_rtsp_session_t));
114 
115   st->rtp_socket = -1;
116   st->rtcp_socket = -1;
117   st->control_url = NULL;
118   st->count = 0;
119 
120   return st;
121 }
122 
123 void
rtp_session_free(struct rtp_rtsp_session_t * st)124 rtp_session_free (struct rtp_rtsp_session_t *st)
125 {
126   if (!st)
127     return;
128 
129   if (st->rtp_socket != -1)
130     close (st->rtp_socket);
131   if (st->rtcp_socket != -1)
132     close (st->rtcp_socket);
133 
134   free (st->control_url);
135   free (st);
136 }
137 
138 static void
rtp_session_set_fd(struct rtp_rtsp_session_t * st,int rtp_sock,int rtcp_sock)139 rtp_session_set_fd (struct rtp_rtsp_session_t *st,
140                     int rtp_sock, int rtcp_sock)
141 {
142   if (!st)
143     return;
144 
145   st->rtp_socket = rtp_sock;
146   st->rtcp_socket = rtcp_sock;
147 }
148 
149 static int
parse_port(const char * line,const char * param,int * rtp_port,int * rtcp_port)150 parse_port (const char *line, const char *param,
151             int *rtp_port, int *rtcp_port)
152 {
153   char *parse1;
154   char *parse2;
155   char *parse3;
156 
157   char *line_copy = strdup (line);
158 
159   parse1 = strstr (line_copy, param);
160 
161   if (parse1)
162   {
163     parse2 = strstr (parse1, "-");
164 
165     if (parse2)
166     {
167       parse3 = strstr (parse2, ";");
168 
169       if (parse3)
170 	parse3[0] = 0;
171 
172       parse2[0] = 0;
173     }
174     else
175     {
176       free (line_copy);
177       return 0;
178     }
179   }
180   else
181   {
182     free (line_copy);
183     return 0;
184   }
185 
186   *rtp_port = atoi (parse1 + strlen (param));
187   *rtcp_port = atoi (parse2 + 1);
188 
189   free (line_copy);
190 
191   return 1;
192 }
193 
194 static char *
parse_destination(const char * line)195 parse_destination (const char *line)
196 {
197   char *parse1;
198   char *parse2;
199 
200   char *dest = NULL;
201   char *line_copy = strdup (line);
202   int len;
203 
204   parse1 = strstr (line_copy, RTSP_SETUP_DESTINATION);
205   if (!parse1)
206   {
207     free (line_copy);
208     return NULL;
209   }
210 
211   parse2 = strstr (parse1, ";");
212   if (!parse2)
213   {
214     free (line_copy);
215     return NULL;
216   }
217 
218   len = strlen (parse1) - strlen (parse2)
219     - strlen (RTSP_SETUP_DESTINATION) + 1;
220   dest = (char *) malloc (len + 1);
221   av_strlcpy (dest, parse1 + strlen (RTSP_SETUP_DESTINATION), len);
222   free (line_copy);
223 
224   return dest;
225 }
226 
227 static int
rtcp_connect(int client_port,int server_port,const char * server_hostname)228 rtcp_connect (int client_port, int server_port, const char* server_hostname)
229 {
230   struct sockaddr_in sin;
231   struct hostent *hp;
232   int s;
233 
234   if (client_port <= 1023)
235     return -1;
236 
237   s = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP);
238   if (s == -1)
239     return -1;
240 
241   hp = gethostbyname (server_hostname);
242   if (!hp)
243   {
244     close (s);
245     return -1;
246   }
247 
248   memset(&sin, 0, sizeof(sin));
249   sin.sin_family = AF_INET;
250   sin.sin_addr.s_addr = INADDR_ANY;
251   sin.sin_port = htons (client_port);
252 
253   if (bind (s, (struct sockaddr *) &sin, sizeof (sin)))
254   {
255 #if !HAVE_WINSOCK2_H
256     if (errno != EINPROGRESS)
257 #else
258     if (WSAGetLastError() != WSAEINPROGRESS)
259 #endif
260     {
261       close (s);
262       return -1;
263     }
264   }
265 
266   sin.sin_family = AF_INET;
267   memcpy (&(sin.sin_addr.s_addr), hp->h_addr, hp->h_length);
268   sin.sin_port = htons (server_port);
269 
270   /* datagram socket */
271   if (connect (s, (struct sockaddr *) &sin, sizeof (sin)) < 0)
272   {
273     close (s);
274     return -1;
275   }
276 
277   return s;
278 }
279 
280 static int
rtp_connect(char * hostname,int port)281 rtp_connect (char *hostname, int port)
282 {
283   struct sockaddr_in sin;
284   struct timeval tv;
285   int err, err_len;
286   int rxsockbufsz;
287   int s;
288   fd_set set;
289 
290   if (port <= 1023)
291     return -1;
292 
293   s = socket (PF_INET, SOCK_DGRAM, 0);
294   if (s == -1)
295     return -1;
296 
297   memset(&sin, 0, sizeof(sin));
298   sin.sin_family = AF_INET;
299   if (!hostname || !strcmp (hostname, "0.0.0.0"))
300     sin.sin_addr.s_addr = htonl (INADDR_ANY);
301   else
302 #if HAVE_INET_PTON
303     inet_pton (AF_INET, hostname, &sin.sin_addr);
304 #elif HAVE_INET_ATON
305     inet_aton (hostname, &sin.sin_addr);
306 #elif HAVE_WINSOCK2_H
307     sin.sin_addr.s_addr = htonl (INADDR_ANY);
308 #endif
309   sin.sin_port = htons (port);
310 
311   /* Increase the socket rx buffer size to maximum -- this is UDP */
312   rxsockbufsz = 240 * 1024;
313   if (setsockopt (s, SOL_SOCKET, SO_RCVBUF,
314                   &rxsockbufsz, sizeof (rxsockbufsz)))
315     mp_msg (MSGT_OPEN, MSGL_ERR, "Couldn't set receive socket buffer size\n");
316 
317   /* if multicast address, add membership */
318   if ((ntohl (sin.sin_addr.s_addr) >> 28) == 0xe)
319   {
320     struct ip_mreq mcast;
321     mcast.imr_multiaddr.s_addr = sin.sin_addr.s_addr;
322     mcast.imr_interface.s_addr = 0;
323 
324     if (setsockopt (s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mcast, sizeof (mcast)))
325     {
326       mp_msg (MSGT_OPEN, MSGL_ERR, "IP_ADD_MEMBERSHIP failed\n");
327       close (s);
328       return -1;
329     }
330   }
331 
332   /* datagram socket */
333   if (bind (s, (struct sockaddr *) &sin, sizeof (sin)))
334   {
335 #if !HAVE_WINSOCK2_H
336     if (errno != EINPROGRESS)
337 #else
338     if (WSAGetLastError() != WSAEINPROGRESS)
339 #endif
340     {
341       mp_msg (MSGT_OPEN, MSGL_ERR, "bind: %s\n", strerror (errno));
342       close (s);
343       return -1;
344     }
345   }
346 
347   tv.tv_sec = 1; /* 1 second timeout */
348   tv.tv_usec = 0;
349 
350   FD_ZERO (&set);
351   FD_SET (s, &set);
352 
353   err = select (s + 1, &set, NULL, NULL, &tv);
354   if (err < 0)
355   {
356     mp_msg (MSGT_OPEN, MSGL_ERR, "Select failed: %s\n", strerror (errno));
357     close (s);
358     return -1;
359   }
360   else if (err == 0)
361   {
362     mp_msg (MSGT_OPEN, MSGL_ERR, "Timeout! No data from host %s\n", hostname);
363     close (s);
364     return -1;
365   }
366 
367   err_len = sizeof (err);
368   getsockopt (s, SOL_SOCKET, SO_ERROR, &err, (socklen_t *) &err_len);
369   if (err)
370   {
371     mp_msg (MSGT_OPEN, MSGL_ERR, "Socket error: %d\n", err);
372     close (s);
373     return -1;
374   }
375 
376   return s;
377 }
378 
379 static int
is_multicast_address(char * addr)380 is_multicast_address (char *addr)
381 {
382   struct sockaddr_in sin;
383 
384   if (!addr)
385     return -1;
386 
387   sin.sin_family = AF_INET;
388 
389 #if HAVE_INET_PTON
390     inet_pton (AF_INET, addr, &sin.sin_addr);
391 #elif HAVE_INET_ATON
392     inet_aton (addr, &sin.sin_addr);
393 #elif HAVE_WINSOCK2_H
394     sin.sin_addr.s_addr = htonl (INADDR_ANY);
395 #endif
396 
397   if ((ntohl (sin.sin_addr.s_addr) >> 28) == 0xe)
398     return 1;
399 
400   return 0;
401 }
402 
403 struct rtp_rtsp_session_t *
rtp_setup_and_play(rtsp_t * rtsp_session)404 rtp_setup_and_play (rtsp_t *rtsp_session)
405 {
406   struct rtp_rtsp_session_t* rtp_session = NULL;
407   const fsdp_media_description_t *med_dsc = NULL;
408   char temp_buf[MAX_LENGTH + 1];
409   char npt[256];
410 
411   char* answer;
412   char* sdp;
413   char *server_addr = NULL;
414   char *destination = NULL;
415 
416   int statut;
417   int content_length = 0;
418   int is_multicast = 0;
419 
420   fsdp_description_t *dsc = NULL;
421   fsdp_error_t result;
422 
423   int client_rtp_port = -1;
424   int client_rtcp_port = -1;
425   int server_rtp_port = -1;
426   int server_rtcp_port = -1;
427   int rtp_sock = -1;
428   int rtcp_sock = -1;
429 
430   /* 1. send a RTSP DESCRIBE request to server */
431   rtsp_schedule_field (rtsp_session, RTSP_ACCEPT_SDP);
432   statut = rtsp_request_describe (rtsp_session, NULL);
433   if (statut < 200 || statut > 299)
434     return NULL;
435 
436   answer = rtsp_search_answers (rtsp_session, RTSP_CONTENT_LENGTH);
437   if (answer)
438     content_length = atoi (answer);
439   else
440     return NULL;
441 
442   answer = rtsp_search_answers (rtsp_session, RTSP_CONTENT_TYPE);
443   if (!answer || !strstr (answer, RTSP_APPLICATION_SDP))
444     return NULL;
445 
446   /* 2. read SDP message from server */
447   sdp = (char *) malloc (content_length + 1);
448   if (rtsp_read_data (rtsp_session, sdp, content_length) <= 0)
449   {
450     free (sdp);
451     return NULL;
452   }
453   sdp[content_length] = 0;
454 
455   /* 3. parse SDP message */
456   dsc = fsdp_description_new ();
457   result = fsdp_parse (sdp, dsc);
458   if (result != FSDPE_OK)
459   {
460     free (sdp);
461     fsdp_description_delete (dsc);
462     return NULL;
463   }
464   mp_msg (MSGT_OPEN, MSGL_V, "SDP:\n%s\n", sdp);
465   free (sdp);
466 
467   /* 4. check for number of media streams: only one is supported */
468   if (fsdp_get_media_count (dsc) != 1)
469   {
470     mp_msg (MSGT_OPEN, MSGL_ERR,
471             "A single media stream only is supported atm.\n");
472     fsdp_description_delete (dsc);
473     return NULL;
474   }
475 
476   /* 5. set the Normal Play Time parameter
477    *    use range provided by server in SDP or start now if empty */
478   sprintf (npt, RTSP_RANGE);
479   if (fsdp_get_range (dsc))
480     strcat (npt, fsdp_get_range (dsc));
481   else
482     strcat (npt, RTSP_NPT_NOW);
483 
484   /* 5. check for a valid media stream */
485   med_dsc = fsdp_get_media (dsc, 0);
486   if (!med_dsc)
487   {
488     fsdp_description_delete (dsc);
489     return NULL;
490   }
491 
492   /* 6. parse the `m=<media>  <port>  <transport> <fmt list>' line */
493 
494   /* check for an A/V media */
495   if (fsdp_get_media_type (med_dsc) != FSDP_MEDIA_VIDEO &&
496       fsdp_get_media_type (med_dsc) != FSDP_MEDIA_AUDIO)
497   {
498     fsdp_description_delete (dsc);
499     return NULL;
500   }
501 
502   /* only RTP/AVP transport method is supported right now */
503   if (fsdp_get_media_transport_protocol (med_dsc) != FSDP_TP_RTP_AVP)
504   {
505     fsdp_description_delete (dsc);
506     return NULL;
507   }
508 
509   /* only MPEG-TS is supported at the moment */
510   if (!fsdp_get_media_format (med_dsc, 0) ||
511       !strstr (fsdp_get_media_format (med_dsc, 0),
512                RTSP_MEDIA_CONTAINER_MPEG_TS))
513   {
514     fsdp_description_delete (dsc);
515     return NULL;
516   }
517 
518   /* get client port (if any) advised by server */
519   client_rtp_port = fsdp_get_media_port (med_dsc);
520   if (client_rtp_port == -1)
521   {
522     fsdp_description_delete (dsc);
523     return NULL;
524   }
525 
526   /* if client_rtp_port = 0 => let client randomly pick one */
527   if (client_rtp_port == 0)
528   {
529     /* TODO: we should check if the port is in use first */
530     if (rtsp_port)
531       client_rtp_port = rtsp_port;
532     else
533       client_rtp_port = RTSP_DEFAULT_PORT;
534   }
535 
536   /* RTCP port generally is RTP port + 1 */
537   client_rtcp_port = client_rtp_port + 1;
538 
539   mp_msg (MSGT_OPEN, MSGL_V,
540           "RTP Port from SDP appears to be: %d\n", client_rtp_port);
541   mp_msg (MSGT_OPEN, MSGL_V,
542           "RTCP Port from SDP appears to be: %d\n", client_rtcp_port);
543 
544   /* 7. parse the `c=<network type> <addr type> <connection address>' line */
545 
546   /* check for a valid media network type (inet) */
547   if (fsdp_get_media_network_type (med_dsc) != FSDP_NETWORK_TYPE_INET)
548   {
549     /* no control for media: try global one instead */
550     if (fsdp_get_global_conn_network_type (dsc) != FSDP_NETWORK_TYPE_INET)
551     {
552       fsdp_description_delete (dsc);
553       return NULL;
554     }
555   }
556 
557   /* only IPv4 is supported atm. */
558   if (fsdp_get_media_address_type (med_dsc) != FSDP_ADDRESS_TYPE_IPV4)
559   {
560     /* no control for media: try global one instead */
561     if (fsdp_get_global_conn_address_type (dsc) != FSDP_ADDRESS_TYPE_IPV4)
562     {
563       fsdp_description_delete (dsc);
564       return NULL;
565     }
566   }
567 
568   /* get the media server address to connect to */
569   if (fsdp_get_media_address (med_dsc))
570     server_addr = strdup (fsdp_get_media_address (med_dsc));
571   else if (fsdp_get_global_conn_address (dsc))
572   {
573     /* no control for media: try global one instead */
574     server_addr = strdup (fsdp_get_global_conn_address (dsc));
575   }
576 
577   if (!server_addr)
578   {
579     fsdp_description_delete (dsc);
580     return NULL;
581   }
582 
583   /* check for a UNICAST or MULTICAST address to connect to */
584   is_multicast = is_multicast_address (server_addr);
585 
586   /* 8. initiate an RTP session */
587   rtp_session = rtp_session_new ();
588   if (!rtp_session)
589   {
590     free (server_addr);
591     fsdp_description_delete (dsc);
592     return NULL;
593   }
594 
595   /* get the media control URL */
596   if (fsdp_get_media_control (med_dsc, 0))
597     rtp_session->control_url = strdup (fsdp_get_media_control (med_dsc, 0));
598   fsdp_description_delete (dsc);
599   if (!rtp_session->control_url)
600   {
601     free (server_addr);
602     rtp_session_free (rtp_session);
603     return NULL;
604   }
605 
606   /* 9. create the payload for RTSP SETUP request */
607   memset (temp_buf, '\0', MAX_LENGTH);
608   snprintf (temp_buf, MAX_LENGTH,
609             RTSP_TRANSPORT_REQUEST,
610             is_multicast ? RTSP_TRANSPORT_MULTICAST : RTSP_TRANSPORT_UNICAST,
611             is_multicast ? RTSP_MULTICAST_PORT : RTSP_UNICAST_CLIENT_PORT,
612             client_rtp_port, client_rtcp_port);
613   mp_msg (MSGT_OPEN, MSGL_V, "RTSP Transport: %s\n", temp_buf);
614 
615   rtsp_unschedule_field (rtsp_session, RTSP_SESSION);
616   rtsp_schedule_field (rtsp_session, temp_buf);
617 
618   /* 10. check for the media control URL type and initiate RTSP SETUP */
619   if (!strncmp (rtp_session->control_url, "rtsp://", 7)) /* absolute URL */
620     statut = rtsp_request_setup (rtsp_session,
621                                  rtp_session->control_url, NULL);
622   else /* relative URL */
623     statut = rtsp_request_setup (rtsp_session,
624                                  NULL, rtp_session->control_url);
625 
626   if (statut < 200 || statut > 299)
627   {
628     free (server_addr);
629     rtp_session_free (rtp_session);
630     return NULL;
631   }
632 
633   /* 11. parse RTSP SETUP response: we need it to actually determine
634    *     the real address and port to connect to */
635   answer = rtsp_search_answers (rtsp_session, RTSP_TRANSPORT);
636   if (!answer)
637   {
638     free (server_addr);
639     rtp_session_free (rtp_session);
640     return NULL;
641   }
642 
643   /* check for RTP and RTCP ports to bind according to how request was done */
644   is_multicast = 0;
645   if (strstr (answer, RTSP_TRANSPORT_MULTICAST))
646     is_multicast = 1;
647 
648   if (is_multicast)
649     parse_port (answer, RTSP_MULTICAST_PORT,
650                 &client_rtp_port, &client_rtcp_port);
651   else
652   {
653     parse_port (answer, RTSP_UNICAST_CLIENT_PORT,
654                 &client_rtp_port, &client_rtcp_port);
655     parse_port (answer, RTSP_UNICAST_SERVER_PORT,
656                 &server_rtp_port, &server_rtcp_port);
657   }
658 
659   /* now check network settings as determined by server */
660   if (rtsp_destination)
661     destination = strdup (rtsp_destination);
662   else
663     destination = parse_destination (answer);
664   if (!destination)
665     destination = strdup (server_addr);
666   free (server_addr);
667 
668   mp_msg (MSGT_OPEN, MSGL_V, "RTSP Destination: %s\n", destination);
669   mp_msg (MSGT_OPEN, MSGL_V, "Client RTP port : %d\n", client_rtp_port);
670   mp_msg (MSGT_OPEN, MSGL_V, "Client RTCP port : %d\n", client_rtcp_port);
671   mp_msg (MSGT_OPEN, MSGL_V, "Server RTP port : %d\n", server_rtp_port);
672   mp_msg (MSGT_OPEN, MSGL_V, "Server RTCP port : %d\n", server_rtcp_port);
673 
674   /* 12. performs RTSP PLAY request */
675   rtsp_schedule_field (rtsp_session, npt);
676   statut = rtsp_request_play (rtsp_session, NULL);
677   if (statut < 200 || statut > 299)
678   {
679     free (destination);
680     rtp_session_free (rtp_session);
681     return NULL;
682   }
683 
684   /* 13. create RTP and RTCP connections */
685   rtp_sock = rtp_connect (destination, client_rtp_port);
686   rtcp_sock = rtcp_connect (client_rtcp_port, server_rtcp_port, destination);
687   rtp_session_set_fd (rtp_session, rtp_sock, rtcp_sock);
688   free (destination);
689 
690   mp_msg (MSGT_OPEN, MSGL_V, "RTP Sock : %d\nRTCP Sock : %d\n",
691           rtp_session->rtp_socket, rtp_session->rtcp_socket);
692 
693   if (rtp_session->rtp_socket == -1)
694   {
695     rtp_session_free (rtp_session);
696     return NULL;
697   }
698 
699   return rtp_session;
700 }
701