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, *psz_position, 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 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
816 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 if( p_rtsp->b_playing && p_rtsp->b_paused )
833 vod_MediaControl( p_vod, p_media, psz_session,
835 p_rtsp->b_paused = VLC_FALSE;
838 else if( p_rtsp->b_playing ) break;
840 if( httpd_ClientIP( cl, ip ) == NULL ) break;
842 p_rtsp->b_playing = VLC_TRUE;
844 /* FIXME for != 1 video and 1 audio */
845 for( i = 0; i < p_rtsp->i_es; i++ )
847 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == AUDIO_ES )
848 i_port_audio = p_rtsp->es[i]->i_port;
849 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == VIDEO_ES )
850 i_port_video = p_rtsp->es[i]->i_port;
853 if( p_media->psz_mux )
857 asprintf( &psz_output, "std{access=udp,dst=%s:%i,mux=%s}",
858 ip, i_port, p_media->psz_mux );
862 asprintf( &psz_output, "rtp{dst=%s,port=%i,mux=%s}",
863 ip, i_port_video, p_media->psz_mux );
868 asprintf( &psz_output, "rtp{dst=%s,port-video=%i,"
869 "port-audio=%i}", ip, i_port_video, i_port_audio );
872 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PLAY,
878 case HTTPD_MSG_DESCRIBE:
881 SDPGenerate( p_media, cl );
883 if( psz_sdp != NULL )
885 answer->i_status = 200;
886 answer->psz_status = strdup( "OK" );
887 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
889 answer->p_body = (uint8_t *)psz_sdp;
890 answer->i_body = strlen( psz_sdp );
894 answer->i_status = 500;
895 answer->psz_status = strdup( "Internal server error" );
896 answer->p_body = NULL;
902 case HTTPD_MSG_PAUSE:
903 psz_session = httpd_MsgGet( query, "Session" );
904 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
906 p_rtsp = RtspClientGet( p_media, psz_session );
909 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
910 p_rtsp->b_paused = VLC_TRUE;
912 answer->i_status = 200;
913 answer->psz_status = strdup( "OK" );
915 answer->p_body = NULL;
918 case HTTPD_MSG_TEARDOWN:
919 /* for now only multicast so easy again */
920 answer->i_status = 200;
921 answer->psz_status = strdup( "OK" );
923 answer->p_body = NULL;
925 psz_session = httpd_MsgGet( query, "Session" );
926 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
928 p_rtsp = RtspClientGet( p_media, psz_session );
931 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_STOP );
932 RtspClientDel( p_media, p_rtsp );
935 case HTTPD_MSG_GETPARAMETER:
936 answer->i_status = 200;
937 answer->psz_status = strdup( "OK" );
939 answer->p_body = NULL;
946 httpd_MsgAdd( answer, "Server", "VLC Server" );
947 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
948 psz_cseq = httpd_MsgGet( query, "Cseq" );
949 psz_cseq ? i_cseq = atoi( psz_cseq ) : 0;
950 httpd_MsgAdd( answer, "CSeq", "%d", i_cseq );
951 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
955 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
961 static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
962 httpd_message_t *answer, httpd_message_t *query )
964 media_es_t *p_es = (media_es_t*)p_args;
965 vod_media_t *p_media = p_es->p_media;
966 vod_t *p_vod = p_media->p_vod;
967 rtsp_client_t *p_rtsp = NULL;
968 char *psz_transport = NULL;
969 char *psz_playnow = NULL; /* support option: x-playNow */
970 char *psz_session = NULL;
971 char *psz_position = NULL;
972 char *psz_cseq = NULL;
976 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
978 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
980 answer->i_proto = HTTPD_PROTO_RTSP;
981 answer->i_version = query->i_version;
982 answer->i_type = HTTPD_MSG_ANSWER;
984 answer->p_body = NULL;
986 switch( query->i_type )
988 case HTTPD_MSG_SETUP:
989 psz_playnow = httpd_MsgGet( query, "x-playNow" );
990 psz_transport = httpd_MsgGet( query, "Transport" );
992 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
994 if( strstr( psz_transport, "unicast" ) &&
995 strstr( psz_transport, "client_port=" ) )
997 rtsp_client_t *p_rtsp;
998 rtsp_client_es_t *p_rtsp_es;
999 char ip[NI_MAXNUMERICHOST];
1000 int i_port = atoi( strstr( psz_transport, "client_port=" ) +
1001 strlen("client_port=") );
1003 if( httpd_ClientIP( cl, ip ) == NULL )
1005 answer->i_status = 500;
1006 answer->psz_status = strdup( "Internal server error" );
1008 answer->p_body = NULL;
1012 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
1015 psz_session = httpd_MsgGet( query, "Session" );
1016 if( !psz_session || !*psz_session )
1018 if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
1019 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
1021 answer->i_status = 503;
1022 answer->psz_status = strdup( "Too many connections" );
1024 answer->p_body = NULL;
1027 asprintf( &psz_session, "%d", rand() );
1028 p_rtsp = RtspClientNew( p_media, psz_session );
1032 p_rtsp = RtspClientGet( p_media, psz_session );
1035 answer->i_status = 454;
1036 answer->psz_status = strdup( "Unknown session id" );
1038 answer->p_body = NULL;
1043 p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
1044 p_rtsp_es->i_port = i_port;
1045 p_rtsp_es->psz_ip = strdup( ip );
1046 p_rtsp_es->p_media_es = p_es;
1047 TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );
1049 answer->i_status = 200;
1050 answer->psz_status = strdup( "OK" );
1052 answer->p_body = NULL;
1054 if( p_media->b_raw )
1056 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
1058 httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
1059 i_port, i_port + 1 );
1061 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1063 httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
1064 i_port, i_port + 1 );
1069 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
1070 i_port, i_port + 1 );
1073 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1075 answer->i_status = 461;
1076 answer->psz_status = strdup( "Unsupported Transport" );
1078 answer->p_body = NULL;
1081 /* Intentional fall-through on x-playNow option in RTSP request */
1085 case HTTPD_MSG_PLAY:
1086 /* This is kind of a kludge. Should we only support Aggregate
1088 psz_session = httpd_MsgGet( query, "Session" );
1089 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1091 p_rtsp = RtspClientGet( p_media, psz_session );
1093 psz_position = httpd_MsgGet( query, "Range" );
1094 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
1100 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1103 /* FIXME: npt= is not necessarily formatted as a float */
1104 f_pos = us_strtod( psz_position, &end );
1105 if( end > psz_position )
1107 f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
1108 vod_MediaControl( p_vod, p_media, psz_session,
1109 VOD_MEDIA_SEEK, f_pos );
1115 answer->i_status = 200;
1116 answer->psz_status = strdup( "OK" );
1118 answer->p_body = NULL;
1122 case HTTPD_MSG_TEARDOWN:
1123 answer->i_status = 200;
1124 answer->psz_status = strdup( "OK" );
1126 answer->p_body = NULL;
1128 psz_session = httpd_MsgGet( query, "Session" );
1129 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1131 p_rtsp = RtspClientGet( p_media, psz_session );
1132 if( !p_rtsp ) break;
1134 for( i = 0; i < p_rtsp->i_es; i++ )
1136 if( p_rtsp->es[i]->p_media_es == p_es )
1138 if( p_rtsp->es[i]->psz_ip ) free( p_rtsp->es[i]->psz_ip );
1139 TAB_REMOVE( p_rtsp->i_es, p_rtsp->es, p_rtsp->es[i] );
1146 vod_MediaControl( p_vod, p_media, psz_session,
1148 RtspClientDel( p_media, p_rtsp );
1152 case HTTPD_MSG_PAUSE:
1153 /* This is kind of a kludge. Should we only support Aggregate
1155 psz_session = httpd_MsgGet( query, "Session" );
1156 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1158 p_rtsp = RtspClientGet( p_media, psz_session );
1159 if( !p_rtsp ) break;
1161 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
1162 p_rtsp->b_paused = VLC_TRUE;
1164 answer->i_status = 200;
1165 answer->psz_status = strdup( "OK" );
1167 answer->p_body = NULL;
1171 return VLC_EGENERIC;
1175 httpd_MsgAdd( answer, "Server", "VLC Server" );
1176 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1177 psz_cseq = httpd_MsgGet( query, "Cseq" );
1179 i_cseq = atoi( psz_cseq );
1182 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1183 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1186 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1191 /*****************************************************************************
1193 * FIXME: need to be moved to a common place ?
1194 *****************************************************************************/
1195 static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
1198 char *p, *psz_sdp, ip[NI_MAXNUMERICHOST], ipv;
1199 const char *psz_control;
1201 if( httpd_ServerIP( cl, ip ) == NULL )
1204 p = strchr( ip, '%' );
1206 *p = '\0'; /* remove scope if present */
1208 ipv = ( strchr( ip, ':' ) != NULL ) ? '6' : '4';
1210 /* Calculate size */
1211 i_size = sizeof( "v=0\r\n" ) +
1212 sizeof( "o=- * * IN IP4 \r\n" ) + 10 + NI_MAXNUMERICHOST +
1213 sizeof( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
1214 sizeof( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
1215 sizeof( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
1216 sizeof( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
1217 sizeof( "t=0 0\r\n" ) + /* FIXME */
1218 sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
1219 sizeof( "c=IN IP4 0.0.0.0\r\n" ) + 20 + 10 +
1220 sizeof( "a=range:npt=0-1000000000.000\r\n" );
1222 psz_control = (ipv == '6') ? p_media->psz_rtsp_control_v6
1223 : p_media->psz_rtsp_control_v4;
1224 for( i = 0; i < p_media->i_es; i++ )
1226 media_es_t *p_es = p_media->es[i];
1228 i_size += sizeof( "m=**d*o * RTP/AVP *\r\n" ) + 19;
1229 if( p_es->psz_rtpmap )
1231 i_size += sizeof( "a=rtpmap:* *\r\n" ) +
1232 strlen( p_es->psz_rtpmap ) + 9;
1234 if( p_es->psz_fmtp )
1236 i_size += sizeof( "a=fmtp:* *\r\n" ) +
1237 strlen( p_es->psz_fmtp ) + 9;
1240 i_size += (strlen( psz_control ) + strlen( ip ) + 9) * p_media->i_es;
1242 p = psz_sdp = malloc( i_size );
1243 p += sprintf( p, "v=0\r\n" );
1244 p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
1245 p_media->i_sdp_id, p_media->i_sdp_version, ipv, ip );
1246 if( *p_media->psz_session_name )
1247 p += sprintf( p, "s=%s\r\n", p_media->psz_session_name );
1248 if( *p_media->psz_session_description )
1249 p += sprintf( p, "i=%s\r\n", p_media->psz_session_description );
1250 if( *p_media->psz_session_url )
1251 p += sprintf( p, "u=%s\r\n", p_media->psz_session_url );
1252 if( *p_media->psz_session_email )
1253 p += sprintf( p, "e=%s\r\n", p_media->psz_session_email );
1255 p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
1256 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
1258 p += sprintf( p, "c=IN IP%c %s\r\n", ipv, ipv == '6' ? "::" : "0.0.0.0" );
1260 if( p_media->i_length > 0 )
1262 lldiv_t d = lldiv( p_media->i_length / 1000, 1000 );
1263 p += sprintf( p, "a=range:npt=0-"I64Fd".%03u\r\n", d.quot,
1267 for( i = 0; i < p_media->i_es; i++ )
1269 media_es_t *p_es = p_media->es[i];
1271 if( p_es->fmt.i_cat == AUDIO_ES )
1273 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
1274 p_es->i_port, p_es->i_payload_type );
1276 else if( p_es->fmt.i_cat == VIDEO_ES )
1278 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
1279 p_es->i_port, p_es->i_payload_type );
1286 if( p_es->psz_rtpmap )
1288 p += sprintf( p, "a=rtpmap:%d %s\r\n", p_es->i_payload_type,
1291 if( p_es->psz_fmtp )
1293 p += sprintf( p, "a=fmtp:%d %s\r\n", p_es->i_payload_type,
1297 p += sprintf( p, psz_control, ip, i );