]> git.sesse.net Git - vlc/blob - modules/misc/rtsp.c
Fix the exact same remote DoS as in RTSP stream output
[vlc] / modules / misc / rtsp.c
1 /*****************************************************************************
2  * rtsp.c: rtsp VoD server module
3  *****************************************************************************
4  * Copyright (C) 2003-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #include <vlc/vlc.h>
30 #include <vlc_input.h>
31 #include <vlc_sout.h>
32 #include <vlc_block.h>
33
34 #include "vlc_httpd.h"
35 #include "vlc_vod.h"
36 #include "vlc_url.h"
37 #include <vlc_network.h>
38 #include <vlc_charset.h>
39 #include <vlc_strings.h>
40
41 #include <errno.h>
42
43 /*****************************************************************************
44  * Module descriptor
45  *****************************************************************************/
46 static int  Open ( vlc_object_t * );
47 static void Close( vlc_object_t * );
48
49 #define HOST_TEXT N_( "RTSP host address" )
50 #define HOST_LONGTEXT N_( \
51     "This defines the address, port and path the RTSP VOD server will listen " \
52     "on.\nSyntax is address:port/path. The default is to listen on all "\
53     "interfaces (address 0.0.0.0), on port 554, with no path.\nTo listen " \
54     "only on the local interface, use \"localhost\" as address." )
55
56 #define THROTLE_TEXT N_( "Maximum number of connections" )
57 #define THROTLE_LONGTEXT N_( "This limits the maximum number of clients " \
58     "that can connect to the RTSP VOD. 0 means no limit."  )
59
60 #define RAWMUX_TEXT N_( "MUX for RAW RTSP transport" )
61
62 #define SESSION_TIMEOUT_TEXT N_( "Sets the timeout option in the RTSP " \
63     "session string" )
64 #define SESSION_TIMEOUT_LONGTEXT N_( "Defines what timeout option to add " \
65     "to the RTSP session ID string. Setting it to a negative number removes " \
66     "the timeout option entirely. This is needed by some IPTV STBs (such as " \
67     "those made by HansunTech) which get confused by it. The default is 5." )
68
69 vlc_module_begin();
70     set_shortname( _("RTSP VoD" ) );
71     set_description( _("RTSP VoD server") );
72     set_category( CAT_SOUT );
73     set_subcategory( SUBCAT_SOUT_VOD );
74     set_capability( "vod server", 1 );
75     set_callbacks( Open, Close );
76     add_shortcut( "rtsp" );
77     add_string ( "rtsp-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
78     add_string( "rtsp-raw-mux", "ts", NULL, RAWMUX_TEXT, RAWMUX_TEXT, VLC_TRUE );
79     add_integer( "rtsp-throttle-users", 0, NULL, THROTLE_TEXT,
80                                            THROTLE_LONGTEXT, VLC_TRUE );
81     add_integer( "rtsp-session-timeout", 5, NULL, SESSION_TIMEOUT_TEXT,
82                  SESSION_TIMEOUT_LONGTEXT, VLC_TRUE );
83 vlc_module_end();
84
85 /*****************************************************************************
86  * Exported prototypes
87  *****************************************************************************/
88
89 typedef struct media_es_t media_es_t;
90
91 typedef struct
92 {
93     media_es_t *p_media_es;
94     char *psz_ip;
95     int i_port;
96
97 } rtsp_client_es_t;
98
99 typedef struct
100 {
101     char *psz_session;
102     int64_t i_last; /* for timeout */
103
104     vlc_bool_t b_playing; /* is it in "play" state */
105     vlc_bool_t b_paused; /* is it in "pause" state */
106
107     int i_es;
108     rtsp_client_es_t **es;
109
110 } rtsp_client_t;
111
112 struct media_es_t
113 {
114     /* VoD server */
115     vod_t *p_vod;
116
117     /* RTSP server */
118     httpd_url_t *p_rtsp_url;
119
120     vod_media_t *p_media;
121
122     es_format_t fmt;
123     int         i_port;
124     uint8_t     i_payload_type;
125     char        *psz_rtpmap;
126     char        *psz_fmtp;
127
128 };
129
130 struct vod_media_t
131 {
132     int id;
133
134     /* VoD server */
135     vod_t *p_vod;
136
137     /* RTSP server */
138     httpd_url_t  *p_rtsp_url;
139     char         *psz_rtsp_control_v4;
140     char         *psz_rtsp_control_v6;
141     char         *psz_rtsp_path;
142
143     int  i_port;
144     int  i_port_audio;
145     int  i_port_video;
146     int  i_ttl;
147     int  i_payload_type;
148
149     int64_t i_sdp_id;
150     int     i_sdp_version;
151
152     vlc_bool_t b_multicast;
153
154     vlc_mutex_t lock;
155
156     /* ES list */
157     int        i_es;
158     media_es_t **es;
159     char       *psz_mux;
160     vlc_bool_t  b_raw;
161
162     /* RTSP client */
163     int           i_rtsp;
164     rtsp_client_t **rtsp;
165
166     /* Infos */
167     char *psz_session_name;
168     char *psz_session_description;
169     char *psz_session_url;
170     char *psz_session_email;
171     mtime_t i_length;
172 };
173
174 struct vod_sys_t
175 {
176     /* RTSP server */
177     httpd_host_t *p_rtsp_host;
178     char *psz_path;
179     int i_port;
180     int i_throttle_users;
181     int i_connections;
182
183     char *psz_raw_mux;
184
185     int i_session_timeout;
186
187     /* List of media */
188     vlc_mutex_t lock_media;
189     int i_media_id;
190     int i_media;
191     vod_media_t **media;
192
193     /* */
194     block_fifo_t *p_fifo_cmd;
195 };
196
197 /* rtsp delayed command (to avoid deadlock between vlm/httpd) */
198 typedef enum
199 {
200     RTSP_CMD_TYPE_NONE,  /* Exit requested */
201
202     RTSP_CMD_TYPE_PLAY,
203     RTSP_CMD_TYPE_PAUSE,
204     RTSP_CMD_TYPE_STOP,
205     RTSP_CMD_TYPE_SEEK,
206     RTSP_CMD_TYPE_REWIND,
207     RTSP_CMD_TYPE_FORWARD,
208 } rtsp_cmd_type_t;
209
210 static vod_media_t *MediaNew( vod_t *, const char *, input_item_t * );
211 static void         MediaDel( vod_t *, vod_media_t * );
212 static int          MediaAddES( vod_t *, vod_media_t *, es_format_t * );
213 static void         MediaDelES( vod_t *, vod_media_t *, es_format_t * );
214
215 static void CommandThread( vlc_object_t *p_this );
216 static void CommandPush( vod_t *, rtsp_cmd_type_t, vod_media_t *, const char *psz_session,
217                          double f_arg, const char *psz_arg );
218
219 static rtsp_client_t *RtspClientNew( vod_media_t *, char * );
220 static rtsp_client_t *RtspClientGet( vod_media_t *, const char * );
221 static void           RtspClientDel( vod_media_t *, rtsp_client_t * );
222
223 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
224                          httpd_message_t *, const httpd_message_t * );
225 static int RtspCallbackES( httpd_callback_sys_t *, httpd_client_t *,
226                            httpd_message_t *, const httpd_message_t * );
227
228 static char *SDPGenerate( const vod_media_t *, httpd_client_t *cl );
229
230 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
231 {
232     static const char hex[16] = "0123456789abcdef";
233     int i;
234
235     for( i = 0; i < i_data; i++ )
236     {
237         s[2*i+0] = hex[(p_data[i]>>4)&0xf];
238         s[2*i+1] = hex[(p_data[i]   )&0xf];
239     }
240     s[2*i_data] = '\0';
241 }
242
243 /*****************************************************************************
244  * Open: Starts the RTSP server module
245  *****************************************************************************/
246 static int Open( vlc_object_t *p_this )
247 {
248     vod_t *p_vod = (vod_t *)p_this;
249     vod_sys_t *p_sys = 0;
250     char *psz_url = 0;
251     vlc_url_t url;
252
253     psz_url = config_GetPsz( p_vod, "rtsp-host" );
254     vlc_UrlParse( &url, psz_url, 0 );
255     if( psz_url ) free( psz_url );
256
257     if( url.i_port <= 0 ) url.i_port = 554;
258
259     p_vod->p_sys = p_sys = malloc( sizeof( vod_sys_t ) );
260     if( !p_sys ) goto error;
261     p_sys->p_rtsp_host = 0;
262
263     p_sys->i_session_timeout = var_CreateGetInteger( p_this, "rtsp-session-timeout" );
264
265     p_sys->i_throttle_users = var_CreateGetInteger( p_this, "rtsp-throttle-users" );
266     msg_Dbg( p_this, "allowing up to %d connections", p_sys->i_throttle_users );
267     p_sys->i_connections = 0;
268
269     p_sys->psz_raw_mux = var_CreateGetString( p_this, "rtsp-raw-mux" );
270
271     p_sys->p_rtsp_host =
272         httpd_HostNew( VLC_OBJECT(p_vod), url.psz_host, url.i_port );
273     if( !p_sys->p_rtsp_host )
274     {
275         msg_Err( p_vod, "cannot create RTSP server (%s:%i)",
276                  url.psz_host, url.i_port );
277         goto error;
278     }
279
280     p_sys->psz_path = strdup( url.psz_path ? url.psz_path : "/" );
281     p_sys->i_port = url.i_port;
282
283     vlc_UrlClean( &url );
284
285     vlc_mutex_init( p_vod, &p_sys->lock_media );
286
287     TAB_INIT( p_sys->i_media, p_sys->media );
288     p_sys->i_media_id = 0;
289
290     p_vod->pf_media_new = MediaNew;
291     p_vod->pf_media_del = MediaDel;
292     p_vod->pf_media_add_es = MediaAddES;
293     p_vod->pf_media_del_es = MediaDelES;
294
295     p_sys->p_fifo_cmd = block_FifoNew( p_vod );
296     if( vlc_thread_create( p_vod, "rtsp vod thread", CommandThread,
297                            VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
298     {
299         msg_Err( p_vod, "cannot spawn rtsp vod thread" );
300         block_FifoRelease( p_sys->p_fifo_cmd );
301         free( p_sys->psz_path );
302         goto error;
303     }
304
305     return VLC_SUCCESS;
306
307 error:
308     if( p_sys && p_sys->p_rtsp_host ) httpd_HostDelete( p_sys->p_rtsp_host );
309     if( p_sys && p_sys->psz_raw_mux ) free( p_sys->psz_raw_mux );
310     if( p_sys ) free( p_sys );
311     vlc_UrlClean( &url );
312
313     return VLC_EGENERIC;
314 }
315
316 /*****************************************************************************
317  * Close:
318  *****************************************************************************/
319 static void Close( vlc_object_t * p_this )
320 {
321     vod_t *p_vod = (vod_t *)p_this;
322     vod_sys_t *p_sys = p_vod->p_sys;
323
324     /* Stop command thread */
325     vlc_object_kill( p_vod );
326     CommandPush( p_vod, RTSP_CMD_TYPE_NONE, NULL, NULL, 0.0, NULL );
327     vlc_thread_join( p_vod );
328
329     block_FifoRelease( p_sys->p_fifo_cmd );
330
331     httpd_HostDelete( p_sys->p_rtsp_host );
332     var_Destroy( p_this, "rtsp-session-timeout" );
333     var_Destroy( p_this, "rtsp-throttle-users" );
334     var_Destroy( p_this, "rtsp-raw-mux" );
335
336     /* Check VLM is not buggy */
337     if( p_sys->i_media > 0 )
338         msg_Err( p_vod, "rtsp vod leaking %d medias", p_sys->i_media );
339     TAB_CLEAN( p_sys->i_media, p_sys->media );
340
341     vlc_mutex_destroy( &p_sys->lock_media );
342
343     free( p_sys->psz_path );
344     free( p_sys->psz_raw_mux );
345     free( p_sys );
346 }
347
348 /*****************************************************************************
349  * Media handling
350  *****************************************************************************/
351 static vod_media_t *MediaNew( vod_t *p_vod, const char *psz_name,
352                               input_item_t *p_item )
353 {
354     vod_sys_t *p_sys = p_vod->p_sys;
355     vod_media_t *p_media = malloc( sizeof(vod_media_t) );
356     int i;
357
358     if( !p_media )
359     {
360         msg_Err( p_vod, "not enough memory" );
361         return NULL;
362     }
363
364     memset( p_media, 0, sizeof(vod_media_t) );
365     p_media->id = p_sys->i_media_id++;
366     TAB_INIT( p_media->i_es, p_media->es );
367     p_media->psz_mux = 0;
368     TAB_INIT( p_media->i_rtsp, p_media->rtsp );
369     p_media->b_raw = VLC_FALSE;
370
371     asprintf( &p_media->psz_rtsp_path, "%s%s", p_sys->psz_path, psz_name );
372     p_media->p_rtsp_url =
373         httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, NULL,
374                             NULL, NULL );
375
376     if( !p_media->p_rtsp_url )
377     {
378         msg_Err( p_vod, "cannot create RTSP url (%s)", p_media->psz_rtsp_path);
379         free( p_media->psz_rtsp_path );
380         free( p_media );
381         return NULL;
382     }
383
384     msg_Dbg( p_vod, "created RTSP url: %s", p_media->psz_rtsp_path );
385
386     asprintf( &p_media->psz_rtsp_control_v4,
387                "a=control:rtsp://%%s:%d%s/trackID=%%d\r\n",
388                p_sys->i_port, p_media->psz_rtsp_path );
389     asprintf( &p_media->psz_rtsp_control_v6,
390                "a=control:rtsp://[%%s]:%d%s/trackID=%%d\r\n",
391               p_sys->i_port, p_media->psz_rtsp_path );
392
393     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_SETUP,
394                     RtspCallback, (void*)p_media );
395     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_DESCRIBE,
396                     RtspCallback, (void*)p_media );
397     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PLAY,
398                     RtspCallback, (void*)p_media );
399     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PAUSE,
400                     RtspCallback, (void*)p_media );
401     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_GETPARAMETER,
402                     RtspCallback, (void*)p_media );
403     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_TEARDOWN,
404                     RtspCallback, (void*)p_media );
405
406     p_media->p_vod = p_vod;
407
408     vlc_mutex_lock( &p_sys->lock_media );
409     TAB_APPEND( p_sys->i_media, p_sys->media, p_media );
410     vlc_mutex_unlock( &p_sys->lock_media );
411
412     vlc_mutex_init( p_vod, &p_media->lock );
413     p_media->psz_session_name = strdup("");
414     p_media->psz_session_description = strdup("");
415     p_media->psz_session_url = strdup("");
416     p_media->psz_session_email = strdup("");
417
418     p_media->i_port_audio = 1234;
419     p_media->i_port_video = 1236;
420     p_media->i_port       = 1238;
421     p_media->i_payload_type = 96;
422
423     p_media->i_sdp_id = mdate();
424     p_media->i_sdp_version = 1;
425     p_media->i_length = input_item_GetDuration( p_item );
426
427     vlc_mutex_lock( &p_item->lock );
428     msg_Dbg( p_vod, "media has %i declared ES", p_item->i_es );
429     for( i = 0; i < p_item->i_es; i++ )
430     {
431         MediaAddES( p_vod, p_media, p_item->es[i] );
432     }
433     vlc_mutex_unlock( &p_item->lock );
434
435     return p_media;
436 }
437
438 static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
439 {
440     vod_sys_t *p_sys = p_vod->p_sys;
441
442     msg_Dbg( p_vod, "deleting media: %s", p_media->psz_rtsp_path );
443
444     vlc_mutex_lock( &p_sys->lock_media );
445     TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
446     vlc_mutex_unlock( &p_sys->lock_media );
447
448     while( p_media->i_rtsp > 0 )
449         RtspClientDel( p_media, p_media->rtsp[0] );
450     TAB_CLEAN( p_media->i_rtsp, p_media->rtsp );
451
452     httpd_UrlDelete( p_media->p_rtsp_url );
453     if( p_media->psz_rtsp_path ) free( p_media->psz_rtsp_path );
454     if( p_media->psz_rtsp_control_v6 ) free( p_media->psz_rtsp_control_v6 );
455     if( p_media->psz_rtsp_control_v4 ) free( p_media->psz_rtsp_control_v4 );
456
457     while( p_media->i_es )
458         MediaDelES( p_vod, p_media, &p_media->es[0]->fmt );
459     TAB_CLEAN( p_media->i_es, p_media->es );
460
461     vlc_mutex_destroy( &p_media->lock );
462
463     if( p_media->psz_session_name ) free( p_media->psz_session_name );
464     if( p_media->psz_session_description ) free( p_media->psz_session_description );
465     if( p_media->psz_session_url ) free( p_media->psz_session_url );
466     if( p_media->psz_session_email ) free( p_media->psz_session_email );
467     if( p_media->psz_mux ) free( p_media->psz_mux );
468     free( p_media );
469 }
470
471 static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
472 {
473     media_es_t *p_es = malloc( sizeof(media_es_t) );
474     char *psz_urlc;
475
476     if( !p_es ) return VLC_ENOMEM;
477     memset( p_es, 0, sizeof(media_es_t) );
478
479     if( p_media->psz_mux ) free( p_media->psz_mux );
480     p_media->psz_mux = NULL;
481
482     /* TODO: update SDP, etc... */
483     asprintf( &psz_urlc, "%s/trackID=%d",
484               p_media->psz_rtsp_path, p_media->i_es );
485     msg_Dbg( p_vod, "  - ES %4.4s (%s)", (char *)&p_fmt->i_codec, psz_urlc );
486
487     switch( p_fmt->i_codec )
488     {
489         case VLC_FOURCC( 's', '1', '6', 'b' ):
490             if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
491             {
492                 p_es->i_payload_type = 11;
493             }
494             else if( p_fmt->audio.i_channels == 2 && p_fmt->audio.i_rate == 44100 )
495             {
496                 p_es->i_payload_type = 10;
497             }
498             else
499             {
500                 p_es->i_payload_type = p_media->i_payload_type++;
501             }
502             p_es->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
503             sprintf( p_es->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
504                     p_fmt->audio.i_channels );
505             break;
506         case VLC_FOURCC( 'u', '8', ' ', ' ' ):
507             p_es->i_payload_type = p_media->i_payload_type++;
508             p_es->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
509             sprintf( p_es->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
510                     p_fmt->audio.i_channels );
511             break;
512         case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
513             p_es->i_payload_type = 14;
514             p_es->psz_rtpmap = strdup( "MPA/90000" );
515             break;
516         case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
517             p_es->i_payload_type = 32;
518             p_es->psz_rtpmap = strdup( "MPV/90000" );
519             break;
520         case VLC_FOURCC( 'a', '5', '2', ' ' ):
521             p_es->i_payload_type = p_media->i_payload_type++;
522             p_es->psz_rtpmap = strdup( "ac3/90000" );
523             break;
524         case VLC_FOURCC( 'H', '2', '6', '3' ):
525             p_es->i_payload_type = p_media->i_payload_type++;
526             p_es->psz_rtpmap = strdup( "H263-1998/90000" );
527             break;
528         case VLC_FOURCC( 'h', '2', '6', '4' ):
529             p_es->i_payload_type = p_media->i_payload_type++;
530             p_es->psz_rtpmap = strdup( "H264/90000" );
531             p_es->psz_fmtp = NULL;
532             /* FIXME AAAAAAAAAAAARRRRRRRRGGGG copied from stream_out/rtp.c */
533             if( p_fmt->i_extra > 0 )
534             {
535                 uint8_t *p_buffer = p_fmt->p_extra;
536                 int     i_buffer = p_fmt->i_extra;
537                 char    *p_64_sps = NULL;
538                 char    *p_64_pps = NULL;
539                 char    hexa[6+1];
540
541                 while( i_buffer > 4 &&
542                        p_buffer[0] == 0 && p_buffer[1] == 0 &&
543                        p_buffer[2] == 0 && p_buffer[3] == 1 )
544                 {
545                     const int i_nal_type = p_buffer[4]&0x1f;
546                     int i_offset;
547                     int i_size      = 0;
548
549                     i_size = i_buffer;
550                     for( i_offset = 4; i_offset+3 < i_buffer ; i_offset++)
551                     {
552                         if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 0 && p_buffer[i_offset+3] == 1 )
553                         {
554                             /* we found another startcode */
555                             i_size = i_offset;
556                             break;
557                         }
558                     }
559                     if( i_nal_type == 7 )
560                     {
561                         p_64_sps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
562                         sprintf_hexa( hexa, &p_buffer[5], 3 );
563                     }
564                     else if( i_nal_type == 8 )
565                     {
566                         p_64_pps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
567                     }
568                     i_buffer -= i_size;
569                     p_buffer += i_size;
570                 }
571                 /* */
572                 if( p_64_sps && p_64_pps )
573                     asprintf( &p_es->psz_fmtp, "packetization-mode=1;profile-level-id=%s;sprop-parameter-sets=%s,%s;", hexa, p_64_sps, p_64_pps );
574                 if( p_64_sps )
575                     free( p_64_sps );
576                 if( p_64_pps )
577                     free( p_64_pps );
578             }
579             if( !p_es->psz_fmtp )
580                 p_es->psz_fmtp = strdup( "packetization-mode=1" );
581             break;
582         case VLC_FOURCC( 'm', 'p', '4', 'v' ):
583             p_es->i_payload_type = p_media->i_payload_type++;
584             p_es->psz_rtpmap = strdup( "MP4V-ES/90000" );
585             if( p_fmt->i_extra > 0 )
586             {
587                 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
588                 p_es->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
589                 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
590                 sprintf( p_es->psz_fmtp,
591                         "profile-level-id=3; config=%s;", p_hexa );
592                 free( p_hexa );
593             }
594             break;
595         case VLC_FOURCC( 'm', 'p', '4', 'a' ):
596             p_es->i_payload_type = p_media->i_payload_type++;
597             p_es->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
598             sprintf( p_es->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
599             if( p_fmt->i_extra > 0 )
600             {
601                 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
602                 p_es->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
603                 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
604                 sprintf( p_es->psz_fmtp,
605                         "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
606                         "config=%s; SizeLength=13;IndexLength=3; "
607                         "IndexDeltaLength=3; Profile=1;", p_hexa );
608                 free( p_hexa );
609             }
610             break;
611         case VLC_FOURCC( 'm', 'p', '2', 't' ):
612             p_media->psz_mux = strdup("ts");
613             p_es->i_payload_type = 33;
614             p_es->psz_rtpmap = strdup( "MP2T/90000" );
615             break;
616         case VLC_FOURCC( 'm', 'p', '2', 'p' ):
617             p_media->psz_mux = strdup("ps");
618             p_es->i_payload_type = p_media->i_payload_type++;
619             p_es->psz_rtpmap = strdup( "MP2P/90000" );
620             break;
621         case VLC_FOURCC( 's', 'a', 'm', 'r' ):
622             p_es->i_payload_type = p_media->i_payload_type++;
623             p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
624                                     "AMR/8000/2" : "AMR/8000" );
625             p_es->psz_fmtp = strdup( "octet-align=1" );
626             break;
627         case VLC_FOURCC( 's', 'a', 'w', 'b' ):
628             p_es->i_payload_type = p_media->i_payload_type++;
629             p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
630                                     "AMR-WB/16000/2" : "AMR-WB/16000" );
631             p_es->psz_fmtp = strdup( "octet-align=1" );
632             break;
633
634         default:
635             msg_Err( p_vod, "cannot add this stream (unsupported "
636                     "codec: %4.4s)", (char*)&p_fmt->i_codec );
637             free( p_es );
638             return VLC_EGENERIC;
639     }
640
641     p_es->p_rtsp_url =
642         httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, NULL, NULL,
643                             NULL );
644
645     if( !p_es->p_rtsp_url )
646     {
647         msg_Err( p_vod, "cannot create RTSP url (%s)", psz_urlc );
648         free( psz_urlc );
649         free( p_es );
650         return VLC_EGENERIC;
651     }
652     free( psz_urlc );
653
654     httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
655                     RtspCallbackES, (void*)p_es );
656     httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_TEARDOWN,
657                     RtspCallbackES, (void*)p_es );
658     httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PLAY,
659                     RtspCallbackES, (void*)p_es );
660     httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PAUSE,
661                     RtspCallbackES, (void*)p_es );
662
663     es_format_Copy( &p_es->fmt, p_fmt );
664     p_es->p_vod = p_vod;
665     p_es->p_media = p_media;
666
667 #if 0
668     /* Choose the port */
669     if( p_fmt->i_cat == AUDIO_ES && p_media->i_port_audio > 0 )
670     {
671         p_es->i_port = p_media->i_port_audio;
672         p_media->i_port_audio = 0;
673     }
674     else if( p_fmt->i_cat == VIDEO_ES && p_media->i_port_video > 0 )
675     {
676         p_es->i_port = p_media->i_port_video;
677         p_media->i_port_video = 0;
678     }
679     while( !p_es->i_port )
680     {
681         if( p_media->i_port != p_media->i_port_audio &&
682             p_media->i_port != p_media->i_port_video )
683         {
684             p_es->i_port = p_media->i_port;
685             p_media->i_port += 2;
686             break;
687         }
688         p_media->i_port += 2;
689     }
690 #else
691
692     p_es->i_port = 0;
693 #endif
694
695     vlc_mutex_lock( &p_media->lock );
696     TAB_APPEND( p_media->i_es, p_media->es, p_es );
697     vlc_mutex_unlock( &p_media->lock );
698
699     p_media->i_sdp_version++;
700
701     return VLC_SUCCESS;
702 }
703
704 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
705 {
706     media_es_t *p_es = NULL;
707     int i;
708
709     /* Find the ES */
710     for( i = 0; i < p_media->i_es; i++ )
711     {
712         if( p_media->es[i]->fmt.i_cat == p_fmt->i_cat &&
713             p_media->es[i]->fmt.i_codec == p_fmt->i_codec &&
714             p_media->es[i]->fmt.i_id == p_fmt->i_id )
715         {
716             p_es = p_media->es[i];
717         }
718     }
719     if( !p_es ) return;
720
721     msg_Dbg( p_vod, "  - Removing ES %4.4s", (char *)&p_fmt->i_codec );
722
723     vlc_mutex_lock( &p_media->lock );
724     TAB_REMOVE( p_media->i_es, p_media->es, p_es );
725     vlc_mutex_unlock( &p_media->lock );
726
727     if( p_es->psz_rtpmap ) free( p_es->psz_rtpmap );
728     if( p_es->psz_fmtp ) free( p_es->psz_fmtp );
729     p_media->i_sdp_version++;
730
731     if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
732     es_format_Clean( &p_es->fmt );
733     free( p_es );
734 }
735
736 /* */
737 typedef struct
738 {
739     int i_type;
740     int i_media_id;
741     //vod_media_t *p_media;
742     char *psz_session;
743     char *psz_arg;
744     double f_arg;
745 } rtsp_cmd_t;
746
747 static void CommandPush( vod_t *p_vod, rtsp_cmd_type_t i_type, vod_media_t *p_media, const char *psz_session,
748                          double f_arg, const char *psz_arg )
749 {
750     rtsp_cmd_t cmd;
751     block_t *p_cmd;
752
753     memset( &cmd, 0, sizeof(cmd) );
754     cmd.i_type = i_type;
755     if( p_media )
756         cmd.i_media_id = p_media->id;
757     if( psz_session )
758         cmd.psz_session = strdup(psz_session);
759     cmd.f_arg = f_arg;
760     if( psz_arg )
761         cmd.psz_arg = strdup(psz_arg);
762
763     p_cmd = block_New( p_vod, sizeof(rtsp_cmd_t) );
764     memcpy( p_cmd->p_buffer, &cmd, sizeof(cmd) );
765
766     block_FifoPut( p_vod->p_sys->p_fifo_cmd, p_cmd );
767 }
768
769 static void CommandThread( vlc_object_t *p_this )
770 {
771     vod_t *p_vod = (vod_t*)p_this;
772     vod_sys_t *p_sys = p_vod->p_sys;
773
774     while( !p_vod->b_die )
775     {
776         block_t *p_block_cmd = block_FifoGet( p_sys->p_fifo_cmd );
777         rtsp_cmd_t cmd;
778         vod_media_t *p_media = NULL;
779         int i;
780
781         if( !p_block_cmd )
782             break;
783
784         memcpy( &cmd, p_block_cmd->p_buffer, sizeof(cmd) );
785         block_Release( p_block_cmd );
786
787         if( cmd.i_type == RTSP_CMD_TYPE_NONE )
788             break;
789
790         /* */
791         vlc_mutex_lock( &p_sys->lock_media );
792         for( i = 0; i < p_sys->i_media; i++ )
793         {
794             if( p_sys->media[i]->id == cmd.i_media_id )
795                 break;
796         }
797         if( i >= p_sys->i_media )
798             goto next;
799         p_media = p_sys->media[i];
800
801         switch( cmd.i_type )
802         {
803         case RTSP_CMD_TYPE_PLAY:
804             vod_MediaControl( p_vod, p_media, cmd.psz_session,
805                               VOD_MEDIA_PLAY, cmd.psz_arg );
806             break;
807         case RTSP_CMD_TYPE_PAUSE:
808             vod_MediaControl( p_vod, p_media, cmd.psz_session,
809                               VOD_MEDIA_PAUSE );
810             break;
811
812         case RTSP_CMD_TYPE_STOP:
813             vod_MediaControl( p_vod, p_media, cmd.psz_session, VOD_MEDIA_STOP );
814             break;
815
816         case RTSP_CMD_TYPE_SEEK:
817             vod_MediaControl( p_vod, p_media, cmd.psz_session,
818                               VOD_MEDIA_SEEK, cmd.f_arg );
819             break;
820
821         case RTSP_CMD_TYPE_REWIND:
822             vod_MediaControl( p_vod, p_media, cmd.psz_session,
823                               VOD_MEDIA_REWIND, cmd.f_arg );
824             break;
825
826         case RTSP_CMD_TYPE_FORWARD:
827             vod_MediaControl( p_vod, p_media, cmd.psz_session,
828                               VOD_MEDIA_FORWARD, cmd.f_arg );
829             break;
830
831         default:
832             break;
833         }
834
835     next:
836         vlc_mutex_unlock( &p_sys->lock_media );
837         if( cmd.psz_session )
838             free( cmd.psz_session );
839         if( cmd.psz_arg )
840             free( cmd.psz_arg );
841     }
842 }
843
844 /****************************************************************************
845  * RTSP server implementation
846  ****************************************************************************/
847 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
848 {
849     rtsp_client_t *p_rtsp = malloc( sizeof(rtsp_client_t) );
850
851     if( !p_rtsp ) return NULL;
852     memset( p_rtsp, 0, sizeof(rtsp_client_t) );
853     p_rtsp->es = 0;
854
855     p_rtsp->psz_session = psz_session;
856     TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
857
858     p_media->p_vod->p_sys->i_connections++;
859     msg_Dbg( p_media->p_vod, "new session: %s, connections: %d",
860              psz_session, p_media->p_vod->p_sys->i_throttle_users );
861
862     return p_rtsp;
863 }
864
865 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, const char *psz_session )
866 {
867     int i;
868
869     for( i = 0; psz_session && i < p_media->i_rtsp; i++ )
870     {
871         if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
872             return p_media->rtsp[i];
873     }
874
875     return NULL;
876 }
877
878 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
879 {
880     p_media->p_vod->p_sys->i_connections--;
881     msg_Dbg( p_media->p_vod, "closing session: %s, connections: %d",
882              p_rtsp->psz_session, p_media->p_vod->p_sys->i_throttle_users );
883
884     while( p_rtsp->i_es-- )
885     {
886         if( p_rtsp->es[p_rtsp->i_es]->psz_ip )
887             free( p_rtsp->es[p_rtsp->i_es]->psz_ip );
888         free( p_rtsp->es[p_rtsp->i_es] );
889         if( !p_rtsp->i_es ) free( p_rtsp->es );
890     }
891
892     TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, p_rtsp );
893
894     free( p_rtsp->psz_session );
895     free( p_rtsp );
896 }
897
898 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
899                          httpd_message_t *answer, const httpd_message_t *query )
900 {
901     vod_media_t *p_media = (vod_media_t*)p_args;
902     vod_t *p_vod = p_media->p_vod;
903     const char *psz_transport = NULL;
904     const char *psz_playnow = NULL; /* support option: x-playNow */
905     const char *psz_session = NULL;
906     const char *psz_cseq = NULL;
907     rtsp_client_t *p_rtsp;
908     int i_port = 0;
909     int i_cseq = 0;
910
911     if( answer == NULL || query == NULL ) return VLC_SUCCESS;
912
913     msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
914
915     answer->i_proto   = HTTPD_PROTO_RTSP;
916     answer->i_version = query->i_version;
917     answer->i_type    = HTTPD_MSG_ANSWER;
918     answer->i_body    = 0;
919     answer->p_body    = NULL;
920
921     switch( query->i_type )
922     {
923         case HTTPD_MSG_SETUP:
924         {
925             psz_playnow = httpd_MsgGet( query, "x-playNow" );
926             psz_transport = httpd_MsgGet( query, "Transport" );
927             if( psz_transport == NULL )
928             {
929                 answer->i_status = 400;
930                 break;
931             }
932             msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
933
934             if( strstr( psz_transport, "unicast" ) &&
935                 strstr( psz_transport, "client_port=" ) )
936             {
937                 rtsp_client_t *p_rtsp = NULL;
938                 char ip[NI_MAXNUMERICHOST];
939                 i_port = atoi( strstr( psz_transport, "client_port=" ) +
940                                 strlen("client_port=") );
941
942                 if( strstr( psz_transport, "MP2T/H2221/UDP" ) ||
943                     strstr( psz_transport, "RAW/RAW/UDP" ) )
944                 {
945                     if( p_media->psz_mux ) free( p_media->psz_mux );
946                     p_media->psz_mux = NULL;
947                     p_media->psz_mux = strdup( p_vod->p_sys->psz_raw_mux );
948                     p_media->b_raw = VLC_TRUE;
949                 }
950
951                 if( httpd_ClientIP( cl, ip ) == NULL )
952                 {
953                     answer->i_status = 500;
954                     answer->i_body = 0;
955                     answer->p_body = NULL;
956                     break;
957                 }
958
959                 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
960                          ip, i_port );
961
962                 psz_session = httpd_MsgGet( query, "Session" );
963                 if( !psz_session || !*psz_session )
964                 {
965                     char *psz_new;
966                     if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
967                         ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
968                     {
969                         answer->i_status = 503;
970                         answer->i_body = 0;
971                         answer->p_body = NULL;
972                         break;
973                     }
974                     asprintf( &psz_new, "%d", rand() );
975                     psz_session = psz_new;
976
977                     p_rtsp = RtspClientNew( p_media, psz_new );
978                     if( !p_rtsp )
979                     {
980                         answer->i_status = 454;
981                         answer->i_body = 0;
982                         answer->p_body = NULL;
983                         break;
984                     }
985                 }
986                 else
987                 {
988                     p_rtsp = RtspClientGet( p_media, psz_session );
989                     if( !p_rtsp )
990                     {
991                         answer->i_status = 454;
992                         answer->i_body = 0;
993                         answer->p_body = NULL;
994                         break;
995                     }
996                 }
997
998                 answer->i_status = 200;
999                 answer->i_body = 0;
1000                 answer->p_body = NULL;
1001
1002                 if( p_media->b_raw )
1003                 {
1004                     if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
1005                     {
1006                         httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
1007                                       i_port, i_port + 1 );
1008                     }
1009                     else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1010                     {
1011                         httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
1012                                       i_port, i_port + 1 );
1013                     }
1014                 }
1015                 else
1016                     httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
1017                                   i_port, i_port + 1 );
1018             }
1019             else /* TODO  strstr( psz_transport, "interleaved" ) ) */
1020             {
1021                 answer->i_status = 461;
1022                 answer->i_body = 0;
1023                 answer->p_body = NULL;
1024             }
1025
1026             /* Intentional fall-through on x-playNow option in RTSP request */
1027             if( !psz_playnow )
1028                 break;
1029         }
1030
1031         case HTTPD_MSG_PLAY:
1032         {
1033             char *psz_output, ip[NI_MAXNUMERICHOST];
1034             int i, i_port_audio = 0, i_port_video = 0;
1035
1036             /* for now only multicast so easy */
1037             if( !psz_playnow )
1038             {
1039                 answer->i_status = 200;
1040                 answer->i_body = 0;
1041                 answer->p_body = NULL;
1042             }
1043
1044             if( !psz_session )
1045                 psz_session = httpd_MsgGet( query, "Session" );
1046             msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1047
1048             p_rtsp = RtspClientGet( p_media, psz_session );
1049             if( !p_rtsp )
1050             {
1051                 answer->i_status = 500;
1052                 answer->i_body = 0;
1053                 answer->p_body = NULL;
1054                 break;
1055             }
1056
1057             if( p_rtsp->b_playing )
1058             {
1059                 const char *psz_position = httpd_MsgGet( query, "Range" );
1060                 const char *psz_scale = httpd_MsgGet( query, "Scale" );
1061                 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
1062                 if( psz_position && !psz_scale )
1063                 {
1064                     double f_pos;
1065                     char *end;
1066
1067                     msg_Dbg( p_vod, "seeking request: %s", psz_position );
1068                     psz_position += 4;
1069                     /* FIXME: npt= is not necessarily formatted as a float */
1070                     f_pos = us_strtod( psz_position, &end );
1071                     if( end > psz_position )
1072                     {
1073                         f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
1074                         CommandPush( p_vod, RTSP_CMD_TYPE_SEEK, p_media, psz_session, f_pos, NULL );
1075                     }
1076                     break;
1077                 }
1078                 if( psz_scale )
1079                 {
1080                     double f_scale = 0.0;
1081                     char *end;
1082
1083                     f_scale = us_strtod( psz_scale, &end );
1084                     if( end > psz_scale )
1085                     {
1086                         f_scale = (f_scale * 30.0);
1087                         if( psz_scale[0] == '-' ) /* rewind */
1088                         {
1089                             msg_Dbg( p_vod, "rewind request: %s", psz_scale );
1090                             CommandPush( p_vod, RTSP_CMD_TYPE_REWIND, p_media, psz_session, f_scale, NULL );
1091                         }
1092                         else if(psz_scale[0] != '1' ) /* fast-forward */
1093                         {
1094                             msg_Dbg( p_vod, "fastforward request: %s", psz_scale );
1095                             CommandPush( p_vod, RTSP_CMD_TYPE_FORWARD, p_media, psz_session, f_scale, NULL );
1096                         }
1097
1098                         if( p_rtsp->b_paused == VLC_TRUE )
1099                         {
1100                             p_rtsp->b_paused = VLC_FALSE;
1101                             CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media, psz_session, 0, NULL );
1102                         }
1103                     }
1104                     break;
1105                 }
1106             }
1107
1108             if( p_rtsp->b_playing && p_rtsp->b_paused )
1109             {
1110                 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media, psz_session, 0, NULL );
1111                 p_rtsp->b_paused = VLC_FALSE;
1112                 break;
1113             }
1114             else if( p_rtsp->b_playing ) break;
1115
1116             if( httpd_ClientIP( cl, ip ) == NULL ) break;
1117
1118             p_rtsp->b_playing = VLC_TRUE;
1119
1120             /* FIXME for != 1 video and 1 audio */
1121             for( i = 0; i < p_rtsp->i_es; i++ )
1122             {
1123                 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == AUDIO_ES )
1124                     i_port_audio = p_rtsp->es[i]->i_port;
1125                 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == VIDEO_ES )
1126                     i_port_video = p_rtsp->es[i]->i_port;
1127             }
1128
1129             if( p_media->psz_mux )
1130             {
1131                 if( p_media->b_raw )
1132                 {
1133                     asprintf( &psz_output, "std{access=udp,dst=%s:%i,mux=%s}",
1134                               ip, i_port, p_media->psz_mux );
1135                 }
1136                 else
1137                 {
1138                     asprintf( &psz_output, "rtp{dst=%s,port=%i,mux=%s}",
1139                               ip, i_port_video, p_media->psz_mux );
1140                 }
1141             }
1142             else
1143             {
1144                 asprintf( &psz_output, "rtp{dst=%s,port-video=%i,"
1145                           "port-audio=%i}", ip, i_port_video, i_port_audio );
1146             }
1147
1148             CommandPush( p_vod, RTSP_CMD_TYPE_PLAY, p_media, psz_session, 0, psz_output );
1149             free( psz_output );
1150             break;
1151         }
1152
1153         case HTTPD_MSG_DESCRIBE:
1154         {
1155             char *psz_sdp =
1156                 SDPGenerate( p_media, cl );
1157
1158             if( psz_sdp != NULL )
1159             {
1160                 answer->i_status = 200;
1161                 httpd_MsgAdd( answer, "Content-type",  "%s", "application/sdp" );
1162
1163                 answer->p_body = (uint8_t *)psz_sdp;
1164                 answer->i_body = strlen( psz_sdp );
1165             }
1166             else
1167             {
1168                 answer->i_status = 500;
1169                 answer->p_body = NULL;
1170                 answer->i_body = 0;
1171             }
1172             break;
1173         }
1174
1175         case HTTPD_MSG_PAUSE:
1176             psz_session = httpd_MsgGet( query, "Session" );
1177             msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1178
1179             p_rtsp = RtspClientGet( p_media, psz_session );
1180             if( !p_rtsp ) break;
1181
1182             CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media, psz_session, 0, NULL );
1183             p_rtsp->b_paused = VLC_TRUE;
1184
1185             answer->i_status = 200;
1186             answer->i_body = 0;
1187             answer->p_body = NULL;
1188             break;
1189
1190         case HTTPD_MSG_TEARDOWN:
1191             /* for now only multicast so easy again */
1192             answer->i_status = 200;
1193             answer->i_body = 0;
1194             answer->p_body = NULL;
1195
1196             psz_session = httpd_MsgGet( query, "Session" );
1197             msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1198
1199             p_rtsp = RtspClientGet( p_media, psz_session );
1200             if( !p_rtsp ) break;
1201
1202             CommandPush( p_vod, RTSP_CMD_TYPE_STOP, p_media, psz_session, 0, NULL );
1203             RtspClientDel( p_media, p_rtsp );
1204             break;
1205
1206         case HTTPD_MSG_GETPARAMETER:
1207             answer->i_status = 200;
1208             answer->i_body = 0;
1209             answer->p_body = NULL;
1210             break;
1211
1212         default:
1213             return VLC_EGENERIC;
1214     }
1215
1216     httpd_MsgAdd( answer, "Server", "VLC Server" );
1217     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1218     psz_cseq = httpd_MsgGet( query, "Cseq" );
1219     psz_cseq ? i_cseq = atoi( psz_cseq ) : 0;
1220     httpd_MsgAdd( answer, "CSeq", "%d", i_cseq );
1221     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1222
1223     if( psz_session )
1224     {
1225          if( p_media->p_vod->p_sys->i_session_timeout >= 0 )
1226              httpd_MsgAdd( answer, "Session", "%s;timeout=%i", psz_session,
1227                p_media->p_vod->p_sys->i_session_timeout );
1228          else
1229               httpd_MsgAdd( answer, "Session", "%s", psz_session );
1230     } 
1231
1232     return VLC_SUCCESS;
1233 }
1234
1235 static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
1236                            httpd_message_t *answer, const httpd_message_t *query )
1237 {
1238     media_es_t *p_es = (media_es_t*)p_args;
1239     vod_media_t *p_media = p_es->p_media;
1240     vod_t *p_vod = p_media->p_vod;
1241     rtsp_client_t *p_rtsp = NULL;
1242     const char *psz_transport = NULL;
1243     const char *psz_playnow = NULL; /* support option: x-playNow */
1244     const char *psz_session = NULL;
1245     const char *psz_position = NULL;
1246     const char *psz_cseq = NULL;
1247     int i_cseq = 0;
1248     int i;
1249
1250     if( answer == NULL || query == NULL ) return VLC_SUCCESS;
1251
1252     msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
1253
1254     answer->i_proto   = HTTPD_PROTO_RTSP;
1255     answer->i_version = query->i_version;
1256     answer->i_type    = HTTPD_MSG_ANSWER;
1257     answer->i_body    = 0;
1258     answer->p_body      = NULL;
1259
1260     switch( query->i_type )
1261     {
1262         case HTTPD_MSG_SETUP:
1263             psz_playnow = httpd_MsgGet( query, "x-playNow" );
1264             psz_transport = httpd_MsgGet( query, "Transport" );
1265
1266             msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
1267
1268             if( strstr( psz_transport, "unicast" ) &&
1269                 strstr( psz_transport, "client_port=" ) )
1270             {
1271                 rtsp_client_t *p_rtsp = NULL;
1272                 rtsp_client_es_t *p_rtsp_es = NULL;
1273                 char ip[NI_MAXNUMERICHOST];
1274                 int i_port = atoi( strstr( psz_transport, "client_port=" ) +
1275                                 strlen("client_port=") );
1276
1277                 if( httpd_ClientIP( cl, ip ) == NULL )
1278                 {
1279                     answer->i_status = 500;
1280                     answer->i_body = 0;
1281                     answer->p_body = NULL;
1282                     break;
1283                 }
1284
1285                 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
1286                         ip, i_port );
1287
1288                 psz_session = httpd_MsgGet( query, "Session" );
1289                 if( !psz_session || !*psz_session )
1290                 {
1291                     char *psz_new;
1292                     if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
1293                         ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
1294                     {
1295                         answer->i_status = 503;
1296                         answer->i_body = 0;
1297                         answer->p_body = NULL;
1298                         break;
1299                     }
1300                     asprintf( &psz_new, "%d", rand() );
1301                     psz_session = psz_new;
1302
1303                     p_rtsp = RtspClientNew( p_media, psz_new );
1304                 }
1305                 else
1306                 {
1307                     p_rtsp = RtspClientGet( p_media, psz_session );
1308                     if( !p_rtsp )
1309                     {
1310                         answer->i_status = 454;
1311                         answer->i_body = 0;
1312                         answer->p_body = NULL;
1313                         break;
1314                     }
1315                 }
1316
1317                 p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
1318                 if( !p_rtsp_es )
1319                 {
1320                     answer->i_status = 500;
1321                     answer->i_body = 0;
1322                     answer->p_body = NULL;
1323                     break;
1324                 }
1325                 p_rtsp_es->i_port = i_port;
1326                 p_rtsp_es->psz_ip = strdup( ip );
1327                 p_rtsp_es->p_media_es = p_es;
1328                 TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );
1329
1330                 answer->i_status = 200;
1331                 answer->i_body = 0;
1332                 answer->p_body = NULL;
1333
1334                 if( p_media->b_raw )
1335                 {
1336                     if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
1337                     {
1338                         httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
1339                                       p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1340                     }
1341                     else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1342                     {
1343                         httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
1344                                       p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1345                     }
1346                 }
1347                 else
1348                 {
1349                     httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
1350                                   p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1351                 }
1352             }
1353             else /* TODO  strstr( psz_transport, "interleaved" ) ) */
1354             {
1355                 answer->i_status = 461;
1356                 answer->i_body = 0;
1357                 answer->p_body = NULL;
1358             }
1359
1360             /* Intentional fall-through on x-playNow option in RTSP request */
1361             if( !psz_playnow )
1362                 break;
1363
1364         case HTTPD_MSG_PLAY:
1365             /* This is kind of a kludge. Should we only support Aggregate
1366              * Operations ? */
1367             psz_session = httpd_MsgGet( query, "Session" );
1368             msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1369
1370             p_rtsp = RtspClientGet( p_media, psz_session );
1371
1372             psz_position = httpd_MsgGet( query, "Range" );
1373             if( psz_position ) psz_position = strstr( psz_position, "npt=" );
1374             if( psz_position )
1375             {
1376                 double f_pos;
1377                 char *end;
1378
1379                 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1380
1381                 psz_position += 4;
1382                 /* FIXME: npt= is not necessarily formatted as a float */
1383                 f_pos = us_strtod( psz_position, &end );
1384                 if( end > psz_position )
1385                 {
1386                     f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
1387                     CommandPush( p_vod, RTSP_CMD_TYPE_SEEK, p_media, psz_session, f_pos, NULL );
1388                 }
1389             }
1390
1391             if( !psz_playnow )
1392             {
1393                 answer->i_status = 200;
1394                 answer->i_body = 0;
1395                 answer->p_body = NULL;
1396             }
1397             break;
1398
1399         case HTTPD_MSG_TEARDOWN:
1400             answer->i_status = 200;
1401             answer->i_body = 0;
1402             answer->p_body = NULL;
1403
1404             psz_session = httpd_MsgGet( query, "Session" );
1405             msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1406
1407             p_rtsp = RtspClientGet( p_media, psz_session );
1408             if( !p_rtsp ) break;
1409
1410             for( i = 0; i < p_rtsp->i_es; i++ )
1411             {
1412                 if( p_rtsp->es[i]->p_media_es == p_es )
1413                 {
1414                     if( p_rtsp->es[i]->psz_ip ) free( p_rtsp->es[i]->psz_ip );
1415                     TAB_REMOVE( p_rtsp->i_es, p_rtsp->es, p_rtsp->es[i] );
1416                     break;
1417                 }
1418             }
1419
1420             if( !p_rtsp->i_es )
1421             {
1422                 CommandPush( p_vod, RTSP_CMD_TYPE_STOP, p_media, psz_session, 0, NULL );
1423                 RtspClientDel( p_media, p_rtsp );
1424             }
1425             break;
1426
1427         case HTTPD_MSG_PAUSE:
1428             /* This is kind of a kludge. Should we only support Aggregate
1429              * Operations ? */
1430             psz_session = httpd_MsgGet( query, "Session" );
1431             msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1432
1433             p_rtsp = RtspClientGet( p_media, psz_session );
1434             if( !p_rtsp ) break;
1435
1436             CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media, psz_session, 0, NULL );
1437             p_rtsp->b_paused = VLC_TRUE;
1438
1439             answer->i_status = 200;
1440             answer->i_body = 0;
1441             answer->p_body = NULL;
1442             break;
1443
1444         default:
1445             return VLC_EGENERIC;
1446             break;
1447     }
1448
1449     httpd_MsgAdd( answer, "Server", "VLC Server" );
1450     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1451     psz_cseq = httpd_MsgGet( query, "Cseq" );
1452     if (psz_cseq)
1453         i_cseq = atoi( psz_cseq );
1454     else
1455         i_cseq = 0;
1456     httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1457     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1458
1459     if( psz_session )
1460         httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1461
1462     return VLC_SUCCESS;
1463 }
1464
1465 /*****************************************************************************
1466  * SDPGenerate: TODO
1467  * FIXME: need to be moved to a common place ?
1468  *****************************************************************************/
1469 static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
1470 {
1471     int i, i_size;
1472     char *p, *psz_sdp, ip[NI_MAXNUMERICHOST], ipv;
1473     const char *psz_control;
1474
1475     if( httpd_ServerIP( cl, ip ) == NULL )
1476         return NULL;
1477
1478     p = strchr( ip, '%' );
1479     if( p != NULL )
1480         *p = '\0'; /* remove scope if present */
1481
1482     ipv = ( strchr( ip, ':' ) != NULL ) ? '6' : '4';
1483
1484     /* Calculate size */
1485     i_size = sizeof( "v=0\r\n" ) +
1486         sizeof( "o=- * * IN IP4 \r\n" ) + 10 + NI_MAXNUMERICHOST +
1487         sizeof( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
1488         sizeof( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
1489         sizeof( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
1490         sizeof( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
1491         sizeof( "c=IN IP4 0.0.0.0\r\n" ) + 20 + 10 +
1492         sizeof( "t=0 0\r\n" ) + /* FIXME */
1493         sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
1494         sizeof( "a=range:npt=0-1000000000.000\r\n" );
1495
1496     psz_control = (ipv == '6') ? p_media->psz_rtsp_control_v6
1497                                : p_media->psz_rtsp_control_v4;
1498     for( i = 0; i < p_media->i_es; i++ )
1499     {
1500         media_es_t *p_es = p_media->es[i];
1501
1502         i_size += sizeof( "m=**d*o * RTP/AVP *\r\n" ) + 19;
1503         if( p_es->psz_rtpmap )
1504         {
1505             i_size += sizeof( "a=rtpmap:* *\r\n" ) +
1506                 strlen( p_es->psz_rtpmap ) + 9;
1507         }
1508         if( p_es->psz_fmtp )
1509         {
1510             i_size += sizeof( "a=fmtp:* *\r\n" ) +
1511                 strlen( p_es->psz_fmtp ) + 9;
1512         }
1513     }
1514     i_size += (strlen( psz_control ) + strlen( ip ) + 9) * p_media->i_es;
1515
1516     p = psz_sdp = malloc( i_size );
1517     p += sprintf( p, "v=0\r\n" );
1518     p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
1519                   p_media->i_sdp_id, p_media->i_sdp_version, ipv, ip );
1520     if( *p_media->psz_session_name )
1521         p += sprintf( p, "s=%s\r\n", p_media->psz_session_name );
1522     if( *p_media->psz_session_description )
1523         p += sprintf( p, "i=%s\r\n", p_media->psz_session_description );
1524     if( *p_media->psz_session_url )
1525         p += sprintf( p, "u=%s\r\n", p_media->psz_session_url );
1526     if( *p_media->psz_session_email )
1527         p += sprintf( p, "e=%s\r\n", p_media->psz_session_email );
1528
1529     p += sprintf( p, "c=IN IP%c %s\r\n", ipv, ipv == '6' ? "::" : "0.0.0.0" );
1530     p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
1531     p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
1532
1533     if( p_media->i_length > 0 )
1534     {
1535         lldiv_t d = lldiv( p_media->i_length / 1000, 1000 );
1536         p += sprintf( p, "a=range:npt=0-%lld.%03u\r\n", d.quot,
1537                       (unsigned)d.rem );
1538     }
1539
1540     for( i = 0; i < p_media->i_es; i++ )
1541     {
1542         media_es_t *p_es = p_media->es[i];
1543
1544         if( p_es->fmt.i_cat == AUDIO_ES )
1545         {
1546             p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
1547                           p_es->i_port, p_es->i_payload_type );
1548         }
1549         else if( p_es->fmt.i_cat == VIDEO_ES )
1550         {
1551             p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
1552                           p_es->i_port, p_es->i_payload_type );
1553         }
1554         else
1555         {
1556             continue;
1557         }
1558
1559         if( p_es->psz_rtpmap )
1560         {
1561             p += sprintf( p, "a=rtpmap:%d %s\r\n", p_es->i_payload_type,
1562                           p_es->psz_rtpmap );
1563         }
1564         if( p_es->psz_fmtp )
1565         {
1566             p += sprintf( p, "a=fmtp:%d %s\r\n", p_es->i_payload_type,
1567                           p_es->psz_fmtp );
1568         }
1569
1570         p += sprintf( p, psz_control, ip, i );
1571     }
1572
1573     return psz_sdp;
1574 }