]> git.sesse.net Git - vlc/blob - modules/misc/rtsp.c
modules/misc/rtsp.c: implement support for raw udp transport for muxed streams
[vlc] / modules / misc / rtsp.c
1 /*****************************************************************************
2  * rtsp.c: rtsp VoD server module
3  *****************************************************************************
4  * Copyright (C) 2003-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29
30 #include <errno.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc/input.h>
34 #include <vlc/sout.h>
35
36 #include "vlc_httpd.h"
37 #include "vlc_vod.h"
38 #include "network.h"
39
40 /*****************************************************************************
41  * Module descriptor
42  *****************************************************************************/
43 static int  Open ( vlc_object_t * );
44 static void Close( vlc_object_t * );
45
46 #define HOST_TEXT N_( "Host address" )
47 #define HOST_LONGTEXT N_( \
48     "You can set the address, port and path the rtsp interface will bind to." \
49     "\nSyntax is address:port/path. Default is to bind to any address "\
50     "on port 554, with no path." )
51
52 vlc_module_begin();
53     set_shortname( _("RTSP VoD" ) );
54     set_description( _("RTSP VoD server") );
55     set_category( CAT_SOUT );
56     set_subcategory( SUBCAT_SOUT_VOD );
57     set_capability( "vod server", 1 );
58     set_callbacks( Open, Close );
59     add_shortcut( "rtsp" );
60     add_string ( "rtsp-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
61 vlc_module_end();
62
63 /*****************************************************************************
64  * Exported prototypes
65  *****************************************************************************/
66
67 typedef struct media_es_t media_es_t;
68
69 typedef struct
70 {
71     media_es_t *p_media_es;
72     char *psz_ip;
73     int i_port;
74
75 } rtsp_client_es_t;
76
77 typedef struct
78 {
79     char *psz_session;
80     int64_t i_last; /* for timeout */
81
82     vlc_bool_t b_playing; /* is it in "play" state */
83     vlc_bool_t b_paused; /* is it in "pause" state */
84
85     int i_es;
86     rtsp_client_es_t **es;
87
88 } rtsp_client_t;
89
90 struct media_es_t
91 {
92     /* VoD server */
93     vod_t *p_vod;
94
95     /* RTSP server */
96     httpd_url_t *p_rtsp_url;
97
98     vod_media_t *p_media;
99
100     es_format_t fmt;
101     int         i_port;
102     uint8_t     i_payload_type;
103     char        *psz_rtpmap;
104     char        *psz_fmtp;
105
106 };
107
108 struct vod_media_t
109 {
110     /* VoD server */
111     vod_t *p_vod;
112
113     /* RTSP server */
114     httpd_url_t  *p_rtsp_url;
115     char         *psz_rtsp_control_v4, *psz_rtsp_control_v6;
116     char         *psz_rtsp_path;
117
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     int        b_raw;
136
137     /* RTSP client */
138     int           i_rtsp;
139     rtsp_client_t **rtsp;
140
141     /* Infos */
142     char *psz_session_name;
143     char *psz_session_description;
144     char *psz_session_url;
145     char *psz_session_email;
146     mtime_t i_length;
147 };
148
149 struct vod_sys_t
150 {
151     /* RTSP server */
152     httpd_host_t *p_rtsp_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 *, const 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( const vod_media_t *, httpd_client_t *cl );
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.i_port <= 0 ) url.i_port = 554;
205
206     p_vod->p_sys = p_sys = malloc( sizeof( vod_sys_t ) );
207     if( !p_sys ) goto error;
208     p_sys->p_rtsp_host = 0;
209
210     p_sys->p_rtsp_host =
211         httpd_HostNew( VLC_OBJECT(p_vod), url.psz_host, url.i_port );
212     if( !p_sys->p_rtsp_host )
213     {
214         msg_Err( p_vod, "cannot create http server (%s:%i)",
215                  url.psz_host, url.i_port );
216         goto error;
217     }
218
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     free( p_sys->psz_path );
253     free( p_sys );
254 }
255
256 /*****************************************************************************
257  * Media handling
258  *****************************************************************************/
259 static vod_media_t *MediaNew( vod_t *p_vod, const char *psz_name,
260                               input_item_t *p_item )
261 {
262     vod_sys_t *p_sys = p_vod->p_sys;
263     vod_media_t *p_media = malloc( sizeof(vod_media_t) );
264     int i;
265
266     if( !p_media )
267     {
268         msg_Err( p_vod, "not enough memory" );
269         return NULL;
270     }
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, NULL,
280                             NULL, NULL );
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 NULL;
288     }
289
290     msg_Dbg( p_vod, "created rtsp url: %s", p_media->psz_rtsp_path );
291
292     asprintf( &p_media->psz_rtsp_control_v4,
293                "a=control:rtsp://%%s:%d%s/trackid=%%d\r\n",
294                p_sys->i_port, p_media->psz_rtsp_path );
295     asprintf( &p_media->psz_rtsp_control_v6,
296                "a=control:rtsp://[%%s]:%d%s/trackid=%%d\r\n",
297               p_sys->i_port, p_media->psz_rtsp_path );
298
299     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_SETUP,
300                     RtspCallback, (void*)p_media );
301     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_DESCRIBE,
302                     RtspCallback, (void*)p_media );
303     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PLAY,
304                     RtspCallback, (void*)p_media );
305     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PAUSE,
306                     RtspCallback, (void*)p_media );
307     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_TEARDOWN,
308                     RtspCallback, (void*)p_media );
309
310     p_media->p_vod = p_vod;
311
312     TAB_APPEND( p_sys->i_media, p_sys->media, p_media );
313
314     vlc_mutex_init( p_vod, &p_media->lock );
315     p_media->psz_session_name = strdup("");
316     p_media->psz_session_description = strdup("");
317     p_media->psz_session_url = strdup("");
318     p_media->psz_session_email = strdup("");
319
320     p_media->i_port_audio = 1234;
321     p_media->i_port_video = 1236;
322     p_media->i_port       = 1238;
323     p_media->i_payload_type = 96;
324
325     p_media->i_sdp_id = mdate();
326     p_media->i_sdp_version = 1;
327     p_media->i_length = p_item->i_duration;
328
329     vlc_mutex_lock( &p_item->lock );
330     msg_Dbg( p_vod, "media has %i declared ES", p_item->i_es );
331     for( i = 0; i < p_item->i_es; i++ )
332     {
333         MediaAddES( p_vod, p_media, p_item->es[i] );
334     }
335     vlc_mutex_unlock( &p_item->lock );
336
337     return p_media;
338 }
339
340 static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
341 {
342     vod_sys_t *p_sys = p_vod->p_sys;
343
344     msg_Dbg( p_vod, "deleting media: %s", p_media->psz_rtsp_path );
345
346     while( p_media->i_rtsp > 0 ) RtspClientDel( p_media, p_media->rtsp[0] );
347     httpd_UrlDelete( p_media->p_rtsp_url );
348     if( p_media->psz_rtsp_path ) free( p_media->psz_rtsp_path );
349     if( p_media->psz_rtsp_control_v6 ) free( p_media->psz_rtsp_control_v6 );
350     if( p_media->psz_rtsp_control_v4 ) free( p_media->psz_rtsp_control_v4 );
351
352     TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
353
354     while( p_media->i_es ) MediaDelES( p_vod, p_media, &p_media->es[0]->fmt );
355
356     vlc_mutex_destroy( &p_media->lock );
357     free( p_media->psz_session_name );
358     free( p_media->psz_session_description );
359     free( p_media->psz_session_url );
360     free( p_media->psz_session_email );
361     free( p_media );
362 }
363
364 static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
365 {
366     media_es_t *p_es = malloc( sizeof(media_es_t) );
367     char *psz_urlc;
368
369     memset( p_es, 0, sizeof(media_es_t) );
370     p_media->psz_mux = NULL;
371
372     /* TODO: update SDP, etc... */
373     asprintf( &psz_urlc, "%s/trackid=%d",
374               p_media->psz_rtsp_path, p_media->i_es );
375     msg_Dbg( p_vod, "  - ES %4.4s (%s)", (char *)&p_fmt->i_codec, psz_urlc );
376
377     switch( p_fmt->i_codec )
378     {
379         case VLC_FOURCC( 's', '1', '6', 'b' ):
380             if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
381             {
382                 p_es->i_payload_type = 11;
383             }
384             else if( p_fmt->audio.i_channels == 2 && p_fmt->audio.i_rate == 44100 )
385             {
386                 p_es->i_payload_type = 10;
387             }
388             else
389             {
390                 p_es->i_payload_type = p_media->i_payload_type++;
391             }
392             p_es->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
393             sprintf( p_es->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
394                     p_fmt->audio.i_channels );
395             break;
396         case VLC_FOURCC( 'u', '8', ' ', ' ' ):
397             p_es->i_payload_type = p_media->i_payload_type++;
398             p_es->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
399             sprintf( p_es->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
400                     p_fmt->audio.i_channels );
401             break;
402         case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
403             p_es->i_payload_type = 14;
404             p_es->psz_rtpmap = strdup( "MPA/90000" );
405             break;
406         case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
407             p_es->i_payload_type = 32;
408             p_es->psz_rtpmap = strdup( "MPV/90000" );
409             break;
410         case VLC_FOURCC( 'a', '5', '2', ' ' ):
411             p_es->i_payload_type = p_media->i_payload_type++;
412             p_es->psz_rtpmap = strdup( "ac3/90000" );
413             break;
414         case VLC_FOURCC( 'H', '2', '6', '3' ):
415             p_es->i_payload_type = p_media->i_payload_type++;
416             p_es->psz_rtpmap = strdup( "H263-1998/90000" );
417             break;
418         case VLC_FOURCC( 'm', 'p', '4', 'v' ):
419             p_es->i_payload_type = p_media->i_payload_type++;
420             p_es->psz_rtpmap = strdup( "MP4V-ES/90000" );
421             if( p_fmt->i_extra > 0 )
422             {
423                 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
424                 p_es->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
425                 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
426                 sprintf( p_es->psz_fmtp,
427                         "profile-level-id=3; config=%s;", p_hexa );
428                 free( p_hexa );
429             }
430             break;
431         case VLC_FOURCC( 'm', 'p', '4', 'a' ):
432             p_es->i_payload_type = p_media->i_payload_type++;
433             p_es->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
434             sprintf( p_es->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
435             if( p_fmt->i_extra > 0 )
436             {
437                 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
438                 p_es->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
439                 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
440                 sprintf( p_es->psz_fmtp,
441                         "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
442                         "config=%s; SizeLength=13;IndexLength=3; "
443                         "IndexDeltaLength=3; Profile=1;", p_hexa );
444                 free( p_hexa );
445             }
446             break;
447         case VLC_FOURCC( 'm', 'p', '2', 't' ):
448             p_media->psz_mux = "ts";
449             p_es->i_payload_type = 33;
450             p_es->psz_rtpmap = strdup( "MP2T/90000" );
451             break;
452         case VLC_FOURCC( 'm', 'p', '2', 'p' ):
453             p_media->psz_mux = "ps";
454             p_es->i_payload_type = p_media->i_payload_type++;
455             p_es->psz_rtpmap = strdup( "MP2P/90000" );
456             break;
457         case VLC_FOURCC( 's', 'a', 'm', 'r' ):
458             p_es->i_payload_type = p_media->i_payload_type++;
459             p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
460                                     "AMR/8000/2" : "AMR/8000" );
461             p_es->psz_fmtp = strdup( "octet-align=1" );
462             break;
463         case VLC_FOURCC( 's', 'a', 'w', 'b' ):
464             p_es->i_payload_type = p_media->i_payload_type++;
465             p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
466                                     "AMR-WB/16000/2" : "AMR-WB/16000" );
467             p_es->psz_fmtp = strdup( "octet-align=1" );
468             break;
469
470         default:
471             msg_Err( p_vod, "cannot add this stream (unsupported "
472                     "codec: %4.4s)", (char*)&p_fmt->i_codec );
473             free( p_es );
474             return VLC_EGENERIC;
475     }
476
477     p_es->p_rtsp_url =
478         httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, NULL, NULL,
479                             NULL );
480
481     if( !p_es->p_rtsp_url )
482     {
483         msg_Err( p_vod, "cannot create http url (%s)", psz_urlc );
484         free( psz_urlc );
485         free( p_es );
486         return VLC_EGENERIC;
487     }
488     free( psz_urlc );
489
490     httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
491                     RtspCallbackES, (void*)p_es );
492     httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_TEARDOWN,
493                     RtspCallbackES, (void*)p_es );
494     httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PLAY,
495                     RtspCallbackES, (void*)p_es );
496     httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PAUSE,
497                     RtspCallbackES, (void*)p_es );
498
499     es_format_Copy( &p_es->fmt, p_fmt );
500     p_es->p_vod = p_vod;
501     p_es->p_media = p_media;
502
503 #if 0
504     /* Choose the port */
505     if( p_fmt->i_cat == AUDIO_ES && p_media->i_port_audio > 0 )
506     {
507         p_es->i_port = p_media->i_port_audio;
508         p_media->i_port_audio = 0;
509     }
510     else if( p_fmt->i_cat == VIDEO_ES && p_media->i_port_video > 0 )
511     {
512         p_es->i_port = p_media->i_port_video;
513         p_media->i_port_video = 0;
514     }
515     while( !p_es->i_port )
516     {
517         if( p_media->i_port != p_media->i_port_audio &&
518             p_media->i_port != p_media->i_port_video )
519         {
520             p_es->i_port = p_media->i_port;
521             p_media->i_port += 2;
522             break;
523         }
524         p_media->i_port += 2;
525     }
526 #else
527
528     p_es->i_port = 0;
529 #endif
530
531     vlc_mutex_lock( &p_media->lock );
532     TAB_APPEND( p_media->i_es, p_media->es, p_es );
533     vlc_mutex_unlock( &p_media->lock );
534
535     p_media->i_sdp_version++;
536
537     return VLC_SUCCESS;
538 }
539
540 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
541 {
542     media_es_t *p_es = 0;
543     int i;
544
545     /* Find the ES */
546     for( i = 0; i < p_media->i_es; i++ )
547     {
548         if( p_media->es[i]->fmt.i_cat == p_fmt->i_cat &&
549             p_media->es[i]->fmt.i_codec == p_fmt->i_codec &&
550             p_media->es[i]->fmt.i_id == p_fmt->i_id )
551         {
552             p_es = p_media->es[i];
553         }
554     }
555     if( !p_es ) return;
556
557     msg_Dbg( p_vod, "  - Removing ES %4.4s", (char *)&p_fmt->i_codec );
558
559     vlc_mutex_lock( &p_media->lock );
560     TAB_REMOVE( p_media->i_es, p_media->es, p_es );
561     vlc_mutex_unlock( &p_media->lock );
562
563     if( p_es->psz_rtpmap ) free( p_es->psz_rtpmap );
564     if( p_es->psz_fmtp ) free( p_es->psz_fmtp );
565     p_media->i_sdp_version++;
566
567     if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
568     es_format_Clean( &p_es->fmt );
569 }
570
571 /****************************************************************************
572  * RTSP server implementation
573  ****************************************************************************/
574 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
575 {
576     rtsp_client_t *p_rtsp = malloc( sizeof(rtsp_client_t) );
577     memset( p_rtsp, 0, sizeof(rtsp_client_t) );
578     p_rtsp->es = 0;
579
580     p_rtsp->psz_session = psz_session;
581     TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
582
583     msg_Dbg( p_media->p_vod, "new session: %s", psz_session );
584
585     return p_rtsp;
586 }
587
588 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, char *psz_session )
589 {
590     int i;
591
592     for( i = 0; psz_session && i < p_media->i_rtsp; i++ )
593     {
594         if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
595         {
596             return p_media->rtsp[i];
597         }
598     }
599
600     return NULL;
601 }
602
603 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
604 {
605     msg_Dbg( p_media->p_vod, "closing session: %s", p_rtsp->psz_session );
606
607     while( p_rtsp->i_es-- )
608     {
609         if( p_rtsp->es[p_rtsp->i_es]->psz_ip )
610             free( p_rtsp->es[p_rtsp->i_es]->psz_ip );
611         free( p_rtsp->es[p_rtsp->i_es] );
612         if( !p_rtsp->i_es ) free( p_rtsp->es );
613     }
614
615     TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, p_rtsp );
616
617     free( p_rtsp->psz_session );
618     free( p_rtsp );
619 }
620
621 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
622                          httpd_message_t *answer, httpd_message_t *query )
623 {
624     vod_media_t *p_media = (vod_media_t*)p_args;
625     vod_t *p_vod = p_media->p_vod;
626     char *psz_transport = NULL;
627     char *psz_playnow = NULL; /* support option: x-playNow */
628     char *psz_session = NULL;
629     rtsp_client_t *p_rtsp;
630     int i_port = 0;
631
632     if( answer == NULL || query == NULL ) return VLC_SUCCESS;
633
634     msg_Info( p_vod, "RtspCallback query: type=%d", query->i_type );
635
636     answer->i_proto   = HTTPD_PROTO_RTSP;
637     answer->i_version = query->i_version;
638     answer->i_type    = HTTPD_MSG_ANSWER;
639
640     switch( query->i_type )
641     {
642         case HTTPD_MSG_SETUP:
643         {
644             psz_playnow = httpd_MsgGet( query, "x-playNow" );
645             psz_transport = httpd_MsgGet( query, "Transport" );
646             msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
647
648             if( strstr( psz_transport, "unicast" ) &&
649                 strstr( psz_transport, "client_port=" ) )
650             {
651                 rtsp_client_t *p_rtsp;
652                 char ip[NI_MAXNUMERICHOST];
653                 i_port = atoi( strstr( psz_transport, "client_port=" ) +
654                                 strlen("client_port=") );
655
656                 if( strstr( psz_transport, "MP2T/H2221/UDP" ) ||
657                     strstr( psz_transport, "RAW/RAW/UDP" ) )
658                 {
659                     p_media->b_raw = 1;
660                 }
661                 if( httpd_ClientIP( cl, ip ) == NULL )
662                 {
663                     answer->i_status = 500;
664                     answer->psz_status = strdup( "Internal server error" );
665                     answer->i_body = 0;
666                     answer->p_body = NULL;
667                     break;
668                 }
669
670                 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
671                         ip, i_port );
672
673                 psz_session = httpd_MsgGet( query, "Session" );
674                 if( !psz_session || !*psz_session )
675                 {
676                     asprintf( &psz_session, "%d", rand() );
677                     p_rtsp = RtspClientNew( p_media, psz_session );
678                 }
679                 else
680                 {
681                     p_rtsp = RtspClientGet( p_media, psz_session );
682                     if( !p_rtsp )
683                     {
684                         /* FIXME right error code */
685                         answer->i_status = 454;
686                         answer->psz_status = strdup( "Unknown session id" );
687                         answer->i_body = 0;
688                         answer->p_body = NULL;
689                         break;
690                     }
691                 }
692
693                 answer->i_status = 200;
694                 answer->psz_status = strdup( "OK" );
695                 answer->i_body = 0;
696                 answer->p_body = NULL;
697
698                 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
699                             i_port, i_port + 1 );
700             }
701             else /* TODO  strstr( psz_transport, "interleaved" ) ) */
702             {
703                 answer->i_status = 461;
704                 answer->psz_status = strdup( "Unsupported Transport" );
705                 answer->i_body = 0;
706                 answer->p_body = NULL;
707             }
708             if( !psz_playnow )
709                 break;
710         }
711
712         case HTTPD_MSG_PLAY:
713         {
714             char *psz_output, ip[NI_MAXNUMERICHOST];
715             int i, i_port_audio = 0, i_port_video = 0;
716
717             /* for now only multicast so easy */
718             answer->i_status = 200;
719             answer->psz_status = strdup( "OK" );
720             answer->i_body = 0;
721             answer->p_body = NULL;
722
723             if( !psz_session )
724                 psz_session = httpd_MsgGet( query, "Session" );
725             msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
726
727             p_rtsp = RtspClientGet( p_media, psz_session );
728             if( !p_rtsp ) break;
729
730             if( p_rtsp->b_playing && p_rtsp->b_paused )
731             {
732                 vod_MediaControl( p_vod, p_media, psz_session,
733                                   VOD_MEDIA_PAUSE );
734                 p_rtsp->b_paused = VLC_FALSE;
735                 break;
736             }
737             else if( p_rtsp->b_playing ) break;
738
739             if( httpd_ClientIP( cl, ip ) == NULL ) break;
740
741             p_rtsp->b_playing = VLC_TRUE;
742
743             /* FIXME for != 1 video and 1 audio */
744             for( i = 0; i < p_rtsp->i_es; i++ )
745             {
746                 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == AUDIO_ES )
747                     i_port_audio = p_rtsp->es[i]->i_port;
748                 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == VIDEO_ES )
749                     i_port_video = p_rtsp->es[i]->i_port;
750             }
751
752             if( p_media->psz_mux )
753             {
754                 if( p_media->b_raw )
755                 {
756                     asprintf( &psz_output, "std{access=udp,dst=%s:%i,mux=%s}",
757                               ip, i_port, p_media->psz_mux );
758                 }
759                 else
760                 {
761                     asprintf( &psz_output, "rtp{dst=%s,port=%i,mux=%s}",
762                               ip, i_port_video, p_media->psz_mux );
763                 }
764             }
765             else
766             {
767                 asprintf( &psz_output, "rtp{dst=%s,port-video=%i,"
768                           "port-audio=%i}", ip, i_port_video, i_port_audio );
769             }
770
771             vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PLAY,
772                               psz_output );
773             free( psz_output );
774             break;
775         }
776
777         case HTTPD_MSG_DESCRIBE:
778         {
779             char *psz_sdp =
780                 SDPGenerate( p_media, cl );
781
782             if( psz_sdp != NULL )
783             {
784                 answer->i_status = 200;
785                 answer->psz_status = strdup( "OK" );
786                 httpd_MsgAdd( answer, "Content-type",  "%s", "application/sdp" );
787
788                 answer->p_body = (uint8_t *)psz_sdp;
789                 answer->i_body = strlen( psz_sdp );
790             }
791             else
792             {
793                 answer->i_status = 500;
794                 answer->psz_status = strdup( "Internal server error" );
795                 answer->p_body = NULL;
796                 answer->i_body = 0;
797             }
798             break;
799         }
800
801         case HTTPD_MSG_PAUSE:
802             psz_session = httpd_MsgGet( query, "Session" );
803             msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
804
805             p_rtsp = RtspClientGet( p_media, psz_session );
806             if( !p_rtsp ) break;
807
808             vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
809             p_rtsp->b_paused = VLC_TRUE;
810
811             answer->i_status = 200;
812             answer->psz_status = strdup( "OK" );
813             answer->i_body = 0;
814             answer->p_body = NULL;
815             break;
816
817         case HTTPD_MSG_TEARDOWN:
818             /* for now only multicast so easy again */
819             answer->i_status = 200;
820             answer->psz_status = strdup( "OK" );
821             answer->i_body = 0;
822             answer->p_body = NULL;
823
824             psz_session = httpd_MsgGet( query, "Session" );
825             msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
826
827             p_rtsp = RtspClientGet( p_media, psz_session );
828             if( !p_rtsp ) break;
829
830             vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_STOP );
831             RtspClientDel( p_media, p_rtsp );
832             break;
833
834         default:
835             return VLC_EGENERIC;
836     }
837
838     httpd_MsgAdd( answer, "Server", "VLC Server" );
839     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
840     httpd_MsgAdd( answer, "Cseq", "%d",
841                   atoi( httpd_MsgGet( query, "Cseq" ) ) );
842     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
843
844     if( psz_session )
845     {
846         httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
847     }
848
849     return VLC_SUCCESS;
850 }
851
852 static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
853                            httpd_message_t *answer, httpd_message_t *query )
854 {
855     media_es_t *p_es = (media_es_t*)p_args;
856     vod_media_t *p_media = p_es->p_media;
857     vod_t *p_vod = p_media->p_vod;
858     rtsp_client_t *p_rtsp = NULL;
859     char *psz_transport = NULL;
860     char *psz_playnow = NULL; /* support option: x-playNow */
861     char *psz_session = NULL;
862     char *psz_position = NULL;
863     int i;
864
865     if( answer == NULL || query == NULL ) return VLC_SUCCESS;
866
867     msg_Info( p_vod, "RtspCallback query: type=%d", query->i_type );
868
869     answer->i_proto   = HTTPD_PROTO_RTSP;
870     answer->i_version = query->i_version;
871     answer->i_type    = HTTPD_MSG_ANSWER;
872
873     switch( query->i_type )
874     {
875         case HTTPD_MSG_SETUP:
876             psz_playnow = httpd_MsgGet( query, "x-playNow" );
877             psz_transport = httpd_MsgGet( query, "Transport" );
878
879             msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
880
881             if( strstr( psz_transport, "unicast" ) &&
882                 strstr( psz_transport, "client_port=" ) )
883             {
884                 rtsp_client_t *p_rtsp;
885                 rtsp_client_es_t *p_rtsp_es;
886                 char ip[NI_MAXNUMERICHOST];
887                 int i_port = atoi( strstr( psz_transport, "client_port=" ) +
888                                 strlen("client_port=") );
889
890                 if( httpd_ClientIP( cl, ip ) == NULL )
891                 {
892                     answer->i_status = 500;
893                     answer->psz_status = strdup( "Internal server error" );
894                     answer->i_body = 0;
895                     answer->p_body = NULL;
896                     break;
897                 }
898
899                 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
900                         ip, i_port );
901
902                 psz_session = httpd_MsgGet( query, "Session" );
903                 if( !psz_session || !*psz_session )
904                 {
905                     asprintf( &psz_session, "%d", rand() );
906                     p_rtsp = RtspClientNew( p_media, psz_session );
907                 }
908                 else
909                 {
910                     p_rtsp = RtspClientGet( p_media, psz_session );
911                     if( !p_rtsp )
912                     {
913                         /* FIXME right error code */
914                         answer->i_status = 454;
915                         answer->psz_status = strdup( "Unknown session id" );
916                         answer->i_body = 0;
917                         answer->p_body = NULL;
918                         break;
919                     }
920                 }
921
922                 p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
923                 p_rtsp_es->i_port = i_port;
924                 p_rtsp_es->psz_ip = strdup( ip );
925                 p_rtsp_es->p_media_es = p_es;
926                 TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );
927
928                 answer->i_status = 200;
929                 answer->psz_status = strdup( "OK" );
930                 answer->i_body = 0;
931                 answer->p_body = NULL;
932
933                 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
934                             i_port, i_port + 1 );
935             }
936             else /* TODO  strstr( psz_transport, "interleaved" ) ) */
937             {
938                 answer->i_status = 461;
939                 answer->psz_status = strdup( "Unsupported Transport" );
940                 answer->i_body = 0;
941                 answer->p_body = NULL;
942             }
943             if( !psz_playnow )
944                 break;
945
946         case HTTPD_MSG_PLAY:
947             /* This is kind of a kludge. Should we only support Aggregate
948              * Operations ? */
949             psz_session = httpd_MsgGet( query, "Session" );
950             msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
951
952             p_rtsp = RtspClientGet( p_media, psz_session );
953
954             psz_position = httpd_MsgGet( query, "Range" );
955             if( psz_position ) psz_position = strstr( psz_position, "npt=" );
956             if( psz_position )
957             {
958                 float f_pos;
959
960                 msg_Dbg( p_vod, "seeking request: %s", psz_position );
961
962                 psz_position += 4;
963                 if( sscanf( psz_position, "%f", &f_pos ) == 1 )
964                 {
965                     f_pos /= ((float)(p_media->i_length/1000))/1000 / 100;
966                     vod_MediaControl( p_vod, p_media, psz_session,
967                                       VOD_MEDIA_SEEK, (double)f_pos );
968                 }
969             }
970
971             answer->i_status = 200;
972             answer->psz_status = strdup( "OK" );
973             answer->i_body = 0;
974             answer->p_body = NULL;
975             break;
976
977         case HTTPD_MSG_TEARDOWN:
978             answer->i_status = 200;
979             answer->psz_status = strdup( "OK" );
980             answer->i_body = 0;
981             answer->p_body = NULL;
982
983             psz_session = httpd_MsgGet( query, "Session" );
984             msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
985
986             p_rtsp = RtspClientGet( p_media, psz_session );
987             if( !p_rtsp ) break;
988
989             for( i = 0; i < p_rtsp->i_es; i++ )
990             {
991                 if( p_rtsp->es[i]->p_media_es == p_es )
992                 {
993                     if( p_rtsp->es[i]->psz_ip ) free( p_rtsp->es[i]->psz_ip );
994                     TAB_REMOVE( p_rtsp->i_es, p_rtsp->es, p_rtsp->es[i] );
995                     break;
996                 }
997             }
998
999             if( !p_rtsp->i_es )
1000             {
1001                 vod_MediaControl( p_vod, p_media, psz_session,
1002                                   VOD_MEDIA_STOP );
1003                 RtspClientDel( p_media, p_rtsp );
1004             }
1005             break;
1006
1007         case HTTPD_MSG_PAUSE:
1008             /* This is kind of a kludge. Should we only support Aggregate
1009              * Operations ? */
1010             psz_session = httpd_MsgGet( query, "Session" );
1011             msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1012
1013             p_rtsp = RtspClientGet( p_media, psz_session );
1014             if( !p_rtsp ) break;
1015
1016             vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
1017             p_rtsp->b_paused = VLC_TRUE;
1018
1019             answer->i_status = 200;
1020             answer->psz_status = strdup( "OK" );
1021             answer->i_body = 0;
1022             answer->p_body = NULL;
1023             break;
1024
1025         default:
1026             return VLC_EGENERIC;
1027             break;
1028     }
1029
1030     httpd_MsgAdd( answer, "Server", "VLC Server" );
1031     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1032     httpd_MsgAdd( answer, "Cseq", "%d",
1033                   atoi( httpd_MsgGet( query, "Cseq" ) ) );
1034     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1035
1036     if( psz_session )
1037         httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1038
1039     return VLC_SUCCESS;
1040 }
1041
1042 /*****************************************************************************
1043  * SDPGenerate: TODO
1044  * FIXME: need to be moved to a common place ?
1045  *****************************************************************************/
1046 static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
1047 {
1048     int i, i_size;
1049     char *p, *psz_sdp, ip[NI_MAXNUMERICHOST], ipv;
1050     const char *psz_control;
1051
1052     if( httpd_ServerIP( cl, ip ) == NULL )
1053         return NULL;
1054
1055     p = strchr( ip, '%' );
1056     if( p != NULL )
1057         *p = '\0'; /* remove scope if present */
1058
1059     ipv = ( strchr( ip, ':' ) != NULL ) ? '6' : '4';
1060
1061     /* Calculate size */
1062     i_size = sizeof( "v=0\r\n" ) +
1063         sizeof( "o=- * * IN IP4 \r\n" ) + 10 + NI_MAXNUMERICHOST +
1064         sizeof( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
1065         sizeof( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
1066         sizeof( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
1067         sizeof( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
1068         sizeof( "t=0 0\r\n" ) + /* FIXME */
1069         sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
1070         sizeof( "c=IN IP4 0.0.0.0\r\n" ) + 20 + 10 +
1071         sizeof( "a=range:npt=0-1000000000.000\r\n" );
1072
1073     psz_control = (ipv == '6') ? p_media->psz_rtsp_control_v6
1074                                : p_media->psz_rtsp_control_v4;
1075     for( i = 0; i < p_media->i_es; i++ )
1076     {
1077         media_es_t *p_es = p_media->es[i];
1078
1079         i_size += sizeof( "m=**d*o * RTP/AVP *\r\n" ) + 19;
1080         if( p_es->psz_rtpmap )
1081         {
1082             i_size += sizeof( "a=rtpmap:* *\r\n" ) +
1083                 strlen( p_es->psz_rtpmap ) + 9;
1084         }
1085         if( p_es->psz_fmtp )
1086         {
1087             i_size += sizeof( "a=fmtp:* *\r\n" ) +
1088                 strlen( p_es->psz_fmtp ) + 9;
1089         }
1090     }
1091     i_size += (strlen( psz_control ) + strlen( ip ) + 9) * p_media->i_es;
1092
1093     p = psz_sdp = malloc( i_size );
1094     p += sprintf( p, "v=0\r\n" );
1095     p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
1096                   p_media->i_sdp_id, p_media->i_sdp_version, ipv, ip );
1097     if( *p_media->psz_session_name )
1098         p += sprintf( p, "s=%s\r\n", p_media->psz_session_name );
1099     if( *p_media->psz_session_description )
1100         p += sprintf( p, "i=%s\r\n", p_media->psz_session_description );
1101     if( *p_media->psz_session_url )
1102         p += sprintf( p, "u=%s\r\n", p_media->psz_session_url );
1103     if( *p_media->psz_session_email )
1104         p += sprintf( p, "e=%s\r\n", p_media->psz_session_email );
1105
1106     p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
1107     p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
1108
1109     p += sprintf( p, "c=IN IP%c %s\r\n", ipv, ipv == '6' ? "::" : "0.0.0.0" );
1110
1111     if( p_media->i_length > 0 )
1112     p += sprintf( p, "a=range:npt=0-%.3f\r\n",
1113                   ((float)(p_media->i_length/1000))/1000 );
1114
1115     for( i = 0; i < p_media->i_es; i++ )
1116     {
1117         media_es_t *p_es = p_media->es[i];
1118
1119         if( p_es->fmt.i_cat == AUDIO_ES )
1120         {
1121             p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
1122                           p_es->i_port, p_es->i_payload_type );
1123         }
1124         else if( p_es->fmt.i_cat == VIDEO_ES )
1125         {
1126             p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
1127                           p_es->i_port, p_es->i_payload_type );
1128         }
1129         else
1130         {
1131             continue;
1132         }
1133
1134         if( p_es->psz_rtpmap )
1135         {
1136             p += sprintf( p, "a=rtpmap:%d %s\r\n", p_es->i_payload_type,
1137                           p_es->psz_rtpmap );
1138         }
1139         if( p_es->psz_fmtp )
1140         {
1141             p += sprintf( p, "a=fmtp:%d %s\r\n", p_es->i_payload_type,
1142                           p_es->psz_fmtp );
1143         }
1144
1145         p += sprintf( p, psz_control, ip, i );
1146     }
1147
1148     return psz_sdp;
1149 }