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