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