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