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