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>
48 /*****************************************************************************
50 *****************************************************************************/
51 static int Open ( vlc_object_t * );
52 static void Close( vlc_object_t * );
54 #define HOST_TEXT N_( "RTSP host address" )
55 #define HOST_LONGTEXT N_( \
56 "This defines the address, port and path the RTSP VOD server will listen " \
57 "on.\nSyntax is address:port/path. The default is to listen on all "\
58 "interfaces (address 0.0.0.0), on port 554, with no path.\nTo listen " \
59 "only on the local interface, use \"localhost\" as address." )
61 #define THROTLE_TEXT N_( "Maximum number of connections" )
62 #define THROTLE_LONGTEXT N_( "This limits the maximum number of clients " \
63 "that can connect to the RTSP VOD. 0 means no limit." )
65 #define RAWMUX_TEXT N_( "MUX for RAW RTSP transport" )
67 #define SESSION_TIMEOUT_TEXT N_( "Sets the timeout option in the RTSP " \
69 #define SESSION_TIMEOUT_LONGTEXT N_( "Defines what timeout option to add " \
70 "to the RTSP session ID string. Setting it to a negative number removes " \
71 "the timeout option entirely. This is needed by some IPTV STBs (such as " \
72 "those made by HansunTech) which get confused by it. The default is 5." )
75 set_shortname( N_("RTSP VoD" ) );
76 set_description( N_("RTSP VoD server") );
77 set_category( CAT_SOUT );
78 set_subcategory( SUBCAT_SOUT_VOD );
79 set_capability( "vod server", 1 );
80 set_callbacks( Open, Close );
81 add_shortcut( "rtsp" );
82 add_string ( "rtsp-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, true );
83 add_string( "rtsp-raw-mux", "ts", NULL, RAWMUX_TEXT,
85 add_integer( "rtsp-throttle-users", 0, NULL, THROTLE_TEXT,
86 THROTLE_LONGTEXT, true );
87 add_integer( "rtsp-session-timeout", 5, NULL, SESSION_TIMEOUT_TEXT,
88 SESSION_TIMEOUT_LONGTEXT, true );
91 /*****************************************************************************
93 *****************************************************************************/
95 typedef struct media_es_t media_es_t;
99 media_es_t *p_media_es;
108 int64_t i_last; /* for timeout */
110 bool b_playing; /* is it in "play" state */
111 bool b_paused; /* is it in "pause" state */
114 rtsp_client_es_t **es;
124 httpd_url_t *p_rtsp_url;
126 vod_media_t *p_media;
130 uint8_t i_payload_type;
144 httpd_url_t *p_rtsp_url;
145 char *psz_rtsp_control_v4;
146 char *psz_rtsp_control_v6;
170 rtsp_client_t **rtsp;
173 char *psz_session_name;
174 char *psz_session_description;
175 char *psz_session_url;
176 char *psz_session_email;
183 httpd_host_t *p_rtsp_host;
186 int i_throttle_users;
191 int i_session_timeout;
194 vlc_mutex_t lock_media;
200 block_fifo_t *p_fifo_cmd;
203 /* rtsp delayed command (to avoid deadlock between vlm/httpd) */
206 RTSP_CMD_TYPE_NONE, /* Exit requested */
212 RTSP_CMD_TYPE_REWIND,
213 RTSP_CMD_TYPE_FORWARD,
216 static vod_media_t *MediaNew( vod_t *, const char *, input_item_t * );
217 static void MediaDel( vod_t *, vod_media_t * );
218 static int MediaAddES( vod_t *, vod_media_t *, es_format_t * );
219 static void MediaDelES( vod_t *, vod_media_t *, es_format_t * );
221 static void CommandThread( vlc_object_t *p_this );
222 static void CommandPush( vod_t *, rtsp_cmd_type_t, vod_media_t *, const char *psz_session,
223 double f_arg, const char *psz_arg );
225 static rtsp_client_t *RtspClientNew( vod_media_t *, char * );
226 static rtsp_client_t *RtspClientGet( vod_media_t *, const char * );
227 static void RtspClientDel( vod_media_t *, rtsp_client_t * );
229 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
230 httpd_message_t *, const httpd_message_t * );
231 static int RtspCallbackES( httpd_callback_sys_t *, httpd_client_t *,
232 httpd_message_t *, const httpd_message_t * );
234 static char *SDPGenerate( const vod_media_t *, httpd_client_t *cl );
236 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
238 static const char hex[16] = "0123456789abcdef";
241 for( i = 0; i < i_data; i++ )
243 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
244 s[2*i+1] = hex[(p_data[i] )&0xf];
249 /*****************************************************************************
250 * Open: Starts the RTSP server module
251 *****************************************************************************/
252 static int Open( vlc_object_t *p_this )
254 vod_t *p_vod = (vod_t *)p_this;
255 vod_sys_t *p_sys = 0;
259 psz_url = config_GetPsz( p_vod, "rtsp-host" );
260 vlc_UrlParse( &url, psz_url, 0 );
263 if( url.i_port <= 0 ) url.i_port = 554;
265 p_vod->p_sys = p_sys = malloc( sizeof( vod_sys_t ) );
266 if( !p_sys ) goto error;
267 p_sys->p_rtsp_host = 0;
269 p_sys->i_session_timeout = var_CreateGetInteger( p_this, "rtsp-session-timeout" );
271 p_sys->i_throttle_users = var_CreateGetInteger( p_this, "rtsp-throttle-users" );
272 msg_Dbg( p_this, "allowing up to %d connections", p_sys->i_throttle_users );
273 p_sys->i_connections = 0;
275 p_sys->psz_raw_mux = var_CreateGetString( p_this, "rtsp-raw-mux" );
278 httpd_HostNew( VLC_OBJECT(p_vod), url.psz_host, url.i_port );
279 if( !p_sys->p_rtsp_host )
281 msg_Err( p_vod, "cannot create RTSP server (%s:%i)",
282 url.psz_host, url.i_port );
286 p_sys->psz_path = strdup( url.psz_path ? url.psz_path : "/" );
287 p_sys->i_port = url.i_port;
289 vlc_UrlClean( &url );
291 vlc_mutex_init( &p_sys->lock_media );
293 TAB_INIT( p_sys->i_media, p_sys->media );
294 p_sys->i_media_id = 0;
296 p_vod->pf_media_new = MediaNew;
297 p_vod->pf_media_del = MediaDel;
298 p_vod->pf_media_add_es = MediaAddES;
299 p_vod->pf_media_del_es = MediaDelES;
301 p_sys->p_fifo_cmd = block_FifoNew();
302 if( vlc_thread_create( p_vod, "rtsp vod thread", CommandThread,
303 VLC_THREAD_PRIORITY_LOW, false ) )
305 msg_Err( p_vod, "cannot spawn rtsp vod thread" );
306 block_FifoRelease( p_sys->p_fifo_cmd );
307 free( p_sys->psz_path );
316 if( p_sys->p_rtsp_host ) httpd_HostDelete( p_sys->p_rtsp_host );
317 free( p_sys->psz_raw_mux );
320 vlc_UrlClean( &url );
325 /*****************************************************************************
327 *****************************************************************************/
328 static void Close( vlc_object_t * p_this )
330 vod_t *p_vod = (vod_t *)p_this;
331 vod_sys_t *p_sys = p_vod->p_sys;
333 /* Stop command thread */
334 vlc_object_kill( p_vod );
335 CommandPush( p_vod, RTSP_CMD_TYPE_NONE, NULL, NULL, 0.0, NULL );
336 vlc_thread_join( p_vod );
338 block_FifoRelease( p_sys->p_fifo_cmd );
340 httpd_HostDelete( p_sys->p_rtsp_host );
341 var_Destroy( p_this, "rtsp-session-timeout" );
342 var_Destroy( p_this, "rtsp-throttle-users" );
343 var_Destroy( p_this, "rtsp-raw-mux" );
345 /* Check VLM is not buggy */
346 if( p_sys->i_media > 0 )
347 msg_Err( p_vod, "rtsp vod leaking %d medias", p_sys->i_media );
348 TAB_CLEAN( p_sys->i_media, p_sys->media );
350 vlc_mutex_destroy( &p_sys->lock_media );
352 free( p_sys->psz_path );
353 free( p_sys->psz_raw_mux );
357 /*****************************************************************************
359 *****************************************************************************/
360 static vod_media_t *MediaNew( vod_t *p_vod, const char *psz_name,
361 input_item_t *p_item )
363 vod_sys_t *p_sys = p_vod->p_sys;
364 vod_media_t *p_media = malloc( sizeof(vod_media_t) );
370 memset( p_media, 0, sizeof(vod_media_t) );
371 p_media->id = p_sys->i_media_id++;
372 TAB_INIT( p_media->i_es, p_media->es );
373 p_media->psz_mux = 0;
374 TAB_INIT( p_media->i_rtsp, p_media->rtsp );
375 p_media->b_raw = false;
377 if( asprintf( &p_media->psz_rtsp_path, "%s%s",
378 p_sys->psz_path, psz_name ) <0 )
380 p_media->p_rtsp_url =
381 httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, NULL,
384 if( !p_media->p_rtsp_url )
386 msg_Err( p_vod, "cannot create RTSP url (%s)", p_media->psz_rtsp_path);
387 free( p_media->psz_rtsp_path );
392 msg_Dbg( p_vod, "created RTSP url: %s", p_media->psz_rtsp_path );
394 if( asprintf( &p_media->psz_rtsp_control_v4,
395 "a=control:rtsp://%%s:%d%s/trackID=%%d\r\n",
396 p_sys->i_port, p_media->psz_rtsp_path ) < 0 )
398 if( asprintf( &p_media->psz_rtsp_control_v6,
399 "a=control:rtsp://[%%s]:%d%s/trackID=%%d\r\n",
400 p_sys->i_port, p_media->psz_rtsp_path ) < 0 )
403 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_SETUP,
404 RtspCallback, (void*)p_media );
405 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_DESCRIBE,
406 RtspCallback, (void*)p_media );
407 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PLAY,
408 RtspCallback, (void*)p_media );
409 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PAUSE,
410 RtspCallback, (void*)p_media );
411 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_GETPARAMETER,
412 RtspCallback, (void*)p_media );
413 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_TEARDOWN,
414 RtspCallback, (void*)p_media );
416 p_media->p_vod = p_vod;
418 vlc_mutex_lock( &p_sys->lock_media );
419 TAB_APPEND( p_sys->i_media, p_sys->media, p_media );
420 vlc_mutex_unlock( &p_sys->lock_media );
422 vlc_mutex_init( &p_media->lock );
423 p_media->psz_session_name = strdup("");
424 p_media->psz_session_description = strdup("");
425 p_media->psz_session_url = strdup("");
426 p_media->psz_session_email = strdup("");
428 p_media->i_port_audio = 1234;
429 p_media->i_port_video = 1236;
430 p_media->i_port = 1238;
431 p_media->i_payload_type = 96;
433 p_media->i_sdp_id = mdate();
434 p_media->i_sdp_version = 1;
435 p_media->i_length = input_item_GetDuration( p_item );
437 vlc_mutex_lock( &p_item->lock );
438 msg_Dbg( p_vod, "media has %i declared ES", p_item->i_es );
439 for( i = 0; i < p_item->i_es; i++ )
441 MediaAddES( p_vod, p_media, p_item->es[i] );
443 vlc_mutex_unlock( &p_item->lock );
448 static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
450 vod_sys_t *p_sys = p_vod->p_sys;
452 msg_Dbg( p_vod, "deleting media: %s", p_media->psz_rtsp_path );
454 vlc_mutex_lock( &p_sys->lock_media );
455 TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
456 vlc_mutex_unlock( &p_sys->lock_media );
458 while( p_media->i_rtsp > 0 )
459 RtspClientDel( p_media, p_media->rtsp[0] );
460 TAB_CLEAN( p_media->i_rtsp, p_media->rtsp );
462 httpd_UrlDelete( p_media->p_rtsp_url );
463 free( p_media->psz_rtsp_path );
464 free( p_media->psz_rtsp_control_v6 );
465 free( p_media->psz_rtsp_control_v4 );
467 while( p_media->i_es )
468 MediaDelES( p_vod, p_media, &p_media->es[0]->fmt );
469 TAB_CLEAN( p_media->i_es, p_media->es );
471 vlc_mutex_destroy( &p_media->lock );
473 free( p_media->psz_session_name );
474 free( p_media->psz_session_description );
475 free( p_media->psz_session_url );
476 free( p_media->psz_session_email );
477 free( p_media->psz_mux );
481 static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
483 media_es_t *p_es = malloc( sizeof(media_es_t) );
486 if( !p_es ) return VLC_ENOMEM;
487 memset( p_es, 0, sizeof(media_es_t) );
489 free( p_media->psz_mux );
490 p_media->psz_mux = NULL;
492 /* TODO: update SDP, etc... */
493 if( asprintf( &psz_urlc, "%s/trackID=%d",
494 p_media->psz_rtsp_path, p_media->i_es ) < 0 )
496 msg_Dbg( p_vod, " - ES %4.4s (%s)", (char *)&p_fmt->i_codec, psz_urlc );
498 switch( p_fmt->i_codec )
500 case VLC_FOURCC( 's', '1', '6', 'b' ):
501 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
503 p_es->i_payload_type = 11;
505 else if( p_fmt->audio.i_channels == 2 &&
506 p_fmt->audio.i_rate == 44100 )
508 p_es->i_payload_type = 10;
512 p_es->i_payload_type = p_media->i_payload_type++;
514 p_es->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
515 sprintf( p_es->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
516 p_fmt->audio.i_channels );
518 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
519 p_es->i_payload_type = p_media->i_payload_type++;
520 p_es->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
521 sprintf( p_es->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
522 p_fmt->audio.i_channels );
524 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
525 case VLC_FOURCC( 'm', 'p', '3', ' ' ):
526 p_es->i_payload_type = 14;
527 p_es->psz_rtpmap = strdup( "MPA/90000" );
529 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
530 p_es->i_payload_type = 32;
531 p_es->psz_rtpmap = strdup( "MPV/90000" );
533 case VLC_FOURCC( 'a', '5', '2', ' ' ):
534 p_es->i_payload_type = p_media->i_payload_type++;
535 asprintf( &p_es->psz_rtpmap, "ac3/%d", p_fmt->audio.i_rate );
537 case VLC_FOURCC( 'H', '2', '6', '3' ):
538 p_es->i_payload_type = p_media->i_payload_type++;
539 p_es->psz_rtpmap = strdup( "H263-1998/90000" );
541 case VLC_FOURCC( 'h', '2', '6', '4' ):
542 p_es->i_payload_type = p_media->i_payload_type++;
543 p_es->psz_rtpmap = strdup( "H264/90000" );
544 p_es->psz_fmtp = NULL;
545 /* FIXME AAAAAAAAAAAARRRRRRRRGGGG copied from stream_out/rtp.c */
546 if( p_fmt->i_extra > 0 )
548 uint8_t *p_buffer = p_fmt->p_extra;
549 int i_buffer = p_fmt->i_extra;
550 char *p_64_sps = NULL;
551 char *p_64_pps = NULL;
554 while( i_buffer > 4 &&
555 p_buffer[0] == 0 && p_buffer[1] == 0 &&
556 p_buffer[2] == 0 && p_buffer[3] == 1 )
558 const int i_nal_type = p_buffer[4]&0x1f;
563 for( i_offset = 4; i_offset+3 < i_buffer ; i_offset++)
565 if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 0 && p_buffer[i_offset+3] == 1 )
567 /* we found another startcode */
572 if( i_nal_type == 7 )
574 p_64_sps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
575 sprintf_hexa( hexa, &p_buffer[5], 3 );
577 else if( i_nal_type == 8 )
579 p_64_pps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
585 if( p_64_sps && p_64_pps )
586 if( asprintf( &p_es->psz_fmtp,
587 "packetization-mode=1;profile-level-id=%s;"
588 "sprop-parameter-sets=%s,%s;", hexa, p_64_sps,
594 if( !p_es->psz_fmtp )
595 p_es->psz_fmtp = strdup( "packetization-mode=1" );
597 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
598 p_es->i_payload_type = p_media->i_payload_type++;
599 p_es->psz_rtpmap = strdup( "MP4V-ES/90000" );
600 if( p_fmt->i_extra > 0 )
602 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
603 p_es->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
604 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
605 sprintf( p_es->psz_fmtp,
606 "profile-level-id=3; config=%s;", p_hexa );
610 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
611 p_es->i_payload_type = p_media->i_payload_type++;
612 p_es->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
613 sprintf( p_es->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
614 if( p_fmt->i_extra > 0 )
616 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
617 p_es->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
618 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
619 sprintf( p_es->psz_fmtp,
620 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
621 "config=%s; SizeLength=13;IndexLength=3; "
622 "IndexDeltaLength=3; Profile=1;", p_hexa );
626 case VLC_FOURCC( 'm', 'p', '2', 't' ):
627 p_media->psz_mux = strdup("ts");
628 p_es->i_payload_type = 33;
629 p_es->psz_rtpmap = strdup( "MP2T/90000" );
631 case VLC_FOURCC( 'm', 'p', '2', 'p' ):
632 p_media->psz_mux = strdup("ps");
633 p_es->i_payload_type = p_media->i_payload_type++;
634 p_es->psz_rtpmap = strdup( "MP2P/90000" );
636 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
637 p_es->i_payload_type = p_media->i_payload_type++;
638 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
639 "AMR/8000/2" : "AMR/8000" );
640 p_es->psz_fmtp = strdup( "octet-align=1" );
642 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
643 p_es->i_payload_type = p_media->i_payload_type++;
644 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
645 "AMR-WB/16000/2" : "AMR-WB/16000" );
646 p_es->psz_fmtp = strdup( "octet-align=1" );
650 msg_Err( p_vod, "cannot add this stream (unsupported "
651 "codec: %4.4s)", (char*)&p_fmt->i_codec );
657 httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, NULL, NULL,
660 if( !p_es->p_rtsp_url )
662 msg_Err( p_vod, "cannot create RTSP url (%s)", psz_urlc );
669 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
670 RtspCallbackES, (void*)p_es );
671 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_TEARDOWN,
672 RtspCallbackES, (void*)p_es );
673 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PLAY,
674 RtspCallbackES, (void*)p_es );
675 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PAUSE,
676 RtspCallbackES, (void*)p_es );
678 es_format_Copy( &p_es->fmt, p_fmt );
680 p_es->p_media = p_media;
683 /* Choose the port */
684 if( p_fmt->i_cat == AUDIO_ES && p_media->i_port_audio > 0 )
686 p_es->i_port = p_media->i_port_audio;
687 p_media->i_port_audio = 0;
689 else if( p_fmt->i_cat == VIDEO_ES && p_media->i_port_video > 0 )
691 p_es->i_port = p_media->i_port_video;
692 p_media->i_port_video = 0;
694 while( !p_es->i_port )
696 if( p_media->i_port != p_media->i_port_audio &&
697 p_media->i_port != p_media->i_port_video )
699 p_es->i_port = p_media->i_port;
700 p_media->i_port += 2;
703 p_media->i_port += 2;
710 vlc_mutex_lock( &p_media->lock );
711 TAB_APPEND( p_media->i_es, p_media->es, p_es );
712 vlc_mutex_unlock( &p_media->lock );
714 p_media->i_sdp_version++;
719 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
721 media_es_t *p_es = NULL;
725 for( i = 0; i < p_media->i_es; i++ )
727 if( p_media->es[i]->fmt.i_cat == p_fmt->i_cat &&
728 p_media->es[i]->fmt.i_codec == p_fmt->i_codec &&
729 p_media->es[i]->fmt.i_id == p_fmt->i_id )
731 p_es = p_media->es[i];
736 msg_Dbg( p_vod, " - Removing ES %4.4s", (char *)&p_fmt->i_codec );
738 vlc_mutex_lock( &p_media->lock );
739 TAB_REMOVE( p_media->i_es, p_media->es, p_es );
740 vlc_mutex_unlock( &p_media->lock );
742 free( p_es->psz_rtpmap );
743 free( p_es->psz_fmtp );
744 p_media->i_sdp_version++;
746 if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
747 es_format_Clean( &p_es->fmt );
756 //vod_media_t *p_media;
762 static void CommandPush( vod_t *p_vod, rtsp_cmd_type_t i_type, vod_media_t *p_media, const char *psz_session,
763 double f_arg, const char *psz_arg )
768 memset( &cmd, 0, sizeof(cmd) );
771 cmd.i_media_id = p_media->id;
773 cmd.psz_session = strdup(psz_session);
776 cmd.psz_arg = strdup(psz_arg);
778 p_cmd = block_New( p_vod, sizeof(rtsp_cmd_t) );
779 memcpy( p_cmd->p_buffer, &cmd, sizeof(cmd) );
781 block_FifoPut( p_vod->p_sys->p_fifo_cmd, p_cmd );
784 static void CommandThread( vlc_object_t *p_this )
786 vod_t *p_vod = (vod_t*)p_this;
787 vod_sys_t *p_sys = p_vod->p_sys;
789 while( !p_vod->b_die )
791 block_t *p_block_cmd = block_FifoGet( p_sys->p_fifo_cmd );
793 vod_media_t *p_media = NULL;
799 memcpy( &cmd, p_block_cmd->p_buffer, sizeof(cmd) );
800 block_Release( p_block_cmd );
802 if( cmd.i_type == RTSP_CMD_TYPE_NONE )
806 vlc_mutex_lock( &p_sys->lock_media );
807 for( i = 0; i < p_sys->i_media; i++ )
809 if( p_sys->media[i]->id == cmd.i_media_id )
812 if( i >= p_sys->i_media )
814 p_media = p_sys->media[i];
818 case RTSP_CMD_TYPE_PLAY:
819 vod_MediaControl( p_vod, p_media, cmd.psz_session,
820 VOD_MEDIA_PLAY, cmd.psz_arg );
822 case RTSP_CMD_TYPE_PAUSE:
823 vod_MediaControl( p_vod, p_media, cmd.psz_session,
827 case RTSP_CMD_TYPE_STOP:
828 vod_MediaControl( p_vod, p_media, cmd.psz_session, VOD_MEDIA_STOP );
831 case RTSP_CMD_TYPE_SEEK:
832 vod_MediaControl( p_vod, p_media, cmd.psz_session,
833 VOD_MEDIA_SEEK, cmd.f_arg );
836 case RTSP_CMD_TYPE_REWIND:
837 vod_MediaControl( p_vod, p_media, cmd.psz_session,
838 VOD_MEDIA_REWIND, cmd.f_arg );
841 case RTSP_CMD_TYPE_FORWARD:
842 vod_MediaControl( p_vod, p_media, cmd.psz_session,
843 VOD_MEDIA_FORWARD, cmd.f_arg );
851 vlc_mutex_unlock( &p_sys->lock_media );
852 free( cmd.psz_session );
857 /****************************************************************************
858 * RTSP server implementation
859 ****************************************************************************/
860 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
862 rtsp_client_t *p_rtsp = malloc( sizeof(rtsp_client_t) );
864 if( !p_rtsp ) return NULL;
865 memset( p_rtsp, 0, sizeof(rtsp_client_t) );
868 p_rtsp->psz_session = psz_session;
869 TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
871 p_media->p_vod->p_sys->i_connections++;
872 msg_Dbg( p_media->p_vod, "new session: %s, connections: %d",
873 psz_session, p_media->p_vod->p_sys->i_throttle_users );
878 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, const char *psz_session )
882 for( i = 0; psz_session && i < p_media->i_rtsp; i++ )
884 if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
885 return p_media->rtsp[i];
891 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
893 p_media->p_vod->p_sys->i_connections--;
894 msg_Dbg( p_media->p_vod, "closing session: %s, connections: %d",
895 p_rtsp->psz_session, p_media->p_vod->p_sys->i_throttle_users );
897 while( p_rtsp->i_es-- )
899 free( p_rtsp->es[p_rtsp->i_es]->psz_ip );
900 free( p_rtsp->es[p_rtsp->i_es] );
901 if( !p_rtsp->i_es ) free( p_rtsp->es );
904 TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, p_rtsp );
906 free( p_rtsp->psz_session );
910 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
911 httpd_message_t *answer, const httpd_message_t *query )
913 vod_media_t *p_media = (vod_media_t*)p_args;
914 vod_t *p_vod = p_media->p_vod;
915 const char *psz_transport = NULL;
916 const char *psz_playnow = NULL; /* support option: x-playNow */
917 const char *psz_session = NULL;
918 const char *psz_cseq = NULL;
919 rtsp_client_t *p_rtsp;
923 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
925 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
927 answer->i_proto = HTTPD_PROTO_RTSP;
928 answer->i_version = query->i_version;
929 answer->i_type = HTTPD_MSG_ANSWER;
931 answer->p_body = NULL;
933 switch( query->i_type )
935 case HTTPD_MSG_SETUP:
937 psz_playnow = httpd_MsgGet( query, "x-playNow" );
938 psz_transport = httpd_MsgGet( query, "Transport" );
939 if( psz_transport == NULL )
941 answer->i_status = 400;
944 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
946 if( strstr( psz_transport, "unicast" ) &&
947 strstr( psz_transport, "client_port=" ) )
949 rtsp_client_t *p_rtsp = NULL;
950 char ip[NI_MAXNUMERICHOST];
951 i_port = atoi( strstr( psz_transport, "client_port=" ) +
952 strlen("client_port=") );
954 if( strstr( psz_transport, "MP2T/H2221/UDP" ) ||
955 strstr( psz_transport, "RAW/RAW/UDP" ) )
957 free( p_media->psz_mux );
958 p_media->psz_mux = NULL;
959 p_media->psz_mux = strdup( p_vod->p_sys->psz_raw_mux );
960 p_media->b_raw = true;
963 if( httpd_ClientIP( cl, ip ) == NULL )
965 answer->i_status = 500;
967 answer->p_body = NULL;
971 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
974 psz_session = httpd_MsgGet( query, "Session" );
975 if( !psz_session || !*psz_session )
978 if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
979 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
981 answer->i_status = 503;
983 answer->p_body = NULL;
986 if( asprintf( &psz_new, "%d", rand() ) < 0 )
988 psz_session = psz_new;
990 p_rtsp = RtspClientNew( p_media, psz_new );
993 answer->i_status = 454;
995 answer->p_body = NULL;
1001 p_rtsp = RtspClientGet( p_media, psz_session );
1004 answer->i_status = 454;
1006 answer->p_body = NULL;
1011 answer->i_status = 200;
1013 answer->p_body = NULL;
1015 if( p_media->b_raw )
1017 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
1019 httpd_MsgAdd( answer, "Transport",
1020 "MP2T/H2221/UDP;unicast;client_port=%d-%d",
1021 i_port, i_port + 1 );
1023 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1025 httpd_MsgAdd( answer, "Transport",
1026 "RAW/RAW/UDP;unicast;client_port=%d-%d",
1027 i_port, i_port + 1 );
1031 httpd_MsgAdd( answer, "Transport",
1032 "RTP/AVP/UDP;unicast;client_port=%d-%d",
1033 i_port, i_port + 1 );
1035 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1037 answer->i_status = 461;
1039 answer->p_body = NULL;
1042 /* Intentional fall-through on x-playNow option in RTSP request */
1047 case HTTPD_MSG_PLAY:
1049 char *psz_output, ip[NI_MAXNUMERICHOST];
1050 int i, i_port_audio = 0, i_port_video = 0;
1052 /* for now only multicast so easy */
1055 answer->i_status = 200;
1057 answer->p_body = NULL;
1061 psz_session = httpd_MsgGet( query, "Session" );
1062 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1064 p_rtsp = RtspClientGet( p_media, psz_session );
1067 answer->i_status = 500;
1069 answer->p_body = NULL;
1073 if( p_rtsp->b_playing )
1075 const char *psz_position = httpd_MsgGet( query, "Range" );
1076 const char *psz_scale = httpd_MsgGet( query, "Scale" );
1078 psz_position = strstr( psz_position, "npt=" );
1079 if( psz_position && !psz_scale )
1084 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1086 /* FIXME: npt= is not necessarily formatted as a float */
1087 f_pos = us_strtod( psz_position, &end );
1088 if( end > psz_position )
1090 f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
1091 CommandPush( p_vod, RTSP_CMD_TYPE_SEEK, p_media,
1092 psz_session, f_pos, NULL );
1098 double f_scale = 0.0;
1101 f_scale = us_strtod( psz_scale, &end );
1102 if( end > psz_scale )
1104 f_scale = (f_scale * 30.0);
1105 if( psz_scale[0] == '-' ) /* rewind */
1107 msg_Dbg( p_vod, "rewind request: %s", psz_scale );
1108 CommandPush( p_vod, RTSP_CMD_TYPE_REWIND, p_media,
1109 psz_session, f_scale, NULL );
1111 else if(psz_scale[0] != '1' ) /* fast-forward */
1113 msg_Dbg( p_vod, "fastforward request: %s",
1115 CommandPush( p_vod, RTSP_CMD_TYPE_FORWARD, p_media,
1116 psz_session, f_scale, NULL );
1119 if( p_rtsp->b_paused == true )
1121 p_rtsp->b_paused = false;
1122 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media,
1123 psz_session, 0, NULL );
1130 if( p_rtsp->b_playing && p_rtsp->b_paused )
1132 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media,
1133 psz_session, 0, NULL );
1134 p_rtsp->b_paused = false;
1137 else if( p_rtsp->b_playing ) break;
1139 if( httpd_ClientIP( cl, ip ) == NULL ) break;
1141 p_rtsp->b_playing = true;
1143 /* FIXME for != 1 video and 1 audio */
1144 for( i = 0; i < p_rtsp->i_es; i++ )
1146 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == AUDIO_ES )
1147 i_port_audio = p_rtsp->es[i]->i_port;
1148 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == VIDEO_ES )
1149 i_port_video = p_rtsp->es[i]->i_port;
1152 if( p_media->psz_mux )
1154 if( p_media->b_raw )
1156 if( asprintf( &psz_output,
1157 "std{access=udp,dst=%s:%i,mux=%s}",
1158 ip, i_port, p_media->psz_mux ) < 0 )
1163 if( asprintf( &psz_output,
1164 "rtp{dst=%s,port=%i,mux=%s}",
1165 ip, i_port_video, p_media->psz_mux ) < 0 )
1171 if( asprintf( &psz_output,
1172 "rtp{dst=%s,port-video=%i,port-audio=%i}",
1173 ip, i_port_video, i_port_audio ) < 0 )
1177 CommandPush( p_vod, RTSP_CMD_TYPE_PLAY, p_media, psz_session,
1183 case HTTPD_MSG_DESCRIBE:
1186 SDPGenerate( p_media, cl );
1188 if( psz_sdp != NULL )
1190 answer->i_status = 200;
1191 httpd_MsgAdd( answer, "Content-type", "%s",
1192 "application/sdp" );
1194 answer->p_body = (uint8_t *)psz_sdp;
1195 answer->i_body = strlen( psz_sdp );
1199 answer->i_status = 500;
1200 answer->p_body = NULL;
1206 case HTTPD_MSG_PAUSE:
1207 psz_session = httpd_MsgGet( query, "Session" );
1208 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1210 p_rtsp = RtspClientGet( p_media, psz_session );
1211 if( !p_rtsp ) break;
1213 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media, psz_session,
1215 p_rtsp->b_paused = true;
1217 answer->i_status = 200;
1219 answer->p_body = NULL;
1222 case HTTPD_MSG_TEARDOWN:
1223 /* for now only multicast so easy again */
1224 answer->i_status = 200;
1226 answer->p_body = NULL;
1228 psz_session = httpd_MsgGet( query, "Session" );
1229 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1231 p_rtsp = RtspClientGet( p_media, psz_session );
1232 if( !p_rtsp ) break;
1234 CommandPush( p_vod, RTSP_CMD_TYPE_STOP, p_media, psz_session,
1236 RtspClientDel( p_media, p_rtsp );
1239 case HTTPD_MSG_GETPARAMETER:
1240 answer->i_status = 200;
1242 answer->p_body = NULL;
1246 return VLC_EGENERIC;
1249 httpd_MsgAdd( answer, "Server", "VLC Server" );
1250 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1251 psz_cseq = httpd_MsgGet( query, "Cseq" );
1252 psz_cseq ? i_cseq = atoi( psz_cseq ) : 0;
1253 httpd_MsgAdd( answer, "CSeq", "%d", i_cseq );
1254 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1258 if( p_media->p_vod->p_sys->i_session_timeout >= 0 )
1259 httpd_MsgAdd( answer, "Session", "%s;timeout=%i", psz_session,
1260 p_media->p_vod->p_sys->i_session_timeout );
1262 httpd_MsgAdd( answer, "Session", "%s", psz_session );
1268 static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
1269 httpd_message_t *answer,
1270 const httpd_message_t *query )
1272 media_es_t *p_es = (media_es_t*)p_args;
1273 vod_media_t *p_media = p_es->p_media;
1274 vod_t *p_vod = p_media->p_vod;
1275 rtsp_client_t *p_rtsp = NULL;
1276 const char *psz_transport = NULL;
1277 const char *psz_playnow = NULL; /* support option: x-playNow */
1278 const char *psz_session = NULL;
1279 const char *psz_position = NULL;
1280 const char *psz_cseq = NULL;
1284 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
1286 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
1288 answer->i_proto = HTTPD_PROTO_RTSP;
1289 answer->i_version = query->i_version;
1290 answer->i_type = HTTPD_MSG_ANSWER;
1292 answer->p_body = NULL;
1294 switch( query->i_type )
1296 case HTTPD_MSG_SETUP:
1297 psz_playnow = httpd_MsgGet( query, "x-playNow" );
1298 psz_transport = httpd_MsgGet( query, "Transport" );
1300 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
1302 if( strstr( psz_transport, "unicast" ) &&
1303 strstr( psz_transport, "client_port=" ) )
1305 rtsp_client_t *p_rtsp = NULL;
1306 rtsp_client_es_t *p_rtsp_es = NULL;
1307 char ip[NI_MAXNUMERICHOST];
1308 int i_port = atoi( strstr( psz_transport, "client_port=" ) +
1309 strlen("client_port=") );
1311 if( httpd_ClientIP( cl, ip ) == NULL )
1313 answer->i_status = 500;
1315 answer->p_body = NULL;
1319 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
1322 psz_session = httpd_MsgGet( query, "Session" );
1323 if( !psz_session || !*psz_session )
1326 if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
1327 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
1329 answer->i_status = 503;
1331 answer->p_body = NULL;
1334 if( asprintf( &psz_new, "%d", rand() ) < 0 )
1336 psz_session = psz_new;
1338 p_rtsp = RtspClientNew( p_media, psz_new );
1342 p_rtsp = RtspClientGet( p_media, psz_session );
1345 answer->i_status = 454;
1347 answer->p_body = NULL;
1352 p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
1355 answer->i_status = 500;
1357 answer->p_body = NULL;
1360 p_rtsp_es->i_port = i_port;
1361 p_rtsp_es->psz_ip = strdup( ip );
1362 p_rtsp_es->p_media_es = p_es;
1363 TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );
1365 answer->i_status = 200;
1367 answer->p_body = NULL;
1369 if( p_media->b_raw )
1371 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
1373 httpd_MsgAdd( answer, "Transport",
1374 "MP2T/H2221/UDP;client_port=%d-%d",
1375 p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1377 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1379 httpd_MsgAdd( answer, "Transport",
1380 "RAW/RAW/UDP;client_port=%d-%d",
1381 p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1386 httpd_MsgAdd( answer, "Transport",
1387 "RTP/AVP/UDP;client_port=%d-%d",
1388 p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1391 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1393 answer->i_status = 461;
1395 answer->p_body = NULL;
1398 /* Intentional fall-through on x-playNow option in RTSP request */
1402 case HTTPD_MSG_PLAY:
1403 /* This is kind of a kludge. Should we only support Aggregate
1405 psz_session = httpd_MsgGet( query, "Session" );
1406 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1408 p_rtsp = RtspClientGet( p_media, psz_session );
1410 psz_position = httpd_MsgGet( query, "Range" );
1411 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
1417 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1420 /* FIXME: npt= is not necessarily formatted as a float */
1421 f_pos = us_strtod( psz_position, &end );
1422 if( end > psz_position )
1424 f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
1425 CommandPush( p_vod, RTSP_CMD_TYPE_SEEK, p_media,
1426 psz_session, f_pos, NULL );
1432 answer->i_status = 200;
1434 answer->p_body = NULL;
1438 case HTTPD_MSG_TEARDOWN:
1439 answer->i_status = 200;
1441 answer->p_body = NULL;
1443 psz_session = httpd_MsgGet( query, "Session" );
1444 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1446 p_rtsp = RtspClientGet( p_media, psz_session );
1447 if( !p_rtsp ) break;
1449 for( i = 0; i < p_rtsp->i_es; i++ )
1451 if( p_rtsp->es[i]->p_media_es == p_es )
1453 free( p_rtsp->es[i]->psz_ip );
1454 TAB_REMOVE( p_rtsp->i_es, p_rtsp->es, p_rtsp->es[i] );
1461 CommandPush( p_vod, RTSP_CMD_TYPE_STOP, p_media, psz_session,
1463 RtspClientDel( p_media, p_rtsp );
1467 case HTTPD_MSG_PAUSE:
1468 /* This is kind of a kludge. Should we only support Aggregate
1470 psz_session = httpd_MsgGet( query, "Session" );
1471 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1473 p_rtsp = RtspClientGet( p_media, psz_session );
1474 if( !p_rtsp ) break;
1476 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media, psz_session,
1478 p_rtsp->b_paused = true;
1480 answer->i_status = 200;
1482 answer->p_body = NULL;
1486 return VLC_EGENERIC;
1490 httpd_MsgAdd( answer, "Server", "VLC Server" );
1491 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1492 psz_cseq = httpd_MsgGet( query, "Cseq" );
1494 i_cseq = atoi( psz_cseq );
1497 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1498 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1501 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1506 /*****************************************************************************
1508 * FIXME: need to be moved to a common place ?
1509 *****************************************************************************/
1510 static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
1513 char *p, *psz_sdp, ip[NI_MAXNUMERICHOST], ipv;
1514 const char *psz_control;
1516 if( httpd_ServerIP( cl, ip ) == NULL )
1519 p = strchr( ip, '%' );
1521 *p = '\0'; /* remove scope if present */
1523 ipv = ( strchr( ip, ':' ) != NULL ) ? '6' : '4';
1525 /* Calculate size */
1526 i_size = sizeof( "v=0\r\n" ) +
1527 sizeof( "o=- * * IN IP4 \r\n" ) + 10 + NI_MAXNUMERICHOST +
1528 sizeof( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
1529 sizeof( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
1530 sizeof( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
1531 sizeof( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
1532 sizeof( "c=IN IP4 0.0.0.0\r\n" ) + 20 + 10 +
1533 sizeof( "t=0 0\r\n" ) + /* FIXME */
1534 sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
1535 sizeof( "a=range:npt=0-1000000000.000\r\n" );
1537 psz_control = (ipv == '6') ? p_media->psz_rtsp_control_v6
1538 : p_media->psz_rtsp_control_v4;
1539 for( i = 0; i < p_media->i_es; i++ )
1541 media_es_t *p_es = p_media->es[i];
1543 i_size += sizeof( "m=**d*o * RTP/AVP *\r\n" ) + 19;
1544 if( p_es->psz_rtpmap )
1546 i_size += sizeof( "a=rtpmap:* *\r\n" ) +
1547 strlen( p_es->psz_rtpmap ) + 9;
1549 if( p_es->psz_fmtp )
1551 i_size += sizeof( "a=fmtp:* *\r\n" ) +
1552 strlen( p_es->psz_fmtp ) + 9;
1555 i_size += (strlen( psz_control ) + strlen( ip ) + 9) * p_media->i_es;
1557 p = psz_sdp = malloc( i_size );
1558 p += sprintf( p, "v=0\r\n" );
1559 p += sprintf( p, "o=- %"PRId64" %d IN IP%c %s\r\n",
1560 p_media->i_sdp_id, p_media->i_sdp_version, ipv, ip );
1561 if( *p_media->psz_session_name )
1562 p += sprintf( p, "s=%s\r\n", p_media->psz_session_name );
1563 if( *p_media->psz_session_description )
1564 p += sprintf( p, "i=%s\r\n", p_media->psz_session_description );
1565 if( *p_media->psz_session_url )
1566 p += sprintf( p, "u=%s\r\n", p_media->psz_session_url );
1567 if( *p_media->psz_session_email )
1568 p += sprintf( p, "e=%s\r\n", p_media->psz_session_email );
1570 p += sprintf( p, "c=IN IP%c %s\r\n", ipv, ipv == '6' ? "::" : "0.0.0.0" );
1571 p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
1572 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
1574 if( p_media->i_length > 0 )
1576 lldiv_t d = lldiv( p_media->i_length / 1000, 1000 );
1577 p += sprintf( p, "a=range:npt=0-%lld.%03u\r\n", d.quot,
1581 for( i = 0; i < p_media->i_es; i++ )
1583 media_es_t *p_es = p_media->es[i];
1585 if( p_es->fmt.i_cat == AUDIO_ES )
1587 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
1588 p_es->i_port, p_es->i_payload_type );
1590 else if( p_es->fmt.i_cat == VIDEO_ES )
1592 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
1593 p_es->i_port, p_es->i_payload_type );
1600 if( p_es->psz_rtpmap )
1602 p += sprintf( p, "a=rtpmap:%d %s\r\n", p_es->i_payload_type,
1605 if( p_es->psz_fmtp )
1607 p += sprintf( p, "a=fmtp:%d %s\r\n", p_es->i_payload_type,
1611 p += sprintf( p, psz_control, ip, i );