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