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