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