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 #define HOST_LONGTEXT N_( \
50 "This defines the address, port and path the RTSP VOD server will listen " \
51 "on.\nSyntax is address:port/path. The default is to listen on all "\
52 "interfaces (address 0.0.0.0), on port 554, with no path.\n To listen " \
53 "only on the local interface, use \"localhost\" as address." )
55 #define THROTLE_TEXT N_( "Maximum number of connections" )
56 #define THROTLE_LONGTEXT N_( "This limits the maximum number of clients " \
57 "that can connect to the RTSP VOD. 0 means no limit." )
60 set_shortname( _("RTSP VoD" ) );
61 set_description( _("RTSP VoD server") );
62 set_category( CAT_SOUT );
63 set_subcategory( SUBCAT_SOUT_VOD );
64 set_capability( "vod server", 1 );
65 set_callbacks( Open, Close );
66 add_shortcut( "rtsp" );
67 add_string ( "rtsp-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
68 add_integer( "rtsp-throttle-users", 0, NULL, THROTLE_TEXT,
69 THROTLE_LONGTEXT, VLC_TRUE );
72 /*****************************************************************************
74 *****************************************************************************/
76 typedef struct media_es_t media_es_t;
80 media_es_t *p_media_es;
89 int64_t i_last; /* for timeout */
91 vlc_bool_t b_playing; /* is it in "play" state */
92 vlc_bool_t b_paused; /* is it in "pause" state */
95 rtsp_client_es_t **es;
105 httpd_url_t *p_rtsp_url;
107 vod_media_t *p_media;
111 uint8_t i_payload_type;
123 httpd_url_t *p_rtsp_url;
124 char *psz_rtsp_control_v4;
125 char *psz_rtsp_control_v6;
137 vlc_bool_t b_multicast;
149 rtsp_client_t **rtsp;
152 char *psz_session_name;
153 char *psz_session_description;
154 char *psz_session_url;
155 char *psz_session_email;
162 httpd_host_t *p_rtsp_host;
165 int i_throttle_users;
173 static vod_media_t *MediaNew( vod_t *, const char *, input_item_t * );
174 static void MediaDel( vod_t *, vod_media_t * );
175 static int MediaAddES( vod_t *, vod_media_t *, es_format_t * );
176 static void MediaDelES( vod_t *, vod_media_t *, es_format_t * );
178 static rtsp_client_t *RtspClientNew( vod_media_t *, char * );
179 static rtsp_client_t *RtspClientGet( vod_media_t *, char * );
180 static void RtspClientDel( vod_media_t *, rtsp_client_t * );
182 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
183 httpd_message_t *, httpd_message_t * );
184 static int RtspCallbackES( httpd_callback_sys_t *, httpd_client_t *,
185 httpd_message_t *, httpd_message_t * );
187 static char *SDPGenerate( const vod_media_t *, httpd_client_t *cl );
189 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
191 static const char hex[16] = "0123456789abcdef";
194 for( i = 0; i < i_data; i++ )
196 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
197 s[2*i+1] = hex[(p_data[i] )&0xf];
202 /*****************************************************************************
203 * Open: Starts the RTSP server module
204 *****************************************************************************/
205 static int Open( vlc_object_t *p_this )
207 vod_t *p_vod = (vod_t *)p_this;
208 vod_sys_t *p_sys = 0;
212 psz_url = config_GetPsz( p_vod, "rtsp-host" );
213 vlc_UrlParse( &url, psz_url, 0 );
214 if( psz_url ) free( psz_url );
216 if( url.i_port <= 0 ) url.i_port = 554;
218 p_vod->p_sys = p_sys = malloc( sizeof( vod_sys_t ) );
219 if( !p_sys ) goto error;
220 p_sys->p_rtsp_host = 0;
222 var_Create( p_this, "rtsp-throttle-users", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
223 p_sys->i_throttle_users = var_GetInteger( p_this, "rtsp-throtle-users" );
224 msg_Dbg( p_this, "allowing up to %d connections", p_sys->i_throttle_users );
225 p_sys->i_connections = 0;
228 httpd_HostNew( VLC_OBJECT(p_vod), url.psz_host, url.i_port );
229 if( !p_sys->p_rtsp_host )
231 msg_Err( p_vod, "cannot create RTSP server (%s:%i)",
232 url.psz_host, url.i_port );
236 p_sys->psz_path = strdup( url.psz_path ? url.psz_path : "/" );
237 p_sys->i_port = url.i_port;
239 vlc_UrlClean( &url );
243 p_vod->pf_media_new = MediaNew;
244 p_vod->pf_media_del = MediaDel;
245 p_vod->pf_media_add_es = MediaAddES;
246 p_vod->pf_media_del_es = MediaDelES;
251 if( p_sys && p_sys->p_rtsp_host ) httpd_HostDelete( p_sys->p_rtsp_host );
252 if( p_sys ) free( p_sys );
253 vlc_UrlClean( &url );
258 /*****************************************************************************
260 *****************************************************************************/
261 static void Close( vlc_object_t * p_this )
263 vod_t *p_vod = (vod_t *)p_this;
264 vod_sys_t *p_sys = p_vod->p_sys;
266 httpd_HostDelete( p_sys->p_rtsp_host );
267 var_Destroy( p_this, "rtsp-throttle-users" );
269 /* TODO delete medias */
270 free( p_sys->psz_path );
274 /*****************************************************************************
276 *****************************************************************************/
277 static vod_media_t *MediaNew( vod_t *p_vod, const char *psz_name,
278 input_item_t *p_item )
280 vod_sys_t *p_sys = p_vod->p_sys;
281 vod_media_t *p_media = malloc( sizeof(vod_media_t) );
286 msg_Err( p_vod, "not enough memory" );
290 memset( p_media, 0, sizeof(vod_media_t) );
292 p_media->psz_mux = 0;
294 p_media->b_raw = VLC_FALSE;
296 asprintf( &p_media->psz_rtsp_path, "%s%s", p_sys->psz_path, psz_name );
297 p_media->p_rtsp_url =
298 httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, NULL,
301 if( !p_media->p_rtsp_url )
303 msg_Err( p_vod, "cannot create RTSP url (%s)", p_media->psz_rtsp_path);
304 free( p_media->psz_rtsp_path );
309 msg_Dbg( p_vod, "created RTSP url: %s", p_media->psz_rtsp_path );
311 asprintf( &p_media->psz_rtsp_control_v4,
312 "a=control:rtsp://%%s:%d%s/trackID=%%d\r\n",
313 p_sys->i_port, p_media->psz_rtsp_path );
314 asprintf( &p_media->psz_rtsp_control_v6,
315 "a=control:rtsp://[%%s]:%d%s/trackID=%%d\r\n",
316 p_sys->i_port, p_media->psz_rtsp_path );
318 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_SETUP,
319 RtspCallback, (void*)p_media );
320 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_DESCRIBE,
321 RtspCallback, (void*)p_media );
322 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PLAY,
323 RtspCallback, (void*)p_media );
324 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PAUSE,
325 RtspCallback, (void*)p_media );
326 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_TEARDOWN,
327 RtspCallback, (void*)p_media );
329 p_media->p_vod = p_vod;
331 TAB_APPEND( p_sys->i_media, p_sys->media, p_media );
333 vlc_mutex_init( p_vod, &p_media->lock );
334 p_media->psz_session_name = strdup("");
335 p_media->psz_session_description = strdup("");
336 p_media->psz_session_url = strdup("");
337 p_media->psz_session_email = strdup("");
339 p_media->i_port_audio = 1234;
340 p_media->i_port_video = 1236;
341 p_media->i_port = 1238;
342 p_media->i_payload_type = 96;
344 p_media->i_sdp_id = mdate();
345 p_media->i_sdp_version = 1;
346 p_media->i_length = p_item->i_duration;
348 vlc_mutex_lock( &p_item->lock );
349 msg_Dbg( p_vod, "media has %i declared ES", p_item->i_es );
350 for( i = 0; i < p_item->i_es; i++ )
352 MediaAddES( p_vod, p_media, p_item->es[i] );
354 vlc_mutex_unlock( &p_item->lock );
359 static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
361 vod_sys_t *p_sys = p_vod->p_sys;
363 msg_Dbg( p_vod, "deleting media: %s", p_media->psz_rtsp_path );
365 while( p_media->i_rtsp > 0 ) RtspClientDel( p_media, p_media->rtsp[0] );
366 httpd_UrlDelete( p_media->p_rtsp_url );
367 if( p_media->psz_rtsp_path ) free( p_media->psz_rtsp_path );
368 if( p_media->psz_rtsp_control_v6 ) free( p_media->psz_rtsp_control_v6 );
369 if( p_media->psz_rtsp_control_v4 ) free( p_media->psz_rtsp_control_v4 );
371 TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
373 while( p_media->i_es ) MediaDelES( p_vod, p_media, &p_media->es[0]->fmt );
375 vlc_mutex_destroy( &p_media->lock );
376 free( p_media->psz_session_name );
377 free( p_media->psz_session_description );
378 free( p_media->psz_session_url );
379 free( p_media->psz_session_email );
383 static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
385 media_es_t *p_es = malloc( sizeof(media_es_t) );
388 memset( p_es, 0, sizeof(media_es_t) );
389 p_media->psz_mux = NULL;
391 /* TODO: update SDP, etc... */
392 asprintf( &psz_urlc, "%s/trackID=%d",
393 p_media->psz_rtsp_path, p_media->i_es );
394 msg_Dbg( p_vod, " - ES %4.4s (%s)", (char *)&p_fmt->i_codec, psz_urlc );
396 switch( p_fmt->i_codec )
398 case VLC_FOURCC( 's', '1', '6', 'b' ):
399 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
401 p_es->i_payload_type = 11;
403 else if( p_fmt->audio.i_channels == 2 && p_fmt->audio.i_rate == 44100 )
405 p_es->i_payload_type = 10;
409 p_es->i_payload_type = p_media->i_payload_type++;
411 p_es->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
412 sprintf( p_es->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
413 p_fmt->audio.i_channels );
415 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
416 p_es->i_payload_type = p_media->i_payload_type++;
417 p_es->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
418 sprintf( p_es->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
419 p_fmt->audio.i_channels );
421 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
422 p_es->i_payload_type = 14;
423 p_es->psz_rtpmap = strdup( "MPA/90000" );
425 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
426 p_es->i_payload_type = 32;
427 p_es->psz_rtpmap = strdup( "MPV/90000" );
429 case VLC_FOURCC( 'a', '5', '2', ' ' ):
430 p_es->i_payload_type = p_media->i_payload_type++;
431 p_es->psz_rtpmap = strdup( "ac3/90000" );
433 case VLC_FOURCC( 'H', '2', '6', '3' ):
434 p_es->i_payload_type = p_media->i_payload_type++;
435 p_es->psz_rtpmap = strdup( "H263-1998/90000" );
437 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
438 p_es->i_payload_type = p_media->i_payload_type++;
439 p_es->psz_rtpmap = strdup( "MP4V-ES/90000" );
440 if( p_fmt->i_extra > 0 )
442 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
443 p_es->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
444 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
445 sprintf( p_es->psz_fmtp,
446 "profile-level-id=3; config=%s;", p_hexa );
450 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
451 p_es->i_payload_type = p_media->i_payload_type++;
452 p_es->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
453 sprintf( p_es->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
454 if( p_fmt->i_extra > 0 )
456 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
457 p_es->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
458 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
459 sprintf( p_es->psz_fmtp,
460 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
461 "config=%s; SizeLength=13;IndexLength=3; "
462 "IndexDeltaLength=3; Profile=1;", p_hexa );
466 case VLC_FOURCC( 'm', 'p', '2', 't' ):
467 p_media->psz_mux = "ts";
468 p_es->i_payload_type = 33;
469 p_es->psz_rtpmap = strdup( "MP2T/90000" );
471 case VLC_FOURCC( 'm', 'p', '2', 'p' ):
472 p_media->psz_mux = "ps";
473 p_es->i_payload_type = p_media->i_payload_type++;
474 p_es->psz_rtpmap = strdup( "MP2P/90000" );
476 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
477 p_es->i_payload_type = p_media->i_payload_type++;
478 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
479 "AMR/8000/2" : "AMR/8000" );
480 p_es->psz_fmtp = strdup( "octet-align=1" );
482 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
483 p_es->i_payload_type = p_media->i_payload_type++;
484 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
485 "AMR-WB/16000/2" : "AMR-WB/16000" );
486 p_es->psz_fmtp = strdup( "octet-align=1" );
490 msg_Err( p_vod, "cannot add this stream (unsupported "
491 "codec: %4.4s)", (char*)&p_fmt->i_codec );
497 httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, NULL, NULL,
500 if( !p_es->p_rtsp_url )
502 msg_Err( p_vod, "cannot create RTSP url (%s)", psz_urlc );
509 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
510 RtspCallbackES, (void*)p_es );
511 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_TEARDOWN,
512 RtspCallbackES, (void*)p_es );
513 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PLAY,
514 RtspCallbackES, (void*)p_es );
515 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PAUSE,
516 RtspCallbackES, (void*)p_es );
518 es_format_Copy( &p_es->fmt, p_fmt );
520 p_es->p_media = p_media;
523 /* Choose the port */
524 if( p_fmt->i_cat == AUDIO_ES && p_media->i_port_audio > 0 )
526 p_es->i_port = p_media->i_port_audio;
527 p_media->i_port_audio = 0;
529 else if( p_fmt->i_cat == VIDEO_ES && p_media->i_port_video > 0 )
531 p_es->i_port = p_media->i_port_video;
532 p_media->i_port_video = 0;
534 while( !p_es->i_port )
536 if( p_media->i_port != p_media->i_port_audio &&
537 p_media->i_port != p_media->i_port_video )
539 p_es->i_port = p_media->i_port;
540 p_media->i_port += 2;
543 p_media->i_port += 2;
550 vlc_mutex_lock( &p_media->lock );
551 TAB_APPEND( p_media->i_es, p_media->es, p_es );
552 vlc_mutex_unlock( &p_media->lock );
554 p_media->i_sdp_version++;
559 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
561 media_es_t *p_es = 0;
565 for( i = 0; i < p_media->i_es; i++ )
567 if( p_media->es[i]->fmt.i_cat == p_fmt->i_cat &&
568 p_media->es[i]->fmt.i_codec == p_fmt->i_codec &&
569 p_media->es[i]->fmt.i_id == p_fmt->i_id )
571 p_es = p_media->es[i];
576 msg_Dbg( p_vod, " - Removing ES %4.4s", (char *)&p_fmt->i_codec );
578 vlc_mutex_lock( &p_media->lock );
579 TAB_REMOVE( p_media->i_es, p_media->es, p_es );
580 vlc_mutex_unlock( &p_media->lock );
582 if( p_es->psz_rtpmap ) free( p_es->psz_rtpmap );
583 if( p_es->psz_fmtp ) free( p_es->psz_fmtp );
584 p_media->i_sdp_version++;
586 if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
587 es_format_Clean( &p_es->fmt );
590 /****************************************************************************
591 * RTSP server implementation
592 ****************************************************************************/
593 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
595 rtsp_client_t *p_rtsp = malloc( sizeof(rtsp_client_t) );
597 if( !p_rtsp ) return NULL;
598 memset( p_rtsp, 0, sizeof(rtsp_client_t) );
601 p_rtsp->psz_session = psz_session;
602 TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
604 p_media->p_vod->p_sys->i_connections++;
605 msg_Dbg( p_media->p_vod, "new session: %s, connections: %d",
606 psz_session, p_media->p_vod->p_sys->i_throttle_users );
611 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, char *psz_session )
615 for( i = 0; psz_session && i < p_media->i_rtsp; i++ )
617 if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
619 return p_media->rtsp[i];
626 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
628 p_media->p_vod->p_sys->i_connections--;
629 msg_Dbg( p_media->p_vod, "closing session: %s, connections: %d",
630 p_rtsp->psz_session, p_media->p_vod->p_sys->i_throttle_users );
632 while( p_rtsp->i_es-- )
634 if( p_rtsp->es[p_rtsp->i_es]->psz_ip )
635 free( p_rtsp->es[p_rtsp->i_es]->psz_ip );
636 free( p_rtsp->es[p_rtsp->i_es] );
637 if( !p_rtsp->i_es ) free( p_rtsp->es );
640 TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, p_rtsp );
642 free( p_rtsp->psz_session );
646 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
647 httpd_message_t *answer, httpd_message_t *query )
649 vod_media_t *p_media = (vod_media_t*)p_args;
650 vod_t *p_vod = p_media->p_vod;
651 char *psz_transport = NULL;
652 char *psz_playnow = NULL; /* support option: x-playNow */
653 char *psz_session = NULL;
654 rtsp_client_t *p_rtsp;
657 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
659 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
661 answer->i_proto = HTTPD_PROTO_RTSP;
662 answer->i_version = query->i_version;
663 answer->i_type = HTTPD_MSG_ANSWER;
665 answer->p_body = NULL;
667 switch( query->i_type )
669 case HTTPD_MSG_SETUP:
671 psz_playnow = httpd_MsgGet( query, "x-playNow" );
672 psz_transport = httpd_MsgGet( query, "Transport" );
673 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
675 if( strstr( psz_transport, "unicast" ) &&
676 strstr( psz_transport, "client_port=" ) )
678 rtsp_client_t *p_rtsp;
679 char ip[NI_MAXNUMERICHOST];
680 i_port = atoi( strstr( psz_transport, "client_port=" ) +
681 strlen("client_port=") );
683 if( strstr( psz_transport, "MP2T/H2221/UDP" ) ||
684 strstr( psz_transport, "RAW/RAW/UDP" ) )
686 p_media->b_raw = VLC_TRUE;
689 if( httpd_ClientIP( cl, ip ) == NULL )
691 answer->i_status = 500;
692 answer->psz_status = strdup( "Internal server error" );
694 answer->p_body = NULL;
698 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
701 psz_session = httpd_MsgGet( query, "Session" );
702 if( !psz_session || !*psz_session )
704 if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
705 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
707 answer->i_status = 503;
708 answer->psz_status = strdup( "Too many connections" );
710 answer->p_body = NULL;
713 asprintf( &psz_session, "%d", rand() );
714 p_rtsp = RtspClientNew( p_media, psz_session );
718 p_rtsp = RtspClientGet( p_media, psz_session );
721 answer->i_status = 454;
722 answer->psz_status = strdup( "Unknown session id" );
724 answer->p_body = NULL;
729 answer->i_status = 200;
730 answer->psz_status = strdup( "OK" );
732 answer->p_body = NULL;
736 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
738 httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
739 i_port, i_port + 1 );
741 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
743 httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
744 i_port, i_port + 1 );
748 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
749 i_port, i_port + 1 );
751 else /* TODO strstr( psz_transport, "interleaved" ) ) */
753 answer->i_status = 461;
754 answer->psz_status = strdup( "Unsupported Transport" );
756 answer->p_body = NULL;
759 if( !psz_playnow || !*psz_playnow )
765 char *psz_output, ip[NI_MAXNUMERICHOST];
766 int i, i_port_audio = 0, i_port_video = 0;
768 /* for now only multicast so easy */
771 answer->i_status = 200;
772 answer->psz_status = strdup( "OK" );
774 answer->p_body = NULL;
778 psz_session = httpd_MsgGet( query, "Session" );
779 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
781 p_rtsp = RtspClientGet( p_media, psz_session );
784 if( p_rtsp->b_playing && p_rtsp->b_paused )
786 vod_MediaControl( p_vod, p_media, psz_session,
788 p_rtsp->b_paused = VLC_FALSE;
791 else if( p_rtsp->b_playing ) break;
793 if( httpd_ClientIP( cl, ip ) == NULL ) break;
795 p_rtsp->b_playing = VLC_TRUE;
797 /* FIXME for != 1 video and 1 audio */
798 for( i = 0; i < p_rtsp->i_es; i++ )
800 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == AUDIO_ES )
801 i_port_audio = p_rtsp->es[i]->i_port;
802 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == VIDEO_ES )
803 i_port_video = p_rtsp->es[i]->i_port;
806 if( p_media->psz_mux )
810 asprintf( &psz_output, "std{access=udp,dst=%s:%i,mux=%s}",
811 ip, i_port, p_media->psz_mux );
815 asprintf( &psz_output, "rtp{dst=%s,port=%i,mux=%s}",
816 ip, i_port_video, p_media->psz_mux );
821 asprintf( &psz_output, "rtp{dst=%s,port-video=%i,"
822 "port-audio=%i}", ip, i_port_video, i_port_audio );
825 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PLAY,
831 case HTTPD_MSG_DESCRIBE:
834 SDPGenerate( p_media, cl );
836 if( psz_sdp != NULL )
838 answer->i_status = 200;
839 answer->psz_status = strdup( "OK" );
840 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
842 answer->p_body = (uint8_t *)psz_sdp;
843 answer->i_body = strlen( psz_sdp );
847 answer->i_status = 500;
848 answer->psz_status = strdup( "Internal server error" );
849 answer->p_body = NULL;
855 case HTTPD_MSG_PAUSE:
856 psz_session = httpd_MsgGet( query, "Session" );
857 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
859 p_rtsp = RtspClientGet( p_media, psz_session );
862 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
863 p_rtsp->b_paused = VLC_TRUE;
865 answer->i_status = 200;
866 answer->psz_status = strdup( "OK" );
868 answer->p_body = NULL;
871 case HTTPD_MSG_TEARDOWN:
872 /* for now only multicast so easy again */
873 answer->i_status = 200;
874 answer->psz_status = strdup( "OK" );
876 answer->p_body = NULL;
878 psz_session = httpd_MsgGet( query, "Session" );
879 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
881 p_rtsp = RtspClientGet( p_media, psz_session );
884 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_STOP );
885 RtspClientDel( p_media, p_rtsp );
892 httpd_MsgAdd( answer, "Server", "VLC Server" );
893 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
894 httpd_MsgAdd( answer, "Cseq", "%d",
895 atoi( httpd_MsgGet( query, "Cseq" ) ) );
896 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
900 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
906 static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
907 httpd_message_t *answer, httpd_message_t *query )
909 media_es_t *p_es = (media_es_t*)p_args;
910 vod_media_t *p_media = p_es->p_media;
911 vod_t *p_vod = p_media->p_vod;
912 rtsp_client_t *p_rtsp = NULL;
913 char *psz_transport = NULL;
914 char *psz_playnow = NULL; /* support option: x-playNow */
915 char *psz_session = NULL;
916 char *psz_position = NULL;
919 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
921 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
923 answer->i_proto = HTTPD_PROTO_RTSP;
924 answer->i_version = query->i_version;
925 answer->i_type = HTTPD_MSG_ANSWER;
927 answer->p_body = NULL;
929 switch( query->i_type )
931 case HTTPD_MSG_SETUP:
932 psz_playnow = httpd_MsgGet( query, "x-playNow" );
933 psz_transport = httpd_MsgGet( query, "Transport" );
935 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
937 if( strstr( psz_transport, "unicast" ) &&
938 strstr( psz_transport, "client_port=" ) )
940 rtsp_client_t *p_rtsp;
941 rtsp_client_es_t *p_rtsp_es;
942 char ip[NI_MAXNUMERICHOST];
943 int i_port = atoi( strstr( psz_transport, "client_port=" ) +
944 strlen("client_port=") );
946 if( httpd_ClientIP( cl, ip ) == NULL )
948 answer->i_status = 500;
949 answer->psz_status = strdup( "Internal server error" );
951 answer->p_body = NULL;
955 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
958 psz_session = httpd_MsgGet( query, "Session" );
959 if( !psz_session || !*psz_session )
961 if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
962 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
964 answer->i_status = 503;
965 answer->psz_status = strdup( "Too many connections" );
967 answer->p_body = NULL;
970 asprintf( &psz_session, "%d", rand() );
971 p_rtsp = RtspClientNew( p_media, psz_session );
975 p_rtsp = RtspClientGet( p_media, psz_session );
978 answer->i_status = 454;
979 answer->psz_status = strdup( "Unknown session id" );
981 answer->p_body = NULL;
986 p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
987 p_rtsp_es->i_port = i_port;
988 p_rtsp_es->psz_ip = strdup( ip );
989 p_rtsp_es->p_media_es = p_es;
990 TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );
992 answer->i_status = 200;
993 answer->psz_status = strdup( "OK" );
995 answer->p_body = NULL;
999 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
1001 httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
1002 i_port, i_port + 1 );
1004 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1006 httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
1007 i_port, i_port + 1 );
1012 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
1013 i_port, i_port + 1 );
1016 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1018 answer->i_status = 461;
1019 answer->psz_status = strdup( "Unsupported Transport" );
1021 answer->p_body = NULL;
1024 if( !psz_playnow || !*psz_playnow )
1027 case HTTPD_MSG_PLAY:
1028 /* This is kind of a kludge. Should we only support Aggregate
1030 psz_session = httpd_MsgGet( query, "Session" );
1031 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1033 p_rtsp = RtspClientGet( p_media, psz_session );
1035 psz_position = httpd_MsgGet( query, "Range" );
1036 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
1042 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1045 /* FIXME: npt= is not necessarily formatted as a float */
1046 f_pos = us_strtod( psz_position, &end );
1047 if( end > psz_position )
1049 f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
1050 vod_MediaControl( p_vod, p_media, psz_session,
1051 VOD_MEDIA_SEEK, f_pos );
1057 answer->i_status = 200;
1058 answer->psz_status = strdup( "OK" );
1060 answer->p_body = NULL;
1064 case HTTPD_MSG_TEARDOWN:
1065 answer->i_status = 200;
1066 answer->psz_status = strdup( "OK" );
1068 answer->p_body = NULL;
1070 psz_session = httpd_MsgGet( query, "Session" );
1071 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1073 p_rtsp = RtspClientGet( p_media, psz_session );
1074 if( !p_rtsp ) break;
1076 for( i = 0; i < p_rtsp->i_es; i++ )
1078 if( p_rtsp->es[i]->p_media_es == p_es )
1080 if( p_rtsp->es[i]->psz_ip ) free( p_rtsp->es[i]->psz_ip );
1081 TAB_REMOVE( p_rtsp->i_es, p_rtsp->es, p_rtsp->es[i] );
1088 vod_MediaControl( p_vod, p_media, psz_session,
1090 RtspClientDel( p_media, p_rtsp );
1094 case HTTPD_MSG_PAUSE:
1095 /* This is kind of a kludge. Should we only support Aggregate
1097 psz_session = httpd_MsgGet( query, "Session" );
1098 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1100 p_rtsp = RtspClientGet( p_media, psz_session );
1101 if( !p_rtsp ) break;
1103 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
1104 p_rtsp->b_paused = VLC_TRUE;
1106 answer->i_status = 200;
1107 answer->psz_status = strdup( "OK" );
1109 answer->p_body = NULL;
1113 return VLC_EGENERIC;
1117 httpd_MsgAdd( answer, "Server", "VLC Server" );
1118 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1119 httpd_MsgAdd( answer, "Cseq", "%d",
1120 atoi( httpd_MsgGet( query, "Cseq" ) ) );
1121 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1124 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1129 /*****************************************************************************
1131 * FIXME: need to be moved to a common place ?
1132 *****************************************************************************/
1133 static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
1136 char *p, *psz_sdp, ip[NI_MAXNUMERICHOST], ipv;
1137 const char *psz_control;
1139 if( httpd_ServerIP( cl, ip ) == NULL )
1142 p = strchr( ip, '%' );
1144 *p = '\0'; /* remove scope if present */
1146 ipv = ( strchr( ip, ':' ) != NULL ) ? '6' : '4';
1148 /* Calculate size */
1149 i_size = sizeof( "v=0\r\n" ) +
1150 sizeof( "o=- * * IN IP4 \r\n" ) + 10 + NI_MAXNUMERICHOST +
1151 sizeof( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
1152 sizeof( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
1153 sizeof( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
1154 sizeof( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
1155 sizeof( "t=0 0\r\n" ) + /* FIXME */
1156 sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
1157 sizeof( "c=IN IP4 0.0.0.0\r\n" ) + 20 + 10 +
1158 sizeof( "a=range:npt=0-1000000000.000\r\n" );
1160 psz_control = (ipv == '6') ? p_media->psz_rtsp_control_v6
1161 : p_media->psz_rtsp_control_v4;
1162 for( i = 0; i < p_media->i_es; i++ )
1164 media_es_t *p_es = p_media->es[i];
1166 i_size += sizeof( "m=**d*o * RTP/AVP *\r\n" ) + 19;
1167 if( p_es->psz_rtpmap )
1169 i_size += sizeof( "a=rtpmap:* *\r\n" ) +
1170 strlen( p_es->psz_rtpmap ) + 9;
1172 if( p_es->psz_fmtp )
1174 i_size += sizeof( "a=fmtp:* *\r\n" ) +
1175 strlen( p_es->psz_fmtp ) + 9;
1178 i_size += (strlen( psz_control ) + strlen( ip ) + 9) * p_media->i_es;
1180 p = psz_sdp = malloc( i_size );
1181 p += sprintf( p, "v=0\r\n" );
1182 p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
1183 p_media->i_sdp_id, p_media->i_sdp_version, ipv, ip );
1184 if( *p_media->psz_session_name )
1185 p += sprintf( p, "s=%s\r\n", p_media->psz_session_name );
1186 if( *p_media->psz_session_description )
1187 p += sprintf( p, "i=%s\r\n", p_media->psz_session_description );
1188 if( *p_media->psz_session_url )
1189 p += sprintf( p, "u=%s\r\n", p_media->psz_session_url );
1190 if( *p_media->psz_session_email )
1191 p += sprintf( p, "e=%s\r\n", p_media->psz_session_email );
1193 p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
1194 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
1196 p += sprintf( p, "c=IN IP%c %s\r\n", ipv, ipv == '6' ? "::" : "0.0.0.0" );
1198 if( p_media->i_length > 0 )
1200 lldiv_t d = lldiv( p_media->i_length / 1000, 1000 );
1201 p += sprintf( p, "a=range:npt=0-"I64Fd".%03u\r\n", d.quot,
1205 for( i = 0; i < p_media->i_es; i++ )
1207 media_es_t *p_es = p_media->es[i];
1209 if( p_es->fmt.i_cat == AUDIO_ES )
1211 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
1212 p_es->i_port, p_es->i_payload_type );
1214 else if( p_es->fmt.i_cat == VIDEO_ES )
1216 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
1217 p_es->i_port, p_es->i_payload_type );
1224 if( p_es->psz_rtpmap )
1226 p += sprintf( p, "a=rtpmap:%d %s\r\n", p_es->i_payload_type,
1229 if( p_es->psz_fmtp )
1231 p += sprintf( p, "a=fmtp:%d %s\r\n", p_es->i_payload_type,
1235 p += sprintf( p, psz_control, ip, i );