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