1 /*****************************************************************************
2 * rtsp.c: rtsp VoD server module
3 *****************************************************************************
4 * Copyright (C) 2003-2006 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_input.h>
37 #include <vlc_block.h>
39 #include <vlc_httpd.h>
42 #include <vlc_network.h>
43 #include <vlc_charset.h>
44 #include <vlc_strings.h>
56 /*****************************************************************************
58 *****************************************************************************/
59 static int Open ( vlc_object_t * );
60 static void Close( vlc_object_t * );
62 #define HOST_TEXT N_( "RTSP host address" )
63 #define HOST_LONGTEXT N_( \
64 "This defines the address, port and path the RTSP VOD server will listen " \
65 "on.\nSyntax is address:port/path. The default is to listen on all "\
66 "interfaces (address 0.0.0.0), on port 554, with no path.\nTo listen " \
67 "only on the local interface, use \"localhost\" as address." )
69 #define THROTLE_TEXT N_( "Maximum number of connections" )
70 #define THROTLE_LONGTEXT N_( "This limits the maximum number of clients " \
71 "that can connect to the RTSP VOD. 0 means no limit." )
73 #define RAWMUX_TEXT N_( "MUX for RAW RTSP transport" )
75 #define SESSION_TIMEOUT_TEXT N_( "Sets the timeout option in the RTSP " \
77 #define SESSION_TIMEOUT_LONGTEXT N_( "Defines what timeout option to add " \
78 "to the RTSP session ID string. Setting it to a negative number removes " \
79 "the timeout option entirely. This is needed by some IPTV STBs (such as " \
80 "those made by HansunTech) which get confused by it. The default is 5." )
83 set_shortname( N_("RTSP VoD" ) )
84 set_description( N_("RTSP VoD server") )
85 set_category( CAT_SOUT )
86 set_subcategory( SUBCAT_SOUT_VOD )
87 set_capability( "vod server", 1 )
88 set_callbacks( Open, Close )
89 add_shortcut( "rtsp" )
90 add_string ( "rtsp-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, true )
91 add_string( "rtsp-raw-mux", "ts", NULL, RAWMUX_TEXT,
93 add_integer( "rtsp-throttle-users", 0, NULL, THROTLE_TEXT,
94 THROTLE_LONGTEXT, true )
95 add_integer( "rtsp-session-timeout", 5, NULL, SESSION_TIMEOUT_TEXT,
96 SESSION_TIMEOUT_LONGTEXT, true )
99 /*****************************************************************************
100 * Exported prototypes
101 *****************************************************************************/
103 typedef struct media_es_t media_es_t;
107 media_es_t *p_media_es;
116 int64_t i_last; /* for timeout */
118 bool b_playing; /* is it in "play" state */
119 bool b_paused; /* is it in "pause" state */
122 rtsp_client_es_t **es;
132 httpd_url_t *p_rtsp_url;
134 vod_media_t *p_media;
138 uint8_t i_payload_type;
152 httpd_url_t *p_rtsp_url;
153 char *psz_rtsp_control_v4;
154 char *psz_rtsp_control_v6;
178 rtsp_client_t **rtsp;
181 char *psz_session_name;
182 char *psz_session_description;
183 char *psz_session_url;
184 char *psz_session_email;
191 httpd_host_t *p_rtsp_host;
194 int i_throttle_users;
199 int i_session_timeout;
202 vlc_mutex_t lock_media;
208 block_fifo_t *p_fifo_cmd;
211 /* rtsp delayed command (to avoid deadlock between vlm/httpd) */
214 RTSP_CMD_TYPE_NONE, /* Exit requested */
220 RTSP_CMD_TYPE_REWIND,
221 RTSP_CMD_TYPE_FORWARD,
232 vod_media_t *p_media;
238 static vod_media_t *MediaNew( vod_t *, const char *, input_item_t * );
239 static void MediaDel( vod_t *, vod_media_t * );
240 static void MediaAskDel ( vod_t *, vod_media_t * );
241 static int MediaAddES( vod_t *, vod_media_t *, es_format_t * );
242 static void MediaDelES( vod_t *, vod_media_t *, es_format_t * );
244 static void* CommandThread( vlc_object_t *p_this );
245 static void CommandPush( vod_t *, rtsp_cmd_type_t, vod_media_t *, const char *psz_session,
246 double f_arg, const char *psz_arg );
248 static rtsp_client_t *RtspClientNew( vod_media_t *, char * );
249 static rtsp_client_t *RtspClientGet( vod_media_t *, const char * );
250 static void RtspClientDel( vod_media_t *, rtsp_client_t * );
252 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
253 httpd_message_t *, const httpd_message_t * );
254 static int RtspCallbackES( httpd_callback_sys_t *, httpd_client_t *,
255 httpd_message_t *, const httpd_message_t * );
257 static char *SDPGenerate( const vod_media_t *, httpd_client_t *cl );
259 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
261 static const char hex[16] = "0123456789abcdef";
264 for( i = 0; i < i_data; i++ )
266 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
267 s[2*i+1] = hex[(p_data[i] )&0xf];
272 /*****************************************************************************
273 * Open: Starts the RTSP server module
274 *****************************************************************************/
275 static int Open( vlc_object_t *p_this )
277 vod_t *p_vod = (vod_t *)p_this;
278 vod_sys_t *p_sys = NULL;
279 char *psz_url = NULL;
282 psz_url = config_GetPsz( p_vod, "rtsp-host" );
283 vlc_UrlParse( &url, psz_url, 0 );
286 if( url.i_port <= 0 ) url.i_port = 554;
288 p_vod->p_sys = p_sys = malloc( sizeof( vod_sys_t ) );
289 if( !p_sys ) goto error;
290 p_sys->p_rtsp_host = 0;
292 p_sys->i_session_timeout = var_CreateGetInteger( p_this, "rtsp-session-timeout" );
294 p_sys->i_throttle_users = var_CreateGetInteger( p_this, "rtsp-throttle-users" );
295 msg_Dbg( p_this, "allowing up to %d connections", p_sys->i_throttle_users );
296 p_sys->i_connections = 0;
298 p_sys->psz_raw_mux = var_CreateGetString( p_this, "rtsp-raw-mux" );
301 httpd_HostNew( VLC_OBJECT(p_vod), url.psz_host, url.i_port );
302 if( !p_sys->p_rtsp_host )
304 msg_Err( p_vod, "cannot create RTSP server (%s:%i)",
305 url.psz_host, url.i_port );
309 p_sys->psz_path = strdup( url.psz_path ? url.psz_path : "/" );
310 p_sys->i_port = url.i_port;
312 vlc_UrlClean( &url );
314 vlc_mutex_init( &p_sys->lock_media );
316 TAB_INIT( p_sys->i_media, p_sys->media );
317 p_sys->i_media_id = 0;
319 p_vod->pf_media_new = MediaNew;
320 p_vod->pf_media_del = MediaAskDel;
321 p_vod->pf_media_add_es = MediaAddES;
322 p_vod->pf_media_del_es = MediaDelES;
324 p_sys->p_fifo_cmd = block_FifoNew();
325 if( vlc_thread_create( p_vod, "rtsp vod thread", CommandThread,
326 VLC_THREAD_PRIORITY_LOW ) )
328 msg_Err( p_vod, "cannot spawn rtsp vod thread" );
329 block_FifoRelease( p_sys->p_fifo_cmd );
330 free( p_sys->psz_path );
339 if( p_sys->p_rtsp_host ) httpd_HostDelete( p_sys->p_rtsp_host );
340 free( p_sys->psz_raw_mux );
343 vlc_UrlClean( &url );
348 /*****************************************************************************
350 *****************************************************************************/
351 static void Close( vlc_object_t * p_this )
353 vod_t *p_vod = (vod_t *)p_this;
354 vod_sys_t *p_sys = p_vod->p_sys;
355 block_t *p_block_cmd;
358 /* Stop command thread */
359 vlc_object_kill( p_vod );
360 CommandPush( p_vod, RTSP_CMD_TYPE_NONE, NULL, NULL, 0.0, NULL );
361 vlc_thread_join( p_vod );
363 while( block_FifoCount( p_sys->p_fifo_cmd ) > 0 )
365 p_block_cmd = block_FifoGet( p_sys->p_fifo_cmd );
366 memcpy( &cmd, p_block_cmd->p_buffer, sizeof(cmd) );
367 block_Release( p_block_cmd );
368 if ( cmd.i_type == RTSP_CMD_TYPE_DEL )
369 MediaDel(p_vod, cmd.p_media);
370 free( cmd.psz_session );
373 block_FifoRelease( p_sys->p_fifo_cmd );
375 httpd_HostDelete( p_sys->p_rtsp_host );
376 var_Destroy( p_this, "rtsp-session-timeout" );
377 var_Destroy( p_this, "rtsp-throttle-users" );
378 var_Destroy( p_this, "rtsp-raw-mux" );
380 /* Check VLM is not buggy */
381 if( p_sys->i_media > 0 )
382 msg_Err( p_vod, "rtsp vod leaking %d medias", p_sys->i_media );
383 TAB_CLEAN( p_sys->i_media, p_sys->media );
385 vlc_mutex_destroy( &p_sys->lock_media );
387 free( p_sys->psz_path );
388 free( p_sys->psz_raw_mux );
392 /*****************************************************************************
394 *****************************************************************************/
395 static vod_media_t *MediaNew( vod_t *p_vod, const char *psz_name,
396 input_item_t *p_item )
399 vod_sys_t *p_sys = p_vod->p_sys;
401 vod_media_t *p_media = calloc( 1, sizeof(vod_media_t) );
405 p_media->id = p_sys->i_media_id++;
406 TAB_INIT( p_media->i_es, p_media->es );
407 p_media->psz_mux = NULL;
408 TAB_INIT( p_media->i_rtsp, p_media->rtsp );
409 p_media->b_raw = false;
411 if( asprintf( &p_media->psz_rtsp_path, "%s%s",
412 p_sys->psz_path, psz_name ) <0 )
414 p_media->p_rtsp_url =
415 httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, NULL,
418 if( !p_media->p_rtsp_url )
420 msg_Err( p_vod, "cannot create RTSP url (%s)", p_media->psz_rtsp_path);
421 free( p_media->psz_rtsp_path );
426 msg_Dbg( p_vod, "created RTSP url: %s", p_media->psz_rtsp_path );
428 if( asprintf( &p_media->psz_rtsp_control_v4,
429 "a=control:rtsp://%%s:%d%s/trackID=%%d\r\n",
430 p_sys->i_port, p_media->psz_rtsp_path ) < 0 )
432 httpd_UrlDelete( p_media->p_rtsp_url );
433 free( p_media->psz_rtsp_path );
437 if( asprintf( &p_media->psz_rtsp_control_v6,
438 "a=control:rtsp://[%%s]:%d%s/trackID=%%d\r\n",
439 p_sys->i_port, p_media->psz_rtsp_path ) < 0 )
441 httpd_UrlDelete( p_media->p_rtsp_url );
442 free( p_media->psz_rtsp_path );
447 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_SETUP,
448 RtspCallback, (void*)p_media );
449 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_DESCRIBE,
450 RtspCallback, (void*)p_media );
451 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PLAY,
452 RtspCallback, (void*)p_media );
453 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PAUSE,
454 RtspCallback, (void*)p_media );
455 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_GETPARAMETER,
456 RtspCallback, (void*)p_media );
457 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_TEARDOWN,
458 RtspCallback, (void*)p_media );
460 p_media->p_vod = p_vod;
462 vlc_mutex_init( &p_media->lock );
463 p_media->psz_session_name = strdup("");
464 p_media->psz_session_description = strdup("");
465 p_media->psz_session_url = strdup("");
466 p_media->psz_session_email = strdup("");
468 p_media->i_port_audio = 1234;
469 p_media->i_port_video = 1236;
470 p_media->i_port = 1238;
471 p_media->i_payload_type = 96;
473 p_media->i_sdp_id = mdate();
474 p_media->i_sdp_version = 1;
475 p_media->i_length = input_item_GetDuration( p_item );
477 vlc_mutex_lock( &p_item->lock );
478 msg_Dbg( p_vod, "media has %i declared ES", p_item->i_es );
479 for( i = 0; i < p_item->i_es; i++ )
481 MediaAddES( p_vod, p_media, p_item->es[i] );
483 vlc_mutex_unlock( &p_item->lock );
485 CommandPush( p_vod, RTSP_CMD_TYPE_ADD, p_media, NULL, 0.0, NULL );
489 static void MediaAskDel ( vod_t *p_vod, vod_media_t *p_media )
491 CommandPush( p_vod, RTSP_CMD_TYPE_DEL, p_media, NULL, 0.0, NULL );
494 static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
496 vod_sys_t *p_sys = p_vod->p_sys;
498 msg_Dbg( p_vod, "deleting media: %s", p_media->psz_rtsp_path );
500 vlc_mutex_lock( &p_sys->lock_media );
501 TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
502 vlc_mutex_unlock( &p_sys->lock_media );
504 httpd_UrlDelete( p_media->p_rtsp_url );
506 while( p_media->i_rtsp > 0 )
507 RtspClientDel( p_media, p_media->rtsp[0] );
508 TAB_CLEAN( p_media->i_rtsp, p_media->rtsp );
510 free( p_media->psz_rtsp_path );
511 free( p_media->psz_rtsp_control_v6 );
512 free( p_media->psz_rtsp_control_v4 );
514 while( p_media->i_es )
515 MediaDelES( p_vod, p_media, &p_media->es[0]->fmt );
516 TAB_CLEAN( p_media->i_es, p_media->es );
518 vlc_mutex_destroy( &p_media->lock );
520 free( p_media->psz_session_name );
521 free( p_media->psz_session_description );
522 free( p_media->psz_session_url );
523 free( p_media->psz_session_email );
524 free( p_media->psz_mux );
528 static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
531 media_es_t *p_es = calloc( 1, sizeof(media_es_t) );
535 free( p_media->psz_mux );
536 p_media->psz_mux = NULL;
538 /* TODO: update SDP, etc... */
539 if( asprintf( &psz_urlc, "%s/trackID=%d",
540 p_media->psz_rtsp_path, p_media->i_es ) < 0 )
545 msg_Dbg( p_vod, " - ES %4.4s (%s)", (char *)&p_fmt->i_codec, psz_urlc );
547 switch( p_fmt->i_codec )
550 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
552 p_es->i_payload_type = 11;
554 else if( p_fmt->audio.i_channels == 2 &&
555 p_fmt->audio.i_rate == 44100 )
557 p_es->i_payload_type = 10;
561 p_es->i_payload_type = p_media->i_payload_type++;
563 if( asprintf( &p_es->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
564 p_fmt->audio.i_channels ) == -1 )
565 p_es->psz_rtpmap = NULL;
568 p_es->i_payload_type = p_media->i_payload_type++;
569 if( asprintf( &p_es->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
570 p_fmt->audio.i_channels ) == -1 )
571 p_es->psz_rtpmap = NULL;
574 p_es->i_payload_type = 14;
575 p_es->psz_rtpmap = strdup( "MPA/90000" );
578 p_es->i_payload_type = 32;
579 p_es->psz_rtpmap = strdup( "MPV/90000" );
582 p_es->i_payload_type = p_media->i_payload_type++;
583 if( asprintf( &p_es->psz_rtpmap, "ac3/%d", p_fmt->audio.i_rate ) == -1 )
584 p_es->psz_rtpmap = NULL;
587 p_es->i_payload_type = p_media->i_payload_type++;
588 p_es->psz_rtpmap = strdup( "H263-1998/90000" );
591 p_es->i_payload_type = p_media->i_payload_type++;
592 p_es->psz_rtpmap = strdup( "H264/90000" );
593 p_es->psz_fmtp = NULL;
594 /* FIXME AAAAAAAAAAAARRRRRRRRGGGG copied from stream_out/rtp.c */
595 if( p_fmt->i_extra > 0 )
597 uint8_t *p_buffer = p_fmt->p_extra;
598 int i_buffer = p_fmt->i_extra;
599 char *p_64_sps = NULL;
600 char *p_64_pps = NULL;
603 while( i_buffer > 4 &&
604 p_buffer[0] == 0 && p_buffer[1] == 0 &&
605 p_buffer[2] == 0 && p_buffer[3] == 1 )
607 const int i_nal_type = p_buffer[4]&0x1f;
612 for( i_offset = 4; i_offset+3 < i_buffer ; i_offset++)
614 if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 0 && p_buffer[i_offset+3] == 1 )
616 /* we found another startcode */
621 if( i_nal_type == 7 )
624 p_64_sps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
625 sprintf_hexa( hexa, &p_buffer[5], 3 );
627 else if( i_nal_type == 8 )
630 p_64_pps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
636 if( p_64_sps && p_64_pps )
638 if( asprintf( &p_es->psz_fmtp,
639 "packetization-mode=1;profile-level-id=%s;"
640 "sprop-parameter-sets=%s,%s;", hexa, p_64_sps,
653 if( !p_es->psz_fmtp )
654 p_es->psz_fmtp = strdup( "packetization-mode=1" );
657 p_es->i_payload_type = p_media->i_payload_type++;
658 p_es->psz_rtpmap = strdup( "MP4V-ES/90000" );
659 if( p_fmt->i_extra > 0 )
661 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
662 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
663 if( asprintf( &p_es->psz_fmtp,
664 "profile-level-id=3; config=%s;", p_hexa ) == -1 )
665 p_es->psz_fmtp = NULL;
670 p_es->i_payload_type = p_media->i_payload_type++;
671 if( asprintf( &p_es->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate ) == -1 )
672 p_es->psz_rtpmap = NULL;
673 if( p_fmt->i_extra > 0 )
675 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
676 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
677 if( asprintf( &p_es->psz_fmtp,
678 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
679 "config=%s; SizeLength=13;IndexLength=3; "
680 "IndexDeltaLength=3; Profile=1;", p_hexa ) == -1 )
681 p_es->psz_fmtp = NULL;
685 case VLC_FOURCC( 'm', 'p', '2', 't' ):
686 p_media->psz_mux = strdup("ts");
687 p_es->i_payload_type = 33;
688 p_es->psz_rtpmap = strdup( "MP2T/90000" );
690 case VLC_FOURCC( 'm', 'p', '2', 'p' ):
691 p_media->psz_mux = strdup("ps");
692 p_es->i_payload_type = p_media->i_payload_type++;
693 p_es->psz_rtpmap = strdup( "MP2P/90000" );
695 case VLC_CODEC_AMR_NB:
696 p_es->i_payload_type = p_media->i_payload_type++;
697 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
698 "AMR/8000/2" : "AMR/8000" );
699 p_es->psz_fmtp = strdup( "octet-align=1" );
701 case VLC_CODEC_AMR_WB:
702 p_es->i_payload_type = p_media->i_payload_type++;
703 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
704 "AMR-WB/16000/2" : "AMR-WB/16000" );
705 p_es->psz_fmtp = strdup( "octet-align=1" );
709 msg_Err( p_vod, "cannot add this stream (unsupported "
710 "codec: %4.4s)", (char*)&p_fmt->i_codec );
717 httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, NULL, NULL,
720 if( !p_es->p_rtsp_url )
722 msg_Err( p_vod, "cannot create RTSP url (%s)", psz_urlc );
729 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
730 RtspCallbackES, (void*)p_es );
731 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_TEARDOWN,
732 RtspCallbackES, (void*)p_es );
733 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PLAY,
734 RtspCallbackES, (void*)p_es );
735 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PAUSE,
736 RtspCallbackES, (void*)p_es );
738 es_format_Copy( &p_es->fmt, p_fmt );
740 p_es->p_media = p_media;
743 /* Choose the port */
744 if( p_fmt->i_cat == AUDIO_ES && p_media->i_port_audio > 0 )
746 p_es->i_port = p_media->i_port_audio;
747 p_media->i_port_audio = 0;
749 else if( p_fmt->i_cat == VIDEO_ES && p_media->i_port_video > 0 )
751 p_es->i_port = p_media->i_port_video;
752 p_media->i_port_video = 0;
754 while( !p_es->i_port )
756 if( p_media->i_port != p_media->i_port_audio &&
757 p_media->i_port != p_media->i_port_video )
759 p_es->i_port = p_media->i_port;
760 p_media->i_port += 2;
763 p_media->i_port += 2;
770 vlc_mutex_lock( &p_media->lock );
771 TAB_APPEND( p_media->i_es, p_media->es, p_es );
772 vlc_mutex_unlock( &p_media->lock );
774 p_media->i_sdp_version++;
779 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
781 media_es_t *p_es = NULL;
785 for( i = 0; i < p_media->i_es; i++ )
787 if( p_media->es[i]->fmt.i_cat == p_fmt->i_cat &&
788 p_media->es[i]->fmt.i_codec == p_fmt->i_codec &&
789 p_media->es[i]->fmt.i_id == p_fmt->i_id )
791 p_es = p_media->es[i];
796 msg_Dbg( p_vod, " - Removing ES %4.4s", (char *)&p_fmt->i_codec );
798 vlc_mutex_lock( &p_media->lock );
799 TAB_REMOVE( p_media->i_es, p_media->es, p_es );
800 vlc_mutex_unlock( &p_media->lock );
802 free( p_es->psz_rtpmap );
803 free( p_es->psz_fmtp );
804 p_media->i_sdp_version++;
806 if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
807 es_format_Clean( &p_es->fmt );
811 static void CommandPush( vod_t *p_vod, rtsp_cmd_type_t i_type, vod_media_t *p_media, const char *psz_session,
812 double f_arg, const char *psz_arg )
817 memset( &cmd, 0, sizeof(cmd) );
819 cmd.p_media = p_media;
821 cmd.i_media_id = p_media->id;
823 cmd.psz_session = strdup(psz_session);
826 cmd.psz_arg = strdup(psz_arg);
828 p_cmd = block_New( p_vod, sizeof(rtsp_cmd_t) );
829 memcpy( p_cmd->p_buffer, &cmd, sizeof(cmd) );
831 block_FifoPut( p_vod->p_sys->p_fifo_cmd, p_cmd );
834 static void* CommandThread( vlc_object_t *p_this )
836 vod_t *p_vod = (vod_t*)p_this;
837 vod_sys_t *p_sys = p_vod->p_sys;
838 int canc = vlc_savecancel ();
840 while( vlc_object_alive (p_vod) )
842 block_t *p_block_cmd = block_FifoGet( p_sys->p_fifo_cmd );
844 vod_media_t *p_media = NULL;
850 memcpy( &cmd, p_block_cmd->p_buffer, sizeof(cmd) );
851 block_Release( p_block_cmd );
853 if( cmd.i_type == RTSP_CMD_TYPE_NONE )
856 if ( cmd.i_type == RTSP_CMD_TYPE_ADD )
858 vlc_mutex_lock( &p_sys->lock_media );
859 TAB_APPEND( p_sys->i_media, p_sys->media, cmd.p_media );
860 vlc_mutex_unlock( &p_sys->lock_media );
864 if ( cmd.i_type == RTSP_CMD_TYPE_DEL )
866 MediaDel(p_vod, cmd.p_media);
871 vlc_mutex_lock( &p_sys->lock_media );
872 for( i = 0; i < p_sys->i_media; i++ )
874 if( p_sys->media[i]->id == cmd.i_media_id )
877 if( i >= p_sys->i_media )
879 vlc_mutex_unlock( &p_sys->lock_media );
882 p_media = p_sys->media[i];
886 case RTSP_CMD_TYPE_PLAY:
887 vod_MediaControl( p_vod, p_media, cmd.psz_session,
888 VOD_MEDIA_PLAY, cmd.psz_arg );
890 case RTSP_CMD_TYPE_PAUSE:
891 vod_MediaControl( p_vod, p_media, cmd.psz_session,
895 case RTSP_CMD_TYPE_STOP:
896 vod_MediaControl( p_vod, p_media, cmd.psz_session, VOD_MEDIA_STOP );
899 case RTSP_CMD_TYPE_SEEK:
900 vod_MediaControl( p_vod, p_media, cmd.psz_session,
901 VOD_MEDIA_SEEK, cmd.f_arg );
904 case RTSP_CMD_TYPE_REWIND:
905 vod_MediaControl( p_vod, p_media, cmd.psz_session,
906 VOD_MEDIA_REWIND, cmd.f_arg );
909 case RTSP_CMD_TYPE_FORWARD:
910 vod_MediaControl( p_vod, p_media, cmd.psz_session,
911 VOD_MEDIA_FORWARD, cmd.f_arg );
917 vlc_mutex_unlock( &p_sys->lock_media );
920 free( cmd.psz_session );
924 vlc_restorecancel (canc);
928 /****************************************************************************
929 * RTSP server implementation
930 ****************************************************************************/
931 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
933 rtsp_client_t *p_rtsp = calloc( 1, sizeof(rtsp_client_t) );
939 p_rtsp->psz_session = psz_session;
940 TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
942 p_media->p_vod->p_sys->i_connections++;
943 msg_Dbg( p_media->p_vod, "new session: %s, connections: %d",
944 psz_session, p_media->p_vod->p_sys->i_throttle_users );
949 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, const char *psz_session )
953 for( i = 0; psz_session && i < p_media->i_rtsp; i++ )
955 if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
956 return p_media->rtsp[i];
962 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
964 p_media->p_vod->p_sys->i_connections--;
965 msg_Dbg( p_media->p_vod, "closing session: %s, connections: %d",
966 p_rtsp->psz_session, p_media->p_vod->p_sys->i_throttle_users );
968 while( p_rtsp->i_es )
971 free( p_rtsp->es[p_rtsp->i_es]->psz_ip );
972 free( p_rtsp->es[p_rtsp->i_es] );
976 TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, p_rtsp );
978 free( p_rtsp->psz_session );
983 static float ParseNPT (const char *str)
985 locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
986 locale_t oldloc = uselocale (loc);
990 if (sscanf (str, "%u:%u:%f", &hour, &min, &sec) == 3)
991 sec += ((hour * 60) + min) * 60;
993 if (sscanf (str, "%f", &sec) != 1)
996 if (loc != (locale_t)0)
1005 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
1006 httpd_message_t *answer, const httpd_message_t *query )
1008 vod_media_t *p_media = (vod_media_t*)p_args;
1009 vod_t *p_vod = p_media->p_vod;
1010 const char *psz_transport = NULL;
1011 const char *psz_playnow = NULL; /* support option: x-playNow */
1012 const char *psz_session = NULL;
1013 const char *psz_cseq = NULL;
1014 rtsp_client_t *p_rtsp;
1018 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
1020 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
1022 answer->i_proto = HTTPD_PROTO_RTSP;
1023 answer->i_version = query->i_version;
1024 answer->i_type = HTTPD_MSG_ANSWER;
1026 answer->p_body = NULL;
1028 switch( query->i_type )
1030 case HTTPD_MSG_SETUP:
1032 psz_playnow = httpd_MsgGet( query, "x-playNow" );
1033 psz_transport = httpd_MsgGet( query, "Transport" );
1034 if( psz_transport == NULL )
1036 answer->i_status = 400;
1039 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
1041 if( strstr( psz_transport, "unicast" ) &&
1042 strstr( psz_transport, "client_port=" ) )
1044 rtsp_client_t *p_rtsp = NULL;
1045 char ip[NI_MAXNUMERICHOST];
1046 i_port = atoi( strstr( psz_transport, "client_port=" ) +
1047 strlen("client_port=") );
1049 if( strstr( psz_transport, "MP2T/H2221/UDP" ) ||
1050 strstr( psz_transport, "RAW/RAW/UDP" ) )
1052 free( p_media->psz_mux );
1053 p_media->psz_mux = NULL;
1054 p_media->psz_mux = strdup( p_vod->p_sys->psz_raw_mux );
1055 p_media->b_raw = true;
1058 if( httpd_ClientIP( cl, ip ) == NULL )
1060 answer->i_status = 500;
1062 answer->p_body = NULL;
1066 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
1069 psz_session = httpd_MsgGet( query, "Session" );
1070 if( !psz_session || !*psz_session )
1073 if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
1074 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
1076 answer->i_status = 503;
1078 answer->p_body = NULL;
1081 if( asprintf( &psz_new, "%d", rand() ) < 0 )
1083 psz_session = psz_new;
1085 p_rtsp = RtspClientNew( p_media, psz_new );
1088 answer->i_status = 454;
1090 answer->p_body = NULL;
1096 p_rtsp = RtspClientGet( p_media, psz_session );
1099 answer->i_status = 454;
1101 answer->p_body = NULL;
1106 answer->i_status = 200;
1108 answer->p_body = NULL;
1110 if( p_media->b_raw )
1112 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
1114 httpd_MsgAdd( answer, "Transport",
1115 "MP2T/H2221/UDP;unicast;client_port=%d-%d",
1116 i_port, i_port + 1 );
1118 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1120 httpd_MsgAdd( answer, "Transport",
1121 "RAW/RAW/UDP;unicast;client_port=%d-%d",
1122 i_port, i_port + 1 );
1126 httpd_MsgAdd( answer, "Transport",
1127 "RTP/AVP/UDP;unicast;client_port=%d-%d",
1128 i_port, i_port + 1 );
1130 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1132 answer->i_status = 461;
1134 answer->p_body = NULL;
1137 /* Intentional fall-through on x-playNow option in RTSP request */
1142 case HTTPD_MSG_PLAY:
1144 char *psz_output, ip[NI_MAXNUMERICHOST];
1145 int i, i_port_audio = 0, i_port_video = 0;
1147 /* for now only multicast so easy */
1150 answer->i_status = 200;
1152 answer->p_body = NULL;
1156 psz_session = httpd_MsgGet( query, "Session" );
1157 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1159 p_rtsp = RtspClientGet( p_media, psz_session );
1162 answer->i_status = 500;
1164 answer->p_body = NULL;
1168 if( p_rtsp->b_playing )
1170 const char *psz_position = httpd_MsgGet( query, "Range" );
1171 const char *psz_scale = httpd_MsgGet( query, "Scale" );
1173 psz_position = strstr( psz_position, "npt=" );
1174 if( psz_position && !psz_scale )
1176 double f_pos = ParseNPT (psz_position + 4);
1177 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1178 f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
1179 CommandPush( p_vod, RTSP_CMD_TYPE_SEEK, p_media,
1180 psz_session, f_pos, NULL );
1185 double f_scale = 0.0;
1188 f_scale = us_strtod( psz_scale, &end );
1189 if( end > psz_scale )
1191 f_scale = (f_scale * 30.0);
1192 if( psz_scale[0] == '-' ) /* rewind */
1194 msg_Dbg( p_vod, "rewind request: %s", psz_scale );
1195 CommandPush( p_vod, RTSP_CMD_TYPE_REWIND, p_media,
1196 psz_session, f_scale, NULL );
1198 else if(psz_scale[0] != '1' ) /* fast-forward */
1200 msg_Dbg( p_vod, "fastforward request: %s",
1202 CommandPush( p_vod, RTSP_CMD_TYPE_FORWARD, p_media,
1203 psz_session, f_scale, NULL );
1206 if( p_rtsp->b_paused == true )
1208 p_rtsp->b_paused = false;
1209 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media,
1210 psz_session, 0, NULL );
1217 if( p_rtsp->b_playing && p_rtsp->b_paused )
1219 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media,
1220 psz_session, 0, NULL );
1221 p_rtsp->b_paused = false;
1224 else if( p_rtsp->b_playing ) break;
1226 if( httpd_ClientIP( cl, ip ) == NULL ) break;
1228 p_rtsp->b_playing = true;
1230 /* FIXME for != 1 video and 1 audio */
1231 for( i = 0; i < p_rtsp->i_es; i++ )
1233 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == AUDIO_ES )
1234 i_port_audio = p_rtsp->es[i]->i_port;
1235 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == VIDEO_ES )
1236 i_port_video = p_rtsp->es[i]->i_port;
1239 if( p_media->psz_mux )
1241 if( p_media->b_raw )
1243 if( asprintf( &psz_output,
1244 "std{access=udp,dst=%s:%i,mux=%s}",
1245 ip, i_port, p_media->psz_mux ) < 0 )
1250 if( asprintf( &psz_output,
1251 "rtp{dst=%s,port=%i,mux=%s}",
1252 ip, i_port_video, p_media->psz_mux ) < 0 )
1258 if( asprintf( &psz_output,
1259 "rtp{dst=%s,port-video=%i,port-audio=%i}",
1260 ip, i_port_video, i_port_audio ) < 0 )
1264 CommandPush( p_vod, RTSP_CMD_TYPE_PLAY, p_media, psz_session,
1270 case HTTPD_MSG_DESCRIBE:
1273 SDPGenerate( p_media, cl );
1275 if( psz_sdp != NULL )
1277 answer->i_status = 200;
1278 httpd_MsgAdd( answer, "Content-type", "%s",
1279 "application/sdp" );
1281 answer->p_body = (uint8_t *)psz_sdp;
1282 answer->i_body = strlen( psz_sdp );
1286 answer->i_status = 500;
1287 answer->p_body = NULL;
1293 case HTTPD_MSG_PAUSE:
1294 psz_session = httpd_MsgGet( query, "Session" );
1295 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1297 p_rtsp = RtspClientGet( p_media, psz_session );
1298 if( !p_rtsp ) break;
1300 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media, psz_session,
1302 p_rtsp->b_paused = true;
1304 answer->i_status = 200;
1306 answer->p_body = NULL;
1309 case HTTPD_MSG_TEARDOWN:
1310 /* for now only multicast so easy again */
1311 answer->i_status = 200;
1313 answer->p_body = NULL;
1315 psz_session = httpd_MsgGet( query, "Session" );
1316 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1318 p_rtsp = RtspClientGet( p_media, psz_session );
1319 if( !p_rtsp ) break;
1321 CommandPush( p_vod, RTSP_CMD_TYPE_STOP, p_media, psz_session,
1323 RtspClientDel( p_media, p_rtsp );
1326 case HTTPD_MSG_GETPARAMETER:
1327 answer->i_status = 200;
1329 answer->p_body = NULL;
1333 return VLC_EGENERIC;
1336 httpd_MsgAdd( answer, "Server", "VLC Server" );
1337 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1338 psz_cseq = httpd_MsgGet( query, "Cseq" );
1339 psz_cseq ? i_cseq = atoi( psz_cseq ) : 0;
1340 httpd_MsgAdd( answer, "CSeq", "%d", i_cseq );
1341 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1345 if( p_media->p_vod->p_sys->i_session_timeout >= 0 )
1346 httpd_MsgAdd( answer, "Session", "%s;timeout=%i", psz_session,
1347 p_media->p_vod->p_sys->i_session_timeout );
1349 httpd_MsgAdd( answer, "Session", "%s", psz_session );
1355 static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
1356 httpd_message_t *answer,
1357 const httpd_message_t *query )
1359 media_es_t *p_es = (media_es_t*)p_args;
1360 vod_media_t *p_media = p_es->p_media;
1361 vod_t *p_vod = p_media->p_vod;
1362 rtsp_client_t *p_rtsp = NULL;
1363 const char *psz_transport = NULL;
1364 const char *psz_playnow = NULL; /* support option: x-playNow */
1365 const char *psz_session = NULL;
1366 const char *psz_position = NULL;
1367 const char *psz_cseq = NULL;
1371 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
1373 msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
1375 answer->i_proto = HTTPD_PROTO_RTSP;
1376 answer->i_version = query->i_version;
1377 answer->i_type = HTTPD_MSG_ANSWER;
1379 answer->p_body = NULL;
1381 switch( query->i_type )
1383 case HTTPD_MSG_SETUP:
1384 psz_playnow = httpd_MsgGet( query, "x-playNow" );
1385 psz_transport = httpd_MsgGet( query, "Transport" );
1387 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
1389 if( strstr( psz_transport, "unicast" ) &&
1390 strstr( psz_transport, "client_port=" ) )
1392 rtsp_client_t *p_rtsp = NULL;
1393 rtsp_client_es_t *p_rtsp_es = NULL;
1394 char ip[NI_MAXNUMERICHOST];
1395 int i_port = atoi( strstr( psz_transport, "client_port=" ) +
1396 strlen("client_port=") );
1398 if( httpd_ClientIP( cl, ip ) == NULL )
1400 answer->i_status = 500;
1402 answer->p_body = NULL;
1406 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
1409 psz_session = httpd_MsgGet( query, "Session" );
1410 if( !psz_session || !*psz_session )
1413 if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
1414 ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
1416 answer->i_status = 503;
1418 answer->p_body = NULL;
1421 if( asprintf( &psz_new, "%d", rand() ) < 0 )
1423 psz_session = psz_new;
1425 p_rtsp = RtspClientNew( p_media, psz_new );
1428 answer->i_status = 454;
1430 answer->p_body = NULL;
1436 p_rtsp = RtspClientGet( p_media, psz_session );
1439 answer->i_status = 454;
1441 answer->p_body = NULL;
1446 p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
1449 answer->i_status = 500;
1451 answer->p_body = NULL;
1454 p_rtsp_es->i_port = i_port;
1455 p_rtsp_es->psz_ip = strdup( ip );
1456 p_rtsp_es->p_media_es = p_es;
1457 TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );
1459 answer->i_status = 200;
1461 answer->p_body = NULL;
1463 if( p_media->b_raw )
1465 if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
1467 httpd_MsgAdd( answer, "Transport",
1468 "MP2T/H2221/UDP;client_port=%d-%d",
1469 p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1471 else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1473 httpd_MsgAdd( answer, "Transport",
1474 "RAW/RAW/UDP;client_port=%d-%d",
1475 p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1480 httpd_MsgAdd( answer, "Transport",
1481 "RTP/AVP/UDP;client_port=%d-%d",
1482 p_rtsp_es->i_port, p_rtsp_es->i_port + 1 );
1485 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1487 answer->i_status = 461;
1489 answer->p_body = NULL;
1492 /* Intentional fall-through on x-playNow option in RTSP request */
1496 case HTTPD_MSG_PLAY:
1497 /* This is kind of a kludge. Should we only support Aggregate
1499 psz_session = httpd_MsgGet( query, "Session" );
1500 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1502 p_rtsp = RtspClientGet( p_media, psz_session );
1504 psz_position = httpd_MsgGet( query, "Range" );
1505 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
1508 double f_pos = ParseNPT (psz_position + 4);
1509 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1510 f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
1511 CommandPush( p_vod, RTSP_CMD_TYPE_SEEK, p_media,
1512 psz_session, f_pos, NULL );
1517 answer->i_status = 200;
1519 answer->p_body = NULL;
1523 case HTTPD_MSG_TEARDOWN:
1524 answer->i_status = 200;
1526 answer->p_body = NULL;
1528 psz_session = httpd_MsgGet( query, "Session" );
1529 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1531 p_rtsp = RtspClientGet( p_media, psz_session );
1532 if( !p_rtsp ) break;
1534 for( i = 0; i < p_rtsp->i_es; i++ )
1536 if( p_rtsp->es[i]->p_media_es == p_es )
1538 free( p_rtsp->es[i]->psz_ip );
1539 TAB_REMOVE( p_rtsp->i_es, p_rtsp->es, p_rtsp->es[i] );
1546 CommandPush( p_vod, RTSP_CMD_TYPE_STOP, p_media, psz_session,
1548 RtspClientDel( p_media, p_rtsp );
1552 case HTTPD_MSG_PAUSE:
1553 /* This is kind of a kludge. Should we only support Aggregate
1555 psz_session = httpd_MsgGet( query, "Session" );
1556 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1558 p_rtsp = RtspClientGet( p_media, psz_session );
1559 if( !p_rtsp ) break;
1561 CommandPush( p_vod, RTSP_CMD_TYPE_PAUSE, p_media, psz_session,
1563 p_rtsp->b_paused = true;
1565 answer->i_status = 200;
1567 answer->p_body = NULL;
1571 return VLC_EGENERIC;
1575 httpd_MsgAdd( answer, "Server", "VLC Server" );
1576 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1577 psz_cseq = httpd_MsgGet( query, "Cseq" );
1579 i_cseq = atoi( psz_cseq );
1582 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1583 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1586 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1591 /*****************************************************************************
1593 * FIXME: need to be moved to a common place ?
1594 *****************************************************************************/
1595 static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
1598 char *p, *psz_sdp, ip[NI_MAXNUMERICHOST], ipv;
1599 const char *psz_control;
1601 if( httpd_ServerIP( cl, ip ) == NULL )
1604 p = strchr( ip, '%' );
1606 *p = '\0'; /* remove scope if present */
1608 ipv = ( strchr( ip, ':' ) != NULL ) ? '6' : '4';
1610 /* Calculate size */
1611 i_size = sizeof( "v=0\r\n" ) +
1612 sizeof( "o=- * * IN IP4 \r\n" ) + 10 + NI_MAXNUMERICHOST +
1613 sizeof( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
1614 sizeof( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
1615 sizeof( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
1616 sizeof( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
1617 sizeof( "c=IN IP4 0.0.0.0\r\n" ) + 20 + 10 +
1618 sizeof( "t=0 0\r\n" ) + /* FIXME */
1619 sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
1620 sizeof( "a=range:npt=0-1000000000.000\r\n" );
1622 psz_control = (ipv == '6') ? p_media->psz_rtsp_control_v6
1623 : p_media->psz_rtsp_control_v4;
1624 for( i = 0; i < p_media->i_es; i++ )
1626 media_es_t *p_es = p_media->es[i];
1628 i_size += sizeof( "m=**d*o * RTP/AVP *\r\n" ) + 19;
1629 if( p_es->psz_rtpmap )
1631 i_size += sizeof( "a=rtpmap:* *\r\n" ) +
1632 strlen( p_es->psz_rtpmap ) + 9;
1634 if( p_es->psz_fmtp )
1636 i_size += sizeof( "a=fmtp:* *\r\n" ) +
1637 strlen( p_es->psz_fmtp ) + 9;
1640 i_size += (strlen( psz_control ) + strlen( ip ) + 9) * p_media->i_es;
1642 p = psz_sdp = malloc( i_size );
1643 p += sprintf( p, "v=0\r\n" );
1644 p += sprintf( p, "o=- %"PRId64" %d IN IP%c %s\r\n",
1645 p_media->i_sdp_id, p_media->i_sdp_version, ipv, ip );
1646 if( *p_media->psz_session_name )
1647 p += sprintf( p, "s=%s\r\n", p_media->psz_session_name );
1648 if( *p_media->psz_session_description )
1649 p += sprintf( p, "i=%s\r\n", p_media->psz_session_description );
1650 if( *p_media->psz_session_url )
1651 p += sprintf( p, "u=%s\r\n", p_media->psz_session_url );
1652 if( *p_media->psz_session_email )
1653 p += sprintf( p, "e=%s\r\n", p_media->psz_session_email );
1655 p += sprintf( p, "c=IN IP%c %s\r\n", ipv, ipv == '6' ? "::" : "0.0.0.0" );
1656 p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
1657 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
1659 if( p_media->i_length > 0 )
1661 lldiv_t d = lldiv( p_media->i_length / 1000, 1000 );
1662 p += sprintf( p, "a=range:npt=0-%lld.%03u\r\n", d.quot,
1666 for( i = 0; i < p_media->i_es; i++ )
1668 media_es_t *p_es = p_media->es[i];
1670 if( p_es->fmt.i_cat == AUDIO_ES )
1672 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
1673 p_es->i_port, p_es->i_payload_type );
1675 else if( p_es->fmt.i_cat == VIDEO_ES )
1677 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
1678 p_es->i_port, p_es->i_payload_type );
1685 if( p_es->psz_rtpmap )
1687 p += sprintf( p, "a=rtpmap:%d %s\r\n", p_es->i_payload_type,
1690 if( p_es->psz_fmtp )
1692 p += sprintf( p, "a=fmtp:%d %s\r\n", p_es->i_payload_type,
1696 p += sprintf( p, psz_control, ip, i );