]> git.sesse.net Git - vlc/blob - modules/stream_out/rtsp.c
Split RTP sout into multiple pieces.
[vlc] / modules / stream_out / rtsp.c
1 /*****************************************************************************
2  * rtsp.c: RTSP support for RTP stream output module
3  *****************************************************************************
4  * Copyright (C) 2003-2007 the VideoLAN team
5  * $Id: rtp.c 21407 2007-08-22 20:10:41Z courmisch $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc_sout.h>
29 #include <vlc_block.h>
30
31 #include <vlc_httpd.h>
32 #include <vlc_url.h>
33 #include <vlc_network.h>
34 #include <vlc_charset.h>
35 #include <vlc_strings.h>
36
37 #include "rtp.h"
38
39 /* For unicast/interleaved streaming */
40 struct rtsp_client_t
41 {
42     char    *psz_session;
43     int64_t i_last; /* for timeout */
44
45     /* is it in "play" state */
46     vlc_bool_t b_playing;
47
48     /* output (id-access) */
49     int               i_id;
50     sout_stream_id_t  **id;
51     int               i_access;
52     sout_access_out_t **access;
53 };
54
55 struct sout_stream_sys_t
56 {
57     /* sdp */
58     int64_t i_sdp_id;
59     int     i_sdp_version;
60     char    *psz_sdp;
61     vlc_mutex_t  lock_sdp;
62
63     char        *psz_session_name;
64     char        *psz_session_description;
65     char        *psz_session_url;
66     char        *psz_session_email;
67
68     /* */
69     vlc_bool_t b_export_sdp_file;
70     char *psz_sdp_file;
71     /* sap */
72     vlc_bool_t b_export_sap;
73     session_descriptor_t *p_session;
74
75     httpd_host_t *p_httpd_host;
76     httpd_file_t *p_httpd_file;
77
78     httpd_host_t *p_rtsp_host;
79     httpd_url_t  *p_rtsp_url;
80     char         *psz_rtsp_control;
81     char         *psz_rtsp_path;
82
83     /* */
84     char *psz_destination;
85     int  i_port;
86     int  i_port_audio;
87     int  i_port_video;
88     int  i_ttl;
89     vlc_bool_t b_latm;
90
91     /* when need to use a private one or when using muxer */
92     int i_payload_type;
93
94     /* in case we do TS/PS over rtp */
95     sout_mux_t        *p_mux;
96     sout_access_out_t *p_access;
97     int               i_mtu;
98     sout_access_out_t *p_grab;
99     uint16_t          i_sequence;
100     uint32_t          i_timestamp_start;
101     uint8_t           ssrc[4];
102     block_t           *packet;
103
104     /* */
105     vlc_mutex_t      lock_es;
106     int              i_es;
107     sout_stream_id_t **es;
108
109     /* */
110     int              i_rtsp;
111     rtsp_client_t    **rtsp;
112 };
113
114
115 static int  RtspCallback( httpd_callback_sys_t *p_args,
116                           httpd_client_t *cl,
117                           httpd_message_t *answer, httpd_message_t *query );
118
119
120
121 int RtspSetup( sout_stream_t *p_stream, vlc_url_t *url )
122 {
123     sout_stream_sys_t *p_sys = p_stream->p_sys;
124
125     msg_Dbg( p_stream, "rtsp setup: %s : %d / %s\n", url->psz_host, url->i_port, url->psz_path );
126
127     p_sys->p_rtsp_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 554 );
128     if( p_sys->p_rtsp_host == NULL )
129     {
130         return VLC_EGENERIC;
131     }
132
133     p_sys->psz_rtsp_path = strdup( url->psz_path ? url->psz_path : "/" );
134     p_sys->psz_rtsp_control = malloc (strlen( url->psz_host ) + 20 + strlen( p_sys->psz_rtsp_path ) + 1 );
135     sprintf( p_sys->psz_rtsp_control, "rtsp://%s:%d%s",
136              url->psz_host,  url->i_port > 0 ? url->i_port : 554, p_sys->psz_rtsp_path );
137
138     p_sys->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, p_sys->psz_rtsp_path, NULL, NULL, NULL );
139     if( p_sys->p_rtsp_url == 0 )
140     {
141         return VLC_EGENERIC;
142     }
143     httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)p_stream );
144     httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_SETUP,    RtspCallback, (void*)p_stream );
145     httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PLAY,     RtspCallback, (void*)p_stream );
146     httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PAUSE,    RtspCallback, (void*)p_stream );
147     httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)p_stream );
148
149     return VLC_SUCCESS;
150 }
151
152
153 static rtsp_client_t *RtspClientNew( sout_stream_t *p_stream, const char *psz_session )
154 {
155     rtsp_client_t *rtsp = malloc( sizeof( rtsp_client_t ));
156
157     rtsp->psz_session = strdup( psz_session );
158     rtsp->i_last = 0;
159     rtsp->b_playing = VLC_FALSE;
160     rtsp->i_id = 0;
161     rtsp->id = NULL;
162     rtsp->i_access = 0;
163     rtsp->access = NULL;
164
165     TAB_APPEND( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
166
167     return rtsp;
168 }
169
170
171 static rtsp_client_t *RtspClientGet( sout_stream_t *p_stream, const char *psz_session )
172 {
173     int i;
174
175     if( !psz_session ) return NULL;
176
177     for( i = 0; i < p_stream->p_sys->i_rtsp; i++ )
178     {
179         if( !strcmp( p_stream->p_sys->rtsp[i]->psz_session, psz_session ) )
180         {
181             return p_stream->p_sys->rtsp[i];
182         }
183     }
184     return NULL;
185 }
186
187
188 /*static*/ void RtspClientDel( sout_stream_t *p_stream, rtsp_client_t *rtsp )
189 {
190     int i;
191     TAB_REMOVE( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
192
193     for( i = 0; i < rtsp->i_access; i++ )
194     {
195         sout_AccessOutDelete( rtsp->access[i] );
196     }
197     if( rtsp->id )     free( rtsp->id );
198     if( rtsp->access ) free( rtsp->access );
199
200     free( rtsp->psz_session );
201     free( rtsp );
202 }
203
204
205 /** Aggregate RTSP callback */
206 static int  RtspCallback( httpd_callback_sys_t *p_args,
207                           httpd_client_t *cl,
208                           httpd_message_t *answer, httpd_message_t *query )
209 {
210     sout_stream_t *p_stream = (sout_stream_t*)p_args;
211     sout_stream_sys_t *p_sys = p_stream->p_sys;
212     char *psz_destination = p_sys->psz_destination;
213     const char *psz_session = NULL;
214     const char *psz_cseq = NULL;
215
216     if( answer == NULL || query == NULL )
217     {
218         return VLC_SUCCESS;
219     }
220     //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
221
222     answer->i_proto = HTTPD_PROTO_RTSP;
223     answer->i_version= query->i_version;
224     answer->i_type   = HTTPD_MSG_ANSWER;
225     answer->i_body = 0;
226     answer->p_body = NULL;
227
228     if( httpd_MsgGet( query, "Require" ) != NULL )
229     {
230         answer->i_status = 551;
231         httpd_MsgAdd( query, "Unsupported", "%s",
232                       httpd_MsgGet( query, "Require" ) );
233     }
234     else
235     switch( query->i_type )
236     {
237         case HTTPD_MSG_DESCRIBE:
238         {
239             char *psz_sdp = SDPGenerate( p_stream, psz_destination, VLC_TRUE );
240
241             answer->i_status = 200;
242             httpd_MsgAdd( answer, "Content-Type",  "%s", "application/sdp" );
243             httpd_MsgAdd( answer, "Content-Base",  "%s", p_sys->psz_rtsp_control );
244             answer->p_body = (uint8_t *)psz_sdp;
245             answer->i_body = strlen( psz_sdp );
246             break;
247         }
248
249         case HTTPD_MSG_SETUP:
250             answer->i_status = 459;
251             break;
252
253         case HTTPD_MSG_PLAY:
254         {
255             rtsp_client_t *rtsp;
256             /* for now only multicast so easy */
257             answer->i_status = 200;
258
259             psz_session = httpd_MsgGet( query, "Session" );
260             rtsp = RtspClientGet( p_stream, psz_session );
261             if( rtsp && !rtsp->b_playing )
262             {
263                 int i_id;
264                 /* FIXME */
265                 rtsp->b_playing = VLC_TRUE;
266
267                 vlc_mutex_lock( &p_sys->lock_es );
268                 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
269                 {
270                     sout_stream_id_t *id = rtsp->id[i_id];
271                     int i;
272
273                     for( i = 0; i < p_sys->i_es; i++ )
274                     {
275                         if( id == p_sys->es[i] )
276                             break;
277                     }
278                     if( i >= p_sys->i_es ) continue;
279
280                     vlc_mutex_lock( &id->lock_rtsp );
281                     TAB_APPEND( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
282                     vlc_mutex_unlock( &id->lock_rtsp );
283                 }
284                 vlc_mutex_unlock( &p_sys->lock_es );
285             }
286             break;
287         }
288
289         case HTTPD_MSG_PAUSE:
290             answer->i_status = 405;
291             httpd_MsgAdd( answer, "Allow", "DESCRIBE, PLAY, TEARDOWN" );
292             break;
293
294         case HTTPD_MSG_TEARDOWN:
295         {
296             rtsp_client_t *rtsp;
297
298             /* for now only multicast so easy again */
299             answer->i_status = 200;
300
301             psz_session = httpd_MsgGet( query, "Session" );
302             rtsp = RtspClientGet( p_stream, psz_session );
303             if( rtsp )
304             {
305                 int i_id;
306
307                 vlc_mutex_lock( &p_sys->lock_es );
308                 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
309                 {
310                     sout_stream_id_t *id = rtsp->id[i_id];
311                     int i;
312
313                     for( i = 0; i < p_sys->i_es; i++ )
314                     {
315                         if( id == p_sys->es[i] )
316                             break;
317                     }
318                     if( i >= p_sys->i_es ) continue;
319
320                     vlc_mutex_lock( &id->lock_rtsp );
321                     TAB_REMOVE( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
322                     vlc_mutex_unlock( &id->lock_rtsp );
323                 }
324                 vlc_mutex_unlock( &p_sys->lock_es );
325
326                 RtspClientDel( p_stream, rtsp );
327             }
328             break;
329         }
330
331         default:
332             return VLC_EGENERIC;
333     }
334
335     httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
336     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
337     psz_cseq = httpd_MsgGet( query, "Cseq" );
338     if( psz_cseq )
339         httpd_MsgAdd( answer, "Cseq", "%s", psz_cseq );
340     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
341
342     if( psz_session )
343         httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
344     return VLC_SUCCESS;
345 }
346
347
348 /** Finds the next transport choice */
349 static inline const char *transport_next( const char *str )
350 {
351     /* Looks for comma */
352     str = strchr( str, ',' );
353     if( str == NULL )
354         return NULL; /* No more transport options */
355
356     str++; /* skips comma */
357     while( strchr( "\r\n\t ", *str ) )
358         str++;
359
360     return (*str) ? str : NULL;
361 }
362
363
364 /** Finds the next transport parameter */
365 static inline const char *parameter_next( const char *str )
366 {
367     while( strchr( ",;", *str ) == NULL )
368         str++;
369
370     return (*str == ';') ? (str + 1) : NULL;
371 }
372
373
374 /** Non-aggregate RTSP callback */
375 /*static*/ int RtspCallbackId( httpd_callback_sys_t *p_args,
376                            httpd_client_t *cl,
377                            httpd_message_t *answer, httpd_message_t *query )
378 {
379     sout_stream_id_t *id = (sout_stream_id_t*)p_args;
380     sout_stream_t    *p_stream = id->p_stream;
381     sout_stream_sys_t *p_sys = p_stream->p_sys;
382     char psz_session_init[21];
383     const char *psz_session;
384     const char *psz_cseq;
385
386     if( answer == NULL || query == NULL )
387         return VLC_SUCCESS;
388     //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
389
390     /* */
391     answer->i_proto = HTTPD_PROTO_RTSP;
392     answer->i_version= query->i_version;
393     answer->i_type   = HTTPD_MSG_ANSWER;
394     answer->i_body = 0;
395     answer->p_body = NULL;
396
397     /* Create new session ID if needed */
398     psz_session = httpd_MsgGet( query, "Session" );
399     if( psz_session == NULL )
400     {
401         /* FIXME: should be somewhat secure randomness */
402         snprintf( psz_session_init, sizeof(psz_session_init), I64Fd,
403                   NTPtime64() + rand() );
404     }
405
406     if( httpd_MsgGet( query, "Require" ) != NULL )
407     {
408         answer->i_status = 551;
409         httpd_MsgAdd( query, "Unsupported", "%s",
410                       httpd_MsgGet( query, "Require" ) );
411     }
412     else
413     switch( query->i_type )
414     {
415         case HTTPD_MSG_SETUP:
416         {
417             answer->i_status = 461;
418
419             for( const char *tpt = httpd_MsgGet( query, "Transport" );
420                  tpt != NULL;
421                  tpt = transport_next( tpt ) )
422             {
423                 vlc_bool_t b_multicast = VLC_TRUE, b_unsupp = VLC_FALSE;
424                 unsigned loport = 5004, hiport = 5005; /* from RFC3551 */
425
426                 /* Check transport protocol. */
427                 /* Currently, we only support RTP/AVP over UDP */
428                 if( strncmp( tpt, "RTP/AVP", 7 ) )
429                     continue;
430                 tpt += 7;
431                 if( strncmp( tpt, "/UDP", 4 ) == 0 )
432                     tpt += 4;
433                 if( strchr( ";,", *tpt ) == NULL )
434                     continue;
435
436                 /* Parse transport options */
437                 for( const char *opt = parameter_next( tpt );
438                      opt != NULL;
439                      opt = parameter_next( opt ) )
440                 {
441                     if( strncmp( opt, "multicast", 9 ) == 0)
442                         b_multicast = VLC_TRUE;
443                     else
444                     if( strncmp( opt, "unicast", 7 ) == 0 )
445                         b_multicast = VLC_FALSE;
446                     else
447                     if( sscanf( opt, "client_port=%u-%u", &loport, &hiport ) == 2 )
448                         ;
449                     else
450                     if( strncmp( opt, "mode=", 5 ) == 0 )
451                     {
452                         if( strncasecmp( opt + 5, "play", 4 )
453                          && strncasecmp( opt + 5, "\"PLAY\"", 6 ) )
454                         {
455                             /* Not playing?! */
456                             b_unsupp = VLC_TRUE;
457                             break;
458                         }
459                     }
460                     else
461                     {
462                     /*
463                      * Every other option is unsupported:
464                      *
465                      * "source" and "append" are invalid.
466                      *
467                      * For multicast, "port", "layers", "ttl" are set by the
468                      * stream output configuration.
469                      *
470                      * For unicast, we do not allow "destination" as it
471                      * carries a DoS risk, and we decide on "server_port".
472                      *
473                      * "interleaved" and "ssrc" are not implemented.
474                      */
475                         b_unsupp = VLC_TRUE;
476                         break;
477                     }
478                 }
479
480                 if( b_unsupp )
481                     continue;
482
483                 if( b_multicast )
484                 {
485                     if( id->psz_destination == NULL )
486                         continue;
487
488                     answer->i_status = 200;
489
490                     httpd_MsgAdd( answer, "Transport",
491                                   "RTP/AVP/UDP;destination=%s;port=%d-%d;"
492                                   "ttl=%d;mode=play",
493                                   id->psz_destination, id->i_port, id->i_port+1,
494                                   ( p_sys->i_ttl > 0 ) ? p_sys->i_ttl : 1 );
495                 }
496                 else
497                 {
498                     char ip[NI_MAXNUMERICHOST], psz_access[22],
499                          url[NI_MAXNUMERICHOST + 8];
500                     sout_access_out_t *p_access;
501                     rtsp_client_t *rtsp = NULL;
502
503                     if( ( hiport - loport ) > 1 )
504                         continue;
505
506                     if( psz_session == NULL )
507                     {
508                         psz_session = psz_session_init;
509                         rtsp = RtspClientNew( p_stream, psz_session );
510                     }
511                     else
512                     {
513                         /* FIXME: we probably need to remove an access out,
514                          * if there is already one for the same ID */
515                         rtsp = RtspClientGet( p_stream, psz_session );
516                         if( rtsp == NULL )
517                         {
518                             answer->i_status = 454;
519                             continue;
520                         }
521                     }
522
523                     if( httpd_ClientIP( cl, ip ) == NULL )
524                     {
525                         answer->i_status = 500;
526                         continue;
527                     }
528
529                     if( p_sys->i_ttl )
530                         snprintf( psz_access, sizeof( psz_access ),
531                                   "udp{raw,rtcp,ttl=%d}", p_sys->i_ttl );
532                     else
533                         strcpy( psz_access, "udp{raw,rtcp}" );
534
535                     snprintf( url, sizeof( url ),
536                               ( strchr( ip, ':' ) != NULL ) ? "[%s]:%d" : "%s:%d",
537                               ip, loport );
538
539                     p_access = sout_AccessOutNew( p_stream->p_sout,
540                                                   psz_access, url );
541                     if( p_access == NULL )
542                     {
543                         msg_Err( p_stream,
544                                  "cannot create access output for %s://%s",
545                                  psz_access, url );
546                         answer->i_status = 500;
547                         break;
548                     }
549
550                     TAB_APPEND( rtsp->i_id, rtsp->id, id );
551                     TAB_APPEND( rtsp->i_access, rtsp->access, p_access );
552
553                     char *src = var_GetNonEmptyString (p_access, "src-addr");
554                     int sport = var_GetInteger (p_access, "src-port");
555
556                     httpd_ServerIP( cl, ip );
557
558                     if( ( src != NULL ) && strcmp( src, ip ) )
559                     {
560                         /* Specify source IP if it is different from the RTSP
561                          * control connection server address */
562                         char *ptr = strchr( src, '%' );
563                         if( ptr != NULL ) *ptr = '\0'; /* remove scope ID */
564
565                         httpd_MsgAdd( answer, "Transport",
566                                       "RTP/AVP/UDP;unicast;source=%s;"
567                                       "client_port=%u-%u;server_port=%u-%u;"
568                                       "mode=play",
569                                       src, loport, hiport, sport, sport + 1 );
570                     }
571                     else
572                     {
573                         httpd_MsgAdd( answer, "Transport",
574                                       "RTP/AVP/UDP;unicast;"
575                                       "client_port=%u-%u;server_port=%u-%u;"
576                                       "mode=play",
577                                       loport, hiport, sport, sport + 1 );
578                     }
579
580                     answer->i_status = 200;
581                     free( src );
582                 }
583                 break;
584             }
585             break;
586         }
587
588         default:
589             answer->i_status = 460;
590             break;
591     }
592
593     psz_cseq = httpd_MsgGet( query, "Cseq" );
594     if( psz_cseq )
595         httpd_MsgAdd( answer, "Cseq", "%s", psz_cseq );
596     httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
597     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
598     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
599
600     if( psz_session )
601         httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
602     return VLC_SUCCESS;
603 }