1 /*****************************************************************************
2 * rtsp.c: rtsp VoD server module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc/input.h>
36 #include "vlc_httpd.h"
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Open ( vlc_object_t * );
44 static void Close( vlc_object_t * );
46 #define HOST_TEXT N_( "Host address" )
47 #define HOST_LONGTEXT N_( \
48 "You can set the address, port and path the rtsp interface will bind to." \
49 ".\n Syntax is address:port/path. Default is to bind to localhost address"\
50 "on port 554, with no path. Use 0.0.0.0 to bind to all addresses." )
52 set_shortname( _("RTSP VoD" ) );
53 set_description( _("RTSP VoD server") );
54 set_category( CAT_SOUT );
55 set_subcategory( SUBCAT_SOUT_VOD );
56 set_capability( "vod server", 1 );
57 set_callbacks( Open, Close );
58 add_shortcut( "rtsp" );
59 add_string ( "rtsp-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
62 /*****************************************************************************
64 *****************************************************************************/
66 typedef struct media_es_t media_es_t;
70 media_es_t *p_media_es;
79 int64_t i_last; /* for timeout */
81 vlc_bool_t b_playing; /* is it in "play" state */
82 vlc_bool_t b_paused; /* is it in "pause" state */
85 rtsp_client_es_t **es;
95 httpd_url_t *p_rtsp_url;
101 uint8_t i_payload_type;
113 httpd_url_t *p_rtsp_url;
114 char *psz_rtsp_control;
117 char *psz_destination;
127 vlc_bool_t b_multicast;
138 rtsp_client_t **rtsp;
141 char *psz_session_name;
142 char *psz_session_description;
143 char *psz_session_url;
144 char *psz_session_email;
151 httpd_host_t *p_rtsp_host;
161 static vod_media_t *MediaNew( vod_t *, char *, input_item_t * );
162 static void MediaDel( vod_t *, vod_media_t * );
163 static int MediaAddES( vod_t *, vod_media_t *, es_format_t * );
164 static void MediaDelES( vod_t *, vod_media_t *, es_format_t * );
166 static rtsp_client_t *RtspClientNew( vod_media_t *, char * );
167 static rtsp_client_t *RtspClientGet( vod_media_t *, char * );
168 static void RtspClientDel( vod_media_t *, rtsp_client_t * );
170 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
171 httpd_message_t *, httpd_message_t * );
172 static int RtspCallbackES( httpd_callback_sys_t *, httpd_client_t *,
173 httpd_message_t *, httpd_message_t * );
175 static char *SDPGenerate( vod_media_t *, char * );
177 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
179 static const char hex[16] = "0123456789abcdef";
182 for( i = 0; i < i_data; i++ )
184 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
185 s[2*i+1] = hex[(p_data[i] )&0xf];
190 /*****************************************************************************
191 * Open: Starts the RTSP server module
192 *****************************************************************************/
193 static int Open( vlc_object_t *p_this )
195 vod_t *p_vod = (vod_t *)p_this;
196 vod_sys_t *p_sys = 0;
200 psz_url = config_GetPsz( p_vod, "rtsp-host" );
201 vlc_UrlParse( &url, psz_url, 0 );
202 if( psz_url ) free( psz_url );
204 if( !url.psz_host || !*url.psz_host )
206 if( url.psz_host ) free( url.psz_host );
207 url.psz_host = strdup( "localhost" );
209 if( url.i_port <= 0 ) url.i_port = 554;
211 p_vod->p_sys = p_sys = malloc( sizeof( vod_sys_t ) );
212 if( !p_sys ) goto error;
213 p_sys->p_rtsp_host = 0;
216 httpd_HostNew( VLC_OBJECT(p_vod), url.psz_host, url.i_port );
217 if( !p_sys->p_rtsp_host )
219 msg_Err( p_vod, "cannot create http server (%s:%i)",
220 url.psz_host, url.i_port );
224 p_sys->psz_host = strdup( url.psz_host );
225 p_sys->psz_path = strdup( url.psz_path ? url.psz_path : "/" );
226 p_sys->i_port = url.i_port;
228 vlc_UrlClean( &url );
232 p_vod->pf_media_new = MediaNew;
233 p_vod->pf_media_del = MediaDel;
234 p_vod->pf_media_add_es = MediaAddES;
235 p_vod->pf_media_del_es = MediaDelES;
241 if( p_sys && p_sys->p_rtsp_host ) httpd_HostDelete( p_sys->p_rtsp_host );
242 if( p_sys ) free( p_sys );
243 vlc_UrlClean( &url );
247 /*****************************************************************************
249 *****************************************************************************/
250 static void Close( vlc_object_t * p_this )
252 vod_t *p_vod = (vod_t *)p_this;
253 vod_sys_t *p_sys = p_vod->p_sys;
255 httpd_HostDelete( p_sys->p_rtsp_host );
257 /* TODO delete medias */
262 /*****************************************************************************
264 *****************************************************************************/
265 static vod_media_t *MediaNew( vod_t *p_vod, char *psz_name,
266 input_item_t *p_item )
268 vod_sys_t *p_sys = p_vod->p_sys;
269 vod_media_t *p_media = malloc( sizeof(vod_media_t) );
272 memset( p_media, 0, sizeof(vod_media_t) );
274 p_media->psz_mux = 0;
277 asprintf( &p_media->psz_rtsp_path, "%s%s", p_sys->psz_path, psz_name );
278 p_media->p_rtsp_url =
279 httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, 0, 0 );
281 if( !p_media->p_rtsp_url )
283 msg_Err( p_vod, "cannot create http url (%s)", p_media->psz_rtsp_path);
284 free( p_media->psz_rtsp_path );
289 msg_Dbg( p_vod, "created rtsp url: %s", p_media->psz_rtsp_path );
291 asprintf( &p_media->psz_rtsp_control, "rtsp://%s:%d%s",
292 p_sys->psz_host, p_sys->i_port, p_media->psz_rtsp_path );
294 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_DESCRIBE,
295 RtspCallback, (void*)p_media );
296 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PLAY,
297 RtspCallback, (void*)p_media );
298 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PAUSE,
299 RtspCallback, (void*)p_media );
300 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_TEARDOWN,
301 RtspCallback, (void*)p_media );
303 p_media->p_vod = p_vod;
305 TAB_APPEND( p_sys->i_media, p_sys->media, p_media );
307 vlc_mutex_init( p_vod, &p_media->lock );
308 p_media->psz_session_name = strdup("");
309 p_media->psz_session_description = strdup("");
310 p_media->psz_session_url = strdup("");
311 p_media->psz_session_email = strdup("");
313 p_media->i_port_audio = 1234;
314 p_media->i_port_video = 1236;
315 p_media->i_port = 1238;
316 p_media->i_payload_type = 96;
318 p_media->i_sdp_id = mdate();
319 p_media->i_sdp_version = 1;
320 p_media->i_length = p_item->i_duration;
322 vlc_mutex_lock( &p_item->lock );
323 msg_Dbg( p_vod, "media has %i declared ES", p_item->i_es );
324 for( i = 0; i < p_item->i_es; i++ )
326 MediaAddES( p_vod, p_media, p_item->es[i] );
328 vlc_mutex_unlock( &p_item->lock );
333 static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
335 vod_sys_t *p_sys = p_vod->p_sys;
337 msg_Dbg( p_vod, "deleting media: %s", p_media->psz_rtsp_path );
339 while( p_media->i_rtsp > 0 ) RtspClientDel( p_media, p_media->rtsp[0] );
340 httpd_UrlDelete( p_media->p_rtsp_url );
341 if( p_media->psz_rtsp_path ) free( p_media->psz_rtsp_path );
342 if( p_media->psz_rtsp_control ) free( p_media->psz_rtsp_control );
344 TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
346 while( p_media->i_es ) MediaDelES( p_vod, p_media, &p_media->es[0]->fmt );
348 vlc_mutex_destroy( &p_media->lock );
352 static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
354 media_es_t *p_es = malloc( sizeof(media_es_t) );
357 memset( p_es, 0, sizeof(media_es_t) );
358 p_media->psz_mux = NULL;
360 /* TODO: update SDP, etc... */
361 asprintf( &psz_urlc, "%s/trackid=%d",
362 p_media->psz_rtsp_path, p_media->i_es );
363 msg_Dbg( p_vod, " - ES %4.4s (%s)", (char *)&p_fmt->i_codec, psz_urlc );
365 switch( p_fmt->i_codec )
367 case VLC_FOURCC( 's', '1', '6', 'b' ):
368 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
370 p_es->i_payload_type = 11;
372 else if( p_fmt->audio.i_channels == 2 && p_fmt->audio.i_rate == 44100 )
374 p_es->i_payload_type = 10;
378 p_es->i_payload_type = p_media->i_payload_type++;
381 p_es->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
382 sprintf( p_es->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
383 p_fmt->audio.i_channels );
385 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
386 p_es->i_payload_type = p_media->i_payload_type++;
387 p_es->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
388 sprintf( p_es->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
389 p_fmt->audio.i_channels );
391 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
392 p_es->i_payload_type = 14;
393 p_es->psz_rtpmap = strdup( "MPA/90000" );
395 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
396 p_es->i_payload_type = 32;
397 p_es->psz_rtpmap = strdup( "MPV/90000" );
399 case VLC_FOURCC( 'a', '5', '2', ' ' ):
400 p_es->i_payload_type = p_media->i_payload_type++;
401 p_es->psz_rtpmap = strdup( "ac3/90000" );
403 case VLC_FOURCC( 'H', '2', '6', '3' ):
404 p_es->i_payload_type = p_media->i_payload_type++;
405 p_es->psz_rtpmap = strdup( "H263-1998/90000" );
407 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
408 p_es->i_payload_type = p_media->i_payload_type++;
409 p_es->psz_rtpmap = strdup( "MP4V-ES/90000" );
410 if( p_fmt->i_extra > 0 )
412 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
413 p_es->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
414 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
415 sprintf( p_es->psz_fmtp,
416 "profile-level-id=3; config=%s;", p_hexa );
420 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
421 p_es->i_payload_type = p_media->i_payload_type++;
422 p_es->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
423 sprintf( p_es->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
424 if( p_fmt->i_extra > 0 )
426 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
427 p_es->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
428 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
429 sprintf( p_es->psz_fmtp,
430 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
431 "config=%s; SizeLength=13;IndexLength=3; "
432 "IndexDeltaLength=3; Profile=1;", p_hexa );
436 case VLC_FOURCC( 'm', 'p', '2', 't' ):
437 p_media->psz_mux = "ts";
438 p_es->i_payload_type = 33;
439 p_es->psz_rtpmap = strdup( "MP2T/90000" );
441 case VLC_FOURCC( 'm', 'p', '2', 'p' ):
442 p_media->psz_mux = "ps";
443 p_es->i_payload_type = p_media->i_payload_type++;
444 p_es->psz_rtpmap = strdup( "MP2P/90000" );
448 msg_Err( p_vod, "cannot add this stream (unsupported "
449 "codec: %4.4s)", (char*)&p_fmt->i_codec );
455 httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, 0, 0 );
457 if( !p_es->p_rtsp_url )
459 msg_Err( p_vod, "cannot create http url (%s)", psz_urlc );
466 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
467 RtspCallbackES, (void*)p_es );
468 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_TEARDOWN,
469 RtspCallbackES, (void*)p_es );
470 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PLAY,
471 RtspCallbackES, (void*)p_es );
472 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PAUSE,
473 RtspCallbackES, (void*)p_es );
475 es_format_Copy( &p_es->fmt, p_fmt );
477 p_es->p_media = p_media;
480 /* Choose the port */
481 if( p_fmt->i_cat == AUDIO_ES && p_media->i_port_audio > 0 )
483 p_es->i_port = p_media->i_port_audio;
484 p_media->i_port_audio = 0;
486 else if( p_fmt->i_cat == VIDEO_ES && p_media->i_port_video > 0 )
488 p_es->i_port = p_media->i_port_video;
489 p_media->i_port_video = 0;
491 while( !p_es->i_port )
493 if( p_media->i_port != p_media->i_port_audio &&
494 p_media->i_port != p_media->i_port_video )
496 p_es->i_port = p_media->i_port;
497 p_media->i_port += 2;
500 p_media->i_port += 2;
507 vlc_mutex_lock( &p_media->lock );
508 TAB_APPEND( p_media->i_es, p_media->es, p_es );
509 vlc_mutex_unlock( &p_media->lock );
511 p_media->i_sdp_version++;
516 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
518 media_es_t *p_es = 0;
522 for( i = 0; i < p_media->i_es; i++ )
524 if( p_media->es[i]->fmt.i_cat == p_fmt->i_cat &&
525 p_media->es[i]->fmt.i_codec == p_fmt->i_codec &&
526 p_media->es[i]->fmt.i_id == p_fmt->i_id )
528 p_es = p_media->es[i];
533 msg_Dbg( p_vod, " - Removing ES %4.4s", (char *)&p_fmt->i_codec );
535 vlc_mutex_lock( &p_media->lock );
536 TAB_REMOVE( p_media->i_es, p_media->es, p_es );
537 vlc_mutex_unlock( &p_media->lock );
539 if( p_es->psz_rtpmap ) free( p_es->psz_rtpmap );
540 if( p_es->psz_fmtp ) free( p_es->psz_fmtp );
541 p_media->i_sdp_version++;
543 if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
544 es_format_Clean( &p_es->fmt );
547 /****************************************************************************
548 * RTSP server implementation
549 ****************************************************************************/
550 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
552 rtsp_client_t *p_rtsp = malloc( sizeof(rtsp_client_t) );
553 memset( p_rtsp, 0, sizeof(rtsp_client_t) );
556 p_rtsp->psz_session = psz_session;
557 TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
559 msg_Dbg( p_media->p_vod, "new session: %s", psz_session );
564 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, char *psz_session )
568 for( i = 0; psz_session && i < p_media->i_rtsp; i++ )
570 if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
572 return p_media->rtsp[i];
579 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
581 msg_Dbg( p_media->p_vod, "closing session: %s", p_rtsp->psz_session );
583 while( p_rtsp->i_es-- )
585 if( p_rtsp->es[p_rtsp->i_es]->psz_ip )
586 free( p_rtsp->es[p_rtsp->i_es]->psz_ip );
587 free( p_rtsp->es[p_rtsp->i_es] );
588 if( !p_rtsp->i_es ) free( p_rtsp->es );
591 TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, p_rtsp );
593 free( p_rtsp->psz_session );
597 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
598 httpd_message_t *answer, httpd_message_t *query )
600 vod_media_t *p_media = (vod_media_t*)p_args;
601 vod_t *p_vod = p_media->p_vod;
602 char *psz_destination = p_media->psz_destination;
603 char *psz_session = NULL;
604 rtsp_client_t *p_rtsp;
606 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
608 fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
610 answer->i_proto = HTTPD_PROTO_RTSP;
611 answer->i_version = query->i_version;
612 answer->i_type = HTTPD_MSG_ANSWER;
614 switch( query->i_type )
616 case HTTPD_MSG_DESCRIBE:
619 SDPGenerate( p_media, psz_destination ?
620 psz_destination : "0.0.0.0" );
622 answer->i_status = 200;
623 answer->psz_status = strdup( "OK" );
624 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
626 answer->p_body = psz_sdp;
627 answer->i_body = strlen( psz_sdp );
633 char *psz_output, *ip;
634 int i, i_port_audio = 0, i_port_video = 0;
636 /* for now only multicast so easy */
637 answer->i_status = 200;
638 answer->psz_status = strdup( "OK" );
640 answer->p_body = NULL;
642 psz_session = httpd_MsgGet( query, "Session" );
643 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
645 p_rtsp = RtspClientGet( p_media, psz_session );
648 if( p_rtsp->b_playing && p_rtsp->b_paused )
650 vod_MediaControl( p_vod, p_media, psz_session,
652 p_rtsp->b_paused = VLC_FALSE;
655 else if( p_rtsp->b_playing ) break;
657 if( !(ip = httpd_ClientIP( cl )) ) break;
659 p_rtsp->b_playing = VLC_TRUE;
661 /* FIXME for != 1 video and 1 audio */
662 for( i = 0; i < p_rtsp->i_es; i++ )
664 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == AUDIO_ES )
665 i_port_audio = p_rtsp->es[i]->i_port;
666 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == VIDEO_ES )
667 i_port_video = p_rtsp->es[i]->i_port;
670 if( p_media->psz_mux )
672 asprintf( &psz_output, "rtp{dst=%s,port=%i,mux=%s}",
673 ip, i_port_video, p_media->psz_mux );
677 asprintf( &psz_output, "rtp{dst=%s,port-video=%i,"
678 "port-audio=%i}", ip, i_port_video, i_port_audio );
681 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PLAY,
688 case HTTPD_MSG_PAUSE:
689 psz_session = httpd_MsgGet( query, "Session" );
690 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
692 p_rtsp = RtspClientGet( p_media, psz_session );
695 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
696 p_rtsp->b_paused = VLC_TRUE;
698 answer->i_status = 200;
699 answer->psz_status = strdup( "OK" );
701 answer->p_body = NULL;
704 case HTTPD_MSG_TEARDOWN:
705 /* for now only multicast so easy again */
706 answer->i_status = 200;
707 answer->psz_status = strdup( "OK" );
709 answer->p_body = NULL;
711 psz_session = httpd_MsgGet( query, "Session" );
712 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
714 p_rtsp = RtspClientGet( p_media, psz_session );
717 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_STOP );
718 RtspClientDel( p_media, p_rtsp );
725 httpd_MsgAdd( answer, "Server", "VLC Server" );
726 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
727 httpd_MsgAdd( answer, "Cseq", "%d",
728 atoi( httpd_MsgGet( query, "Cseq" ) ) );
729 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
733 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
739 static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
740 httpd_message_t *answer, httpd_message_t *query )
742 media_es_t *p_es = (media_es_t*)p_args;
743 vod_media_t *p_media = p_es->p_media;
744 vod_t *p_vod = p_media->p_vod;
745 rtsp_client_t *p_rtsp = NULL;
746 char *psz_session = NULL;
747 char *psz_transport = NULL;
748 char *psz_position = NULL;
751 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
753 fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
755 answer->i_proto = HTTPD_PROTO_RTSP;
756 answer->i_version = query->i_version;
757 answer->i_type = HTTPD_MSG_ANSWER;
759 switch( query->i_type )
761 case HTTPD_MSG_SETUP:
762 psz_transport = httpd_MsgGet( query, "Transport" );
763 fprintf( stderr, "HTTPD_MSG_SETUP: transport=%s\n", psz_transport );
765 if( strstr( psz_transport, "multicast" ) && p_media->psz_destination )
767 fprintf( stderr, "HTTPD_MSG_SETUP: multicast\n" );
768 answer->i_status = 200;
769 answer->psz_status = strdup( "OK" );
771 answer->p_body = NULL;
773 psz_session = httpd_MsgGet( query, "Session" );
774 if( !psz_session || !*psz_session )
776 asprintf( &psz_session, "%d", rand() );
779 httpd_MsgAdd( answer, "Transport",
780 "RTP/AVP/UDP;destination=%s;port=%d-%d;ttl=%d",
781 p_media->psz_destination, p_media->i_port,
782 p_media->i_port+1, p_media->i_ttl );
784 else if( strstr( psz_transport, "unicast" ) &&
785 strstr( psz_transport, "client_port=" ) )
787 rtsp_client_t *p_rtsp;
788 rtsp_client_es_t *p_rtsp_es;
789 char *ip = httpd_ClientIP( cl );
790 int i_port = atoi( strstr( psz_transport, "client_port=" ) +
791 strlen("client_port=") );
795 answer->i_status = 400;
796 answer->psz_status = strdup( "Internal server error" );
798 answer->p_body = NULL;
802 fprintf( stderr, "HTTPD_MSG_SETUP: unicast ip=%s port=%d\n",
805 psz_session = httpd_MsgGet( query, "Session" );
806 if( !psz_session || !*psz_session )
808 asprintf( &psz_session, "%d", rand() );
809 p_rtsp = RtspClientNew( p_media, psz_session );
813 p_rtsp = RtspClientGet( p_media, psz_session );
816 /* FIXME right error code */
817 answer->i_status = 400;
818 answer->psz_status = strdup( "Unknown session id" );
820 answer->p_body = NULL;
826 p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
827 p_rtsp_es->i_port = i_port;
828 p_rtsp_es->psz_ip = strdup( ip );
829 p_rtsp_es->p_media_es = p_es;
830 TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );
832 answer->i_status = 200;
833 answer->psz_status = strdup( "OK" );
835 answer->p_body = NULL;
837 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
838 i_port, i_port + 1 );
840 else /* TODO strstr( psz_transport, "interleaved" ) ) */
842 answer->i_status = 400;
843 answer->psz_status = strdup( "Bad Request" );
845 answer->p_body = NULL;
849 case HTTPD_MSG_TEARDOWN:
850 answer->i_status = 200;
851 answer->psz_status = strdup( "OK" );
853 answer->p_body = NULL;
855 psz_session = httpd_MsgGet( query, "Session" );
856 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
858 p_rtsp = RtspClientGet( p_media, psz_session );
861 for( i = 0; i < p_rtsp->i_es; i++ )
863 if( p_rtsp->es[i]->p_media_es == p_es )
865 if( p_rtsp->es[i]->psz_ip ) free( p_rtsp->es[i]->psz_ip );
866 TAB_REMOVE( p_rtsp->i_es, p_rtsp->es, p_rtsp->es[i] );
873 vod_MediaControl( p_vod, p_media, psz_session,
875 RtspClientDel( p_media, p_rtsp );
880 /* This is kind of a kludge. Should we only support Aggregate
882 psz_session = httpd_MsgGet( query, "Session" );
883 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
885 p_rtsp = RtspClientGet( p_media, psz_session );
887 psz_position = httpd_MsgGet( query, "Range" );
888 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
893 msg_Dbg( p_vod, "seeking request: %s", psz_position );
896 if( sscanf( psz_position, "%f", &f_pos ) == 1 )
898 f_pos /= ((float)(p_media->i_length/1000))/1000 / 100;
899 vod_MediaControl( p_vod, p_media, psz_session,
900 VOD_MEDIA_SEEK, (double)f_pos );
904 answer->i_status = 200;
905 answer->psz_status = strdup( "OK" );
907 answer->p_body = NULL;
910 case HTTPD_MSG_PAUSE:
911 /* This is kind of a kludge. Should we only support Aggregate
913 psz_session = httpd_MsgGet( query, "Session" );
914 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
916 p_rtsp = RtspClientGet( p_media, psz_session );
919 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
920 p_rtsp->b_paused = VLC_TRUE;
922 answer->i_status = 200;
923 answer->psz_status = strdup( "OK" );
925 answer->p_body = NULL;
933 httpd_MsgAdd( answer, "Server", "VLC Server" );
934 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
935 httpd_MsgAdd( answer, "Cseq", "%d",
936 atoi( httpd_MsgGet( query, "Cseq" ) ) );
937 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
941 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
947 /*****************************************************************************
949 * FIXME: need to be moved to a common place ?
950 *****************************************************************************/
951 static char *SDPGenerate( vod_media_t *p_media, char *psz_destination )
957 i_size = strlen( "v=0\r\n" ) +
958 strlen( "o=- * * IN IP4 127.0.0.1\r\n" ) + 10 + 10 +
959 strlen( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
960 strlen( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
961 strlen( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
962 strlen( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
963 strlen( "t=0 0\r\n" ) + /* FIXME */
964 strlen( "a=tool:"PACKAGE_STRING"\r\n" ) +
965 strlen( "c=IN IP4 */*\r\n" ) + 20 + 10 +
966 strlen( psz_destination ? psz_destination : "0.0.0.0" ) +
967 strlen( "a=range:npt=0-1000000000.000\r\n" );
969 for( i = 0; i < p_media->i_es; i++ )
971 media_es_t *p_es = p_media->es[i];
973 i_size += strlen( "m=**d*o * RTP/AVP *\r\n" ) + 10 + 10;
974 if( p_es->psz_rtpmap )
976 i_size += strlen( "a=rtpmap:* *\r\n" ) +
977 strlen( p_es->psz_rtpmap ) + 10;
981 i_size += strlen( "a=fmtp:* *\r\n" ) +
982 strlen( p_es->psz_fmtp ) + 10;
985 i_size += strlen( "a=control:*/trackid=*\r\n" ) +
986 strlen( p_media->psz_rtsp_control ) + 10;
989 p = psz_sdp = malloc( i_size );
990 p += sprintf( p, "v=0\r\n" );
991 p += sprintf( p, "o=- "I64Fd" %d IN IP4 127.0.0.1\r\n",
992 p_media->i_sdp_id, p_media->i_sdp_version );
993 if( *p_media->psz_session_name )
994 p += sprintf( p, "s=%s\r\n", p_media->psz_session_name );
995 if( *p_media->psz_session_description )
996 p += sprintf( p, "i=%s\r\n", p_media->psz_session_description );
997 if( *p_media->psz_session_url )
998 p += sprintf( p, "u=%s\r\n", p_media->psz_session_url );
999 if( *p_media->psz_session_email )
1000 p += sprintf( p, "e=%s\r\n", p_media->psz_session_email );
1002 p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
1003 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
1005 p += sprintf( p, "c=IN IP4 %s/%d\r\n", psz_destination ?
1006 psz_destination : "0.0.0.0", p_media->i_ttl );
1008 if( p_media->i_length > 0 )
1009 p += sprintf( p, "a=range:npt=0-%.3f\r\n",
1010 ((float)(p_media->i_length/1000))/1000 );
1012 for( i = 0; i < p_media->i_es; i++ )
1014 media_es_t *p_es = p_media->es[i];
1016 if( p_es->fmt.i_cat == AUDIO_ES )
1018 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
1019 p_es->i_port, p_es->i_payload_type );
1021 else if( p_es->fmt.i_cat == VIDEO_ES )
1023 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
1024 p_es->i_port, p_es->i_payload_type );
1031 if( p_es->psz_rtpmap )
1033 p += sprintf( p, "a=rtpmap:%d %s\r\n", p_es->i_payload_type,
1036 if( p_es->psz_fmtp )
1038 p += sprintf( p, "a=fmtp:%d %s\r\n", p_es->i_payload_type,
1042 p += sprintf( p, "a=control:%s/trackid=%d\r\n",
1043 p_media->psz_rtsp_control, i );
1046 fprintf( stderr, psz_sdp );