1 /*****************************************************************************
2 * rtsp.c: rtsp VoD server module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 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., 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 "\nSyntax is address:port/path. Default is to bind to any address "\
50 "on port 554, with no path." )
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;
126 vlc_bool_t b_multicast;
137 rtsp_client_t **rtsp;
140 char *psz_session_name;
141 char *psz_session_description;
142 char *psz_session_url;
143 char *psz_session_email;
150 httpd_host_t *p_rtsp_host;
160 static vod_media_t *MediaNew( vod_t *, char *, input_item_t * );
161 static void MediaDel( vod_t *, vod_media_t * );
162 static int MediaAddES( vod_t *, vod_media_t *, es_format_t * );
163 static void MediaDelES( vod_t *, vod_media_t *, es_format_t * );
165 static rtsp_client_t *RtspClientNew( vod_media_t *, char * );
166 static rtsp_client_t *RtspClientGet( vod_media_t *, char * );
167 static void RtspClientDel( vod_media_t *, rtsp_client_t * );
169 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
170 httpd_message_t *, httpd_message_t * );
171 static int RtspCallbackES( httpd_callback_sys_t *, httpd_client_t *,
172 httpd_message_t *, httpd_message_t * );
174 static char *SDPGenerate( const vod_media_t *, httpd_client_t *cl );
176 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
178 static const char hex[16] = "0123456789abcdef";
181 for( i = 0; i < i_data; i++ )
183 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
184 s[2*i+1] = hex[(p_data[i] )&0xf];
189 /*****************************************************************************
190 * Open: Starts the RTSP server module
191 *****************************************************************************/
192 static int Open( vlc_object_t *p_this )
194 vod_t *p_vod = (vod_t *)p_this;
195 vod_sys_t *p_sys = 0;
199 psz_url = config_GetPsz( p_vod, "rtsp-host" );
200 vlc_UrlParse( &url, psz_url, 0 );
201 if( psz_url ) free( psz_url );
203 if( url.i_port <= 0 ) url.i_port = 554;
205 p_vod->p_sys = p_sys = malloc( sizeof( vod_sys_t ) );
206 if( !p_sys ) goto error;
207 p_sys->p_rtsp_host = 0;
210 httpd_HostNew( VLC_OBJECT(p_vod), url.psz_host, url.i_port );
211 if( !p_sys->p_rtsp_host )
213 msg_Err( p_vod, "cannot create http server (%s:%i)",
214 url.psz_host, url.i_port );
218 p_sys->psz_host = strdup( url.psz_host );
219 p_sys->psz_path = strdup( url.psz_path ? url.psz_path : "/" );
220 p_sys->i_port = url.i_port;
222 vlc_UrlClean( &url );
226 p_vod->pf_media_new = MediaNew;
227 p_vod->pf_media_del = MediaDel;
228 p_vod->pf_media_add_es = MediaAddES;
229 p_vod->pf_media_del_es = MediaDelES;
235 if( p_sys && p_sys->p_rtsp_host ) httpd_HostDelete( p_sys->p_rtsp_host );
236 if( p_sys ) free( p_sys );
237 vlc_UrlClean( &url );
241 /*****************************************************************************
243 *****************************************************************************/
244 static void Close( vlc_object_t * p_this )
246 vod_t *p_vod = (vod_t *)p_this;
247 vod_sys_t *p_sys = p_vod->p_sys;
249 httpd_HostDelete( p_sys->p_rtsp_host );
251 /* TODO delete medias */
253 free( p_sys->psz_host );
254 free( p_sys->psz_path );
258 /*****************************************************************************
260 *****************************************************************************/
261 static vod_media_t *MediaNew( vod_t *p_vod, char *psz_name,
262 input_item_t *p_item )
264 vod_sys_t *p_sys = p_vod->p_sys;
265 vod_media_t *p_media = malloc( sizeof(vod_media_t) );
268 memset( p_media, 0, sizeof(vod_media_t) );
270 p_media->psz_mux = 0;
273 asprintf( &p_media->psz_rtsp_path, "%s%s", p_sys->psz_path, psz_name );
274 p_media->p_rtsp_url =
275 httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, NULL,
278 if( !p_media->p_rtsp_url )
280 msg_Err( p_vod, "cannot create http url (%s)", p_media->psz_rtsp_path);
281 free( p_media->psz_rtsp_path );
286 msg_Dbg( p_vod, "created rtsp url: %s", p_media->psz_rtsp_path );
288 asprintf( &p_media->psz_rtsp_control, "rtsp://%s:%d%s",
289 p_sys->psz_host, p_sys->i_port, p_media->psz_rtsp_path );
291 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_DESCRIBE,
292 RtspCallback, (void*)p_media );
293 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PLAY,
294 RtspCallback, (void*)p_media );
295 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PAUSE,
296 RtspCallback, (void*)p_media );
297 httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_TEARDOWN,
298 RtspCallback, (void*)p_media );
300 p_media->p_vod = p_vod;
302 TAB_APPEND( p_sys->i_media, p_sys->media, p_media );
304 vlc_mutex_init( p_vod, &p_media->lock );
305 p_media->psz_session_name = strdup("");
306 p_media->psz_session_description = strdup("");
307 p_media->psz_session_url = strdup("");
308 p_media->psz_session_email = strdup("");
310 p_media->i_port_audio = 1234;
311 p_media->i_port_video = 1236;
312 p_media->i_port = 1238;
313 p_media->i_payload_type = 96;
315 p_media->i_sdp_id = mdate();
316 p_media->i_sdp_version = 1;
317 p_media->i_length = p_item->i_duration;
319 vlc_mutex_lock( &p_item->lock );
320 msg_Dbg( p_vod, "media has %i declared ES", p_item->i_es );
321 for( i = 0; i < p_item->i_es; i++ )
323 MediaAddES( p_vod, p_media, p_item->es[i] );
325 vlc_mutex_unlock( &p_item->lock );
330 static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
332 vod_sys_t *p_sys = p_vod->p_sys;
334 msg_Dbg( p_vod, "deleting media: %s", p_media->psz_rtsp_path );
336 while( p_media->i_rtsp > 0 ) RtspClientDel( p_media, p_media->rtsp[0] );
337 httpd_UrlDelete( p_media->p_rtsp_url );
338 if( p_media->psz_rtsp_path ) free( p_media->psz_rtsp_path );
339 if( p_media->psz_rtsp_control ) free( p_media->psz_rtsp_control );
341 TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
343 while( p_media->i_es ) MediaDelES( p_vod, p_media, &p_media->es[0]->fmt );
345 vlc_mutex_destroy( &p_media->lock );
346 free( p_media->psz_session_name );
347 free( p_media->psz_session_description );
348 free( p_media->psz_session_url );
349 free( p_media->psz_session_email );
353 static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
355 media_es_t *p_es = malloc( sizeof(media_es_t) );
358 memset( p_es, 0, sizeof(media_es_t) );
359 p_media->psz_mux = NULL;
361 /* TODO: update SDP, etc... */
362 asprintf( &psz_urlc, "%s/trackid=%d",
363 p_media->psz_rtsp_path, p_media->i_es );
364 msg_Dbg( p_vod, " - ES %4.4s (%s)", (char *)&p_fmt->i_codec, psz_urlc );
366 switch( p_fmt->i_codec )
368 case VLC_FOURCC( 's', '1', '6', 'b' ):
369 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
371 p_es->i_payload_type = 11;
373 else if( p_fmt->audio.i_channels == 2 && p_fmt->audio.i_rate == 44100 )
375 p_es->i_payload_type = 10;
379 p_es->i_payload_type = p_media->i_payload_type++;
382 p_es->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
383 sprintf( p_es->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
384 p_fmt->audio.i_channels );
386 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
387 p_es->i_payload_type = p_media->i_payload_type++;
388 p_es->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
389 sprintf( p_es->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
390 p_fmt->audio.i_channels );
392 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
393 p_es->i_payload_type = 14;
394 p_es->psz_rtpmap = strdup( "MPA/90000" );
396 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
397 p_es->i_payload_type = 32;
398 p_es->psz_rtpmap = strdup( "MPV/90000" );
400 case VLC_FOURCC( 'a', '5', '2', ' ' ):
401 p_es->i_payload_type = p_media->i_payload_type++;
402 p_es->psz_rtpmap = strdup( "ac3/90000" );
404 case VLC_FOURCC( 'H', '2', '6', '3' ):
405 p_es->i_payload_type = p_media->i_payload_type++;
406 p_es->psz_rtpmap = strdup( "H263-1998/90000" );
408 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
409 p_es->i_payload_type = p_media->i_payload_type++;
410 p_es->psz_rtpmap = strdup( "MP4V-ES/90000" );
411 if( p_fmt->i_extra > 0 )
413 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
414 p_es->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
415 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
416 sprintf( p_es->psz_fmtp,
417 "profile-level-id=3; config=%s;", p_hexa );
421 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
422 p_es->i_payload_type = p_media->i_payload_type++;
423 p_es->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
424 sprintf( p_es->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
425 if( p_fmt->i_extra > 0 )
427 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
428 p_es->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
429 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
430 sprintf( p_es->psz_fmtp,
431 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
432 "config=%s; SizeLength=13;IndexLength=3; "
433 "IndexDeltaLength=3; Profile=1;", p_hexa );
437 case VLC_FOURCC( 'm', 'p', '2', 't' ):
438 p_media->psz_mux = "ts";
439 p_es->i_payload_type = 33;
440 p_es->psz_rtpmap = strdup( "MP2T/90000" );
442 case VLC_FOURCC( 'm', 'p', '2', 'p' ):
443 p_media->psz_mux = "ps";
444 p_es->i_payload_type = p_media->i_payload_type++;
445 p_es->psz_rtpmap = strdup( "MP2P/90000" );
447 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
448 p_es->i_payload_type = p_media->i_payload_type++;
449 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
450 "AMR/8000/2" : "AMR/8000" );
451 p_es->psz_fmtp = strdup( "octet-align=1" );
453 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
454 p_es->i_payload_type = p_media->i_payload_type++;
455 p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
456 "AMR-WB/16000/2" : "AMR-WB/16000" );
457 p_es->psz_fmtp = strdup( "octet-align=1" );
461 msg_Err( p_vod, "cannot add this stream (unsupported "
462 "codec: %4.4s)", (char*)&p_fmt->i_codec );
468 httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, NULL, NULL,
471 if( !p_es->p_rtsp_url )
473 msg_Err( p_vod, "cannot create http url (%s)", psz_urlc );
480 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
481 RtspCallbackES, (void*)p_es );
482 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_TEARDOWN,
483 RtspCallbackES, (void*)p_es );
484 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PLAY,
485 RtspCallbackES, (void*)p_es );
486 httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PAUSE,
487 RtspCallbackES, (void*)p_es );
489 es_format_Copy( &p_es->fmt, p_fmt );
491 p_es->p_media = p_media;
494 /* Choose the port */
495 if( p_fmt->i_cat == AUDIO_ES && p_media->i_port_audio > 0 )
497 p_es->i_port = p_media->i_port_audio;
498 p_media->i_port_audio = 0;
500 else if( p_fmt->i_cat == VIDEO_ES && p_media->i_port_video > 0 )
502 p_es->i_port = p_media->i_port_video;
503 p_media->i_port_video = 0;
505 while( !p_es->i_port )
507 if( p_media->i_port != p_media->i_port_audio &&
508 p_media->i_port != p_media->i_port_video )
510 p_es->i_port = p_media->i_port;
511 p_media->i_port += 2;
514 p_media->i_port += 2;
521 vlc_mutex_lock( &p_media->lock );
522 TAB_APPEND( p_media->i_es, p_media->es, p_es );
523 vlc_mutex_unlock( &p_media->lock );
525 p_media->i_sdp_version++;
530 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
532 media_es_t *p_es = 0;
536 for( i = 0; i < p_media->i_es; i++ )
538 if( p_media->es[i]->fmt.i_cat == p_fmt->i_cat &&
539 p_media->es[i]->fmt.i_codec == p_fmt->i_codec &&
540 p_media->es[i]->fmt.i_id == p_fmt->i_id )
542 p_es = p_media->es[i];
547 msg_Dbg( p_vod, " - Removing ES %4.4s", (char *)&p_fmt->i_codec );
549 vlc_mutex_lock( &p_media->lock );
550 TAB_REMOVE( p_media->i_es, p_media->es, p_es );
551 vlc_mutex_unlock( &p_media->lock );
553 if( p_es->psz_rtpmap ) free( p_es->psz_rtpmap );
554 if( p_es->psz_fmtp ) free( p_es->psz_fmtp );
555 p_media->i_sdp_version++;
557 if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
558 es_format_Clean( &p_es->fmt );
561 /****************************************************************************
562 * RTSP server implementation
563 ****************************************************************************/
564 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
566 rtsp_client_t *p_rtsp = malloc( sizeof(rtsp_client_t) );
567 memset( p_rtsp, 0, sizeof(rtsp_client_t) );
570 p_rtsp->psz_session = psz_session;
571 TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
573 msg_Dbg( p_media->p_vod, "new session: %s", psz_session );
578 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, char *psz_session )
582 for( i = 0; psz_session && i < p_media->i_rtsp; i++ )
584 if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
586 return p_media->rtsp[i];
593 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
595 msg_Dbg( p_media->p_vod, "closing session: %s", p_rtsp->psz_session );
597 while( p_rtsp->i_es-- )
599 if( p_rtsp->es[p_rtsp->i_es]->psz_ip )
600 free( p_rtsp->es[p_rtsp->i_es]->psz_ip );
601 free( p_rtsp->es[p_rtsp->i_es] );
602 if( !p_rtsp->i_es ) free( p_rtsp->es );
605 TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, p_rtsp );
607 free( p_rtsp->psz_session );
611 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
612 httpd_message_t *answer, httpd_message_t *query )
614 vod_media_t *p_media = (vod_media_t*)p_args;
615 vod_t *p_vod = p_media->p_vod;
616 char *psz_session = NULL;
617 rtsp_client_t *p_rtsp;
619 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
621 fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
623 answer->i_proto = HTTPD_PROTO_RTSP;
624 answer->i_version = query->i_version;
625 answer->i_type = HTTPD_MSG_ANSWER;
627 switch( query->i_type )
629 case HTTPD_MSG_DESCRIBE:
632 SDPGenerate( p_media, cl );
634 if( psz_sdp != NULL )
636 answer->i_status = 200;
637 answer->psz_status = strdup( "OK" );
638 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
640 answer->p_body = (uint8_t *)psz_sdp;
641 answer->i_body = strlen( psz_sdp );
645 answer->i_status = 500;
646 answer->psz_status = strdup( "Internal server error" );
647 answer->p_body = NULL;
655 char *psz_output, ip[NI_MAXNUMERICHOST];
656 int i, i_port_audio = 0, i_port_video = 0;
658 /* for now only multicast so easy */
659 answer->i_status = 200;
660 answer->psz_status = strdup( "OK" );
662 answer->p_body = NULL;
664 psz_session = httpd_MsgGet( query, "Session" );
665 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
667 p_rtsp = RtspClientGet( p_media, psz_session );
670 if( p_rtsp->b_playing && p_rtsp->b_paused )
672 vod_MediaControl( p_vod, p_media, psz_session,
674 p_rtsp->b_paused = VLC_FALSE;
677 else if( p_rtsp->b_playing ) break;
679 if( httpd_ClientIP( cl, ip ) == NULL ) break;
681 p_rtsp->b_playing = VLC_TRUE;
683 /* FIXME for != 1 video and 1 audio */
684 for( i = 0; i < p_rtsp->i_es; i++ )
686 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == AUDIO_ES )
687 i_port_audio = p_rtsp->es[i]->i_port;
688 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == VIDEO_ES )
689 i_port_video = p_rtsp->es[i]->i_port;
692 if( p_media->psz_mux )
694 asprintf( &psz_output, "rtp{dst=%s,port=%i,mux=%s}",
695 ip, i_port_video, p_media->psz_mux );
699 asprintf( &psz_output, "rtp{dst=%s,port-video=%i,"
700 "port-audio=%i}", ip, i_port_video, i_port_audio );
703 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PLAY,
709 case HTTPD_MSG_PAUSE:
710 psz_session = httpd_MsgGet( query, "Session" );
711 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
713 p_rtsp = RtspClientGet( p_media, psz_session );
716 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
717 p_rtsp->b_paused = VLC_TRUE;
719 answer->i_status = 200;
720 answer->psz_status = strdup( "OK" );
722 answer->p_body = NULL;
725 case HTTPD_MSG_TEARDOWN:
726 /* for now only multicast so easy again */
727 answer->i_status = 200;
728 answer->psz_status = strdup( "OK" );
730 answer->p_body = NULL;
732 psz_session = httpd_MsgGet( query, "Session" );
733 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
735 p_rtsp = RtspClientGet( p_media, psz_session );
738 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_STOP );
739 RtspClientDel( p_media, p_rtsp );
746 httpd_MsgAdd( answer, "Server", "VLC Server" );
747 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
748 httpd_MsgAdd( answer, "Cseq", "%d",
749 atoi( httpd_MsgGet( query, "Cseq" ) ) );
750 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
754 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
760 static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
761 httpd_message_t *answer, httpd_message_t *query )
763 media_es_t *p_es = (media_es_t*)p_args;
764 vod_media_t *p_media = p_es->p_media;
765 vod_t *p_vod = p_media->p_vod;
766 rtsp_client_t *p_rtsp = NULL;
767 char *psz_session = NULL;
768 char *psz_transport = NULL;
769 char *psz_position = NULL;
772 if( answer == NULL || query == NULL ) return VLC_SUCCESS;
774 fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
776 answer->i_proto = HTTPD_PROTO_RTSP;
777 answer->i_version = query->i_version;
778 answer->i_type = HTTPD_MSG_ANSWER;
780 switch( query->i_type )
782 case HTTPD_MSG_SETUP:
783 psz_transport = httpd_MsgGet( query, "Transport" );
784 fprintf( stderr, "HTTPD_MSG_SETUP: transport=%s\n", psz_transport );
786 if( strstr( psz_transport, "unicast" ) &&
787 strstr( psz_transport, "client_port=" ) )
789 rtsp_client_t *p_rtsp;
790 rtsp_client_es_t *p_rtsp_es;
791 char ip[NI_MAXNUMERICHOST];
792 int i_port = atoi( strstr( psz_transport, "client_port=" ) +
793 strlen("client_port=") );
795 if( httpd_ClientIP( cl, ip ) == NULL )
797 answer->i_status = 500;
798 answer->psz_status = strdup( "Internal server error" );
800 answer->p_body = NULL;
804 fprintf( stderr, "HTTPD_MSG_SETUP: unicast ip=%s port=%d\n",
807 psz_session = httpd_MsgGet( query, "Session" );
808 if( !psz_session || !*psz_session )
810 asprintf( &psz_session, "%d", rand() );
811 p_rtsp = RtspClientNew( p_media, psz_session );
815 p_rtsp = RtspClientGet( p_media, psz_session );
818 /* FIXME right error code */
819 answer->i_status = 454;
820 answer->psz_status = strdup( "Unknown session id" );
822 answer->p_body = NULL;
828 p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
829 p_rtsp_es->i_port = i_port;
830 p_rtsp_es->psz_ip = strdup( ip );
831 p_rtsp_es->p_media_es = p_es;
832 TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );
834 answer->i_status = 200;
835 answer->psz_status = strdup( "OK" );
837 answer->p_body = NULL;
839 httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
840 i_port, i_port + 1 );
842 else /* TODO strstr( psz_transport, "interleaved" ) ) */
844 answer->i_status = 461;
845 answer->psz_status = strdup( "Unsupported Transport" );
847 answer->p_body = NULL;
851 case HTTPD_MSG_TEARDOWN:
852 answer->i_status = 200;
853 answer->psz_status = strdup( "OK" );
855 answer->p_body = NULL;
857 psz_session = httpd_MsgGet( query, "Session" );
858 msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
860 p_rtsp = RtspClientGet( p_media, psz_session );
863 for( i = 0; i < p_rtsp->i_es; i++ )
865 if( p_rtsp->es[i]->p_media_es == p_es )
867 if( p_rtsp->es[i]->psz_ip ) free( p_rtsp->es[i]->psz_ip );
868 TAB_REMOVE( p_rtsp->i_es, p_rtsp->es, p_rtsp->es[i] );
875 vod_MediaControl( p_vod, p_media, psz_session,
877 RtspClientDel( p_media, p_rtsp );
882 /* This is kind of a kludge. Should we only support Aggregate
884 psz_session = httpd_MsgGet( query, "Session" );
885 msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
887 p_rtsp = RtspClientGet( p_media, psz_session );
889 psz_position = httpd_MsgGet( query, "Range" );
890 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
895 msg_Dbg( p_vod, "seeking request: %s", psz_position );
898 if( sscanf( psz_position, "%f", &f_pos ) == 1 )
900 f_pos /= ((float)(p_media->i_length/1000))/1000 / 100;
901 vod_MediaControl( p_vod, p_media, psz_session,
902 VOD_MEDIA_SEEK, (double)f_pos );
906 answer->i_status = 200;
907 answer->psz_status = strdup( "OK" );
909 answer->p_body = NULL;
912 case HTTPD_MSG_PAUSE:
913 /* This is kind of a kludge. Should we only support Aggregate
915 psz_session = httpd_MsgGet( query, "Session" );
916 msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
918 p_rtsp = RtspClientGet( p_media, psz_session );
921 vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
922 p_rtsp->b_paused = VLC_TRUE;
924 answer->i_status = 200;
925 answer->psz_status = strdup( "OK" );
927 answer->p_body = NULL;
935 httpd_MsgAdd( answer, "Server", "VLC Server" );
936 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
937 httpd_MsgAdd( answer, "Cseq", "%d",
938 atoi( httpd_MsgGet( query, "Cseq" ) ) );
939 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
943 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
949 /*****************************************************************************
951 * FIXME: need to be moved to a common place ?
952 *****************************************************************************/
953 static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
956 char *p, *psz_sdp, ip[NI_MAXNUMERICHOST], ipv;
958 if( httpd_ServerIP( cl, ip ) == NULL )
961 p = strchr( ip, '%' );
963 *p = '\0'; /* remove scope if present */
965 ipv = ( strchr( ip, ':' ) != NULL ) ? '6' : '4';
968 i_size = sizeof( "v=0\r\n" ) +
969 sizeof( "o=- * * IN IP4 \r\n" ) + 10 + NI_MAXNUMERICHOST +
970 sizeof( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
971 sizeof( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
972 sizeof( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
973 sizeof( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
974 sizeof( "t=0 0\r\n" ) + /* FIXME */
975 sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
976 sizeof( "c=IN IP4 0.0.0.0\r\n" ) + 20 + 10 +
977 sizeof( "a=range:npt=0-1000000000.000\r\n" );
979 for( i = 0; i < p_media->i_es; i++ )
981 media_es_t *p_es = p_media->es[i];
983 i_size += sizeof( "m=**d*o * RTP/AVP *\r\n" ) + 19;
984 if( p_es->psz_rtpmap )
986 i_size += sizeof( "a=rtpmap:* *\r\n" ) +
987 strlen( p_es->psz_rtpmap ) + 9;
991 i_size += sizeof( "a=fmtp:* *\r\n" ) +
992 strlen( p_es->psz_fmtp ) + 9;
995 i_size += sizeof( "a=control:*/trackid=*\r\n" ) +
996 strlen( p_media->psz_rtsp_control ) + 9;
999 p = psz_sdp = malloc( i_size );
1000 p += sprintf( p, "v=0\r\n" );
1001 p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
1002 p_media->i_sdp_id, p_media->i_sdp_version, ipv, ip );
1003 if( *p_media->psz_session_name )
1004 p += sprintf( p, "s=%s\r\n", p_media->psz_session_name );
1005 if( *p_media->psz_session_description )
1006 p += sprintf( p, "i=%s\r\n", p_media->psz_session_description );
1007 if( *p_media->psz_session_url )
1008 p += sprintf( p, "u=%s\r\n", p_media->psz_session_url );
1009 if( *p_media->psz_session_email )
1010 p += sprintf( p, "e=%s\r\n", p_media->psz_session_email );
1012 p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
1013 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
1015 p += sprintf( p, "c=IN IP%c %s\r\n", ipv, ipv == '6' ? "::" : "0.0.0.0" );
1017 if( p_media->i_length > 0 )
1018 p += sprintf( p, "a=range:npt=0-%.3f\r\n",
1019 ((float)(p_media->i_length/1000))/1000 );
1021 for( i = 0; i < p_media->i_es; i++ )
1023 media_es_t *p_es = p_media->es[i];
1025 if( p_es->fmt.i_cat == AUDIO_ES )
1027 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
1028 p_es->i_port, p_es->i_payload_type );
1030 else if( p_es->fmt.i_cat == VIDEO_ES )
1032 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
1033 p_es->i_port, p_es->i_payload_type );
1040 if( p_es->psz_rtpmap )
1042 p += sprintf( p, "a=rtpmap:%d %s\r\n", p_es->i_payload_type,
1045 if( p_es->psz_fmtp )
1047 p += sprintf( p, "a=fmtp:%d %s\r\n", p_es->i_payload_type,
1051 p += sprintf( p, "a=control:%s/trackid=%d\r\n",
1052 p_media->psz_rtsp_control, i );