]> git.sesse.net Git - vlc/blob - modules/misc/rtsp.c
Fix some memleaks
[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->psz_host );
254     free( p_sys->psz_path );
255     free( p_sys );
256 }
257
258 /*****************************************************************************
259  * Media handling
260  *****************************************************************************/
261 static vod_media_t *MediaNew( vod_t *p_vod, char *psz_name,
262                               input_item_t *p_item )
263 {
264     vod_sys_t *p_sys = p_vod->p_sys;
265     vod_media_t *p_media = malloc( sizeof(vod_media_t) );
266     int i;
267
268     memset( p_media, 0, sizeof(vod_media_t) );
269     p_media->es = 0;
270     p_media->psz_mux = 0;
271     p_media->rtsp = 0;
272
273     asprintf( &p_media->psz_rtsp_path, "%s%s", p_sys->psz_path, psz_name );
274     p_media->p_rtsp_url =
275         httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, NULL,
276                             NULL, NULL );
277
278     if( !p_media->p_rtsp_url )
279     {
280         msg_Err( p_vod, "cannot create http url (%s)", p_media->psz_rtsp_path);
281         free( p_media->psz_rtsp_path );
282         free( p_media );
283         return 0;
284     }
285
286     msg_Dbg( p_vod, "created rtsp url: %s", p_media->psz_rtsp_path );
287
288     asprintf( &p_media->psz_rtsp_control, "rtsp://%s:%d%s",
289               p_sys->psz_host, 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 ) free( p_media->psz_rtsp_control );
340
341     TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
342
343     while( p_media->i_es ) MediaDelES( p_vod, p_media, &p_media->es[0]->fmt );
344
345     vlc_mutex_destroy( &p_media->lock );
346     free( p_media->psz_session_name );
347     free( p_media->psz_session_description );
348     free( p_media->psz_session_url );
349     free( p_media->psz_session_email );
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     case VLC_FOURCC( 's', 'a', 'm', 'r' ):
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/8000/2" : "AMR/8000" );
451         p_es->psz_fmtp = strdup( "octet-align=1" );
452         break; 
453     case VLC_FOURCC( 's', 'a', 'w', 'b' ):
454         p_es->i_payload_type = p_media->i_payload_type++;
455         p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
456                                    "AMR-WB/16000/2" : "AMR-WB/16000" );
457         p_es->psz_fmtp = strdup( "octet-align=1" );
458         break; 
459
460     default:
461         msg_Err( p_vod, "cannot add this stream (unsupported "
462                  "codec: %4.4s)", (char*)&p_fmt->i_codec );
463         free( p_es );
464         return VLC_EGENERIC;
465     }
466
467     p_es->p_rtsp_url =
468         httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, NULL, NULL,
469                             NULL );
470
471     if( !p_es->p_rtsp_url )
472     {
473         msg_Err( p_vod, "cannot create http url (%s)", psz_urlc );
474         free( psz_urlc );
475         free( p_es );
476         return VLC_EGENERIC;
477     }
478     free( psz_urlc );
479
480     httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
481                     RtspCallbackES, (void*)p_es );
482     httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_TEARDOWN,
483                     RtspCallbackES, (void*)p_es );
484     httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PLAY,
485                     RtspCallbackES, (void*)p_es );
486     httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PAUSE,
487                     RtspCallbackES, (void*)p_es );
488
489     es_format_Copy( &p_es->fmt, p_fmt );
490     p_es->p_vod = p_vod;
491     p_es->p_media = p_media;
492
493 #if 0
494     /* Choose the port */
495     if( p_fmt->i_cat == AUDIO_ES && p_media->i_port_audio > 0 )
496     {
497         p_es->i_port = p_media->i_port_audio;
498         p_media->i_port_audio = 0;
499     }
500     else if( p_fmt->i_cat == VIDEO_ES && p_media->i_port_video > 0 )
501     {
502         p_es->i_port = p_media->i_port_video;
503         p_media->i_port_video = 0;
504     }
505     while( !p_es->i_port )
506     {
507         if( p_media->i_port != p_media->i_port_audio &&
508             p_media->i_port != p_media->i_port_video )
509         {
510             p_es->i_port = p_media->i_port;
511             p_media->i_port += 2;
512             break;
513         }
514         p_media->i_port += 2;
515     }
516 #else
517
518     p_es->i_port = 0;
519 #endif
520
521     vlc_mutex_lock( &p_media->lock );
522     TAB_APPEND( p_media->i_es, p_media->es, p_es );
523     vlc_mutex_unlock( &p_media->lock );
524
525     p_media->i_sdp_version++;
526
527     return VLC_SUCCESS;
528 }
529
530 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
531 {
532     media_es_t *p_es = 0;
533     int i;
534
535     /* Find the ES */
536     for( i = 0; i < p_media->i_es; i++ )
537     {
538         if( p_media->es[i]->fmt.i_cat == p_fmt->i_cat &&
539             p_media->es[i]->fmt.i_codec == p_fmt->i_codec &&
540             p_media->es[i]->fmt.i_id == p_fmt->i_id )
541         {
542             p_es = p_media->es[i];
543         }
544     }
545     if( !p_es ) return;
546
547     msg_Dbg( p_vod, "  - Removing ES %4.4s", (char *)&p_fmt->i_codec );
548
549     vlc_mutex_lock( &p_media->lock );
550     TAB_REMOVE( p_media->i_es, p_media->es, p_es );
551     vlc_mutex_unlock( &p_media->lock );
552
553     if( p_es->psz_rtpmap ) free( p_es->psz_rtpmap );
554     if( p_es->psz_fmtp ) free( p_es->psz_fmtp );
555     p_media->i_sdp_version++;
556
557     if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
558     es_format_Clean( &p_es->fmt );
559 }
560
561 /****************************************************************************
562  * RTSP server implementation
563  ****************************************************************************/
564 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
565 {
566     rtsp_client_t *p_rtsp = malloc( sizeof(rtsp_client_t) );
567     memset( p_rtsp, 0, sizeof(rtsp_client_t) );
568     p_rtsp->es = 0;
569
570     p_rtsp->psz_session = psz_session;
571     TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
572
573     msg_Dbg( p_media->p_vod, "new session: %s", psz_session );
574
575     return p_rtsp;
576 }
577
578 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, char *psz_session )
579 {
580     int i;
581
582     for( i = 0; psz_session && i < p_media->i_rtsp; i++ )
583     {
584         if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
585         {
586             return p_media->rtsp[i];
587         }
588     }
589
590     return NULL;
591 }
592
593 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
594 {
595     msg_Dbg( p_media->p_vod, "closing session: %s", p_rtsp->psz_session );
596
597     while( p_rtsp->i_es-- )
598     {
599         if( p_rtsp->es[p_rtsp->i_es]->psz_ip )
600             free( p_rtsp->es[p_rtsp->i_es]->psz_ip );
601         free( p_rtsp->es[p_rtsp->i_es] );
602         if( !p_rtsp->i_es ) free( p_rtsp->es );
603     }
604
605     TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, p_rtsp );
606
607     free( p_rtsp->psz_session );
608     free( p_rtsp );
609 }
610
611 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
612                          httpd_message_t *answer, httpd_message_t *query )
613 {
614     vod_media_t *p_media = (vod_media_t*)p_args;
615     vod_t *p_vod = p_media->p_vod;
616     char *psz_session = NULL;
617     rtsp_client_t *p_rtsp;
618
619     if( answer == NULL || query == NULL ) return VLC_SUCCESS;
620
621     fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
622
623     answer->i_proto   = HTTPD_PROTO_RTSP;
624     answer->i_version = query->i_version;
625     answer->i_type    = HTTPD_MSG_ANSWER;
626
627     switch( query->i_type )
628     {
629         case HTTPD_MSG_DESCRIBE:
630         {
631             char *psz_sdp =
632                 SDPGenerate( p_media, cl );
633
634             if( psz_sdp != NULL )
635             {
636                 answer->i_status = 200;
637                 answer->psz_status = strdup( "OK" );
638                 httpd_MsgAdd( answer, "Content-type",  "%s", "application/sdp" );
639     
640                 answer->p_body = (uint8_t *)psz_sdp;
641                 answer->i_body = strlen( psz_sdp );
642             }
643             else
644             {
645                 answer->i_status = 500;
646                 answer->psz_status = strdup( "Internal server error" );
647                 answer->p_body = NULL;
648                 answer->i_body = 0;
649             }
650             break;
651         }
652
653         case HTTPD_MSG_PLAY:
654         {
655             char *psz_output, ip[NI_MAXNUMERICHOST];
656             int i, i_port_audio = 0, i_port_video = 0;
657
658             /* for now only multicast so easy */
659             answer->i_status = 200;
660             answer->psz_status = strdup( "OK" );
661             answer->i_body = 0;
662             answer->p_body = NULL;
663
664             psz_session = httpd_MsgGet( query, "Session" );
665             msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
666
667             p_rtsp = RtspClientGet( p_media, psz_session );
668             if( !p_rtsp ) break;
669
670             if( p_rtsp->b_playing && p_rtsp->b_paused )
671             {
672                 vod_MediaControl( p_vod, p_media, psz_session,
673                                   VOD_MEDIA_PAUSE );
674                 p_rtsp->b_paused = VLC_FALSE;
675                 break;
676             }
677             else if( p_rtsp->b_playing ) break;
678
679             if( httpd_ClientIP( cl, ip ) == NULL ) break;
680
681             p_rtsp->b_playing = VLC_TRUE;
682
683             /* FIXME for != 1 video and 1 audio */
684             for( i = 0; i < p_rtsp->i_es; i++ )
685             {
686                 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == AUDIO_ES )
687                     i_port_audio = p_rtsp->es[i]->i_port;
688                 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == VIDEO_ES )
689                     i_port_video = p_rtsp->es[i]->i_port;
690             }
691
692             if( p_media->psz_mux )
693             {
694                 asprintf( &psz_output, "rtp{dst=%s,port=%i,mux=%s}",
695                           ip, i_port_video, p_media->psz_mux );
696             }
697             else
698             {
699                 asprintf( &psz_output, "rtp{dst=%s,port-video=%i,"
700                           "port-audio=%i}", ip, i_port_video, i_port_audio );
701             }
702
703             vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PLAY,
704                               psz_output );
705             free( psz_output );
706             break;
707         }
708
709         case HTTPD_MSG_PAUSE:
710             psz_session = httpd_MsgGet( query, "Session" );
711             msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
712
713             p_rtsp = RtspClientGet( p_media, psz_session );
714             if( !p_rtsp ) break;
715
716             vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
717             p_rtsp->b_paused = VLC_TRUE;
718
719             answer->i_status = 200;
720             answer->psz_status = strdup( "OK" );
721             answer->i_body = 0;
722             answer->p_body = NULL;
723             break;
724
725         case HTTPD_MSG_TEARDOWN:
726             /* for now only multicast so easy again */
727             answer->i_status = 200;
728             answer->psz_status = strdup( "OK" );
729             answer->i_body = 0;
730             answer->p_body = NULL;
731
732             psz_session = httpd_MsgGet( query, "Session" );
733             msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
734
735             p_rtsp = RtspClientGet( p_media, psz_session );
736             if( !p_rtsp ) break;
737
738             vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_STOP );
739             RtspClientDel( p_media, p_rtsp );
740             break;
741
742         default:
743             return VLC_EGENERIC;
744     }
745
746     httpd_MsgAdd( answer, "Server", "VLC Server" );
747     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
748     httpd_MsgAdd( answer, "Cseq", "%d",
749                   atoi( httpd_MsgGet( query, "Cseq" ) ) );
750     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
751
752     if( psz_session )
753     {
754         httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
755     }
756
757     return VLC_SUCCESS;
758 }
759
760 static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
761                            httpd_message_t *answer, httpd_message_t *query )
762 {
763     media_es_t *p_es = (media_es_t*)p_args;
764     vod_media_t *p_media = p_es->p_media;
765     vod_t *p_vod = p_media->p_vod;
766     rtsp_client_t *p_rtsp = NULL;
767     char *psz_session = NULL;
768     char *psz_transport = NULL;
769     char *psz_position = NULL;
770     int i;
771
772     if( answer == NULL || query == NULL ) return VLC_SUCCESS;
773
774     fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
775
776     answer->i_proto   = HTTPD_PROTO_RTSP;
777     answer->i_version = query->i_version;
778     answer->i_type    = HTTPD_MSG_ANSWER;
779
780     switch( query->i_type )
781     {
782     case HTTPD_MSG_SETUP:
783         psz_transport = httpd_MsgGet( query, "Transport" );
784         fprintf( stderr, "HTTPD_MSG_SETUP: transport=%s\n", psz_transport );
785
786         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[NI_MAXNUMERICHOST];
792             int i_port = atoi( strstr( psz_transport, "client_port=" ) +
793                                strlen("client_port=") );
794
795             if( httpd_ClientIP( cl, ip ) == NULL )
796             {
797                 answer->i_status = 500;
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 = 454;
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 = 461;
845             answer->psz_status = strdup( "Unsupported Transport" );
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( const vod_media_t *p_media, httpd_client_t *cl )
954 {
955     int i, i_size;
956     char *p, *psz_sdp, ip[NI_MAXNUMERICHOST], ipv;
957
958     if( httpd_ServerIP( cl, ip ) == NULL )
959         return NULL;
960
961     p = strchr( ip, '%' );
962     if( p != NULL )
963         *p = '\0'; /* remove scope if present */
964
965     ipv = ( strchr( ip, ':' ) != NULL ) ? '6' : '4';
966
967     /* Calculate size */
968     i_size = sizeof( "v=0\r\n" ) +
969         sizeof( "o=- * * IN IP4 \r\n" ) + 10 + NI_MAXNUMERICHOST +
970         sizeof( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
971         sizeof( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
972         sizeof( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
973         sizeof( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
974         sizeof( "t=0 0\r\n" ) + /* FIXME */
975         sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
976         sizeof( "c=IN IP4 0.0.0.0\r\n" ) + 20 + 10 +
977         sizeof( "a=range:npt=0-1000000000.000\r\n" );
978
979     for( i = 0; i < p_media->i_es; i++ )
980     {
981         media_es_t *p_es = p_media->es[i];
982
983         i_size += sizeof( "m=**d*o * RTP/AVP *\r\n" ) + 19;
984         if( p_es->psz_rtpmap )
985         {
986             i_size += sizeof( "a=rtpmap:* *\r\n" ) +
987                 strlen( p_es->psz_rtpmap ) + 9;
988         }
989         if( p_es->psz_fmtp )
990         {
991             i_size += sizeof( "a=fmtp:* *\r\n" ) +
992                 strlen( p_es->psz_fmtp ) + 9;
993         }
994
995         i_size += sizeof( "a=control:*/trackid=*\r\n" ) +
996             strlen( p_media->psz_rtsp_control ) + 9;
997     }
998
999     p = psz_sdp = malloc( i_size );
1000     p += sprintf( p, "v=0\r\n" );
1001     p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
1002                   p_media->i_sdp_id, p_media->i_sdp_version, ipv, ip );
1003     if( *p_media->psz_session_name )
1004         p += sprintf( p, "s=%s\r\n", p_media->psz_session_name );
1005     if( *p_media->psz_session_description )
1006         p += sprintf( p, "i=%s\r\n", p_media->psz_session_description );
1007     if( *p_media->psz_session_url )
1008         p += sprintf( p, "u=%s\r\n", p_media->psz_session_url );
1009     if( *p_media->psz_session_email )
1010         p += sprintf( p, "e=%s\r\n", p_media->psz_session_email );
1011
1012     p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
1013     p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
1014
1015     p += sprintf( p, "c=IN IP%c %s\r\n", ipv, ipv == '6' ? "::" : "0.0.0.0" );
1016
1017     if( p_media->i_length > 0 )
1018     p += sprintf( p, "a=range:npt=0-%.3f\r\n",
1019                   ((float)(p_media->i_length/1000))/1000 );
1020
1021     for( i = 0; i < p_media->i_es; i++ )
1022     {
1023         media_es_t *p_es = p_media->es[i];
1024
1025         if( p_es->fmt.i_cat == AUDIO_ES )
1026         {
1027             p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
1028                           p_es->i_port, p_es->i_payload_type );
1029         }
1030         else if( p_es->fmt.i_cat == VIDEO_ES )
1031         {
1032             p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
1033                           p_es->i_port, p_es->i_payload_type );
1034         }
1035         else
1036         {
1037             continue;
1038         }
1039
1040         if( p_es->psz_rtpmap )
1041         {
1042             p += sprintf( p, "a=rtpmap:%d %s\r\n", p_es->i_payload_type,
1043                           p_es->psz_rtpmap );
1044         }
1045         if( p_es->psz_fmtp )
1046         {
1047             p += sprintf( p, "a=fmtp:%d %s\r\n", p_es->i_payload_type,
1048                           p_es->psz_fmtp );
1049         }
1050
1051         p += sprintf( p, "a=control:%s/trackid=%d\r\n",
1052                       p_media->psz_rtsp_control, i );
1053     }
1054
1055     return psz_sdp;
1056 }