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