]> git.sesse.net Git - vlc/blob - modules/misc/rtsp.c
Fixed H264 vod (import code from rtp.c, there is a lot of code duplicated here).
[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 = NULL;
476             /* FIXME AAAAAAAAAAAARRRRRRRRGGGG copied from stream_out/rtp.c */
477             if( p_fmt->i_extra > 0 )
478             {
479                 uint8_t *p_buffer = p_fmt->p_extra;
480                 int     i_buffer = p_fmt->i_extra;
481                 char    *p_64_sps = NULL;
482                 char    *p_64_pps = NULL;
483                 char    hexa[6+1];
484
485                 while( i_buffer > 4 &&
486                        p_buffer[0] == 0 && p_buffer[1] == 0 &&
487                        p_buffer[2] == 0 && p_buffer[3] == 1 )
488                 {
489                     const int i_nal_type = p_buffer[4]&0x1f;
490                     int i_offset;
491                     int i_size      = 0;
492
493                     i_size = i_buffer;
494                     for( i_offset = 4; i_offset+3 < i_buffer ; i_offset++)
495                     {
496                         if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 0 && p_buffer[i_offset+3] == 1 )
497                         {
498                             /* we found another startcode */
499                             i_size = i_offset;
500                             break;
501                         }
502                     }
503                     if( i_nal_type == 7 )
504                     {
505                         p_64_sps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
506                         sprintf_hexa( hexa, &p_buffer[5], 3 );
507                     }
508                     else if( i_nal_type == 8 )
509                     {
510                         p_64_pps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
511                     }
512                     i_buffer -= i_size;
513                     p_buffer += i_size;
514                 }
515                 /* */
516                 if( p_64_sps && p_64_pps )
517                     asprintf( &p_es->psz_fmtp, "packetization-mode=1;profile-level-id=%s;sprop-parameter-sets=%s,%s;", hexa, p_64_sps, p_64_pps );
518                 if( p_64_sps )
519                     free( p_64_sps );
520                 if( p_64_pps )
521                     free( p_64_pps );
522             }
523             if( !p_es->psz_fmtp )
524                 p_es->psz_fmtp = strdup( "packetization-mode=1" );
525             break;
526         case VLC_FOURCC( 'm', 'p', '4', 'v' ):
527             p_es->i_payload_type = p_media->i_payload_type++;
528             p_es->psz_rtpmap = strdup( "MP4V-ES/90000" );
529             if( p_fmt->i_extra > 0 )
530             {
531                 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
532                 p_es->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
533                 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
534                 sprintf( p_es->psz_fmtp,
535                         "profile-level-id=3; config=%s;", p_hexa );
536                 free( p_hexa );
537             }
538             break;
539         case VLC_FOURCC( 'm', 'p', '4', 'a' ):
540             p_es->i_payload_type = p_media->i_payload_type++;
541             p_es->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
542             sprintf( p_es->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
543             if( p_fmt->i_extra > 0 )
544             {
545                 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
546                 p_es->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
547                 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
548                 sprintf( p_es->psz_fmtp,
549                         "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
550                         "config=%s; SizeLength=13;IndexLength=3; "
551                         "IndexDeltaLength=3; Profile=1;", p_hexa );
552                 free( p_hexa );
553             }
554             break;
555         case VLC_FOURCC( 'm', 'p', '2', 't' ):
556             p_media->psz_mux = strdup("ts");
557             p_es->i_payload_type = 33;
558             p_es->psz_rtpmap = strdup( "MP2T/90000" );
559             break;
560         case VLC_FOURCC( 'm', 'p', '2', 'p' ):
561             p_media->psz_mux = strdup("ps");
562             p_es->i_payload_type = p_media->i_payload_type++;
563             p_es->psz_rtpmap = strdup( "MP2P/90000" );
564             break;
565         case VLC_FOURCC( 's', 'a', 'm', 'r' ):
566             p_es->i_payload_type = p_media->i_payload_type++;
567             p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
568                                     "AMR/8000/2" : "AMR/8000" );
569             p_es->psz_fmtp = strdup( "octet-align=1" );
570             break;
571         case VLC_FOURCC( 's', 'a', 'w', 'b' ):
572             p_es->i_payload_type = p_media->i_payload_type++;
573             p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
574                                     "AMR-WB/16000/2" : "AMR-WB/16000" );
575             p_es->psz_fmtp = strdup( "octet-align=1" );
576             break;
577
578         default:
579             msg_Err( p_vod, "cannot add this stream (unsupported "
580                     "codec: %4.4s)", (char*)&p_fmt->i_codec );
581             free( p_es );
582             return VLC_EGENERIC;
583     }
584
585     p_es->p_rtsp_url =
586         httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, NULL, NULL,
587                             NULL );
588
589     if( !p_es->p_rtsp_url )
590     {
591         msg_Err( p_vod, "cannot create RTSP url (%s)", psz_urlc );
592         free( psz_urlc );
593         free( p_es );
594         return VLC_EGENERIC;
595     }
596     free( psz_urlc );
597
598     httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
599                     RtspCallbackES, (void*)p_es );
600     httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_TEARDOWN,
601                     RtspCallbackES, (void*)p_es );
602     httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PLAY,
603                     RtspCallbackES, (void*)p_es );
604     httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PAUSE,
605                     RtspCallbackES, (void*)p_es );
606
607     es_format_Copy( &p_es->fmt, p_fmt );
608     p_es->p_vod = p_vod;
609     p_es->p_media = p_media;
610
611 #if 0
612     /* Choose the port */
613     if( p_fmt->i_cat == AUDIO_ES && p_media->i_port_audio > 0 )
614     {
615         p_es->i_port = p_media->i_port_audio;
616         p_media->i_port_audio = 0;
617     }
618     else if( p_fmt->i_cat == VIDEO_ES && p_media->i_port_video > 0 )
619     {
620         p_es->i_port = p_media->i_port_video;
621         p_media->i_port_video = 0;
622     }
623     while( !p_es->i_port )
624     {
625         if( p_media->i_port != p_media->i_port_audio &&
626             p_media->i_port != p_media->i_port_video )
627         {
628             p_es->i_port = p_media->i_port;
629             p_media->i_port += 2;
630             break;
631         }
632         p_media->i_port += 2;
633     }
634 #else
635
636     p_es->i_port = 0;
637 #endif
638
639     vlc_mutex_lock( &p_media->lock );
640     TAB_APPEND( p_media->i_es, p_media->es, p_es );
641     vlc_mutex_unlock( &p_media->lock );
642
643     p_media->i_sdp_version++;
644
645     return VLC_SUCCESS;
646 }
647
648 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
649 {
650     media_es_t *p_es = NULL;
651     int i;
652
653     /* Find the ES */
654     for( i = 0; i < p_media->i_es; i++ )
655     {
656         if( p_media->es[i]->fmt.i_cat == p_fmt->i_cat &&
657             p_media->es[i]->fmt.i_codec == p_fmt->i_codec &&
658             p_media->es[i]->fmt.i_id == p_fmt->i_id )
659         {
660             p_es = p_media->es[i];
661         }
662     }
663     if( !p_es ) return;
664
665     msg_Dbg( p_vod, "  - Removing ES %4.4s", (char *)&p_fmt->i_codec );
666
667     vlc_mutex_lock( &p_media->lock );
668     TAB_REMOVE( p_media->i_es, p_media->es, p_es );
669     vlc_mutex_unlock( &p_media->lock );
670
671     if( p_es->psz_rtpmap ) free( p_es->psz_rtpmap );
672     if( p_es->psz_fmtp ) free( p_es->psz_fmtp );
673     p_media->i_sdp_version++;
674
675     if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
676     es_format_Clean( &p_es->fmt );
677 }
678
679 /****************************************************************************
680  * RTSP server implementation
681  ****************************************************************************/
682 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
683 {
684     rtsp_client_t *p_rtsp = malloc( sizeof(rtsp_client_t) );
685
686     if( !p_rtsp ) return NULL;
687     memset( p_rtsp, 0, sizeof(rtsp_client_t) );
688     p_rtsp->es = 0;
689
690     p_rtsp->psz_session = psz_session;
691     TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
692
693     p_media->p_vod->p_sys->i_connections++;
694     msg_Dbg( p_media->p_vod, "new session: %s, connections: %d",
695              psz_session, p_media->p_vod->p_sys->i_throttle_users );
696
697     return p_rtsp;
698 }
699
700 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, char *psz_session )
701 {
702     int i;
703
704     for( i = 0; psz_session && i < p_media->i_rtsp; i++ )
705     {
706         if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
707         {
708             return p_media->rtsp[i];
709         }
710     }
711
712     return NULL;
713 }
714
715 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
716 {
717     p_media->p_vod->p_sys->i_connections--;
718     msg_Dbg( p_media->p_vod, "closing session: %s, connections: %d",
719              p_rtsp->psz_session, p_media->p_vod->p_sys->i_throttle_users );
720
721     while( p_rtsp->i_es-- )
722     {
723         if( p_rtsp->es[p_rtsp->i_es]->psz_ip )
724             free( p_rtsp->es[p_rtsp->i_es]->psz_ip );
725         free( p_rtsp->es[p_rtsp->i_es] );
726         if( !p_rtsp->i_es ) free( p_rtsp->es );
727     }
728
729     TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, p_rtsp );
730
731     free( p_rtsp->psz_session );
732     free( p_rtsp );
733 }
734
735 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
736                          httpd_message_t *answer, httpd_message_t *query )
737 {
738     vod_media_t *p_media = (vod_media_t*)p_args;
739     vod_t *p_vod = p_media->p_vod;
740     char *psz_transport = NULL;
741     char *psz_playnow = NULL; /* support option: x-playNow */
742     char *psz_session = NULL;
743     char *psz_cseq = NULL;
744     rtsp_client_t *p_rtsp;
745     int i_port = 0;
746     int i_cseq = 0;
747
748     if( answer == NULL || query == NULL ) return VLC_SUCCESS;
749
750     msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
751
752     answer->i_proto   = HTTPD_PROTO_RTSP;
753     answer->i_version = query->i_version;
754     answer->i_type    = HTTPD_MSG_ANSWER;
755     answer->i_body    = 0;
756     answer->p_body    = NULL;
757
758     switch( query->i_type )
759     {
760         case HTTPD_MSG_SETUP:
761         {
762             psz_playnow = httpd_MsgGet( query, "x-playNow" );
763             psz_transport = httpd_MsgGet( query, "Transport" );
764             msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
765
766             if( strstr( psz_transport, "unicast" ) &&
767                 strstr( psz_transport, "client_port=" ) )
768             {
769                 rtsp_client_t *p_rtsp = NULL;
770                 char ip[NI_MAXNUMERICHOST];
771                 i_port = atoi( strstr( psz_transport, "client_port=" ) +
772                                 strlen("client_port=") );
773
774                 if( strstr( psz_transport, "MP2T/H2221/UDP" ) ||
775                     strstr( psz_transport, "RAW/RAW/UDP" ) )
776                 {
777                     if( p_media->psz_mux ) free( p_media->psz_mux );
778                     p_media->psz_mux = NULL;
779                     p_media->psz_mux = strdup( p_vod->p_sys->psz_raw_mux );
780                     p_media->b_raw = VLC_TRUE;
781                 }
782
783                 if( httpd_ClientIP( cl, ip ) == NULL )
784                 {
785                     answer->i_status = 500;
786                     answer->psz_status = strdup( "Internal server error" );
787                     answer->i_body = 0;
788                     answer->p_body = NULL;
789                     break;
790                 }
791
792                 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
793                          ip, i_port );
794
795                 psz_session = httpd_MsgGet( query, "Session" );
796                 if( !psz_session || !*psz_session )
797                 {
798                     if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
799                         ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
800                     {
801                         answer->i_status = 503;
802                         answer->psz_status = strdup( "Too many connections" );
803                         answer->i_body = 0;
804                         answer->p_body = NULL;
805                         break;
806                     }
807                     asprintf( &psz_session, "%d", rand() );
808                     p_rtsp = RtspClientNew( p_media, psz_session );
809                     if( !p_rtsp )
810                     {
811                         answer->i_status = 454;
812                         answer->psz_status = strdup( "Unknown session id" );
813                         answer->i_body = 0;
814                         answer->p_body = NULL;
815                         break;
816                     }
817                 }
818                 else
819                 {
820                     p_rtsp = RtspClientGet( p_media, psz_session );
821                     if( !p_rtsp )
822                     {
823                         answer->i_status = 454;
824                         answer->psz_status = strdup( "Unknown session id" );
825                         answer->i_body = 0;
826                         answer->p_body = NULL;
827                         break;
828                     }
829                 }
830
831                 answer->i_status = 200;
832                 answer->psz_status = strdup( "OK" );
833                 answer->i_body = 0;
834                 answer->p_body = NULL;
835
836                 if( p_media->b_raw )
837                 {
838                     if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
839                     {
840                         httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
841                                       i_port, i_port + 1 );
842                     }
843                     else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
844                     {
845                         httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
846                                       i_port, i_port + 1 );
847                     }
848                 }
849                 else
850                     httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
851                                   i_port, i_port + 1 );
852             }
853             else /* TODO  strstr( psz_transport, "interleaved" ) ) */
854             {
855                 answer->i_status = 461;
856                 answer->psz_status = strdup( "Unsupported Transport" );
857                 answer->i_body = 0;
858                 answer->p_body = NULL;
859             }
860
861             /* Intentional fall-through on x-playNow option in RTSP request */
862             if( !psz_playnow )
863                 break;
864         }
865
866         case HTTPD_MSG_PLAY:
867         {
868             char *psz_output, ip[NI_MAXNUMERICHOST];
869             int i, i_port_audio = 0, i_port_video = 0;
870
871             /* for now only multicast so easy */
872             if( !psz_playnow )
873             {
874                 answer->i_status = 200;
875                 answer->psz_status = strdup( "OK" );
876                 answer->i_body = 0;
877                 answer->p_body = NULL;
878             }
879
880             if( !psz_session )
881                 psz_session = httpd_MsgGet( query, "Session" );
882             msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
883
884             p_rtsp = RtspClientGet( p_media, psz_session );
885             if( !p_rtsp )
886             {
887                 answer->i_status = 500;
888                 answer->psz_status = strdup( "Internal server error" );
889                 answer->i_body = 0;
890                 answer->p_body = NULL;
891                 break;
892             }
893
894             if( p_rtsp->b_playing )
895             {
896                 char *psz_position = httpd_MsgGet( query, "Range" );
897                 char *psz_scale = httpd_MsgGet( query, "Scale" );
898                 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
899                 if( psz_position && !psz_scale )
900                 {
901                     double f_pos;
902                     char *end;
903
904                     msg_Dbg( p_vod, "seeking request: %s", psz_position );
905                     psz_position += 4;
906                     /* FIXME: npt= is not necessarily formatted as a float */
907                     f_pos = us_strtod( psz_position, &end );
908                     if( end > psz_position )
909                     {
910                         f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
911                         vod_MediaControl( p_vod, p_media, psz_session,
912                                       VOD_MEDIA_SEEK, f_pos );
913                     }
914                     break;
915                 }
916                 if( psz_scale )
917                 {
918                     double f_scale = 0.0;
919                     char *end;
920
921                     f_scale = us_strtod( psz_scale, &end );
922                     if( end > psz_scale )
923                     {
924                         f_scale = (f_scale * 30.0);
925                         if( psz_scale[0] == '-' ) /* rewind */
926                         {
927                             msg_Dbg( p_vod, "rewind request: %s", psz_scale );
928                             vod_MediaControl( p_vod, p_media, psz_session,
929                                                 VOD_MEDIA_REWIND, f_scale );
930                         }
931                         else if(psz_scale[0] != '1' ) /* fast-forward */
932                         {
933                             msg_Dbg( p_vod, "fastforward request: %s", psz_scale );
934                             vod_MediaControl( p_vod, p_media, psz_session,
935                                                 VOD_MEDIA_FORWARD, f_scale );
936                         }
937
938                         if( p_rtsp->b_paused == VLC_TRUE )
939                         {
940                             p_rtsp->b_paused = VLC_FALSE;
941                             vod_MediaControl( p_vod, p_media, psz_session,
942                                                 VOD_MEDIA_PAUSE, psz_output );
943                         }
944                     }
945                     break;
946                 }
947             }
948
949             if( p_rtsp->b_playing && p_rtsp->b_paused )
950             {
951                 vod_MediaControl( p_vod, p_media, psz_session,
952                                   VOD_MEDIA_PAUSE );
953                 p_rtsp->b_paused = VLC_FALSE;
954                 break;
955             }
956             else if( p_rtsp->b_playing ) break;
957
958             if( httpd_ClientIP( cl, ip ) == NULL ) break;
959
960             p_rtsp->b_playing = VLC_TRUE;
961
962             /* FIXME for != 1 video and 1 audio */
963             for( i = 0; i < p_rtsp->i_es; i++ )
964             {
965                 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == AUDIO_ES )
966                     i_port_audio = p_rtsp->es[i]->i_port;
967                 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == VIDEO_ES )
968                     i_port_video = p_rtsp->es[i]->i_port;
969             }
970
971             if( p_media->psz_mux )
972             {
973                 if( p_media->b_raw )
974                 {
975                     asprintf( &psz_output, "std{access=udp,dst=%s:%i,mux=%s}",
976                               ip, i_port, p_media->psz_mux );
977                 }
978                 else
979                 {
980                     asprintf( &psz_output, "rtp{dst=%s,port=%i,mux=%s}",
981                               ip, i_port_video, p_media->psz_mux );
982                 }
983             }
984             else
985             {
986                 asprintf( &psz_output, "rtp{dst=%s,port-video=%i,"
987                           "port-audio=%i}", ip, i_port_video, i_port_audio );
988             }
989
990             vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PLAY,
991                               psz_output );
992             free( psz_output );
993             break;
994         }
995
996         case HTTPD_MSG_DESCRIBE:
997         {
998             char *psz_sdp =
999                 SDPGenerate( p_media, cl );
1000
1001             if( psz_sdp != NULL )
1002             {
1003                 answer->i_status = 200;
1004                 answer->psz_status = strdup( "OK" );
1005                 httpd_MsgAdd( answer, "Content-type",  "%s", "application/sdp" );
1006
1007                 answer->p_body = (uint8_t *)psz_sdp;
1008                 answer->i_body = strlen( psz_sdp );
1009             }
1010             else
1011             {
1012                 answer->i_status = 500;
1013                 answer->psz_status = strdup( "Internal server error" );
1014                 answer->p_body = NULL;
1015                 answer->i_body = 0;
1016             }
1017             break;
1018         }
1019
1020         case HTTPD_MSG_PAUSE:
1021             psz_session = httpd_MsgGet( query, "Session" );
1022             msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1023
1024             p_rtsp = RtspClientGet( p_media, psz_session );
1025             if( !p_rtsp ) break;
1026
1027             vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
1028             p_rtsp->b_paused = VLC_TRUE;
1029
1030             answer->i_status = 200;
1031             answer->psz_status = strdup( "OK" );
1032             answer->i_body = 0;
1033             answer->p_body = NULL;
1034             break;
1035
1036         case HTTPD_MSG_TEARDOWN:
1037             /* for now only multicast so easy again */
1038             answer->i_status = 200;
1039             answer->psz_status = strdup( "OK" );
1040             answer->i_body = 0;
1041             answer->p_body = NULL;
1042
1043             psz_session = httpd_MsgGet( query, "Session" );
1044             msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1045
1046             p_rtsp = RtspClientGet( p_media, psz_session );
1047             if( !p_rtsp ) break;
1048
1049             vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_STOP );
1050             RtspClientDel( p_media, p_rtsp );
1051             break;
1052
1053         case HTTPD_MSG_GETPARAMETER:
1054             answer->i_status = 200;
1055             answer->psz_status = strdup( "OK" );
1056             answer->i_body = 0;
1057             answer->p_body = NULL;
1058             break;
1059
1060         default:
1061             return VLC_EGENERIC;
1062     }
1063
1064     httpd_MsgAdd( answer, "Server", "VLC Server" );
1065     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1066     psz_cseq = httpd_MsgGet( query, "Cseq" );
1067     psz_cseq ? i_cseq = atoi( psz_cseq ) : 0;
1068     httpd_MsgAdd( answer, "CSeq", "%d", i_cseq );
1069     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1070
1071     if( psz_session )
1072     {
1073          if( p_media->p_vod->p_sys->i_session_timeout >= 0 )
1074              httpd_MsgAdd( answer, "Session", "%s;timeout=%i", psz_session,
1075                p_media->p_vod->p_sys->i_session_timeout );
1076          else
1077               httpd_MsgAdd( answer, "Session", "%s", psz_session );
1078     } 
1079
1080     return VLC_SUCCESS;
1081 }
1082
1083 static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
1084                            httpd_message_t *answer, httpd_message_t *query )
1085 {
1086     media_es_t *p_es = (media_es_t*)p_args;
1087     vod_media_t *p_media = p_es->p_media;
1088     vod_t *p_vod = p_media->p_vod;
1089     rtsp_client_t *p_rtsp = NULL;
1090     char *psz_transport = NULL;
1091     char *psz_playnow = NULL; /* support option: x-playNow */
1092     char *psz_session = NULL;
1093     char *psz_position = NULL;
1094     char *psz_cseq = NULL;
1095     int i_cseq = 0;
1096     int i;
1097
1098     if( answer == NULL || query == NULL ) return VLC_SUCCESS;
1099
1100     msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
1101
1102     answer->i_proto   = HTTPD_PROTO_RTSP;
1103     answer->i_version = query->i_version;
1104     answer->i_type    = HTTPD_MSG_ANSWER;
1105     answer->i_body    = 0;
1106     answer->p_body      = NULL;
1107
1108     switch( query->i_type )
1109     {
1110         case HTTPD_MSG_SETUP:
1111             psz_playnow = httpd_MsgGet( query, "x-playNow" );
1112             psz_transport = httpd_MsgGet( query, "Transport" );
1113
1114             msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
1115
1116             if( strstr( psz_transport, "unicast" ) &&
1117                 strstr( psz_transport, "client_port=" ) )
1118             {
1119                 rtsp_client_t *p_rtsp = NULL;
1120                 rtsp_client_es_t *p_rtsp_es = NULL;
1121                 char ip[NI_MAXNUMERICHOST];
1122                 int i_port = atoi( strstr( psz_transport, "client_port=" ) +
1123                                 strlen("client_port=") );
1124
1125                 if( httpd_ClientIP( cl, ip ) == NULL )
1126                 {
1127                     answer->i_status = 500;
1128                     answer->psz_status = strdup( "Internal server error" );
1129                     answer->i_body = 0;
1130                     answer->p_body = NULL;
1131                     break;
1132                 }
1133
1134                 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
1135                         ip, i_port );
1136
1137                 psz_session = httpd_MsgGet( query, "Session" );
1138                 if( !psz_session || !*psz_session )
1139                 {
1140                     if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
1141                         ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
1142                     {
1143                         answer->i_status = 503;
1144                         answer->psz_status = strdup( "Too many connections" );
1145                         answer->i_body = 0;
1146                         answer->p_body = NULL;
1147                         break;
1148                     }
1149                     asprintf( &psz_session, "%d", rand() );
1150                     p_rtsp = RtspClientNew( p_media, psz_session );
1151                 }
1152                 else
1153                 {
1154                     p_rtsp = RtspClientGet( p_media, psz_session );
1155                     if( !p_rtsp )
1156                     {
1157                         answer->i_status = 454;
1158                         answer->psz_status = strdup( "Unknown session id" );
1159                         answer->i_body = 0;
1160                         answer->p_body = NULL;
1161                         break;
1162                     }
1163                 }
1164
1165                 p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
1166                 if( !p_rtsp_es )
1167                 {
1168                     answer->i_status = 500;
1169                     answer->psz_status = strdup( "Internal server error" );
1170                     answer->i_body = 0;
1171                     answer->p_body = NULL;
1172                     break;
1173                 }
1174                 p_rtsp_es->i_port = i_port;
1175                 p_rtsp_es->psz_ip = strdup( ip );
1176                 p_rtsp_es->p_media_es = p_es;
1177                 TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );
1178
1179                 answer->i_status = 200;
1180                 answer->psz_status = strdup( "OK" );
1181                 answer->i_body = 0;
1182                 answer->p_body = NULL;
1183
1184                 if( p_media->b_raw )
1185                 {
1186                     if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
1187                     {
1188                         httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
1189                                       p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1190                     }
1191                     else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1192                     {
1193                         httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
1194                                       p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1195                     }
1196                 }
1197                 else
1198                 {
1199                     httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
1200                                   p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1201                 }
1202             }
1203             else /* TODO  strstr( psz_transport, "interleaved" ) ) */
1204             {
1205                 answer->i_status = 461;
1206                 answer->psz_status = strdup( "Unsupported Transport" );
1207                 answer->i_body = 0;
1208                 answer->p_body = NULL;
1209             }
1210
1211             /* Intentional fall-through on x-playNow option in RTSP request */
1212             if( !psz_playnow )
1213                 break;
1214
1215         case HTTPD_MSG_PLAY:
1216             /* This is kind of a kludge. Should we only support Aggregate
1217              * Operations ? */
1218             psz_session = httpd_MsgGet( query, "Session" );
1219             msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1220
1221             p_rtsp = RtspClientGet( p_media, psz_session );
1222
1223             psz_position = httpd_MsgGet( query, "Range" );
1224             if( psz_position ) psz_position = strstr( psz_position, "npt=" );
1225             if( psz_position )
1226             {
1227                 double f_pos;
1228                 char *end;
1229
1230                 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1231
1232                 psz_position += 4;
1233                 /* FIXME: npt= is not necessarily formatted as a float */
1234                 f_pos = us_strtod( psz_position, &end );
1235                 if( end > psz_position )
1236                 {
1237                     f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
1238                     vod_MediaControl( p_vod, p_media, psz_session,
1239                                       VOD_MEDIA_SEEK, f_pos );
1240                 }
1241             }
1242
1243             if( !psz_playnow )
1244             {
1245                 answer->i_status = 200;
1246                 answer->psz_status = strdup( "OK" );
1247                 answer->i_body = 0;
1248                 answer->p_body = NULL;
1249             }
1250             break;
1251
1252         case HTTPD_MSG_TEARDOWN:
1253             answer->i_status = 200;
1254             answer->psz_status = strdup( "OK" );
1255             answer->i_body = 0;
1256             answer->p_body = NULL;
1257
1258             psz_session = httpd_MsgGet( query, "Session" );
1259             msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1260
1261             p_rtsp = RtspClientGet( p_media, psz_session );
1262             if( !p_rtsp ) break;
1263
1264             for( i = 0; i < p_rtsp->i_es; i++ )
1265             {
1266                 if( p_rtsp->es[i]->p_media_es == p_es )
1267                 {
1268                     if( p_rtsp->es[i]->psz_ip ) free( p_rtsp->es[i]->psz_ip );
1269                     TAB_REMOVE( p_rtsp->i_es, p_rtsp->es, p_rtsp->es[i] );
1270                     break;
1271                 }
1272             }
1273
1274             if( !p_rtsp->i_es )
1275             {
1276                 vod_MediaControl( p_vod, p_media, psz_session,
1277                                   VOD_MEDIA_STOP );
1278                 RtspClientDel( p_media, p_rtsp );
1279             }
1280             break;
1281
1282         case HTTPD_MSG_PAUSE:
1283             /* This is kind of a kludge. Should we only support Aggregate
1284              * Operations ? */
1285             psz_session = httpd_MsgGet( query, "Session" );
1286             msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1287
1288             p_rtsp = RtspClientGet( p_media, psz_session );
1289             if( !p_rtsp ) break;
1290
1291             vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
1292             p_rtsp->b_paused = VLC_TRUE;
1293
1294             answer->i_status = 200;
1295             answer->psz_status = strdup( "OK" );
1296             answer->i_body = 0;
1297             answer->p_body = NULL;
1298             break;
1299
1300         default:
1301             return VLC_EGENERIC;
1302             break;
1303     }
1304
1305     httpd_MsgAdd( answer, "Server", "VLC Server" );
1306     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1307     psz_cseq = httpd_MsgGet( query, "Cseq" );
1308     if (psz_cseq)
1309         i_cseq = atoi( psz_cseq );
1310     else
1311         i_cseq = 0;
1312     httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1313     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1314
1315     if( psz_session )
1316         httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1317
1318     return VLC_SUCCESS;
1319 }
1320
1321 /*****************************************************************************
1322  * SDPGenerate: TODO
1323  * FIXME: need to be moved to a common place ?
1324  *****************************************************************************/
1325 static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
1326 {
1327     int i, i_size;
1328     char *p, *psz_sdp, ip[NI_MAXNUMERICHOST], ipv;
1329     const char *psz_control;
1330
1331     if( httpd_ServerIP( cl, ip ) == NULL )
1332         return NULL;
1333
1334     p = strchr( ip, '%' );
1335     if( p != NULL )
1336         *p = '\0'; /* remove scope if present */
1337
1338     ipv = ( strchr( ip, ':' ) != NULL ) ? '6' : '4';
1339
1340     /* Calculate size */
1341     i_size = sizeof( "v=0\r\n" ) +
1342         sizeof( "o=- * * IN IP4 \r\n" ) + 10 + NI_MAXNUMERICHOST +
1343         sizeof( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
1344         sizeof( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
1345         sizeof( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
1346         sizeof( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
1347         sizeof( "c=IN IP4 0.0.0.0\r\n" ) + 20 + 10 +
1348         sizeof( "t=0 0\r\n" ) + /* FIXME */
1349         sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
1350         sizeof( "a=range:npt=0-1000000000.000\r\n" );
1351
1352     psz_control = (ipv == '6') ? p_media->psz_rtsp_control_v6
1353                                : p_media->psz_rtsp_control_v4;
1354     for( i = 0; i < p_media->i_es; i++ )
1355     {
1356         media_es_t *p_es = p_media->es[i];
1357
1358         i_size += sizeof( "m=**d*o * RTP/AVP *\r\n" ) + 19;
1359         if( p_es->psz_rtpmap )
1360         {
1361             i_size += sizeof( "a=rtpmap:* *\r\n" ) +
1362                 strlen( p_es->psz_rtpmap ) + 9;
1363         }
1364         if( p_es->psz_fmtp )
1365         {
1366             i_size += sizeof( "a=fmtp:* *\r\n" ) +
1367                 strlen( p_es->psz_fmtp ) + 9;
1368         }
1369     }
1370     i_size += (strlen( psz_control ) + strlen( ip ) + 9) * p_media->i_es;
1371
1372     p = psz_sdp = malloc( i_size );
1373     p += sprintf( p, "v=0\r\n" );
1374     p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
1375                   p_media->i_sdp_id, p_media->i_sdp_version, ipv, ip );
1376     if( *p_media->psz_session_name )
1377         p += sprintf( p, "s=%s\r\n", p_media->psz_session_name );
1378     if( *p_media->psz_session_description )
1379         p += sprintf( p, "i=%s\r\n", p_media->psz_session_description );
1380     if( *p_media->psz_session_url )
1381         p += sprintf( p, "u=%s\r\n", p_media->psz_session_url );
1382     if( *p_media->psz_session_email )
1383         p += sprintf( p, "e=%s\r\n", p_media->psz_session_email );
1384
1385     p += sprintf( p, "c=IN IP%c %s\r\n", ipv, ipv == '6' ? "::" : "0.0.0.0" );
1386     p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
1387     p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
1388
1389     if( p_media->i_length > 0 )
1390     {
1391         lldiv_t d = lldiv( p_media->i_length / 1000, 1000 );
1392         p += sprintf( p, "a=range:npt=0-%lld.%03u\r\n", d.quot,
1393                       (unsigned)d.rem );
1394     }
1395
1396     for( i = 0; i < p_media->i_es; i++ )
1397     {
1398         media_es_t *p_es = p_media->es[i];
1399
1400         if( p_es->fmt.i_cat == AUDIO_ES )
1401         {
1402             p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
1403                           p_es->i_port, p_es->i_payload_type );
1404         }
1405         else if( p_es->fmt.i_cat == VIDEO_ES )
1406         {
1407             p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
1408                           p_es->i_port, p_es->i_payload_type );
1409         }
1410         else
1411         {
1412             continue;
1413         }
1414
1415         if( p_es->psz_rtpmap )
1416         {
1417             p += sprintf( p, "a=rtpmap:%d %s\r\n", p_es->i_payload_type,
1418                           p_es->psz_rtpmap );
1419         }
1420         if( p_es->psz_fmtp )
1421         {
1422             p += sprintf( p, "a=fmtp:%d %s\r\n", p_es->i_payload_type,
1423                           p_es->psz_fmtp );
1424         }
1425
1426         p += sprintf( p, psz_control, ip, i );
1427     }
1428
1429     return psz_sdp;
1430 }