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