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