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