1 /*****************************************************************************
2 * rtsp.c: rtsp VoD server module
3 *****************************************************************************
4 * Copyright (C) 2003-2006 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
34 #include <vlc_input.h>
37 #include "vlc_httpd.h"
40 #include <vlc_network.h>
41 #include <vlc_charset.h>
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Open ( vlc_object_t * );
47 static void Close( vlc_object_t * );
49 #define HOST_TEXT N_( "RTSP host address" )
50 /// \bug [String] extra space
51 #define HOST_LONGTEXT N_( \
52 "This defines the address, port and path the RTSP VOD server will listen " \
53 "on.\nSyntax is address:port/path. The default is to listen on all "\
54 "interfaces (address 0.0.0.0), on port 554, with no path.\n To listen " \
55 "only on the local interface, use \"localhost\" as address." )
57 #define THROTLE_TEXT N_( "Maximum number of connections" )
58 #define THROTLE_LONGTEXT N_( "This limits the maximum number of clients " \
59 "that can connect to the RTSP VOD. 0 means no limit." )
61 #define RAWMUX_TEXT N_( "MUX for RAW RTSP transport" )
63 #define SESSION_TIMEOUT_TEXT N_( "Sets the timeout option in the RTSP " \
65 #define SESSION_TIMEOUT_LONGTEXT N_( "Defines what timeout option to add " \
66 "to the RTSP session ID string. Setting it to a negative number removes " \
67 "the timeout option entirely. This is needed by some IPTV STBs (such as " \
68 "those made by HansunTech) which get confused by it. The default is 5." )
71 set_shortname( _("RTSP VoD" ) );
72 set_description( _("RTSP VoD server") );
73 set_category( CAT_SOUT );
74 set_subcategory( SUBCAT_SOUT_VOD );
75 set_capability( "vod server", 1 );
76 set_callbacks( Open, Close );
77 add_shortcut( "rtsp" );
78 add_string ( "rtsp-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
79 add_string( "rtsp-raw-mux", "ts", NULL, RAWMUX_TEXT, RAWMUX_TEXT, VLC_TRUE );
80 add_integer( "rtsp-throttle-users", 0, NULL, THROTLE_TEXT,
81 THROTLE_LONGTEXT, VLC_TRUE );
82 add_integer( "rtsp-session-timeout", 5, NULL, SESSION_TIMEOUT_TEXT,
83 SESSION_TIMEOUT_LONGTEXT, VLC_TRUE );
86 /*****************************************************************************
88 *****************************************************************************/
90 typedef struct media_es_t media_es_t;
94 media_es_t *p_media_es;
103 int64_t i_last; /* for timeout */
105 vlc_bool_t b_playing; /* is it in "play" state */
106 vlc_bool_t b_paused; /* is it in "pause" state */
109 rtsp_client_es_t **es;
119 httpd_url_t *p_rtsp_url;
121 vod_media_t *p_media;
125 uint8_t i_payload_type;
137 httpd_url_t *p_rtsp_url;
138 char *psz_rtsp_control_v4;
139 char *psz_rtsp_control_v6;
151 vlc_bool_t b_multicast;
163 rtsp_client_t **rtsp;
166 char *psz_session_name;
167 char *psz_session_description;
168 char *psz_session_url;
169 char *psz_session_email;
176 httpd_host_t *p_rtsp_host;
179 int i_throttle_users;
184 int i_session_timeout;
191 static vod_media_t *MediaNew( vod_t *, const char *, input_item_t * );
192 static void MediaDel( vod_t *, vod_media_t * );
193 static int MediaAddES( vod_t *, vod_media_t *, es_format_t * );
194 static void MediaDelES( vod_t *, vod_media_t *, es_format_t * );
196 static rtsp_client_t *RtspClientNew( vod_media_t *, char * );
197 static rtsp_client_t *RtspClientGet( vod_media_t *, char * );
198 static void RtspClientDel( vod_media_t *, rtsp_client_t * );
200 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
201 httpd_message_t *, httpd_message_t * );
202 static int RtspCallbackES( httpd_callback_sys_t *, httpd_client_t *,
203 httpd_message_t *, httpd_message_t * );
205 static char *SDPGenerate( const vod_media_t *, httpd_client_t *cl );
207 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
209 static const char hex[16] = "0123456789abcdef";
212 for( i = 0; i < i_data; i++ )
214 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
215 s[2*i+1] = hex[(p_data[i] )&0xf];
220 /*****************************************************************************
221 * Open: Starts the RTSP server module
222 *****************************************************************************/
223 static int Open( vlc_object_t *p_this )
225 vod_t *p_vod = (vod_t *)p_this;
226 vod_sys_t *p_sys = 0;
230 psz_url = config_GetPsz( p_vod, "rtsp-host" );
231 vlc_UrlParse( &url, psz_url, 0 );
232 if( psz_url ) free( psz_url );
234 if( url.i_port <= 0 ) url.i_port = 554;
236 p_vod->p_sys = p_sys = malloc( sizeof( vod_sys_t ) );
237 if( !p_sys ) goto error;
238 p_sys->p_rtsp_host = 0;
240 var_Create( p_this, "rtsp-session-timeout", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
241 p_sys->i_session_timeout = var_GetInteger( p_this, "rtsp-session-timeout" );
243 var_Create( p_this, "rtsp-throttle-users", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
244 p_sys->i_throttle_users = var_GetInteger( p_this, "rtsp-throtle-users" );
245 msg_Dbg( p_this, "allowing up to %d connections", p_sys->i_throttle_users );
246 p_sys->i_connections = 0;
248 var_Create( p_this, "rtsp-raw-mux", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
249 p_sys->psz_raw_mux = var_GetString( p_this, "rtsp-raw-mux" );
252 httpd_HostNew( VLC_OBJECT(p_vod), url.psz_host, url.i_port );
253 if( !p_sys->p_rtsp_host )
255 msg_Err( p_vod, "cannot create RTSP server (%s:%i)",
256 url.psz_host, url.i_port );
260 p_sys->psz_path = strdup( url.psz_path ? url.psz_path : "/" );
261 p_sys->i_port = url.i_port;
263 vlc_UrlClean( &url );
267 p_vod->pf_media_new = MediaNew;
268 p_vod->pf_media_del = MediaDel;
269 p_vod->pf_media_add_es = MediaAddES;
270 p_vod->pf_media_del_es = MediaDelES;
275 if( p_sys && p_sys->p_rtsp_host ) httpd_HostDelete( p_sys->p_rtsp_host );
276 if( p_sys && p_sys->psz_raw_mux ) free( p_sys->psz_raw_mux );
277 if( p_sys ) free( p_sys );
278 vlc_UrlClean( &url );
283 /*****************************************************************************
285 *****************************************************************************/
286 static void Close( vlc_object_t * p_this )
288 vod_t *p_vod = (vod_t *)p_this;
289 vod_sys_t *p_sys = p_vod->p_sys;
291 httpd_HostDelete( p_sys->p_rtsp_host );
292 var_Destroy( p_this, "rtsp-session-timeout" );
293 var_Destroy( p_this, "rtsp-throttle-users" );
294 var_Destroy( p_this, "rtsp-raw-mux" );
296 /* TODO delete medias */
297 free( p_sys->psz_path );
298 free( p_sys->psz_raw_mux );
302 /*****************************************************************************
304 *****************************************************************************/
305 static vod_media_t *MediaNew( vod_t *p_vod, const char *psz_name,
306 input_item_t *p_item )
308 vod_sys_t *p_sys = p_vod->p_sys;
309 vod_media_t *p_media = malloc( sizeof(vod_media_t) );
314 msg_Err( p_vod, "not enough memory" );
318 memset( p_media, 0, sizeof(vod_media_t) );
320 p_media->psz_mux = 0;
322 p_media->b_raw = VLC_FALSE;
324 asprintf( &p_media->psz_rtsp_path, "%s%s", p_sys->psz_path, psz_name );
325 p_media->p_rtsp_url =
326 httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, NULL,
329 if( !p_media->p_rtsp_url )
331 msg_Err( p_vod, "cannot create RTSP url (%s)", p_media->psz_rtsp_path);
332 free( p_media->psz_rtsp_path );
337 msg_Dbg( p_vod, "created RTSP url: %s", p_media->psz_rtsp_path );
339 asprintf( &p_media->psz_rtsp_control_v4,
340 "a=control:rtsp://%%s:%d%s/trackID=%%d\r\n",
341 p_sys->i_port, p_media->psz_rtsp_path );
342 asprintf( &p_media->psz_rtsp_control_v6,
343 "a=control:rtsp://[%%s]:%d%s/trackID=%%d\r\n",
344 p_sys->i_port, p_media->psz_rtsp_path );
346 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_SETUP,
347 RtspCallback, (void*)p_media );
348 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_DESCRIBE,
349 RtspCallback, (void*)p_media );
350 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PLAY,
351 RtspCallback, (void*)p_media );
352 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PAUSE,
353 RtspCallback, (void*)p_media );
354 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_GETPARAMETER,
355 RtspCallback, (void*)p_media );
356 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_TEARDOWN,
357 RtspCallback, (void*)p_media );
359 p_media->p_vod = p_vod;
361 TAB_APPEND( p_sys->i_media, p_sys->media, p_media );
363 vlc_mutex_init( p_vod, &p_media->lock );
364 p_media->psz_session_name = strdup("");
365 p_media->psz_session_description = strdup("");
366 p_media->psz_session_url = strdup("");
367 p_media->psz_session_email = strdup("");
369 p_media->i_port_audio = 1234;
370 p_media->i_port_video = 1236;
371 p_media->i_port = 1238;
372 p_media->i_payload_type = 96;
374 p_media->i_sdp_id = mdate();
375 p_media->i_sdp_version = 1;
376 p_media->i_length = p_item->i_duration;
378 vlc_mutex_lock( &p_item->lock );
379 msg_Dbg( p_vod, "media has %i declared ES", p_item->i_es );
380 for( i = 0; i < p_item->i_es; i++ )
382 MediaAddES( p_vod, p_media, p_item->es[i] );
384 vlc_mutex_unlock( &p_item->lock );
389 static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
391 vod_sys_t *p_sys = p_vod->p_sys;
393 msg_Dbg( p_vod, "deleting media: %s", p_media->psz_rtsp_path );
395 while( p_media->i_rtsp > 0 ) RtspClientDel( p_media, p_media->rtsp[0] );
396 httpd_UrlDelete( p_media->p_rtsp_url );
397 if( p_media->psz_rtsp_path ) free( p_media->psz_rtsp_path );
398 if( p_media->psz_rtsp_control_v6 ) free( p_media->psz_rtsp_control_v6 );
399 if( p_media->psz_rtsp_control_v4 ) free( p_media->psz_rtsp_control_v4 );
401 TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
403 while( p_media->i_es ) MediaDelES( p_vod, p_media, &p_media->es[0]->fmt );
405 vlc_mutex_destroy( &p_media->lock );
407 if( p_media->psz_session_name ) free( p_media->psz_session_name );
408 if( p_media->psz_session_description ) free( p_media->psz_session_description );
409 if( p_media->psz_session_url ) free( p_media->psz_session_url );
410 if( p_media->psz_session_email ) free( p_media->psz_session_email );
411 if( p_media->psz_mux ) free( p_media->psz_mux );
415 static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
417 media_es_t *p_es = malloc( sizeof(media_es_t) );
420 if( !p_es ) return VLC_ENOMEM;
421 memset( p_es, 0, sizeof(media_es_t) );
423 if( p_media->psz_mux ) free( p_media->psz_mux );
424 p_media->psz_mux = NULL;
426 /* TODO: update SDP, etc... */
427 asprintf( &psz_urlc, "%s/trackID=%d",
428 p_media->psz_rtsp_path, p_media->i_es );
429 msg_Dbg( p_vod, " - ES %4.4s (%s)", (char *)&p_fmt->i_codec, psz_urlc );
431 switch( p_fmt->i_codec )
433 case VLC_FOURCC( 's', '1', '6', 'b' ):
434 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
436 p_es->i_payload_type = 11;
438 else if( p_fmt->audio.i_channels == 2 && p_fmt->audio.i_rate == 44100 )
440 p_es->i_payload_type = 10;
444 p_es->i_payload_type = p_media->i_payload_type++;
446 p_es->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
447 sprintf( p_es->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
448 p_fmt->audio.i_channels );
450 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
451 p_es->i_payload_type = p_media->i_payload_type++;
452 p_es->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
453 sprintf( p_es->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
454 p_fmt->audio.i_channels );
456 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
457 p_es->i_payload_type = 14;
458 p_es->psz_rtpmap = strdup( "MPA/90000" );
460 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
461 p_es->i_payload_type = 32;
462 p_es->psz_rtpmap = strdup( "MPV/90000" );
464 case VLC_FOURCC( 'a', '5', '2', ' ' ):
465 p_es->i_payload_type = p_media->i_payload_type++;
466 p_es->psz_rtpmap = strdup( "ac3/90000" );
468 case VLC_FOURCC( 'H', '2', '6', '3' ):
469 p_es->i_payload_type = p_media->i_payload_type++;
470 p_es->psz_rtpmap = strdup( "H263-1998/90000" );
472 case VLC_FOURCC( 'h', '2', '6', '4' ):
473 p_es->i_payload_type = p_media->i_payload_type++;
474 p_es->psz_rtpmap = strdup( "H264/90000" );
475 p_es->psz_fmtp = NULL;
476 /* FIXME AAAAAAAAAAAARRRRRRRRGGGG copied from stream_out/rtp.c */
477 if( p_fmt->i_extra > 0 )
479 uint8_t *p_buffer = p_fmt->p_extra;
480 int i_buffer = p_fmt->i_extra;
481 char *p_64_sps = NULL;
482 char *p_64_pps = NULL;
485 while( i_buffer > 4 &&
486 p_buffer[0] == 0 && p_buffer[1] == 0 &&
487 p_buffer[2] == 0 && p_buffer[3] == 1 )
489 const int i_nal_type = p_buffer[4]&0x1f;
494 for( i_offset = 4; i_offset+3 < i_buffer ; i_offset++)
496 if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 0 && p_buffer[i_offset+3] == 1 )
498 /* we found another startcode */
503 if( i_nal_type == 7 )
505 p_64_sps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
506 sprintf_hexa( hexa, &p_buffer[5], 3 );
508 else if( i_nal_type == 8 )
510 p_64_pps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
516 if( p_64_sps && p_64_pps )
517 asprintf( &p_es->psz_fmtp, "packetization-mode=1;profile-level-id=%s;sprop-parameter-sets=%s,%s;", hexa, p_64_sps, p_64_pps );
523 if( !p_es->psz_fmtp )
524 p_es->psz_fmtp = strdup( "packetization-mode=1" );
526 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
527 p_es->i_payload_type = p_media->i_payload_type++;
528 p_es->psz_rtpmap = strdup( "MP4V-ES/90000" );
529 if( p_fmt->i_extra > 0 )
531 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
532 p_es->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
533 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
534 sprintf( p_es->psz_fmtp,
535 "profile-level-id=3; config=%s;", p_hexa );
539 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
540 p_es->i_payload_type = p_media->i_payload_type++;
541 p_es->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
542 sprintf( p_es->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
543 if( p_fmt->i_extra > 0 )
545 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
546 p_es->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
547 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
548 sprintf( p_es->psz_fmtp,
549 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
550 "config=%s; SizeLength=13;IndexLength=3; "
551 "IndexDeltaLength=3; Profile=1;", p_hexa );
555 case VLC_FOURCC( 'm', 'p', '2', 't' ):
556 p_media->psz_mux = strdup("ts");
557 p_es->i_payload_type = 33;
558 p_es->psz_rtpmap = strdup( "MP2T/90000" );
560 case VLC_FOURCC( 'm', 'p', '2', 'p' ):
561 p_media->psz_mux = strdup("ps");
562 p_es->i_payload_type = p_media->i_payload_type++;
563 p_es->psz_rtpmap = strdup( "MP2P/90000" );
565 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
566 p_es->i_payload_type = p_media->i_payload_type++;
567 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
568 "AMR/8000/2" : "AMR/8000" );
569 p_es->psz_fmtp = strdup( "octet-align=1" );
571 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
572 p_es->i_payload_type = p_media->i_payload_type++;
573 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
574 "AMR-WB/16000/2" : "AMR-WB/16000" );
575 p_es->psz_fmtp = strdup( "octet-align=1" );
579 msg_Err( p_vod, "cannot add this stream (unsupported "
580 "codec: %4.4s)", (char*)&p_fmt->i_codec );
586 httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, NULL, NULL,
589 if( !p_es->p_rtsp_url )
591 msg_Err( p_vod, "cannot create RTSP url (%s)", psz_urlc );
598 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
599 RtspCallbackES, (void*)p_es );
600 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_TEARDOWN,
601 RtspCallbackES, (void*)p_es );
602 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PLAY,
603 RtspCallbackES, (void*)p_es );
604 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PAUSE,
605 RtspCallbackES, (void*)p_es );
607 es_format_Copy( &p_es->fmt, p_fmt );
609 p_es->p_media = p_media;
612 /* Choose the port */
613 if( p_fmt->i_cat == AUDIO_ES && p_media->i_port_audio > 0 )
615 p_es->i_port = p_media->i_port_audio;
616 p_media->i_port_audio = 0;
618 else if( p_fmt->i_cat == VIDEO_ES && p_media->i_port_video > 0 )
620 p_es->i_port = p_media->i_port_video;
621 p_media->i_port_video = 0;
623 while( !p_es->i_port )
625 if( p_media->i_port != p_media->i_port_audio &&
626 p_media->i_port != p_media->i_port_video )
628 p_es->i_port = p_media->i_port;
629 p_media->i_port += 2;
632 p_media->i_port += 2;
639 vlc_mutex_lock( &p_media->lock );
640 TAB_APPEND( p_media->i_es, p_media->es, p_es );
641 vlc_mutex_unlock( &p_media->lock );
643 p_media->i_sdp_version++;
648 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
650 media_es_t *p_es = NULL;
654 for( i = 0; i < p_media->i_es; i++ )
656 if( p_media->es[i]->fmt.i_cat == p_fmt->i_cat &&
657 p_media->es[i]->fmt.i_codec == p_fmt->i_codec &&
658 p_media->es[i]->fmt.i_id == p_fmt->i_id )
660 p_es = p_media->es[i];
665 msg_Dbg( p_vod, " - Removing ES %4.4s", (char *)&p_fmt->i_codec );
667 vlc_mutex_lock( &p_media->lock );
668 TAB_REMOVE( p_media->i_es, p_media->es, p_es );
669 vlc_mutex_unlock( &p_media->lock );
671 if( p_es->psz_rtpmap ) free( p_es->psz_rtpmap );
672 if( p_es->psz_fmtp ) free( p_es->psz_fmtp );
673 p_media->i_sdp_version++;
675 if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
676 es_format_Clean( &p_es->fmt );
679 /****************************************************************************
680 * RTSP server implementation
681 ****************************************************************************/
682 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
684 rtsp_client_t *p_rtsp = malloc( sizeof(rtsp_client_t) );
686 if( !p_rtsp ) return NULL;
687 memset( p_rtsp, 0, sizeof(rtsp_client_t) );
690 p_rtsp->psz_session = psz_session;
691 TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
693 p_media->p_vod->p_sys->i_connections++;
694 msg_Dbg( p_media->p_vod, "new session: %s, connections: %d",
695 psz_session, p_media->p_vod->p_sys->i_throttle_users );
700 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, char *psz_session )
704 for( i = 0; psz_session && i < p_media->i_rtsp; i++ )
706 if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
708 return p_media->rtsp[i];
715 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
717 p_media->p_vod->p_sys->i_connections--;
718 msg_Dbg( p_media->p_vod, "closing session: %s, connections: %d",
719 p_rtsp->psz_session, p_media->p_vod->p_sys->i_throttle_users );
721 while( p_rtsp->i_es-- )
723 if( p_rtsp->es[p_rtsp->i_es]->psz_ip )
724 free( p_rtsp->es[p_rtsp->i_es]->psz_ip );
725 free( p_rtsp->es[p_rtsp->i_es] );
726 if( !p_rtsp->i_es ) free( p_rtsp->es );
729 TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, p_rtsp );
731 free( p_rtsp->psz_session );
735 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
736 httpd_message_t *answer, httpd_message_t *query )
738 vod_media_t *p_media = (vod_media_t*)p_args;
739 vod_t *p_vod = p_media->p_vod;
740 char *psz_transport = NULL;
741 char *psz_playnow = NULL; /* support option: x-playNow */
742 char *psz_session = NULL;
743 char *psz_cseq = NULL;
744 rtsp_client_t *p_rtsp;
748 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
750 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
752 answer->i_proto = HTTPD_PROTO_RTSP;
753 answer->i_version = query->i_version;
754 answer->i_type = HTTPD_MSG_ANSWER;
756 answer->p_body = NULL;
758 switch( query->i_type )
760 case HTTPD_MSG_SETUP:
762 psz_playnow = httpd_MsgGet( query, "x-playNow" );
763 psz_transport = httpd_MsgGet( query, "Transport" );
764 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
766 if( strstr( psz_transport, "unicast" ) &&
767 strstr( psz_transport, "client_port=" ) )
769 rtsp_client_t *p_rtsp = NULL;
770 char ip[NI_MAXNUMERICHOST];
771 i_port = atoi( strstr( psz_transport, "client_port=" ) +
772 strlen("client_port=") );
774 if( strstr( psz_transport, "MP2T/H2221/UDP" ) ||
775 strstr( psz_transport, "RAW/RAW/UDP" ) )
777 if( p_media->psz_mux ) free( p_media->psz_mux );
778 p_media->psz_mux = NULL;
779 p_media->psz_mux = strdup( p_vod->p_sys->psz_raw_mux );
780 p_media->b_raw = VLC_TRUE;
783 if( httpd_ClientIP( cl, ip ) == NULL )
785 answer->i_status = 500;
786 answer->psz_status = strdup( "Internal server error" );
788 answer->p_body = NULL;
792 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
795 psz_session = httpd_MsgGet( query, "Session" );
796 if( !psz_session || !*psz_session )
798 if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
799 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
801 answer->i_status = 503;
802 answer->psz_status = strdup( "Too many connections" );
804 answer->p_body = NULL;
807 asprintf( &psz_session, "%d", rand() );
808 p_rtsp = RtspClientNew( p_media, psz_session );
811 answer->i_status = 454;
812 answer->psz_status = strdup( "Unknown session id" );
814 answer->p_body = NULL;
820 p_rtsp = RtspClientGet( p_media, psz_session );
823 answer->i_status = 454;
824 answer->psz_status = strdup( "Unknown session id" );
826 answer->p_body = NULL;
831 answer->i_status = 200;
832 answer->psz_status = strdup( "OK" );
834 answer->p_body = NULL;
838 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
840 httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
841 i_port, i_port + 1 );
843 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
845 httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
846 i_port, i_port + 1 );
850 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
851 i_port, i_port + 1 );
853 else /* TODO strstr( psz_transport, "interleaved" ) ) */
855 answer->i_status = 461;
856 answer->psz_status = strdup( "Unsupported Transport" );
858 answer->p_body = NULL;
861 /* Intentional fall-through on x-playNow option in RTSP request */
868 char *psz_output, ip[NI_MAXNUMERICHOST];
869 int i, i_port_audio = 0, i_port_video = 0;
871 /* for now only multicast so easy */
874 answer->i_status = 200;
875 answer->psz_status = strdup( "OK" );
877 answer->p_body = NULL;
881 psz_session = httpd_MsgGet( query, "Session" );
882 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
884 p_rtsp = RtspClientGet( p_media, psz_session );
887 answer->i_status = 500;
888 answer->psz_status = strdup( "Internal server error" );
890 answer->p_body = NULL;
894 if( p_rtsp->b_playing )
896 char *psz_position = httpd_MsgGet( query, "Range" );
897 char *psz_scale = httpd_MsgGet( query, "Scale" );
898 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
899 if( psz_position && !psz_scale )
904 msg_Dbg( p_vod, "seeking request: %s", psz_position );
906 /* FIXME: npt= is not necessarily formatted as a float */
907 f_pos = us_strtod( psz_position, &end );
908 if( end > psz_position )
910 f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
911 vod_MediaControl( p_vod, p_media, psz_session,
912 VOD_MEDIA_SEEK, f_pos );
918 double f_scale = 0.0;
921 f_scale = us_strtod( psz_scale, &end );
922 if( end > psz_scale )
924 f_scale = (f_scale * 30.0);
925 if( psz_scale[0] == '-' ) /* rewind */
927 msg_Dbg( p_vod, "rewind request: %s", psz_scale );
928 vod_MediaControl( p_vod, p_media, psz_session,
929 VOD_MEDIA_REWIND, f_scale );
931 else if(psz_scale[0] != '1' ) /* fast-forward */
933 msg_Dbg( p_vod, "fastforward request: %s", psz_scale );
934 vod_MediaControl( p_vod, p_media, psz_session,
935 VOD_MEDIA_FORWARD, f_scale );
938 if( p_rtsp->b_paused == VLC_TRUE )
940 p_rtsp->b_paused = VLC_FALSE;
941 vod_MediaControl( p_vod, p_media, psz_session,
942 VOD_MEDIA_PAUSE, psz_output );
949 if( p_rtsp->b_playing && p_rtsp->b_paused )
951 vod_MediaControl( p_vod, p_media, psz_session,
953 p_rtsp->b_paused = VLC_FALSE;
956 else if( p_rtsp->b_playing ) break;
958 if( httpd_ClientIP( cl, ip ) == NULL ) break;
960 p_rtsp->b_playing = VLC_TRUE;
962 /* FIXME for != 1 video and 1 audio */
963 for( i = 0; i < p_rtsp->i_es; i++ )
965 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == AUDIO_ES )
966 i_port_audio = p_rtsp->es[i]->i_port;
967 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == VIDEO_ES )
968 i_port_video = p_rtsp->es[i]->i_port;
971 if( p_media->psz_mux )
975 asprintf( &psz_output, "std{access=udp,dst=%s:%i,mux=%s}",
976 ip, i_port, p_media->psz_mux );
980 asprintf( &psz_output, "rtp{dst=%s,port=%i,mux=%s}",
981 ip, i_port_video, p_media->psz_mux );
986 asprintf( &psz_output, "rtp{dst=%s,port-video=%i,"
987 "port-audio=%i}", ip, i_port_video, i_port_audio );
990 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PLAY,
996 case HTTPD_MSG_DESCRIBE:
999 SDPGenerate( p_media, cl );
1001 if( psz_sdp != NULL )
1003 answer->i_status = 200;
1004 answer->psz_status = strdup( "OK" );
1005 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
1007 answer->p_body = (uint8_t *)psz_sdp;
1008 answer->i_body = strlen( psz_sdp );
1012 answer->i_status = 500;
1013 answer->psz_status = strdup( "Internal server error" );
1014 answer->p_body = NULL;
1020 case HTTPD_MSG_PAUSE:
1021 psz_session = httpd_MsgGet( query, "Session" );
1022 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1024 p_rtsp = RtspClientGet( p_media, psz_session );
1025 if( !p_rtsp ) break;
1027 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
1028 p_rtsp->b_paused = VLC_TRUE;
1030 answer->i_status = 200;
1031 answer->psz_status = strdup( "OK" );
1033 answer->p_body = NULL;
1036 case HTTPD_MSG_TEARDOWN:
1037 /* for now only multicast so easy again */
1038 answer->i_status = 200;
1039 answer->psz_status = strdup( "OK" );
1041 answer->p_body = NULL;
1043 psz_session = httpd_MsgGet( query, "Session" );
1044 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1046 p_rtsp = RtspClientGet( p_media, psz_session );
1047 if( !p_rtsp ) break;
1049 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_STOP );
1050 RtspClientDel( p_media, p_rtsp );
1053 case HTTPD_MSG_GETPARAMETER:
1054 answer->i_status = 200;
1055 answer->psz_status = strdup( "OK" );
1057 answer->p_body = NULL;
1061 return VLC_EGENERIC;
1064 httpd_MsgAdd( answer, "Server", "VLC Server" );
1065 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1066 psz_cseq = httpd_MsgGet( query, "Cseq" );
1067 psz_cseq ? i_cseq = atoi( psz_cseq ) : 0;
1068 httpd_MsgAdd( answer, "CSeq", "%d", i_cseq );
1069 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1073 if( p_media->p_vod->p_sys->i_session_timeout >= 0 )
1074 httpd_MsgAdd( answer, "Session", "%s;timeout=%i", psz_session,
1075 p_media->p_vod->p_sys->i_session_timeout );
1077 httpd_MsgAdd( answer, "Session", "%s", psz_session );
1083 static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
1084 httpd_message_t *answer, httpd_message_t *query )
1086 media_es_t *p_es = (media_es_t*)p_args;
1087 vod_media_t *p_media = p_es->p_media;
1088 vod_t *p_vod = p_media->p_vod;
1089 rtsp_client_t *p_rtsp = NULL;
1090 char *psz_transport = NULL;
1091 char *psz_playnow = NULL; /* support option: x-playNow */
1092 char *psz_session = NULL;
1093 char *psz_position = NULL;
1094 char *psz_cseq = NULL;
1098 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
1100 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
1102 answer->i_proto = HTTPD_PROTO_RTSP;
1103 answer->i_version = query->i_version;
1104 answer->i_type = HTTPD_MSG_ANSWER;
1106 answer->p_body = NULL;
1108 switch( query->i_type )
1110 case HTTPD_MSG_SETUP:
1111 psz_playnow = httpd_MsgGet( query, "x-playNow" );
1112 psz_transport = httpd_MsgGet( query, "Transport" );
1114 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
1116 if( strstr( psz_transport, "unicast" ) &&
1117 strstr( psz_transport, "client_port=" ) )
1119 rtsp_client_t *p_rtsp = NULL;
1120 rtsp_client_es_t *p_rtsp_es = NULL;
1121 char ip[NI_MAXNUMERICHOST];
1122 int i_port = atoi( strstr( psz_transport, "client_port=" ) +
1123 strlen("client_port=") );
1125 if( httpd_ClientIP( cl, ip ) == NULL )
1127 answer->i_status = 500;
1128 answer->psz_status = strdup( "Internal server error" );
1130 answer->p_body = NULL;
1134 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
1137 psz_session = httpd_MsgGet( query, "Session" );
1138 if( !psz_session || !*psz_session )
1140 if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
1141 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
1143 answer->i_status = 503;
1144 answer->psz_status = strdup( "Too many connections" );
1146 answer->p_body = NULL;
1149 asprintf( &psz_session, "%d", rand() );
1150 p_rtsp = RtspClientNew( p_media, psz_session );
1154 p_rtsp = RtspClientGet( p_media, psz_session );
1157 answer->i_status = 454;
1158 answer->psz_status = strdup( "Unknown session id" );
1160 answer->p_body = NULL;
1165 p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
1168 answer->i_status = 500;
1169 answer->psz_status = strdup( "Internal server error" );
1171 answer->p_body = NULL;
1174 p_rtsp_es->i_port = i_port;
1175 p_rtsp_es->psz_ip = strdup( ip );
1176 p_rtsp_es->p_media_es = p_es;
1177 TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );
1179 answer->i_status = 200;
1180 answer->psz_status = strdup( "OK" );
1182 answer->p_body = NULL;
1184 if( p_media->b_raw )
1186 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
1188 httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
1189 p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1191 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1193 httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
1194 p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1199 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
1200 p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1203 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1205 answer->i_status = 461;
1206 answer->psz_status = strdup( "Unsupported Transport" );
1208 answer->p_body = NULL;
1211 /* Intentional fall-through on x-playNow option in RTSP request */
1215 case HTTPD_MSG_PLAY:
1216 /* This is kind of a kludge. Should we only support Aggregate
1218 psz_session = httpd_MsgGet( query, "Session" );
1219 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1221 p_rtsp = RtspClientGet( p_media, psz_session );
1223 psz_position = httpd_MsgGet( query, "Range" );
1224 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
1230 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1233 /* FIXME: npt= is not necessarily formatted as a float */
1234 f_pos = us_strtod( psz_position, &end );
1235 if( end > psz_position )
1237 f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
1238 vod_MediaControl( p_vod, p_media, psz_session,
1239 VOD_MEDIA_SEEK, f_pos );
1245 answer->i_status = 200;
1246 answer->psz_status = strdup( "OK" );
1248 answer->p_body = NULL;
1252 case HTTPD_MSG_TEARDOWN:
1253 answer->i_status = 200;
1254 answer->psz_status = strdup( "OK" );
1256 answer->p_body = NULL;
1258 psz_session = httpd_MsgGet( query, "Session" );
1259 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1261 p_rtsp = RtspClientGet( p_media, psz_session );
1262 if( !p_rtsp ) break;
1264 for( i = 0; i < p_rtsp->i_es; i++ )
1266 if( p_rtsp->es[i]->p_media_es == p_es )
1268 if( p_rtsp->es[i]->psz_ip ) free( p_rtsp->es[i]->psz_ip );
1269 TAB_REMOVE( p_rtsp->i_es, p_rtsp->es, p_rtsp->es[i] );
1276 vod_MediaControl( p_vod, p_media, psz_session,
1278 RtspClientDel( p_media, p_rtsp );
1282 case HTTPD_MSG_PAUSE:
1283 /* This is kind of a kludge. Should we only support Aggregate
1285 psz_session = httpd_MsgGet( query, "Session" );
1286 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1288 p_rtsp = RtspClientGet( p_media, psz_session );
1289 if( !p_rtsp ) break;
1291 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
1292 p_rtsp->b_paused = VLC_TRUE;
1294 answer->i_status = 200;
1295 answer->psz_status = strdup( "OK" );
1297 answer->p_body = NULL;
1301 return VLC_EGENERIC;
1305 httpd_MsgAdd( answer, "Server", "VLC Server" );
1306 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1307 psz_cseq = httpd_MsgGet( query, "Cseq" );
1309 i_cseq = atoi( psz_cseq );
1312 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1313 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1316 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1321 /*****************************************************************************
1323 * FIXME: need to be moved to a common place ?
1324 *****************************************************************************/
1325 static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
1328 char *p, *psz_sdp, ip[NI_MAXNUMERICHOST], ipv;
1329 const char *psz_control;
1331 if( httpd_ServerIP( cl, ip ) == NULL )
1334 p = strchr( ip, '%' );
1336 *p = '\0'; /* remove scope if present */
1338 ipv = ( strchr( ip, ':' ) != NULL ) ? '6' : '4';
1340 /* Calculate size */
1341 i_size = sizeof( "v=0\r\n" ) +
1342 sizeof( "o=- * * IN IP4 \r\n" ) + 10 + NI_MAXNUMERICHOST +
1343 sizeof( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
1344 sizeof( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
1345 sizeof( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
1346 sizeof( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
1347 sizeof( "c=IN IP4 0.0.0.0\r\n" ) + 20 + 10 +
1348 sizeof( "t=0 0\r\n" ) + /* FIXME */
1349 sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
1350 sizeof( "a=range:npt=0-1000000000.000\r\n" );
1352 psz_control = (ipv == '6') ? p_media->psz_rtsp_control_v6
1353 : p_media->psz_rtsp_control_v4;
1354 for( i = 0; i < p_media->i_es; i++ )
1356 media_es_t *p_es = p_media->es[i];
1358 i_size += sizeof( "m=**d*o * RTP/AVP *\r\n" ) + 19;
1359 if( p_es->psz_rtpmap )
1361 i_size += sizeof( "a=rtpmap:* *\r\n" ) +
1362 strlen( p_es->psz_rtpmap ) + 9;
1364 if( p_es->psz_fmtp )
1366 i_size += sizeof( "a=fmtp:* *\r\n" ) +
1367 strlen( p_es->psz_fmtp ) + 9;
1370 i_size += (strlen( psz_control ) + strlen( ip ) + 9) * p_media->i_es;
1372 p = psz_sdp = malloc( i_size );
1373 p += sprintf( p, "v=0\r\n" );
1374 p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
1375 p_media->i_sdp_id, p_media->i_sdp_version, ipv, ip );
1376 if( *p_media->psz_session_name )
1377 p += sprintf( p, "s=%s\r\n", p_media->psz_session_name );
1378 if( *p_media->psz_session_description )
1379 p += sprintf( p, "i=%s\r\n", p_media->psz_session_description );
1380 if( *p_media->psz_session_url )
1381 p += sprintf( p, "u=%s\r\n", p_media->psz_session_url );
1382 if( *p_media->psz_session_email )
1383 p += sprintf( p, "e=%s\r\n", p_media->psz_session_email );
1385 p += sprintf( p, "c=IN IP%c %s\r\n", ipv, ipv == '6' ? "::" : "0.0.0.0" );
1386 p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
1387 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
1389 if( p_media->i_length > 0 )
1391 lldiv_t d = lldiv( p_media->i_length / 1000, 1000 );
1392 p += sprintf( p, "a=range:npt=0-%lld.%03u\r\n", d.quot,
1396 for( i = 0; i < p_media->i_es; i++ )
1398 media_es_t *p_es = p_media->es[i];
1400 if( p_es->fmt.i_cat == AUDIO_ES )
1402 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
1403 p_es->i_port, p_es->i_payload_type );
1405 else if( p_es->fmt.i_cat == VIDEO_ES )
1407 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
1408 p_es->i_port, p_es->i_payload_type );
1415 if( p_es->psz_rtpmap )
1417 p += sprintf( p, "a=rtpmap:%d %s\r\n", p_es->i_payload_type,
1420 if( p_es->psz_fmtp )
1422 p += sprintf( p, "a=fmtp:%d %s\r\n", p_es->i_payload_type,
1426 p += sprintf( p, psz_control, ip, i );