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 *****************************************************************************/
33 #include <vlc/input.h>
36 #include "vlc_httpd.h"
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Open ( vlc_object_t * );
46 static void Close( vlc_object_t * );
48 #define HOST_TEXT N_( "RTSP host address" )
49 /// \bug [String] extra space
50 #define HOST_LONGTEXT N_( \
51 "This defines the address, port and path the RTSP VOD server will listen " \
52 "on.\nSyntax is address:port/path. The default is to listen on all "\
53 "interfaces (address 0.0.0.0), on port 554, with no path.\n To listen " \
54 "only on the local interface, use \"localhost\" as address." )
56 #define THROTLE_TEXT N_( "Maximum number of connections" )
57 #define THROTLE_LONGTEXT N_( "This limits the maximum number of clients " \
58 "that can connect to the RTSP VOD. 0 means no limit." )
60 #define RAWMUX_TEXT N_( "MUX for RAW RTSP transport" )
63 set_shortname( _("RTSP VoD" ) );
64 set_description( _("RTSP VoD server") );
65 set_category( CAT_SOUT );
66 set_subcategory( SUBCAT_SOUT_VOD );
67 set_capability( "vod server", 1 );
68 set_callbacks( Open, Close );
69 add_shortcut( "rtsp" );
70 add_string ( "rtsp-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
71 add_string( "rtsp-raw-mux", "ts", NULL, RAWMUX_TEXT, RAWMUX_TEXT, VLC_TRUE );
72 add_integer( "rtsp-throttle-users", 0, NULL, THROTLE_TEXT,
73 THROTLE_LONGTEXT, VLC_TRUE );
76 /*****************************************************************************
78 *****************************************************************************/
80 typedef struct media_es_t media_es_t;
84 media_es_t *p_media_es;
93 int64_t i_last; /* for timeout */
95 vlc_bool_t b_playing; /* is it in "play" state */
96 vlc_bool_t b_paused; /* is it in "pause" state */
99 rtsp_client_es_t **es;
109 httpd_url_t *p_rtsp_url;
111 vod_media_t *p_media;
115 uint8_t i_payload_type;
127 httpd_url_t *p_rtsp_url;
128 char *psz_rtsp_control_v4;
129 char *psz_rtsp_control_v6;
141 vlc_bool_t b_multicast;
153 rtsp_client_t **rtsp;
156 char *psz_session_name;
157 char *psz_session_description;
158 char *psz_session_url;
159 char *psz_session_email;
166 httpd_host_t *p_rtsp_host;
169 int i_throttle_users;
179 static vod_media_t *MediaNew( vod_t *, const char *, input_item_t * );
180 static void MediaDel( vod_t *, vod_media_t * );
181 static int MediaAddES( vod_t *, vod_media_t *, es_format_t * );
182 static void MediaDelES( vod_t *, vod_media_t *, es_format_t * );
184 static rtsp_client_t *RtspClientNew( vod_media_t *, char * );
185 static rtsp_client_t *RtspClientGet( vod_media_t *, char * );
186 static void RtspClientDel( vod_media_t *, rtsp_client_t * );
188 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
189 httpd_message_t *, httpd_message_t * );
190 static int RtspCallbackES( httpd_callback_sys_t *, httpd_client_t *,
191 httpd_message_t *, httpd_message_t * );
193 static char *SDPGenerate( const vod_media_t *, httpd_client_t *cl );
195 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
197 static const char hex[16] = "0123456789abcdef";
200 for( i = 0; i < i_data; i++ )
202 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
203 s[2*i+1] = hex[(p_data[i] )&0xf];
208 /*****************************************************************************
209 * Open: Starts the RTSP server module
210 *****************************************************************************/
211 static int Open( vlc_object_t *p_this )
213 vod_t *p_vod = (vod_t *)p_this;
214 vod_sys_t *p_sys = 0;
218 psz_url = config_GetPsz( p_vod, "rtsp-host" );
219 vlc_UrlParse( &url, psz_url, 0 );
220 if( psz_url ) free( psz_url );
222 if( url.i_port <= 0 ) url.i_port = 554;
224 p_vod->p_sys = p_sys = malloc( sizeof( vod_sys_t ) );
225 if( !p_sys ) goto error;
226 p_sys->p_rtsp_host = 0;
228 var_Create( p_this, "rtsp-throttle-users", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
229 p_sys->i_throttle_users = var_GetInteger( p_this, "rtsp-throtle-users" );
230 msg_Dbg( p_this, "allowing up to %d connections", p_sys->i_throttle_users );
231 p_sys->i_connections = 0;
233 var_Create( p_this, "rtsp-raw-mux", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
234 p_sys->psz_raw_mux = var_GetString( p_this, "rtsp-raw-mux" );
237 httpd_HostNew( VLC_OBJECT(p_vod), url.psz_host, url.i_port );
238 if( !p_sys->p_rtsp_host )
240 msg_Err( p_vod, "cannot create RTSP server (%s:%i)",
241 url.psz_host, url.i_port );
245 p_sys->psz_path = strdup( url.psz_path ? url.psz_path : "/" );
246 p_sys->i_port = url.i_port;
248 vlc_UrlClean( &url );
252 p_vod->pf_media_new = MediaNew;
253 p_vod->pf_media_del = MediaDel;
254 p_vod->pf_media_add_es = MediaAddES;
255 p_vod->pf_media_del_es = MediaDelES;
260 if( p_sys && p_sys->p_rtsp_host ) httpd_HostDelete( p_sys->p_rtsp_host );
261 if( p_sys && p_sys->psz_raw_mux ) free( p_sys->psz_raw_mux );
262 if( p_sys ) free( p_sys );
263 vlc_UrlClean( &url );
268 /*****************************************************************************
270 *****************************************************************************/
271 static void Close( vlc_object_t * p_this )
273 vod_t *p_vod = (vod_t *)p_this;
274 vod_sys_t *p_sys = p_vod->p_sys;
276 httpd_HostDelete( p_sys->p_rtsp_host );
277 var_Destroy( p_this, "rtsp-throttle-users" );
278 var_Destroy( p_this, "rtsp-raw-mux" );
280 /* TODO delete medias */
281 free( p_sys->psz_path );
282 free( p_sys->psz_raw_mux );
286 /*****************************************************************************
288 *****************************************************************************/
289 static vod_media_t *MediaNew( vod_t *p_vod, const char *psz_name,
290 input_item_t *p_item )
292 vod_sys_t *p_sys = p_vod->p_sys;
293 vod_media_t *p_media = malloc( sizeof(vod_media_t) );
298 msg_Err( p_vod, "not enough memory" );
302 memset( p_media, 0, sizeof(vod_media_t) );
304 p_media->psz_mux = 0;
306 p_media->b_raw = VLC_FALSE;
308 asprintf( &p_media->psz_rtsp_path, "%s%s", p_sys->psz_path, psz_name );
309 p_media->p_rtsp_url =
310 httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, NULL,
313 if( !p_media->p_rtsp_url )
315 msg_Err( p_vod, "cannot create RTSP url (%s)", p_media->psz_rtsp_path);
316 free( p_media->psz_rtsp_path );
321 msg_Dbg( p_vod, "created RTSP url: %s", p_media->psz_rtsp_path );
323 asprintf( &p_media->psz_rtsp_control_v4,
324 "a=control:rtsp://%%s:%d%s/trackID=%%d\r\n",
325 p_sys->i_port, p_media->psz_rtsp_path );
326 asprintf( &p_media->psz_rtsp_control_v6,
327 "a=control:rtsp://[%%s]:%d%s/trackID=%%d\r\n",
328 p_sys->i_port, p_media->psz_rtsp_path );
330 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_SETUP,
331 RtspCallback, (void*)p_media );
332 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_DESCRIBE,
333 RtspCallback, (void*)p_media );
334 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PLAY,
335 RtspCallback, (void*)p_media );
336 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PAUSE,
337 RtspCallback, (void*)p_media );
338 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_GETPARAMETER,
339 RtspCallback, (void*)p_media );
340 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_TEARDOWN,
341 RtspCallback, (void*)p_media );
343 p_media->p_vod = p_vod;
345 TAB_APPEND( p_sys->i_media, p_sys->media, p_media );
347 vlc_mutex_init( p_vod, &p_media->lock );
348 p_media->psz_session_name = strdup("");
349 p_media->psz_session_description = strdup("");
350 p_media->psz_session_url = strdup("");
351 p_media->psz_session_email = strdup("");
353 p_media->i_port_audio = 1234;
354 p_media->i_port_video = 1236;
355 p_media->i_port = 1238;
356 p_media->i_payload_type = 96;
358 p_media->i_sdp_id = mdate();
359 p_media->i_sdp_version = 1;
360 p_media->i_length = p_item->i_duration;
362 vlc_mutex_lock( &p_item->lock );
363 msg_Dbg( p_vod, "media has %i declared ES", p_item->i_es );
364 for( i = 0; i < p_item->i_es; i++ )
366 MediaAddES( p_vod, p_media, p_item->es[i] );
368 vlc_mutex_unlock( &p_item->lock );
373 static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
375 vod_sys_t *p_sys = p_vod->p_sys;
377 msg_Dbg( p_vod, "deleting media: %s", p_media->psz_rtsp_path );
379 while( p_media->i_rtsp > 0 ) RtspClientDel( p_media, p_media->rtsp[0] );
380 httpd_UrlDelete( p_media->p_rtsp_url );
381 if( p_media->psz_rtsp_path ) free( p_media->psz_rtsp_path );
382 if( p_media->psz_rtsp_control_v6 ) free( p_media->psz_rtsp_control_v6 );
383 if( p_media->psz_rtsp_control_v4 ) free( p_media->psz_rtsp_control_v4 );
385 TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
387 while( p_media->i_es ) MediaDelES( p_vod, p_media, &p_media->es[0]->fmt );
389 vlc_mutex_destroy( &p_media->lock );
390 free( p_media->psz_session_name );
391 free( p_media->psz_session_description );
392 free( p_media->psz_session_url );
393 free( p_media->psz_session_email );
397 static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
399 media_es_t *p_es = malloc( sizeof(media_es_t) );
402 memset( p_es, 0, sizeof(media_es_t) );
403 p_media->psz_mux = NULL;
405 /* TODO: update SDP, etc... */
406 asprintf( &psz_urlc, "%s/trackID=%d",
407 p_media->psz_rtsp_path, p_media->i_es );
408 msg_Dbg( p_vod, " - ES %4.4s (%s)", (char *)&p_fmt->i_codec, psz_urlc );
410 switch( p_fmt->i_codec )
412 case VLC_FOURCC( 's', '1', '6', 'b' ):
413 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
415 p_es->i_payload_type = 11;
417 else if( p_fmt->audio.i_channels == 2 && p_fmt->audio.i_rate == 44100 )
419 p_es->i_payload_type = 10;
423 p_es->i_payload_type = p_media->i_payload_type++;
425 p_es->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
426 sprintf( p_es->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
427 p_fmt->audio.i_channels );
429 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
430 p_es->i_payload_type = p_media->i_payload_type++;
431 p_es->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
432 sprintf( p_es->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
433 p_fmt->audio.i_channels );
435 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
436 p_es->i_payload_type = 14;
437 p_es->psz_rtpmap = strdup( "MPA/90000" );
439 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
440 p_es->i_payload_type = 32;
441 p_es->psz_rtpmap = strdup( "MPV/90000" );
443 case VLC_FOURCC( 'a', '5', '2', ' ' ):
444 p_es->i_payload_type = p_media->i_payload_type++;
445 p_es->psz_rtpmap = strdup( "ac3/90000" );
447 case VLC_FOURCC( 'H', '2', '6', '3' ):
448 p_es->i_payload_type = p_media->i_payload_type++;
449 p_es->psz_rtpmap = strdup( "H263-1998/90000" );
451 case VLC_FOURCC( 'h', '2', '6', '4' ):
452 p_es->i_payload_type = p_media->i_payload_type++;
453 p_es->psz_rtpmap = strdup( "H264/90000" );
454 p_es->psz_fmtp = strdup( "packetization-mode=1" );
456 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
457 p_es->i_payload_type = p_media->i_payload_type++;
458 p_es->psz_rtpmap = strdup( "MP4V-ES/90000" );
459 if( p_fmt->i_extra > 0 )
461 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
462 p_es->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
463 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
464 sprintf( p_es->psz_fmtp,
465 "profile-level-id=3; config=%s;", p_hexa );
469 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
470 p_es->i_payload_type = p_media->i_payload_type++;
471 p_es->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
472 sprintf( p_es->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
473 if( p_fmt->i_extra > 0 )
475 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
476 p_es->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
477 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
478 sprintf( p_es->psz_fmtp,
479 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
480 "config=%s; SizeLength=13;IndexLength=3; "
481 "IndexDeltaLength=3; Profile=1;", p_hexa );
485 case VLC_FOURCC( 'm', 'p', '2', 't' ):
486 p_media->psz_mux = "ts";
487 p_es->i_payload_type = 33;
488 p_es->psz_rtpmap = strdup( "MP2T/90000" );
490 case VLC_FOURCC( 'm', 'p', '2', 'p' ):
491 p_media->psz_mux = "ps";
492 p_es->i_payload_type = p_media->i_payload_type++;
493 p_es->psz_rtpmap = strdup( "MP2P/90000" );
495 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
496 p_es->i_payload_type = p_media->i_payload_type++;
497 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
498 "AMR/8000/2" : "AMR/8000" );
499 p_es->psz_fmtp = strdup( "octet-align=1" );
501 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
502 p_es->i_payload_type = p_media->i_payload_type++;
503 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
504 "AMR-WB/16000/2" : "AMR-WB/16000" );
505 p_es->psz_fmtp = strdup( "octet-align=1" );
509 msg_Err( p_vod, "cannot add this stream (unsupported "
510 "codec: %4.4s)", (char*)&p_fmt->i_codec );
516 httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, NULL, NULL,
519 if( !p_es->p_rtsp_url )
521 msg_Err( p_vod, "cannot create RTSP url (%s)", psz_urlc );
528 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
529 RtspCallbackES, (void*)p_es );
530 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_TEARDOWN,
531 RtspCallbackES, (void*)p_es );
532 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PLAY,
533 RtspCallbackES, (void*)p_es );
534 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PAUSE,
535 RtspCallbackES, (void*)p_es );
537 es_format_Copy( &p_es->fmt, p_fmt );
539 p_es->p_media = p_media;
542 /* Choose the port */
543 if( p_fmt->i_cat == AUDIO_ES && p_media->i_port_audio > 0 )
545 p_es->i_port = p_media->i_port_audio;
546 p_media->i_port_audio = 0;
548 else if( p_fmt->i_cat == VIDEO_ES && p_media->i_port_video > 0 )
550 p_es->i_port = p_media->i_port_video;
551 p_media->i_port_video = 0;
553 while( !p_es->i_port )
555 if( p_media->i_port != p_media->i_port_audio &&
556 p_media->i_port != p_media->i_port_video )
558 p_es->i_port = p_media->i_port;
559 p_media->i_port += 2;
562 p_media->i_port += 2;
569 vlc_mutex_lock( &p_media->lock );
570 TAB_APPEND( p_media->i_es, p_media->es, p_es );
571 vlc_mutex_unlock( &p_media->lock );
573 p_media->i_sdp_version++;
578 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
580 media_es_t *p_es = 0;
584 for( i = 0; i < p_media->i_es; i++ )
586 if( p_media->es[i]->fmt.i_cat == p_fmt->i_cat &&
587 p_media->es[i]->fmt.i_codec == p_fmt->i_codec &&
588 p_media->es[i]->fmt.i_id == p_fmt->i_id )
590 p_es = p_media->es[i];
595 msg_Dbg( p_vod, " - Removing ES %4.4s", (char *)&p_fmt->i_codec );
597 vlc_mutex_lock( &p_media->lock );
598 TAB_REMOVE( p_media->i_es, p_media->es, p_es );
599 vlc_mutex_unlock( &p_media->lock );
601 if( p_es->psz_rtpmap ) free( p_es->psz_rtpmap );
602 if( p_es->psz_fmtp ) free( p_es->psz_fmtp );
603 p_media->i_sdp_version++;
605 if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
606 es_format_Clean( &p_es->fmt );
609 /****************************************************************************
610 * RTSP server implementation
611 ****************************************************************************/
612 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
614 rtsp_client_t *p_rtsp = malloc( sizeof(rtsp_client_t) );
616 if( !p_rtsp ) return NULL;
617 memset( p_rtsp, 0, sizeof(rtsp_client_t) );
620 p_rtsp->psz_session = psz_session;
621 TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
623 p_media->p_vod->p_sys->i_connections++;
624 msg_Dbg( p_media->p_vod, "new session: %s, connections: %d",
625 psz_session, p_media->p_vod->p_sys->i_throttle_users );
630 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, char *psz_session )
634 for( i = 0; psz_session && i < p_media->i_rtsp; i++ )
636 if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
638 return p_media->rtsp[i];
645 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
647 p_media->p_vod->p_sys->i_connections--;
648 msg_Dbg( p_media->p_vod, "closing session: %s, connections: %d",
649 p_rtsp->psz_session, p_media->p_vod->p_sys->i_throttle_users );
651 while( p_rtsp->i_es-- )
653 if( p_rtsp->es[p_rtsp->i_es]->psz_ip )
654 free( p_rtsp->es[p_rtsp->i_es]->psz_ip );
655 free( p_rtsp->es[p_rtsp->i_es] );
656 if( !p_rtsp->i_es ) free( p_rtsp->es );
659 TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, p_rtsp );
661 free( p_rtsp->psz_session );
665 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
666 httpd_message_t *answer, httpd_message_t *query )
668 vod_media_t *p_media = (vod_media_t*)p_args;
669 vod_t *p_vod = p_media->p_vod;
670 char *psz_transport = NULL;
671 char *psz_playnow = NULL; /* support option: x-playNow */
672 char *psz_session = NULL;
673 char *psz_cseq = NULL;
674 rtsp_client_t *p_rtsp;
678 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
680 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
682 answer->i_proto = HTTPD_PROTO_RTSP;
683 answer->i_version = query->i_version;
684 answer->i_type = HTTPD_MSG_ANSWER;
686 answer->p_body = NULL;
688 switch( query->i_type )
690 case HTTPD_MSG_SETUP:
692 psz_playnow = httpd_MsgGet( query, "x-playNow" );
693 psz_transport = httpd_MsgGet( query, "Transport" );
694 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
696 if( strstr( psz_transport, "unicast" ) &&
697 strstr( psz_transport, "client_port=" ) )
699 rtsp_client_t *p_rtsp;
700 char ip[NI_MAXNUMERICHOST];
701 i_port = atoi( strstr( psz_transport, "client_port=" ) +
702 strlen("client_port=") );
704 if( strstr( psz_transport, "MP2T/H2221/UDP" ) ||
705 strstr( psz_transport, "RAW/RAW/UDP" ) )
707 p_media->psz_mux = p_vod->p_sys->psz_raw_mux;
708 p_media->b_raw = VLC_TRUE;
711 if( httpd_ClientIP( cl, ip ) == NULL )
713 answer->i_status = 500;
714 answer->psz_status = strdup( "Internal server error" );
716 answer->p_body = NULL;
720 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
723 psz_session = httpd_MsgGet( query, "Session" );
724 if( !psz_session || !*psz_session )
726 if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
727 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
729 answer->i_status = 503;
730 answer->psz_status = strdup( "Too many connections" );
732 answer->p_body = NULL;
735 asprintf( &psz_session, "%d", rand() );
736 p_rtsp = RtspClientNew( p_media, psz_session );
740 p_rtsp = RtspClientGet( p_media, psz_session );
743 answer->i_status = 454;
744 answer->psz_status = strdup( "Unknown session id" );
746 answer->p_body = NULL;
751 answer->i_status = 200;
752 answer->psz_status = strdup( "OK" );
754 answer->p_body = NULL;
758 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
760 httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
761 i_port, i_port + 1 );
763 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
765 httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
766 i_port, i_port + 1 );
770 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
771 i_port, i_port + 1 );
773 else /* TODO strstr( psz_transport, "interleaved" ) ) */
775 answer->i_status = 461;
776 answer->psz_status = strdup( "Unsupported Transport" );
778 answer->p_body = NULL;
781 /* Intentional fall-through on x-playNow option in RTSP request */
788 char *psz_output, ip[NI_MAXNUMERICHOST];
789 int i, i_port_audio = 0, i_port_video = 0;
791 /* for now only multicast so easy */
794 answer->i_status = 200;
795 answer->psz_status = strdup( "OK" );
797 answer->p_body = NULL;
801 psz_session = httpd_MsgGet( query, "Session" );
802 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
804 p_rtsp = RtspClientGet( p_media, psz_session );
807 if( p_rtsp->b_playing )
809 char *psz_position = httpd_MsgGet( query, "Range" );
810 char *psz_scale = httpd_MsgGet( query, "Scale" );
811 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
812 if( psz_position && !psz_scale )
817 msg_Dbg( p_vod, "seeking request: %s", psz_position );
819 /* FIXME: npt= is not necessarily formatted as a float */
820 f_pos = us_strtod( psz_position, &end );
821 if( end > psz_position )
823 f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
824 vod_MediaControl( p_vod, p_media, psz_session,
825 VOD_MEDIA_SEEK, f_pos );
831 double f_scale = 0.0;
834 f_scale = us_strtod( psz_scale, &end );
835 if( end > psz_scale )
837 f_scale = (f_scale * 30.0);
838 if( psz_scale[0] == '-' ) /* rewind */
840 msg_Dbg( p_vod, "rewind request: %s", psz_scale );
841 vod_MediaControl( p_vod, p_media, psz_session,
842 VOD_MEDIA_REWIND, f_scale );
844 else if(psz_scale[0] != '1' ) /* fast-forward */
846 msg_Dbg( p_vod, "fastforward request: %s", psz_scale );
847 vod_MediaControl( p_vod, p_media, psz_session,
848 VOD_MEDIA_FORWARD, f_scale );
851 if( p_rtsp->b_paused == VLC_TRUE )
853 p_rtsp->b_paused = VLC_FALSE;
854 vod_MediaControl( p_vod, p_media, psz_session,
855 VOD_MEDIA_PAUSE, psz_output );
862 if( p_rtsp->b_playing && p_rtsp->b_paused )
864 vod_MediaControl( p_vod, p_media, psz_session,
866 p_rtsp->b_paused = VLC_FALSE;
869 else if( p_rtsp->b_playing ) break;
871 if( httpd_ClientIP( cl, ip ) == NULL ) break;
873 p_rtsp->b_playing = VLC_TRUE;
875 /* FIXME for != 1 video and 1 audio */
876 for( i = 0; i < p_rtsp->i_es; i++ )
878 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == AUDIO_ES )
879 i_port_audio = p_rtsp->es[i]->i_port;
880 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == VIDEO_ES )
881 i_port_video = p_rtsp->es[i]->i_port;
884 if( p_media->psz_mux )
888 asprintf( &psz_output, "std{access=udp,dst=%s:%i,mux=%s}",
889 ip, i_port, p_media->psz_mux );
893 asprintf( &psz_output, "rtp{dst=%s,port=%i,mux=%s}",
894 ip, i_port_video, p_media->psz_mux );
899 asprintf( &psz_output, "rtp{dst=%s,port-video=%i,"
900 "port-audio=%i}", ip, i_port_video, i_port_audio );
903 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PLAY,
909 case HTTPD_MSG_DESCRIBE:
912 SDPGenerate( p_media, cl );
914 if( psz_sdp != NULL )
916 answer->i_status = 200;
917 answer->psz_status = strdup( "OK" );
918 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
920 answer->p_body = (uint8_t *)psz_sdp;
921 answer->i_body = strlen( psz_sdp );
925 answer->i_status = 500;
926 answer->psz_status = strdup( "Internal server error" );
927 answer->p_body = NULL;
933 case HTTPD_MSG_PAUSE:
934 psz_session = httpd_MsgGet( query, "Session" );
935 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
937 p_rtsp = RtspClientGet( p_media, psz_session );
940 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
941 p_rtsp->b_paused = VLC_TRUE;
943 answer->i_status = 200;
944 answer->psz_status = strdup( "OK" );
946 answer->p_body = NULL;
949 case HTTPD_MSG_TEARDOWN:
950 /* for now only multicast so easy again */
951 answer->i_status = 200;
952 answer->psz_status = strdup( "OK" );
954 answer->p_body = NULL;
956 psz_session = httpd_MsgGet( query, "Session" );
957 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
959 p_rtsp = RtspClientGet( p_media, psz_session );
962 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_STOP );
963 RtspClientDel( p_media, p_rtsp );
966 case HTTPD_MSG_GETPARAMETER:
967 answer->i_status = 200;
968 answer->psz_status = strdup( "OK" );
970 answer->p_body = NULL;
977 httpd_MsgAdd( answer, "Server", "VLC Server" );
978 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
979 psz_cseq = httpd_MsgGet( query, "Cseq" );
980 psz_cseq ? i_cseq = atoi( psz_cseq ) : 0;
981 httpd_MsgAdd( answer, "CSeq", "%d", i_cseq );
982 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
986 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
992 static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
993 httpd_message_t *answer, httpd_message_t *query )
995 media_es_t *p_es = (media_es_t*)p_args;
996 vod_media_t *p_media = p_es->p_media;
997 vod_t *p_vod = p_media->p_vod;
998 rtsp_client_t *p_rtsp = NULL;
999 char *psz_transport = NULL;
1000 char *psz_playnow = NULL; /* support option: x-playNow */
1001 char *psz_session = NULL;
1002 char *psz_position = NULL;
1003 char *psz_cseq = NULL;
1007 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
1009 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
1011 answer->i_proto = HTTPD_PROTO_RTSP;
1012 answer->i_version = query->i_version;
1013 answer->i_type = HTTPD_MSG_ANSWER;
1015 answer->p_body = NULL;
1017 switch( query->i_type )
1019 case HTTPD_MSG_SETUP:
1020 psz_playnow = httpd_MsgGet( query, "x-playNow" );
1021 psz_transport = httpd_MsgGet( query, "Transport" );
1023 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
1025 if( strstr( psz_transport, "unicast" ) &&
1026 strstr( psz_transport, "client_port=" ) )
1028 rtsp_client_t *p_rtsp;
1029 rtsp_client_es_t *p_rtsp_es;
1030 char ip[NI_MAXNUMERICHOST];
1031 int i_port = atoi( strstr( psz_transport, "client_port=" ) +
1032 strlen("client_port=") );
1034 if( httpd_ClientIP( cl, ip ) == NULL )
1036 answer->i_status = 500;
1037 answer->psz_status = strdup( "Internal server error" );
1039 answer->p_body = NULL;
1043 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
1046 psz_session = httpd_MsgGet( query, "Session" );
1047 if( !psz_session || !*psz_session )
1049 if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
1050 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
1052 answer->i_status = 503;
1053 answer->psz_status = strdup( "Too many connections" );
1055 answer->p_body = NULL;
1058 asprintf( &psz_session, "%d", rand() );
1059 p_rtsp = RtspClientNew( p_media, psz_session );
1063 p_rtsp = RtspClientGet( p_media, psz_session );
1066 answer->i_status = 454;
1067 answer->psz_status = strdup( "Unknown session id" );
1069 answer->p_body = NULL;
1074 p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
1075 p_rtsp_es->i_port = i_port;
1076 p_rtsp_es->psz_ip = strdup( ip );
1077 p_rtsp_es->p_media_es = p_es;
1078 TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );
1080 answer->i_status = 200;
1081 answer->psz_status = strdup( "OK" );
1083 answer->p_body = NULL;
1085 if( p_media->b_raw )
1087 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
1089 httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
1090 i_port, i_port + 1 );
1092 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1094 httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
1095 i_port, i_port + 1 );
1100 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
1101 i_port, i_port + 1 );
1104 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1106 answer->i_status = 461;
1107 answer->psz_status = strdup( "Unsupported Transport" );
1109 answer->p_body = NULL;
1112 /* Intentional fall-through on x-playNow option in RTSP request */
1116 case HTTPD_MSG_PLAY:
1117 /* This is kind of a kludge. Should we only support Aggregate
1119 psz_session = httpd_MsgGet( query, "Session" );
1120 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1122 p_rtsp = RtspClientGet( p_media, psz_session );
1124 psz_position = httpd_MsgGet( query, "Range" );
1125 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
1131 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1134 /* FIXME: npt= is not necessarily formatted as a float */
1135 f_pos = us_strtod( psz_position, &end );
1136 if( end > psz_position )
1138 f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
1139 vod_MediaControl( p_vod, p_media, psz_session,
1140 VOD_MEDIA_SEEK, f_pos );
1146 answer->i_status = 200;
1147 answer->psz_status = strdup( "OK" );
1149 answer->p_body = NULL;
1153 case HTTPD_MSG_TEARDOWN:
1154 answer->i_status = 200;
1155 answer->psz_status = strdup( "OK" );
1157 answer->p_body = NULL;
1159 psz_session = httpd_MsgGet( query, "Session" );
1160 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1162 p_rtsp = RtspClientGet( p_media, psz_session );
1163 if( !p_rtsp ) break;
1165 for( i = 0; i < p_rtsp->i_es; i++ )
1167 if( p_rtsp->es[i]->p_media_es == p_es )
1169 if( p_rtsp->es[i]->psz_ip ) free( p_rtsp->es[i]->psz_ip );
1170 TAB_REMOVE( p_rtsp->i_es, p_rtsp->es, p_rtsp->es[i] );
1177 vod_MediaControl( p_vod, p_media, psz_session,
1179 RtspClientDel( p_media, p_rtsp );
1183 case HTTPD_MSG_PAUSE:
1184 /* This is kind of a kludge. Should we only support Aggregate
1186 psz_session = httpd_MsgGet( query, "Session" );
1187 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1189 p_rtsp = RtspClientGet( p_media, psz_session );
1190 if( !p_rtsp ) break;
1192 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
1193 p_rtsp->b_paused = VLC_TRUE;
1195 answer->i_status = 200;
1196 answer->psz_status = strdup( "OK" );
1198 answer->p_body = NULL;
1202 return VLC_EGENERIC;
1206 httpd_MsgAdd( answer, "Server", "VLC Server" );
1207 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1208 psz_cseq = httpd_MsgGet( query, "Cseq" );
1210 i_cseq = atoi( psz_cseq );
1213 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1214 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1217 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1222 /*****************************************************************************
1224 * FIXME: need to be moved to a common place ?
1225 *****************************************************************************/
1226 static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
1229 char *p, *psz_sdp, ip[NI_MAXNUMERICHOST], ipv;
1230 const char *psz_control;
1232 if( httpd_ServerIP( cl, ip ) == NULL )
1235 p = strchr( ip, '%' );
1237 *p = '\0'; /* remove scope if present */
1239 ipv = ( strchr( ip, ':' ) != NULL ) ? '6' : '4';
1241 /* Calculate size */
1242 i_size = sizeof( "v=0\r\n" ) +
1243 sizeof( "o=- * * IN IP4 \r\n" ) + 10 + NI_MAXNUMERICHOST +
1244 sizeof( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
1245 sizeof( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
1246 sizeof( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
1247 sizeof( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
1248 sizeof( "c=IN IP4 0.0.0.0\r\n" ) + 20 + 10 +
1249 sizeof( "t=0 0\r\n" ) + /* FIXME */
1250 sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
1251 sizeof( "a=range:npt=0-1000000000.000\r\n" );
1253 psz_control = (ipv == '6') ? p_media->psz_rtsp_control_v6
1254 : p_media->psz_rtsp_control_v4;
1255 for( i = 0; i < p_media->i_es; i++ )
1257 media_es_t *p_es = p_media->es[i];
1259 i_size += sizeof( "m=**d*o * RTP/AVP *\r\n" ) + 19;
1260 if( p_es->psz_rtpmap )
1262 i_size += sizeof( "a=rtpmap:* *\r\n" ) +
1263 strlen( p_es->psz_rtpmap ) + 9;
1265 if( p_es->psz_fmtp )
1267 i_size += sizeof( "a=fmtp:* *\r\n" ) +
1268 strlen( p_es->psz_fmtp ) + 9;
1271 i_size += (strlen( psz_control ) + strlen( ip ) + 9) * p_media->i_es;
1273 p = psz_sdp = malloc( i_size );
1274 p += sprintf( p, "v=0\r\n" );
1275 p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
1276 p_media->i_sdp_id, p_media->i_sdp_version, ipv, ip );
1277 if( *p_media->psz_session_name )
1278 p += sprintf( p, "s=%s\r\n", p_media->psz_session_name );
1279 if( *p_media->psz_session_description )
1280 p += sprintf( p, "i=%s\r\n", p_media->psz_session_description );
1281 if( *p_media->psz_session_url )
1282 p += sprintf( p, "u=%s\r\n", p_media->psz_session_url );
1283 if( *p_media->psz_session_email )
1284 p += sprintf( p, "e=%s\r\n", p_media->psz_session_email );
1286 p += sprintf( p, "c=IN IP%c %s\r\n", ipv, ipv == '6' ? "::" : "0.0.0.0" );
1287 p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
1288 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
1290 if( p_media->i_length > 0 )
1292 lldiv_t d = lldiv( p_media->i_length / 1000, 1000 );
1293 p += sprintf( p, "a=range:npt=0-%lld.%03u\r\n", d.quot,
1297 for( i = 0; i < p_media->i_es; i++ )
1299 media_es_t *p_es = p_media->es[i];
1301 if( p_es->fmt.i_cat == AUDIO_ES )
1303 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
1304 p_es->i_port, p_es->i_payload_type );
1306 else if( p_es->fmt.i_cat == VIDEO_ES )
1308 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
1309 p_es->i_port, p_es->i_payload_type );
1316 if( p_es->psz_rtpmap )
1318 p += sprintf( p, "a=rtpmap:%d %s\r\n", p_es->i_payload_type,
1321 if( p_es->psz_fmtp )
1323 p += sprintf( p, "a=fmtp:%d %s\r\n", p_es->i_payload_type,
1327 p += sprintf( p, psz_control, ip, i );