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