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