Improve randomness of RTSP session ID.
[vlc.git] / modules / stream_out / rtsp.c
1 /*****************************************************************************
2  * rtsp.c: RTSP support for RTP stream output module
3  *****************************************************************************
4  * Copyright (C) 2003-2004 the VideoLAN team
5  * Copyright © 2007 Rémi Denis-Courmont
6  *
7  * $Id: rtp.c 21407 2007-08-22 20:10:41Z courmisch $
8  *
9  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <vlc/vlc.h>
30 #include <vlc_sout.h>
31
32 #include <vlc_httpd.h>
33 #include <vlc_url.h>
34 #include <vlc_network.h>
35 #include <vlc_rand.h>
36 #include <assert.h>
37 #include <errno.h>
38 #include <stdlib.h>
39
40 #include "rtp.h"
41
42 typedef struct rtsp_session_t rtsp_session_t;
43
44 struct rtsp_stream_t
45 {
46     vlc_mutex_t     lock;
47     sout_stream_t  *owner;
48     httpd_host_t   *host;
49     httpd_url_t    *url;
50     char           *psz_path;
51     unsigned        port;
52
53     int             sessionc;
54     rtsp_session_t **sessionv;
55 };
56
57
58 static int  RtspCallback( httpd_callback_sys_t *p_args,
59                           httpd_client_t *cl, httpd_message_t *answer,
60                           const httpd_message_t *query );
61 static int  RtspCallbackId( httpd_callback_sys_t *p_args,
62                             httpd_client_t *cl, httpd_message_t *answer,
63                             const httpd_message_t *query );
64 static void RtspClientDel( rtsp_stream_t *rtsp, rtsp_session_t *session );
65
66 rtsp_stream_t *RtspSetup( sout_stream_t *p_stream, const vlc_url_t *url )
67 {
68     rtsp_stream_t *rtsp = malloc( sizeof( *rtsp ) );
69
70     if( rtsp == NULL || ( url->i_port > 99999 ) )
71         return NULL;
72
73     rtsp->owner = p_stream;
74     rtsp->sessionc = 0;
75     rtsp->sessionv = NULL;
76     vlc_mutex_init( p_stream, &rtsp->lock );
77
78     msg_Dbg( p_stream, "rtsp setup: %s : %d / %s\n",
79              url->psz_host, url->i_port, url->psz_path );
80
81     rtsp->port = (url->i_port > 0) ? url->i_port : 554;
82     if( url->psz_path != NULL )
83         rtsp->psz_path = strdup( url->psz_path + 1 );
84     else
85         rtsp->psz_path = NULL;
86
87 #if 0
88     if( asprintf( &rtsp->psz_control, "rtsp://%s:%d%s",
89                   url->psz_host,  url->i_port > 0 ? url->i_port : 554,
90                   rtsp->psz_path ) == -1 )
91     {
92         rtsp->psz_control = NULL;
93         goto error;
94     }
95 #endif
96
97     rtsp->host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host,
98                                 rtsp->port );
99     if( rtsp->host == NULL )
100         goto error;
101
102     rtsp->url = httpd_UrlNewUnique( rtsp->host,
103                                     url->psz_path ? url->psz_path : "/", NULL,
104                                     NULL, NULL );
105     if( rtsp->url == NULL )
106         goto error;
107
108     httpd_UrlCatch( rtsp->url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)rtsp );
109     httpd_UrlCatch( rtsp->url, HTTPD_MSG_SETUP,    RtspCallback, (void*)rtsp );
110     httpd_UrlCatch( rtsp->url, HTTPD_MSG_PLAY,     RtspCallback, (void*)rtsp );
111     httpd_UrlCatch( rtsp->url, HTTPD_MSG_PAUSE,    RtspCallback, (void*)rtsp );
112     httpd_UrlCatch( rtsp->url, HTTPD_MSG_GETPARAMETER, RtspCallback,
113                     (void*)rtsp );
114     httpd_UrlCatch( rtsp->url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)rtsp );
115     return rtsp;
116
117 error:
118     RtspUnsetup( rtsp );
119     return NULL;
120 }
121
122
123 void RtspUnsetup( rtsp_stream_t *rtsp )
124 {
125     while( rtsp->sessionc > 0 )
126         RtspClientDel( rtsp, rtsp->sessionv[0] );
127
128     if( rtsp->url )
129         httpd_UrlDelete( rtsp->url );
130
131     if( rtsp->host )
132         httpd_HostDelete( rtsp->host );
133
134     vlc_mutex_destroy( &rtsp->lock );
135 }
136
137
138 struct rtsp_stream_id_t
139 {
140     rtsp_stream_t    *stream;
141     sout_stream_id_t *sout_id;
142     httpd_url_t      *url;
143     const char       *dst;
144     int               ttl;
145     unsigned          loport, hiport;
146 };
147
148
149 typedef struct rtsp_strack_t rtsp_strack_t;
150
151 /* For unicast streaming */
152 struct rtsp_session_t
153 {
154     rtsp_stream_t *stream;
155     uint64_t       id;
156
157     /* output (id-access) */
158     int            trackc;
159     rtsp_strack_t *trackv;
160 };
161
162
163 /* Unicast session track */
164 struct rtsp_strack_t
165 {
166     sout_stream_id_t  *id;
167     sout_access_out_t *access;
168     vlc_bool_t         playing;
169 };
170
171
172 rtsp_stream_id_t *RtspAddId( rtsp_stream_t *rtsp, sout_stream_id_t *sid,
173                              unsigned num,
174                              /* Multicast stuff - TODO: cleanup */
175                              const char *dst, int ttl,
176                              unsigned loport, unsigned hiport )
177 {
178     char urlbuf[sizeof( "//trackID=123" ) + strlen( rtsp->psz_path )];
179     rtsp_stream_id_t *id = malloc( sizeof( *id ) );
180     httpd_url_t *url;
181
182     if( id == NULL )
183         return NULL;
184
185     id->stream = rtsp;
186     id->sout_id = sid;
187     /* TODO: can we assume that this need not be strdup'd? */
188     id->dst = dst;
189     if( id->dst != NULL )
190     {
191         id->ttl = ttl;
192         id->loport = loport;
193         id->hiport = hiport;
194     }
195
196     snprintf( urlbuf, sizeof( urlbuf ), "/%s/trackID=%u", rtsp->psz_path,
197               num );
198     msg_Dbg( rtsp->owner, "RTSP: adding %s\n", urlbuf );
199     url = id->url = httpd_UrlNewUnique( rtsp->host, urlbuf, NULL, NULL, NULL );
200
201     if( url == NULL )
202     {
203         free( id );
204         return NULL;
205     }
206
207     httpd_UrlCatch( url, HTTPD_MSG_DESCRIBE, RtspCallbackId, (void *)id );
208     httpd_UrlCatch( url, HTTPD_MSG_SETUP,    RtspCallbackId, (void *)id );
209     httpd_UrlCatch( url, HTTPD_MSG_PLAY,     RtspCallbackId, (void *)id );
210     httpd_UrlCatch( url, HTTPD_MSG_PAUSE,    RtspCallbackId, (void *)id );
211     httpd_UrlCatch( url, HTTPD_MSG_GETPARAMETER, RtspCallbackId, (void *)id );
212     httpd_UrlCatch( url, HTTPD_MSG_TEARDOWN, RtspCallbackId, (void *)id );
213
214     return id;
215 }
216
217
218 void RtspDelId( rtsp_stream_t *rtsp, rtsp_stream_id_t *id )
219 {
220     vlc_mutex_lock( &rtsp->lock );
221     for( int i = 0; i < rtsp->sessionc; i++ )
222     {
223         rtsp_session_t *ses = rtsp->sessionv[i];
224
225         for( int j = 0; j < ses->trackc; j++ )
226         {
227             if( ses->trackv[j].id == id->sout_id )
228             {
229                 rtsp_strack_t *tr = ses->trackv + j;
230                 sout_AccessOutDelete( tr->access );
231                 REMOVE_ELEM( ses->trackv, ses->trackc, j );
232                 /* FIXME: are we supposed to notify the client? */
233             }
234         }
235     }
236
237     vlc_mutex_unlock( &rtsp->lock );
238     httpd_UrlDelete( id->url );
239     free( id );
240 }
241
242
243 /** rtsp must be locked */
244 static
245 rtsp_session_t *RtspClientNew( rtsp_stream_t *rtsp )
246 {
247     rtsp_session_t *s = malloc( sizeof( *s ) );
248     if( s == NULL )
249         return NULL;
250
251     s->stream = rtsp;
252     vlc_rand_bytes (&s->id, sizeof (s->id));
253     s->trackc = 0;
254     s->trackv = NULL;
255
256     TAB_APPEND( rtsp->sessionc, rtsp->sessionv, s );
257
258     return s;
259 }
260
261
262 /** rtsp must be locked */
263 static
264 rtsp_session_t *RtspClientGet( rtsp_stream_t *rtsp, const char *name )
265 {
266     char *end;
267     uint64_t id;
268     int i;
269
270     if( name == NULL )
271         return NULL;
272
273     errno = 0;
274     id = strtoull( name, &end, 0x10 );
275     if( errno || *end )
276         return NULL;
277
278     /* FIXME: use a hash/dictionary */
279     for( i = 0; i < rtsp->sessionc; i++ )
280     {
281         if( rtsp->sessionv[i]->id == id )
282             return rtsp->sessionv[i];
283     }
284     return NULL;
285 }
286
287
288 /** rtsp must be locked */
289 static
290 void RtspClientDel( rtsp_stream_t *rtsp, rtsp_session_t *session )
291 {
292     int i;
293     TAB_REMOVE( rtsp->sessionc, rtsp->sessionv, session );
294
295     for( i = 0; i < session->trackc; i++ )
296     {
297         rtp_del_sink( session->trackv[i].id, session->trackv[i].access );
298         sout_AccessOutDelete( session->trackv[i].access );
299     }
300
301     free( session->trackv );
302     free( session );
303 }
304
305
306 /** Aggregate RTSP callback */
307 static int RtspCallback( httpd_callback_sys_t *p_args,
308                          httpd_client_t *cl,
309                          httpd_message_t *answer,
310                          const httpd_message_t *query )
311 {
312     rtsp_stream_t *rtsp = (rtsp_stream_t *)p_args;
313     const char *psz_session = NULL, *psz;
314
315     if( answer == NULL || query == NULL )
316     {
317         return VLC_SUCCESS;
318     }
319
320     answer->i_proto = HTTPD_PROTO_RTSP;
321     answer->i_version= query->i_version;
322     answer->i_type   = HTTPD_MSG_ANSWER;
323     answer->i_body = 0;
324     answer->p_body = NULL;
325
326     if( httpd_MsgGet( query, "Require" ) != NULL )
327     {
328         answer->i_status = 551;
329         httpd_MsgAdd( answer, "Unsupported", "%s",
330                       httpd_MsgGet( query, "Require" ) );
331     }
332     else
333     switch( query->i_type )
334     {
335         case HTTPD_MSG_DESCRIBE:
336         {
337             char ip[NI_MAXNUMERICHOST], *ptr;
338             char control[sizeof("rtsp://[]:12345/") + sizeof( ip )
339                             + strlen( rtsp->psz_path )];
340
341             /* Build self-referential URL */
342             httpd_ServerIP( cl, ip );
343             ptr = strchr( ip, '%' );
344             if( ptr != NULL )
345                 *ptr = '\0';
346
347             if( strchr( ip, ':' ) != NULL )
348                 sprintf( control, "rtsp://[%s]:%u/%s", ip, rtsp->port,
349                          ( rtsp->psz_path != NULL ) ? rtsp->psz_path : "" );
350             else
351                 sprintf( control, "rtsp://%s:%u/%s", ip, rtsp->port,
352                          ( rtsp->psz_path != NULL ) ? rtsp->psz_path : "" );
353
354             ptr = SDPGenerate( rtsp->owner, control );
355
356             answer->i_status = 200;
357             httpd_MsgAdd( answer, "Content-Type",  "%s", "application/sdp" );
358             httpd_MsgAdd( answer, "Content-Base",  "%s", control );
359             answer->p_body = (uint8_t *)ptr;
360             answer->i_body = strlen( ptr );
361             break;
362         }
363
364         case HTTPD_MSG_SETUP:
365             answer->i_status = 459;
366             break;
367
368         case HTTPD_MSG_PLAY:
369         {
370             rtsp_session_t *ses;
371             answer->i_status = 200;
372
373             psz_session = httpd_MsgGet( query, "Session" );
374             if( httpd_MsgGet( query, "Range" ) != NULL )
375             {
376                 answer->i_status = 456; /* cannot seek */
377                 break;
378             }
379
380             vlc_mutex_lock( &rtsp->lock );
381             ses = RtspClientGet( rtsp, psz_session );
382             if( ses != NULL )
383             {
384                 for( int i = 0; i < ses->trackc; i++ )
385                 {
386                     rtsp_strack_t *tr = ses->trackv + i;
387                     if( !tr->playing )
388                     {
389                         tr->playing = VLC_TRUE;
390                         rtp_add_sink( tr->id, tr->access );
391                     }
392                 }
393             }
394             vlc_mutex_unlock( &rtsp->lock );
395
396             if( httpd_MsgGet( query, "Scale" ) != NULL )
397                 httpd_MsgAdd( answer, "Scale", "1." );
398             break;
399         }
400
401         case HTTPD_MSG_PAUSE:
402             answer->i_status = 405;
403             httpd_MsgAdd( answer, "Allow",
404                           "DESCRIBE, TEARDOWN, PLAY, GET_PARAMETER" );
405             break;
406
407         case HTTPD_MSG_GETPARAMETER:
408             if( query->i_body > 0 )
409             {
410                 answer->i_status = 451;
411                 break;
412             }
413
414             answer->i_status = 200;
415             break;
416
417         case HTTPD_MSG_TEARDOWN:
418         {
419             rtsp_session_t *ses;
420
421             /* for now only multicast so easy again */
422             answer->i_status = 200;
423
424             psz_session = httpd_MsgGet( query, "Session" );
425
426             vlc_mutex_lock( &rtsp->lock );
427             ses = RtspClientGet( rtsp, psz_session );
428             if( ses != NULL )
429                 RtspClientDel( rtsp, ses );
430             vlc_mutex_unlock( &rtsp->lock );
431             break;
432         }
433
434         default:
435             return VLC_EGENERIC;
436     }
437
438     httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
439     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
440
441     psz = httpd_MsgGet( query, "Cseq" );
442     if( psz != NULL )
443         httpd_MsgAdd( answer, "Cseq", "%s", psz );
444     psz = httpd_MsgGet( query, "Timestamp" );
445     if( psz != NULL )
446         httpd_MsgAdd( answer, "Timestamp", "%s", psz );
447
448     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
449
450     if( psz_session )
451         httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
452     return VLC_SUCCESS;
453 }
454
455
456 /** Finds the next transport choice */
457 static inline const char *transport_next( const char *str )
458 {
459     /* Looks for comma */
460     str = strchr( str, ',' );
461     if( str == NULL )
462         return NULL; /* No more transport options */
463
464     str++; /* skips comma */
465     while( strchr( "\r\n\t ", *str ) )
466         str++;
467
468     return (*str) ? str : NULL;
469 }
470
471
472 /** Finds the next transport parameter */
473 static inline const char *parameter_next( const char *str )
474 {
475     while( strchr( ",;", *str ) == NULL )
476         str++;
477
478     return (*str == ';') ? (str + 1) : NULL;
479 }
480
481
482 /** Non-aggregate RTSP callback */
483 static int RtspCallbackId( httpd_callback_sys_t *p_args,
484                            httpd_client_t *cl,
485                            httpd_message_t *answer,
486                            const httpd_message_t *query )
487 {
488     rtsp_stream_id_t *id = (rtsp_stream_id_t *)p_args;
489     rtsp_stream_t    *rtsp = id->stream;
490     sout_stream_t    *p_stream = id->stream->owner;
491     char psz_sesbuf[17];
492     const char *psz_session, *psz;
493
494     if( answer == NULL || query == NULL )
495         return VLC_SUCCESS;
496
497     /* */
498     answer->i_proto = HTTPD_PROTO_RTSP;
499     answer->i_version= query->i_version;
500     answer->i_type   = HTTPD_MSG_ANSWER;
501     answer->i_body = 0;
502     answer->p_body = NULL;
503
504     psz_session = httpd_MsgGet( query, "Session" );
505
506     if( httpd_MsgGet( query, "Require" ) != NULL )
507     {
508         answer->i_status = 551;
509         httpd_MsgAdd( answer, "Unsupported", "%s",
510                       httpd_MsgGet( query, "Require" ) );
511     }
512     else
513     switch( query->i_type )
514     {
515         case HTTPD_MSG_SETUP:
516         {
517             answer->i_status = 461;
518
519             for( const char *tpt = httpd_MsgGet( query, "Transport" );
520                  tpt != NULL;
521                  tpt = transport_next( tpt ) )
522             {
523                 vlc_bool_t b_multicast = VLC_TRUE, b_unsupp = VLC_FALSE;
524                 unsigned loport = 5004, hiport = 5005; /* from RFC3551 */
525
526                 /* Check transport protocol. */
527                 /* Currently, we only support RTP/AVP over UDP */
528                 if( strncmp( tpt, "RTP/AVP", 7 ) )
529                     continue;
530                 tpt += 7;
531                 if( strncmp( tpt, "/UDP", 4 ) == 0 )
532                     tpt += 4;
533                 if( strchr( ";,", *tpt ) == NULL )
534                     continue;
535
536                 /* Parse transport options */
537                 for( const char *opt = parameter_next( tpt );
538                      opt != NULL;
539                      opt = parameter_next( opt ) )
540                 {
541                     if( strncmp( opt, "multicast", 9 ) == 0)
542                         b_multicast = VLC_TRUE;
543                     else
544                     if( strncmp( opt, "unicast", 7 ) == 0 )
545                         b_multicast = VLC_FALSE;
546                     else
547                     if( sscanf( opt, "client_port=%u-%u", &loport, &hiport ) == 2 )
548                         ;
549                     else
550                     if( strncmp( opt, "mode=", 5 ) == 0 )
551                     {
552                         if( strncasecmp( opt + 5, "play", 4 )
553                          && strncasecmp( opt + 5, "\"PLAY\"", 6 ) )
554                         {
555                             /* Not playing?! */
556                             b_unsupp = VLC_TRUE;
557                             break;
558                         }
559                     }
560                     else
561                     if( strncmp( opt,"destination=", 12 ) == 0 )
562                     {
563                         answer->i_status = 403;
564                         b_unsupp = VLC_TRUE;
565                     }
566                     else
567                     {
568                     /*
569                      * Every other option is unsupported:
570                      *
571                      * "source" and "append" are invalid (server-only);
572                      * "ssrc" also (as clarified per RFC2326bis).
573                      *
574                      * For multicast, "port", "layers", "ttl" are set by the
575                      * stream output configuration.
576                      *
577                      * For unicast, we want to decide "server_port" values.
578                      *
579                      * "interleaved" is not implemented.
580                      */
581                         b_unsupp = VLC_TRUE;
582                         break;
583                     }
584                 }
585
586                 if( b_unsupp )
587                     continue;
588
589                 if( b_multicast )
590                 {
591                     const char *dst = id->dst;
592                     if( dst == NULL )
593                         continue;
594
595                     answer->i_status = 200;
596
597                     httpd_MsgAdd( answer, "Transport",
598                                   "RTP/AVP/UDP;destination=%s;port=%u-%u;"
599                                   "ttl=%d;mode=play",
600                                   dst, id->loport, id->hiport,
601                                   ( id->ttl > 0 ) ? id->ttl : 1 );
602                 }
603                 else
604                 {
605                     char ip[NI_MAXNUMERICHOST], url[NI_MAXNUMERICHOST + 8];
606                     static const char access[] = "udp{raw,rtcp}";
607                     rtsp_session_t *ses = NULL;
608                     rtsp_strack_t track = { id->sout_id, NULL, VLC_FALSE };
609
610                     if( httpd_ClientIP( cl, ip ) == NULL )
611                     {
612                         answer->i_status = 500;
613                         continue;
614                     }
615
616                     snprintf( url, sizeof( url ),
617                               ( strchr( ip, ':' ) != NULL ) ? "[%s]:%d" : "%s:%d",
618                               ip, loport );
619
620                     track.access = sout_AccessOutNew( p_stream->p_sout,
621                                                       access, url );
622                     if( track.access == NULL )
623                     {
624                         msg_Err( p_stream,
625                                  "cannot create access output for %s://%s",
626                                  access, url );
627                         answer->i_status = 500;
628                         continue;
629                     }
630
631                     char *src = var_GetNonEmptyString( track.access, "src-addr" );
632                     int sport = var_GetInteger( track.access, "src-port" );
633
634                     vlc_mutex_lock( &rtsp->lock );
635                     if( psz_session == NULL )
636                     {
637                         ses = RtspClientNew( rtsp );
638                         snprintf( psz_sesbuf, sizeof( psz_sesbuf ), I64Fx,
639                                   ses->id );
640                         psz_session = psz_sesbuf;
641                     }
642                     else
643                     {
644                         /* FIXME: we probably need to remove an access out,
645                          * if there is already one for the same ID */
646                         ses = RtspClientGet( rtsp, psz_session );
647                         if( ses == NULL )
648                         {
649                             answer->i_status = 454;
650                             vlc_mutex_unlock( &rtsp->lock );
651                             continue;
652                         }
653                     }
654
655                     INSERT_ELEM( ses->trackv, ses->trackc, ses->trackc, track );
656                     vlc_mutex_unlock( &rtsp->lock );
657
658                     httpd_ServerIP( cl, ip );
659
660                     if( ( src != NULL ) && strcmp( src, ip ) )
661                     {
662                         /* Specify source IP if it is different from the RTSP
663                          * control connection server address */
664                         char *ptr = strchr( src, '%' );
665                         if( ptr != NULL ) *ptr = '\0'; /* remove scope ID */
666
667                         httpd_MsgAdd( answer, "Transport",
668                                       "RTP/AVP/UDP;unicast;source=%s;"
669                                       "client_port=%u-%u;server_port=%u-%u;"
670                                       "mode=play",
671                                       src, loport, loport + 1, sport, sport + 1 );
672                     }
673                     else
674                     {
675                         httpd_MsgAdd( answer, "Transport",
676                                       "RTP/AVP/UDP;unicast;"
677                                       "client_port=%u-%u;server_port=%u-%u;"
678                                       "mode=play",
679                                       loport, loport + 1, sport, sport + 1 );
680                     }
681
682                     answer->i_status = 200;
683                     free( src );
684                 }
685                 break;
686             }
687             break;
688         }
689
690         case HTTPD_MSG_PLAY:
691         {
692             rtsp_session_t *ses;
693             answer->i_status = 200;
694
695             psz_session = httpd_MsgGet( query, "Session" );
696             if( httpd_MsgGet( query, "Range" ) != NULL )
697             {
698                 answer->i_status = 456; /* cannot seek */
699                 break;
700             }
701
702             vlc_mutex_lock( &rtsp->lock );
703             ses = RtspClientGet( rtsp, psz_session );
704             if( ses != NULL )
705             {
706                 for( int i = 0; i < ses->trackc; i++ )
707                 {
708                     rtsp_strack_t *tr = ses->trackv + i;
709                     if( !tr->playing && ( tr->id == id->sout_id ) )
710                     {
711                         tr->playing = VLC_TRUE;
712                         rtp_add_sink( tr->id, tr->access );
713                     }
714                 }
715             }
716             vlc_mutex_unlock( &rtsp->lock );
717
718             if( httpd_MsgGet( query, "Scale" ) != NULL )
719                 httpd_MsgAdd( answer, "Scale", "1." );
720             break;
721         }
722
723         case HTTPD_MSG_PAUSE:
724             answer->i_status = 405;
725             httpd_MsgAdd( answer, "Allow",
726                           "SETUP, TEARDOWN, PLAY, GET_PARAMETER" );
727             break;
728
729         case HTTPD_MSG_GETPARAMETER:
730             if( query->i_body > 0 )
731             {
732                 answer->i_status = 451;
733                 break;
734             }
735
736             answer->i_status = 200;
737             break;
738
739         case HTTPD_MSG_TEARDOWN:
740         {
741             rtsp_session_t *ses;
742
743             answer->i_status = 200;
744
745             psz_session = httpd_MsgGet( query, "Session" );
746
747             vlc_mutex_lock( &rtsp->lock );
748             ses = RtspClientGet( rtsp, psz_session );
749             if( ses != NULL )
750             {
751                 for( int i = 0; i < ses->trackc; i++ )
752                 {
753                     if( ses->trackv[i].id == id->sout_id )
754                     {
755                         rtp_del_sink( id->sout_id, ses->trackv[i].access );
756                         sout_AccessOutDelete( ses->trackv[i].access );
757                         REMOVE_ELEM( ses->trackv, ses->trackc, i );
758                     }
759                 }
760             }
761             vlc_mutex_unlock( &rtsp->lock );
762             break;
763         }
764
765         default:
766             answer->i_status = 460;
767             break;
768     }
769
770     psz = httpd_MsgGet( query, "Cseq" );
771     if( psz != NULL )
772         httpd_MsgAdd( answer, "Cseq", "%s", psz );
773     psz = httpd_MsgGet( query, "Timestamp" );
774     if( psz != NULL )
775         httpd_MsgAdd( answer, "Timestamp", "%s", psz );
776     httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
777     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
778     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
779
780     if( psz_session )
781         httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
782     return VLC_SUCCESS;
783 }