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"
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Open ( vlc_object_t * );
45 static void Close( vlc_object_t * );
47 #define HOST_TEXT N_( "Host address" )
48 #define HOST_LONGTEXT N_( \
49 "You can set the address, port and path the rtsp interface will bind to." \
50 "\nSyntax is address:port/path. Default is to bind to any address "\
51 "on port 554, with no path." )
53 #define THROTLE_TEXT N_( "Maximum number of connections" )
54 #define THROTLE_LONGTEXT N_( "Limit the number of connections " \
55 "to a maximum. (0 = unlimited, N = maximum clients)" )
58 set_shortname( _("RTSP VoD" ) );
59 set_description( _("RTSP VoD server") );
60 set_category( CAT_SOUT );
61 set_subcategory( SUBCAT_SOUT_VOD );
62 set_capability( "vod server", 1 );
63 set_callbacks( Open, Close );
64 add_shortcut( "rtsp" );
65 add_string ( "rtsp-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
66 add_integer( "rtsp-throtle-users", 0, NULL, THROTLE_TEXT, THROTLE_LONGTEXT, VLC_TRUE );
69 /*****************************************************************************
71 *****************************************************************************/
73 typedef struct media_es_t media_es_t;
77 media_es_t *p_media_es;
86 int64_t i_last; /* for timeout */
88 vlc_bool_t b_playing; /* is it in "play" state */
89 vlc_bool_t b_paused; /* is it in "pause" state */
92 rtsp_client_es_t **es;
102 httpd_url_t *p_rtsp_url;
104 vod_media_t *p_media;
108 uint8_t i_payload_type;
120 httpd_url_t *p_rtsp_url;
121 char *psz_rtsp_control_v4;
122 char *psz_rtsp_control_v6;
134 vlc_bool_t b_multicast;
146 rtsp_client_t **rtsp;
149 char *psz_session_name;
150 char *psz_session_description;
151 char *psz_session_url;
152 char *psz_session_email;
159 httpd_host_t *p_rtsp_host;
170 static vod_media_t *MediaNew( vod_t *, const char *, input_item_t * );
171 static void MediaDel( vod_t *, vod_media_t * );
172 static int MediaAddES( vod_t *, vod_media_t *, es_format_t * );
173 static void MediaDelES( vod_t *, vod_media_t *, es_format_t * );
175 static rtsp_client_t *RtspClientNew( vod_media_t *, char * );
176 static rtsp_client_t *RtspClientGet( vod_media_t *, char * );
177 static void RtspClientDel( vod_media_t *, rtsp_client_t * );
179 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
180 httpd_message_t *, httpd_message_t * );
181 static int RtspCallbackES( httpd_callback_sys_t *, httpd_client_t *,
182 httpd_message_t *, httpd_message_t * );
184 static char *SDPGenerate( const vod_media_t *, httpd_client_t *cl );
186 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
188 static const char hex[16] = "0123456789abcdef";
191 for( i = 0; i < i_data; i++ )
193 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
194 s[2*i+1] = hex[(p_data[i] )&0xf];
199 /*****************************************************************************
200 * Open: Starts the RTSP server module
201 *****************************************************************************/
202 static int Open( vlc_object_t *p_this )
204 vod_t *p_vod = (vod_t *)p_this;
205 vod_sys_t *p_sys = 0;
209 psz_url = config_GetPsz( p_vod, "rtsp-host" );
210 vlc_UrlParse( &url, psz_url, 0 );
211 if( psz_url ) free( psz_url );
213 if( url.i_port <= 0 ) url.i_port = 554;
215 p_vod->p_sys = p_sys = malloc( sizeof( vod_sys_t ) );
216 if( !p_sys ) goto error;
217 p_sys->p_rtsp_host = 0;
219 var_Create( p_this, "rtsp-throtle-users", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
220 p_sys->i_throtle_users = var_GetInteger( p_this, "rtsp-throtle-users" );
221 msg_Dbg( p_this, "Allowing up to %d connections", p_sys->i_throtle_users );
222 p_sys->i_connections = 0;
225 httpd_HostNew( VLC_OBJECT(p_vod), url.psz_host, url.i_port );
226 if( !p_sys->p_rtsp_host )
228 msg_Err( p_vod, "cannot create http server (%s:%i)",
229 url.psz_host, url.i_port );
233 p_sys->psz_path = strdup( url.psz_path ? url.psz_path : "/" );
234 p_sys->i_port = url.i_port;
236 vlc_UrlClean( &url );
240 p_vod->pf_media_new = MediaNew;
241 p_vod->pf_media_del = MediaDel;
242 p_vod->pf_media_add_es = MediaAddES;
243 p_vod->pf_media_del_es = MediaDelES;
248 if( p_sys && p_sys->p_rtsp_host ) httpd_HostDelete( p_sys->p_rtsp_host );
249 if( p_sys ) free( p_sys );
250 vlc_UrlClean( &url );
255 /*****************************************************************************
257 *****************************************************************************/
258 static void Close( vlc_object_t * p_this )
260 vod_t *p_vod = (vod_t *)p_this;
261 vod_sys_t *p_sys = p_vod->p_sys;
263 httpd_HostDelete( p_sys->p_rtsp_host );
264 var_Destroy( p_this, "rtsp-throtle-users" );
266 /* TODO delete medias */
267 free( p_sys->psz_path );
271 /*****************************************************************************
273 *****************************************************************************/
274 static vod_media_t *MediaNew( vod_t *p_vod, const char *psz_name,
275 input_item_t *p_item )
277 vod_sys_t *p_sys = p_vod->p_sys;
278 vod_media_t *p_media = malloc( sizeof(vod_media_t) );
283 msg_Err( p_vod, "not enough memory" );
287 memset( p_media, 0, sizeof(vod_media_t) );
289 p_media->psz_mux = 0;
291 p_media->b_raw = VLC_FALSE;
293 asprintf( &p_media->psz_rtsp_path, "%s%s", p_sys->psz_path, psz_name );
294 p_media->p_rtsp_url =
295 httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, NULL,
298 if( !p_media->p_rtsp_url )
300 msg_Err( p_vod, "cannot create http url (%s)", p_media->psz_rtsp_path);
301 free( p_media->psz_rtsp_path );
306 msg_Dbg( p_vod, "created rtsp url: %s", p_media->psz_rtsp_path );
308 asprintf( &p_media->psz_rtsp_control_v4,
309 "a=control:rtsp://%%s:%d%s/trackid=%%d\r\n",
310 p_sys->i_port, p_media->psz_rtsp_path );
311 asprintf( &p_media->psz_rtsp_control_v6,
312 "a=control:rtsp://[%%s]:%d%s/trackid=%%d\r\n",
313 p_sys->i_port, p_media->psz_rtsp_path );
315 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_SETUP,
316 RtspCallback, (void*)p_media );
317 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_DESCRIBE,
318 RtspCallback, (void*)p_media );
319 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PLAY,
320 RtspCallback, (void*)p_media );
321 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PAUSE,
322 RtspCallback, (void*)p_media );
323 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_TEARDOWN,
324 RtspCallback, (void*)p_media );
326 p_media->p_vod = p_vod;
328 TAB_APPEND( p_sys->i_media, p_sys->media, p_media );
330 vlc_mutex_init( p_vod, &p_media->lock );
331 p_media->psz_session_name = strdup("");
332 p_media->psz_session_description = strdup("");
333 p_media->psz_session_url = strdup("");
334 p_media->psz_session_email = strdup("");
336 p_media->i_port_audio = 1234;
337 p_media->i_port_video = 1236;
338 p_media->i_port = 1238;
339 p_media->i_payload_type = 96;
341 p_media->i_sdp_id = mdate();
342 p_media->i_sdp_version = 1;
343 p_media->i_length = p_item->i_duration;
345 vlc_mutex_lock( &p_item->lock );
346 msg_Dbg( p_vod, "media has %i declared ES", p_item->i_es );
347 for( i = 0; i < p_item->i_es; i++ )
349 MediaAddES( p_vod, p_media, p_item->es[i] );
351 vlc_mutex_unlock( &p_item->lock );
356 static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
358 vod_sys_t *p_sys = p_vod->p_sys;
360 msg_Dbg( p_vod, "deleting media: %s", p_media->psz_rtsp_path );
362 while( p_media->i_rtsp > 0 ) RtspClientDel( p_media, p_media->rtsp[0] );
363 httpd_UrlDelete( p_media->p_rtsp_url );
364 if( p_media->psz_rtsp_path ) free( p_media->psz_rtsp_path );
365 if( p_media->psz_rtsp_control_v6 ) free( p_media->psz_rtsp_control_v6 );
366 if( p_media->psz_rtsp_control_v4 ) free( p_media->psz_rtsp_control_v4 );
368 TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
370 while( p_media->i_es ) MediaDelES( p_vod, p_media, &p_media->es[0]->fmt );
372 vlc_mutex_destroy( &p_media->lock );
373 free( p_media->psz_session_name );
374 free( p_media->psz_session_description );
375 free( p_media->psz_session_url );
376 free( p_media->psz_session_email );
380 static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
382 media_es_t *p_es = malloc( sizeof(media_es_t) );
385 memset( p_es, 0, sizeof(media_es_t) );
386 p_media->psz_mux = NULL;
388 /* TODO: update SDP, etc... */
389 asprintf( &psz_urlc, "%s/trackid=%d",
390 p_media->psz_rtsp_path, p_media->i_es );
391 msg_Dbg( p_vod, " - ES %4.4s (%s)", (char *)&p_fmt->i_codec, psz_urlc );
393 switch( p_fmt->i_codec )
395 case VLC_FOURCC( 's', '1', '6', 'b' ):
396 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
398 p_es->i_payload_type = 11;
400 else if( p_fmt->audio.i_channels == 2 && p_fmt->audio.i_rate == 44100 )
402 p_es->i_payload_type = 10;
406 p_es->i_payload_type = p_media->i_payload_type++;
408 p_es->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
409 sprintf( p_es->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
410 p_fmt->audio.i_channels );
412 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
413 p_es->i_payload_type = p_media->i_payload_type++;
414 p_es->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
415 sprintf( p_es->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
416 p_fmt->audio.i_channels );
418 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
419 p_es->i_payload_type = 14;
420 p_es->psz_rtpmap = strdup( "MPA/90000" );
422 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
423 p_es->i_payload_type = 32;
424 p_es->psz_rtpmap = strdup( "MPV/90000" );
426 case VLC_FOURCC( 'a', '5', '2', ' ' ):
427 p_es->i_payload_type = p_media->i_payload_type++;
428 p_es->psz_rtpmap = strdup( "ac3/90000" );
430 case VLC_FOURCC( 'H', '2', '6', '3' ):
431 p_es->i_payload_type = p_media->i_payload_type++;
432 p_es->psz_rtpmap = strdup( "H263-1998/90000" );
434 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
435 p_es->i_payload_type = p_media->i_payload_type++;
436 p_es->psz_rtpmap = strdup( "MP4V-ES/90000" );
437 if( p_fmt->i_extra > 0 )
439 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
440 p_es->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
441 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
442 sprintf( p_es->psz_fmtp,
443 "profile-level-id=3; config=%s;", p_hexa );
447 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
448 p_es->i_payload_type = p_media->i_payload_type++;
449 p_es->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
450 sprintf( p_es->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
451 if( p_fmt->i_extra > 0 )
453 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
454 p_es->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
455 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
456 sprintf( p_es->psz_fmtp,
457 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
458 "config=%s; SizeLength=13;IndexLength=3; "
459 "IndexDeltaLength=3; Profile=1;", p_hexa );
463 case VLC_FOURCC( 'm', 'p', '2', 't' ):
464 p_media->psz_mux = "ts";
465 p_es->i_payload_type = 33;
466 p_es->psz_rtpmap = strdup( "MP2T/90000" );
468 case VLC_FOURCC( 'm', 'p', '2', 'p' ):
469 p_media->psz_mux = "ps";
470 p_es->i_payload_type = p_media->i_payload_type++;
471 p_es->psz_rtpmap = strdup( "MP2P/90000" );
473 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
474 p_es->i_payload_type = p_media->i_payload_type++;
475 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
476 "AMR/8000/2" : "AMR/8000" );
477 p_es->psz_fmtp = strdup( "octet-align=1" );
479 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
480 p_es->i_payload_type = p_media->i_payload_type++;
481 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
482 "AMR-WB/16000/2" : "AMR-WB/16000" );
483 p_es->psz_fmtp = strdup( "octet-align=1" );
487 msg_Err( p_vod, "cannot add this stream (unsupported "
488 "codec: %4.4s)", (char*)&p_fmt->i_codec );
494 httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, NULL, NULL,
497 if( !p_es->p_rtsp_url )
499 msg_Err( p_vod, "cannot create http url (%s)", psz_urlc );
506 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
507 RtspCallbackES, (void*)p_es );
508 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_TEARDOWN,
509 RtspCallbackES, (void*)p_es );
510 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PLAY,
511 RtspCallbackES, (void*)p_es );
512 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PAUSE,
513 RtspCallbackES, (void*)p_es );
515 es_format_Copy( &p_es->fmt, p_fmt );
517 p_es->p_media = p_media;
520 /* Choose the port */
521 if( p_fmt->i_cat == AUDIO_ES && p_media->i_port_audio > 0 )
523 p_es->i_port = p_media->i_port_audio;
524 p_media->i_port_audio = 0;
526 else if( p_fmt->i_cat == VIDEO_ES && p_media->i_port_video > 0 )
528 p_es->i_port = p_media->i_port_video;
529 p_media->i_port_video = 0;
531 while( !p_es->i_port )
533 if( p_media->i_port != p_media->i_port_audio &&
534 p_media->i_port != p_media->i_port_video )
536 p_es->i_port = p_media->i_port;
537 p_media->i_port += 2;
540 p_media->i_port += 2;
547 vlc_mutex_lock( &p_media->lock );
548 TAB_APPEND( p_media->i_es, p_media->es, p_es );
549 vlc_mutex_unlock( &p_media->lock );
551 p_media->i_sdp_version++;
556 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
558 media_es_t *p_es = 0;
562 for( i = 0; i < p_media->i_es; i++ )
564 if( p_media->es[i]->fmt.i_cat == p_fmt->i_cat &&
565 p_media->es[i]->fmt.i_codec == p_fmt->i_codec &&
566 p_media->es[i]->fmt.i_id == p_fmt->i_id )
568 p_es = p_media->es[i];
573 msg_Dbg( p_vod, " - Removing ES %4.4s", (char *)&p_fmt->i_codec );
575 vlc_mutex_lock( &p_media->lock );
576 TAB_REMOVE( p_media->i_es, p_media->es, p_es );
577 vlc_mutex_unlock( &p_media->lock );
579 if( p_es->psz_rtpmap ) free( p_es->psz_rtpmap );
580 if( p_es->psz_fmtp ) free( p_es->psz_fmtp );
581 p_media->i_sdp_version++;
583 if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
584 es_format_Clean( &p_es->fmt );
587 /****************************************************************************
588 * RTSP server implementation
589 ****************************************************************************/
590 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
592 rtsp_client_t *p_rtsp = malloc( sizeof(rtsp_client_t) );
594 if( !p_rtsp ) return NULL;
595 memset( p_rtsp, 0, sizeof(rtsp_client_t) );
598 p_rtsp->psz_session = psz_session;
599 TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
601 p_media->p_vod->p_sys->i_connections++;
602 msg_Dbg( p_media->p_vod, "new session: %s, connections: %d",
603 psz_session, p_media->p_vod->p_sys->i_throtle_users );
608 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, char *psz_session )
612 for( i = 0; psz_session && i < p_media->i_rtsp; i++ )
614 if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
616 return p_media->rtsp[i];
623 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
625 p_media->p_vod->p_sys->i_connections--;
626 msg_Dbg( p_media->p_vod, "closing session: %s, connections: %d",
627 p_rtsp->psz_session, p_media->p_vod->p_sys->i_throtle_users );
629 while( p_rtsp->i_es-- )
631 if( p_rtsp->es[p_rtsp->i_es]->psz_ip )
632 free( p_rtsp->es[p_rtsp->i_es]->psz_ip );
633 free( p_rtsp->es[p_rtsp->i_es] );
634 if( !p_rtsp->i_es ) free( p_rtsp->es );
637 TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, p_rtsp );
639 free( p_rtsp->psz_session );
643 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
644 httpd_message_t *answer, httpd_message_t *query )
646 vod_media_t *p_media = (vod_media_t*)p_args;
647 vod_t *p_vod = p_media->p_vod;
648 char *psz_transport = NULL;
649 char *psz_playnow = NULL; /* support option: x-playNow */
650 char *psz_session = NULL;
651 rtsp_client_t *p_rtsp;
654 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
656 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
658 answer->i_proto = HTTPD_PROTO_RTSP;
659 answer->i_version = query->i_version;
660 answer->i_type = HTTPD_MSG_ANSWER;
662 answer->p_body = NULL;
664 switch( query->i_type )
666 case HTTPD_MSG_SETUP:
668 psz_playnow = httpd_MsgGet( query, "x-playNow" );
669 psz_transport = httpd_MsgGet( query, "Transport" );
670 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
672 if( strstr( psz_transport, "unicast" ) &&
673 strstr( psz_transport, "client_port=" ) )
675 rtsp_client_t *p_rtsp;
676 char ip[NI_MAXNUMERICHOST];
677 i_port = atoi( strstr( psz_transport, "client_port=" ) +
678 strlen("client_port=") );
680 if( strstr( psz_transport, "MP2T/H2221/UDP" ) ||
681 strstr( psz_transport, "RAW/RAW/UDP" ) )
683 p_media->b_raw = VLC_TRUE;
686 if( httpd_ClientIP( cl, ip ) == NULL )
688 answer->i_status = 500;
689 answer->psz_status = strdup( "Internal server error" );
691 answer->p_body = NULL;
695 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
698 psz_session = httpd_MsgGet( query, "Session" );
699 if( !psz_session || !*psz_session )
701 if( ( p_vod->p_sys->i_throtle_users > 0 ) &&
702 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throtle_users ) )
704 answer->i_status = 500; // FIXME: GET THE RIGHT ERROR STATUS
705 answer->psz_status = strdup( "Too many connections" );
707 answer->p_body = NULL;
710 asprintf( &psz_session, "%d", rand() );
711 p_rtsp = RtspClientNew( p_media, psz_session );
715 p_rtsp = RtspClientGet( p_media, psz_session );
718 /* FIXME right error code */
719 answer->i_status = 454;
720 answer->psz_status = strdup( "Unknown session id" );
722 answer->p_body = NULL;
727 answer->i_status = 200;
728 answer->psz_status = strdup( "OK" );
730 answer->p_body = NULL;
734 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
736 httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
737 i_port, i_port + 1 );
739 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
741 httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
742 i_port, i_port + 1 );
746 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
747 i_port, i_port + 1 );
749 else /* TODO strstr( psz_transport, "interleaved" ) ) */
751 answer->i_status = 461;
752 answer->psz_status = strdup( "Unsupported Transport" );
754 answer->p_body = NULL;
757 if( !psz_playnow || !*psz_playnow )
763 char *psz_output, ip[NI_MAXNUMERICHOST];
764 int i, i_port_audio = 0, i_port_video = 0;
766 /* for now only multicast so easy */
769 answer->i_status = 200;
770 answer->psz_status = strdup( "OK" );
772 answer->p_body = NULL;
776 psz_session = httpd_MsgGet( query, "Session" );
777 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
779 p_rtsp = RtspClientGet( p_media, psz_session );
782 if( p_rtsp->b_playing && p_rtsp->b_paused )
784 vod_MediaControl( p_vod, p_media, psz_session,
786 p_rtsp->b_paused = VLC_FALSE;
789 else if( p_rtsp->b_playing ) break;
791 if( httpd_ClientIP( cl, ip ) == NULL ) break;
793 p_rtsp->b_playing = VLC_TRUE;
795 /* FIXME for != 1 video and 1 audio */
796 for( i = 0; i < p_rtsp->i_es; i++ )
798 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == AUDIO_ES )
799 i_port_audio = p_rtsp->es[i]->i_port;
800 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == VIDEO_ES )
801 i_port_video = p_rtsp->es[i]->i_port;
804 if( p_media->psz_mux )
808 asprintf( &psz_output, "std{access=udp,dst=%s:%i,mux=%s}",
809 ip, i_port, p_media->psz_mux );
813 asprintf( &psz_output, "rtp{dst=%s,port=%i,mux=%s}",
814 ip, i_port_video, p_media->psz_mux );
819 asprintf( &psz_output, "rtp{dst=%s,port-video=%i,"
820 "port-audio=%i}", ip, i_port_video, i_port_audio );
823 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PLAY,
829 case HTTPD_MSG_DESCRIBE:
832 SDPGenerate( p_media, cl );
834 if( psz_sdp != NULL )
836 answer->i_status = 200;
837 answer->psz_status = strdup( "OK" );
838 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
840 answer->p_body = (uint8_t *)psz_sdp;
841 answer->i_body = strlen( psz_sdp );
845 answer->i_status = 500;
846 answer->psz_status = strdup( "Internal server error" );
847 answer->p_body = NULL;
853 case HTTPD_MSG_PAUSE:
854 psz_session = httpd_MsgGet( query, "Session" );
855 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
857 p_rtsp = RtspClientGet( p_media, psz_session );
860 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
861 p_rtsp->b_paused = VLC_TRUE;
863 answer->i_status = 200;
864 answer->psz_status = strdup( "OK" );
866 answer->p_body = NULL;
869 case HTTPD_MSG_TEARDOWN:
870 /* for now only multicast so easy again */
871 answer->i_status = 200;
872 answer->psz_status = strdup( "OK" );
874 answer->p_body = NULL;
876 psz_session = httpd_MsgGet( query, "Session" );
877 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
879 p_rtsp = RtspClientGet( p_media, psz_session );
882 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_STOP );
883 RtspClientDel( p_media, p_rtsp );
890 httpd_MsgAdd( answer, "Server", "VLC Server" );
891 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
892 httpd_MsgAdd( answer, "Cseq", "%d",
893 atoi( httpd_MsgGet( query, "Cseq" ) ) );
894 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
898 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
904 static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
905 httpd_message_t *answer, httpd_message_t *query )
907 media_es_t *p_es = (media_es_t*)p_args;
908 vod_media_t *p_media = p_es->p_media;
909 vod_t *p_vod = p_media->p_vod;
910 rtsp_client_t *p_rtsp = NULL;
911 char *psz_transport = NULL;
912 char *psz_playnow = NULL; /* support option: x-playNow */
913 char *psz_session = NULL;
914 char *psz_position = NULL;
917 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
919 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
921 answer->i_proto = HTTPD_PROTO_RTSP;
922 answer->i_version = query->i_version;
923 answer->i_type = HTTPD_MSG_ANSWER;
925 answer->p_body = NULL;
927 switch( query->i_type )
929 case HTTPD_MSG_SETUP:
930 psz_playnow = httpd_MsgGet( query, "x-playNow" );
931 psz_transport = httpd_MsgGet( query, "Transport" );
933 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
935 if( strstr( psz_transport, "unicast" ) &&
936 strstr( psz_transport, "client_port=" ) )
938 rtsp_client_t *p_rtsp;
939 rtsp_client_es_t *p_rtsp_es;
940 char ip[NI_MAXNUMERICHOST];
941 int i_port = atoi( strstr( psz_transport, "client_port=" ) +
942 strlen("client_port=") );
944 if( httpd_ClientIP( cl, ip ) == NULL )
946 answer->i_status = 500;
947 answer->psz_status = strdup( "Internal server error" );
949 answer->p_body = NULL;
953 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
956 psz_session = httpd_MsgGet( query, "Session" );
957 if( !psz_session || !*psz_session )
959 if( ( p_vod->p_sys->i_throtle_users > 0 ) &&
960 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throtle_users ) )
962 answer->i_status = 500; // FIXME: GET THE RIGHT ERROR STATUS
963 answer->psz_status = strdup( "Too many connections" );
965 answer->p_body = NULL;
968 asprintf( &psz_session, "%d", rand() );
969 p_rtsp = RtspClientNew( p_media, psz_session );
973 p_rtsp = RtspClientGet( p_media, psz_session );
976 /* FIXME right error code */
977 answer->i_status = 454;
978 answer->psz_status = strdup( "Unknown session id" );
980 answer->p_body = NULL;
985 p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
986 p_rtsp_es->i_port = i_port;
987 p_rtsp_es->psz_ip = strdup( ip );
988 p_rtsp_es->p_media_es = p_es;
989 TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );
991 answer->i_status = 200;
992 answer->psz_status = strdup( "OK" );
994 answer->p_body = NULL;
998 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
1000 httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
1001 i_port, i_port + 1 );
1003 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1005 httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
1006 i_port, i_port + 1 );
1011 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
1012 i_port, i_port + 1 );
1015 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1017 answer->i_status = 461;
1018 answer->psz_status = strdup( "Unsupported Transport" );
1020 answer->p_body = NULL;
1023 if( !psz_playnow || !*psz_playnow )
1026 case HTTPD_MSG_PLAY:
1027 /* This is kind of a kludge. Should we only support Aggregate
1029 psz_session = httpd_MsgGet( query, "Session" );
1030 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1032 p_rtsp = RtspClientGet( p_media, psz_session );
1034 psz_position = httpd_MsgGet( query, "Range" );
1035 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
1040 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1043 if( sscanf( psz_position, "%f", &f_pos ) == 1 )
1045 f_pos /= ((float)(p_media->i_length/1000))/1000 / 100;
1046 vod_MediaControl( p_vod, p_media, psz_session,
1047 VOD_MEDIA_SEEK, (double)f_pos );
1053 answer->i_status = 200;
1054 answer->psz_status = strdup( "OK" );
1056 answer->p_body = NULL;
1060 case HTTPD_MSG_TEARDOWN:
1061 answer->i_status = 200;
1062 answer->psz_status = strdup( "OK" );
1064 answer->p_body = NULL;
1066 psz_session = httpd_MsgGet( query, "Session" );
1067 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1069 p_rtsp = RtspClientGet( p_media, psz_session );
1070 if( !p_rtsp ) break;
1072 for( i = 0; i < p_rtsp->i_es; i++ )
1074 if( p_rtsp->es[i]->p_media_es == p_es )
1076 if( p_rtsp->es[i]->psz_ip ) free( p_rtsp->es[i]->psz_ip );
1077 TAB_REMOVE( p_rtsp->i_es, p_rtsp->es, p_rtsp->es[i] );
1084 vod_MediaControl( p_vod, p_media, psz_session,
1086 RtspClientDel( p_media, p_rtsp );
1090 case HTTPD_MSG_PAUSE:
1091 /* This is kind of a kludge. Should we only support Aggregate
1093 psz_session = httpd_MsgGet( query, "Session" );
1094 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1096 p_rtsp = RtspClientGet( p_media, psz_session );
1097 if( !p_rtsp ) break;
1099 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
1100 p_rtsp->b_paused = VLC_TRUE;
1102 answer->i_status = 200;
1103 answer->psz_status = strdup( "OK" );
1105 answer->p_body = NULL;
1109 return VLC_EGENERIC;
1113 httpd_MsgAdd( answer, "Server", "VLC Server" );
1114 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1115 httpd_MsgAdd( answer, "Cseq", "%d",
1116 atoi( httpd_MsgGet( query, "Cseq" ) ) );
1117 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1120 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1125 /*****************************************************************************
1127 * FIXME: need to be moved to a common place ?
1128 *****************************************************************************/
1129 static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
1132 char *p, *psz_sdp, ip[NI_MAXNUMERICHOST], ipv;
1133 const char *psz_control;
1135 if( httpd_ServerIP( cl, ip ) == NULL )
1138 p = strchr( ip, '%' );
1140 *p = '\0'; /* remove scope if present */
1142 ipv = ( strchr( ip, ':' ) != NULL ) ? '6' : '4';
1144 /* Calculate size */
1145 i_size = sizeof( "v=0\r\n" ) +
1146 sizeof( "o=- * * IN IP4 \r\n" ) + 10 + NI_MAXNUMERICHOST +
1147 sizeof( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
1148 sizeof( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
1149 sizeof( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
1150 sizeof( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
1151 sizeof( "t=0 0\r\n" ) + /* FIXME */
1152 sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
1153 sizeof( "c=IN IP4 0.0.0.0\r\n" ) + 20 + 10 +
1154 sizeof( "a=range:npt=0-1000000000.000\r\n" );
1156 psz_control = (ipv == '6') ? p_media->psz_rtsp_control_v6
1157 : p_media->psz_rtsp_control_v4;
1158 for( i = 0; i < p_media->i_es; i++ )
1160 media_es_t *p_es = p_media->es[i];
1162 i_size += sizeof( "m=**d*o * RTP/AVP *\r\n" ) + 19;
1163 if( p_es->psz_rtpmap )
1165 i_size += sizeof( "a=rtpmap:* *\r\n" ) +
1166 strlen( p_es->psz_rtpmap ) + 9;
1168 if( p_es->psz_fmtp )
1170 i_size += sizeof( "a=fmtp:* *\r\n" ) +
1171 strlen( p_es->psz_fmtp ) + 9;
1174 i_size += (strlen( psz_control ) + strlen( ip ) + 9) * p_media->i_es;
1176 p = psz_sdp = malloc( i_size );
1177 p += sprintf( p, "v=0\r\n" );
1178 p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
1179 p_media->i_sdp_id, p_media->i_sdp_version, ipv, ip );
1180 if( *p_media->psz_session_name )
1181 p += sprintf( p, "s=%s\r\n", p_media->psz_session_name );
1182 if( *p_media->psz_session_description )
1183 p += sprintf( p, "i=%s\r\n", p_media->psz_session_description );
1184 if( *p_media->psz_session_url )
1185 p += sprintf( p, "u=%s\r\n", p_media->psz_session_url );
1186 if( *p_media->psz_session_email )
1187 p += sprintf( p, "e=%s\r\n", p_media->psz_session_email );
1189 p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
1190 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
1192 p += sprintf( p, "c=IN IP%c %s\r\n", ipv, ipv == '6' ? "::" : "0.0.0.0" );
1194 if( p_media->i_length > 0 )
1195 p += sprintf( p, "a=range:npt=0-%.3f\r\n",
1196 ((float)(p_media->i_length/1000))/1000 );
1198 for( i = 0; i < p_media->i_es; i++ )
1200 media_es_t *p_es = p_media->es[i];
1202 if( p_es->fmt.i_cat == AUDIO_ES )
1204 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
1205 p_es->i_port, p_es->i_payload_type );
1207 else if( p_es->fmt.i_cat == VIDEO_ES )
1209 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
1210 p_es->i_port, p_es->i_payload_type );
1217 if( p_es->psz_rtpmap )
1219 p += sprintf( p, "a=rtpmap:%d %s\r\n", p_es->i_payload_type,
1222 if( p_es->psz_fmtp )
1224 p += sprintf( p, "a=fmtp:%d %s\r\n", p_es->i_payload_type,
1228 p += sprintf( p, psz_control, ip, i );