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." )
61 set_shortname( _("RTSP VoD" ) );
62 set_description( _("RTSP VoD server") );
63 set_category( CAT_SOUT );
64 set_subcategory( SUBCAT_SOUT_VOD );
65 set_capability( "vod server", 1 );
66 set_callbacks( Open, Close );
67 add_shortcut( "rtsp" );
68 add_string ( "rtsp-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
69 add_integer( "rtsp-throttle-users", 0, NULL, THROTLE_TEXT,
70 THROTLE_LONGTEXT, VLC_TRUE );
73 /*****************************************************************************
75 *****************************************************************************/
77 typedef struct media_es_t media_es_t;
81 media_es_t *p_media_es;
90 int64_t i_last; /* for timeout */
92 vlc_bool_t b_playing; /* is it in "play" state */
93 vlc_bool_t b_paused; /* is it in "pause" state */
96 rtsp_client_es_t **es;
106 httpd_url_t *p_rtsp_url;
108 vod_media_t *p_media;
112 uint8_t i_payload_type;
124 httpd_url_t *p_rtsp_url;
125 char *psz_rtsp_control_v4;
126 char *psz_rtsp_control_v6;
138 vlc_bool_t b_multicast;
150 rtsp_client_t **rtsp;
153 char *psz_session_name;
154 char *psz_session_description;
155 char *psz_session_url;
156 char *psz_session_email;
163 httpd_host_t *p_rtsp_host;
166 int i_throttle_users;
174 static vod_media_t *MediaNew( vod_t *, const char *, input_item_t * );
175 static void MediaDel( vod_t *, vod_media_t * );
176 static int MediaAddES( vod_t *, vod_media_t *, es_format_t * );
177 static void MediaDelES( vod_t *, vod_media_t *, es_format_t * );
179 static rtsp_client_t *RtspClientNew( vod_media_t *, char * );
180 static rtsp_client_t *RtspClientGet( vod_media_t *, char * );
181 static void RtspClientDel( vod_media_t *, rtsp_client_t * );
183 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
184 httpd_message_t *, httpd_message_t * );
185 static int RtspCallbackES( httpd_callback_sys_t *, httpd_client_t *,
186 httpd_message_t *, httpd_message_t * );
188 static char *SDPGenerate( const vod_media_t *, httpd_client_t *cl );
190 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
192 static const char hex[16] = "0123456789abcdef";
195 for( i = 0; i < i_data; i++ )
197 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
198 s[2*i+1] = hex[(p_data[i] )&0xf];
203 /*****************************************************************************
204 * Open: Starts the RTSP server module
205 *****************************************************************************/
206 static int Open( vlc_object_t *p_this )
208 vod_t *p_vod = (vod_t *)p_this;
209 vod_sys_t *p_sys = 0;
213 psz_url = config_GetPsz( p_vod, "rtsp-host" );
214 vlc_UrlParse( &url, psz_url, 0 );
215 if( psz_url ) free( psz_url );
217 if( url.i_port <= 0 ) url.i_port = 554;
219 p_vod->p_sys = p_sys = malloc( sizeof( vod_sys_t ) );
220 if( !p_sys ) goto error;
221 p_sys->p_rtsp_host = 0;
223 var_Create( p_this, "rtsp-throttle-users", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
224 p_sys->i_throttle_users = var_GetInteger( p_this, "rtsp-throtle-users" );
225 msg_Dbg( p_this, "allowing up to %d connections", p_sys->i_throttle_users );
226 p_sys->i_connections = 0;
229 httpd_HostNew( VLC_OBJECT(p_vod), url.psz_host, url.i_port );
230 if( !p_sys->p_rtsp_host )
232 msg_Err( p_vod, "cannot create RTSP server (%s:%i)",
233 url.psz_host, url.i_port );
237 p_sys->psz_path = strdup( url.psz_path ? url.psz_path : "/" );
238 p_sys->i_port = url.i_port;
240 vlc_UrlClean( &url );
244 p_vod->pf_media_new = MediaNew;
245 p_vod->pf_media_del = MediaDel;
246 p_vod->pf_media_add_es = MediaAddES;
247 p_vod->pf_media_del_es = MediaDelES;
252 if( p_sys && p_sys->p_rtsp_host ) httpd_HostDelete( p_sys->p_rtsp_host );
253 if( p_sys ) free( p_sys );
254 vlc_UrlClean( &url );
259 /*****************************************************************************
261 *****************************************************************************/
262 static void Close( vlc_object_t * p_this )
264 vod_t *p_vod = (vod_t *)p_this;
265 vod_sys_t *p_sys = p_vod->p_sys;
267 httpd_HostDelete( p_sys->p_rtsp_host );
268 var_Destroy( p_this, "rtsp-throttle-users" );
270 /* TODO delete medias */
271 free( p_sys->psz_path );
275 /*****************************************************************************
277 *****************************************************************************/
278 static vod_media_t *MediaNew( vod_t *p_vod, const char *psz_name,
279 input_item_t *p_item )
281 vod_sys_t *p_sys = p_vod->p_sys;
282 vod_media_t *p_media = malloc( sizeof(vod_media_t) );
287 msg_Err( p_vod, "not enough memory" );
291 memset( p_media, 0, sizeof(vod_media_t) );
293 p_media->psz_mux = 0;
295 p_media->b_raw = VLC_FALSE;
297 asprintf( &p_media->psz_rtsp_path, "%s%s", p_sys->psz_path, psz_name );
298 p_media->p_rtsp_url =
299 httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, NULL,
302 if( !p_media->p_rtsp_url )
304 msg_Err( p_vod, "cannot create RTSP url (%s)", p_media->psz_rtsp_path);
305 free( p_media->psz_rtsp_path );
310 msg_Dbg( p_vod, "created RTSP url: %s", p_media->psz_rtsp_path );
312 asprintf( &p_media->psz_rtsp_control_v4,
313 "a=control:rtsp://%%s:%d%s/trackID=%%d\r\n",
314 p_sys->i_port, p_media->psz_rtsp_path );
315 asprintf( &p_media->psz_rtsp_control_v6,
316 "a=control:rtsp://[%%s]:%d%s/trackID=%%d\r\n",
317 p_sys->i_port, p_media->psz_rtsp_path );
319 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_SETUP,
320 RtspCallback, (void*)p_media );
321 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_DESCRIBE,
322 RtspCallback, (void*)p_media );
323 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PLAY,
324 RtspCallback, (void*)p_media );
325 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PAUSE,
326 RtspCallback, (void*)p_media );
327 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_TEARDOWN,
328 RtspCallback, (void*)p_media );
330 p_media->p_vod = p_vod;
332 TAB_APPEND( p_sys->i_media, p_sys->media, p_media );
334 vlc_mutex_init( p_vod, &p_media->lock );
335 p_media->psz_session_name = strdup("");
336 p_media->psz_session_description = strdup("");
337 p_media->psz_session_url = strdup("");
338 p_media->psz_session_email = strdup("");
340 p_media->i_port_audio = 1234;
341 p_media->i_port_video = 1236;
342 p_media->i_port = 1238;
343 p_media->i_payload_type = 96;
345 p_media->i_sdp_id = mdate();
346 p_media->i_sdp_version = 1;
347 p_media->i_length = p_item->i_duration;
349 vlc_mutex_lock( &p_item->lock );
350 msg_Dbg( p_vod, "media has %i declared ES", p_item->i_es );
351 for( i = 0; i < p_item->i_es; i++ )
353 MediaAddES( p_vod, p_media, p_item->es[i] );
355 vlc_mutex_unlock( &p_item->lock );
360 static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
362 vod_sys_t *p_sys = p_vod->p_sys;
364 msg_Dbg( p_vod, "deleting media: %s", p_media->psz_rtsp_path );
366 while( p_media->i_rtsp > 0 ) RtspClientDel( p_media, p_media->rtsp[0] );
367 httpd_UrlDelete( p_media->p_rtsp_url );
368 if( p_media->psz_rtsp_path ) free( p_media->psz_rtsp_path );
369 if( p_media->psz_rtsp_control_v6 ) free( p_media->psz_rtsp_control_v6 );
370 if( p_media->psz_rtsp_control_v4 ) free( p_media->psz_rtsp_control_v4 );
372 TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
374 while( p_media->i_es ) MediaDelES( p_vod, p_media, &p_media->es[0]->fmt );
376 vlc_mutex_destroy( &p_media->lock );
377 free( p_media->psz_session_name );
378 free( p_media->psz_session_description );
379 free( p_media->psz_session_url );
380 free( p_media->psz_session_email );
384 static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
386 media_es_t *p_es = malloc( sizeof(media_es_t) );
389 memset( p_es, 0, sizeof(media_es_t) );
390 p_media->psz_mux = NULL;
392 /* TODO: update SDP, etc... */
393 asprintf( &psz_urlc, "%s/trackID=%d",
394 p_media->psz_rtsp_path, p_media->i_es );
395 msg_Dbg( p_vod, " - ES %4.4s (%s)", (char *)&p_fmt->i_codec, psz_urlc );
397 switch( p_fmt->i_codec )
399 case VLC_FOURCC( 's', '1', '6', 'b' ):
400 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
402 p_es->i_payload_type = 11;
404 else if( p_fmt->audio.i_channels == 2 && p_fmt->audio.i_rate == 44100 )
406 p_es->i_payload_type = 10;
410 p_es->i_payload_type = p_media->i_payload_type++;
412 p_es->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
413 sprintf( p_es->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
414 p_fmt->audio.i_channels );
416 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
417 p_es->i_payload_type = p_media->i_payload_type++;
418 p_es->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
419 sprintf( p_es->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
420 p_fmt->audio.i_channels );
422 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
423 p_es->i_payload_type = 14;
424 p_es->psz_rtpmap = strdup( "MPA/90000" );
426 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
427 p_es->i_payload_type = 32;
428 p_es->psz_rtpmap = strdup( "MPV/90000" );
430 case VLC_FOURCC( 'a', '5', '2', ' ' ):
431 p_es->i_payload_type = p_media->i_payload_type++;
432 p_es->psz_rtpmap = strdup( "ac3/90000" );
434 case VLC_FOURCC( 'H', '2', '6', '3' ):
435 p_es->i_payload_type = p_media->i_payload_type++;
436 p_es->psz_rtpmap = strdup( "H263-1998/90000" );
438 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
439 p_es->i_payload_type = p_media->i_payload_type++;
440 p_es->psz_rtpmap = strdup( "MP4V-ES/90000" );
441 if( p_fmt->i_extra > 0 )
443 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
444 p_es->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
445 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
446 sprintf( p_es->psz_fmtp,
447 "profile-level-id=3; config=%s;", p_hexa );
451 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
452 p_es->i_payload_type = p_media->i_payload_type++;
453 p_es->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
454 sprintf( p_es->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
455 if( p_fmt->i_extra > 0 )
457 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
458 p_es->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
459 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
460 sprintf( p_es->psz_fmtp,
461 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
462 "config=%s; SizeLength=13;IndexLength=3; "
463 "IndexDeltaLength=3; Profile=1;", p_hexa );
467 case VLC_FOURCC( 'm', 'p', '2', 't' ):
468 p_media->psz_mux = "ts";
469 p_es->i_payload_type = 33;
470 p_es->psz_rtpmap = strdup( "MP2T/90000" );
472 case VLC_FOURCC( 'm', 'p', '2', 'p' ):
473 p_media->psz_mux = "ps";
474 p_es->i_payload_type = p_media->i_payload_type++;
475 p_es->psz_rtpmap = strdup( "MP2P/90000" );
477 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
478 p_es->i_payload_type = p_media->i_payload_type++;
479 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
480 "AMR/8000/2" : "AMR/8000" );
481 p_es->psz_fmtp = strdup( "octet-align=1" );
483 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
484 p_es->i_payload_type = p_media->i_payload_type++;
485 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
486 "AMR-WB/16000/2" : "AMR-WB/16000" );
487 p_es->psz_fmtp = strdup( "octet-align=1" );
491 msg_Err( p_vod, "cannot add this stream (unsupported "
492 "codec: %4.4s)", (char*)&p_fmt->i_codec );
498 httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, NULL, NULL,
501 if( !p_es->p_rtsp_url )
503 msg_Err( p_vod, "cannot create RTSP url (%s)", psz_urlc );
510 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
511 RtspCallbackES, (void*)p_es );
512 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_TEARDOWN,
513 RtspCallbackES, (void*)p_es );
514 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PLAY,
515 RtspCallbackES, (void*)p_es );
516 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PAUSE,
517 RtspCallbackES, (void*)p_es );
519 es_format_Copy( &p_es->fmt, p_fmt );
521 p_es->p_media = p_media;
524 /* Choose the port */
525 if( p_fmt->i_cat == AUDIO_ES && p_media->i_port_audio > 0 )
527 p_es->i_port = p_media->i_port_audio;
528 p_media->i_port_audio = 0;
530 else if( p_fmt->i_cat == VIDEO_ES && p_media->i_port_video > 0 )
532 p_es->i_port = p_media->i_port_video;
533 p_media->i_port_video = 0;
535 while( !p_es->i_port )
537 if( p_media->i_port != p_media->i_port_audio &&
538 p_media->i_port != p_media->i_port_video )
540 p_es->i_port = p_media->i_port;
541 p_media->i_port += 2;
544 p_media->i_port += 2;
551 vlc_mutex_lock( &p_media->lock );
552 TAB_APPEND( p_media->i_es, p_media->es, p_es );
553 vlc_mutex_unlock( &p_media->lock );
555 p_media->i_sdp_version++;
560 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
562 media_es_t *p_es = 0;
566 for( i = 0; i < p_media->i_es; i++ )
568 if( p_media->es[i]->fmt.i_cat == p_fmt->i_cat &&
569 p_media->es[i]->fmt.i_codec == p_fmt->i_codec &&
570 p_media->es[i]->fmt.i_id == p_fmt->i_id )
572 p_es = p_media->es[i];
577 msg_Dbg( p_vod, " - Removing ES %4.4s", (char *)&p_fmt->i_codec );
579 vlc_mutex_lock( &p_media->lock );
580 TAB_REMOVE( p_media->i_es, p_media->es, p_es );
581 vlc_mutex_unlock( &p_media->lock );
583 if( p_es->psz_rtpmap ) free( p_es->psz_rtpmap );
584 if( p_es->psz_fmtp ) free( p_es->psz_fmtp );
585 p_media->i_sdp_version++;
587 if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
588 es_format_Clean( &p_es->fmt );
591 /****************************************************************************
592 * RTSP server implementation
593 ****************************************************************************/
594 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
596 rtsp_client_t *p_rtsp = malloc( sizeof(rtsp_client_t) );
598 if( !p_rtsp ) return NULL;
599 memset( p_rtsp, 0, sizeof(rtsp_client_t) );
602 p_rtsp->psz_session = psz_session;
603 TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
605 p_media->p_vod->p_sys->i_connections++;
606 msg_Dbg( p_media->p_vod, "new session: %s, connections: %d",
607 psz_session, p_media->p_vod->p_sys->i_throttle_users );
612 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, char *psz_session )
616 for( i = 0; psz_session && i < p_media->i_rtsp; i++ )
618 if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
620 return p_media->rtsp[i];
627 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
629 p_media->p_vod->p_sys->i_connections--;
630 msg_Dbg( p_media->p_vod, "closing session: %s, connections: %d",
631 p_rtsp->psz_session, p_media->p_vod->p_sys->i_throttle_users );
633 while( p_rtsp->i_es-- )
635 if( p_rtsp->es[p_rtsp->i_es]->psz_ip )
636 free( p_rtsp->es[p_rtsp->i_es]->psz_ip );
637 free( p_rtsp->es[p_rtsp->i_es] );
638 if( !p_rtsp->i_es ) free( p_rtsp->es );
641 TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, p_rtsp );
643 free( p_rtsp->psz_session );
647 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
648 httpd_message_t *answer, httpd_message_t *query )
650 vod_media_t *p_media = (vod_media_t*)p_args;
651 vod_t *p_vod = p_media->p_vod;
652 char *psz_transport = NULL;
653 char *psz_playnow = NULL; /* support option: x-playNow */
654 char *psz_session = NULL;
655 char *psz_cseq = NULL;
656 rtsp_client_t *p_rtsp;
660 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
662 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
664 answer->i_proto = HTTPD_PROTO_RTSP;
665 answer->i_version = query->i_version;
666 answer->i_type = HTTPD_MSG_ANSWER;
668 answer->p_body = NULL;
670 switch( query->i_type )
672 case HTTPD_MSG_SETUP:
674 psz_playnow = httpd_MsgGet( query, "x-playNow" );
675 psz_transport = httpd_MsgGet( query, "Transport" );
676 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
678 if( strstr( psz_transport, "unicast" ) &&
679 strstr( psz_transport, "client_port=" ) )
681 rtsp_client_t *p_rtsp;
682 char ip[NI_MAXNUMERICHOST];
683 i_port = atoi( strstr( psz_transport, "client_port=" ) +
684 strlen("client_port=") );
686 if( strstr( psz_transport, "MP2T/H2221/UDP" ) ||
687 strstr( psz_transport, "RAW/RAW/UDP" ) )
689 p_media->b_raw = VLC_TRUE;
692 if( httpd_ClientIP( cl, ip ) == NULL )
694 answer->i_status = 500;
695 answer->psz_status = strdup( "Internal server error" );
697 answer->p_body = NULL;
701 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
704 psz_session = httpd_MsgGet( query, "Session" );
705 if( !psz_session || !*psz_session )
707 if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
708 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
710 answer->i_status = 503;
711 answer->psz_status = strdup( "Too many connections" );
713 answer->p_body = NULL;
716 asprintf( &psz_session, "%d", rand() );
717 p_rtsp = RtspClientNew( p_media, psz_session );
721 p_rtsp = RtspClientGet( p_media, psz_session );
724 answer->i_status = 454;
725 answer->psz_status = strdup( "Unknown session id" );
727 answer->p_body = NULL;
732 answer->i_status = 200;
733 answer->psz_status = strdup( "OK" );
735 answer->p_body = NULL;
739 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
741 httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
742 i_port, i_port + 1 );
744 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
746 httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
747 i_port, i_port + 1 );
751 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
752 i_port, i_port + 1 );
754 else /* TODO strstr( psz_transport, "interleaved" ) ) */
756 answer->i_status = 461;
757 answer->psz_status = strdup( "Unsupported Transport" );
759 answer->p_body = NULL;
762 /* Intentional fall-through on x-playNow option in RTSP request */
769 char *psz_output, ip[NI_MAXNUMERICHOST];
770 int i, i_port_audio = 0, i_port_video = 0;
772 /* for now only multicast so easy */
775 answer->i_status = 200;
776 answer->psz_status = strdup( "OK" );
778 answer->p_body = NULL;
782 psz_session = httpd_MsgGet( query, "Session" );
783 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
785 p_rtsp = RtspClientGet( p_media, psz_session );
788 if( p_rtsp->b_playing && p_rtsp->b_paused )
790 vod_MediaControl( p_vod, p_media, psz_session,
792 p_rtsp->b_paused = VLC_FALSE;
795 else if( p_rtsp->b_playing ) break;
797 if( httpd_ClientIP( cl, ip ) == NULL ) break;
799 p_rtsp->b_playing = VLC_TRUE;
801 /* FIXME for != 1 video and 1 audio */
802 for( i = 0; i < p_rtsp->i_es; i++ )
804 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == AUDIO_ES )
805 i_port_audio = p_rtsp->es[i]->i_port;
806 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == VIDEO_ES )
807 i_port_video = p_rtsp->es[i]->i_port;
810 if( p_media->psz_mux )
814 asprintf( &psz_output, "std{access=udp,dst=%s:%i,mux=%s}",
815 ip, i_port, p_media->psz_mux );
819 asprintf( &psz_output, "rtp{dst=%s,port=%i,mux=%s}",
820 ip, i_port_video, p_media->psz_mux );
825 asprintf( &psz_output, "rtp{dst=%s,port-video=%i,"
826 "port-audio=%i}", ip, i_port_video, i_port_audio );
829 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PLAY,
835 case HTTPD_MSG_DESCRIBE:
838 SDPGenerate( p_media, cl );
840 if( psz_sdp != NULL )
842 answer->i_status = 200;
843 answer->psz_status = strdup( "OK" );
844 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
846 answer->p_body = (uint8_t *)psz_sdp;
847 answer->i_body = strlen( psz_sdp );
851 answer->i_status = 500;
852 answer->psz_status = strdup( "Internal server error" );
853 answer->p_body = NULL;
859 case HTTPD_MSG_PAUSE:
860 psz_session = httpd_MsgGet( query, "Session" );
861 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
863 p_rtsp = RtspClientGet( p_media, psz_session );
866 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
867 p_rtsp->b_paused = VLC_TRUE;
869 answer->i_status = 200;
870 answer->psz_status = strdup( "OK" );
872 answer->p_body = NULL;
875 case HTTPD_MSG_TEARDOWN:
876 /* for now only multicast so easy again */
877 answer->i_status = 200;
878 answer->psz_status = strdup( "OK" );
880 answer->p_body = NULL;
882 psz_session = httpd_MsgGet( query, "Session" );
883 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
885 p_rtsp = RtspClientGet( p_media, psz_session );
888 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_STOP );
889 RtspClientDel( p_media, p_rtsp );
896 httpd_MsgAdd( answer, "Server", "VLC Server" );
897 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
898 psz_cseq = httpd_MsgGet( query, "Cseq" );
899 psz_cseq ? i_cseq = atoi( psz_cseq ) : 0;
900 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
901 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
905 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
911 static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
912 httpd_message_t *answer, httpd_message_t *query )
914 media_es_t *p_es = (media_es_t*)p_args;
915 vod_media_t *p_media = p_es->p_media;
916 vod_t *p_vod = p_media->p_vod;
917 rtsp_client_t *p_rtsp = NULL;
918 char *psz_transport = NULL;
919 char *psz_playnow = NULL; /* support option: x-playNow */
920 char *psz_session = NULL;
921 char *psz_position = NULL;
922 char *psz_cseq = NULL;
926 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
928 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
930 answer->i_proto = HTTPD_PROTO_RTSP;
931 answer->i_version = query->i_version;
932 answer->i_type = HTTPD_MSG_ANSWER;
934 answer->p_body = NULL;
936 switch( query->i_type )
938 case HTTPD_MSG_SETUP:
939 psz_playnow = httpd_MsgGet( query, "x-playNow" );
940 psz_transport = httpd_MsgGet( query, "Transport" );
942 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
944 if( strstr( psz_transport, "unicast" ) &&
945 strstr( psz_transport, "client_port=" ) )
947 rtsp_client_t *p_rtsp;
948 rtsp_client_es_t *p_rtsp_es;
949 char ip[NI_MAXNUMERICHOST];
950 int i_port = atoi( strstr( psz_transport, "client_port=" ) +
951 strlen("client_port=") );
953 if( httpd_ClientIP( cl, ip ) == NULL )
955 answer->i_status = 500;
956 answer->psz_status = strdup( "Internal server error" );
958 answer->p_body = NULL;
962 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
965 psz_session = httpd_MsgGet( query, "Session" );
966 if( !psz_session || !*psz_session )
968 if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
969 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
971 answer->i_status = 503;
972 answer->psz_status = strdup( "Too many connections" );
974 answer->p_body = NULL;
977 asprintf( &psz_session, "%d", rand() );
978 p_rtsp = RtspClientNew( p_media, psz_session );
982 p_rtsp = RtspClientGet( p_media, psz_session );
985 answer->i_status = 454;
986 answer->psz_status = strdup( "Unknown session id" );
988 answer->p_body = NULL;
993 p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
994 p_rtsp_es->i_port = i_port;
995 p_rtsp_es->psz_ip = strdup( ip );
996 p_rtsp_es->p_media_es = p_es;
997 TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );
999 answer->i_status = 200;
1000 answer->psz_status = strdup( "OK" );
1002 answer->p_body = NULL;
1004 if( p_media->b_raw )
1006 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
1008 httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
1009 i_port, i_port + 1 );
1011 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1013 httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
1014 i_port, i_port + 1 );
1019 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
1020 i_port, i_port + 1 );
1023 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1025 answer->i_status = 461;
1026 answer->psz_status = strdup( "Unsupported Transport" );
1028 answer->p_body = NULL;
1031 /* Intentional fall-through on x-playNow option in RTSP request */
1035 case HTTPD_MSG_PLAY:
1036 /* This is kind of a kludge. Should we only support Aggregate
1038 psz_session = httpd_MsgGet( query, "Session" );
1039 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1041 p_rtsp = RtspClientGet( p_media, psz_session );
1043 psz_position = httpd_MsgGet( query, "Range" );
1044 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
1050 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1053 /* FIXME: npt= is not necessarily formatted as a float */
1054 f_pos = us_strtod( psz_position, &end );
1055 if( end > psz_position )
1057 f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
1058 vod_MediaControl( p_vod, p_media, psz_session,
1059 VOD_MEDIA_SEEK, f_pos );
1065 answer->i_status = 200;
1066 answer->psz_status = strdup( "OK" );
1068 answer->p_body = NULL;
1072 case HTTPD_MSG_TEARDOWN:
1073 answer->i_status = 200;
1074 answer->psz_status = strdup( "OK" );
1076 answer->p_body = NULL;
1078 psz_session = httpd_MsgGet( query, "Session" );
1079 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1081 p_rtsp = RtspClientGet( p_media, psz_session );
1082 if( !p_rtsp ) break;
1084 for( i = 0; i < p_rtsp->i_es; i++ )
1086 if( p_rtsp->es[i]->p_media_es == p_es )
1088 if( p_rtsp->es[i]->psz_ip ) free( p_rtsp->es[i]->psz_ip );
1089 TAB_REMOVE( p_rtsp->i_es, p_rtsp->es, p_rtsp->es[i] );
1096 vod_MediaControl( p_vod, p_media, psz_session,
1098 RtspClientDel( p_media, p_rtsp );
1102 case HTTPD_MSG_PAUSE:
1103 /* This is kind of a kludge. Should we only support Aggregate
1105 psz_session = httpd_MsgGet( query, "Session" );
1106 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1108 p_rtsp = RtspClientGet( p_media, psz_session );
1109 if( !p_rtsp ) break;
1111 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
1112 p_rtsp->b_paused = VLC_TRUE;
1114 answer->i_status = 200;
1115 answer->psz_status = strdup( "OK" );
1117 answer->p_body = NULL;
1121 return VLC_EGENERIC;
1125 httpd_MsgAdd( answer, "Server", "VLC Server" );
1126 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1127 psz_cseq = httpd_MsgGet( query, "Cseq" );
1129 i_cseq = atoi( psz_cseq );
1132 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1133 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1136 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1141 /*****************************************************************************
1143 * FIXME: need to be moved to a common place ?
1144 *****************************************************************************/
1145 static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
1148 char *p, *psz_sdp, ip[NI_MAXNUMERICHOST], ipv;
1149 const char *psz_control;
1151 if( httpd_ServerIP( cl, ip ) == NULL )
1154 p = strchr( ip, '%' );
1156 *p = '\0'; /* remove scope if present */
1158 ipv = ( strchr( ip, ':' ) != NULL ) ? '6' : '4';
1160 /* Calculate size */
1161 i_size = sizeof( "v=0\r\n" ) +
1162 sizeof( "o=- * * IN IP4 \r\n" ) + 10 + NI_MAXNUMERICHOST +
1163 sizeof( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
1164 sizeof( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
1165 sizeof( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
1166 sizeof( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
1167 sizeof( "t=0 0\r\n" ) + /* FIXME */
1168 sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
1169 sizeof( "c=IN IP4 0.0.0.0\r\n" ) + 20 + 10 +
1170 sizeof( "a=range:npt=0-1000000000.000\r\n" );
1172 psz_control = (ipv == '6') ? p_media->psz_rtsp_control_v6
1173 : p_media->psz_rtsp_control_v4;
1174 for( i = 0; i < p_media->i_es; i++ )
1176 media_es_t *p_es = p_media->es[i];
1178 i_size += sizeof( "m=**d*o * RTP/AVP *\r\n" ) + 19;
1179 if( p_es->psz_rtpmap )
1181 i_size += sizeof( "a=rtpmap:* *\r\n" ) +
1182 strlen( p_es->psz_rtpmap ) + 9;
1184 if( p_es->psz_fmtp )
1186 i_size += sizeof( "a=fmtp:* *\r\n" ) +
1187 strlen( p_es->psz_fmtp ) + 9;
1190 i_size += (strlen( psz_control ) + strlen( ip ) + 9) * p_media->i_es;
1192 p = psz_sdp = malloc( i_size );
1193 p += sprintf( p, "v=0\r\n" );
1194 p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
1195 p_media->i_sdp_id, p_media->i_sdp_version, ipv, ip );
1196 if( *p_media->psz_session_name )
1197 p += sprintf( p, "s=%s\r\n", p_media->psz_session_name );
1198 if( *p_media->psz_session_description )
1199 p += sprintf( p, "i=%s\r\n", p_media->psz_session_description );
1200 if( *p_media->psz_session_url )
1201 p += sprintf( p, "u=%s\r\n", p_media->psz_session_url );
1202 if( *p_media->psz_session_email )
1203 p += sprintf( p, "e=%s\r\n", p_media->psz_session_email );
1205 p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
1206 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
1208 p += sprintf( p, "c=IN IP%c %s\r\n", ipv, ipv == '6' ? "::" : "0.0.0.0" );
1210 if( p_media->i_length > 0 )
1212 lldiv_t d = lldiv( p_media->i_length / 1000, 1000 );
1213 p += sprintf( p, "a=range:npt=0-"I64Fd".%03u\r\n", d.quot,
1217 for( i = 0; i < p_media->i_es; i++ )
1219 media_es_t *p_es = p_media->es[i];
1221 if( p_es->fmt.i_cat == AUDIO_ES )
1223 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
1224 p_es->i_port, p_es->i_payload_type );
1226 else if( p_es->fmt.i_cat == VIDEO_ES )
1228 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
1229 p_es->i_port, p_es->i_payload_type );
1236 if( p_es->psz_rtpmap )
1238 p += sprintf( p, "a=rtpmap:%d %s\r\n", p_es->i_payload_type,
1241 if( p_es->psz_fmtp )
1243 p += sprintf( p, "a=fmtp:%d %s\r\n", p_es->i_payload_type,
1247 p += sprintf( p, psz_control, ip, i );