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