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