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