]> git.sesse.net Git - vlc/blob - modules/misc/rtsp.c
Plugins: push cancellation down
[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 = 0;
264     char *psz_url = 0;
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, false ) )
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     vod_sys_t *p_sys = p_vod->p_sys;
372     vod_media_t *p_media = malloc( sizeof(vod_media_t) );
373     int i;
374
375     if( !p_media )
376         return NULL;
377
378     memset( p_media, 0, sizeof(vod_media_t) );
379     p_media->id = p_sys->i_media_id++;
380     TAB_INIT( p_media->i_es, p_media->es );
381     p_media->psz_mux = 0;
382     TAB_INIT( p_media->i_rtsp, p_media->rtsp );
383     p_media->b_raw = false;
384
385     if( asprintf( &p_media->psz_rtsp_path, "%s%s",
386                   p_sys->psz_path, psz_name ) <0 )
387         return NULL;
388     p_media->p_rtsp_url =
389         httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, NULL,
390                             NULL, NULL );
391
392     if( !p_media->p_rtsp_url )
393     {
394         msg_Err( p_vod, "cannot create RTSP url (%s)", p_media->psz_rtsp_path);
395         free( p_media->psz_rtsp_path );
396         free( p_media );
397         return NULL;
398     }
399
400     msg_Dbg( p_vod, "created RTSP url: %s", p_media->psz_rtsp_path );
401
402     if( asprintf( &p_media->psz_rtsp_control_v4,
403                "a=control:rtsp://%%s:%d%s/trackID=%%d\r\n",
404                p_sys->i_port, p_media->psz_rtsp_path ) < 0 )
405     {
406         httpd_UrlDelete( p_media->p_rtsp_url );
407         free( p_media->psz_rtsp_path );
408         free( p_media );
409         return NULL;
410     }
411     if( asprintf( &p_media->psz_rtsp_control_v6,
412                "a=control:rtsp://[%%s]:%d%s/trackID=%%d\r\n",
413               p_sys->i_port, p_media->psz_rtsp_path ) < 0 )
414     {
415         httpd_UrlDelete( p_media->p_rtsp_url );
416         free( p_media->psz_rtsp_path );
417         free( p_media );
418         return NULL;
419     }
420
421     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_SETUP,
422                     RtspCallback, (void*)p_media );
423     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_DESCRIBE,
424                     RtspCallback, (void*)p_media );
425     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PLAY,
426                     RtspCallback, (void*)p_media );
427     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PAUSE,
428                     RtspCallback, (void*)p_media );
429     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_GETPARAMETER,
430                     RtspCallback, (void*)p_media );
431     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_TEARDOWN,
432                     RtspCallback, (void*)p_media );
433
434     p_media->p_vod = p_vod;
435
436     vlc_mutex_lock( &p_sys->lock_media );
437     TAB_APPEND( p_sys->i_media, p_sys->media, p_media );
438     vlc_mutex_unlock( &p_sys->lock_media );
439
440     vlc_mutex_init( &p_media->lock );
441     p_media->psz_session_name = strdup("");
442     p_media->psz_session_description = strdup("");
443     p_media->psz_session_url = strdup("");
444     p_media->psz_session_email = strdup("");
445
446     p_media->i_port_audio = 1234;
447     p_media->i_port_video = 1236;
448     p_media->i_port       = 1238;
449     p_media->i_payload_type = 96;
450
451     p_media->i_sdp_id = mdate();
452     p_media->i_sdp_version = 1;
453     p_media->i_length = input_item_GetDuration( p_item );
454
455     vlc_mutex_lock( &p_item->lock );
456     msg_Dbg( p_vod, "media has %i declared ES", p_item->i_es );
457     for( i = 0; i < p_item->i_es; i++ )
458     {
459         MediaAddES( p_vod, p_media, p_item->es[i] );
460     }
461     vlc_mutex_unlock( &p_item->lock );
462
463     return p_media;
464 }
465
466 static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
467 {
468     vod_sys_t *p_sys = p_vod->p_sys;
469
470     msg_Dbg( p_vod, "deleting media: %s", p_media->psz_rtsp_path );
471
472     vlc_mutex_lock( &p_sys->lock_media );
473     TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
474     vlc_mutex_unlock( &p_sys->lock_media );
475
476     while( p_media->i_rtsp > 0 )
477         RtspClientDel( p_media, p_media->rtsp[0] );
478     TAB_CLEAN( p_media->i_rtsp, p_media->rtsp );
479
480     httpd_UrlDelete( p_media->p_rtsp_url );
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     media_es_t *p_es = malloc( sizeof(media_es_t) );
502     char *psz_urlc;
503
504     if( !p_es ) return VLC_ENOMEM;
505     memset( p_es, 0, sizeof(media_es_t) );
506
507     free( p_media->psz_mux );
508     p_media->psz_mux = NULL;
509
510     /* TODO: update SDP, etc... */
511     if( asprintf( &psz_urlc, "%s/trackID=%d",
512               p_media->psz_rtsp_path, p_media->i_es ) < 0 )
513     {
514         free( p_es );
515         return VLC_ENOMEM;
516     }
517     msg_Dbg( p_vod, "  - ES %4.4s (%s)", (char *)&p_fmt->i_codec, psz_urlc );
518
519     switch( p_fmt->i_codec )
520     {
521         case VLC_FOURCC( 's', '1', '6', 'b' ):
522             if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
523             {
524                 p_es->i_payload_type = 11;
525             }
526             else if( p_fmt->audio.i_channels == 2 &&
527                      p_fmt->audio.i_rate == 44100 )
528             {
529                 p_es->i_payload_type = 10;
530             }
531             else
532             {
533                 p_es->i_payload_type = p_media->i_payload_type++;
534             }
535             if( asprintf( &p_es->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
536                           p_fmt->audio.i_channels ) == -1 )
537                 p_es->psz_rtpmap = NULL;
538             break;
539         case VLC_FOURCC( 'u', '8', ' ', ' ' ):
540             p_es->i_payload_type = p_media->i_payload_type++;
541             if( asprintf( &p_es->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
542                           p_fmt->audio.i_channels ) == -1 )
543                 p_es->psz_rtpmap = NULL;
544             break;
545         case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
546         case VLC_FOURCC( 'm', 'p', '3', ' ' ):
547             p_es->i_payload_type = 14;
548             p_es->psz_rtpmap = strdup( "MPA/90000" );
549             break;
550         case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
551             p_es->i_payload_type = 32;
552             p_es->psz_rtpmap = strdup( "MPV/90000" );
553             break;
554         case VLC_FOURCC( 'a', '5', '2', ' ' ):
555             p_es->i_payload_type = p_media->i_payload_type++;
556             if( asprintf( &p_es->psz_rtpmap, "ac3/%d", p_fmt->audio.i_rate ) == -1 )
557                 p_es->psz_rtpmap = NULL;
558             break;
559         case VLC_FOURCC( 'H', '2', '6', '3' ):
560             p_es->i_payload_type = p_media->i_payload_type++;
561             p_es->psz_rtpmap = strdup( "H263-1998/90000" );
562             break;
563         case VLC_FOURCC( 'h', '2', '6', '4' ):
564             p_es->i_payload_type = p_media->i_payload_type++;
565             p_es->psz_rtpmap = strdup( "H264/90000" );
566             p_es->psz_fmtp = NULL;
567             /* FIXME AAAAAAAAAAAARRRRRRRRGGGG copied from stream_out/rtp.c */
568             if( p_fmt->i_extra > 0 )
569             {
570                 uint8_t *p_buffer = p_fmt->p_extra;
571                 int     i_buffer = p_fmt->i_extra;
572                 char    *p_64_sps = NULL;
573                 char    *p_64_pps = NULL;
574                 char    hexa[6+1];
575
576                 while( i_buffer > 4 &&
577                        p_buffer[0] == 0 && p_buffer[1] == 0 &&
578                        p_buffer[2] == 0 && p_buffer[3] == 1 )
579                 {
580                     const int i_nal_type = p_buffer[4]&0x1f;
581                     int i_offset;
582                     int i_size      = 0;
583
584                     i_size = i_buffer;
585                     for( i_offset = 4; i_offset+3 < i_buffer ; i_offset++)
586                     {
587                         if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 0 && p_buffer[i_offset+3] == 1 )
588                         {
589                             /* we found another startcode */
590                             i_size = i_offset;
591                             break;
592                         }
593                     }
594                     if( i_nal_type == 7 )
595                     {
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                         p_64_pps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
602                     }
603                     i_buffer -= i_size;
604                     p_buffer += i_size;
605                 }
606                 /* */
607                 if( p_64_sps && p_64_pps )
608                 {
609                     if( asprintf( &p_es->psz_fmtp,
610                                   "packetization-mode=1;profile-level-id=%s;"
611                                   "sprop-parameter-sets=%s,%s;", hexa, p_64_sps,
612                                   p_64_pps ) < 0 )
613                     {
614                         free( p_64_sps );
615                         free( p_64_pps );
616                         free( psz_urlc );
617                         free( p_es );
618                         return VLC_ENOMEM;
619                     }
620                 }
621                 free( p_64_sps );
622                 free( p_64_pps );
623             }
624             if( !p_es->psz_fmtp )
625                 p_es->psz_fmtp = strdup( "packetization-mode=1" );
626             break;
627         case VLC_FOURCC( 'm', 'p', '4', 'v' ):
628             p_es->i_payload_type = p_media->i_payload_type++;
629             p_es->psz_rtpmap = strdup( "MP4V-ES/90000" );
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_FOURCC( 'm', 'p', '4', 'a' ):
641             p_es->i_payload_type = p_media->i_payload_type++;
642             if( asprintf( &p_es->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate ) == -1 )
643                 p_es->psz_rtpmap = NULL;
644             if( p_fmt->i_extra > 0 )
645             {
646                 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
647                 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
648                 if( asprintf( &p_es->psz_fmtp,
649                               "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
650                               "config=%s; SizeLength=13;IndexLength=3; "
651                               "IndexDeltaLength=3; Profile=1;", p_hexa ) == -1 )
652                     p_es->psz_fmtp = NULL;
653                 free( p_hexa );
654             }
655             break;
656         case VLC_FOURCC( 'm', 'p', '2', 't' ):
657             p_media->psz_mux = strdup("ts");
658             p_es->i_payload_type = 33;
659             p_es->psz_rtpmap = strdup( "MP2T/90000" );
660             break;
661         case VLC_FOURCC( 'm', 'p', '2', 'p' ):
662             p_media->psz_mux = strdup("ps");
663             p_es->i_payload_type = p_media->i_payload_type++;
664             p_es->psz_rtpmap = strdup( "MP2P/90000" );
665             break;
666         case VLC_FOURCC( 's', 'a', 'm', 'r' ):
667             p_es->i_payload_type = p_media->i_payload_type++;
668             p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
669                                     "AMR/8000/2" : "AMR/8000" );
670             p_es->psz_fmtp = strdup( "octet-align=1" );
671             break;
672         case VLC_FOURCC( 's', 'a', 'w', 'b' ):
673             p_es->i_payload_type = p_media->i_payload_type++;
674             p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
675                                     "AMR-WB/16000/2" : "AMR-WB/16000" );
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 #if 0
714     /* Choose the port */
715     if( p_fmt->i_cat == AUDIO_ES && p_media->i_port_audio > 0 )
716     {
717         p_es->i_port = p_media->i_port_audio;
718         p_media->i_port_audio = 0;
719     }
720     else if( p_fmt->i_cat == VIDEO_ES && p_media->i_port_video > 0 )
721     {
722         p_es->i_port = p_media->i_port_video;
723         p_media->i_port_video = 0;
724     }
725     while( !p_es->i_port )
726     {
727         if( p_media->i_port != p_media->i_port_audio &&
728             p_media->i_port != p_media->i_port_video )
729         {
730             p_es->i_port = p_media->i_port;
731             p_media->i_port += 2;
732             break;
733         }
734         p_media->i_port += 2;
735     }
736 #else
737
738     p_es->i_port = 0;
739 #endif
740
741     vlc_mutex_lock( &p_media->lock );
742     TAB_APPEND( p_media->i_es, p_media->es, p_es );
743     vlc_mutex_unlock( &p_media->lock );
744
745     p_media->i_sdp_version++;
746
747     return VLC_SUCCESS;
748 }
749
750 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
751 {
752     media_es_t *p_es = NULL;
753     int i;
754
755     /* Find the ES */
756     for( i = 0; i < p_media->i_es; i++ )
757     {
758         if( p_media->es[i]->fmt.i_cat == p_fmt->i_cat &&
759             p_media->es[i]->fmt.i_codec == p_fmt->i_codec &&
760             p_media->es[i]->fmt.i_id == p_fmt->i_id )
761         {
762             p_es = p_media->es[i];
763         }
764     }
765     if( !p_es ) return;
766
767     msg_Dbg( p_vod, "  - Removing ES %4.4s", (char *)&p_fmt->i_codec );
768
769     vlc_mutex_lock( &p_media->lock );
770     TAB_REMOVE( p_media->i_es, p_media->es, p_es );
771     vlc_mutex_unlock( &p_media->lock );
772
773     free( p_es->psz_rtpmap );
774     free( p_es->psz_fmtp );
775     p_media->i_sdp_version++;
776
777     if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
778     es_format_Clean( &p_es->fmt );
779     free( p_es );
780 }
781
782 /* */
783 typedef struct
784 {
785     int i_type;
786     int i_media_id;
787     //vod_media_t *p_media;
788     char *psz_session;
789     char *psz_arg;
790     double f_arg;
791 } rtsp_cmd_t;
792
793 static void CommandPush( vod_t *p_vod, rtsp_cmd_type_t i_type, vod_media_t *p_media, const char *psz_session,
794                          double f_arg, const char *psz_arg )
795 {
796     rtsp_cmd_t cmd;
797     block_t *p_cmd;
798
799     memset( &cmd, 0, sizeof(cmd) );
800     cmd.i_type = i_type;
801     if( p_media )
802         cmd.i_media_id = p_media->id;
803     if( psz_session )
804         cmd.psz_session = strdup(psz_session);
805     cmd.f_arg = f_arg;
806     if( psz_arg )
807         cmd.psz_arg = strdup(psz_arg);
808
809     p_cmd = block_New( p_vod, sizeof(rtsp_cmd_t) );
810     memcpy( p_cmd->p_buffer, &cmd, sizeof(cmd) );
811
812     block_FifoPut( p_vod->p_sys->p_fifo_cmd, p_cmd );
813 }
814
815 static void* CommandThread( vlc_object_t *p_this )
816 {
817     vod_t *p_vod = (vod_t*)p_this;
818     vod_sys_t *p_sys = p_vod->p_sys;
819     int canc = vlc_savecancel ();
820
821     while( vlc_object_alive (p_vod) )
822     {
823         block_t *p_block_cmd = block_FifoGet( p_sys->p_fifo_cmd );
824         rtsp_cmd_t cmd;
825         vod_media_t *p_media = NULL;
826         int i;
827
828         if( !p_block_cmd )
829             break;
830
831         memcpy( &cmd, p_block_cmd->p_buffer, sizeof(cmd) );
832         block_Release( p_block_cmd );
833
834         if( cmd.i_type == RTSP_CMD_TYPE_NONE )
835             break;
836
837         /* */
838         vlc_mutex_lock( &p_sys->lock_media );
839         for( i = 0; i < p_sys->i_media; i++ )
840         {
841             if( p_sys->media[i]->id == cmd.i_media_id )
842                 break;
843         }
844         if( i >= p_sys->i_media )
845             goto next;
846         p_media = p_sys->media[i];
847
848         switch( cmd.i_type )
849         {
850         case RTSP_CMD_TYPE_PLAY:
851             vod_MediaControl( p_vod, p_media, cmd.psz_session,
852                               VOD_MEDIA_PLAY, cmd.psz_arg );
853             break;
854         case RTSP_CMD_TYPE_PAUSE:
855             vod_MediaControl( p_vod, p_media, cmd.psz_session,
856                               VOD_MEDIA_PAUSE );
857             break;
858
859         case RTSP_CMD_TYPE_STOP:
860             vod_MediaControl( p_vod, p_media, cmd.psz_session, VOD_MEDIA_STOP );
861             break;
862
863         case RTSP_CMD_TYPE_SEEK:
864             vod_MediaControl( p_vod, p_media, cmd.psz_session,
865                               VOD_MEDIA_SEEK, cmd.f_arg );
866             break;
867
868         case RTSP_CMD_TYPE_REWIND:
869             vod_MediaControl( p_vod, p_media, cmd.psz_session,
870                               VOD_MEDIA_REWIND, cmd.f_arg );
871             break;
872
873         case RTSP_CMD_TYPE_FORWARD:
874             vod_MediaControl( p_vod, p_media, cmd.psz_session,
875                               VOD_MEDIA_FORWARD, cmd.f_arg );
876             break;
877
878         default:
879             break;
880         }
881
882     next:
883         vlc_mutex_unlock( &p_sys->lock_media );
884         free( cmd.psz_session );
885         free( cmd.psz_arg );
886     }
887
888     vlc_restorecancel (canc);
889     return NULL;
890 }
891
892 /****************************************************************************
893  * RTSP server implementation
894  ****************************************************************************/
895 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
896 {
897     rtsp_client_t *p_rtsp = malloc( sizeof(rtsp_client_t) );
898
899     if( !p_rtsp ) return NULL;
900     memset( p_rtsp, 0, sizeof(rtsp_client_t) );
901     p_rtsp->es = 0;
902
903     p_rtsp->psz_session = psz_session;
904     TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
905
906     p_media->p_vod->p_sys->i_connections++;
907     msg_Dbg( p_media->p_vod, "new session: %s, connections: %d",
908              psz_session, p_media->p_vod->p_sys->i_throttle_users );
909
910     return p_rtsp;
911 }
912
913 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, const char *psz_session )
914 {
915     int i;
916
917     for( i = 0; psz_session && i < p_media->i_rtsp; i++ )
918     {
919         if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
920             return p_media->rtsp[i];
921     }
922
923     return NULL;
924 }
925
926 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
927 {
928     p_media->p_vod->p_sys->i_connections--;
929     msg_Dbg( p_media->p_vod, "closing session: %s, connections: %d",
930              p_rtsp->psz_session, p_media->p_vod->p_sys->i_throttle_users );
931
932     while( p_rtsp->i_es-- )
933     {
934         free( p_rtsp->es[p_rtsp->i_es]->psz_ip );
935         free( p_rtsp->es[p_rtsp->i_es] );
936         if( !p_rtsp->i_es ) free( p_rtsp->es );
937     }
938
939     TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, p_rtsp );
940
941     free( p_rtsp->psz_session );
942     free( p_rtsp );
943 }
944
945
946 static float ParseNPT (const char *str)
947 {
948      locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
949      locale_t oldloc = uselocale (loc);
950      unsigned hour, min;
951      float sec;
952
953      if (sscanf (str, "%u:%u:%f", &hour, &min, &sec) == 3)
954          sec += ((hour * 60) + min) * 60;
955      else
956      if (sscanf (str, "%f", &sec) != 1)
957          sec = 0.;
958
959      if (loc != (locale_t)0)
960      {
961          uselocale (oldloc);
962          freelocale (loc);
963      }
964      return sec;
965 }
966
967
968 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
969                          httpd_message_t *answer, const httpd_message_t *query )
970 {
971     vod_media_t *p_media = (vod_media_t*)p_args;
972     vod_t *p_vod = p_media->p_vod;
973     const char *psz_transport = NULL;
974     const char *psz_playnow = NULL; /* support option: x-playNow */
975     const char *psz_session = NULL;
976     const char *psz_cseq = NULL;
977     rtsp_client_t *p_rtsp;
978     int i_port = 0;
979     int i_cseq = 0;
980
981     if( answer == NULL || query == NULL ) return VLC_SUCCESS;
982
983     msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
984
985     answer->i_proto   = HTTPD_PROTO_RTSP;
986     answer->i_version = query->i_version;
987     answer->i_type    = HTTPD_MSG_ANSWER;
988     answer->i_body    = 0;
989     answer->p_body    = NULL;
990
991     switch( query->i_type )
992     {
993         case HTTPD_MSG_SETUP:
994         {
995             psz_playnow = httpd_MsgGet( query, "x-playNow" );
996             psz_transport = httpd_MsgGet( query, "Transport" );
997             if( psz_transport == NULL )
998             {
999                 answer->i_status = 400;
1000                 break;
1001             }
1002             msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
1003
1004             if( strstr( psz_transport, "unicast" ) &&
1005                 strstr( psz_transport, "client_port=" ) )
1006             {
1007                 rtsp_client_t *p_rtsp = NULL;
1008                 char ip[NI_MAXNUMERICHOST];
1009                 i_port = atoi( strstr( psz_transport, "client_port=" ) +
1010                                 strlen("client_port=") );
1011
1012                 if( strstr( psz_transport, "MP2T/H2221/UDP" ) ||
1013                     strstr( psz_transport, "RAW/RAW/UDP" ) )
1014                 {
1015                     free( p_media->psz_mux );
1016                     p_media->psz_mux = NULL;
1017                     p_media->psz_mux = strdup( p_vod->p_sys->psz_raw_mux );
1018                     p_media->b_raw = true;
1019                 }
1020
1021                 if( httpd_ClientIP( cl, ip ) == NULL )
1022                 {
1023                     answer->i_status = 500;
1024                     answer->i_body = 0;
1025                     answer->p_body = NULL;
1026                     break;
1027                 }
1028
1029                 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
1030                          ip, i_port );
1031
1032                 psz_session = httpd_MsgGet( query, "Session" );
1033                 if( !psz_session || !*psz_session )
1034                 {
1035                     char *psz_new;
1036                     if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
1037                         ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
1038                     {
1039                         answer->i_status = 503;
1040                         answer->i_body = 0;
1041                         answer->p_body = NULL;
1042                         break;
1043                     }
1044                     if( asprintf( &psz_new, "%d", rand() ) < 0 )
1045                         return VLC_ENOMEM;
1046                     psz_session = psz_new;
1047
1048                     p_rtsp = RtspClientNew( p_media, psz_new );
1049                     if( !p_rtsp )
1050                     {
1051                         answer->i_status = 454;
1052                         answer->i_body = 0;
1053                         answer->p_body = NULL;
1054                         break;
1055                     }
1056                 }
1057                 else
1058                 {
1059                     p_rtsp = RtspClientGet( p_media, psz_session );
1060                     if( !p_rtsp )
1061                     {
1062                         answer->i_status = 454;
1063                         answer->i_body = 0;
1064                         answer->p_body = NULL;
1065                         break;
1066                     }
1067                 }
1068
1069                 answer->i_status = 200;
1070                 answer->i_body = 0;
1071                 answer->p_body = NULL;
1072
1073                 if( p_media->b_raw )
1074                 {
1075                     if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
1076                     {
1077                         httpd_MsgAdd( answer, "Transport",
1078                                       "MP2T/H2221/UDP;unicast;client_port=%d-%d",
1079                                       i_port, i_port + 1 );
1080                     }
1081                     else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1082                     {
1083                         httpd_MsgAdd( answer, "Transport",
1084                                       "RAW/RAW/UDP;unicast;client_port=%d-%d",
1085                                       i_port, i_port + 1 );
1086                     }
1087                 }
1088                 else
1089                     httpd_MsgAdd( answer, "Transport",
1090                                   "RTP/AVP/UDP;unicast;client_port=%d-%d",
1091                                   i_port, i_port + 1 );
1092             }
1093             else /* TODO  strstr( psz_transport, "interleaved" ) ) */
1094             {
1095                 answer->i_status = 461;
1096                 answer->i_body = 0;
1097                 answer->p_body = NULL;
1098             }
1099
1100             /* Intentional fall-through on x-playNow option in RTSP request */
1101             if( !psz_playnow )
1102                 break;
1103         }
1104
1105         case HTTPD_MSG_PLAY:
1106         {
1107             char *psz_output, ip[NI_MAXNUMERICHOST];
1108             int i, i_port_audio = 0, i_port_video = 0;
1109
1110             /* for now only multicast so easy */
1111             if( !psz_playnow )
1112             {
1113                 answer->i_status = 200;
1114                 answer->i_body = 0;
1115                 answer->p_body = NULL;
1116             }
1117
1118             if( !psz_session )
1119                 psz_session = httpd_MsgGet( query, "Session" );
1120             msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1121
1122             p_rtsp = RtspClientGet( p_media, psz_session );
1123             if( !p_rtsp )
1124             {
1125                 answer->i_status = 500;
1126                 answer->i_body = 0;
1127                 answer->p_body = NULL;
1128                 break;
1129             }
1130
1131             if( p_rtsp->b_playing )
1132             {
1133                 const char *psz_position = httpd_MsgGet( query, "Range" );
1134                 const char *psz_scale = httpd_MsgGet( query, "Scale" );
1135                 if( psz_position )
1136                     psz_position = strstr( psz_position, "npt=" );
1137                 if( psz_position && !psz_scale )
1138                 {
1139                     double f_pos = ParseNPT (psz_position + 4);
1140                     msg_Dbg( p_vod, "seeking request: %s", psz_position );
1141                     f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
1142                     CommandPush( p_vod, RTSP_CMD_TYPE_SEEK, p_media,
1143                                  psz_session, f_pos, NULL );
1144                     break;
1145                 }
1146                 if( psz_scale )
1147                 {
1148                     double f_scale = 0.0;
1149                     char *end;
1150
1151                     f_scale = us_strtod( psz_scale, &end );
1152                     if( end > psz_scale )
1153                     {
1154                         f_scale = (f_scale * 30.0);
1155                         if( psz_scale[0] == '-' ) /* rewind */
1156                         {
1157                             msg_Dbg( p_vod, "rewind request: %s", psz_scale );
1158                             CommandPush( p_vod, RTSP_CMD_TYPE_REWIND, p_media,
1159                                          psz_session, f_scale, NULL );
1160                         }
1161                         else if(psz_scale[0] != '1' ) /* fast-forward */
1162                         {
1163                             msg_Dbg( p_vod, "fastforward request: %s",
1164                                      psz_scale );
1165                             CommandPush( p_vod, RTSP_CMD_TYPE_FORWARD, p_media,
1166                                          psz_session, f_scale, NULL );
1167                         }
1168
1169                         if( p_rtsp->b_paused == true )
1170                         {
1171                             p_rtsp->b_paused = false;
1172                             CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media,
1173                                          psz_session, 0, NULL );
1174                         }
1175                     }
1176                     break;
1177                 }
1178             }
1179
1180             if( p_rtsp->b_playing && p_rtsp->b_paused )
1181             {
1182                 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media,
1183                              psz_session, 0, NULL );
1184                 p_rtsp->b_paused = false;
1185                 break;
1186             }
1187             else if( p_rtsp->b_playing ) break;
1188
1189             if( httpd_ClientIP( cl, ip ) == NULL ) break;
1190
1191             p_rtsp->b_playing = true;
1192
1193             /* FIXME for != 1 video and 1 audio */
1194             for( i = 0; i < p_rtsp->i_es; i++ )
1195             {
1196                 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == AUDIO_ES )
1197                     i_port_audio = p_rtsp->es[i]->i_port;
1198                 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == VIDEO_ES )
1199                     i_port_video = p_rtsp->es[i]->i_port;
1200             }
1201
1202             if( p_media->psz_mux )
1203             {
1204                 if( p_media->b_raw )
1205                 {
1206                     if( asprintf( &psz_output,
1207                               "std{access=udp,dst=%s:%i,mux=%s}",
1208                               ip, i_port, p_media->psz_mux ) < 0 )
1209                         return VLC_ENOMEM;
1210                 }
1211                 else
1212                 {
1213                     if( asprintf( &psz_output,
1214                               "rtp{dst=%s,port=%i,mux=%s}",
1215                               ip, i_port_video, p_media->psz_mux ) < 0 )
1216                         return VLC_ENOMEM;
1217                 }
1218             }
1219             else
1220             {
1221                 if( asprintf( &psz_output,
1222                               "rtp{dst=%s,port-video=%i,port-audio=%i}",
1223                               ip, i_port_video, i_port_audio ) < 0 )
1224                     return VLC_ENOMEM;
1225             }
1226
1227             CommandPush( p_vod, RTSP_CMD_TYPE_PLAY, p_media, psz_session,
1228                          0, psz_output );
1229             free( psz_output );
1230             break;
1231         }
1232
1233         case HTTPD_MSG_DESCRIBE:
1234         {
1235             char *psz_sdp =
1236                 SDPGenerate( p_media, cl );
1237
1238             if( psz_sdp != NULL )
1239             {
1240                 answer->i_status = 200;
1241                 httpd_MsgAdd( answer, "Content-type",  "%s",
1242                               "application/sdp" );
1243
1244                 answer->p_body = (uint8_t *)psz_sdp;
1245                 answer->i_body = strlen( psz_sdp );
1246             }
1247             else
1248             {
1249                 answer->i_status = 500;
1250                 answer->p_body = NULL;
1251                 answer->i_body = 0;
1252             }
1253             break;
1254         }
1255
1256         case HTTPD_MSG_PAUSE:
1257             psz_session = httpd_MsgGet( query, "Session" );
1258             msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1259
1260             p_rtsp = RtspClientGet( p_media, psz_session );
1261             if( !p_rtsp ) break;
1262
1263             CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media, psz_session,
1264                          0, NULL );
1265             p_rtsp->b_paused = true;
1266
1267             answer->i_status = 200;
1268             answer->i_body = 0;
1269             answer->p_body = NULL;
1270             break;
1271
1272         case HTTPD_MSG_TEARDOWN:
1273             /* for now only multicast so easy again */
1274             answer->i_status = 200;
1275             answer->i_body = 0;
1276             answer->p_body = NULL;
1277
1278             psz_session = httpd_MsgGet( query, "Session" );
1279             msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1280
1281             p_rtsp = RtspClientGet( p_media, psz_session );
1282             if( !p_rtsp ) break;
1283
1284             CommandPush( p_vod, RTSP_CMD_TYPE_STOP, p_media, psz_session,
1285                          0, NULL );
1286             RtspClientDel( p_media, p_rtsp );
1287             break;
1288
1289         case HTTPD_MSG_GETPARAMETER:
1290             answer->i_status = 200;
1291             answer->i_body = 0;
1292             answer->p_body = NULL;
1293             break;
1294
1295         default:
1296             return VLC_EGENERIC;
1297     }
1298
1299     httpd_MsgAdd( answer, "Server", "VLC Server" );
1300     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1301     psz_cseq = httpd_MsgGet( query, "Cseq" );
1302     psz_cseq ? i_cseq = atoi( psz_cseq ) : 0;
1303     httpd_MsgAdd( answer, "CSeq", "%d", i_cseq );
1304     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1305
1306     if( psz_session )
1307     {
1308          if( p_media->p_vod->p_sys->i_session_timeout >= 0 )
1309              httpd_MsgAdd( answer, "Session", "%s;timeout=%i", psz_session,
1310                p_media->p_vod->p_sys->i_session_timeout );
1311          else
1312               httpd_MsgAdd( answer, "Session", "%s", psz_session );
1313     }
1314
1315     return VLC_SUCCESS;
1316 }
1317
1318 static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
1319                            httpd_message_t *answer,
1320                            const httpd_message_t *query )
1321 {
1322     media_es_t *p_es = (media_es_t*)p_args;
1323     vod_media_t *p_media = p_es->p_media;
1324     vod_t *p_vod = p_media->p_vod;
1325     rtsp_client_t *p_rtsp = NULL;
1326     const char *psz_transport = NULL;
1327     const char *psz_playnow = NULL; /* support option: x-playNow */
1328     const char *psz_session = NULL;
1329     const char *psz_position = NULL;
1330     const char *psz_cseq = NULL;
1331     int i_cseq = 0;
1332     int i;
1333
1334     if( answer == NULL || query == NULL ) return VLC_SUCCESS;
1335
1336     msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
1337
1338     answer->i_proto   = HTTPD_PROTO_RTSP;
1339     answer->i_version = query->i_version;
1340     answer->i_type    = HTTPD_MSG_ANSWER;
1341     answer->i_body    = 0;
1342     answer->p_body      = NULL;
1343
1344     switch( query->i_type )
1345     {
1346         case HTTPD_MSG_SETUP:
1347             psz_playnow = httpd_MsgGet( query, "x-playNow" );
1348             psz_transport = httpd_MsgGet( query, "Transport" );
1349
1350             msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
1351
1352             if( strstr( psz_transport, "unicast" ) &&
1353                 strstr( psz_transport, "client_port=" ) )
1354             {
1355                 rtsp_client_t *p_rtsp = NULL;
1356                 rtsp_client_es_t *p_rtsp_es = NULL;
1357                 char ip[NI_MAXNUMERICHOST];
1358                 int i_port = atoi( strstr( psz_transport, "client_port=" ) +
1359                                    strlen("client_port=") );
1360
1361                 if( httpd_ClientIP( cl, ip ) == NULL )
1362                 {
1363                     answer->i_status = 500;
1364                     answer->i_body = 0;
1365                     answer->p_body = NULL;
1366                     break;
1367                 }
1368
1369                 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
1370                         ip, i_port );
1371
1372                 psz_session = httpd_MsgGet( query, "Session" );
1373                 if( !psz_session || !*psz_session )
1374                 {
1375                     char *psz_new;
1376                     if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
1377                         ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
1378                     {
1379                         answer->i_status = 503;
1380                         answer->i_body = 0;
1381                         answer->p_body = NULL;
1382                         break;
1383                     }
1384                     if( asprintf( &psz_new, "%d", rand() ) < 0 )
1385                         return VLC_ENOMEM;
1386                     psz_session = psz_new;
1387
1388                     p_rtsp = RtspClientNew( p_media, psz_new );
1389                 }
1390                 else
1391                 {
1392                     p_rtsp = RtspClientGet( p_media, psz_session );
1393                     if( !p_rtsp )
1394                     {
1395                         answer->i_status = 454;
1396                         answer->i_body = 0;
1397                         answer->p_body = NULL;
1398                         break;
1399                     }
1400                 }
1401
1402                 p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
1403                 if( !p_rtsp_es )
1404                 {
1405                     answer->i_status = 500;
1406                     answer->i_body = 0;
1407                     answer->p_body = NULL;
1408                     break;
1409                 }
1410                 p_rtsp_es->i_port = i_port;
1411                 p_rtsp_es->psz_ip = strdup( ip );
1412                 p_rtsp_es->p_media_es = p_es;
1413                 TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );
1414
1415                 answer->i_status = 200;
1416                 answer->i_body = 0;
1417                 answer->p_body = NULL;
1418
1419                 if( p_media->b_raw )
1420                 {
1421                     if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
1422                     {
1423                         httpd_MsgAdd( answer, "Transport",
1424                                      "MP2T/H2221/UDP;client_port=%d-%d",
1425                                      p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1426                     }
1427                     else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1428                     {
1429                         httpd_MsgAdd( answer, "Transport",
1430                                      "RAW/RAW/UDP;client_port=%d-%d",
1431                                      p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1432                     }
1433                 }
1434                 else
1435                 {
1436                     httpd_MsgAdd( answer, "Transport",
1437                                   "RTP/AVP/UDP;client_port=%d-%d",
1438                                   p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1439                 }
1440             }
1441             else /* TODO  strstr( psz_transport, "interleaved" ) ) */
1442             {
1443                 answer->i_status = 461;
1444                 answer->i_body = 0;
1445                 answer->p_body = NULL;
1446             }
1447
1448             /* Intentional fall-through on x-playNow option in RTSP request */
1449             if( !psz_playnow )
1450                 break;
1451
1452         case HTTPD_MSG_PLAY:
1453             /* This is kind of a kludge. Should we only support Aggregate
1454              * Operations ? */
1455             psz_session = httpd_MsgGet( query, "Session" );
1456             msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1457
1458             p_rtsp = RtspClientGet( p_media, psz_session );
1459
1460             psz_position = httpd_MsgGet( query, "Range" );
1461             if( psz_position ) psz_position = strstr( psz_position, "npt=" );
1462             if( psz_position )
1463             {
1464                 double f_pos = ParseNPT (psz_position + 4);
1465                 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1466                 f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
1467                 CommandPush( p_vod, RTSP_CMD_TYPE_SEEK, p_media,
1468                              psz_session, f_pos, NULL );
1469             }
1470
1471             if( !psz_playnow )
1472             {
1473                 answer->i_status = 200;
1474                 answer->i_body = 0;
1475                 answer->p_body = NULL;
1476             }
1477             break;
1478
1479         case HTTPD_MSG_TEARDOWN:
1480             answer->i_status = 200;
1481             answer->i_body = 0;
1482             answer->p_body = NULL;
1483
1484             psz_session = httpd_MsgGet( query, "Session" );
1485             msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1486
1487             p_rtsp = RtspClientGet( p_media, psz_session );
1488             if( !p_rtsp ) break;
1489
1490             for( i = 0; i < p_rtsp->i_es; i++ )
1491             {
1492                 if( p_rtsp->es[i]->p_media_es == p_es )
1493                 {
1494                     free( p_rtsp->es[i]->psz_ip );
1495                     TAB_REMOVE( p_rtsp->i_es, p_rtsp->es, p_rtsp->es[i] );
1496                     break;
1497                 }
1498             }
1499
1500             if( !p_rtsp->i_es )
1501             {
1502                 CommandPush( p_vod, RTSP_CMD_TYPE_STOP, p_media, psz_session,
1503                              0, NULL );
1504                 RtspClientDel( p_media, p_rtsp );
1505             }
1506             break;
1507
1508         case HTTPD_MSG_PAUSE:
1509             /* This is kind of a kludge. Should we only support Aggregate
1510              * Operations ? */
1511             psz_session = httpd_MsgGet( query, "Session" );
1512             msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1513
1514             p_rtsp = RtspClientGet( p_media, psz_session );
1515             if( !p_rtsp ) break;
1516
1517             CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media, psz_session,
1518                          0, NULL );
1519             p_rtsp->b_paused = true;
1520
1521             answer->i_status = 200;
1522             answer->i_body = 0;
1523             answer->p_body = NULL;
1524             break;
1525
1526         default:
1527             return VLC_EGENERIC;
1528             break;
1529     }
1530
1531     httpd_MsgAdd( answer, "Server", "VLC Server" );
1532     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1533     psz_cseq = httpd_MsgGet( query, "Cseq" );
1534     if (psz_cseq)
1535         i_cseq = atoi( psz_cseq );
1536     else
1537         i_cseq = 0;
1538     httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1539     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1540
1541     if( psz_session )
1542         httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1543
1544     return VLC_SUCCESS;
1545 }
1546
1547 /*****************************************************************************
1548  * SDPGenerate: TODO
1549  * FIXME: need to be moved to a common place ?
1550  *****************************************************************************/
1551 static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
1552 {
1553     int i, i_size;
1554     char *p, *psz_sdp, ip[NI_MAXNUMERICHOST], ipv;
1555     const char *psz_control;
1556
1557     if( httpd_ServerIP( cl, ip ) == NULL )
1558         return NULL;
1559
1560     p = strchr( ip, '%' );
1561     if( p != NULL )
1562         *p = '\0'; /* remove scope if present */
1563
1564     ipv = ( strchr( ip, ':' ) != NULL ) ? '6' : '4';
1565
1566     /* Calculate size */
1567     i_size = sizeof( "v=0\r\n" ) +
1568         sizeof( "o=- * * IN IP4 \r\n" ) + 10 + NI_MAXNUMERICHOST +
1569         sizeof( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
1570         sizeof( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
1571         sizeof( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
1572         sizeof( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
1573         sizeof( "c=IN IP4 0.0.0.0\r\n" ) + 20 + 10 +
1574         sizeof( "t=0 0\r\n" ) + /* FIXME */
1575         sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
1576         sizeof( "a=range:npt=0-1000000000.000\r\n" );
1577
1578     psz_control = (ipv == '6') ? p_media->psz_rtsp_control_v6
1579                                : p_media->psz_rtsp_control_v4;
1580     for( i = 0; i < p_media->i_es; i++ )
1581     {
1582         media_es_t *p_es = p_media->es[i];
1583
1584         i_size += sizeof( "m=**d*o * RTP/AVP *\r\n" ) + 19;
1585         if( p_es->psz_rtpmap )
1586         {
1587             i_size += sizeof( "a=rtpmap:* *\r\n" ) +
1588                 strlen( p_es->psz_rtpmap ) + 9;
1589         }
1590         if( p_es->psz_fmtp )
1591         {
1592             i_size += sizeof( "a=fmtp:* *\r\n" ) +
1593                 strlen( p_es->psz_fmtp ) + 9;
1594         }
1595     }
1596     i_size += (strlen( psz_control ) + strlen( ip ) + 9) * p_media->i_es;
1597
1598     p = psz_sdp = malloc( i_size );
1599     p += sprintf( p, "v=0\r\n" );
1600     p += sprintf( p, "o=- %"PRId64" %d IN IP%c %s\r\n",
1601                   p_media->i_sdp_id, p_media->i_sdp_version, ipv, ip );
1602     if( *p_media->psz_session_name )
1603         p += sprintf( p, "s=%s\r\n", p_media->psz_session_name );
1604     if( *p_media->psz_session_description )
1605         p += sprintf( p, "i=%s\r\n", p_media->psz_session_description );
1606     if( *p_media->psz_session_url )
1607         p += sprintf( p, "u=%s\r\n", p_media->psz_session_url );
1608     if( *p_media->psz_session_email )
1609         p += sprintf( p, "e=%s\r\n", p_media->psz_session_email );
1610
1611     p += sprintf( p, "c=IN IP%c %s\r\n", ipv, ipv == '6' ? "::" : "0.0.0.0" );
1612     p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
1613     p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
1614
1615     if( p_media->i_length > 0 )
1616     {
1617         lldiv_t d = lldiv( p_media->i_length / 1000, 1000 );
1618         p += sprintf( p, "a=range:npt=0-%lld.%03u\r\n", d.quot,
1619                       (unsigned)d.rem );
1620     }
1621
1622     for( i = 0; i < p_media->i_es; i++ )
1623     {
1624         media_es_t *p_es = p_media->es[i];
1625
1626         if( p_es->fmt.i_cat == AUDIO_ES )
1627         {
1628             p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
1629                           p_es->i_port, p_es->i_payload_type );
1630         }
1631         else if( p_es->fmt.i_cat == VIDEO_ES )
1632         {
1633             p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
1634                           p_es->i_port, p_es->i_payload_type );
1635         }
1636         else
1637         {
1638             continue;
1639         }
1640
1641         if( p_es->psz_rtpmap )
1642         {
1643             p += sprintf( p, "a=rtpmap:%d %s\r\n", p_es->i_payload_type,
1644                           p_es->psz_rtpmap );
1645         }
1646         if( p_es->psz_fmtp )
1647         {
1648             p += sprintf( p, "a=fmtp:%d %s\r\n", p_es->i_payload_type,
1649                           p_es->psz_fmtp );
1650         }
1651
1652         p += sprintf( p, psz_control, ip, i );
1653     }
1654
1655     return psz_sdp;
1656 }