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_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_input.h>
37 #include <vlc_block.h>
39 #include <vlc_httpd.h>
42 #include <vlc_network.h>
43 #include <vlc_charset.h>
44 #include <vlc_strings.h>
54 /*****************************************************************************
56 *****************************************************************************/
57 static int Open ( vlc_object_t * );
58 static void Close( vlc_object_t * );
60 #define HOST_TEXT N_( "RTSP host address" )
61 #define HOST_LONGTEXT N_( \
62 "This defines the address, port and path the RTSP VOD server will listen " \
63 "on.\nSyntax is address:port/path. The default is to listen on all "\
64 "interfaces (address 0.0.0.0), on port 554, with no path.\nTo listen " \
65 "only on the local interface, use \"localhost\" as address." )
67 #define THROTTLE_TEXT N_( "Maximum number of connections" )
68 #define THROTTLE_LONGTEXT N_( "This limits the maximum number of clients " \
69 "that can connect to the RTSP VOD. 0 means no limit." )
71 #define RAWMUX_TEXT N_( "MUX for RAW RTSP transport" )
73 #define SESSION_TIMEOUT_TEXT N_( "Sets the timeout option in the RTSP " \
75 #define SESSION_TIMEOUT_LONGTEXT N_( "Defines what timeout option to add " \
76 "to the RTSP session ID string. Setting it to a negative number removes " \
77 "the timeout option entirely. This is needed by some IPTV STBs (such as " \
78 "those made by HansunTech) which get confused by it. The default is 5." )
81 set_shortname( N_("RTSP VoD" ) )
82 set_description( N_("RTSP VoD server") )
83 set_category( CAT_SOUT )
84 set_subcategory( SUBCAT_SOUT_VOD )
85 set_capability( "vod server", 1 )
86 set_callbacks( Open, Close )
87 add_shortcut( "rtsp" )
88 add_string ( "rtsp-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, true )
89 add_string( "rtsp-raw-mux", "ts", NULL, RAWMUX_TEXT,
91 add_integer( "rtsp-throttle-users", 0, NULL, THROTTLE_TEXT,
92 THROTTLE_LONGTEXT, true )
93 add_integer( "rtsp-session-timeout", 5, NULL, SESSION_TIMEOUT_TEXT,
94 SESSION_TIMEOUT_LONGTEXT, true )
97 /*****************************************************************************
99 *****************************************************************************/
101 typedef struct media_es_t media_es_t;
105 media_es_t *p_media_es;
114 bool b_playing; /* is it in "play" state */
115 bool b_paused; /* is it in "pause" state */
118 rtsp_client_es_t **es;
128 httpd_url_t *p_rtsp_url;
130 vod_media_t *p_media;
133 uint8_t i_payload_type;
134 const char *psz_ptname;
135 unsigned i_clock_rate;
149 httpd_url_t *p_rtsp_url;
150 char *psz_rtsp_control_v4;
151 char *psz_rtsp_control_v6;
166 rtsp_client_t **rtsp;
175 httpd_host_t *p_rtsp_host;
178 int i_throttle_users;
183 int i_session_timeout;
186 vlc_mutex_t lock_media;
192 block_fifo_t *p_fifo_cmd;
195 /* rtsp delayed command (to avoid deadlock between vlm/httpd) */
198 RTSP_CMD_TYPE_NONE, /* Exit requested */
204 RTSP_CMD_TYPE_REWIND,
205 RTSP_CMD_TYPE_FORWARD,
216 vod_media_t *p_media;
222 static vod_media_t *MediaNew( vod_t *, const char *, input_item_t * );
223 static void MediaDel( vod_t *, vod_media_t * );
224 static void MediaAskDel ( vod_t *, vod_media_t * );
225 static int MediaAddES( vod_t *, vod_media_t *, es_format_t * );
226 static void MediaDelES( vod_t *, vod_media_t *, es_format_t * );
228 static void* CommandThread( vlc_object_t *p_this );
229 static void CommandPush( vod_t *, rtsp_cmd_type_t, vod_media_t *, const char *psz_session,
230 double f_arg, const char *psz_arg );
232 static rtsp_client_t *RtspClientNew( vod_media_t *, char * );
233 static rtsp_client_t *RtspClientGet( vod_media_t *, const char * );
234 static void RtspClientDel( vod_media_t *, rtsp_client_t * );
236 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
237 httpd_message_t *, const httpd_message_t * );
238 static int RtspCallbackES( httpd_callback_sys_t *, httpd_client_t *,
239 httpd_message_t *, const httpd_message_t * );
241 static char *SDPGenerate( const vod_media_t *, httpd_client_t *cl );
243 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
245 static const char hex[16] = "0123456789abcdef";
248 for( i = 0; i < i_data; i++ )
250 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
251 s[2*i+1] = hex[(p_data[i] )&0xf];
256 /*****************************************************************************
257 * Open: Starts the RTSP server module
258 *****************************************************************************/
259 static int Open( vlc_object_t *p_this )
261 vod_t *p_vod = (vod_t *)p_this;
262 vod_sys_t *p_sys = NULL;
263 char *psz_url = NULL;
266 psz_url = var_InheritString( p_vod, "rtsp-host" );
267 vlc_UrlParse( &url, psz_url, 0 );
270 if( url.i_port <= 0 ) url.i_port = 554;
272 p_vod->p_sys = p_sys = malloc( sizeof( vod_sys_t ) );
273 if( !p_sys ) goto error;
274 p_sys->p_rtsp_host = 0;
276 p_sys->i_session_timeout = var_CreateGetInteger( p_this, "rtsp-session-timeout" );
278 p_sys->i_throttle_users = var_CreateGetInteger( p_this, "rtsp-throttle-users" );
279 msg_Dbg( p_this, "allowing up to %d connections", p_sys->i_throttle_users );
280 p_sys->i_connections = 0;
282 p_sys->psz_raw_mux = var_CreateGetString( p_this, "rtsp-raw-mux" );
285 httpd_HostNew( VLC_OBJECT(p_vod), url.psz_host, url.i_port );
286 if( !p_sys->p_rtsp_host )
288 msg_Err( p_vod, "cannot create RTSP server (%s:%i)",
289 url.psz_host, url.i_port );
293 p_sys->psz_path = strdup( url.psz_path ? url.psz_path : "/" );
294 p_sys->i_port = url.i_port;
296 vlc_UrlClean( &url );
298 vlc_mutex_init( &p_sys->lock_media );
300 TAB_INIT( p_sys->i_media, p_sys->media );
301 p_sys->i_media_id = 0;
303 p_vod->pf_media_new = MediaNew;
304 p_vod->pf_media_del = MediaAskDel;
305 p_vod->pf_media_add_es = MediaAddES;
306 p_vod->pf_media_del_es = MediaDelES;
308 p_sys->p_fifo_cmd = block_FifoNew();
309 if( vlc_thread_create( p_vod, "rtsp vod thread", CommandThread,
310 VLC_THREAD_PRIORITY_LOW ) )
312 msg_Err( p_vod, "cannot spawn rtsp vod thread" );
313 block_FifoRelease( p_sys->p_fifo_cmd );
314 free( p_sys->psz_path );
323 if( p_sys->p_rtsp_host ) httpd_HostDelete( p_sys->p_rtsp_host );
324 free( p_sys->psz_raw_mux );
327 vlc_UrlClean( &url );
332 /*****************************************************************************
334 *****************************************************************************/
335 static void Close( vlc_object_t * p_this )
337 vod_t *p_vod = (vod_t *)p_this;
338 vod_sys_t *p_sys = p_vod->p_sys;
339 block_t *p_block_cmd;
342 /* Stop command thread */
343 vlc_object_kill( p_vod );
344 CommandPush( p_vod, RTSP_CMD_TYPE_NONE, NULL, NULL, 0.0, NULL );
345 vlc_thread_join( p_vod );
347 while( block_FifoCount( p_sys->p_fifo_cmd ) > 0 )
349 p_block_cmd = block_FifoGet( p_sys->p_fifo_cmd );
350 memcpy( &cmd, p_block_cmd->p_buffer, sizeof(cmd) );
351 block_Release( p_block_cmd );
352 if ( cmd.i_type == RTSP_CMD_TYPE_DEL )
353 MediaDel(p_vod, cmd.p_media);
354 free( cmd.psz_session );
357 block_FifoRelease( p_sys->p_fifo_cmd );
359 httpd_HostDelete( p_sys->p_rtsp_host );
360 var_Destroy( p_this, "rtsp-session-timeout" );
361 var_Destroy( p_this, "rtsp-throttle-users" );
362 var_Destroy( p_this, "rtsp-raw-mux" );
364 /* Check VLM is not buggy */
365 if( p_sys->i_media > 0 )
366 msg_Err( p_vod, "rtsp vod leaking %d medias", p_sys->i_media );
367 TAB_CLEAN( p_sys->i_media, p_sys->media );
369 vlc_mutex_destroy( &p_sys->lock_media );
371 free( p_sys->psz_path );
372 free( p_sys->psz_raw_mux );
376 /*****************************************************************************
378 *****************************************************************************/
379 static vod_media_t *MediaNew( vod_t *p_vod, const char *psz_name,
380 input_item_t *p_item )
383 vod_sys_t *p_sys = p_vod->p_sys;
385 vod_media_t *p_media = calloc( 1, sizeof(vod_media_t) );
389 p_media->id = p_sys->i_media_id++;
390 TAB_INIT( p_media->i_es, p_media->es );
391 p_media->psz_mux = NULL;
392 TAB_INIT( p_media->i_rtsp, p_media->rtsp );
393 p_media->b_raw = false;
395 if( asprintf( &p_media->psz_rtsp_path, "%s%s",
396 p_sys->psz_path, psz_name ) <0 )
398 p_media->p_rtsp_url =
399 httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, NULL,
402 if( !p_media->p_rtsp_url )
404 msg_Err( p_vod, "cannot create RTSP url (%s)", p_media->psz_rtsp_path);
405 free( p_media->psz_rtsp_path );
410 msg_Dbg( p_vod, "created RTSP url: %s", p_media->psz_rtsp_path );
412 if( asprintf( &p_media->psz_rtsp_control_v4,
413 "rtsp://%%s:%d%s/trackID=%%d",
414 p_sys->i_port, p_media->psz_rtsp_path ) < 0 )
416 httpd_UrlDelete( p_media->p_rtsp_url );
417 free( p_media->psz_rtsp_path );
421 if( asprintf( &p_media->psz_rtsp_control_v6,
422 "rtsp://[%%s]:%d%s/trackID=%%d",
423 p_sys->i_port, p_media->psz_rtsp_path ) < 0 )
425 httpd_UrlDelete( p_media->p_rtsp_url );
426 free( p_media->psz_rtsp_path );
431 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_SETUP,
432 RtspCallback, (void*)p_media );
433 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_DESCRIBE,
434 RtspCallback, (void*)p_media );
435 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PLAY,
436 RtspCallback, (void*)p_media );
437 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PAUSE,
438 RtspCallback, (void*)p_media );
439 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_GETPARAMETER,
440 RtspCallback, (void*)p_media );
441 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_TEARDOWN,
442 RtspCallback, (void*)p_media );
444 p_media->p_vod = p_vod;
446 vlc_mutex_init( &p_media->lock );
448 p_media->i_payload_type = 96;
450 p_media->i_length = input_item_GetDuration( p_item );
452 vlc_mutex_lock( &p_item->lock );
453 msg_Dbg( p_vod, "media has %i declared ES", p_item->i_es );
454 for( i = 0; i < p_item->i_es; i++ )
456 MediaAddES( p_vod, p_media, p_item->es[i] );
458 vlc_mutex_unlock( &p_item->lock );
460 CommandPush( p_vod, RTSP_CMD_TYPE_ADD, p_media, NULL, 0.0, NULL );
464 static void MediaAskDel ( vod_t *p_vod, vod_media_t *p_media )
466 CommandPush( p_vod, RTSP_CMD_TYPE_DEL, p_media, NULL, 0.0, NULL );
469 static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
471 vod_sys_t *p_sys = p_vod->p_sys;
473 msg_Dbg( p_vod, "deleting media: %s", p_media->psz_rtsp_path );
475 vlc_mutex_lock( &p_sys->lock_media );
476 TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
477 vlc_mutex_unlock( &p_sys->lock_media );
479 httpd_UrlDelete( p_media->p_rtsp_url );
481 while( p_media->i_rtsp > 0 )
482 RtspClientDel( p_media, p_media->rtsp[0] );
483 TAB_CLEAN( p_media->i_rtsp, p_media->rtsp );
485 free( p_media->psz_rtsp_path );
486 free( p_media->psz_rtsp_control_v6 );
487 free( p_media->psz_rtsp_control_v4 );
489 while( p_media->i_es )
490 MediaDelES( p_vod, p_media, &p_media->es[0]->fmt );
491 TAB_CLEAN( p_media->i_es, p_media->es );
493 vlc_mutex_destroy( &p_media->lock );
498 static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
501 media_es_t *p_es = calloc( 1, sizeof(media_es_t) );
505 p_media->psz_mux = NULL;
507 /* TODO: update SDP, etc... */
508 if( asprintf( &psz_urlc, "%s/trackID=%d",
509 p_media->psz_rtsp_path, p_media->i_es ) < 0 )
514 msg_Dbg( p_vod, " - ES %4.4s (%s)", (char *)&p_fmt->i_codec, psz_urlc );
516 p_es->i_clock_rate = 90000;
517 p_es->i_channels = 1;
519 switch( p_fmt->i_codec )
522 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
524 p_es->i_payload_type = 11;
526 else if( p_fmt->audio.i_channels == 2 &&
527 p_fmt->audio.i_rate == 44100 )
529 p_es->i_payload_type = 10;
533 p_es->i_payload_type = p_media->i_payload_type++;
535 p_es->psz_ptname = "L16";
536 p_es->i_clock_rate = p_fmt->audio.i_rate;
537 p_es->i_channels = p_fmt->audio.i_channels;
540 p_es->i_payload_type = p_media->i_payload_type++;
541 p_es->psz_ptname = "L8";
542 p_es->i_clock_rate = p_fmt->audio.i_rate;
543 p_es->i_channels = p_fmt->audio.i_channels;
546 p_es->i_payload_type = 14;
547 p_es->psz_ptname = "MPA";
550 p_es->i_payload_type = 32;
551 p_es->psz_ptname = "MPV";
554 p_es->i_payload_type = p_media->i_payload_type++;
555 p_es->psz_ptname = "ac3";
556 p_es->i_clock_rate = p_fmt->audio.i_rate;
559 p_es->i_payload_type = p_media->i_payload_type++;
560 p_es->psz_ptname = "H263-1998";
563 p_es->i_payload_type = p_media->i_payload_type++;
564 p_es->psz_ptname = "H264";
565 p_es->psz_fmtp = NULL;
566 /* FIXME AAAAAAAAAAAARRRRRRRRGGGG copied from stream_out/rtp.c */
567 if( p_fmt->i_extra > 0 )
569 uint8_t *p_buffer = p_fmt->p_extra;
570 int i_buffer = p_fmt->i_extra;
571 char *p_64_sps = NULL;
572 char *p_64_pps = NULL;
575 while( i_buffer > 4 &&
576 p_buffer[0] == 0 && p_buffer[1] == 0 &&
577 p_buffer[2] == 0 && p_buffer[3] == 1 )
579 const int i_nal_type = p_buffer[4]&0x1f;
584 for( i_offset = 4; i_offset+3 < i_buffer ; i_offset++)
586 if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 0 && p_buffer[i_offset+3] == 1 )
588 /* we found another startcode */
593 if( i_nal_type == 7 )
596 p_64_sps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
597 sprintf_hexa( hexa, &p_buffer[5], 3 );
599 else if( i_nal_type == 8 )
602 p_64_pps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
608 if( p_64_sps && p_64_pps )
610 if( asprintf( &p_es->psz_fmtp,
611 "packetization-mode=1;profile-level-id=%s;"
612 "sprop-parameter-sets=%s,%s;", hexa, p_64_sps,
625 if( !p_es->psz_fmtp )
626 p_es->psz_fmtp = strdup( "packetization-mode=1" );
629 p_es->i_payload_type = p_media->i_payload_type++;
630 p_es->psz_ptname = "MP4V-ES";
631 if( p_fmt->i_extra > 0 )
633 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
634 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
635 if( asprintf( &p_es->psz_fmtp,
636 "profile-level-id=3; config=%s;", p_hexa ) == -1 )
637 p_es->psz_fmtp = NULL;
642 p_es->i_payload_type = p_media->i_payload_type++;
643 p_es->psz_ptname = "mpeg4-generic";
644 p_es->i_clock_rate = p_fmt->audio.i_rate;
645 if( p_fmt->i_extra > 0 )
647 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
648 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
649 if( asprintf( &p_es->psz_fmtp,
650 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
651 "config=%s; SizeLength=13;IndexLength=3; "
652 "IndexDeltaLength=3; Profile=1;", p_hexa ) == -1 )
653 p_es->psz_fmtp = NULL;
657 case VLC_FOURCC( 'm', 'p', '2', 't' ):
658 p_media->psz_mux = "ts";
659 p_es->i_payload_type = 33;
660 p_es->psz_ptname = "MP2T";
662 case VLC_FOURCC( 'm', 'p', '2', 'p' ):
663 p_media->psz_mux = "ps";
664 p_es->i_payload_type = p_media->i_payload_type++;
665 p_es->psz_ptname = "MP2P";
667 case VLC_CODEC_AMR_NB:
668 p_es->i_payload_type = p_media->i_payload_type++;
669 p_es->psz_ptname = "AMR";
670 p_es->i_clock_rate = 8000;
671 if(p_fmt->audio.i_channels == 2 )
672 p_es->i_channels = 2;
673 p_es->psz_fmtp = strdup( "octet-align=1" );
675 case VLC_CODEC_AMR_WB:
676 p_es->i_payload_type = p_media->i_payload_type++;
677 p_es->psz_ptname = "AMR-WB";
678 p_es->i_clock_rate = 16000;
679 if(p_fmt->audio.i_channels == 2 )
680 p_es->i_channels = 2;
681 p_es->psz_fmtp = strdup( "octet-align=1" );
685 msg_Err( p_vod, "cannot add this stream (unsupported "
686 "codec: %4.4s)", (char*)&p_fmt->i_codec );
693 httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, NULL, NULL,
696 if( !p_es->p_rtsp_url )
698 msg_Err( p_vod, "cannot create RTSP url (%s)", psz_urlc );
705 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
706 RtspCallbackES, (void*)p_es );
707 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_TEARDOWN,
708 RtspCallbackES, (void*)p_es );
709 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PLAY,
710 RtspCallbackES, (void*)p_es );
711 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PAUSE,
712 RtspCallbackES, (void*)p_es );
714 es_format_Copy( &p_es->fmt, p_fmt );
716 p_es->p_media = p_media;
718 vlc_mutex_lock( &p_media->lock );
719 TAB_APPEND( p_media->i_es, p_media->es, p_es );
720 vlc_mutex_unlock( &p_media->lock );
725 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
727 media_es_t *p_es = NULL;
731 for( i = 0; i < p_media->i_es; i++ )
733 if( p_media->es[i]->fmt.i_cat == p_fmt->i_cat &&
734 p_media->es[i]->fmt.i_codec == p_fmt->i_codec &&
735 p_media->es[i]->fmt.i_id == p_fmt->i_id )
737 p_es = p_media->es[i];
742 msg_Dbg( p_vod, " - Removing ES %4.4s", (char *)&p_fmt->i_codec );
744 vlc_mutex_lock( &p_media->lock );
745 TAB_REMOVE( p_media->i_es, p_media->es, p_es );
746 vlc_mutex_unlock( &p_media->lock );
748 free( p_es->psz_fmtp );
750 if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
751 es_format_Clean( &p_es->fmt );
755 static void CommandPush( vod_t *p_vod, rtsp_cmd_type_t i_type, vod_media_t *p_media, const char *psz_session,
756 double f_arg, const char *psz_arg )
761 memset( &cmd, 0, sizeof(cmd) );
763 cmd.p_media = p_media;
765 cmd.i_media_id = p_media->id;
767 cmd.psz_session = strdup(psz_session);
770 cmd.psz_arg = strdup(psz_arg);
772 p_cmd = block_New( p_vod, sizeof(rtsp_cmd_t) );
773 memcpy( p_cmd->p_buffer, &cmd, sizeof(cmd) );
775 block_FifoPut( p_vod->p_sys->p_fifo_cmd, p_cmd );
778 static void* CommandThread( vlc_object_t *p_this )
780 vod_t *p_vod = (vod_t*)p_this;
781 vod_sys_t *p_sys = p_vod->p_sys;
782 int canc = vlc_savecancel ();
784 while( vlc_object_alive (p_vod) )
786 block_t *p_block_cmd = block_FifoGet( p_sys->p_fifo_cmd );
788 vod_media_t *p_media = NULL;
794 memcpy( &cmd, p_block_cmd->p_buffer, sizeof(cmd) );
795 block_Release( p_block_cmd );
797 if( cmd.i_type == RTSP_CMD_TYPE_NONE )
800 if ( cmd.i_type == RTSP_CMD_TYPE_ADD )
802 vlc_mutex_lock( &p_sys->lock_media );
803 TAB_APPEND( p_sys->i_media, p_sys->media, cmd.p_media );
804 vlc_mutex_unlock( &p_sys->lock_media );
808 if ( cmd.i_type == RTSP_CMD_TYPE_DEL )
810 MediaDel(p_vod, cmd.p_media);
815 vlc_mutex_lock( &p_sys->lock_media );
816 for( i = 0; i < p_sys->i_media; i++ )
818 if( p_sys->media[i]->id == cmd.i_media_id )
821 if( i >= p_sys->i_media )
823 vlc_mutex_unlock( &p_sys->lock_media );
826 p_media = p_sys->media[i];
830 case RTSP_CMD_TYPE_PLAY:
831 vod_MediaControl( p_vod, p_media, cmd.psz_session,
832 VOD_MEDIA_PLAY, cmd.psz_arg );
834 case RTSP_CMD_TYPE_PAUSE:
835 vod_MediaControl( p_vod, p_media, cmd.psz_session,
839 case RTSP_CMD_TYPE_STOP:
840 vod_MediaControl( p_vod, p_media, cmd.psz_session, VOD_MEDIA_STOP );
843 case RTSP_CMD_TYPE_SEEK:
844 vod_MediaControl( p_vod, p_media, cmd.psz_session,
845 VOD_MEDIA_SEEK, cmd.f_arg );
848 case RTSP_CMD_TYPE_REWIND:
849 vod_MediaControl( p_vod, p_media, cmd.psz_session,
850 VOD_MEDIA_REWIND, cmd.f_arg );
853 case RTSP_CMD_TYPE_FORWARD:
854 vod_MediaControl( p_vod, p_media, cmd.psz_session,
855 VOD_MEDIA_FORWARD, cmd.f_arg );
861 vlc_mutex_unlock( &p_sys->lock_media );
864 free( cmd.psz_session );
868 vlc_restorecancel (canc);
872 /****************************************************************************
873 * RTSP server implementation
874 ****************************************************************************/
875 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
877 rtsp_client_t *p_rtsp = calloc( 1, sizeof(rtsp_client_t) );
883 p_rtsp->psz_session = psz_session;
884 TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
886 p_media->p_vod->p_sys->i_connections++;
887 msg_Dbg( p_media->p_vod, "new session: %s, connections: %d",
888 psz_session, p_media->p_vod->p_sys->i_throttle_users );
893 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, const char *psz_session )
897 for( i = 0; psz_session && i < p_media->i_rtsp; i++ )
899 if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
900 return p_media->rtsp[i];
906 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
908 p_media->p_vod->p_sys->i_connections--;
909 msg_Dbg( p_media->p_vod, "closing session: %s, connections: %d",
910 p_rtsp->psz_session, p_media->p_vod->p_sys->i_throttle_users );
912 while( p_rtsp->i_es )
915 free( p_rtsp->es[p_rtsp->i_es] );
919 TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, p_rtsp );
921 free( p_rtsp->psz_session );
926 static float ParseNPT (const char *str)
928 locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
929 locale_t oldloc = uselocale (loc);
933 if (sscanf (str, "%u:%u:%f", &hour, &min, &sec) == 3)
934 sec += ((hour * 60) + min) * 60;
936 if (sscanf (str, "%f", &sec) != 1)
939 if (loc != (locale_t)0)
948 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
949 httpd_message_t *answer, const httpd_message_t *query )
951 vod_media_t *p_media = (vod_media_t*)p_args;
952 vod_t *p_vod = p_media->p_vod;
953 const char *psz_transport = NULL;
954 const char *psz_playnow = NULL; /* support option: x-playNow */
955 const char *psz_session = NULL;
956 const char *psz_cseq = NULL;
957 rtsp_client_t *p_rtsp;
961 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
963 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
965 answer->i_proto = HTTPD_PROTO_RTSP;
966 answer->i_version = query->i_version;
967 answer->i_type = HTTPD_MSG_ANSWER;
969 answer->p_body = NULL;
971 switch( query->i_type )
973 case HTTPD_MSG_SETUP:
975 psz_playnow = httpd_MsgGet( query, "x-playNow" );
976 psz_transport = httpd_MsgGet( query, "Transport" );
977 if( psz_transport == NULL )
979 answer->i_status = 400;
982 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
984 if( strstr( psz_transport, "unicast" ) &&
985 strstr( psz_transport, "client_port=" ) )
987 rtsp_client_t *p_rtsp = NULL;
988 char ip[NI_MAXNUMERICHOST];
989 i_port = atoi( strstr( psz_transport, "client_port=" ) +
990 strlen("client_port=") );
992 if( strstr( psz_transport, "MP2T/H2221/UDP" ) ||
993 strstr( psz_transport, "RAW/RAW/UDP" ) )
995 p_media->psz_mux = p_vod->p_sys->psz_raw_mux;
996 p_media->b_raw = true;
999 if( httpd_ClientIP( cl, ip ) == NULL )
1001 answer->i_status = 500;
1003 answer->p_body = NULL;
1007 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
1010 psz_session = httpd_MsgGet( query, "Session" );
1011 if( !psz_session || !*psz_session )
1014 if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
1015 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
1017 answer->i_status = 503;
1019 answer->p_body = NULL;
1022 if( asprintf( &psz_new, "%d", rand() ) < 0 )
1024 psz_session = psz_new;
1026 p_rtsp = RtspClientNew( p_media, psz_new );
1029 answer->i_status = 454;
1031 answer->p_body = NULL;
1037 p_rtsp = RtspClientGet( p_media, psz_session );
1040 answer->i_status = 454;
1042 answer->p_body = NULL;
1047 answer->i_status = 200;
1049 answer->p_body = NULL;
1051 if( p_media->b_raw )
1053 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
1055 httpd_MsgAdd( answer, "Transport",
1056 "MP2T/H2221/UDP;unicast;client_port=%d-%d",
1057 i_port, i_port + 1 );
1059 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1061 httpd_MsgAdd( answer, "Transport",
1062 "RAW/RAW/UDP;unicast;client_port=%d-%d",
1063 i_port, i_port + 1 );
1067 httpd_MsgAdd( answer, "Transport",
1068 "RTP/AVP/UDP;unicast;client_port=%d-%d",
1069 i_port, i_port + 1 );
1071 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1073 answer->i_status = 461;
1075 answer->p_body = NULL;
1078 /* Intentional fall-through on x-playNow option in RTSP request */
1083 case HTTPD_MSG_PLAY:
1085 char *psz_output, ip[NI_MAXNUMERICHOST];
1086 int i, i_port_audio = 0, i_port_video = 0;
1088 /* for now only multicast so easy */
1091 answer->i_status = 200;
1093 answer->p_body = NULL;
1097 psz_session = httpd_MsgGet( query, "Session" );
1098 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1100 p_rtsp = RtspClientGet( p_media, psz_session );
1103 answer->i_status = 500;
1105 answer->p_body = NULL;
1109 if( p_rtsp->b_playing )
1111 const char *psz_position = httpd_MsgGet( query, "Range" );
1112 const char *psz_scale = httpd_MsgGet( query, "Scale" );
1114 psz_position = strstr( psz_position, "npt=" );
1115 if( psz_position && !psz_scale )
1117 double f_pos = ParseNPT (psz_position + 4);
1118 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1119 f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
1120 CommandPush( p_vod, RTSP_CMD_TYPE_SEEK, p_media,
1121 psz_session, f_pos, NULL );
1123 else if( psz_scale )
1125 double f_scale = 0.0;
1128 f_scale = us_strtod( psz_scale, &end );
1129 if( end > psz_scale )
1131 f_scale = (f_scale * 30.0);
1132 if( psz_scale[0] == '-' ) /* rewind */
1134 msg_Dbg( p_vod, "rewind request: %s", psz_scale );
1135 CommandPush( p_vod, RTSP_CMD_TYPE_REWIND, p_media,
1136 psz_session, f_scale, NULL );
1138 else if(psz_scale[0] != '1' ) /* fast-forward */
1140 msg_Dbg( p_vod, "fastforward request: %s",
1142 CommandPush( p_vod, RTSP_CMD_TYPE_FORWARD, p_media,
1143 psz_session, f_scale, NULL );
1146 if( p_rtsp->b_paused == true )
1148 p_rtsp->b_paused = false;
1149 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media,
1150 psz_session, 0, NULL );
1157 if( p_rtsp->b_playing && p_rtsp->b_paused )
1159 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media,
1160 psz_session, 0, NULL );
1161 p_rtsp->b_paused = false;
1164 else if( p_rtsp->b_playing ) break;
1166 if( httpd_ClientIP( cl, ip ) == NULL ) break;
1168 p_rtsp->b_playing = true;
1170 /* FIXME for != 1 video and 1 audio */
1171 for( i = 0; i < p_rtsp->i_es; i++ )
1173 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == AUDIO_ES )
1174 i_port_audio = p_rtsp->es[i]->i_port;
1175 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == VIDEO_ES )
1176 i_port_video = p_rtsp->es[i]->i_port;
1179 if( p_media->psz_mux )
1181 if( p_media->b_raw )
1183 if( asprintf( &psz_output,
1184 "std{access=udp,dst=%s:%i,mux=%s}",
1185 ip, i_port, p_media->psz_mux ) < 0 )
1190 if( asprintf( &psz_output,
1191 "rtp{dst=%s,port=%i,mux=%s}",
1192 ip, i_port_video, p_media->psz_mux ) < 0 )
1198 if( asprintf( &psz_output,
1199 "rtp{dst=%s,port-video=%i,port-audio=%i}",
1200 ip, i_port_video, i_port_audio ) < 0 )
1204 CommandPush( p_vod, RTSP_CMD_TYPE_PLAY, p_media, psz_session,
1210 case HTTPD_MSG_DESCRIBE:
1213 SDPGenerate( p_media, cl );
1215 if( psz_sdp != NULL )
1217 answer->i_status = 200;
1218 httpd_MsgAdd( answer, "Content-type", "%s",
1219 "application/sdp" );
1221 answer->p_body = (uint8_t *)psz_sdp;
1222 answer->i_body = strlen( psz_sdp );
1226 answer->i_status = 500;
1227 answer->p_body = NULL;
1233 case HTTPD_MSG_PAUSE:
1234 psz_session = httpd_MsgGet( query, "Session" );
1235 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1237 p_rtsp = RtspClientGet( p_media, psz_session );
1238 if( !p_rtsp ) break;
1240 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media, psz_session,
1242 p_rtsp->b_paused = true;
1244 answer->i_status = 200;
1246 answer->p_body = NULL;
1249 case HTTPD_MSG_TEARDOWN:
1250 /* for now only multicast so easy again */
1251 answer->i_status = 200;
1253 answer->p_body = NULL;
1255 psz_session = httpd_MsgGet( query, "Session" );
1256 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1258 p_rtsp = RtspClientGet( p_media, psz_session );
1259 if( !p_rtsp ) break;
1261 CommandPush( p_vod, RTSP_CMD_TYPE_STOP, p_media, psz_session,
1263 RtspClientDel( p_media, p_rtsp );
1266 case HTTPD_MSG_GETPARAMETER:
1267 answer->i_status = 200;
1269 answer->p_body = NULL;
1273 return VLC_EGENERIC;
1276 httpd_MsgAdd( answer, "Server", "VLC Server" );
1277 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1278 psz_cseq = httpd_MsgGet( query, "Cseq" );
1279 psz_cseq ? i_cseq = atoi( psz_cseq ) : 0;
1280 httpd_MsgAdd( answer, "CSeq", "%d", i_cseq );
1281 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1285 if( p_media->p_vod->p_sys->i_session_timeout >= 0 )
1286 httpd_MsgAdd( answer, "Session", "%s;timeout=%i", psz_session,
1287 p_media->p_vod->p_sys->i_session_timeout );
1289 httpd_MsgAdd( answer, "Session", "%s", psz_session );
1295 static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
1296 httpd_message_t *answer,
1297 const httpd_message_t *query )
1299 media_es_t *p_es = (media_es_t*)p_args;
1300 vod_media_t *p_media = p_es->p_media;
1301 vod_t *p_vod = p_media->p_vod;
1302 rtsp_client_t *p_rtsp = NULL;
1303 const char *psz_transport = NULL;
1304 const char *psz_playnow = NULL; /* support option: x-playNow */
1305 const char *psz_session = NULL;
1306 const char *psz_position = NULL;
1307 const char *psz_cseq = NULL;
1311 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
1313 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
1315 answer->i_proto = HTTPD_PROTO_RTSP;
1316 answer->i_version = query->i_version;
1317 answer->i_type = HTTPD_MSG_ANSWER;
1319 answer->p_body = NULL;
1321 switch( query->i_type )
1323 case HTTPD_MSG_SETUP:
1324 psz_playnow = httpd_MsgGet( query, "x-playNow" );
1325 psz_transport = httpd_MsgGet( query, "Transport" );
1327 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
1329 if( strstr( psz_transport, "unicast" ) &&
1330 strstr( psz_transport, "client_port=" ) )
1332 rtsp_client_t *p_rtsp = NULL;
1333 rtsp_client_es_t *p_rtsp_es = NULL;
1334 char ip[NI_MAXNUMERICHOST];
1335 int i_port = atoi( strstr( psz_transport, "client_port=" ) +
1336 strlen("client_port=") );
1338 if( httpd_ClientIP( cl, ip ) == NULL )
1340 answer->i_status = 500;
1342 answer->p_body = NULL;
1346 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
1349 psz_session = httpd_MsgGet( query, "Session" );
1350 if( !psz_session || !*psz_session )
1353 if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
1354 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
1356 answer->i_status = 503;
1358 answer->p_body = NULL;
1361 if( asprintf( &psz_new, "%d", rand() ) < 0 )
1363 psz_session = psz_new;
1365 p_rtsp = RtspClientNew( p_media, psz_new );
1368 answer->i_status = 454;
1370 answer->p_body = NULL;
1376 p_rtsp = RtspClientGet( p_media, psz_session );
1379 answer->i_status = 454;
1381 answer->p_body = NULL;
1386 p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
1389 answer->i_status = 500;
1391 answer->p_body = NULL;
1394 p_rtsp_es->i_port = i_port;
1395 p_rtsp_es->p_media_es = p_es;
1396 TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );
1398 answer->i_status = 200;
1400 answer->p_body = NULL;
1402 if( p_media->b_raw )
1404 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
1406 httpd_MsgAdd( answer, "Transport",
1407 "MP2T/H2221/UDP;client_port=%d-%d",
1408 p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1410 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1412 httpd_MsgAdd( answer, "Transport",
1413 "RAW/RAW/UDP;client_port=%d-%d",
1414 p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1419 httpd_MsgAdd( answer, "Transport",
1420 "RTP/AVP/UDP;client_port=%d-%d",
1421 p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1424 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1426 answer->i_status = 461;
1428 answer->p_body = NULL;
1431 /* Intentional fall-through on x-playNow option in RTSP request */
1435 case HTTPD_MSG_PLAY:
1436 /* This is kind of a kludge. Should we only support Aggregate
1438 psz_session = httpd_MsgGet( query, "Session" );
1439 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1441 p_rtsp = RtspClientGet( p_media, psz_session );
1443 psz_position = httpd_MsgGet( query, "Range" );
1444 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
1447 double f_pos = ParseNPT (psz_position + 4);
1448 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1449 f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
1450 CommandPush( p_vod, RTSP_CMD_TYPE_SEEK, p_media,
1451 psz_session, f_pos, NULL );
1456 answer->i_status = 200;
1458 answer->p_body = NULL;
1462 case HTTPD_MSG_TEARDOWN:
1463 answer->i_status = 200;
1465 answer->p_body = NULL;
1467 psz_session = httpd_MsgGet( query, "Session" );
1468 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1470 p_rtsp = RtspClientGet( p_media, psz_session );
1471 if( !p_rtsp ) break;
1473 for( i = 0; i < p_rtsp->i_es; i++ )
1475 if( p_rtsp->es[i]->p_media_es == p_es )
1477 TAB_REMOVE( p_rtsp->i_es, p_rtsp->es, p_rtsp->es[i] );
1484 CommandPush( p_vod, RTSP_CMD_TYPE_STOP, p_media, psz_session,
1486 RtspClientDel( p_media, p_rtsp );
1490 case HTTPD_MSG_PAUSE:
1491 /* This is kind of a kludge. Should we only support Aggregate
1493 psz_session = httpd_MsgGet( query, "Session" );
1494 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1496 p_rtsp = RtspClientGet( p_media, psz_session );
1497 if( !p_rtsp ) break;
1499 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media, psz_session,
1501 p_rtsp->b_paused = true;
1503 answer->i_status = 200;
1505 answer->p_body = NULL;
1509 return VLC_EGENERIC;
1513 httpd_MsgAdd( answer, "Server", "VLC Server" );
1514 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1515 psz_cseq = httpd_MsgGet( query, "Cseq" );
1517 i_cseq = atoi( psz_cseq );
1520 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1521 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1524 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1529 /*****************************************************************************
1531 * FIXME: need to be moved to a common place ?
1532 *****************************************************************************/
1533 static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
1535 char *psz_sdp, ip[NI_MAXNUMERICHOST];
1536 const char *psz_control;
1538 if( httpd_ServerIP( cl, ip ) == NULL )
1541 bool ipv6 = ( strchr( ip, ':' ) != NULL );
1543 psz_control = ipv6 ? p_media->psz_rtsp_control_v6
1544 : p_media->psz_rtsp_control_v4;
1546 /* Dummy destination address for RTSP */
1547 struct sockaddr_storage dst;
1548 socklen_t dstlen = ipv6 ? sizeof( struct sockaddr_in6 )
1549 : sizeof( struct sockaddr_in );
1550 memset (&dst, 0, dstlen);
1551 dst.ss_family = ipv6 ? AF_INET6 : AF_INET;
1553 dst.ss_len = dstlen;
1556 psz_sdp = vlc_sdp_Start( VLC_OBJECT( p_media->p_vod ), "sout-rtp-",
1557 NULL, 0, (struct sockaddr *)&dst, dstlen );
1558 if( psz_sdp == NULL )
1561 if( p_media->i_length > 0 )
1563 lldiv_t d = lldiv( p_media->i_length / 1000, 1000 );
1564 sdp_AddAttribute( &psz_sdp, "range"," npt=0-%lld.%03u", d.quot,
1568 for( int i = 0; i < p_media->i_es; i++ )
1570 media_es_t *p_es = p_media->es[i];
1571 const char *mime_major; /* major MIME type */
1573 switch( p_es->fmt.i_cat )
1576 mime_major = "video";
1579 mime_major = "audio";
1582 mime_major = "text";
1588 sdp_AddMedia( &psz_sdp, mime_major, "RTP/AVP", 0 /* p_es->i_port */,
1589 p_es->i_payload_type, false, 0,
1590 p_es->psz_ptname, p_es->i_clock_rate, p_es->i_channels,
1593 sdp_AddAttribute( &psz_sdp, "control", psz_control, ip, i );