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