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