1 /*****************************************************************************
2 * rtsp.c: rtsp VoD server module
3 *****************************************************************************
4 * Copyright (C) 2003-2006 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
34 #include <vlc_input.h>
36 #include <vlc_block.h>
38 #include "vlc_httpd.h"
41 #include <vlc_network.h>
42 #include <vlc_charset.h>
44 /*****************************************************************************
46 *****************************************************************************/
47 static int Open ( vlc_object_t * );
48 static void Close( vlc_object_t * );
50 #define HOST_TEXT N_( "RTSP host address" )
51 /// \bug [String] extra space
52 #define HOST_LONGTEXT N_( \
53 "This defines the address, port and path the RTSP VOD server will listen " \
54 "on.\nSyntax is address:port/path. The default is to listen on all "\
55 "interfaces (address 0.0.0.0), on port 554, with no path.\n To listen " \
56 "only on the local interface, use \"localhost\" as address." )
58 #define THROTLE_TEXT N_( "Maximum number of connections" )
59 #define THROTLE_LONGTEXT N_( "This limits the maximum number of clients " \
60 "that can connect to the RTSP VOD. 0 means no limit." )
62 #define RAWMUX_TEXT N_( "MUX for RAW RTSP transport" )
64 #define SESSION_TIMEOUT_TEXT N_( "Sets the timeout option in the RTSP " \
66 #define SESSION_TIMEOUT_LONGTEXT N_( "Defines what timeout option to add " \
67 "to the RTSP session ID string. Setting it to a negative number removes " \
68 "the timeout option entirely. This is needed by some IPTV STBs (such as " \
69 "those made by HansunTech) which get confused by it. The default is 5." )
72 set_shortname( _("RTSP VoD" ) );
73 set_description( _("RTSP VoD server") );
74 set_category( CAT_SOUT );
75 set_subcategory( SUBCAT_SOUT_VOD );
76 set_capability( "vod server", 1 );
77 set_callbacks( Open, Close );
78 add_shortcut( "rtsp" );
79 add_string ( "rtsp-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
80 add_string( "rtsp-raw-mux", "ts", NULL, RAWMUX_TEXT, RAWMUX_TEXT, VLC_TRUE );
81 add_integer( "rtsp-throttle-users", 0, NULL, THROTLE_TEXT,
82 THROTLE_LONGTEXT, VLC_TRUE );
83 add_integer( "rtsp-session-timeout", 5, NULL, SESSION_TIMEOUT_TEXT,
84 SESSION_TIMEOUT_LONGTEXT, VLC_TRUE );
87 /*****************************************************************************
89 *****************************************************************************/
91 typedef struct media_es_t media_es_t;
95 media_es_t *p_media_es;
104 int64_t i_last; /* for timeout */
106 vlc_bool_t b_playing; /* is it in "play" state */
107 vlc_bool_t b_paused; /* is it in "pause" state */
110 rtsp_client_es_t **es;
120 httpd_url_t *p_rtsp_url;
122 vod_media_t *p_media;
126 uint8_t i_payload_type;
140 httpd_url_t *p_rtsp_url;
141 char *psz_rtsp_control_v4;
142 char *psz_rtsp_control_v6;
154 vlc_bool_t b_multicast;
166 rtsp_client_t **rtsp;
169 char *psz_session_name;
170 char *psz_session_description;
171 char *psz_session_url;
172 char *psz_session_email;
179 httpd_host_t *p_rtsp_host;
182 int i_throttle_users;
187 int i_session_timeout;
190 vlc_mutex_t lock_media;
196 block_fifo_t *p_fifo_cmd;
199 /* rtsp delayed command (to avoid deadlock between vlm/httpd) */
202 RTSP_CMD_TYPE_NONE, /* Exit requested */
208 RTSP_CMD_TYPE_REWIND,
209 RTSP_CMD_TYPE_FORWARD,
212 static vod_media_t *MediaNew( vod_t *, const char *, input_item_t * );
213 static void MediaDel( vod_t *, vod_media_t * );
214 static int MediaAddES( vod_t *, vod_media_t *, es_format_t * );
215 static void MediaDelES( vod_t *, vod_media_t *, es_format_t * );
217 static void CommandThread( vlc_object_t *p_this );
218 static void CommandPush( vod_t *, rtsp_cmd_type_t, vod_media_t *, const char *psz_session,
219 double f_arg, const char *psz_arg );
221 static rtsp_client_t *RtspClientNew( vod_media_t *, char * );
222 static rtsp_client_t *RtspClientGet( vod_media_t *, const char * );
223 static void RtspClientDel( vod_media_t *, rtsp_client_t * );
225 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
226 httpd_message_t *, httpd_message_t * );
227 static int RtspCallbackES( httpd_callback_sys_t *, httpd_client_t *,
228 httpd_message_t *, httpd_message_t * );
230 static char *SDPGenerate( const vod_media_t *, httpd_client_t *cl );
232 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
234 static const char hex[16] = "0123456789abcdef";
237 for( i = 0; i < i_data; i++ )
239 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
240 s[2*i+1] = hex[(p_data[i] )&0xf];
245 /*****************************************************************************
246 * Open: Starts the RTSP server module
247 *****************************************************************************/
248 static int Open( vlc_object_t *p_this )
250 vod_t *p_vod = (vod_t *)p_this;
251 vod_sys_t *p_sys = 0;
255 psz_url = config_GetPsz( p_vod, "rtsp-host" );
256 vlc_UrlParse( &url, psz_url, 0 );
257 if( psz_url ) free( psz_url );
259 if( url.i_port <= 0 ) url.i_port = 554;
261 p_vod->p_sys = p_sys = malloc( sizeof( vod_sys_t ) );
262 if( !p_sys ) goto error;
263 p_sys->p_rtsp_host = 0;
265 p_sys->i_session_timeout = var_CreateGetInteger( p_this, "rtsp-session-timeout" );
267 p_sys->i_throttle_users = var_CreateGetInteger( p_this, "rtsp-throttle-users" );
268 msg_Dbg( p_this, "allowing up to %d connections", p_sys->i_throttle_users );
269 p_sys->i_connections = 0;
271 p_sys->psz_raw_mux = var_CreateGetString( p_this, "rtsp-raw-mux" );
274 httpd_HostNew( VLC_OBJECT(p_vod), url.psz_host, url.i_port );
275 if( !p_sys->p_rtsp_host )
277 msg_Err( p_vod, "cannot create RTSP server (%s:%i)",
278 url.psz_host, url.i_port );
282 p_sys->psz_path = strdup( url.psz_path ? url.psz_path : "/" );
283 p_sys->i_port = url.i_port;
285 vlc_UrlClean( &url );
287 vlc_mutex_init( p_vod, &p_sys->lock_media );
289 TAB_INIT( p_sys->i_media, p_sys->media );
290 p_sys->i_media_id = 0;
292 p_vod->pf_media_new = MediaNew;
293 p_vod->pf_media_del = MediaDel;
294 p_vod->pf_media_add_es = MediaAddES;
295 p_vod->pf_media_del_es = MediaDelES;
297 p_sys->p_fifo_cmd = block_FifoNew( p_vod );
298 if( vlc_thread_create( p_vod, "rtsp vod thread", CommandThread,
299 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
301 msg_Err( p_vod, "cannot spawn rtsp vod thread" );
302 block_FifoRelease( p_sys->p_fifo_cmd );
303 free( p_sys->psz_path );
310 if( p_sys && p_sys->p_rtsp_host ) httpd_HostDelete( p_sys->p_rtsp_host );
311 if( p_sys && p_sys->psz_raw_mux ) free( p_sys->psz_raw_mux );
312 if( p_sys ) free( p_sys );
313 vlc_UrlClean( &url );
318 /*****************************************************************************
320 *****************************************************************************/
321 static void Close( vlc_object_t * p_this )
323 vod_t *p_vod = (vod_t *)p_this;
324 vod_sys_t *p_sys = p_vod->p_sys;
326 /* Stop command thread */
327 p_vod->b_die = VLC_TRUE;
328 CommandPush( p_vod, RTSP_CMD_TYPE_NONE, NULL, NULL, 0.0, NULL );
329 vlc_thread_join( p_vod );
331 block_FifoRelease( p_sys->p_fifo_cmd );
333 httpd_HostDelete( p_sys->p_rtsp_host );
334 var_Destroy( p_this, "rtsp-session-timeout" );
335 var_Destroy( p_this, "rtsp-throttle-users" );
336 var_Destroy( p_this, "rtsp-raw-mux" );
338 /* Check VLM is not buggy */
339 if( p_sys->i_media > 0 )
340 msg_Err( p_vod, "rtsp vod leaking %d medias", p_sys->i_media );
341 TAB_CLEAN( p_sys->i_media, p_sys->media );
343 vlc_mutex_destroy( &p_sys->lock_media );
345 free( p_sys->psz_path );
346 free( p_sys->psz_raw_mux );
350 /*****************************************************************************
352 *****************************************************************************/
353 static vod_media_t *MediaNew( vod_t *p_vod, const char *psz_name,
354 input_item_t *p_item )
356 vod_sys_t *p_sys = p_vod->p_sys;
357 vod_media_t *p_media = malloc( sizeof(vod_media_t) );
362 msg_Err( p_vod, "not enough memory" );
366 memset( p_media, 0, sizeof(vod_media_t) );
367 p_media->id = p_sys->i_media_id++;
368 TAB_INIT( p_media->i_es, p_media->es );
369 p_media->psz_mux = 0;
370 TAB_INIT( p_media->i_rtsp, p_media->rtsp );
371 p_media->b_raw = VLC_FALSE;
373 asprintf( &p_media->psz_rtsp_path, "%s%s", p_sys->psz_path, psz_name );
374 p_media->p_rtsp_url =
375 httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, NULL,
378 if( !p_media->p_rtsp_url )
380 msg_Err( p_vod, "cannot create RTSP url (%s)", p_media->psz_rtsp_path);
381 free( p_media->psz_rtsp_path );
386 msg_Dbg( p_vod, "created RTSP url: %s", p_media->psz_rtsp_path );
388 asprintf( &p_media->psz_rtsp_control_v4,
389 "a=control:rtsp://%%s:%d%s/trackID=%%d\r\n",
390 p_sys->i_port, p_media->psz_rtsp_path );
391 asprintf( &p_media->psz_rtsp_control_v6,
392 "a=control:rtsp://[%%s]:%d%s/trackID=%%d\r\n",
393 p_sys->i_port, p_media->psz_rtsp_path );
395 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_SETUP,
396 RtspCallback, (void*)p_media );
397 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_DESCRIBE,
398 RtspCallback, (void*)p_media );
399 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PLAY,
400 RtspCallback, (void*)p_media );
401 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PAUSE,
402 RtspCallback, (void*)p_media );
403 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_GETPARAMETER,
404 RtspCallback, (void*)p_media );
405 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_TEARDOWN,
406 RtspCallback, (void*)p_media );
408 p_media->p_vod = p_vod;
410 vlc_mutex_lock( &p_sys->lock_media );
411 TAB_APPEND( p_sys->i_media, p_sys->media, p_media );
412 vlc_mutex_unlock( &p_sys->lock_media );
414 vlc_mutex_init( p_vod, &p_media->lock );
415 p_media->psz_session_name = strdup("");
416 p_media->psz_session_description = strdup("");
417 p_media->psz_session_url = strdup("");
418 p_media->psz_session_email = strdup("");
420 p_media->i_port_audio = 1234;
421 p_media->i_port_video = 1236;
422 p_media->i_port = 1238;
423 p_media->i_payload_type = 96;
425 p_media->i_sdp_id = mdate();
426 p_media->i_sdp_version = 1;
427 p_media->i_length = p_item->i_duration;
429 vlc_mutex_lock( &p_item->lock );
430 msg_Dbg( p_vod, "media has %i declared ES", p_item->i_es );
431 for( i = 0; i < p_item->i_es; i++ )
433 MediaAddES( p_vod, p_media, p_item->es[i] );
435 vlc_mutex_unlock( &p_item->lock );
440 static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
442 vod_sys_t *p_sys = p_vod->p_sys;
444 msg_Dbg( p_vod, "deleting media: %s", p_media->psz_rtsp_path );
446 vlc_mutex_lock( &p_sys->lock_media );
447 TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
448 vlc_mutex_unlock( &p_sys->lock_media );
450 while( p_media->i_rtsp > 0 )
451 RtspClientDel( p_media, p_media->rtsp[0] );
452 TAB_CLEAN( p_media->i_rtsp, p_media->rtsp );
453 httpd_UrlDelete( p_media->p_rtsp_url );
454 if( p_media->psz_rtsp_path ) free( p_media->psz_rtsp_path );
455 if( p_media->psz_rtsp_control_v6 ) free( p_media->psz_rtsp_control_v6 );
456 if( p_media->psz_rtsp_control_v4 ) free( p_media->psz_rtsp_control_v4 );
458 while( p_media->i_es )
459 MediaDelES( p_vod, p_media, &p_media->es[0]->fmt );
460 TAB_CLEAN( p_media->i_es, p_media->es );
462 vlc_mutex_destroy( &p_media->lock );
464 if( p_media->psz_session_name ) free( p_media->psz_session_name );
465 if( p_media->psz_session_description ) free( p_media->psz_session_description );
466 if( p_media->psz_session_url ) free( p_media->psz_session_url );
467 if( p_media->psz_session_email ) free( p_media->psz_session_email );
468 if( p_media->psz_mux ) free( p_media->psz_mux );
472 static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
474 media_es_t *p_es = malloc( sizeof(media_es_t) );
477 if( !p_es ) return VLC_ENOMEM;
478 memset( p_es, 0, sizeof(media_es_t) );
480 if( p_media->psz_mux ) free( p_media->psz_mux );
481 p_media->psz_mux = NULL;
483 /* TODO: update SDP, etc... */
484 asprintf( &psz_urlc, "%s/trackID=%d",
485 p_media->psz_rtsp_path, p_media->i_es );
486 msg_Dbg( p_vod, " - ES %4.4s (%s)", (char *)&p_fmt->i_codec, psz_urlc );
488 switch( p_fmt->i_codec )
490 case VLC_FOURCC( 's', '1', '6', 'b' ):
491 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
493 p_es->i_payload_type = 11;
495 else if( p_fmt->audio.i_channels == 2 && p_fmt->audio.i_rate == 44100 )
497 p_es->i_payload_type = 10;
501 p_es->i_payload_type = p_media->i_payload_type++;
503 p_es->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
504 sprintf( p_es->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
505 p_fmt->audio.i_channels );
507 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
508 p_es->i_payload_type = p_media->i_payload_type++;
509 p_es->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
510 sprintf( p_es->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
511 p_fmt->audio.i_channels );
513 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
514 p_es->i_payload_type = 14;
515 p_es->psz_rtpmap = strdup( "MPA/90000" );
517 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
518 p_es->i_payload_type = 32;
519 p_es->psz_rtpmap = strdup( "MPV/90000" );
521 case VLC_FOURCC( 'a', '5', '2', ' ' ):
522 p_es->i_payload_type = p_media->i_payload_type++;
523 p_es->psz_rtpmap = strdup( "ac3/90000" );
525 case VLC_FOURCC( 'H', '2', '6', '3' ):
526 p_es->i_payload_type = p_media->i_payload_type++;
527 p_es->psz_rtpmap = strdup( "H263-1998/90000" );
529 case VLC_FOURCC( 'h', '2', '6', '4' ):
530 p_es->i_payload_type = p_media->i_payload_type++;
531 p_es->psz_rtpmap = strdup( "H264/90000" );
532 p_es->psz_fmtp = NULL;
533 /* FIXME AAAAAAAAAAAARRRRRRRRGGGG copied from stream_out/rtp.c */
534 if( p_fmt->i_extra > 0 )
536 uint8_t *p_buffer = p_fmt->p_extra;
537 int i_buffer = p_fmt->i_extra;
538 char *p_64_sps = NULL;
539 char *p_64_pps = NULL;
542 while( i_buffer > 4 &&
543 p_buffer[0] == 0 && p_buffer[1] == 0 &&
544 p_buffer[2] == 0 && p_buffer[3] == 1 )
546 const int i_nal_type = p_buffer[4]&0x1f;
551 for( i_offset = 4; i_offset+3 < i_buffer ; i_offset++)
553 if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 0 && p_buffer[i_offset+3] == 1 )
555 /* we found another startcode */
560 if( i_nal_type == 7 )
562 p_64_sps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
563 sprintf_hexa( hexa, &p_buffer[5], 3 );
565 else if( i_nal_type == 8 )
567 p_64_pps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
573 if( p_64_sps && p_64_pps )
574 asprintf( &p_es->psz_fmtp, "packetization-mode=1;profile-level-id=%s;sprop-parameter-sets=%s,%s;", hexa, p_64_sps, p_64_pps );
580 if( !p_es->psz_fmtp )
581 p_es->psz_fmtp = strdup( "packetization-mode=1" );
583 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
584 p_es->i_payload_type = p_media->i_payload_type++;
585 p_es->psz_rtpmap = strdup( "MP4V-ES/90000" );
586 if( p_fmt->i_extra > 0 )
588 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
589 p_es->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
590 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
591 sprintf( p_es->psz_fmtp,
592 "profile-level-id=3; config=%s;", p_hexa );
596 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
597 p_es->i_payload_type = p_media->i_payload_type++;
598 p_es->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
599 sprintf( p_es->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
600 if( p_fmt->i_extra > 0 )
602 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
603 p_es->psz_fmtp = malloc( 200 + 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 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
607 "config=%s; SizeLength=13;IndexLength=3; "
608 "IndexDeltaLength=3; Profile=1;", p_hexa );
612 case VLC_FOURCC( 'm', 'p', '2', 't' ):
613 p_media->psz_mux = strdup("ts");
614 p_es->i_payload_type = 33;
615 p_es->psz_rtpmap = strdup( "MP2T/90000" );
617 case VLC_FOURCC( 'm', 'p', '2', 'p' ):
618 p_media->psz_mux = strdup("ps");
619 p_es->i_payload_type = p_media->i_payload_type++;
620 p_es->psz_rtpmap = strdup( "MP2P/90000" );
622 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
623 p_es->i_payload_type = p_media->i_payload_type++;
624 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
625 "AMR/8000/2" : "AMR/8000" );
626 p_es->psz_fmtp = strdup( "octet-align=1" );
628 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
629 p_es->i_payload_type = p_media->i_payload_type++;
630 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
631 "AMR-WB/16000/2" : "AMR-WB/16000" );
632 p_es->psz_fmtp = strdup( "octet-align=1" );
636 msg_Err( p_vod, "cannot add this stream (unsupported "
637 "codec: %4.4s)", (char*)&p_fmt->i_codec );
643 httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, NULL, NULL,
646 if( !p_es->p_rtsp_url )
648 msg_Err( p_vod, "cannot create RTSP url (%s)", psz_urlc );
655 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
656 RtspCallbackES, (void*)p_es );
657 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_TEARDOWN,
658 RtspCallbackES, (void*)p_es );
659 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PLAY,
660 RtspCallbackES, (void*)p_es );
661 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PAUSE,
662 RtspCallbackES, (void*)p_es );
664 es_format_Copy( &p_es->fmt, p_fmt );
666 p_es->p_media = p_media;
669 /* Choose the port */
670 if( p_fmt->i_cat == AUDIO_ES && p_media->i_port_audio > 0 )
672 p_es->i_port = p_media->i_port_audio;
673 p_media->i_port_audio = 0;
675 else if( p_fmt->i_cat == VIDEO_ES && p_media->i_port_video > 0 )
677 p_es->i_port = p_media->i_port_video;
678 p_media->i_port_video = 0;
680 while( !p_es->i_port )
682 if( p_media->i_port != p_media->i_port_audio &&
683 p_media->i_port != p_media->i_port_video )
685 p_es->i_port = p_media->i_port;
686 p_media->i_port += 2;
689 p_media->i_port += 2;
696 vlc_mutex_lock( &p_media->lock );
697 TAB_APPEND( p_media->i_es, p_media->es, p_es );
698 vlc_mutex_unlock( &p_media->lock );
700 p_media->i_sdp_version++;
705 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
707 media_es_t *p_es = NULL;
711 for( i = 0; i < p_media->i_es; i++ )
713 if( p_media->es[i]->fmt.i_cat == p_fmt->i_cat &&
714 p_media->es[i]->fmt.i_codec == p_fmt->i_codec &&
715 p_media->es[i]->fmt.i_id == p_fmt->i_id )
717 p_es = p_media->es[i];
722 msg_Dbg( p_vod, " - Removing ES %4.4s", (char *)&p_fmt->i_codec );
724 vlc_mutex_lock( &p_media->lock );
725 TAB_REMOVE( p_media->i_es, p_media->es, p_es );
726 vlc_mutex_unlock( &p_media->lock );
728 if( p_es->psz_rtpmap ) free( p_es->psz_rtpmap );
729 if( p_es->psz_fmtp ) free( p_es->psz_fmtp );
730 p_media->i_sdp_version++;
732 if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
733 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;
949 answer->psz_status = strdup( "Internal server error" );
951 answer->p_body = NULL;
955 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
958 psz_session = httpd_MsgGet( query, "Session" );
959 if( !psz_session || !*psz_session )
962 if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
963 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
965 answer->i_status = 503;
966 answer->psz_status = strdup( "Too many connections" );
968 answer->p_body = NULL;
971 asprintf( &psz_new, "%d", rand() );
972 psz_session = psz_new;
974 p_rtsp = RtspClientNew( p_media, psz_new );
977 answer->i_status = 454;
978 answer->psz_status = strdup( "Unknown session id" );
980 answer->p_body = NULL;
986 p_rtsp = RtspClientGet( p_media, psz_session );
989 answer->i_status = 454;
990 answer->psz_status = strdup( "Unknown session id" );
992 answer->p_body = NULL;
997 answer->i_status = 200;
998 answer->psz_status = strdup( "OK" );
1000 answer->p_body = NULL;
1002 if( p_media->b_raw )
1004 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
1006 httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
1007 i_port, i_port + 1 );
1009 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1011 httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
1012 i_port, i_port + 1 );
1016 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
1017 i_port, i_port + 1 );
1019 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1021 answer->i_status = 461;
1022 answer->psz_status = strdup( "Unsupported Transport" );
1024 answer->p_body = NULL;
1027 /* Intentional fall-through on x-playNow option in RTSP request */
1032 case HTTPD_MSG_PLAY:
1034 char *psz_output, ip[NI_MAXNUMERICHOST];
1035 int i, i_port_audio = 0, i_port_video = 0;
1037 /* for now only multicast so easy */
1040 answer->i_status = 200;
1041 answer->psz_status = strdup( "OK" );
1043 answer->p_body = NULL;
1047 psz_session = httpd_MsgGet( query, "Session" );
1048 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1050 p_rtsp = RtspClientGet( p_media, psz_session );
1053 answer->i_status = 500;
1054 answer->psz_status = strdup( "Internal server error" );
1056 answer->p_body = NULL;
1060 if( p_rtsp->b_playing )
1062 const char *psz_position = httpd_MsgGet( query, "Range" );
1063 const char *psz_scale = httpd_MsgGet( query, "Scale" );
1064 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
1065 if( psz_position && !psz_scale )
1070 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1072 /* FIXME: npt= is not necessarily formatted as a float */
1073 f_pos = us_strtod( psz_position, &end );
1074 if( end > psz_position )
1076 f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
1077 CommandPush( p_vod, RTSP_CMD_TYPE_SEEK, p_media, psz_session, f_pos, NULL );
1083 double f_scale = 0.0;
1086 f_scale = us_strtod( psz_scale, &end );
1087 if( end > psz_scale )
1089 f_scale = (f_scale * 30.0);
1090 if( psz_scale[0] == '-' ) /* rewind */
1092 msg_Dbg( p_vod, "rewind request: %s", psz_scale );
1093 CommandPush( p_vod, RTSP_CMD_TYPE_REWIND, p_media, psz_session, f_scale, NULL );
1095 else if(psz_scale[0] != '1' ) /* fast-forward */
1097 msg_Dbg( p_vod, "fastforward request: %s", psz_scale );
1098 CommandPush( p_vod, RTSP_CMD_TYPE_FORWARD, p_media, psz_session, f_scale, NULL );
1101 if( p_rtsp->b_paused == VLC_TRUE )
1103 p_rtsp->b_paused = VLC_FALSE;
1104 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media, psz_session, 0, NULL );
1111 if( p_rtsp->b_playing && p_rtsp->b_paused )
1113 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media, psz_session, 0, NULL );
1114 p_rtsp->b_paused = VLC_FALSE;
1117 else if( p_rtsp->b_playing ) break;
1119 if( httpd_ClientIP( cl, ip ) == NULL ) break;
1121 p_rtsp->b_playing = VLC_TRUE;
1123 /* FIXME for != 1 video and 1 audio */
1124 for( i = 0; i < p_rtsp->i_es; i++ )
1126 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == AUDIO_ES )
1127 i_port_audio = p_rtsp->es[i]->i_port;
1128 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == VIDEO_ES )
1129 i_port_video = p_rtsp->es[i]->i_port;
1132 if( p_media->psz_mux )
1134 if( p_media->b_raw )
1136 asprintf( &psz_output, "std{access=udp,dst=%s:%i,mux=%s}",
1137 ip, i_port, p_media->psz_mux );
1141 asprintf( &psz_output, "rtp{dst=%s,port=%i,mux=%s}",
1142 ip, i_port_video, p_media->psz_mux );
1147 asprintf( &psz_output, "rtp{dst=%s,port-video=%i,"
1148 "port-audio=%i}", ip, i_port_video, i_port_audio );
1151 CommandPush( p_vod, RTSP_CMD_TYPE_PLAY, p_media, psz_session, 0, psz_output );
1156 case HTTPD_MSG_DESCRIBE:
1159 SDPGenerate( p_media, cl );
1161 if( psz_sdp != NULL )
1163 answer->i_status = 200;
1164 answer->psz_status = strdup( "OK" );
1165 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
1167 answer->p_body = (uint8_t *)psz_sdp;
1168 answer->i_body = strlen( psz_sdp );
1172 answer->i_status = 500;
1173 answer->psz_status = strdup( "Internal server error" );
1174 answer->p_body = NULL;
1180 case HTTPD_MSG_PAUSE:
1181 psz_session = httpd_MsgGet( query, "Session" );
1182 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1184 p_rtsp = RtspClientGet( p_media, psz_session );
1185 if( !p_rtsp ) break;
1187 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media, psz_session, 0, NULL );
1188 p_rtsp->b_paused = VLC_TRUE;
1190 answer->i_status = 200;
1191 answer->psz_status = strdup( "OK" );
1193 answer->p_body = NULL;
1196 case HTTPD_MSG_TEARDOWN:
1197 /* for now only multicast so easy again */
1198 answer->i_status = 200;
1199 answer->psz_status = strdup( "OK" );
1201 answer->p_body = NULL;
1203 psz_session = httpd_MsgGet( query, "Session" );
1204 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1206 p_rtsp = RtspClientGet( p_media, psz_session );
1207 if( !p_rtsp ) break;
1209 CommandPush( p_vod, RTSP_CMD_TYPE_STOP, p_media, psz_session, 0, NULL );
1210 RtspClientDel( p_media, p_rtsp );
1213 case HTTPD_MSG_GETPARAMETER:
1214 answer->i_status = 200;
1215 answer->psz_status = strdup( "OK" );
1217 answer->p_body = NULL;
1221 return VLC_EGENERIC;
1224 httpd_MsgAdd( answer, "Server", "VLC Server" );
1225 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1226 psz_cseq = httpd_MsgGet( query, "Cseq" );
1227 psz_cseq ? i_cseq = atoi( psz_cseq ) : 0;
1228 httpd_MsgAdd( answer, "CSeq", "%d", i_cseq );
1229 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1233 if( p_media->p_vod->p_sys->i_session_timeout >= 0 )
1234 httpd_MsgAdd( answer, "Session", "%s;timeout=%i", psz_session,
1235 p_media->p_vod->p_sys->i_session_timeout );
1237 httpd_MsgAdd( answer, "Session", "%s", psz_session );
1243 static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
1244 httpd_message_t *answer, httpd_message_t *query )
1246 media_es_t *p_es = (media_es_t*)p_args;
1247 vod_media_t *p_media = p_es->p_media;
1248 vod_t *p_vod = p_media->p_vod;
1249 rtsp_client_t *p_rtsp = NULL;
1250 const char *psz_transport = NULL;
1251 const char *psz_playnow = NULL; /* support option: x-playNow */
1252 const char *psz_session = NULL;
1253 const char *psz_position = NULL;
1254 const char *psz_cseq = NULL;
1258 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
1260 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
1262 answer->i_proto = HTTPD_PROTO_RTSP;
1263 answer->i_version = query->i_version;
1264 answer->i_type = HTTPD_MSG_ANSWER;
1266 answer->p_body = NULL;
1268 switch( query->i_type )
1270 case HTTPD_MSG_SETUP:
1271 psz_playnow = httpd_MsgGet( query, "x-playNow" );
1272 psz_transport = httpd_MsgGet( query, "Transport" );
1274 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
1276 if( strstr( psz_transport, "unicast" ) &&
1277 strstr( psz_transport, "client_port=" ) )
1279 rtsp_client_t *p_rtsp = NULL;
1280 rtsp_client_es_t *p_rtsp_es = NULL;
1281 char ip[NI_MAXNUMERICHOST];
1282 int i_port = atoi( strstr( psz_transport, "client_port=" ) +
1283 strlen("client_port=") );
1285 if( httpd_ClientIP( cl, ip ) == NULL )
1287 answer->i_status = 500;
1288 answer->psz_status = strdup( "Internal server error" );
1290 answer->p_body = NULL;
1294 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
1297 psz_session = httpd_MsgGet( query, "Session" );
1298 if( !psz_session || !*psz_session )
1301 if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
1302 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
1304 answer->i_status = 503;
1305 answer->psz_status = strdup( "Too many connections" );
1307 answer->p_body = NULL;
1310 asprintf( &psz_new, "%d", rand() );
1311 psz_session = psz_new;
1313 p_rtsp = RtspClientNew( p_media, psz_new );
1317 p_rtsp = RtspClientGet( p_media, psz_session );
1320 answer->i_status = 454;
1321 answer->psz_status = strdup( "Unknown session id" );
1323 answer->p_body = NULL;
1328 p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
1331 answer->i_status = 500;
1332 answer->psz_status = strdup( "Internal server error" );
1334 answer->p_body = NULL;
1337 p_rtsp_es->i_port = i_port;
1338 p_rtsp_es->psz_ip = strdup( ip );
1339 p_rtsp_es->p_media_es = p_es;
1340 TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );
1342 answer->i_status = 200;
1343 answer->psz_status = strdup( "OK" );
1345 answer->p_body = NULL;
1347 if( p_media->b_raw )
1349 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
1351 httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
1352 p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1354 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1356 httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
1357 p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1362 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
1363 p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1366 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1368 answer->i_status = 461;
1369 answer->psz_status = strdup( "Unsupported Transport" );
1371 answer->p_body = NULL;
1374 /* Intentional fall-through on x-playNow option in RTSP request */
1378 case HTTPD_MSG_PLAY:
1379 /* This is kind of a kludge. Should we only support Aggregate
1381 psz_session = httpd_MsgGet( query, "Session" );
1382 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1384 p_rtsp = RtspClientGet( p_media, psz_session );
1386 psz_position = httpd_MsgGet( query, "Range" );
1387 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
1393 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1396 /* FIXME: npt= is not necessarily formatted as a float */
1397 f_pos = us_strtod( psz_position, &end );
1398 if( end > psz_position )
1400 f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
1401 CommandPush( p_vod, RTSP_CMD_TYPE_SEEK, p_media, psz_session, f_pos, NULL );
1407 answer->i_status = 200;
1408 answer->psz_status = strdup( "OK" );
1410 answer->p_body = NULL;
1414 case HTTPD_MSG_TEARDOWN:
1415 answer->i_status = 200;
1416 answer->psz_status = strdup( "OK" );
1418 answer->p_body = NULL;
1420 psz_session = httpd_MsgGet( query, "Session" );
1421 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1423 p_rtsp = RtspClientGet( p_media, psz_session );
1424 if( !p_rtsp ) break;
1426 for( i = 0; i < p_rtsp->i_es; i++ )
1428 if( p_rtsp->es[i]->p_media_es == p_es )
1430 if( p_rtsp->es[i]->psz_ip ) free( p_rtsp->es[i]->psz_ip );
1431 TAB_REMOVE( p_rtsp->i_es, p_rtsp->es, p_rtsp->es[i] );
1438 CommandPush( p_vod, RTSP_CMD_TYPE_STOP, p_media, psz_session, 0, NULL );
1439 RtspClientDel( p_media, p_rtsp );
1443 case HTTPD_MSG_PAUSE:
1444 /* This is kind of a kludge. Should we only support Aggregate
1446 psz_session = httpd_MsgGet( query, "Session" );
1447 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1449 p_rtsp = RtspClientGet( p_media, psz_session );
1450 if( !p_rtsp ) break;
1452 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media, psz_session, 0, NULL );
1453 p_rtsp->b_paused = VLC_TRUE;
1455 answer->i_status = 200;
1456 answer->psz_status = strdup( "OK" );
1458 answer->p_body = NULL;
1462 return VLC_EGENERIC;
1466 httpd_MsgAdd( answer, "Server", "VLC Server" );
1467 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1468 psz_cseq = httpd_MsgGet( query, "Cseq" );
1470 i_cseq = atoi( psz_cseq );
1473 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1474 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1477 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1482 /*****************************************************************************
1484 * FIXME: need to be moved to a common place ?
1485 *****************************************************************************/
1486 static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
1489 char *p, *psz_sdp, ip[NI_MAXNUMERICHOST], ipv;
1490 const char *psz_control;
1492 if( httpd_ServerIP( cl, ip ) == NULL )
1495 p = strchr( ip, '%' );
1497 *p = '\0'; /* remove scope if present */
1499 ipv = ( strchr( ip, ':' ) != NULL ) ? '6' : '4';
1501 /* Calculate size */
1502 i_size = sizeof( "v=0\r\n" ) +
1503 sizeof( "o=- * * IN IP4 \r\n" ) + 10 + NI_MAXNUMERICHOST +
1504 sizeof( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
1505 sizeof( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
1506 sizeof( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
1507 sizeof( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
1508 sizeof( "c=IN IP4 0.0.0.0\r\n" ) + 20 + 10 +
1509 sizeof( "t=0 0\r\n" ) + /* FIXME */
1510 sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
1511 sizeof( "a=range:npt=0-1000000000.000\r\n" );
1513 psz_control = (ipv == '6') ? p_media->psz_rtsp_control_v6
1514 : p_media->psz_rtsp_control_v4;
1515 for( i = 0; i < p_media->i_es; i++ )
1517 media_es_t *p_es = p_media->es[i];
1519 i_size += sizeof( "m=**d*o * RTP/AVP *\r\n" ) + 19;
1520 if( p_es->psz_rtpmap )
1522 i_size += sizeof( "a=rtpmap:* *\r\n" ) +
1523 strlen( p_es->psz_rtpmap ) + 9;
1525 if( p_es->psz_fmtp )
1527 i_size += sizeof( "a=fmtp:* *\r\n" ) +
1528 strlen( p_es->psz_fmtp ) + 9;
1531 i_size += (strlen( psz_control ) + strlen( ip ) + 9) * p_media->i_es;
1533 p = psz_sdp = malloc( i_size );
1534 p += sprintf( p, "v=0\r\n" );
1535 p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
1536 p_media->i_sdp_id, p_media->i_sdp_version, ipv, ip );
1537 if( *p_media->psz_session_name )
1538 p += sprintf( p, "s=%s\r\n", p_media->psz_session_name );
1539 if( *p_media->psz_session_description )
1540 p += sprintf( p, "i=%s\r\n", p_media->psz_session_description );
1541 if( *p_media->psz_session_url )
1542 p += sprintf( p, "u=%s\r\n", p_media->psz_session_url );
1543 if( *p_media->psz_session_email )
1544 p += sprintf( p, "e=%s\r\n", p_media->psz_session_email );
1546 p += sprintf( p, "c=IN IP%c %s\r\n", ipv, ipv == '6' ? "::" : "0.0.0.0" );
1547 p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
1548 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
1550 if( p_media->i_length > 0 )
1552 lldiv_t d = lldiv( p_media->i_length / 1000, 1000 );
1553 p += sprintf( p, "a=range:npt=0-%lld.%03u\r\n", d.quot,
1557 for( i = 0; i < p_media->i_es; i++ )
1559 media_es_t *p_es = p_media->es[i];
1561 if( p_es->fmt.i_cat == AUDIO_ES )
1563 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
1564 p_es->i_port, p_es->i_payload_type );
1566 else if( p_es->fmt.i_cat == VIDEO_ES )
1568 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
1569 p_es->i_port, p_es->i_payload_type );
1576 if( p_es->psz_rtpmap )
1578 p += sprintf( p, "a=rtpmap:%d %s\r\n", p_es->i_payload_type,
1581 if( p_es->psz_fmtp )
1583 p += sprintf( p, "a=fmtp:%d %s\r\n", p_es->i_payload_type,
1587 p += sprintf( p, psz_control, ip, i );