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 *****************************************************************************/
32 #include <vlc_input.h>
34 #include <vlc_block.h>
36 #include "vlc_httpd.h"
39 #include <vlc_network.h>
40 #include <vlc_charset.h>
41 #include <vlc_strings.h>
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Open ( vlc_object_t * );
47 static void Close( vlc_object_t * );
49 #define HOST_TEXT N_( "RTSP host address" )
50 #define HOST_LONGTEXT N_( \
51 "This defines the address, port and path the RTSP VOD server will listen " \
52 "on.\nSyntax is address:port/path. The default is to listen on all "\
53 "interfaces (address 0.0.0.0), on port 554, with no path.\nTo listen " \
54 "only on the local interface, use \"localhost\" as address." )
56 #define THROTLE_TEXT N_( "Maximum number of connections" )
57 #define THROTLE_LONGTEXT N_( "This limits the maximum number of clients " \
58 "that can connect to the RTSP VOD. 0 means no limit." )
60 #define RAWMUX_TEXT N_( "MUX for RAW RTSP transport" )
62 #define SESSION_TIMEOUT_TEXT N_( "Sets the timeout option in the RTSP " \
64 #define SESSION_TIMEOUT_LONGTEXT N_( "Defines what timeout option to add " \
65 "to the RTSP session ID string. Setting it to a negative number removes " \
66 "the timeout option entirely. This is needed by some IPTV STBs (such as " \
67 "those made by HansunTech) which get confused by it. The default is 5." )
70 set_shortname( _("RTSP VoD" ) );
71 set_description( _("RTSP VoD server") );
72 set_category( CAT_SOUT );
73 set_subcategory( SUBCAT_SOUT_VOD );
74 set_capability( "vod server", 1 );
75 set_callbacks( Open, Close );
76 add_shortcut( "rtsp" );
77 add_string ( "rtsp-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
78 add_string( "rtsp-raw-mux", "ts", NULL, RAWMUX_TEXT, RAWMUX_TEXT, VLC_TRUE );
79 add_integer( "rtsp-throttle-users", 0, NULL, THROTLE_TEXT,
80 THROTLE_LONGTEXT, VLC_TRUE );
81 add_integer( "rtsp-session-timeout", 5, NULL, SESSION_TIMEOUT_TEXT,
82 SESSION_TIMEOUT_LONGTEXT, VLC_TRUE );
85 /*****************************************************************************
87 *****************************************************************************/
89 typedef struct media_es_t media_es_t;
93 media_es_t *p_media_es;
102 int64_t i_last; /* for timeout */
104 vlc_bool_t b_playing; /* is it in "play" state */
105 vlc_bool_t b_paused; /* is it in "pause" state */
108 rtsp_client_es_t **es;
118 httpd_url_t *p_rtsp_url;
120 vod_media_t *p_media;
124 uint8_t i_payload_type;
138 httpd_url_t *p_rtsp_url;
139 char *psz_rtsp_control_v4;
140 char *psz_rtsp_control_v6;
152 vlc_bool_t b_multicast;
164 rtsp_client_t **rtsp;
167 char *psz_session_name;
168 char *psz_session_description;
169 char *psz_session_url;
170 char *psz_session_email;
177 httpd_host_t *p_rtsp_host;
180 int i_throttle_users;
185 int i_session_timeout;
188 vlc_mutex_t lock_media;
194 block_fifo_t *p_fifo_cmd;
197 /* rtsp delayed command (to avoid deadlock between vlm/httpd) */
200 RTSP_CMD_TYPE_NONE, /* Exit requested */
206 RTSP_CMD_TYPE_REWIND,
207 RTSP_CMD_TYPE_FORWARD,
210 static vod_media_t *MediaNew( vod_t *, const char *, input_item_t * );
211 static void MediaDel( vod_t *, vod_media_t * );
212 static int MediaAddES( vod_t *, vod_media_t *, es_format_t * );
213 static void MediaDelES( vod_t *, vod_media_t *, es_format_t * );
215 static void CommandThread( vlc_object_t *p_this );
216 static void CommandPush( vod_t *, rtsp_cmd_type_t, vod_media_t *, const char *psz_session,
217 double f_arg, const char *psz_arg );
219 static rtsp_client_t *RtspClientNew( vod_media_t *, char * );
220 static rtsp_client_t *RtspClientGet( vod_media_t *, const char * );
221 static void RtspClientDel( vod_media_t *, rtsp_client_t * );
223 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
224 httpd_message_t *, httpd_message_t * );
225 static int RtspCallbackES( httpd_callback_sys_t *, httpd_client_t *,
226 httpd_message_t *, httpd_message_t * );
228 static char *SDPGenerate( const vod_media_t *, httpd_client_t *cl );
230 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
232 static const char hex[16] = "0123456789abcdef";
235 for( i = 0; i < i_data; i++ )
237 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
238 s[2*i+1] = hex[(p_data[i] )&0xf];
243 /*****************************************************************************
244 * Open: Starts the RTSP server module
245 *****************************************************************************/
246 static int Open( vlc_object_t *p_this )
248 vod_t *p_vod = (vod_t *)p_this;
249 vod_sys_t *p_sys = 0;
253 psz_url = config_GetPsz( p_vod, "rtsp-host" );
254 vlc_UrlParse( &url, psz_url, 0 );
255 if( psz_url ) free( psz_url );
257 if( url.i_port <= 0 ) url.i_port = 554;
259 p_vod->p_sys = p_sys = malloc( sizeof( vod_sys_t ) );
260 if( !p_sys ) goto error;
261 p_sys->p_rtsp_host = 0;
263 p_sys->i_session_timeout = var_CreateGetInteger( p_this, "rtsp-session-timeout" );
265 p_sys->i_throttle_users = var_CreateGetInteger( p_this, "rtsp-throttle-users" );
266 msg_Dbg( p_this, "allowing up to %d connections", p_sys->i_throttle_users );
267 p_sys->i_connections = 0;
269 p_sys->psz_raw_mux = var_CreateGetString( p_this, "rtsp-raw-mux" );
272 httpd_HostNew( VLC_OBJECT(p_vod), url.psz_host, url.i_port );
273 if( !p_sys->p_rtsp_host )
275 msg_Err( p_vod, "cannot create RTSP server (%s:%i)",
276 url.psz_host, url.i_port );
280 p_sys->psz_path = strdup( url.psz_path ? url.psz_path : "/" );
281 p_sys->i_port = url.i_port;
283 vlc_UrlClean( &url );
285 vlc_mutex_init( p_vod, &p_sys->lock_media );
287 TAB_INIT( p_sys->i_media, p_sys->media );
288 p_sys->i_media_id = 0;
290 p_vod->pf_media_new = MediaNew;
291 p_vod->pf_media_del = MediaDel;
292 p_vod->pf_media_add_es = MediaAddES;
293 p_vod->pf_media_del_es = MediaDelES;
295 p_sys->p_fifo_cmd = block_FifoNew( p_vod );
296 if( vlc_thread_create( p_vod, "rtsp vod thread", CommandThread,
297 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
299 msg_Err( p_vod, "cannot spawn rtsp vod thread" );
300 block_FifoRelease( p_sys->p_fifo_cmd );
301 free( p_sys->psz_path );
308 if( p_sys && p_sys->p_rtsp_host ) httpd_HostDelete( p_sys->p_rtsp_host );
309 if( p_sys && p_sys->psz_raw_mux ) free( p_sys->psz_raw_mux );
310 if( p_sys ) free( p_sys );
311 vlc_UrlClean( &url );
316 /*****************************************************************************
318 *****************************************************************************/
319 static void Close( vlc_object_t * p_this )
321 vod_t *p_vod = (vod_t *)p_this;
322 vod_sys_t *p_sys = p_vod->p_sys;
324 /* Stop command thread */
325 vlc_object_kill( p_vod );
326 CommandPush( p_vod, RTSP_CMD_TYPE_NONE, NULL, NULL, 0.0, NULL );
327 vlc_thread_join( p_vod );
329 block_FifoRelease( p_sys->p_fifo_cmd );
331 httpd_HostDelete( p_sys->p_rtsp_host );
332 var_Destroy( p_this, "rtsp-session-timeout" );
333 var_Destroy( p_this, "rtsp-throttle-users" );
334 var_Destroy( p_this, "rtsp-raw-mux" );
336 /* Check VLM is not buggy */
337 if( p_sys->i_media > 0 )
338 msg_Err( p_vod, "rtsp vod leaking %d medias", p_sys->i_media );
339 TAB_CLEAN( p_sys->i_media, p_sys->media );
341 vlc_mutex_destroy( &p_sys->lock_media );
343 free( p_sys->psz_path );
344 free( p_sys->psz_raw_mux );
348 /*****************************************************************************
350 *****************************************************************************/
351 static vod_media_t *MediaNew( vod_t *p_vod, const char *psz_name,
352 input_item_t *p_item )
354 vod_sys_t *p_sys = p_vod->p_sys;
355 vod_media_t *p_media = malloc( sizeof(vod_media_t) );
360 msg_Err( p_vod, "not enough memory" );
364 memset( p_media, 0, sizeof(vod_media_t) );
365 p_media->id = p_sys->i_media_id++;
366 TAB_INIT( p_media->i_es, p_media->es );
367 p_media->psz_mux = 0;
368 TAB_INIT( p_media->i_rtsp, p_media->rtsp );
369 p_media->b_raw = VLC_FALSE;
371 asprintf( &p_media->psz_rtsp_path, "%s%s", p_sys->psz_path, psz_name );
372 p_media->p_rtsp_url =
373 httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, NULL,
376 if( !p_media->p_rtsp_url )
378 msg_Err( p_vod, "cannot create RTSP url (%s)", p_media->psz_rtsp_path);
379 free( p_media->psz_rtsp_path );
384 msg_Dbg( p_vod, "created RTSP url: %s", p_media->psz_rtsp_path );
386 asprintf( &p_media->psz_rtsp_control_v4,
387 "a=control:rtsp://%%s:%d%s/trackID=%%d\r\n",
388 p_sys->i_port, p_media->psz_rtsp_path );
389 asprintf( &p_media->psz_rtsp_control_v6,
390 "a=control:rtsp://[%%s]:%d%s/trackID=%%d\r\n",
391 p_sys->i_port, p_media->psz_rtsp_path );
393 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_SETUP,
394 RtspCallback, (void*)p_media );
395 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_DESCRIBE,
396 RtspCallback, (void*)p_media );
397 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PLAY,
398 RtspCallback, (void*)p_media );
399 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PAUSE,
400 RtspCallback, (void*)p_media );
401 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_GETPARAMETER,
402 RtspCallback, (void*)p_media );
403 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_TEARDOWN,
404 RtspCallback, (void*)p_media );
406 p_media->p_vod = p_vod;
408 vlc_mutex_lock( &p_sys->lock_media );
409 TAB_APPEND( p_sys->i_media, p_sys->media, p_media );
410 vlc_mutex_unlock( &p_sys->lock_media );
412 vlc_mutex_init( p_vod, &p_media->lock );
413 p_media->psz_session_name = strdup("");
414 p_media->psz_session_description = strdup("");
415 p_media->psz_session_url = strdup("");
416 p_media->psz_session_email = strdup("");
418 p_media->i_port_audio = 1234;
419 p_media->i_port_video = 1236;
420 p_media->i_port = 1238;
421 p_media->i_payload_type = 96;
423 p_media->i_sdp_id = mdate();
424 p_media->i_sdp_version = 1;
425 p_media->i_length = p_item->i_duration;
427 vlc_mutex_lock( &p_item->lock );
428 msg_Dbg( p_vod, "media has %i declared ES", p_item->i_es );
429 for( i = 0; i < p_item->i_es; i++ )
431 MediaAddES( p_vod, p_media, p_item->es[i] );
433 vlc_mutex_unlock( &p_item->lock );
438 static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
440 vod_sys_t *p_sys = p_vod->p_sys;
442 msg_Dbg( p_vod, "deleting media: %s", p_media->psz_rtsp_path );
444 vlc_mutex_lock( &p_sys->lock_media );
445 TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
446 vlc_mutex_unlock( &p_sys->lock_media );
448 while( p_media->i_rtsp > 0 )
449 RtspClientDel( p_media, p_media->rtsp[0] );
450 TAB_CLEAN( p_media->i_rtsp, p_media->rtsp );
452 httpd_UrlDelete( p_media->p_rtsp_url );
453 if( p_media->psz_rtsp_path ) free( p_media->psz_rtsp_path );
454 if( p_media->psz_rtsp_control_v6 ) free( p_media->psz_rtsp_control_v6 );
455 if( p_media->psz_rtsp_control_v4 ) free( p_media->psz_rtsp_control_v4 );
457 while( p_media->i_es )
458 MediaDelES( p_vod, p_media, &p_media->es[0]->fmt );
459 TAB_CLEAN( p_media->i_es, p_media->es );
461 vlc_mutex_destroy( &p_media->lock );
463 if( p_media->psz_session_name ) free( p_media->psz_session_name );
464 if( p_media->psz_session_description ) free( p_media->psz_session_description );
465 if( p_media->psz_session_url ) free( p_media->psz_session_url );
466 if( p_media->psz_session_email ) free( p_media->psz_session_email );
467 if( p_media->psz_mux ) free( p_media->psz_mux );
471 static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
473 media_es_t *p_es = malloc( sizeof(media_es_t) );
476 if( !p_es ) return VLC_ENOMEM;
477 memset( p_es, 0, sizeof(media_es_t) );
479 if( p_media->psz_mux ) free( p_media->psz_mux );
480 p_media->psz_mux = NULL;
482 /* TODO: update SDP, etc... */
483 asprintf( &psz_urlc, "%s/trackID=%d",
484 p_media->psz_rtsp_path, p_media->i_es );
485 msg_Dbg( p_vod, " - ES %4.4s (%s)", (char *)&p_fmt->i_codec, psz_urlc );
487 switch( p_fmt->i_codec )
489 case VLC_FOURCC( 's', '1', '6', 'b' ):
490 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
492 p_es->i_payload_type = 11;
494 else if( p_fmt->audio.i_channels == 2 && p_fmt->audio.i_rate == 44100 )
496 p_es->i_payload_type = 10;
500 p_es->i_payload_type = p_media->i_payload_type++;
502 p_es->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
503 sprintf( p_es->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
504 p_fmt->audio.i_channels );
506 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
507 p_es->i_payload_type = p_media->i_payload_type++;
508 p_es->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
509 sprintf( p_es->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
510 p_fmt->audio.i_channels );
512 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
513 p_es->i_payload_type = 14;
514 p_es->psz_rtpmap = strdup( "MPA/90000" );
516 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
517 p_es->i_payload_type = 32;
518 p_es->psz_rtpmap = strdup( "MPV/90000" );
520 case VLC_FOURCC( 'a', '5', '2', ' ' ):
521 p_es->i_payload_type = p_media->i_payload_type++;
522 p_es->psz_rtpmap = strdup( "ac3/90000" );
524 case VLC_FOURCC( 'H', '2', '6', '3' ):
525 p_es->i_payload_type = p_media->i_payload_type++;
526 p_es->psz_rtpmap = strdup( "H263-1998/90000" );
528 case VLC_FOURCC( 'h', '2', '6', '4' ):
529 p_es->i_payload_type = p_media->i_payload_type++;
530 p_es->psz_rtpmap = strdup( "H264/90000" );
531 p_es->psz_fmtp = NULL;
532 /* FIXME AAAAAAAAAAAARRRRRRRRGGGG copied from stream_out/rtp.c */
533 if( p_fmt->i_extra > 0 )
535 uint8_t *p_buffer = p_fmt->p_extra;
536 int i_buffer = p_fmt->i_extra;
537 char *p_64_sps = NULL;
538 char *p_64_pps = NULL;
541 while( i_buffer > 4 &&
542 p_buffer[0] == 0 && p_buffer[1] == 0 &&
543 p_buffer[2] == 0 && p_buffer[3] == 1 )
545 const int i_nal_type = p_buffer[4]&0x1f;
550 for( i_offset = 4; i_offset+3 < i_buffer ; i_offset++)
552 if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 0 && p_buffer[i_offset+3] == 1 )
554 /* we found another startcode */
559 if( i_nal_type == 7 )
561 p_64_sps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
562 sprintf_hexa( hexa, &p_buffer[5], 3 );
564 else if( i_nal_type == 8 )
566 p_64_pps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
572 if( p_64_sps && p_64_pps )
573 asprintf( &p_es->psz_fmtp, "packetization-mode=1;profile-level-id=%s;sprop-parameter-sets=%s,%s;", hexa, p_64_sps, p_64_pps );
579 if( !p_es->psz_fmtp )
580 p_es->psz_fmtp = strdup( "packetization-mode=1" );
582 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
583 p_es->i_payload_type = p_media->i_payload_type++;
584 p_es->psz_rtpmap = strdup( "MP4V-ES/90000" );
585 if( p_fmt->i_extra > 0 )
587 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
588 p_es->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
589 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
590 sprintf( p_es->psz_fmtp,
591 "profile-level-id=3; config=%s;", p_hexa );
595 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
596 p_es->i_payload_type = p_media->i_payload_type++;
597 p_es->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
598 sprintf( p_es->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
599 if( p_fmt->i_extra > 0 )
601 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
602 p_es->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
603 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
604 sprintf( p_es->psz_fmtp,
605 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
606 "config=%s; SizeLength=13;IndexLength=3; "
607 "IndexDeltaLength=3; Profile=1;", p_hexa );
611 case VLC_FOURCC( 'm', 'p', '2', 't' ):
612 p_media->psz_mux = strdup("ts");
613 p_es->i_payload_type = 33;
614 p_es->psz_rtpmap = strdup( "MP2T/90000" );
616 case VLC_FOURCC( 'm', 'p', '2', 'p' ):
617 p_media->psz_mux = strdup("ps");
618 p_es->i_payload_type = p_media->i_payload_type++;
619 p_es->psz_rtpmap = strdup( "MP2P/90000" );
621 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
622 p_es->i_payload_type = p_media->i_payload_type++;
623 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
624 "AMR/8000/2" : "AMR/8000" );
625 p_es->psz_fmtp = strdup( "octet-align=1" );
627 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
628 p_es->i_payload_type = p_media->i_payload_type++;
629 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
630 "AMR-WB/16000/2" : "AMR-WB/16000" );
631 p_es->psz_fmtp = strdup( "octet-align=1" );
635 msg_Err( p_vod, "cannot add this stream (unsupported "
636 "codec: %4.4s)", (char*)&p_fmt->i_codec );
642 httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, NULL, NULL,
645 if( !p_es->p_rtsp_url )
647 msg_Err( p_vod, "cannot create RTSP url (%s)", psz_urlc );
654 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
655 RtspCallbackES, (void*)p_es );
656 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_TEARDOWN,
657 RtspCallbackES, (void*)p_es );
658 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PLAY,
659 RtspCallbackES, (void*)p_es );
660 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PAUSE,
661 RtspCallbackES, (void*)p_es );
663 es_format_Copy( &p_es->fmt, p_fmt );
665 p_es->p_media = p_media;
668 /* Choose the port */
669 if( p_fmt->i_cat == AUDIO_ES && p_media->i_port_audio > 0 )
671 p_es->i_port = p_media->i_port_audio;
672 p_media->i_port_audio = 0;
674 else if( p_fmt->i_cat == VIDEO_ES && p_media->i_port_video > 0 )
676 p_es->i_port = p_media->i_port_video;
677 p_media->i_port_video = 0;
679 while( !p_es->i_port )
681 if( p_media->i_port != p_media->i_port_audio &&
682 p_media->i_port != p_media->i_port_video )
684 p_es->i_port = p_media->i_port;
685 p_media->i_port += 2;
688 p_media->i_port += 2;
695 vlc_mutex_lock( &p_media->lock );
696 TAB_APPEND( p_media->i_es, p_media->es, p_es );
697 vlc_mutex_unlock( &p_media->lock );
699 p_media->i_sdp_version++;
704 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
706 media_es_t *p_es = NULL;
710 for( i = 0; i < p_media->i_es; i++ )
712 if( p_media->es[i]->fmt.i_cat == p_fmt->i_cat &&
713 p_media->es[i]->fmt.i_codec == p_fmt->i_codec &&
714 p_media->es[i]->fmt.i_id == p_fmt->i_id )
716 p_es = p_media->es[i];
721 msg_Dbg( p_vod, " - Removing ES %4.4s", (char *)&p_fmt->i_codec );
723 vlc_mutex_lock( &p_media->lock );
724 TAB_REMOVE( p_media->i_es, p_media->es, p_es );
725 vlc_mutex_unlock( &p_media->lock );
727 if( p_es->psz_rtpmap ) free( p_es->psz_rtpmap );
728 if( p_es->psz_fmtp ) free( p_es->psz_fmtp );
729 p_media->i_sdp_version++;
731 if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
732 es_format_Clean( &p_es->fmt );
741 //vod_media_t *p_media;
747 static void CommandPush( vod_t *p_vod, rtsp_cmd_type_t i_type, vod_media_t *p_media, const char *psz_session,
748 double f_arg, const char *psz_arg )
753 memset( &cmd, 0, sizeof(cmd) );
756 cmd.i_media_id = p_media->id;
758 cmd.psz_session = strdup(psz_session);
761 cmd.psz_arg = strdup(psz_arg);
763 p_cmd = block_New( p_vod, sizeof(rtsp_cmd_t) );
764 memcpy( p_cmd->p_buffer, &cmd, sizeof(cmd) );
766 block_FifoPut( p_vod->p_sys->p_fifo_cmd, p_cmd );
769 static void CommandThread( vlc_object_t *p_this )
771 vod_t *p_vod = (vod_t*)p_this;
772 vod_sys_t *p_sys = p_vod->p_sys;
774 while( !p_vod->b_die )
776 block_t *p_block_cmd = block_FifoGet( p_sys->p_fifo_cmd );
778 vod_media_t *p_media = NULL;
784 memcpy( &cmd, p_block_cmd->p_buffer, sizeof(cmd) );
785 block_Release( p_block_cmd );
787 if( cmd.i_type == RTSP_CMD_TYPE_NONE )
791 vlc_mutex_lock( &p_sys->lock_media );
792 for( i = 0; i < p_sys->i_media; i++ )
794 if( p_sys->media[i]->id == cmd.i_media_id )
797 if( i >= p_sys->i_media )
799 p_media = p_sys->media[i];
803 case RTSP_CMD_TYPE_PLAY:
804 vod_MediaControl( p_vod, p_media, cmd.psz_session,
805 VOD_MEDIA_PLAY, cmd.psz_arg );
807 case RTSP_CMD_TYPE_PAUSE:
808 vod_MediaControl( p_vod, p_media, cmd.psz_session,
812 case RTSP_CMD_TYPE_STOP:
813 vod_MediaControl( p_vod, p_media, cmd.psz_session, VOD_MEDIA_STOP );
816 case RTSP_CMD_TYPE_SEEK:
817 vod_MediaControl( p_vod, p_media, cmd.psz_session,
818 VOD_MEDIA_SEEK, cmd.f_arg );
821 case RTSP_CMD_TYPE_REWIND:
822 vod_MediaControl( p_vod, p_media, cmd.psz_session,
823 VOD_MEDIA_REWIND, cmd.f_arg );
826 case RTSP_CMD_TYPE_FORWARD:
827 vod_MediaControl( p_vod, p_media, cmd.psz_session,
828 VOD_MEDIA_FORWARD, cmd.f_arg );
836 vlc_mutex_unlock( &p_sys->lock_media );
837 if( cmd.psz_session )
838 free( cmd.psz_session );
844 /****************************************************************************
845 * RTSP server implementation
846 ****************************************************************************/
847 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
849 rtsp_client_t *p_rtsp = malloc( sizeof(rtsp_client_t) );
851 if( !p_rtsp ) return NULL;
852 memset( p_rtsp, 0, sizeof(rtsp_client_t) );
855 p_rtsp->psz_session = psz_session;
856 TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
858 p_media->p_vod->p_sys->i_connections++;
859 msg_Dbg( p_media->p_vod, "new session: %s, connections: %d",
860 psz_session, p_media->p_vod->p_sys->i_throttle_users );
865 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, const char *psz_session )
869 for( i = 0; psz_session && i < p_media->i_rtsp; i++ )
871 if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
872 return p_media->rtsp[i];
878 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
880 p_media->p_vod->p_sys->i_connections--;
881 msg_Dbg( p_media->p_vod, "closing session: %s, connections: %d",
882 p_rtsp->psz_session, p_media->p_vod->p_sys->i_throttle_users );
884 while( p_rtsp->i_es-- )
886 if( p_rtsp->es[p_rtsp->i_es]->psz_ip )
887 free( p_rtsp->es[p_rtsp->i_es]->psz_ip );
888 free( p_rtsp->es[p_rtsp->i_es] );
889 if( !p_rtsp->i_es ) free( p_rtsp->es );
892 TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, p_rtsp );
894 free( p_rtsp->psz_session );
898 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
899 httpd_message_t *answer, httpd_message_t *query )
901 vod_media_t *p_media = (vod_media_t*)p_args;
902 vod_t *p_vod = p_media->p_vod;
903 const char *psz_transport = NULL;
904 const char *psz_playnow = NULL; /* support option: x-playNow */
905 const char *psz_session = NULL;
906 const char *psz_cseq = NULL;
907 rtsp_client_t *p_rtsp;
911 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
913 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
915 answer->i_proto = HTTPD_PROTO_RTSP;
916 answer->i_version = query->i_version;
917 answer->i_type = HTTPD_MSG_ANSWER;
919 answer->p_body = NULL;
921 switch( query->i_type )
923 case HTTPD_MSG_SETUP:
925 psz_playnow = httpd_MsgGet( query, "x-playNow" );
926 psz_transport = httpd_MsgGet( query, "Transport" );
927 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
929 if( strstr( psz_transport, "unicast" ) &&
930 strstr( psz_transport, "client_port=" ) )
932 rtsp_client_t *p_rtsp = NULL;
933 char ip[NI_MAXNUMERICHOST];
934 i_port = atoi( strstr( psz_transport, "client_port=" ) +
935 strlen("client_port=") );
937 if( strstr( psz_transport, "MP2T/H2221/UDP" ) ||
938 strstr( psz_transport, "RAW/RAW/UDP" ) )
940 if( p_media->psz_mux ) free( p_media->psz_mux );
941 p_media->psz_mux = NULL;
942 p_media->psz_mux = strdup( p_vod->p_sys->psz_raw_mux );
943 p_media->b_raw = VLC_TRUE;
946 if( httpd_ClientIP( cl, ip ) == NULL )
948 answer->i_status = 500;
950 answer->p_body = NULL;
954 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
957 psz_session = httpd_MsgGet( query, "Session" );
958 if( !psz_session || !*psz_session )
961 if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
962 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
964 answer->i_status = 503;
966 answer->p_body = NULL;
969 asprintf( &psz_new, "%d", rand() );
970 psz_session = psz_new;
972 p_rtsp = RtspClientNew( p_media, psz_new );
975 answer->i_status = 454;
977 answer->p_body = NULL;
983 p_rtsp = RtspClientGet( p_media, psz_session );
986 answer->i_status = 454;
988 answer->p_body = NULL;
993 answer->i_status = 200;
995 answer->p_body = NULL;
999 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
1001 httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
1002 i_port, i_port + 1 );
1004 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1006 httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
1007 i_port, i_port + 1 );
1011 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
1012 i_port, i_port + 1 );
1014 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1016 answer->i_status = 461;
1018 answer->p_body = NULL;
1021 /* Intentional fall-through on x-playNow option in RTSP request */
1026 case HTTPD_MSG_PLAY:
1028 char *psz_output, ip[NI_MAXNUMERICHOST];
1029 int i, i_port_audio = 0, i_port_video = 0;
1031 /* for now only multicast so easy */
1034 answer->i_status = 200;
1036 answer->p_body = NULL;
1040 psz_session = httpd_MsgGet( query, "Session" );
1041 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1043 p_rtsp = RtspClientGet( p_media, psz_session );
1046 answer->i_status = 500;
1048 answer->p_body = NULL;
1052 if( p_rtsp->b_playing )
1054 const char *psz_position = httpd_MsgGet( query, "Range" );
1055 const char *psz_scale = httpd_MsgGet( query, "Scale" );
1056 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
1057 if( psz_position && !psz_scale )
1062 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1064 /* FIXME: npt= is not necessarily formatted as a float */
1065 f_pos = us_strtod( psz_position, &end );
1066 if( end > psz_position )
1068 f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
1069 CommandPush( p_vod, RTSP_CMD_TYPE_SEEK, p_media, psz_session, f_pos, NULL );
1075 double f_scale = 0.0;
1078 f_scale = us_strtod( psz_scale, &end );
1079 if( end > psz_scale )
1081 f_scale = (f_scale * 30.0);
1082 if( psz_scale[0] == '-' ) /* rewind */
1084 msg_Dbg( p_vod, "rewind request: %s", psz_scale );
1085 CommandPush( p_vod, RTSP_CMD_TYPE_REWIND, p_media, psz_session, f_scale, NULL );
1087 else if(psz_scale[0] != '1' ) /* fast-forward */
1089 msg_Dbg( p_vod, "fastforward request: %s", psz_scale );
1090 CommandPush( p_vod, RTSP_CMD_TYPE_FORWARD, p_media, psz_session, f_scale, NULL );
1093 if( p_rtsp->b_paused == VLC_TRUE )
1095 p_rtsp->b_paused = VLC_FALSE;
1096 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media, psz_session, 0, NULL );
1103 if( p_rtsp->b_playing && p_rtsp->b_paused )
1105 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media, psz_session, 0, NULL );
1106 p_rtsp->b_paused = VLC_FALSE;
1109 else if( p_rtsp->b_playing ) break;
1111 if( httpd_ClientIP( cl, ip ) == NULL ) break;
1113 p_rtsp->b_playing = VLC_TRUE;
1115 /* FIXME for != 1 video and 1 audio */
1116 for( i = 0; i < p_rtsp->i_es; i++ )
1118 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == AUDIO_ES )
1119 i_port_audio = p_rtsp->es[i]->i_port;
1120 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == VIDEO_ES )
1121 i_port_video = p_rtsp->es[i]->i_port;
1124 if( p_media->psz_mux )
1126 if( p_media->b_raw )
1128 asprintf( &psz_output, "std{access=udp,dst=%s:%i,mux=%s}",
1129 ip, i_port, p_media->psz_mux );
1133 asprintf( &psz_output, "rtp{dst=%s,port=%i,mux=%s}",
1134 ip, i_port_video, p_media->psz_mux );
1139 asprintf( &psz_output, "rtp{dst=%s,port-video=%i,"
1140 "port-audio=%i}", ip, i_port_video, i_port_audio );
1143 CommandPush( p_vod, RTSP_CMD_TYPE_PLAY, p_media, psz_session, 0, psz_output );
1148 case HTTPD_MSG_DESCRIBE:
1151 SDPGenerate( p_media, cl );
1153 if( psz_sdp != NULL )
1155 answer->i_status = 200;
1156 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
1158 answer->p_body = (uint8_t *)psz_sdp;
1159 answer->i_body = strlen( psz_sdp );
1163 answer->i_status = 500;
1164 answer->p_body = NULL;
1170 case HTTPD_MSG_PAUSE:
1171 psz_session = httpd_MsgGet( query, "Session" );
1172 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1174 p_rtsp = RtspClientGet( p_media, psz_session );
1175 if( !p_rtsp ) break;
1177 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media, psz_session, 0, NULL );
1178 p_rtsp->b_paused = VLC_TRUE;
1180 answer->i_status = 200;
1182 answer->p_body = NULL;
1185 case HTTPD_MSG_TEARDOWN:
1186 /* for now only multicast so easy again */
1187 answer->i_status = 200;
1189 answer->p_body = NULL;
1191 psz_session = httpd_MsgGet( query, "Session" );
1192 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1194 p_rtsp = RtspClientGet( p_media, psz_session );
1195 if( !p_rtsp ) break;
1197 CommandPush( p_vod, RTSP_CMD_TYPE_STOP, p_media, psz_session, 0, NULL );
1198 RtspClientDel( p_media, p_rtsp );
1201 case HTTPD_MSG_GETPARAMETER:
1202 answer->i_status = 200;
1204 answer->p_body = NULL;
1208 return VLC_EGENERIC;
1211 httpd_MsgAdd( answer, "Server", "VLC Server" );
1212 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1213 psz_cseq = httpd_MsgGet( query, "Cseq" );
1214 psz_cseq ? i_cseq = atoi( psz_cseq ) : 0;
1215 httpd_MsgAdd( answer, "CSeq", "%d", i_cseq );
1216 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1220 if( p_media->p_vod->p_sys->i_session_timeout >= 0 )
1221 httpd_MsgAdd( answer, "Session", "%s;timeout=%i", psz_session,
1222 p_media->p_vod->p_sys->i_session_timeout );
1224 httpd_MsgAdd( answer, "Session", "%s", psz_session );
1230 static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
1231 httpd_message_t *answer, httpd_message_t *query )
1233 media_es_t *p_es = (media_es_t*)p_args;
1234 vod_media_t *p_media = p_es->p_media;
1235 vod_t *p_vod = p_media->p_vod;
1236 rtsp_client_t *p_rtsp = NULL;
1237 const char *psz_transport = NULL;
1238 const char *psz_playnow = NULL; /* support option: x-playNow */
1239 const char *psz_session = NULL;
1240 const char *psz_position = NULL;
1241 const char *psz_cseq = NULL;
1245 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
1247 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
1249 answer->i_proto = HTTPD_PROTO_RTSP;
1250 answer->i_version = query->i_version;
1251 answer->i_type = HTTPD_MSG_ANSWER;
1253 answer->p_body = NULL;
1255 switch( query->i_type )
1257 case HTTPD_MSG_SETUP:
1258 psz_playnow = httpd_MsgGet( query, "x-playNow" );
1259 psz_transport = httpd_MsgGet( query, "Transport" );
1261 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
1263 if( strstr( psz_transport, "unicast" ) &&
1264 strstr( psz_transport, "client_port=" ) )
1266 rtsp_client_t *p_rtsp = NULL;
1267 rtsp_client_es_t *p_rtsp_es = NULL;
1268 char ip[NI_MAXNUMERICHOST];
1269 int i_port = atoi( strstr( psz_transport, "client_port=" ) +
1270 strlen("client_port=") );
1272 if( httpd_ClientIP( cl, ip ) == NULL )
1274 answer->i_status = 500;
1276 answer->p_body = NULL;
1280 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
1283 psz_session = httpd_MsgGet( query, "Session" );
1284 if( !psz_session || !*psz_session )
1287 if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
1288 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
1290 answer->i_status = 503;
1292 answer->p_body = NULL;
1295 asprintf( &psz_new, "%d", rand() );
1296 psz_session = psz_new;
1298 p_rtsp = RtspClientNew( p_media, psz_new );
1302 p_rtsp = RtspClientGet( p_media, psz_session );
1305 answer->i_status = 454;
1307 answer->p_body = NULL;
1312 p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
1315 answer->i_status = 500;
1317 answer->p_body = NULL;
1320 p_rtsp_es->i_port = i_port;
1321 p_rtsp_es->psz_ip = strdup( ip );
1322 p_rtsp_es->p_media_es = p_es;
1323 TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );
1325 answer->i_status = 200;
1327 answer->p_body = NULL;
1329 if( p_media->b_raw )
1331 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
1333 httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
1334 p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1336 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1338 httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
1339 p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1344 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
1345 p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1348 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1350 answer->i_status = 461;
1352 answer->p_body = NULL;
1355 /* Intentional fall-through on x-playNow option in RTSP request */
1359 case HTTPD_MSG_PLAY:
1360 /* This is kind of a kludge. Should we only support Aggregate
1362 psz_session = httpd_MsgGet( query, "Session" );
1363 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1365 p_rtsp = RtspClientGet( p_media, psz_session );
1367 psz_position = httpd_MsgGet( query, "Range" );
1368 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
1374 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1377 /* FIXME: npt= is not necessarily formatted as a float */
1378 f_pos = us_strtod( psz_position, &end );
1379 if( end > psz_position )
1381 f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
1382 CommandPush( p_vod, RTSP_CMD_TYPE_SEEK, p_media, psz_session, f_pos, NULL );
1388 answer->i_status = 200;
1390 answer->p_body = NULL;
1394 case HTTPD_MSG_TEARDOWN:
1395 answer->i_status = 200;
1397 answer->p_body = NULL;
1399 psz_session = httpd_MsgGet( query, "Session" );
1400 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1402 p_rtsp = RtspClientGet( p_media, psz_session );
1403 if( !p_rtsp ) break;
1405 for( i = 0; i < p_rtsp->i_es; i++ )
1407 if( p_rtsp->es[i]->p_media_es == p_es )
1409 if( p_rtsp->es[i]->psz_ip ) free( p_rtsp->es[i]->psz_ip );
1410 TAB_REMOVE( p_rtsp->i_es, p_rtsp->es, p_rtsp->es[i] );
1417 CommandPush( p_vod, RTSP_CMD_TYPE_STOP, p_media, psz_session, 0, NULL );
1418 RtspClientDel( p_media, p_rtsp );
1422 case HTTPD_MSG_PAUSE:
1423 /* This is kind of a kludge. Should we only support Aggregate
1425 psz_session = httpd_MsgGet( query, "Session" );
1426 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1428 p_rtsp = RtspClientGet( p_media, psz_session );
1429 if( !p_rtsp ) break;
1431 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media, psz_session, 0, NULL );
1432 p_rtsp->b_paused = VLC_TRUE;
1434 answer->i_status = 200;
1436 answer->p_body = NULL;
1440 return VLC_EGENERIC;
1444 httpd_MsgAdd( answer, "Server", "VLC Server" );
1445 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1446 psz_cseq = httpd_MsgGet( query, "Cseq" );
1448 i_cseq = atoi( psz_cseq );
1451 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1452 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1455 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1460 /*****************************************************************************
1462 * FIXME: need to be moved to a common place ?
1463 *****************************************************************************/
1464 static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
1467 char *p, *psz_sdp, ip[NI_MAXNUMERICHOST], ipv;
1468 const char *psz_control;
1470 if( httpd_ServerIP( cl, ip ) == NULL )
1473 p = strchr( ip, '%' );
1475 *p = '\0'; /* remove scope if present */
1477 ipv = ( strchr( ip, ':' ) != NULL ) ? '6' : '4';
1479 /* Calculate size */
1480 i_size = sizeof( "v=0\r\n" ) +
1481 sizeof( "o=- * * IN IP4 \r\n" ) + 10 + NI_MAXNUMERICHOST +
1482 sizeof( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
1483 sizeof( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
1484 sizeof( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
1485 sizeof( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
1486 sizeof( "c=IN IP4 0.0.0.0\r\n" ) + 20 + 10 +
1487 sizeof( "t=0 0\r\n" ) + /* FIXME */
1488 sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
1489 sizeof( "a=range:npt=0-1000000000.000\r\n" );
1491 psz_control = (ipv == '6') ? p_media->psz_rtsp_control_v6
1492 : p_media->psz_rtsp_control_v4;
1493 for( i = 0; i < p_media->i_es; i++ )
1495 media_es_t *p_es = p_media->es[i];
1497 i_size += sizeof( "m=**d*o * RTP/AVP *\r\n" ) + 19;
1498 if( p_es->psz_rtpmap )
1500 i_size += sizeof( "a=rtpmap:* *\r\n" ) +
1501 strlen( p_es->psz_rtpmap ) + 9;
1503 if( p_es->psz_fmtp )
1505 i_size += sizeof( "a=fmtp:* *\r\n" ) +
1506 strlen( p_es->psz_fmtp ) + 9;
1509 i_size += (strlen( psz_control ) + strlen( ip ) + 9) * p_media->i_es;
1511 p = psz_sdp = malloc( i_size );
1512 p += sprintf( p, "v=0\r\n" );
1513 p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
1514 p_media->i_sdp_id, p_media->i_sdp_version, ipv, ip );
1515 if( *p_media->psz_session_name )
1516 p += sprintf( p, "s=%s\r\n", p_media->psz_session_name );
1517 if( *p_media->psz_session_description )
1518 p += sprintf( p, "i=%s\r\n", p_media->psz_session_description );
1519 if( *p_media->psz_session_url )
1520 p += sprintf( p, "u=%s\r\n", p_media->psz_session_url );
1521 if( *p_media->psz_session_email )
1522 p += sprintf( p, "e=%s\r\n", p_media->psz_session_email );
1524 p += sprintf( p, "c=IN IP%c %s\r\n", ipv, ipv == '6' ? "::" : "0.0.0.0" );
1525 p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
1526 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
1528 if( p_media->i_length > 0 )
1530 lldiv_t d = lldiv( p_media->i_length / 1000, 1000 );
1531 p += sprintf( p, "a=range:npt=0-%lld.%03u\r\n", d.quot,
1535 for( i = 0; i < p_media->i_es; i++ )
1537 media_es_t *p_es = p_media->es[i];
1539 if( p_es->fmt.i_cat == AUDIO_ES )
1541 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
1542 p_es->i_port, p_es->i_payload_type );
1544 else if( p_es->fmt.i_cat == VIDEO_ES )
1546 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
1547 p_es->i_port, p_es->i_payload_type );
1554 if( p_es->psz_rtpmap )
1556 p += sprintf( p, "a=rtpmap:%d %s\r\n", p_es->i_payload_type,
1559 if( p_es->psz_fmtp )
1561 p += sprintf( p, "a=fmtp:%d %s\r\n", p_es->i_payload_type,
1565 p += sprintf( p, psz_control, ip, i );