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