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