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