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