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