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>
56 /*****************************************************************************
58 *****************************************************************************/
59 static int Open ( vlc_object_t * );
60 static void Close( vlc_object_t * );
62 #define HOST_TEXT N_( "RTSP host address" )
63 #define HOST_LONGTEXT N_( \
64 "This defines the address, port and path the RTSP VOD server will listen " \
65 "on.\nSyntax is address:port/path. The default is to listen on all "\
66 "interfaces (address 0.0.0.0), on port 554, with no path.\nTo listen " \
67 "only on the local interface, use \"localhost\" as address." )
69 #define THROTLE_TEXT N_( "Maximum number of connections" )
70 #define THROTLE_LONGTEXT N_( "This limits the maximum number of clients " \
71 "that can connect to the RTSP VOD. 0 means no limit." )
73 #define RAWMUX_TEXT N_( "MUX for RAW RTSP transport" )
75 #define SESSION_TIMEOUT_TEXT N_( "Sets the timeout option in the RTSP " \
77 #define SESSION_TIMEOUT_LONGTEXT N_( "Defines what timeout option to add " \
78 "to the RTSP session ID string. Setting it to a negative number removes " \
79 "the timeout option entirely. This is needed by some IPTV STBs (such as " \
80 "those made by HansunTech) which get confused by it. The default is 5." )
83 set_shortname( N_("RTSP VoD" ) )
84 set_description( N_("RTSP VoD server") )
85 set_category( CAT_SOUT )
86 set_subcategory( SUBCAT_SOUT_VOD )
87 set_capability( "vod server", 1 )
88 set_callbacks( Open, Close )
89 add_shortcut( "rtsp" )
90 add_string ( "rtsp-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, true )
91 add_string( "rtsp-raw-mux", "ts", NULL, RAWMUX_TEXT,
93 add_integer( "rtsp-throttle-users", 0, NULL, THROTLE_TEXT,
94 THROTLE_LONGTEXT, true )
95 add_integer( "rtsp-session-timeout", 5, NULL, SESSION_TIMEOUT_TEXT,
96 SESSION_TIMEOUT_LONGTEXT, true )
99 /*****************************************************************************
100 * Exported prototypes
101 *****************************************************************************/
103 typedef struct media_es_t media_es_t;
107 media_es_t *p_media_es;
116 int64_t i_last; /* for timeout */
118 bool b_playing; /* is it in "play" state */
119 bool b_paused; /* is it in "pause" state */
122 rtsp_client_es_t **es;
132 httpd_url_t *p_rtsp_url;
134 vod_media_t *p_media;
138 uint8_t i_payload_type;
152 httpd_url_t *p_rtsp_url;
153 char *psz_rtsp_control_v4;
154 char *psz_rtsp_control_v6;
178 rtsp_client_t **rtsp;
181 char *psz_session_name;
182 char *psz_session_description;
183 char *psz_session_url;
184 char *psz_session_email;
191 httpd_host_t *p_rtsp_host;
194 int i_throttle_users;
199 int i_session_timeout;
202 vlc_mutex_t lock_media;
208 block_fifo_t *p_fifo_cmd;
211 /* rtsp delayed command (to avoid deadlock between vlm/httpd) */
214 RTSP_CMD_TYPE_NONE, /* Exit requested */
220 RTSP_CMD_TYPE_REWIND,
221 RTSP_CMD_TYPE_FORWARD,
229 //vod_media_t *p_media;
235 static vod_media_t *MediaNew( vod_t *, const char *, input_item_t * );
236 static void MediaDel( vod_t *, vod_media_t * );
237 static int MediaAddES( vod_t *, vod_media_t *, es_format_t * );
238 static void MediaDelES( vod_t *, vod_media_t *, es_format_t * );
240 static void* CommandThread( vlc_object_t *p_this );
241 static void CommandPush( vod_t *, rtsp_cmd_type_t, vod_media_t *, const char *psz_session,
242 double f_arg, const char *psz_arg );
244 static rtsp_client_t *RtspClientNew( vod_media_t *, char * );
245 static rtsp_client_t *RtspClientGet( vod_media_t *, const char * );
246 static void RtspClientDel( vod_media_t *, rtsp_client_t * );
248 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
249 httpd_message_t *, const httpd_message_t * );
250 static int RtspCallbackES( httpd_callback_sys_t *, httpd_client_t *,
251 httpd_message_t *, const httpd_message_t * );
253 static char *SDPGenerate( const vod_media_t *, httpd_client_t *cl );
255 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
257 static const char hex[16] = "0123456789abcdef";
260 for( i = 0; i < i_data; i++ )
262 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
263 s[2*i+1] = hex[(p_data[i] )&0xf];
268 /*****************************************************************************
269 * Open: Starts the RTSP server module
270 *****************************************************************************/
271 static int Open( vlc_object_t *p_this )
273 vod_t *p_vod = (vod_t *)p_this;
274 vod_sys_t *p_sys = NULL;
275 char *psz_url = NULL;
278 psz_url = config_GetPsz( p_vod, "rtsp-host" );
279 vlc_UrlParse( &url, psz_url, 0 );
282 if( url.i_port <= 0 ) url.i_port = 554;
284 p_vod->p_sys = p_sys = malloc( sizeof( vod_sys_t ) );
285 if( !p_sys ) goto error;
286 p_sys->p_rtsp_host = 0;
288 p_sys->i_session_timeout = var_CreateGetInteger( p_this, "rtsp-session-timeout" );
290 p_sys->i_throttle_users = var_CreateGetInteger( p_this, "rtsp-throttle-users" );
291 msg_Dbg( p_this, "allowing up to %d connections", p_sys->i_throttle_users );
292 p_sys->i_connections = 0;
294 p_sys->psz_raw_mux = var_CreateGetString( p_this, "rtsp-raw-mux" );
297 httpd_HostNew( VLC_OBJECT(p_vod), url.psz_host, url.i_port );
298 if( !p_sys->p_rtsp_host )
300 msg_Err( p_vod, "cannot create RTSP server (%s:%i)",
301 url.psz_host, url.i_port );
305 p_sys->psz_path = strdup( url.psz_path ? url.psz_path : "/" );
306 p_sys->i_port = url.i_port;
308 vlc_UrlClean( &url );
310 vlc_mutex_init( &p_sys->lock_media );
312 TAB_INIT( p_sys->i_media, p_sys->media );
313 p_sys->i_media_id = 0;
315 p_vod->pf_media_new = MediaNew;
316 p_vod->pf_media_del = MediaDel;
317 p_vod->pf_media_add_es = MediaAddES;
318 p_vod->pf_media_del_es = MediaDelES;
320 p_sys->p_fifo_cmd = block_FifoNew();
321 if( vlc_thread_create( p_vod, "rtsp vod thread", CommandThread,
322 VLC_THREAD_PRIORITY_LOW ) )
324 msg_Err( p_vod, "cannot spawn rtsp vod thread" );
325 block_FifoRelease( p_sys->p_fifo_cmd );
326 free( p_sys->psz_path );
335 if( p_sys->p_rtsp_host ) httpd_HostDelete( p_sys->p_rtsp_host );
336 free( p_sys->psz_raw_mux );
339 vlc_UrlClean( &url );
344 /*****************************************************************************
346 *****************************************************************************/
347 static void Close( vlc_object_t * p_this )
349 vod_t *p_vod = (vod_t *)p_this;
350 vod_sys_t *p_sys = p_vod->p_sys;
351 block_t *p_block_cmd;
354 /* Stop command thread */
355 vlc_object_kill( p_vod );
356 CommandPush( p_vod, RTSP_CMD_TYPE_NONE, NULL, NULL, 0.0, NULL );
357 vlc_thread_join( p_vod );
359 while( block_FifoCount( p_sys->p_fifo_cmd ) > 0 )
361 p_block_cmd = block_FifoGet( p_sys->p_fifo_cmd );
362 memcpy( &cmd, p_block_cmd->p_buffer, sizeof(cmd) );
363 block_Release( p_block_cmd );
364 free( cmd.psz_session );
367 block_FifoRelease( p_sys->p_fifo_cmd );
369 httpd_HostDelete( p_sys->p_rtsp_host );
370 var_Destroy( p_this, "rtsp-session-timeout" );
371 var_Destroy( p_this, "rtsp-throttle-users" );
372 var_Destroy( p_this, "rtsp-raw-mux" );
374 /* Check VLM is not buggy */
375 if( p_sys->i_media > 0 )
376 msg_Err( p_vod, "rtsp vod leaking %d medias", p_sys->i_media );
377 TAB_CLEAN( p_sys->i_media, p_sys->media );
379 vlc_mutex_destroy( &p_sys->lock_media );
381 free( p_sys->psz_path );
382 free( p_sys->psz_raw_mux );
386 /*****************************************************************************
388 *****************************************************************************/
389 static vod_media_t *MediaNew( vod_t *p_vod, const char *psz_name,
390 input_item_t *p_item )
393 vod_sys_t *p_sys = p_vod->p_sys;
395 vod_media_t *p_media = calloc( 1, sizeof(vod_media_t) );
399 p_media->id = p_sys->i_media_id++;
400 TAB_INIT( p_media->i_es, p_media->es );
401 p_media->psz_mux = NULL;
402 TAB_INIT( p_media->i_rtsp, p_media->rtsp );
403 p_media->b_raw = false;
405 if( asprintf( &p_media->psz_rtsp_path, "%s%s",
406 p_sys->psz_path, psz_name ) <0 )
408 p_media->p_rtsp_url =
409 httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, NULL,
412 if( !p_media->p_rtsp_url )
414 msg_Err( p_vod, "cannot create RTSP url (%s)", p_media->psz_rtsp_path);
415 free( p_media->psz_rtsp_path );
420 msg_Dbg( p_vod, "created RTSP url: %s", p_media->psz_rtsp_path );
422 if( asprintf( &p_media->psz_rtsp_control_v4,
423 "a=control:rtsp://%%s:%d%s/trackID=%%d\r\n",
424 p_sys->i_port, p_media->psz_rtsp_path ) < 0 )
426 httpd_UrlDelete( p_media->p_rtsp_url );
427 free( p_media->psz_rtsp_path );
431 if( asprintf( &p_media->psz_rtsp_control_v6,
432 "a=control:rtsp://[%%s]:%d%s/trackID=%%d\r\n",
433 p_sys->i_port, p_media->psz_rtsp_path ) < 0 )
435 httpd_UrlDelete( p_media->p_rtsp_url );
436 free( p_media->psz_rtsp_path );
441 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_SETUP,
442 RtspCallback, (void*)p_media );
443 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_DESCRIBE,
444 RtspCallback, (void*)p_media );
445 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PLAY,
446 RtspCallback, (void*)p_media );
447 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PAUSE,
448 RtspCallback, (void*)p_media );
449 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_GETPARAMETER,
450 RtspCallback, (void*)p_media );
451 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_TEARDOWN,
452 RtspCallback, (void*)p_media );
454 p_media->p_vod = p_vod;
456 vlc_mutex_lock( &p_sys->lock_media );
457 TAB_APPEND( p_sys->i_media, p_sys->media, p_media );
458 vlc_mutex_unlock( &p_sys->lock_media );
460 vlc_mutex_init( &p_media->lock );
461 p_media->psz_session_name = strdup("");
462 p_media->psz_session_description = strdup("");
463 p_media->psz_session_url = strdup("");
464 p_media->psz_session_email = strdup("");
466 p_media->i_port_audio = 1234;
467 p_media->i_port_video = 1236;
468 p_media->i_port = 1238;
469 p_media->i_payload_type = 96;
471 p_media->i_sdp_id = mdate();
472 p_media->i_sdp_version = 1;
473 p_media->i_length = input_item_GetDuration( p_item );
475 vlc_mutex_lock( &p_item->lock );
476 msg_Dbg( p_vod, "media has %i declared ES", p_item->i_es );
477 for( i = 0; i < p_item->i_es; i++ )
479 MediaAddES( p_vod, p_media, p_item->es[i] );
481 vlc_mutex_unlock( &p_item->lock );
486 static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
488 vod_sys_t *p_sys = p_vod->p_sys;
490 msg_Dbg( p_vod, "deleting media: %s", p_media->psz_rtsp_path );
492 vlc_mutex_lock( &p_sys->lock_media );
493 TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
494 vlc_mutex_unlock( &p_sys->lock_media );
496 httpd_UrlDelete( p_media->p_rtsp_url );
498 while( p_media->i_rtsp > 0 )
499 RtspClientDel( p_media, p_media->rtsp[0] );
500 TAB_CLEAN( p_media->i_rtsp, p_media->rtsp );
502 free( p_media->psz_rtsp_path );
503 free( p_media->psz_rtsp_control_v6 );
504 free( p_media->psz_rtsp_control_v4 );
506 while( p_media->i_es )
507 MediaDelES( p_vod, p_media, &p_media->es[0]->fmt );
508 TAB_CLEAN( p_media->i_es, p_media->es );
510 vlc_mutex_destroy( &p_media->lock );
512 free( p_media->psz_session_name );
513 free( p_media->psz_session_description );
514 free( p_media->psz_session_url );
515 free( p_media->psz_session_email );
516 free( p_media->psz_mux );
520 static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
523 media_es_t *p_es = calloc( 1, sizeof(media_es_t) );
527 free( p_media->psz_mux );
528 p_media->psz_mux = NULL;
530 /* TODO: update SDP, etc... */
531 if( asprintf( &psz_urlc, "%s/trackID=%d",
532 p_media->psz_rtsp_path, p_media->i_es ) < 0 )
537 msg_Dbg( p_vod, " - ES %4.4s (%s)", (char *)&p_fmt->i_codec, psz_urlc );
539 switch( p_fmt->i_codec )
542 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
544 p_es->i_payload_type = 11;
546 else if( p_fmt->audio.i_channels == 2 &&
547 p_fmt->audio.i_rate == 44100 )
549 p_es->i_payload_type = 10;
553 p_es->i_payload_type = p_media->i_payload_type++;
555 if( asprintf( &p_es->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
556 p_fmt->audio.i_channels ) == -1 )
557 p_es->psz_rtpmap = NULL;
560 p_es->i_payload_type = p_media->i_payload_type++;
561 if( asprintf( &p_es->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
562 p_fmt->audio.i_channels ) == -1 )
563 p_es->psz_rtpmap = NULL;
566 p_es->i_payload_type = 14;
567 p_es->psz_rtpmap = strdup( "MPA/90000" );
570 p_es->i_payload_type = 32;
571 p_es->psz_rtpmap = strdup( "MPV/90000" );
574 p_es->i_payload_type = p_media->i_payload_type++;
575 if( asprintf( &p_es->psz_rtpmap, "ac3/%d", p_fmt->audio.i_rate ) == -1 )
576 p_es->psz_rtpmap = NULL;
579 p_es->i_payload_type = p_media->i_payload_type++;
580 p_es->psz_rtpmap = strdup( "H263-1998/90000" );
583 p_es->i_payload_type = p_media->i_payload_type++;
584 p_es->psz_rtpmap = strdup( "H264/90000" );
585 p_es->psz_fmtp = NULL;
586 /* FIXME AAAAAAAAAAAARRRRRRRRGGGG copied from stream_out/rtp.c */
587 if( p_fmt->i_extra > 0 )
589 uint8_t *p_buffer = p_fmt->p_extra;
590 int i_buffer = p_fmt->i_extra;
591 char *p_64_sps = NULL;
592 char *p_64_pps = NULL;
595 while( i_buffer > 4 &&
596 p_buffer[0] == 0 && p_buffer[1] == 0 &&
597 p_buffer[2] == 0 && p_buffer[3] == 1 )
599 const int i_nal_type = p_buffer[4]&0x1f;
604 for( i_offset = 4; i_offset+3 < i_buffer ; i_offset++)
606 if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 0 && p_buffer[i_offset+3] == 1 )
608 /* we found another startcode */
613 if( i_nal_type == 7 )
616 p_64_sps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
617 sprintf_hexa( hexa, &p_buffer[5], 3 );
619 else if( i_nal_type == 8 )
622 p_64_pps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
628 if( p_64_sps && p_64_pps )
630 if( asprintf( &p_es->psz_fmtp,
631 "packetization-mode=1;profile-level-id=%s;"
632 "sprop-parameter-sets=%s,%s;", hexa, p_64_sps,
645 if( !p_es->psz_fmtp )
646 p_es->psz_fmtp = strdup( "packetization-mode=1" );
649 p_es->i_payload_type = p_media->i_payload_type++;
650 p_es->psz_rtpmap = strdup( "MP4V-ES/90000" );
651 if( p_fmt->i_extra > 0 )
653 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
654 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
655 if( asprintf( &p_es->psz_fmtp,
656 "profile-level-id=3; config=%s;", p_hexa ) == -1 )
657 p_es->psz_fmtp = NULL;
662 p_es->i_payload_type = p_media->i_payload_type++;
663 if( asprintf( &p_es->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate ) == -1 )
664 p_es->psz_rtpmap = NULL;
665 if( p_fmt->i_extra > 0 )
667 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
668 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
669 if( asprintf( &p_es->psz_fmtp,
670 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
671 "config=%s; SizeLength=13;IndexLength=3; "
672 "IndexDeltaLength=3; Profile=1;", p_hexa ) == -1 )
673 p_es->psz_fmtp = NULL;
677 case VLC_FOURCC( 'm', 'p', '2', 't' ):
678 p_media->psz_mux = strdup("ts");
679 p_es->i_payload_type = 33;
680 p_es->psz_rtpmap = strdup( "MP2T/90000" );
682 case VLC_FOURCC( 'm', 'p', '2', 'p' ):
683 p_media->psz_mux = strdup("ps");
684 p_es->i_payload_type = p_media->i_payload_type++;
685 p_es->psz_rtpmap = strdup( "MP2P/90000" );
687 case VLC_CODEC_AMR_NB:
688 p_es->i_payload_type = p_media->i_payload_type++;
689 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
690 "AMR/8000/2" : "AMR/8000" );
691 p_es->psz_fmtp = strdup( "octet-align=1" );
693 case VLC_CODEC_AMR_WB:
694 p_es->i_payload_type = p_media->i_payload_type++;
695 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
696 "AMR-WB/16000/2" : "AMR-WB/16000" );
697 p_es->psz_fmtp = strdup( "octet-align=1" );
701 msg_Err( p_vod, "cannot add this stream (unsupported "
702 "codec: %4.4s)", (char*)&p_fmt->i_codec );
709 httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, NULL, NULL,
712 if( !p_es->p_rtsp_url )
714 msg_Err( p_vod, "cannot create RTSP url (%s)", psz_urlc );
721 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
722 RtspCallbackES, (void*)p_es );
723 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_TEARDOWN,
724 RtspCallbackES, (void*)p_es );
725 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PLAY,
726 RtspCallbackES, (void*)p_es );
727 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PAUSE,
728 RtspCallbackES, (void*)p_es );
730 es_format_Copy( &p_es->fmt, p_fmt );
732 p_es->p_media = p_media;
735 /* Choose the port */
736 if( p_fmt->i_cat == AUDIO_ES && p_media->i_port_audio > 0 )
738 p_es->i_port = p_media->i_port_audio;
739 p_media->i_port_audio = 0;
741 else if( p_fmt->i_cat == VIDEO_ES && p_media->i_port_video > 0 )
743 p_es->i_port = p_media->i_port_video;
744 p_media->i_port_video = 0;
746 while( !p_es->i_port )
748 if( p_media->i_port != p_media->i_port_audio &&
749 p_media->i_port != p_media->i_port_video )
751 p_es->i_port = p_media->i_port;
752 p_media->i_port += 2;
755 p_media->i_port += 2;
762 vlc_mutex_lock( &p_media->lock );
763 TAB_APPEND( p_media->i_es, p_media->es, p_es );
764 vlc_mutex_unlock( &p_media->lock );
766 p_media->i_sdp_version++;
771 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
773 media_es_t *p_es = NULL;
777 for( i = 0; i < p_media->i_es; i++ )
779 if( p_media->es[i]->fmt.i_cat == p_fmt->i_cat &&
780 p_media->es[i]->fmt.i_codec == p_fmt->i_codec &&
781 p_media->es[i]->fmt.i_id == p_fmt->i_id )
783 p_es = p_media->es[i];
788 msg_Dbg( p_vod, " - Removing ES %4.4s", (char *)&p_fmt->i_codec );
790 vlc_mutex_lock( &p_media->lock );
791 TAB_REMOVE( p_media->i_es, p_media->es, p_es );
792 vlc_mutex_unlock( &p_media->lock );
794 free( p_es->psz_rtpmap );
795 free( p_es->psz_fmtp );
796 p_media->i_sdp_version++;
798 if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
799 es_format_Clean( &p_es->fmt );
803 static void CommandPush( vod_t *p_vod, rtsp_cmd_type_t i_type, vod_media_t *p_media, const char *psz_session,
804 double f_arg, const char *psz_arg )
809 memset( &cmd, 0, sizeof(cmd) );
812 cmd.i_media_id = p_media->id;
814 cmd.psz_session = strdup(psz_session);
817 cmd.psz_arg = strdup(psz_arg);
819 p_cmd = block_New( p_vod, sizeof(rtsp_cmd_t) );
820 memcpy( p_cmd->p_buffer, &cmd, sizeof(cmd) );
822 block_FifoPut( p_vod->p_sys->p_fifo_cmd, p_cmd );
825 static void* CommandThread( vlc_object_t *p_this )
827 vod_t *p_vod = (vod_t*)p_this;
828 vod_sys_t *p_sys = p_vod->p_sys;
829 int canc = vlc_savecancel ();
831 while( vlc_object_alive (p_vod) )
833 block_t *p_block_cmd = block_FifoGet( p_sys->p_fifo_cmd );
835 vod_media_t *p_media = NULL;
841 memcpy( &cmd, p_block_cmd->p_buffer, sizeof(cmd) );
842 block_Release( p_block_cmd );
844 if( cmd.i_type == RTSP_CMD_TYPE_NONE )
848 vlc_mutex_lock( &p_sys->lock_media );
849 for( i = 0; i < p_sys->i_media; i++ )
851 if( p_sys->media[i]->id == cmd.i_media_id )
854 if( i >= p_sys->i_media )
856 p_media = p_sys->media[i];
860 case RTSP_CMD_TYPE_PLAY:
861 vod_MediaControl( p_vod, p_media, cmd.psz_session,
862 VOD_MEDIA_PLAY, cmd.psz_arg );
864 case RTSP_CMD_TYPE_PAUSE:
865 vod_MediaControl( p_vod, p_media, cmd.psz_session,
869 case RTSP_CMD_TYPE_STOP:
870 vod_MediaControl( p_vod, p_media, cmd.psz_session, VOD_MEDIA_STOP );
873 case RTSP_CMD_TYPE_SEEK:
874 vod_MediaControl( p_vod, p_media, cmd.psz_session,
875 VOD_MEDIA_SEEK, cmd.f_arg );
878 case RTSP_CMD_TYPE_REWIND:
879 vod_MediaControl( p_vod, p_media, cmd.psz_session,
880 VOD_MEDIA_REWIND, cmd.f_arg );
883 case RTSP_CMD_TYPE_FORWARD:
884 vod_MediaControl( p_vod, p_media, cmd.psz_session,
885 VOD_MEDIA_FORWARD, cmd.f_arg );
893 vlc_mutex_unlock( &p_sys->lock_media );
894 free( cmd.psz_session );
898 vlc_restorecancel (canc);
902 /****************************************************************************
903 * RTSP server implementation
904 ****************************************************************************/
905 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
907 rtsp_client_t *p_rtsp = calloc( 1, sizeof(rtsp_client_t) );
913 p_rtsp->psz_session = psz_session;
914 TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
916 p_media->p_vod->p_sys->i_connections++;
917 msg_Dbg( p_media->p_vod, "new session: %s, connections: %d",
918 psz_session, p_media->p_vod->p_sys->i_throttle_users );
923 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, const char *psz_session )
927 for( i = 0; psz_session && i < p_media->i_rtsp; i++ )
929 if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
930 return p_media->rtsp[i];
936 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
938 p_media->p_vod->p_sys->i_connections--;
939 msg_Dbg( p_media->p_vod, "closing session: %s, connections: %d",
940 p_rtsp->psz_session, p_media->p_vod->p_sys->i_throttle_users );
942 while( p_rtsp->i_es )
945 free( p_rtsp->es[p_rtsp->i_es]->psz_ip );
946 free( p_rtsp->es[p_rtsp->i_es] );
950 TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, p_rtsp );
952 free( p_rtsp->psz_session );
957 static float ParseNPT (const char *str)
959 locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
960 locale_t oldloc = uselocale (loc);
964 if (sscanf (str, "%u:%u:%f", &hour, &min, &sec) == 3)
965 sec += ((hour * 60) + min) * 60;
967 if (sscanf (str, "%f", &sec) != 1)
970 if (loc != (locale_t)0)
979 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
980 httpd_message_t *answer, const httpd_message_t *query )
982 vod_media_t *p_media = (vod_media_t*)p_args;
983 vod_t *p_vod = p_media->p_vod;
984 const char *psz_transport = NULL;
985 const char *psz_playnow = NULL; /* support option: x-playNow */
986 const char *psz_session = NULL;
987 const char *psz_cseq = NULL;
988 rtsp_client_t *p_rtsp;
992 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
994 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
996 answer->i_proto = HTTPD_PROTO_RTSP;
997 answer->i_version = query->i_version;
998 answer->i_type = HTTPD_MSG_ANSWER;
1000 answer->p_body = NULL;
1002 switch( query->i_type )
1004 case HTTPD_MSG_SETUP:
1006 psz_playnow = httpd_MsgGet( query, "x-playNow" );
1007 psz_transport = httpd_MsgGet( query, "Transport" );
1008 if( psz_transport == NULL )
1010 answer->i_status = 400;
1013 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
1015 if( strstr( psz_transport, "unicast" ) &&
1016 strstr( psz_transport, "client_port=" ) )
1018 rtsp_client_t *p_rtsp = NULL;
1019 char ip[NI_MAXNUMERICHOST];
1020 i_port = atoi( strstr( psz_transport, "client_port=" ) +
1021 strlen("client_port=") );
1023 if( strstr( psz_transport, "MP2T/H2221/UDP" ) ||
1024 strstr( psz_transport, "RAW/RAW/UDP" ) )
1026 free( p_media->psz_mux );
1027 p_media->psz_mux = NULL;
1028 p_media->psz_mux = strdup( p_vod->p_sys->psz_raw_mux );
1029 p_media->b_raw = true;
1032 if( httpd_ClientIP( cl, ip ) == NULL )
1034 answer->i_status = 500;
1036 answer->p_body = NULL;
1040 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
1043 psz_session = httpd_MsgGet( query, "Session" );
1044 if( !psz_session || !*psz_session )
1047 if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
1048 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
1050 answer->i_status = 503;
1052 answer->p_body = NULL;
1055 if( asprintf( &psz_new, "%d", rand() ) < 0 )
1057 psz_session = psz_new;
1059 p_rtsp = RtspClientNew( p_media, psz_new );
1062 answer->i_status = 454;
1064 answer->p_body = NULL;
1070 p_rtsp = RtspClientGet( p_media, psz_session );
1073 answer->i_status = 454;
1075 answer->p_body = NULL;
1080 answer->i_status = 200;
1082 answer->p_body = NULL;
1084 if( p_media->b_raw )
1086 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
1088 httpd_MsgAdd( answer, "Transport",
1089 "MP2T/H2221/UDP;unicast;client_port=%d-%d",
1090 i_port, i_port + 1 );
1092 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1094 httpd_MsgAdd( answer, "Transport",
1095 "RAW/RAW/UDP;unicast;client_port=%d-%d",
1096 i_port, i_port + 1 );
1100 httpd_MsgAdd( answer, "Transport",
1101 "RTP/AVP/UDP;unicast;client_port=%d-%d",
1102 i_port, i_port + 1 );
1104 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1106 answer->i_status = 461;
1108 answer->p_body = NULL;
1111 /* Intentional fall-through on x-playNow option in RTSP request */
1116 case HTTPD_MSG_PLAY:
1118 char *psz_output, ip[NI_MAXNUMERICHOST];
1119 int i, i_port_audio = 0, i_port_video = 0;
1121 /* for now only multicast so easy */
1124 answer->i_status = 200;
1126 answer->p_body = NULL;
1130 psz_session = httpd_MsgGet( query, "Session" );
1131 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1133 p_rtsp = RtspClientGet( p_media, psz_session );
1136 answer->i_status = 500;
1138 answer->p_body = NULL;
1142 if( p_rtsp->b_playing )
1144 const char *psz_position = httpd_MsgGet( query, "Range" );
1145 const char *psz_scale = httpd_MsgGet( query, "Scale" );
1147 psz_position = strstr( psz_position, "npt=" );
1148 if( psz_position && !psz_scale )
1150 double f_pos = ParseNPT (psz_position + 4);
1151 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1152 f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
1153 CommandPush( p_vod, RTSP_CMD_TYPE_SEEK, p_media,
1154 psz_session, f_pos, NULL );
1159 double f_scale = 0.0;
1162 f_scale = us_strtod( psz_scale, &end );
1163 if( end > psz_scale )
1165 f_scale = (f_scale * 30.0);
1166 if( psz_scale[0] == '-' ) /* rewind */
1168 msg_Dbg( p_vod, "rewind request: %s", psz_scale );
1169 CommandPush( p_vod, RTSP_CMD_TYPE_REWIND, p_media,
1170 psz_session, f_scale, NULL );
1172 else if(psz_scale[0] != '1' ) /* fast-forward */
1174 msg_Dbg( p_vod, "fastforward request: %s",
1176 CommandPush( p_vod, RTSP_CMD_TYPE_FORWARD, p_media,
1177 psz_session, f_scale, NULL );
1180 if( p_rtsp->b_paused == true )
1182 p_rtsp->b_paused = false;
1183 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media,
1184 psz_session, 0, NULL );
1191 if( p_rtsp->b_playing && p_rtsp->b_paused )
1193 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media,
1194 psz_session, 0, NULL );
1195 p_rtsp->b_paused = false;
1198 else if( p_rtsp->b_playing ) break;
1200 if( httpd_ClientIP( cl, ip ) == NULL ) break;
1202 p_rtsp->b_playing = true;
1204 /* FIXME for != 1 video and 1 audio */
1205 for( i = 0; i < p_rtsp->i_es; i++ )
1207 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == AUDIO_ES )
1208 i_port_audio = p_rtsp->es[i]->i_port;
1209 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == VIDEO_ES )
1210 i_port_video = p_rtsp->es[i]->i_port;
1213 if( p_media->psz_mux )
1215 if( p_media->b_raw )
1217 if( asprintf( &psz_output,
1218 "std{access=udp,dst=%s:%i,mux=%s}",
1219 ip, i_port, p_media->psz_mux ) < 0 )
1224 if( asprintf( &psz_output,
1225 "rtp{dst=%s,port=%i,mux=%s}",
1226 ip, i_port_video, p_media->psz_mux ) < 0 )
1232 if( asprintf( &psz_output,
1233 "rtp{dst=%s,port-video=%i,port-audio=%i}",
1234 ip, i_port_video, i_port_audio ) < 0 )
1238 CommandPush( p_vod, RTSP_CMD_TYPE_PLAY, p_media, psz_session,
1244 case HTTPD_MSG_DESCRIBE:
1247 SDPGenerate( p_media, cl );
1249 if( psz_sdp != NULL )
1251 answer->i_status = 200;
1252 httpd_MsgAdd( answer, "Content-type", "%s",
1253 "application/sdp" );
1255 answer->p_body = (uint8_t *)psz_sdp;
1256 answer->i_body = strlen( psz_sdp );
1260 answer->i_status = 500;
1261 answer->p_body = NULL;
1267 case HTTPD_MSG_PAUSE:
1268 psz_session = httpd_MsgGet( query, "Session" );
1269 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1271 p_rtsp = RtspClientGet( p_media, psz_session );
1272 if( !p_rtsp ) break;
1274 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media, psz_session,
1276 p_rtsp->b_paused = true;
1278 answer->i_status = 200;
1280 answer->p_body = NULL;
1283 case HTTPD_MSG_TEARDOWN:
1284 /* for now only multicast so easy again */
1285 answer->i_status = 200;
1287 answer->p_body = NULL;
1289 psz_session = httpd_MsgGet( query, "Session" );
1290 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1292 p_rtsp = RtspClientGet( p_media, psz_session );
1293 if( !p_rtsp ) break;
1295 CommandPush( p_vod, RTSP_CMD_TYPE_STOP, p_media, psz_session,
1297 RtspClientDel( p_media, p_rtsp );
1300 case HTTPD_MSG_GETPARAMETER:
1301 answer->i_status = 200;
1303 answer->p_body = NULL;
1307 return VLC_EGENERIC;
1310 httpd_MsgAdd( answer, "Server", "VLC Server" );
1311 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1312 psz_cseq = httpd_MsgGet( query, "Cseq" );
1313 psz_cseq ? i_cseq = atoi( psz_cseq ) : 0;
1314 httpd_MsgAdd( answer, "CSeq", "%d", i_cseq );
1315 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1319 if( p_media->p_vod->p_sys->i_session_timeout >= 0 )
1320 httpd_MsgAdd( answer, "Session", "%s;timeout=%i", psz_session,
1321 p_media->p_vod->p_sys->i_session_timeout );
1323 httpd_MsgAdd( answer, "Session", "%s", psz_session );
1329 static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
1330 httpd_message_t *answer,
1331 const httpd_message_t *query )
1333 media_es_t *p_es = (media_es_t*)p_args;
1334 vod_media_t *p_media = p_es->p_media;
1335 vod_t *p_vod = p_media->p_vod;
1336 rtsp_client_t *p_rtsp = NULL;
1337 const char *psz_transport = NULL;
1338 const char *psz_playnow = NULL; /* support option: x-playNow */
1339 const char *psz_session = NULL;
1340 const char *psz_position = NULL;
1341 const char *psz_cseq = NULL;
1345 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
1347 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
1349 answer->i_proto = HTTPD_PROTO_RTSP;
1350 answer->i_version = query->i_version;
1351 answer->i_type = HTTPD_MSG_ANSWER;
1353 answer->p_body = NULL;
1355 switch( query->i_type )
1357 case HTTPD_MSG_SETUP:
1358 psz_playnow = httpd_MsgGet( query, "x-playNow" );
1359 psz_transport = httpd_MsgGet( query, "Transport" );
1361 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
1363 if( strstr( psz_transport, "unicast" ) &&
1364 strstr( psz_transport, "client_port=" ) )
1366 rtsp_client_t *p_rtsp = NULL;
1367 rtsp_client_es_t *p_rtsp_es = NULL;
1368 char ip[NI_MAXNUMERICHOST];
1369 int i_port = atoi( strstr( psz_transport, "client_port=" ) +
1370 strlen("client_port=") );
1372 if( httpd_ClientIP( cl, ip ) == NULL )
1374 answer->i_status = 500;
1376 answer->p_body = NULL;
1380 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
1383 psz_session = httpd_MsgGet( query, "Session" );
1384 if( !psz_session || !*psz_session )
1387 if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
1388 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
1390 answer->i_status = 503;
1392 answer->p_body = NULL;
1395 if( asprintf( &psz_new, "%d", rand() ) < 0 )
1397 psz_session = psz_new;
1399 p_rtsp = RtspClientNew( p_media, psz_new );
1403 p_rtsp = RtspClientGet( p_media, psz_session );
1406 answer->i_status = 454;
1408 answer->p_body = NULL;
1413 p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
1416 answer->i_status = 500;
1418 answer->p_body = NULL;
1421 p_rtsp_es->i_port = i_port;
1422 p_rtsp_es->psz_ip = strdup( ip );
1423 p_rtsp_es->p_media_es = p_es;
1424 TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );
1426 answer->i_status = 200;
1428 answer->p_body = NULL;
1430 if( p_media->b_raw )
1432 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
1434 httpd_MsgAdd( answer, "Transport",
1435 "MP2T/H2221/UDP;client_port=%d-%d",
1436 p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1438 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1440 httpd_MsgAdd( answer, "Transport",
1441 "RAW/RAW/UDP;client_port=%d-%d",
1442 p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1447 httpd_MsgAdd( answer, "Transport",
1448 "RTP/AVP/UDP;client_port=%d-%d",
1449 p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1452 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1454 answer->i_status = 461;
1456 answer->p_body = NULL;
1459 /* Intentional fall-through on x-playNow option in RTSP request */
1463 case HTTPD_MSG_PLAY:
1464 /* This is kind of a kludge. Should we only support Aggregate
1466 psz_session = httpd_MsgGet( query, "Session" );
1467 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1469 p_rtsp = RtspClientGet( p_media, psz_session );
1471 psz_position = httpd_MsgGet( query, "Range" );
1472 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
1475 double f_pos = ParseNPT (psz_position + 4);
1476 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1477 f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
1478 CommandPush( p_vod, RTSP_CMD_TYPE_SEEK, p_media,
1479 psz_session, f_pos, NULL );
1484 answer->i_status = 200;
1486 answer->p_body = NULL;
1490 case HTTPD_MSG_TEARDOWN:
1491 answer->i_status = 200;
1493 answer->p_body = NULL;
1495 psz_session = httpd_MsgGet( query, "Session" );
1496 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1498 p_rtsp = RtspClientGet( p_media, psz_session );
1499 if( !p_rtsp ) break;
1501 for( i = 0; i < p_rtsp->i_es; i++ )
1503 if( p_rtsp->es[i]->p_media_es == p_es )
1505 free( p_rtsp->es[i]->psz_ip );
1506 TAB_REMOVE( p_rtsp->i_es, p_rtsp->es, p_rtsp->es[i] );
1513 CommandPush( p_vod, RTSP_CMD_TYPE_STOP, p_media, psz_session,
1515 RtspClientDel( p_media, p_rtsp );
1519 case HTTPD_MSG_PAUSE:
1520 /* This is kind of a kludge. Should we only support Aggregate
1522 psz_session = httpd_MsgGet( query, "Session" );
1523 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1525 p_rtsp = RtspClientGet( p_media, psz_session );
1526 if( !p_rtsp ) break;
1528 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media, psz_session,
1530 p_rtsp->b_paused = true;
1532 answer->i_status = 200;
1534 answer->p_body = NULL;
1538 return VLC_EGENERIC;
1542 httpd_MsgAdd( answer, "Server", "VLC Server" );
1543 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1544 psz_cseq = httpd_MsgGet( query, "Cseq" );
1546 i_cseq = atoi( psz_cseq );
1549 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1550 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1553 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1558 /*****************************************************************************
1560 * FIXME: need to be moved to a common place ?
1561 *****************************************************************************/
1562 static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
1565 char *p, *psz_sdp, ip[NI_MAXNUMERICHOST], ipv;
1566 const char *psz_control;
1568 if( httpd_ServerIP( cl, ip ) == NULL )
1571 p = strchr( ip, '%' );
1573 *p = '\0'; /* remove scope if present */
1575 ipv = ( strchr( ip, ':' ) != NULL ) ? '6' : '4';
1577 /* Calculate size */
1578 i_size = sizeof( "v=0\r\n" ) +
1579 sizeof( "o=- * * IN IP4 \r\n" ) + 10 + NI_MAXNUMERICHOST +
1580 sizeof( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
1581 sizeof( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
1582 sizeof( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
1583 sizeof( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
1584 sizeof( "c=IN IP4 0.0.0.0\r\n" ) + 20 + 10 +
1585 sizeof( "t=0 0\r\n" ) + /* FIXME */
1586 sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
1587 sizeof( "a=range:npt=0-1000000000.000\r\n" );
1589 psz_control = (ipv == '6') ? p_media->psz_rtsp_control_v6
1590 : p_media->psz_rtsp_control_v4;
1591 for( i = 0; i < p_media->i_es; i++ )
1593 media_es_t *p_es = p_media->es[i];
1595 i_size += sizeof( "m=**d*o * RTP/AVP *\r\n" ) + 19;
1596 if( p_es->psz_rtpmap )
1598 i_size += sizeof( "a=rtpmap:* *\r\n" ) +
1599 strlen( p_es->psz_rtpmap ) + 9;
1601 if( p_es->psz_fmtp )
1603 i_size += sizeof( "a=fmtp:* *\r\n" ) +
1604 strlen( p_es->psz_fmtp ) + 9;
1607 i_size += (strlen( psz_control ) + strlen( ip ) + 9) * p_media->i_es;
1609 p = psz_sdp = malloc( i_size );
1610 p += sprintf( p, "v=0\r\n" );
1611 p += sprintf( p, "o=- %"PRId64" %d IN IP%c %s\r\n",
1612 p_media->i_sdp_id, p_media->i_sdp_version, ipv, ip );
1613 if( *p_media->psz_session_name )
1614 p += sprintf( p, "s=%s\r\n", p_media->psz_session_name );
1615 if( *p_media->psz_session_description )
1616 p += sprintf( p, "i=%s\r\n", p_media->psz_session_description );
1617 if( *p_media->psz_session_url )
1618 p += sprintf( p, "u=%s\r\n", p_media->psz_session_url );
1619 if( *p_media->psz_session_email )
1620 p += sprintf( p, "e=%s\r\n", p_media->psz_session_email );
1622 p += sprintf( p, "c=IN IP%c %s\r\n", ipv, ipv == '6' ? "::" : "0.0.0.0" );
1623 p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
1624 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
1626 if( p_media->i_length > 0 )
1628 lldiv_t d = lldiv( p_media->i_length / 1000, 1000 );
1629 p += sprintf( p, "a=range:npt=0-%lld.%03u\r\n", d.quot,
1633 for( i = 0; i < p_media->i_es; i++ )
1635 media_es_t *p_es = p_media->es[i];
1637 if( p_es->fmt.i_cat == AUDIO_ES )
1639 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
1640 p_es->i_port, p_es->i_payload_type );
1642 else if( p_es->fmt.i_cat == VIDEO_ES )
1644 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
1645 p_es->i_port, p_es->i_payload_type );
1652 if( p_es->psz_rtpmap )
1654 p += sprintf( p, "a=rtpmap:%d %s\r\n", p_es->i_payload_type,
1657 if( p_es->psz_fmtp )
1659 p += sprintf( p, "a=fmtp:%d %s\r\n", p_es->i_payload_type,
1663 p += sprintf( p, psz_control, ip, i );