]> git.sesse.net Git - vlc/blob - modules/stream_out/rtsp.c
Cosmetics
[vlc] / 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 )
548                                 == 2 )
549                         ;
550                     else
551                     if( strncmp( opt, "mode=", 5 ) == 0 )
552                     {
553                         if( strncasecmp( opt + 5, "play", 4 )
554                          && strncasecmp( opt + 5, "\"PLAY\"", 6 ) )
555                         {
556                             /* Not playing?! */
557                             b_unsupp = VLC_TRUE;
558                             break;
559                         }
560                     }
561                     else
562                     if( strncmp( opt,"destination=", 12 ) == 0 )
563                     {
564                         answer->i_status = 403;
565                         b_unsupp = VLC_TRUE;
566                     }
567                     else
568                     {
569                     /*
570                      * Every other option is unsupported:
571                      *
572                      * "source" and "append" are invalid (server-only);
573                      * "ssrc" also (as clarified per RFC2326bis).
574                      *
575                      * For multicast, "port", "layers", "ttl" are set by the
576                      * stream output configuration.
577                      *
578                      * For unicast, we want to decide "server_port" values.
579                      *
580                      * "interleaved" is not implemented.
581                      */
582                         b_unsupp = VLC_TRUE;
583                         break;
584                     }
585                 }
586
587                 if( b_unsupp )
588                     continue;
589
590                 if( b_multicast )
591                 {
592                     const char *dst = id->dst;
593                     if( dst == NULL )
594                         continue;
595
596                     answer->i_status = 200;
597
598                     httpd_MsgAdd( answer, "Transport",
599                                   "RTP/AVP/UDP;destination=%s;port=%u-%u;"
600                                   "ttl=%d;mode=play",
601                                   dst, id->loport, id->hiport,
602                                   ( id->ttl > 0 ) ? id->ttl : 1 );
603                 }
604                 else
605                 {
606                     char ip[NI_MAXNUMERICHOST], url[NI_MAXNUMERICHOST + 8];
607                     static const char access[] = "udp{raw,rtcp}";
608                     rtsp_session_t *ses = NULL;
609                     rtsp_strack_t track = { id->sout_id, NULL, VLC_FALSE };
610
611                     if( httpd_ClientIP( cl, ip ) == NULL )
612                     {
613                         answer->i_status = 500;
614                         continue;
615                     }
616
617                     snprintf( url, sizeof( url ),
618                               ( strchr( ip, ':' ) != NULL )
619                                   ? "[%s]:%d" : "%s:%d",
620                               ip, loport );
621
622                     track.access = sout_AccessOutNew( p_stream->p_sout,
623                                                       access, url );
624                     if( track.access == NULL )
625                     {
626                         msg_Err( p_stream,
627                                  "cannot create access output for %s://%s",
628                                  access, url );
629                         answer->i_status = 500;
630                         continue;
631                     }
632
633                     char *src = var_GetNonEmptyString( track.access,
634                                                        "src-addr" );
635                     int sport = var_GetInteger( track.access, "src-port" );
636
637                     vlc_mutex_lock( &rtsp->lock );
638                     if( psz_session == NULL )
639                     {
640                         ses = RtspClientNew( rtsp );
641                         snprintf( psz_sesbuf, sizeof( psz_sesbuf ), I64Fx,
642                                   ses->id );
643                         psz_session = psz_sesbuf;
644                     }
645                     else
646                     {
647                         /* FIXME: we probably need to remove an access out,
648                          * if there is already one for the same ID */
649                         ses = RtspClientGet( rtsp, psz_session );
650                         if( ses == NULL )
651                         {
652                             answer->i_status = 454;
653                             vlc_mutex_unlock( &rtsp->lock );
654                             continue;
655                         }
656                     }
657
658                     INSERT_ELEM( ses->trackv, ses->trackc, ses->trackc, track );
659                     vlc_mutex_unlock( &rtsp->lock );
660
661                     httpd_ServerIP( cl, ip );
662
663                     if( ( src != NULL ) && strcmp( src, ip ) )
664                     {
665                         /* Specify source IP if it is different from the RTSP
666                          * control connection server address */
667                         char *ptr = strchr( src, '%' );
668                         if( ptr != NULL ) *ptr = '\0'; /* remove scope ID */
669
670                         httpd_MsgAdd( answer, "Transport",
671                                       "RTP/AVP/UDP;unicast;source=%s;"
672                                       "client_port=%u-%u;server_port=%u-%u;"
673                                       "mode=play",
674                                       src, loport, loport + 1, sport,
675                                       sport + 1 );
676                     }
677                     else
678                     {
679                         httpd_MsgAdd( answer, "Transport",
680                                       "RTP/AVP/UDP;unicast;"
681                                       "client_port=%u-%u;server_port=%u-%u;"
682                                       "mode=play",
683                                       loport, loport + 1, sport, sport + 1 );
684                     }
685
686                     answer->i_status = 200;
687                     free( src );
688                 }
689                 break;
690             }
691             break;
692         }
693
694         case HTTPD_MSG_PLAY:
695         {
696             rtsp_session_t *ses;
697             answer->i_status = 200;
698
699             psz_session = httpd_MsgGet( query, "Session" );
700             if( httpd_MsgGet( query, "Range" ) != NULL )
701             {
702                 answer->i_status = 456; /* cannot seek */
703                 break;
704             }
705
706             vlc_mutex_lock( &rtsp->lock );
707             ses = RtspClientGet( rtsp, psz_session );
708             if( ses != NULL )
709             {
710                 for( int i = 0; i < ses->trackc; i++ )
711                 {
712                     rtsp_strack_t *tr = ses->trackv + i;
713                     if( !tr->playing && ( tr->id == id->sout_id ) )
714                     {
715                         tr->playing = VLC_TRUE;
716                         rtp_add_sink( tr->id, tr->access );
717                     }
718                 }
719             }
720             vlc_mutex_unlock( &rtsp->lock );
721
722             if( httpd_MsgGet( query, "Scale" ) != NULL )
723                 httpd_MsgAdd( answer, "Scale", "1." );
724             break;
725         }
726
727         case HTTPD_MSG_PAUSE:
728             answer->i_status = 405;
729             httpd_MsgAdd( answer, "Allow",
730                           "SETUP, TEARDOWN, PLAY, GET_PARAMETER" );
731             break;
732
733         case HTTPD_MSG_GETPARAMETER:
734             if( query->i_body > 0 )
735             {
736                 answer->i_status = 451;
737                 break;
738             }
739
740             answer->i_status = 200;
741             break;
742
743         case HTTPD_MSG_TEARDOWN:
744         {
745             rtsp_session_t *ses;
746
747             answer->i_status = 200;
748
749             psz_session = httpd_MsgGet( query, "Session" );
750
751             vlc_mutex_lock( &rtsp->lock );
752             ses = RtspClientGet( rtsp, psz_session );
753             if( ses != NULL )
754             {
755                 for( int i = 0; i < ses->trackc; i++ )
756                 {
757                     if( ses->trackv[i].id == id->sout_id )
758                     {
759                         rtp_del_sink( id->sout_id, ses->trackv[i].access );
760                         sout_AccessOutDelete( ses->trackv[i].access );
761                         REMOVE_ELEM( ses->trackv, ses->trackc, i );
762                     }
763                 }
764             }
765             vlc_mutex_unlock( &rtsp->lock );
766             break;
767         }
768
769         default:
770             answer->i_status = 460;
771             break;
772     }
773
774     psz = httpd_MsgGet( query, "Cseq" );
775     if( psz != NULL )
776         httpd_MsgAdd( answer, "Cseq", "%s", psz );
777     psz = httpd_MsgGet( query, "Timestamp" );
778     if( psz != NULL )
779         httpd_MsgAdd( answer, "Timestamp", "%s", psz );
780     httpd_MsgAdd( answer, "Server", "%s", PACKAGE_STRING );
781     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
782     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
783
784     if( psz_session )
785         httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
786     return VLC_SUCCESS;
787 }