1 /*****************************************************************************
2 * rtsp.c: rtsp VoD server module
3 *****************************************************************************
4 * Copyright (C) 2003-2006 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 *****************************************************************************/
34 #include <vlc/input.h>
37 #include "vlc_httpd.h"
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Open ( vlc_object_t * );
47 static void Close( vlc_object_t * );
49 #define HOST_TEXT N_( "RTSP host address" )
50 /// \bug [String] extra space
51 #define HOST_LONGTEXT N_( \
52 "This defines the address, port and path the RTSP VOD server will listen " \
53 "on.\nSyntax is address:port/path. The default is to listen on all "\
54 "interfaces (address 0.0.0.0), on port 554, with no path.\n To listen " \
55 "only on the local interface, use \"localhost\" as address." )
57 #define THROTLE_TEXT N_( "Maximum number of connections" )
58 #define THROTLE_LONGTEXT N_( "This limits the maximum number of clients " \
59 "that can connect to the RTSP VOD. 0 means no limit." )
61 #define RAWMUX_TEXT N_( "MUX for RAW RTSP transport" )
64 set_shortname( _("RTSP VoD" ) );
65 set_description( _("RTSP VoD server") );
66 set_category( CAT_SOUT );
67 set_subcategory( SUBCAT_SOUT_VOD );
68 set_capability( "vod server", 1 );
69 set_callbacks( Open, Close );
70 add_shortcut( "rtsp" );
71 add_string ( "rtsp-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
72 add_string( "rtsp-raw-mux", "ts", NULL, RAWMUX_TEXT, RAWMUX_TEXT, VLC_TRUE );
73 add_integer( "rtsp-throttle-users", 0, NULL, THROTLE_TEXT,
74 THROTLE_LONGTEXT, VLC_TRUE );
77 /*****************************************************************************
79 *****************************************************************************/
81 typedef struct media_es_t media_es_t;
85 media_es_t *p_media_es;
94 int64_t i_last; /* for timeout */
96 vlc_bool_t b_playing; /* is it in "play" state */
97 vlc_bool_t b_paused; /* is it in "pause" state */
100 rtsp_client_es_t **es;
110 httpd_url_t *p_rtsp_url;
112 vod_media_t *p_media;
116 uint8_t i_payload_type;
128 httpd_url_t *p_rtsp_url;
129 char *psz_rtsp_control_v4;
130 char *psz_rtsp_control_v6;
142 vlc_bool_t b_multicast;
154 rtsp_client_t **rtsp;
157 char *psz_session_name;
158 char *psz_session_description;
159 char *psz_session_url;
160 char *psz_session_email;
167 httpd_host_t *p_rtsp_host;
170 int i_throttle_users;
180 static vod_media_t *MediaNew( vod_t *, const char *, input_item_t * );
181 static void MediaDel( vod_t *, vod_media_t * );
182 static int MediaAddES( vod_t *, vod_media_t *, es_format_t * );
183 static void MediaDelES( vod_t *, vod_media_t *, es_format_t * );
185 static rtsp_client_t *RtspClientNew( vod_media_t *, char * );
186 static rtsp_client_t *RtspClientGet( vod_media_t *, char * );
187 static void RtspClientDel( vod_media_t *, rtsp_client_t * );
189 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
190 httpd_message_t *, httpd_message_t * );
191 static int RtspCallbackES( httpd_callback_sys_t *, httpd_client_t *,
192 httpd_message_t *, httpd_message_t * );
194 static char *SDPGenerate( const vod_media_t *, httpd_client_t *cl );
196 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
198 static const char hex[16] = "0123456789abcdef";
201 for( i = 0; i < i_data; i++ )
203 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
204 s[2*i+1] = hex[(p_data[i] )&0xf];
209 /*****************************************************************************
210 * Open: Starts the RTSP server module
211 *****************************************************************************/
212 static int Open( vlc_object_t *p_this )
214 vod_t *p_vod = (vod_t *)p_this;
215 vod_sys_t *p_sys = 0;
219 psz_url = config_GetPsz( p_vod, "rtsp-host" );
220 vlc_UrlParse( &url, psz_url, 0 );
221 if( psz_url ) free( psz_url );
223 if( url.i_port <= 0 ) url.i_port = 554;
225 p_vod->p_sys = p_sys = malloc( sizeof( vod_sys_t ) );
226 if( !p_sys ) goto error;
227 p_sys->p_rtsp_host = 0;
229 var_Create( p_this, "rtsp-throttle-users", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
230 p_sys->i_throttle_users = var_GetInteger( p_this, "rtsp-throtle-users" );
231 msg_Dbg( p_this, "allowing up to %d connections", p_sys->i_throttle_users );
232 p_sys->i_connections = 0;
234 var_Create( p_this, "rtsp-raw-mux", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
235 p_sys->psz_raw_mux = var_GetString( p_this, "rtsp-raw-mux" );
238 httpd_HostNew( VLC_OBJECT(p_vod), url.psz_host, url.i_port );
239 if( !p_sys->p_rtsp_host )
241 msg_Err( p_vod, "cannot create RTSP server (%s:%i)",
242 url.psz_host, url.i_port );
246 p_sys->psz_path = strdup( url.psz_path ? url.psz_path : "/" );
247 p_sys->i_port = url.i_port;
249 vlc_UrlClean( &url );
253 p_vod->pf_media_new = MediaNew;
254 p_vod->pf_media_del = MediaDel;
255 p_vod->pf_media_add_es = MediaAddES;
256 p_vod->pf_media_del_es = MediaDelES;
261 if( p_sys && p_sys->p_rtsp_host ) httpd_HostDelete( p_sys->p_rtsp_host );
262 if( p_sys && p_sys->psz_raw_mux ) free( p_sys->psz_raw_mux );
263 if( p_sys ) free( p_sys );
264 vlc_UrlClean( &url );
269 /*****************************************************************************
271 *****************************************************************************/
272 static void Close( vlc_object_t * p_this )
274 vod_t *p_vod = (vod_t *)p_this;
275 vod_sys_t *p_sys = p_vod->p_sys;
277 httpd_HostDelete( p_sys->p_rtsp_host );
278 var_Destroy( p_this, "rtsp-throttle-users" );
279 var_Destroy( p_this, "rtsp-raw-mux" );
281 /* TODO delete medias */
282 free( p_sys->psz_path );
283 free( p_sys->psz_raw_mux );
287 /*****************************************************************************
289 *****************************************************************************/
290 static vod_media_t *MediaNew( vod_t *p_vod, const char *psz_name,
291 input_item_t *p_item )
293 vod_sys_t *p_sys = p_vod->p_sys;
294 vod_media_t *p_media = malloc( sizeof(vod_media_t) );
299 msg_Err( p_vod, "not enough memory" );
303 memset( p_media, 0, sizeof(vod_media_t) );
305 p_media->psz_mux = 0;
307 p_media->b_raw = VLC_FALSE;
309 asprintf( &p_media->psz_rtsp_path, "%s%s", p_sys->psz_path, psz_name );
310 p_media->p_rtsp_url =
311 httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, NULL,
314 if( !p_media->p_rtsp_url )
316 msg_Err( p_vod, "cannot create RTSP url (%s)", p_media->psz_rtsp_path);
317 free( p_media->psz_rtsp_path );
322 msg_Dbg( p_vod, "created RTSP url: %s", p_media->psz_rtsp_path );
324 asprintf( &p_media->psz_rtsp_control_v4,
325 "a=control:rtsp://%%s:%d%s/trackID=%%d\r\n",
326 p_sys->i_port, p_media->psz_rtsp_path );
327 asprintf( &p_media->psz_rtsp_control_v6,
328 "a=control:rtsp://[%%s]:%d%s/trackID=%%d\r\n",
329 p_sys->i_port, p_media->psz_rtsp_path );
331 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_SETUP,
332 RtspCallback, (void*)p_media );
333 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_DESCRIBE,
334 RtspCallback, (void*)p_media );
335 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PLAY,
336 RtspCallback, (void*)p_media );
337 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PAUSE,
338 RtspCallback, (void*)p_media );
339 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_GETPARAMETER,
340 RtspCallback, (void*)p_media );
341 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_TEARDOWN,
342 RtspCallback, (void*)p_media );
344 p_media->p_vod = p_vod;
346 TAB_APPEND( p_sys->i_media, p_sys->media, p_media );
348 vlc_mutex_init( p_vod, &p_media->lock );
349 p_media->psz_session_name = strdup("");
350 p_media->psz_session_description = strdup("");
351 p_media->psz_session_url = strdup("");
352 p_media->psz_session_email = strdup("");
354 p_media->i_port_audio = 1234;
355 p_media->i_port_video = 1236;
356 p_media->i_port = 1238;
357 p_media->i_payload_type = 96;
359 p_media->i_sdp_id = mdate();
360 p_media->i_sdp_version = 1;
361 p_media->i_length = p_item->i_duration;
363 vlc_mutex_lock( &p_item->lock );
364 msg_Dbg( p_vod, "media has %i declared ES", p_item->i_es );
365 for( i = 0; i < p_item->i_es; i++ )
367 MediaAddES( p_vod, p_media, p_item->es[i] );
369 vlc_mutex_unlock( &p_item->lock );
374 static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
376 vod_sys_t *p_sys = p_vod->p_sys;
378 msg_Dbg( p_vod, "deleting media: %s", p_media->psz_rtsp_path );
380 while( p_media->i_rtsp > 0 ) RtspClientDel( p_media, p_media->rtsp[0] );
381 httpd_UrlDelete( p_media->p_rtsp_url );
382 if( p_media->psz_rtsp_path ) free( p_media->psz_rtsp_path );
383 if( p_media->psz_rtsp_control_v6 ) free( p_media->psz_rtsp_control_v6 );
384 if( p_media->psz_rtsp_control_v4 ) free( p_media->psz_rtsp_control_v4 );
386 TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
388 while( p_media->i_es ) MediaDelES( p_vod, p_media, &p_media->es[0]->fmt );
390 vlc_mutex_destroy( &p_media->lock );
391 free( p_media->psz_session_name );
392 free( p_media->psz_session_description );
393 free( p_media->psz_session_url );
394 free( p_media->psz_session_email );
398 static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
400 media_es_t *p_es = malloc( sizeof(media_es_t) );
403 memset( p_es, 0, sizeof(media_es_t) );
404 p_media->psz_mux = NULL;
406 /* TODO: update SDP, etc... */
407 asprintf( &psz_urlc, "%s/trackID=%d",
408 p_media->psz_rtsp_path, p_media->i_es );
409 msg_Dbg( p_vod, " - ES %4.4s (%s)", (char *)&p_fmt->i_codec, psz_urlc );
411 switch( p_fmt->i_codec )
413 case VLC_FOURCC( 's', '1', '6', 'b' ):
414 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
416 p_es->i_payload_type = 11;
418 else if( p_fmt->audio.i_channels == 2 && p_fmt->audio.i_rate == 44100 )
420 p_es->i_payload_type = 10;
424 p_es->i_payload_type = p_media->i_payload_type++;
426 p_es->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
427 sprintf( p_es->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
428 p_fmt->audio.i_channels );
430 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
431 p_es->i_payload_type = p_media->i_payload_type++;
432 p_es->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
433 sprintf( p_es->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
434 p_fmt->audio.i_channels );
436 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
437 p_es->i_payload_type = 14;
438 p_es->psz_rtpmap = strdup( "MPA/90000" );
440 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
441 p_es->i_payload_type = 32;
442 p_es->psz_rtpmap = strdup( "MPV/90000" );
444 case VLC_FOURCC( 'a', '5', '2', ' ' ):
445 p_es->i_payload_type = p_media->i_payload_type++;
446 p_es->psz_rtpmap = strdup( "ac3/90000" );
448 case VLC_FOURCC( 'H', '2', '6', '3' ):
449 p_es->i_payload_type = p_media->i_payload_type++;
450 p_es->psz_rtpmap = strdup( "H263-1998/90000" );
452 case VLC_FOURCC( 'h', '2', '6', '4' ):
453 p_es->i_payload_type = p_media->i_payload_type++;
454 p_es->psz_rtpmap = strdup( "H264/90000" );
455 p_es->psz_fmtp = strdup( "packetization-mode=1" );
457 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
458 p_es->i_payload_type = p_media->i_payload_type++;
459 p_es->psz_rtpmap = strdup( "MP4V-ES/90000" );
460 if( p_fmt->i_extra > 0 )
462 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
463 p_es->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
464 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
465 sprintf( p_es->psz_fmtp,
466 "profile-level-id=3; config=%s;", p_hexa );
470 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
471 p_es->i_payload_type = p_media->i_payload_type++;
472 p_es->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
473 sprintf( p_es->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
474 if( p_fmt->i_extra > 0 )
476 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
477 p_es->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
478 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
479 sprintf( p_es->psz_fmtp,
480 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
481 "config=%s; SizeLength=13;IndexLength=3; "
482 "IndexDeltaLength=3; Profile=1;", p_hexa );
486 case VLC_FOURCC( 'm', 'p', '2', 't' ):
487 p_media->psz_mux = "ts";
488 p_es->i_payload_type = 33;
489 p_es->psz_rtpmap = strdup( "MP2T/90000" );
491 case VLC_FOURCC( 'm', 'p', '2', 'p' ):
492 p_media->psz_mux = "ps";
493 p_es->i_payload_type = p_media->i_payload_type++;
494 p_es->psz_rtpmap = strdup( "MP2P/90000" );
496 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
497 p_es->i_payload_type = p_media->i_payload_type++;
498 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
499 "AMR/8000/2" : "AMR/8000" );
500 p_es->psz_fmtp = strdup( "octet-align=1" );
502 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
503 p_es->i_payload_type = p_media->i_payload_type++;
504 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
505 "AMR-WB/16000/2" : "AMR-WB/16000" );
506 p_es->psz_fmtp = strdup( "octet-align=1" );
510 msg_Err( p_vod, "cannot add this stream (unsupported "
511 "codec: %4.4s)", (char*)&p_fmt->i_codec );
517 httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, NULL, NULL,
520 if( !p_es->p_rtsp_url )
522 msg_Err( p_vod, "cannot create RTSP url (%s)", psz_urlc );
529 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
530 RtspCallbackES, (void*)p_es );
531 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_TEARDOWN,
532 RtspCallbackES, (void*)p_es );
533 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PLAY,
534 RtspCallbackES, (void*)p_es );
535 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PAUSE,
536 RtspCallbackES, (void*)p_es );
538 es_format_Copy( &p_es->fmt, p_fmt );
540 p_es->p_media = p_media;
543 /* Choose the port */
544 if( p_fmt->i_cat == AUDIO_ES && p_media->i_port_audio > 0 )
546 p_es->i_port = p_media->i_port_audio;
547 p_media->i_port_audio = 0;
549 else if( p_fmt->i_cat == VIDEO_ES && p_media->i_port_video > 0 )
551 p_es->i_port = p_media->i_port_video;
552 p_media->i_port_video = 0;
554 while( !p_es->i_port )
556 if( p_media->i_port != p_media->i_port_audio &&
557 p_media->i_port != p_media->i_port_video )
559 p_es->i_port = p_media->i_port;
560 p_media->i_port += 2;
563 p_media->i_port += 2;
570 vlc_mutex_lock( &p_media->lock );
571 TAB_APPEND( p_media->i_es, p_media->es, p_es );
572 vlc_mutex_unlock( &p_media->lock );
574 p_media->i_sdp_version++;
579 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
581 media_es_t *p_es = 0;
585 for( i = 0; i < p_media->i_es; i++ )
587 if( p_media->es[i]->fmt.i_cat == p_fmt->i_cat &&
588 p_media->es[i]->fmt.i_codec == p_fmt->i_codec &&
589 p_media->es[i]->fmt.i_id == p_fmt->i_id )
591 p_es = p_media->es[i];
596 msg_Dbg( p_vod, " - Removing ES %4.4s", (char *)&p_fmt->i_codec );
598 vlc_mutex_lock( &p_media->lock );
599 TAB_REMOVE( p_media->i_es, p_media->es, p_es );
600 vlc_mutex_unlock( &p_media->lock );
602 if( p_es->psz_rtpmap ) free( p_es->psz_rtpmap );
603 if( p_es->psz_fmtp ) free( p_es->psz_fmtp );
604 p_media->i_sdp_version++;
606 if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
607 es_format_Clean( &p_es->fmt );
610 /****************************************************************************
611 * RTSP server implementation
612 ****************************************************************************/
613 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
615 rtsp_client_t *p_rtsp = malloc( sizeof(rtsp_client_t) );
617 if( !p_rtsp ) return NULL;
618 memset( p_rtsp, 0, sizeof(rtsp_client_t) );
621 p_rtsp->psz_session = psz_session;
622 TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
624 p_media->p_vod->p_sys->i_connections++;
625 msg_Dbg( p_media->p_vod, "new session: %s, connections: %d",
626 psz_session, p_media->p_vod->p_sys->i_throttle_users );
631 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, char *psz_session )
635 for( i = 0; psz_session && i < p_media->i_rtsp; i++ )
637 if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
639 return p_media->rtsp[i];
646 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
648 p_media->p_vod->p_sys->i_connections--;
649 msg_Dbg( p_media->p_vod, "closing session: %s, connections: %d",
650 p_rtsp->psz_session, p_media->p_vod->p_sys->i_throttle_users );
652 while( p_rtsp->i_es-- )
654 if( p_rtsp->es[p_rtsp->i_es]->psz_ip )
655 free( p_rtsp->es[p_rtsp->i_es]->psz_ip );
656 free( p_rtsp->es[p_rtsp->i_es] );
657 if( !p_rtsp->i_es ) free( p_rtsp->es );
660 TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, p_rtsp );
662 free( p_rtsp->psz_session );
666 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
667 httpd_message_t *answer, httpd_message_t *query )
669 vod_media_t *p_media = (vod_media_t*)p_args;
670 vod_t *p_vod = p_media->p_vod;
671 char *psz_transport = NULL;
672 char *psz_playnow = NULL; /* support option: x-playNow */
673 char *psz_session = NULL;
674 char *psz_cseq = NULL;
675 rtsp_client_t *p_rtsp;
679 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
681 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
683 answer->i_proto = HTTPD_PROTO_RTSP;
684 answer->i_version = query->i_version;
685 answer->i_type = HTTPD_MSG_ANSWER;
687 answer->p_body = NULL;
689 switch( query->i_type )
691 case HTTPD_MSG_SETUP:
693 psz_playnow = httpd_MsgGet( query, "x-playNow" );
694 psz_transport = httpd_MsgGet( query, "Transport" );
695 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
697 if( strstr( psz_transport, "unicast" ) &&
698 strstr( psz_transport, "client_port=" ) )
700 rtsp_client_t *p_rtsp;
701 char ip[NI_MAXNUMERICHOST];
702 i_port = atoi( strstr( psz_transport, "client_port=" ) +
703 strlen("client_port=") );
705 if( strstr( psz_transport, "MP2T/H2221/UDP" ) ||
706 strstr( psz_transport, "RAW/RAW/UDP" ) )
708 p_media->psz_mux = p_vod->p_sys->psz_raw_mux;
709 p_media->b_raw = VLC_TRUE;
712 if( httpd_ClientIP( cl, ip ) == NULL )
714 answer->i_status = 500;
715 answer->psz_status = strdup( "Internal server error" );
717 answer->p_body = NULL;
721 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
724 psz_session = httpd_MsgGet( query, "Session" );
725 if( !psz_session || !*psz_session )
727 if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
728 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
730 answer->i_status = 503;
731 answer->psz_status = strdup( "Too many connections" );
733 answer->p_body = NULL;
736 asprintf( &psz_session, "%d", rand() );
737 p_rtsp = RtspClientNew( p_media, psz_session );
741 p_rtsp = RtspClientGet( p_media, psz_session );
744 answer->i_status = 454;
745 answer->psz_status = strdup( "Unknown session id" );
747 answer->p_body = NULL;
752 answer->i_status = 200;
753 answer->psz_status = strdup( "OK" );
755 answer->p_body = NULL;
759 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
761 httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
762 i_port, i_port + 1 );
764 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
766 httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
767 i_port, i_port + 1 );
771 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
772 i_port, i_port + 1 );
774 else /* TODO strstr( psz_transport, "interleaved" ) ) */
776 answer->i_status = 461;
777 answer->psz_status = strdup( "Unsupported Transport" );
779 answer->p_body = NULL;
782 /* Intentional fall-through on x-playNow option in RTSP request */
789 char *psz_output, ip[NI_MAXNUMERICHOST];
790 int i, i_port_audio = 0, i_port_video = 0;
792 /* for now only multicast so easy */
795 answer->i_status = 200;
796 answer->psz_status = strdup( "OK" );
798 answer->p_body = NULL;
802 psz_session = httpd_MsgGet( query, "Session" );
803 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
805 p_rtsp = RtspClientGet( p_media, psz_session );
808 if( p_rtsp->b_playing )
810 char *psz_position = httpd_MsgGet( query, "Range" );
811 char *psz_scale = httpd_MsgGet( query, "Scale" );
812 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
813 if( psz_position && !psz_scale )
818 msg_Dbg( p_vod, "seeking request: %s", psz_position );
820 /* FIXME: npt= is not necessarily formatted as a float */
821 f_pos = us_strtod( psz_position, &end );
822 if( end > psz_position )
824 f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
825 vod_MediaControl( p_vod, p_media, psz_session,
826 VOD_MEDIA_SEEK, f_pos );
832 double f_scale = 0.0;
835 f_scale = us_strtod( psz_scale, &end );
836 if( end > psz_scale )
838 f_scale = (f_scale * 30.0);
839 if( psz_scale[0] == '-' ) /* rewind */
841 msg_Dbg( p_vod, "rewind request: %s", psz_scale );
842 vod_MediaControl( p_vod, p_media, psz_session,
843 VOD_MEDIA_REWIND, f_scale );
845 else if(psz_scale[0] != '1' ) /* fast-forward */
847 msg_Dbg( p_vod, "fastforward request: %s", psz_scale );
848 vod_MediaControl( p_vod, p_media, psz_session,
849 VOD_MEDIA_FORWARD, f_scale );
852 if( p_rtsp->b_paused == VLC_TRUE )
854 p_rtsp->b_paused = VLC_FALSE;
855 vod_MediaControl( p_vod, p_media, psz_session,
856 VOD_MEDIA_PAUSE, psz_output );
863 if( p_rtsp->b_playing && p_rtsp->b_paused )
865 vod_MediaControl( p_vod, p_media, psz_session,
867 p_rtsp->b_paused = VLC_FALSE;
870 else if( p_rtsp->b_playing ) break;
872 if( httpd_ClientIP( cl, ip ) == NULL ) break;
874 p_rtsp->b_playing = VLC_TRUE;
876 /* FIXME for != 1 video and 1 audio */
877 for( i = 0; i < p_rtsp->i_es; i++ )
879 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == AUDIO_ES )
880 i_port_audio = p_rtsp->es[i]->i_port;
881 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == VIDEO_ES )
882 i_port_video = p_rtsp->es[i]->i_port;
885 if( p_media->psz_mux )
889 asprintf( &psz_output, "std{access=udp,dst=%s:%i,mux=%s}",
890 ip, i_port, p_media->psz_mux );
894 asprintf( &psz_output, "rtp{dst=%s,port=%i,mux=%s}",
895 ip, i_port_video, p_media->psz_mux );
900 asprintf( &psz_output, "rtp{dst=%s,port-video=%i,"
901 "port-audio=%i}", ip, i_port_video, i_port_audio );
904 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PLAY,
910 case HTTPD_MSG_DESCRIBE:
913 SDPGenerate( p_media, cl );
915 if( psz_sdp != NULL )
917 answer->i_status = 200;
918 answer->psz_status = strdup( "OK" );
919 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
921 answer->p_body = (uint8_t *)psz_sdp;
922 answer->i_body = strlen( psz_sdp );
926 answer->i_status = 500;
927 answer->psz_status = strdup( "Internal server error" );
928 answer->p_body = NULL;
934 case HTTPD_MSG_PAUSE:
935 psz_session = httpd_MsgGet( query, "Session" );
936 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
938 p_rtsp = RtspClientGet( p_media, psz_session );
941 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
942 p_rtsp->b_paused = VLC_TRUE;
944 answer->i_status = 200;
945 answer->psz_status = strdup( "OK" );
947 answer->p_body = NULL;
950 case HTTPD_MSG_TEARDOWN:
951 /* for now only multicast so easy again */
952 answer->i_status = 200;
953 answer->psz_status = strdup( "OK" );
955 answer->p_body = NULL;
957 psz_session = httpd_MsgGet( query, "Session" );
958 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
960 p_rtsp = RtspClientGet( p_media, psz_session );
963 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_STOP );
964 RtspClientDel( p_media, p_rtsp );
967 case HTTPD_MSG_GETPARAMETER:
968 answer->i_status = 200;
969 answer->psz_status = strdup( "OK" );
971 answer->p_body = NULL;
978 httpd_MsgAdd( answer, "Server", "VLC Server" );
979 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
980 psz_cseq = httpd_MsgGet( query, "Cseq" );
981 psz_cseq ? i_cseq = atoi( psz_cseq ) : 0;
982 httpd_MsgAdd( answer, "CSeq", "%d", i_cseq );
983 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
987 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
993 static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
994 httpd_message_t *answer, httpd_message_t *query )
996 media_es_t *p_es = (media_es_t*)p_args;
997 vod_media_t *p_media = p_es->p_media;
998 vod_t *p_vod = p_media->p_vod;
999 rtsp_client_t *p_rtsp = NULL;
1000 char *psz_transport = NULL;
1001 char *psz_playnow = NULL; /* support option: x-playNow */
1002 char *psz_session = NULL;
1003 char *psz_position = NULL;
1004 char *psz_cseq = NULL;
1008 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
1010 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
1012 answer->i_proto = HTTPD_PROTO_RTSP;
1013 answer->i_version = query->i_version;
1014 answer->i_type = HTTPD_MSG_ANSWER;
1016 answer->p_body = NULL;
1018 switch( query->i_type )
1020 case HTTPD_MSG_SETUP:
1021 psz_playnow = httpd_MsgGet( query, "x-playNow" );
1022 psz_transport = httpd_MsgGet( query, "Transport" );
1024 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
1026 if( strstr( psz_transport, "unicast" ) &&
1027 strstr( psz_transport, "client_port=" ) )
1029 rtsp_client_t *p_rtsp;
1030 rtsp_client_es_t *p_rtsp_es;
1031 char ip[NI_MAXNUMERICHOST];
1032 int i_port = atoi( strstr( psz_transport, "client_port=" ) +
1033 strlen("client_port=") );
1035 if( httpd_ClientIP( cl, ip ) == NULL )
1037 answer->i_status = 500;
1038 answer->psz_status = strdup( "Internal server error" );
1040 answer->p_body = NULL;
1044 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
1047 psz_session = httpd_MsgGet( query, "Session" );
1048 if( !psz_session || !*psz_session )
1050 if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
1051 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
1053 answer->i_status = 503;
1054 answer->psz_status = strdup( "Too many connections" );
1056 answer->p_body = NULL;
1059 asprintf( &psz_session, "%d", rand() );
1060 p_rtsp = RtspClientNew( p_media, psz_session );
1064 p_rtsp = RtspClientGet( p_media, psz_session );
1067 answer->i_status = 454;
1068 answer->psz_status = strdup( "Unknown session id" );
1070 answer->p_body = NULL;
1075 p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
1076 p_rtsp_es->i_port = i_port;
1077 p_rtsp_es->psz_ip = strdup( ip );
1078 p_rtsp_es->p_media_es = p_es;
1079 TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );
1081 answer->i_status = 200;
1082 answer->psz_status = strdup( "OK" );
1084 answer->p_body = NULL;
1086 if( p_media->b_raw )
1088 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
1090 httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
1091 i_port, i_port + 1 );
1093 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1095 httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
1096 i_port, i_port + 1 );
1101 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
1102 i_port, i_port + 1 );
1105 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1107 answer->i_status = 461;
1108 answer->psz_status = strdup( "Unsupported Transport" );
1110 answer->p_body = NULL;
1113 /* Intentional fall-through on x-playNow option in RTSP request */
1117 case HTTPD_MSG_PLAY:
1118 /* This is kind of a kludge. Should we only support Aggregate
1120 psz_session = httpd_MsgGet( query, "Session" );
1121 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1123 p_rtsp = RtspClientGet( p_media, psz_session );
1125 psz_position = httpd_MsgGet( query, "Range" );
1126 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
1132 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1135 /* FIXME: npt= is not necessarily formatted as a float */
1136 f_pos = us_strtod( psz_position, &end );
1137 if( end > psz_position )
1139 f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
1140 vod_MediaControl( p_vod, p_media, psz_session,
1141 VOD_MEDIA_SEEK, f_pos );
1147 answer->i_status = 200;
1148 answer->psz_status = strdup( "OK" );
1150 answer->p_body = NULL;
1154 case HTTPD_MSG_TEARDOWN:
1155 answer->i_status = 200;
1156 answer->psz_status = strdup( "OK" );
1158 answer->p_body = NULL;
1160 psz_session = httpd_MsgGet( query, "Session" );
1161 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1163 p_rtsp = RtspClientGet( p_media, psz_session );
1164 if( !p_rtsp ) break;
1166 for( i = 0; i < p_rtsp->i_es; i++ )
1168 if( p_rtsp->es[i]->p_media_es == p_es )
1170 if( p_rtsp->es[i]->psz_ip ) free( p_rtsp->es[i]->psz_ip );
1171 TAB_REMOVE( p_rtsp->i_es, p_rtsp->es, p_rtsp->es[i] );
1178 vod_MediaControl( p_vod, p_media, psz_session,
1180 RtspClientDel( p_media, p_rtsp );
1184 case HTTPD_MSG_PAUSE:
1185 /* This is kind of a kludge. Should we only support Aggregate
1187 psz_session = httpd_MsgGet( query, "Session" );
1188 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1190 p_rtsp = RtspClientGet( p_media, psz_session );
1191 if( !p_rtsp ) break;
1193 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
1194 p_rtsp->b_paused = VLC_TRUE;
1196 answer->i_status = 200;
1197 answer->psz_status = strdup( "OK" );
1199 answer->p_body = NULL;
1203 return VLC_EGENERIC;
1207 httpd_MsgAdd( answer, "Server", "VLC Server" );
1208 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1209 psz_cseq = httpd_MsgGet( query, "Cseq" );
1211 i_cseq = atoi( psz_cseq );
1214 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1215 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1218 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1223 /*****************************************************************************
1225 * FIXME: need to be moved to a common place ?
1226 *****************************************************************************/
1227 static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
1230 char *p, *psz_sdp, ip[NI_MAXNUMERICHOST], ipv;
1231 const char *psz_control;
1233 if( httpd_ServerIP( cl, ip ) == NULL )
1236 p = strchr( ip, '%' );
1238 *p = '\0'; /* remove scope if present */
1240 ipv = ( strchr( ip, ':' ) != NULL ) ? '6' : '4';
1242 /* Calculate size */
1243 i_size = sizeof( "v=0\r\n" ) +
1244 sizeof( "o=- * * IN IP4 \r\n" ) + 10 + NI_MAXNUMERICHOST +
1245 sizeof( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
1246 sizeof( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
1247 sizeof( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
1248 sizeof( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
1249 sizeof( "c=IN IP4 0.0.0.0\r\n" ) + 20 + 10 +
1250 sizeof( "t=0 0\r\n" ) + /* FIXME */
1251 sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
1252 sizeof( "a=range:npt=0-1000000000.000\r\n" );
1254 psz_control = (ipv == '6') ? p_media->psz_rtsp_control_v6
1255 : p_media->psz_rtsp_control_v4;
1256 for( i = 0; i < p_media->i_es; i++ )
1258 media_es_t *p_es = p_media->es[i];
1260 i_size += sizeof( "m=**d*o * RTP/AVP *\r\n" ) + 19;
1261 if( p_es->psz_rtpmap )
1263 i_size += sizeof( "a=rtpmap:* *\r\n" ) +
1264 strlen( p_es->psz_rtpmap ) + 9;
1266 if( p_es->psz_fmtp )
1268 i_size += sizeof( "a=fmtp:* *\r\n" ) +
1269 strlen( p_es->psz_fmtp ) + 9;
1272 i_size += (strlen( psz_control ) + strlen( ip ) + 9) * p_media->i_es;
1274 p = psz_sdp = malloc( i_size );
1275 p += sprintf( p, "v=0\r\n" );
1276 p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
1277 p_media->i_sdp_id, p_media->i_sdp_version, ipv, ip );
1278 if( *p_media->psz_session_name )
1279 p += sprintf( p, "s=%s\r\n", p_media->psz_session_name );
1280 if( *p_media->psz_session_description )
1281 p += sprintf( p, "i=%s\r\n", p_media->psz_session_description );
1282 if( *p_media->psz_session_url )
1283 p += sprintf( p, "u=%s\r\n", p_media->psz_session_url );
1284 if( *p_media->psz_session_email )
1285 p += sprintf( p, "e=%s\r\n", p_media->psz_session_email );
1287 p += sprintf( p, "c=IN IP%c %s\r\n", ipv, ipv == '6' ? "::" : "0.0.0.0" );
1288 p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
1289 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
1291 if( p_media->i_length > 0 )
1293 lldiv_t d = lldiv( p_media->i_length / 1000, 1000 );
1294 p += sprintf( p, "a=range:npt=0-%lld.%03u\r\n", d.quot,
1298 for( i = 0; i < p_media->i_es; i++ )
1300 media_es_t *p_es = p_media->es[i];
1302 if( p_es->fmt.i_cat == AUDIO_ES )
1304 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
1305 p_es->i_port, p_es->i_payload_type );
1307 else if( p_es->fmt.i_cat == VIDEO_ES )
1309 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
1310 p_es->i_port, p_es->i_payload_type );
1317 if( p_es->psz_rtpmap )
1319 p += sprintf( p, "a=rtpmap:%d %s\r\n", p_es->i_payload_type,
1322 if( p_es->psz_fmtp )
1324 p += sprintf( p, "a=fmtp:%d %s\r\n", p_es->i_payload_type,
1328 p += sprintf( p, psz_control, ip, i );