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