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