1 /*****************************************************************************
2 * rtsp.c: rtsp VoD server module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc/input.h>
36 #include "vlc_httpd.h"
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Open ( vlc_object_t * );
44 static void Close( vlc_object_t * );
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." )
52 #define THROTLE_TEXT N_( "Maximum number of connections" )
53 #define THROTLE_LONGTEXT N_( "Limit the number of connections " \
54 "to a maximum. (0 = unlimited, N = maximum clients)" )
57 set_shortname( _("RTSP VoD" ) );
58 set_description( _("RTSP VoD server") );
59 set_category( CAT_SOUT );
60 set_subcategory( SUBCAT_SOUT_VOD );
61 set_capability( "vod server", 1 );
62 set_callbacks( Open, Close );
63 add_shortcut( "rtsp" );
64 add_string ( "rtsp-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
65 add_integer( "rtsp-throtle-users", 0, NULL, THROTLE_TEXT, THROTLE_LONGTEXT, VLC_TRUE );
68 /*****************************************************************************
70 *****************************************************************************/
72 typedef struct media_es_t media_es_t;
76 media_es_t *p_media_es;
85 int64_t i_last; /* for timeout */
87 vlc_bool_t b_playing; /* is it in "play" state */
88 vlc_bool_t b_paused; /* is it in "pause" state */
91 rtsp_client_es_t **es;
101 httpd_url_t *p_rtsp_url;
103 vod_media_t *p_media;
107 uint8_t i_payload_type;
119 httpd_url_t *p_rtsp_url;
120 char *psz_rtsp_control_v4;
121 char *psz_rtsp_control_v6;
133 vlc_bool_t b_multicast;
145 rtsp_client_t **rtsp;
148 char *psz_session_name;
149 char *psz_session_description;
150 char *psz_session_url;
151 char *psz_session_email;
158 httpd_host_t *p_rtsp_host;
169 static vod_media_t *MediaNew( vod_t *, const char *, input_item_t * );
170 static void MediaDel( vod_t *, vod_media_t * );
171 static int MediaAddES( vod_t *, vod_media_t *, es_format_t * );
172 static void MediaDelES( vod_t *, vod_media_t *, es_format_t * );
174 static rtsp_client_t *RtspClientNew( vod_media_t *, char * );
175 static rtsp_client_t *RtspClientGet( vod_media_t *, char * );
176 static void RtspClientDel( vod_media_t *, rtsp_client_t * );
178 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
179 httpd_message_t *, httpd_message_t * );
180 static int RtspCallbackES( httpd_callback_sys_t *, httpd_client_t *,
181 httpd_message_t *, httpd_message_t * );
183 static char *SDPGenerate( const vod_media_t *, httpd_client_t *cl );
185 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
187 static const char hex[16] = "0123456789abcdef";
190 for( i = 0; i < i_data; i++ )
192 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
193 s[2*i+1] = hex[(p_data[i] )&0xf];
198 /*****************************************************************************
199 * Open: Starts the RTSP server module
200 *****************************************************************************/
201 static int Open( vlc_object_t *p_this )
203 vod_t *p_vod = (vod_t *)p_this;
204 vod_sys_t *p_sys = 0;
208 psz_url = config_GetPsz( p_vod, "rtsp-host" );
209 vlc_UrlParse( &url, psz_url, 0 );
210 if( psz_url ) free( psz_url );
212 if( url.i_port <= 0 ) url.i_port = 554;
214 p_vod->p_sys = p_sys = malloc( sizeof( vod_sys_t ) );
215 if( !p_sys ) goto error;
216 p_sys->p_rtsp_host = 0;
218 var_Create( p_this, "rtsp-throtle-users", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
219 p_sys->i_throtle_users = var_GetInteger( p_this, "rtsp-throtle-users" );
220 msg_Dbg( p_this, "Allowing up to %d connections", p_sys->i_throtle_users );
221 p_sys->i_connections = 0;
224 httpd_HostNew( VLC_OBJECT(p_vod), url.psz_host, url.i_port );
225 if( !p_sys->p_rtsp_host )
227 msg_Err( p_vod, "cannot create http server (%s:%i)",
228 url.psz_host, url.i_port );
232 p_sys->psz_path = strdup( url.psz_path ? url.psz_path : "/" );
233 p_sys->i_port = url.i_port;
235 vlc_UrlClean( &url );
239 p_vod->pf_media_new = MediaNew;
240 p_vod->pf_media_del = MediaDel;
241 p_vod->pf_media_add_es = MediaAddES;
242 p_vod->pf_media_del_es = MediaDelES;
247 if( p_sys && p_sys->p_rtsp_host ) httpd_HostDelete( p_sys->p_rtsp_host );
248 if( p_sys ) free( p_sys );
249 vlc_UrlClean( &url );
254 /*****************************************************************************
256 *****************************************************************************/
257 static void Close( vlc_object_t * p_this )
259 vod_t *p_vod = (vod_t *)p_this;
260 vod_sys_t *p_sys = p_vod->p_sys;
262 httpd_HostDelete( p_sys->p_rtsp_host );
263 var_Destroy( p_this, "rtsp-throtle-users" );
265 /* TODO delete medias */
266 free( p_sys->psz_path );
270 /*****************************************************************************
272 *****************************************************************************/
273 static vod_media_t *MediaNew( vod_t *p_vod, const char *psz_name,
274 input_item_t *p_item )
276 vod_sys_t *p_sys = p_vod->p_sys;
277 vod_media_t *p_media = malloc( sizeof(vod_media_t) );
282 msg_Err( p_vod, "not enough memory" );
286 memset( p_media, 0, sizeof(vod_media_t) );
288 p_media->psz_mux = 0;
290 p_media->b_raw = VLC_FALSE;
292 asprintf( &p_media->psz_rtsp_path, "%s%s", p_sys->psz_path, psz_name );
293 p_media->p_rtsp_url =
294 httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, NULL,
297 if( !p_media->p_rtsp_url )
299 msg_Err( p_vod, "cannot create http url (%s)", p_media->psz_rtsp_path);
300 free( p_media->psz_rtsp_path );
305 msg_Dbg( p_vod, "created rtsp url: %s", p_media->psz_rtsp_path );
307 asprintf( &p_media->psz_rtsp_control_v4,
308 "a=control:rtsp://%%s:%d%s/trackid=%%d\r\n",
309 p_sys->i_port, p_media->psz_rtsp_path );
310 asprintf( &p_media->psz_rtsp_control_v6,
311 "a=control:rtsp://[%%s]:%d%s/trackid=%%d\r\n",
312 p_sys->i_port, p_media->psz_rtsp_path );
314 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_SETUP,
315 RtspCallback, (void*)p_media );
316 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_DESCRIBE,
317 RtspCallback, (void*)p_media );
318 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PLAY,
319 RtspCallback, (void*)p_media );
320 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PAUSE,
321 RtspCallback, (void*)p_media );
322 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_TEARDOWN,
323 RtspCallback, (void*)p_media );
325 p_media->p_vod = p_vod;
327 TAB_APPEND( p_sys->i_media, p_sys->media, p_media );
329 vlc_mutex_init( p_vod, &p_media->lock );
330 p_media->psz_session_name = strdup("");
331 p_media->psz_session_description = strdup("");
332 p_media->psz_session_url = strdup("");
333 p_media->psz_session_email = strdup("");
335 p_media->i_port_audio = 1234;
336 p_media->i_port_video = 1236;
337 p_media->i_port = 1238;
338 p_media->i_payload_type = 96;
340 p_media->i_sdp_id = mdate();
341 p_media->i_sdp_version = 1;
342 p_media->i_length = p_item->i_duration;
344 vlc_mutex_lock( &p_item->lock );
345 msg_Dbg( p_vod, "media has %i declared ES", p_item->i_es );
346 for( i = 0; i < p_item->i_es; i++ )
348 MediaAddES( p_vod, p_media, p_item->es[i] );
350 vlc_mutex_unlock( &p_item->lock );
355 static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
357 vod_sys_t *p_sys = p_vod->p_sys;
359 msg_Dbg( p_vod, "deleting media: %s", p_media->psz_rtsp_path );
361 while( p_media->i_rtsp > 0 ) RtspClientDel( p_media, p_media->rtsp[0] );
362 httpd_UrlDelete( p_media->p_rtsp_url );
363 if( p_media->psz_rtsp_path ) free( p_media->psz_rtsp_path );
364 if( p_media->psz_rtsp_control_v6 ) free( p_media->psz_rtsp_control_v6 );
365 if( p_media->psz_rtsp_control_v4 ) free( p_media->psz_rtsp_control_v4 );
367 TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
369 while( p_media->i_es ) MediaDelES( p_vod, p_media, &p_media->es[0]->fmt );
371 vlc_mutex_destroy( &p_media->lock );
372 free( p_media->psz_session_name );
373 free( p_media->psz_session_description );
374 free( p_media->psz_session_url );
375 free( p_media->psz_session_email );
379 static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
381 media_es_t *p_es = malloc( sizeof(media_es_t) );
384 memset( p_es, 0, sizeof(media_es_t) );
385 p_media->psz_mux = NULL;
387 /* TODO: update SDP, etc... */
388 asprintf( &psz_urlc, "%s/trackid=%d",
389 p_media->psz_rtsp_path, p_media->i_es );
390 msg_Dbg( p_vod, " - ES %4.4s (%s)", (char *)&p_fmt->i_codec, psz_urlc );
392 switch( p_fmt->i_codec )
394 case VLC_FOURCC( 's', '1', '6', 'b' ):
395 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
397 p_es->i_payload_type = 11;
399 else if( p_fmt->audio.i_channels == 2 && p_fmt->audio.i_rate == 44100 )
401 p_es->i_payload_type = 10;
405 p_es->i_payload_type = p_media->i_payload_type++;
407 p_es->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
408 sprintf( p_es->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
409 p_fmt->audio.i_channels );
411 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
412 p_es->i_payload_type = p_media->i_payload_type++;
413 p_es->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
414 sprintf( p_es->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
415 p_fmt->audio.i_channels );
417 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
418 p_es->i_payload_type = 14;
419 p_es->psz_rtpmap = strdup( "MPA/90000" );
421 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
422 p_es->i_payload_type = 32;
423 p_es->psz_rtpmap = strdup( "MPV/90000" );
425 case VLC_FOURCC( 'a', '5', '2', ' ' ):
426 p_es->i_payload_type = p_media->i_payload_type++;
427 p_es->psz_rtpmap = strdup( "ac3/90000" );
429 case VLC_FOURCC( 'H', '2', '6', '3' ):
430 p_es->i_payload_type = p_media->i_payload_type++;
431 p_es->psz_rtpmap = strdup( "H263-1998/90000" );
433 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
434 p_es->i_payload_type = p_media->i_payload_type++;
435 p_es->psz_rtpmap = strdup( "MP4V-ES/90000" );
436 if( p_fmt->i_extra > 0 )
438 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
439 p_es->psz_fmtp = malloc( 100 + 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 "profile-level-id=3; config=%s;", p_hexa );
446 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
447 p_es->i_payload_type = p_media->i_payload_type++;
448 p_es->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
449 sprintf( p_es->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
450 if( p_fmt->i_extra > 0 )
452 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
453 p_es->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
454 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
455 sprintf( p_es->psz_fmtp,
456 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
457 "config=%s; SizeLength=13;IndexLength=3; "
458 "IndexDeltaLength=3; Profile=1;", p_hexa );
462 case VLC_FOURCC( 'm', 'p', '2', 't' ):
463 p_media->psz_mux = "ts";
464 p_es->i_payload_type = 33;
465 p_es->psz_rtpmap = strdup( "MP2T/90000" );
467 case VLC_FOURCC( 'm', 'p', '2', 'p' ):
468 p_media->psz_mux = "ps";
469 p_es->i_payload_type = p_media->i_payload_type++;
470 p_es->psz_rtpmap = strdup( "MP2P/90000" );
472 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
473 p_es->i_payload_type = p_media->i_payload_type++;
474 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
475 "AMR/8000/2" : "AMR/8000" );
476 p_es->psz_fmtp = strdup( "octet-align=1" );
478 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
479 p_es->i_payload_type = p_media->i_payload_type++;
480 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
481 "AMR-WB/16000/2" : "AMR-WB/16000" );
482 p_es->psz_fmtp = strdup( "octet-align=1" );
486 msg_Err( p_vod, "cannot add this stream (unsupported "
487 "codec: %4.4s)", (char*)&p_fmt->i_codec );
493 httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, NULL, NULL,
496 if( !p_es->p_rtsp_url )
498 msg_Err( p_vod, "cannot create http url (%s)", psz_urlc );
505 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
506 RtspCallbackES, (void*)p_es );
507 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_TEARDOWN,
508 RtspCallbackES, (void*)p_es );
509 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PLAY,
510 RtspCallbackES, (void*)p_es );
511 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PAUSE,
512 RtspCallbackES, (void*)p_es );
514 es_format_Copy( &p_es->fmt, p_fmt );
516 p_es->p_media = p_media;
519 /* Choose the port */
520 if( p_fmt->i_cat == AUDIO_ES && p_media->i_port_audio > 0 )
522 p_es->i_port = p_media->i_port_audio;
523 p_media->i_port_audio = 0;
525 else if( p_fmt->i_cat == VIDEO_ES && p_media->i_port_video > 0 )
527 p_es->i_port = p_media->i_port_video;
528 p_media->i_port_video = 0;
530 while( !p_es->i_port )
532 if( p_media->i_port != p_media->i_port_audio &&
533 p_media->i_port != p_media->i_port_video )
535 p_es->i_port = p_media->i_port;
536 p_media->i_port += 2;
539 p_media->i_port += 2;
546 vlc_mutex_lock( &p_media->lock );
547 TAB_APPEND( p_media->i_es, p_media->es, p_es );
548 vlc_mutex_unlock( &p_media->lock );
550 p_media->i_sdp_version++;
555 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
557 media_es_t *p_es = 0;
561 for( i = 0; i < p_media->i_es; i++ )
563 if( p_media->es[i]->fmt.i_cat == p_fmt->i_cat &&
564 p_media->es[i]->fmt.i_codec == p_fmt->i_codec &&
565 p_media->es[i]->fmt.i_id == p_fmt->i_id )
567 p_es = p_media->es[i];
572 msg_Dbg( p_vod, " - Removing ES %4.4s", (char *)&p_fmt->i_codec );
574 vlc_mutex_lock( &p_media->lock );
575 TAB_REMOVE( p_media->i_es, p_media->es, p_es );
576 vlc_mutex_unlock( &p_media->lock );
578 if( p_es->psz_rtpmap ) free( p_es->psz_rtpmap );
579 if( p_es->psz_fmtp ) free( p_es->psz_fmtp );
580 p_media->i_sdp_version++;
582 if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
583 es_format_Clean( &p_es->fmt );
586 /****************************************************************************
587 * RTSP server implementation
588 ****************************************************************************/
589 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
591 rtsp_client_t *p_rtsp = malloc( sizeof(rtsp_client_t) );
593 if( !p_rtsp ) return NULL;
594 memset( p_rtsp, 0, sizeof(rtsp_client_t) );
597 p_rtsp->psz_session = psz_session;
598 TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
600 p_media->p_vod->p_sys->i_connections++;
601 msg_Dbg( p_media->p_vod, "new session: %s, connections: %d",
602 psz_session, p_media->p_vod->p_sys->i_throtle_users );
607 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, char *psz_session )
611 for( i = 0; psz_session && i < p_media->i_rtsp; i++ )
613 if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
615 return p_media->rtsp[i];
622 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
624 p_media->p_vod->p_sys->i_connections--;
625 msg_Dbg( p_media->p_vod, "closing session: %s, connections: %d",
626 p_rtsp->psz_session, p_media->p_vod->p_sys->i_throtle_users );
628 while( p_rtsp->i_es-- )
630 if( p_rtsp->es[p_rtsp->i_es]->psz_ip )
631 free( p_rtsp->es[p_rtsp->i_es]->psz_ip );
632 free( p_rtsp->es[p_rtsp->i_es] );
633 if( !p_rtsp->i_es ) free( p_rtsp->es );
636 TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, p_rtsp );
638 free( p_rtsp->psz_session );
642 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
643 httpd_message_t *answer, httpd_message_t *query )
645 vod_media_t *p_media = (vod_media_t*)p_args;
646 vod_t *p_vod = p_media->p_vod;
647 char *psz_transport = NULL;
648 char *psz_playnow = NULL; /* support option: x-playNow */
649 char *psz_session = NULL;
650 rtsp_client_t *p_rtsp;
653 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
655 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
657 answer->i_proto = HTTPD_PROTO_RTSP;
658 answer->i_version = query->i_version;
659 answer->i_type = HTTPD_MSG_ANSWER;
661 answer->p_body = NULL;
663 switch( query->i_type )
665 case HTTPD_MSG_SETUP:
667 psz_playnow = httpd_MsgGet( query, "x-playNow" );
668 psz_transport = httpd_MsgGet( query, "Transport" );
669 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
671 if( strstr( psz_transport, "unicast" ) &&
672 strstr( psz_transport, "client_port=" ) )
674 rtsp_client_t *p_rtsp;
675 char ip[NI_MAXNUMERICHOST];
676 i_port = atoi( strstr( psz_transport, "client_port=" ) +
677 strlen("client_port=") );
679 if( strstr( psz_transport, "MP2T/H2221/UDP" ) ||
680 strstr( psz_transport, "RAW/RAW/UDP" ) )
682 p_media->b_raw = VLC_TRUE;
685 if( httpd_ClientIP( cl, ip ) == NULL )
687 answer->i_status = 500;
688 answer->psz_status = strdup( "Internal server error" );
690 answer->p_body = NULL;
694 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
697 psz_session = httpd_MsgGet( query, "Session" );
698 if( !psz_session || !*psz_session )
700 if( ( p_vod->p_sys->i_throtle_users > 0 ) &&
701 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throtle_users ) )
703 answer->i_status = 500; // FIXME: GET THE RIGHT ERROR STATUS
704 answer->psz_status = strdup( "Too many connections" );
706 answer->p_body = NULL;
709 asprintf( &psz_session, "%d", rand() );
710 p_rtsp = RtspClientNew( p_media, psz_session );
714 p_rtsp = RtspClientGet( p_media, psz_session );
717 /* FIXME right error code */
718 answer->i_status = 454;
719 answer->psz_status = strdup( "Unknown session id" );
721 answer->p_body = NULL;
726 answer->i_status = 200;
727 answer->psz_status = strdup( "OK" );
729 answer->p_body = NULL;
733 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
735 httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
736 i_port, i_port + 1 );
738 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
740 httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
741 i_port, i_port + 1 );
745 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
746 i_port, i_port + 1 );
748 else /* TODO strstr( psz_transport, "interleaved" ) ) */
750 answer->i_status = 461;
751 answer->psz_status = strdup( "Unsupported Transport" );
753 answer->p_body = NULL;
756 if( !psz_playnow || !*psz_playnow )
762 char *psz_output, ip[NI_MAXNUMERICHOST];
763 int i, i_port_audio = 0, i_port_video = 0;
765 /* for now only multicast so easy */
768 answer->i_status = 200;
769 answer->psz_status = strdup( "OK" );
771 answer->p_body = NULL;
775 psz_session = httpd_MsgGet( query, "Session" );
776 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
778 p_rtsp = RtspClientGet( p_media, psz_session );
781 if( p_rtsp->b_playing && p_rtsp->b_paused )
783 vod_MediaControl( p_vod, p_media, psz_session,
785 p_rtsp->b_paused = VLC_FALSE;
788 else if( p_rtsp->b_playing ) break;
790 if( httpd_ClientIP( cl, ip ) == NULL ) break;
792 p_rtsp->b_playing = VLC_TRUE;
794 /* FIXME for != 1 video and 1 audio */
795 for( i = 0; i < p_rtsp->i_es; i++ )
797 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == AUDIO_ES )
798 i_port_audio = p_rtsp->es[i]->i_port;
799 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == VIDEO_ES )
800 i_port_video = p_rtsp->es[i]->i_port;
803 if( p_media->psz_mux )
807 asprintf( &psz_output, "std{access=udp,dst=%s:%i,mux=%s}",
808 ip, i_port, p_media->psz_mux );
812 asprintf( &psz_output, "rtp{dst=%s,port=%i,mux=%s}",
813 ip, i_port_video, p_media->psz_mux );
818 asprintf( &psz_output, "rtp{dst=%s,port-video=%i,"
819 "port-audio=%i}", ip, i_port_video, i_port_audio );
822 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PLAY,
828 case HTTPD_MSG_DESCRIBE:
831 SDPGenerate( p_media, cl );
833 if( psz_sdp != NULL )
835 answer->i_status = 200;
836 answer->psz_status = strdup( "OK" );
837 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
839 answer->p_body = (uint8_t *)psz_sdp;
840 answer->i_body = strlen( psz_sdp );
844 answer->i_status = 500;
845 answer->psz_status = strdup( "Internal server error" );
846 answer->p_body = NULL;
852 case HTTPD_MSG_PAUSE:
853 psz_session = httpd_MsgGet( query, "Session" );
854 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
856 p_rtsp = RtspClientGet( p_media, psz_session );
859 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
860 p_rtsp->b_paused = VLC_TRUE;
862 answer->i_status = 200;
863 answer->psz_status = strdup( "OK" );
865 answer->p_body = NULL;
868 case HTTPD_MSG_TEARDOWN:
869 /* for now only multicast so easy again */
870 answer->i_status = 200;
871 answer->psz_status = strdup( "OK" );
873 answer->p_body = NULL;
875 psz_session = httpd_MsgGet( query, "Session" );
876 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
878 p_rtsp = RtspClientGet( p_media, psz_session );
881 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_STOP );
882 RtspClientDel( p_media, p_rtsp );
889 httpd_MsgAdd( answer, "Server", "VLC Server" );
890 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
891 httpd_MsgAdd( answer, "Cseq", "%d",
892 atoi( httpd_MsgGet( query, "Cseq" ) ) );
893 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
897 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
903 static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
904 httpd_message_t *answer, httpd_message_t *query )
906 media_es_t *p_es = (media_es_t*)p_args;
907 vod_media_t *p_media = p_es->p_media;
908 vod_t *p_vod = p_media->p_vod;
909 rtsp_client_t *p_rtsp = NULL;
910 char *psz_transport = NULL;
911 char *psz_playnow = NULL; /* support option: x-playNow */
912 char *psz_session = NULL;
913 char *psz_position = NULL;
916 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
918 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
920 answer->i_proto = HTTPD_PROTO_RTSP;
921 answer->i_version = query->i_version;
922 answer->i_type = HTTPD_MSG_ANSWER;
924 answer->p_body = NULL;
926 switch( query->i_type )
928 case HTTPD_MSG_SETUP:
929 psz_playnow = httpd_MsgGet( query, "x-playNow" );
930 psz_transport = httpd_MsgGet( query, "Transport" );
932 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
934 if( strstr( psz_transport, "unicast" ) &&
935 strstr( psz_transport, "client_port=" ) )
937 rtsp_client_t *p_rtsp;
938 rtsp_client_es_t *p_rtsp_es;
939 char ip[NI_MAXNUMERICHOST];
940 int i_port = atoi( strstr( psz_transport, "client_port=" ) +
941 strlen("client_port=") );
943 if( httpd_ClientIP( cl, ip ) == NULL )
945 answer->i_status = 500;
946 answer->psz_status = strdup( "Internal server error" );
948 answer->p_body = NULL;
952 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
955 psz_session = httpd_MsgGet( query, "Session" );
956 if( !psz_session || !*psz_session )
958 if( ( p_vod->p_sys->i_throtle_users > 0 ) &&
959 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throtle_users ) )
961 answer->i_status = 500; // FIXME: GET THE RIGHT ERROR STATUS
962 answer->psz_status = strdup( "Too many connections" );
964 answer->p_body = NULL;
967 asprintf( &psz_session, "%d", rand() );
968 p_rtsp = RtspClientNew( p_media, psz_session );
972 p_rtsp = RtspClientGet( p_media, psz_session );
975 /* FIXME right error code */
976 answer->i_status = 454;
977 answer->psz_status = strdup( "Unknown session id" );
979 answer->p_body = NULL;
984 p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
985 p_rtsp_es->i_port = i_port;
986 p_rtsp_es->psz_ip = strdup( ip );
987 p_rtsp_es->p_media_es = p_es;
988 TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );
990 answer->i_status = 200;
991 answer->psz_status = strdup( "OK" );
993 answer->p_body = NULL;
997 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
999 httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
1000 i_port, i_port + 1 );
1002 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1004 httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
1005 i_port, i_port + 1 );
1010 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
1011 i_port, i_port + 1 );
1014 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1016 answer->i_status = 461;
1017 answer->psz_status = strdup( "Unsupported Transport" );
1019 answer->p_body = NULL;
1022 if( !psz_playnow || !*psz_playnow )
1025 case HTTPD_MSG_PLAY:
1026 /* This is kind of a kludge. Should we only support Aggregate
1028 psz_session = httpd_MsgGet( query, "Session" );
1029 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1031 p_rtsp = RtspClientGet( p_media, psz_session );
1033 psz_position = httpd_MsgGet( query, "Range" );
1034 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
1039 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1042 if( sscanf( psz_position, "%f", &f_pos ) == 1 )
1044 f_pos /= ((float)(p_media->i_length/1000))/1000 / 100;
1045 vod_MediaControl( p_vod, p_media, psz_session,
1046 VOD_MEDIA_SEEK, (double)f_pos );
1052 answer->i_status = 200;
1053 answer->psz_status = strdup( "OK" );
1055 answer->p_body = NULL;
1059 case HTTPD_MSG_TEARDOWN:
1060 answer->i_status = 200;
1061 answer->psz_status = strdup( "OK" );
1063 answer->p_body = NULL;
1065 psz_session = httpd_MsgGet( query, "Session" );
1066 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1068 p_rtsp = RtspClientGet( p_media, psz_session );
1069 if( !p_rtsp ) break;
1071 for( i = 0; i < p_rtsp->i_es; i++ )
1073 if( p_rtsp->es[i]->p_media_es == p_es )
1075 if( p_rtsp->es[i]->psz_ip ) free( p_rtsp->es[i]->psz_ip );
1076 TAB_REMOVE( p_rtsp->i_es, p_rtsp->es, p_rtsp->es[i] );
1083 vod_MediaControl( p_vod, p_media, psz_session,
1085 RtspClientDel( p_media, p_rtsp );
1089 case HTTPD_MSG_PAUSE:
1090 /* This is kind of a kludge. Should we only support Aggregate
1092 psz_session = httpd_MsgGet( query, "Session" );
1093 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1095 p_rtsp = RtspClientGet( p_media, psz_session );
1096 if( !p_rtsp ) break;
1098 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
1099 p_rtsp->b_paused = VLC_TRUE;
1101 answer->i_status = 200;
1102 answer->psz_status = strdup( "OK" );
1104 answer->p_body = NULL;
1108 return VLC_EGENERIC;
1112 httpd_MsgAdd( answer, "Server", "VLC Server" );
1113 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1114 httpd_MsgAdd( answer, "Cseq", "%d",
1115 atoi( httpd_MsgGet( query, "Cseq" ) ) );
1116 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1119 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1124 /*****************************************************************************
1126 * FIXME: need to be moved to a common place ?
1127 *****************************************************************************/
1128 static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
1131 char *p, *psz_sdp, ip[NI_MAXNUMERICHOST], ipv;
1132 const char *psz_control;
1134 if( httpd_ServerIP( cl, ip ) == NULL )
1137 p = strchr( ip, '%' );
1139 *p = '\0'; /* remove scope if present */
1141 ipv = ( strchr( ip, ':' ) != NULL ) ? '6' : '4';
1143 /* Calculate size */
1144 i_size = sizeof( "v=0\r\n" ) +
1145 sizeof( "o=- * * IN IP4 \r\n" ) + 10 + NI_MAXNUMERICHOST +
1146 sizeof( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
1147 sizeof( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
1148 sizeof( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
1149 sizeof( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
1150 sizeof( "t=0 0\r\n" ) + /* FIXME */
1151 sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
1152 sizeof( "c=IN IP4 0.0.0.0\r\n" ) + 20 + 10 +
1153 sizeof( "a=range:npt=0-1000000000.000\r\n" );
1155 psz_control = (ipv == '6') ? p_media->psz_rtsp_control_v6
1156 : p_media->psz_rtsp_control_v4;
1157 for( i = 0; i < p_media->i_es; i++ )
1159 media_es_t *p_es = p_media->es[i];
1161 i_size += sizeof( "m=**d*o * RTP/AVP *\r\n" ) + 19;
1162 if( p_es->psz_rtpmap )
1164 i_size += sizeof( "a=rtpmap:* *\r\n" ) +
1165 strlen( p_es->psz_rtpmap ) + 9;
1167 if( p_es->psz_fmtp )
1169 i_size += sizeof( "a=fmtp:* *\r\n" ) +
1170 strlen( p_es->psz_fmtp ) + 9;
1173 i_size += (strlen( psz_control ) + strlen( ip ) + 9) * p_media->i_es;
1175 p = psz_sdp = malloc( i_size );
1176 p += sprintf( p, "v=0\r\n" );
1177 p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
1178 p_media->i_sdp_id, p_media->i_sdp_version, ipv, ip );
1179 if( *p_media->psz_session_name )
1180 p += sprintf( p, "s=%s\r\n", p_media->psz_session_name );
1181 if( *p_media->psz_session_description )
1182 p += sprintf( p, "i=%s\r\n", p_media->psz_session_description );
1183 if( *p_media->psz_session_url )
1184 p += sprintf( p, "u=%s\r\n", p_media->psz_session_url );
1185 if( *p_media->psz_session_email )
1186 p += sprintf( p, "e=%s\r\n", p_media->psz_session_email );
1188 p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
1189 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
1191 p += sprintf( p, "c=IN IP%c %s\r\n", ipv, ipv == '6' ? "::" : "0.0.0.0" );
1193 if( p_media->i_length > 0 )
1194 p += sprintf( p, "a=range:npt=0-%.3f\r\n",
1195 ((float)(p_media->i_length/1000))/1000 );
1197 for( i = 0; i < p_media->i_es; i++ )
1199 media_es_t *p_es = p_media->es[i];
1201 if( p_es->fmt.i_cat == AUDIO_ES )
1203 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
1204 p_es->i_port, p_es->i_payload_type );
1206 else if( p_es->fmt.i_cat == VIDEO_ES )
1208 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
1209 p_es->i_port, p_es->i_payload_type );
1216 if( p_es->psz_rtpmap )
1218 p += sprintf( p, "a=rtpmap:%d %s\r\n", p_es->i_payload_type,
1221 if( p_es->psz_fmtp )
1223 p += sprintf( p, "a=fmtp:%d %s\r\n", p_es->i_payload_type,
1227 p += sprintf( p, psz_control, ip, i );