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>
43 #include <vlc_strings.h>
45 /*****************************************************************************
47 *****************************************************************************/
48 static int Open ( vlc_object_t * );
49 static void Close( vlc_object_t * );
51 #define HOST_TEXT N_( "RTSP host address" )
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.\nTo 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 vlc_object_kill( p_vod );
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 );
454 httpd_UrlDelete( p_media->p_rtsp_url );
455 if( p_media->psz_rtsp_path ) free( p_media->psz_rtsp_path );
456 if( p_media->psz_rtsp_control_v6 ) free( p_media->psz_rtsp_control_v6 );
457 if( p_media->psz_rtsp_control_v4 ) free( p_media->psz_rtsp_control_v4 );
459 while( p_media->i_es )
460 MediaDelES( p_vod, p_media, &p_media->es[0]->fmt );
461 TAB_CLEAN( p_media->i_es, p_media->es );
463 vlc_mutex_destroy( &p_media->lock );
465 if( p_media->psz_session_name ) free( p_media->psz_session_name );
466 if( p_media->psz_session_description ) free( p_media->psz_session_description );
467 if( p_media->psz_session_url ) free( p_media->psz_session_url );
468 if( p_media->psz_session_email ) free( p_media->psz_session_email );
469 if( p_media->psz_mux ) free( p_media->psz_mux );
473 static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
475 media_es_t *p_es = malloc( sizeof(media_es_t) );
478 if( !p_es ) return VLC_ENOMEM;
479 memset( p_es, 0, sizeof(media_es_t) );
481 if( p_media->psz_mux ) free( p_media->psz_mux );
482 p_media->psz_mux = NULL;
484 /* TODO: update SDP, etc... */
485 asprintf( &psz_urlc, "%s/trackID=%d",
486 p_media->psz_rtsp_path, p_media->i_es );
487 msg_Dbg( p_vod, " - ES %4.4s (%s)", (char *)&p_fmt->i_codec, psz_urlc );
489 switch( p_fmt->i_codec )
491 case VLC_FOURCC( 's', '1', '6', 'b' ):
492 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
494 p_es->i_payload_type = 11;
496 else if( p_fmt->audio.i_channels == 2 && p_fmt->audio.i_rate == 44100 )
498 p_es->i_payload_type = 10;
502 p_es->i_payload_type = p_media->i_payload_type++;
504 p_es->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
505 sprintf( p_es->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
506 p_fmt->audio.i_channels );
508 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
509 p_es->i_payload_type = p_media->i_payload_type++;
510 p_es->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
511 sprintf( p_es->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
512 p_fmt->audio.i_channels );
514 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
515 p_es->i_payload_type = 14;
516 p_es->psz_rtpmap = strdup( "MPA/90000" );
518 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
519 p_es->i_payload_type = 32;
520 p_es->psz_rtpmap = strdup( "MPV/90000" );
522 case VLC_FOURCC( 'a', '5', '2', ' ' ):
523 p_es->i_payload_type = p_media->i_payload_type++;
524 p_es->psz_rtpmap = strdup( "ac3/90000" );
526 case VLC_FOURCC( 'H', '2', '6', '3' ):
527 p_es->i_payload_type = p_media->i_payload_type++;
528 p_es->psz_rtpmap = strdup( "H263-1998/90000" );
530 case VLC_FOURCC( 'h', '2', '6', '4' ):
531 p_es->i_payload_type = p_media->i_payload_type++;
532 p_es->psz_rtpmap = strdup( "H264/90000" );
533 p_es->psz_fmtp = NULL;
534 /* FIXME AAAAAAAAAAAARRRRRRRRGGGG copied from stream_out/rtp.c */
535 if( p_fmt->i_extra > 0 )
537 uint8_t *p_buffer = p_fmt->p_extra;
538 int i_buffer = p_fmt->i_extra;
539 char *p_64_sps = NULL;
540 char *p_64_pps = NULL;
543 while( i_buffer > 4 &&
544 p_buffer[0] == 0 && p_buffer[1] == 0 &&
545 p_buffer[2] == 0 && p_buffer[3] == 1 )
547 const int i_nal_type = p_buffer[4]&0x1f;
552 for( i_offset = 4; i_offset+3 < i_buffer ; i_offset++)
554 if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 0 && p_buffer[i_offset+3] == 1 )
556 /* we found another startcode */
561 if( i_nal_type == 7 )
563 p_64_sps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
564 sprintf_hexa( hexa, &p_buffer[5], 3 );
566 else if( i_nal_type == 8 )
568 p_64_pps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
574 if( p_64_sps && p_64_pps )
575 asprintf( &p_es->psz_fmtp, "packetization-mode=1;profile-level-id=%s;sprop-parameter-sets=%s,%s;", hexa, p_64_sps, p_64_pps );
581 if( !p_es->psz_fmtp )
582 p_es->psz_fmtp = strdup( "packetization-mode=1" );
584 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
585 p_es->i_payload_type = p_media->i_payload_type++;
586 p_es->psz_rtpmap = strdup( "MP4V-ES/90000" );
587 if( p_fmt->i_extra > 0 )
589 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
590 p_es->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
591 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
592 sprintf( p_es->psz_fmtp,
593 "profile-level-id=3; config=%s;", p_hexa );
597 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
598 p_es->i_payload_type = p_media->i_payload_type++;
599 p_es->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
600 sprintf( p_es->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
601 if( p_fmt->i_extra > 0 )
603 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
604 p_es->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
605 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
606 sprintf( p_es->psz_fmtp,
607 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
608 "config=%s; SizeLength=13;IndexLength=3; "
609 "IndexDeltaLength=3; Profile=1;", p_hexa );
613 case VLC_FOURCC( 'm', 'p', '2', 't' ):
614 p_media->psz_mux = strdup("ts");
615 p_es->i_payload_type = 33;
616 p_es->psz_rtpmap = strdup( "MP2T/90000" );
618 case VLC_FOURCC( 'm', 'p', '2', 'p' ):
619 p_media->psz_mux = strdup("ps");
620 p_es->i_payload_type = p_media->i_payload_type++;
621 p_es->psz_rtpmap = strdup( "MP2P/90000" );
623 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
624 p_es->i_payload_type = p_media->i_payload_type++;
625 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
626 "AMR/8000/2" : "AMR/8000" );
627 p_es->psz_fmtp = strdup( "octet-align=1" );
629 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
630 p_es->i_payload_type = p_media->i_payload_type++;
631 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
632 "AMR-WB/16000/2" : "AMR-WB/16000" );
633 p_es->psz_fmtp = strdup( "octet-align=1" );
637 msg_Err( p_vod, "cannot add this stream (unsupported "
638 "codec: %4.4s)", (char*)&p_fmt->i_codec );
644 httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, NULL, NULL,
647 if( !p_es->p_rtsp_url )
649 msg_Err( p_vod, "cannot create RTSP url (%s)", psz_urlc );
656 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
657 RtspCallbackES, (void*)p_es );
658 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_TEARDOWN,
659 RtspCallbackES, (void*)p_es );
660 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PLAY,
661 RtspCallbackES, (void*)p_es );
662 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PAUSE,
663 RtspCallbackES, (void*)p_es );
665 es_format_Copy( &p_es->fmt, p_fmt );
667 p_es->p_media = p_media;
670 /* Choose the port */
671 if( p_fmt->i_cat == AUDIO_ES && p_media->i_port_audio > 0 )
673 p_es->i_port = p_media->i_port_audio;
674 p_media->i_port_audio = 0;
676 else if( p_fmt->i_cat == VIDEO_ES && p_media->i_port_video > 0 )
678 p_es->i_port = p_media->i_port_video;
679 p_media->i_port_video = 0;
681 while( !p_es->i_port )
683 if( p_media->i_port != p_media->i_port_audio &&
684 p_media->i_port != p_media->i_port_video )
686 p_es->i_port = p_media->i_port;
687 p_media->i_port += 2;
690 p_media->i_port += 2;
697 vlc_mutex_lock( &p_media->lock );
698 TAB_APPEND( p_media->i_es, p_media->es, p_es );
699 vlc_mutex_unlock( &p_media->lock );
701 p_media->i_sdp_version++;
706 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
708 media_es_t *p_es = NULL;
712 for( i = 0; i < p_media->i_es; i++ )
714 if( p_media->es[i]->fmt.i_cat == p_fmt->i_cat &&
715 p_media->es[i]->fmt.i_codec == p_fmt->i_codec &&
716 p_media->es[i]->fmt.i_id == p_fmt->i_id )
718 p_es = p_media->es[i];
723 msg_Dbg( p_vod, " - Removing ES %4.4s", (char *)&p_fmt->i_codec );
725 vlc_mutex_lock( &p_media->lock );
726 TAB_REMOVE( p_media->i_es, p_media->es, p_es );
727 vlc_mutex_unlock( &p_media->lock );
729 if( p_es->psz_rtpmap ) free( p_es->psz_rtpmap );
730 if( p_es->psz_fmtp ) free( p_es->psz_fmtp );
731 p_media->i_sdp_version++;
733 if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
734 es_format_Clean( &p_es->fmt );
743 //vod_media_t *p_media;
749 static void CommandPush( vod_t *p_vod, rtsp_cmd_type_t i_type, vod_media_t *p_media, const char *psz_session,
750 double f_arg, const char *psz_arg )
755 memset( &cmd, 0, sizeof(cmd) );
758 cmd.i_media_id = p_media->id;
760 cmd.psz_session = strdup(psz_session);
763 cmd.psz_arg = strdup(psz_arg);
765 p_cmd = block_New( p_vod, sizeof(rtsp_cmd_t) );
766 memcpy( p_cmd->p_buffer, &cmd, sizeof(cmd) );
768 block_FifoPut( p_vod->p_sys->p_fifo_cmd, p_cmd );
771 static void CommandThread( vlc_object_t *p_this )
773 vod_t *p_vod = (vod_t*)p_this;
774 vod_sys_t *p_sys = p_vod->p_sys;
776 while( !p_vod->b_die )
778 block_t *p_block_cmd = block_FifoGet( p_sys->p_fifo_cmd );
780 vod_media_t *p_media = NULL;
786 memcpy( &cmd, p_block_cmd->p_buffer, sizeof(cmd) );
787 block_Release( p_block_cmd );
789 if( cmd.i_type == RTSP_CMD_TYPE_NONE )
793 vlc_mutex_lock( &p_sys->lock_media );
794 for( i = 0; i < p_sys->i_media; i++ )
796 if( p_sys->media[i]->id == cmd.i_media_id )
799 if( i >= p_sys->i_media )
801 p_media = p_sys->media[i];
805 case RTSP_CMD_TYPE_PLAY:
806 vod_MediaControl( p_vod, p_media, cmd.psz_session,
807 VOD_MEDIA_PLAY, cmd.psz_arg );
809 case RTSP_CMD_TYPE_PAUSE:
810 vod_MediaControl( p_vod, p_media, cmd.psz_session,
814 case RTSP_CMD_TYPE_STOP:
815 vod_MediaControl( p_vod, p_media, cmd.psz_session, VOD_MEDIA_STOP );
818 case RTSP_CMD_TYPE_SEEK:
819 vod_MediaControl( p_vod, p_media, cmd.psz_session,
820 VOD_MEDIA_SEEK, cmd.f_arg );
823 case RTSP_CMD_TYPE_REWIND:
824 vod_MediaControl( p_vod, p_media, cmd.psz_session,
825 VOD_MEDIA_REWIND, cmd.f_arg );
828 case RTSP_CMD_TYPE_FORWARD:
829 vod_MediaControl( p_vod, p_media, cmd.psz_session,
830 VOD_MEDIA_FORWARD, cmd.f_arg );
838 vlc_mutex_unlock( &p_sys->lock_media );
839 if( cmd.psz_session )
840 free( cmd.psz_session );
846 /****************************************************************************
847 * RTSP server implementation
848 ****************************************************************************/
849 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
851 rtsp_client_t *p_rtsp = malloc( sizeof(rtsp_client_t) );
853 if( !p_rtsp ) return NULL;
854 memset( p_rtsp, 0, sizeof(rtsp_client_t) );
857 p_rtsp->psz_session = psz_session;
858 TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
860 p_media->p_vod->p_sys->i_connections++;
861 msg_Dbg( p_media->p_vod, "new session: %s, connections: %d",
862 psz_session, p_media->p_vod->p_sys->i_throttle_users );
867 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, const char *psz_session )
871 for( i = 0; psz_session && i < p_media->i_rtsp; i++ )
873 if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
874 return p_media->rtsp[i];
880 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
882 p_media->p_vod->p_sys->i_connections--;
883 msg_Dbg( p_media->p_vod, "closing session: %s, connections: %d",
884 p_rtsp->psz_session, p_media->p_vod->p_sys->i_throttle_users );
886 while( p_rtsp->i_es-- )
888 if( p_rtsp->es[p_rtsp->i_es]->psz_ip )
889 free( p_rtsp->es[p_rtsp->i_es]->psz_ip );
890 free( p_rtsp->es[p_rtsp->i_es] );
891 if( !p_rtsp->i_es ) free( p_rtsp->es );
894 TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, p_rtsp );
896 free( p_rtsp->psz_session );
900 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
901 httpd_message_t *answer, httpd_message_t *query )
903 vod_media_t *p_media = (vod_media_t*)p_args;
904 vod_t *p_vod = p_media->p_vod;
905 const char *psz_transport = NULL;
906 const char *psz_playnow = NULL; /* support option: x-playNow */
907 const char *psz_session = NULL;
908 const char *psz_cseq = NULL;
909 rtsp_client_t *p_rtsp;
913 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
915 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
917 answer->i_proto = HTTPD_PROTO_RTSP;
918 answer->i_version = query->i_version;
919 answer->i_type = HTTPD_MSG_ANSWER;
921 answer->p_body = NULL;
923 switch( query->i_type )
925 case HTTPD_MSG_SETUP:
927 psz_playnow = httpd_MsgGet( query, "x-playNow" );
928 psz_transport = httpd_MsgGet( query, "Transport" );
929 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
931 if( strstr( psz_transport, "unicast" ) &&
932 strstr( psz_transport, "client_port=" ) )
934 rtsp_client_t *p_rtsp = NULL;
935 char ip[NI_MAXNUMERICHOST];
936 i_port = atoi( strstr( psz_transport, "client_port=" ) +
937 strlen("client_port=") );
939 if( strstr( psz_transport, "MP2T/H2221/UDP" ) ||
940 strstr( psz_transport, "RAW/RAW/UDP" ) )
942 if( p_media->psz_mux ) free( p_media->psz_mux );
943 p_media->psz_mux = NULL;
944 p_media->psz_mux = strdup( p_vod->p_sys->psz_raw_mux );
945 p_media->b_raw = VLC_TRUE;
948 if( httpd_ClientIP( cl, ip ) == NULL )
950 answer->i_status = 500;
951 answer->psz_status = strdup( "Internal server error" );
953 answer->p_body = NULL;
957 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
960 psz_session = httpd_MsgGet( query, "Session" );
961 if( !psz_session || !*psz_session )
964 if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
965 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
967 answer->i_status = 503;
968 answer->psz_status = strdup( "Too many connections" );
970 answer->p_body = NULL;
973 asprintf( &psz_new, "%d", rand() );
974 psz_session = psz_new;
976 p_rtsp = RtspClientNew( p_media, psz_new );
979 answer->i_status = 454;
980 answer->psz_status = strdup( "Unknown session id" );
982 answer->p_body = NULL;
988 p_rtsp = RtspClientGet( p_media, psz_session );
991 answer->i_status = 454;
992 answer->psz_status = strdup( "Unknown session id" );
994 answer->p_body = NULL;
999 answer->i_status = 200;
1000 answer->psz_status = strdup( "OK" );
1002 answer->p_body = NULL;
1004 if( p_media->b_raw )
1006 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
1008 httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
1009 i_port, i_port + 1 );
1011 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1013 httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
1014 i_port, i_port + 1 );
1018 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
1019 i_port, i_port + 1 );
1021 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1023 answer->i_status = 461;
1024 answer->psz_status = strdup( "Unsupported Transport" );
1026 answer->p_body = NULL;
1029 /* Intentional fall-through on x-playNow option in RTSP request */
1034 case HTTPD_MSG_PLAY:
1036 char *psz_output, ip[NI_MAXNUMERICHOST];
1037 int i, i_port_audio = 0, i_port_video = 0;
1039 /* for now only multicast so easy */
1042 answer->i_status = 200;
1043 answer->psz_status = strdup( "OK" );
1045 answer->p_body = NULL;
1049 psz_session = httpd_MsgGet( query, "Session" );
1050 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1052 p_rtsp = RtspClientGet( p_media, psz_session );
1055 answer->i_status = 500;
1056 answer->psz_status = strdup( "Internal server error" );
1058 answer->p_body = NULL;
1062 if( p_rtsp->b_playing )
1064 const char *psz_position = httpd_MsgGet( query, "Range" );
1065 const char *psz_scale = httpd_MsgGet( query, "Scale" );
1066 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
1067 if( psz_position && !psz_scale )
1072 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1074 /* FIXME: npt= is not necessarily formatted as a float */
1075 f_pos = us_strtod( psz_position, &end );
1076 if( end > psz_position )
1078 f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
1079 CommandPush( p_vod, RTSP_CMD_TYPE_SEEK, p_media, psz_session, f_pos, NULL );
1085 double f_scale = 0.0;
1088 f_scale = us_strtod( psz_scale, &end );
1089 if( end > psz_scale )
1091 f_scale = (f_scale * 30.0);
1092 if( psz_scale[0] == '-' ) /* rewind */
1094 msg_Dbg( p_vod, "rewind request: %s", psz_scale );
1095 CommandPush( p_vod, RTSP_CMD_TYPE_REWIND, p_media, psz_session, f_scale, NULL );
1097 else if(psz_scale[0] != '1' ) /* fast-forward */
1099 msg_Dbg( p_vod, "fastforward request: %s", psz_scale );
1100 CommandPush( p_vod, RTSP_CMD_TYPE_FORWARD, p_media, psz_session, f_scale, NULL );
1103 if( p_rtsp->b_paused == VLC_TRUE )
1105 p_rtsp->b_paused = VLC_FALSE;
1106 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media, psz_session, 0, NULL );
1113 if( p_rtsp->b_playing && p_rtsp->b_paused )
1115 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media, psz_session, 0, NULL );
1116 p_rtsp->b_paused = VLC_FALSE;
1119 else if( p_rtsp->b_playing ) break;
1121 if( httpd_ClientIP( cl, ip ) == NULL ) break;
1123 p_rtsp->b_playing = VLC_TRUE;
1125 /* FIXME for != 1 video and 1 audio */
1126 for( i = 0; i < p_rtsp->i_es; i++ )
1128 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == AUDIO_ES )
1129 i_port_audio = p_rtsp->es[i]->i_port;
1130 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == VIDEO_ES )
1131 i_port_video = p_rtsp->es[i]->i_port;
1134 if( p_media->psz_mux )
1136 if( p_media->b_raw )
1138 asprintf( &psz_output, "std{access=udp,dst=%s:%i,mux=%s}",
1139 ip, i_port, p_media->psz_mux );
1143 asprintf( &psz_output, "rtp{dst=%s,port=%i,mux=%s}",
1144 ip, i_port_video, p_media->psz_mux );
1149 asprintf( &psz_output, "rtp{dst=%s,port-video=%i,"
1150 "port-audio=%i}", ip, i_port_video, i_port_audio );
1153 CommandPush( p_vod, RTSP_CMD_TYPE_PLAY, p_media, psz_session, 0, psz_output );
1158 case HTTPD_MSG_DESCRIBE:
1161 SDPGenerate( p_media, cl );
1163 if( psz_sdp != NULL )
1165 answer->i_status = 200;
1166 answer->psz_status = strdup( "OK" );
1167 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
1169 answer->p_body = (uint8_t *)psz_sdp;
1170 answer->i_body = strlen( psz_sdp );
1174 answer->i_status = 500;
1175 answer->psz_status = strdup( "Internal server error" );
1176 answer->p_body = NULL;
1182 case HTTPD_MSG_PAUSE:
1183 psz_session = httpd_MsgGet( query, "Session" );
1184 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1186 p_rtsp = RtspClientGet( p_media, psz_session );
1187 if( !p_rtsp ) break;
1189 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media, psz_session, 0, NULL );
1190 p_rtsp->b_paused = VLC_TRUE;
1192 answer->i_status = 200;
1193 answer->psz_status = strdup( "OK" );
1195 answer->p_body = NULL;
1198 case HTTPD_MSG_TEARDOWN:
1199 /* for now only multicast so easy again */
1200 answer->i_status = 200;
1201 answer->psz_status = strdup( "OK" );
1203 answer->p_body = NULL;
1205 psz_session = httpd_MsgGet( query, "Session" );
1206 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1208 p_rtsp = RtspClientGet( p_media, psz_session );
1209 if( !p_rtsp ) break;
1211 CommandPush( p_vod, RTSP_CMD_TYPE_STOP, p_media, psz_session, 0, NULL );
1212 RtspClientDel( p_media, p_rtsp );
1215 case HTTPD_MSG_GETPARAMETER:
1216 answer->i_status = 200;
1217 answer->psz_status = strdup( "OK" );
1219 answer->p_body = NULL;
1223 return VLC_EGENERIC;
1226 httpd_MsgAdd( answer, "Server", "VLC Server" );
1227 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1228 psz_cseq = httpd_MsgGet( query, "Cseq" );
1229 psz_cseq ? i_cseq = atoi( psz_cseq ) : 0;
1230 httpd_MsgAdd( answer, "CSeq", "%d", i_cseq );
1231 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1235 if( p_media->p_vod->p_sys->i_session_timeout >= 0 )
1236 httpd_MsgAdd( answer, "Session", "%s;timeout=%i", psz_session,
1237 p_media->p_vod->p_sys->i_session_timeout );
1239 httpd_MsgAdd( answer, "Session", "%s", psz_session );
1245 static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
1246 httpd_message_t *answer, httpd_message_t *query )
1248 media_es_t *p_es = (media_es_t*)p_args;
1249 vod_media_t *p_media = p_es->p_media;
1250 vod_t *p_vod = p_media->p_vod;
1251 rtsp_client_t *p_rtsp = NULL;
1252 const char *psz_transport = NULL;
1253 const char *psz_playnow = NULL; /* support option: x-playNow */
1254 const char *psz_session = NULL;
1255 const char *psz_position = NULL;
1256 const char *psz_cseq = NULL;
1260 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
1262 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
1264 answer->i_proto = HTTPD_PROTO_RTSP;
1265 answer->i_version = query->i_version;
1266 answer->i_type = HTTPD_MSG_ANSWER;
1268 answer->p_body = NULL;
1270 switch( query->i_type )
1272 case HTTPD_MSG_SETUP:
1273 psz_playnow = httpd_MsgGet( query, "x-playNow" );
1274 psz_transport = httpd_MsgGet( query, "Transport" );
1276 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
1278 if( strstr( psz_transport, "unicast" ) &&
1279 strstr( psz_transport, "client_port=" ) )
1281 rtsp_client_t *p_rtsp = NULL;
1282 rtsp_client_es_t *p_rtsp_es = NULL;
1283 char ip[NI_MAXNUMERICHOST];
1284 int i_port = atoi( strstr( psz_transport, "client_port=" ) +
1285 strlen("client_port=") );
1287 if( httpd_ClientIP( cl, ip ) == NULL )
1289 answer->i_status = 500;
1290 answer->psz_status = strdup( "Internal server error" );
1292 answer->p_body = NULL;
1296 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
1299 psz_session = httpd_MsgGet( query, "Session" );
1300 if( !psz_session || !*psz_session )
1303 if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
1304 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
1306 answer->i_status = 503;
1307 answer->psz_status = strdup( "Too many connections" );
1309 answer->p_body = NULL;
1312 asprintf( &psz_new, "%d", rand() );
1313 psz_session = psz_new;
1315 p_rtsp = RtspClientNew( p_media, psz_new );
1319 p_rtsp = RtspClientGet( p_media, psz_session );
1322 answer->i_status = 454;
1323 answer->psz_status = strdup( "Unknown session id" );
1325 answer->p_body = NULL;
1330 p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
1333 answer->i_status = 500;
1334 answer->psz_status = strdup( "Internal server error" );
1336 answer->p_body = NULL;
1339 p_rtsp_es->i_port = i_port;
1340 p_rtsp_es->psz_ip = strdup( ip );
1341 p_rtsp_es->p_media_es = p_es;
1342 TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );
1344 answer->i_status = 200;
1345 answer->psz_status = strdup( "OK" );
1347 answer->p_body = NULL;
1349 if( p_media->b_raw )
1351 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
1353 httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
1354 p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1356 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1358 httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
1359 p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1364 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
1365 p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1368 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1370 answer->i_status = 461;
1371 answer->psz_status = strdup( "Unsupported Transport" );
1373 answer->p_body = NULL;
1376 /* Intentional fall-through on x-playNow option in RTSP request */
1380 case HTTPD_MSG_PLAY:
1381 /* This is kind of a kludge. Should we only support Aggregate
1383 psz_session = httpd_MsgGet( query, "Session" );
1384 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1386 p_rtsp = RtspClientGet( p_media, psz_session );
1388 psz_position = httpd_MsgGet( query, "Range" );
1389 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
1395 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1398 /* FIXME: npt= is not necessarily formatted as a float */
1399 f_pos = us_strtod( psz_position, &end );
1400 if( end > psz_position )
1402 f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
1403 CommandPush( p_vod, RTSP_CMD_TYPE_SEEK, p_media, psz_session, f_pos, NULL );
1409 answer->i_status = 200;
1410 answer->psz_status = strdup( "OK" );
1412 answer->p_body = NULL;
1416 case HTTPD_MSG_TEARDOWN:
1417 answer->i_status = 200;
1418 answer->psz_status = strdup( "OK" );
1420 answer->p_body = NULL;
1422 psz_session = httpd_MsgGet( query, "Session" );
1423 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1425 p_rtsp = RtspClientGet( p_media, psz_session );
1426 if( !p_rtsp ) break;
1428 for( i = 0; i < p_rtsp->i_es; i++ )
1430 if( p_rtsp->es[i]->p_media_es == p_es )
1432 if( p_rtsp->es[i]->psz_ip ) free( p_rtsp->es[i]->psz_ip );
1433 TAB_REMOVE( p_rtsp->i_es, p_rtsp->es, p_rtsp->es[i] );
1440 CommandPush( p_vod, RTSP_CMD_TYPE_STOP, p_media, psz_session, 0, NULL );
1441 RtspClientDel( p_media, p_rtsp );
1445 case HTTPD_MSG_PAUSE:
1446 /* This is kind of a kludge. Should we only support Aggregate
1448 psz_session = httpd_MsgGet( query, "Session" );
1449 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1451 p_rtsp = RtspClientGet( p_media, psz_session );
1452 if( !p_rtsp ) break;
1454 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media, psz_session, 0, NULL );
1455 p_rtsp->b_paused = VLC_TRUE;
1457 answer->i_status = 200;
1458 answer->psz_status = strdup( "OK" );
1460 answer->p_body = NULL;
1464 return VLC_EGENERIC;
1468 httpd_MsgAdd( answer, "Server", "VLC Server" );
1469 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1470 psz_cseq = httpd_MsgGet( query, "Cseq" );
1472 i_cseq = atoi( psz_cseq );
1475 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1476 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1479 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1484 /*****************************************************************************
1486 * FIXME: need to be moved to a common place ?
1487 *****************************************************************************/
1488 static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
1491 char *p, *psz_sdp, ip[NI_MAXNUMERICHOST], ipv;
1492 const char *psz_control;
1494 if( httpd_ServerIP( cl, ip ) == NULL )
1497 p = strchr( ip, '%' );
1499 *p = '\0'; /* remove scope if present */
1501 ipv = ( strchr( ip, ':' ) != NULL ) ? '6' : '4';
1503 /* Calculate size */
1504 i_size = sizeof( "v=0\r\n" ) +
1505 sizeof( "o=- * * IN IP4 \r\n" ) + 10 + NI_MAXNUMERICHOST +
1506 sizeof( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
1507 sizeof( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
1508 sizeof( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
1509 sizeof( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
1510 sizeof( "c=IN IP4 0.0.0.0\r\n" ) + 20 + 10 +
1511 sizeof( "t=0 0\r\n" ) + /* FIXME */
1512 sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
1513 sizeof( "a=range:npt=0-1000000000.000\r\n" );
1515 psz_control = (ipv == '6') ? p_media->psz_rtsp_control_v6
1516 : p_media->psz_rtsp_control_v4;
1517 for( i = 0; i < p_media->i_es; i++ )
1519 media_es_t *p_es = p_media->es[i];
1521 i_size += sizeof( "m=**d*o * RTP/AVP *\r\n" ) + 19;
1522 if( p_es->psz_rtpmap )
1524 i_size += sizeof( "a=rtpmap:* *\r\n" ) +
1525 strlen( p_es->psz_rtpmap ) + 9;
1527 if( p_es->psz_fmtp )
1529 i_size += sizeof( "a=fmtp:* *\r\n" ) +
1530 strlen( p_es->psz_fmtp ) + 9;
1533 i_size += (strlen( psz_control ) + strlen( ip ) + 9) * p_media->i_es;
1535 p = psz_sdp = malloc( i_size );
1536 p += sprintf( p, "v=0\r\n" );
1537 p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
1538 p_media->i_sdp_id, p_media->i_sdp_version, ipv, ip );
1539 if( *p_media->psz_session_name )
1540 p += sprintf( p, "s=%s\r\n", p_media->psz_session_name );
1541 if( *p_media->psz_session_description )
1542 p += sprintf( p, "i=%s\r\n", p_media->psz_session_description );
1543 if( *p_media->psz_session_url )
1544 p += sprintf( p, "u=%s\r\n", p_media->psz_session_url );
1545 if( *p_media->psz_session_email )
1546 p += sprintf( p, "e=%s\r\n", p_media->psz_session_email );
1548 p += sprintf( p, "c=IN IP%c %s\r\n", ipv, ipv == '6' ? "::" : "0.0.0.0" );
1549 p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
1550 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
1552 if( p_media->i_length > 0 )
1554 lldiv_t d = lldiv( p_media->i_length / 1000, 1000 );
1555 p += sprintf( p, "a=range:npt=0-%lld.%03u\r\n", d.quot,
1559 for( i = 0; i < p_media->i_es; i++ )
1561 media_es_t *p_es = p_media->es[i];
1563 if( p_es->fmt.i_cat == AUDIO_ES )
1565 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
1566 p_es->i_port, p_es->i_payload_type );
1568 else if( p_es->fmt.i_cat == VIDEO_ES )
1570 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
1571 p_es->i_port, p_es->i_payload_type );
1578 if( p_es->psz_rtpmap )
1580 p += sprintf( p, "a=rtpmap:%d %s\r\n", p_es->i_payload_type,
1583 if( p_es->psz_fmtp )
1585 p += sprintf( p, "a=fmtp:%d %s\r\n", p_es->i_payload_type,
1589 p += sprintf( p, psz_control, ip, i );