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