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