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