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