]> git.sesse.net Git - vlc/blob - modules/stream_out/rtsp.c
Fix struct duplication
[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                     vlc_mutex_lock( &id->lock_rtsp );
217                     TAB_APPEND( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
218                     vlc_mutex_unlock( &id->lock_rtsp );
219                 }
220                 vlc_mutex_unlock( &p_sys->lock_es );
221             }
222             break;
223         }
224
225         case HTTPD_MSG_PAUSE:
226             answer->i_status = 405;
227             httpd_MsgAdd( answer, "Allow", "DESCRIBE, PLAY, TEARDOWN" );
228             break;
229
230         case HTTPD_MSG_TEARDOWN:
231         {
232             rtsp_client_t *rtsp;
233
234             /* for now only multicast so easy again */
235             answer->i_status = 200;
236
237             psz_session = httpd_MsgGet( query, "Session" );
238             rtsp = RtspClientGet( p_stream, psz_session );
239             if( rtsp )
240             {
241                 int i_id;
242
243                 vlc_mutex_lock( &p_sys->lock_es );
244                 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
245                 {
246                     sout_stream_id_t *id = rtsp->id[i_id];
247                     int i;
248
249                     for( i = 0; i < p_sys->i_es; i++ )
250                     {
251                         if( id == p_sys->es[i] )
252                             break;
253                     }
254                     if( i >= p_sys->i_es ) continue;
255
256                     vlc_mutex_lock( &id->lock_rtsp );
257                     TAB_REMOVE( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
258                     vlc_mutex_unlock( &id->lock_rtsp );
259                 }
260                 vlc_mutex_unlock( &p_sys->lock_es );
261
262                 RtspClientDel( p_stream, rtsp );
263             }
264             break;
265         }
266
267         default:
268             return VLC_EGENERIC;
269     }
270
271     httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
272     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
273     psz_cseq = httpd_MsgGet( query, "Cseq" );
274     if( psz_cseq )
275         httpd_MsgAdd( answer, "Cseq", "%s", psz_cseq );
276     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
277
278     if( psz_session )
279         httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
280     return VLC_SUCCESS;
281 }
282
283
284 /** Finds the next transport choice */
285 static inline const char *transport_next( const char *str )
286 {
287     /* Looks for comma */
288     str = strchr( str, ',' );
289     if( str == NULL )
290         return NULL; /* No more transport options */
291
292     str++; /* skips comma */
293     while( strchr( "\r\n\t ", *str ) )
294         str++;
295
296     return (*str) ? str : NULL;
297 }
298
299
300 /** Finds the next transport parameter */
301 static inline const char *parameter_next( const char *str )
302 {
303     while( strchr( ",;", *str ) == NULL )
304         str++;
305
306     return (*str == ';') ? (str + 1) : NULL;
307 }
308
309
310 /** Non-aggregate RTSP callback */
311 /*static*/ int RtspCallbackId( httpd_callback_sys_t *p_args,
312                            httpd_client_t *cl,
313                            httpd_message_t *answer, httpd_message_t *query )
314 {
315     sout_stream_id_t *id = (sout_stream_id_t*)p_args;
316     sout_stream_t    *p_stream = id->p_stream;
317     sout_stream_sys_t *p_sys = p_stream->p_sys;
318     char psz_session_init[21];
319     const char *psz_session;
320     const char *psz_cseq;
321
322     if( answer == NULL || query == NULL )
323         return VLC_SUCCESS;
324     //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
325
326     /* */
327     answer->i_proto = HTTPD_PROTO_RTSP;
328     answer->i_version= query->i_version;
329     answer->i_type   = HTTPD_MSG_ANSWER;
330     answer->i_body = 0;
331     answer->p_body = NULL;
332
333     /* Create new session ID if needed */
334     psz_session = httpd_MsgGet( query, "Session" );
335     if( psz_session == NULL )
336     {
337         /* FIXME: should be somewhat secure randomness */
338         snprintf( psz_session_init, sizeof(psz_session_init), I64Fd,
339                   NTPtime64() + rand() );
340     }
341
342     if( httpd_MsgGet( query, "Require" ) != NULL )
343     {
344         answer->i_status = 551;
345         httpd_MsgAdd( query, "Unsupported", "%s",
346                       httpd_MsgGet( query, "Require" ) );
347     }
348     else
349     switch( query->i_type )
350     {
351         case HTTPD_MSG_SETUP:
352         {
353             answer->i_status = 461;
354
355             for( const char *tpt = httpd_MsgGet( query, "Transport" );
356                  tpt != NULL;
357                  tpt = transport_next( tpt ) )
358             {
359                 vlc_bool_t b_multicast = VLC_TRUE, b_unsupp = VLC_FALSE;
360                 unsigned loport = 5004, hiport = 5005; /* from RFC3551 */
361
362                 /* Check transport protocol. */
363                 /* Currently, we only support RTP/AVP over UDP */
364                 if( strncmp( tpt, "RTP/AVP", 7 ) )
365                     continue;
366                 tpt += 7;
367                 if( strncmp( tpt, "/UDP", 4 ) == 0 )
368                     tpt += 4;
369                 if( strchr( ";,", *tpt ) == NULL )
370                     continue;
371
372                 /* Parse transport options */
373                 for( const char *opt = parameter_next( tpt );
374                      opt != NULL;
375                      opt = parameter_next( opt ) )
376                 {
377                     if( strncmp( opt, "multicast", 9 ) == 0)
378                         b_multicast = VLC_TRUE;
379                     else
380                     if( strncmp( opt, "unicast", 7 ) == 0 )
381                         b_multicast = VLC_FALSE;
382                     else
383                     if( sscanf( opt, "client_port=%u-%u", &loport, &hiport ) == 2 )
384                         ;
385                     else
386                     if( strncmp( opt, "mode=", 5 ) == 0 )
387                     {
388                         if( strncasecmp( opt + 5, "play", 4 )
389                          && strncasecmp( opt + 5, "\"PLAY\"", 6 ) )
390                         {
391                             /* Not playing?! */
392                             b_unsupp = VLC_TRUE;
393                             break;
394                         }
395                     }
396                     else
397                     {
398                     /*
399                      * Every other option is unsupported:
400                      *
401                      * "source" and "append" are invalid.
402                      *
403                      * For multicast, "port", "layers", "ttl" are set by the
404                      * stream output configuration.
405                      *
406                      * For unicast, we do not allow "destination" as it
407                      * carries a DoS risk, and we decide on "server_port".
408                      *
409                      * "interleaved" and "ssrc" are not implemented.
410                      */
411                         b_unsupp = VLC_TRUE;
412                         break;
413                     }
414                 }
415
416                 if( b_unsupp )
417                     continue;
418
419                 if( b_multicast )
420                 {
421                     if( id->psz_destination == NULL )
422                         continue;
423
424                     answer->i_status = 200;
425
426                     httpd_MsgAdd( answer, "Transport",
427                                   "RTP/AVP/UDP;destination=%s;port=%d-%d;"
428                                   "ttl=%d;mode=play",
429                                   id->psz_destination, id->i_port, id->i_port+1,
430                                   ( p_sys->i_ttl > 0 ) ? p_sys->i_ttl : 1 );
431                 }
432                 else
433                 {
434                     char ip[NI_MAXNUMERICHOST], psz_access[22],
435                          url[NI_MAXNUMERICHOST + 8];
436                     sout_access_out_t *p_access;
437                     rtsp_client_t *rtsp = NULL;
438
439                     if( ( hiport - loport ) > 1 )
440                         continue;
441
442                     if( psz_session == NULL )
443                     {
444                         psz_session = psz_session_init;
445                         rtsp = RtspClientNew( p_stream, psz_session );
446                     }
447                     else
448                     {
449                         /* FIXME: we probably need to remove an access out,
450                          * if there is already one for the same ID */
451                         rtsp = RtspClientGet( p_stream, psz_session );
452                         if( rtsp == NULL )
453                         {
454                             answer->i_status = 454;
455                             continue;
456                         }
457                     }
458
459                     if( httpd_ClientIP( cl, ip ) == NULL )
460                     {
461                         answer->i_status = 500;
462                         continue;
463                     }
464
465                     if( p_sys->i_ttl )
466                         snprintf( psz_access, sizeof( psz_access ),
467                                   "udp{raw,rtcp,ttl=%d}", p_sys->i_ttl );
468                     else
469                         strcpy( psz_access, "udp{raw,rtcp}" );
470
471                     snprintf( url, sizeof( url ),
472                               ( strchr( ip, ':' ) != NULL ) ? "[%s]:%d" : "%s:%d",
473                               ip, loport );
474
475                     p_access = sout_AccessOutNew( p_stream->p_sout,
476                                                   psz_access, url );
477                     if( p_access == NULL )
478                     {
479                         msg_Err( p_stream,
480                                  "cannot create access output for %s://%s",
481                                  psz_access, url );
482                         answer->i_status = 500;
483                         break;
484                     }
485
486                     TAB_APPEND( rtsp->i_id, rtsp->id, id );
487                     TAB_APPEND( rtsp->i_access, rtsp->access, p_access );
488
489                     char *src = var_GetNonEmptyString (p_access, "src-addr");
490                     int sport = var_GetInteger (p_access, "src-port");
491
492                     httpd_ServerIP( cl, ip );
493
494                     if( ( src != NULL ) && strcmp( src, ip ) )
495                     {
496                         /* Specify source IP if it is different from the RTSP
497                          * control connection server address */
498                         char *ptr = strchr( src, '%' );
499                         if( ptr != NULL ) *ptr = '\0'; /* remove scope ID */
500
501                         httpd_MsgAdd( answer, "Transport",
502                                       "RTP/AVP/UDP;unicast;source=%s;"
503                                       "client_port=%u-%u;server_port=%u-%u;"
504                                       "mode=play",
505                                       src, loport, hiport, sport, sport + 1 );
506                     }
507                     else
508                     {
509                         httpd_MsgAdd( answer, "Transport",
510                                       "RTP/AVP/UDP;unicast;"
511                                       "client_port=%u-%u;server_port=%u-%u;"
512                                       "mode=play",
513                                       loport, hiport, sport, sport + 1 );
514                     }
515
516                     answer->i_status = 200;
517                     free( src );
518                 }
519                 break;
520             }
521             break;
522         }
523
524         default:
525             answer->i_status = 460;
526             break;
527     }
528
529     psz_cseq = httpd_MsgGet( query, "Cseq" );
530     if( psz_cseq )
531         httpd_MsgAdd( answer, "Cseq", "%s", psz_cseq );
532     httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
533     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
534     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
535
536     if( psz_session )
537         httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
538     return VLC_SUCCESS;
539 }