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