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