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