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